Fix deprecated call to Minetest settings
[tsm_mines/sys4-tsm_mines.git] / init.lua
blob46a3dd3f82e38dcc0bb09765444e096bb344ad0c
1 local MINE_DEEP_MIN = tonumber(minetest.settings:get("mines_deep_min")) or -64
2 local MINE_DEEP_MAX = tonumber(minetest.settings:get("mines_deep_max")) or -380
3 local MINE_FACTOR = tonumber(minetest.settings:get("mines_spawnfactor")) or 1.5
5 minetest.log("info", "Mod \"tsm_mines\" set MINE_DEEP_MIN to "..MINE_DEEP_MIN)
6 minetest.log("info", "Mod \"tsm_mines\" set MINE_DEEP_MAX to "..MINE_DEEP_MAX)
7 minetest.log("info", "Mod \"tsm_mines\" set MINE_FACTOR to "..MINE_FACTOR)
9 minetest.register_node("tsm_mines:dummy", {
10 description = "tsm_mines dummy node",
11 _doc_items_create_entry = false,
12 inventory_image = "unknown_node.png",
13 wield_image = "unknown_node.png",
14 drawtype = "airlike",
15 paramtype = "light",
16 sunlight_propagates = true,
17 walkable = false,
18 pointable = false,
19 diggable = false,
20 buildable_to = true,
21 air_equivalent = true,
22 drop = "",
23 groups = {not_in_creative_inventory=1},
27 local ids = {
28 air = minetest.get_content_id("air"),
29 fence = minetest.get_content_id("default:fence_wood"),
30 wood = minetest.get_content_id("default:wood"),
31 dummy = minetest.get_content_id("tsm_mines:dummy")
34 local chest_stuff
35 if(minetest.get_modpath("farming")~=nil) then
36 chest_stuff = {
37 {name="default:apple", max = 3},
38 {name="farming:bread", max = 3},
39 {name="default:steel_ingot", max = 2},
40 {name="default:gold_ingot", max = 2},
41 {name="default:diamond", max = 1},
42 {name="default:pick_steel", max = 1},
43 {name="default:pick_diamond", max = 1}
45 else
46 chest_stuff = {
47 {name="default:apple", max = 3},
48 {name="default:steel_ingot", max = 2},
49 {name="default:gold_ingot", max = 2},
50 {name="default:diamond", max = 1},
51 {name="default:pick_steel", max = 1},
52 {name="default:pick_diamond", max = 1}
54 end
56 local function rotate_torch(pos)
57 minetest.after(8, function()
58 local n = minetest.get_node(pos)
59 if n ~= nil then
60 if n.name == "tsm_mines:dummy" then
61 minetest.set_node(pos, {name="default:torch",param2=1})
62 end
63 end
64 end)
65 end
67 local function fill_chest(pos)
68 minetest.after(5, function()
69 local n = minetest.get_node(pos)
70 if n ~= nil then
71 if n.name == "tsm_mines:dummy" then
72 minetest.set_node(pos, {name="default:chest"})
73 local meta = minetest.get_meta(pos)
74 local inv = meta:get_inventory()
75 inv:set_size("main", 8*4)
76 local stacks = {}
77 if(minetest.get_modpath("treasurer") ~= nil) then
78 stacks = treasurer.select_random_treasures(3,4,6,{"minetool", "food", "crafting_component"})
79 else
80 for i=0,2,1 do
81 local stuff = chest_stuff[math.random(1,#chest_stuff)]
82 table.insert(stacks,{name=stuff.name, count = math.random(1,stuff.max)})
83 end
84 end
85 for s=1, #stacks do
86 if not inv:contains_item("main", stacks[s]) then
87 inv:set_stack("main", math.random(1,32), stacks[s])
88 end
89 end
90 end
91 end
92 end)
93 end
95 local function check_dir(dir,old_dir)
96 if old_dir == 1 and dir == 3 then
97 return true
98 elseif old_dir == 3 and dir == 1 then
99 return true
100 elseif old_dir == 2 and dir == 4 then
101 return true
102 elseif old_dir == 4 and dir == 2 then
103 return true
105 return false
107 local function make_mine(mpos,p2,p3, vm_data, vx_area,cnt)
108 local pos = {x=mpos.x,y=mpos.y,z=mpos.z}
109 for j=0,12,1 do
110 local switch = cnt+1
111 local n_switch = math.random(1,4)
112 while check_dir(n_switch,switch) == true do
113 n_switch = math.random(1,4)
115 switch = n_switch
117 for i=0,20,1 do
118 local pillar = ids.air
119 local pillar_top = ids.air
120 if i==0 or i == 5 or i == 10 or i == 15 or i == 20 then
121 pillar = ids.fence
122 pillar_top = ids.wood
124 local x1, x2, x3, x4, x5
125 local z1, z2, z3, z4, z5
126 if switch == 1 then
127 x1 = pos.x+1
128 x2 = pos.x
129 x3 = pos.x-1
130 x4 = pos.x
131 x5 = pos.x+1
133 z1 = pos.z+i
134 z2 = pos.z+i
135 z3 = pos.z+i
136 z4 = pos.z+i-1
137 z5 = pos.z+i
138 elseif switch == 2 then
139 x1 = pos.x+i
140 x2 = pos.x+i
141 x3 = pos.x+i
142 x4 = pos.x+i-1
143 x5 = pos.x+i
145 z1 = pos.z+1
146 z2 = pos.z
147 z3 = pos.z-1
148 z4 = pos.z
149 z5 = pos.z+1
150 elseif switch == 3 then
151 x1 = pos.x+1
152 x2 = pos.x
153 x3 = pos.x-1
154 x4 = pos.x
155 x5 = pos.x+1
157 z1 = pos.z-i
158 z2 = pos.z-i
159 z3 = pos.z-i
160 z4 = pos.z-i-1
161 z5 = pos.z-i
162 else
163 x1 = pos.x-i
164 x2 = pos.x-i
165 x3 = pos.x-i
166 x4 = pos.x-i-1
167 x5 = pos.x-i
169 z1 = pos.z+1
170 z2 = pos.z
171 z3 = pos.z-1
172 z4 = pos.z
173 z5 = pos.z+1
175 vm_data[vx_area:indexp({x=x1, y=pos.y-1, z=z1})] = pillar
176 vm_data[vx_area:indexp({x=x2, y=pos.y-1, z=z2})] = ids.air
177 vm_data[vx_area:indexp({x=x3, y=pos.y-1, z=z3})] = pillar
179 vm_data[vx_area:indexp({x=x1, y=pos.y, z=z1})] = pillar
180 vm_data[vx_area:indexp({x=x2, y=pos.y, z=z2})] = ids.air
181 vm_data[vx_area:indexp({x=x3, y=pos.y, z=z3})] = pillar
183 vm_data[vx_area:indexp({x=x1, y=pos.y+1, z=z1})] = pillar_top
184 vm_data[vx_area:indexp({x=x2, y=pos.y+1, z=z2})] = pillar_top
185 vm_data[vx_area:indexp({x=x3, y=pos.y+1, z=z3})] = pillar_top
187 if math.random(0,6) == 3 then
188 vm_data[vx_area:indexp({x=x4, y=pos.y-1, z=z4})] = ids.dummy
189 rotate_torch({x=x4, y=pos.y-1, z=z4})
191 if math.random(0,60) == 13 then
192 local p = {x=x5, y=pos.y-1, z=z5}
193 if vm_data[vx_area:indexp(p)] ~= ids.fence then
194 vm_data[vx_area:indexp(p)] = ids.dummy
195 fill_chest(p)
199 if switch == 1 then
200 pos.z = pos.z+20
201 elseif switch == 2 then
202 pos.x = pos.x+20
203 elseif switch == 3 then
204 pos.z = pos.z-20
205 elseif switch == 4 then
206 pos.x = pos.x-20
209 if cnt == 0 then
210 minetest.log("action", "Created mine at ("..mpos.x..","..mpos.y..","..mpos.z..")")
211 local out2 = make_mine(p2,p3,mpos,vm_data,vx_area,1)
212 local out3 = make_mine(p3,p2,mpos,out2,vx_area,2)
213 return out3
214 else
215 return vm_data
219 local function find_cave(min,max,vm_data, vx_area)
220 local out = nil
221 for i in vx_area:iterp(min, max) do
222 if vm_data[i] == ids.air then
223 local p = vx_area:position(i)
224 if p.y <= MINE_DEEP_MIN then out = p end
225 end
227 return out
230 local cnt = 0
231 minetest.register_on_generated(function(minp, maxp, seed)
232 if minp.y > MINE_DEEP_MIN or minp.y < MINE_DEEP_MAX then
233 return
235 cnt = cnt+1
236 if cnt < 8/MINE_FACTOR then return end
237 cnt = 0
239 local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
240 local data = vm:get_data()
241 local va = VoxelArea:new{ MinEdge = emin, MaxEdge = emax }
242 local mpos = find_cave(emin,emax,data,va)
243 if mpos == nil then return end
244 local mpos2 = {x=mpos.x+math.random(0,3),y=mpos.y-1,z=mpos.z}
245 local mpos3 = {x=mpos.x,y=mpos.y-2,z=mpos.z+math.random(0,3)}
246 data = make_mine(mpos,mpos2,mpos3, data, va, 0)
247 vm:set_data(data)
248 vm:calc_lighting(emin,emax)
249 vm:update_liquids()
250 vm:write_to_map()
251 end)