Coder Social home page Coder Social logo

wilhantian / fsm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from r-lyeh-archived/fsm

1.0 1.0 0.0 62 KB

:bookmark_tabs: Simple and lightweight Hierarchical/Finite-State Machine (H-FSM) class. Written in C++11

License: zlib License

C++ 100.00%

fsm's Introduction

fsm

  • FSM is a lightweight Hierarchical/Finite-state Machine (H/FSM) class.
  • FSM is expressive. Basic usage around on(state,trigger) -> do lambda expression.
  • FSM is simple. Both HFSM and FSM implementations are provided.
  • FSM is tiny. Header-only.
  • FSM is cross-platform.
  • FSM is stand-alone. No dependencies.
  • FSM is zlib/libpng licensed.

API

@todoc

Links

Finite-State Machines: Theory and Implementation

Sample

// basic hfsm sample

#include <iostream>
#include "fsm.hpp"

// common triggers (verbs). you must provide these for all FSMs
fsm::state begin("begin"), end("end"), pause("pause"), resume("resume");

// custom states (gerunds) and triggers (verbs)
fsm::state walking("walking"), attacking("attacking");
fsm::state update("udpate");

class ant {
public:
        fsm::stack fsm;
        int health, distance, flow;

        ant() : health(0), distance(0), flow(1) {
            // set initial fsm state
            fsm = walking;

            // define fsm transitions: on(state,trigger) -> do lambda
            fsm.on(walking,resume) = [&]( const fsm::args &args ) {
                std::cout << "pre-walk! distance:" << distance << std::endl;
            };
            fsm.on(walking,update) = [&]( const fsm::args &args ) {
                std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
                distance += flow;
                if( 1000 == distance ) {
                    std::cout << "at food!" << std::endl;
                    flow = -flow;
                }
                if( -1000 == distance ) {
                    std::cout << "at home!" << std::endl;
                    flow = -flow;
                }
            };
            fsm.on(attacking,begin) = [&]( const fsm::args &args ) {
                std::cout << "pre-attack!" << std::endl;
                health = 1000;
            };
            fsm.on(attacking,update) = [&]( const fsm::args &args ) {
                std::cout << "\r" << "\\|/-$"[ distance % 4 ] << " fighting !! hp:(" << health << ")   ";
                --health;
                if( health < 0 ) {
                    std::cout << std::endl;
                    fsm.pop();
                }
            };
        }
};

int main( int argc, const char **argv ) {
    // hfsm ant sample
    ant mini;
    for(;;) {
        if( 0 == rand() % 10000 ) mini.fsm.push(attacking);
        mini.fsm(update());
    }
}

fsm's People

Stargazers

Mark avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.