Actions: Added some fundamental movement actions.
Places: Added functions for adding and getting places, and for finding places of specific types. Added beds, chairs and chest as places. NPC: Added places map and action queue.
This commit is contained in:
85
actions/actions.lua
Normal file
85
actions/actions.lua
Normal file
@ -0,0 +1,85 @@
|
||||
-- Actions code for Advanced NPC by Zorman2000
|
||||
---------------------------------------------------------------------------------------
|
||||
-- Action functionality
|
||||
---------------------------------------------------------------------------------------
|
||||
-- The NPCs will be able to perform five fundamental actions that will allow
|
||||
-- for them to perform any other kind of interaction in the world. These
|
||||
-- fundamental actions are: place a node, dig a node, put items on an inventory,
|
||||
-- take items from an inventory, find a node closeby (radius 3) and
|
||||
-- walk a step on specific direction. These actions will be set on an action queue.
|
||||
-- The queue will have the specific steps, in order, for the NPC to be able to do
|
||||
-- something (example, go to a specific place and put a chest there). The
|
||||
-- fundamental actions are added to the action queue to make a complete task for the NPC.
|
||||
|
||||
npc.actions = {}
|
||||
|
||||
function npc.actions.rotate(args)
|
||||
local self = args.self
|
||||
local dir = args.dir
|
||||
local yaw = 0
|
||||
self.rotate = 0
|
||||
if dir == npc.direction.north then
|
||||
yaw = 315
|
||||
elseif dir == npc.direction.east then
|
||||
yaw = 225
|
||||
elseif dir == npc.direction.south then
|
||||
yaw = 135
|
||||
elseif dir == npc.direction.west then
|
||||
yaw = 45
|
||||
end
|
||||
self.object:setyaw(yaw)
|
||||
end
|
||||
|
||||
-- This function will make the NPC walk one step on a
|
||||
-- specifc direction. One step means one node. It returns
|
||||
-- true if it can move on that direction, and false if there is an obstacle
|
||||
function npc.actions.walk_step(args)
|
||||
local self = args.self
|
||||
local dir = args.dir
|
||||
local vel = {}
|
||||
if dir == npc.direction.north then
|
||||
vel = {x=0, y=0, z=1}
|
||||
elseif dir == npc.direction.east then
|
||||
vel = {x=1, y=0, z=0}
|
||||
elseif dir == npc.direction.south then
|
||||
vel = {x=0, y=0, z=-1}
|
||||
elseif dir == npc.direction.west then
|
||||
vel = {x=-1, y=0, z=0}
|
||||
end
|
||||
set_animation(self, "walk")
|
||||
npc.rotate({self=self, dir=dir})
|
||||
self.object:setvelocity(vel)
|
||||
end
|
||||
|
||||
-- This action makes the NPC stand and remain like that
|
||||
function npc.actions.stand(args)
|
||||
local self = args.self
|
||||
-- Stop NPC
|
||||
self.object:setvelocity({x=0, y=0, z=0})
|
||||
-- Set stand animation
|
||||
set_animation(self, "stand")
|
||||
end
|
||||
|
||||
-- This action makes the NPC sit on the node where it is
|
||||
function npc.actions.sit(args)
|
||||
local self = args.self
|
||||
-- Stop NPC
|
||||
self.object:setvelocity({x=0, y=0, z=0})
|
||||
-- Set sit animation
|
||||
self.object:set_animation({
|
||||
x = npc.ANIMATION_SIT_START,
|
||||
y = npc.ANIMATION_SIT_END},
|
||||
self.animation.speed_normal, 0)
|
||||
end
|
||||
|
||||
-- This action makes the NPC lay on the node where it is
|
||||
function npc.actions.lay(args)
|
||||
local self = args.self
|
||||
-- Stop NPC
|
||||
self.object:setvelocity({x=0, y=0, z=0})
|
||||
-- Set sit animation
|
||||
self.object:set_animation({
|
||||
x = npc.ANIMATION_LAY_START,
|
||||
y = npc.ANIMATION_LAY_END},
|
||||
self.animation.speed_normal, 0)
|
||||
end
|
82
actions/places.lua
Normal file
82
actions/places.lua
Normal file
@ -0,0 +1,82 @@
|
||||
-- Places code for Advanced NPC by Zorman2000
|
||||
---------------------------------------------------------------------------------------
|
||||
-- Places functionality
|
||||
---------------------------------------------------------------------------------------
|
||||
-- In addition, the NPCs need to know where some places are, and know
|
||||
-- where there are nodes they can use. For example, they need to know where the
|
||||
-- chest they use is located, both to walk to it and to use it. They also need
|
||||
-- to know where the farm they work is located, or where the bed they sleep is.
|
||||
-- Other mods have to be supported for this to work correctly, as there are
|
||||
-- many sitting nodes, many beds, many tables, chests, etc. For now, by default,
|
||||
-- support for default and cottages is going to be provided.
|
||||
|
||||
npc.places = {}
|
||||
|
||||
npc.places.nodes = {
|
||||
BEDS = {
|
||||
"beds:bed_bottom",
|
||||
"beds:fancy_bed_bottom"
|
||||
},
|
||||
CHAIRS = {
|
||||
"cottages:bench"
|
||||
},
|
||||
CHESTS = {
|
||||
"default:chest",
|
||||
"default:chest_locked"
|
||||
}
|
||||
}
|
||||
|
||||
npc.places.PLACE_TYPE = {
|
||||
"OWN_BED",
|
||||
"OWN_CHEST",
|
||||
"HOUSE_CHAIR",
|
||||
"HOUSE_TABLE",
|
||||
"HOUSE_FURNACE",
|
||||
"HOUSE_ENTRANCE"
|
||||
}
|
||||
|
||||
|
||||
function npc.places.add(self, place_name, place_type, pos)
|
||||
self.places_map[place_name] = {type=place_type, pos=pos}
|
||||
end
|
||||
|
||||
-- Adds a specific node to the NPC places, and modifies the
|
||||
-- node metadata to identify the NPC as the owner. This allows
|
||||
-- other NPCs to avoid to take this as their own.
|
||||
function npc.places.add_owned(self, place_name, place_type, pos)
|
||||
-- Get node metadata
|
||||
local meta = minetest.get_meta(pos)
|
||||
-- Check if it is owned by an NPC?
|
||||
if meta:get_string("npc_owner") == "" then
|
||||
-- Set owned by NPC
|
||||
meta:set_string("npc_owner", self.npc_id)
|
||||
-- Add place to list
|
||||
npc.places.add(self, place_name, place_type, pos)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function npc.places.get_by_type(self, place_type)
|
||||
local result = {}
|
||||
for place_name, place_entry in pairs(self.places_map) do
|
||||
if place_entry.type == place_type then
|
||||
table.insert(result, place_name)
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
-- This function searches on a squared are of the given radius
|
||||
-- for nodes of the given type. The type should be npc.places.nodes
|
||||
function npc.places.find_new_nearby(self, type, radius)
|
||||
-- Get current pos
|
||||
local current_pos = self.object:getpos()
|
||||
-- Determine area points
|
||||
local start_pos = {x=current_pos.x - radius, y=current_pos.y - 1, z=current_pos.z - radius}
|
||||
local end_pos = {x=current_pos.x + radius, y=current_pos.y + 1, z=current_pos.z + radius}
|
||||
-- Get nodes
|
||||
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
|
||||
|
||||
return nodes
|
||||
end
|
Reference in New Issue
Block a user