tvine.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. -- Twisted Vine Mod
  2. -- Idea and textures by alauer
  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. -- Manually placed vines are not climbable.
  284. selection_box = {
  285. type = "fixed",
  286. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  287. },
  288. groups = utility.dig_groups("plant", {flammable = 2}),
  289. sounds = default.node_sound_leaves_defaults(),
  290. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  291. on_construct = function(...) return tvine.on_display_construct(...) end,
  292. })
  293. minetest.register_node("default:tvine_seed", {
  294. description = "Twisted Vine Seed",
  295. tiles = {"tvine_seed.png"},
  296. wield_image = "tvine_seed.png",
  297. inventory_image = "tvine_seed.png",
  298. drawtype = "signlike",
  299. paramtype = "light",
  300. paramtype2 = "wallmounted",
  301. walkable = false,
  302. sunlight_propagates = true,
  303. selection_box = {
  304. type = "fixed",
  305. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}
  306. },
  307. groups = utility.dig_groups("seeds", {seed = 1, attached_node = 1, flammable = 2, notify_destruct = 1}),
  308. on_place = function(itemstack, placer, pointed_thing)
  309. return farming.place_seed(itemstack, placer, pointed_thing, "default:tvine_seed")
  310. end,
  311. soil_nodes = {
  312. "default:dirt_with_grass",
  313. "default:dirt",
  314. "moregrass:darkgrass",
  315. "talinite:ore",
  316. "talinite:desert_ore",
  317. },
  318. on_timer = function(...) return farming.grow_plant(...) end,
  319. minlight = tvine.minlight,
  320. maxlight = tvine.maxlight,
  321. next_plant = {"default:tvine_stunted"},
  322. sounds = default.node_sound_dirt_defaults({
  323. dug = {name = "default_grass_footstep", gain = 0.2},
  324. place = {name = "default_place_node", gain = 0.25},
  325. }),
  326. farming_minerals_unused = true,
  327. })
  328. -- This version provides seeds!
  329. minetest.register_node("default:tvine_stunted", {
  330. description = "Twisted Vine (Hacker!)",
  331. drawtype = "plantlike",
  332. tiles = {"default_tvine_display.png"},
  333. inventory_image = "default_tvine.png",
  334. wield_image = "default_tvine.png",
  335. paramtype = "light",
  336. paramtype2 = "degrotate",
  337. sunlight_propagates = true,
  338. light_source = tvine.light_source,
  339. walkable = false,
  340. -- Manually placed vines are not climbable.
  341. selection_box = {
  342. type = "fixed",
  343. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  344. },
  345. drop = "default:tvine_seed",
  346. groups = utility.dig_groups("plant", {flammable = 2}),
  347. sounds = default.node_sound_leaves_defaults(),
  348. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  349. next_plant = {"default:tvine_top", "default:tvine_top_alt"},
  350. on_timer = function(...) return farming.grow_plant(...) end,
  351. minlight = tvine.minlight,
  352. maxlight = tvine.maxlight,
  353. soil_nodes = {
  354. "default:dirt_with_grass",
  355. "default:dirt",
  356. "moregrass:darkgrass",
  357. "talinite:ore",
  358. "talinite:desert_ore",
  359. },
  360. farming_minerals_unused = true,
  361. farming_growing_time_min = 60*10,
  362. farming_growing_time_max = 60*40,
  363. })
  364. stalk_drops = {
  365. max_items = 1,
  366. items = {
  367. {items = {'talinite:dust'}, rarity = 3},
  368. },
  369. }
  370. minetest.register_node("default:tvine", {
  371. description = "Twisted Vine (Hacker!)",
  372. drawtype = "plantlike",
  373. tiles = {"default_tvine.png"},
  374. inventory_image = "default_tvine.png",
  375. wield_image = "default_tvine.png",
  376. paramtype = "light",
  377. paramtype2 = "degrotate",
  378. sunlight_propagates = true,
  379. light_source = tvine.light_source,
  380. walkable = false,
  381. climbable = true,
  382. drop = stalk_drops,
  383. selection_box = {
  384. type = "fixed",
  385. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  386. },
  387. groups = utility.dig_groups("plant", {flammable = 2}),
  388. sounds = default.node_sound_leaves_defaults(),
  389. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  390. on_construct = function(...)
  391. return tvine.on_construct(...)
  392. end,
  393. on_destruct = function(...)
  394. return tvine.on_destruct(...)
  395. end,
  396. on_timer = function(...)
  397. return tvine.on_timer(...)
  398. end,
  399. after_dig_node = function(...)
  400. return tvine.after_dig_node(...)
  401. end,
  402. })
  403. minetest.register_node("default:tvine_alt", {
  404. description = "Twisted Vine (Hacker!)",
  405. drawtype = "plantlike",
  406. tiles = {"default_tvine_alt.png"},
  407. inventory_image = "default_tvine.png",
  408. wield_image = "default_tvine.png",
  409. paramtype = "light",
  410. paramtype2 = "degrotate",
  411. sunlight_propagates = true,
  412. light_source = tvine.light_source,
  413. walkable = false,
  414. climbable = true,
  415. drop = stalk_drops,
  416. selection_box = {
  417. type = "fixed",
  418. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  419. },
  420. groups = utility.dig_groups("plant", {flammable = 2}),
  421. sounds = default.node_sound_leaves_defaults(),
  422. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  423. on_construct = function(...)
  424. return tvine.on_construct(...)
  425. end,
  426. on_destruct = function(...)
  427. return tvine.on_destruct(...)
  428. end,
  429. on_timer = function(...)
  430. return tvine.on_timer(...)
  431. end,
  432. after_dig_node = function(...)
  433. return tvine.after_dig_node(...)
  434. end,
  435. })
  436. top_drops = {
  437. max_items = 2,
  438. items = {
  439. {items = {'default:tvine_display'}, rarity = 5},
  440. {items = {'default:tvine_seed'}},
  441. {items = {'default:tvine_seed'}, rarity = 10},
  442. },
  443. }
  444. minetest.register_node("default:tvine_top", {
  445. description = "Twisted Vine (Hacker!)",
  446. drawtype = "plantlike",
  447. tiles = {"default_tvine_top.png"},
  448. inventory_image = "default_tvine.png",
  449. wield_image = "default_tvine.png",
  450. paramtype = "light",
  451. paramtype2 = "degrotate",
  452. sunlight_propagates = true,
  453. light_source = tvine.light_source,
  454. walkable = false,
  455. drop = top_drops,
  456. selection_box = {
  457. type = "fixed",
  458. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  459. },
  460. groups = utility.dig_groups("plant", {flammable = 2}),
  461. sounds = default.node_sound_leaves_defaults(),
  462. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  463. on_construct = function(...)
  464. return tvine.on_construct(...)
  465. end,
  466. on_destruct = function(...)
  467. return tvine.on_destruct(...)
  468. end,
  469. on_timer = function(...)
  470. return tvine.on_timer(...)
  471. end,
  472. after_dig_node = function(...)
  473. return tvine.after_dig_node(...)
  474. end,
  475. -- Instruct farming mod to restart the timer.
  476. -- Otherwise, after growing the last plant, the timer would halt.
  477. farming_restart_timer = true,
  478. farming_growing_time_min = tvine.steptime.min,
  479. farming_growing_time_max = tvine.steptime.max,
  480. })
  481. minetest.register_node("default:tvine_top_alt", {
  482. description = "Twisted Vine (Hacker!)",
  483. drawtype = "plantlike",
  484. tiles = {"default_tvine_top_alt.png"},
  485. inventory_image = "default_tvine.png",
  486. wield_image = "default_tvine.png",
  487. paramtype = "light",
  488. paramtype2 = "degrotate",
  489. sunlight_propagates = true,
  490. light_source = tvine.light_source,
  491. walkable = false,
  492. drop = top_drops,
  493. selection_box = {
  494. type = "fixed",
  495. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  496. },
  497. groups = utility.dig_groups("plant", {flammable = 2}),
  498. sounds = default.node_sound_leaves_defaults(),
  499. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  500. on_construct = function(...)
  501. return tvine.on_construct(...)
  502. end,
  503. on_destruct = function(...)
  504. return tvine.on_destruct(...)
  505. end,
  506. on_timer = function(...)
  507. return tvine.on_timer(...)
  508. end,
  509. after_dig_node = function(...)
  510. return tvine.after_dig_node(...)
  511. end,
  512. -- Instruct farming mod to restart the timer.
  513. -- Otherwise, after growing the last plant, the timer would halt.
  514. farming_restart_timer = true,
  515. farming_growing_time_min = tvine.steptime.min,
  516. farming_growing_time_max = tvine.steptime.max,
  517. })
  518. local c = "tvine:core"
  519. local f = tvine.modpath .. "/tvine.lua"
  520. reload.register_file(c, f, false)
  521. tvine.run_once = true
  522. end