public static IntVec3 FindNoWipeSpawnLocNear(IntVec3 near, Map map, ThingDef thingToSpawn, Rot4 rot, int maxDist = 2, Predicate <IntVec3> extraValidator = null) { int num = GenRadial.NumCellsInRadius(maxDist); IntVec3 result = IntVec3.Invalid; float num2 = 0f; for (int i = 0; i < num; i++) { IntVec3 intVec = near + GenRadial.RadialPattern[i]; if (!intVec.InBounds(map)) { continue; } CellRect cellRect = GenAdj.OccupiedRect(intVec, rot, thingToSpawn.size); if (!cellRect.InBounds(map) || !GenSight.LineOfSight(near, intVec, map, skipFirstCell: true) || (extraValidator != null && !extraValidator(intVec)) || (thingToSpawn.category == ThingCategory.Building && !GenConstruct.CanBuildOnTerrain(thingToSpawn, intVec, map, rot))) { continue; } bool flag = false; bool flag2 = false; tmpUniqueWipedThings.Clear(); foreach (IntVec3 item in cellRect) { if (item.Impassable(map)) { flag2 = true; } List <Thing> thingList = item.GetThingList(map); for (int j = 0; j < thingList.Count; j++) { if (thingList[j] is Pawn) { flag = true; } else if (GenSpawn.SpawningWipes(thingToSpawn, thingList[j].def) && !tmpUniqueWipedThings.Contains(thingList[j])) { tmpUniqueWipedThings.Add(thingList[j]); } } } if (flag && thingToSpawn.passability == Traversability.Impassable) { tmpUniqueWipedThings.Clear(); continue; } if (flag2 && thingToSpawn.category == ThingCategory.Item) { tmpUniqueWipedThings.Clear(); continue; } float num3 = 0f; for (int k = 0; k < tmpUniqueWipedThings.Count; k++) { if (tmpUniqueWipedThings[k].def.category == ThingCategory.Building && !tmpUniqueWipedThings[k].def.costList.NullOrEmpty() && tmpUniqueWipedThings[k].def.costStuffCount == 0) { List <ThingDefCountClass> list = tmpUniqueWipedThings[k].CostListAdjusted(); for (int l = 0; l < list.Count; l++) { num3 += list[l].thingDef.GetStatValueAbstract(StatDefOf.MarketValue) * (float)list[l].count * (float)tmpUniqueWipedThings[k].stackCount; } } else { num3 += tmpUniqueWipedThings[k].MarketValue * (float)tmpUniqueWipedThings[k].stackCount; } if (tmpUniqueWipedThings[k].def.category == ThingCategory.Building || tmpUniqueWipedThings[k].def.category == ThingCategory.Item) { num3 = Mathf.Max(num3, 0.001f); } } tmpUniqueWipedThings.Clear(); if (!result.IsValid || num3 < num2) { if (num3 == 0f) { return(intVec); } result = intVec; num2 = num3; } } if (!result.IsValid) { return(near); } return(result); }
public static IntVec3 FindNoWipeSpawnLocNear(IntVec3 near, Map map, ThingDef thingToSpawn, Rot4 rot, int maxDist = 2, Predicate <IntVec3> extraValidator = null) { int num = GenRadial.NumCellsInRadius((float)maxDist); IntVec3 intVec = IntVec3.Invalid; float num2 = 0f; for (int i = 0; i < num; i++) { IntVec3 intVec2 = near + GenRadial.RadialPattern[i]; if (intVec2.InBounds(map)) { CellRect cellRect = GenAdj.OccupiedRect(intVec2, rot, thingToSpawn.size); if (cellRect.InBounds(map)) { if (GenSight.LineOfSight(near, intVec2, map, true, null, 0, 0)) { if (extraValidator == null || extraValidator(intVec2)) { if (thingToSpawn.category != ThingCategory.Building || GenConstruct.CanBuildOnTerrain(thingToSpawn, intVec2, map, rot, null)) { bool flag = false; bool flag2 = false; CellFinder.tmpUniqueWipedThings.Clear(); CellRect.CellRectIterator iterator = cellRect.GetIterator(); while (!iterator.Done()) { if (iterator.Current.Impassable(map)) { flag2 = true; } List <Thing> thingList = iterator.Current.GetThingList(map); for (int j = 0; j < thingList.Count; j++) { if (thingList[j] is Pawn) { flag = true; } else if (GenSpawn.SpawningWipes(thingToSpawn, thingList[j].def) && !CellFinder.tmpUniqueWipedThings.Contains(thingList[j])) { CellFinder.tmpUniqueWipedThings.Add(thingList[j]); } } iterator.MoveNext(); } if (flag && thingToSpawn.passability == Traversability.Impassable) { CellFinder.tmpUniqueWipedThings.Clear(); } else if (flag2 && thingToSpawn.category == ThingCategory.Item) { CellFinder.tmpUniqueWipedThings.Clear(); } else { float num3 = 0f; for (int k = 0; k < CellFinder.tmpUniqueWipedThings.Count; k++) { if (CellFinder.tmpUniqueWipedThings[k].def.category == ThingCategory.Building && !CellFinder.tmpUniqueWipedThings[k].def.costList.NullOrEmpty <ThingDefCountClass>() && CellFinder.tmpUniqueWipedThings[k].def.costStuffCount == 0) { List <ThingDefCountClass> list = CellFinder.tmpUniqueWipedThings[k].CostListAdjusted(); for (int l = 0; l < list.Count; l++) { num3 += list[l].thingDef.GetStatValueAbstract(StatDefOf.MarketValue, null) * (float)list[l].count * (float)CellFinder.tmpUniqueWipedThings[k].stackCount; } } else { num3 += CellFinder.tmpUniqueWipedThings[k].MarketValue * (float)CellFinder.tmpUniqueWipedThings[k].stackCount; } if (CellFinder.tmpUniqueWipedThings[k].def.category == ThingCategory.Building || CellFinder.tmpUniqueWipedThings[k].def.category == ThingCategory.Item) { num3 = Mathf.Max(num3, 0.001f); } } CellFinder.tmpUniqueWipedThings.Clear(); if (!intVec.IsValid || num3 < num2) { if (num3 == 0f) { return(intVec2); } intVec = intVec2; num2 = num3; } } } } } } } } return((!intVec.IsValid) ? near : intVec); }
public static Thing Spawn(Thing newThing, IntVec3 loc, Map map, Rot4 rot, WipeMode wipeMode = WipeMode.Vanish, bool respawningAfterLoad = false) { if (map == null) { Log.Error("Tried to spawn " + newThing.ToStringSafe <Thing>() + " in a null map.", false); return(null); } if (!loc.InBounds(map)) { Log.Error(string.Concat(new object[] { "Tried to spawn ", newThing.ToStringSafe <Thing>(), " out of bounds at ", loc, "." }), false); return(null); } if (newThing.def.randomizeRotationOnSpawn) { rot = Rot4.Random; } CellRect occupiedRect = GenAdj.OccupiedRect(loc, rot, newThing.def.Size); if (!occupiedRect.InBounds(map)) { Log.Error(string.Concat(new object[] { "Tried to spawn ", newThing.ToStringSafe <Thing>(), " out of bounds at ", loc, " (out of bounds because size is ", newThing.def.Size, ")." }), false); return(null); } if (newThing.Spawned) { Log.Error("Tried to spawn " + newThing + " but it's already spawned.", false); return(newThing); } if (wipeMode == WipeMode.Vanish) { GenSpawn.WipeExistingThings(loc, rot, newThing.def, map, DestroyMode.Vanish); } else if (wipeMode == WipeMode.FullRefund) { GenSpawn.WipeAndRefundExistingThings(loc, rot, newThing.def, map); } if (newThing.def.category == ThingCategory.Item) { foreach (IntVec3 current in occupiedRect) { foreach (Thing current2 in current.GetThingList(map).ToList <Thing>()) { if (current2 != newThing) { if (current2.def.category == ThingCategory.Item) { current2.DeSpawn(DestroyMode.Vanish); if (!GenPlace.TryPlaceThing(current2, current, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x))) { current2.Destroy(DestroyMode.Vanish); } } } } } } newThing.Rotation = rot; newThing.Position = loc; if (newThing.holdingOwner != null) { newThing.holdingOwner.Remove(newThing); } newThing.SpawnSetup(map, respawningAfterLoad); if (newThing.Spawned && newThing.stackCount == 0) { Log.Error("Spawned thing with 0 stackCount: " + newThing, false); newThing.Destroy(DestroyMode.Vanish); return(null); } if (newThing.def.passability == Traversability.Impassable) { foreach (IntVec3 current3 in occupiedRect) { foreach (Thing current4 in current3.GetThingList(map).ToList <Thing>()) { if (current4 != newThing) { Pawn pawn = current4 as Pawn; if (pawn != null) { pawn.pather.TryRecoverFromUnwalkablePosition(false); } } } } } return(newThing); }
public static Thing Spawn(Thing newThing, IntVec3 loc, Map map, Rot4 rot, WipeMode wipeMode = WipeMode.Vanish, bool respawningAfterLoad = false) { if (map == null) { Log.Error("Tried to spawn " + newThing.ToStringSafe() + " in a null map."); return(null); } if (!loc.InBounds(map)) { Log.Error("Tried to spawn " + newThing.ToStringSafe() + " out of bounds at " + loc + "."); return(null); } if (newThing.def.randomizeRotationOnSpawn) { rot = Rot4.Random; } CellRect occupiedRect = GenAdj.OccupiedRect(loc, rot, newThing.def.Size); if (!occupiedRect.InBounds(map)) { Log.Error("Tried to spawn " + newThing.ToStringSafe() + " out of bounds at " + loc + " (out of bounds because size is " + newThing.def.Size + ")."); return(null); } if (newThing.Spawned) { Log.Error("Tried to spawn " + newThing + " but it's already spawned."); return(newThing); } switch (wipeMode) { case WipeMode.Vanish: WipeExistingThings(loc, rot, newThing.def, map, DestroyMode.Vanish); break; case WipeMode.FullRefund: WipeAndRefundExistingThings(loc, rot, newThing.def, map); break; } if (newThing.def.category == ThingCategory.Item) { foreach (IntVec3 item in occupiedRect) { foreach (Thing item2 in item.GetThingList(map).ToList()) { if (item2 != newThing && item2.def.category == ThingCategory.Item) { item2.DeSpawn(); if (!GenPlace.TryPlaceThing(item2, item, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x))) { item2.Destroy(); } } } } } newThing.Rotation = rot; newThing.Position = loc; if (newThing.holdingOwner != null) { newThing.holdingOwner.Remove(newThing); } newThing.SpawnSetup(map, respawningAfterLoad); if (newThing.Spawned && newThing.stackCount == 0) { Log.Error("Spawned thing with 0 stackCount: " + newThing); newThing.Destroy(); return(null); } if (newThing.def.passability == Traversability.Impassable) { foreach (IntVec3 item3 in occupiedRect) { foreach (Thing item4 in item3.GetThingList(map).ToList()) { if (item4 != newThing) { (item4 as Pawn)?.pather.TryRecoverFromUnwalkablePosition(error: false); } } } return(newThing); } return(newThing); }