Advertisement
Guest User

Rimworld Modding

a guest
May 11th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.15 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Lost_RD
  4.  * Date: 11/05/2016
  5.  * Time: 5:36 PM
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9.  
  10. using Verse;
  11. using RimWorld;
  12. using System;
  13. using System.Linq;
  14.  
  15. namespace BuildWallsOnRoughStone
  16. {
  17.     [StaticConstructorOnStartup]
  18.     public static class GenSpawn
  19.     {
  20.        
  21.         /*
  22.         public static Thing Spawn(ThingDef def, IntVec3 loc)
  23.         {
  24.             return GenSpawn.Spawn(ThingMaker.MakeThing(def, null), loc);
  25.         }
  26.  
  27.         public static Thing Spawn(Thing newThing, IntVec3 loc)
  28.         {
  29.             return GenSpawn.Spawn(newThing, loc, Rot4.North);
  30.         }
  31.  
  32.         public static Thing Spawn(Thing newThing, IntVec3 loc, Rot4 rot)
  33.         {
  34.             if (!loc.InBounds())
  35.             {
  36.                 Log.Error(string.Concat(new object[]
  37.                 {
  38.                     "Tried to spawn ",
  39.                     newThing,
  40.                     " out of bounds at ",
  41.                     loc,
  42.                     "."
  43.                 }));
  44.                 return null;
  45.             }
  46.             GenSpawn.WipeExistingThings(loc, rot, newThing.def, false);
  47.             if (newThing.def.randomizeRotationOnSpawn)
  48.             {
  49.                 newThing.Rotation = Rot4.Random;
  50.             }
  51.             else
  52.             {
  53.                 newThing.Rotation = rot;
  54.             }
  55.             newThing.SetPositionDirect(IntVec3.Invalid);
  56.             newThing.Position = loc;
  57.             newThing.SpawnSetup();
  58.             if (newThing.stackCount == 0)
  59.             {
  60.                 Log.Error("Spawned thing with 0 stackCount: " + newThing);
  61.                 newThing.Destroy(DestroyMode.Vanish);
  62.                 return null;
  63.             }
  64.             return newThing;
  65.         }
  66.         */
  67.  
  68.         public static BuildableDef BuiltDefOf(ThingDef def)
  69.         {
  70.             return (def.entityDefToBuild == null) ? def : def.entityDefToBuild;
  71.         }
  72.  
  73.         public static bool CanPlaceBlueprintOver(BuildableDef newDef, ThingDef oldDef)
  74.         {
  75.             // true if the old building is haulable
  76.             if (oldDef.EverHaulable)
  77.             {
  78.                 return true;
  79.             }
  80.             // return geothermal if the old building is a geyser
  81.             if (oldDef == ThingDefOf.SteamGeyser)
  82.             {
  83.                 return newDef == ThingDefOf.GeothermalGenerator;
  84.             }
  85.             return true;
  86.             ThingDef thingDef = newDef as ThingDef;
  87.             BuildableDef buildableDef = GenSpawn.BuiltDefOf(oldDef);
  88.             ThingDef thingDef2 = buildableDef as ThingDef;
  89.             // false if old building is an impassable plant and the new building can't be placed on impassable plants
  90.             if (oldDef.category == ThingCategory.Plant && oldDef.passability == Traversability.Impassable && thingDef != null && thingDef.category == ThingCategory.Building && !thingDef.building.canPlaceOverImpassablePlant)
  91.             {
  92.                 return false;
  93.             }
  94.             // if old building is a building or a blueprint or an "unspecified building frame"
  95.             if (oldDef.category == ThingCategory.Building || oldDef.IsBlueprint || oldDef.IsFrame)
  96.             {
  97.                 // if the new building is not null (very sensible start)
  98.                 if (thingDef != null)
  99.                 {
  100.                     // if new building is not an edifice - i.e. is a power conduit
  101.                     // return true if no old building or can build conduit under AND either the old or new building does not transmit power
  102.                     // this is the place conduits under things case
  103.                     if (!thingDef.IsEdifice())
  104.                     {
  105.                         return (oldDef.building == null || oldDef.building.canBuildNonEdificesUnder) && (!thingDef.EverTransmitsPower || !oldDef.EverTransmitsPower);
  106.                     }
  107.                     // if new building is not a conduit and old building is a conduit
  108.                     // return true if conduits can exist under new building
  109.                     // this is the place things on conduits case
  110.                     if (thingDef.IsEdifice() && oldDef != null && oldDef.category == ThingCategory.Building && !oldDef.IsEdifice())
  111.                     {
  112.                         return thingDef.building == null || thingDef.building.canBuildNonEdificesUnder;
  113.                     }
  114.                     // return true if old building exists and is a wall and new building exists and can be placed over a wall
  115.                     // this is the door case
  116.                     if (thingDef2 != null && thingDef2 == ThingDefOf.Wall && thingDef.building != null && thingDef.building.canPlaceOverWall)
  117.                     {
  118.                         return true;
  119.                     }
  120.                     // return true if new building is not a conduit and old building is a conduit
  121.                     // this is another place things on conduits case
  122.                     if (newDef != ThingDefOf.PowerConduit && buildableDef == ThingDefOf.PowerConduit)
  123.                     {
  124.                         return true;
  125.                     }
  126.                 }
  127.                 // return true if placing a floor and existing structure coexists with floors or is already a floor
  128.                 return (newDef is TerrainDef && buildableDef is ThingDef && ((ThingDef)buildableDef).CoexistsWithFloors) || (buildableDef is TerrainDef && !(newDef is TerrainDef));
  129.             }
  130.             // return true if all the other shit is not relevant
  131.             return true;
  132.         }
  133.  
  134.         /*
  135.         public static void WipeExistingThings(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, bool reclaimResources)
  136.         {
  137.             GenSpawn.WipeExistingThings(thingPos, thingRot, thingDef, reclaimResources, null);
  138.         }
  139.  
  140.         public static void WipeExistingThings(IntVec3 thingPos, Rot4 thingRot, BuildableDef thingDef, bool reclaimResources, CellRect? leavingsRect)
  141.         {
  142.             foreach (IntVec3 current in GenAdj.CellsOccupiedBy(thingPos, thingRot, thingDef.Size))
  143.             {
  144.                 foreach (Thing current2 in Find.ThingGrid.ThingsAt(current).ToList<Thing>())
  145.                 {
  146.                     if (GenSpawn.SpawningWipes(thingDef, current2.def))
  147.                     {
  148.                         if (reclaimResources && current2 is Building)
  149.                         {
  150.                             CellRect leavingsRect2 = current2.OccupiedRect();
  151.                             if (leavingsRect.HasValue)
  152.                             {
  153.                                 leavingsRect2 = leavingsRect.Value;
  154.                             }
  155.                             GenLeaving.DoLeavingsFor(current2, DestroyMode.Deconstruct, leavingsRect2);
  156.                         }
  157.                         DestroyMode mode = (!reclaimResources) ? DestroyMode.Vanish : DestroyMode.Cancel;
  158.                         current2.Destroy(mode);
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.  
  164.         public static bool SpawningWipes(BuildableDef newEntDef, BuildableDef oldEntDef)
  165.         {
  166.             ThingDef thingDef = newEntDef as ThingDef;
  167.             ThingDef thingDef2 = oldEntDef as ThingDef;
  168.             if (thingDef == null || thingDef2 == null)
  169.             {
  170.                 return false;
  171.             }
  172.             if (thingDef.category == ThingCategory.Attachment || thingDef.category == ThingCategory.Mote || thingDef.category == ThingCategory.Filth || thingDef.category == ThingCategory.Projectile)
  173.             {
  174.                 return false;
  175.             }
  176.             if (!thingDef2.destroyable)
  177.             {
  178.                 return false;
  179.             }
  180.             if (thingDef.category == ThingCategory.Plant)
  181.             {
  182.                 return false;
  183.             }
  184.             if (thingDef2.category == ThingCategory.Filth && thingDef.passability != Traversability.Standable)
  185.             {
  186.                 return true;
  187.             }
  188.             if (thingDef.EverTransmitsPower && thingDef2 == ThingDefOf.PowerConduit)
  189.             {
  190.                 return true;
  191.             }
  192.             if (thingDef.IsFrame && GenSpawn.SpawningWipes(thingDef.entityDefToBuild, oldEntDef))
  193.             {
  194.                 return true;
  195.             }
  196.             BuildableDef buildableDef = GenSpawn.BuiltDefOf(thingDef);
  197.             BuildableDef buildableDef2 = GenSpawn.BuiltDefOf(thingDef2);
  198.             if (buildableDef == null || buildableDef2 == null)
  199.             {
  200.                 return false;
  201.             }
  202.             ThingDef thingDef3 = thingDef.entityDefToBuild as ThingDef;
  203.             if (thingDef2.IsBlueprint)
  204.             {
  205.                 if (thingDef.IsBlueprint)
  206.                 {
  207.                     if (thingDef3 != null && thingDef3.building != null && thingDef3.building.canPlaceOverWall && thingDef2.entityDefToBuild is ThingDef && (ThingDef)thingDef2.entityDefToBuild == ThingDefOf.Wall)
  208.                     {
  209.                         return true;
  210.                     }
  211.                     if (thingDef2.entityDefToBuild is TerrainDef)
  212.                     {
  213.                         if (thingDef.entityDefToBuild is ThingDef && ((ThingDef)thingDef.entityDefToBuild).coversFloor)
  214.                         {
  215.                             return true;
  216.                         }
  217.                         if (thingDef.entityDefToBuild is TerrainDef)
  218.                         {
  219.                             return true;
  220.                         }
  221.                     }
  222.                 }
  223.                 return thingDef2.entityDefToBuild == ThingDefOf.PowerConduit && thingDef.entityDefToBuild is ThingDef && (thingDef.entityDefToBuild as ThingDef).EverTransmitsPower;
  224.             }
  225.             if ((thingDef2.IsFrame || thingDef2.IsBlueprint) && thingDef2.entityDefToBuild is TerrainDef)
  226.             {
  227.                 ThingDef thingDef4 = buildableDef as ThingDef;
  228.                 if (thingDef4 != null && !thingDef4.CoexistsWithFloors)
  229.                 {
  230.                     return true;
  231.                 }
  232.             }
  233.             if (thingDef2 == ThingDefOf.DropPod)
  234.             {
  235.                 return false;
  236.             }
  237.             if (thingDef == ThingDefOf.DropPod)
  238.             {
  239.                 return thingDef2 != ThingDefOf.DropPod && (thingDef2.category == ThingCategory.Building && thingDef2.passability == Traversability.Impassable);
  240.             }
  241.             if (thingDef.IsEdifice())
  242.             {
  243.                 if (thingDef.BlockPlanting && thingDef2.category == ThingCategory.Plant)
  244.                 {
  245.                     return true;
  246.                 }
  247.                 if (!(buildableDef is TerrainDef) && buildableDef2.IsEdifice())
  248.                 {
  249.                     return true;
  250.                 }
  251.             }
  252.             return false;
  253.         }
  254.  
  255.         public static bool BlocksFramePlacement(Blueprint blue, Thing t)
  256.         {
  257.             if (t.def.category == ThingCategory.Plant)
  258.             {
  259.                 return t.def.plant.harvestWork > 200f;
  260.             }
  261.             if (blue.def.entityDefToBuild is TerrainDef || blue.def.entityDefToBuild.passability == Traversability.Standable)
  262.             {
  263.                 return false;
  264.             }
  265.             if (blue.def.entityDefToBuild == ThingDefOf.GeothermalGenerator && t.def == ThingDefOf.SteamGeyser)
  266.             {
  267.                 return false;
  268.             }
  269.             ThingDef thingDef = blue.def.entityDefToBuild as ThingDef;
  270.             if (thingDef != null)
  271.             {
  272.                 if (thingDef.EverTransmitsPower && t.def == ThingDefOf.PowerConduit && thingDef != ThingDefOf.PowerConduit)
  273.                 {
  274.                     return false;
  275.                 }
  276.                 if (t.def == ThingDefOf.Wall && thingDef.building != null && thingDef.building.canPlaceOverWall)
  277.                 {
  278.                     return false;
  279.                 }
  280.             }
  281.             return (t.def.IsEdifice() && thingDef.IsEdifice()) || (t.def.category == ThingCategory.Pawn || (t.def.category == ThingCategory.Item && blue.def.entityDefToBuild.passability == Traversability.Impassable)) || (t.def.Fillage >= FillCategory.Partial && thingDef != null && thingDef.Fillage >= FillCategory.Partial);
  282.         }
  283.         */
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement