<?xml version="1.0" encoding="UTF-8"?>
<otrunk id="5f772b8a-e74a-4e0b-885c-af0b348fd6cb">
  <imports>
    <import class="org.concord.otrunk.OTIncludeRootObject" />
    <import class="org.concord.otrunk.OTInclude" />
    <import class="org.concord.otrunk.OTSystem" />
    <import class="org.concord.otrunk.biologica.OTOrganismPlacement" />
    <import class="org.concord.otrunk.biologica.OTEnvironment" />
    <import class="org.concord.otrunk.biologica.OTOrganism" />
    <import class="org.concord.otrunk.biologica.OTWorld" />
    <import class="org.concord.otrunk.biologica.agent.OTAgentSpecies" />
    <import class="org.concord.otrunk.biologica.agent.OTAgentTrait" />
    <import class="org.concord.otrunk.biologica.agent.OTAgentPropertyImageMap" />
    <import class="org.concord.otrunk.biologica.environment.OTAddAgentButton" />
    <import class="org.concord.otrunk.biologica.environment.OTEnvironmentHolder" />
    <import class="org.concord.otrunk.biologica.rules.OTRule" />
    <import class="org.concord.otrunk.biologica.rules.OTRuleCondition" />
    <import class="org.concord.otrunk.biologica.rules.OTRuleConsequence" />
    <import class="org.concord.otrunk.ui.OTCurriculumUnit" />
    <import class="org.concord.otrunk.ui.OTPlacement" />
    <import class="org.concord.otrunk.ui.snapshot.OTSnapshot" />
    <import class="org.concord.otrunk.ui.snapshot.OTSnapshotButton" />
    <import class="org.concord.otrunk.ui.snapshot.OTSnapshotAlbum" />
    <import class="org.concord.otrunk.ui.snapshot.OTSnapshotChooser" />
    <import class="org.concord.otrunk.view.OTViewEntry" />
    <import class="org.concord.otrunk.view.OTViewBundle" />
    <import class="org.concord.otrunk.view.OTViewMode" />
    <import class="org.concord.otrunk.view.document.OTCompoundDoc" />
    <import class="org.concord.otrunk.script.ui.OTScriptObject" />
    <import class="org.concord.otrunk.script.js.OTJavascript" />
    <import class="org.concord.otrunk.script.OTScriptEngineBundle" />
    <import class="org.concord.otrunk.script.OTScriptEngineEntry" />
    <import class="org.concord.otrunk.script.ui.OTScriptVariable" />
    <import class="org.concord.otrunk.script.ui.OTScriptVariableComponent" />
    <import class="org.concord.otrunk.script.ui.OTScriptVariableRealObject" />
    <import class="org.concord.otrunk.script.ui.OTScriptVariableView" />
    <import class="org.concord.otrunk.script.ui.OTScriptButton" />
    <import class="org.concord.otrunk.ui.OTSlider" />
    <import class="org.concord.framework.otrunk.view.OTFrame" />
  </imports>
  <objects>
    <OTSystem>
      <bundles>
        <object refid="8d880970-c22a-11dc-95ff-0800200c9a66" />
        <OTScriptEngineBundle>
          <engines>
            <OTScriptEngineEntry objectClass="org.concord.otrunk.script.js.OTJavascript" engineClass="org.concord.otrunk.script.js.OTJavascriptEngine" />
          </engines>
        </OTScriptEngineBundle>
      </bundles>
      <includes>
        <OTInclude href="../global-imports/er-view-bundle.otml" />
        <OTInclude href="species/annual-varied-plants.otml" />
      </includes>
      <root>
        <OTCompoundDoc local_id="mountain-model">
          <bodyText>
            <table>
              <tr>
                <td>
                  <object refid="${env-holder}" />
                </td>
                <td>
                  <object refid="${slider}" />
                </td>
              </tr>
            </table>
            <br />
            <object refid="${slider-script}" />
          </bodyText>
          <documentRefs>
            <OTEnvironmentHolder local_id="env-holder" toolbar="run,cursor,reset" useSpeedSlider="true" width="590" height="460">
              <environment>
                <OTEnvironment local_id="environment" environmentWidth="15" environmentHeight="15" showRunButton="false" winterLength="2" summerLength="80" orgSizeMultiplier="1.5" backgroundImageSrc="images/mountains1.png">
                  <world>
                    <OTWorld local_id="peasimple_world" speciesPath="org/concord/biologica/worlds/peasimple.xml" />
                  </world>
                  <barriers>
                    <OTPlacement x="39" y="0" width="42" height="13" />
                    <OTPlacement x="42" y="13" width="39" height="5" />
                    <OTPlacement x="39" y="18" width="47" height="14" />
                    <OTPlacement x="45" y="32" width="39" height="7" />
                    <OTPlacement x="43" y="39" width="47" height="6" />
                    <OTPlacement x="45" y="45" width="49" height="13" />
                    <OTPlacement x="50" y="58" width="44" height="7" />
                    <OTPlacement x="47" y="65" width="48" height="7" />
                    <OTPlacement x="52" y="72" width="47" height="11" />
                    <OTPlacement x="55" y="83" width="47" height="12" />
                    <OTPlacement x="60" y="95" width="44" height="7" />
                    <OTPlacement x="57" y="102" width="47" height="9" />
                    <OTPlacement x="61" y="111" width="44" height="16" />
                    <OTPlacement x="63" y="127" width="47" height="10" />
                    <OTPlacement x="66" y="137" width="48" height="7" />
                    <OTPlacement x="71" y="144" width="47" height="6" />
                  </barriers>
                  <rules>
                    <OTRule description="Health depends on leaves and sunlight">
                      <conditions>
                        <OTRuleCondition always="true" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="Health" ruleScript="var diff = Math.abs(leaves - (sunlight*10)); return 1.0 - ((diff*9) / 100)">
                          <propertyVariables>
                            <string>leaves</string>
                            <string>sunlight</string>
                          </propertyVariables>
                        </OTRuleConsequence>
                      </consequences>
                    </OTRule>
                    <OTRule description="No flowers on withered plants!">
                      <conditions>
                        <OTRuleCondition property="Health" minValue="0.0" maxValue="0.99" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="Chance of flowering" newValueFloat="0.0" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Healthy and withered plants live through spring">
                      <conditions>
                        <OTRuleCondition property="Season" equals="spring" />
                        <OTRuleCondition property="Health" minValue="0.8" maxValue="1.1" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="true" />
                        <OTRuleConsequence property="Days to seed" newValueFloat="100.0" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Healthy plants live through summer and flower">
                      <conditions>
                        <OTRuleCondition property="Season" equals="summer" />
                        <OTRuleCondition property="Health" minValue="0.98" maxValue="1.1" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="true" />
                        <OTRuleConsequence property="Chance of flowering" newValueFloat="0.2" />
                        <OTRuleConsequence property="Days to seed" newValueFloat="100.0" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Plants die in fall">
                      <conditions>
                        <OTRuleCondition property="Season" equals="fall" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="false" />
                        <OTRuleConsequence property="Days to seed" newValueFloat="8.0" />
                      </consequences>
                    </OTRule>
                    <OTRule description="All plants die in winter">
                      <conditions>
                        <OTRuleCondition property="Season" equals="winter" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="Health" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Reduce number of offspring">
                      <conditions>
                        <OTRuleCondition always="true" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="min offspring" newValueFloat="2.0" />
                        <OTRuleConsequence property="max offspring" newValueFloat="4.0" />
                      </consequences>
                    </OTRule>
                  </rules>
                </OTEnvironment>
              </environment>
              <scripts>
                <OTScriptObject local_id="setup-script" name="Setup world">
                  <script>
                    <OTJavascript src="scripts/gradated-sunlight/setup.js">
                      <script>importPackage(Packages.java.lang);
						importPackage(Packages.java.awt);
						importPackage(Packages.org.concord.biologica.environment);
						importClass(Packages.javax.swing.JOptionPane);
						  			
						function init() {
							env.removeAllAgents()
							env.setStepCount(0)
							envHolder.pauseAction()
							var world = env.getWorld();
							env.setWrapNorthSouth(false);
							env.setWrapEastWest(false);
							for (var i = 0; i &lt; 15; i++) {
						        for (var j = 0; j &lt; 15; j++) {
						        	var envUnit = new EnvironmentUnit(env, null, i, j)
						        	env.setEnvironmentUnit(envUnit, i, j)
						        	envUnit.setProperty("sunlight", new Float(0.5))
						        	var color = new Color(0.3, 0.2, 0.1)
					        		var terrain = new Terrain(world, "terrain", color)
					        		envUnit.setTerrain(terrain)
					            }
					        }
						return true;
						}
					
					function save() {
						return true;
					}</script>
                      <scripts>
                        <OTJavascript src="scripts/common/message.js" />
                      </scripts>
                    </OTJavascript>
                  </script>
                  <variables>
                    <OTScriptVariableRealObject name="env">
                      <reference>
                        <object refid="${environment}" />
                      </reference>
                    </OTScriptVariableRealObject>
                    <OTScriptVariableRealObject name="envHolder">
                      <reference>
                        <object refid="${env-holder}" />
                      </reference>
                    </OTScriptVariableRealObject>
                  </variables>
                  <scriptState />
                </OTScriptObject>
                <OTScriptObject local_id="setup-species-script" name="Setup species">
                  <script>
                    <OTJavascript>
                      <script>function init()
		      				{
		      					species.setMutationChance(0.2)
		      				}</script>
                    </OTJavascript>
                  </script>
                  <variables>
                    <OTScriptVariableRealObject name="species">
                      <reference>
                        <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
                      </reference>
                    </OTScriptVariableRealObject>
                  </variables>
                  <scriptState />
                </OTScriptObject>
              </scripts>
              <traitsToShow>
                <string>leaves</string>
              </traitsToShow>
              <addAgentsButtons>
                <OTAddAgentButton text="Add Plant A" quantity="20" click="false">
                  <species>
                    <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
                  </species>
                  <agentTraits>
                    <OTAgentTrait property="roots">
                      <defaultValues>
                        <float>5.0</float>
                      </defaultValues>
                    </OTAgentTrait>
                    <OTAgentTrait property="leaves">
                      <defaultValues>
                        <float>5.0</float>
                      </defaultValues>
                    </OTAgentTrait>
                    <OTAgentTrait property="min offspring distance">
                      <defaultValues>
                        <int>5</int>
                      </defaultValues>
                    </OTAgentTrait>
                    <OTAgentTrait property="max offspring distance">
                      <defaultValues>
                        <int>20</int>
                      </defaultValues>
                    </OTAgentTrait>
                  </agentTraits>
                </OTAddAgentButton>
              </addAgentsButtons>
            </OTEnvironmentHolder>
            <OTScriptObject local_id="slider-script" name="Script object">
              <script>
                <OTJavascript>
                  <script>importPackage(Packages.java.lang);
importPackage(Packages.java.net);
importPackage(Packages.javax.swing);
importPackage(Packages.org.concord.framework.otrunk);
importPackage(Packages.org.concord.biologica.environment);

var changeSpeed = 0
var mountainHeight = 0
var currentUrl;
var env = envHolder.getEnvironment()
             
function init() {
	env.addStepListener(stepListener)
	slider.addOTChangeListener(sliderChangeListener)
	env.addChangeListener(resetListener)
}

var stepHandler = {
	environmentStepped: function(){
		mountainHeight += changeSpeed
		checkMountainHeight()
	}		
}
			
var stepListener = new EnvironmentStepListener(stepHandler)	

function checkMountainHeight() {
	 var baseUrl = otEnvHolder.getOTObjectService().getCodebase(otEnvHolder)
	 
	 if (currentUrl == null){
	 	currentUrl = new URL(baseUrl, "images/mountains1.png")
	 }
	 
     var url
     if (mountainHeight &lt; 100){
     	url = new URL(baseUrl, "images/mountains1.png")
     	setSunlight(0.5,0.5)
     } else if (mountainHeight &lt; 200){
     	url = new URL(baseUrl, "images/mountains2.png")
     	setSunlight(0.4,0.6)
     } else if (mountainHeight &lt; 300){
     	url = new URL(baseUrl, "images/mountains3.png")
     	setSunlight(0.3,0.7)
     } else if (mountainHeight &lt; 400){
     	url = new URL(baseUrl, "images/mountains4.png")
     	setSunlight(0.2,0.8)
     } else {
     	url = new URL(baseUrl, "images/mountains5.png")
     	setSunlight(0.1,0.9)
     }
     if (!url.toExternalForm().equalsIgnoreCase(currentUrl.toExternalForm())) {
     	envHolder.getEnvironmentView().setBackgroundImageUrl(url)
     	currentUrl = url;
     }
}

var sliderChangeHandler =
{
    stateChanged: function(evt)
    {
    	var val = evt.getValue()
        if (val == 0){
        	changeSpeed = 0
        } else if (val == 1){
        	changeSpeed = 0.41
        } else if (val == 2){
        	changeSpeed = 0.61
        } else if (val == 3){
        	changeSpeed = 1.25
        } else if (val == 4){
        	changeSpeed = 2
        }
    }

}
var sliderChangeListener = new OTChangeListener(sliderChangeHandler)



function setSunlight(left, right){
	               	for (var i = 0; i &lt; 15; i++) {
			var sunlight
			if (i &lt; 8)
				sunlight = left
			else
				sunlight = right
			for (var j = 0; j &lt; 15; j++) {
				env.getEnvironmentUnit(i,j).setProperty("sunlight", new Float(sunlight))
			}
		}
}

var resetHandler= {
	environmentChanged: function(evt){
		if (evt.getType() == EnvironmentChangeEvent.RESET){
			mountainHeight = 0
			checkMountainHeight()
		}
	}
}
			
var resetListener = new EnvironmentChangeListener(resetHandler)

function save() {
  return true;
}</script>
                  <scripts>
                    <OTJavascript src="scripts/common/message.js" />
                  </scripts>
                </OTJavascript>
              </script>
              <variables>
                <OTScriptVariable name="otEnvHolder">
                  <reference>
                    <object refid="${env-holder}" />
                  </reference>
                </OTScriptVariable>
                <OTScriptVariableRealObject name="envHolder">
                  <reference>
                    <object refid="${env-holder}" />
                  </reference>
                </OTScriptVariableRealObject>
                <OTScriptVariable name="slider">
                  <reference>
                    <OTSlider local_id="slider" minimum="0.0" maximum="4.0" snapToMarks="true" precision="1.0" numMajorTicks="4" />
                  </reference>
                </OTScriptVariable>
              </variables>
            </OTScriptObject>
          </documentRefs>
        </OTCompoundDoc>
      </root>
    </OTSystem>
  </objects>
</otrunk>

