sapling.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. plants.saplings = {}
  2. local function plants_sapling_formspec(name)
  3. local data = plants.saplings[name]
  4. local trunk_node = 'levels:tree_1'
  5. local leaf_node = 'levels:leaves_1'
  6. local trunk_height = 3
  7. local height = 7
  8. if data then
  9. trunk_node = data['trunk_node']
  10. leaf_node = data['leaf_node']
  11. trunk_height = data['trunk_height']
  12. height = data['height']
  13. end
  14. local formspec =
  15. 'formspec_version[3]'..
  16. 'size[8,8]'..
  17. 'textarea[.5,.25;7,1;;;Set your tree options below and click save. Place the sapling to generate your tree.]'..
  18. 'field[.5,1.5;7,.5;trunk_n;Trunk Node;'..trunk_node..']'..
  19. 'field[.5,2.5;7,.5;leaf_n;Leaf Node;'..leaf_node..']'..
  20. 'field[.5,3.5;3,.5;height_overall;Overall Height;'..height..']'..
  21. 'field[4.5,3.5;3,.5;height_trunk;Trunk Height;'..trunk_height..']'..
  22. 'button_exit[3,6.5;2,1;save;Save]'
  23. return formspec
  24. end
  25. minetest.register_craftitem('plants:sapling', {
  26. description = 'Tree Tool',
  27. inventory_image = 'plants_sapling.png',
  28. stack_max = 1,
  29. on_drop = lobby.no_drop,
  30. on_place = function(itemstack, placer, pointed_thing)
  31. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  32. if minetest.is_protected(pos, placer:get_player_name()) then
  33. minetest.chat_send_player(placer:get_player_name(), 'Sorry, can\'t do that here.')
  34. else
  35. local name = placer:get_player_name()
  36. local data = plants.saplings[name]
  37. if data then
  38. local trunk_node = data['trunk_node'] or 'levels:tree_1'
  39. local leaf_node = data['leaf_node'] or 'levels:leaves_1'
  40. local trunk_height = tonumber(data['trunk_height']) or 3
  41. local height = math.max((tonumber(data['height']) or 7), 21)
  42. local leaf_height = height - trunk_height
  43. local leaf_width = math.max(3, math.ceil(leaf_height/2))
  44. if leaf_width % 2 == 0 then
  45. leaf_width = leaf_width + 1
  46. end
  47. local crown_width = math.floor(leaf_width/2)
  48. local leaf_base = {x = pos.x, y = pos.y+trunk_height, z = pos.z}
  49. local leaf_top = {x = pos.x, y = pos.y+(height-1), z = pos.z}
  50. local crown1 = {x = pos.x-crown_width, y = pos.y+(height-1), z = pos.z-crown_width}
  51. local crown2 = {x = pos.x+crown_width, y = pos.y+(height-1), z = pos.z+crown_width}
  52. worldedit.cube(leaf_base, leaf_width, (leaf_height-1), leaf_width, leaf_node, false)
  53. worldedit.set(crown1, crown2, {leaf_node, 'air'})
  54. local i = 1
  55. minetest.set_node(pos, {name = trunk_node})
  56. while i <= trunk_height do --Setting trunk
  57. local new_pos = {x=pos.x, y=pos.y+i, z=pos.z}
  58. minetest.set_node(new_pos, {name = trunk_node})
  59. i = i + 1
  60. end
  61. else
  62. minetest.chat_send_player(name, 'Set sapling parameters first.')
  63. end
  64. end
  65. end,
  66. on_use = function(itemstack, user)
  67. local name = user:get_player_name()
  68. minetest.show_formspec(name, 'plants:sapling', plants_sapling_formspec(name))
  69. end
  70. })
  71. minetest.register_on_player_receive_fields(function(player, formname, fields)
  72. local name = player:get_player_name()
  73. if formname == 'plants:sapling' then
  74. if fields.save then
  75. local passing = 0
  76. local data = {}
  77. local trunk_node = fields.trunk_n
  78. local leaf_node = fields.leaf_n
  79. if minetest.registered_nodes[trunk_node] then
  80. data.trunk_node = trunk_node
  81. passing = passing + 1
  82. end
  83. if minetest.registered_nodes[leaf_node] then
  84. data.leaf_node = leaf_node
  85. passing = passing + 1
  86. end
  87. if passing == 2 then
  88. data.height = fields.height_overall
  89. data.trunk_height = fields.height_trunk
  90. plants.saplings[name] = data
  91. minetest.chat_send_player(name, 'Right-click to place your tree. Punch again to change parameters.')
  92. else
  93. minetest.chat_send_player(name, 'Looks like some of your data is bad, try again.')
  94. end
  95. end
  96. end
  97. end)
  98. --[[
  99. Saving data:
  100. local data = {}
  101. data.level_pos = {x = pos_x, y = pos_y, z = pos_z}
  102. data.xp = tonumber(fields.xp)
  103. lobby.savedata.IDs[map_id] = true
  104. lobby.savedata.data[map_id] = data
  105. ----
  106. Retriving data:
  107. local game_data = lobby.savedata.data[map_id]
  108. local game_pos = game_data['level_pos']
  109. ]]