From 7e41e328e8efa44f3778e77482835b2fffd91c21 Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Wed, 11 Jan 2017 07:54:50 -0500 Subject: [PATCH] Actions: Support for sitting on benches and stairs. --- actions/actions.lua | 42 +++++++++++-------------------- actions/node_registry.lua | 52 ++++++++++++++++++++++++++++++++++++--- npc.lua | 8 +++--- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/actions/actions.lua b/actions/actions.lua index 4dbb9ef..110d079 100644 --- a/actions/actions.lua +++ b/actions/actions.lua @@ -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 diff --git a/actions/node_registry.lua b/actions/node_registry.lua index 9d41428..f540aec 100644 --- a/actions/node_registry.lua +++ b/actions/node_registry.lua @@ -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 \ No newline at end of file diff --git a/npc.lua b/npc.lua index 7da8c9e..8a839bc 100755 --- a/npc.lua +++ b/npc.lua @@ -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})