//toggling defense for building centers: void ToggleCenterDefense(Vector3 attackPos, bool enable) { //if we're enabling center defense: if (enable == true) { //if the last defense center was the capital then do not change it: if (lastDefenseCenter != gameMgr.Factions[factionMgr.FactionID].CapitalBuilding) { //get closest capital building to attack position: lastDefenseCenter = BuildingManager.GetClosestBuilding(attackPos, factionMgr.BuildingCenters); } //if no valid center is assigned: if (lastDefenseCenter == null) { return; //do not continue } } //go through the army units and set the defense center: foreach (Unit u in factionMgr.Army) { //make sure the unit is not deployed for attack: if (npcMgr.attackManager_NPC.IsUnitDeployed(u) == false) { //if there are multiple attack components if (u.MultipleAttacksMgr != null) { //go through them and set the same settings. foreach (Attack a in u.MultipleAttacksMgr.AttackTypes) { //enabling or disabling? if (enable) { a.AttackRangeCenter = lastDefenseCenter; a.AttackRangeFromCenter = true; } else { a.AttackRangeFromCenter = false; } } } else //only one attack component: { //enabling or disabling? if (enable) { u.AttackMgr.AttackRangeCenter = lastDefenseCenter; u.AttackMgr.AttackRangeFromCenter = true; } else { u.AttackMgr.AttackRangeFromCenter = false; } } } } }
//toggling defense for building centers: void ToggleCenterDefense(Vector3 attackPos, bool enable) { //if we're enabling center defense: if (enable == true) { //if the last defense center was the capital then do not change it: if (lastDefenseCenter != gameMgr.GetFaction(factionMgr.FactionID).GetCapitalBuilding()) { //get closest capital building to attack position: lastDefenseCenter = BuildingManager.GetClosestBuilding(attackPos, factionMgr.GetBuildingCenters()); } //if no valid center is assigned: if (lastDefenseCenter == null) { return; //do not continue } } //go through the army units and set the defense center: foreach (Unit u in factionMgr.GetAttackUnits()) { //make sure the unit is not deployed for attack: if (npcMgr.attackManager_NPC.IsUnitDeployed(u) == false) { //if there are multiple attack components if (u.MultipleAttackMgr != null) { //go through them and set the same settings. foreach (AttackEntity a in u.MultipleAttackMgr.AttackEntities) { a.SearchRangeCenter = (enable == true) ? lastDefenseCenter.BorderComp : null; } } else //only one attack component: { u.AttackComp.SearchRangeCenter = (enable == true) ? lastDefenseCenter.BorderComp : null; } } } }
//a method that launches the NPC faction's /// <summary> /// Launches the NPC faction's defense mode to defend a certain position. /// </summary> /// <param name="defensePosition">The position to defend.</param> /// <param name="forceCapitalChange">True to allow units to defend territory that's not under the capital building even if capital building's territory is under attack, otherwise false.</param> public void LaunchDefense(Vector3 defensePosition, bool forceCapitalChange) { //reload the defense timer: cancelDefenseTimer = cancelDefenseReloadRange.getRandomValue(); //Get the building that is closest to where the damage has been done. Building defenseCenter = BuildingManager.GetClosestBuilding(defensePosition, factionMgr.GetBuildingCenters()); ToggleCenterDefense(defenseCenter, true, forceCapitalChange); //enable defense for the closest building center to the defense position. //if the defense is already activated if (IsActive) { return; //do not proceed. } Activate(); //activate defense mode //is the NPC faction is undergoing an active attack on another faction but it's not allowed in defense mode? if (cancelAttackOnDefense && npcMgr.GetNPCComp <NPCAttackManager>().IsAttacking) { //cancel attack: npcMgr.GetNPCComp <NPCAttackManager>().CancelAttack(); } }