public void DoUpdateDef() { if (def != null) { return; } if (!DefDatabase <CustomRumorOfDef> .AllDefs.TryRandomElementByWeight(def => def.occureChance, out def)) { def = null; } //Log.Error("Count of defs: " + DefDatabase<CustomRumorOfDef>.AllDefs.Count()); }
//private void DoPawnAwayHandling() //{ // if (!Active) // return; // if (PawnsOutOfMap == null || PawnsOutOfMap.Count == 0) // return; // Dialog_RumorOf_Fight.CreateThisDialog(); //} private void DoPawnReturningHandling() { if (!Active) { return; } if (PawnsOutOfMap == null || PawnsOutOfMap.Count == 0) { return; } // Set the time the pawns will be gone, if not already set if (returnTicks < 0) { SetNewTravelTime(); } returnTicks--; if (returnTicks > 0) { return; } // returnTicks == 0 -> Do this: if (IncidentPoints < incidentPointsMin) { IncidentPoints = incidentPointsMin; } if (GatherSpotLister.activeSpots.Count > 0) { // Find a gather spot not inside a prison cell IntVec3 GatherSpotCell = IntVec3.Invalid; int n = 0; while (n < 25) { CompGatherSpot gs = GatherSpotLister.activeSpots.RandomElement(); GatherSpotCell = gs.parent.Position; if (GatherSpotCell.IsInPrisonCell()) { GatherSpotCell = IntVec3.Invalid; } if (GatherSpotCell != IntVec3.Invalid) { ReturnToBaseCell = GatherSpotCell; break; } n++; } } //// Nothing -> Try TradeDropSpot //if (ReturnToBaseCell == IntVec3.Invalid) // ReturnToBaseCell = RCellFinder.TradeDropSpot(); //Nothing -> Try Colonist Position if (ReturnToBaseCell == IntVec3.Invalid) { ReturnToBaseCell = Find.MapPawns.FreeColonistsSpawned.RandomElement().Position; } // Infos for return message bool treasureFound = false; Pawn treasureCarrier = null; bool enemyPursuit = false; List <Thing> treasures = new List <Thing>(); // Calculate a value of the weapons taken with them. Result may reduce the spawn enemy chance float weaponPoints = 0f; for (int i = 0; i < PawnsOutOfMap.Count; i++) { Pawn weaponPawn = PawnsOutOfMap[i] as Pawn; if (weaponPawn == null || weaponPawn.Destroyed || weaponPawn.Spawned) { continue; } weaponPoints += CalcVerbValue(weaponPawn.TryGetAttackVerb(true)); // Result: between 5 and 25 } // spawn the returning pawns float remainingPoints = IncidentPoints; if (GenDate.YearsPassed >= 2) { remainingPoints = remainingPoints * GenDate.YearsPassed / 3; } IntVec3 spawnCell; Predicate <IntVec3> predicateSpawnCell = (IntVec3 c) => { if (!c.Standable() || !c.CanReachColony()) { return(false); } return(true); }; bool allowTreasure = Rand.RangeInclusive(1, 100) < this.def.treasureChance; if (!allowTreasure) { remainingPoints = 0; } for (int i = 0; i < PawnsOutOfMap.Count; i++) { Pawn retPawn = PawnsOutOfMap[i] as Pawn; if (retPawn == null || retPawn.Destroyed || retPawn.Spawned) { continue; } CellFinder.TryFindRandomReachableCellNear(ExitMapCell, 13, TraverseParms.For(TraverseMode.PassDoors, Danger.Deadly, false), predicateSpawnCell, null, out spawnCell); retPawn = GenSpawn.Spawn(retPawn, spawnCell) as Pawn; if (retPawn == null) { continue; } Thing treasure = null; //Create the treasure (as long as there are points remaining) and carry it back if (remainingPoints > 0) { treasure = TryGetTreasure(); } // Treasure found: Add treasure to pawn and bring it to return cell if (treasure != null) { treasure = GenSpawn.Spawn(treasure, retPawn.Position); treasure.SetForbidden(true); IntVec3 deliverCell = CellFinder.RandomClosewalkCellNear(ReturnToBaseCell, 6); Job job = new Job(JobDefOf.HaulToCell, treasure, deliverCell); job.maxNumToCarry = treasure.stackCount; job.haulMode = HaulMode.ToCellNonStorage; job.ignoreForbidden = true; retPawn.jobs.StartJob(job); remainingPoints -= (treasure.MarketValue * treasure.stackCount / 10); //Log.Error("Remaining Points: " + remainingPoints.ToString()); // Infos for return message if (!treasureFound) { treasureCarrier = retPawn; } treasures.Add(treasure); treasureFound = true; } // No treasure? go to return cell else { IntVec3 returnCell = CellFinder.RandomClosewalkCellNear(ReturnToBaseCell, 6); retPawn.jobs.StartJob(new Job(JobDefOf.Goto, returnCell)); } } // Add a new found pawn Pawn pawnFound = null; if (def.pawnFoundChance > 0) { bool pawnFoundSuccess = Rand.RangeInclusive(0, 100) <= def.pawnFoundChance; if (pawnFoundSuccess && def.pawnFoundDefs != null && def.pawnFoundDefs.Count > 0) { Faction faction1 = Find.FactionManager.FirstFactionOfDef(FactionDefOf.Outlander); pawnFound = PawnGenerator.GeneratePawn(def.pawnFoundDefs.RandomElement(), faction1); /// Test-Equipment //ThingWithComps t = ThingMaker.MakeThing(DefDatabase<ThingDef>.GetNamed("Gun_Pistol")) as ThingWithComps; //pawnFound.equipment.AddEquipment(t); if (!def.pawnWithEquipment) { List <ThingWithComps> equipment = new List <ThingWithComps>(); foreach (ThingWithComps eq in pawnFound.equipment.AllEquipment) { equipment.Add(eq); } for (int i = equipment.Count; i > 0; i--) { ThingWithComps eq = equipment[i - 1]; if (!eq.def.IsApparel) { pawnFound.equipment.DestroyEquipment(eq); } } } CellFinder.TryFindRandomReachableCellNear(ExitMapCell, 10, TraverseParms.For(TraverseMode.NoPassClosedDoors), predicateSpawnCell, null, out spawnCell); GenSpawn.Spawn(pawnFound, spawnCell); pawnFound.SetFaction(Faction.OfPlayer); } } // Maybe start enemy spawn counter float randomValue = (float)Rand.RangeInclusive(0, 100); // If the weapons are good enough, reduce the chance for enemies (value * 2) if (weaponPoints * 3 > IncidentPoints / 10 ? true : false) { randomValue *= 2; } if ((treasureFound && randomValue < def.enemySpawnChanceTreasure) || (!treasureFound && randomValue < def.enemySpawnChanceNoTreasure)) { // Start enemy spawn ticks enemyPursuit = TryExecuteRaid(); } // Do returned letter handling LetterType letterType = LetterType.Good; StringBuilder returnMessage = new StringBuilder(); returnMessage.AppendLine(def.LetterMessage_ReturnedBaseVariable.Translate()).AppendLine(); if (treasureFound) { returnMessage.AppendLine(def.LetterMessage_ReturnedWithTreasureVariable.Translate()).AppendLine(); StringBuilder treasureString = new StringBuilder(); for (int i = 0; i < treasures.Count; i++) { //if (i > 0) // treasureString.Append(", "); //treasureString.Append(treasures[i].LabelCap); treasureString.AppendLine(treasures[i].LabelCap); } returnMessage.AppendLine(treasureString.ToString()); } else { returnMessage.AppendLine(def.LetterMessage_ReturnedNoTreasureVariable.Translate()); } if (pawnFound != null) { string message = def.LetterMessage_ReturnedWithNewColonistVariable.Translate(); returnMessage.AppendLine(message.AdjustedFor(pawnFound)); } if (enemyPursuit) { //returnMessage.AppendLine(); returnMessage.Append(def.LetterMessage_ReturnedWithEnemyVariable.Translate()); letterType = LetterType.BadNonUrgent; } // Show letter Letter letter = new Letter(def.LetterLabel_ReturnedVariable.Translate(), returnMessage.ToString(), letterType, treasureCarrier); Find.LetterStack.ReceiveLetter(letter, null); // Add letter to history //Find.History.AddHistoryRecordFromLetter(letter, null); // Reset Incident PawnsOutOfMap.Clear(); returnTicks = -1; autoDeactivateTicks = -1; Active = false; // Load random new def for next start def = null; }