<?xml version="1.0" encoding="UTF-8"?>
<otrunk id="53d3567e-4d72-47e4-aa18-0f16d0481e4f">
	<imports>
		<import class="org.concord.data.state.OTDataStore" />
		<import class="org.concord.data.state.OTDataChannelDescription" />
		<import class="org.concord.data.state.OTDataField" />
		<import class="org.concord.data.state.OTDataTable" />
		<import class="org.concord.datagraph.state.OTDataGraph" />
		<import class="org.concord.datagraph.state.OTDataAxis" />
		<import class="org.concord.datagraph.state.OTDataGraphable" />
		<import class="org.concord.datagraph.state.OTDataCollector" />
		<import class="org.concord.datagraph.state.OTMultiDataGraph" />
		<import class="org.concord.datagraph.state.OTPluginView" />
		<import class="org.concord.datagraph.state.OTDataBarGraphable" />
		<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.data.OTBiologicaModelDataProducer" />
		<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.otrunk.ui.OTSliderLabel" />
		<import class="org.concord.otrunk.ui.OTText" />
		<import class="org.concord.framework.otrunk.view.OTFrame" />
		<import class="org.concord.framework.otrunk.wrapper.OTObjectSet" />
	</imports>
	<idMap>
		<idMapping local_id="rabbit-species" id="8b5a6e28-684f-4673-b1d2-19a0c9b044be" />
		<idMapping local_id="plant-species" id="6904840c-03f6-4f2d-b822-bc85aebe70db" />
	</idMap>
	<objects>
		<OTSystem>
			<bundles>
				<object refid="8d880970-c22a-11dc-95ff-0800200c9a66" />
				<OTViewBundle>
					<frame>
						<OTFrame useScrollPane="false" openMaximized="false"
							width="900" height="500" />
					</frame>
				</OTViewBundle>
				<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/fast-plants-roots.otml" />
				<OTInclude href="species/varied-rabbits.otml" />
			</includes>
			<root>
				<OTCompoundDoc>
					<bodyText>
						<table>
              <tr>
                <td>
                  <object refid="${env-holder}" />
                </td>
                <td>
                  <table>
                    <tr>
                      <td align="center">Water in the soil</td>
                    </tr>
                    <tr>
                      <td align="center">
                        <br />
                        <br />
                        <object refid="${water-slider}" />
                        <br />
                        <br />
                      </td>
                     </tr>
                  </table>
                  <object refid="${script}"/>
                </td>
              </tr>
            </table>
					</bodyText>
					<documentRefs>
						<OTEnvironmentHolder local_id="env-holder"
							width="590" height="460" toolbar="run,reset,info" useSpeedSlider="false">
							<environment>
								<OTEnvironment local_id="environment"
									environmentWidth="4" environmentHeight="4" showRunButton="false"
									winterLength="5" summerLength="50" showWinterImage="false"
									orgSizeMultiplier="1.5" backgroundImageSrc="images/soil_water_level10.png">
									<world>
										<OTWorld local_id="peasimple_world"
											speciesPath="org/concord/biologica/worlds/peasimple.xml" />
									</world>
									<rules>
		               <OTRule description="roots 1 needs lots of water">
		                  <conditions>
		                    <OTRuleCondition property="roots" equalsValue="1"/>
                        <OTRuleCondition property="water" maxValue="0.9"/>
		                  </conditions>
		                  <consequences>
		                    <!-- chance of survival is 1.0 at watdarn.er > 0.9, 0.8 at water < 0.5, and decreases linearly between -->
		                    <OTRuleConsequence property="chance of survival" ruleScript="var distFrom90 = 0.9-water; var dryness = Math.min(1.0, distFrom90/0.4);  return 1 - (dryness * 0.2)">
		                      <propertyVariables>
		                        <string>water</string>
		                      </propertyVariables>
                        </OTRuleConsequence>
                        <!-- growth rate is 0.04 at water > 0.9, 0 at water < 0.5, and decreases linearly between  -->
                        <OTRuleConsequence property="growth rate" ruleScript="var distFrom90 = 0.9-water; var dryness = Math.min(1.0, distFrom90/0.4); return 0.04 - (dryness * .04)">
                          <propertyVariables>
                            <string>water</string>
                          </propertyVariables>
                        </OTRuleConsequence>
		                  </consequences>
		                </OTRule>
		                <OTRule description="roots 2 needs medium of water">
                      <conditions>
                        <OTRuleCondition property="roots" equalsValue="2"/>
                        <OTRuleCondition property="water" maxValue="0.7"/>
                      </conditions>
                      <consequences>
                        <!-- chance of survival is 1.0 at water > 0.7, 0.8 at water < 0.3, and decreases linearly between -->
                        <OTRuleConsequence property="chance of survival" ruleScript="var distFrom70 = 0.7-water; var dryness = Math.min(1.0, distFrom70/0.4); return 1 - (dryness * 0.2)">
                          <propertyVariables>
                            <string>water</string>
                          </propertyVariables>
                        </OTRuleConsequence>
                        <!-- growth rate is 0.04 at water > 0.7, 0 at water < 0.3, and decreases linearly between  -->
                        <OTRuleConsequence property="growth rate" ruleScript="var distFrom70 = 0.7-water; var dryness = Math.min(1.0, distFrom70/0.4); return 0.04 - (dryness * .04)">
                          <propertyVariables>
                            <string>water</string>
                          </propertyVariables>
                        </OTRuleConsequence>
                      </consequences>
                    </OTRule>
		              </rules>
								</OTEnvironment>
							</environment>
							<addAgentsButtons>
								<OTAddAgentButton text="Add Plant A" limit="-1">
									<species>
										<object refid="${plant-species}" />
									</species>
									<agentTraits>
										<OTAgentTrait property="roots">
											<defaultValues>
												<int>1</int>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="growth rate">
											<defaultValues>
												<float>0.04</float>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="age of maturity">
											<defaultValues>
												<int>15</int>
											</defaultValues>
										</OTAgentTrait>
									</agentTraits>
								</OTAddAgentButton>
								<OTAddAgentButton text="Add Plant B" limit="-1">
									<species>
										<object refid="${plant-species}" />
									</species>
									<agentTraits>
										<OTAgentTrait property="roots">
											<defaultValues>
												<int>2</int>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="growth rate">
											<defaultValues>
												<float>0.04</float>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="age of maturity">
											<defaultValues>
												<int>15</int>
											</defaultValues>
										</OTAgentTrait>
									</agentTraits>
								</OTAddAgentButton>
								<OTAddAgentButton text="Add Plant C" limit="-1">
									<species>
										<object refid="${plant-species}" />
									</species>
									<agentTraits>
										<OTAgentTrait property="roots">
											<defaultValues>
												<int>3</int>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="growth rate">
											<defaultValues>
												<float>0.04</float>
											</defaultValues>
										</OTAgentTrait>
										<OTAgentTrait property="age of maturity">
											<defaultValues>
												<int>15</int>
											</defaultValues>
										</OTAgentTrait>
									</agentTraits>
								</OTAddAgentButton>
							</addAgentsButtons>
							<traitsToShow>
								<string>energy</string>
							</traitsToShow>
						</OTEnvironmentHolder>
						<OTScriptObject local_id="script" name="Setup script">
                  <script>
                    <OTJavascript src="scripts/gradated-sunlight-five-blocks/setup.js">
                      <script>
                        importPackage(Packages.java.lang);
                        importPackage(Packages.java.awt);
                        importPackage(Packages.java.net);
                        importPackage(Packages.org.concord.biologica.environment);
                        
                        var startTime
                        var time
                        var previousTime
                        var isRunning = false
                        var recentlyChangedWater = true      // added a rabbit this past second
              
                        var CHANGE_WATER_RATE = 2;     // seconds before drying out environment
                        var currentWater = 1.0;
                        
                        
                        var baseUrl = otEnvHolder.getOTObjectService().getCodebase(otEnvHolder)

                        function init() {
                          env.removeAllAgents()
                          env.setStepCount(0)
                          envHolder.pauseAction()
                          env.setWrapNorthSouth(false)
                          env.setWrapEastWest(false)
                          
                          
                          var url = new URL(baseUrl, "images/soil_water_level1.png")
                          envHolder.getEnvironmentView().addBackgroundImageUrl(url)
                          url = new URL(baseUrl, "images/soil_water_level2.png")
                          envHolder.getEnvironmentView().addBackgroundImageUrl(url)
                          url = new URL(baseUrl, "images/soil_water_level4.png")
                          envHolder.getEnvironmentView().addBackgroundImageUrl(url)
                          url = new URL(baseUrl, "images/soil_water_level7.png")
                          envHolder.getEnvironmentView().addBackgroundImageUrl(url)
                          url = new URL(baseUrl, "images/soil_water_level10.png")
                          envHolder.getEnvironmentView().addBackgroundImageUrl(url)
                          
                          
                          env.addStepListener(stepListener)
                          env.addChangeListener(resetListener)
                          
                          setup();
  
                          return true;
                        }
                        
                        function setup() {
                          var url = new URL(baseUrl, "images/soil_water_level10.png");
                          envHolder.getEnvironmentView().setBackgroundImageUrl(url);
                          recentlyChangedWater = true
                          time = 0
                          previousTime = 0
                          
                          setPropertyOnEnvironment("water", 1.0)
                          currentWater = 1.0
                          slider.setValue(100)
                        }
                        
              var stepHandler =
              {
                environmentStepped: function()
                {
                  updateTimer();
                  checkWaterUpdating();
                }
              }
              var stepListener = new EnvironmentStepListener(stepHandler)
              
              var resetRunHandler= {
                  environmentChanged: function(evt){
                    if (evt.getType() == EnvironmentChangeEvent.RESET){
                      isRunning = false
                      setup()
                    } else if (evt.getType() == EnvironmentChangeEvent.RUN){
                      isRunning = true
                      startTime = System.currentTimeMillis()
                    } else if (evt.getType() == EnvironmentChangeEvent.PAUSE){
                      previousTime = time;
                    }
                  }
                }
                      
              var resetListener = new EnvironmentChangeListener(resetRunHandler)
              
              function updateTimer(){
                timeSinceLastStart = System.currentTimeMillis() - startTime;
                time =  previousTime + timeSinceLastStart;
              }
              
              function checkWaterUpdating(){
                seconds = Math.floor(time / 1000);
                if (seconds % CHANGE_WATER_RATE == 0){
                  if (!recentlyChangedWater) {
                    recentlyChangedWater = true;
                    makeDrier();
                  }
                } else {
                  recentlyChangedWater = false;
                }
              }
              
              function makeDrier() {
                if (currentWater &gt; 0){
                  currentWater -= 0.02;
                }
                setPropertyOnEnvironment("water", currentWater);
                slider.setValue(currentWater * 100);
                
                if (currentWater &gt; 0.9){
                  var url = new URL(baseUrl, "images/soil_water_level10.png")
                } else if (currentWater &gt; 0.69){
                  var url = new URL(baseUrl, "images/soil_water_level7.png")
                } else if (currentWater &gt; 0.39){
                  var url = new URL(baseUrl, "images/soil_water_level4.png")
                } else if (currentWater == 0.29){
                  var url = new URL(baseUrl, "images/soil_water_level3.png")
                } else {
                  var url = new URL(baseUrl, "images/soil_water_level1.png")
                }
                envHolder.getEnvironmentView().setBackgroundImageUrl(url)
              }
                        
                        function setPropertyOnEnvironment(prop, val){
                            for (var i = 0; i &lt; env.getColumns(); i++) {
                              for (var j = 0; j &lt; env.getRows(); j++) {
                                env.getEnvironmentUnit(i,j).setProperty(prop, new Float(val))
                              }
                            }
                        }

                        function save() {
                          return true;
                        }
                        </script>
                    </OTJavascript>
                  </script>
                  <variables>
                    <OTScriptVariableRealObject name="env">
                      <reference>
                        <object refid="${environment}" />
                      </reference>
                    </OTScriptVariableRealObject>
                    <OTScriptVariableRealObject name="envHolder">
                      <reference>
                        <object refid="${env-holder}" />
                      </reference>
                    </OTScriptVariableRealObject>
                    <OTScriptVariable name="otEnvHolder">
                      <reference>
                        <object refid="${env-holder}" />
                      </reference>
                    </OTScriptVariable>
                    <OTScriptVariableRealObject name="holder">
                      <reference>
                        <object refid="${env-holder}" />
                      </reference>
                    </OTScriptVariableRealObject>
                    <OTScriptVariable name="slider">
                      <reference>
                        <object refid="${water-slider}"/>
                      </reference>
                    </OTScriptVariable>
                  </variables>
                  <scriptState />
                </OTScriptObject>
						<OTSlider local_id="water-slider" minimum="30" maximum="100" value="100" numMajorTicks="2" snapToMarks="false" precision="1" length="200" numTicks="0" showTickMarks="false">
                  <labels>
                    <OTSliderLabel value="30" label="Little water" />
                    <OTSliderLabel value="100" label="Lots of water" />
                  </labels>
             </OTSlider>
					</documentRefs>
				</OTCompoundDoc>
			</root>
		</OTSystem>
	</objects>
</otrunk>

