3 local compass_frames
= 32
5 local default_spawn_settings
= minetest
.settings
:get("static_spawnpoint")
7 -- Timer for random compass spinning
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
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
30 if has_compass(player
) then
31 local pos
= player
:getpos()
32 local _
, dim
= mcl_util
.y_to_layer(pos
.y
)
34 -- Compasses do not work in certain zones
35 if not mcl_util
.compass_works(pos
) then
36 compass_image
= random_frame
38 local spawn
= {x
=0,y
=0,z
=0}
39 local ssp
= minetest
.setting_get_pos("static_spawnpoint")
42 if type(spawn
) ~= "table" or type(spawn
.x
) ~= "number" or type(spawn
.y
) ~= "number" or type(spawn
.z
) ~= "number" then
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
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
))
66 for frame
= 0, compass_frames
-1 do
67 local s
= string.format("%02d", frame
)
68 table.insert(images
, "mcl_compass_compass_"..s
..".png")
71 local doc_mod
= minetest
.get_modpath("doc") ~= nil
73 local stereotype_frame
= 18
74 for i
,img
in ipairs(images
) do
76 if i
== stereotype_frame
then
79 local use_doc
, longdesc
, usagehelp
80 use_doc
= i
== stereotype_frame
82 longdesc
= "Compasses are tools which point to the world origin (X=0, Z=0) or the spawn point in the Overworld."
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
,
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
)
102 minetest
.register_craft({
103 output
= 'mcl_compass:'..stereotype_frame
,
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
)