// create extra field, requested by unit if he accidentally leaves a valid sector public void CreateExtraField(WorldArea area, Tile tile, FlowFieldPath flowFieldPath, AddToPathJob pathJob) { if (tile != null && tile != flowFieldPath.destination) { List <int> neighbourSectors = new List <int>(); foreach (int neighbour in worldData.multiLevelSectorManager.GetNeighboursIndexes(area.sectorGrid[0][tile.sectorIndex], area)) { if (flowFieldPath.intergrationField.field.ContainsKey(new IntVector2(area.index, neighbour))) { neighbourSectors.Add(neighbour); } } if (neighbourSectors.Count > 0) { CreateFieldToAdd(tile.sectorIndex, neighbourSectors, flowFieldPath, pathJob, area); } else { // there are no valid neighboursectors to guide the flowfield // we must find a new connection to the path from here, and fill the flowfields accordingly //Debug.Log("there are no valid neighboursector"); worldData.pathfinder.AddToPath(area, tile, flowFieldPath); } } }
// extra fields are being created, create & add a field by expanding from neighbouring sectors public void CreateFieldToAdd(int emptySector, List <int> neighbourSectors, FlowFieldPath flowFieldPath, AddToPathJob pathJob, WorldArea area) { // get all tiles between the sectors List <Tile> tilesOnEdge = new List <Tile>(); List <Tile> tilesChangedInNeigbourSectors = new List <Tile>(); List <int> allSectors = new List <int> { emptySector }; allSectors.AddRange(neighbourSectors); // set which fields/sectors we can do a tile expasion over worldData.multiLevelSectorManager.SetSearchFields(allSectors, area, true); // for each neighbour sector in the Path foreach (int neighbourSector in neighbourSectors) { // get the tiles on edge tilesOnEdge = worldData.multiLevelSectorManager.RowBetweenSectorsWithinWorldArea(area.sectorGrid[0][neighbourSector], area.sectorGrid[0][emptySector], area); //request integration values int[] fieldValues = flowFieldPath.intergrationField.field[new IntVector2(area.index, neighbourSector)]; // put the integration back in the tiles foreach (Tile tile in worldData.multiLevelSectorManager.GetTilesInSector(area.sectorGrid[0][neighbourSector], area)) { tile.integrationValue = fieldValues[tile.indexWithinSector]; tilesChangedInNeigbourSectors.Add(tile); } tilesSearchList.AddRange(tilesOnEdge); } // expand over this and neighbouring sectors, starting at the edges WaveExpansionSearchTiles(area); // remove tiles that have direct connection to a diffrent World Area, their flow direction must not change foreach (int neighbourSector in neighbourSectors) { foreach (AbstractNode node in area.sectorGrid[0][neighbourSector].worldAreaNodes.Keys) { closedSet.Remove(node.tileConnection); } } // add new values to flow field pat worldData.flowFieldManager.AddToFlowFieldPath(closedSet, allSectors, flowFieldPath.destination, pathJob, area, flowFieldPath); flowFieldPath.flowField.FillFlowField(closedSet, worldData); // reset earlier removed tiles foreach (int neighbourSector in neighbourSectors) { foreach (AbstractNode node in area.sectorGrid[0][neighbourSector].worldAreaNodes.Keys) { node.tileConnection.integrationValue = worldData.tileManager.tileResetIntegrationValue; } } closedSet.AddRange(tilesChangedInNeigbourSectors); closedSetFinish.AddRange(closedSet); ResetTilesAfterSearch(); // set search fields back to false worldData.multiLevelSectorManager.SetSearchFields(allSectors, area, false); }
// add sectors/fields public void AddToFlowFieldPath(List <Tile> tiles, List <int> sectors, Tile destination, AddToPathJob pathJob, WorldArea area, FlowFieldPath path) { int amount = worldData.multiLevelSectorManager.GetSectorWidthAtLevel(area, 0) * worldData.multiLevelSectorManager.GetSectorHeightAtLevel(area, 0); path.intergrationField.AddFields(sectors, amount, tiles, area); path.flowField.AddFields(sectors, amount, area); }