Skip to content

Opponents

Philipp van Kempen edited this page Jul 14, 2020 · 3 revisions

Introduction

You can find a directory called opponents on the master branch as well as on the pong branch containing a short README.md document as well as the binaries for the game opponents.

It is intentional that we do not proved the source code to compile those programs as internal routines of the opponents should be hidden to the outer world. They are compiled to work on x86 Linux machines only, so using Windows or MacOS is not an option.

Usage

The usage of the opponents should be the same for both types. You just have to replace example_opponent by either pong_opponent or space_invaders_opponent:

./example_opponent [-v] [--lock] [--host HOSTNAME] [--port PORT] [--difficulty 1|2|3]

Parameters:

  • --verbose, -v Shows detailed logging output if toggled on Default: false

  • --lock, -l Disable ability change the difficulty via the sockets. Not implemented!

  • --host, -h Hostname of the system running the Game/Emulator Default: localhost

  • --port, -p Port the transmission (TX) socket. (RX will use port+1) Default: 1234

  • --difficulty, -d Hardness of the opponent. (1 is lowest, 2 medium and 3 highest) Default: 2 (medium)

Communicating with the Opponents

You need to use 2 UDP sockets (see AsyncIO) inside your FreeRTOS-Emulator program. By default the Ports can be defined like this:

#define UDP_RECEIVE_PORT 1234
#define UDP_TRANSMIT_PORT 1235

While port 1234 will be used to listen of commands sent from the opponent, port 1235 can be used to write messages to the opponent. The format of those messages will be explained later.

To reduce the performance impact of the communication only neccesary commands should be send e.g. just notify the opponent/emulator when a state or variable has changed instead of spamming the same value every frame or more often.

As the UDP Handler is not an FreeRTOS task, you have to pay special attention when using Queues and Semaphores inside the callback. Have a look at pong.c for more details.

Common for all Games

Commands are literary short strings send via an UDP socket to a given port (1234/1235) and hostname (here: localhost/127.0.0.1/0.0.0.0).

Game -> Opponent:

  • +num/-num: A signed +/- difference (delta value) in pixels between the two objects on the screen with a leading. Replace num by an unsigned int. (Pong: num=paddle_y-ball_y, SpaceInvaders: num=player_x-enemy_x)
  • PAUSE/RESUME: Pause/Resume the sending of responses from the AI binary
  • D1/D2/D3: Sets the difficulty level of the AI, with 3 (D3) being the hardest (more or less impossible to win).

Opponent -> Game:

  • INC/DEC: Keep Incrementing/Decrementing the opponents position along the X/Y axis
  • HALT: Stop changing the opponents position.

Pong-specific

Nothing.

SpaceInvaders-specific

Game -> Opponent:

  • ATTACKING: The player has an active bullet (Impying that the opponent ship should try to dodge to stay alive)
  • PASSIVE: The player does not have an active bullet (The opponent ship may follow or escape from the player)

Warning

Some features do not work 100% at this point in time. They are listed here

  • Using a different hostname than localhost
  • Changing the default ports
Clone this wiki locally