barrel.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. minetest.register_craftitem("bitumen:oil_drum_filled", {
  2. description = "Filled Oil Drum",
  3. inventory_image = "bitumen_drum_side.png",
  4. stack_max = 1,
  5. on_place = function(itemstack, placer, pointed_thing)
  6. local imeta = itemstack:get_meta()
  7. local fluid = imeta:get_string("fluid")
  8. local fill = imeta:get_int("fill")
  9. local max_fill = imeta:get_int("maxfill")
  10. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  11. -- todo: check if buildable/ protected/ etc
  12. minetest.set_node(pos, {name="bitumen:oil_drum"})
  13. local bmeta = minetest.get_meta(pos)
  14. bmeta:set_string("fluid", fluid)
  15. bmeta:set_float("fill", fill)
  16. bmeta:set_float("maxfill", max_fill)
  17. bmeta:set_string("infotext", fluid .." (".. math.floor(((fill*100)/max_fill)+0.5) .."%)")
  18. itemstack:take_item()
  19. return itemstack
  20. end,
  21. })
  22. local function user_name(user)
  23. return user and user:get_player_name() or ""
  24. end
  25. -- Returns a logging function. For empty names, does not log.
  26. local function make_log(name)
  27. return name ~= "" and core.log or function() end
  28. end
  29. minetest.register_node("bitumen:oil_drum", {
  30. description = "Oil Drum",
  31. tiles = {"bitumen_drum_top.png", "bitumen_drum_bottom.png", "bitumen_drum_side.png",
  32. "bitumen_drum_side.png", "bitumen_drum_side.png", "bitumen_drum_side.png"},
  33. paramtype2 = "facedir",
  34. -- inventory_image = "bitumen_oil_drum.png",
  35. groups = {
  36. cracky=2,
  37. oddly_breakable_by_hand=2,
  38. oil_container = 1
  39. },
  40. paramtype = "light",
  41. drawtype = "nodebox",
  42. node_box = {
  43. type = "fixed",
  44. fixed = {
  45. --11.25
  46. {-0.49, -0.5, -0.10, 0.49, 0.5, 0.10},
  47. {-0.10, -0.5, -0.49, 0.10, 0.5, 0.49},
  48. --22.5
  49. {-0.46, -0.5, -0.19, 0.46, 0.5, 0.19},
  50. {-0.19, -0.5, -0.46, 0.19, 0.5, 0.46},
  51. -- 33.75
  52. {-0.416, -0.5, -0.28, 0.416, 0.5, 0.28},
  53. {-0.28, -0.5, -0.416, 0.28, 0.5, 0.416},
  54. --45
  55. {-0.35, -0.5, -0.35, 0.35, 0.5, 0.35},
  56. },
  57. },
  58. selection_box = {
  59. type = "fixed",
  60. fixed = {
  61. {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  62. },
  63. },
  64. stack_max = 99,
  65. -- tube = tubes_properties,legacy_facedir_simple = true,
  66. -- sounds = default.node_sound_wood_defaults(),
  67. on_construct = function(pos)
  68. local meta = minetest.env:get_meta(pos)
  69. meta:set_string("infotext", "Oil Drum (Empty)")
  70. meta:set_string("fluid", "air")
  71. meta:set_float("fill", "0")
  72. meta:set_float("maxfill", "48")
  73. meta:set_string("infotext", "Empty drum")
  74. -- meta:set_string("fillmax", "200") -- 55 US Gal, 44 Imp. Gal, 200 L
  75. end,
  76. -- can_dig = function(pos, player)
  77. -- local meta = minetest.env:get_meta(pos)
  78. -- if meta:get_int('filllevel') > 0 then
  79. -- return false
  80. -- end
  81. -- return true
  82. -- end,
  83. on_dig = function(pos, node, digger)
  84. local diggername = user_name(digger)
  85. local log = make_log(diggername)
  86. local def = minetest.registered_nodes[node.name]
  87. if def and (not def.diggable or
  88. (def.can_dig and not def.can_dig(pos, digger))) then
  89. minetest.log("info", diggername .. " tried to dig "
  90. .. node.name .. " which is not diggable "
  91. .. minetest.pos_to_string(pos))
  92. return
  93. end
  94. if minetest.is_protected(pos, diggername) then
  95. log("action", diggername
  96. .. " tried to dig " .. node.name
  97. .. " at protected position "
  98. .. minetest.pos_to_string(pos))
  99. minetest.record_protection_violation(pos, diggername)
  100. return
  101. end
  102. -- create an item and propagate meta
  103. local item
  104. local bmeta = minetest.get_meta(pos)
  105. local fluid = bmeta:get_string("fluid")
  106. local fill = bmeta:get_int("fill")
  107. local max_fill = bmeta:get_int("maxfill")
  108. print("fill: "..fill.." fluid:"..fluid)
  109. if fill == 0 or fluid == "air" then
  110. item = ItemStack("bitumen:oil_drum 1")
  111. else
  112. item = ItemStack("bitumen:oil_drum_filled 1")
  113. local imeta = item:get_meta()
  114. imeta:set_string("fluid", fluid)
  115. imeta:set_string("infotext", fluid .. " drum")
  116. imeta:set_float("fill", fill)
  117. imeta:set_float("maxfill", max_fill)
  118. end
  119. -- check if the digger has enough inventory
  120. local inv = digger and digger:get_inventory()
  121. if inv and inv:room_for_item("main", item) then
  122. inv:add_item("main", item)
  123. else
  124. -- can't dig item
  125. return
  126. end
  127. minetest.remove_node(pos)
  128. -- Run callback
  129. if def and def.after_dig_node then
  130. -- Copy pos and node because callback can modify them
  131. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  132. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  133. def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
  134. end
  135. -- Run script hook
  136. local _, callback
  137. for _, callback in ipairs(core.registered_on_dignodes) do
  138. local origin = minetest.callback_origins[callback]
  139. if origin then
  140. minetest.set_last_run_mod(origin.mod)
  141. --print("Running " .. tostring(callback) ..
  142. -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
  143. else
  144. --print("No data associated with callback")
  145. end
  146. -- Copy pos and node because callback can modify them
  147. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  148. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  149. callback(pos_copy, node_copy, digger)
  150. end
  151. end
  152. })
  153. -- filler
  154. minetest.register_node("bitumen:drum_filler", {
  155. description = "Petroleum Drum Filler",
  156. drawtype = "nodebox",
  157. node_box = {
  158. type = "fixed",
  159. fixed = {
  160. { -.1, -.1, -.1, .1, .5, .1},
  161. { -.4, -.5, -.4, .4, 0, .4},
  162. },
  163. },
  164. selection_box = {
  165. type = "fixed",
  166. fixed = {
  167. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  168. },
  169. },
  170. paramtype = "light",
  171. is_ground_content = false,
  172. tiles = { "default_bronze_block.png" },
  173. walkable = true,
  174. groups = { cracky = 3, petroleum_fixture = 1 },
  175. })
  176. minetest.register_abm({
  177. nodenames = {"bitumen:drum_filler"},
  178. neighbors = {"bitumen:oil_drum"},
  179. interval = 2,
  180. chance = 1,
  181. action = function(pos, node)
  182. local npos = {x=pos.x, y=pos.y + 1, z=pos.z}
  183. local pnet = bitumen.pipes.get_net(npos)
  184. if not pnet then
  185. return
  186. end
  187. local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
  188. local bmeta = minetest.env:get_meta(bpos)
  189. local fluid = bmeta:get_string("fluid")
  190. local fill = bmeta:get_int("fill")
  191. local max_fill = bmeta:get_int("maxfill")
  192. if pnet.buffer <= 0 then
  193. print("barrel filler: no oil in pipe")
  194. return -- no water in the pipe
  195. end
  196. if pnet.fluid ~= fluid and fluid ~= "air" then
  197. print("barrel filler: bad_fluid")
  198. return -- incompatible fluids
  199. end
  200. local cap = math.max(max_fill - fill, 0)
  201. -- print("cap: "..cap)
  202. local to_take = math.min(10, math.min(cap, pnet.buffer))
  203. if to_take == 0 then
  204. -- print("barrel full")
  205. return
  206. end
  207. local taken, fluid = bitumen.pipes.take_fluid(npos, to_take)
  208. if fluid == "air" or fill == 0 then
  209. bmeta:set_string("fluid", pnet.fluid)
  210. end
  211. bmeta:set_float("fill", math.min(taken + fill, max_fill))
  212. bmeta:set_string("infotext", fluid .." (".. math.floor(((taken+fill)*100/max_fill)+0.5) .."%)")
  213. print("barrel took ".. taken)
  214. end,
  215. })
  216. -- extractor
  217. minetest.register_node("bitumen:drum_extractor", {
  218. description = "Petroleum Drum Extractor",
  219. drawtype = "nodebox",
  220. node_box = {
  221. type = "fixed",
  222. fixed = {
  223. { -.1, -.4, -.1, .1, .5, .1},
  224. { -.4, -.5, -.4, .4, -.3, .4},
  225. },
  226. },
  227. selection_box = {
  228. type = "fixed",
  229. fixed = {
  230. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  231. },
  232. },
  233. paramtype = "light",
  234. is_ground_content = false,
  235. tiles = { "default_tin_block.png" },
  236. walkable = true,
  237. groups = { cracky = 3, petroleum_fixture = 1 },
  238. })
  239. minetest.register_abm({
  240. nodenames = {"bitumen:drum_extractor"},
  241. neighbors = {"bitumen:oil_drum"},
  242. interval = 2,
  243. chance = 1,
  244. action = function(pos, node)
  245. local npos = {x=pos.x, y=pos.y + 1, z=pos.z}
  246. local pnet = bitumen.pipes.get_net(npos)
  247. local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
  248. local bmeta = minetest.env:get_meta(bpos)
  249. local fluid = bmeta:get_string("fluid")
  250. local fill = bmeta:get_int("fill")
  251. local max_fill = bmeta:get_int("maxfill")
  252. local lift = 4
  253. local max_push = 3
  254. if pnet.buffer >= 64 then
  255. return
  256. end
  257. local to_push = math.min(max_push, math.min(64 - pnet.buffer, fill))
  258. if to_push <= 0 then
  259. --print("barrel extractor: barrel empty")
  260. return
  261. end
  262. print("to_push: "..to_push)
  263. if pnet.fluid ~= fluid and pnet.fluid ~= "air" then
  264. --print("barrel extractor: bad fluid")
  265. return -- incompatible fluids
  266. end
  267. local pushed = bitumen.pipes.push_fluid(npos, fluid, to_push, lift)
  268. bmeta:set_float("fill", math.max(fill - pushed, 0))
  269. bmeta:set_string("infotext", fluid .." (".. math.floor(((fill-pushed)*100/max_fill)+0.5) .."%)")
  270. --print("barrel pushed ".. pushed)
  271. end,
  272. })