﻿<?xml version="1.0" encoding="utf-8" ?>
<Defs>

  <ThinkTreeDef>
    <defName>Animal</defName>
    <thinkRoot Class="ThinkNode_Priority">
      <subNodes>
        <!-- Keep lying down if we have to -->
        <li Class="ThinkNode_ConditionalMustKeepLyingDown">
          <subNodes>
            <!-- Do a queued job if possible -->
            <li Class="ThinkNode_QueuedJob">
              <inBedOnly>true</inBedOnly>
            </li>

            <!-- Keep lying down -->
            <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="JobGiver_ReactToCloseMeleeThreat" />

        <!-- Do a queued job -->
        <li Class="ThinkNode_QueuedJob" />

        <!-- 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>

        <!-- Behavior when roped -->
        <li Class="ThinkNode_Subtree">
          <treeDef>RopedPawn</treeDef>
        </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>

            <!-- Wild animals randomly leave map -->
            <li Class="ThinkNode_ChancePerHour_Constant">
              <mtbDays>60</mtbDays>
              <subNodes>
                <li Class="ThinkNode_Tagger">
                  <tagToGive>Misc</tagToGive>
                  <subNodes>
                    <li Class="JobGiver_ExitMapRandom">
                      <defaultLocomotion>Walk</defaultLocomotion>
                    </li>
                  </subNodes>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>

        <!-- Lay eggs you need to -->
        <li Class="JobGiver_LayEgg" />

        <!-- Tame animal -->
        <li Class="ThinkNode_ConditionalOfPlayerFaction">
          <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="JobGiver_AIDefendMaster">
                          <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>75</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">
          <mtbDays>60</mtbDays>
          <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>

            <!-- Roamers gonna roam -->
            <li Class="ThinkNode_ChancePerDay_Roam">
              <subNodes>
                <li Class="ThinkNode_Tagger">
                  <tagToGive>Misc</tagToGive>
                  <subNodes>
                    <li Class="JobGiver_StartRoaming" />
                  </subNodes>
                </li>
              </subNodes>
            </li>

            <!-- Trained behavior: Haul-->
            <li Class="ThinkNode_ChancePerHour_Constant">
              <mtbHours>1.5</mtbHours>
              <subNodes>
                <li Class="ThinkNode_ConditionalRequireCapacities">
                  <requiredCapacities>
                    <li>Manipulation</li>
                  </requiredCapacities>
                  <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>
              </subNodes>
            </li>
          </subNodes>
        </li>

        <!-- Insertion hook for modders -->
        <li Class="ThinkNode_SubtreesByTag">
          <insertTag>Animal_PreWander</insertTag>
        </li>
        
        <!-- Tame animal: wander near colony if possible -->
        <li Class="ThinkNode_ConditionalOfPlayerFaction">
          <subNodes>
            <li Class="ThinkNode_Tagger">
              <tagToGive>Idle</tagToGive>
              <subNodes>
                <!-- Wander near your current position if in hostile map -->
                <li Class="ThinkNode_ConditionalAnyEnemyInHostileMap">
                  <subNodes>
                    <li Class="JobGiver_WanderAnywhere">
                      <maxDanger>None</maxDanger>
                      <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>
                    </li>
                  </subNodes>
                </li>

                <li Class="ThinkNode_ConditionalRoamer">
                  <subNodes>
                    <li Class="JobGiver_WanderInRoofedCellsInPen">
                      <maxDanger>None</maxDanger>
                      <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>
                      <expiryInterval>500</expiryInterval>
                    </li>

                    <!-- tame roamers should not wander too far, and if unenclosed wander near a suitable pen marker 10% of the time -->
                    <li Class="ThinkNode_ConditionalRandom">
                      <chance>0.1</chance>
                      <subNodes>
                        <li Class="JobGiver_WanderInPen">
                          <maxDanger>None</maxDanger>
                          <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>
                          <expiryInterval>500</expiryInterval>
                        </li>
                      </subNodes>
                    </li>

                    <li Class="JobGiver_WanderAnywhere">
                      <maxDanger>None</maxDanger>
                      <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>
                    </li>
                  </subNodes>
                </li>

                <!-- Wander near colony -->
                <li Class="JobGiver_WanderColony">
                  <maxDanger>None</maxDanger>
                  <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>
                </li>
              </subNodes>
            </li>
          </subNodes>
        </li>
        
        <!-- Of neutral faction: rest and then exit the map -->
        <li Class="ThinkNode_ConditionalNonPlayerNonHostileFaction">
          <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>120~240</ticksBetweenWandersRange>
                </li>
              </subNodes>
            </li>
            
            <li Class="JobGiver_WanderAnywhere">
              <maxDanger>Deadly</maxDanger>
              <ticksBetweenWandersRange>120~240</ticksBetweenWandersRange>    
            </li>
          </subNodes>
        </li>

        <li Class="JobGiver_IdleError"/>
      </subNodes>
    </thinkRoot>
  </ThinkTreeDef>

  <ThinkTreeDef>
    <defName>AnimalConstant</defName>
    <thinkRoot Class="ThinkNode_Priority">
      <subNodes>
        <!-- Despawned -->
        <li Class="ThinkNode_Subtree">
          <treeDef>Despawned</treeDef>
        </li>

        <li Class="ThinkNode_ConditionalCanDoConstantThinkTreeJobNow">
          <subNodes>
            <!-- Flee enemies, tornados, meteorites, etc. -->
            <li Class="JobGiver_AnimalFlee" />

            <!-- Join auto joinable caravan -->
            <li Class="ThinkNode_Subtree">
              <treeDef>JoinAutoJoinableCaravan</treeDef>
            </li>
          </subNodes>
        </li>

        <li Class="ThinkNode_ConditionalCanDoLordJobNow">
          <subNodes>
            <!-- Lord directives -->
            <li Class="ThinkNode_Subtree">
              <treeDef>LordDutyConstant</treeDef>
            </li>
          </subNodes>
        </li>
      </subNodes>
    </thinkRoot>
  </ThinkTreeDef>

</Defs>
