Rename mobs mod to mcl_mobs
[MineClone/MineClone2.git] / mods / MAPGEN / mcl_structures / init.lua
blob25f51e092a6d58988efa6f8ac4a15233359b4842
1 local init = os.clock()
2 mcl_structures ={}
4 mcl_structures.get_struct = function(file)
5 local localfile = minetest.get_modpath("mcl_structures").."/schematics/"..file
6 local file, errorload = io.open(localfile, "rb")
7 if errorload ~= nil then
8 minetest.log("error", '[mcl_structures] Could not open this struct: ' .. localfile)
9 return nil
10 end
12 local allnode = file:read("*a")
13 file:close()
15 return allnode
16 end
18 -- The call of Struct
19 mcl_structures.call_struct = function(pos, struct_style, rotation)
20 if not rotation then
21 rotation = "random"
22 end
23 if struct_style == "village" then
24 return mcl_structures.generate_village(pos, rotation)
25 elseif struct_style == "desert_temple" then
26 return mcl_structures.generate_desert_temple(pos, rotation)
27 elseif struct_style == "desert_well" then
28 return mcl_structures.generate_desert_well(pos, rotation)
29 elseif struct_style == "igloo" then
30 return mcl_structures.generate_igloo_top(pos, rotation)
31 elseif struct_style == "witch_hut" then
32 return mcl_structures.generate_witch_hut(pos, rotation)
33 elseif struct_style == "ice_spike_small" then
34 return mcl_structures.generate_ice_spike_small(pos, rotation)
35 elseif struct_style == "ice_spike_large" then
36 return mcl_structures.generate_ice_spike_large(pos, rotation)
37 elseif struct_style == "boulder" then
38 return mcl_structures.generate_boulder(pos, rotation)
39 elseif struct_style == "fossil" then
40 return mcl_structures.generate_fossil(pos, rotation)
41 elseif struct_style == "end_exit_portal" then
42 return mcl_structures.generate_end_exit_portal(pos, rotation)
43 elseif struct_style == "end_portal_shrine" then
44 return mcl_structures.generate_end_portal_shrine(pos, rotation)
45 end
46 end
48 mcl_structures.generate_village = function(pos)
49 -- No generating for the moment, only place it :D
50 -- TODO: Do complete overhaul of the algorithm
51 local newpos = {x=pos.x,y=pos.y-1,z=pos.z}
52 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_village.mts"
53 return minetest.place_schematic(newpos, path, "random", nil, true)
54 end
56 mcl_structures.generate_desert_well = function(pos)
57 local newpos = {x=pos.x,y=pos.y-2,z=pos.z}
58 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_desert_well.mts"
59 return minetest.place_schematic(newpos, path, "0", nil, true)
60 end
62 mcl_structures.generate_igloo_top = function(pos)
63 -- FIXME: This spawns bookshelf instead of furnace. Fix this!
64 -- Furnace does ot work atm because apparently meta is not set. :-(
65 local newpos = {x=pos.x,y=pos.y-1,z=pos.z}
66 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_igloo_top.mts"
67 return minetest.place_schematic(newpos, path, "random", nil, true)
68 end
70 mcl_structures.generate_igloo_basement = function(pos, orientation)
71 -- TODO: Add brewing stand
72 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_igloo_basement.mts"
73 return minetest.place_schematic(pos, path, orientation, nil, true)
74 end
76 mcl_structures.generate_boulder = function(pos)
77 -- Choose between 2 boulder sizes (2×2×2 or 3×3×3)
78 local r = math.random(1, 10)
79 local path
80 if r <= 3 then
81 path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_boulder_small.mts"
82 else
83 path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_boulder.mts"
84 end
86 local newpos = {x=pos.x,y=pos.y-1,z=pos.z}
87 return minetest.place_schematic(newpos, path)
88 end
90 mcl_structures.generate_witch_hut = function(pos, rotation)
91 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_witch_hut.mts"
92 return minetest.place_schematic(pos, path, rotation, nil, true)
93 end
95 mcl_structures.generate_ice_spike_small = function(pos)
96 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_ice_spike_small.mts"
97 return minetest.place_schematic(pos, path, "random", nil, false)
98 end
100 mcl_structures.generate_ice_spike_large = function(pos)
101 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_ice_spike_large.mts"
102 return minetest.place_schematic(pos, path, "random", nil, false)
105 mcl_structures.generate_fossil = function(pos)
106 -- Generates one out of 8 possible fossil pieces
107 local newpos = {x=pos.x,y=pos.y-1,z=pos.z}
108 local fossils = {
109 "mcl_structures_fossil_skull_1.mts", -- 4×5×5
110 "mcl_structures_fossil_skull_2.mts", -- 5×5×5
111 "mcl_structures_fossil_skull_3.mts", -- 5×5×7
112 "mcl_structures_fossil_skull_4.mts", -- 7×5×5
113 "mcl_structures_fossil_spine_1.mts", -- 3×3×13
114 "mcl_structures_fossil_spine_2.mts", -- 5×4×13
115 "mcl_structures_fossil_spine_3.mts", -- 7×4×13
116 "mcl_structures_fossil_spine_4.mts", -- 8×5×13
118 local r = math.random(1, #fossils)
119 local path = minetest.get_modpath("mcl_structures").."/schematics/"..fossils[r]
120 return minetest.place_schematic(newpos, path, "random", nil, true)
123 mcl_structures.generate_end_exit_portal = function(pos)
124 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_end_exit_portal.mts"
125 return minetest.place_schematic(pos, path, "0", nil, true)
128 mcl_structures.generate_end_portal_shrine = function(pos)
129 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_end_portal_room_simple.mts"
130 local offset = {x=6, y=8, z=6}
131 local size = {x=13, y=8, z=13}
132 local newpos = { x = pos.x - offset.x, y = pos.y, z = pos.z - offset.z }
133 local ret = minetest.place_schematic(newpos, path, "0", nil, true)
134 if ret == nil then
135 return ret
138 local area_start, area_end = newpos, vector.add(newpos, size)
139 -- Find and setup spawner with silverfish
140 local spawners = minetest.find_nodes_in_area(area_start, area_end, "mcl_mobspawners:spawner")
141 for s=1, #spawners do
142 local meta = minetest.get_meta(spawners[s])
143 mcl_mobspawners.setup_spawner(spawners[s], "mobs_mc:silverfish")
146 -- Shuffle stone brick types
147 local bricks = minetest.find_nodes_in_area(area_start, area_end, "mcl_core:stonebrick")
148 -- FIXME: Use better seeding
149 local pr = PseudoRandom(math.random(0, 4294967295))
150 for b=1, #bricks do
151 local r_bricktype = pr:next(1, 100)
152 local r_infested = pr:next(1, 100)
153 local bricktype
154 if r_infested <= 5 then
155 if r_bricktype <= 30 then -- 30%
156 bricktype = "mcl_monster_eggs:monster_egg_stonebrickmossy"
157 elseif r_bricktype <= 50 then -- 20%
158 bricktype = "mcl_monster_eggs:monster_egg_stonebrickcracked"
159 else -- 50%
160 bricktype = "mcl_monster_eggs:monster_egg_stonebrick"
162 else
163 if r_bricktype <= 30 then -- 30%
164 bricktype = "mcl_core:stonebrickmossy"
165 elseif r_bricktype <= 50 then -- 20%
166 bricktype = "mcl_core:stonebrickcracked"
168 -- 50% stonebrick (no change necessary)
170 if bricktype ~= nil then
171 minetest.set_node(bricks[b], { name = bricktype })
175 -- Also replace stairs
176 local stairs = minetest.find_nodes_in_area(area_start, area_end, {"mcl_stairs:stair_stonebrick", "mcl_stairs:stair_stonebrick_outer", "mcl_stairs:stair_stonebrick_inner"})
177 for s=1, #stairs do
178 local stair = minetest.get_node(stairs[s])
179 local r_type = pr:next(1, 100)
180 if r_type <= 30 then -- 30% mossy
181 if stair.name == "mcl_stairs:stair_stonebrick" then
182 stair.name = "mcl_stairs:stair_stonebrickmossy"
183 elseif stair.name == "mcl_stairs:stair_stonebrick_outer" then
184 stair.name = "mcl_stairs:stair_stonebrickmossy_outer"
185 elseif stair.name == "mcl_stairs:stair_stonebrick_inner" then
186 stair.name = "mcl_stairs:stair_stonebrickmossy_inner"
188 minetest.set_node(stairs[s], stair)
189 elseif r_type <= 50 then -- 20% cracky
190 if stair.name == "mcl_stairs:stair_stonebrick" then
191 stair.name = "mcl_stairs:stair_stonebrickcracked"
192 elseif stair.name == "mcl_stairs:stair_stonebrick_outer" then
193 stair.name = "mcl_stairs:stair_stonebrickcracked_outer"
194 elseif stair.name == "mcl_stairs:stair_stonebrick_inner" then
195 stair.name = "mcl_stairs:stair_stonebrickcracked_inner"
197 minetest.set_node(stairs[s], stair)
199 -- 50% no change
202 -- Randomly add ender eyes into end portal frames, but never fill the entire frame
203 local frames = minetest.find_nodes_in_area(area_start, area_end, "mcl_portals:end_portal_frame")
204 local eyes = 0
205 for f=1, #frames do
206 local r_eye = pr:next(1, 10)
207 if r_eye == 1 then
208 eyes = eyes + 1
209 if eyes < #frames then
210 local frame_node = minetest.get_node(frames[f])
211 frame_node.name = "mcl_portals:end_portal_frame_eye"
212 minetest.set_node(frames[f], frame_node)
217 return ret
220 mcl_structures.generate_desert_temple = function(pos)
221 -- No Generating for the temple ... Why using it ? No Change
222 local path = minetest.get_modpath("mcl_structures").."/schematics/mcl_structures_desert_temple.mts"
223 local newpos = {x=pos.x,y=pos.y-12,z=pos.z}
224 local size = {x=22, y=24, z=22}
225 if newpos == nil then
226 return
228 local ret = minetest.place_schematic(newpos, path, "random", nil, true)
229 if ret == nil then
230 return ret
233 -- Find chests.
234 -- FIXME: Searching this large area just for the chets is not efficient. Need a better way to find the chests;
235 -- probably let's just infer it from newpos because the schematic always the same.
236 local chests = minetest.find_nodes_in_area({x=newpos.x-size.x, y=newpos.y, z=newpos.z-size.z}, vector.add(newpos, size), "mcl_chests:chest")
238 -- Add desert temple loot into chests
239 -- FIXME: Use better seeding
240 local pr = PseudoRandom(math.random(0, 4294967295))
241 for c=1, #chests do
242 -- FIXME: Use better seeding
243 local lootitems = mcl_loot.get_multi_loot({
245 stacks_min = 2,
246 stacks_max = 4,
247 items = {
248 { itemstring = "mcl_mobitems:bone", weight = 25, amount_min = 4, amount_max=6 },
249 { itemstring = "mcl_mobitems:rotten_flesh", weight = 25, amount_min = 3, amount_max=7 },
250 { itemstring = "mcl_mobitems:spider_eye", weight = 25, amount_min = 1, amount_max=3 },
251 -- TODO: Enchanted Book
252 { itemstring = "mcl_books:book", weight = 20, },
253 { itemstring = "mcl_mobitems:saddle", weight = 20, },
254 { itemstring = "mcl_core:apple_gold", weight = 20, },
255 { itemstring = "mcl_core:gold_ingot", weight = 15, amount_min = 2, amount_max = 7 },
256 { itemstring = "mcl_core:iron_ingot", weight = 15, amount_min = 1, amount_max = 5 },
257 { itemstring = "mcl_core:emerald", weight = 15, amount_min = 1, amount_max = 3 },
258 { itemstring = "", weight = 15, },
259 { itemstring = "mobs_mc:iron_horse_armor", weight = 15, },
260 { itemstring = "mobs_mc:gold_horse_armor", weight = 10, },
261 { itemstring = "mobs_mc:diamond_horse_armor", weight = 5, },
262 { itemstring = "mcl_core:diamond", weight = 5, amount_min = 1, amount_max = 3 },
263 -- TODO: Enchanted Golden Apple
264 { itemstring = "mcl_core:apple_gold", weight = 2, },
268 stacks_min = 4,
269 stacks_max = 4,
270 items = {
271 { itemstring = "mcl_mobitems:bone", weight = 10, amount_min = 1, amount_max = 8 },
272 { itemstring = "mcl_mobitems:rotten_flesh", weight = 10, amount_min = 1, amount_max = 8 },
273 { itemstring = "mcl_mobitems:gunpowder", weight = 10, amount_min = 1, amount_max = 8 },
274 { itemstring = "mcl_core:sand", weight = 10, amount_min = 1, amount_max = 8 },
275 { itemstring = "mcl_mobitems:string", weight = 10, amount_min = 1, amount_max = 8 },
277 }}, pr)
279 local meta = minetest.get_meta(chests[c])
280 local inv = meta:get_inventory()
281 inv:set_size("main", 9*3)
282 for i=1, #lootitems do
283 inv:add_item("main", lootitems[i])
287 -- Initialize pressure plates and randomly remove up to 5 plates
288 local pplates = minetest.find_nodes_in_area({x=newpos.x-size.x, y=newpos.y, z=newpos.z-size.z}, vector.add(newpos, size), "mesecons_pressureplates:pressure_plate_stone_off")
289 local pplates_remove = 5
290 for p=1, #pplates do
291 if pplates_remove > 0 and pr:next(1, 100) >= 50 then
292 -- Remove plate
293 minetest.remove_node(pplates[p])
294 pplates_remove = pplates_remove - 1
295 else
296 -- Initialize plate
297 minetest.registered_nodes["mesecons_pressureplates:pressure_plate_stone_off"].on_construct(pplates[p])
301 return ret
304 local registered_structures = {}
306 --[[ Returns a table of structure of the specified type.
307 Currently the only valid parameter is "stronghold".
308 Format of return value:
310 { pos = <position>, generated=<true/false> }, -- first structure
311 { pos = <position>, generated=<true/false> }, -- second structure
312 -- and so on
315 TODO: Implement this function for all other structure types as well.
317 mcl_structures.get_registered_structures = function(structure_type)
318 if registered_structures[structure_type] then
319 return table.copy(registered_structures[structure_type])
320 else
321 return {}
325 -- Register a structures table for the given type. The table format is the same as for
326 -- mcl_structures.get_registered_structures.
327 mcl_structures.register_structures = function(structure_type, structures)
328 registered_structures[structure_type] = structures
331 -- Debug command
332 minetest.register_chatcommand("spawnstruct", {
333 params = "desert_temple | desert_well | igloo | village | witch_hut | boulder | ice_spike_small | ice_spike_large | fossil | end_exit_portal | end_portal_shrine",
334 description = "Generate a pre-defined structure near your position.",
335 privs = {debug = true},
336 func = function(name, param)
337 local pos= minetest.get_player_by_name(name):getpos()
338 if not pos then
339 return
341 local errord = false
342 if param == "village" then
343 mcl_structures.generate_village(pos)
344 minetest.chat_send_player(name, "Village built. WARNING: Villages are experimental and might have bugs.")
345 elseif param == "desert_temple" then
346 mcl_structures.generate_desert_temple(pos)
347 minetest.chat_send_player(name, "Desert temple built.")
348 elseif param == "desert_well" then
349 mcl_structures.generate_desert_well(pos)
350 minetest.chat_send_player(name, "Desert well built.")
351 elseif param == "igloo" then
352 mcl_structures.generate_igloo_top(pos)
353 minetest.chat_send_player(name, "Igloo built.")
354 elseif param == "witch_hut" then
355 mcl_structures.generate_witch_hut(pos)
356 minetest.chat_send_player(name, "Witch hut built.")
357 elseif param == "boulder" then
358 mcl_structures.generate_boulder(pos)
359 minetest.chat_send_player(name, "Moss stone boulder placed.")
360 elseif param == "fossil" then
361 mcl_structures.generate_fossil(pos)
362 minetest.chat_send_player(name, "Fossil placed.")
363 elseif param == "ice_spike_small" then
364 mcl_structures.generate_ice_spike_small(pos)
365 minetest.chat_send_player(name, "Small ice spike placed.")
366 elseif param == "ice_spike_large" then
367 mcl_structures.generate_ice_spike_large(pos)
368 minetest.chat_send_player(name, "Large ice spike placed.")
369 elseif param == "end_exit_portal" then
370 mcl_structures.generate_end_exit_portal(pos)
371 minetest.chat_send_player(name, "End exit portal placed.")
372 elseif param == "end_portal_shrine" then
373 mcl_structures.generate_end_portal_shrine(pos)
374 minetest.chat_send_player(name, "End portal shrine placed.")
375 elseif param == "" then
376 minetest.chat_send_player(name, "Error: No structure type given. Please use “/spawnstruct <type>”.")
377 errord = true
378 else
379 minetest.chat_send_player(name, "Error: Unknown structure type. Please use “/spawnstruct <type>”.")
380 errord = true
382 if errord then
383 minetest.chat_send_player(name, "Use /help spawnstruct to see a list of avaiable types.")
388 local time_to_load= os.clock() - init
389 minetest.log("action", (string.format("[MOD] "..minetest.get_current_modname().." loaded in %.4f s", time_to_load)))