Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using System;
  2. using RimWorld;
  3. using Verse;
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6.  
  7. namespace AnaestheticThoughtCode // change to whatever you want
  8. {
  9.     // This class "ThoughtWorkerAnaesthetic" is derived from ThoughtWorker_Hediff, hence the " : "
  10.     // This means it inherits all of its functions unless otherwise overridden.
  11.     // A class is like an "object" which can contain logic and variables
  12.     public class ThoughtWorker_Anaesthetic : ThoughtWorker_Hediff
  13.     {
  14.         // Notice the "override" keyword below. We are overriding the CurrentStateInternal method from ThoughtWorker_Hediff
  15.         // which is what decides whether or not the thought should be active or inactive based on what we ask for.
  16.         // Also notice "ThoughtState". This is what kind of data the method will return upon execution, if any. If no
  17.         // data is given by the method's execution, it is considered a "void" method, which only executes a function but
  18.         // does not return any information.
  19.  
  20.         protected override ThoughtState CurrentStateInternal (Pawn pawn) // 'Pawn' is the class of the variable passed to the method, and 'pawn' is our variable name
  21.         {
  22.             // Does the pawn have the required hediff?
  23.             Hediff firstHediffOfDef = pawn.health.hediffSet.GetFirstHediffOfDef (def.hediff);
  24.             if (firstHediffOfDef == null || firstHediffOfDef.def.stages == null) {
  25.                 // I guess not
  26.                 // "return" ends the method's execution right where it is stated. Nothing below this will be executed if the logic gets here
  27.                 return ThoughtState.Inactive;
  28.             }
  29.                
  30.             int stageIndex = 0; // This will decide what stage of the thought will be used
  31.             Trait drug_desire = pawn.story.traits.GetTrait (TraitDefOf.DrugDesire); // Finds the trait we need to get info from
  32.             if (drug_desire.Degree == -1) { // degree of -1 for DrugDesire is TeeTotaller
  33.                 stageIndex = 0;// so let's set our stage index of our thought
  34.             }
  35.             else if (drug_desire.Degree == 1) { // degree of 1 is chemical interest
  36.                 stageIndex = 1;
  37.             }
  38.             else if (drug_desire.Degree == 2) { // degree of 2 is chemical fascination as I'm sure you've guessed at this point
  39.                 stageIndex = 2;
  40.             }
  41.  
  42.             return ThoughtState.ActiveAtStage ( stageIndex ); // gives the
  43.  
  44.             // This is a lot of if / else statements though, we could do this a lot cleaner by just using a dictionary to "translate" the degree
  45.             // of the trait "DrugDesire" directly to the stage of the Thought
  46.  
  47.             // Dictionaries are lists of key/value pairs. With a dictionary, when you ask it for a specific "key", it will return
  48.             // the corresponding "value". So in the dictionary below, if we ask it for '-1', it will give us '0', etc.
  49.  
  50.             Dictionary<int,int> degree_to_stage = new Dictionary<int, int> () { {-1,0}, {1,1}, {2,2} };
  51.  
  52.             // To get a value from a dictionary using a key, the syntax is as follows:
  53.             // my_dict[key]
  54.             // So below, we ask the dictionary 'degree_to_stage' we've made above for the current Degree of the pawn's drugdesire trait
  55.             return ThoughtState.ActiveAtStage ( degree_to_stage[drug_desire.Degree] ); // Give the ThoughtWorker what stage it ought to be at!
  56.  
  57.             // This is a lot of if / else statements though, we could do this a lot cleaner by just using a dictionary to "translate" the degree
  58.             // of the trait "DrugDesire" directly to the stage of the Thought.
  59.         }
  60.  
  61.         protected override ThoughtState CurrentStateInternal (Pawn pawn) // 'Pawn' is the class of the variable passed to the method, and 'pawn' is our variable name
  62.         {
  63.             Hediff firstHediffOfDef = pawn.health.hediffSet.GetFirstHediffOfDef (def.hediff);
  64.             if (firstHediffOfDef == null || firstHediffOfDef.def.stages == null) {
  65.                 return ThoughtState.Inactive;
  66.             }
  67.  
  68.             Trait drug_desire = pawn.story.traits.GetTrait (TraitDefOf.DrugDesire);
  69.             // Dictionaries are lists of key/value pairs. With a dictionary, when you ask it for a specific "key", it will return
  70.             // the corresponding "value". So in the dictionary below, if we ask it for '-1', it will give us '0', etc.
  71.             // The <int,int> part defines what KIND of data the key/value pairs are. For our uses we are simply using integers,
  72.             // but you could make a dictionary that uses any kinds of data types. eg: <Vector3, string> to translate a 3D position to a sentence (for whatever reason)
  73.             Dictionary<int,int> degree_to_stage = new Dictionary<int, int> () { {-1,0}, {1,1}, {2,2} };
  74.  
  75.             // To get a value from a dictionary using a key, the syntax is as follows:
  76.             // my_dict[key]
  77.             // So below, we ask the dictionary 'degree_to_stage' we've made above for the current Degree of the pawn's drugdesire trait
  78.             return ThoughtState.ActiveAtStage ( degree_to_stage[drug_desire.Degree] ); // Give the ThoughtWorker what stage it ought to be at!
  79.         }
  80.  
  81.         // In the end our method will look simple as this
  82.         // Note that only this last method should be in the actual code used, you don't want to have all three in reality...
  83.         // Only for example :p
  84.  
  85.         protected override ThoughtState CurrentStateInternal (Pawn pawn) // 'Pawn' is the class of the variable passed to the method, and 'pawn' is our variable name
  86.         {
  87.             Hediff firstHediffOfDef = pawn.health.hediffSet.GetFirstHediffOfDef (def.hediff);
  88.             if (firstHediffOfDef == null || firstHediffOfDef.def.stages == null) {
  89.                 return ThoughtState.Inactive;
  90.             }
  91.  
  92.             Trait drug_desire = pawn.story.traits.GetTrait (TraitDefOf.DrugDesire);
  93.             Dictionary<int,int> degree_to_stage = new Dictionary<int, int> () { {-1,0}, {1,1}, {2,2} };
  94.  
  95.             return ThoughtState.ActiveAtStage ( degree_to_stage[drug_desire.Degree] );
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement