コード例 #1
0
 public void reCalcUnits()
 {                                                    //function to recalculate number of each units this player has
     //this should be constantly called before assigning action to AI units (basically so our AI understands its own capability)
     Array.Clear(this.workingUnit, 0, this.numUnits); //clear array to zero
     foreach (Unit u in allUnits[this.playerNum])
     {
         if (notDead(u)) //first check to make sure unit is not dead
         {
             if (u.unitType == nameTranslation("Peasant") && ((u.constructing || u.goldMining) || (u.constructing == false && u.goldMining == false)))
             {
                 this.workingUnit[u.unitType] += 1; //it's a peasant that was invisible due to construction or goldmining
             }
             else if (gameData.checkInvisible(u))   //if unit is not invisible
             {
                 this.workingUnit[u.unitType] += 1;
             }
         }
     }
 }
コード例 #2
0
        private bool openSpace(int x, int y, int w, int h, List <Unit>[] allUnits, tempMap gameData, int mode)
        { //check if there is already a unit at that position (even if unit is movable, we can't let build over; maybe in future we can)
            //also checks if there is collision between images
            //different checks for unit creation vs building construction
            for (int i = 0; i <= gameData.numPlayers; i++)
            {
                foreach (Unit u in allUnits[i])
                {
                    if (gameData.checkInvisible(u) && Math.Abs(u.x - x) < 10 && Math.Abs(u.y - y) < 10) //only check units nearby to selected coordinates and only if unit is not invisble //invisible == 0 default
                    {
                        int unitW = u.unitTileWidth;
                        int unitH = u.unitTileHeight;
                        //int unitW = u.defaultUnitTileW;
                        //int unitH = u.defaultUnitTileH;

                        if (!gameData.canPlaceOn(x, y))
                        {
                            return(false);                           //check if it's traversible terrain
                        }
                        //One case for this unit being inside other unit, other case for other unit being inside this unit

                        /*if (x >= u.x && x < u.x + unitW && y >= u.y && y < u.y + unitH) //check top left corner
                         * { //touching another unit
                         *  return false;
                         * }
                         * else if (x + w > u.x && x + w < u.x + unitW && y + h > u.y && y + h < u.y + unitH) //check bottom right corner
                         * { //touching another unit (w/0 = sign)
                         *  return false;
                         * }*/

                        if (mode == 0)
                        {                                                                             //strict restriction for building (so no overlap)
                            //currently prevents building right next to each other (all buildings have gap)
                            if (x >= u.x && x <= u.x + unitW - 1 && y >= u.y && y <= u.y + unitH - 1) //check top left corner
                            {                                                                         //touching another unit
                                return(false);
                            }
                            else if (x + w - 1 >= u.x && x + w - 1 <= u.x + unitW - 1 && y + h - 1 >= u.y && y + h - 1 <= u.y + unitH - 1) //check bottom right corner
                            {                                                                                                              //touching another unit
                                return(false);
                            }
                            else if (x >= u.x && x <= u.x + unitW - 1 && y + h - 1 >= u.y && y + h - 1 <= u.y + unitH - 1) //check bottom left corner
                            {                                                                                              //touching another unit
                                return(false);
                            }
                            else if (x + w - 1 >= u.x && x + w - 1 <= u.x + unitW - 1 && y >= u.y && y <= u.y + unitH - 1) //check top right corner
                            {                                                                                              //touching another unit
                                return(false);
                            }
                            else if (x <= u.x && x + w - 1 >= u.x + unitW - 1 && y <= u.y && y + h - 1 >= u.y + unitH - 1) //check if smaller unit is at the spot (so we don't build on top of them)
                            {                                                                                              //touching another unit
                                return(false);
                            }
                        }
                        else if (mode == 1)
                        {                                                                   //less restrictive so can spawn unit next to building
                            if (x >= u.x && x < u.x + unitW && y >= u.y && y < u.y + unitH) //check top left corner
                            {                                                               //touching another unit
                                return(false);
                            }
                            else if (x + w > u.x && x + w < u.x + unitW && y + h > u.y && y + h < u.y + unitH) //check bottom right corner
                            {                                                                                  //touching another unit (w/0 = sign)
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }