Print

Print


I have a simple agent spawning a child in an Object2Dgrid and I am having difficulty overcoming an issue that can be seen in the three pictures.
One is the run and when it occupies the next space I expect it to stop.
Two is the space occupied and Three is two steps later when the agent has teleported through the occupied cell and spawned once again.
Here is a copy of the Agent code.
Any advice will be gratefully received.

Kind regards 
Stewart Aitken

package sim.alg1;

import sim.engine.SimState;
import sim.engine.Steppable;
import sim.field.grid.Grid2D;
import sim.field.grid.ObjectGrid2D;
import sim.util.Bag;
import sim.util.Int2D;
import sim.util.IntBag;

/**
 * Created by Your PC on 14/06/2015.
 */
public class Agent implements Steppable {
    public static final int gridWidth = 12;
    public static final int gridHeight = 10;

    private int myX, myY, nextX, nextY;
    private double age;
    
    // This constructor for offspring
    public Agent(){
        age = 0.0;
    }

    public Agent(SimEnviro theModel) {
        placeFirstAgent(theModel);
        age = 1.0;
    }

    public void placeFirstAgent(SimEnviro theModel) {
        ObjectGrid2D mySpace = theModel.getAgentSpace();

		do {
			myX = theModel.random.nextInt(gridWidth);
			myY = theModel.random.nextInt(gridHeight);
		}
		
        while (mySpace.field[myX][myY] != null);
        mySpace.field[myX][myY] = this;
    }

    public void step(SimState state) {

    public void replicate(SimState state) {
        SimEnviro theModel = (SimEnviro) state;
        Bag theAgents = theModel.getAgents();

        ObjectGrid2D mySpace = theModel.getAgentSpace();
        
        Bag vNObjects = new Bag(4);
        IntBag xNeighbors = new IntBag(4);
        IntBag yNeighbors = new IntBag(4);

   		mySpace.getVonNeumannNeighbors(myX, myY, 1, Grid2D.BOUNDED, false, vNObjects, xNeighbors, yNeighbors);

   		
        int len = xNeighbors.size();
        for (int i=0; i<len; i++) {
            Agent child = new Agent();
                nextX = mySpace.tx(xNeighbors.get(i));
                nextY = mySpace.tx(yNeighbors.get(i));
                if (mySpace.field[nextX][nextY]== null) {
                	System.out.print(" nextX = " + nextX);
                	System.out.println(" : NextY = " + nextY);
                    mySpace.field[nextX][nextY] = child;
                    child.myX = nextX;
                    child.myY = nextY;
                    theAgents.add(child);
                    break;
                }
        		
            } myX = nextX; myY = nextY; age++;
    }

	private void checkIfFinished(Bag theAgents) {
		// TODO Auto-generated method stub
		
	}
}