Anpcscript is the programming language for anpc
. anpc
takes the idea that NPCs are independent beings, able to move and interact with their environment (the Minetest world) in different ways, and over time (and sometimes, only over time!). In order for NPCs to execute tasks, anpc
implements each NPC in a simplified OS-programming model. Therefore, each NPC has:
- Data storage
- Global
- For each process
- Temporary
- A process queue
- Timers
Therefore, the main concept for creating NPCs with anpc
are programs, which are created using an instruction set.
Programs are made of three basic concepts:
- Instructions
- Program control
- Variables
The state process is a program that is continually executed. When its execution is over, the execution starts again.
The state process information is stored under self.process.state
. You can retrieve the name and arguments of the current state process by using self.process.state.name
and self.process.state.args
respectively.
Instructions are the basic building block of programs. They are made of Lua code. Their name is usually of the syntax :, but after "namespace" there could be more names which actually represent grouping. These are merely conventions and are not really needed - the name is just the name and is not used by anpc
in any way. Instructions can receive an arbitrary number of parameters. Some instructions are built-in (and these are named always with "npc" as the namespace) while others can be registered locally.
This list is exhaustive of all built-in instructions supported.
npc:execute
- Executes another program
- Arguments:
name
: string. Name of the program to be executed. Must be the name of any program that is registered withnpc.proc.register_program
args
: table. Arguments for the program
npc:wait
- Stop process execution for the desired time. This is not a busy wait, it instead changes the process interval in the right way.
- Arguments:
time
: number. The time to wait, in seconds. Decimal values are supported, but it should be taken into account that it should never be smaller thandtime
(the server's step time). This is not validated.
npc:random
- Returns a random number in the given range. Same as Lua's
math.random
- Arguments:
start
: number. The beginning of the rangeend
: number. The end of the range
- Returns a random number in the given range. Same as Lua's
npc:distance_to
- Returns the distance from the NPC to a given node or object
- Arguments:
pos
: table, with x, y and z. If given, will return the distance to the given position. Shouldn't be used together withobject
object
: userdata. If given, will return distance from NPC to given object at that time. Shouldn't be used together withpos
round
: boolean. If given, will round distance value usingvector.round()