public bool _CanBeParentOf(PawnMask mask)
#endif
        {
            //var list = Attributes.Select((arg1, arg2) => new { source = arg1, index = arg2 });
            //var specifiedList = list.Where((arg) => !arg.source.Unspecified);

            //var text = "";
            //foreach (var current in specifiedList)
            //{
            //	text += "\n" + current.source.Value.ToString() + "=" + mask.Attributes[current.index].Value.ToString();
            //}

            //Log.Message("CanBeParentOf() :" + text);

            //if (specifiedList.Any((arg) => mask.Attributes[arg.index].Unspecified))
            //	return false;

            //return specifiedList.All((arg) => mask.Attributes[arg.index].Value == arg.source.Value);

            for (int i = 0; i < Attributes.Length; i++)
            {
                var parent = Attributes[i];
                var child  = mask.Attributes[i];

                if (!parent.Matches(child.Value))
                {
                    return(false);
                }
            }

            return(true);

            //Attributes.Where((MaskAttribute arg) => !arg.Unspecified).All((MaskAttribute arg) => arg.);
        }
        public bool CanBeParentOrEqualOf(PawnMask mask)
#if DEBUG
        {
            var result = _CanBeParentOf(mask);

            //Log.Message(string.Format("CanBeParentOf() {0} | {1} = {2}", this, mask, result));
            return(result);
        }
        public static PawnMask MakeCompleteMaskFromPawn(Pawn pawn)
        {
            var mask = new PawnMask();

            if (pawn.IsAscetic())
            {
                mask.ascetic.Value = true;
            }

            if (pawn.IsCannibal())
            {
                mask.cannibal.Value = true;
            }

            if (pawn.IsIncapacitated())
            {
                mask.incapacitated.Value = true;
            }

            if (pawn.IsPrisonerOfColony)
            {
                mask.factionCategory.Value = PawnMaskFaction.Prisoner;
            }
            else if (!pawn.Faction.IsPlayer && !pawn.Faction.HostileTo(Faction.OfPlayer))
            {
                mask.factionCategory.Value = PawnMaskFaction.Friendly;
            }
            else if (pawn.IsColonist || pawn.Faction.IsPlayer)
            {
                mask.factionCategory.Value = PawnMaskFaction.Colonist;
            }
            else
            {
                goto invalidpawn;
            }

            if (pawn.RaceProps.Humanlike)
            {
                mask.factionCategory.Value = PawnMaskType.Human;
            }
            else if (pawn.RaceProps.Animal)
            {
                if (pawn.Faction != null && !pawn.isWildAnimal())
                {
                    mask.factionCategory.Value = PawnMaskType.Pet;
                }
                else
                {
                    goto invalidpawn;
                }
            }

            return(mask);

invalidpawn:
            throw new Exception("Trying to create a mask for an invalid pawn: " + pawn);
        }
        internal static PawnMask Parse(string syntax, Policy policy)
        {
            var commands   = syntax.Split(':');
            var maskSyntax = commands.Last();
            var maskFlags  = maskSyntax.Split('/');

            PawnMask mask = new PawnMask();

            foreach (var flagName in maskFlags)
            {
                var attribute = mask.Attributes.FirstOrDefault((arg) => arg.GetValueNamed(flagName) != null);

                if (attribute != null)
                {
                    attribute.SetValueNamed(flagName);
                }
                else
                {
                    goto error;
                }
            }

            if (commands.Any((arg) => arg.CaseUnsensitiveCompare("default")))
            {
                mask.targetDefault = policy;
            }

            if (!mask.factionCategory.Unspecified && ((PawnMaskFaction)mask.factionCategory.Value == PawnMaskFaction.Colonist ||
                                                      (PawnMaskFaction)mask.factionCategory.Value == PawnMaskFaction.Prisoner)
                ||
                (!mask.ascetic.Unspecified && (bool)mask.ascetic.Value) ||
                (!mask.cannibal.Unspecified && (bool)mask.cannibal.Value))
            {
                mask.pawnType.Value = PawnMaskType.Human;
            }

            return(mask);

error:
            throw new Exception("Wrong mask format: " + syntax);
        }
Example #5
0
        //TODO: print more about syntax errors
        private void _DefsLoaded()
        {
            if (unrestricted)
            {
                allowUnlisted = true;
            }

            //assignablePawns.DefsLoaded();

            if (label == "" || label == null)
            {
                label = string.Format("Policy #{1} for {0}", (conditions != null && conditions.Any()) ? string.Join(" / ", conditions.ToArray()) : "everyone", PoliciesCount);
            }

            // ----------- Parse masks -----------

            if (conditions != null)
            {
                foreach (var textmask in conditions)
                {
                    pawnMasks.Add(PawnMask.Parse(textmask, this));
                }
                var pawnMasks2 = pawnMasks.Distinct().ToList();

                if (pawnMasks2.Count() < pawnMasks.Count())
                {
                    var list = pawnMasks2.Where((arg) => !pawnMasks.Any((arg2) => arg2 == arg)).Select((arg) => arg.ToString());
                    Log.Warning("Policy " + label + " has redundant conditions: " + list);
                }

                pawnMasks = pawnMasks2;
            }

            // ----------- Parse base diet -----------

            if (diet != null && diet.Any())
            {
                //	var diet = new Diet();
                var dietElements = Regex.Replace(diet, "[\n\r\t ]", "").Split('/');

                if (dietElements.Any())
                {
                    foreach (var item in dietElements)
                    {
                        float offset        = Diet.DietElement.DefaultOffset;
                        var   levelElements = item.Split('=');

                        foreach (var item2 in levelElements)
                        {
                            bool  customOffsetDefined = false;
                            float num;

                            //not implemented
                            if (float.TryParse(item2, out num))
                            {
                                if (customOffsetDefined)
                                {
                                    Log.Warning("Diet level has several custom offset values set in policy " + this.ToString());
                                }

                                offset = num;
                                customOffsetDefined = true;
                            }
                            else
                            {
                                baseDiet.Add(new Diet.DietElement()
                                {
                                    foodCategory = (FoodCategory)Enum.Parse(typeof(FoodCategory), item2),
                                    scoreOffset  = 0
                                });
                            }
                        }

                        //todo: fix custom offset
                        baseDiet.Last().scoreOffset = -Math.Abs(offset);
                    }
                }
            }

            // ----------- Create diets for every related races -----------

            bool hasCustomPawnDefsSet = targetPawnDefs.Any();

            foreach (var current in DefDatabaseHelper.AllPawnDefs)
            {
                if (AdmitsPawnDef(current))
                {
                    if (!hasCustomPawnDefsSet)
                    {
                        targetPawnDefs.Add(current);
                    }

                    PerRacesDiet.Add(current, new Diet(current, this));
                }
            }

            PoliciesCount++;
        }
        internal static void AssignToAllPawnsWithMaskOnMap(Policy policy, PawnMask mask)
        {
            Func <Pawn, bool> validator = (arg) => mask.MatchesPawn(arg);

            AssignToAllPawnsMatchingOnMap(policy, validator);
        }