List <ScoredSoldier> ScoreUnits(List <SoldierController> units, int targetGridRow) { if (units == null) { Debug.LogWarningFormat(this, "{0} units is empty", this.name); return(new List <ScoredSoldier> ()); } LogicalGrid grid = this.gameManager.GetGrid(); List <ScoredSoldier> scoredSoldiers = new List <ScoredSoldier>(); for (int i = 0; i < units.Count; i++) { ScoredSoldier scored = new ScoredSoldier(); scored.Soldier = units [i]; float minMovesLeft = Mathf.Abs(targetGridRow - scored.Soldier.Position.y); scored.MinMovesToWin = (int)minMovesLeft; scored.LikelyToWin = minMovesLeft / grid.Height; if (scored.Soldier.gameObject.active) { scoredSoldiers.Add(scored); Debug.LogFormat(scored.Soldier, "ScoreUnits[{0}]: minMovesLeft: {1}; ", scored.Soldier.name, scored.MinMovesToWin, scored.LikelyToWin); } } //not optimal// scoredSoldiers.Sort((x, y) => x.LikelyToWin.CompareTo(y.LikelyToWin)); return(scoredSoldiers); }
// Use this for initialization void Awake() { soldierCount = gridWidth; mainCamera = GameObject.FindGameObjectWithTag("MainCamera").transform; mainCamera.position = new Vector3(gridWidth / 2 * tileWidth + cameraOffset.x, Mathf.Max(gridWidth, gridHeight) * 10 + cameraOffset.y, gridWidth / 2 * tileWidth); float xoffset = 0, yoffset = 0; if (gridWidth % 2 == 0) { xoffset = 5; } if (gridHeight % 2 == 0) { yoffset = 5; } for (int ix = 0; ix < gridWidth; ix++) { for (int iy = 0; iy < gridHeight; iy++) { if ((ix + iy) % 2 == 0) { gridPrefab = lightTile; } else { gridPrefab = darkTile; } Transform tile = Instantiate(gridPrefab, new Vector3(ix * 10 + xoffset, 0, iy * 10 + yoffset), Quaternion.identity); tile.parent = this.transform; } } logicalGrid = this.gameObject.AddComponent <LogicalGrid> (); logicalGrid.Setup(this.gridWidth, this.gridHeight); // 0 for team A this.teamA = new List <Transform>(); SpawnSoldiers(teamA, 0, 0, 0); catKing = Instantiate(catKingPrefab, GridPosToWorldPos(-1, 0), Quaternion.Euler(0, 180, 0)); logicalGrid.KingATransform = catKing; // 1 for team B this.teamB = new List <Transform>(); SpawnSoldiers(teamB, 1, 0, gridHeight - 1); //Mathf.RoundToInt(gridWidth), dogKing = Instantiate(dogKingPrefab, GridPosToWorldPos(-1, gridHeight - 1), Quaternion.Euler(0, 180, 0)); logicalGrid.KingBTransform = dogKing; }
IBehaviourTreeNode BuildSimpleAI() { BehaviourTreeBuilder treeBuilder = new BehaviourTreeBuilder(); IBehaviourTreeNode node = treeBuilder .Sequence("simpleAI", true) .Do("DoWeHaveUnits", t => { if (this.aiData != null && this.aiData.myScoredUnits != null && this.aiData.myScoredUnits.Count > 0) { return(BehaviourTreeStatus.Success); } return(BehaviourTreeStatus.Failure); }) .Do("SendCommandTo", t => { LogicalGrid grid = this.gameManager.GetGrid(); List <Direction> posibleMoves = new List <Direction> (); SoldierController selectedSoldier = null; int tryCount = 0; while (selectedSoldier == null && tryCount < 25) { tryCount++; foreach (ScoredSoldier scored in this.aiData.myScoredUnits) { posibleMoves.Clear(); int rnd = Random.Range(0, 10); if (rnd % 2 == 0) { continue; } List <string> posibleMovesStr = new List <string> (); if (grid.CanMakeMove(scored.Soldier, Direction.UP)) { posibleMoves.Add(Direction.UP); posibleMovesStr.Add("Up"); } if (grid.CanMakeMove(scored.Soldier, Direction.DOWN)) { posibleMoves.Add(Direction.DOWN); posibleMovesStr.Add("Down"); } if (grid.CanMakeMove(scored.Soldier, Direction.LEFT)) { posibleMoves.Add(Direction.LEFT); posibleMovesStr.Add("Left"); } if (grid.CanMakeMove(scored.Soldier, Direction.RIGHT)) { posibleMoves.Add(Direction.RIGHT); posibleMovesStr.Add("Right"); } Debug.LogFormat("{0}:: posibleMoves: {1}", scored.Soldier.name, string.Join(", ", posibleMovesStr.ToArray())); if (posibleMoves.Count == 0) { //Dam! Debug.LogWarningFormat(scored.Soldier, "{0}[{1}] Has no possible moves", scored.Soldier.name, scored.Soldier.Position); continue; } selectedSoldier = scored.Soldier; break; } if (selectedSoldier != null) { Debug.LogFormat(selectedSoldier, "{0} SelectedSoldier: {1}; moves: {2}", this.name, selectedSoldier.name, posibleMoves.Count); int selectedMove = Random.Range(0, posibleMoves.Count); Debug.LogFormat(this, "{0}[t:{1}].selected: {2} Move: {3}", this.name, this.turnN, selectedSoldier.name, selectedMove); this.gameManager.IssueCommandTo(this.playerId, selectedSoldier, posibleMoves [selectedMove]); return(BehaviourTreeStatus.Success); } Debug.LogWarningFormat(this, "{0} Cant find soldier with moves", this.name); } Debug.LogWarningFormat(this, "{0} return Failure", this.name); return(BehaviourTreeStatus.Failure); }) .End() .Build(); return(node); }
// Use this for initialization void Start() { this.gridCreator = GameObject.FindObjectOfType <GridCreator>(); this.logicalGrid = this.gridCreator.GetGrid(); }