Esempio n. 1
0
        // Update is called once per frame
        void Update()
        {
            //Defending:
            if (Defending == true)
            {
                //defense timer
                if (DefenseTimer > 0)
                {
                    DefenseTimer -= Time.deltaTime;
                }
                if (DefenseTimer < 0)
                {
                    //stop defending when the timer is done.
                    StopDefending();
                }
            }

            //searching the target faction timer:
            if (SetTargetTimer > 0)
            {
                SetTargetTimer -= Time.deltaTime;
            }
            if (SetTargetTimer < 0)
            {
                //pick the target faction:
                SetTargetTimer = 0.0f;
                PickTargetFaction();
            }

            //if we don't have target yet and the target timer is not running and we're no longer in peace time
            if (TargetFaction == null && GameMgr.PeaceTime == 0.0f && SetTargetTimer == 0.0f)
            {
                //start the timer that will set the target faction at the end.
                SetTargetTimer = Random.Range(SetTargetReload.x, SetTargetReload.y);
            }

            //if we do have a target faction
            if (TargetFaction != null)
            {
                //if the target faction is the same as our faction or the targe faction has lost.
                if (TargetFaction.FactionID == FactionID || GameMgr.Factions [TargetFaction.FactionID].Lost == true)
                {
                    //then the target faction is invalid.
                    TargetFaction = null;
                }
            }

            //taking care of the army, creating army units and buildings:
            if (CheckArmyTimer > 0)
            {
                CheckArmyTimer -= Time.deltaTime;
            }
            if (CheckArmyTimer <= 0)
            {
                CheckArmyTimer = Random.Range(CheckArmyReload.x, CheckArmyReload.y);
                //Try to make the army:
                if (ArmyUnits.Length > 0)
                {
                    //Decide how many army units we need to create:

                    float ArmyPower = 0.0f;
                    if (TargetFaction != null)
                    {
                        ArmyPower = TargetFaction.ArmyPower * ArmyPowerRatio;
                    }
                    if (ArmyPower < MinArmyPower)
                    {
                        ArmyPower = MinArmyPower;
                    }

                    //Loop through all different types of army units:
                    for (int i = 0; i < ArmyUnits.Length; i++)
                    {
                        //Determine how many units we need:
                        ArmyUnits [i].AmountNeeded = Mathf.RoundToInt(((ArmyUnits [i].AmountPerHundred * ArmyPower) / 100) / ArmyUnits [i].Prefab.gameObject.GetComponent <Attack>().UnitDamage) + 1;

                        if (ArmyUnits [i].CurrentUnits.Count < ArmyUnits [i].AmountNeeded)
                        {
                            int j = 0;
                            //We will check if the units we're searching for are already spawned.
                            while (j < FactionMgr.Army.Count && ArmyUnits [i].CurrentUnits.Count < ArmyUnits [i].AmountNeeded)
                            {
                                //When the needed unit is found, add it to the list:
                                if (FactionMgr.Army [j] != null && ArmyUnits [i].Prefab != null)
                                {
                                    if (ArmyUnits [i].CurrentUnits.Contains(FactionMgr.Army [j].UnitMgr) == false)
                                    {
                                        //making sure the army unit has the code that we're looking for:
                                        if (FactionMgr.Army [j].UnitMgr.Code == ArmyUnits [i].Prefab.Code)
                                        {
                                            //add it to tAAhe list.
                                            ArmyUnits [i].CurrentUnits.Add(FactionMgr.Army [j].UnitMgr);
                                            FactionMgr.Army [j].ArmyUnitID = i;

                                            //Make all enemy units attack on range:
                                            FactionMgr.Army [j].AttackInRange = true;

                                            if (ArmyUnits [i].ProgressAmount > 0)
                                            {
                                                ArmyUnits [i].ProgressAmount--;
                                            }
                                        }
                                    }
                                }
                                j++;
                            }

                            int ID = -1;                             //This will hold the task ID that produces the required army type, if found.
                            //If we still haven't found enough units then we'll simply look for buildings that can produce them.
                            if (ArmyUnits [i].CurrentUnits.Count + ArmyUnits [i].ProgressAmount < ArmyUnits [i].AmountNeeded)
                            {
                                int n = 0;
                                while (n < ArmyUnits [i].Prefabs [ArmyUnits [i].PrefabID].SpawnedBuildings.Count && ArmyUnits [i].CurrentUnits.Count + ArmyUnits [i].ProgressAmount < ArmyUnits [i].AmountNeeded)
                                {
                                    Building Building = ArmyUnits [i].Prefabs [ArmyUnits [i].PrefabID].SpawnedBuildings [n];
                                    //Make sure that the current building has enough health and is completely built.
                                    //Check if the building can produce army units and that it still have avilable space for a new task and that the building is not upgrading.
                                    if (Building.ArmyUnits.Count > 0 && Building.TasksQueue.Count < Building.MaxTasks && Building.BuildingUpgrading == false)
                                    {
                                        int Counter = 0;
                                        while (ID == -1 && Counter < Building.ArmyUnits.Count)
                                        {
                                            //if this is the unit we're looking for.
                                            if (Building.FactionID == FactionID && Building.BuildingTasksList[Building.ArmyUnits[Counter]].UnitPrefab.Code == ArmyUnits[i].Prefab.Code)
                                            {
                                                //make sure that the unit hasn't reached its limit:
                                                if (FactionMgr.HasReachedLimit(Building.BuildingTasksList[Building.ArmyUnits[Counter]].UnitPrefab.Code) == false)
                                                {
                                                    ID = Building.ArmyUnits[Counter];
                                                }
                                            }
                                            Counter++;
                                        }
                                        if (ID >= 0)
                                        {
                                            if (Building.IsBuilt == true && Building.Health >= Building.MinTaskHealth)
                                            {
                                                //Check if we have enough resources to launch this task:
                                                while (GameMgr.ResourceMgr.CheckResources(Building.BuildingTasksList[ID].RequiredResources, FactionID, GameMgr.ResourceMgr.FactionResourcesInfo[FactionID].NeedRatio) == true && ArmyUnits [i].CurrentUnits.Count + ArmyUnits [i].ProgressAmount < ArmyUnits [i].AmountNeeded && ArmyUnits [i].ProgressAmount < (ArmyUnits [i].AmountNeeded - ArmyUnits [i].CurrentUnits.Count) && GameMgr.Factions [FactionID].CurrentPopulation < GameMgr.Factions [FactionID].MaxPopulation)
                                                {
                                                    //Add the new task to the building's task queue
                                                    GameMgr.TaskMgr.LaunchTask(Building, ID, -1, TaskManager.TaskTypes.CreateUnit);

                                                    //Start waiting for the new unit to be created:
                                                    ArmyUnits [i].ProgressAmount++;
                                                }

                                                if (GameMgr.ResourceMgr.CheckResources(Building.BuildingTasksList[ID].RequiredResources, FactionID, GameMgr.ResourceMgr.FactionResourcesInfo[FactionID].NeedRatio) == false)                                                    // No resources are available for this task:
                                                {
                                                    int RequiredAmount = ArmyUnits [i].AmountNeeded - (ArmyUnits [i].CurrentUnits.Count + ArmyUnits [i].ProgressAmount);
                                                    for (int x = 0; x < Building.BuildingTasksList[ID].RequiredResources.Length; x++)
                                                    {
                                                        //ask the resource manager for resources to launch the unit creation task.
                                                        if (ArmyUnits [i].AskedForTaskRscr == true)
                                                        {
                                                            RequiredAmount = 0;
                                                        }
                                                        FactionMgr.ResourceMgr.LookForResources(Building.BuildingTasksList [ID].RequiredResources [x].Name, Building.BuildingTasksList [ID].RequiredResources [x].Amount * RequiredAmount);
                                                    }

                                                    //Ask for resources:
                                                    ArmyUnits[i].AskedForTaskRscr = true;
                                                }
                                                //If we don't have any more space for new units:
                                                if (GameMgr.Factions [FactionID].CurrentPopulation >= GameMgr.Factions [FactionID].MaxPopulation)
                                                {
                                                    FactionMgr.BuildingMgr.AttemptToAddBuilding(FactionMgr.BuildingMgr.PopulationBuilding, false, null);
                                                }
                                            }
                                            else
                                            {
                                                //Call for builders as the building hasn't been built yet.
                                                if (Building.Placed == true)
                                                {
                                                    FactionMgr.BuildingMgr.ConstructBuilding(Building);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //...
                                    }

                                    n++;
                                }
                            }
                            //No building to create
                            if (ID == -1 && ArmyUnits [i].CurrentUnits.Count + ArmyUnits [i].ProgressAmount < ArmyUnits [i].AmountNeeded)
                            {
                                //We need to construct a buildign that can create the required army unit:
                                //look for the suitable building to build this unit if we don't already have it saved:
                                if (ArmyUnits [i].SourceBuilding == null)
                                {
                                    ArmyUnits [i].SourceBuilding = FactionMgr.BuildingMgr.GetBuildingByUnit(ArmyUnits [i].Prefab);
                                }
                                FactionMgr.BuildingMgr.AttemptToAddBuilding(ArmyUnits [i].SourceBuilding, false, null);
                            }
                        }
                    }

                    //Make sure it's not the peace time and that the AI player is not under attack:
                    if (GameMgr.PeaceTime == 0.0f && Defending == false && Attacking == false && UnderAttack == false)
                    {
                        //Attempt to launch an attack:
                        if (CanAttack == true && TargetFaction != null)
                        {
                            //If the team can actually launch an attack and they have enough army power:
                            if (FactionMgr.ArmyPower / TargetFaction.ArmyPower >= ArmyPowerRatio && FactionMgr.ArmyPower >= MinArmyPower)
                            {
                                Attacking = true;
                                LaunchAttack();
                            }
                        }
                    }
                }
            }


            if (Attacking == true)               //If the faction is currently in war:
            {
                if (TargetFaction != null)
                {
                    if (AttackTimer > 0)
                    {
                        //Handling the attack timer.
                        AttackTimer -= Time.deltaTime;

                        if (TargetBuilding == null)
                        {
                            AttackTimer = -1.0f;
                        }
                        else if (TargetingBuilders == false && TargetBuilding.WorkerMgr.CurrentWorkers > 0)
                        {
                            SendUnitsToTargetBuilding();
                        }
                    }
                    if (AttackTimer < 0)
                    {
                        //Each time the attack timer is done, the faction gives order to its attacking units:

                        //First check if we have reached the surrender army power:
                        if (AttackingArmyPower <= SurrenderArmyPower)
                        {
                            //Cancel the attack.
                            StopAttack();
                        }
                        else
                        {
                            if (TargetBuilding == null)                               //If the target building is not valid
                            //Search for one:
                            {
                                SetTargetBuilding();
                            }

                            if (TargetBuilding != null)
                            {
                                //If we've found a target building, command the attacking units.
                                SendUnitsToTargetBuilding();
                            }

                            AttackTimer = Random.Range(AttackTimeRange.x, AttackTimeRange.y);                              //Reload the attack timer.
                        }
                    }
                }
                else
                {
                    StopAttack();
                }
            }
        }
 /// <summary>
 /// Determines whether the regulator component has reached the maximum allowed amount for active instances or not.
 /// </summary>
 /// <returns>True if the maximum amount of instances is reached, otherwise false.</returns>
 public bool HasReachedMaxAmount()
 {
     return(Count >= MaxAmount || factionMgr.HasReachedLimit(Code, Category) || pendingAmount >= MaxPendingAmount);
 }
Esempio n. 3
0
        void Update()
        {
            if (Units.Length > 0 && AllCreated == false)
            {
                int ReadyUnits = 0;                    //amount of unit type that are created with the correct amounts.
                for (int i = 0; i < Units.Length; i++) //loop through the units to create
                {
                    if (Units [i].StartCreatingAfter > 0)
                    {
                        Units [i].StartCreatingAfter -= Time.deltaTime;                         //the timer before starting to create any unit
                    }
                    else
                    {
                        //spawn timer:
                        if (Units [i].SpawnTimer > 0)
                        {
                            Units [i].SpawnTimer -= Time.deltaTime;
                        }
                        else
                        {
                            Units [i].AllCreated = true;                             //initially we will set this to true then check:
                            ReadyUnits++;

                            //restart the timer:
                            Units[i].SpawnTimer = Random.Range(Units[i].SpawnReloadRange.x, Units[i].SpawnReloadRange.y);

                            //calculate the amount to create from this unit:
                            Units [i].AmountNeeded = Mathf.RoundToInt(GameMgr.Factions [FactionID].MaxPopulation * Random.Range(Units [i].PopulationRatio.x, Units [i].PopulationRatio.y));
                            if (Units [i].AmountNeeded > Units [i].MaxAmount)                               //if the current goal is greater than the max allowed amount.
                            {
                                Units [i].AmountNeeded = Units [i].MaxAmount;                               //set it to the limit
                            }

                            if (Units [i].CurrentUnits.Count < Units [i].AmountNeeded)                               //still haven't reached our goal?
                            {
                                Units [i].AllCreated = false;
                                ReadyUnits--;
                            }


                            if (Units [i].AllCreated == false)                               //still need to reach our goal:
                            //check if the unit hasn't reached its limit:
                            {
                                if (FactionMgr.HasReachedLimit(Units[i].Prefab.Code) == false)
                                {
                                    if ((GameMgr.Factions[FactionID].MaxPopulation - GameMgr.Factions[FactionID].CurrentPopulation) > 0)
                                    {                                                                                                   //if we still have space in the population slots:
                                      //search for a building to create this unit:
                                        int AmountLeft = Units[i].AmountNeeded - Units[i].CurrentUnits.Count - Units[i].ProgressAmount; //Amount of units that we still need to create
                                        int j          = 0;

                                        while (AmountLeft > 0 && j < Units[i].SourceBuildings.Count)
                                        {     //go through the buildings that can create this unit:
                                            while (AmountLeft > 0 && Units[i].SourceBuildings[j].TasksQueue.Count < Units[i].SourceBuildings[j].MaxTasks)
                                            { //if this still has space for one more task and we still haven't finished creating all units
                                                //check if we have enough resources:
                                                if (GameMgr.ResourceMgr.CheckResources(Units[i].SourceBuildings[j].BuildingTasksList[Units[i].TasksIDs[j]].RequiredResources, FactionID, FactionMgr.ResourceMgr.ResourceNeedRatio))
                                                {
                                                    //create a unit
                                                    GameMgr.TaskMgr.LaunchTask(Units[i].SourceBuildings[j], Units[i].TasksIDs[j], i, TaskManager.TaskTypes.CreateUnit);
                                                    AmountLeft--;
                                                    Units[i].ProgressAmount++; //mark the new unit creation as pending
                                                }
                                                else
                                                {
                                                    AmountLeft = 0;
                                                }
                                            }
                                            j++;
                                        }
                                    }
                                    else
                                    {
                                        //not population
                                    }
                                }
                                else
                                {
                                    //limit reached
                                }
                            }
                        }
                    }
                }

                if (ReadyUnits == Units.Length)
                {
                    AllCreated = true;                     //all units have been created.
                }
            }
        }
Esempio n. 4
0
 //determine if we have reached the maximum amount or not on this regulator or if the item limits have been reached
 public bool HasReachedMaxAmount()
 {
     return(amount >= maxAmount || factionMgr.HasReachedLimit(code) || pendingAmount >= maxPendingAmount);
 }
Esempio n. 5
0
        //----------------------------------------------------------------------------------------------------------------------------------------------------------------

        //Called each time we try to add a building to construct:
        public void AttemptToAddBuilding(Building Building, bool AllowMultiple, GameObject BuildAround)
        {
            /*
             * Building: The building prefab that we want to add.
             * Allow Multiple: When true, we need to wait to complete placing the same building before placing another.
             * Build Around: Sometimes we don't need to build directly around a city center but around a specific position (such as a resource).
             *
             * */

            //if the building's limit has been reached, don't place building:
            if (FactionMgr.HasReachedLimit(Building.Code))
            {
                return; //don't place building
            }

            //check building placement priorities:
            if (FactionMgr.ResourceMgr.MustHaveDropOff == true)
            {     //if the faction must have a drop off building
                if (FactionMgr.DropOffBuildings.Count == 0)
                { //and there's still no drop off buildings
                  //if the building that the faction is attempting to place is not a drop off building
                    if (Building.ResourceDropOff == false)
                    {
                        return; //don't place it.
                    }
                }
            }
            //if the same building is already getting placed and we can't have multiple buildings of the same type at once.
            if (IsBeingBuilt(Building) == true && AllowMultiple == false)
            {
                return;
            }


            //Check if the faction have enough resources to build this building:
            if (ResourceMgr.CheckResources(Building.BuildingResources, FactionID, ResourceMgr.FactionResourcesInfo[FactionID].NeedRatio) == true)
            {
                //Search a building center to construct building inside its borders:
                int CenterID = -1;
                if (BuildAround == null)
                {                                                   //if we can build anywhere.
                    CenterID = GetAvailableCenterID(Building.Code); //search for an available center to build around.
                }
                else
                { //else get the nearest center to our build around point
                    CenterID = FactionMgr.BuildingCenters.IndexOf(FactionMgr.BuildingMgr.GetNearestBuilding(BuildAround.transform.position, FactionMgr.BuildingMgr.BuildingCenter.Code));
                }

                //If we found a suitable building center:
                if (CenterID != -1)
                {
                    //If the building must be placed around a resource and it still hasn't one:
                    if (Building.PlaceNearResource == true && BuildAround == null)
                    {
                        Resource BuildAroundResource = GetBorderResource(Building.ResourceName, FactionMgr.BuildingCenters[CenterID].BorderMgr);
                        if (BuildAroundResource != null) //if a resource to build around is found
                        {
                            BuildAround = BuildAroundResource.gameObject;
                        }
                        else
                        {
                            return; //No resource to build around is found, abort building placement
                        }
                    }

                    //run this method to add a new building to place:
                    AddBuildingToList(Building, CenterID, BuildAround);
                }
                else
                {                                                      //if we can't find a building center
                    AttemptToAddBuilding(BuildingCenter, false, null); //attempt to create a new building center.
                }
            }
            else
            { //no available resources
                //if this is a population building:
                if (Building == PopulationBuilding)
                {
                    //if we still can ask for population building resources:
                    if (AskForPopulationResources == true)
                    {
                        AskForPopulationResources = false;
                    }
                    else
                    {
                        return; //if not stop here.
                    }
                }

                //ask for resources:
                AskForBuildingResources(Building);
            }
        }