From e69272c3591a50ace3bf68d23654d5ccef13d835 Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Tue, 10 Jan 2017 14:21:45 -0500 Subject: [PATCH] Actions: Default doors and cottages' mod doors are fully supported now. Default bed and cottages' mod beds fully supported now. --- actions/actions.lua | 12 ++++++--- actions/node_registry.lua | 53 ++++++++++++++++++++++++++++++++++++++ actions/nodes_registry.lua | 25 ------------------ npc.lua | 4 +-- 4 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 actions/node_registry.lua delete mode 100644 actions/nodes_registry.lua diff --git a/actions/actions.lua b/actions/actions.lua index c068e01..4dbb9ef 100644 --- a/actions/actions.lua +++ b/actions/actions.lua @@ -342,20 +342,26 @@ function npc.actions.use_bed(self, pos, action) if action == npc.actions.const.beds.LAY then -- Get position - local bed_pos = npc.actions.nodes.beds[node.name].get_lay_pos(pos) + local bed_pos = npc.actions.nodes.beds[node.name].get_lay_pos(pos, dir) -- Sit down on bed, rotate to correct direction npc.add_action(self, npc.actions.sit, {self=self, pos=bed_pos, dir=(node.param2 + 2) % 4}) -- Lay down npc.add_action(self, npc.actions.lay, {self=self}) else -- Calculate position to get up - local bed_pos = {x = pos.x, y = pos.y + 1, z = pos.z} + local bed_pos_y = npc.actions.nodes.beds[node.name].get_lay_pos(pos, dir).y + local bed_pos = {x = pos.x, y = bed_pos_y, z = pos.z} -- Sit up npc.add_action(self, npc.actions.sit, {self=self, pos=bed_pos}) -- Initialize direction: Default is front of bottom of bed local dir = (node.param2 + 2) % 4 -- Find empty node around node - local empty_nodes = npc.places.find_node_orthogonally(bed_pos, {"air", "cottages:bench"}, -1) + -- Take into account that mats are close to the floor, so y adjustmen is zero + local y_adjustment = -1 + if npc.actions.nodes.beds[node.name].type == "mat" then + y_adjustment = 0 + end + local empty_nodes = npc.places.find_node_orthogonally(bed_pos, {"air", "cottages:bench"}, y_adjustment) if empty_nodes ~= nil then -- Get direction to the empty node dir = npc.actions.get_direction(bed_pos, empty_nodes[1].pos) diff --git a/actions/node_registry.lua b/actions/node_registry.lua new file mode 100644 index 0000000..9d41428 --- /dev/null +++ b/actions/node_registry.lua @@ -0,0 +1,53 @@ +-- Node functionality registry for NPC actions by Zorman2000 +--------------------------------------------------------------------------------------- +-- In this script, some functionality and information required for nodes +-- to be used correctly by an NPC is described. +-- To avoid as many definitions as possible, the names of the nodes +-- can actually be prefixes. + +-- This table will contain the registered nodes +npc.actions.nodes = { + doors = {}, + beds = {}, + sittable = {} +} + + +--------------------------------------------------------------------------------------- +-- 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 +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} + end, + type = "bed" +} + +-- The code used in get_lay_pos is from cottages mod and slightly modified. +local cottages_bed_registration = { + get_lay_pos = function(pos, dir) + return {x = pos.x + dir.x / 2, y = pos.y + 1.4, z = pos.z + dir.z / 2} + end, + type = "bed" +} + +local cottages_mat_registration = { + get_lay_pos = function(pos, dir) + return {x = pos.x + dir.x / 2, y = pos.y + 1, z = pos.z + dir.z / 2} + end, + type = "mat" +} + +--------------------------------------------------------------------------------------- +-- Beds +--------------------------------------------------------------------------------------- +-- Default beds. +npc.actions.nodes.beds["beds:bed_bottom"] = npc.actions.nodes.default_bed_registration +npc.actions.nodes.beds["beds:fancy_bed_bottom"] = npc.actions.nodes.default_bed_registration + +-- Cottages beds +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 diff --git a/actions/nodes_registry.lua b/actions/nodes_registry.lua deleted file mode 100644 index 705cfc0..0000000 --- a/actions/nodes_registry.lua +++ /dev/null @@ -1,25 +0,0 @@ --- Node functionality for actions by Zorman2000 --- In this script, some functionality and information required for nodes --- to be used correctly by an NPC is described. - --- Attempt to keep a register of how to use some nodes -npc.actions.nodes = { - doors = {}, - beds = {}, - sittable = {} -} - --- Register default beds. Always register bottom node only -npc.actions.nodes.beds["beds:bed_bottom"] = { - get_lay_pos = function(pos) - return {x = pos.x + dir.x / 2, y = pos.y + 1, z = pos.z + dir.z / 2} - end -} - -npc.actions.nodes.beds["beds:fancy_bed_bottom"] = { - get_lay_pos = function(pos) - return {x = pos.x + dir.x / 2, y = pos.y + 1, z = pos.z + dir.z / 2} - end -} - -npc.actions.nodes.beds[""] \ No newline at end of file diff --git a/npc.lua b/npc.lua index 96eee49..7da8c9e 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:bed_bottom"}, 30) + local nodes = npc.places.find_node_nearby(ent, {"beds:fancy_bed_bottom"}, 30) minetest.log("Found nodes: "..dump(nodes)) --local path = pathfinder.find_path(ent.object:getpos(), nodes[1], 20) @@ -358,7 +358,7 @@ local function npc_spawn(self, pos) --npc.add_action(ent, npc.actions.use_door, {self = ent, pos = nodes[1], action = npc.actions.door_action.OPEN}) --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], {"cottages:bench"}) + 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.add_action(ent, npc.actions.lay, {self = ent})