tvine.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. -- Twisted Vine Mod
  2. -- Idea and textures by alauer/Dresdan
  3. -- Code by MustTest
  4. tvine = tvine or {}
  5. tvine.modpath = minetest.get_modpath("default")
  6. tvine.steptime = {min=60*5, max=60*20}
  7. --tvine.steptime = {min=1, max=1}
  8. tvine.maxheight = 16
  9. tvine.minlight = 10
  10. tvine.maxlight = 15
  11. tvine.light_source = 9
  12. -- Localize for performance.
  13. local math_floor = math.floor
  14. local math_random = math.random
  15. function tvine.is_plant_name(name)
  16. if name == "default:tvine" then
  17. return true
  18. elseif name == "default:tvine_alt" then
  19. return true
  20. elseif name == "default:tvine_top" then
  21. return true
  22. elseif name == "default:tvine_top_alt" then
  23. return true
  24. end
  25. return false
  26. end
  27. function tvine.is_stalk_name(name)
  28. if name == "default:tvine" then
  29. return true
  30. elseif name == "default:tvine_alt" then
  31. return true
  32. end
  33. return false
  34. end
  35. function tvine.is_top_name(name)
  36. if name == "default:tvine_top" then
  37. return true
  38. elseif name == "default:tvine_top_alt" then
  39. return true
  40. end
  41. return false
  42. end
  43. -- Should return a random height for an individual plant to grow.
  44. function tvine.random_height()
  45. local m = tvine.maxheight
  46. local h = math_floor(m / 2)
  47. local x = 2
  48. if h < x then h = x end
  49. if m < h then m = h end
  50. return math_floor(math_random(math_random(x, h), math_random(h, m)))
  51. end
  52. function tvine.is_dirt_name(name)
  53. if name == "default:dirt_with_grass" or
  54. name == "default:dirt" or
  55. name == "moregrass:darkgrass" or
  56. name == "talinite:ore" or
  57. name == "talinite:desert_ore" or
  58. minetest.get_item_group(name, "soil") > 1 then
  59. return true
  60. end
  61. end
  62. function tvine.has_dirt(pos)
  63. local p = vector.add(pos, {x=0, y=-1, z=0})
  64. local name = minetest.get_node(p).name
  65. -- Must be on dirt or grass.
  66. if tvine.is_dirt_name(name) then
  67. return true
  68. end
  69. end
  70. function tvine.can_grow(pos)
  71. -- Must have dirt nearby.
  72. if not tvine.has_dirt(pos) then
  73. return
  74. end
  75. -- Must have water nearby.
  76. local p = vector.add(pos, {x=0, y=-1, z=0})
  77. if not minetest.find_node_near(p, 3, {"group:water"}) then
  78. return
  79. end
  80. -- Also needs minerals.
  81. if not minetest.find_node_near(p, 3, {"glowstone:minerals"}) then
  82. return
  83. end
  84. return true
  85. end
  86. -- Obtain growth height from soil, initializing it if not done yet.
  87. function tvine.get_grow_height(pos)
  88. local meta = minetest.get_meta({x=pos.x, y=pos.y-1, z=pos.z})
  89. local maxh = meta:get_int("tvine_height")
  90. if maxh == 0 then
  91. maxh = tvine.random_height()
  92. meta:set_int("tvine_height", maxh)
  93. meta:mark_as_private("tvine_height")
  94. end
  95. return maxh
  96. end
  97. -- Should be called when plant is dug.
  98. function tvine.reset_grow_height_and_timer(pos)
  99. -- Find soil node below plant.
  100. local p = vector.new(pos)
  101. local name = minetest.get_node(p).name
  102. local d = 0
  103. while not tvine.is_dirt_name(name) and d < tvine.maxheight do
  104. -- All except bottom-most node must be plant.
  105. if not tvine.is_plant_name(name) then
  106. return
  107. end
  108. p.y = p.y - 1
  109. d = d + 1
  110. name = minetest.get_node(p).name
  111. end
  112. -- Must be on dirt or grass.
  113. if tvine.is_dirt_name(name) then
  114. local meta = minetest.get_meta(p)
  115. local maxh = tvine.random_height()
  116. meta:set_int("tvine_height", maxh)
  117. meta:mark_as_private("tvine_height")
  118. else
  119. return
  120. end
  121. -- Restart timer for plant directly above soil.
  122. p.y = p.y + 1
  123. if not tvine.is_plant_name(minetest.get_node(p).name) then
  124. return
  125. end
  126. local min = tvine.steptime.min
  127. local max = tvine.steptime.max
  128. minetest.get_node_timer(p):start(math_random(min, max))
  129. end
  130. -- Attempt to grow tvine.
  131. -- Return 0 means nothing to report.
  132. -- 10 means plant has reached max height.
  133. function tvine.grow(pos, node)
  134. -- Check if we can grow.
  135. if not tvine.can_grow(pos) then
  136. return 0
  137. end
  138. if minetest.find_node_near(pos, 2, "group:cold") then
  139. return 12
  140. end
  141. -- Get how high we can grow.
  142. local maxh = tvine.get_grow_height(pos)
  143. -- Find current height of plant.
  144. local height = 0
  145. while tvine.is_plant_name(node.name) and height < maxh do
  146. height = height + 1
  147. pos.y = pos.y + 1
  148. node = minetest.get_node(pos)
  149. end
  150. if height >= maxh then
  151. -- Plant has reached max height.
  152. return 10
  153. end
  154. -- Check if we have room to grow some more.
  155. if not (node.name == "air") then
  156. return 0
  157. end
  158. -- Check if we have enough light.
  159. if minetest.get_node_light(pos) < tvine.minlight then
  160. return 0
  161. end
  162. -- Grow!
  163. local p2 = {x=pos.x, y=pos.y-1, z=pos.z}
  164. local n2 = minetest.get_node(p2)
  165. if tvine.is_plant_name(n2.name) then
  166. minetest.swap_node(p2, {name = tvine.chose_bot_name(p2), param2=n2.param2})
  167. end
  168. minetest.swap_node(pos, {name = tvine.chose_top_name(pos), param2=n2.param2})
  169. return 0
  170. end
  171. function tvine.chose_name(pos)
  172. local p = {x=pos.x, y=pos.y-1, z=pos.z}
  173. local n = minetest.get_node(p).name
  174. if n == "default:tvine" then
  175. return "default:tvine_alt"
  176. elseif n == "default:tvine_alt" then
  177. return "default:tvine"
  178. end
  179. return "default:tvine"
  180. end
  181. function tvine.chose_top_name(pos)
  182. local p = {x=pos.x, y=pos.y-1, z=pos.z}
  183. local n = minetest.get_node(p).name
  184. if n == "default:tvine" then
  185. return "default:tvine_top_alt"
  186. elseif n == "default:tvine_alt" then
  187. return "default:tvine_top"
  188. end
  189. return "default:tvine"
  190. end
  191. function tvine.chose_bot_name(pos)
  192. local p = {x=pos.x, y=pos.y-1, z=pos.z}
  193. local n = minetest.get_node(p).name
  194. if n == "default:tvine" then
  195. return "default:tvine_alt"
  196. elseif n == "default:tvine_alt" then
  197. return "default:tvine"
  198. elseif n == "default:tvine_top" then
  199. return "default:tvine_alt"
  200. elseif n == "default:tvine_top_alt" then
  201. return "default:tvine"
  202. end
  203. if math_random(1, 2) == 1 then
  204. return "default:tvine"
  205. end
  206. return "default:tvine_alt"
  207. end
  208. function tvine.on_construct(pos)
  209. local p = {x=pos.x, y=pos.y-1, z=pos.z}
  210. local n = minetest.get_node(p)
  211. if not tvine.is_plant_name(n.name) then
  212. if math_random(1, 2) == 1 then
  213. minetest.swap_node(pos, {name="default:tvine_top", param2=math_random(0, 239)})
  214. else
  215. minetest.swap_node(pos, {name="default:tvine_top_alt", param2=math_random(0, 239)})
  216. end
  217. else
  218. local n2 = minetest.get_node(pos)
  219. n2.param2 = n.param2
  220. minetest.swap_node(pos, n2)
  221. end
  222. end
  223. function tvine.on_seed_timer(pos, elapsed)
  224. if math_random(1, 2) == 1 then
  225. minetest.set_node(pos, {name = "default:tvine_top"})
  226. else
  227. minetest.set_node(pos, {name = "default:tvine_top_alt"})
  228. end
  229. -- Only the ground-level plant piece should have nodetimer.
  230. -- If plant is not placed on soil, it will never have nodetimer.
  231. if tvine.has_dirt(pos) then
  232. local min = tvine.steptime.min
  233. local max = tvine.steptime.max
  234. minetest.get_node_timer(pos):start(math_random(min, max))
  235. end
  236. end
  237. function tvine.on_destruct(pos)
  238. tvine.reset_grow_height_and_timer(pos)
  239. end
  240. function tvine.on_timer(pos, elapsed)
  241. --minetest.chat_send_all("# Server: Plant timer @ " .. minetest.pos_to_string(pos) .. "!")
  242. local node = minetest.get_node(pos)
  243. local result = tvine.grow(pos, node)
  244. -- Plant has reached max height.
  245. if result == 10 then return end
  246. -- Plant cannot grow because of ice.
  247. if result == 12 then return end
  248. local min = tvine.steptime.min
  249. local max = tvine.steptime.max
  250. minetest.get_node_timer(pos):start(math_random(min, max))
  251. --return true
  252. end
  253. function tvine.after_dig_node(pos, node, metadata, digger)
  254. tvine.dig_up(pos, node, digger)
  255. -- No return value.
  256. end
  257. function tvine.dig_up(pos, node, digger)
  258. if digger == nil then return end
  259. local np = {x = pos.x, y = pos.y + 1, z = pos.z}
  260. local nn = minetest.get_node(np)
  261. if tvine.is_plant_name(nn.name) then
  262. minetest.node_dig(np, nn, digger)
  263. end
  264. end
  265. function tvine.on_display_construct(pos)
  266. local n = minetest.get_node(pos)
  267. n.param2 = math_random(0, 239)
  268. minetest.swap_node(pos, n)
  269. end
  270. if not tvine.run_once then
  271. -- This version is for display only - does not grow or provide seeds.
  272. minetest.register_node("default:tvine_display", {
  273. description = "Twisted Vine",
  274. drawtype = "plantlike",
  275. tiles = {"default_tvine_display.png"},
  276. inventory_image = "default_tvine_display.png",
  277. wield_image = "default_tvine_display.png",
  278. paramtype = "light",
  279. paramtype2 = "degrotate",
  280. sunlight_propagates = true,
  281. light_source = tvine.light_source,
  282. walkable = false,
  283. waving = 1,
  284. -- Manually placed vines are not climbable.
  285. selection_box = {
  286. type = "fixed",
  287. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  288. },
  289. groups = utility.dig_groups("plant", {flammable = 2}),
  290. sounds = default.node_sound_leaves_defaults(),
  291. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  292. on_construct = function(...) return tvine.on_display_construct(...) end,
  293. })
  294. minetest.register_node("default:tvine_seed", {
  295. description = "Twisted Vine Seed",
  296. tiles = {"tvine_seed.png"},
  297. wield_image = "tvine_seed.png",
  298. inventory_image = "tvine_seed.png",
  299. drawtype = "signlike",
  300. paramtype = "light",
  301. paramtype2 = "wallmounted",
  302. walkable = false,
  303. sunlight_propagates = true,
  304. selection_box = {
  305. type = "fixed",
  306. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}
  307. },
  308. groups = utility.dig_groups("seeds", {seed = 1, attached_node = 1, flammable = 2, notify_destruct = 1}),
  309. on_place = function(itemstack, placer, pointed_thing)
  310. return farming.place_seed(itemstack, placer, pointed_thing, "default:tvine_seed")
  311. end,
  312. soil_nodes = {
  313. "default:dirt_with_grass",
  314. "default:dirt",
  315. "moregrass:darkgrass",
  316. "talinite:ore",
  317. "talinite:desert_ore",
  318. },
  319. on_timer = function(...) return farming.grow_plant(...) end,
  320. minlight = tvine.minlight,
  321. maxlight = tvine.maxlight,
  322. next_plant = {"default:tvine_stunted"},
  323. sounds = default.node_sound_dirt_defaults({
  324. dug = {name = "default_grass_footstep", gain = 0.2},
  325. place = {name = "default_place_node", gain = 0.25},
  326. }),
  327. farming_minerals_unused = true,
  328. })
  329. -- This version provides seeds!
  330. minetest.register_node("default:tvine_stunted", {
  331. description = "Twisted Vine (Hacker!)",
  332. drawtype = "plantlike",
  333. tiles = {"default_tvine_display.png"},
  334. inventory_image = "default_tvine.png",
  335. wield_image = "default_tvine.png",
  336. paramtype = "light",
  337. paramtype2 = "degrotate",
  338. sunlight_propagates = true,
  339. light_source = tvine.light_source,
  340. walkable = false,
  341. waving = 1,
  342. -- Manually placed vines are not climbable.
  343. selection_box = {
  344. type = "fixed",
  345. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  346. },
  347. drop = "default:tvine_seed",
  348. groups = utility.dig_groups("plant", {flammable = 2}),
  349. sounds = default.node_sound_leaves_defaults(),
  350. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  351. next_plant = {"default:tvine_top", "default:tvine_top_alt"},
  352. on_timer = function(...) return farming.grow_plant(...) end,
  353. minlight = tvine.minlight,
  354. maxlight = tvine.maxlight,
  355. soil_nodes = {
  356. "default:dirt_with_grass",
  357. "default:dirt",
  358. "moregrass:darkgrass",
  359. "talinite:ore",
  360. "talinite:desert_ore",
  361. },
  362. farming_minerals_unused = true,
  363. farming_growing_time_min = 60*10,
  364. farming_growing_time_max = 60*40,
  365. on_collapse_to_entity = function(pos, node)
  366. minetest.add_item(pos, {name="default:tvine_display"})
  367. end,
  368. })
  369. stalk_drops = {
  370. max_items = 1,
  371. items = {
  372. {items = {'talinite:dust'}, rarity = 2},
  373. },
  374. }
  375. minetest.register_node("default:tvine", {
  376. description = "Twisted Vine (Hacker!)",
  377. drawtype = "plantlike",
  378. tiles = {"default_tvine.png"},
  379. inventory_image = "default_tvine.png",
  380. wield_image = "default_tvine.png",
  381. paramtype = "light",
  382. paramtype2 = "degrotate",
  383. sunlight_propagates = true,
  384. light_source = tvine.light_source,
  385. walkable = false,
  386. climbable = true,
  387. drop = stalk_drops,
  388. selection_box = {
  389. type = "fixed",
  390. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  391. },
  392. groups = utility.dig_groups("plant", {flammable = 2}),
  393. sounds = default.node_sound_leaves_defaults(),
  394. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  395. on_construct = function(...)
  396. return tvine.on_construct(...)
  397. end,
  398. on_destruct = function(...)
  399. return tvine.on_destruct(...)
  400. end,
  401. on_timer = function(...)
  402. return tvine.on_timer(...)
  403. end,
  404. after_dig_node = function(...)
  405. return tvine.after_dig_node(...)
  406. end,
  407. on_finish_collapse = function(pos, node)
  408. minetest.swap_node(pos, {name="default:tvine_display", param2=math_random(0, 239)})
  409. end,
  410. on_collapse_to_entity = function(pos, node)
  411. minetest.add_item(pos, {name="default:tvine_display"})
  412. end,
  413. })
  414. minetest.register_node("default:tvine_alt", {
  415. description = "Twisted Vine (Hacker!)",
  416. drawtype = "plantlike",
  417. tiles = {"default_tvine_alt.png"},
  418. inventory_image = "default_tvine.png",
  419. wield_image = "default_tvine.png",
  420. paramtype = "light",
  421. paramtype2 = "degrotate",
  422. sunlight_propagates = true,
  423. light_source = tvine.light_source,
  424. walkable = false,
  425. climbable = true,
  426. drop = stalk_drops,
  427. selection_box = {
  428. type = "fixed",
  429. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  430. },
  431. groups = utility.dig_groups("plant", {flammable = 2}),
  432. sounds = default.node_sound_leaves_defaults(),
  433. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  434. on_construct = function(...)
  435. return tvine.on_construct(...)
  436. end,
  437. on_destruct = function(...)
  438. return tvine.on_destruct(...)
  439. end,
  440. on_timer = function(...)
  441. return tvine.on_timer(...)
  442. end,
  443. after_dig_node = function(...)
  444. return tvine.after_dig_node(...)
  445. end,
  446. on_finish_collapse = function(pos, node)
  447. minetest.swap_node(pos, {name="default:tvine_display", param2=math_random(0, 239)})
  448. end,
  449. on_collapse_to_entity = function(pos, node)
  450. minetest.add_item(pos, {name="default:tvine_display"})
  451. end,
  452. })
  453. top_drops = {
  454. max_items = 2,
  455. items = {
  456. {items = {'default:tvine_display'}, rarity = 5},
  457. {items = {'default:tvine_seed'}},
  458. {items = {'default:tvine_seed'}, rarity = 10},
  459. },
  460. }
  461. minetest.register_node("default:tvine_top", {
  462. description = "Twisted Vine (Hacker!)",
  463. drawtype = "plantlike",
  464. tiles = {"default_tvine_top.png"},
  465. inventory_image = "default_tvine.png",
  466. wield_image = "default_tvine.png",
  467. paramtype = "light",
  468. paramtype2 = "degrotate",
  469. sunlight_propagates = true,
  470. light_source = tvine.light_source,
  471. walkable = false,
  472. waving = 1,
  473. drop = top_drops,
  474. selection_box = {
  475. type = "fixed",
  476. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  477. },
  478. groups = utility.dig_groups("plant", {flammable = 2}),
  479. sounds = default.node_sound_leaves_defaults(),
  480. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  481. on_construct = function(...)
  482. return tvine.on_construct(...)
  483. end,
  484. on_destruct = function(...)
  485. return tvine.on_destruct(...)
  486. end,
  487. on_timer = function(...)
  488. return tvine.on_timer(...)
  489. end,
  490. after_dig_node = function(...)
  491. return tvine.after_dig_node(...)
  492. end,
  493. on_finish_collapse = function(pos, node)
  494. minetest.swap_node(pos, {name="default:tvine_display", param2=math_random(0, 239)})
  495. end,
  496. on_collapse_to_entity = function(pos, node)
  497. minetest.add_item(pos, {name="default:tvine_display"})
  498. end,
  499. -- Instruct farming mod to restart the timer.
  500. -- Otherwise, after growing the last plant, the timer would halt.
  501. farming_restart_timer = true,
  502. farming_growing_time_min = tvine.steptime.min,
  503. farming_growing_time_max = tvine.steptime.max,
  504. })
  505. minetest.register_node("default:tvine_top_alt", {
  506. description = "Twisted Vine (Hacker!)",
  507. drawtype = "plantlike",
  508. tiles = {"default_tvine_top_alt.png"},
  509. inventory_image = "default_tvine.png",
  510. wield_image = "default_tvine.png",
  511. paramtype = "light",
  512. paramtype2 = "degrotate",
  513. sunlight_propagates = true,
  514. light_source = tvine.light_source,
  515. walkable = false,
  516. waving = 1,
  517. drop = top_drops,
  518. selection_box = {
  519. type = "fixed",
  520. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  521. },
  522. groups = utility.dig_groups("plant", {flammable = 2}),
  523. sounds = default.node_sound_leaves_defaults(),
  524. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  525. on_construct = function(...)
  526. return tvine.on_construct(...)
  527. end,
  528. on_destruct = function(...)
  529. return tvine.on_destruct(...)
  530. end,
  531. on_timer = function(...)
  532. return tvine.on_timer(...)
  533. end,
  534. after_dig_node = function(...)
  535. return tvine.after_dig_node(...)
  536. end,
  537. on_finish_collapse = function(pos, node)
  538. minetest.swap_node(pos, {name="default:tvine_display", param2=math_random(0, 239)})
  539. end,
  540. on_collapse_to_entity = function(pos, node)
  541. minetest.add_item(pos, {name="default:tvine_display"})
  542. end,
  543. -- Instruct farming mod to restart the timer.
  544. -- Otherwise, after growing the last plant, the timer would halt.
  545. farming_restart_timer = true,
  546. farming_growing_time_min = tvine.steptime.min,
  547. farming_growing_time_max = tvine.steptime.max,
  548. })
  549. minetest.register_craft({
  550. type = "extracting",
  551. output = 'talinite:dust 4',
  552. recipe = 'default:tvine_display',
  553. time = 5,
  554. })
  555. local c = "tvine:core"
  556. local f = tvine.modpath .. "/tvine.lua"
  557. reload.register_file(c, f, false)
  558. tvine.run_once = true
  559. end