Advertisement
Guest User

Leaf_Decay Mod 0.3

a guest
Feb 1st, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. -- leaf_decay (Minetest 0.4 mod)
  2. -- Decaying leaves after a tree is chopped down
  3. -- Copyright 2011 Mark Holmquist, licensed under the GPLv3
  4.  
  5. -- NOTE: It is recommended that you add a line to your mapgen.cpp
  6. -- if you want leaf decay to work with sapling-grown trees.
  7. -- Specifically, under the following line:
  8. -- MapNode applenode(LEGN(ndef, "CONTENT_APPLE"));
  9. -- in mapgen.cpp, the function make_tree, you should add:
  10. -- leavesnode.param2 = 1;
  11.  
  12. local version = "0.3"
  13.  
  14. tree_types = {}
  15. tree_distance = {}
  16.  
  17. register_tree_type = function(type, distance)
  18. tree_types[type] = true
  19. tree_distance[type] = distance
  20. end
  21.  
  22. register_tree_type("default:tree", 2)
  23. register_tree_type("default:jungletree", 3)
  24.  
  25. minetest.register_on_generated(function(minp, maxp)
  26. for x=minp.x,maxp.x do
  27. for y=minp.y,maxp.y do
  28. for z=minp.z,maxp.z do
  29. local test_p = {x=x, y=y, z=z}
  30. local test_node = minetest.env:get_node(test_p)
  31. if not test_node.param2 == 1 and test_node.name == "default:leaves" then
  32. test_node.param2 = 1
  33. minetest.env:add_node(test_p, test_node)
  34. end
  35. end
  36. end
  37. end
  38. end
  39. )
  40.  
  41. minetest.register_entity("leaf_decay:sapling",
  42. {
  43. physical = true,
  44. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  45. visual = "sprite",
  46. textures = {"default_sapling.png"},
  47. on_activate = function(self, staticdata) self.object:setacceleration({x=0, y=-4, z=0}) end,
  48. on_punch = function(self, hitter)
  49. self.object:remove()
  50. hitter:get_inventory():add_item("main", "default:sapling")
  51. end,
  52. })
  53.  
  54. minetest.register_entity("leaf_decay:apple",
  55. {
  56. physical = true,
  57. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  58. visual = "sprite",
  59. textures = {"default_apple.png"},
  60. on_activate = function(self, staticdata) self.object:setacceleration({x=0, y=-4, z=0}) end,
  61. on_punch = function(self, hitter)
  62. self.object:remove()
  63. hitter:get_inventory():add_item("main", "default:apple")
  64. end,
  65. })
  66.  
  67. minetest.register_abm({
  68. nodenames = {"default:leaves"},
  69. interval = 5.0,
  70. chance = 1,
  71. action = function(pos, node, active_object_count, active_object_count_wider)
  72. if not (node.param2 == 1) then
  73. return false
  74. end
  75. local DISTANCE = 3
  76. local found_tree = false
  77. for x=-DISTANCE,DISTANCE do
  78. for y=-DISTANCE,DISTANCE do
  79. for z=-DISTANCE,DISTANCE do
  80. local test_p = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
  81. local test_node = minetest.env:get_node(test_p)
  82. if tree_types[test_node.name] then
  83. local nx = x < 0 and -x or x
  84. local ny = y < 0 and -y or y
  85. local nz = z < 0 and -z or z
  86. local maxd = tree_distance[test_node.name]
  87. if nx <= maxd and ny <= maxd and nz <= maxd then
  88. found_tree = true
  89. break
  90. end
  91. end
  92. end
  93. if found_tree then break end
  94. end
  95. if found_tree then break end
  96. end
  97. if not found_tree then
  98. minetest.env:remove_node(pos)
  99. if math.random(1,100) <= 5 then
  100. local new_p = {x=(pos.x + (math.random(1,100)*.01)), y=(pos.y + (math.random(1,100)*.01)), z=(pos.z + (math.random(1,100)*.01))}
  101. minetest.env:add_entity(new_p, "leaf_decay:sapling")
  102. end
  103. end
  104. end,
  105. })
  106.  
  107. minetest.register_abm({
  108. nodenames = {"default:apple"},
  109. interval = 5.0,
  110. chance = 1,
  111. action = function(pos, node, active_object_count, active_object_count_wider)
  112. local DISTANCE = 1
  113. local found_leaf = false
  114. for x=-DISTANCE,DISTANCE do
  115. for y=-DISTANCE,DISTANCE do
  116. for z=-DISTANCE,DISTANCE do
  117. local test_p = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
  118. local test_node = minetest.env:get_node(test_p)
  119. if test_node.name == "default:leaves" then
  120. found_leaf = true
  121. break
  122. end
  123. end
  124. if found_leaf then break end
  125. end
  126. if found_leaf then break end
  127. end
  128. if not found_leaf then
  129. minetest.env:remove_node(pos)
  130. local new_p = {x=(pos.x + (math.random(1,100)*.003)), y=(pos.y + (math.random(1,100)*.003)), z=(pos.z + (math.random(1,100)*.003))}
  131. minetest.env:add_entity(new_p, "leaf_decay:apple")
  132. end
  133. end,
  134. })
  135.  
  136. print("Leaf Decay mod", version, " loaded")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement