private void UpdateLord() { IntVec3 rallyPoint = IntVec3.Invalid; // Check there is no already existing defense lord. if ((Find.LordManager.lords != null) && (Find.LordManager.lords.Count > 0)) { foreach (Lord lord in Find.LordManager.lords) { if ((lord.faction != null) && (lord.faction == OG_Util.FactionOfMiningCo)) { return; } } } // Look for hostile in outpost perimeter. IntVec3 hostilePosition = FindHostileInPerimeter(); if (hostilePosition.IsValid) { Area outpostArea = OG_Util.FindOutpostArea(); if ((outpostArea != null) && (outpostArea.ActiveCells.Contains(hostilePosition))) { // Ennemy is inside outpost area. rallyPoint = hostilePosition; } else { const int sectionsNumber = 100; Vector3 sectionVector = (this.outpostCenter - hostilePosition).ToVector3(); sectionVector = sectionVector / sectionsNumber; // Default value if OutpostArea does not exist (should not occur, just a safety...). rallyPoint = (hostilePosition.ToVector3() + sectionVector * 0.2f * (float)sectionsNumber).ToIntVec3(); for (int i = 1; i <= sectionsNumber; i++) { Vector3 potentialRallyPoint = hostilePosition.ToVector3() + sectionVector * i; if ((outpostArea != null) && (outpostArea.ActiveCells.Contains(potentialRallyPoint.ToIntVec3()))) { // Ensure rallyPoint is completely inside the outpost area. rallyPoint = (potentialRallyPoint + sectionVector * 0.1f * (float)sectionsNumber).ToIntVec3(); break; } } } } else { // Look for damaged turret to defend. Rot4 turretRotation = Rot4.Invalid; IntVec3 turretPosition = IntVec3.Invalid; FindDamagedTurret(out turretPosition, out turretRotation); if (turretPosition.IsValid) { if (OG_Util.IsModActive("MiningCo. ForceField")) { // Look for nearest force field to cover behind. foreach (Thing thing in Find.ListerThings.ThingsOfDef(OG_Util.ForceFieldGeneratorDef)) { if (thing.Position.InHorDistOf(turretPosition, 23f)) { rallyPoint = thing.Position + new IntVec3(0, 0, -2).RotatedBy(thing.Rotation); break; } } } else { // Just go near the attacked turret. rallyPoint = turretPosition + new IntVec3(0, 0, -5).RotatedBy(turretRotation); } } } if (rallyPoint.IsValid) { // Generate defense lord. LordJob_Joinable_DefendOutpost lordJob = new LordJob_Joinable_DefendOutpost(rallyPoint); LordMaker.MakeNewLord(OG_Util.FactionOfMiningCo, lordJob); SoundDef soundDef = SoundDefOf.MessageSeriousAlert; // TODO: add a siren like in alert speaker! soundDef.PlayOneShot(rallyPoint); // Stop all pawns job. foreach (Pawn pawn in Find.MapPawns.AllPawns) { if ((pawn.Faction != null) && (pawn.Faction == OG_Util.FactionOfMiningCo) && (pawn.kindDef != OG_Util.OutpostTechnicianDef)) { pawn.ClearMind(); } } } }
private void UpdateLord() { IntVec3 rallyPoint = IntVec3.Invalid; Log.Message("UpdateLord1"); // Check there is no already existing defense lord. if ((Find.LordManager.lords != null) && (Find.LordManager.lords.Count > 0)) { foreach (Lord lord in Find.LordManager.lords) { if ((lord.faction != null) && (lord.faction == OG_Util.FactionOfMiningCo)) { return; } } } Log.Message("UpdateLord2"); // Look for hostile in outpost perimeter. IntVec3 hostilePosition = FindHostileInPerimeter(); if (hostilePosition.IsValid) { Log.Message("UpdateLord2"); Area outpostArea = OG_Util.FindOutpostArea(); if ((outpostArea != null) && (outpostArea.ActiveCells.Contains(hostilePosition))) { Log.Message("UpdateLord4"); // Ennemy is inside outpost area. rallyPoint = hostilePosition; } else { Log.Message("UpdateLord5"); const int sectionsNumber = 100; Vector3 sectionVector = (this.outpostCenter - hostilePosition).ToVector3(); sectionVector = sectionVector / sectionsNumber; // Default value if OutpostArea does not exist (should not occur, just a safety...). rallyPoint = (hostilePosition.ToVector3() + sectionVector * 0.2f * (float)sectionsNumber).ToIntVec3(); for (int i = 1; i <= sectionsNumber; i++) { Vector3 potentialRallyPoint = hostilePosition.ToVector3() + sectionVector * i; if ((outpostArea != null) && (outpostArea.ActiveCells.Contains(potentialRallyPoint.ToIntVec3()))) { // Ensure rallyPoint is completely inside the outpost area. rallyPoint = (potentialRallyPoint + sectionVector * 0.1f * (float)sectionsNumber).ToIntVec3(); break; } } } } else { Log.Message("UpdateLord6"); // Look for damaged turret to defend. Rot4 turretRotation = Rot4.Invalid; IntVec3 turretPosition = IntVec3.Invalid; FindDamagedTurret(out turretPosition, out turretRotation); if (turretPosition.IsValid) { if (OG_Util.IsModActive("MiningCo. ForceField")) { // Look for nearest force field to cover behind. foreach (Thing thing in Find.ListerThings.ThingsOfDef(OG_Util.ForceFieldGeneratorDef)) { if (thing.Position.InHorDistOf(turretPosition, 23f)) { rallyPoint = thing.Position + new IntVec3(0, 0, -2).RotatedBy(thing.Rotation); break; } } } else { // Just go near the attacked turret. rallyPoint = turretPosition + new IntVec3(0, 0, -5).RotatedBy(turretRotation); } } } Log.Message("UpdateLord7"); if (rallyPoint.IsValid) { Log.Message("rallyPoint = " + rallyPoint.ToString()); // Generate defense lord. LordJob_Joinable_DefendOutpost lordJob = new LordJob_Joinable_DefendOutpost(rallyPoint); LordMaker.MakeNewLord(OG_Util.FactionOfMiningCo, lordJob); SoundDef soundDef = SoundDefOf.MessageSeriousAlert; // TODO: add a siren like in alert speaker! soundDef.PlayOneShot(rallyPoint); // Stop all pawns job. foreach (Pawn pawn in Find.MapPawns.AllPawns) { if ((pawn.Faction != null) && (pawn.Faction == OG_Util.FactionOfMiningCo) && (pawn.kindDef != OG_Util.OutpostTechnicianDef)) { pawn.ClearMind(); } } } }