Fix various flood_sand bugs
[minetest_pyramids/tsm_pyramids.git] / init.lua
blobe7ed4b5ed617278b64a61c0de6a92239c7ee4eb2
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 add_spawner({x=pos.x+11,y=pos.y+2, z=pos.z+17})
137 make_entrance({x=pos.x,y=pos.y, z=pos.z}, brick, sand, flood_sand)
138 minetest.log("action", "Created pyramid at ("..pos.x..","..pos.y..","..pos.z..")")
139 return ok, msg
142 local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
143 local perlin1
145 local function hlp_fnct(pos, name)
146 local n = minetest.get_node_or_nil(pos)
147 if n and n.name and n.name == name then
148 return true
149 else
150 return false
153 local function ground(pos, old)
154 local p2 = pos
155 while hlp_fnct(p2, "air") do
156 p2.y = p2.y -1
158 if p2.y < old.y then
159 return p2
160 else
161 return old
166 minetest.register_on_generated(function(minp, maxp, seed)
167 if maxp.y < 0 then return end
168 math.randomseed(seed)
169 local cnt = 0
170 if not perlin1 then
171 perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
173 local noise1 = perlin1:get_2d({x=minp.x,y=minp.y})--,z=minp.z})
175 if noise1 > 0.25 or noise1 < -0.26 then
176 local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
178 local sands = {"default:desert_sand", "default:sand"}
179 local p2
180 local sand
181 for s=1, #sands do
182 sand = sands[s]
183 p2 = minetest.find_node_near(mpos, 25, sand)
184 while p2 == nil and cnt < 5 do
185 cnt = cnt+1
186 mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
187 p2 = minetest.find_node_near(mpos, 25, sand)
189 if p2 ~= nil then
190 break
193 if p2 == nil then return end
194 if p2.y < 0 then return end
196 local off = 0
197 local opos1 = {x=p2.x+22,y=p2.y-1,z=p2.z+22}
198 local opos2 = {x=p2.x+22,y=p2.y-1,z=p2.z}
199 local opos3 = {x=p2.x,y=p2.y-1,z=p2.z+22}
200 local opos1_n = minetest.get_node_or_nil(opos1)
201 local opos2_n = minetest.get_node_or_nil(opos2)
202 local opos3_n = minetest.get_node_or_nil(opos3)
203 if opos1_n and opos1_n.name and opos1_n.name == "air" then
204 p2 = ground(opos1, p2)
206 if opos2_n and opos2_n.name and opos2_n.name == "air" then
207 p2 = ground(opos2, p2)
209 if opos3_n and opos3_n.name and opos3_n.name == "air" then
210 p2 = ground(opos3, p2)
212 p2.y = p2.y - 3
213 if p2.y < 0 then p2.y = 0 end
214 if minetest.find_node_near(p2, 25, {"default:water_source"}) ~= nil or
215 minetest.find_node_near(p2, 22, {"default:dirt_with_grass"}) ~= nil or
216 minetest.find_node_near(p2, 52, {"default:sandstonebrick"}) ~= nil or
217 minetest.find_node_near(p2, 52, {"default:desert_sandstone_brick"}) ~= nil then
218 return
221 if math.random(0,10) > 7 then
222 return
224 if sand == "default:desert_sand" then
225 minetest.after(0.8, make, p2, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert")
226 else
227 minetest.after(0.8, make, p2, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone")
230 end)
232 -- Add backwards-compability for nodes from the original pyramids mod
233 if minetest.get_modpath("pyramids") == nil then
234 -- Nodes
235 minetest.register_alias("pyramids:trap", "tsm_pyramids:trap")
236 minetest.register_alias("pyramids:trap_2", "tsm_pyramids:trap_2")
237 minetest.register_alias("pyramids:deco_stone1", "tsm_pyramids:deco_stone1")
238 minetest.register_alias("pyramids:deco_stone2", "tsm_pyramids:deco_stone2")
239 minetest.register_alias("pyramids:deco_stone3", "tsm_pyramids:deco_stone3")
240 minetest.register_alias("pyramids:spawner_mummy", "tsm_pyramids:spawner_mummy")
242 -- FIXME: Entities are currently NOT backwards-compatible
243 -- TODO: Update README when full backwards-compability is achieved
246 minetest.register_chatcommand("spawnpyramid", {
247 description = S("Generate a pyramid"),
248 params = S("[<room_type>]"),
249 privs = { server = true },
250 func = function(name, param)
251 local player = minetest.get_player_by_name(name)
252 if not player then
253 return false, S("No player.")
255 local pos = player:get_pos()
256 pos = vector.round(pos)
257 local s = math.random(1,2)
258 local r = tonumber(param)
259 local room_id
260 if r then
261 room_id = r
263 local ok, msg
264 if s == 1 then
265 ok, msg = make(pos, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone", room_id)
266 else
267 ok, msg = make(pos, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert", room_id)
269 if ok then
270 return true, S("Pyramid generated at @1.", minetest.pos_to_string(pos))
271 else
272 return false, msg
274 end,