Fix global variable warnings; push to 0.4.2
[minetest_pyramids.git] / init.lua
blobdefce93d22a5fc640046b5cfebd34530855b8025
1 pyramids = {}
3 dofile(minetest.get_modpath("pyramids").."/mummy.lua")
4 dofile(minetest.get_modpath("pyramids").."/nodes.lua")
5 dofile(minetest.get_modpath("pyramids").."/room.lua")
7 local chest_stuff = {
8 {name="default:apple", max = 3},
9 {name="farming:bread", max = 3},
10 {name="default:steel_ingot", max = 2},
11 {name="default:gold_ingot", max = 2},
12 {name="default:diamond", max = 1},
13 {name="default:pick_steel", max = 1},
14 {name="default:pick_diamond", max = 1}
18 function pyramids.fill_chest(pos)
19 minetest.after(2, function()
20 local n = minetest.get_node(pos)
21 if n and n.name and n.name == "default:chest" then
22 local meta = minetest.get_meta(pos)
23 local inv = meta:get_inventory()
24 inv:set_size("main", 8*4)
25 if math.random(1,10) < 7 then return end
26 for i=0,2,1 do
27 local stuff = chest_stuff[math.random(1,#chest_stuff)]
28 if stuff.name == "farming:bread" and not minetest.get_modpath("farming") then stuff = chest_stuff[1] end
29 local stack = {name=stuff.name, count = math.random(1,stuff.max)}
30 if not inv:contains_item("main", stack) then
31 inv:set_stack("main", math.random(1,32), stack)
32 end
33 end
34 end
35 end)
36 end
38 local function add_spawner(pos)
39 minetest.set_node(pos, {name="pyramids:spawner_mummy"})
40 if not minetest.setting_getbool("only_peaceful_mobs") then pyramids.spawn_mummy({x=pos.x,y=pos.y,z=pos.z-2},2) end
41 end
43 local function can_replace(pos)
44 local n = minetest.get_node_or_nil(pos)
45 if n and n.name and minetest.registered_nodes[n.name] and not minetest.registered_nodes[n.name].walkable then
46 return true
47 elseif not n then
48 return true
49 else
50 return false
51 end
52 end
54 local function underground(pos)
55 local p2 = pos
56 local cnt = 0
57 local mat = "desert_sand"
58 p2.y = p2.y-1
59 while can_replace(p2)==true do
60 cnt = cnt+1
61 if cnt > 25 then break end
62 if cnt>math.random(2,4) then mat = "desert_stone"end
63 minetest.set_node(p2, {name="default:"..mat})
64 p2.y = p2.y-1
65 end
66 end
68 local function make_entrance(pos)
69 local gang = {x=pos.x+10,y=pos.y, z=pos.z}
70 for iy=2,3,1 do
71 for iz=0,6,1 do
72 minetest.remove_node({x=gang.x+1,y=gang.y+iy,z=gang.z+iz})
73 if iz >=3 and iy == 3 then
74 minetest.set_node({x=gang.x,y=gang.y+iy+1,z=gang.z+iz}, {name="default:sandstonebrick"})
75 minetest.set_node({x=gang.x+1,y=gang.y+iy+1,z=gang.z+iz}, {name="default:sandstonebrick"})
76 minetest.set_node({x=gang.x+2,y=gang.y+iy+1,z=gang.z+iz}, {name="default:sandstonebrick"})
77 end
78 end
79 end
80 end
82 local function make(pos)
83 minetest.log("action", "Created pyramid at ("..pos.x..","..pos.y..","..pos.z..")")
84 for iy=0,10,1 do
85 for ix=iy,22-iy,1 do
86 for iz=iy,22-iy,1 do
87 if iy <1 then underground({x=pos.x+ix,y=pos.y,z=pos.z+iz}) end
88 minetest.set_node({x=pos.x+ix,y=pos.y+iy,z=pos.z+iz}, {name="default:sandstonebrick"})
89 for yy=1,10-iy,1 do
90 local n = minetest.get_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz})
91 if n and n.name and n.name == "default:desert_stone" then minetest.set_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz},{name="default:desert_sand"}) end
92 end
93 end
94 end
95 end
97 pyramids.make_room(pos)
98 minetest.after(2, pyramids.make_traps, pos)
99 add_spawner({x=pos.x+11,y=pos.y+2, z=pos.z+17})
100 make_entrance({x=pos.x,y=pos.y, z=pos.z})
103 local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
105 local function hlp_fnct(pos, name)
106 local n = minetest.get_node_or_nil(pos)
107 if n and n.name and n.name == name then
108 return true
109 else
110 return false
113 local function ground(pos, old)
114 local p2 = pos
115 while hlp_fnct(p2, "air") do
116 p2.y = p2.y -1
118 if p2.y < old.y then
119 return p2
120 else
121 return old
126 minetest.register_on_generated(function(minp, maxp, seed)
127 if maxp.y < 0 then return end
128 math.randomseed(seed)
129 local cnt = 0
131 local perlin1 = minetest.env:get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
132 local noise1 = perlin1:get2d({x=minp.x,y=minp.y})--,z=minp.z})
134 if noise1 > 0.25 or noise1 < -0.26 then
135 local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
137 local p2 = minetest.find_node_near(mpos, 25, {"default:desert_sand"})
138 while p2 == nil and cnt < 5 do
139 cnt = cnt+1
140 mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
141 p2 = minetest.find_node_near(mpos, 25, {"default:desert_sand"})
143 if p2 == nil then return end
144 if p2.y < 0 then return end
146 local off = 0
147 local opos1 = {x=p2.x+22,y=p2.y-1,z=p2.z+22}
148 local opos2 = {x=p2.x+22,y=p2.y-1,z=p2.z}
149 local opos3 = {x=p2.x,y=p2.y-1,z=p2.z+22}
150 local opos1_n = minetest.get_node_or_nil(opos1)
151 local opos2_n = minetest.get_node_or_nil(opos2)
152 local opos3_n = minetest.get_node_or_nil(opos3)
153 if opos1_n and opos1_n.name and opos1_n.name == "air" then
154 p2 = ground(opos1, p2)
156 if opos2_n and opos2_n.name and opos2_n.name == "air" then
157 p2 = ground(opos2, p2)
159 if opos3_n and opos3_n.name and opos3_n.name == "air" then
160 p2 = ground(opos3, p2)
162 p2.y = p2.y - 3
163 if p2.y < 0 then p2.y = 0 end
164 if minetest.find_node_near(p2, 25, {"default:water_source"}) ~= nil or minetest.find_node_near(p2, 22, {"default:dirt_with_grass"}) ~= nil or minetest.find_node_near(p2, 52, {"default:sandstonebrick"}) ~= nil then return end
166 if math.random(0,10) > 7 then return end
167 minetest.after(0.8,make,p2)
169 end)