コード例 #1
0
ファイル: HexAI.cs プロジェクト: BradZzz/Hex
    public HexCell GetNextPlayer(HexCell[] cells)
    {
        List <HexCell> playerCells = new List <HexCell> ();

        foreach (HexCell cell in cells)
        {
            if (cell.GetPlayer() == player && (cell.GetInfo().actions > 0 || cell.GetInfo().attacks > 0))
            {
                playerCells.Add(cell);
            }
        }
        for (int i = 0; i < playerCells.Count; i++)
        {
            HexCell[] path = HexAI.aStar(cells, playerCells[i]);
            if (path.Length > 0)
            {
                return(playerCells [i]);
            }
            else if (i == playerCells.Count - 1)
            {
                return(playerCells [i]);
            }
        }
//
//
//		foreach(HexCell cell in cells){
//			if (cell.GetPlayer() == player && (cell.GetInfo().actions > 0 || cell.GetInfo().attacks > 0)) {
//				return cell;
//			}
//		}
        return(null);
    }
コード例 #2
0
    void calcDistance(HexCell[] cells)
    {
        foreach (QuestInfo quest in game.quests)
        {
            HexCell start  = cells [0];
            HexCell finish = cells [0];
            foreach (HexCell tCell in cells)
            {
                if (tCell.GetInfo().human)
                {
                    start = tCell;
                }
                if (quest.completed)
                {
                    if (tCell.coordinates.Equals(quest.startIdx))
                    {
                        finish = tCell;
                    }
                }
                else
                {
                    if (tCell.coordinates.Equals(quest.endIdx))
                    {
                        finish = tCell;
                    }
                }
//        if (tCell.coordinates.Equals(quest.startIdx)) {
//          print ("Found start");
//          start = tCell;
//        }
//        if (tCell.coordinates.Equals(quest.endIdx)){
//          print ("Found finish");
//          finish = tCell;
//        }
            }

            HexCell[] path = HexAI.aStar(cells, start, finish);
            if (path != null)
            {
                print("Proximity Length: " + path.Length.ToString());
                if (path.Length < 5)
                {
                    quest.distance = QuestInfo.DistanceType.Burning;
                }
                else if (path.Length < 8)
                {
                    quest.distance = QuestInfo.DistanceType.Warmer;
                }
                else
                {
                    quest.distance = QuestInfo.DistanceType.Cold;
                }
                BaseSaver.putGame(game);
            }
            else
            {
                print("No path found to goal");
            }
        }
    }
コード例 #3
0
ファイル: HexAdventureGenerator.cs プロジェクト: BradZzz/Hex
    /*
     * Need the distance in numbers between two points here
     */

    private static void addCastle(HexCell[] cells, HexCell start, HexCell end)
    {
        HexCell[] road = HexAI.aStar(cells, start, end);
        start.setType(TileInfo.tileType.Road);
        foreach (HexCell cell in road)
        {
            cell.setType(TileInfo.tileType.Road);
        }
    }
コード例 #4
0
ファイル: HexAdventureGenerator.cs プロジェクト: BradZzz/Hex
 private static HexCell[] addRoad(HexCell[] cells, HexCell start, HexCell end)
 {
     HexCell[] road = HexAI.aStar(cells, start, end);
     start.setType(TileInfo.tileType.Road);
     foreach (HexCell cell in road)
     {
         cell.setType(TileInfo.tileType.Road);
     }
     return(road);
 }
コード例 #5
0
    private IEnumerator WaitAndContinue(HexAI ai, float waitTime)
    {
        HexCell player = ai.GetNextPlayer(cells);

        if (player)
        {
            locked = true;
            bool moved    = false;
            bool attacked = false;
            if (!attacked && (player.GetInfo().type != UnitInfo.unitType.Knight && player.GetInfo().actions > 0) ||
                (player.GetInfo().type == UnitInfo.unitType.Knight && player.GetInfo().actions > 1))
            {
                HexCell[] path = HexAI.aStar(cells, player);
                if (path.Length > 0)
                {
                    for (int i = path.Length - 1; i >= 0; i--)
                    {
                        if (path [i].GetPlayer() == -1 && player.GetNeighborDir(path [i]) != HexDirection.None)
                        {
                            moveCell(player, path [i]);
                            moved = true;
                            break;
                        }
                    }
                }
            }
            if (!moved && player.getActiveEnemyAttack() != HexDirection.None && player.GetInfo().attacks > 0)
            {
                HexDirection enemyDir = player.getActiveEnemyAttack();
                attackCell(player, player.GetNeighbor(enemyDir));
                if (player.checkRanged() && player.GetNeighbor(enemyDir).GetNeighbor(enemyDir))
                {
                    attackCell(player, player.GetNeighbor(enemyDir).GetNeighbor(enemyDir));
                }
                attacked = true;
                player.GetInfo().attacks--;
            }
            if (!moved && !attacked)
            {
                if (player.GetInfo().type != UnitInfo.unitType.Swordsman && player.GetInfo().actions > 0 &&
                    player.getActiveEnemy() != HexDirection.None)
                {
                    foreach (HexDirection dir in player.dirs)
                    {
                        HexCell neigh = player.GetNeighbor(dir);
                        if (neigh)
                        {
                            if (neigh.GetPlayer() == -1 && neigh.getActiveEnemy(player.GetPlayer()) == HexDirection.None)
                            {
                                moveCell(player, neigh);
                                neigh.StripTurn();
                                break;
                            }
                        }
                    }
                }
                player.StripTurn();
            }
            hexMesh.Triangulate(cells);
            yield return(new WaitForSeconds(waitTime));

            PlayAI();
        }
        else
        {
            locked = false;
            EndTurn();
        }
    }
コード例 #6
0
    void Start()
    {
        checkEndGame();

        TileInfo[] bTiles = BaseSaver.getTiles();
        UnitInfo[] bUnits = BaseSaver.getUnits();

        HexCell hPos = cells [0];

        if (bTiles != null && bUnits != null)
        {
            game = BaseSaver.getGame();
            for (int i = 0; i < cells.Length; i++)
            {
                cells [i].SetInfo(bUnits[i]);
                cells [i].SetTile(bTiles[i]);

                if (bUnits[i].human)
                {
                    int mv = game.movement - game.fatigue;
                    cells [i].GetInfo().actions = mv < 0 ? 0 : mv;
                    hPos = cells [i];
                    focusOnCell(cells [i]);
                }
            }

            GameObject.Find("TurnImg").GetComponent <Image>().color = playerColors [0];
            setPTurn(0);

            /*
             * Here is where the new quests need to be calculated if there are any
             */
            foreach (QuestInfo quest in game.quests)
            {
                if (!quest.placed)
                {
                    quest.startIdx = hPos.coordinates;
                    List <HexCell> dests = new List <HexCell> ();
                    for (int i = 0; i < cells.Length; i++)
                    {
                        if (cells[i].GetTile().type == quest.locType && !cells[i].GetTile().interaction)
                        {
                            HexCell[] path = HexAI.aStar(cells, hPos, cells[i]);
                            if (path != null && path.Length < 10)
                            {
                                dests.Add(cells[i]);
                            }
                        }
                    }
                    if (dests.Count > 0)
                    {
                        HexCell[] theseDests = dests.ToArray();
                        HexUtilities.ShuffleArray(theseDests);
                        theseDests [0].GetTile().interaction = true;
                        quest.endIdx = theseDests [0].coordinates;
                        quest.placed = true;
                        Debug.Log("Destination Set: " + quest.endIdx.ToString());
                        Debug.Log("Tiles saved");
                        BaseSaver.putBoard(cells, BaseSaver.getBoardInfo().name,
                                           BaseSaver.getBoardInfo().height, BaseSaver.getBoardInfo().width);
                        BaseSaver.putGame(game);
                    }
                    else
                    {
                        Debug.Log("No destinations! Quest invalid...");
                    }
                }
            }

            ResetCells();
            hexMesh.Triangulate(cells);
//			BaseSaver.resetBoard ();
        }
        else
        {
            Debug.Log("Maps: ");
            foreach (MapInfo mp in BaseSaver.getMaps())
            {
                Debug.Log(mp.name);
            }


//			foreach(HexCell cell in cells) {
//				cell.GetTile ().fog = true;
//			}

//			MapInfo map = BaseSaver.getMap("Basic Level");
//      MapInfo map = BaseSaver.getMap(MAP_NAME);

            Debug.Log("Next Map: " + BaseSaver.getNextMap());

            MapInfo map = BaseSaver.getMap(BaseSaver.getNextMap());

            List <int> playerPos = new List <int> ();
            List <int> enemyPos  = new List <int> ();

            for (int i = 0; i < cells.Length; i++)
            {
                if (map.tiles[i].type == TileInfo.tileType.pSpawn)
                {
                    cells[i].setType(TileInfo.tileType.Road);
                    playerPos.Add(i);
                }
                else if (map.tiles[i].type == TileInfo.tileType.eSpawn)
                {
                    cells[i].setType(TileInfo.tileType.Road);
                    enemyPos.Add(i);
                }
                else
                {
                    cells[i].setType(map.tiles[i].type);
                    if (cells[i].GetTile().type == TileInfo.tileType.Castle || cells[i].GetTile().type == TileInfo.tileType.City)
                    {
                        cells [i].GetTile().meta = StaticNames.getTownName();
                        Debug.Log("Town Name: " + cells [i].GetTile().meta);
                    }
                }
            }

            int[] pArr = playerPos.ToArray();
            int[] eArr = enemyPos.ToArray();

            HexUtilities.ShuffleArray(pArr);
            HexUtilities.ShuffleArray(eArr);


            placePlayer(cells[pArr[0]], 0, false, UnitInfo.unitType.Adventure, true);
            cells [pArr[0]].removeFog();

            placePlayer(cells[eArr[0]], 1, false, UnitInfo.unitType.Adventure, false);

//			if (players > 2) {
//				placePlayer(cells[cells.Length - width], 2, false, UnitInfo.unitType.Adventure, false);
//			}
//
//			if (players > 3) {
//				placePlayer (cells [width - 1], 3, false, UnitInfo.unitType.Adventure, false);
//			}

//			foreach (HexCell cell in cells) {
//				//			cell.setType((TileInfo.tileType) Random.Range(0, 7));
//				cell.setType(TileInfo.tileType.Grass);
//			}

//			HexCell[] road = HexAI.aStar (cells, cells[0], cells[cells.Length - 1]);
//			foreach (HexCell cell in road) {
//				cell.setType(TileInfo.tileType.Road);
//			}
//
//      HexAdventureGenerator.generateMap (cells, height, width);
//
//			int mountain = Random.Range (3, cells.Length - 3);
//			cells [mountain].setType (TileInfo.tileType.Mountain);
//			foreach(HexDirection dir in cells [mountain].dirs) {
//				if (cells [mountain].GetNeighbor(dir) && TileInfo.tileType.Grass == cells [mountain].GetNeighbor(dir).GetTile().type) {
//					cells [mountain].GetNeighbor (dir).setType (TileInfo.tileType.Forest);
//				}
//			}

//			foreach (HexCell cell in cells){
//				if (cell.GetPlayer() == -1) {
//					int chp = Random.Range(0, 4);
//					if (chp == 0) {
//						cell.GetTile ().interaction = true;
////						cell.setLabel ("I");
//					}
//				}
//			}

            hexMesh.Triangulate(cells);
            setPTurn(players - 1);
            EndTurn();
            ResetCells();
        }
    }