Actions: Support for sitting on benches and stairs.

This commit is contained in:
zorman2000 2017-01-11 07:54:50 -05:00
parent e69272c359
commit 7e41e328e8
3 changed files with 67 additions and 35 deletions

View File

@ -394,37 +394,23 @@ function npc.actions.use_sittable(self, pos, action)
if action == npc.actions.const.sittable.SIT then
-- Calculate position depending on bench
-- For cottages bench (code taken from Sokomine's cottages mod):
local p2 = {x=pos.x, y=pos.y, z=pos.z};
if not( node ) or node.param2 == 0 then
p2.z = p2.z+0.3;
elseif node.param2 == 1 then
p2.x = p2.x+0.3;
elseif node.param2 == 2 then
p2.z = p2.z-0.3;
elseif node.param2 == 3 then
p2.x = p2.x-0.3;
end
-- For stairs (based on the above code):
local p2 = {x=pos.x, y=pos.y, z=pos.z};
if not( node ) or node.param2 == 0 then
p2.z = p2.z-0.2;
elseif node.param2 == 1 then
p2.x = p2.x-0.2;
elseif node.param2 == 2 then
p2.z = p2.z+0.2;
elseif node.param2 == 3 then
p2.x = p2.x+0.2;
end
minetest.log("Got sit position: "..dump(sit_pos))
local sit_pos = npc.actions.nodes.sittable[node.name].get_sit_pos(pos, node.param2)
-- Sit down on bench/chair/stairs
npc.add_action(self, npc.actions.sit, {self=self, pos=p2})
-- Rotate to the correct position
npc.add_action(self, npc.actions.rotate, {self=self, dir=node.param2 + 2 % 4})
npc.add_action(self, npc.actions.sit, {self=self, pos=sit_pos, dir=(node.param2 + 2) % 4})
else
-- Walk up from bed
npc.add_action(self, npc.actions.walk_step, {self=self, dir=param2.param2 + 2 % 4})
-- Find empty areas around chair
local dir = node.param2 + 2 % 4
local empty_nodes = npc.places.find_node_orthogonally(pos, {"air"}, 0)
if empty_nodes ~= nil then
-- Get direction to the empty node
dir = npc.actions.get_direction(pos, empty_nodes[1].pos)
end
-- Calculate position to get out of bed
local pos_out_of_sittable =
{x=empty_nodes[1].pos.x, y=empty_nodes[1].pos.y + 1, z=empty_nodes[1].pos.z}
-- Stand
npc.add_action(self, npc.actions.stand, {self=self})
npc.add_action(self, npc.actions.stand, {self=self, pos=pos_out_of_sittable, dir=dir})
end
end

View File

@ -16,8 +16,9 @@ npc.actions.nodes = {
---------------------------------------------------------------------------------------
-- Beds functionality supported by default
---------------------------------------------------------------------------------------
-- Functionality for default beds. Since other mods may be used in the
-- same way as the default beds, this one is a global registration
-- Functionality for default beds.
-- Since other mods may be used in the same way as the default beds,
-- this one is a global registration
npc.actions.nodes.default_bed_registration = {
get_lay_pos = function(pos, dir)
return {x = pos.x + dir.x / 2, y = pos.y + 1, z = pos.z + dir.z / 2}
@ -41,7 +42,44 @@ local cottages_mat_registration = {
}
---------------------------------------------------------------------------------------
-- Beds
-- Sitting functionality supported by default
---------------------------------------------------------------------------------------
-- Functionality for allowing the NPC to sit on default stairs and cottages' bench
local sittable_stair_registration = {
get_sit_pos = function(pos, param2)
local result = {x=pos.x, y=pos.y+1, z=pos.z};
if param2 == 0 then
result.z = result.z-0.2;
elseif param2 == 1 then
result.x = result.x-0.2;
elseif param2 == 2 then
result.z = result.z+0.2;
elseif param2 == 3 then
result.x = result.x+0.2;
end
return result
end
}
local cottages_bench_registration = {
get_sit_pos = function(pos, param2)
local result = {x=pos.x, y=pos.y+1, z=pos.z};
if param2 == 0 then
result.z = result.z+0.3;
elseif param2 == 1 then
result.x = result.x+0.3;
elseif param2 == 2 then
result.z = result.z-0.3;
elseif param2 == 3 then
result.x = result.x-0.3;
end
return result
end
}
---------------------------------------------------------------------------------------
-- Registry of bed nodes
---------------------------------------------------------------------------------------
-- Default beds.
npc.actions.nodes.beds["beds:bed_bottom"] = npc.actions.nodes.default_bed_registration
@ -51,3 +89,11 @@ npc.actions.nodes.beds["beds:fancy_bed_bottom"] = npc.actions.nodes.default_bed_
npc.actions.nodes.beds["cottages:bed_foot"] = cottages_bed_registration
npc.actions.nodes.beds["cottages:sleeping_mat"] = cottages_mat_registration
npc.actions.nodes.beds["cottages:straw_mat"] = cottages_mat_registration
---------------------------------------------------------------------------------------
-- Registry of sittable nodes
---------------------------------------------------------------------------------------
-- Normal wooden stairs
npc.actions.nodes.sittable["stairs:stair_wood"] = sittable_stair_registration
-- Cottages bench
npc.actions.nodes.sittable["cottages:bench"] = cottages_bench_registration

View File

@ -350,7 +350,7 @@ local function npc_spawn(self, pos)
ent.places_map = {}
-- Temporary initialization of actions for testing
local nodes = npc.places.find_node_nearby(ent, {"beds:fancy_bed_bottom"}, 30)
local nodes = npc.places.find_node_nearby(ent, {"cottages:bench"}, 30)
minetest.log("Found nodes: "..dump(nodes))
--local path = pathfinder.find_path(ent.object:getpos(), nodes[1], 20)
@ -359,12 +359,12 @@ local function npc_spawn(self, pos)
--npc.add_action(ent, npc.actions.stand, {self = ent})
--npc.add_action(ent, npc.actions.stand, {self = ent})
npc.actions.walk_to_pos(ent, nodes[1], {})
npc.actions.use_bed(ent, nodes[1], npc.actions.const.beds.LAY)
npc.add_action(ent, npc.actions.lay, {self = ent})
npc.actions.use_sittable(ent, nodes[1], npc.actions.const.sittable.SIT)
npc.add_action(ent, npc.actions.sit, {self = ent})
-- npc.add_action(ent, npc.actions.lay, {self = ent})
-- npc.add_action(ent, npc.actions.lay, {self = ent})
-- npc.add_action(ent, npc.actions.lay, {self = ent})
npc.actions.use_bed(ent, nodes[1], npc.actions.const.beds.GET_UP)
npc.actions.use_sittable(ent, nodes[1], npc.actions.const.sittable.GET_UP)
-- npc.add_action(ent, npc.action.stand, {self = ent})