protected override GrammarRequest GenerateGrammarRequest() { GrammarRequest result = base.GenerateGrammarRequest(); if (subjectPawn != null) { result.Rules.AddRange(GrammarUtility.RulesForPawn("SUBJECT", subjectPawn, result.Constants)); } else if (subjectThing != null) { result.Rules.AddRange(GrammarUtility.RulesForDef("SUBJECT", subjectThing)); } result.Includes.Add(transitionDef); if (initiator != null) { result.Rules.AddRange(GrammarUtility.RulesForPawn("INITIATOR", initiator, result.Constants)); } if (culpritHediffDef != null) { result.Rules.AddRange(GrammarUtility.RulesForHediffDef("CULPRITHEDIFF", culpritHediffDef, culpritHediffTargetPart)); } if (culpritHediffTargetPart != null) { result.Rules.AddRange(GrammarUtility.RulesForBodyPartRecord("CULPRITHEDIFF_target", culpritHediffTargetPart)); } if (culpritTargetPart != null) { result.Rules.AddRange(GrammarUtility.RulesForBodyPartRecord("CULPRITHEDIFF_originaltarget", culpritTargetPart)); } return(result); }
/// <summary> /// create the main log text /// </summary> /// <param name="pov">The pov.</param> /// <param name="forceLog">if set to <c>true</c> [force log].</param> /// <returns></returns> protected override string ToGameStringFromPOV_Worker(Thing pov, bool forceLog) { Assert(pov == _pawn, "pov == _pawn"); Rand.PushState(logID); // Does not need a MP-safe seed. try { GrammarRequest grammarRequest = GenerateGrammarRequest(); RulePackDef mutationRulePack = (_mutationDef as MutationDef)?.mutationLogRulePack; if (mutationRulePack != null) { grammarRequest.Includes.Add(mutationRulePack); } else { grammarRequest.Includes.Add(PMRulePackDefOf.GetDefaultPackForMutation(_mutationDef)); grammarRequest.Rules.Add(new Rule_String("DATE", GenDate.DateFullStringAt(ticksAbs, Vector2.zero))); } AddCustomRules(grammarRequest.Rules); IEnumerable <Rule> pawnR = GrammarUtility.RulesForPawn(PAWN_IDENTIFIER, _pawn, grammarRequest.Constants); BodyPartRecord partR = BodyDefOf.Human.AllParts.Where(r => _mutatedRecords.Contains(r.def)).RandomElement(); IEnumerable <Rule> partRules = GrammarUtility.RulesForBodyPartRecord(PART_LABEL, partR); IEnumerable <Rule> mutR = GrammarUtility.RulesForHediffDef(MUTATION_IDENTIFIER, _mutationDef, partR); // Add the rules. grammarRequest.Rules.AddRange(pawnR); grammarRequest.Rules.AddRange(mutR); grammarRequest.Rules.AddRange(partRules); return(GrammarResolver.Resolve(RP_ROOT_RULE, grammarRequest, "mutation log", forceLog)); } catch (Exception exception) { Log.Error($"encountered {exception.GetType().Name} exception while generating string for mutation log\n\t{exception}"); } finally { Rand.PopState(); // Make sure to always pop rand. } return(_mutationDef.LabelCap); //TODO generate string }
private static void AddSlateVar(ref GrammarRequest req, string absoluteName, object obj) { if (obj == null) { return; } if (obj is BodyPartRecord) { req.Rules.AddRange(GrammarUtility.RulesForBodyPartRecord(absoluteName, (BodyPartRecord)obj)); } else if (obj is Def) { req.Rules.AddRange(GrammarUtility.RulesForDef(absoluteName, (Def)obj)); } else if (obj is Faction) { Faction faction = (Faction)obj; req.Rules.AddRange(GrammarUtility.RulesForFaction(absoluteName, faction)); if (faction.leader != null) { req.Rules.AddRange(GrammarUtility.RulesForPawn(absoluteName + "_leader", faction.leader, req.Constants)); } } else if (obj is Pawn) { Pawn pawn = (Pawn)obj; req.Rules.AddRange(GrammarUtility.RulesForPawn(absoluteName, pawn, req.Constants)); if (pawn.Faction != null) { req.Rules.AddRange(GrammarUtility.RulesForFaction(absoluteName + "_faction", pawn.Faction)); } } else if (obj is WorldObject) { req.Rules.AddRange(GrammarUtility.RulesForWorldObject(absoluteName, (WorldObject)obj)); } else if (obj is Map) { req.Rules.AddRange(GrammarUtility.RulesForWorldObject(absoluteName, ((Map)obj).Parent)); } else if (obj is IntVec2) { req.Rules.Add(new Rule_String(absoluteName, ((IntVec2)obj).ToStringCross())); } else if (obj is IEnumerable && !(obj is string)) { if (obj is IEnumerable <Thing> ) { req.Rules.Add(new Rule_String(absoluteName, GenLabel.ThingsLabel(((IEnumerable <Thing>)obj).Where((Thing x) => x != null)))); } else if (obj is IEnumerable <Pawn> ) { req.Rules.Add(new Rule_String(absoluteName, GenLabel.ThingsLabel(((IEnumerable <Pawn>)obj).Where((Pawn x) => x != null).Cast <Thing>()))); } else if (obj is IEnumerable <object> && ((IEnumerable <object>)obj).Any() && ((IEnumerable <object>)obj).All((object x) => x is Thing)) { req.Rules.Add(new Rule_String(absoluteName, GenLabel.ThingsLabel(((IEnumerable <object>)obj).Where((object x) => x != null).Cast <Thing>()))); } else { List <string> list = new List <string>(); foreach (object item in (IEnumerable)obj) { if (item != null) { list.Add(item.ToString()); } } req.Rules.Add(new Rule_String(absoluteName, list.ToCommaList(useAnd: true))); } req.Rules.Add(new Rule_String(absoluteName + "_count", ((IEnumerable)obj).EnumerableCount().ToString())); int num = 0; foreach (object item2 in (IEnumerable)obj) { AddSlateVar(ref req, absoluteName + num, item2); num++; } } else { req.Rules.Add(new Rule_String(absoluteName, obj.ToString())); if (ConvertHelper.CanConvert <int>(obj)) { req.Rules.Add(new Rule_String(absoluteName + "_duration", ConvertHelper.Convert <int>(obj).ToStringTicksToPeriod(allowSeconds: true, shortForm: false, canUseDecimals: true, allowYears: false).Colorize(ColoredText.DateTimeColor))); } if (ConvertHelper.CanConvert <float>(obj)) { req.Rules.Add(new Rule_String(absoluteName + "_money", ConvertHelper.Convert <float>(obj).ToStringMoney())); } if (ConvertHelper.CanConvert <float>(obj)) { req.Rules.Add(new Rule_String(absoluteName + "_percent", ConvertHelper.Convert <float>(obj).ToStringPercent())); } if (ConvertHelper.CanConvert <FloatRange>(obj)) { AddSlateVar(ref req, absoluteName + "_average", ConvertHelper.Convert <FloatRange>(obj).Average); } if (ConvertHelper.CanConvert <FloatRange>(obj)) { AddSlateVar(ref req, absoluteName + "_min", ConvertHelper.Convert <FloatRange>(obj).min); } if (ConvertHelper.CanConvert <FloatRange>(obj)) { AddSlateVar(ref req, absoluteName + "_max", ConvertHelper.Convert <FloatRange>(obj).max); } } if (obj is Def) { if (!req.Constants.ContainsKey(absoluteName)) { req.Constants.Add(absoluteName, ((Def)obj).defName); } } else if (obj is Faction) { if (!req.Constants.ContainsKey(absoluteName)) { req.Constants.Add(absoluteName, ((Faction)obj).def.defName); } } else if ((obj.GetType().IsPrimitive || obj is string || obj.GetType().IsEnum) && !req.Constants.ContainsKey(absoluteName)) { req.Constants.Add(absoluteName, obj.ToString()); } if (obj is IEnumerable && !(obj is string)) { string key = absoluteName + "_count"; if (!req.Constants.ContainsKey(key)) { req.Constants.Add(key, ((IEnumerable)obj).EnumerableCount().ToString()); } } }