2.2. A Simple Example

"Hello, World" is a very simple example which can be used for any programming language. It does not, however, give any insight into how 3D multiagent simulations are written in steve. This section shows the code for a simple 3D multiagent simulation.

The simulation below is very heavily commented—all of the lines beginning with the # character are descriptive comments which do not effect the execution of the simulation, but clarify for the user what the simulation is doing.

The simple simulation below features a number of agents which perform a "random walk" through 3D space. That is to say that at each time-step, the agents will move in a random direction.

@include "Control.tz"           
@include "Mobile.tz"           

#
# RandomWalker is a simulation in which small spheres do
# a random walk.  This is a very simple simulation which
# can be used as a skeleton for more complex simulations.
#

# First tell the breve engine the name of our controller class

Controller myControl.             

# Subclass "Control" and define our "myControl" object.

Control : myControl {
    # Here we define the class variables.  

    + variables:
        walkerShape (object).
        
    # Next we define a method called init.  The init method is called 
    # automatically when our class is created.  Since this is the controller
    # class, an instance gets created when we launch the simulation,
    # so this is the entry point for our simulation.
        
    + to init:
        print "Setting up the simulation.".

        self point-camera at (0, 0, 0) from (0, 60, 0).

        # set up a shape that all of the RandomWalker objects will use.
        
        walkerShape = new Shape.
        walkerShape init-with-sphere radius 1.
        
        # Create a bunch of RandomWalkers.  You can create as few or 
        # as many as you want... 

        200 new RandomWalker.

    # The get-walker-shape is a method that allows other objects 
    # to look at the walkerShape variable.  we do this so that 
    # each RandomWalker object can reuse the same Shape object.
    # This is not strictly required--each RandomWalker could create
    # it's own copy of the same Shape, but fewer objects means 
    # less memory used and more efficient simulations, so it's 
    # a good programming practice.
        
    + to get-walker-shape:
        return walkerShape.
}

# The RandomWalker object is the physical object in the simulation
# that does the random walk.

Mobile : RandomWalker {
    + to init:
        # During init, the object asks the controller for the shape 
        # it should use.  It then sets itself to a random color.

        self register with-shape (controller get-walker-shape).
        self set-color to random[(1.0, 1.0, 1.0)].
        self move to random[(.1, .1, .1)].
    
    + to iterate:
        # Set a new random velocity at every timestep.
        
        self set-velocity to random[(60, 60, 60)] - (30, 30, 30).
}