Randomize spawner position (a bit)
[minetest_pyramids/tsm_pyramids.git] / init.lua
blob2ab2749698fbe43b20828e35638c5e366ad2cfe0
1 local S = minetest.get_translator("tsm_pyramids")
3 tsm_pyramids = {}
5 dofile(minetest.get_modpath("tsm_pyramids").."/mummy.lua")
6 dofile(minetest.get_modpath("tsm_pyramids").."/nodes.lua")
7 dofile(minetest.get_modpath("tsm_pyramids").."/room.lua")
9 local chest_stuff = {
10 {name="default:apple", max = 3},
11 {name="default:steel_ingot", max = 3},
12 {name="default:copper_ingot", max = 3},
13 {name="default:gold_ingot", max = 2},
14 {name="default:diamond", max = 1},
15 {name="default:pick_steel", max = 1},
16 {name="default:pick_diamond", max = 1},
17 {name="default:papyrus", max = 9},
20 if minetest.get_modpath("farming") then
21 table.insert(chest_stuff, {name="farming:bread", max = 3})
22 table.insert(chest_stuff, {name="farming:cotton", max = 8})
23 else
24 table.insert(chest_stuff, {name="farming:apple", max = 8})
25 table.insert(chest_stuff, {name="farming:apple", max = 3})
26 end
27 if minetest.get_modpath("tnt") then
28 table.insert(chest_stuff, {name="tnt:gunpowder", max = 6})
29 else
30 table.insert(chest_stuff, {name="farming:apple", max = 3})
31 end
33 function tsm_pyramids.fill_chest(pos, stype, flood_sand)
34 minetest.after(2, function()
35 local sand = "default:sand"
36 if stype == "desert" then
37 sand = "default:desert_sand"
38 end
39 local n = minetest.get_node(pos)
40 if n and n.name and n.name == "default:chest" then
41 local meta = minetest.get_meta(pos)
42 local inv = meta:get_inventory()
43 inv:set_size("main", 8*4)
44 local stacks = {}
45 -- Fill with sand in sand-flooded pyramids
46 if flood_sand then
47 table.insert(stacks, {name=sand, count = math.random(1,32)})
48 end
49 -- Add treasures
50 if math.random(1,10) >= 7 then
51 if minetest.get_modpath("treasurer") ~= nil then
52 stacks = treasurer.select_random_treasures(3,7,9,{"minetool", "food", "crafting_component"})
53 else
54 for i=0,2,1 do
55 local stuff = chest_stuff[math.random(1,#chest_stuff)]
56 table.insert(stacks, {name=stuff.name, count = math.random(1,stuff.max)})
57 end
58 end
59 end
60 for s=1,#stacks do
61 if not inv:contains_item("main", stacks[s]) then
62 inv:set_stack("main", math.random(1,32), stacks[s])
63 end
64 end
66 end
67 end)
68 end
70 local function add_spawner(pos)
71 minetest.set_node(pos, {name="tsm_pyramids:spawner_mummy"})
72 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
73 end
75 local function can_replace(pos)
76 local n = minetest.get_node_or_nil(pos)
77 if n and n.name and minetest.registered_nodes[n.name] and not minetest.registered_nodes[n.name].walkable then
78 return true
79 elseif not n then
80 return true
81 else
82 return false
83 end
84 end
86 local function underground(pos, stone, sand)
87 local p2 = pos
88 local cnt = 0
89 local mat = stone
90 p2.y = p2.y-1
91 while can_replace(p2)==true do
92 cnt = cnt+1
93 if cnt > 25 then break end
94 if cnt>math.random(2,4) then mat = stone end
95 minetest.set_node(p2, {name=mat})
96 p2.y = p2.y-1
97 end
98 end
100 local function make_entrance(pos, brick, sand, flood_sand)
101 local gang = {x=pos.x+10,y=pos.y, z=pos.z}
102 local max_sand_height = math.random(1,3)
103 for iz=0,6,1 do
104 local sand_height = math.random(1,max_sand_height)
105 for iy=2,3,1 do
106 if flood_sand and iy <= sand_height and iz >= 3 then
107 minetest.set_node({x=gang.x+1,y=gang.y+iy,z=gang.z+iz}, {name=sand})
108 else
109 minetest.remove_node({x=gang.x+1,y=gang.y+iy,z=gang.z+iz})
111 if iz >=3 and iy == 3 then
112 minetest.set_node({x=gang.x,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
113 minetest.set_node({x=gang.x+1,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
114 minetest.set_node({x=gang.x+2,y=gang.y+iy+1,z=gang.z+iz}, {name=brick})
120 local function make(pos, brick, sandstone, stone, sand, ptype, room_id)
121 for iy=0,10,1 do
122 for ix=iy,22-iy,1 do
123 for iz=iy,22-iy,1 do
124 if iy <1 then underground({x=pos.x+ix,y=pos.y,z=pos.z+iz}, stone, sand) end
125 minetest.set_node({x=pos.x+ix,y=pos.y+iy,z=pos.z+iz}, {name=brick})
126 for yy=1,10-iy,1 do
127 local n = minetest.get_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz})
128 if n and n.name and n.name == stone then
129 minetest.set_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz},{name=sand})
135 local ok, msg, flood_sand = tsm_pyramids.make_room(pos, ptype, room_id)
136 local r = math.random(1,3)
137 if r == 1 then
138 add_spawner({x=pos.x+11,y=pos.y+2, z=pos.z+17})
139 elseif r == 2 then
140 add_spawner({x=pos.x+17,y=pos.y+2, z=pos.z+11})
141 else
142 add_spawner({x=pos.x+5,y=pos.y+2, z=pos.z+11})
144 make_entrance({x=pos.x,y=pos.y, z=pos.z}, brick, sand, flood_sand)
145 minetest.log("action", "Created pyramid at ("..pos.x..","..pos.y..","..pos.z..")")
146 return ok, msg
149 local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
150 local perlin1
152 local function hlp_fnct(pos, name)
153 local n = minetest.get_node_or_nil(pos)
154 if n and n.name and n.name == name then
155 return true
156 else
157 return false
160 local function ground(pos, old)
161 local p2 = pos
162 while hlp_fnct(p2, "air") do
163 p2.y = p2.y -1
165 if p2.y < old.y then
166 return p2
167 else
168 return old
173 minetest.register_on_generated(function(minp, maxp, seed)
174 if maxp.y < 0 then return end
175 math.randomseed(seed)
176 local cnt = 0
177 if not perlin1 then
178 perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
180 local noise1 = perlin1:get_2d({x=minp.x,y=minp.y})--,z=minp.z})
182 if noise1 > 0.25 or noise1 < -0.26 then
183 local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
185 local sands = {"default:desert_sand", "default:sand"}
186 local p2
187 local sand
188 for s=1, #sands do
189 sand = sands[s]
190 p2 = minetest.find_node_near(mpos, 25, sand)
191 while p2 == nil and cnt < 5 do
192 cnt = cnt+1
193 mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
194 p2 = minetest.find_node_near(mpos, 25, sand)
196 if p2 ~= nil then
197 break
200 if p2 == nil then return end
201 if p2.y < 0 then return end
203 local off = 0
204 local opos1 = {x=p2.x+22,y=p2.y-1,z=p2.z+22}
205 local opos2 = {x=p2.x+22,y=p2.y-1,z=p2.z}
206 local opos3 = {x=p2.x,y=p2.y-1,z=p2.z+22}
207 local opos1_n = minetest.get_node_or_nil(opos1)
208 local opos2_n = minetest.get_node_or_nil(opos2)
209 local opos3_n = minetest.get_node_or_nil(opos3)
210 if opos1_n and opos1_n.name and opos1_n.name == "air" then
211 p2 = ground(opos1, p2)
213 if opos2_n and opos2_n.name and opos2_n.name == "air" then
214 p2 = ground(opos2, p2)
216 if opos3_n and opos3_n.name and opos3_n.name == "air" then
217 p2 = ground(opos3, p2)
219 p2.y = p2.y - 3
220 if p2.y < 0 then p2.y = 0 end
221 if minetest.find_node_near(p2, 25, {"default:water_source"}) ~= nil or
222 minetest.find_node_near(p2, 22, {"default:dirt_with_grass"}) ~= nil or
223 minetest.find_node_near(p2, 52, {"default:sandstonebrick"}) ~= nil or
224 minetest.find_node_near(p2, 52, {"default:desert_sandstone_brick"}) ~= nil then
225 return
228 if math.random(0,10) > 7 then
229 return
231 if sand == "default:desert_sand" then
232 minetest.after(0.8, make, p2, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert")
233 else
234 minetest.after(0.8, make, p2, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone")
237 end)
239 -- Add backwards-compability for nodes from the original pyramids mod
240 if minetest.get_modpath("pyramids") == nil then
241 -- Nodes
242 minetest.register_alias("pyramids:trap", "tsm_pyramids:trap")
243 minetest.register_alias("pyramids:trap_2", "tsm_pyramids:trap_2")
244 minetest.register_alias("pyramids:deco_stone1", "tsm_pyramids:deco_stone1")
245 minetest.register_alias("pyramids:deco_stone2", "tsm_pyramids:deco_stone2")
246 minetest.register_alias("pyramids:deco_stone3", "tsm_pyramids:deco_stone3")
247 minetest.register_alias("pyramids:spawner_mummy", "tsm_pyramids:spawner_mummy")
249 -- FIXME: Entities are currently NOT backwards-compatible
250 -- TODO: Update README when full backwards-compability is achieved
253 minetest.register_chatcommand("spawnpyramid", {
254 description = S("Generate a pyramid"),
255 params = S("[<room_type>]"),
256 privs = { server = true },
257 func = function(name, param)
258 local player = minetest.get_player_by_name(name)
259 if not player then
260 return false, S("No player.")
262 local pos = player:get_pos()
263 pos = vector.round(pos)
264 local s = math.random(1,2)
265 local r = tonumber(param)
266 local room_id
267 if r then
268 room_id = r
270 local ok, msg
271 if s == 1 then
272 ok, msg = make(pos, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone", room_id)
273 else
274 ok, msg = make(pos, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert", room_id)
276 if ok then
277 return true, S("Pyramid generated at @1.", minetest.pos_to_string(pos))
278 else
279 return false, msg
281 end,