2019-03-07 23:40:43 +01:00
|
|
|
local S = minetest.get_translator("mcl_compass")
|
|
|
|
|
2017-02-20 21:14:10 +01:00
|
|
|
mcl_compass = {}
|
2015-06-29 19:55:56 +02:00
|
|
|
|
2017-08-17 21:26:09 +02:00
|
|
|
local compass_frames = 32
|
|
|
|
|
2017-08-09 16:17:00 +02:00
|
|
|
local default_spawn_settings = minetest.settings:get("static_spawnpoint")
|
2015-06-29 19:55:56 +02:00
|
|
|
|
2017-08-17 21:26:09 +02:00
|
|
|
-- Timer for random compass spinning
|
|
|
|
local random_timer = 0
|
|
|
|
local random_timer_trigger = 0.5 -- random compass spinning tick in seconds. Increase if there are performance problems
|
|
|
|
|
|
|
|
local random_frame = math.random(0, compass_frames-1)
|
|
|
|
|
2015-06-29 19:55:56 +02:00
|
|
|
minetest.register_globalstep(function(dtime)
|
2017-08-17 21:26:09 +02:00
|
|
|
random_timer = random_timer + dtime
|
2015-06-29 19:55:56 +02:00
|
|
|
local players = minetest.get_connected_players()
|
2017-08-17 21:26:09 +02:00
|
|
|
|
|
|
|
if random_timer >= random_timer_trigger then
|
|
|
|
random_frame = (random_frame + math.random(-1, 1)) % compass_frames
|
|
|
|
random_timer = 0
|
|
|
|
end
|
2015-06-29 19:55:56 +02:00
|
|
|
for i,player in ipairs(players) do
|
|
|
|
local function has_compass(player)
|
|
|
|
for _,stack in ipairs(player:get_inventory():get_list("main")) do
|
|
|
|
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if has_compass(player) then
|
2019-02-01 06:33:07 +01:00
|
|
|
local pos = player:get_pos()
|
2017-11-24 03:10:02 +01:00
|
|
|
local dim = mcl_worlds.pos_to_dimension(pos)
|
2017-08-17 21:26:09 +02:00
|
|
|
local compass_image
|
2017-08-22 18:18:53 +02:00
|
|
|
-- Compasses do not work in certain zones
|
2017-11-24 03:10:02 +01:00
|
|
|
if not mcl_worlds.compass_works(pos) then
|
2017-08-17 21:26:09 +02:00
|
|
|
compass_image = random_frame
|
|
|
|
else
|
|
|
|
local spawn = {x=0,y=0,z=0}
|
2017-11-05 02:50:31 +01:00
|
|
|
local ssp = minetest.setting_get_pos("static_spawnpoint")
|
|
|
|
if ssp then
|
|
|
|
spawn = ssp
|
|
|
|
if type(spawn) ~= "table" or type(spawn.x) ~= "number" or type(spawn.y) ~= "number" or type(spawn.z) ~= "number" then
|
2017-08-17 21:26:09 +02:00
|
|
|
spawn = {x=0,y=0,z=0}
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 20:40:55 +02:00
|
|
|
local dir = player:get_look_horizontal()
|
|
|
|
local angle_north = math.deg(math.atan2(spawn.x - pos.x, spawn.z - pos.z))
|
|
|
|
if angle_north < 0 then angle_north = angle_north + 360 end
|
|
|
|
local angle_dir = -math.deg(dir)
|
|
|
|
local angle_relative = (angle_north - angle_dir + 180) % 360
|
2017-08-17 21:26:09 +02:00
|
|
|
compass_image = math.floor((angle_relative/11.25) + 0.5) % compass_frames
|
|
|
|
end
|
2015-06-29 19:55:56 +02:00
|
|
|
|
2017-08-17 21:26:09 +02:00
|
|
|
for j,stack in ipairs(player:get_inventory():get_list("main")) do
|
|
|
|
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 and
|
|
|
|
minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image then
|
|
|
|
local count = stack:get_count()
|
|
|
|
player:get_inventory():set_stack("main", j, ItemStack("mcl_compass:"..compass_image.." "..count))
|
2015-06-29 19:55:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-01-09 02:03:09 +01:00
|
|
|
local images = {}
|
2017-08-17 21:26:09 +02:00
|
|
|
for frame = 0, compass_frames-1 do
|
2017-07-20 22:04:15 +02:00
|
|
|
local s = string.format("%02d", frame)
|
|
|
|
table.insert(images, "mcl_compass_compass_"..s..".png")
|
2017-01-09 02:03:09 +01:00
|
|
|
end
|
2015-06-29 19:55:56 +02:00
|
|
|
|
2017-03-20 18:12:05 +01:00
|
|
|
local doc_mod = minetest.get_modpath("doc") ~= nil
|
|
|
|
|
2017-01-09 03:26:00 +01:00
|
|
|
local stereotype_frame = 18
|
2015-06-29 19:55:56 +02:00
|
|
|
for i,img in ipairs(images) do
|
|
|
|
local inv = 1
|
2017-01-09 03:26:00 +01:00
|
|
|
if i == stereotype_frame then
|
2015-06-29 19:55:56 +02:00
|
|
|
inv = 0
|
|
|
|
end
|
2020-02-19 04:54:17 +01:00
|
|
|
local use_doc, longdesc, usagehelp, tt
|
2017-03-20 18:12:05 +01:00
|
|
|
use_doc = i == stereotype_frame
|
|
|
|
if use_doc then
|
2020-02-19 04:54:17 +01:00
|
|
|
tt = S("Points to the world origin")
|
2019-03-07 23:40:43 +01:00
|
|
|
longdesc = S("Compasses are tools which point to the world origin (X=0, Z=0) or the spawn point in the Overworld.")
|
2017-03-02 21:55:25 +01:00
|
|
|
end
|
2017-03-20 18:12:05 +01:00
|
|
|
local itemstring = "mcl_compass:"..(i-1)
|
|
|
|
minetest.register_craftitem(itemstring, {
|
2019-03-07 23:40:43 +01:00
|
|
|
description = S("Compass"),
|
2020-02-19 04:54:17 +01:00
|
|
|
_tt_help = tt,
|
2017-03-20 18:12:05 +01:00
|
|
|
_doc_items_create_entry = use_doc,
|
2017-03-02 21:55:25 +01:00
|
|
|
_doc_items_longdesc = longdesc,
|
|
|
|
_doc_items_usagehelp = usagehelp,
|
2015-06-29 19:55:56 +02:00
|
|
|
inventory_image = img,
|
|
|
|
wield_image = img,
|
2017-02-08 16:59:34 +01:00
|
|
|
stack_max = 64,
|
2018-12-07 21:23:39 +01:00
|
|
|
groups = {not_in_creative_inventory=inv, compass=i, tool=1, disable_repair=1}
|
2015-06-29 19:55:56 +02:00
|
|
|
})
|
2017-03-20 18:12:05 +01:00
|
|
|
|
|
|
|
-- Help aliases. Makes sure the lookup tool works correctly
|
|
|
|
if not use_doc and doc_mod then
|
|
|
|
doc.add_entry_alias("craftitems", "mcl_compass:"..(stereotype_frame-1), "craftitems", itemstring)
|
|
|
|
end
|
2015-06-29 19:55:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_craft({
|
2017-01-09 03:26:00 +01:00
|
|
|
output = 'mcl_compass:'..stereotype_frame,
|
2015-06-29 19:55:56 +02:00
|
|
|
recipe = {
|
2017-02-11 21:14:40 +01:00
|
|
|
{'', 'mcl_core:iron_ingot', ''},
|
|
|
|
{'mcl_core:iron_ingot', 'mesecons:redstone', 'mcl_core:iron_ingot'},
|
|
|
|
{'', 'mcl_core:iron_ingot', ''}
|
2015-06-29 19:55:56 +02:00
|
|
|
}
|
2017-01-04 05:04:13 +01:00
|
|
|
})
|
2017-01-09 03:26:00 +01:00
|
|
|
|
|
|
|
minetest.register_alias("mcl_compass:compass", "mcl_compass:"..stereotype_frame)
|
2017-02-20 21:14:10 +01:00
|
|
|
|
|
|
|
-- Export stereotype item for other mods to use
|
|
|
|
mcl_compass.stereotype = "mcl_compass:"..tostring(stereotype_frame)
|
|
|
|
|
|
|
|
|