A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand

Overview

finitestatemachine

A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand,Written by Roberto Cezar Bianchini, July 2010 ported to php by MrFerrys.

Description

A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand,Written by Roberto Cezar Bianchini, July 2010 ported to php by MrFerrys.

Getting Started

Dependencies

  • PHP >= 5

Installation

composer require mrferrys/finitestatemachine

Usage

  • How to use it.
stateID DoBeforeEntering
"; } public function DoBeforeLeaving(){ echo "StateID: $this->stateID DoBeforeLeaving
"; } public function Reason(){ echo "StateID: $this->stateID Reason
"; if($this->fsm!=null) { $this->fsm->PerformTransition($this->stateID); } } public function Act(){ echo "StateID: $this->stateID ACTION
"; } } class stateMachine{ public $fsm; public $states=array( "nullState"=>0, "state_1"=>1, "state_2"=>2, "finish"=>3 ); public $transitions=array( "nullTransition"=>0, "state_1_done" => 1, "state_2_done"=> 2, "finishing"=>3, ); function __construct() { } public function buildFSM(){ $this->fsm = new fsmClass(); $s1 = new State_1($this->states["state_1"]); $s1->fsm = $this->fsm; $s1->AddTransition($this->transitions["state_1_done"],$this->states["state_2"]); $s2 = new State_1($this->states["state_2"]); $s2->fsm = $this->fsm; $s2->AddTransition($this->transitions["state_2_done"],$this->states["finish"]); $s3 = new State_1($this->states["finish"]); $s3->fsm = $this->fsm; $this->fsm->AddState($s1); $this->fsm->AddState($s2); $this->fsm->AddState($s3); } public function startProcess(){ while($this->fsm!=null && $this->fsm->getCurrentStateID() != $this->states["finish"] ) { $this->fsm->getCurrentState()->Reason(); $this->fsm->getCurrentState()->Act(); $this->fsm->PerformTransition($this->fsm->getCurrentStateID()); } } } //MAIN $machine= new stateMachine(); $machine->buildFSM(); $machine->startProcess();">
	use mrferrys\finitestatemachine\finiteStateMachineClass as fsmClass;
	use mrferrys\finitestatemachine\stateClass as stateClass;
	
	class State_1 extends stateClass
	{
		
		function __construct($id)
		{
		   $this->stateID=$id;
		}
		public function DoBeforeEntering(){
			echo "StateID: $this->stateID DoBeforeEntering 
"; } public function DoBeforeLeaving(){ echo "StateID: $this->stateID DoBeforeLeaving
"; } public function Reason(){ echo "StateID: $this->stateID Reason
"; if($this->fsm!=null) { $this->fsm->PerformTransition($this->stateID); } } public function Act(){ echo "StateID: $this->stateID ACTION
"; } } class stateMachine{ public $fsm; public $states=array( "nullState"=>0, "state_1"=>1, "state_2"=>2, "finish"=>3 ); public $transitions=array( "nullTransition"=>0, "state_1_done" => 1, "state_2_done"=> 2, "finishing"=>3, ); function __construct() { } public function buildFSM(){ $this->fsm = new fsmClass(); $s1 = new State_1($this->states["state_1"]); $s1->fsm = $this->fsm; $s1->AddTransition($this->transitions["state_1_done"],$this->states["state_2"]); $s2 = new State_1($this->states["state_2"]); $s2->fsm = $this->fsm; $s2->AddTransition($this->transitions["state_2_done"],$this->states["finish"]); $s3 = new State_1($this->states["finish"]); $s3->fsm = $this->fsm; $this->fsm->AddState($s1); $this->fsm->AddState($s2); $this->fsm->AddState($s3); } public function startProcess(){ while($this->fsm!=null && $this->fsm->getCurrentStateID() != $this->states["finish"] ) { $this->fsm->getCurrentState()->Reason(); $this->fsm->getCurrentState()->Act(); $this->fsm->PerformTransition($this->fsm->getCurrentStateID()); } } } //MAIN $machine= new stateMachine(); $machine->buildFSM(); $machine->startProcess();

Authors

MrFerrys

Version History

  • 1.0.0
    • Initial Release (X.Y.Z MAJOR.MINOR.PATCH)

License

Finite State Machine A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand

Written by Roberto Cezar Bianchini, July 2010

How to use: 1. Place the labels for the transitions and the states of the Finite State System in the corresponding enums.

2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
    These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
    transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM. 
    You can't have one transition leading to two different states.

   Method Reason is used to determine which transition should be fired.
   You can write the code to fire transitions in another place, and leave this method empty if you
   feel it's more appropriate to your project.

   Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
   You can write the code for the actions in another place, and leave this method empty if you
   feel it's more appropriate to your project.

3. Create an instance of FSMSystem class and add the states to it.

4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
     behave in your game) from your Update or FixedUpdate methods.

Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used, 
just call the Method PerformTransition from your FSMSystem instance with the correct Transition 
when the event occurs.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Ported to PHP by MrFerrys Author Roberto Cezar Bianchini

See the LICENSE file for details

Acknowledgments

Finite State Machine:

You might also like...
PHP Machine Learning library
PHP Machine Learning library

PHP-ML - Machine Learning library for PHP Fresh approach to Machine Learning in PHP. Algorithms, Cross Validation, Neural Network, Preprocessing, Feat

A high-level machine learning and deep learning library for the PHP language.

Rubix ML A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Show caffeine intake stats for our office coffee machine
Show caffeine intake stats for our office coffee machine

Coffee brewing statistics for the Spatie office Over at Spatie we drink a lot of coffee. How much coffee? We had no idea until we created this dashboa

Rubix ML - A high-level machine learning and deep learning library for the PHP language.

A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

A virtual machine for executing programs written in Hack.

HHVM HHVM page | HHVM documentation | Hacklang page | General group | Dev group | Twitter HHVM is an open-source virtual machine designed for executin

A list of ICs and IPs for AI, Machine Learning and Deep Learning.
A list of ICs and IPs for AI, Machine Learning and Deep Learning.

AI Chip (ICs and IPs) Editor S.T.(Linkedin) Welcome to My Wechat Blog StarryHeavensAbove for more AI chip related articles Latest updates Add news of

Explore , Experiment with data science and machine learning.

sodiumchloride Project name : sodium chloride objective : Explore,Experiment your data with datascience and machine learning version : beta 0.1.2 rele

A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2).

MRZ (Machine Readable Zones) Parser for PHP A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform. It allows you to hear the sound

Owner
null
XState - A State Machine for PHP

XState - A State Machine for PHP State machine library to play with any complex behavior of your PHP objects Installation You can install the package

Mouad Ziani 68 Dec 18, 2022
Simple game server with php without socket programming. Uses the Api request post(json).

QMA server Simple game server with php without socket programming. Uses the Api request post(json). What does this code do? Register the user as a gue

reza malekpour 3 Sep 4, 2021
🐋 This project aims to broaden knowledge of system administration by using Docker: virtualizing several Docker images, creating them in a new personal virtual machine.

?? This project aims to broaden knowledge of system administration by using Docker: virtualizing several Docker images, creating them in a new personal virtual machine.

Anton Kliek 1 Jan 26, 2022
A text-based, persistent browser-based strategy game (PBBG) in a fantasy war setting

Note: OpenDominion is still in development. Some features of the game have not been implemented yet. Introduction OpenDominion is a free and open-sour

null 180 Dec 28, 2022
PHP Machine Learning with Naive Bayes to classify the right contraceptive based on your medical history

What is php-ml-bayes PHP-ML Bayes is a Machine Learning with Naive Bayes Algorithm to classify the right contraceptive based on your medical history.

Fikri Lazuardi 2 Jan 21, 2022
Small library providing some functional programming tools for PHP, based on Rambda

Functional library for PHP. Features: set of useful functions helpful in functional programming all functions are automatically curried every array ca

Wojciech Nawalaniec 5 Jun 16, 2022
Generally, a graceful shutdown is preferable in the case of any OS that saves its state

Graceful Shutdown with PHP Generally, a graceful shutdown is preferable in the case of any OS that saves its state.

Leonardo Carmo 17 Oct 14, 2022
Reset UI Bookmarks allows admin users to reset their own UI bookmarks such as state of filters, column positions and applied sorting ( e.g Sales > Orders ).

Reset Ui Bookmarks Reset UI Bookmarks becomes an invaluable tool while working daily in the admin panel, especially on Magento® instances with a large

Magenizr 23 Oct 19, 2022
Rules to detect game engines and other technologies based on Steam depot file lists

SteamDB File Detection Rule Sets This is a set of scripts that are used by SteamDB to make educated guesses about the engine(s) & technology used to b

Steam Database 103 Dec 14, 2022
Core functionality for Legend of the Green Dragon, a text-based RPG game.

Legend of the Green Dragon (Core) Legend of the Green Dragon is a text-based RPG originally developed by Eric Stevens and JT Traub as a remake of and

Legend of the Green Dragon 135 Dec 28, 2022