public void loadBehaviourScopes()
        {
            scopesByRole = new Dictionary <string, BehaviorVariableScope>();
            foreach (BehaviorRoleDef behaviourDef in Main.settings.behaviours)
            {
                string filePath = $"{Main.modDir}/{Main.settings.behaviourDirectory}/{behaviourDef.behaviourFile}.json";
                if (File.Exists(filePath))
                {
                    string jData = File.ReadAllText(filePath);

                    BehaviorVariableScope behaviorVariableScope = getScope(behaviourDef.roleTag);
                    if (behaviorVariableScope == null)
                    {
                        behaviorVariableScope = new BehaviorVariableScope();
                        behaviorVariableScope.FromJSON(jData);
                        scopesByRole[behaviourDef.roleTag] = behaviorVariableScope;
                        Main.modLog.LogMessage($"Loaded {behaviourDef.behaviourFile} for tag: {behaviourDef.roleTag}");
                    }

                    foreach (AIMood mood in behaviourDef.moods)
                    {
                        if (mood != AIMood.Undefined)
                        {
                            behaviorVariableScope.ScopesByMood[mood] = new BehaviorVariableScope();
                            behaviorVariableScope.ScopesByMood[mood].FromJSON(jData);
                            Main.modLog.LogMessage($"Applied {behaviourDef.behaviourFile} for tag: {behaviourDef.roleTag}, with Mood {mood.ToString()}");
                        }
                    }
                }
                else
                {
                    Main.modLog.LogError($"Missing Behaviour file: {behaviourDef.behaviourFile}");
                }
            }
        }
        static void logBVsForScope(string scopeName, BehaviorVariableScope scope)
        {
            logger.Log(string.Format(" ** Behavior variables on the {0} **", scopeName));
            List <BehaviorVariableName> variables = scope.VariableNames;

            for (int bvIndex = 0; bvIndex < variables.Count; ++bvIndex)
            {
                BehaviorVariableValue bvv = scope.GetVariable(variables[bvIndex]);
                logger.Log(bvToString(variables[bvIndex], bvv));
            }
        }
Exemplo n.º 3
0
        public void OnBehaviorVariableScopeLoaded(string id, string json)
        {
            ScopeDesc             scopeDescription = scopeDescriptions.Find(item => item.Name == id);
            BehaviorVariableScope scope            = null;

            switch (scopeDescription.ScopeKind)
            {
            case ScopeKind.Global:
                scope = globalBehaviorVariableScope;
                break;

            case ScopeKind.Faction:
                scope = scopesByFaction[scopeDescription.FactionValue.ID];
                break;

            case ScopeKind.UnitRole:
                scope = scopesByRole[scopeDescription.UnitRole];
                break;

            case ScopeKind.Personality:
                scope = scopesByAIPersonality[scopeDescription.AIPersonality];
                break;

            case ScopeKind.SkillBased:
                scope = scopesByAISkill[scopeDescription.AISkillID];
                break;

            default:
                Debug.LogError("unhandled scopeKind: " + scopeDescription.ScopeKind);
                break;
            }
            if (scopeDescription.Mood != AIMood.Undefined)
            {
                scope = scope.ScopesByMood[scopeDescription.Mood];
            }
            scope.FromJSON(json);
        }
        private static BehaviorVariableValue GetBehaviorVariableValueDirectly(BehaviorTree bTree, BehaviorVariableName name)
        {
            BehaviorVariableValue behaviorVariableValue = bTree.unitBehaviorVariables.GetVariable(name);

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

            Pilot pilot = bTree.unit.GetPilot();

            if (pilot != null)
            {
                BehaviorVariableScope scopeForAIPersonality = bTree.unit.Combat.BattleTechGame.BehaviorVariableScopeManager.GetScopeForAIPersonality(pilot.pilotDef.AIPersonality);
                if (scopeForAIPersonality != null)
                {
                    behaviorVariableValue = scopeForAIPersonality.GetVariableWithMood(name, bTree.unit.BehaviorTree.mood);
                    if (behaviorVariableValue != null)
                    {
                        return(behaviorVariableValue);
                    }
                }
            }

            if (bTree.unit.lance != null)
            {
                behaviorVariableValue = bTree.unit.lance.BehaviorVariables.GetVariable(name);
                if (behaviorVariableValue != null)
                {
                    return(behaviorVariableValue);
                }
            }

            if (bTree.unit.team != null)
            {
                Traverse bvT = Traverse.Create(bTree.unit.team).Field("BehaviorVariables");
                BehaviorVariableScope bvs = bvT.GetValue <BehaviorVariableScope>();
                behaviorVariableValue = bvs.GetVariable(name);
                if (behaviorVariableValue != null)
                {
                    return(behaviorVariableValue);
                }
            }

            UnitRole unitRole = bTree.unit.DynamicUnitRole;

            if (unitRole == UnitRole.Undefined)
            {
                unitRole = bTree.unit.StaticUnitRole;
            }

            BehaviorVariableScope scopeForRole = bTree.unit.Combat.BattleTechGame.BehaviorVariableScopeManager.GetScopeForRole(unitRole);

            if (scopeForRole != null)
            {
                behaviorVariableValue = scopeForRole.GetVariableWithMood(name, bTree.unit.BehaviorTree.mood);
                if (behaviorVariableValue != null)
                {
                    return(behaviorVariableValue);
                }
            }

            if (bTree.unit.CanMoveAfterShooting)
            {
                BehaviorVariableScope scopeForAISkill = bTree.unit.Combat.BattleTechGame.BehaviorVariableScopeManager.GetScopeForAISkill(AISkillID.Reckless);
                if (scopeForAISkill != null)
                {
                    behaviorVariableValue = scopeForAISkill.GetVariableWithMood(name, bTree.unit.BehaviorTree.mood);
                    if (behaviorVariableValue != null)
                    {
                        return(behaviorVariableValue);
                    }
                }
            }

            behaviorVariableValue = bTree.unit.Combat.BattleTechGame.BehaviorVariableScopeManager.GetGlobalScope().GetVariableWithMood(name, bTree.unit.BehaviorTree.mood);
            if (behaviorVariableValue != null)
            {
                return(behaviorVariableValue);
            }

            return(DefaultBehaviorVariableValue.GetSingleton());
        }
Exemplo n.º 5
0
        public BehaviorVariableScopeManager(GameInstance gameInstance)
        {
            scopeDescriptions = new List <ScopeDesc> {
                new ScopeDesc("global", AIMood.Undefined),
                new ScopeDesc("global_def", AIMood.Defensive),
                new ScopeDesc("global_sensorlock", AIMood.SensorLocking),
                new ScopeDesc("global_ruthless", AIMood.Ruthless),
                new ScopeDesc("role_brawler", AIMood.Undefined, UnitRole.Brawler),
                new ScopeDesc("role_brawler_def", AIMood.Defensive, UnitRole.Brawler),
                new ScopeDesc("role_ecmcarrier", AIMood.Undefined, UnitRole.EcmCarrier),
                new ScopeDesc("role_ecmcarrier_def", AIMood.Defensive, UnitRole.EcmCarrier),
                new ScopeDesc("role_ewe", AIMood.Undefined, UnitRole.Ewe),
                new ScopeDesc("role_ewe_def", AIMood.Defensive, UnitRole.Ewe),
                new ScopeDesc("role_activeprobe", AIMood.Undefined, UnitRole.ActiveProbe),
                new ScopeDesc("role_activeprobe_def", AIMood.Defensive, UnitRole.ActiveProbe),
                new ScopeDesc("role_sniper", AIMood.Undefined, UnitRole.Sniper),
                new ScopeDesc("role_sniper_def", AIMood.Defensive, UnitRole.Sniper),
                new ScopeDesc("role_scout", AIMood.Undefined, UnitRole.Scout),
                new ScopeDesc("role_scout_def", AIMood.Defensive, UnitRole.Scout),
                new ScopeDesc("role_lastmanstanding", AIMood.Undefined, UnitRole.LastManStanding),
                new ScopeDesc("role_lastmanstanding_def", AIMood.Defensive, UnitRole.LastManStanding),
                new ScopeDesc("role_meleeonly", AIMood.Undefined, UnitRole.MeleeOnly),
                new ScopeDesc("role_meleeonly_def", AIMood.Defensive, UnitRole.MeleeOnly),
                new ScopeDesc("role_noncombatant", AIMood.Undefined, UnitRole.NonCombatant),
                new ScopeDesc("role_noncombatant_def", AIMood.Defensive, UnitRole.NonCombatant),
                new ScopeDesc("role_turret", AIMood.Undefined, UnitRole.Turret),
                new ScopeDesc("role_turret_def", AIMood.Defensive, UnitRole.Turret),
                new ScopeDesc("role_vehicle", AIMood.Undefined, UnitRole.Vehicle),
                new ScopeDesc("role_vehicle_def", AIMood.Defensive, UnitRole.Vehicle),
            };

            List <FactionValue> factionList = FactionEnumeration.AIBehaviorVariableScopeList;

            for (int i = 0; i < factionList.Count; ++i)
            {
                FactionValue faction = factionList[i];
                if (faction.HasAIBehaviorVariableScope)
                {
                    string undefined = string.Format("faction_{0}", faction.Name.ToLower());
                    string defensive = string.Format("{0}_def", undefined);
                    scopeDescriptions.Add(new ScopeDesc(undefined, AIMood.Undefined, faction));
                    scopeDescriptions.Add(new ScopeDesc(defensive, AIMood.Defensive, faction));
                }
            }

            scopeDescriptions.Add(new ScopeDesc("personality_disciplined", AIMood.Undefined, AIPersonality.Disciplined));
            scopeDescriptions.Add(new ScopeDesc("personality_disciplined_def", AIMood.Defensive, AIPersonality.Disciplined));
            scopeDescriptions.Add(new ScopeDesc("personality_aggressive", AIMood.Undefined, AIPersonality.Aggressive));
            scopeDescriptions.Add(new ScopeDesc("personality_aggressive_def", AIMood.Defensive, AIPersonality.Aggressive));
            scopeDescriptions.Add(new ScopeDesc("personality_qapersonality", AIMood.Undefined, AIPersonality.QAPersonality));
            scopeDescriptions.Add(new ScopeDesc("personality_qapersonality_def", AIMood.Defensive, AIPersonality.QAPersonality));
            scopeDescriptions.Add(new ScopeDesc("skill_reckless", AIMood.Undefined, AISkillID.Reckless));
            scopeDescriptions.Add(new ScopeDesc("skill_reckless_def", AIMood.Defensive, AISkillID.Reckless));

            scopesByRole          = new Dictionary <UnitRole, BehaviorVariableScope>();
            scopesByFaction       = new Dictionary <int, BehaviorVariableScope>();
            scopesByAIPersonality = new Dictionary <AIPersonality, BehaviorVariableScope>();
            scopesByAISkill       = new Dictionary <AISkillID, BehaviorVariableScope>();

            LoadRequest loadRequest = gameInstance.DataManager.CreateLoadRequest();

            for (int i = 0; i < scopeDescriptions.Count; ++i)
            {
                ScopeDesc scopeDescription = scopeDescriptions[i];
                loadRequest.AddLoadRequest <string>(BattleTechResourceType.BehaviorVariableScope, scopeDescription.Name, OnBehaviorVariableScopeLoaded);

                switch (scopeDescription.ScopeKind)
                {
                case ScopeKind.Global:
                    if (scopeDescription.Mood == AIMood.Undefined)
                    {
                        globalBehaviorVariableScope = new BehaviorVariableScope();
                    }
                    else
                    {
                        globalBehaviorVariableScope.ScopesByMood[scopeDescription.Mood] = new BehaviorVariableScope();
                    }
                    break;

                case ScopeKind.UnitRole:
                    if (scopeDescription.Mood == AIMood.Undefined)
                    {
                        scopesByRole[scopeDescription.UnitRole] = new BehaviorVariableScope();
                    }
                    else
                    {
                        scopesByRole[scopeDescription.UnitRole].ScopesByMood[scopeDescription.Mood] = new BehaviorVariableScope();
                    }
                    break;

                case ScopeKind.Faction:
                    if (scopeDescription.Mood == AIMood.Undefined)
                    {
                        scopesByFaction[scopeDescription.FactionValue.ID] = new BehaviorVariableScope();
                    }
                    else
                    {
                        scopesByFaction[scopeDescription.FactionValue.ID].ScopesByMood[scopeDescription.Mood] = new BehaviorVariableScope();
                    }
                    break;

                case ScopeKind.Personality:
                    if (scopeDescription.Mood == AIMood.Undefined)
                    {
                        scopesByAIPersonality[scopeDescription.AIPersonality] = new BehaviorVariableScope();
                    }
                    else
                    {
                        scopesByAIPersonality[scopeDescription.AIPersonality].ScopesByMood[scopeDescription.Mood] = new BehaviorVariableScope();
                    }
                    break;

                case ScopeKind.SkillBased:
                    if (scopeDescription.Mood == AIMood.Undefined)
                    {
                        scopesByAISkill[scopeDescription.AISkillID] = new BehaviorVariableScope();
                    }
                    else
                    {
                        scopesByAISkill[scopeDescription.AISkillID].ScopesByMood[scopeDescription.Mood] = new BehaviorVariableScope();
                    }
                    break;
                }
            }

            loadRequest.ProcessRequests();
        }