renamed fork to ccompass (calibrated compass) + rework

This commit is contained in:
Alexander Weber 2017-06-13 13:45:42 +02:00
parent 35ceb3ddf6
commit c7f630c18b
15 changed files with 132 additions and 44 deletions

157
init.lua
View File

@ -1,61 +1,143 @@
-- default to static_spawnpoint or 0/0/0
local default_spawn = minetest.setting_get_pos("static_spawnpoint") or {x=0, y=0, z=0}
-- compass configuration interface - adjustable from other mods or minetest.conf settings
local ccompass = {}
-- supported modes: default, sethome, beds, origin
local compass_mode = minetest.setting_get("compass_mode") or "default"
-- default target to static_spawnpoint or 0/0/0
ccompass.default_target = minetest.setting_get_pos("static_spawnpoint") or {x=0, y=0, z=0}
-- Get players spawn point (compass target) in order sethome, beds-spawn and static_spawnpoint
-- Re-calibration allowed
ccompass.recalibrate = minetest.setting_getbool("ccompass_recalibrate")
if ccompass.recalibrate == nil then
ccompass.recalibrate = true
end
-- Target restriction
ccompass.restrict_target = minetest.setting_getbool("ccompass_restrict_target")
ccompass.restrict_target_nodes = {}
local nodes_setting = minetest.setting_get("ccompass_restrict_target_nodes")
if nodes_setting then
nodes_setting:gsub("[^,]+", function(z)
ccompass.restrict_target_nodes[z] = true
end)
end
if minetest.setting_getbool("ccompass_aliasses") then
minetest.register_alias("compass:0", "ccompass:0")
minetest.register_alias("compass:1", "ccompass:1")
minetest.register_alias("compass:2", "ccompass:2")
minetest.register_alias("compass:3", "ccompass:3")
minetest.register_alias("compass:4", "ccompass:4")
minetest.register_alias("compass:5", "ccompass:5")
minetest.register_alias("compass:6", "ccompass:6")
minetest.register_alias("compass:7", "ccompass:7")
minetest.register_alias("compass:8", "ccompass:8")
minetest.register_alias("compass:9", "ccompass:9")
minetest.register_alias("compass:10", "ccompass:10")
minetest.register_alias("compass:11", "ccompass:11")
end
-- Get compass target
local function get_destination(player, stack)
if compass_mode == "default" then
return minetest.setting_get_pos("static_spawnpoint") or default_spawn
elseif compass_mode == "sethome" then
return sethome.get(player:get_player_name()) or default_spawn
elseif compass_mode == "beds" then
return beds.spawn[player:get_player_name()] or default_spawn
elseif compass_mode == "origin" then
return minetest.string_to_pos(stack:get_metadata())
local posstring = stack:get_meta():get_string("target_pos")
if posstring ~= "" then
return minetest.string_to_pos(posstring)
else
return ccompass.default_target
end
end
-- get right image number for players compas
local function get_compass_stack(player, stack)
local spawn = get_destination(player, stack)
local target = get_destination(player, stack)
local pos = player:getpos()
local dir = player:get_look_yaw()
local angle_north = math.deg(math.atan2(spawn.x - pos.x, spawn.z - pos.z))
local angle_north = math.deg(math.atan2(target.x - pos.x, target.z - pos.z))
if angle_north < 0 then
angle_north = angle_north + 360
end
local angle_dir = 90 - math.deg(dir)
local angle_relative = (angle_north - angle_dir) % 360
local compass_image = math.floor((angle_relative/30) + 0.5)%12
local meta = stack:get_metadata()
local newstack = ItemStack("compass:"..compass_image)
--meta:set_string("description", "Compass to "..minetest.pos_to_string(spawn)) -- does not work on stable 0.4.15
newstack:set_metadata(meta)
-- create new stack with metadata copied
local metadata = stack:get_meta():to_table()
local newstack = ItemStack("ccompass:"..compass_image)
if metadata then
newstack:get_meta():from_table(metadata)
end
return newstack
end
-- Calibrate compass on pointed_thing
local function on_use_function(itemstack, user, pointed_thing)
-- possible only on nodes
if pointed_thing.type ~= "node" then --support nodes only for destination
minetest.chat_send_player(user:get_player_name(), "Calibration can be done on nodes only")
return
end
-- recalibration allowed?
if not ccompass.recalibrate then
local destination = itemstack:get_meta():get_string("target_pos")
if destination ~= "" then
minetest.chat_send_player(user:get_player_name(), "Compass already calibrated")
return
end
end
-- target nodes restricted?
local nodepos = minetest.get_pointed_thing_position(pointed_thing)
if ccompass.restrict_target then
local node = minetest.get_node(nodepos)
if not ccompass.restrict_target_nodes[node.name] then
minetest.chat_send_player(user:get_player_name(), "Calibration on this node not possible")
return
end
end
-- check if waypoint name set in target node
local nodepos_string = minetest.pos_to_string(nodepos)
local nodemeta = minetest.get_meta(nodepos)
local waypoint_name = nodemeta:get_string("waypoint_name")
-- show the formspec to user
itemstack:get_meta():set_string("tmp_target_pos", nodepos_string) --just save temporary
minetest.show_formspec(user:get_player_name(), "ccompass",
"size[10,2.5]" ..
"field[1,1;8,1;name;Destination name:;"..waypoint_name.."]"..
"button_exit[0.7,2;5,1;ok;Calibrate]" ..
"button_exit[5.7,2;3,1;cancel;Cancel]")
return itemstack
end
-- Process the calibration using entered data
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "ccompass" and fields.name and fields.ok then
local stack=player:get_wielded_item()
local meta=stack:get_meta()
local pos_string = meta:get_string("tmp_target_pos")
meta:set_string("target_pos", pos_string)
meta:set_string("tmp_target_pos", "")
if fields.name == "" then
meta:set_string("description", "Compass to "..pos_string)
else
meta:set_string("description", "Compass to "..fields.name)
end
player:set_wielded_item(stack)
end
end)
-- update inventory
minetest.register_globalstep(function(dtime)
for i,player in ipairs(minetest.get_connected_players()) do
if player:get_inventory() then
for i,stack in ipairs(player:get_inventory():get_list("main")) do
if string.sub(stack:get_name(), 0, 8) == "compass:" then
if compass_mode == "origin" then
local meta = stack:get_metadata()
if not meta or meta == "" then
meta = minetest.pos_to_string(player:getpos())
stack:set_metadata(meta)
player:get_inventory():set_stack("main", i, stack)
end
elseif i > 8 then
break
end
if i <= 8 then
player:get_inventory():set_stack("main", i, get_compass_stack(player, stack))
end
if i > 8 then
break
end
if string.sub(stack:get_name(), 0, 9) == "ccompass:" then
player:get_inventory():set_stack("main", i, get_compass_stack(player, stack))
end
end
end
@ -64,21 +146,22 @@ end)
-- register items
for i = 0, 11 do
local image = "compass_"..i..".png"
local image = "ccompass_"..i..".png"
local groups = {}
if i > 0 then
groups.not_in_creative_inventory = 1
end
minetest.register_tool("compass:"..i, {
minetest.register_tool("ccompass:"..i, {
description = "Compass",
inventory_image = image,
wield_image = image,
groups = groups
groups = groups,
on_use = on_use_function,
})
end
minetest.register_craft({
output = 'compass:0',
output = 'ccompass:0',
recipe = {
{'', 'default:steel_ingot', ''},
{'default:steel_ingot', 'default:mese_crystal_fragment', 'default:steel_ingot'},

View File

@ -1 +1 @@
name = compass
name = ccompass

View File

@ -1,6 +1,11 @@
# Where the compass should point
# default = points to the "static_spawnpoint" setting coordinates or to 0,0,0 if nothing set
# sethome = points to the players home from sethome mod, default as fallback
# beds = points to the players spawn point trough the beds mod, default as fallback
# origin = points to the individual compass origin that means the place the compass was crafted or picked first time
compass_mode (Compass Mode) string default
# If disabled each compass can be calibrated one time only
ccompass_recalibrate (Allow compass recalibration) bool true
# If disabled (default) all nodes are allowed to be compass target
ccompass_restrict_target (Restrict nodes usable for calibration) bool false
# List of technical node names allowed for compass calibration, separated by ','
ccompass_restrict_target_nodes (Nodes list allowed for calibration) string
# Enable aliasses to replace other compass mods
ccompass_aliasses (Enable compatibility aliasses) bool false

View File

Before

Width:  |  Height:  |  Size: 485 B

After

Width:  |  Height:  |  Size: 485 B

View File

Before

Width:  |  Height:  |  Size: 492 B

After

Width:  |  Height:  |  Size: 492 B

View File

Before

Width:  |  Height:  |  Size: 499 B

After

Width:  |  Height:  |  Size: 499 B

View File

Before

Width:  |  Height:  |  Size: 499 B

After

Width:  |  Height:  |  Size: 499 B

View File

Before

Width:  |  Height:  |  Size: 490 B

After

Width:  |  Height:  |  Size: 490 B

View File

Before

Width:  |  Height:  |  Size: 487 B

After

Width:  |  Height:  |  Size: 487 B

View File

Before

Width:  |  Height:  |  Size: 499 B

After

Width:  |  Height:  |  Size: 499 B

View File

Before

Width:  |  Height:  |  Size: 492 B

After

Width:  |  Height:  |  Size: 492 B

View File

Before

Width:  |  Height:  |  Size: 484 B

After

Width:  |  Height:  |  Size: 484 B

View File

Before

Width:  |  Height:  |  Size: 492 B

After

Width:  |  Height:  |  Size: 492 B

View File

Before

Width:  |  Height:  |  Size: 495 B

After

Width:  |  Height:  |  Size: 495 B

View File

Before

Width:  |  Height:  |  Size: 487 B

After

Width:  |  Height:  |  Size: 487 B