• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    C++
  • License
    zlib License
  • Created almost 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📑 Simple and lightweight Hierarchical/Finite-State Machine (H-FSM) class (C++11)

fsm

FSM is a lightweight finite-state machine class. Both Hierarchical FSM and simple FSM implementations are provided.

Features

  • Expressive. Basic usage around on(state,trigger) -> do lambda expression.
  • Tiny, cross-platform, stand-alone, header-only.
  • ZLIB/libPNG licensed.

Links

Finite-State Machines: Theory and Implementation

Showcase

// basic hfsm sample

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

// custom states (gerunds) and actions (infinitives)

enum {
    walking = 'WALK',
    defending = 'DEFN',

    tick = 'tick',
};

struct ant_t {
    fsm::stack fsm;
    int health, distance, flow;

    ant_t() : health(0), distance(0), flow(1) {
        // define fsm transitions: on(state,trigger) -> do lambda
        fsm.on(walking, 'init') = [&]( const fsm::args &args ) {
            std::cout << "initializing" << std::endl;
        };
        fsm.on(walking, 'quit') = [&]( const fsm::args &args ) {
            std::cout << "exiting" << std::endl;
        };
        fsm.on(walking, 'push') = [&]( const fsm::args &args ) {
            std::cout << "pushing current task." << std::endl;
        };
        fsm.on(walking, 'back') = [&]( const fsm::args &args ) {
            std::cout << "back from another task. remaining distance: " << distance << std::endl;
        };
        fsm.on(walking, tick) = [&]( 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(defending, 'init') = [&]( const fsm::args &args ) {
            health = 1000;
            std::cout << "somebody is attacking me! he has " << health << " health points" << std::endl;
        };
        fsm.on(defending, tick) = [&]( const fsm::args &args ) {
            std::cout << "\r" << "\\|/-$"[ health % 4 ] << " health: (" << health << ")   ";
            --health;
            if( health < 0 ) {
                std::cout << std::endl;
                fsm.pop();
            }
        };

        // set initial fsm state
        fsm.set( walking );
    }
};

int main() {
    ant_t ant;
    for(int i = 0; i < 12000; ++i) {
        if( 0 == rand() % 10000 ) {
            ant.fsm.push(defending);
        }
        ant.fsm.command(tick);
    }
}

Changelog

  • v1.0.0 (2015/11/29): Code revisited to use fourcc integers (much faster); clean ups suggested by Chang Qian
  • v0.0.0 (2014/02/15): Initial version

More Repositories

1

bundle

📦 Bundle, an embeddable compression library: DEFLATE, LZMA, LZIP, BZIP2, ZPAQ, LZ4, ZSTD, BROTLI, BSC, CSC, BCM, MCM, ZMOLLY, ZLING, TANGELO, SHRINKER, CRUSH, LZJB and SHOCO streams in a ZIP file (C++03)(C++11)
C++
623
star
2

scriptorium

📜 Game Scripting Languages benchmarked
C
486
star
3

statvs

Hopefully updated status of all my github repositories
344
star
4

AVA

A tiny unlicensed 3D game engine in C; with C++ and Lua interfaces. Written in 32 random ̷d̷a̷y̷s̷ m̷o̷n̷t̷h̷s̷ years.
C
336
star
5

getopt

Simple command-line options handler (C++11)
C++
327
star
6

sole

🍩 Sole is a lightweight C++11 library to generate universally unique identificators (UUID), both v1 and v4.
C++
296
star
7

tracey

:squirrel: Tracey is a lightweight and simple C++ memory leak finder with no dependencies.
C++
226
star
8

ltalloc

LightweighT Almost Lock-Less Oriented for C++ programs memory allocator
C++
164
star
9

spot

🌀 Compact and embeddable RGBA/HSLA library that supports WEBP, JPG, progressive JPG, PNG, TGA, DDS DXT1/2/3/4/5, BMP, PSD, GIF, PVR2/3 (ETC1/PVRTC), KTX (ETC1/PVRTC), PKM (ETC1), HDR, PIC, PNM (PPM/PGM), CRN, PUG, FLIF, CCZ, EXR and vectorial SVG files (C++11)
C
134
star
10

kult

🔮 Lightweight entity/component/system library (C++11)
C++
127
star
11

dessert

🍰 Lightweight unit-testing framework (C++11).
C++
87
star
12

img2sky

🌒 A vertex-color mesh builder tool for skyboxes and static geometry, as seen in HomeWorld 2 .HOD files
C
76
star
13

dollar

💰 A portable CPU profiler with ASCII,CSV,TSV,Markdown,chrome://tracing support (C++11)
C++
64
star
14

base

Base91 / Base85 / Base64 de/encoders (C++03)
C++
61
star
15

trie

Trie is a lightweight and simple autocompletion data structure written in C++11.
C++
43
star
16

attila

🔥 Attila is a tiny atlas texture-packer (tool)
C++
40
star
17

quant

🍥 Tiny quantization suite supporting conversion to/from half-floats, s/unorm bytes, quaternions and vectors (C++03).
C++
39
star
18

wire

🔌 Wire is a drop-in std::string replacement with extended functionality and safe C/C++ formatters (C++11).
C++
39
star
19

tween

👯 Tween is a lightweight easing library. Written in C++03
C++
35
star
20

apathy

💾 Apathy is a lightweight path/file/mstream/mmap IO library (C++03)
C++
35
star
21

pug

🐶 pug, png with a twist: lossy image format
33
star
22

malloc-survey

📈 Allocation benchmarks
C++
30
star
23

mINI

▫️ A very minimal .INI reader/writer (C++11)
C++
29
star
24

id

:suspect: ID, a compile-time string hasher and sequential ID generator (C++11)
C++
27
star
25

LRU

A lightweight LRU cache structure for list<T> and map<K,V> containers. Written in C++11
C++
27
star
26

bubble

💬 A simple and lightweight C++11 dialog library (for Windows)
C++
25
star
27

knot

Knot is a lightweight and simple TCP networking C++ library with no dependencies.
C++
25
star
28

sqlight

🔦 SQLight is a lightweight MySQL client written in C++11. Based on code by Ladislav Nevery
C++
25
star
29

DrEcho

💊 Dr Echo spices your terminal up (C++11)
C++
24
star
30

cocoa

🍫 Cocoa is an uniform hashing library with no dependencies that provides interface for CRC32, CRC64, GCRC, RS, JS, PJW, ELF, BKDR, SBDM, DJB, DJB2, BP, FNV, FNV1a, AP, BJ1, MH2, SHA1, SFH (C++11)
C++
24
star
31

fmt11

Tiny format/mustache templating library (C++11)
C++
22
star
32

auth

Simple, lightweight and safe client-server authentication system. Written in C++
C++
22
star
33

bundler

📦 Command-line archiver: DEFLATE, LZMA, LZIP, BZIP2, ZPAQ, LZ4, ZSTD, BROTLI, BSC, CSC, BCM, MCM, ZMOLLY, ZLING, TANGELO, SHRINKER, CRUSH, LZJB and SHOCO streams in a ZIP file.
C++
21
star
34

moon9

a game framework. warning: wip, dev, unstable, radiation hazard, defcon 3
C++
21
star
35

assume

🙈 Assume is a smarter assert replacement (C++03)
C++
21
star
36

bourne

😏 Bourne is a lightweight JSON de/serializer (C++11).
C++
21
star
37

unify

🔗 A C++11 function to normalize resource identificators
C++
20
star
38

collage

✂️ A lightweight C++ library to diff and patch arbitrary data
C++
20
star
39

metrics

📊 Pretty table metrics w/ benchmarking, unit conversions in CSV,TSV,ASCII,markdown (C++11)
C++
19
star
40

sand

⌛ Sand is a lightweight time controller (C++11)
C++
19
star
41

hertz

⌚ Hertz, simple framerate locker (C++11)
C++
18
star
42

frodo

💍 A lightweight ring dependency system (C++11)
C++
18
star
43

giant

🗿 Giant is a tiny C++11 library to handle little/big endianness.
C++
18
star
44

vle

Simple variable-length encoder/decoder (C99)(C++03)
C++
15
star
45

heal

💉 Heal is a lightweight C++ library to aid and debug applications. Heal requires C++11 (or C++03 with boost at least).
C++
15
star
46

live

🎭 Automatic reloader of constants during runtime, featuring type inference. Written in C++11.
C++
15
star
47

crawler

Crawler, a quick prototiping platform for Windows (C++11)
C
14
star
48

units

fork of http://calumgrant.net/units (unavailable) + new units + c++0x support + markdown documentation (C++03)
C++
14
star
49

unifont

Embeddable console 1bpp font that supports many european/eastern unicode codepoints. Aimed to gamedev (C++11).
C
13
star
50

warp

♻️ A handy string interpolator (C++11)
C++
13
star
51

flow

🎈 Lightweight C++ network downloader with native fallbacks. Aimed to gamedev.
C++
13
star
52

burg

Simple burg linear predictor (C++11)
C++
13
star
53

oak

🌳 A simple and lightweight tree container (C++03)
C++
13
star
54

memo

📌 Simple and lightweight factory class, featuring automatic type casting. Written in C++11
C++
12
star
55

journey

🐫 Lightweight append-only, header-less, journaling file format (C++11)
C++
12
star
56

route66

⛽ Lightweight embeddable HTTP server. Written in C++11 (or C++03 w/boost).
C++
12
star
57

duty

Duty is a lightweight C++11 task manager for parallel coroutines and serial jobs. Duty requires no dependencies.
C++
10
star
58

unlzma

A very compact LZMA decoder (C++03)
C++
9
star
59

sentry

Sentry is a lightweight data monitor (C++11)
C++
9
star
60

vitae

📄 My self-compilable C++ resume
C++
9
star
61

codex

Codex is a lightweight and simple C++ library to escape, unescape, read, write and convert from/to different encoding charsets. Codex requires no dependencies.
C++
8
star
62

flare

✨ Lightweight C++ API to deal with digital signals/logical buttons (C++03).
C++
7
star
63

solace-wip

Solace is a modern console replacement. Written in C++03
C++
6
star
64

error64

Handle custom 64-bit error codes, with extended meta-info (C, C++)
C
6
star
65

fortfont

💱 Collection of western, CJK and iconographic fonts free for commercial usage
CSS
5
star
66

cash-of-clans

a free re-implementation of a working game economy system
C++
5
star
67

gpulib

C++
4
star
68

rgb332

custom uniform RGB332 palette
C++
4
star
69

hyde

🎮 Hyde is a lightweight and simple Human Inferface Device (HID) C++ framework with no dependencies.
C++
4
star
70

bridge

A standard C++11 <--> C++03/boost compatibility layer, plus a few utils
C++
3
star
71

pitch

📒 A forkable game pitch template (CC0, Markdown)
3
star
72

unordered_map

Portable header for std::unordered_map<K,V> template
C++
3
star
73

variant

Variant is a varying C++11 class that clones javascript behaviour as much as possible
C++
2
star
74

emojis

:octocat: Emojis, atlased and indexed
2
star
75

CLDR

Compact data from the Unicode Common Locale Data Repository
C++
2
star
76

blender-wip

An agnostic keyframe interpolation and animation controller. Written in C++11.
C++
2
star
77

atom-wip

A small library to multiplex, to interweave, to sort, to split and to join (sub)streams of binary data (C++11).
C++
2
star
78

depot

asset depository for all my repos
C++
2
star
79

vision

a few assorted premises and thoughts
2
star
80

jabba-wip

jabba, the hud engine. yet another generic hud/ui engine
C++
2
star
81

JXMLex

JXMLex description format
1
star
82

watchmen-wip

Dependency system for your libraries of game assets in run-time
C++
1
star
83

JXML

JXML is a loss-less representation of JSON in XML, so data can be reused with XML tools.
1
star
84

rescle

Automatically exported from code.google.com/p/rescle
C++
1
star
85

tint-wip

Colorful logging tool
C++
1
star
86

stringbase

Stringbase is a collaborative effort aimed to translate common texts found in videogames and regular apps. Free to use. Commits welcome!
1
star