Example #1
0
        private void gatherResources()
        { //this function will send idle peasants to gather resource
            int id = findFirstUnit("Peasant");

            if (id != -1) //while we still have an idle peasant
            {
                Unit temp = allUnits[playerNum][id];
                if (temp.onCommand == false && (temp.gold <= 0 && temp.lumber <= 0)) //only use peasant if the peasant not already on duty (and not carrying resources)
                {
                    //Check which resource to gather (send peasant gathering the resource that AI has less of at the moment)
                    if (gameData.gold[playerNum] > gameData.lumber[playerNum]) //we have more gold
                    {                                                          //so gather lumber
                        unitPtr tempTarget = new unitPtr();
                        tempTarget.x = (int)temp.x;
                        tempTarget.y = (int)temp.y;
                        unitPtr ret = gameData.findNearestTree(tempTarget, temp, 10, allUnits); //search a range of 10 for tree

                        /*if (ret.owner == -1) //we did not find a tree
                         * {//there was no tree in range (hence expand search distance once)
                         *  ret = gameData.findNearestTree(tempTarget, temp, 10); //search a range of 25 (more extensive search)
                         * }*/
                        if (ret.owner == 0) //after all search, if we had found a tree
                        {                   //move peasant to chop it
                            moveUnit(id, ret.x, ret.y);
                            //this.waitAndTryAgainG += waitTimer;
                        }
                        else //we gather gold as there is usually always gold
                        {
                            unitPtr retG = gameData.findNearestBuilding(allUnits, temp, "gather"); //find a goldMine
                            if (retG.owner != -1) //if there is an existing goldMine
                            {
                                Unit tempGM = allUnits[0][retG.index];
                                moveUnit(id, (int)tempGM.x, (int)tempGM.y); //send peasant to goldMine
                                //this.waitAndTryAgainG += waitTimer;
                            }
                        }
                        //else AI will do nothing as most likely trees ran out around AI
                    }
                    else //we have more lumber or equal so gather gold (priority on gold for army)
                    {
                        unitPtr ret = gameData.findNearestBuilding(allUnits, temp, "gather"); //find a goldMine
                        if (ret.owner != -1) //if there is an existing goldMine
                        {
                            Unit tempGM = allUnits[0][ret.index];
                            moveUnit(id, (int)tempGM.x, (int)tempGM.y); //send peasant to goldMine
                            //this.waitAndTryAgainG += waitTimer;
                        }
                    }
                }
                else if ((temp.gold > 0 || temp.lumber > 0)) //peasant carrying resources, so store resource first
                {
                    if (temp.gold > 0)
                    {
                        unitPtr goTo = gameData.findNearestBuilding(allUnits, temp, "gold");
                        if (goTo.owner != -1) //we actually found a  building
                        {
                            Unit tempBuilding = allUnits[goTo.owner][goTo.index];
                            moveUnit(id, (int)tempBuilding.x, (int)tempBuilding.y);
                        }
                    }
                    else
                    {
                        unitPtr goTo = gameData.findNearestBuilding(allUnits, temp, "lumber");
                        if (goTo.owner != -1) //we actually found a  building
                        {
                            Unit tempBuilding = allUnits[goTo.owner][goTo.index];
                            moveUnit(id, (int)tempBuilding.x, (int)tempBuilding.y);
                        }
                    }
                }
            }
            else
            {
                this.waitAndTryAgainG += waitTimer;
            }
        }