Here is some programming tips for low-level game programming.

C++ is probably one of the most common language for game programming. When programming the bot engine, such as a finite state machine fsm), generally the rules are implemented in the following format: state + event -> new_state

/*

suppose state and event are both variables.

Also, lets say there are five values for state (using enum STATE1, STATE2, STATE3, STATE4, STATE5) and

three event types (again using enum EVENT1, EVENT2, EVENT3).

The following is a commonly found syntax of a fsm implementation:

*/

if (state == STATE1 && EVENT == EVENT1) {

... ACTION 1 ....

}

else if (state == STATE1 && EVENT == EVENT2) {

... ACTION 2 ...

}

.... and so on.

If the fsm is to be implemented at machine level through some assembly language, then the way to encode state and event would be quite different.

Instead of encoding the five states as STATE1, STATE2 etc, which would eventually be converted to 0, 1, 2 etc,

it is more efficient to encode the states using 5 bits which are all packed together in 1 byte:

STATE1 becomes 00001

STATE2 becomes 00010

STATE3 becomes 00100

and so on.

The events can be implemented likewise using 3 bits: 001, 010, 100.

There are many advantages for this implementation over using the integer values like 0, 1, 2, 3 etc.

Here are the lovely properties of the 5-bit code format:

First, the bit codes are all linearly independent unlike the integer-based codes. So, the state-event combination can be packed into a longer string of 8 bits. For example,

STATE1 + EVENT1 -> STATE2

00001 001 -> 00010 000

This implementation greatly improves the processing speed.

Second, this way of encoding events is closer to what really happens at the hardware level. If a bot is to be on a real mechanical device, it is likely that events are triggered by signals from sensors. For example, a bump on an obstacle triggers a tactile event, which turns on a tactile bit.

## Bookmarks