001    import swarm.*;
002    import swarm.objectbase.*;
003    import swarm.defobj.*;
004    import swarm.space.*;
005    
006    public class Bug extends SwarmObjectImpl {
007            int xPos, yPos;
008            int worldXSize, worldYSize;
009            FoodSpace foodSpace;
010            Grid2d world;
011            int haveEaten;
012            
013            public Bug(Zone aZone){
014                    super(aZone);
015            }
016            
017            public Object setWorld$Food(Grid2d w,FoodSpace f){
018                    world=w;
019                    foodSpace=f;
020                    setWorldSizeX$Y(world.getSizeX(),world.getSizeY());
021                    return this;
022            }
023            
024            public Object setX$Y(int x, int y){
025                    xPos = x;
026                    yPos = y;
027                    world.putObject$atX$Y(this,xPos,yPos);
028                    return this;
029            }
030            
031            public Object setWorldSizeX$Y(int xSize, int ySize){
032                    worldXSize=xSize;
033                    worldYSize=ySize;
034                    return this;
035            }
036            
037            public void step(){
038                    int newX,newY;
039                    haveEaten=0;
040                    
041                    newX = xPos + Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1,1);
042                    newY = yPos + Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1,1);
043                    newX = (newX + worldXSize) % worldXSize;
044                    newY = (newY + worldYSize) % worldYSize;
045                    
046                    if (world.getObjectAtX$Y(newX,newY) == null){
047                            world.putObject$atX$Y(null,xPos,yPos);
048                            setX$Y(newX, newY);
049                    }
050                    
051                    if (foodSpace.getValueAtX$Y(xPos,yPos) == 1){
052                            foodSpace.putValue$atX$Y(0,xPos,yPos);
053                            haveEaten=1; // set flag for reporting
054                    }
055            }
056            
057            public Object report(){
058                    if(haveEaten==1)
059                            System.out.println("I found food at X = " + xPos + " Y = " + yPos +"!");
060                    return this;
061            }
062    }