AddTraction Diary Entry of 2004-01-26

Generic Player Interface

There are two things any generic player needs to do:

  1. Looking on the fields
  2. Take a turn (when it is her turn)

Equipped with this knowledge, the easiest interface would consist of one callback and one normal function:

  • int get_field(int x, int y) to be called by the player
  • (int,int) get_turn() is what the player has to implement.

However, this naive approach has two problems:

  1. Keyboard and mouse players will both use the event system and can not be encapsulated into a get_turn function very elegantly
  2. AI and network players would block the game while in the function call waiting for the turn.

Threads would be an alternative here, but they introduce other programming overhead (such as synchronisation and communication). Also threads might not be portable across all platforms and behave differently on some.

Devoid of threads, what to do? Coroutines, as in Lua would serve well, however, they are not a feature of the C language. But it is possible to emulate them by interface:

The get_field(Field *f, int x, int y) function stays the same.
But the player now has two callbacks:

  • int think(Field *F) will be called as long as it returns 0.
  • (int, int) get_turn(Field *F) will be called once when think returns 1.

This way, the game can call think multiple times in the event loop, alternating between "thinking" and event handling.

Keyboard and mouse players will implement the think function as returning a variable which will be set by the event handler and reset by get_turn.

AI and network players will probably have more "meat" in think. To make sure that every AI / network player plays fair and does not hog system resources, the calling contract for think and get_turn should include that no more than one second may be used within one function call. With fine granularity, this limit should be met even on slow machines.

Note that I used (int, int) as a shorthand for some struct, it is to my knowledge not valid C syntax. Also the interface will probably be rounded by some init and exit functions the players may implement to set up (and down) their innards.

Back to main page

Gfx by Gimp  Valid XHTML 1.0 
Transitional  SourceForge Logo last modified:
2004-01-27 by llogiq