guards/formspec.lua

101 lines
3.3 KiB
Lua

-- which NPC object did the player talk to?
guards.player_talked_to = {}
guards.on_receive_fields = function(player, formname, fields)
local name = player:get_player_name()
local self = guards.player_talked_to[name]
if(not(self)) then
return
end
if self.owner ~= name then
return
end
if fields.origin then
self.origin.pos = self.object:get_pos()
self.origin.yaw = self.object:get_yaw()
end
if fields.follow_owner and fields.follow_owner == "true" then
self.metadata.patrol = "false"
self.order = "follow"
self.following = player
minetest.chat_send_player(name,
"New Order: "..tostring(self.order)..
" State: "..tostring(self.state)..
" Owner: "..tostring(self.owner))
elseif fields.walk_speed and fields.walk_speed ~= "" and fields.walk_speed ~= tostring(self.walk_velocity) then
self.walk_velocity = math.max(1, tonumber(fields.walk_speed)) * 1.0
elseif fields.fear_height and fields.fear_height ~= "" and fields.fear_height ~= tostring(self.fear_height) then
self.fear_height = math.max(1, tonumber(fields.fear_height))
elseif fields.follow_owner and fields.follow_owner == "false" then
self.order = "stand"
self.following = nil
elseif fields.patrol then
if fields.patrol == "false" then
self.metadata.patrol_index = 0
end
self.order = "patrol"
self.following = nil
elseif fields.add_patrol then
local pos = self.object:get_pos()
if pos then
table.insert(self.metadata.patrol_points, pos)
end
elseif fields.clear_patrol then
self.metadata.patrol_points = {}
else
return
end
minetest.show_formspec(name, "guards:main", guards.get_formspec(self, player))
end
guards.get_formspec = function(self, clicker)
return "size[8,8.5]"
.."checkbox[0.5,4.5;follow_owner;Follow;"..tostring(self.order == "follow").."]"
.."checkbox[3.5,4.5;patrol;Patrol;"..self.metadata.patrol.."]"
.."field[6.0,2.5;2.0.0,0.5;walk_speed;Walking speed;"..tostring(self.walk_velocity).."]"
.."field[6.0,4.0;2.0.0,0.5;fear_height;Fear height;"..tostring(self.fear_height).."]"
.."field[6.0,5.5;2.0.0,0.5;rest_time;Rest (sec);"..self.metadata.patrol_rest.."]"
.."label[0.5,6.5;Patrol Points: "..#self.metadata.patrol_points.."]"
.."button[5.5,6.5;2.5,0.5;add_patrol;Add Point]"
.."button[3.5,6.5;2.0,0.5;clear_patrol;Clear]"
.."button[0.0,8.0;2.0,0.5;origin;Set Origin]"
.."button_exit[7.0,8.0;1.0,0.5;;Ok]"
end
guards.on_rightclick = function(self, clicker)
if(not(clicker)) then
return
end
-- heal with bread
local item = clicker:get_wielded_item()
if(item and item:get_name() == "farming:bread") then
if(self.health < (self.hp_max or self.health)) then
item:take_item()
clicker:set_wielded_item(item)
self.health = math.min((self.hp_max or self.health), self.health + 10)
end
return
end
local name = clicker:get_player_name()
if name and name == self.owner then
-- catch the guard with a lasso
if mobs:capture_mob(self, clicker, nil, 100, 100, true, nil) then
return
end
guards.player_talked_to[name] = self
minetest.show_formspec(name, "guards:main", guards.get_formspec(self, clicker))
end
end
minetest.register_on_player_receive_fields( function(player, formname, fields)
if(formname == "guards:main") then
guards.on_receive_fields(player, formname, fields)
return true
end
end)