/* * (c) 1998-2000 The Brookings Institution, All Rights Reserved * * Permission to use this software and its documentation for non-commercial * purposes and without fee is hereby granted, provided this copyright statement * is included. Please contact us for permission for redistribution and other uses. *  * BROOKINGS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,  * OR NON-INFRINGEMENT. BROOKINGS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING THIS SOFTWARE OR ITS DERIVATIVES. * * _Sugarscape_ * See _Growing Artificial Societies: Social Science from the Ground Up_ * Brookings Institution Press, The MIT Press * Joshua Epstein * jepstein@brook.edu * Robert Axtell * raxtell@brook.edu * * Miles Parker * mparker@brook.edu * http://www.brook.edu/es/dynamics/models/ascape * The Brookings Institution * Washington, D.C. * * Alan Lockard * alockard@gmu.edu *//** * @history changed variables from private to protected so SpiceCell could inherit * seperated peak creation from initialize() */package edu.brook.sugarscape;import java.lang.*;import java.awt.Color;import edu.brook.ascape.model.*;import edu.brook.ascape.rule.*;import edu.brook.ascape.util.*;public class SpiceCell extends SugarCell {    public final static Rule SUGAR_SPICE_GROW_BACK_1_RULE = new Rule("Spice Grow Back 1") {        public void execute(Agent agent) {            ((SugarCell) agent).sugarGrowBack1();            ((SpiceCell) agent).spiceGrowBack1();        }    };    public final static Rule SUGAR_SPICE_GROW_BACK_INF_RULE = new Rule("Spice Grow Back Infinte") {        public void execute(Agent agent) {            ((SugarCell) agent).sugarGrowBackInf();            ((SpiceCell) agent).spiceGrowBackInf();        }    };        public static Coordinate2DDiscrete[] spicePeaks;     public static int MAX_SPICE = 5; // was 4        public CommoditySource spice;        public void initialize() {        super.initialize();        spice = new CommoditySource();        spice.setName("spice");        spice.setOwner(this);        Coordinate2DDiscrete size = (Coordinate2DDiscrete) ((ScapeGraph) getScape()).getExtent();        if (spicePeaks == null) {            spicePeaks = new Coordinate2DDiscrete[2];            //spicePeaks[0] = new Coordinate2DDiscrete((int) (size.getXValue() * .8), (int) (size.getYValue() * .2));             //spicePeaks[1] = new Coordinate2DDiscrete((int) (size.getXValue() * .3), (int) (size.getYValue() * .7));             spicePeaks[0] = new Coordinate2DDiscrete((int) (size.getXValue() * .3), (int) (size.getYValue() * .2));             spicePeaks[1] = new Coordinate2DDiscrete((int) (size.getXValue() * .8), (int) (size.getYValue() * .7));         }        Coordinate2DDiscrete here = (Coordinate2DDiscrete) getCoordinate();        float calc = 0.0f;        for (int i = 0; i < spicePeaks.length; i++) {            calc += Math.exp(-((GAS_SpiceBase) scape.getModel()).getSpiceMoundness() * Math.pow(here.getDistance(spicePeaks[i]), 2));        }        spice.setCapacity((float)(int) Math.round(MAX_SPICE * Math.min(1, calc)));        spice.setQuantity(spice.getCapacity());    }    /**     * Override default cell method so that iterate won't be called on each cell.     *///    public void scapeCreated() {//    }    public double getValue(Object object) {        return (1.0 - (double) ((CommoditySource) object).getQuantity() / (double) SpiceCell.MAX_SPICE);    }    public float getPerceivedValue() {        return spice.getQuantity() * sugar.getQuantity();    }    public float getPotentialValue(float someSugar,                                    float someSpice,				   int sugarMetabolism,                                   int spiceMetabolism) {        return ((someSugar + sugar.getQuantity())/sugarMetabolism) *               ((someSpice + spice.getQuantity())/spiceMetabolism);    }        public CommoditySource getSpice() {        return spice;    }    public void spiceGrowBack1() {        spice.growBack1();    }    public void spiceGrowBackInf() {        spice.growBackInf();    }    public void spiceGrowBackEpsilon(float epsilon) {        spice.growBackEpsilon(epsilon);    }    public float takeSpice() {        return spice.takeQuantity();    }    public float getSpiceQuantity() {        return spice.getQuantity();    }    public void setSpiceMoundness(float moundness) {        ((GAS_SpiceBase) scape.getModel()).setSpiceMoundness(moundness);    }    public String getName() {        return "Spice Cell";    }    public void scapeCreated() {        StatCollector[] stats = {	    new StatCollectorCSAMMVar() {                public double getValue(Object object) {                    return ((SpiceCell) object).getSugarQuantity();                }                public String getName() {                    return "Cell Sugar";                }            },            new StatCollectorCSAMMVar() {                public double getValue(Object object) {                    return ((SpiceCell) object).getSpiceQuantity();                }                public String getName() {                    return "Cell Spice";                }	    }	};            scape.addStatCollectors(stats);    }}
