Advertisement
Guest User

Untitled

a guest
Dec 5th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --Habitat is a mod which adds the function to spawn nodes near certain nodes and away from others on map generate
  2.  
  3. local function arrayContains(array, value)
  4.     for _,v in pairs(array) do
  5.       if v == value then
  6.         return true
  7.       end
  8.     end
  9.  
  10.     return false
  11. end
  12.  
  13. --HOW TO USE
  14. --minetest.register_on_generated(function(minp, maxp, seed)
  15.     --generate("plants:lavender_wild", {"default:dirt_with_grass"}, minp, maxp, -10, 60, 4, 4, {"default:sand",})
  16. --end)
  17.  
  18. function generate(node, surfaces, minp, maxp, height_min, height_max, spread, habitat_size, habitat_nodes, antitat_size, antitat_nodes)
  19.    
  20.     if height_min > maxp.y or height_max < minp.y then --stop if min and max height of plant spawning is not generated area
  21.         return
  22.     end
  23.    
  24.     local y_min = math.max(minp.y, height_min)
  25.     local y_max = math.min(maxp.y, height_max)
  26.    
  27.     local width   = maxp.x-minp.x
  28.     local length  = maxp.z-minp.z
  29.     local height  = height_max-height_min
  30.    
  31.     local y_current
  32.     local x_current
  33.     local z_current
  34.    
  35.     local x_deviation
  36.     local z_deviation
  37.    
  38.     local p
  39.     local n
  40.     local p_top
  41.     local n_top
  42.    
  43.     --loop and take steps depending on the spread of the plants
  44.     for x_current = spread/2, width, spread do
  45.         for z_current = spread/2, length, spread do
  46.  
  47.             x_deviation = math.floor(math.random(spread))-spread/2
  48.             z_deviation = math.floor(math.random(spread))-spread/2
  49.  
  50.             for y_current = height_max, height_min, -1 do --loop to look for the node that is not air
  51.                 p = {x=minp.x+x_current+x_deviation, y=y_current, z=minp.z+z_current+z_deviation}
  52.                 n = minetest.env:get_node(p)
  53.                 p_top = {x=p.x, y=p.y+1, z=p.z}
  54.                 n_top = minetest.env:get_node(p_top)
  55.                
  56.                 if n.name ~= "air" and n_top.name == "air" then
  57.                     if arrayContains(surfaces, n.name) then
  58.                         if minetest.env:find_node_near(p_top, habitat_size, habitat_nodes) ~= nil and minetest.env:find_node_near(p_top, antitat_size, antitat_nodes) == nil  then
  59.                             minetest.env:add_node(p_top, {name=node})
  60.                         end
  61.                     end
  62.                 end
  63.             end
  64.             z_current = z_current + spread
  65.         end
  66.     end
  67. end
  68.  
  69. print("[Habitat] Loaded!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement