Randomly rotate pyramids
[minetest_pyramids/tsm_pyramids.git] / init.lua
blob61fbcc6f285ac49ce409eead8d5208f7a74fc3c1
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 mg_name = minetest.get_mapgen_setting("mg_name")
11 local chest_stuff = {
12 {name="default:apple", max = 3},
13 {name="default:steel_ingot", max = 3},
14 {name="default:copper_ingot", max = 3},
15 {name="default:gold_ingot", max = 2},
16 {name="default:diamond", max = 1},
17 {name="default:pick_steel", max = 1},
18 {name="default:pick_diamond", max = 1},
19 {name="default:papyrus", max = 9},
22 if minetest.get_modpath("farming") then
23 table.insert(chest_stuff, {name="farming:bread", max = 3})
24 table.insert(chest_stuff, {name="farming:cotton", max = 8})
25 else
26 table.insert(chest_stuff, {name="farming:apple", max = 8})
27 table.insert(chest_stuff, {name="farming:apple", max = 3})
28 end
29 if minetest.get_modpath("tnt") then
30 table.insert(chest_stuff, {name="tnt:gunpowder", max = 6})
31 else
32 table.insert(chest_stuff, {name="farming:apple", max = 3})
33 end
35 function tsm_pyramids.fill_chest(pos, stype, flood_sand)
36 minetest.after(2, function()
37 local sand = "default:sand"
38 if stype == "desert" then
39 sand = "default:desert_sand"
40 end
41 local n = minetest.get_node(pos)
42 if n and n.name and n.name == "default:chest" then
43 local meta = minetest.get_meta(pos)
44 local inv = meta:get_inventory()
45 inv:set_size("main", 8*4)
46 local stacks = {}
47 -- Fill with sand in sand-flooded pyramids
48 if flood_sand then
49 table.insert(stacks, {name=sand, count = math.random(1,32)})
50 end
51 -- Add treasures
52 if math.random(1,10) >= 7 then
53 if minetest.get_modpath("treasurer") ~= nil then
54 stacks = treasurer.select_random_treasures(3,7,9,{"minetool", "food", "crafting_component"})
55 else
56 for i=0,2,1 do
57 local stuff = chest_stuff[math.random(1,#chest_stuff)]
58 table.insert(stacks, {name=stuff.name, count = math.random(1,stuff.max)})
59 end
60 end
61 end
62 for s=1,#stacks do
63 if not inv:contains_item("main", stacks[s]) then
64 inv:set_stack("main", math.random(1,32), stacks[s])
65 end
66 end
68 end
69 end)
70 end
72 local function add_spawner(pos, mummy_offset)
73 minetest.set_node(pos, {name="tsm_pyramids:spawner_mummy"})
74 if not minetest.settings:get_bool("only_peaceful_mobs") then tsm_pyramids.spawn_mummy(vector.add(pos, mummy_offset),2) end
75 end
77 local function can_replace(pos)
78 local n = minetest.get_node_or_nil(pos)
79 if n and n.name and minetest.registered_nodes[n.name] and not minetest.registered_nodes[n.name].walkable then
80 return true
81 elseif not n then
82 return true
83 else
84 return false
85 end
86 end
88 local function make_foundation_part(pos, set_to_stone)
89 local p2 = pos
90 local cnt = 0
91 p2.y = p2.y-1
92 while can_replace(p2)==true do
93 cnt = cnt+1
94 if cnt > 25 then
95 break
96 end
97 table.insert(set_to_stone, table.copy(p2))
98 p2.y = p2.y-1
99 end
102 local function make_entrance(pos, rot, brick, sand, flood_sand)
103 local roffset_arr = {
104 { x=0, y=0, z=1 }, -- front
105 { x=-1, y=0, z=0 }, -- left
106 { x=0, y=0, z=-1 }, -- back
107 { x=1, y=0, z=0 }, -- right
109 local roffset = roffset_arr[rot + 1]
110 local way
111 if rot == 0 then
112 way = vector.add(pos, {x=11, y=0, z=0})
113 elseif rot == 1 then
114 way = vector.add(pos, {x=22, y=0, z=11})
115 elseif rot == 2 then
116 way = vector.add(pos, {x=11, y=0, z=22})
117 else
118 way = vector.add(pos, {x=0, y=0, z=11})
120 local max_sand_height = math.random(1,3)
121 for ie=0,6,1 do
122 local sand_height = math.random(1,max_sand_height)
123 for iy=2,3,1 do
124 -- dig hallway
125 local way_dir = vector.add(vector.add(way, {x=0,y=iy,z=0}), vector.multiply(roffset, ie))
126 if flood_sand and iy <= sand_height and ie >= 3 then
127 minetest.set_node(way_dir, {name=sand})
128 else
129 minetest.remove_node(way_dir)
131 -- build decoration above entrance
132 if ie >=3 and iy == 3 then
133 local deco = {x=way_dir.x, y=way_dir.y+1,z=way_dir.z}
134 minetest.set_node(deco, {name=brick})
135 if rot == 0 or rot == 2 then
136 minetest.set_node(vector.add(deco, {x=-1, y=0, z=0}), {name=brick})
137 minetest.set_node(vector.add(deco, {x=1, y=0, z=0}), {name=brick})
138 else
139 minetest.set_node(vector.add(deco, {x=0, y=0, z=-1}), {name=brick})
140 minetest.set_node(vector.add(deco, {x=0, y=0, z=1}), {name=brick})
147 local function make_pyramid(pos, brick, sandstone, stone, sand)
148 local set_to_brick = {}
149 local set_to_sand = {}
150 local set_to_stone = {}
151 -- Build pyramid
152 for iy=0,math.random(10,11),1 do
153 for ix=iy,22-iy,1 do
154 for iz=iy,22-iy,1 do
155 if iy < 1 then
156 make_foundation_part({x=pos.x+ix,y=pos.y,z=pos.z+iz}, set_to_stone)
158 table.insert(set_to_brick, {x=pos.x+ix,y=pos.y+iy,z=pos.z+iz})
159 for yy=1,10-iy,1 do
160 local n = minetest.get_node({x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz})
161 if n and n.name and n.name == stone then
162 table.insert(set_to_sand, {x=pos.x+ix,y=pos.y+iy+yy,z=pos.z+iz})
168 minetest.bulk_set_node(set_to_stone , {name=stone})
169 minetest.bulk_set_node(set_to_brick, {name=brick})
170 minetest.bulk_set_node(set_to_sand, {name=sand})
173 local function make(pos, brick, sandstone, stone, sand, ptype, room_id)
174 -- Build pyramid
175 make_pyramid(pos, brick, sandstone, stone, sand)
177 local rot = math.random(0, 3)
178 -- Build room
179 local ok, msg, flood_sand = tsm_pyramids.make_room(pos, ptype, room_id, rot)
180 -- Place mummy spawner
181 local r = math.random(1,3)
182 -- 4 possible spawner positions
183 local spawner_posses = {
184 -- front
185 {{x=pos.x+11,y=pos.y+2, z=pos.z+5}, {x=0, y=0, z=2}},
186 -- left
187 {{x=pos.x+17,y=pos.y+2, z=pos.z+11}, {x=-2, y=0, z=0}},
188 -- back
189 {{x=pos.x+11,y=pos.y+2, z=pos.z+17}, {x=0, y=0, z=-2}},
190 -- right
191 {{x=pos.x+5,y=pos.y+2, z=pos.z+11}, {x=2, y=0, z=0}},
193 -- Delete the spawner position in which the entrance will be placed
194 table.remove(spawner_posses, (rot % 4) + 1)
195 add_spawner(spawner_posses[r][1], spawner_posses[r][2])
196 -- Build entrance
197 make_entrance(pos, rot, brick, sand, flood_sand)
198 -- Done
199 minetest.log("action", "Created pyramid at ("..pos.x..","..pos.y..","..pos.z..")")
200 return ok, msg
203 local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
204 local perlin1
206 local function hlp_fnct(pos, name)
207 local n = minetest.get_node_or_nil(pos)
208 if n and n.name and n.name == name then
209 return true
210 else
211 return false
214 local function ground(pos, old)
215 local p2 = pos
216 while hlp_fnct(p2, "air") do
217 p2.y = p2.y -1
219 if p2.y < old.y then
220 return p2
221 else
222 return old
227 minetest.register_on_generated(function(minp, maxp, seed)
228 if maxp.y < 0 then return end
229 math.randomseed(seed)
230 if not perlin1 then
231 perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
233 local noise1 = perlin1:get_2d({x=minp.x,y=minp.y})--,z=minp.z})
235 if noise1 > 0.25 or noise1 < -0.26 then
236 local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
238 local sands = {"default:sand", "default:desert_sand"}
239 local p2
240 local psand = {}
241 local sand
242 local cnt = 0
243 local cnt_min = 100
244 for s=1, #sands do
245 cnt = 0
246 sand = sands[s]
247 psand[s] = minetest.find_node_near(mpos, 25, sand)
248 while psand == nil and cnt < 5 do
249 cnt = cnt+1
250 mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
251 psand[s] = minetest.find_node_near(mpos, 25, sand)
253 if psand[s] ~= nil then
254 if cnt < cnt_min then
255 cnt_min = cnt
256 p2 = psand[s]
260 if p2 == nil then return end
261 if p2.y < 0 then return end
263 local off = 0
264 local opos1 = {x=p2.x+22,y=p2.y-1,z=p2.z+22}
265 local opos2 = {x=p2.x+22,y=p2.y-1,z=p2.z}
266 local opos3 = {x=p2.x,y=p2.y-1,z=p2.z+22}
267 local opos1_n = minetest.get_node_or_nil(opos1)
268 local opos2_n = minetest.get_node_or_nil(opos2)
269 local opos3_n = minetest.get_node_or_nil(opos3)
270 if opos1_n and opos1_n.name and opos1_n.name == "air" then
271 p2 = ground(opos1, p2)
273 if opos2_n and opos2_n.name and opos2_n.name == "air" then
274 p2 = ground(opos2, p2)
276 if opos3_n and opos3_n.name and opos3_n.name == "air" then
277 p2 = ground(opos3, p2)
279 p2.y = p2.y - 3
280 if p2.y < 0 then p2.y = 0 end
281 if minetest.find_node_near(p2, 25, {"default:water_source"}) ~= nil or
282 minetest.find_node_near(p2, 22, {"default:dirt_with_grass"}) ~= nil or
283 minetest.find_node_near(p2, 52, {"default:sandstonebrick"}) ~= nil or
284 minetest.find_node_near(p2, 52, {"default:desert_sandstone_brick"}) ~= nil then
285 return
288 if math.random(0,10) > 7 then
289 return
291 if (mg_name == "v6" and math.random(1, 2) == 1) then
292 sand = "default:sand"
294 if sand == "default:desert_sand" then
295 -- Desert sandstone pyramid
296 minetest.after(0.8, make, p2, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert")
297 else
298 -- Sandstone pyramid
299 minetest.after(0.8, make, p2, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone")
302 end)
304 -- Add backwards-compability for nodes from the original pyramids mod
305 if minetest.get_modpath("pyramids") == nil then
306 -- Nodes
307 minetest.register_alias("pyramids:trap", "tsm_pyramids:trap")
308 minetest.register_alias("pyramids:trap_2", "tsm_pyramids:trap_2")
309 minetest.register_alias("pyramids:deco_stone1", "tsm_pyramids:deco_stone1")
310 minetest.register_alias("pyramids:deco_stone2", "tsm_pyramids:deco_stone2")
311 minetest.register_alias("pyramids:deco_stone3", "tsm_pyramids:deco_stone3")
312 minetest.register_alias("pyramids:spawner_mummy", "tsm_pyramids:spawner_mummy")
314 -- FIXME: Entities are currently NOT backwards-compatible
315 -- TODO: Update README when full backwards-compability is achieved
318 minetest.register_chatcommand("spawnpyramid", {
319 description = S("Generate a pyramid"),
320 params = S("[<room_type>]"),
321 privs = { server = true },
322 func = function(name, param)
323 local player = minetest.get_player_by_name(name)
324 if not player then
325 return false, S("No player.")
327 local pos = player:get_pos()
328 pos = vector.round(pos)
329 local s = math.random(1,2)
330 local r = tonumber(param)
331 local room_id
332 if r then
333 room_id = r
335 local ok, msg
336 pos = vector.add(pos, {x=-11, y=-1, z=0})
337 if s == 1 then
338 ok, msg = make(pos, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone", room_id)
339 else
340 ok, msg = make(pos, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert", room_id)
342 if ok then
343 return true, S("Pyramid generated at @1.", minetest.pos_to_string(pos))
344 else
345 return false, msg
347 end,