How to get scripted programs governing game entities run in parallel with a game loop?

| | August 4, 2015

I recently discovered Crobot which is (briefly) a game where each player codes a virtual robot in a pseudo-C language. Each robot is then put in an arena where it fights against other robots.

A robots’ source code has this shape :

/* Beginning file robot.r */
    while (1)
        /* Do whatever you want */
/* End file robot.r */

You can see that :

  1. The code is totally independent from any library/include
  2. Some predefined functions are available (move, fire, etc…)
  3. The program has its own game loop, and consequently is not called every frame

My question is: How to achieve a similar result using scripted languages in collaboration with a C/C++ main program ?

I found a possible approach using Python, multi-threading and shared memory, although I am not sure yet that it is possible this way. TCP/IP seems a bit too complicated for this kind of application.

One Response to “How to get scripted programs governing game entities run in parallel with a game loop?”

  1. A way to implement it is to use Python, threads and TCP/IP. What you need is :

    1. A main application managing the game itself
    2. A python thread for each bot
    3. A python script to connect a bot to the main application (sending/receiving messages)

    What you do not need (in this particular case) is :

    1. Embed Python in the main application or vice versa
    2. Mutex, semaphore or any other stuff preventing the memory to get corrupted (thanks to TCP/IP)

    I can suggest the following design which worked for me :

    Game design picture

    Using this design, I came up with a very simple interface for the bots and a fairly playable game. A bot looks like :

    # Beginning of file
    def main(bot):
        while True:
            scanResult = bot.scan()
    # End of file

    …where you can do anything you want since it is a Python script (provided that you keep the main function’s signature as it is) !

    Update: For those interested, here is the link to the actual game on GitHub.

Leave a Reply