cylinder_tank.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. local function check_tank_foundation(bpos)
  2. local meta = minetest.get_meta(bpos)
  3. local height = meta:get_int("height")
  4. if height < 1 then
  5. return
  6. end
  7. local d = math.ceil(height / 5)
  8. local ret = bitumen.check_foundation(
  9. {x = bpos.x - 2, y = bpos.y - 1 - d, z = bpos.z - 2},
  10. {x = bpos.x + 2, y = bpos.y - 2 , z = bpos.z + 2},
  11. {
  12. ["default:stone"] = 1,
  13. ["default:desert_stone"] = 1,
  14. ["default:steelblock"] = 1,
  15. ["bitumen:concrete"] = 1,
  16. }
  17. )
  18. if ret == true then
  19. return true
  20. end
  21. -- try for the steel foundation
  22. ret = bitumen.check_foundation(
  23. {x = bpos.x - 1, y = bpos.y - 1 - d, z = bpos.z - 1},
  24. {x = bpos.x + 1, y = bpos.y - 1 , z = bpos.z + 1},
  25. { ["default:steelblock"] = 1 }
  26. )
  27. if not ret then
  28. return false
  29. end
  30. -- todo: check steel legs
  31. return true
  32. end
  33. -- check poor foundation
  34. minetest.register_abm({
  35. nodenames = {"bitumen:cylinder_tank_bottom"},
  36. interval = 30,
  37. chance = 10,
  38. action = function(pos)
  39. if not check_tank_foundation(pos) then
  40. --print("tank failure")
  41. local meta = minetest.get_meta(pos)
  42. local fill = meta:get_int("fill")
  43. local height = meta:get_int("height")
  44. if height < 2 then
  45. -- no middle segments
  46. return
  47. end
  48. local fillh = math.ceil(fill / (9 * 60))
  49. local y = math.random(1, fillh)
  50. minetest.set_node({x=pos.x, y=pos.y+y, z=pos.z}, {name="bitumen:cylinder_tank_cracked"})
  51. end
  52. end
  53. })
  54. local function try_add_fluid(tpos)
  55. -- find the bottom node
  56. local tmeta = minetest.get_meta(tpos)
  57. local rbpos = tmeta:get_string("bpos")
  58. if not rbpos then
  59. return
  60. end
  61. -- grab the input network
  62. local npos = {x=tpos.x, y=tpos.y+1, z=tpos.z}
  63. local tnet = bitumen.pipes.get_net(npos)
  64. if not tnet or not tnet.fluid or tnet.fluid == "air" then
  65. return
  66. end
  67. -- all the data is in the bottom node
  68. local bpos = minetest.deserialize(rbpos)
  69. local bmeta = minetest.get_meta(bpos)
  70. local fill = bmeta:get_int("fill")
  71. local capacity = bmeta:get_int("capacity")
  72. local fluid = bmeta:get_string("fluid")
  73. -- check for full
  74. if fill >= capacity then
  75. return
  76. end
  77. if fill > 0 and fluid ~= tnet.fluid then
  78. return
  79. end
  80. local remcap = capacity - fill
  81. local taken, tfluid = bitumen.pipes.take_fluid(npos, remcap)
  82. if taken == 0 then
  83. return
  84. end
  85. -- set or change fluids
  86. if fluid == "air" or fill == 0 then
  87. bmeta:set_string("fluid", tfluid)
  88. tmeta:set_string("fluid", tfluid)
  89. end
  90. fill = fill + taken
  91. bmeta:set_int("fill", fill)
  92. end
  93. local function try_give_fluid(bpos)
  94. -- grab the output network
  95. local npos = {x=bpos.x, y=bpos.y-1, z=bpos.z}
  96. local tnet = bitumen.pipes.get_net(npos)
  97. if not tnet then
  98. return
  99. end
  100. -- grab the data
  101. local bmeta = minetest.get_meta(bpos)
  102. local fill = bmeta:get_int("fill")
  103. local capacity = bmeta:get_int("capacity")
  104. local fluid = bmeta:get_string("fluid")
  105. -- check for empty
  106. if fill <= 0 or fluid == "air" then
  107. return
  108. end
  109. local lift = capacity / (9 * 60)
  110. local pushed = bitumen.pipes.push_fluid(npos, fluid, math.min(fill, 64), lift)
  111. if pushed == 0 then
  112. return
  113. end
  114. fill = math.max(fill - pushed, 0)
  115. bmeta:set_int("fill", fill)
  116. end
  117. -- tank data is stored based on the bottom position
  118. local function init_tank(tpos, bpos)
  119. local fluid = "air"
  120. local tnet = bitumen.pipes.get_net({x=tpos.x, y=tpos.y+1, z=tpos.z})
  121. if tnet and tnet.fluid then
  122. fluid = tnet.fluid
  123. end
  124. local tmetad = { fields = {
  125. bpos = minetest.serialize(bpos),
  126. fluid = fluid,
  127. }}
  128. local tmeta = minetest.get_meta(tpos)
  129. tmeta:from_table(tmetad)
  130. local height = tpos.y - bpos.y
  131. local cap = height * 60 * 9
  132. local bmeta = minetest.get_meta(bpos)
  133. local bmetad = {fields = {
  134. capacity = cap,
  135. fill = 0,
  136. fluid = fluid,
  137. height = height,
  138. tpos = minetest.serialize(tpos),
  139. }}
  140. bmeta:from_table(bmetad)
  141. end
  142. local function find_bottom(pos)
  143. local p = {x=pos.x, y=pos.y, z=pos.z}
  144. while 1==1 do
  145. -- find the bottom and check the fill
  146. local n = minetest.get_node(p)
  147. if n.name == "bitumen:cylinder_tank_bottom" then
  148. return p
  149. elseif n.name ~= "bitumen:cylinder_tank"
  150. and n.name ~= "bitumen:cylinder_tank_cracked"
  151. and n.name ~= "bitumen:cylinder_tank_top"
  152. then
  153. return nil
  154. end
  155. p.y = p.y - 1
  156. end
  157. end
  158. local function can_dig_tank(pos, player)
  159. --if 1==1 then return true end
  160. -- check owner
  161. -- TODO: fix ownership
  162. -- local nmeta = minetest.get_meta(pos);
  163. -- local owner = nmeta:get_string("owner")
  164. -- if player:get_player_name() ~= owner then
  165. -- return false
  166. -- end
  167. local n = find_bottom(pos)
  168. if n == nil then
  169. return true
  170. else
  171. local meta = minetest.get_meta(pos)
  172. local fill = meta:get_int("fill")
  173. return fill <= 0
  174. end
  175. end
  176. minetest.register_node("bitumen:cylinder_tank", {
  177. paramtype = "light",
  178. drawtype = "nodebox",
  179. description = "Cylinder Tank Segment",
  180. tiles = {
  181. "default_steel_block.png",
  182. },
  183. node_box = {
  184. type = "fixed",
  185. fixed = {
  186. { -1.3, -.5, -1.3, 1.3, .5, 1.3 },
  187. { -1.5, -.5, -1.1, 1.5, .5, 1.1 },
  188. { -1.1, -.5, -1.5, 1.1, .5, 1.5 },
  189. -- { -8.2, -.5, -.2, -7.8, 10, .2 },
  190. -- { -.2, -.5, -8.2, .2, 10, -7.8 },
  191. -- { 8.2, -.5, -.2, 7.8, 10, .2 },
  192. -- { -.2, -.5, 8.2, .2, 10, 7.8 },
  193. },
  194. },
  195. collision_box = {
  196. type = "fixed",
  197. fixed = {
  198. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  199. }
  200. },
  201. selection_box = {
  202. type = "fixed",
  203. fixed = {
  204. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  205. }
  206. },
  207. paramtype2 = "facedir",
  208. groups = {cracky=1, level =2},
  209. sounds = default.node_sound_wood_defaults(),
  210. on_construct = function(pos)
  211. -- local meta = minetest.get_meta(pos)
  212. -- if placer then
  213. -- local owner = placer:get_player_name()
  214. -- meta:set_string("owner", owner)
  215. -- end
  216. -- meta:set_float("fluid_level", 0)
  217. -- meta:set_float("capacity", math.floor(3.14159 * .75 * 9 * 9 * 9 * 64))
  218. -- meta:set_string("infotext", "0%")
  219. --bitumen.pipes.on_construct(pos)
  220. end,
  221. -- on_destruct = bitumen.magic.on_destruct,
  222. can_dig = can_dig_tank,
  223. })
  224. minetest.register_node("bitumen:cylinder_tank_cracked", {
  225. paramtype = "light",
  226. drawtype = "nodebox",
  227. description = "Cracked Cylinder Tank Segment",
  228. tiles = {
  229. "default_tin_block.png",
  230. },
  231. node_box = {
  232. type = "fixed",
  233. fixed = {
  234. { -1.3, -.5, -1.3, 1.3, .5, 1.3 },
  235. { -1.5, -.5, -1.1, 1.5, .5, 1.1 },
  236. { -1.1, -.5, -1.5, 1.1, .5, 1.5 },
  237. -- { -8.2, -.5, -.2, -7.8, 10, .2 },
  238. -- { -.2, -.5, -8.2, .2, 10, -7.8 },
  239. -- { 8.2, -.5, -.2, 7.8, 10, .2 },
  240. -- { -.2, -.5, 8.2, .2, 10, 7.8 },
  241. },
  242. },
  243. collision_box = {
  244. type = "fixed",
  245. fixed = {
  246. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  247. }
  248. },
  249. selection_box = {
  250. type = "fixed",
  251. fixed = {
  252. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  253. }
  254. },
  255. paramtype2 = "facedir",
  256. groups = {cracky=1, level =2},
  257. sounds = default.node_sound_wood_defaults(),
  258. on_construct = function(pos)
  259. -- local meta = minetest.get_meta(pos)
  260. -- if placer then
  261. -- local owner = placer:get_player_name()
  262. -- meta:set_string("owner", owner)
  263. -- end
  264. -- meta:set_float("fluid_level", 0)
  265. -- meta:set_float("capacity", math.floor(3.14159 * .75 * 9 * 9 * 9 * 64))
  266. -- meta:set_string("infotext", "0%")
  267. --bitumen.pipes.on_construct(pos)
  268. end,
  269. -- on_destruct = bitumen.magic.on_destruct,
  270. can_dig = can_dig_tank,
  271. })
  272. minetest.register_node("bitumen:cylinder_tank_top", {
  273. paramtype = "light",
  274. drawtype = "nodebox",
  275. description = "Cylinder Tank Top",
  276. tiles = {
  277. "default_steel_block.png",
  278. },
  279. node_box = {
  280. type = "fixed",
  281. fixed = {
  282. { -1.3, -.5, -1.3, 1.3, .0, 1.3 },
  283. { -1.5, -.5, -1.1, 1.5, .0, 1.1 },
  284. { -1.1, -.5, -1.5, 1.1, .0, 1.5 },
  285. { -1.2, -.1, -1.2, 1.2, .2, 1.2 },
  286. { -.7, -.1, -.7, .7, .4, .7 },
  287. { -.1, .1, -.1, .1, .5, .1 },
  288. },
  289. },
  290. collision_box = {
  291. type = "fixed",
  292. fixed = {
  293. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  294. }
  295. },
  296. selection_box = {
  297. type = "fixed",
  298. fixed = {
  299. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  300. }
  301. },
  302. paramtype2 = "facedir",
  303. groups = {cracky=1, level =2, petroleum_fixture=1},
  304. sounds = default.node_sound_wood_defaults(),
  305. on_construct = function(pos)
  306. local p = {x=pos.x, y=pos.y, z=pos.z}
  307. local segs = 1
  308. while 1==1 do
  309. p.y = p.y - 1
  310. local n = minetest.get_node(p)
  311. if n.name == "bitumen:cylinder_tank_bottom" then
  312. -- done
  313. break
  314. elseif n.name == "bitumen:cylinder_tank" then
  315. segs = segs + 1
  316. else
  317. print("invalid top segment placement")
  318. return
  319. end
  320. end
  321. print("tank segments: " .. segs .. ", capacity: ".. (segs*9*60))
  322. init_tank(pos, p)
  323. local meta = minetest.get_meta(pos)
  324. if placer then
  325. local owner = placer:get_player_name()
  326. meta:set_string("owner", owner)
  327. end
  328. -- meta:set_string("infotext", "0%")
  329. end,
  330. -- on_destruct = bitumen.magic.on_destruct,
  331. can_dig = can_dig_tank,
  332. })
  333. minetest.register_node("bitumen:cylinder_tank_bottom", {
  334. paramtype = "light",
  335. drawtype = "nodebox",
  336. description = "Cylinder Tank Bottom",
  337. tiles = {
  338. "default_steel_block.png",
  339. },
  340. node_box = {
  341. type = "fixed",
  342. fixed = {
  343. { -1.3, .0, -1.3, 1.3, .5, 1.3 },
  344. { -1.5, .0, -1.1, 1.5, .5, 1.1 },
  345. { -1.1, .0, -1.5, 1.1, .5, 1.5 },
  346. { -1.0, -.2, -1.0, 1.0, .1, 1.0 },
  347. { -.7, -.4, -.7, .7, .1, .7 },
  348. { -.1, -.5, -.1, .1, .1, .1 },
  349. -- legs
  350. { -1.25, -1.55, -1.25, -1.15, 0, -1.15 },
  351. { 1.15, -1.55, -1.15, 1.25, 0, -1.25 },
  352. { -1.25, -1.55, 1.15, -1.15, 0, 1.25 },
  353. { 1.15, -1.55, 1.15, 1.25, 0, 1.25 },
  354. },
  355. },
  356. collision_box = {
  357. type = "fixed",
  358. fixed = {
  359. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  360. }
  361. },
  362. selection_box = {
  363. type = "fixed",
  364. fixed = {
  365. { -1.5, -.5, -1.5, 1.5, .5, 1.5 },
  366. }
  367. },
  368. paramtype2 = "facedir",
  369. groups = {cracky=1, level =2, petroleum_fixture=1},
  370. sounds = default.node_sound_wood_defaults(),
  371. on_construct = function(pos)
  372. local p = {x=pos.x, y=pos.y, z=pos.z}
  373. local segs = 1
  374. while 1==1 do
  375. p.y = p.y + 1
  376. local n = minetest.get_node(p)
  377. if n.name == "bitumen:cylinder_tank_top" then
  378. -- done
  379. break
  380. elseif n.name == "bitumen:cylinder_tank" then
  381. segs = segs + 1
  382. else
  383. print("invalid bottom segment placement")
  384. return
  385. end
  386. end
  387. init_tank(p, pos)
  388. local meta = minetest.get_meta(pos)
  389. if placer then
  390. local owner = placer:get_player_name()
  391. meta:set_string("owner", owner)
  392. end
  393. -- meta:set_float("fluid_level", 0)
  394. -- meta:set_float("capacity", math.floor(3.14159 * .75 * 9 * 9 * 9 * 64))
  395. -- meta:set_string("infotext", "0%")
  396. end,
  397. -- on_destruct = bitumen.magic.on_destruct,
  398. can_dig = can_dig_tank,
  399. })
  400. minetest.register_abm({
  401. nodenames = {"bitumen:cylinder_tank_top"},
  402. interval = 2,
  403. chance = 1,
  404. action = function(pos, node, active_object_count, active_object_count_wider)
  405. try_add_fluid(pos)
  406. end,
  407. })
  408. minetest.register_abm({
  409. nodenames = {"bitumen:cylinder_tank_bottom"},
  410. interval = 2,
  411. chance = 1,
  412. action = function(pos, node, active_object_count, active_object_count_wider)
  413. try_give_fluid(pos)
  414. end,
  415. })
  416. -- leaking
  417. minetest.register_abm({
  418. nodenames = {"bitumen:cylinder_tank_cracked"},
  419. interval = 10,
  420. chance = 5,
  421. action = function(pos, node, active_object_count, active_object_count_wider)
  422. local p = find_bottom(pos)
  423. if p == nil then
  424. return
  425. end
  426. local meta = minetest.get_meta(p)
  427. local fill = meta:get_int("fill")
  428. local fillh = math.ceil(fill / (9 * 60))
  429. local dh = pos.y - p.y
  430. -- fill level is below the crack
  431. if fillh < dh then
  432. return
  433. end
  434. -- choose a random place to leak
  435. local airs = minetest.find_nodes_in_area({x=pos.x-2, y=pos.y-1, z=pos.z-2}, {x=pos.x+2, y=pos.y, z=pos.z+2}, {"air"})
  436. if not airs then
  437. return
  438. end
  439. local ap = airs[math.random(#airs)]
  440. local l = math.min(fill, math.min(64, math.random(5, 30)))
  441. local fluid = meta:get_string("fluid")
  442. minetest.set_node(ap, {name=fluid})
  443. minetest.set_node_level(ap, l)
  444. meta:set_int("fill", fill - l)
  445. end,
  446. })