<?xml version="1.0" encoding="UTF-8"?>
<otrunk id="fa2d7fe7-4fe8-4fcd-b574-bb9fae224900">
  <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.OTText" />
    <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.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-roots.otml" />
      </includes>
      <root>
        <OTCompoundDoc>
          <bodyText>
            <table>
              <tr>
                <td>
                  <object refid="${env-holder}" />
                </td>
                <td>
                  <table border="1">
                    <tr>
                      <td>
                        Money remaining:
                        <br />
                        <object refid="${money-text}" viewid="c93a7460-e18a-11dc-95ff-0800200c9a66" />
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </bodyText>
          <documentRefs>
            <OTText local_id="money-text" text="$20" fontSize="25" />
            <OTEnvironmentHolder local_id="env-holder" width="590" height="460" toolbar="run,cursor,reset" useSpeedSlider="true">
              <environment>
                <OTEnvironment local_id="environment" environmentWidth="10" environmentHeight="4" showRunButton="true" winterLength="15" summerLength="50" orgSizeMultiplier="1.5" backgroundImageSrc="images/water10levels.png">
                  <world>
                    <OTWorld local_id="peasimple_world" speciesPath="org/concord/biologica/worlds/peasimple.xml" />
                  </world>
                  <rules>
                    <OTRule description="Health depends on roots and water">
                      <conditions>
                        <OTRuleCondition always="true" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="Health" ruleScript="                        var diff = Math.abs(size_of_roots - (water*10));                       var healthAtOneUnitDiff = 0.983;                        var a = 0.2 - 0.25*healthAtOneUnitDiff;          var b =1.25*healthAtOneUnitDiff -1.2;        var c = 1;        var h = a*diff*diff + b*diff + c;        if (h &gt; 0) return h;        else return 0;">
                          <propertyVariables>
                            <string>size of roots</string>
                            <string>water</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 plants flower in summer!">
                      <conditions>
                        <OTRuleCondition property="Health" minValue="0.99" maxValue="1.01" />
                        <OTRuleCondition property="Season" equals="summer" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="Chance of flowering" newValueFloat="1.0" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Some withered plants die in summer">
                      <conditions>
                        <OTRuleCondition property="Health" minValue="0.95" maxValue="0.99" />
                        <OTRuleCondition property="Season" equals="summer" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="true" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Withered plants immortal in spring">
                      <conditions>
                        <OTRuleCondition property="Health" minValue="0.85" maxValue="0.99" />
                        <OTRuleCondition property="Season" equals="spring" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="true" />
                      </consequences>
                    </OTRule>
                    <OTRule description="Withered plants die in fall">
                      <conditions>
                        <OTRuleCondition property="Health" minValue="0.85" maxValue="0.99" />
                        <OTRuleCondition property="Season" equals="fall" />
                      </conditions>
                      <consequences>
                        <OTRuleConsequence property="is immortal" newValueBoolean="false" />
                      </consequences>
                    </OTRule>
                  </rules>
                  <barriers>
                    <OTPlacement x="0" y="36" width="102" height="5" />
                  </barriers>
                </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();
								var columns = env.getColumns()
								var rows = env.getRows()
								env.setWrapNorthSouth(false);
								env.setWrapEastWest(false);
								for (var i = 0; i &lt; columns; i++) {
							        for (var j = 0; j &lt; rows; j++) {
							        	var envUnit = new EnvironmentUnit(env, null, i, j)
							        	env.setEnvironmentUnit(envUnit, i, j)
							        	envUnit.setProperty("water", new Float((i+1)/10))
							        	var color = new Color(0.3, 0.2, 0.1)
							        	var terrain = new Terrain(world, "terrain", color)
							        	envUnit.setTerrain(terrain)
						            }
						        }
							env.addStepListener(stepListener)
							return true;
							}
						var winterCounter = 0;
						var waitingFor = "winter";
						var stepHandler = {
							environmentStepped: function(){
								var firstSquare = env.getEnvironmentUnit(0, 0);
								var currentSeason = firstSquare.getProperty("Season");
								if (waitingFor == currentSeason) {
									if (waitingFor == "winter"){
										winterCounter ++; 								
										waitingFor = "spring";
										envHolder.snowCover(1.0);	
									return;					
									}
									if (waitingFor == "spring"){
										waitingFor = "winter";
										envHolder.snowCover(0.0);
									}
								} 
								return;
							}		
						}
						
						var stepListener = new EnvironmentStepListener(stepHandler)	
						
						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="money-script" name="Money script">
                  <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);
							
							var INITIAL_MONEY = 20
							var money = INITIAL_MONEY
							  			
							function init() {
								reset()
								env.addChangeListener(changeListener)
								return true;
							}
							
							var changeHandler = {
								environmentChanged: function(evt){
									if (evt.getType() == EnvironmentChangeEvent.USER_ADDED_AGENTS){
										money = money - 1
										moneyText.setText("$"+money)
									}
									
									if (evt.getType() == EnvironmentChangeEvent.RESET){
										reset()
									}
								}		
							}
						
							var changeListener = new EnvironmentChangeListener(changeHandler)	
							
							function reset() {
								money = INITIAL_MONEY
								moneyText.setText("$"+money)
							}
						
							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>
                    <OTScriptVariable name="moneyText">
                      <reference>
                        <object refid="${money-text}" />
                      </reference>
                    </OTScriptVariable>
                  </variables>
                  <scriptState />
                </OTScriptObject>
                <OTScriptObject local_id="setup-species-script" name="Setup species">
                  <script>
                    <OTJavascript>
                      <script>function init()
			      				{
			      					species.setMutationChance(0.35)
			      				}</script>
                    </OTJavascript>
                  </script>
                  <variables>
                    <OTScriptVariableRealObject name="species">
                      <reference>
                        <object refid="ad89cbed-b580-4345-9896-5af9a9588fc2" />
                      </reference>
                    </OTScriptVariableRealObject>
                  </variables>
                  <scriptState />
                </OTScriptObject>
              </scripts>
              <traitsToShow>
                <string>size of roots</string>
                <string>size of leaves</string>
              </traitsToShow>
              <addAgentsButtons>
                <OTAddAgentButton text="Add Plant Z" limit="15">
                  <species>
                    <object refid="ad89cbed-b580-4345-9896-5af9a9588fc2" />
                  </species>
                  <agentTraits>
                    <OTAgentTrait property="size of leaves">
                      <defaultValues>
                        <float>5.0</float>
                      </defaultValues>
                    </OTAgentTrait>
                    <OTAgentTrait property="size of roots">
                      <defaultValues>
                        <float>1.0</float>
                      </defaultValues>
                    </OTAgentTrait>
                  </agentTraits>
                </OTAddAgentButton>
              </addAgentsButtons>
            </OTEnvironmentHolder>
          </documentRefs>
        </OTCompoundDoc>
      </root>
    </OTSystem>
  </objects>
</otrunk>

