Exemplo n.º 1
0
		//called to update AI unit trigger status whenever a player unit moved to a new tile
		public static void UpdateHostileUnitTriggerStatus(Unit unit){
			if(!instance.hasAIInGame) return;
			
			List<Unit> unitList=GetAllHostileUnit(unit.factionID);
			for(int i=0; i<unitList.Count; i++){
				if(GridManager.GetDistance(unit.tile, unitList[i].tile)<=unitList[i].GetSight()){
					unitList[i].trigger=true;
				}
			}
		}
Exemplo n.º 2
0
 //used to check if a particular tile is visible to a particular unit
 public static bool IsTileVisibleToUnit(Tile tile, Unit unit)
 {
     if (GridManager.GetDistance(tile, unit.tile) <= unit.GetSight())
     {
         if (InLOS(tile, unit.tile))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 3
0
		public static void SetupHostileInRangeforTile(Unit unit, List<Tile> tileList){
			List<Unit> allUnitList=FactionManager.GetAllUnit();
			List<Unit> allHostileUnitList=new List<Unit>();
			for(int i=0; i<allUnitList.Count; i++){
				if(allUnitList[i].factionID!=unit.factionID) allHostileUnitList.Add(allUnitList[i]);
			}
			
			List<Unit> allFriendlyUnitList=new List<Unit>();
			if(GameControl.EnableFogOfWar()) allFriendlyUnitList=FactionManager.GetAllUnitsOfFaction(unit.factionID);
			
			int range=unit.GetAttackRange();
			int rangeMin=unit.GetAttackRangeMin();
			int sight=unit.GetSight();
			
			for(int i=0; i<tileList.Count; i++){
				Tile srcTile=tileList[i];
				List<Tile> hostileInRangeList=new List<Tile>();
				
				for(int j=0; j<allHostileUnitList.Count; j++){
					Tile targetTile=allHostileUnitList[j].tile;
					
					if(GridManager.GetDistance(srcTile, targetTile)>range) continue;
					if(GridManager.GetDistance(srcTile, targetTile)<rangeMin) continue;
					
					if(!GameControl.EnableFogOfWar() && !GameControl.AttackThroughObstacle()){
						if(!FogOfWar.InLOS(srcTile, targetTile, 0)) continue;
					}
					
					bool inSight=GameControl.EnableFogOfWar() ? false : true;
					if(GameControl.EnableFogOfWar()){
						if(FogOfWar.InLOS(srcTile, targetTile) && GridManager.GetDistance(srcTile, targetTile)<=sight){
							inSight=true;
						}
						else if(!unit.requireDirectLOSToAttack){
							for(int n=0; n<allFriendlyUnitList.Count; n++){
								if(allFriendlyUnitList[n]==unit) continue;
								if(GridManager.GetDistance(allFriendlyUnitList[n].tile, targetTile)>allFriendlyUnitList[n].GetSight()) continue;
								if(FogOfWar.InLOS(allFriendlyUnitList[n].tile, targetTile)){
									inSight=true;
									break;
								}
							}
						}
					}
					
					if(inSight) hostileInRangeList.Add(targetTile);
					
				}
				
				tileList[i].SetHostileInRange(hostileInRangeList);
			}
		}
Exemplo n.º 4
0
        //used to check if AI faction can see a given tile
        public static bool IsTileVisibleToFaction(Tile tile, int factionID)
        {
            List <Unit> unitList = FactionManager.GetAllUnitsOfFaction(factionID);

            for (int i = 0; i < unitList.Count; i++)
            {
                if (GridManager.GetDistance(tile, unitList[i].tile) <= unitList[i].GetSight())               //return true;
                {
                    if (InLOS(tile, unitList[i].tile))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 5
0
        //check a tile visiblility to player's faction
        public static bool CheckTileVisibility(Tile tile)
        {
            List <Unit> unitList = FactionManager.GetAllPlayerUnits();

            for (int i = 0; i < unitList.Count; i++)
            {
                if (GridManager.GetDistance(tile, unitList[i].tile) <= unitList[i].GetSight())               //return true;
                //if(InLOS(tile, unitList[i].tile, true)) return true;		//for showing LOS cast
                {
                    if (InLOS(tile, unitList[i].tile))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        private Tile AnalyseCoverSystem(Unit unit, List <Tile> walkableTilesInRange, List <Unit> allHostileInSight)
        {
            List <Tile> walkableTilesInRangeAlt = new List <Tile>();    //for cover system, secondary tiles with less cover

            List <Tile> halfCoveredList = new List <Tile>();            //a list for all the tiles with half Cover
            List <Tile> fullCoveredList = new List <Tile>();            //a list for all the tiles with full Cover

            if (allHostileInSight.Count == 0)
            {
                fullCoveredList = walkableTilesInRange;
            }
            else                //if there are hostile in sight
                                //loop through all the walkable, record their score based on
            {
                for (int i = 0; i < walkableTilesInRange.Count; i++)
                {
                    Tile tile = walkableTilesInRange[i];
                    tile.hostileCount = 0;
                    tile.coverScore   = 0;

                    //iterate through all hostile, add the count, and cover type to the tile, this will then be used in tile.GetCoverRating() when this loop is complete
                    for (int n = 0; n < allHostileInSight.Count; n++)
                    {
                        // if the hostile is out of range, ignore it
                        int hostileRange = allHostileInSight[n].GetMoveRange() + allHostileInSight[n].GetAttackRange();
                        if (GridManager.GetDistance(allHostileInSight[n].tile, tile) > hostileRange)
                        {
                            continue;
                        }

                        tile.hostileCount += 1;

                        CoverSystem._CoverType coverType = CoverSystem.GetCoverType(allHostileInSight[n].tile, walkableTilesInRange[i]);
                        if (coverType == CoverSystem._CoverType.Half)
                        {
                            tile.coverScore += 1;
                        }
                        else if (coverType == CoverSystem._CoverType.Full)
                        {
                            tile.coverScore += 2;
                        }
                    }

                    //get cover rating for the tile
                    //if score is >=2, the tile has full cover from all hostile, so add it to fullCoveredList
                    //if score is >=1 && <2, the tile has half cover from all hostile, so add it to halfCoveredList
                    //if anything <1, the tile is exposed to hostile in some manner
                    if (tile.GetCoverRating() >= 2)
                    {
                        fullCoveredList.Add(tile);
                    }
                    else if (tile.GetCoverRating() >= 1)
                    {
                        halfCoveredList.Add(tile);
                    }
                }
            }

            //if either of the CoveredList is not empty, replace walkableTilesInRange with that since there's no need to consider to move into tiles without cover
            if (fullCoveredList.Count != 0)
            {
                walkableTilesInRange    = fullCoveredList;
                walkableTilesInRangeAlt = halfCoveredList;
            }
            else if (halfCoveredList.Count != 0)
            {
                walkableTilesInRange = halfCoveredList;
            }

            //if there are hostile
            if (allHostileInSight.Count > 0)
            {
                //fill up the walkableTilesInRange hostile list
                //then filter thru walkableTilesInRange, those that have a hostile in range will be add to a tilesWithHostileInRange
                List <Tile> tilesWithHostileInRange = new List <Tile>();
                GridManager.SetupHostileInRangeforTile(unit, walkableTilesInRange);
                for (int i = 0; i < walkableTilesInRange.Count; i++)
                {
                    if (walkableTilesInRange[i].GetHostileInRange().Count > 0)
                    {
                        tilesWithHostileInRange.Add(walkableTilesInRange[i]);
                    }
                }

                if (tilesWithHostileInRange.Count == 0)
                {
                    GridManager.SetupHostileInRangeforTile(unit, walkableTilesInRangeAlt);
                    for (int i = 0; i < walkableTilesInRangeAlt.Count; i++)
                    {
                        if (walkableTilesInRangeAlt[i].GetHostileInRange().Count > 0)
                        {
                            tilesWithHostileInRange.Add(walkableTilesInRangeAlt[i]);
                        }
                    }
                }

                //if the tilesWithHostileInRange is not empty after the process, means there's tiles which the unit can move into and attack
                //return one of those in the tilesWithHostileInRange so the unit can attack
                if (tilesWithHostileInRange.Count > 0)
                {
                    //if the unit current tile is one of those tiles with hostile, just stay put and attack
                    if (tilesWithHostileInRange.Contains(unit.tile))
                    {
                        //randomize it a bit so the unit do move around but not stay in place all the time
                        if (Random.Range(0f, 1f) > 0.25f)
                        {
                            return(unit.tile);
                        }
                    }
                    return(tilesWithHostileInRange[Random.Range(0, tilesWithHostileInRange.Count)]);
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        //analyse the grid to know where the unit should move to
        private Tile Analyse(Unit unit, _AIMode activeMode)
        {
            //get all wakable tiles in range first
            List <Tile> walkableTilesInRange = GridManager.GetTilesWithinDistance(unit.tile, unit.GetEffectiveMoveRange(), true);

            walkableTilesInRange.Add(unit.tile);

            //get all visible hostile
            List <Unit> allHostileInSight = FactionManager.GetAllHostileUnit(unit.factionID);

            if (GameControl.EnableFogOfWar())
            {
                for (int i = 0; i < allHostileInSight.Count; i++)
                {
                    if (!FogOfWar.IsTileVisibleToFaction(allHostileInSight[i].tile, unit.factionID))
                    {
                        allHostileInSight.RemoveAt(i);  i -= 1;
                    }
                }
            }

            //if cover system is in used
            if (GameControl.EnableCover())
            {
                Tile tile = AnalyseCoverSystem(unit, walkableTilesInRange, allHostileInSight);
                if (tile != null)
                {
                    return(tile);
                }
            }


            //if there are hostile
            if (allHostileInSight.Count > 0)
            {
                //fill up the walkableTilesInRange hostile list
                //then filter thru walkableTilesInRange, those that have a hostile in range will be add to a tilesWithHostileInRange
                List <Tile> tilesWithHostileInRange = new List <Tile>();
                GridManager.SetupHostileInRangeforTile(unit, walkableTilesInRange);
                for (int i = 0; i < walkableTilesInRange.Count; i++)
                {
                    if (walkableTilesInRange[i].GetHostileInRange().Count > 0)
                    {
                        tilesWithHostileInRange.Add(walkableTilesInRange[i]);
                    }
                }

                //if the tilesWithHostileInRange is not empty after the process, means there's tiles which the unit can move into and attack
                //return one of those in the tilesWithHostileInRange so the unit can attack
                if (tilesWithHostileInRange.Count > 0)
                {
                    //if the unit current tile is one of those tiles with hostile, just stay put and attack
                    if (tilesWithHostileInRange.Contains(unit.tile))
                    {
                        //randomize it a bit so the unit do move around but not stay in place all the time
                        if (Random.Range(0f, 1f) > 0.25f)
                        {
                            return(unit.tile);
                        }
                    }
                    return(tilesWithHostileInRange[Random.Range(0, tilesWithHostileInRange.Count)]);
                }
            }


            //if there's not potential target at all, check if the unit has any previous attacker
            //if there are, go after the last attacker
            if (unit.lastAttacker != null)
            {
                return(unit.lastAttacker.tile);
            }


            //for aggresive mode with FogOfWar disabled, try move towards the nearest unit
            if (activeMode == _AIMode.Aggressive && Random.Range(0f, 1f) > 0.25f)
            {
                List <Unit> allHostile = FactionManager.GetAllHostileUnit(unit.factionID);
                float       nearest = Mathf.Infinity;   int nearestIndex = 0;
                for (int i = 0; i < allHostile.Count; i++)
                {
                    float dist = GridManager.GetDistance(allHostile[i].tile, unit.tile);
                    if (dist < nearest)
                    {
                        nearest      = dist;
                        nearestIndex = i;
                    }
                }
                return(allHostile[nearestIndex].tile);
            }


            //if there's really no hostile to go after, then just move randomly in one of the walkable
            int rand = Random.Range(0, walkableTilesInRange.Count);

            //clear in hostileInRange list for all moveable tile so, just in case the list is not empty (hostileInRange dont clear after each move)
            //so the unit dont try to attack anything when it moves into the targetTile
            walkableTilesInRange[rand].SetHostileInRange(new List <Tile>());

            return(walkableTilesInRange[rand]);
        }
Exemplo n.º 8
0
        //~ public void ActivateTargetModeUnit(Tile tile, int range, int AOE, bool normalAttack, bool requireDirectLOS, _TargetType type, TargetModeCallBack sCallBack, TargetModeCallBack eCallBack){
        public void ActivateTargetModeUnit(Tile tile, UnitAbility ability, int abIndex, TargetModeCallBack sCallBack, ExitTargetModeCallBack eCallBack)
        {
            TBTK.OnUnitABTargetMode(abIndex);
            _ActivateTargetMode(abIndex, ability.GetAOERange(), ability.targetType, sCallBack, eCallBack);

            if (!ability.AttackInLine())
            {
                if (!ability.normalAttack)
                {
                    if (targetModeType == _TargetType.EmptyTile)
                    {
                        targetModeTileList = GridManager.GetTilesWithinDistance(tile, ability.GetRange(), true);
                    }
                    else
                    {
                        targetModeTileList = GridManager.GetTilesWithinDistance(tile, ability.GetRange());
                    }
                }
                else
                {
                    targetModeTileList = new List <Tile>();
                    List <Tile> tilesInRangeList = GridManager.GetTilesWithinDistance(tile, ability.GetRange());

                    int         sight = tile.unit.GetSight();
                    List <Unit> allFriendlyUnitList = FactionManager.GetAllUnitsOfFaction(tile.unit.factionID);

                    for (int i = 0; i < tilesInRangeList.Count; i++)
                    {
                        Tile targetTile = tilesInRangeList[i];

                        if (!GameControl.EnableFogOfWar() && !GameControl.AttackThroughObstacle())
                        {
                            if (!FogOfWar.InLOS(tile, targetTile, 0))
                            {
                                continue;
                            }
                        }

                        bool inSight = GameControl.EnableFogOfWar() ? false : true;
                        if (GameControl.EnableFogOfWar())
                        {
                            if (FogOfWar.InLOS(tile, targetTile) && GridManager.GetDistance(tile, targetTile) <= sight)
                            {
                                inSight = true;
                            }
                            else if (!ability.requireDirectLOS)
                            {
                                for (int n = 0; n < allFriendlyUnitList.Count; n++)
                                {
                                    if (allFriendlyUnitList[n] == tile.unit)
                                    {
                                        continue;
                                    }
                                    if (GridManager.GetDistance(allFriendlyUnitList[n].tile, targetTile) > allFriendlyUnitList[n].GetSight())
                                    {
                                        continue;
                                    }
                                    if (FogOfWar.InLOS(allFriendlyUnitList[n].tile, targetTile))
                                    {
                                        inSight = true;
                                        break;
                                    }
                                }
                            }
                        }

                        if (inSight)
                        {
                            targetModeTileList.Add(targetTile);
                        }
                    }
                }
            }
            else
            {
                /*
                 * List<Tile> neighbourList=tile.GetNeighbourList();
                 * for(int i=0; i<neighbourList.Count; i++){
                 *      bool walkableOnly=(ability.type==UnitAbility._AbilityType.ChargeAttack);
                 *      List<Tile> tileList=GridManager.GetTilesInALine(tile, neighbourList[i], ability.GetRange(), walkableOnly);
                 *
                 *      if(tileList.Count>0){
                 *              if(targetModeType!=_TargetType.EmptyTile) targetModeTileList.Add(tileList[tileList.Count-1]);
                 *              else if (tileList[tileList.Count-1].unit==null) targetModeTileList.Add(tileList[tileList.Count-1]);
                 *      }
                 * }
                 */
            }

            //for(int i=0; i<targetModeTileList.Count; i++) targetModeTileList[i].SetState(_TileState.Range);
            OverlayManager.ShowAbilityRangeIndicator(targetModeTileList);
        }