- Table of contents
- Set of rules
- Screen saver basics
- Release notes
This article describes the implementation of an OpenGL screen saver based on a discrete simulation of predator-prey interaction. The simulation is called Wa-Tor. It was originally described by Alexander K. Dewdney in the Scientific American magazine.
It simulates the hypothetical toroidal Planet Wa-Tor (Water Torus) whose surface is completely covered with water, occupied by two species: fish and sharks. The sharks are the predators. They eat the fish. The fish exist on a never ending supply of plankton. Both sharks and fish live according to a strict set of rules. This simulation of a simple ecology is highly dynamic as both species are walking a thin line between continuing life and extinction.
The screensaver is based on a dialog based MFC application combined with OpenGL for the 2D drawing. As usual, for OpenGL, using the binary without a hardware or drivers correctly supporting it will result in very low frame rates. The source code requires Microsoft Visual Studio V7.1 in order to compile, MSVC6 is not supported.
Set of rules
In this paragraph, I will briefly explain the basic rules for a Wator simulation and describe the changes necessary in order to implement the screensaver. In general, the simulation is based on discrete time steps. The simulation runs on a rectangular grid. To represent the toroidal world, opposing sides of the grid are connected. If an individual moves out on one side of the simulation domain, it reenters immediately on the opposing side. Fish and shark move every time step (if possible) and interact according to the following set of rules:
Rules for fish
In every time step, a fish moves randomly to one of the four neighboring fields, provided it is empty. Every fish has a predefined "breed time". On exceeding this time, it gives birth to a new fish in one of the neighboring cells, provided this randomly selected cell is free. (If not nothing happens.) Breed time counter of both the original and the descendant fish will be reset to zero. Technically fish never die. They live until they reach the breed time, then they clone and both parent as well as offspring restart their life cycle. (To clarify: according to the simulation the parent dies and creates two offspring individuals, but I don't delete the parent, I just reset it.)
The following picture shows options for prey movement. Arrows indicate possible movements. Fish are not allowed to move to cells occupied by sharks. If there are no free neighboring cells no movement occurs.
Rules for sharks
Sharks move randomly to fields that are either free or occupied by fish. Every round they lose one point of energy. If they enter a field occupied by a fish they eat the fish and gain a defined amount of energy. If the energy level drops below zero the shark dies. If the energy exceeds a predefined value sharks create an offspring in a free neighboring field. The energy is split evenly between the parent and the child.
Modifications made to the original algorithm
In order to turn this simulation into a screensaver, I made some changes to the original set of rules. According to the original algorithm, sharks do not move randomly. Instead they move specifically to cells in their neighborhood containing fish. Testing this is slow, thus I allow only random movement. Surprisingly there is no visible difference. Alexander K. Dewdney suggested an implementation very similar to a cellular automata. If you implement Wa-Tor that way and by looping over all cells in the simulation domain, you are forced to include cells that do not contain any individual in the calculation. Since this is too slow I chose to implement it as a linked list, thus instead of looping over all cells, my algorithm loops over existing individuals only.
The information provided here is taken from the Wikipedia article on Lotka-Volterra differential equations. For a more detailed explanation I refer you to this article.
Drawing the number of individuals per species versus time yields curves characteristic for the so called Lotka-Volterra equations (also known as predator-prey equations).
Diagram: Population size vs. time for a predator prey model..
This behaviour can be described by two differential equations. There usual form is:
- y is the number of some predator (here: sharks)
- x is the number of its prey (here: fish)
- t represents the development of the two populations against time
- alpha, beta, gamma and delta are parameters representing the interaction of the two species.
Although the solution to these differential equations show a periodic behaviour, they can not be expressed in terms of normal trigonometric functions. However, an approximate linearised solution yields a simple harmonic motion with the population of predators leading that of prey by 90%.
Screen saver basics
Technically a Windows screensaver is a normal executable with the extension scr. Additionally it has to have a defined command line interface in order to display itself in configuration mode, in preview mode or in screen saver mode.
|Command line optionn||Saver action|
|/P:###||Start the screensaver in preview mode. Treat ### as the decimal representation of a |
HWND. Popup a child window of this
HWND and run in preview mode inside that window.
|/C:###||Open the screen saver configuration dialog. If ### is not present |
GetForegroundWindow() should be the parent of this dialog. Otherwise ### should be treated as the decimal representation of a
HWND, which should be used as a parent.
|/S||Run as screen saver in full screen mode. When there is some kind of keyboard or mouse activity in the screen saver mode, the screen saver is ended.|
Table 1: Command line options.
Download the archive with the binary and copy the file WatorSaver.scr to your Windows/System322 subdirectory. I do not provide a setup program due to the simplicity of the installation process.
After moving the screen saver to your windows/system32 directory, you can edit its settings like any other screen saver settings. Go to the Windows desktop, right click, and in the context menu, select the display options entry.
The configuration dialog allows setting up the simulation parameters described above as well as additional settings for the display options.
All changes made to the parameters will be reflected instantly in the preview window. The following table lists the parameters and their meaning for the simulation process:
|Prey breed time||The number of time steps necessary before a prey can create an offspring.|
|Energy per hunted prey||The energy a predator gets for each prey individual it eats. In each time step, the predator loses one energy point. It dies when the energy falls below zero.|
|Energy to create offspring||The minimum amount of energy a predator must have in order to create an offspring. When creating an offspring, the energy is evenly split between the newly created offspring and the parent.|
|Pixelsize||The size of an individual in pixels. Reducing this number will require very high performance!|
Like most screensavers, this is a program featuring a controlled amusing misuse of system resources. It is currently a beta version and you use it without warranty of any kind!
Currently the screen saver does not support multiple monitors. Moreover it will only save your screen if you disable all static text displays in its configuration dialog. This won't change in future versions but I don't consider this a problem since today's TFT Displays don't need a screensaver anyway.
You use it at your own risk, no warranties of any kind!
Copyright 2007 Ingo Berg
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
A more detailed introduction on screen saver programming and issues related to this article can be found at the following places:
Rev 0.90 (Beta): 31/07/2005
First version of the screensaver released. This is a beta version released for testing purposes only. There is much room for improvement like adding multiple monitor support.
Rev 1.0: 18/08/2005
This release contains bug fixes and slight modifications that make the screensaver look prettier (nothing spectacular).
- Fixed bug that prevented the screensaver from running on ATI graphics cards.
- Added a background pattern.
- Changed title bitmap.
- Statistic is now scaled between its minimum and maximum.
Rev 1.01: 21/01/2006
- Support for multiple monitors added.
- The screensaver can now be deactivated by mouse moves.
Rev 1.02: 03/03/2007
- License changed to GPL-II.