Esempio n. 1
0
 public override void Save(XmlElement elem)
 {
     elem.SetAttribute("x", Loc.X.ToString());
     elem.SetAttribute("y", Loc.Y.ToString());
     elem.SetAttribute("map", DefMap.ToString());
     elem.SetAttribute("color", _col.ToArgb().ToString());
     elem.SetAttribute("text", _text);
 }
        public override void SpawnMyPawn()
        {
            base.SpawnMyPawn();
            if (myPawn.story == null)
            {
                myPawn.story = new Pawn_StoryTracker(myPawn);
            }
            if (myPawn.skills == null)
            {
                myPawn.skills = new Pawn_SkillTracker(myPawn);
            }
            if (myPawn.workSettings == null)
            {
                myPawn.workSettings = new Pawn_WorkSettings(myPawn);
            }
            if (myPawn.relations == null)
            {
                myPawn.relations = new Pawn_RelationsTracker(myPawn);
            }
            DefMap <WorkTypeDef, int> priorities = new DefMap <WorkTypeDef, int>();

            priorities.SetAll(0);
            typeof(Pawn_WorkSettings).GetField("priorities", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(myPawn.workSettings, priorities);

            foreach (WorkTypeDef workType in Props.allowedWorkTypes)
            {
                foreach (SkillDef skill in workType.relevantSkills)
                {
                    SkillRecord record = myPawn.skills.skills.Find(rec => rec.def == skill);
                    record.levelInt = Props.skillLevel;
                }
                myPawn.workSettings.SetPriority(workType, 1);
            }
            if (myPawn.TryGetComp <CompMachine>().Props.violent)
            {
                if (myPawn.drafter == null)
                {
                    myPawn.drafter = new Pawn_DraftController(myPawn);
                }
                if (Props.spawnWithWeapon != null)
                {
                    ThingWithComps thing = (ThingWithComps)ThingMaker.MakeThing(Props.spawnWithWeapon);
                    myPawn.equipment.AddEquipment(thing);
                }
            }
            if (myPawn.needs.TryGetNeed <Need_Power>() == null)
            {
                typeof(Pawn_NeedsTracker).GetMethod("AddNeed", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(myPawn.needs, new object[] { DefDatabase <NeedDef> .GetNamed("VFE_Mechanoids_Power") });
            }
            myPawn.needs.TryGetNeed <Need_Power>().CurLevel = 0;

            if (myPawn.playerSettings != null)
            {
                myPawn.playerSettings.AreaRestriction = allowedArea;
            }

            wantsRespawn = false;
        }
        public static Thing MakeBrainScan(Pawn pawn, ThingDef genomeDef)
        {
            Thing             brainScanThing = ThingMaker.MakeThing(genomeDef);
            BrainScanTemplate brainScan      = brainScanThing as BrainScanTemplate;

            if (brainScan != null)
            {
                //Standard.
                brainScan.sourceName = pawn?.Name?.ToStringFull ?? null;

                //Backgrounds
                Pawn_StoryTracker story = pawn.story;
                if (story != null)
                {
                    brainScan.backStoryChild = story.childhood;
                    brainScan.backStoryAdult = story.adulthood;
                }

                //Skills
                Pawn_SkillTracker skillTracker = pawn.skills;
                if (skillTracker != null)
                {
                    foreach (SkillRecord skill in skillTracker.skills)
                    {
                        brainScan.skills.Add(new SkillRecord()
                        {
                            def     = skill.def,
                            Level   = skill.Level,
                            passion = skill.passion
                        });
                    }
                }

                //Animal
                brainScan.isAnimal = pawn.RaceProps.Animal;

                //Training
                Pawn_TrainingTracker trainingTracker = pawn.training;
                if (trainingTracker != null)
                {
                    DefMap <TrainableDef, bool> learned = (DefMap <TrainableDef, bool>)AccessTools.Field(typeof(Pawn_TrainingTracker), "learned").GetValue(trainingTracker);
                    DefMap <TrainableDef, int>  steps   = (DefMap <TrainableDef, int>)AccessTools.Field(typeof(Pawn_TrainingTracker), "steps").GetValue(trainingTracker);

                    //Copy
                    foreach (var item in learned)
                    {
                        brainScan.trainingLearned[item.Key] = item.Value;
                    }
                    foreach (var item in steps)
                    {
                        brainScan.trainingSteps[item.Key] = item.Value;
                    }
                }
            }

            return(brainScanThing);
        }
Esempio n. 4
0
 public void ExposeData()
 {
     Scribe_Deep.Look(ref tolerances, "tolerances");
     Scribe_Deep.Look(ref bored, "bored");
     if (bored == null)
     {
         bored = new DefMap <JoyKindDef, bool>();
     }
 }
Esempio n. 5
0
 public void ExposeData()
 {
     Scribe_Deep.Look <DefMap <JoyKindDef, float> >(ref this.tolerances, "tolerances", new object[0]);
     Scribe_Deep.Look <DefMap <JoyKindDef, bool> >(ref this.bored, "bored", new object[0]);
     if (this.bored == null)
     {
         this.bored = new DefMap <JoyKindDef, bool>();
     }
 }
Esempio n. 6
0
        static void Postfix(Pawn_WorkSettings __instance, object __state, WorkTypeDef w)
        {
            if (__state != null)
            {
                DefMap <WorkTypeDef, int> pawnPriorities = AccessTools.Field(typeof(Pawn_WorkSettings), "priorities").GetValue(__instance) as DefMap <WorkTypeDef, int>;

                pawnPriorities[w] = (int)__state;
            }
        }
        public static IEnumerable <T> Values <D, T>(this DefMap <D, T> map) where D : Def, new() where T : new()
        {
            var values = new List <T>();

            for (var i = 0; i < map.Count; i++)
            {
                values.Add(map[i]);
            }
            return(values);
        }
Esempio n. 8
0
 static void Postfix(DefMap <TrainableDef, int> ___steps, DefMap <TrainableDef, int> __state)
 {
     if (stopTamenessDecay)
     {
         foreach (KeyValuePair <TrainableDef, int> kvp in ___steps)
         {
             ___steps[kvp.Key] = __state[kvp.Key];
         }
     }
 }
        public static void ApplyBrainScanTemplateOnPawn(Pawn thePawn, BrainScanTemplate brainScan, float efficency = 1f)
        {
            if(thePawn.IsValidBrainScanningTarget())
            {
                //Backgrounds
                Pawn_StoryTracker storyTracker = thePawn.story;
                if (storyTracker != null)
                {
                    //story.childhood = brainScan.backStoryChild;
                    storyTracker.adulthood = brainScan.backStoryAdult;
                }

                //Skills
                Pawn_SkillTracker skillTracker = thePawn.skills;
                if (skillTracker != null)
                {
                    foreach (ComparableSkillRecord skill in brainScan.skills)
                    {
                        SkillRecord pawnSkill = skillTracker.GetSkill(skill.def);
                        pawnSkill.Level = (int)Math.Floor((float)skill.level * efficency);
                        pawnSkill.passion = skill.passion;
                        pawnSkill.Notify_SkillDisablesChanged();
                    }
                }

                //Dirty hack ahoy!
                if(storyTracker != null)
                {
                    AccessTools.Field(typeof(Pawn_StoryTracker), "cachedDisabledWorkTypes").SetValue(storyTracker, null);
                }

                //Training
                Pawn_TrainingTracker trainingTracker = thePawn.training;
                if (trainingTracker != null)
                {
                    DefMap<TrainableDef, bool> learned = (DefMap<TrainableDef, bool>)AccessTools.Field(typeof(Pawn_TrainingTracker), "learned").GetValue(trainingTracker);
                    DefMap<TrainableDef, int> steps = (DefMap<TrainableDef, int>)AccessTools.Field(typeof(Pawn_TrainingTracker), "steps").GetValue(trainingTracker);

                    //Copy
                    foreach (var item in brainScan.trainingLearned)
                    {
                        learned[item.Key] = item.Value;
                    }
                    foreach (var item in brainScan.trainingSteps)
                    {
                        steps[item.Key] = (int)Math.Floor((float)item.Value * efficency);
                    }
                }

                //Apply Hediff
                thePawn.health.AddHediff(QEHediffDefOf.QE_BrainTemplated);

                Messages.Message("QE_BrainTemplatingComplete".Translate(thePawn.Named("PAWN")), MessageTypeDefOf.PositiveEvent, false);
            }
        }
Esempio n. 10
0
        static bool Prefix(ref Job __result, Pawn pawn, Thing t, bool forced)
        {
            if (!Settings.give_sick_joy_drugs)
            {
                return(true);
            }
            //
            Pawn sick = (Pawn)t;
            List <JoyGiverDef> defs = DefDatabase <JoyGiverDef> .AllDefsListForReading.FindAll(
                d => d.giverClass == typeof(JoyGiver_Ingest) ||
                d.giverClass == typeof(JoyGiver_TakeDrug_Patient) ||
                d.giverClass == typeof(JoyGiver_VisitSickPawn));

            //
            var chances = new DefMap <JoyGiverDef, float>();

            foreach (var d in defs)
            {
                if (d.Worker.CanBeGivenTo(pawn))
                {
                    chances[d] = (d.giverClass == typeof(JoyGiver_TakeDrug_Patient) ? 4f : d.Worker.GetChance(sick)) * Mathf.Max(0.001f, Mathf.Pow(1f - sick.needs.joy.tolerances[d.joyKind], 5f));
                }
                else
                {
                    chances[d] = 0f;
                }
            }
            JoyGiverDef def     = null;
            Job         newJob  = null;
            int         counter = 0;

            while (counter < defs.Count && defs.TryRandomElementByWeight(d => chances[d], out def))
            {
                if (def.giverClass == typeof(JoyGiver_VisitSickPawn))
                {
                    return(true);
                }
                //
                newJob = def.Worker.TryGiveJob(sick);
                if (newJob != null)
                {
                    __result         = JobMaker.MakeJob(JobDefOf.FeedPatient);
                    __result.targetA = newJob.targetA;
                    __result.targetB = t;
                    __result.count   = newJob.count;

                    return(false);
                }
                chances[def] = 0f;
                counter++;
            }

            return(true);
        }
        public new void EnableAndInitialize()
        {
            if (this.prioritiesReflect == null)
            {
                this.prioritiesReflect = new DefMap <WorkTypeDef, int>();
            }
            this.prioritiesReflect.SetAll(0);


            //int num = 0;
            foreach (WorkTypeDef current in from w in DefDatabase <WorkTypeDef> .AllDefs
                     //where !w.alwaysStartActive && !this.pawn.story.WorkTypeIsDisabled(w)
                     //orderby this.pawn2.skills.AverageOfRelevantSkillsFor(w) descending
                     select w)
            {
                bool found = false;
                foreach (X2_ThingDef_AIRobot.RobotWorkTypes rwtdef in (this.pawn2.def as X2_ThingDef_AIRobot).robotWorkTypes)
                {
                    if (rwtdef.workTypeDef.defName == current.defName)
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    this.SetPriority(current, 3);

                    try { base.SetPriority(current, 3); }
                    catch (Exception ex)
                    {
                        if (countLogPriority < 1)
                        {
                            Log.Warning("Thrown error while setting priority. This can be an issue with another mod!" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                            countLogPriority++;
                        }
                    }
                }
                else
                {
                    this.SetPriority(current, 0);

                    try { base.SetPriority(current, 0); }
                    catch (Exception ex)
                    {
                        if (countLogPriority < 1)
                        {
                            Log.Warning("Thrown error while setting priority. This can be an issue with another mod!" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                            countLogPriority++;
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public static DefMap <WorkTypeDef, int> Copy(this DefMap <WorkTypeDef, int> m)
        {
            var map = new DefMap <WorkTypeDef, int>();

            foreach (KeyValuePair <WorkTypeDef, int> pair in m)
            {
                map[pair.Key] = pair.Value;
            }

            return(map);
        }
Esempio n. 13
0
        public static bool Prefix(DefMap <RecordDef, float> __instance)
        {
            var field = __instance.GetType().GetField("values", BindingFlags.GetField | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance);
            var l     = (field.GetValue(__instance) as List <float>);

            while (l.Count < DefDatabase <RecordDef> .DefCount)
            {
                l.Add(0f);
            }
            //field.SetValue(__instance, (int)() + 1);
            return(true);
        }
Esempio n. 14
0
        public static bool Notify_CapacityLevelsDirty(PawnCapacitiesHandler __instance)
        {
            if (cachedCapacityLevelsDict[__instance] == null)
            {
                cachedCapacityLevelsDict[__instance] = new DefMap <PawnCapacityDef, CacheElement2>();
            }

            for (int i = 0; i < cachedCapacityLevelsDict[__instance].Count; i++)
            {
                cachedCapacityLevelsDict[__instance][i].status = CacheStatus.Uncached;
            }
            return(false);
        }
Esempio n. 15
0
        protected override void CopyFrom(Pawn p)
        {
            if (clipboard == null)
            {
                clipboard = new DefMap <WorkTypeDef, int>();
            }
            List <WorkTypeDef> allDefsListForReading = DefDatabase <WorkTypeDef> .AllDefsListForReading;

            for (int i = 0; i < allDefsListForReading.Count; i++)
            {
                WorkTypeDef workTypeDef = allDefsListForReading[i];
                clipboard[workTypeDef] = ((!p.WorkTypeIsDisabled(workTypeDef)) ? p.workSettings.GetPriority(workTypeDef) : 3);
            }
        }
		protected override void ResolveSubnodes()
		{
			this.dutyDefToSubNode = new DefMap<DutyDef, int>();
			this.dutyDefToSubNode.SetAll(-1);
			foreach (DutyDef dutyDef in DefDatabase<DutyDef>.AllDefs)
			{
				if (dutyDef.constantThinkNode != null)
				{
					this.dutyDefToSubNode[dutyDef] = this.subNodes.Count;
					dutyDef.constantThinkNode.ResolveSubnodesAndRecur();
					this.subNodes.Add(dutyDef.constantThinkNode.DeepCopy(true));
				}
			}
		}
        protected override void CopyFrom(Pawn p)
        {
            if (PawnColumnWorker_CopyPasteWorkPriorities.clipboard == null)
            {
                PawnColumnWorker_CopyPasteWorkPriorities.clipboard = new DefMap <WorkTypeDef, int>();
            }
            List <WorkTypeDef> allDefsListForReading = DefDatabase <WorkTypeDef> .AllDefsListForReading;

            for (int i = 0; i < allDefsListForReading.Count; i++)
            {
                WorkTypeDef workTypeDef = allDefsListForReading[i];
                PawnColumnWorker_CopyPasteWorkPriorities.clipboard[workTypeDef] = (p.story.WorkTypeIsDisabled(workTypeDef) ? 3 : p.workSettings.GetPriority(workTypeDef));
            }
        }
Esempio n. 18
0
 protected override void ResolveSubnodes()
 {
     dutyDefToSubNode = new DefMap <DutyDef, int>();
     dutyDefToSubNode.SetAll(-1);
     foreach (DutyDef allDef in DefDatabase <DutyDef> .AllDefs)
     {
         if (allDef.constantThinkNode != null)
         {
             dutyDefToSubNode[allDef] = subNodes.Count;
             allDef.constantThinkNode.ResolveSubnodesAndRecur();
             subNodes.Add(allDef.constantThinkNode.DeepCopy());
         }
     }
 }
Esempio n. 19
0
        internal void TryStoreWorkPriorities()
        {
            if (!Settings.Priorities)
            {
                return;
            }

            if (Parent.workSettings == null)
            {
                return;
            }

            _workCache =
                (MemoryHelper.WorkSettingsMap.GetValue(Parent.workSettings) as DefMap <WorkTypeDef, int>)?.Copy();
        }
Esempio n. 20
0
        private static CacheElement2 get_cacheElement(PawnCapacitiesHandler __instance, PawnCapacityDef capacity)
        {
            DefMap <PawnCapacityDef, CacheElement2> defMap = cachedCapacityLevelsDict[__instance];

            if (defMap == null)
            {
                defMap = new DefMap <PawnCapacityDef, CacheElement2>();
                cachedCapacityLevelsDict[__instance] = defMap;
                for (int i = 0; i < defMap.Count; i++)
                {
                    defMap[i].status = CacheStatus.Uncached;
                }
            }
            return(defMap[capacity]);
        }
 public void Reset()
 {
     if (this.priorities == null)
     {
         this.priorities = new DefMap <WorkTypeDef, int>();
     }
     if (this.priorities.Count == 0)
     {
         foreach (WorkTypeDef current in DefDatabase <WorkTypeDef> .AllDefs)
         {
             this.SetPriority(current, 0);
         }
     }
     this.priorities.SetAll(0);
 }
Esempio n. 22
0
 static void Prefix(DefMap <TrainableDef, int> ___steps, out DefMap <TrainableDef, int> __state)
 {
     if (stopTamenessDecay)
     {
         __state = new DefMap <TrainableDef, int>();
         foreach (KeyValuePair <TrainableDef, int> kvp in ___steps)
         {
             __state[kvp.Key] = kvp.Value;
         }
     }
     else
     {
         __state = null;
     }
 }
        public static bool Prefix(Pawn ingester, Thing __instance, ref JoyState __state)
        {
            ThingDef        ingestThingDef = __instance.def;
            JoyToleranceSet set            = ingester?.needs?.joy?.tolerances;

            if (ingestThingDef == null || set == null || !DessertDefs.AllDeserts.Contains(ingestThingDef))
            {
                __state = new JoyState(false, 0, null);
                return(true);
            }

            DefMap <JoyKindDef, float> tolerances = (DefMap <JoyKindDef, float>)_tolerances.GetValue(set);

            __state = new JoyState(true, tolerances[JoyKindDefOf.Gluttonous], tolerances);
            return(true);
        }
        public new void EnableAndInitialize()
        {
            if (this.prioritiesReflected == null)
            {
                this.prioritiesReflected = new DefMap <WorkTypeDef, int>();
            }
            this.prioritiesReflected.SetAll(0);



            //int num = 0;
            foreach (WorkTypeDef current in from w in DefDatabase <WorkTypeDef> .AllDefs
                     //where !w.alwaysStartActive && !this.pawn.story.WorkTypeIsDisabled(w)
                     //orderby this.pawn2.skills.AverageOfRelevantSkillsFor(w) descending
                     select w)
            {
                //bool found = false;
                //foreach (X2_ThingDef_AIRobot.RobotWorkTypes rwtdef in (this.pawn2.def as X2_ThingDef_AIRobot).robotWorkTypes)
                //{
                //    if (rwtdef.workTypeDef == current)
                //    {
                //        found = true;
                //        break;
                //    }
                //}
                //if (found)
                //    this.SetPriority(current, 3);
                //else
                //    this.SetPriority(current, 0);


                this.SetPriority(current, 3);

                //num++;
                //if (num >= 6)
                //{
                //    break;
                //}
            }
            //foreach (WorkTypeDef current3 in this.pawn.story.DisabledWorkTypes)
            //{
            //    this.Disable(current3);
            //}
        }
Esempio n. 25
0
        private static Definition FindDefinition(DefMap[] globalDefs, BasicBlock current, Register reg)
        {
            foreach (BasicBlock block in SelfAndImmediateDominators(current))
            {
                DefMap defMap = globalDefs[block.Index];

                if (defMap.TryGetOperand(reg, out Operand lastDef))
                {
                    return(new Definition(block, lastDef));
                }

                if (defMap.HasPhi(reg))
                {
                    return(new Definition(block, InsertPhi(globalDefs, block, reg)));
                }
            }

            return(new Definition(current, Undef()));
        }
        public static void MakeTablePairsByThing(List <ThingStuffPair> pairList)
        {
            DefMap <ThingDef, float> totalCommMult = new DefMap <ThingDef, float>();
            DefMap <ThingDef, float> totalComm     = new DefMap <ThingDef, float>();
            DefMap <ThingDef, int>   pairCount     = new DefMap <ThingDef, int>();

            foreach (ThingStuffPair pair in pairList)
            {
                ThingStuffPair           current = pair;
                ThingDef                 thing;
                DefMap <ThingDef, float> defMap;
                (defMap = totalCommMult)[thing = current.thing] = defMap[thing] + current.commonalityMultiplier;
                ThingDef thing2;
                (defMap = totalComm)[thing2 = current.thing] = defMap[thing2] + current.Commonality;
                DefMap <ThingDef, int> defMap2;
                ThingDef thing3;
                (defMap2 = pairCount)[thing3 = current.thing] = defMap2[thing3] + 1;
            }
            DebugTables.MakeTablesDialog(from d in DefDatabase <ThingDef> .AllDefs
                                         where pairList.Any((ThingStuffPair pa) => pa.thing == d)
                                         select d, new TableDataGetter <ThingDef>("thing", (ThingDef t) => t.defName), new TableDataGetter <ThingDef>("pair count", (ThingDef t) => pairCount[t].ToString()), new TableDataGetter <ThingDef>("total commonality multiplier ", (ThingDef t) => totalCommMult[t].ToString("F4")), new TableDataGetter <ThingDef>("total commonality", (ThingDef t) => totalComm[t].ToString("F4")), new TableDataGetter <ThingDef>("def-commonality", (ThingDef t) => t.generateCommonality.ToString("F4")));
        }
Esempio n. 27
0
        static public void CopyPawnRecords(Pawn pawn, Pawn newPawn)
        {
            //Who has a relationship with this pet?
            Pawn pawnMaster = null;
            Map  map        = pawn.Map;

            foreach (Pawn current in map.mapPawns.AllPawns)
            {
                if (current.relations.DirectRelationExists(PawnRelationDefOf.Bond, pawn))
                {
                    pawnMaster = current;
                }
            }

            //Fix the relations
            if (pawnMaster != null)
            {
                pawnMaster.relations.TryRemoveDirectRelation(PawnRelationDefOf.Bond, pawn);
                pawnMaster.relations.AddDirectRelation(PawnRelationDefOf.Bond, newPawn);
                //Train that stuff!

                DefMap <TrainableDef, int> oldMap = (DefMap <TrainableDef, int>) typeof(Pawn_TrainingTracker).GetField("steps", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(pawn.training);
                DefMap <TrainableDef, int> newMap = (DefMap <TrainableDef, int>) typeof(Pawn_TrainingTracker).GetField("steps", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(newPawn.training);

                foreach (TrainableDef def in DefDatabase <TrainableDef> .AllDefs)
                {
                    newMap[def] = oldMap[def];
                }
            }


            foreach (Hediff hediff in pawn.health.hediffSet.hediffs)
            {
                newPawn.health.AddHediff(hediff);
            }
        }
 public JoyState(bool isVceDesert, float previousJoy, DefMap <JoyKindDef, float> tolerances)
 {
     IsVCEDesert       = isVceDesert;
     PreviousTolerance = previousJoy;
     Tolerances        = tolerances;
 }
Esempio n. 29
0
        public static void Rename(BasicBlock[] blocks)
        {
            DefMap[] globalDefs = new DefMap[blocks.Length];

            for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
            {
                globalDefs[blkIndex] = new DefMap();
            }

            Queue <BasicBlock> dfPhiBlocks = new Queue <BasicBlock>();

            // First pass, get all defs and locals uses.
            for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
            {
                Operand[] localDefs = new Operand[RegisterConsts.TotalCount];

                Operand RenameLocal(Operand operand)
                {
                    if (operand != null && operand.Type == OperandType.Register)
                    {
                        Operand local = localDefs[GetKeyFromRegister(operand.GetRegister())];

                        operand = local ?? operand;
                    }

                    return(operand);
                }

                BasicBlock block = blocks[blkIndex];

                LinkedListNode <INode> node = block.Operations.First;

                while (node != null)
                {
                    if (node.Value is Operation operation)
                    {
                        for (int index = 0; index < operation.SourcesCount; index++)
                        {
                            operation.SetSource(index, RenameLocal(operation.GetSource(index)));
                        }

                        for (int index = 0; index < operation.DestsCount; index++)
                        {
                            Operand dest = operation.GetDest(index);

                            if (dest.Type == OperandType.Register)
                            {
                                Operand local = Local();

                                localDefs[GetKeyFromRegister(dest.GetRegister())] = local;

                                operation.SetDest(index, local);
                            }
                        }
                    }

                    node = node.Next;
                }

                for (int index = 0; index < RegisterConsts.TotalCount; index++)
                {
                    Operand local = localDefs[index];

                    if (local == null)
                    {
                        continue;
                    }

                    Register reg = GetRegisterFromKey(index);

                    globalDefs[block.Index].TryAddOperand(reg, local);

                    dfPhiBlocks.Enqueue(block);

                    while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
                    {
                        foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
                        {
                            if (globalDefs[domFrontier.Index].AddPhi(reg))
                            {
                                dfPhiBlocks.Enqueue(domFrontier);
                            }
                        }
                    }
                }
            }

            // Second pass, rename variables with definitions on different blocks.
            for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
            {
                Operand[] localDefs = new Operand[RegisterConsts.TotalCount];

                BasicBlock block = blocks[blkIndex];

                Operand RenameGlobal(Operand operand)
                {
                    if (operand != null && operand.Type == OperandType.Register)
                    {
                        int key = GetKeyFromRegister(operand.GetRegister());

                        Operand local = localDefs[key];

                        if (local != null)
                        {
                            return(local);
                        }

                        operand = FindDefinitionForCurr(globalDefs, block, operand.GetRegister());

                        localDefs[key] = operand;
                    }

                    return(operand);
                }

                for (LinkedListNode <INode> node = block.Operations.First; node != null; node = node.Next)
                {
                    if (node.Value is Operation operation)
                    {
                        for (int index = 0; index < operation.SourcesCount; index++)
                        {
                            operation.SetSource(index, RenameGlobal(operation.GetSource(index)));
                        }
                    }
                }
            }
        }
Esempio n. 30
0
 public override void ResolveReferences()
 {
     joyGiverChances = new DefMap <JoyGiverDef, float>();
 }