]> git.lizzy.rs Git - elidragon.git/blob - skyblock.lua
Merge pull request #2 from EliasFleckenstein03/rank_fix
[elidragon.git] / skyblock.lua
1 elidragon.skyblock = {} 
2
3 --helper functions
4
5 -- http://rosettacode.org/wiki/Spiral_matrix#Lua
6 av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
7 local function sindex(y, x)
8         if y == -x and y >= x then return (2*y+1)^2 end
9         local l = math.max(av(y), av(x))
10         return (2*l-1)^2+4*l+2*l*sn(x+y)+sn(y^2-x^2)*(l-(av(y)==l and sn(y)*x or sn(x)*y))
11 end
12 local function spiralt(side)
13         local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
14         for i = 1, side do
15                 for j = 1, side do
16                         local id = side^2 - sindex(stop - i + 1,start + j - 1)
17                         ret[id] = {x=i,z=j}
18                 end
19         end
20         return ret
21 end
22
23 local function ripairs(t)
24         local function ripairs_it(t,i)
25                 i=i-1
26                 local v=t[i]
27                 if v==nil then return v end
28                 return i,v
29         end
30         return ripairs_it, t, #t+1
31 end
32
33 -- start positions
34
35 function elidragon.skyblock.load_legacy_start_positions()
36         local file = io.open(minetest.get_worldpath() .. "/skyblock.start_positions", "r")
37         if file then
38                 local start_positions = {}
39                 while true do
40                         local x = file:read("*n")
41                         if x == nil then
42                                 break
43                         end
44                         local y = file:read("*n")
45                         local z = file:read("*n")
46                         table.insert(start_positions, {x = x, y = y, z = z})
47                 end
48                 file:close()
49                 return start_positions
50         end
51 end
52
53 function elidragon.skyblock.load_start_positions()
54         local file = io.open(minetest.get_worldpath() .. "/start_positions", "r")
55         if file then
56                 local start_positions = minetest.deserialize(file:read())
57                 file:close()
58                 return start_positions
59         end
60 end
61
62 function elidragon.skyblock.save_start_positions(start_positions)
63         local file = io.open(minetest.get_worldpath() .. "/start_positions", "w")
64         file:write(minetest.serialize(start_positions))
65         file:close()
66 end
67
68 function elidragon.skyblock.generate_start_positions()
69         local start_positions = {}
70         for _, v in ripairs(spiralt(1000)) do
71                 local pos = {x = v.x * 32, y = math.random(4 - 8, 4 + 8), z = v.z * 32}
72                 table.insert(start_positions, pos)
73         end
74         return start_positions
75 end
76
77 elidragon.skyblock.start_positions = elidragon.skyblock.load_start_positions() 
78
79 if not elidragon.skyblock.start_positions then
80         elidragon.skyblock.start_positions = elidragon.skyblock.load_legacy_start_positions() or elidragon.skyblock.generate_start_positions()
81         elidragon.skyblock.save_start_positions(elidragon.skyblock.start_positions)
82 end
83
84 function elidragon.skyblock.load_legacy_last_start_id()
85         local file = io.open(minetest.get_worldpath() .. "/skyblock.last_start_id", "r")
86         if file then
87                 local last_start_id = tonumber(file:read())
88                 file:close()
89                 return last_start_id
90         end
91 end
92
93 elidragon.savedata.last_start_id = elidragon.savedata.last_start_id or elidragon.skyblock.load_legacy_last_start_id() or 0
94
95 -- spawns
96
97 function elidragon.skyblock.get_spawn(name)
98         return elidragon.savedata.spawns[name]
99 end
100
101 function elidragon.skyblock.set_spawn(name, pos)
102         elidragon.savedata.spawns[name] = pos
103 end
104
105 function elidragon.skyblock.spawn_player(player)
106         if not player then return end
107         local name = player:get_player_name()
108         local spawn = elidragon.skyblock.get_spawn(name) or elidragon.skyblock.new_spawn(name)
109         player:set_pos({x = spawn.x + 2, y = spawn.y + 2, z = spawn.z + 2})
110 end
111
112 function elidragon.skyblock.new_spawn(name)
113         local spawn
114         repeat
115                 elidragon.savedata.last_start_id = elidragon.savedata.last_start_id + 1
116                 spawn = elidragon.skyblock.start_positions[elidragon.savedata.last_start_id] 
117         until not minetest.is_protected(spawn, name)
118         elidragon.skyblock.set_spawn(name, spawn)       
119         local file = io.open(minetest.get_modpath("elidragon") .. "/schems/island.we", "r")
120         local schem = file:read()
121         file:close()
122         worldedit.deserialize(vector.add(spawn, {x = -3, y = -4, z = -3}), schem) 
123         return spawn
124 end
125
126 function elidragon.skyblock.load_legacy_spawns()
127     local file = io.open(minetest.get_worldpath() .. "/skyblock.spawn", "r")
128     if file then
129                 local spawns = {}
130         while true do
131             local x = file:read("*n")
132             if x == nil then
133                 break
134             end
135             local y = file:read("*n")
136             local z = file:read("*n")
137             local name = file:read("*l")
138             spawns[name:sub(2)] = {x = x, y = y, z = z}
139         end
140         file:close()
141         return spawns
142         end
143 end
144
145 elidragon.savedata.spawns = elidragon.savedata.spawns or elidragon.skyblock.load_legacy_spawns() or {}
146
147 -- node
148
149 minetest.register_node("elidragon:skyblock", {
150         description = "Skyblock",
151         tiles = {"elidragon_quest.png"},
152         paramtype = "light",
153         light_source = 14,
154         groups = {crumbly=2, cracky=2},
155 })
156
157 minetest.register_alias("skyblock:quest", "elidragon:skyblock")
158
159 -- mapgen
160
161 minetest.register_on_mapgen_init(function(mgparams)
162         minetest.set_mapgen_params({mgname = "singlenode", water_level = -32000})
163 end)
164
165 -- respawn
166
167 minetest.register_on_respawnplayer(function(player)
168         elidragon.skyblock.spawn_player(player)
169         return true
170 end)
171
172 -- remove legacy cloud layer
173
174 minetest.register_lbm({
175         nodenames = {"default:cloud"},
176         name = "elidragon:remove_cloud_layer",
177         action = function(pos)
178                 if pos.y == -10 then
179                         minetest.set_node(pos, { name = "air"})
180                 end
181         end
182 })
183
184 -- remove inventory from quest block
185
186 minetest.register_lbm({
187         nodenames = {"elidragon:skyblock", "skyblock:quest"},
188         name = "elidragon:remove_inventory_from_quest_block",
189         action = function(pos)
190                 minetest.get_meta(pos):set_string("formspec", "")
191                 minetest.get_meta(pos):set_string("infotext", "")
192         end
193 })
194
195 -- ores
196
197 minetest.after(0, function()
198         default.cool_lava = function(pos, oldnode)
199                 local node
200                 if oldnode.name == "default:lava_source" then
201                         node = "default:obsidian"
202                 elseif math.random() < 0.0001 then
203                         node = "moreores:mineral_mithril"
204                 elseif math.random() < 0.0003 then
205                         node = "default:stone_with_diamond"
206                 elseif math.random() < 0.0005 then
207                         node = "default:stone_with_mese"
208                 elseif math.random() < 0.001 then
209                         node = "default:stone_with_gold"
210                 elseif math.random() < 0.001 then
211                         node = "technic:mineral_chromium"
212                 elseif math.random() < 0.001 then
213                         node = "technic:mineral_zinc"
214                 elseif math.random() < 0.0012 then
215                         node = "technic:mineral_uranium"
216                 elseif math.random() < 0.0015 then
217                         node = "default:stone_with_tin"
218                 elseif math.random() < 0.002 then
219                         node = "default:stone_with_copper"
220                 elseif math.random() < 0.0025 then
221                         node = "technic:mineral_sulfur"
222                 elseif math.random() < 0.0033 then
223                         node = "default:stone_with_iron"
224                 elseif math.random() < 0.004 then
225                         node = "moreores:mineral_silver"
226                 elseif math.random() < 0.0045 then
227                         node = "technic:mineral_lead"
228                 elseif math.random() < 0.005 then
229                         node = "default:stone_with_coal"
230                 else
231                         node = "default:stone"
232                 end
233                 minetest.set_node(pos, {name = node})
234                 minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25}, true)
235         end
236 end)
237
238 -- saplings
239
240 minetest.after(0, function()
241         minetest.register_alias("default:pine_leaves", "default:pine_needles")
242         minetest.register_alias("default:pine_bush_leaves", "default:pine_bush_needles")
243         local trees = {"default:", "default:jungle", "default:pine_", "default:acacia_", "default:aspen_", "default:bush_", "default:blueberry_bush_", "default:acacia_bush_", "default:pine_bush_", "moretrees:apple_tree_", "moretrees:beech_", "moretrees:cedar_", "moretrees:date_palm_", "moretrees:fir_", "moretrees:oak_", "moretrees:palm_", "moretrees:poplar_", "moretrees:sequoia_", "moretrees:spruce_", "moretrees:willow_", }
244         for _, tree in pairs(trees) do
245                 local items = {}
246                 items[#items + 1] = {
247                         items = {tree .. "sapling"},
248                         rarity = 20,
249                 }
250                 for _, stree in pairs(trees) do
251                         if stree ~= tree then
252                                 items[#items + 1] = {
253                                         items = {stree .. "sapling"},
254                                         rarity = 10000,
255                                 }
256                         end
257                 end
258                 items[#items + 1] = {
259                         items = {tree .. "leaves"},
260                 }
261                 minetest.registered_nodes[tree .. "leaves"].drop = {max_items = 1, items = items}
262         end
263 end)
264
265 -- flowers
266
267 minetest.register_abm({
268         nodenames = {"default:dirt_with_grass"},
269         interval = 300,
270         chance = 100,
271         action = function(pos, node)
272                 pos.y = pos.y + 1
273                 local light = minetest.get_node_light(pos) or 0
274                 if minetest.get_node(pos).name == "air" and light > 12 and not minetest.find_node_near(pos, 2, {"group:flora"}) then
275                         local flowers = {"default:junglegrass", "default:grass_1", "flowers:dandelion_white", "flowers:dandelion_yellow", "flowers:geranium", "flowers:rose", "flowers:tulip", "flowers:tulip_black", "flowers:viola", "flowers:chrysanthemum_green"}
276                         minetest.set_node(pos, {name = flowers[math.random(#flowers)]})
277                 end
278         end
279 })
280
281 -- recipes
282
283 minetest.register_craft({
284         output = "default:desert_sand",
285         recipe = {
286                 {"default:sand"},
287                 {"default:gravel"},
288         }
289 })
290
291 minetest.register_craft({
292         output = "default:desert_stone",
293         recipe = {
294                 {"default:desert_sand", "default:desert_sand", "default:desert_sand"},
295                 {"default:desert_sand", "default:desert_sand", "default:desert_sand"},
296                 {"default:desert_sand", "default:desert_sand", "default:desert_sand"},
297         }
298 })
299
300 minetest.register_craft({
301         output = "default:sand 4",
302         recipe = {
303                 {"default:obsidian_shard"},
304         }
305 })
306
307 minetest.register_craft({
308         output = "default:gravel 2",
309         recipe = {
310                 {"default:cobble"},
311         }
312 })
313
314 minetest.register_craft({
315         output = "default:dirt 2",
316         recipe = {
317                 {"default:gravel"},
318         }
319 })
320
321 minetest.register_craft({
322         output = "default:clay_lump 4",
323         recipe = {
324                 {"default:dirt"},
325         }
326 })
327
328 minetest.register_craft({
329         output = "default:ice",
330         recipe = {
331                 {"bucket:bucket_water"},
332         }
333 })
334
335 minetest.register_craft({
336         output = "default:snowblock 4",
337         recipe = {
338                 {"default:ice"},
339         }
340 })
341
342 minetest.register_craft({
343         type = "cooking",
344         output = "default:lava_source",
345         recipe = "default:stone",
346 })
347
348 minetest.register_craft({
349         output = "default:silver_sand 9",
350         recipe = {
351                 {"default:sand", "default:sand", "default:sand"},
352                 {"default:sand", "moreores:silver_lump", "default:sand"},
353                 {"default:sand", "default:sand", "default:sand"},
354         }
355 })
356
357 -- commands
358
359 minetest.register_chatcommand("set_skyblock_spawn", {
360     param = "<player> <x> <y> <z>",
361     desc = "Change the skyblocks spawn of a player",
362     privs = {server = true},
363     func = function(admin, param)
364         local name = param:split(" ")[1]
365         local x = tonumber(param:split(" ")[2])
366         local y = tonumber(param:split(" ")[3])
367         local z = tonumber(param:split(" ")[4])
368         if name and x and y and z then
369             elidragon.skyblock.set_spawn(name, {x = x, y = y, z = z})
370         else
371             minetest.chat_send_player(admin, "Invalid usage.")
372         end
373     end
374 })
375
376 minetest.register_chatcommand("island", {
377         params = "",
378         description = "Teleport to your Island",
379         func = function(name, param)
380                 elidragon.skyblock.spawn_player(minetest.get_player_by_name(name))
381         end,
382 })