storage_tank.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. minetest.register_node("factory:storage_tank", {
  2. description = "Storage Tank",
  3. drawtype = "glasslike_framed",
  4. tiles = {"factory_steel_noise.png","factory_glass.png^factory_measure.png","factory_glass.png^factory_port.png", "factory_steel_noise.png"},
  5. inventory_image = "factory_storage_tank.png",
  6. paramtype = "light",
  7. sunlight_propagates = true,
  8. groups = {oddly_breakable_by_hand = 2},
  9. on_rightclick = function(pos, node, player, itemstack, pointed_thing)
  10. local stack = ItemStack(itemstack)
  11. if stack:get_name() == "bucket:bucket_water" then
  12. minetest.swap_node(pos, {name = "factory:storage_tank_water", param2 = 3 + 64 + 128})
  13. local meta = minetest.get_meta(pos)
  14. meta:set_int("stored", 3)
  15. return ItemStack("bucket:bucket_empty")
  16. end
  17. if stack:get_name() == "bucket:bucket_lava" then
  18. minetest.swap_node(pos, {name = "factory:storage_tank_lava", param2 = 7 + 64 + 128})
  19. local meta = minetest.get_meta(pos)
  20. meta:set_int("stored", 7)
  21. return ItemStack("bucket:bucket_empty")
  22. end
  23. end,
  24. })
  25. function factory.register_storage_tank(name, increment, tiles, plaintile, light, bucket_full, bucket_empty)
  26. minetest.register_node("factory:storage_tank_" .. name, {
  27. drawtype = "glasslike_framed",
  28. tiles = {"factory_steel_noise.png","factory_glass.png^factory_measure.png","factory_glass.png^factory_port.png", "factory_steel_noise.png"},
  29. special_tiles = tiles,
  30. paramtype = "light",
  31. sunlight_propagates = true,
  32. light_source = light,
  33. groups = {oddly_breakable_by_hand = 2, not_in_creative_inventory = 1},
  34. drop = nil,
  35. on_dig = function(pos, node, digger)
  36. local inv = digger:get_inventory()
  37. local meta = minetest.get_meta(pos)
  38. local stored = meta:get_int("stored")
  39. local stack = ItemStack({name="factory:storage_tank_" .. name .. "_inventory", count=1, metadata=stored})
  40. if inv:room_for_item("main", stack) then
  41. inv:add_item("main", stack)
  42. else
  43. minetest.add_item(pos, stack)
  44. end
  45. minetest.set_node(pos, {name = "air"})
  46. end,
  47. on_rightclick = function(pos, node, player, itemstack, pointed_thing)
  48. local stack = ItemStack(itemstack)
  49. if stack:get_name() == bucket_full then
  50. local meta = minetest.get_meta(pos)
  51. local stored = meta:get_int("stored")
  52. if stored < 63 then
  53. stored = stored + increment
  54. meta:set_int("stored", stored)
  55. meta:set_string("infotext", "Storage Tank (" .. name .. "): "..math.floor((100/63)*stored).."% full")
  56. minetest.swap_node(pos, {name = "factory:storage_tank_" .. name, param2 = stored + 64 + 128})
  57. return ItemStack(bucket_empty)
  58. end
  59. end
  60. if stack:get_name() == bucket_empty then
  61. local meta = minetest.get_meta(pos)
  62. local stored = meta:get_int("stored")
  63. if stored > increment then
  64. stored = stored - increment
  65. meta:set_int("stored", stored)
  66. meta:set_string("infotext", "Storage Tank (" .. name .. "): "..math.floor((100/63)*stored).."% full")
  67. minetest.swap_node(pos, {name = "factory:storage_tank_" .. name, param2 = stored + 64 + 128})
  68. return ItemStack(bucket_full)
  69. elseif stored <= increment then
  70. meta:set_string("infotext", nil)
  71. minetest.swap_node(pos, {name = "factory:storage_tank"})
  72. return ItemStack(bucket_full)
  73. end
  74. end
  75. end,
  76. })
  77. minetest.register_abm({
  78. nodenames = {"factory:storage_tank_" .. name},
  79. neighbors = nil,
  80. interval = 1,
  81. chance = 1,
  82. action = function(pos, node, active_object_count, active_object_count_wider)
  83. local meta = minetest.get_meta(pos)
  84. local stored = meta:get_int("stored")
  85. minetest.swap_node(pos, {name = "factory:storage_tank_" .. name, param2 = stored + 64 + 128})
  86. end,
  87. })
  88. minetest.register_craftitem("factory:storage_tank_" .. name .. "_inventory", {
  89. description = "Storage Tank (" .. name .. ")",
  90. inventory_image = plaintile .. "^factory_storage_tank.png",
  91. wield_image = "factory_storage_tank.png",
  92. groups = {not_in_creative_inventory = 1},
  93. stack_max = 1,
  94. on_place = function(itemstack, placer, pointed_thing)
  95. local pt = pointed_thing
  96. if not pt then
  97. return
  98. end
  99. if pt.type ~= "node" then
  100. return
  101. end
  102. local under = minetest.get_node(pt.under)
  103. local above = minetest.get_node(pt.above)
  104. if not minetest.registered_nodes[under.name] then
  105. return
  106. end
  107. if not minetest.registered_nodes[above.name] then
  108. return
  109. end
  110. if pt.above.y ~= pt.under.y+1 then
  111. return
  112. end
  113. if not minetest.registered_nodes[above.name].buildable_to then
  114. return
  115. end
  116. minetest.place_node(pt.above, {name="factory:storage_tank_" .. name, param2 = tonumber(itemstack:get_metadata()) + 64 + 128})
  117. local meta = minetest.get_meta(pt.above)
  118. meta:set_int("stored", tonumber(itemstack:get_metadata()))
  119. meta:set_string("infotext", "Storage Tank (" .. name .. "): "..math.floor((100/63)*tonumber(itemstack:get_metadata())).."% full")
  120. return ""
  121. end
  122. })
  123. end
  124. -- don't forget to add your liquid to the initial node around line 10
  125. -- I may improve this later
  126. factory.register_storage_tank("water", 4, {{name="default_water_source_animated.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}}}, "default_water.png", 0, "bucket:bucket_water", "bucket:bucket_empty")
  127. factory.register_storage_tank("lava", 8, {{name="default_lava_source_animated.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=3.0}}}, "default_lava.png", 13, "bucket:bucket_lava", "bucket:bucket_empty")