Fix crash on join w/ compass + static_spawnpoint set in bracket format
[MineClone/MineClone2/killajk2201.git] / mods / ITEMS / mcl_compass / init.lua
blob02ed39bf8f7113fcca4af0c6d4f5e2f954cc2e67
1 mcl_compass = {}
3 local compass_frames = 32
5 local default_spawn_settings = minetest.settings:get("static_spawnpoint")
7 -- Timer for random compass spinning
8 local random_timer = 0
9 local random_timer_trigger = 0.5 -- random compass spinning tick in seconds. Increase if there are performance problems
11 local random_frame = math.random(0, compass_frames-1)
13 minetest.register_globalstep(function(dtime)
14 random_timer = random_timer + dtime
15 local players = minetest.get_connected_players()
17 if random_timer >= random_timer_trigger then
18 random_frame = (random_frame + math.random(-1, 1)) % compass_frames
19 random_timer = 0
20 end
21 for i,player in ipairs(players) do
22 local function has_compass(player)
23 for _,stack in ipairs(player:get_inventory():get_list("main")) do
24 if minetest.get_item_group(stack:get_name(), "compass") ~= 0 then
25 return true
26 end
27 end
28 return false
29 end
30 if has_compass(player) then
31 local pos = player:getpos()
32 local _, dim = mcl_util.y_to_layer(pos.y)
33 local compass_image
34 -- Compasses do not work in certain zones
35 if not mcl_util.compass_works(pos) then
36 compass_image = random_frame
37 else
38 local spawn = {x=0,y=0,z=0}
39 local ssp = minetest.setting_get_pos("static_spawnpoint")
40 if ssp then
41 spawn = ssp
42 if type(spawn) ~= "table" or type(spawn.x) ~= "number" or type(spawn.y) ~= "number" or type(spawn.z) ~= "number" then
43 spawn = {x=0,y=0,z=0}
44 end
45 end
46 local dir = player:get_look_horizontal()
47 local angle_north = math.deg(math.atan2(spawn.x - pos.x, spawn.z - pos.z))
48 if angle_north < 0 then angle_north = angle_north + 360 end
49 local angle_dir = -math.deg(dir)
50 local angle_relative = (angle_north - angle_dir + 180) % 360
51 compass_image = math.floor((angle_relative/11.25) + 0.5) % compass_frames
52 end
54 for j,stack in ipairs(player:get_inventory():get_list("main")) do
55 if minetest.get_item_group(stack:get_name(), "compass") ~= 0 and
56 minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image then
57 local count = stack:get_count()
58 player:get_inventory():set_stack("main", j, ItemStack("mcl_compass:"..compass_image.." "..count))
59 end
60 end
61 end
62 end
63 end)
65 local images = {}
66 for frame = 0, compass_frames-1 do
67 local s = string.format("%02d", frame)
68 table.insert(images, "mcl_compass_compass_"..s..".png")
69 end
71 local doc_mod = minetest.get_modpath("doc") ~= nil
73 local stereotype_frame = 18
74 for i,img in ipairs(images) do
75 local inv = 1
76 if i == stereotype_frame then
77 inv = 0
78 end
79 local use_doc, longdesc, usagehelp
80 use_doc = i == stereotype_frame
81 if use_doc then
82 longdesc = "Compasses are tools which point to the world origin (X=0, Z=0) or the spawn point in the Overworld."
83 end
84 local itemstring = "mcl_compass:"..(i-1)
85 minetest.register_craftitem(itemstring, {
86 description = "Compass",
87 _doc_items_create_entry = use_doc,
88 _doc_items_longdesc = longdesc,
89 _doc_items_usagehelp = usagehelp,
90 inventory_image = img,
91 wield_image = img,
92 stack_max = 64,
93 groups = {not_in_creative_inventory=inv, compass=i, tool=1}
96 -- Help aliases. Makes sure the lookup tool works correctly
97 if not use_doc and doc_mod then
98 doc.add_entry_alias("craftitems", "mcl_compass:"..(stereotype_frame-1), "craftitems", itemstring)
99 end
102 minetest.register_craft({
103 output = 'mcl_compass:'..stereotype_frame,
104 recipe = {
105 {'', 'mcl_core:iron_ingot', ''},
106 {'mcl_core:iron_ingot', 'mesecons:redstone', 'mcl_core:iron_ingot'},
107 {'', 'mcl_core:iron_ingot', ''}
111 minetest.register_alias("mcl_compass:compass", "mcl_compass:"..stereotype_frame)
113 -- Export stereotype item for other mods to use
114 mcl_compass.stereotype = "mcl_compass:"..tostring(stereotype_frame)