public static void WipeAndRefundExistingThings(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, Map map) { CellRect occupiedRect = GenAdj.OccupiedRect(thingPos, thingRot, thingDef.Size); foreach (IntVec3 current in occupiedRect) { foreach (Thing current2 in current.GetThingList(map).ToList <Thing>()) { if (GenSpawn.SpawningWipes(thingDef, current2.def)) { 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); } } else { GenSpawn.Refund(current2, map, occupiedRect); } } } } }
public static void WipeAndRefundExistingThings(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, Map map) { CellRect occupiedRect = GenAdj.OccupiedRect(thingPos, thingRot, thingDef.Size); foreach (IntVec3 item in occupiedRect) { foreach (Thing item2 in item.GetThingList(map).ToList()) { if (SpawningWipes(thingDef, item2.def)) { if (item2.def.category == ThingCategory.Item) { item2.DeSpawn(); if (!GenPlace.TryPlaceThing(item2, item, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x))) { item2.Destroy(); } } else { Refund(item2, map, occupiedRect); } } } } }
public static bool TryFindSkyfallerCell(ThingDef skyfaller, Map map, out IntVec3 cell, int minDistToEdge = 10, IntVec3 nearLoc = default(IntVec3), int nearLocMaxDist = -1, bool allowRoofedCells = true, bool allowCellsWithItems = false, bool allowCellsWithBuildings = false, bool colonyReachable = false, bool avoidColonistsIfExplosive = true, bool alwaysAvoidColonists = false, Predicate <IntVec3> extraValidator = null) { bool avoidColonists = (avoidColonistsIfExplosive && skyfaller.skyfaller.CausesExplosion) || alwaysAvoidColonists; Predicate <IntVec3> validator = delegate(IntVec3 x) { foreach (IntVec3 item in GenAdj.OccupiedRect(x, Rot4.North, skyfaller.size)) { if (!item.InBounds(map) || item.Fogged(map) || !item.Standable(map) || (item.Roofed(map) && item.GetRoof(map).isThickRoof)) { return(false); } if (!allowRoofedCells && item.Roofed(map)) { return(false); } if (!allowCellsWithItems && item.GetFirstItem(map) != null) { return(false); } if (!allowCellsWithBuildings && item.GetFirstBuilding(map) != null) { return(false); } if (item.GetFirstSkyfaller(map) != null) { return(false); } foreach (Thing thing in item.GetThingList(map)) { if (thing.def.preventSkyfallersLandingOn) { return(false); } } } if (avoidColonists && SkyfallerUtility.CanPossiblyFallOnColonist(skyfaller, x, map)) { return(false); } if (minDistToEdge > 0 && x.DistanceToEdge(map) < minDistToEdge) { return(false); } if (colonyReachable && !map.reachability.CanReachColony(x)) { return(false); } return((extraValidator == null || extraValidator(x)) ? true : false); }; if (nearLocMaxDist > 0) { return(CellFinder.TryFindRandomCellNear(nearLoc, map, nearLocMaxDist, validator, out cell)); } return(TryFindRandomNotEdgeCellWith(minDistToEdge, validator, map, out cell)); }
public static bool TryFindSkyfallerCell(ThingDef skyfaller, Map map, out IntVec3 cell, int minDistToEdge = 10, IntVec3 nearLoc = default(IntVec3), int nearLocMaxDist = -1, bool allowRoofedCells = true, bool allowCellsWithItems = false, bool allowCellsWithBuildings = false, bool colonyReachable = false, bool avoidColonistsIfExplosive = true, bool alwaysAvoidColonists = false, Predicate <IntVec3> extraValidator = null) { bool avoidColonists = (avoidColonistsIfExplosive && skyfaller.skyfaller.CausesExplosion) || alwaysAvoidColonists; Predicate <IntVec3> validator = delegate(IntVec3 x) { CellRect.CellRectIterator iterator = GenAdj.OccupiedRect(x, Rot4.North, skyfaller.size).GetIterator(); while (!iterator.Done()) { IntVec3 current = iterator.Current; if (!current.InBounds(map) || current.Fogged(map) || !current.Standable(map) || (current.Roofed(map) && current.GetRoof(map).isThickRoof)) { return(false); } if (!allowRoofedCells && current.Roofed(map)) { return(false); } if (!allowCellsWithItems && current.GetFirstItem(map) != null) { return(false); } if (!allowCellsWithBuildings && current.GetFirstBuilding(map) != null) { return(false); } if (current.GetFirstSkyfaller(map) != null) { return(false); } iterator.MoveNext(); } if (avoidColonists && SkyfallerUtility.CanPossiblyFallOnColonist(skyfaller, x, map)) { return(false); } if (minDistToEdge > 0 && x.DistanceToEdge(map) < minDistToEdge) { return(false); } if (colonyReachable && !map.reachability.CanReachColony(x)) { return(false); } if (extraValidator != null && !extraValidator(x)) { return(false); } return(true); }; if (nearLocMaxDist > 0) { return(CellFinder.TryFindRandomCellNear(nearLoc, map, nearLocMaxDist, validator, out cell)); } return(TryFindRandomNotEdgeCellWith(minDistToEdge, validator, map, out cell)); }
private bool IsRotationValid(IntVec3 loc, Rot4 rot, Map map) { if (!GenAdj.OccupiedRect(loc, rot, thingDef.size).InBounds(map)) { return(false); } if (GenSpawn.WouldWipeAnythingWith(loc, rot, thingDef, map, (Thing x) => x.def == thingDef || (x.def.category != ThingCategory.Plant && x.def.category != ThingCategory.Filth))) { return(false); } return(true); }
public static bool TryFindSkyfallerCell(ThingDef skyfaller, Map map, out IntVec3 cell, int minDistToEdge = 10, IntVec3 nearLoc = default(IntVec3), int nearLocMaxDist = -1, bool allowRoofedCells = true, bool allowCellsWithItems = false, bool allowCellsWithBuildings = false, bool colonyReachable = false, bool avoidColonistsIfExplosive = true, bool alwaysAvoidColonists = false, Predicate <IntVec3> extraValidator = null) { bool avoidColonists = (avoidColonistsIfExplosive && skyfaller.skyfaller.CausesExplosion) || alwaysAvoidColonists; Predicate <IntVec3> validator = delegate(IntVec3 x) { CellRect.CellRectIterator iterator = GenAdj.OccupiedRect(x, Rot4.North, skyfaller.size).GetIterator(); while (!iterator.Done()) { IntVec3 c = iterator.Current; bool result2; if (!c.InBounds(map) || c.Fogged(map) || !c.Standable(map) || (c.Roofed(map) && c.GetRoof(map).isThickRoof)) { result2 = false; } else if (!allowRoofedCells && c.Roofed(map)) { result2 = false; } else if (!allowCellsWithItems && c.GetFirstItem(map) != null) { result2 = false; } else if (!allowCellsWithBuildings && c.GetFirstBuilding(map) != null) { result2 = false; } else { if (c.GetFirstSkyfaller(map) == null) { iterator.MoveNext(); continue; } result2 = false; } return(result2); } return((!avoidColonists || !SkyfallerUtility.CanPossiblyFallOnColonist(skyfaller, x, map)) && (minDistToEdge <= 0 || x.DistanceToEdge(map) >= minDistToEdge) && (!colonyReachable || map.reachability.CanReachColony(x)) && (extraValidator == null || extraValidator(x))); }; bool result; if (nearLocMaxDist > 0) { result = CellFinder.TryFindRandomCellNear(nearLoc, map, nearLocMaxDist, validator, out cell, -1); } else { result = CellFinderLoose.TryFindRandomNotEdgeCellWith(minDistToEdge, validator, map, out cell); } return(result); }
public static void CheckMoveItemsAside(IntVec3 thingPos, Rot4 thingRot, ThingDef thingDef, Map map) { if (thingDef.surfaceType == SurfaceType.None && thingDef.passability != 0) { CellRect occupiedRect = GenAdj.OccupiedRect(thingPos, thingRot, thingDef.Size); foreach (IntVec3 item in occupiedRect) { foreach (Thing item2 in item.GetThingList(map).ToList()) { if (item2.def.category == ThingCategory.Item) { item2.DeSpawn(); if (!GenPlace.TryPlaceThing(item2, item, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x))) { item2.Destroy(); } } } } } }
public static bool TryFindSkyfallerCell(ThingDef skyfaller, Map map, out IntVec3 cell, int minDistToEdge = 10, IntVec3 nearLoc = default(IntVec3), int nearLocMaxDist = -1, bool allowRoofedCells = true, bool allowCellsWithItems = false, bool allowCellsWithBuildings = false, bool colonyReachable = false, Predicate <IntVec3> extraValidator = null) { Predicate <IntVec3> validator = delegate(IntVec3 x) { CellRect.CellRectIterator iterator = GenAdj.OccupiedRect(x, Rot4.North, skyfaller.size).GetIterator(); while (!iterator.Done()) { IntVec3 current = iterator.Current; if (!current.InBounds(map) || current.Fogged(map) || !current.Standable(map) || (current.Roofed(map) && current.GetRoof(map).isThickRoof)) { return(false); } if (!allowRoofedCells && current.Roofed(map)) { return(false); } if (!allowCellsWithItems && current.GetFirstItem(map) != null) { return(false); } if (!allowCellsWithBuildings && current.GetFirstBuilding(map) != null) { return(false); } if (current.GetFirstSkyfaller(map) != null) { return(false); } iterator.MoveNext(); } return((minDistToEdge <= 0 || x.DistanceToEdge(map) >= minDistToEdge) && (!colonyReachable || map.reachability.CanReachColony(x)) && (extraValidator == null || extraValidator(x))); }; if (nearLocMaxDist > 0) { return(CellFinder.TryFindRandomCellNear(nearLoc, map, nearLocMaxDist, validator, out cell)); } return(CellFinderLoose.TryFindRandomNotEdgeCellWith(minDistToEdge, validator, map, out cell)); }
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 InteractionCellWhenAt(ThingDef def, IntVec3 center, Rot4 rot, Map map) { if (def.hasInteractionCell) { IntVec3 b = def.interactionCellOffset.RotatedBy(rot); return(center + b); } if (def.Size.x == 1 && def.Size.z == 1) { for (int i = 0; i < 8; i++) { IntVec3 intVec = center + GenAdj.AdjacentCells[i]; if (intVec.Standable(map) && intVec.GetDoor(map) == null && ReachabilityImmediate.CanReachImmediate(intVec, center, map, PathEndMode.Touch, null)) { return(intVec); } } for (int j = 0; j < 8; j++) { IntVec3 intVec2 = center + GenAdj.AdjacentCells[j]; if (intVec2.Standable(map) && ReachabilityImmediate.CanReachImmediate(intVec2, center, map, PathEndMode.Touch, null)) { return(intVec2); } } for (int k = 0; k < 8; k++) { IntVec3 intVec3 = center + GenAdj.AdjacentCells[k]; if (intVec3.Walkable(map) && ReachabilityImmediate.CanReachImmediate(intVec3, center, map, PathEndMode.Touch, null)) { return(intVec3); } } return(center); } List <IntVec3> list = GenAdjFast.AdjacentCells8Way(center, rot, def.size); CellRect rect = GenAdj.OccupiedRect(center, rot, def.size); for (int l = 0; l < list.Count; l++) { if (list[l].Standable(map) && list[l].GetDoor(map) == null && ReachabilityImmediate.CanReachImmediate(list[l], rect, map, PathEndMode.Touch, null)) { return(list[l]); } } for (int m = 0; m < list.Count; m++) { if (list[m].Standable(map) && ReachabilityImmediate.CanReachImmediate(list[m], rect, map, PathEndMode.Touch, null)) { return(list[m]); } } for (int n = 0; n < list.Count; n++) { if (list[n].Walkable(map) && ReachabilityImmediate.CanReachImmediate(list[n], rect, map, PathEndMode.Touch, null)) { return(list[n]); } } return(center); }
public static bool WouldWipeAnythingWith(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, Map map, Predicate <Thing> predicate) { return(GenSpawn.WouldWipeAnythingWith(GenAdj.OccupiedRect(thingPos, thingRot, thingDef.Size), thingDef, map, predicate)); }
public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish) { var map = Map; // before DeSpawn! base.DeSpawn(mode); if (def.IsEdifice()) { map.edificeGrid.DeRegister(this); } if (mode != DestroyMode.WillReplace) { if (def.MakeFog) { map.fogGrid.Notify_FogBlockerRemoved(Position); } if (def.holdsRoof) { RoofCollapseCellsFinder.Notify_RoofHolderDespawned(this, map); } if (def.IsSmoothable) { SmoothSurfaceDesignatorUtility.Notify_BuildingDespawned(this, map); } } if (sustainerAmbient != null) { sustainerAmbient.End(); } CellRect occRect = GenAdj.OccupiedRect(this); for (int z = occRect.minZ; z <= occRect.maxZ; z++) { for (int x = occRect.minX; x <= occRect.maxX; x++) { IntVec3 c = new IntVec3(x, 0, z); MapMeshFlag changeType = MapMeshFlag.Buildings; if (def.coversFloor) { changeType |= MapMeshFlag.Terrain; } if (def.Fillage == FillCategory.Full) { changeType |= MapMeshFlag.Roofs; changeType |= MapMeshFlag.Snow; } map.mapDrawer.MapMeshDirty(c, changeType); map.glowGrid.MarkGlowGridDirty(c); } } map.listerBuildings.Remove(this); map.listerBuildingsRepairable.Notify_BuildingDeSpawned(this); if (def.building.leaveTerrain != null && Current.ProgramState == ProgramState.Playing && canChangeTerrainOnDestroyed) { for (var cri = GenAdj.OccupiedRect(this).GetIterator(); !cri.Done(); cri.MoveNext()) { map.terrainGrid.SetTerrain(cri.Current, def.building.leaveTerrain); } } //Mining, planning, etc map.designationManager.Notify_BuildingDespawned(this); if (!this.CanBeSeenOver()) { map.exitMapGrid.Notify_LOSBlockerDespawned(); } if (def.building.hasFuelingPort) { var fuelingPortCell = FuelingPortUtility.GetFuelingPortCell(Position, Rotation); var launchable = FuelingPortUtility.LaunchableAt(fuelingPortCell, map); if (launchable != null) { launchable.Notify_FuelingPortSourceDeSpawned(); } } //Must go after removing from buildings list map.avoidGrid.Notify_BuildingDespawned(this); }
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 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 CellRect OccupiedRect(this Thing t) { return(GenAdj.OccupiedRect(t.Position, t.Rotation, t.def.size)); }
private static PlaceSpotQuality PlaceSpotQualityAt(IntVec3 c, Rot4 rot, Map map, Thing thing, IntVec3 center, bool allowStacking, Predicate <IntVec3> extraValidator = null) { if (!c.InBounds(map) || !c.Walkable(map)) { return(PlaceSpotQuality.Unusable); } if (!GenAdj.OccupiedRect(c, rot, thing.def.Size).InBounds(map)) { return(PlaceSpotQuality.Unusable); } if (extraValidator != null && !extraValidator(c)) { return(PlaceSpotQuality.Unusable); } List <Thing> list = map.thingGrid.ThingsListAt(c); for (int i = 0; i < list.Count; i++) { Thing thing2 = list[i]; if (thing.def.saveCompressible && thing2.def.saveCompressible) { return(PlaceSpotQuality.Unusable); } if (thing.def.category == ThingCategory.Item && thing2.def.category == ThingCategory.Item && (!thing2.CanStackWith(thing) || thing2.stackCount >= thing.def.stackLimit)) { return(PlaceSpotQuality.Unusable); } } if (thing is Building) { foreach (IntVec3 item in GenAdj.OccupiedRect(c, rot, thing.def.size)) { Building edifice = item.GetEdifice(map); if (edifice != null && GenSpawn.SpawningWipes(thing.def, edifice.def)) { return(PlaceSpotQuality.Awful); } } } if (c.GetRoom(map) != center.GetRoom(map)) { if (!map.reachability.CanReach(center, c, PathEndMode.OnCell, TraverseMode.PassDoors, Danger.Deadly)) { return(PlaceSpotQuality.Awful); } return(PlaceSpotQuality.Bad); } if (allowStacking) { for (int j = 0; j < list.Count; j++) { Thing thing3 = list[j]; if (thing3.def.category == ThingCategory.Item && thing3.CanStackWith(thing) && thing3.stackCount < thing.def.stackLimit) { return(PlaceSpotQuality.Perfect); } } } bool flag = (thing as Pawn)?.Downed ?? false; PlaceSpotQuality placeSpotQuality = PlaceSpotQuality.Perfect; for (int k = 0; k < list.Count; k++) { Thing thing4 = list[k]; if (thing4.def.IsDoor) { return(PlaceSpotQuality.Bad); } if (thing4 is Building_WorkTable) { return(PlaceSpotQuality.Bad); } Pawn pawn = thing4 as Pawn; if (pawn != null) { if (pawn.Downed | flag) { return(PlaceSpotQuality.Bad); } if ((int)placeSpotQuality > 3) { placeSpotQuality = PlaceSpotQuality.Okay; } } if (thing4.def.category == ThingCategory.Plant && thing4.def.selectable && (int)placeSpotQuality > 3) { placeSpotQuality = PlaceSpotQuality.Okay; } } return(placeSpotQuality); }
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); }