FSM: Difference between revisions

From Unofficial EFPSE wiki Extended
Jump to navigation Jump to search
(Added the note about the way random number generation actually works.)
No edit summary
Line 180: Line 180:
* '''PARTICLES imageindex i,l,x,y,z dx,dy,dz''' - creates particles. imageindex - number of sprite loaded, i - number of particles, l - particle lifetime, x,y,z - coordinates. Coordinates are local to enemy or player, meaning that 0,0,1 will always be in front of enemy or player. x - side offset, y - vertical offset, z - forward offset. dx,dy,dz - particle velocity. Also local to entity.
* '''PARTICLES imageindex i,l,x,y,z dx,dy,dz''' - creates particles. imageindex - number of sprite loaded, i - number of particles, l - particle lifetime, x,y,z - coordinates. Coordinates are local to enemy or player, meaning that 0,0,1 will always be in front of enemy or player. x - side offset, y - vertical offset, z - forward offset. dx,dy,dz - particle velocity. Also local to entity.
* '''EXPLOSION name radius''' - creates an explosion with a name and radius where name is name of explosion sprites. Sprites must be place in Data/Sprites/Effects (enemies only).
* '''EXPLOSION name radius''' - creates an explosion with a name and radius where name is name of explosion sprites. Sprites must be place in Data/Sprites/Effects (enemies only).
* '''SPAWN name x,y,z f,v''' - creates an entity with a name (enemies and decoration) or a keyword (Key1, Key2, Key3, Hp1, Hp2, Hp3, Hp4). x,y,z - just like with particles, f - front velocity when spawned, v - vertical velocity when spawning. (enemies and decorations only).
* '''SPAWN name x,y,z f,v''' - creates an entity with a name (enemies and decoration) or a keyword (Key1, Key2, Key3, Hp1, Hp2, Hp3, Hp4, Ammo1, Ammo2, Ammo3... etc.). x,y,z - just like with particles, f - front velocity when spawned, v - vertical velocity when spawning. (enemies and decorations only).
* '''PLAYERSPEED''' - controls player's movement. You can only entirely freeze the movement (if set to 0) and enable it back (if set to 1), or double it (if set to 2).
* '''PLAYERSPEED''' - controls player's movement. You can only entirely freeze the movement (if set to 0) and enable it back (if set to 1), or double it (if set to 2).



Revision as of 20:52, 7 September 2023

FSM or Finite State Machine allows you to extend sprites animation and entities behavior on general. It is a set of lines that describe what sprites enemy or decorations will have and what will they do. In short, you can change how many sprites an entity has and how fast frames will switch one another.

To add a state machine, you need to create a file with the .states extension and the name of the weapon, enemy, or decoration in the ProjectName/States folder. When adding a state machine, the attack and reload speed will no longer depend on the setting in the editor - everything will be controlled only by the duration of the frames specified in the file. In addition, all sprites and (for weapons) some of the sounds will need to be loaded manually using the image and sound commands.

Best practices

Never have a space or tab Infront of any string. (White space is also counted when the engine reads the FSM/Script or script and unexpected spaces will break the FSM/Script)

Basic FSM for a weapon:

image Weapon 0 7

sound WeaponFire

sound WeaponReload

state IDLE NONE 0

frame 1 0.25 0 0 0 NONE

frame 1 0.25 0 0 0 READY

state ATTACK IDLE 0

frame 2 1 0 0 0 NONE

frame 3 1 0 0 0 SOUNDANDATTACK 0

frame 4 1 0 0 0 MUZZLEFLASH

frame 5 1 0 0 0 READY

frame 6 1 0 0 0 NONE

frame 7 1 0 0 0 NONE

frame 1 0.25 0 0 0 NONE

frame 1 0.25 0 0 0 NONE

state RELOAD IDLE 0

frame 1 0 0 0 0 NONE

frame 1 0.1 0 0 0 NONE

frame 1 0.1 0 200 0 NONE

frame 1 0.1 0 200 0 RELOAD

frame 1 0.1 0 0 0 SOUND 1

Basic FSM for alt attack:

state ALTATTACK IDLE 0

frame 1 0 0 0 NONE

frame 2 0 0 0 SOUNDATTACK 0     (plays the attack sound)

frame 3 0 0 0 MUZZLEFLASH         (Sets the muzzel flash)

frameset 4 28 0 0 0 NONE               (This plays frames 4 through to 28 so that you don't have to write out each line)

frame 29 0 0 0 ATTACK          (sets the attack)

frame 30 0 0 0 READY

Basic FSM for enemies:

image Enemy 0 12

state IDLE NONE 0

frame 0 0.125 0 0 0 NONE

frame 0 0.125 0 0 0 READY

state SEE CHASE 0

frame 0 0.125 0 0 0 NONE

frame 0 0.125 0 0 0 READY

state CHASE NONE 0

frame 1 1 0 0 0 NONE

frame 2 1 0 0 0 NONE

frame 3 1 0 0 0 NONE

frame 4 1 0 0 0 READY

state ATTACK CHASE 0

frame 5 0.25 0 0 0 NONE

frame 5 0.25 0 0 0 NONE

frame 6 0.0625 0 0 0 NONE

frame 6 0.0625 0 0 0 ATTACK

frame 5 0.25 0 0 0 NONE

frame 5 0.25 0 0 0 READY

state HURT CHASE 0

frame 7 0.125 0 0 0 NONE

frame 8 0.125 0 0 0 NONE

state DEATH DEAD 0

frame 9 0.166 0 0 0 NONE

frame 10 0.166 0 0 0 NONE

frame 11 0.166 0 0 0 NONE

state DEAD NONE 0

frame 12 0.125 0 0 0 NONE

frame 12 0.125 0 0 0 NONE

Basic FSM for decorations:

image Decoration 0 3

state IDLE NONE 0

frame 0 0.25 0 0 0 NONE

frame 0 0.25 0 0 0 READY

state DEATH DEAD 0

frame 1 0.166 0 0 0 NONE

frame 2 0.166 0 0 0 NONE

frame 3 0.166 0 0 0 NONE

state DEAD NONE 0

frame 3 0.25 0 0 0 NONE

frame 3 0.25 0 0 0 NONE

Let's take a look:

image Decoration 0 3 - loads sprites and adds them to a list. They are numbered from 0. In this case this command will load every sprite from "Decoration0" to "Decoration3". Sprites must be placed in Data/Sprites/ with apropriate entity folder (Decorations or Enemies) or weapons(Weapons). e.g. "Data/Sprites/Decorations". Weapon sprites MUST have frame 0 which is used for sprites on the floor.

image Decoration - will load only one sprite (in this case named "Decoration") and add it to a list.

sound WeaponFire - will load a sound and add it to a list. They are numbered from 0. Sounds must be placed in Data/Sounds.

state IDLE NONE 0 - creates a new state, where IDLE is a name, NONE - name of the next state to jump to after this one is done (if it's NONE, current state will be looped), 0 (или 1) - sets if you want this state to interpolate model frames or not (if you use 3D models).

frame 0 0.25 0 0 0 NONE - adds a frame to the latest created state. Here 0 - number of a sprite that was loaded (see earlier), 0.25 - time this frame will be staying on screen, 0 0 0 - offset coordinates (only for weapons), NONE - action (see further down).

States shown in examples are MANDATORY to create. There might be many other states, however enemies will only move while in CHASE state. Enemy will spawn blood when in state HURT.

There's also an additional state for weapons called ALTATTACK and can be used to use your weapon with right mouse button.

Actions:
  • NONE - empty. Does nothing.
  • ATTACK - weapon or enemy attack
  • READY - sets if entity is ready to attack. Mandatory after enemy or weapon attack.
  • SOUND 0 - plays a sound, 0 - number of a loaded sound (see earlier).
  • SOUNDANDATTACK 0 - does attack and plays sound in one action.
  • SETVAR name value - sets variable to a certain value. You can use RANDOM(0,100) instead of value. The value will be randomized from 0 to 100. You can also use global. and map. prefixes to set global or map variables (see Scripting). Actually - the generated random number will always be lower than the top value by one. This means that RANDOM(0,100) will generate any number in numeric segment from 0 to 99. It will never generate value equal to 100. If you would, for example, simulate a coin flip with each side having 50% chance, with zero means heads and one means tails - you must use RANDOM(0,2) action, because it will generate only zero's or one's. RANDOM(0,1) will always output only zero's. This is true for the latest - Dec 28, 2022 - 12th version of EFPSE currently available on Itch.io.
  • INCREMENT name value - increments the value of a variable, i.e. increases by 1.
  • DECREMENT name value - decrements the value of a variable, i.e. decreases by 1.
  • JUMPIFEQUALS variable value state - moves to another state if variable value equal to the one we want. You can also use global. and map. prefixes.
  • JUMPIFNEQUALS variable value state - moves to another state if variable value is not equal to the one we want. You can also use global. and map. prefixes.
  • JUMPIFGEQUALS variable value state - moves to another state if variable value equal or greater to the one we want. You can also use global. and map. prefixes.
  • JUMPIFLEQUALS variable value state - moves to another state if variable value equal or less to the one we want. You can also use global. and map. prefixes.
  • JUMPIFGREATER variable value state - moves to another state if variable value greater to the one we want. You can also use global. and map. prefixes.
  • JUMPIFLESS variable value state - moves to another state if variable value less to the one we want. You can also use global. and map. prefixes.
  • JUMPIFLESSAMMO state - moves to another value if ammo in magazine is lower than maximum (weapons only).
  • JUMPIFNOAMMO state - moves to another state if magazine is empty (weapons only).
  • GIVEAMMO - gives 1 ammo to a magazine. (weapons only).
  • TAKEAMMO - takes one ammo from magazine (weapons only).
  • RELOAD - reloads magazine (weapons only).
  • MUZZLEFLASH - shows muzzle flash (weapons only).
  • PARTICLES imageindex i,l,x,y,z dx,dy,dz - creates particles. imageindex - number of sprite loaded, i - number of particles, l - particle lifetime, x,y,z - coordinates. Coordinates are local to enemy or player, meaning that 0,0,1 will always be in front of enemy or player. x - side offset, y - vertical offset, z - forward offset. dx,dy,dz - particle velocity. Also local to entity.
  • EXPLOSION name radius - creates an explosion with a name and radius where name is name of explosion sprites. Sprites must be place in Data/Sprites/Effects (enemies only).
  • SPAWN name x,y,z f,v - creates an entity with a name (enemies and decoration) or a keyword (Key1, Key2, Key3, Hp1, Hp2, Hp3, Hp4, Ammo1, Ammo2, Ammo3... etc.). x,y,z - just like with particles, f - front velocity when spawned, v - vertical velocity when spawning. (enemies and decorations only).
  • PLAYERSPEED - controls player's movement. You can only entirely freeze the movement (if set to 0) and enable it back (if set to 1), or double it (if set to 2).

All key words are CASE SENSITIVE Also be wary of whitespaces, they are important for engine to correctly parse parameters. Only latin characters are allowed.


An Example of a working FSM for a weapon with a Alt fire which fires 3 shots in quick succession.

image ColtNavy 0 12

sound ColtNavyFire

sound ColtNavyReload

state IDLE NONE 0

frame 1 0.1 0 0 0 NONE

frame 1 0.1 0 0 0 READY

state ATTACK IDLE 0

frame 2 0.10 0 0 0 NONE

frame 3 0.10 0 0 0 NONE

frame 4 0.10 0 0 0 SOUNDANDATTACK 0

frame 5 0.10 0 0 0 MUZZLEFLASH

frame 6 0.10 0 0 0 NONE

frame 7 0.10 0 0 0 READY

state ALTATTACK IDLE 0

frame 8 0.04 0 0 0 NONE

frame 8 0.04 0 0 0 JUMPIFNOAMMO IDLE

frame 8 0.04 0 0 0 READY

frame 9 0.04 0 0 0 SOUNDANDATTACK 0

frame 10 0.04 0 0 0 MUZZLEFLASH

frame 10 0.04 0 0 0 JUMPIFNOAMMO IDLE

frame 11 0.04 0 0 0 READY

frame 9 0.04 0 0 0 SOUNDANDATTACK 0

frame 10 0.04 0 0 0 MUZZLEFLASH

frame 10 0.04 0 0 0 JUMPIFNOAMMO IDLE

frame 11 0.04 0 0 0 READY

frame 11 0.04 0 0 0 SOUNDANDATTACK 0

frame 10 0.04 0 0 0 MUZZLEFLASH

frame 9 0.04 0 0 0 NONE

frame 8 0.04 0 0 0 READY

state RELOAD IDLE 0

frame 1 0 0 0 0 NONE

frame 1 0.1 0 0 0 SOUND 1

frame 1 0.1 0 200 0 NONE

frame 1 0.1 0 200 0 RELOAD