Randomize trap positions
[minetest_pyramids/tsm_pyramids.git] / init.lua
blob20d1eeef8a61f41496fe8e7e993a6dfb739c4118
1 tsm_pyramids = {}
3 dofile(minetest.get_modpath("tsm_pyramids").."/mummy.lua")
4 dofile(minetest.get_modpath("tsm_pyramids").."/nodes.lua")
5 dofile(minetest.get_modpath("tsm_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 tsm_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 local stacks = {}
27 if minetest.get_modpath("treasurer") ~= nil then
28 stacks = treasurer.select_random_treasures(3,7,9,{"minetool", "food", "crafting_component"})
29 else
30 for i=0,2,1 do
31 local stuff = chest_stuff[math.random(1,#chest_stuff)]
32 if stuff.name == "farming:bread" and not minetest.get_modpath("farming") then stuff = chest_stuff[1] end
33 table.insert(stacks, {name=stuff.name, count = math.random(1,stuff.max)})
34 end
35 end
36 for s=1,#stacks do
37 if not inv:contains_item("main", stacks[s]) then
38 inv:set_stack("main", math.random(1,32), stacks[s])
39 end
40 end
42 end
43 end)
44 end
46 local function add_spawner(pos)
47 minetest.set_node(pos, {name="tsm_pyramids:spawner_mummy"})
48 if not minetest.settings:get_bool("only_peaceful_mobs") then tsm_pyramids.spawn_mummy({x=pos.x,y=pos.y,z=pos.z-2},2) end
49 end
51 local function can_replace(pos)
52 local n = minetest.get_node_or_nil(pos)
53 if n and n.name and minetest.registered_nodes[n.name] and not minetest.registered_nodes[n.name].walkable then
54 return true
55 elseif not n then
56 return true
57 else
58 return false
59 end
60 end
62 local function underground(pos, stone, sand)
63 local p2 = pos
64 local cnt = 0
65 local mat = stone
66 p2.y = p2.y-1
67 while can_replace(p2)==true do
68 cnt = cnt+1
69 if cnt > 25 then break end
70 if cnt>math.random(2,4) then mat = stone end
71 minetest.set_node(p2, {name=mat})
72 p2.y = p2.y-1
73 end
74 end
76 local function make_entrance(pos, brick)
77 local gang = {x=pos.x+10,y=pos.y, z=pos.z}
78 for iy=2,3,1 do
79 for iz=0,6,1 do
80 minetest.remove_node({x=gang.x+1,y=gang.y+iy,z=gang.z+iz})
81 if iz >=3 and iy == 3 then
82 minetest.set_node({x=gang.x,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
83 minetest.set_node({x=gang.x+1,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
84 minetest.set_node({x=gang.x+2,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
85 end
86 end
87 end
88 end
90 local function make(pos, brick, sandstone, stone, sand, ptype)
91 minetest.log("action", "Created pyramid at ("..pos.x..","..pos.y..","..pos.z..")")
92 for iy=0,10,1 do
93 for ix=iy,22-iy,1 do
94 for iz=iy,22-iy,1 do
95 if iy <1 then underground({x=pos.x+ix,y=pos.y,z=pos.z+iz}, stone, sand) end
96 minetest.set_node({x=pos.x+ix,y=pos.y+iy,z=pos.z+iz}, {name=brick})
97 for yy=1,10-iy,1 do
98 local n = minetest.get_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz})
99 if n and n.name and n.name == stone then
100 minetest.set_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz},{name=sand})
107 tsm_pyramids.make_room(pos, ptype)
108 minetest.after(2, tsm_pyramids.make_traps, pos, ptype)
109 add_spawner({x=pos.x+11,y=pos.y+2, z=pos.z+17})
110 make_entrance({x=pos.x,y=pos.y, z=pos.z}, brick)
113 local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
114 local perlin1
116 local function hlp_fnct(pos, name)
117 local n = minetest.get_node_or_nil(pos)
118 if n and n.name and n.name == name then
119 return true
120 else
121 return false
124 local function ground(pos, old)
125 local p2 = pos
126 while hlp_fnct(p2, "air") do
127 p2.y = p2.y -1
129 if p2.y < old.y then
130 return p2
131 else
132 return old
137 minetest.register_on_generated(function(minp, maxp, seed)
138 if maxp.y < 0 then return end
139 math.randomseed(seed)
140 local cnt = 0
141 if not perlin1 then
142 perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
144 local noise1 = perlin1:get_2d({x=minp.x,y=minp.y})--,z=minp.z})
146 if noise1 > 0.25 or noise1 < -0.26 then
147 local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
149 local sands = {"default:desert_sand", "default:sand"}
150 local p2
151 local sand
152 for s=1, #sands do
153 sand = sands[s]
154 p2 = minetest.find_node_near(mpos, 25, sand)
155 while p2 == nil and cnt < 5 do
156 cnt = cnt+1
157 mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
158 p2 = minetest.find_node_near(mpos, 25, sand)
160 if p2 ~= nil then
161 break
164 if p2 == nil then return end
165 if p2.y < 0 then return end
167 local off = 0
168 local opos1 = {x=p2.x+22,y=p2.y-1,z=p2.z+22}
169 local opos2 = {x=p2.x+22,y=p2.y-1,z=p2.z}
170 local opos3 = {x=p2.x,y=p2.y-1,z=p2.z+22}
171 local opos1_n = minetest.get_node_or_nil(opos1)
172 local opos2_n = minetest.get_node_or_nil(opos2)
173 local opos3_n = minetest.get_node_or_nil(opos3)
174 if opos1_n and opos1_n.name and opos1_n.name == "air" then
175 p2 = ground(opos1, p2)
177 if opos2_n and opos2_n.name and opos2_n.name == "air" then
178 p2 = ground(opos2, p2)
180 if opos3_n and opos3_n.name and opos3_n.name == "air" then
181 p2 = ground(opos3, p2)
183 p2.y = p2.y - 3
184 if p2.y < 0 then p2.y = 0 end
185 if minetest.find_node_near(p2, 25, {"default:water_source"}) ~= nil or
186 minetest.find_node_near(p2, 22, {"default:dirt_with_grass"}) ~= nil or
187 minetest.find_node_near(p2, 52, {"default:sandstonebrick"}) ~= nil or
188 minetest.find_node_near(p2, 52, {"default:desert_sandstone_brick"}) ~= nil then
189 return
192 if math.random(0,10) > 7 then
193 return
195 if sand == "default:desert_sand" then
196 minetest.after(0.8, make, p2, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert")
197 else
198 minetest.after(0.8, make, p2, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone")
201 end)
203 -- Add backwards-compability for nodes from the original pyramids mod
204 if minetest.get_modpath("pyramids") == nil then
205 -- Nodes
206 minetest.register_alias("pyramids:trap", "tsm_pyramids:trap")
207 minetest.register_alias("pyramids:trap_2", "tsm_pyramids:trap_2")
208 minetest.register_alias("pyramids:deco_stone1", "tsm_pyramids:deco_stone1")
209 minetest.register_alias("pyramids:deco_stone2", "tsm_pyramids:deco_stone2")
210 minetest.register_alias("pyramids:deco_stone3", "tsm_pyramids:deco_stone3")
211 minetest.register_alias("pyramids:spawner_mummy", "tsm_pyramids:spawner_mummy")
213 -- FIXME: Entities are currently NOT backwards-compatible
214 -- TODO: Update README when full backwards-compability is achieved