public SwarmOrder createOrder(byte source, byte target, int spawnZone) { PathSolution ps = _computedPaths [spawnZone]; if (source != 0) { ps = ps.getSpecifiSolution(source); } Vector3 targetAvgPos = new Vector3(); Queue <Vector3> path = null; if (target == MAIN_TARGET) { path = ps.getPathToTarget(); targetAvgPos = SpawnTable.getInstance().getTargetSpawns() [0].getAveragePosition(); } else { path = ps.getPathToTower(target); if (path == null) { Debug.Log("Null order. source: " + source + ", target: " + target + ", spawnZone: " + spawnZone); return(null); } targetAvgPos = SpawnTable.getInstance().getTowerSpawns() [target].getAveragePosition(); } SwarmOrder order = new SwarmOrder(target, targetAvgPos, path); return(order); }
private static PathSolution PathFindingProcess(Logger logger, GameContext context, PaintBrush brush, NPCProvider npcProvider, PathFinderProvider pathFinder) { bool[,] negativeSquare = context.Square.To2DBinaryArray(context.Enemies.Select(x => x.Position)); Context pathFinderContext = pathFinder .CreateContext( context.Player.Position, context.Target, negativeSquare); // Speed-up for solution, where shortest path is key pathFinderContext.Field = pathFinder.DeadFill( pathFinderContext.Field, context.Square, brush, new List <Position> { context.Player.Position, context.Target }, pathFinderContext); PathSolution solution = pathFinder.FindPath(pathFinderContext); return(solution); }
public void setTowerCustomSolution(byte towerSpawn, PathSolution solution) { if (_solutionsFromTowers == null) { _solutionsFromTowers = new Dictionary <byte, PathSolution> (); } _solutionsFromTowers.Add(towerSpawn, solution); }
public void preComputePaths() { _computedPaths.Clear(); Dictionary <int, SpawnPoint> enemySpawns = SpawnTable.getInstance().getMonsterSpawnPoints(); foreach (int soldierSpawnIndex in enemySpawns.Keys) { SpawnPoint spawnObject = enemySpawns [soldierSpawnIndex]; SpawnPoint target = SpawnTable.getInstance().getTargetSpawns() [0]; float closer = Mathf.Infinity; int index = 0; foreach (int vIndex in target.getBounds().Keys) { float sqrtMag = (target.getBounds() [vIndex] - spawnObject.getAveragePosition()).sqrMagnitude; if (sqrtMag < closer) { closer = sqrtMag; index = vIndex; } } Vector3 finalTarget = Formulas.getPositionInMap(target.getBounds() [index]); Pathfinder pf = new Pathfinder(spawnObject, finalTarget); PathSolution ps = pf.computePaths(); GameObject go = new GameObject("Spawn_Zone_Controller_" + soldierSpawnIndex); go.transform.parent = World.getInstance().getTerrainTransform(); go.transform.position = Formulas.getPositionInMap(spawnObject.getAveragePosition()); // Irrelevant, but still... go.AddComponent <SpawnZoneController> ()._spawnZone = soldierSpawnIndex; _spawnZoneControllers.Add(soldierSpawnIndex, go); _computedPaths.Add(soldierSpawnIndex, ps); } SwarmAI.getInstance().initialize(_computedPaths); }
public void initialize(Dictionary <int, PathSolution> computedPaths) { _activeTowers.Clear(); foreach (int spawnZone in computedPaths.Keys) { PathSolution ps = computedPaths [spawnZone]; Dictionary <byte, TowerSpawnInfo> pathInfo = new Dictionary <byte, TowerSpawnInfo> (); Dictionary <byte, Queue <Vector3> > .KeyCollection kc = ps.getAllTowersInPath(); TowerSpawnInfo targetInfo = new TowerSpawnInfo(); targetInfo._active = true; targetInfo._aggro = 1.0f; pathInfo.Add(0, targetInfo); foreach (byte b in kc) { pathInfo.Add(b, new TowerSpawnInfo()); } _activeTowers.Add(spawnZone, pathInfo); } }
public PathSolution computePaths() { // 1 - Build the path from Spawn to Final target int initialSpawnPoint = findBestSpawnPoint(); PathSolution ps = new PathSolution(initialSpawnPoint); Node fromStartToEnd = findPath(initialSpawnPoint, _mainTarget, true); Stack <int> mainPath = buildSolution(fromStartToEnd); ps.setMainPath(mainPath); int width = World.getInstance().getWorldWidth(); // 2 - Loop over towers in the path from the spawn to the target foreach (byte towerSpawn in _towerPositions.Keys) { // 2.1 Compute path from spawn to tower X int index = _towerPositions [towerSpawn]; Vector3 worldPos = _swarm._positions [index]; Node endNode = findPath(initialSpawnPoint, worldPos, false); int lastValidIndex = endNode._i * width + endNode._j; Stack <int> pathToTower = buildSolution(endNode); ps.setTowerPath(towerSpawn, pathToTower); // 2.2 Create custom solution from tower X itself PathSolution towerSolution = new PathSolution(lastValidIndex); // 2.2.1 Compute path from tower X to main target endNode = findPath(lastValidIndex, _mainTarget, false); towerSolution.setMainPath(buildSolution(endNode)); float distanceFromTowerToTarget = (_mainTarget - worldPos).sqrMagnitude; // 2.2.2 Compute path from tower X to the rest of towers foreach (byte innerTowerSpawn in _towerPositions.Keys) { if (innerTowerSpawn == towerSpawn) { continue; } // Do NOT add a tower which is not in the way to the target Vector3 nextTowerPosition = _swarm._positions [_towerPositions [innerTowerSpawn]]; if ((_mainTarget - nextTowerPosition).sqrMagnitude >= distanceFromTowerToTarget) { continue; } Node solutionForTower = findPath(lastValidIndex, nextTowerPosition, false); towerSolution.setTowerPath(innerTowerSpawn, buildSolution(solutionForTower)); } // 2.3 Set custom tower solution within main solution ps.setTowerCustomSolution(towerSpawn, towerSolution); } return(ps); }