<?xml version="1.0" encoding="utf-8" ?>
<ThinkTrees>

	<ThinkTreeDef>
		<defName>RangedAnimal</defName>
		<thinkRoot Class="ThinkNode_Priority">
      <subNodes>
        <!-- Keep lying down if we have to -->
				<li Class="ThinkNode_ConditionalMustKeepLyingDown">
					<subNodes>
            <li Class="JobGiver_KeepLyingDown" />
          </subNodes>
        </li>
        
        <li Class="ThinkNode_Subtree">
          <treeDef>Downed</treeDef>
        </li>
        <li Class="ThinkNode_Subtree">
          <treeDef>BurningResponse</treeDef>
        </li>
        <li Class="ThinkNode_Subtree">
          <treeDef>MentalStateCritical</treeDef>
        </li>
        
        <!-- React to close melee threat -->
				<li Class="AnimalRangedVerbsUnlocker.JobGiver_ReactToCloseMeleeThreatWithRanged" />

        <!-- Wild animals dig out if no path to map edge and starving -->
        <li Class="ThinkNode_ConditionalHasFaction">
          <invert>true</invert>
          <subNodes>
            <li Class="ThinkNode_ConditionalStarving">
              <subNodes>
                <li Class="ThinkNode_ConditionalBodySize">
                  <min>0.7</min>
                  <subNodes>
                    <li Class="ThinkNode_Subtree">
                      <treeDef>DigOutIfCannotReachMapEdge</treeDef>
                    </li>
                  </subNodes>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        
        <!-- Leave if timed out -->
        <li Class="ThinkNode_ConditionalExitTimedOut">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>Misc</tagToGive>
              <subNodes>
                <li Class="JobGiver_ExitMapRandom">
                  <defaultLocomotion>Walk</defaultLocomotion>     
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>

        <!-- Mental state non critical -->
        <li Class="ThinkNode_Subtree">
          <treeDef>MentalStateNonCritical</treeDef>
        </li>

        <!-- Forced goto -->
        <li Class="ThinkNode_ConditionalForcedGoto">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>Misc</tagToGive>
              <subNodes>
                <li Class="JobGiver_ForcedGoto" />
              </subNodes>
            </li>
          </subNodes>
        </li>
        
				<!-- Lord directives -->
        <li Class="ThinkNode_Subtree">
          <treeDef>LordDuty</treeDef>
        </li>
        
        <!-- Insertion hook for modders -->
        <li Class="ThinkNode_SubtreesByTag">
          <insertTag>Animal_PreMain</insertTag>
        </li>

        <!-- Wild animals leave map in some conditions -->
        <li Class="ThinkNode_ConditionalHasFaction">
          <invert>true</invert>
          <subNodes>
            <li Class="ThinkNode_Subtree">
              <treeDef>LeaveIfWrongSeason</treeDef>
            </li>
            <li Class="ThinkNode_Subtree">
              <treeDef>LeaveIfStarving</treeDef>
            </li>
          </subNodes>
        </li>

        <!-- Lay eggs you need to -->
        <li Class="JobGiver_LayEgg" />

        <!-- Tame animal -->
        <li Class="ThinkNode_ConditionalColonyFaction">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>TrainedAnimalBehavior</tagToGive>
              <subNodes>
                <!-- Trained behavior: obedience: Follow and defend master -->
                <li Class="ThinkNode_ConditionalTrainableCompleted">
                  <trainable>Obedience</trainable>
                  <subNodes>
                    <li Class="ThinkNode_ConditionalShouldFollowMaster" >
                      <subNodes>
                        <li Class="AnimalRangedVerbsUnlocker.JobGiver_AIDefendMasterRanged">
                          <attackMeleeThreatEvenIfNotHostile>true</attackMeleeThreatEvenIfNotHostile>
                        </li>
                        <li Class="JobGiver_AIFollowMaster"/>
                        <li Class="JobGiver_WanderNearMaster" />
                      </subNodes>
                    </li>
                  </subNodes>
                </li>

                <!-- Trained behavior: Rescue-->
                <li Class="ThinkNode_ConditionalTrainableCompleted">
                  <trainable>Rescue</trainable>
                  <subNodes>
                    <li Class="JobGiver_RescueNearby">
                      <radius>30</radius>
                    </li>
                  </subNodes>
                </li>
              </subNodes>
            </li>

            <!-- Take care of critical needs (below rescue - so heroic!)-->
            
            <li Class="ThinkNode_Tagger">
              <tagToGive>RestingForMedicalReasons</tagToGive>
              <subNodes>
                <li Class="JobGiver_PatientGoToBed" />
              </subNodes>
            </li>
            
            <li Class="JobGiver_SeekAllowedArea" />
            <li Class="JobGiver_SeekSafeTemperature" />
            
          </subNodes>
        </li>
        
				<!-- Eat random things out of curiosity -->
				<li Class="ThinkNode_ChancePerHour_Constant">
					<mtbHours>600</mtbHours>
					<subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>SatisfyingNeeds</tagToGive>
              <subNodes>
                <li Class="JobGiver_EatRandom"/>
              </subNodes>
            </li>
					</subNodes>
				</li>
				
        <!-- Satisfy basic needs -->
        <li Class="ThinkNode_Subtree">
          <treeDef>SatisfyBasicNeeds</treeDef>
        </li>

        <!-- Tame animal: do useful things for the colony-->
        <li Class="ThinkNode_ConditionalHasFaction">
          <subNodes>
            
            <!-- Try to mate -->
            <li Class="ThinkNode_ChancePerHour_Mate">
              <subNodes>
                <li Class="ThinkNode_Tagger">
                  <tagToGive>SatisfyingNeeds</tagToGive>
                  <subNodes>
                    <li Class="JobGiver_Mate" />
                  </subNodes>
                </li>
              </subNodes>
            </li>

            <!-- Nuzzle randoms -->
            <li Class="ThinkNode_ChancePerHour_Nuzzle">
              <subNodes>
                <li Class="ThinkNode_Tagger">
                  <tagToGive>Misc</tagToGive>
                  <subNodes>
                    <li Class="JobGiver_Nuzzle" />
                  </subNodes>
                </li>
              </subNodes>
            </li>

            <!-- Trained behavior: Haul-->
            <li Class="ThinkNode_ChancePerHour_Constant">
              <mtbHours>1.5</mtbHours>
              <subNodes>
                <li Class="ThinkNode_ConditionalTrainableCompleted">
                  <trainable>Haul</trainable>
                  <subNodes>
                    <li Class="ThinkNode_Tagger">
                      <tagToGive>TrainedAnimalBehavior</tagToGive>
                      <subNodes>
                        <li Class="JobGiver_Haul" />
                      </subNodes>
                    </li>
                  </subNodes>
                </li>
              </subNodes>
            </li>

            <!-- Join auto joinable caravan -->
            <li Class="ThinkNode_Subtree">
              <treeDef>JoinAutoJoinableCaravan</treeDef>
            </li>
          </subNodes>
        </li>

        <!-- Insertion hook for modders -->
        <li Class="ThinkNode_SubtreesByTag">
          <insertTag>Animal_PreWander</insertTag>
        </li>
        
        <!-- Tame animal: wander colony -->
        <li Class="ThinkNode_ConditionalColonyFaction">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>Idle</tagToGive>
              <subNodes>
                <li Class="JobGiver_WanderColony">
                  <maxDanger>None</maxDanger>
                  <ticksBetweenWandersRange>
                    <min>120</min>
                    <max>240</max>
                  </ticksBetweenWandersRange>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        
        <!-- Of neutral faction: rest and then exit the map -->
        <li Class="ThinkNode_ConditionalNeutralFaction">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>RestingForMedicalReasons</tagToGive>
              <subNodes>
                <li Class="JobGiver_PatientGoToBed" />
              </subNodes>
            </li>
            
            <li Class="ThinkNode_Tagger">
              <tagToGive>Misc</tagToGive>
              <subNodes>
                <li Class="JobGiver_ExitMapBest">
                  <defaultLocomotion>Walk</defaultLocomotion>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        
        <!-- Wander -->
        <li Class="ThinkNode_Tagger">
          <tagToGive>Idle</tagToGive>
          <subNodes>
            <li Class="ThinkNode_ConditionalHerdAnimal">
              <subNodes>
                <li Class="JobGiver_WanderHerd">
                  <maxDanger>Deadly</maxDanger>
                  <ticksBetweenWandersRange>
                    <min>120</min>
                    <max>240</max>
                  </ticksBetweenWandersRange>
                </li>
              </subNodes>
            </li>
            
            <li Class="JobGiver_WanderAnywhere">
              <maxDanger>Deadly</maxDanger>
              <ticksBetweenWandersRange>
                <min>120</min>
                <max>240</max>
              </ticksBetweenWandersRange>    
            </li>
          </subNodes>
        </li>

        <li Class="JobGiver_IdleError"/>
      </subNodes>
		</thinkRoot>
	</ThinkTreeDef>

	<ThinkTreeDef>
	
		<defName>RangedAnimalConstant</defName>
		<thinkRoot Class="ThinkNode_Priority">
			<subNodes>
        <li Class="ThinkNode_ConditionalCanDoConstantThinkTreeJobNow">
          <subNodes>
            <!-- Lord directives -->
            <li Class="ThinkNode_Subtree">
              <treeDef>LordDutyConstant</treeDef>
            </li>
          </subNodes>
        </li>
      </subNodes>
		</thinkRoot>
	</ThinkTreeDef>

	 <ThinkTreeDef>
    <defName>MentalStateNonCritical</defName>
    <thinkRoot Class="ThinkNode_Tagger">
      <tagToGive>InMentalState</tagToGive>
      <subNodes>
        <!-- Various wanders  -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>WanderPsychotic</state>
          <subNodes>
            <li Class="JobGiver_WanderAnywhere">
              <maxDanger>Deadly</maxDanger>
            </li>
          </subNodes>
        </li>
        <li Class="ThinkNode_ConditionalMentalState">
          <state>WanderSad</state>
          <subNodes>
            <li Class="JobGiver_WanderColony">
              <maxDanger>Deadly</maxDanger>
            </li>
          </subNodes>
        </li>
        <li Class="ThinkNode_ConditionalMentalState">
          <state>WanderConfused</state>
          <subNodes>
            <li Class="JobGiver_WanderAnywhere">
              <maxDanger>Deadly</maxDanger>
            </li>
          </subNodes>
        </li>
        
        <!-- Hide in room -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>WanderOwnRoom</state>
          <subNodes>
            <li Class="JobGiver_WanderOwnRoom">
              <maxDanger>Deadly</maxDanger>
            </li>
          </subNodes>
        </li>
        
        <!-- Binging on drug -->
        <li Class="ThinkNode_ConditionalMentalStates">
          <states>
            <li>BingingDrugMajor</li>
            <li>BingingDrugExtreme</li>
          </states>
          <subNodes>
						<!-- We can't put GetRest in the priority sorter because then the player can just assign the pawn to sleep,
								and they'll go to sleep immediately and end the binging state. So, instead, we have the pawn sleep if and only if
								exhausted-->
						<li Class="ThinkNode_ConditionalNeedPercentageAbove">
							<need>Rest</need>
							<threshold>0.05</threshold>
							<invert>true</invert>
							<subNodes>
								<li Class="JobGiver_GetRest"/>
							</subNodes>
						</li>
						<li Class="ThinkNode_PrioritySorter">
							<minPriority>0.5</minPriority>
							<subNodes>
								<li Class="JobGiver_GetFood"/>
								<li Class="JobGiver_SatisfyChemicalNeed"/>
                <li Class="ThinkNode_Priority_GetJoy">
                  <subNodes>
                    <li Class="JobGiver_GetJoy"/>
                  </subNodes>
                </li>
              </subNodes>
            </li>
            <li Class="JobGiver_BingeDrug"/>
            <li Class="JobGiver_WanderColony" />
          </subNodes>
        </li>

        <!-- Binging on food -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>BingingFood</state>
          <subNodes>
            <li Class="ThinkNode_PrioritySorter">
              <minPriority>0.5</minPriority>
              <subNodes>
                <li Class="JobGiver_GetFood"/>
                <li Class="JobGiver_GetRest"/>
                <li Class="JobGiver_SatisfyChemicalNeed"/>
                <li Class="ThinkNode_Priority_GetJoy">
                  <subNodes>
                    <li Class="JobGiver_GetJoy"/>
                  </subNodes>
                </li>
              </subNodes>
            </li>
            <li Class="JobGiver_BingeFood"/>
            <li Class="JobGiver_WanderColony" />
          </subNodes>
        </li>
        
        <!-- Manhunter -->
        <!-- Hack: these have to be identical -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>Manhunter</state>
          <subNodes>
            <li Class="ThinkNode_Priority">
              <subNodes>
                <li Class="ThinkNode_PrioritySorter">
                  <subNodes>
                    <li Class="JobGiver_GetFood">
                      <minCategory>Starving</minCategory>
                    </li>
                  </subNodes>
                </li>
                <li Class="AnimalRangedVerbsUnlocker.JobGiver_ManhunterRanged" />
                <li Class="JobGiver_WanderAnywhere">
                  <maxDanger>Deadly</maxDanger>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        <li Class="ThinkNode_ConditionalMentalState">
          <state>ManhunterPermanent</state>
          <subNodes>
            <li Class="ThinkNode_Priority">
              <subNodes>
                <li Class="ThinkNode_PrioritySorter">
                  <subNodes>
                    <li Class="JobGiver_GetFood">
                      <minCategory>Starving</minCategory>
                    </li>
                  </subNodes>
                </li>
                <li Class="AnimalRangedVerbsUnlocker.JobGiver_ManhunterRanged" />
                <li Class="JobGiver_WanderAnywhere">
                  <maxDanger>Deadly</maxDanger>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        
        <!-- Panic flee -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>PanicFlee</state>
          <subNodes>
            <li Class="JobGiver_ExitMapPanic" />
            <li Class="JobGiver_WanderAnywhere">
              <!-- Can happen if there is no way to exit the map -->
              <maxDanger>Some</maxDanger>
              <locomotionUrgency>Jog</locomotionUrgency>
            </li>
          </subNodes>
        </li>

        <!-- Give up and leave -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>GiveUpExit</state>
          <subNodes>
            <li Class="JobGiver_ExitMapRandom">
              <defaultLocomotion>Walk</defaultLocomotion>
            </li>
            <li Class="JobGiver_WanderAnywhere">
              <maxDanger>Deadly</maxDanger>
            </li>
          </subNodes>
        </li>
          
        <!-- Fire starting spree -->
        <li Class="ThinkNode_ConditionalMentalState">
          <state>FireStartingSpree</state>
          <subNodes>
            <li Class="JobGiver_FireStartingSpree"></li>
          </subNodes>
        </li>
      </subNodes>
    </thinkRoot>
  </ThinkTreeDef>
 
</ThinkTrees>