Glyphical

Untitled

Jul 26th, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. oh the math --
  2. -- Properties
  3. --
  4.  
  5. PlaceWindowsBaricades = {};
  6. ErosionSpeedValues = {20, 50, 100, 200, 500}
  7. TimeSinceApoValues = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360}
  8. CurrentErosionPercentage = 0
  9. CurrentWorldAgeDays = 0
  10.  
  11. -- Methods
  12.  
  13. function PlaceWindowsBaricades.loadGridsquare(sq)
  14.  
  15. if isClient() == false then
  16.  
  17. for i=0,sq:getObjects():size()-1 do
  18.  
  19. -- Searching for windows
  20. local tileIsoObject = sq:getObjects():get(i);
  21.  
  22. if tileIsoObject then
  23.  
  24. if instanceof(tileIsoObject, "IsoWindow") then
  25.  
  26. local modData = tileIsoObject:getModData()
  27.  
  28. -- If the window have not been loaded for the past 30 days, retry Barricaded World code
  29. if (not modData["BarricadedWorld:isDefinitiveParsed"]) and (not modData["BarricadedWorld:isPlayerPlaced"])
  30. and (not modData["BarricadedWorld:parsedDate"] or modData["BarricadedWorld:parsedDate"] + 30 < CurrentWorldAgeDays) then
  31.  
  32. local coords = { x = sq:getX(), y = sq:getY(), z = sq:getZ() }
  33. object = PlaceWindowsBaricades.getBarricadeAble(coords.x, coords.y, coords.z, tileIsoObject:getObjectIndex());
  34.  
  35. -- The more advanced the erosion, the more chances the following code has to happen.
  36. -- 25% of current erosion advancement means 25% chance for a windows to go through the Barricaded World code.
  37. if object and (ZombRand(0,100) < CurrentErosionPercentage) then
  38.  
  39. if ZombRand(0,100) >= 40 then
  40. object:smashWindow()
  41. end
  42.  
  43. if coords.z == 0 then
  44. local args = {}
  45. local randomBaricadeLocation = ZombRand(0,4)
  46. local barricadeLocation = true
  47.  
  48. if (randomBaricadeLocation > 2) then
  49. barricadeLocation = false
  50. end
  51.  
  52. local randomBaricade = ZombRand(0,100)
  53.  
  54. if (randomBaricade >= 94) then
  55. -- print("This window has metal sheet")
  56. local args = { x=coords.x, y=coords.y, z=coords.z, index=i, isMetal=true, isMetalBar=false, itemID='Base.MetalBar', condition=10, amount=1 }
  57. PlaceWindowsBaricades.placeBarricade(args, object, barricadeLocation);
  58. elseif (randomBaricade < 94) and (randomBaricade >= 49) then
  59. -- print("This window random planks")
  60. tileIsoObject:addRandomBarricades()
  61. end
  62. end
  63.  
  64. end
  65.  
  66. if CurrentErosionPercentage > 100 then
  67. modData["BarricadedWorld:isDefinitiveParsed"] = true
  68. tileIsoObject:transmitModData()
  69. end
  70.  
  71. end
  72.  
  73. modData["BarricadedWorld:parsedDate"] = CurrentWorldAgeDays
  74. tileIsoObject:transmitModData()
  75.  
  76. elseif instanceof(tileIsoObject, "IsoDoor") and not tileIsoObject:getSprite():getProperties():Is("GarageDoor") then
  77.  
  78. local modData = tileIsoObject:getModData()
  79.  
  80. -- If the window have not been loaded for the past 30 days, retry Barricaded World code
  81. if (not modData["BarricadedWorld:isDefinitiveParsed"]) and (not modData["BarricadedWorld:isPlayerPlaced"])
  82. and (not modData["BarricadedWorld:parsedDate"] or modData["BarricadedWorld:parsedDate"] + 30 < CurrentWorldAgeDays) then
  83.  
  84. local coords = { x = sq:getX(), y = sq:getY(), z = sq:getZ() }
  85.  
  86. -- The more advanced the erosion, the more chances the following code has to happen.
  87. -- 25% of current erosion advancement means 25% chance for a windows to go through the Barricaded World code.
  88. if (ZombRand(0,100) < CurrentErosionPercentage) then
  89.  
  90. local randomRoll = ZombRand(0,100)
  91.  
  92. -- If exterior door, has 20% break chances & 55% baricade chances
  93. if tileIsoObject:isExteriorDoor(nil) then
  94. if (randomRoll >= 60) then
  95. tileIsoObject:destroy()
  96. break
  97. elseif (randomRoll < 60) and (randomRoll >= 30) then
  98. tileIsoObject:addRandomBarricades()
  99. end
  100. -- If interior door, has 20% break chances & no barricades
  101. else
  102. if (randomRoll >= 60) then
  103. tileIsoObject:destroy()
  104. break
  105. end
  106. end
  107.  
  108. end
  109.  
  110. if CurrentErosionPercentage > 100 then
  111. modData["BarricadedWorld:isDefinitiveParsed"] = true
  112. tileIsoObject:transmitModData()
  113. end
  114.  
  115. end
  116.  
  117. modData["BarricadedWorld:parsedDate"] = CurrentWorldAgeDays
  118. tileIsoObject:transmitModData()
  119.  
  120. end
  121.  
  122. end
  123.  
  124. end
  125.  
  126. end
  127.  
  128. end
  129.  
  130. function PlaceWindowsBaricades.getBarricadeAble(x, y, z, index)
  131. local sq = getCell():getGridSquare(x, y, z)
  132. if sq and index >= 0 and index < sq:getObjects():size() then
  133. o = sq:getObjects():get(index)
  134. if instanceof(o, 'BarricadeAble') then
  135. return o
  136. end
  137. end
  138. return nil
  139. end
  140.  
  141. function PlaceWindowsBaricades.placeBarricade(args, object, barricadeLocation)
  142.  
  143. local barricade = IsoBarricade.AddBarricadeToObject(object, barricadeLocation)
  144.  
  145. if barricade then
  146. if args.isMetal then
  147. local metal = InventoryItemFactory.CreateItem('Base.SheetMetal')
  148. metal:setCondition(args.condition)
  149. barricade:addMetal(character, metal)
  150. barricade:transmitCompleteItemToClients()
  151. barricade:sendObjectChange('state')
  152. elseif args.isMetalBar then
  153. local metal = InventoryItemFactory.CreateItem('Base.MetalBar')
  154. metal:setCondition(args.condition)
  155. barricade:addMetalBar(character, metal)
  156. barricade:transmitCompleteItemToClients()
  157. else
  158. local plank = InventoryItemFactory.CreateItem('Base.Plank')
  159.  
  160. for i=0, args.amount - 1 do
  161. barricade:addPlank(character, plank)
  162.  
  163. if isServer() then
  164. if barricade:getNumPlanks() == 1 then
  165. barricade:transmitCompleteItemToClients()
  166. else
  167. barricade:sendObjectChange('state')
  168. end
  169. end
  170. end
  171. end
  172. end
  173. -- else print("No barricade location found");
  174. end
  175.  
  176. function PlaceWindowsBaricades.onObjectAdded(isoObject)
  177. if instanceof(isoObject, "IsoWindow") or instanceof(isoObject, "IsoDoor") then
  178. local modData = isoObject:getModData()
  179. modData["BarricadedWorld:isPlayerPlaced"] = true
  180. isoObject:transmitModData()
  181. end
  182. end
  183.  
  184. function PlaceWindowsBaricades.preCalculateErosion()
  185. CurrentWorldAgeDays = getGameTime():getWorldAgeHours() / 24
  186. local sandboxOptions = getSandboxOptions()
  187. local timeSpent = CurrentWorldAgeDays + TimeSinceApoValues[sandboxOptions:getTimeSinceApo()]
  188. CurrentErosionPercentage = (timeSpent/ErosionSpeedValues[sandboxOptions:getErosionSpeed()])*100
  189. end
  190.  
  191. Events.OnGameTimeLoaded.Add(PlaceWindowsBaricades.preCalculateErosion)
  192. Events.EveryDays.Add(PlaceWindowsBaricades.preCalculateErosion)
  193. Events.OnGameStart.Add(PlaceWindowsBaricades.preCalculateErosion)
  194.  
  195. Events.OnObjectAdded.Add(PlaceWindowsBaricades.onObjectAdded)
  196. Events.LoadGridsquare.Add(PlaceWindowsBaricades.loadGridsquare);
  197.  
Advertisement
Add Comment
Please, Sign In to add comment