/* * (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. */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.*;/** * @history 19990624 AAL changed variables from private to protected so SpiceCell could inherit * seperated peak creation from initialize() * @history 19990629 AAL removed a variety of "sugar"related methods and *          attributes, and put them in new class CommoditySource */public class SugarCell extends HostCell {    public final static Rule SUGAR_GROW_BACK_1_RULE = new Rule("Sugar-Grow Back 1") {        public void execute(Agent agent) {            ((SugarCell) agent).sugarGrowBack1();        }    };    public final static Rule SUGAR_GROW_BACK_INF_RULE = new Rule("Sugar-Grow Back Infinite") {        public void execute(Agent agent) {            ((SugarCell) agent).sugarGrowBackInf();        }    };        public static Coordinate2DDiscrete[] sugarPeaks;     public CommoditySource sugar;    public static int MAX_SUGAR = 4; // was 4        public void initialize() {        super.initialize();        sugar = new CommoditySource();        sugar.setName("sugar");        sugar.setOwner(this);        if (sugarPeaks == null) {	        Coordinate2DDiscrete size = (Coordinate2DDiscrete) ((ScapeGraph) getScape()).getExtent();            sugarPeaks = new Coordinate2DDiscrete[2];            sugarPeaks[0] = new Coordinate2DDiscrete((int) (size.getXValue() * .8), (int) (size.getYValue() * .2));             sugarPeaks[1] = new Coordinate2DDiscrete((int) (size.getXValue() * .3), (int) (size.getYValue() * .7));         }        Coordinate2DDiscrete here = (Coordinate2DDiscrete) getCoordinate();        float calc = 0.0f;        for (int i = 0; i < sugarPeaks.length; i++) {            calc += Math.exp(-((GAS_Base) scape.getModel()).getSugarMoundness() * Math.pow(here.getDistance(sugarPeaks[i]), 2));        }        sugar.setCapacity((float)(int) Math.round(MAX_SUGAR * Math.min(1, calc)));        sugar.setQuantity(sugar.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) SugarCell.MAX_SUGAR);    }    public float getPerceivedValue() {        return sugar.getQuantity();    }        public Color getColor() {        return Color.black;    }    public String getName() {        return "Sugar Cell";    }    public CommoditySource getSugar() {        return sugar;    }    public float getSugarAmount() {        return sugar.getQuantity();    }    public void setSugarAmount(float sugarAmout) {        sugar.setQuantity(sugarAmout);    }    public void sugarGrowBack1() {        sugar.growBack1();    }    public void sugarGrowBackInf() {        sugar.growBackInf();    }    public void sugarGrowBackEpsilon(float epsilon) {        sugar.growBackEpsilon(epsilon);    }    public float takeSugar() {        return sugar.takeQuantity();    }    public float getSugarQuantity() {        return sugar.getQuantity();    }    public void setSugarMoundness(float moundness) {        ((GAS_Base) scape.getModel()).setSugarMoundness(moundness);    }}
