public static bool TrySpawnThing(this HediffComp_RandySpawnUponDeath comp, Thing thing, int randomQuantity) { Map map = thing.Map; if (comp.Props.spawnMaxAdjacent >= 0) { int num = 0; for (int i = 0; i < 9; i++) { IntVec3 curCell = thing.Position + GenAdj.AdjacentCellsAndInside[i]; if (!curCell.InBounds(map)) { continue; } List <Thing> thingList = (curCell).GetThingList(map); for (int j = 0; j < thingList.Count; j++) { if (thingList[j].def == comp.ChosenItem.thingToSpawn) { num += thingList[j].stackCount; if (num >= comp.Props.spawnMaxAdjacent) { return(false); } } } } } int numSpawned = 0; int remainingSpawnCount = randomQuantity; int loopBreaker = 0; while (numSpawned < randomQuantity) { if (comp.TryFindSpawnCell(thing, randomQuantity, map, out IntVec3 center)) { Thing newThing = ThingMaker.MakeThing(comp.ChosenItem.thingToSpawn, null); newThing.stackCount = remainingSpawnCount; if (newThing.def.stackLimit > 0) { if (newThing.stackCount > newThing.def.stackLimit) { newThing.stackCount = newThing.def.stackLimit; } } numSpawned += newThing.stackCount; remainingSpawnCount -= newThing.stackCount; GenPlace.TryPlaceThing(newThing, center, map, ThingPlaceMode.Direct, out Thing t, null); if (comp.Props.spawnForbidden) { t.SetForbidden(true, true); } } if (loopBreaker++ > 10) { if (comp.MyDebug) { Log.Warning("Had to break the loop"); } return(false); } } if (remainingSpawnCount <= 0) { return(true); } return(false); }