subtixx

Untitled

Jun 18th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. function myClearModdedBlocks()
  2. local bl = { self.miab.pos[1], self.miab.pos[2] }
  3. local tr = { self.miab.pos[1] + blueprint.boundingBoxSize[1], self.miab.pos[2] + blueprint.boundingBoxSize[2] }
  4.  
  5. for _y = tr[2], bl[2], -1 do
  6. for _x = bl[1], tr[1], 1 do
  7. local __y = tostring(math.floor(_y - bl[2]))
  8. local __x = tostring(math.floor(_x - bl[1]))
  9. if (blueprint.layoutTableForeground ~= nil and
  10. blueprint.layoutTableForeground[__y] ~= nil and
  11. blueprint.layoutTableForeground[__y][__x] ~= nil and
  12. blueprint.layoutTableForeground[__y][__x] > 0) then breakBlock({_x, _y}, "foreground") end
  13. if (blueprint.layoutTableForeground ~= nil and
  14. blueprint.layoutTableBackground[__y] ~= nil and
  15. blueprint.layoutTableBackground[__y][__x] ~= nil and
  16. blueprint.layoutTableBackground[__y][__x] > 0) then breakBlock({_x, _y}, "background") end
  17. end
  18. end
  19.  
  20. self.miab.buildingStage = PLACEBLOCKS
  21. end
  22.  
  23. function scanBuilding()
  24. if self.miab.boundingBox == nil then
  25. endUnsuccessfully("Scan area is undefined")
  26. return
  27. end
  28.  
  29. local anythingScanned = false
  30.  
  31. local dist = world.distance({self.miab.boundingBox[3], self.miab.boundingBox[4]}, {self.miab.boundingBox[1], self.miab.boundingBox[2]})
  32. local xMax = dist[1]
  33. local yMax = dist[2]
  34. if xMax < 1 or yMax < 1 then
  35. endUnsuccessfully("Scan area is too small")
  36. return
  37. end
  38.  
  39. -- scan liquids
  40. local pos = nil
  41. local liquid = nil
  42. for y = 0, yMax, 1 do
  43. for x = 0, xMax, 1 do
  44. pos = {self.miab.boundingBox[1] + x, self.miab.boundingBox[2] + y}
  45. if (not self.miab.areaToIgnore) or (self.miab.areaToIgnore and not blueprint.isInsideBoundingBox(pos, self.miab.areaToIgnore)) then -- ignore pixels in areaToIgnore if it is defined
  46. liquid = world.liquidAt(pos)
  47. if (liquid) then
  48. blueprint.setLiquid(x, y, liquid)
  49. end
  50. end
  51. end
  52. end
  53.  
  54. -- scan blocks + mods
  55. local matNameBack = nil
  56. local matNameFore = nil
  57. local modNameBack = nil
  58. local modNameFore = nil
  59. for y = 0, yMax, 1 do
  60. for x = 0, xMax, 1 do
  61. pos = {self.miab.boundingBox[1] + x, self.miab.boundingBox[2] + y}
  62. if (not self.miab.areaToIgnore) or (self.miab.areaToIgnore and not blueprint.isInsideBoundingBox(pos, self.miab.areaToIgnore)) then -- ignore pixels in areaToIgnore if it is defined
  63. matNameBack = world.material(pos, "background")
  64. matNameFore = world.material(pos, "foreground")
  65. modNameBack = world.mod(pos, "background")
  66. modNameFore = world.mod(pos, "foreground")
  67. -- some impassable things like doors leave "metamaterial:<something>" now which cant be printed later on ...
  68. if matNameBack and not string.find(matNameBack, "metamaterial:") then
  69. -- store in blueprint
  70. blueprint.setBlock(x, y, matNameBack, "background")
  71.  
  72. anythingScanned = true
  73. else
  74. -- store as scaffold location
  75. blueprint.setBlock(x, y, "miab_scaffold", "background")
  76. end
  77.  
  78. -- some impassable things like doors leave "metamaterial:<something>" now which cant be printed later on ...
  79. if matNameFore and not string.find(matNameFore, "metamaterial:") then
  80. -- store in blueprint
  81. blueprint.setBlock(x, y, matNameFore, "foreground")
  82.  
  83. anythingScanned = true
  84. else
  85. -- store as scaffold location
  86. blueprint.setBlock(x, y, "miab_scaffold", "foreground")
  87. end
  88.  
  89. if (modNameBack) then
  90. -- store in blueprint
  91. blueprint.setMod(x, y, modNameBack, "background")
  92.  
  93. anythingScanned = true
  94. end
  95.  
  96. if (modNameFore) then
  97. -- store in blueprint
  98. blueprint.setMod(x, y, modNameFore, "foreground")
  99.  
  100. anythingScanned = true
  101. end
  102. end
  103. end
  104. end
  105.  
  106. -- scan objects
  107. -- local objectIds = fixedObjectQuery({self.miab.boundingBox[1], self.miab.boundingBox[2]}, {self.miab.boundingBox[3], self.miab.boundingBox[4]}) -- the in game objectquery should be world wrap safe now
  108. local objectIds = world.objectQuery({self.miab.boundingBox[1], self.miab.boundingBox[2]}, {self.miab.boundingBox[3], self.miab.boundingBox[4]}, { withoutEntityId = entity.id() })
  109. if (objectIds) then
  110. for _, objectId in pairs(objectIds) do
  111. pos = world.entityPosition(objectId)
  112. dist = world.distance(pos, {self.miab.boundingBox[1], self.miab.boundingBox[2]})
  113. -- this if is needed as objects that overlap the BB but are not placed
  114. -- "at a block inside the BB" dont need to be copied
  115. if (blueprint.isInsideBoundingBox(pos, self.miab.boundingBox)) then
  116. if (not self.miab.areaToIgnore) or (self.miab.areaToIgnore and not blueprint.isInsideBoundingBox(pos, self.miab.areaToIgnore)) then -- ignore objects in areaToIgnore if it is defined
  117. -- store in blueprint
  118. blueprint.setObject(dist[1], dist[2], objectId, self.miab.clearArea)
  119. -- smash it (this is now done in dropObjects because why was I doing the same thing in two different places?)
  120. -- world.breakObject(objectId, true)
  121.  
  122. anythingScanned = true
  123. end
  124. end
  125. end
  126. end
  127.  
  128. if (not anythingScanned) then
  129. endUnsuccessfully("Scan area is empty")
  130. end
  131. end
  132.  
  133. function myClearArea()
  134. self.miab.obstructionTable = {}
  135. self.miab.anythingPrinted = false
  136.  
  137. local bl = { self.miab.pos[1], self.miab.pos[2] }
  138. local tr = { self.miab.pos[1] + blueprint.boundingBoxSize[1], self.miab.pos[2] + blueprint.boundingBoxSize[2] }
  139. local entityCount = nil
  140. local entityIDs = {}
  141. local currentEntityType = nil
  142.  
  143.  
  144. for _y = tr[2], bl[2], -1 do
  145. for _x = bl[1], tr[1], 1 do
  146. world.destroyLiquid({_x, _y})
  147. end
  148. end
  149.  
  150. entityCount = 0
  151. -- entityIDs = fixedEntityQuery(bl, tr) -- we should be fine with trusting the game's entityquery around the world wrap now
  152. entityIDs = world.entityQuery(bl, tr, { boundMode = "collisionarea", withoutEntityId = entity.id() })
  153. self.miab.looseEnds = {} -- this is for cleaning up rogue item drops (from stuff that drops itself in die(), where we can't get at it)
  154. for _, entID in pairs(entityIDs) do
  155. if (entityInOurBox(entID, bl, tr)) then
  156. currentEntityType = world.entityType(entID) -- "player", "monster", "object", "itemdrop", "projectile", "plant", "plantdrop", "effect", "npc"
  157.  
  158. --[[if (currentEntityType == "player") or (currentEntityType == "npc") then
  159. entityCount = entityCount + 1
  160. self.miab.obstructionTable[entID] = true
  161. elseif (currentEntityType == "monster") then
  162. world.sendEntityMessage(entID, "despawn")
  163. if (world.monsterType(entID) == "petweasel")
  164. or (world.monsterType(entID) == "petbunny")
  165. or (world.monsterType(entID) == "petsnake")
  166. or (world.monsterType(entID) == "piglett")
  167. or (world.monsterType(entID) == "petcat")
  168. or (world.monsterType(entID) == "crasberry")
  169. or (world.monsterType(entID) == "snugget")
  170. or (world.monsterType(endID) == "petorbis") then
  171. -- its the imortal ship pet - we have to surrender
  172. entityCount = entityCount + 1
  173. end
  174. else]]if (currentEntityType == "object") then
  175. if (world.containerSize(entID)) then
  176. world.containerTakeAll(entID) -- dropContents is false and the object has an inventory so we delete that inventory rather than drop it
  177. end
  178. -- here's where we compile our list of loose end item drops
  179. local objectBreakDrop = world.getObjectParameter(entID, "breakDropOptions")
  180. if (objectBreakDrop) then self.miab.looseEnds[world.entityPosition(entID)] = world.entityName(entID) end
  181. --
  182. if(world.entityName(entID) ~= "teleporter") then -- Blacklist.
  183. world.breakObject(entID, true)
  184. end
  185. elseif (currentEntityType == "plant") then
  186. blueprint.clearBlock(world.entityPosition(entID), "foreground")
  187. else
  188. --sb.logWarn ("Modules in a Box: Unhandled Entity detected during clearArea() of type " .. currentEntityType)
  189. end
  190. end
  191. end
  192.  
  193. for _y = tr[2], bl[2], -1 do
  194. for _x = bl[1], tr[1], 1 do
  195. local __y = tostring(math.floor(_y - bl[2]))
  196. local __x = tostring(math.floor(_x - bl[1]))
  197. if (blueprint.layoutTableForeground ~= nil and
  198. blueprint.layoutTableForeground[__y] ~= nil and
  199. blueprint.layoutTableForeground[__y][__x] ~= nil and
  200. blueprint.layoutTableForeground[__y][__x] > 0) then
  201. breakBlock({_x, _y}, "foreground")
  202. end
  203. if (blueprint.layoutTableBackground ~= nil and
  204. blueprint.layoutTableBackground[__y] ~= nil and
  205. blueprint.layoutTableBackground[__y][__x] ~= nil and
  206. blueprint.layoutTableBackground[__y][__x] > 0) then
  207. breakBlock({_x, _y}, "background")
  208. end
  209. end
  210. end
  211.  
  212. if (entityCount > 0) then
  213. -- there are entities blocking placement
  214. self.miab.buildingStage = PRINTOBSTRUCTED
  215. else
  216. self.miab.buildingStage = DOUBLETAPBLOCKS
  217. end
  218. end
  219.  
  220. --- Object init event.
  221. -- Gets executed when this object is placed.
  222. -- @param virtual if this is a virtual call?
  223. function init(virtual)
  224. if virtual then return end
  225.  
  226. object.setInteractive(true)
  227.  
  228. -- Set handler for UI message
  229. message.setHandler("checkController", checkController)
  230. end
  231.  
  232. --- Object update event
  233. -- Gets executed when this object updates.
  234. -- @param dt delta time, time is specified in *.object as scriptDelta (60 = 1 second)
  235. function update(dt)
  236. end
  237.  
  238. --- Object on container action event.
  239. -- Gets executed when a player "does" things in the container
  240. -- For example place item in slot, then this function gets called
  241. function containerCallback()
  242. -- Check valid pod here, if it's not valid "spit" the item back out.
  243. local podItem = world.containerItemAt(entity.id(), 0) -- << Get item in itemSlot 0
  244.  
  245. if podItem == nil then return end -- << Not a valid item, jump out
  246.  
  247. if not validatePod(podItem) then -- << Not a valid pod, spit it out
  248. if world.containerConsume(entity.id(), podItem) then
  249. if world.spawnItem(podItem, entity.position()) == nil then -- << If the item was not spawned, readd to the container
  250. world.containerAddItems(entity.id(), podItem)
  251. end
  252. end
  253. end
  254. end
  255.  
  256. -- Custom functions here
  257.  
  258. --- Message function, when user clicks button on UI
  259. function checkController()
  260. local podItem = world.containerItemAt(entity.id(), 0) -- << Get item in itemSlot 0
  261.  
  262. if podItem == nil then return end -- << Not a valid item, jump out
  263.  
  264. -- This is the message that gets send when the button in the GUI is pressed
  265. -- One could just "re-check" here if it's a valid pod.
  266. if not validatePod(podItem) then return end -- << Don't do anything with the
  267.  
  268. local itemConf = root.itemConfig(podItem)
  269.  
  270. -- For the scanner to be able to "scan" miab_breakStuff has to be FALSE!
  271. --[[object.setConfigParameter("miab_breakStuff", itemConf["config"]["miab_breakStuff"])
  272. object.setConfigParameter("miab_clearOnly", itemConf["config"]["miab_clearOnly"])
  273. object.setConfigParameter("miab_dumpJSON", itemConf["config"]["miab_dumpJSON"])
  274. object.setConfigParameter("miab_printerCount", itemConf["config"]["miab_printerCount"])
  275. object.setConfigParameter("miab_basestore_blueprint", itemConf["config"]["miab_basestore_blueprint"])
  276. object.setConfigParameter("miab_printer_offset", itemConf["config"]["miab_printer_offset"])]]
  277. object.setConfigParameter("miab_dumpJSON", itemConf["config"]["miab_dumpJSON"])
  278. object.setConfigParameter("miab_basestore_blueprint", itemConf["config"]["miab_basestore_blueprint"])
  279. object.setConfigParameter("miab_fixed_area_to_ignore_during_scan_bounding_box", itemConf["config"]["miab_fixed_area_to_ignore_during_scan_bounding_box"])
  280. object.setConfigParameter("miab_fixed_area_to_scan_bounding_box", itemConf["config"]["miab_fixed_area_to_scan_bounding_box"])
  281.  
  282. if (not readerBusy()) then
  283. -- Options for read process
  284. local readerOptions = {}
  285. --[[
  286. readerOptions.readerPosition = object.toAbsolutePosition({ 0.0, 0.0 })
  287. readerOptions.spawnPrinterPosition = object.toAbsolutePosition({ 0, 4 })
  288. readerOptions.breakStuff = config.getParameter("miab_breakStuff", true)
  289. readerOptions.clearOnly = config.getParameter("miab_clearOnly", false)
  290. readerOptions.plotJSON = config.getParameter("miab_dumpJSON", false)
  291. readerOptions.printerCount = config.getParameter("miab_printerCount", 1)
  292. readerOptions.animationDuration = 3
  293. ]]
  294.  
  295. readerOptions = config.getParameter("miabScannerOptions", nil)
  296. readerOptions.readerPosition = object.toAbsolutePosition({ 0.0, 0.0 })
  297. readerOptions.spawnPrinterPosition = object.toAbsolutePosition({ 0, 4 })
  298. readerOptions.saveBlueprint = config.getParameter("miab_dumpJSON", false)
  299. readerOptions.areaToIgnore = config.getParameter("miab_fixed_area_to_ignore_during_scan_bounding_box", nil) -- [left, bottom, right, top]
  300. readerOptions.areaToScan = config.getParameter("miab_fixed_area_to_scan_bounding_box", nil) -- [left, bottom, right, top]
  301. readerOptions.animationDuration = 0
  302.  
  303. --if (readerOptions.areaToScan ~= nil) then
  304. -- start reading process
  305. readStart(readerOptions)
  306. --end
  307. end
  308. end
  309.  
  310. --- Object update event
  311. -- Gets executed when this object updates.
  312. -- @param dt delta time, time is specified in *.object as scriptDelta (60 = 1 second)
  313. function update(dt)
  314. -- this needs to be polled until done
  315. if (self.miab == nil) then return end -- not initialized
  316.  
  317. --[[if (self.miab.readingStage) then -- only accessed during read
  318. if (self.miab.readingStage == READBUILDING) then
  319. scanBuilding()
  320. if (self.miab.breakStuff) then destroyBlocks() end
  321. self.miab.readingStage = SPITOUTPRINTER
  322. elseif (self.miab.readingStage == SPITOUTPRINTER) then
  323. if (self.miab.breakStuff) then destroyBlocks() end
  324. -- spawnPrinterItem()
  325. produceJSONOutput()
  326. ---if (not self.miab.breakStuff) then object.smash() end
  327. self.miab.readingStage = READSUCCESS
  328. elseif (self.miab.readingStage == READSUCCESS) then
  329. -- TODO: Check for self.
  330.  
  331. self.miab = nil -- reset scanner state
  332.  
  333. -- \/ This can be used to spawn an item with blueprint :D!
  334. self.scaned_configTable = blueprint.toConfigTable() -- save scanned table
  335. blueprint.Init({0, 0}) -- reset blueprint
  336.  
  337. -- Start to configure writing process
  338. local writerOptions = {}
  339. local area_to_scan_bounding_box = config.getParameter("miab_fixed_area_to_scan_bounding_box", nil)
  340. writerOptions.writerPosition = ({area_to_scan_bounding_box[1],area_to_scan_bounding_box[2]})
  341. printInit(writerOptions)
  342. donePrinting = false -- flag to end polling
  343. end
  344. end]]
  345.  
  346. if (self.miab.readingStage) then -- only accessed during read
  347. if (self.miab.readingStage == READBUILDING) then
  348. if (setDefaultAnimation) then setDefaultAnimation() end
  349. scanBuilding()
  350. if (self.miab == nil) then return end
  351. if (self.miab.clearArea) then
  352. dropLiquids()
  353. --dropObjects()
  354. dropBlocks()
  355. end
  356. self.miab.readingStage = SPITOUTPRINTER
  357. elseif (self.miab.readingStage == SPITOUTPRINTER) then
  358. if (self.miab.clearArea) then
  359. cleanUpLooseEnds()
  360. dropBlocks()
  361. end
  362. if (self.miab.givePrinter) then spawnPrinterItem() end
  363. if (self.miab.saveBlueprint) then produceJSONOutput() end
  364. self.miab.readingStage = DISPLAYANIMATION
  365. elseif (self.miab.readingStage == DISPLAYANIMATION) then
  366. --setScannerStatus("Scan completed successfully", "bell", "green")
  367. if (self.miab.animationDuration > 0) then
  368. displayBlueprintAnimation()
  369. else
  370. self.miab.readingStage = READSUCCESS
  371. end
  372. elseif (self.miab.readingStage == READSUCCESS) then
  373. endSuccessfully()
  374.  
  375. -- Start to configure writing process
  376. local writerOptions = {}
  377. local area_to_scan_bounding_box = config.getParameter("miab_fixed_area_to_scan_bounding_box", nil)
  378. writerOptions.writerPosition = ({area_to_scan_bounding_box[1],area_to_scan_bounding_box[2]})
  379. printInit(writerOptions)
  380. donePrinting = false -- flag to end polling
  381. end
  382. end
  383.  
  384. if (self.miab.buildingStage) then -- only accessed during write
  385. if (self.miab.buildingStage == PRINTINITIALISE) then -- read the Blueprint to be built
  386. --readBlueprint()
  387. blueprint.fromEntityConfig()
  388. self.miab.buildingStage = CLEARBUILDAREA
  389. elseif (self.miab.buildingStage == PREVIEWBUILDAREA) then -- display the build area indicator
  390. if (self.miab.time_to == nil) then self.miab.time_to = (os.time() - 1) end
  391. if (os.time() >= self.miab.time_to) then
  392. self.miab.time_to = os.time() + printPreview({self.miab.pos[1], self.miab.pos[2]})
  393. end
  394. elseif (self.miab.buildingStage == CLEARBUILDAREA) then -- clear build area
  395. -- make sure the area we're looking at is active
  396. if (not world.regionActive({self.miab.pos[1], self.miab.pos[2], self.miab.pos[1] + blueprint.boundingBoxSize[1], self.miab.pos[2] + blueprint.boundingBoxSize[2]})) then
  397. world.loadRegion({self.miab.pos[1], self.miab.pos[2], self.miab.pos[1] + blueprint.boundingBoxSize[1], self.miab.pos[2] + blueprint.boundingBoxSize[2]})
  398. end
  399. myClearArea()
  400. elseif (self.miab.buildingStage == DOUBLETAPBLOCKS) then -- clear build area
  401. cleanUpLooseEnds()
  402. myClearModdedBlocks()
  403. elseif (self.miab.buildingStage == PLACEBLOCKS) then -- place scaffolding + blocks
  404. printBlocks()
  405. elseif (self.miab.buildingStage == PLACETILEMODS) then -- place mods on tiles
  406. printTileMods()
  407. elseif (self.miab.buildingStage == REMOVESCAFFOLD) then -- remove scaffolding
  408. clearScaffolding()
  409. elseif (self.miab.buildingStage == PLACEOBJECTS) then -- place objects
  410. printObjects()
  411. elseif (self.miab.buildingStage == PLACELIQUIDS) then -- place liquids
  412. printLiquids()
  413. elseif (self.miab.buildingStage == PRINTOBSTRUCTED) then -- area was obstructed
  414. FloatObstructed()
  415. elseif (self.miab.buildingStage == PRINTUNANCHORED) then -- area was in a void, no blocks could be placed
  416. FloatUnanchored()
  417. elseif (self.miab.buildingStage == PRINTSUCCESS) then
  418. blueprint.Init({0, 0})
  419. self.miab = nil
  420.  
  421. local podItem = world.containerItemAt(entity.id(), 0)
  422. if podItem == nil then return end
  423. world.containerConsume(entity.id(), podItem)
  424. end
  425. end
  426. end
  427.  
  428. --- Function to check if the item is a valid pod
  429. -- @param itm The item to check
  430. -- @return true if the item is valid, false otherwise
  431. function validatePod(itm)
  432. return root.itemHasTag(itm["name"], "shipcontroller")
  433. end
Advertisement
Add Comment
Please, Sign In to add comment