Пример #1
0
        public static Lord MakeNewLord(Faction faction, LordJob lordJob, Map map, IEnumerable <Pawn> startingPawns = null)
        {
            Lord result;

            if (map == null)
            {
                Log.Warning("Tried to create a lord with null map.", false);
                result = null;
            }
            else
            {
                Lord lord = new Lord();
                lord.loadID  = Find.UniqueIDsManager.GetNextLordID();
                lord.faction = faction;
                map.lordManager.AddLord(lord);
                lord.SetJob(lordJob);
                lord.GotoToil(lord.Graph.StartingToil);
                if (startingPawns != null)
                {
                    foreach (Pawn p in startingPawns)
                    {
                        lord.AddPawn(p);
                    }
                }
                result = lord;
            }
            return(result);
        }
Пример #2
0
 public void SetJob(LordJob lordJob)
 {
     if (this.curJob != null)
     {
         this.curJob.Cleanup();
     }
     this.curJob      = lordJob;
     this.curLordToil = null;
     lordJob.lord     = this;
     Rand.PushState();
     Rand.Seed  = this.loadID * 193;
     this.graph = lordJob.CreateGraph();
     Rand.PopState();
     this.graph.ErrorCheck();
     if (this.faction != null && !this.faction.IsPlayer && this.faction.def.autoFlee && lordJob.AddFleeToil)
     {
         LordToil_PanicFlee lordToil_PanicFlee = new LordToil_PanicFlee();
         lordToil_PanicFlee.avoidGridMode = AvoidGridMode.Smart;
         for (int i = 0; i < this.graph.lordToils.Count; i++)
         {
             Transition transition = new Transition(this.graph.lordToils[i], lordToil_PanicFlee, false, true);
             transition.AddPreAction(new TransitionAction_Message("MessageFightersFleeing".Translate(new object[]
             {
                 this.faction.def.pawnsPlural.CapitalizeFirst(),
                 this.faction.Name
             }), null, 1f));
             transition.AddTrigger(new Trigger_FractionPawnsLost(0.5f));
             this.graph.AddTransition(transition, true);
         }
         this.graph.AddToil(lordToil_PanicFlee);
     }
     for (int j = 0; j < this.graph.lordToils.Count; j++)
     {
         this.graph.lordToils[j].lord = this;
     }
     for (int k = 0; k < this.ownedPawns.Count; k++)
     {
         this.Map.attackTargetsCache.UpdateTarget(this.ownedPawns[k]);
     }
 }
Пример #3
0
 public void SetJob(LordJob lordJob)
 {
     if (curJob != null)
     {
         curJob.Cleanup();
     }
     curJob       = lordJob;
     curLordToil  = null;
     lordJob.lord = this;
     Rand.PushState();
     Rand.Seed = loadID * 193;
     graph     = lordJob.CreateGraph();
     Rand.PopState();
     graph.ErrorCheck();
     if (faction != null && !faction.IsPlayer && faction.def.autoFlee && lordJob.AddFleeToil)
     {
         LordToil_PanicFlee lordToil_PanicFlee = new LordToil_PanicFlee();
         lordToil_PanicFlee.useAvoidGrid = true;
         for (int i = 0; i < graph.lordToils.Count; i++)
         {
             Transition transition = new Transition(graph.lordToils[i], lordToil_PanicFlee);
             transition.AddPreAction(new TransitionAction_Message("MessageFightersFleeing".Translate(faction.def.pawnsPlural.CapitalizeFirst(), faction.Name)));
             transition.AddTrigger(new Trigger_FractionPawnsLost(faction.def.attackersDownPercentageRangeForAutoFlee.RandomInRangeSeeded(loadID)));
             graph.AddTransition(transition, highPriority: true);
         }
         graph.AddToil(lordToil_PanicFlee);
     }
     for (int j = 0; j < graph.lordToils.Count; j++)
     {
         graph.lordToils[j].lord = this;
     }
     for (int k = 0; k < ownedPawns.Count; k++)
     {
         Map.attackTargetsCache.UpdateTarget(ownedPawns[k]);
     }
 }
Пример #4
0
 private void ExposeData_StateGraph()
 {
     if (Scribe.mode == LoadSaveMode.Saving)
     {
         this.tmpLordToilData.Clear();
         for (int i = 0; i < this.graph.lordToils.Count; i++)
         {
             if (this.graph.lordToils[i].data != null)
             {
                 this.tmpLordToilData.Add(i, this.graph.lordToils[i].data);
             }
         }
         this.tmpTriggerData.Clear();
         int num = 0;
         for (int j = 0; j < this.graph.transitions.Count; j++)
         {
             for (int k = 0; k < this.graph.transitions[j].triggers.Count; k++)
             {
                 if (this.graph.transitions[j].triggers[k].data != null)
                 {
                     this.tmpTriggerData.Add(num, this.graph.transitions[j].triggers[k].data);
                 }
                 num++;
             }
         }
         this.tmpCurLordToilIdx = this.graph.lordToils.IndexOf(this.curLordToil);
     }
     Scribe_Collections.Look <int, LordToilData>(ref this.tmpLordToilData, "lordToilData", LookMode.Value, LookMode.Deep);
     Scribe_Collections.Look <int, TriggerData>(ref this.tmpTriggerData, "triggerData", LookMode.Value, LookMode.Deep);
     Scribe_Values.Look <int>(ref this.tmpCurLordToilIdx, "curLordToilIdx", -1, false);
     if (Scribe.mode == LoadSaveMode.PostLoadInit)
     {
         if (this.curJob.LostImportantReferenceDuringLoading)
         {
             this.lordManager.RemoveLord(this);
         }
         else
         {
             LordJob job = this.curJob;
             this.curJob = null;
             this.SetJob(job);
             foreach (KeyValuePair <int, LordToilData> current in this.tmpLordToilData)
             {
                 if (current.Key < 0 || current.Key >= this.graph.lordToils.Count)
                 {
                     Log.Error(string.Concat(new object[]
                     {
                         "Could not find lord toil for lord toil data of type \"",
                         current.Value.GetType(),
                         "\" (lord job: \"",
                         this.curJob.GetType(),
                         "\"), because lord toil index is out of bounds: ",
                         current.Key
                     }), false);
                 }
                 else
                 {
                     this.graph.lordToils[current.Key].data = current.Value;
                 }
             }
             this.tmpLordToilData.Clear();
             foreach (KeyValuePair <int, TriggerData> current2 in this.tmpTriggerData)
             {
                 Trigger triggerByIndex = this.GetTriggerByIndex(current2.Key);
                 if (triggerByIndex == null)
                 {
                     Log.Error(string.Concat(new object[]
                     {
                         "Could not find trigger for trigger data of type \"",
                         current2.Value.GetType(),
                         "\" (lord job: \"",
                         this.curJob.GetType(),
                         "\"), because trigger index is out of bounds: ",
                         current2.Key
                     }), false);
                 }
                 else
                 {
                     triggerByIndex.data = current2.Value;
                 }
             }
             this.tmpTriggerData.Clear();
             if (this.tmpCurLordToilIdx < 0 || this.tmpCurLordToilIdx >= this.graph.lordToils.Count)
             {
                 Log.Error(string.Concat(new object[]
                 {
                     "Current lord toil index out of bounds (lord job: \"",
                     this.curJob.GetType(),
                     "\"): ",
                     this.tmpCurLordToilIdx
                 }), false);
             }
             else
             {
                 this.curLordToil = this.graph.lordToils[this.tmpCurLordToilIdx];
             }
         }
     }
 }
Пример #5
0
        private void ExposeData_StateGraph()
        {
            if (Scribe.mode == LoadSaveMode.Saving)
            {
                tmpLordToilData.Clear();
                for (int i = 0; i < graph.lordToils.Count; i++)
                {
                    if (graph.lordToils[i].data != null)
                    {
                        tmpLordToilData.Add(i, graph.lordToils[i].data);
                    }
                }
                tmpTriggerData.Clear();
                int num = 0;
                for (int j = 0; j < graph.transitions.Count; j++)
                {
                    for (int k = 0; k < graph.transitions[j].triggers.Count; k++)
                    {
                        if (graph.transitions[j].triggers[k].data != null)
                        {
                            tmpTriggerData.Add(num, graph.transitions[j].triggers[k].data);
                        }
                        num++;
                    }
                }
                tmpCurLordToilIdx = graph.lordToils.IndexOf(curLordToil);
            }
            Scribe_Collections.Look(ref tmpLordToilData, "lordToilData", LookMode.Value, LookMode.Deep);
            Scribe_Collections.Look(ref tmpTriggerData, "triggerData", LookMode.Value, LookMode.Deep);
            Scribe_Values.Look(ref tmpCurLordToilIdx, "curLordToilIdx", -1);
            if (Scribe.mode != LoadSaveMode.PostLoadInit)
            {
                return;
            }
            if (curJob.LostImportantReferenceDuringLoading)
            {
                lordManager.RemoveLord(this);
                return;
            }
            LordJob job = curJob;

            curJob = null;
            SetJob(job);
            foreach (KeyValuePair <int, LordToilData> tmpLordToilDatum in tmpLordToilData)
            {
                if (tmpLordToilDatum.Key < 0 || tmpLordToilDatum.Key >= graph.lordToils.Count)
                {
                    Log.Error(string.Concat("Could not find lord toil for lord toil data of type \"", tmpLordToilDatum.Value.GetType(), "\" (lord job: \"", curJob.GetType(), "\"), because lord toil index is out of bounds: ", tmpLordToilDatum.Key));
                }
                else
                {
                    graph.lordToils[tmpLordToilDatum.Key].data = tmpLordToilDatum.Value;
                }
            }
            tmpLordToilData.Clear();
            foreach (KeyValuePair <int, TriggerData> tmpTriggerDatum in tmpTriggerData)
            {
                Trigger triggerByIndex = GetTriggerByIndex(tmpTriggerDatum.Key);
                if (triggerByIndex == null)
                {
                    Log.Error(string.Concat("Could not find trigger for trigger data of type \"", tmpTriggerDatum.Value.GetType(), "\" (lord job: \"", curJob.GetType(), "\"), because trigger index is out of bounds: ", tmpTriggerDatum.Key));
                }
                else
                {
                    triggerByIndex.data = tmpTriggerDatum.Value;
                }
            }
            tmpTriggerData.Clear();
            if (tmpCurLordToilIdx < 0 || tmpCurLordToilIdx >= graph.lordToils.Count)
            {
                Log.Error(string.Concat("Current lord toil index out of bounds (lord job: \"", curJob.GetType(), "\"): ", tmpCurLordToilIdx));
            }
            else
            {
                curLordToil = graph.lordToils[tmpCurLordToilIdx];
            }
        }