/* * * HEATBUGS in MAML * (c) 1998, CEU Systems Laboratory * * This simulation is based on the following model: * * Heatbugs application. Copyright (C) 1996 Santa Fe Institute. * This library is distributed without any warranty; without even the * implied warranty of merchantability or fitness for a particular purpose. * See file LICENSE for details and terms of copying. * */ // The HeatSpace is a simple object to represent the heat in the // world: a spatial variable. HeatSpace inherits most of its behaviour // from the "Diffuse" space Object. It is basically a rectangular grid. // Heat values are stored in each cell. // The heat diffuses to the neighbouring cells as time goes by. @agent HeatSpace : Diffuse2d { @uses ; // We need this Swarm package as // Discrete2d is there. // Shorthands borrowed from the Swarm implementeation $define HeatValue : "int"; $define maxHeat: "0x7fff"; $define HeatExtremeType : "int"; $define hot : "42", cold : "24"; // Increase the heat in a cell. The maximum is limited to "maxHeat". @sub: (void) addHeat: (HeatValue) moreHeat X: (int) x Y: (int) y { HeatValue heatHere = [self getValueAtX: x Y: y]; heatHere = ( (moreHeat <= maxHeat-heatHere) ? heatHere + moreHeat : maxHeat ); [self putValue: heatHere atX: x Y: y]; } // Try to find a better place in the neighbourhood. // Better means colder or warmer according to "type". // Sets "px" and "py" to the best position and returns the heat there. // The % (modulo) treatment of the indeces results in a thoric behavior. @sub: (HeatValue) findExtremeType: (HeatExtremeType) type X: ([] int) px Y: ([] int) py { HeatValue bestHeat; int x, y; int bestX, bestY; bestX = *px; bestY = *py; bestHeat = [self getValueAtX: bestX Y: bestY]; for (x = *px - 1; x <= *px + 1; x++) { // search all neighbours for (y = *py - 1; y <= *py + 1; y++) { HeatValue heatHere; boolean hereIsBetter; heatHere = [self getValueAtX: (x+xsize)%xsize Y: (y+ysize)%ysize]; hereIsBetter = (type == cold) ? (heatHere < bestHeat) : (heatHere > bestHeat); if (hereIsBetter) { bestHeat = heatHere; bestX = x; bestY = y; } } } *px = (bestX + xsize) % xsize; *py = (bestY + ysize) % ysize; return bestHeat; } }