Support for deviant target position in waypoint metadata. Support for skipping waypoint name change

This commit is contained in:
Alexander Weber 2017-06-15 01:24:52 +02:00
parent e5a3742e8d
commit 35cf197ce4
2 changed files with 70 additions and 24 deletions

View File

@ -29,7 +29,9 @@ The mod support the next settings:
1. It is possible to change compass settings from other mods by changing values in global table ccompass. So it is possible for example to add a waypoint node to the target-nodes by
```
ccompass.restrict_target_nodes[nodename] = true
ccompass.recalibrate = true
ccompass.restrict_target = true
ccompass.restrict_target_nodes["schnitzeljagd:waypoint"] = true
```
2. The pointed node metadata will be checked for "waypoint_name" attribute. It this attribute is set, the calibration screen take this string as proposal. This can be used for a game specific calibration node. To get it working working just set in node definition something like
@ -38,21 +40,32 @@ The mod support the next settings:
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("waypoint_name", "the unique and wunderfull place")
meta:set_string("waypoint_pos", minetest.pos_to_string(target_pos)) -- if an other position should be the target instead of the node position
meta:set_string("waypoint_skip_namechange", "skip") -- do not ask for the waypoint name
end,
```
3. It is possible to create pre-calibrated compasses trough other mods. Just write the position to the Itemstack meta:
```
local stack = ItemStack("ccompass:0")
stack:get_meta():set_string("target_pos", minetest.pos_to_string(pos))
```
Recalibration related to a user should be done by function call
```
local stack = ItemStack("ccompass:0")
ccompass.set_target(stack, {
target_pos_string = minetest.pos_to_string(pos),
target_name = waypoint_name,
playername = player:get_player_name()
})
```
4. Each time the compass is updated, a hook is called, if defined in other mod. The hook is usefull to implement wear or any other compass manipulation logic.
```
function ccompass.usage_hook(compass_stack, player)
--do anything with compasS_stack
--do anything with compass_stack
return modified_compass_stack
end
```

View File

@ -35,6 +35,28 @@ if minetest.setting_getbool("ccompass_aliasses") then
minetest.register_alias("compass:11", "ccompass:11")
end
-- set a position to the compass stack
function ccompass.set_target(stack, param)
param = param or {}
-- param.target_pos_string
-- param.target_name
-- param.playername
local meta=stack:get_meta()
meta:set_string("target_pos", param.target_pos_string)
if param.target_name == "" then
meta:set_string("description", "Compass to "..param.target_pos_string)
else
meta:set_string("description", "Compass to "..param.target_name)
end
if param.playername then
local player = minetest.get_player_by_name(param.playername)
minetest.chat_send_player(param.playername, "Calibration done to "..param.target_name.." "..param.target_pos_string)
minetest.sound_play({ name = "ccompass_calibrate", gain = 1 }, { pos = player:getpos(), max_hear_distance = 3 })
end
end
-- Get compass target
local function get_destination(player, stack)
@ -73,10 +95,10 @@ local function get_compass_stack(player, stack)
end
-- Calibrate compass on pointed_thing
local function on_use_function(itemstack, user, pointed_thing)
local function on_use_function(itemstack, player, 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")
minetest.chat_send_player(player:get_player_name(), "Calibration can be done on nodes only")
return
end
@ -84,7 +106,7 @@ local function on_use_function(itemstack, user, pointed_thing)
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")
minetest.chat_send_player(player:get_player_name(), "Compass already calibrated")
return
end
end
@ -94,7 +116,7 @@ local function on_use_function(itemstack, user, 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")
minetest.chat_send_player(player:get_player_name(), "Calibration on this node not possible")
return
end
end
@ -103,14 +125,30 @@ local function on_use_function(itemstack, user, pointed_thing)
local nodepos_string = minetest.pos_to_string(nodepos)
local nodemeta = minetest.get_meta(nodepos)
local waypoint_name = nodemeta:get_string("waypoint_name")
local waypoint_pos = nodemeta:get_string("waypoint_pos")
local skip_namechange = nodemeta:get_string("waypoint_skip_namechange")
local itemmeta=itemstack:get_meta()
-- 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]")
if waypoint_pos and waypoint_pos ~= "" then
nodepos_string = waypoint_pos
end
if skip_namechange ~= "" then
ccompass.set_target(itemstack, {
target_pos_string = nodepos_string,
target_name = waypoint_name,
playername = player:get_player_name()
})
else
-- show the formspec to player
itemmeta:set_string("tmp_target_pos", nodepos_string) --just save temporary
minetest.show_formspec(player: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]")
end
return itemstack
end
@ -119,18 +157,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "ccompass" and fields.name and (fields.ok or fields.key_enter) then
local stack=player:get_wielded_item()
local meta=stack:get_meta()
local pos_string = meta:get_string("tmp_target_pos")
local pos = player:getpos()
meta:set_string("target_pos", pos_string)
ccompass.set_target(stack, {
target_pos_string = meta:get_string("tmp_target_pos"),
target_name = fields.name,
playername = player:get_player_name()
})
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)
minetest.chat_send_player(player:get_player_name(), "Calibration done to "..fields.name.." "..pos_string)
minetest.sound_play({ name = "ccompass_calibrate", gain = 1 }, { pos = pos, max_hear_distance = 3 })
end
end)