init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. local weirding = {
  2. just_placed = {},
  3. }
  4. local function clear()
  5. weirding.just_placed = {}
  6. minetest.after(1, clear)
  7. end
  8. minetest.after(1, clear)
  9. minetest.register_node("weirding:mover", {
  10. description = "mover",
  11. tiles = {
  12. "default_dirt.png^weirding_yellow_arrow.png",
  13. "default_dirt.png",
  14. "default_dirt.png",
  15. "default_dirt.png",
  16. "default_dirt.png",
  17. "default_dirt.png",
  18. },
  19. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  20. is_ground_content = false,
  21. paramtype2 = "facedir",
  22. on_place = minetest.rotate_node,
  23. sounds = default.node_sound_dirt_defaults({
  24. footstep = {name = "default_stone_footstep", gain = 0.25},
  25. }),
  26. })
  27. minetest.register_abm({
  28. nodenames = {"weirding:mover"},
  29. -- neighbors = {"group:soil"},
  30. interval = 1,
  31. chance = 1,
  32. -- catch_up = true,
  33. action = function(pos)
  34. local node = minetest.get_node(pos)
  35. local back_dir = minetest.facedir_to_dir(node.param2)
  36. local top_dir = ({[0]={x=0, y=1, z=0},
  37. {x=0, y=0, z=1},
  38. {x=0, y=0, z=-1},
  39. {x=1, y=0, z=0},
  40. {x=-1, y=0, z=0},
  41. {x=0, y=-1, z=0}})[math.floor(node.param2/4)]
  42. local right_dir = vector.cross(top_dir, back_dir)
  43. -- local front_dir = vector.multiply(back_dir, -1)
  44. local backpos = vector.add(pos, top_dir)
  45. local frontpos = vector.add(backpos, back_dir)
  46. local bnode = minetest.get_node(backpos)
  47. if bnode == nil or bnode.name == "air" then
  48. return
  49. end
  50. local bhash = minetest.hash_node_position(backpos)
  51. if weirding.just_placed[bhash] == true then
  52. return
  53. end
  54. local fnode = minetest.get_node(frontpos)
  55. if fnode ~= nil and fnode.name ~= "air" then
  56. return
  57. end
  58. local fhash = minetest.hash_node_position(frontpos)
  59. weirding.just_placed[fhash] = true
  60. minetest.swap_node(frontpos, bnode)
  61. minetest.set_node(backpos, {name="air"})
  62. minetest.check_for_falling(frontpos)
  63. end,
  64. })
  65. minetest.register_node("weirding:dropper", {
  66. description = "mover",
  67. tiles = {
  68. "default_dirt.png^weirding_yellow_arrow_circle.png",
  69. "default_dirt.png",
  70. "default_dirt.png",
  71. "default_dirt.png",
  72. "default_dirt.png",
  73. "default_dirt.png",
  74. },
  75. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  76. is_ground_content = false,
  77. paramtype2 = "facedir",
  78. on_place = minetest.rotate_node,
  79. sounds = default.node_sound_dirt_defaults({
  80. footstep = {name = "default_stone_footstep", gain = 0.25},
  81. }),
  82. })
  83. minetest.register_abm({
  84. nodenames = {"weirding:dropper"},
  85. -- neighbors = {"group:soil"},
  86. interval = 1,
  87. chance = 1,
  88. -- catch_up = true,
  89. action = function(pos)
  90. local node = minetest.get_node(pos)
  91. local back_dir = minetest.facedir_to_dir(node.param2)
  92. local top_dir = ({[0]={x=0, y=1, z=0},
  93. {x=0, y=0, z=1},
  94. {x=0, y=0, z=-1},
  95. {x=1, y=0, z=0},
  96. {x=-1, y=0, z=0},
  97. {x=0, y=-1, z=0}})[math.floor(node.param2/4)]
  98. local front_dir = vector.multiply(back_dir, -1)
  99. local toppos = vector.add(pos, top_dir)
  100. local backpos = vector.add(pos, back_dir)
  101. local frontpos = vector.add(pos, back_dir)
  102. local tnode = minetest.get_node(toppos)
  103. if tnode == nil or tnode.name == "air" then
  104. return
  105. end
  106. local thash = minetest.hash_node_position(toppos)
  107. if weirding.just_placed[thash] == true then
  108. return
  109. end
  110. -- try the front
  111. local fnode = minetest.get_node(frontpos)
  112. if fnode ~= nil and fnode.name == "air" then
  113. local fhash = minetest.hash_node_position(frontpos)
  114. weirding.just_placed[fhash] = true
  115. minetest.swap_node(frontpos, tnode)
  116. minetest.set_node(toppos, {name="air"})
  117. minetest.check_for_falling(frontpos)
  118. return
  119. end
  120. -- try moving on
  121. local frontpos = vector.add(toppos, back_dir)
  122. local fnode = minetest.get_node(frontpos)
  123. if fnode ~= nil and fnode.name == "air" then
  124. local fhash = minetest.hash_node_position(frontpos)
  125. weirding.just_placed[fhash] = true
  126. minetest.swap_node(frontpos, tnode)
  127. minetest.set_node(toppos, {name="air"})
  128. minetest.check_for_falling(frontpos)
  129. return
  130. end
  131. end,
  132. })
  133. --[[
  134. minetest.register_node("weirding:rotator", {
  135. description = "rotator",
  136. tiles = {
  137. "default_dirt.png^weirding_green_circle_arrow.png",
  138. "default_dirt.png",
  139. "default_dirt.png",
  140. "default_dirt.png",
  141. "default_dirt.png",
  142. "default_dirt.png",
  143. },
  144. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  145. is_ground_content = false,
  146. paramtype2 = "facedir",
  147. on_place = minetest.rotate_node,
  148. sounds = default.node_sound_dirt_defaults({
  149. footstep = {name = "default_stone_footstep", gain = 0.25},
  150. }),
  151. })
  152. minetest.register_abm({
  153. nodenames = {"weirding:rotator"},
  154. -- neighbors = {"group:soil"},
  155. interval = 10000000,
  156. chance = 1,
  157. -- catch_up = true,
  158. action = function(pos)
  159. local node = minetest.get_node(pos)
  160. local back_dir = minetest.facedir_to_dir(node.param2)
  161. local top_dir = ({[0]={x=0, y=1, z=0},
  162. {x=0, y=0, z=1},
  163. {x=0, y=0, z=-1},
  164. {x=1, y=0, z=0},
  165. {x=-1, y=0, z=0},
  166. {x=0, y=-1, z=0}})[math.floor(node.param2/4)]
  167. local right_dir = vector.cross(top_dir, back_dir)
  168. -- local front_dir = vector.multiply(back_dir, -1)
  169. local backpos = vector.add(pos, back_dir)
  170. local i = 0
  171. while true do
  172. local frontpos = vector.add(pos, right_dir)
  173. local bnode = minetest.get_node(backpos)
  174. if bnode == nil or bnode.name == "air" then
  175. return
  176. end
  177. local bhash = minetest.hash_node_position(backpos)
  178. if weirding.just_placed[bhash] == true then
  179. return
  180. end
  181. local fnode = minetest.get_node(frontpos)
  182. if fnode ~= nil and fnode.name ~= "air" then
  183. return
  184. end
  185. local fhash = minetest.hash_node_position(frontpos)
  186. weirding.just_placed[fhash] = true
  187. minetest.swap_node(frontpos, bnode)
  188. minetest.set_node(backpos, {name="air"})
  189. break
  190. end
  191. end,
  192. })
  193. ]]
  194. minetest.register_abm({
  195. nodenames = {"weirding:rotator"},
  196. -- neighbors = {"group:soil"},
  197. interval = 1,
  198. chance = 1,
  199. -- catch_up = true,
  200. action = function(pos)
  201. local node = minetest.get_node(pos)
  202. local back_dir = minetest.facedir_to_dir(node.param2)
  203. local backpos = vector.add(pos, back_dir)
  204. local front_dir = vector.cross(back_dir, -1)
  205. local frontpos = vector.add(pos, front_dir)
  206. local bnode = minetest.get_node(backpos)
  207. if bnode == nil or bnode.name == "air" then
  208. return
  209. end
  210. local bhash = minetest.hash_node_position(backpos)
  211. if weirding.just_placed[bhash] == true then
  212. return
  213. end
  214. local fnode = minetest.get_node(frontpos)
  215. if fnode ~= nil and fnode.name ~= "air" then
  216. return
  217. end
  218. local fhash = minetest.hash_node_position(frontpos)
  219. weirding.just_placed[fhash] = true
  220. minetest.swap_node(frontpos, bnode)
  221. minetest.set_node(backpos, {name="air"})
  222. end,
  223. })
  224. minetest.register_node("weirding:eater", {
  225. description = "eater",
  226. tiles = {
  227. "default_dirt.png^weirding_red_arrow.png",
  228. "default_dirt.png",
  229. "default_dirt.png",
  230. "default_dirt.png",
  231. "default_dirt.png",
  232. "default_dirt.png",
  233. },
  234. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  235. is_ground_content = false,
  236. paramtype2 = "facedir",
  237. on_place = minetest.rotate_node,
  238. sounds = default.node_sound_dirt_defaults({
  239. footstep = {name = "default_stone_footstep", gain = 0.25},
  240. }),
  241. })
  242. minetest.register_abm({
  243. nodenames = {"weirding:eater"},
  244. -- neighbors = {"group:soil"},
  245. interval = 1,
  246. chance = 1,
  247. -- catch_up = true,
  248. action = function(pos)
  249. local node = minetest.get_node(pos)
  250. local front_dir = minetest.facedir_to_dir(node.param2)
  251. local frontpos = vector.add(pos, front_dir)
  252. local back_dir = vector.multiply(front_dir, -1)
  253. local backpos = vector.add(pos, back_dir)
  254. local bnode = minetest.get_node(backpos)
  255. if bnode == nil or bnode.name == "air" then
  256. return
  257. end
  258. local bhash = minetest.hash_node_position(backpos)
  259. if weirding.just_placed[bhash] == true then
  260. return
  261. end
  262. local meta = minetest.get_meta(frontpos)
  263. if meta == nil then return end
  264. local inv = meta:get_inventory()
  265. if inv == nil then return end
  266. if inv:get_size("main") == 0 then return end
  267. local drops = minetest.get_node_drops(bnode.name, nil)
  268. for _,v in ipairs(drops) do
  269. inv:add_item("main", v)
  270. end
  271. minetest.set_node(backpos, {name="air"})
  272. end,
  273. })
  274. minetest.register_node("weirding:green_node", {
  275. description = "green node",
  276. tiles = {
  277. "default_dirt.png^[colorize:green:120",
  278. },
  279. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  280. is_ground_content = false,
  281. -- paramtype2 = "facedir",
  282. -- on_place = minetest.rotate_node,
  283. sounds = default.node_sound_dirt_defaults({
  284. footstep = {name = "default_glass_footstep", gain = 0.25},
  285. }),
  286. })
  287. minetest.register_node("weirding:purple_node", {
  288. description = "purple node",
  289. tiles = {
  290. "default_dirt.png^[colorize:purple:120",
  291. },
  292. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  293. is_ground_content = false,
  294. -- paramtype2 = "facedir",
  295. -- on_place = minetest.rotate_node,
  296. sounds = default.node_sound_dirt_defaults({
  297. footstep = {name = "default_glass_footstep", gain = 0.25},
  298. }),
  299. })
  300. minetest.register_node("weirding:orange_node", {
  301. description = "orange node",
  302. tiles = {
  303. "default_dirt.png^[colorize:orange:120",
  304. },
  305. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  306. is_ground_content = false,
  307. -- paramtype2 = "facedir",
  308. -- on_place = minetest.rotate_node,
  309. sounds = default.node_sound_dirt_defaults({
  310. footstep = {name = "default_glass_footstep", gain = 0.25},
  311. }),
  312. })
  313. minetest.register_node("weirding:red_node", {
  314. description = "red node",
  315. tiles = {
  316. "default_dirt.png^[colorize:red:120",
  317. },
  318. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  319. is_ground_content = false,
  320. -- paramtype2 = "facedir",
  321. -- on_place = minetest.rotate_node,
  322. sounds = default.node_sound_dirt_defaults({
  323. footstep = {name = "default_glass_footstep", gain = 0.25},
  324. }),
  325. })
  326. minetest.register_node("weirding:yellow_node", {
  327. description = "yellow node",
  328. tiles = {
  329. "default_dirt.png^[colorize:yellow:120",
  330. },
  331. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  332. is_ground_content = false,
  333. -- paramtype2 = "facedir",
  334. -- on_place = minetest.rotate_node,
  335. sounds = default.node_sound_dirt_defaults({
  336. footstep = {name = "default_glass_footstep", gain = 0.25},
  337. }),
  338. })
  339. minetest.register_node("weirding:gray_node", {
  340. description = "gray node",
  341. tiles = {
  342. "default_dirt.png^[colorize:gray:120",
  343. },
  344. groups = {cracky = 1, oddly_breakable_by_hand = 3},
  345. is_ground_content = false,
  346. -- paramtype2 = "facedir",
  347. -- on_place = minetest.rotate_node,
  348. sounds = default.node_sound_dirt_defaults({
  349. footstep = {name = "default_glass_footstep", gain = 0.25},
  350. }),
  351. on_timer = function(pos)
  352. local meta = minetest.get_meta(pos)
  353. local n = meta:get_string("name")
  354. if n ~= "" then
  355. minetest.set_node(pos, {
  356. name = n,
  357. param2 = meta:get_int("param2"),
  358. })
  359. end
  360. end,
  361. })
  362. local function set_later(pos, name)
  363. local n = name
  364. local p = {x=pos.x, y=pos.y, z=pos.z}
  365. minetest.after(1, function()
  366. minetest.set_node(p, {name=n})
  367. end)
  368. end
  369. local function set_now(pos, name)
  370. minetest.set_node(pos, {name=name})
  371. end
  372. minetest.register_abm({
  373. nodenames = {"weirding:red_node"},
  374. neighbors = {"weirding:red_node"},
  375. interval = 2,
  376. chance = 1,
  377. -- catch_up = true,
  378. action = function(pos)
  379. local node = minetest.get_node(pos)
  380. if node.name ~= "weirding:red_node" then return end
  381. -- local bhash = minetest.hash_node_position(pos)
  382. -- if weirding.just_placed[bhash] == true then
  383. -- return
  384. -- end
  385. -- weirding.just_placed[bhash] = true
  386. local red_nodes = minetest.find_nodes_in_area(
  387. {x=pos.x - 1, y=pos.y - 1, z=pos.z - 1},
  388. {x=pos.x + 1, y=pos.y + 1, z=pos.z + 1},
  389. {"weirding:red_node"}
  390. )
  391. -- if #red_nodes >= 4 then
  392. -- minetest.set_node(pos, {name="weirding:green_node"})
  393. -- return
  394. -- end
  395. for _,rnp in ipairs(red_nodes) do
  396. -- local bhash = minetest.hash_node_position(rnp)
  397. -- if weirding.just_placed[bhash] == true then
  398. -- return
  399. -- end
  400. -- weirding.just_placed[bhash] = true
  401. local d = vector.subtract(pos, rnp);
  402. local opp = vector.add(pos, d);
  403. local on = minetest.get_node(opp)
  404. if on ~= nil and (on.name == "air" or minetest.registered_nodes[on.name].buildable_to) then
  405. -- minetest.set_node(rnp, {name="air"})
  406. set_later(opp, "weirding:red_node")
  407. end
  408. end
  409. --
  410. -- minetest.set_node(pos, {name="weirding:red_node"})
  411. end,
  412. })
  413. minetest.register_abm({
  414. nodenames = {"weirding:yellow_node"},
  415. neighbors = {"weirding:red_node"},
  416. interval = 2,
  417. chance = 1,
  418. -- catch_up = true,
  419. action = function(pos)
  420. local node = minetest.get_node(pos)
  421. if node.name ~= "weirding:yellow_node" then return end
  422. local red_nodes = minetest.find_nodes_in_area(
  423. {x=pos.x - 1, y=pos.y - 1, z=pos.z - 1},
  424. {x=pos.x + 1, y=pos.y + 1, z=pos.z + 1},
  425. {"weirding:red_node"}
  426. )
  427. for _,rnp in ipairs(red_nodes) do
  428. local bhash = minetest.hash_node_position(rnp)
  429. if weirding.just_placed[bhash] == true then
  430. return
  431. end
  432. weirding.just_placed[bhash] = true
  433. local d = vector.subtract(pos, rnp);
  434. local opp = vector.add(pos, d);
  435. local on = minetest.get_node(opp)
  436. if on ~= nil and on.name ~= "air" then
  437. -- minetest.set_node(rnp, {name="air"})
  438. minetest.set_node(rnp, {name="weirding:gray_node"})
  439. local meta = minetest.get_meta(rnp)
  440. meta:set_string("name", on.name)
  441. meta:set_int("param2", on.param2)
  442. local t = minetest.get_node_timer(rnp)
  443. t:start(10)
  444. end
  445. end
  446. end,
  447. })
  448. minetest.register_abm({
  449. nodenames = {"weirding:gray_node"},
  450. neighbors = {"weirding:red_node"},
  451. interval = 1,
  452. chance = 1,
  453. -- catch_up = true,
  454. action = function(pos)
  455. local node = minetest.get_node(pos)
  456. if node.name ~= "weirding:gray_node" then return end
  457. local nmeta = minetest.get_meta(pos)
  458. local param2 = nmeta:get_int("param2")
  459. local name = nmeta:get_string("name")
  460. local red_nodes = minetest.find_nodes_in_area(
  461. {x=pos.x - 1, y=pos.y - 1, z=pos.z - 1},
  462. {x=pos.x + 1, y=pos.y + 1, z=pos.z + 1},
  463. {"weirding:red_node"}
  464. )
  465. for _,rnp in ipairs(red_nodes) do
  466. -- minetest.set_node(rnp, {name="air"})
  467. minetest.set_node(rnp, {name="weirding:gray_node"})
  468. local meta = minetest.get_meta(rnp)
  469. meta:set_string("name", name)
  470. meta:set_int("param2", param2)
  471. local t = minetest.get_node_timer(rnp)
  472. t:start(10)
  473. end
  474. end,
  475. })
  476. -- clear
  477. minetest.register_abm({
  478. nodenames = {"weirding:green_node", "weirding:red_node"},
  479. interval = 100000000,
  480. chance = 1,
  481. -- catch_up = true,
  482. action = function(pos)
  483. -- minetest.set_node(pos, {name="air"})
  484. end,
  485. })
  486. minetest.register_abm({
  487. nodenames = {"weirding:green_node"},
  488. neighbors = {"weirding:purple_node"},
  489. interval = 2,
  490. chance = 1,
  491. -- catch_up = true,
  492. action = function(pos)
  493. local node = minetest.get_node(pos)
  494. if node.name ~= "weirding:green_node" then
  495. -- minetest.set_node(pos, {name="air"})
  496. return
  497. end
  498. local bhash = minetest.hash_node_position(pos)
  499. if weirding.just_placed[bhash] == true then
  500. return
  501. end
  502. local purple_nodes = minetest.find_nodes_in_area(
  503. {x=pos.x - 1, y=pos.y - 1, z=pos.z - 1},
  504. {x=pos.x + 1, y=pos.y + 1, z=pos.z + 1},
  505. {"weirding:purple_node"}
  506. )
  507. if #purple_nodes ~= 1 then
  508. minetest.set_node(pos, {name="default:glass"})
  509. return
  510. end
  511. local pu_pos = purple_nodes[1]
  512. local orange_nodes = minetest.find_nodes_in_area(
  513. {x=pu_pos.x - 1, y=pu_pos.y - 1, z=pu_pos.z - 1},
  514. {x=pu_pos.x + 1, y=pu_pos.y + 1, z=pu_pos.z + 1},
  515. {"weirding:orange_node"}
  516. )
  517. if #orange_nodes ~= 1 then
  518. minetest.set_node(pos, {name="default:glass"})
  519. return
  520. end
  521. local or_pos = orange_nodes[1]
  522. local mvdir = vector.subtract(pos, pu_pos)
  523. local or_next_pos = vector.add(or_pos, mvdir)
  524. local orn = minetest.get_node(or_next_pos)
  525. if orn and not minetest.registered_nodes[orn.name].buildable_to then
  526. minetest.set_node(pos, {name="default:glass"})
  527. return
  528. end
  529. local dir = vector.subtract(pu_pos, or_pos)
  530. -- local inv_dir = vector.multiply(dir, -1)
  531. -- from the corner to behind
  532. local source_pos_a = vector.add(pos, mvdir)
  533. local target_pos_a = vector.add(source_pos_a, dir)
  534. local src_a = minetest.get_node(source_pos_a)
  535. local tar_a = minetest.get_node(target_pos_a)
  536. minetest.swap_node(source_pos_a, tar_a)
  537. minetest.swap_node(target_pos_a, src_a)
  538. local mvdir = vector.subtract(pos, pu_pos)
  539. minetest.set_node(pu_pos, {name="air"})
  540. minetest.set_node(or_pos, {name="air"})
  541. minetest.set_node(pos, {name="air"})
  542. set_now(vector.add(pu_pos, mvdir), "weirding:purple_node")
  543. set_now(vector.add(or_pos, mvdir), "weirding:orange_node")
  544. set_now(vector.add(pos, mvdir), "weirding:green_node")
  545. weirding.just_placed[minetest.hash_node_position(vector.add(pos, mvdir))] = true
  546. end,
  547. })