-
Notifications
You must be signed in to change notification settings - Fork 5
Dev_Actions_Schedules
The interesting part of Advanced NPC is its ability to simulate realistic behavior in NPCs. Realistic behavior is defined simply as being able to perform tasks at a certain time of the day, like usually people do. This allow the NPC to go to bed, sleep, get up from it, sit in benches, etc. But how all of this is simulated? The answer is: tasks and scheduling.
The implementation resembles a rough OS process scheduling algorithm where only one process is allowed at a time. The processes or tasks are held in a queue, where they are executed one at a time in queue fashion. Interruptions are allowed, and the interrupted action is re-started once the interruption is finished.
Actions are "atomic" executable actions the NPC can perform. Tasks are sequences of actions that are common enough to be supported by default. Advanced NPC supports 16 actions and tasks by default which can be used together to simulate tasks that players can do. Each action or task is wrapped on a Lua table which tells the action/task to be executed and the arguments to be used. However, this is encapsulated to the user in the following two functions:
npc.add_action(action, args)
npc.add_task(task, args)
For both of the above, action/task is a constant defined in npc.actions.cmd
, and args
is a Lua table specific to each action/task. The following is a list of the 16 actions/tasks supported by default:
-
SET_INTERVAL
: This action sets the interval at which the action/tasks are executed. The default is 1 second.- Arguments:
-
interval
: A decimal number, in seconds -
freeze
: Boolean, iftrue
, mobs_redo API will not execute until interval is set
-
- Arguments:
-
FREEZE
: This action allows to stop/execute mobs_redo API. This is good for stopping the NPC from fighting, wandering, etc.- Arguments:
-
freeze
: Boolean, iftrue
, mobs_redo API will not execute.
-
- Arguments:
-
DIG
: Digs a node as a player would do. The NPC will use mining animation and the node's dug sound will be played if enabled. It is also possible to respect or bypass protection.- Arguments:
-
pos
: Position where to dig -
add_to_inventory
: Boolean, iftrue
, adds the first defined drop to the NPC's inventory. Note: This doesn't uses Minetest's drop system. It will take only the first item defined in the drops table. -
bypass_protection
: Boolean, iftrue
, protection will not be respected and NPC will dig node anyways. -
play_sound
: Boolean, iftrue
, node's dug sound will be played. Default istrue
.
-
- Arguments:
-
PLACE
: Places a node as a player would do.- Arguments:
-
pos
: The position where the node will be placed -
node
: A string containing the name of the node to be placed -
source
: Specifies where the node will be sourced. There are three options:-
take_from_inventory
: The node will be taken from NPC's inventory. If not present, no node will be placed. -
take_from_inventory_forced
: The node will be taken from the inventory if present, and if not present, it will place it anyways. -
force_place
: The node will be placed and not taken from the NPC's inventory.
-
-
bypass_protection
: Boolean, if set totrue
, the node will be placed regardless of protection. -
play_sound
: Boolean, if set totrue
, node's place sound will be played
-
- Arguments:
-
ROTATE
: Rotates a NPC to one of 8 possible directions.- Arguments:
-
dir
: One of the 8 directions in the enumnpc.direction
. The options are:- North, northeast, east, southeast, south, southwest, west, northwest
-
start_pos
: If given, thedir
argument is ignored. It will calculate the direction fromstart_pos
toend_pos
. Requiresend_pos
. -
end_pos
: If given, thedir
argument is ignored. It will calculate the direction fromstart_pos
toend_pos
. Requiresstart_pos
.
-
- Arguments:
-
WALK_STEP
: Walks one step looking into specified direction.- Arguments:
-
dir
: Can be either:- One of the 8 directions to look at and walk to. Can be found in enum
npc.direction
, or; -
"random"
,"random_orthogonal"
,"random_all"
, which will give a random direction as specified by the parameter. Requiresstart_pos
- One of the 8 directions to look at and walk to. Can be found in enum
-
speed
: Double number specifying the speed that the NPC will be moved. Three pre-defined speeds exists atnpc.actions.one_nps_speed
,npc.actions.one_half_nps_speed
,npc.actions.two_nps_speed
. -
target_pos
: Position that specifies where the NPC should be at the end of moving. -
start_pos
: Position where the NPC is before executing movement. Required to calculate random direction. Requiresdir
.
-
- Arguments:
-
STAND
: NPC's animation is changed to stand and velocity is set to 0. This is usually required to stop the NPC after it was walking.- Arguments:
-
pos
: If given, NPC will stand at the given position. -
dir
: If given, NPC will stand and look at dir.
-
- Arguments:
-
SIT
: NPC's animation is changed to sit and velocity is set to 0.- Arguments:
-
pos
: If given, NPC will sit at the given position. -
dir
: If given, NPC will sit and look at dir.
-
- Arguments:
-
LAY
: The NPC's animation is changed to lay and velocity is set to 0.- Arguments:
-
pos
: If given, NPC will lay at the given position. -
dir
: If given, NPC will lay and look at dir.
-
- Arguments:
-
PUT_ITEM
: -
TAKE_ITEM
: -
CHECK_ITEM
: -
USE_OPENABLE
: Task. NPC will open/close any supported openable node like a door, fence gate, etc. -
USE_FURNACE
: USE_BED
USE_SITTABLE
-
WALK_TO_POS
: Task. NPC will walk to the given position. This task uses the pathfinder to calculate the nodes in the path that the NPC will walk through, then enqueueswalk_step
actions, combined with correct directional rotations and opening/closing of doors on the path.