Fixed bugs related to door positioning, opening/closing, and finding

positions in general.
This commit is contained in:
Hector Franqui 2017-09-23 10:43:51 -04:00
parent 43c69ffee4
commit d92b729e19
3 changed files with 69 additions and 43 deletions

View File

@ -603,11 +603,18 @@ end
-- TODO: Refactor this function so that it uses a table to check
-- for doors instead of having separate logic for each door type
function npc.actions.get_openable_node_state(node, npc_dir)
function npc.actions.get_openable_node_state(node, pos, npc_dir)
--minetest.log("Node name: "..dump(node.name))
local state = npc.actions.const.doors.state.CLOSED
-- Check for default doors and gates
local a_i1, a_i2 = string.find(node.name, "_a")
-- Check for MTG doors and gates
local mtg_door_closed = false
if minetest.get_item_group(node.name, "door") > 0 then
local back_pos = vector.add(pos, minetest.facedir_to_dir(node.param2))
local back_node = minetest.get_node(back_pos)
if back_node.name == "air" or minetest.registered_nodes[back_node.name].walkable == false then
mtg_door_closed = true
end
end
-- Check for cottages gates
local open_i1, open_i2 = string.find(node.name, "_close")
-- Check for cottages half door
@ -615,7 +622,7 @@ function npc.actions.get_openable_node_state(node, npc_dir)
if node.name == "cottages:half_door" then
half_door_is_closed = (node.param2 + 2) % 4 == npc_dir
end
if a_i1 == nil and open_i1 == nil and not half_door_is_closed then
if mtg_door_closed == false and open_i1 == nil and half_door_is_closed == false then
state = npc.actions.const.doors.state.OPEN
end
--minetest.log("Door state: "..dump(state))
@ -630,7 +637,7 @@ function npc.actions.use_openable(self, args)
local action = args.action
local dir = args.dir
local node = minetest.get_node(pos)
local state = npc.actions.get_openable_node_state(node, dir)
local state = npc.actions.get_openable_node_state(node, pos, dir)
local clicker = self.object
if action ~= state then
@ -1048,7 +1055,8 @@ function npc.actions.walk_to_pos(self, args)
-- Add stand animation at end
if use_access_node == true then
dir = npc.actions.get_direction(end_pos, node_pos)
end
end
minetest.log("Dir: "..dump(dir))
-- Change dir if using access_node
npc.add_action(self, npc.actions.cmd.STAND, {dir = dir})
break
@ -1059,7 +1067,7 @@ function npc.actions.walk_to_pos(self, args)
if path[i+1].type == npc.pathfinder.node_types.openable then
-- Check if door is already open
local node = minetest.get_node(path[i+1].pos)
if npc.actions.get_openable_node_state(node, dir) == npc.actions.const.doors.state.CLOSED then
if npc.actions.get_openable_node_state(node, path[i+1].pos, dir) == npc.actions.const.doors.state.CLOSED then
--minetest.log("Opening action to open door")
-- Stop to open door, this avoids misplaced movements later on
npc.add_action(self, npc.actions.cmd.STAND, {dir=dir})

View File

@ -109,7 +109,7 @@ function npc.places.add_owned_accessible_place(self, nodes, place_type, walkable
if nodes[i].owner == "" then
-- If node has no owner, check if it is accessible
local empty_nodes = npc.places.find_orthogonal_accessible_node(
nodes[i].node_pos, nil, 0, true, walkables)
nodes[i].node_pos, nil, 0, true, walkables)
-- Check if node is accessible
if #empty_nodes > 0 then
-- Set owner to this NPC
@ -237,10 +237,10 @@ function npc.places.find_orthogonal_accessible_node(pos, nodes, y_adjustment, in
else
-- Search for air, walkable nodes, or any node availble in the extra_walkables array
if node.name == "air"
or (include_walkables == true
and minetest.registered_nodes[node.name].walkable
or (include_walkables == true
and minetest.registered_nodes[node.name].walkable == false
and minetest.registered_nodes[node.name].groups.fence ~= 1)
or (extra_walkables and npc.utils.array_contains(extra_walkables, node.name)) then
or (extra_walkables and npc.utils.array_contains(extra_walkables, node.name)) then
table.insert(result, {name=node.name, pos=point, param2=node.param2})
end
end
@ -635,39 +635,56 @@ end
-- Specialized function to find the node position right behind
-- a door. Used to make NPCs enter buildings.
function npc.places.find_node_behind_door(door_pos)
function npc.places.find_node_in_front_and_behind_door(door_pos)
local door = minetest.get_node(door_pos)
if door.param2 == 0 then
-- Looking south
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
elseif door.param2 == 1 then
-- Looking east
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
elseif door.param2 == 2 then
-- Looking north
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
-- Looking west
elseif door.param2 == 3 then
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
local facedir_vector = minetest.facedir_to_dir(door.param2)
local back_pos = vector.add(door_pos, facedir_vector)
local back_node = minetest.get_node(back_pos)
local front_pos
if back_node.name == "air" or minetest.registered_nodes[back_node.name].walkable == false then
-- Door is closed, so back_pos is the actual behind position.
-- Calculate front node
front_pos = vector.add(door_pos, vector.multiply(facedir_vector, -1))
else
-- Door is open, need to find the front and back pos
facedir_vector = minetest.facedir_to_dir((door.param2 + 2) % 4)
back_pos = vector.add(door_pos, facedir_vector)
front_pos = vector.add(door_pos, vector.multiply(facedir_vector, -1))
end
return back_pos, front_pos
-- if door.param2 == 0 then
-- -- Looking south
-- return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
-- elseif door.param2 == 1 then
-- -- Looking east
-- return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
-- elseif door.param2 == 2 then
-- -- Looking north
-- return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
-- -- Looking west
-- elseif door.param2 == 3 then
-- return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
-- end
end
-- Specialized function to find the node position right in
-- front of a door. Used to make NPCs exit buildings.
function npc.places.find_node_in_front_of_door(door_pos)
local door = minetest.get_node(door_pos)
--minetest.log("Param2 of door: "..dump(door.param2))
if door.param2 == 0 then
-- Looking south
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
elseif door.param2 == 1 then
-- Looking east
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
elseif door.param2 == 2 then
-- Looking north
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
elseif door.param2 == 3 then
-- Looking west
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
end
end
--function npc.places.find_node_in_front_of_door(door_pos)
-- local door = minetest.get_node(door_pos)
-- --minetest.log("Param2 of door: "..dump(door.param2))
-- if door.param2 == 0 then
-- -- Looking south
-- return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
-- elseif door.param2 == 1 then
-- -- Looking east
-- return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
-- elseif door.param2 == 2 then
-- -- Looking north
-- return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
-- elseif door.param2 == 3 then
-- -- Looking west
-- return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
-- end
--end

View File

@ -499,8 +499,9 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
if entrance ~= nil and entrance.node_pos ~= nil then
npc.places.add_shared(self, npc.places.PLACE_TYPE.OPENABLE.HOME_ENTRANCE_DOOR, npc.places.PLACE_TYPE.OPENABLE.HOME_ENTRANCE_DOOR, entrance.node_pos)
-- Find the position inside and outside the door
local entrance_inside = npc.places.find_node_behind_door(entrance.node_pos)
local entrance_outside = npc.places.find_node_in_front_of_door(entrance.node_pos)
local entrance_inside, entrance_outside = npc.places.find_node_in_front_and_behind_door(entrance.node_pos)
--local entrance_inside = npc.places.find_node_behind_door(entrance.node_pos)
--local entrance_outside = npc.places.find_node_in_front_of_door(entrance.node_pos)
-- Assign these places to NPC
npc.places.add_shared(self, npc.places.PLACE_TYPE.OTHER.HOME_INSIDE, npc.places.PLACE_TYPE.OTHER.HOME_INSIDE, entrance_inside)
npc.places.add_shared(self, npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE, npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE, entrance_outside)