liquids.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. -- == Liquid features == --
  2. -- Tank wagons can get support for Techage liquids.
  3. -- advtrains provides a "Tank car filling spigot" and a "Tank car unloading funnel" which are placed above resp. below the tracks.
  4. -- They act as proxy nodes from/to which liquids can be pumped using the Techage pumps
  5. -- Note: for tank cars to support filling with liquid, add the "techage_liquid_capacity = <capacity_in_liquid_units>" property to the wagon definition.
  6. -- Get Techage variables
  7. local Pipe = techage.LiquidPipe
  8. local liquid = networks.liquid
  9. -- Nodes
  10. -- checks for and gets a tank car at given reference position. Ref Pos must be the track this node relates to.
  11. -- For a wagon to be valid it must be standing at the node in question
  12. -- If found, returns liquid_info, capacity, wagon_id (where liquid_info is {name=..., amount=...} in same format as the techage tank nvms)
  13. -- If not found, returns nil
  14. local function get_tank_car_liquidinfo(ref_pos)
  15. local trains = advtrains.occ.get_trains_at(ref_pos)
  16. for train_id, index in pairs(trains) do
  17. local train = advtrains.trains[train_id]
  18. -- make sure that the train is stopped
  19. if train.velocity == 0 then
  20. local wagon_num, wid, data, offset_from_center = advtrains.get_wagon_at_index(train_id, index)
  21. if wagon_num and offset_from_center < 1 then
  22. -- this wagon is possibly a tank car. Check the wagon def
  23. local _, proto = advtrains.get_wagon_prototype(data)
  24. if proto.techage_liquid_capacity then
  25. -- Found a tank car. Get its liquid info from the data table, create it if necessary
  26. if not data.techage_liquid then
  27. data.techage_liquid = {}
  28. end
  29. return data.techage_liquid, proto.techage_liquid_capacity, wid
  30. end
  31. end
  32. end
  33. end
  34. return nil
  35. end
  36. local function loader_relpos(pos)
  37. return {x=pos.x, y=pos.y-3, z=pos.z}
  38. end
  39. local function tankcar_put_liquid(pos, name, amount)
  40. local lic, capa, wid = get_tank_car_liquidinfo(pos)
  41. if lic then
  42. if lic.name then
  43. if lic.name ~= name then
  44. -- different liquid than already in here - deny
  45. return amount
  46. end
  47. -- else add the amount
  48. lic.amount = lic.amount + amount
  49. else
  50. -- does not contain liquid yet, set name and amount to 0
  51. lic.name = name
  52. lic.amount = amount
  53. end
  54. --atdebug("tankcar_put_liquid: put",name,amount,", now contains ",lic)
  55. if lic.amount > capa then
  56. -- capacity was hit, reject too much liquid
  57. local reject = lic.amount - capa
  58. lic.amount = capa
  59. --atdebug("tankcar_put_liquid: over capa", capa, ", rejects ", reject)
  60. return reject
  61. end
  62. return 0
  63. end
  64. return amount
  65. end
  66. local function unloader_relpos(pos)
  67. return {x=pos.x, y=pos.y+1, z=pos.z}
  68. end
  69. local function tankcar_take_liquid(pos, name, amount)
  70. local lic, capa, wid = get_tank_car_liquidinfo(pos)
  71. if lic then
  72. if lic.name then
  73. -- note: name parameter may be nil (aka any), then do not prevent
  74. if name and lic.name ~= name then
  75. -- different liquid than already in here - deny
  76. return 0
  77. end
  78. else
  79. -- does not contain liquid, nothing to take
  80. return 0
  81. end
  82. if lic.amount <= amount then
  83. -- pumping last bit of liquid
  84. local rest, oldname = lic.amount, lic.name
  85. lic.amount = 0
  86. lic.name = nil -- reset the name since car is now empty
  87. --atdebug("tankcar_take_liquid: took",name,amount,", now empty")
  88. return rest, oldname
  89. end
  90. -- no underflow, subtract
  91. lic.amount = lic.amount - amount
  92. --atdebug("tankcar_take_liquid: took",name,amount,", now left ",lic)
  93. return amount, lic.name
  94. end
  95. return 0 -- no wagon here
  96. end
  97. minetest.register_node("advtrains_techage:liquid_loader", {
  98. description = attrans("Tank Car Filling Spigot"),
  99. tiles = {
  100. "advtrains_ta_spigot_back.png^[transformR180",
  101. "advtrains_ta_spigot_ahead.png",
  102. "advtrains_ta_spigot_side.png",
  103. "advtrains_ta_spigot_side.png^[transformR270",
  104. "advtrains_ta_spigot_ahead.png",
  105. "advtrains_ta_spigot_back.png",
  106. },
  107. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  108. Pipe:after_dig_tube(pos, oldnode, oldmetadata)
  109. end,
  110. paramtype2 = "facedir", -- important!
  111. drawtype = "nodebox",
  112. node_box = {
  113. type = "fixed",
  114. fixed = {
  115. {-1/8, -6/8, -1/8, 1/8, 1/8, 1/8},
  116. {-2/8, -5/8, -2/8, 2/8, -1/4, 2/8},
  117. {-1/8, -1/8, 1/8, 1/8, 1/8, 4/8},
  118. {-2/8, -2/8, 13/32, 2/8, 2/8, 1/2},
  119. },
  120. },
  121. on_rotate = screwdriver.disallow, -- important!
  122. paramtype = "light",
  123. use_texture_alpha = techage.CLIP,
  124. sunlight_propagates = true,
  125. is_ground_content = false,
  126. groups = {crumbly = 2, cracky = 2, snappy = 2},
  127. sounds = default.node_sound_metal_defaults(),
  128. })
  129. liquid.register_nodes({"advtrains_techage:liquid_loader"},
  130. Pipe, "tank", {"B"}, {
  131. capa = 42, -- capa is ignored by put function, but needs to be given anyway.
  132. peek = function(pos, indir) -- should provide the type of liquid anyway, because pump uses it to check whether it can pump here
  133. local lic, capa, wid = get_tank_car_liquidinfo(loader_relpos(pos))
  134. --atdebug("loader peeked: ", lic, capa)
  135. if lic and lic.name and lic.amount > 0 then
  136. return lic.name
  137. end
  138. return nil
  139. end,
  140. put = function(pos, indir, name, amount)
  141. return tankcar_put_liquid(loader_relpos(pos), name, amount)
  142. end,
  143. take = function(pos, indir, name, amount)
  144. return 0 -- cannot take anything from the loader
  145. end,
  146. untake = function(pos, indir, name, amount)
  147. return tankcar_put_liquid(loader_relpos(pos), name, amount)
  148. end,
  149. }
  150. )
  151. minetest.register_node("advtrains_techage:liquid_unloader", {
  152. description = attrans("Tank Car Unloading Drain Funnel"),
  153. tiles = {
  154. "advtrains_ta_spigot_ahead.png^[transformR180",
  155. "advtrains_ta_spigot_back.png",
  156. "advtrains_ta_spigot_side.png^[transformR90",
  157. "advtrains_ta_spigot_side.png^[transformR180",
  158. "advtrains_ta_spigot_ahead.png^[transformR180",
  159. "advtrains_ta_spigot_back.png^[transformR180",
  160. },
  161. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  162. Pipe:after_dig_tube(pos, oldnode, oldmetadata)
  163. end,
  164. paramtype2 = "facedir", -- important!
  165. drawtype = "nodebox",
  166. node_box = {
  167. type = "fixed",
  168. fixed = {
  169. {-1/8, -1/8, -1/8, 1/8, 5/8, 1/8},
  170. {-2/8, 1/4, -2/8, 2/8, 5/8, 2/8},
  171. {-1/8, -1/8, 1/8, 1/8, 1/8, 4/8},
  172. {-2/8, -2/8, 13/32, 2/8, 2/8, 4/8},
  173. },
  174. },
  175. on_rotate = screwdriver.disallow, -- important!
  176. paramtype = "light",
  177. use_texture_alpha = techage.CLIP,
  178. sunlight_propagates = true,
  179. is_ground_content = false,
  180. groups = {crumbly = 2, cracky = 2, snappy = 2},
  181. sounds = default.node_sound_metal_defaults(),
  182. })
  183. liquid.register_nodes({"advtrains_techage:liquid_unloader"},
  184. Pipe, "tank", {"B"}, {
  185. capa = 42, -- capa is ignored by put function, but needs to be given anyway.
  186. peek = function(pos, indir)
  187. local lic, capa, wid = get_tank_car_liquidinfo(unloader_relpos(pos))
  188. --atdebug("unloader peeked: ", lic, capa)
  189. if lic and lic.name and lic.amount > 0 then
  190. return lic.name
  191. end
  192. return nil
  193. end,
  194. put = function(pos, indir, name, amount)
  195. return amount -- cannot put here
  196. end,
  197. take = function(pos, indir, name, amount)
  198. return tankcar_take_liquid(unloader_relpos(pos), name, amount)
  199. end,
  200. untake = function(pos, indir, name, amount)
  201. -- untake has to work like a put would!
  202. return tankcar_put_liquid(loader_relpos(pos), name, amount)
  203. end,
  204. }
  205. )
  206. minetest.register_craft({
  207. output = "advtrains_techage:liquid_loader",
  208. recipe = {
  209. {"techage:ta3_pipeS"},
  210. {"minecart:hopper"},
  211. },
  212. })
  213. minetest.register_craft({
  214. output = "advtrains_techage:liquid_unloader",
  215. recipe = {
  216. {"minecart:hopper"},
  217. {"techage:ta3_pipeS"},
  218. },
  219. })