<?xml version="1.0" encoding="UTF-8"?>
<otrunk id="13d3fb4f-123a-425f-9d60-d84e145c0678">
  <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.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.otml" />
      </includes>
      <root>
        <OTEnvironmentHolder local_id="env-holder" width="730" height="600" toolbar="run">
          <environment>
            <OTEnvironment local_id="environment" width="550" height="500" environmentWidth="10" environmentHeight="10" showRunButton="false" winterLength="15" summerLength="50" orgSizeMultiplier="1.5">
              <world>
                <OTWorld local_id="peasimple_world" speciesPath="org/concord/biologica/worlds/peasimple.xml" />
              </world>
              <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*5) / 100)">
                      <propertyVariables>
                        <string>leaves</string>
                        <string>sunlight</string>
                      </propertyVariables>
                    </OTRuleConsequence>
                  </consequences>
                </OTRule>
                <OTRule description="Health description rule">
                  <conditions>
                    <OTRuleCondition property="Health" maxValue="0.95" />
                  </conditions>
                  <consequences>
                    <OTRuleConsequence property="health" newValueString="Unhealthy" />
                  </consequences>
                </OTRule>
                <OTRule description="Health description rule">
                  <conditions>
                    <OTRuleCondition property="Health" minValue="0.9501" />
                  </conditions>
                  <consequences>
                    <OTRuleConsequence property="health" newValueString="Healthy" />
                  </consequences>
                </OTRule>
                <OTRule description="Offspring rule">
                  <conditions>
                    <OTRuleCondition always="true" />
                  </conditions>
                  <consequences>
                    <OTRuleConsequence property="max offspring" newValueFloat="12.0" />
                    <OTRuleConsequence property="min offspring" newValueFloat="5.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() {
						var world = env.getWorld();
						env.setColumns(15);
						env.setRows(15);
						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(1 - (j/15)))
					        	var percentSunlight = envUnit.getProperty("sunlight")
					        	var r =  (1*percentSunlight.floatValue()) * 0.5
					        	var g = r * 0.5
					        	var b = g * 0.2
					        	var color = new Color(r+0.3, g+0.2, b+0.1)
					        	var terrain = new Terrain(world, "terrain", color)
					        	envUnit.setTerrain(terrain)
				            }
				        }
					env.addStepListener(stepListener)
					return true;
					}
				var winterCounter = 0;
				var waitingFor = "winter"; <!-- We start off waiting for winter... -->	
				var stepHandler = {
					environmentStepped: function(){
						var firstSquare = env.getEnvironmentUnit(0, 0);
						var currentSeason = firstSquare.getProperty("Season");
						if (currentSeason == "winter"){
							if (waitingFor == "winter"){
//								System.out.println ("winter counter = " + winterCounter + ", waiting for " + waitingFor);
								waitingFor = "spring"; <!--  If it's winter we're waiting for spring -->
//								System.out.println ("winter counter = " + winterCounter + ", waiting for " + waitingFor);
								winterCounter ++; <!-- It's the first step of winter, up the count -->
//								System.out.println ("winter counter = " + winterCounter + ", waiting for " + waitingFor);
								
								if (winterCounter == 1){
									JOptionPane.showMessageDialog(null,"Oops!!  " +
									"Winter has come. \n"+
									"All your plants have died. Boo hoo! \n" +
									"But if they've made seeds maybe baby plants will grow. \n" +
									"Click 'OK' and let's see what happens.");

								}
								else if (winterCounter > 1) {
	//							System.out.println ("winter counter = " + winterCounter + ", waiting for " + waitingFor);
			      					
			      					var agents = env.getAllAgents();
			      					var thin = 0
			      					var medium = 0
			      					var bushy = 0
			      					for (var i=0; i &lt; agents.size(); i++){
			      					    var agent = agents.get(i)
			      						var leaves = agent.getPropertyValueAsInteger("leaves")
			      						if (leaves &lt; 4)
			      						     bushy++
			      						else if (leaves &lt; 7)
			      						     medium++
			      						else
			      						     thin++
			      					}
			      				var messageString = "There are:\n  "+thin+" thin plants,\n  "+medium+" medium plants, and\n  "+bushy+" bushy plants.";			      					
//								System.out.println (messageString);
								JOptionPane.showMessageDialog(null,"This is winter number " + winterCounter + ".\n"
									+ messageString);
								}							
							} <!-- end of waiting for winter -->
							return; <!--  waiting for spring: do nothing -->
						} <!--  end of current season winter: start waiting for next winter -->	
						waitingFor = "winter";
						return;
					}		
				}
				
				var stepListener = new EnvironmentStepListener(stepHandler)	
				
				function save() {
					return true;
				}
			</script>
                </OTJavascript>
              </script>
              <variables>
                <OTScriptVariableRealObject name="env">
                  <reference>
                    <object refid="${environment}" />
                  </reference>
                </OTScriptVariableRealObject>
              </variables>
              <scriptState />
            </OTScriptObject>
            <OTScriptObject local_id="setup-species-script" name="Setup species">
              <script>
                <OTJavascript>
                  <script>function init()
	      				{
	      					species.setMutationChance(0)
	      				}</script>
                </OTJavascript>
              </script>
              <variables>
                <OTScriptVariableRealObject name="species">
                  <reference>
                    <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
                  </reference>
                </OTScriptVariableRealObject>
              </variables>
              <scriptState />
            </OTScriptObject>
          </scripts>
          <buttons>
            <!--            <OTScriptButton local_id="add-mixed-ferns-button" text="Scatter mixed seeds">
              <script>
                <OTJavascript local_id="add-mixed.js">
                  <script>function clicked()
      				{
      					var defaultProperties = new Array(new Array("leaves",10))
      					for (var i = 0; i&lt;20; i++){
      						environment.addAgent(species.createAgent(defaultProperties));
      					}
      					
      					defaultProperties = new Array(new Array("leaves",5))
      					for (var i = 0; i&lt;20; i++){
      						environment.addAgent(species.createAgent(defaultProperties));
      					}
      					
      					defaultProperties = new Array(new Array("leaves",1))
      					for (var i = 0; i&lt;20; i++){
      						environment.addAgent(species.createAgent(defaultProperties));
      					}
      				}</script>
                </OTJavascript>
              </script>
              <scriptVariables>
                <OTScriptVariableRealObject name="environment">
                  <reference>
                    <object refid="${environment}" />
                  </reference>
                </OTScriptVariableRealObject>
                <OTScriptVariableRealObject name="species">
                  <reference>
                    <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
                  </reference>
                </OTScriptVariableRealObject>
              </scriptVariables>
            </OTScriptButton>
                <OTScriptButton local_id="survey-button" text="Survey">
              <script>
                <OTJavascript>
                  <script>importPackage(Packages.java.lang);
		     				importPackage(Packages.java.awt);
		     				importPackage(Packages.org.concord.biologica.environment);
		     				importClass(Packages.javax.swing.JOptionPane);
		     				
		     				function clicked()
		      				{
		      					var agents = environment.getAllAgents();
		      					var thin = 0
		      					var medium = 0
		      					var bushy = 0
		      					for (var i=0; i &lt; agents.size(); i++){
		      					    var agent = agents.get(i)
		      						var leaves = agent.getPropertyValueAsInteger("leaves")
		      						if (leaves &lt; 4)
		      						     bushy++
		      						else if (leaves &lt; 7)
		      						     medium++
		      						else
		      						     thin++
		      					}
		      					var messageString = "There are:\n  "+thin+" thin plants,\n  "+medium+" medium plants, and\n  "+bushy+" bushy plants.";
		      					
								JOptionPane.showMessageDialog(null, messageString);
		      				}</script>
                </OTJavascript>
              </script>
              <scriptVariables>
                <OTScriptVariableRealObject name="environment">
                  <reference>
                    <object refid="${environment}" />
                  </reference>
                </OTScriptVariableRealObject>
              </scriptVariables>
            </OTScriptButton> -->
            <OTScriptButton local_id="clear-button" text="Reset">
              <script>
                <OTJavascript>
                  <script>importPackage(Packages.java.lang);
	     				importPackage(Packages.java.awt);
	     				importPackage(Packages.org.concord.biologica.environment);
	     				
	     				function clicked()
	      				{
	      					environment.removeAllAgents();
	      					environment.setStepCount(0);
	      					holder.pauseAction();
	      					var world = environment.getWorld()
	      					for (var i = 0; i &lt; 15; i++) {
						        for (var j = 0; j &lt; 15; j++) {
						        	var envUnit = new EnvironmentUnit(environment, null, i, j)
						        	environment.setEnvironmentUnit(envUnit, i, j)
						        	envUnit.setProperty("sunlight", new Float(1 - (j/15)))
						        	
						        	var percentSunlight = envUnit.getProperty("sunlight")
						        	var r =  (1*percentSunlight.floatValue()) * 0.5
						        	var g = r * 0.5
						        	var b = g * 0.2
						        	var color = new Color(r+0.3, g+0.2, b+0.1)
						        	var terrain = new Terrain(world, "terrain", color)
						        	envUnit.setTerrain(terrain)
					            }
					        }
	      				}</script>
                </OTJavascript>
              </script>
              <scriptVariables>
                <OTScriptVariableRealObject name="environment">
                  <reference>
                    <object refid="${environment}" />
                  </reference>
                </OTScriptVariableRealObject>
                <OTScriptVariableRealObject name="holder">
                  <reference>
                    <object refid="${env-holder}" />
                  </reference>
                </OTScriptVariableRealObject>
              </scriptVariables>
            </OTScriptButton>
          </buttons>
          <traitsToShow>
            <string>type of leaves</string>
            <string>Health</string>
            <string>max offspring</string>
          </traitsToShow>
          <addAgentsButtons>
            <OTAddAgentButton text="Add Plant A">
              <species>
                <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
              </species>
              <agentTraits>
                <OTAgentTrait property="leaves">
                  <defaultValues>
                    <int>5</int>
                  </defaultValues>
                </OTAgentTrait>
              </agentTraits>
            </OTAddAgentButton>
            <OTAddAgentButton text="Add Plant B">
              <species>
                <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
              </species>
              <agentTraits>
                <OTAgentTrait property="leaves">
                  <defaultValues>
                    <int>9</int>
                  </defaultValues>
                </OTAgentTrait>
              </agentTraits>
            </OTAddAgentButton>
            <OTAddAgentButton text="Add Plant C">
              <species>
                <object refid="f5732706-c9c2-4e29-8c99-4c668dda89b0" />
              </species>
              <agentTraits>
                <OTAgentTrait property="leaves">
                  <defaultValues>
                    <int>1</int>
                  </defaultValues>
                </OTAgentTrait>
              </agentTraits>
            </OTAddAgentButton>
          </addAgentsButtons>
        </OTEnvironmentHolder>
      </root>
    </OTSystem>
  </objects>
</otrunk>

