Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
0
        private void ParseTraceLine(string line, string[] words)
        {
            switch (words[0])
            {
            case "[mk-quant]":
            {
                Term[] args = GetArgs(3, words);

                /*
                 * if (words[2] == "not" && args[0].Name == "or") {
                 *  words[2] = "And";
                 *  args = NegateAll(args[0].Args);
                 * }
                 */

                Term t = new Term("FORALL" + words[1], args);
                model.terms[words[1]] = t;

                if (args.Length != 0)
                {
                    Quantifier q = CreateQuantifier(words[1], words[2]);
                    q.BodyTerm = t;
                    if (words[2] != "null")
                    {
                        q.PrintName = words[2] + "[" + words[1] + "]";
                    }
                    else
                    {
                        q.PrintName = words[1];
                    }
                    q.ComputeBody();
                }
            } break;

            case "[mk-app]": {
                Term[] args = GetArgs(3, words);

                /*
                 * if (words[2] == "not" && args[0].Name == "or") {
                 *  words[2] = "And";
                 *  args = NegateAll(args[0].Args);
                 * }
                 */

                Term t = new Term(words[2], args);
                model.terms[words[1]] = t;
            } break;

            case "[attach-enode]": {
                Term t   = GetTerm(words[1]);
                int  gen = int.Parse(words[2]);
                if (lastInst != null && t.Responsible != lastInst)
                {
                    // make a copy of the term, if we were going to override the Responsible field
                    t = new Term(t);
                    model.terms[words[1]] = t;
                }
                if (lastInst != null)
                {
                    t.Responsible = lastInst;
                }
            }  break;

            case "[new-match]": {
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (words.Length < 3)
                {
                    break;
                }
                Instantiation inst      = new Instantiation();
                Term[]        args      = GetArgs(3, words);
                int           firstNull = Array.IndexOf(args, null);
                if (firstNull < 0)
                {
                    inst.Bindings    = args;
                    inst.Responsible = EmptyTerms;
                }
                else
                {
                    inst.Bindings    = new Term[firstNull];
                    inst.Responsible = new Term[args.Length - firstNull - 1];
                    Array.Copy(args, 0, inst.Bindings, 0, inst.Bindings.Length);
                    Array.Copy(args, firstNull + 1, inst.Responsible, 0, inst.Responsible.Length);
                }

                inst.Quant = CreateQuantifier(words[2], words[2]);
                model.fingerprints[words[1]] = inst;
            } break;

            case "[instance]": {
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                Instantiation inst;
                if (!model.fingerprints.TryGetValue(words[1], out inst))
                {
                    System.Console.WriteLine("fingerprint not found {0} {1}", words[0], words[1]);
                    break;
                }
                // model.fingerprints.Remove(words[1]);
                if (inst.LineNo != 0)
                {
                    var tmp = new Instantiation();
                    inst.CopyTo(tmp);
                    inst = tmp;
                }
                AddInstance(inst);
                int pos = 2;
                if (words.Length > pos && words[pos] != ";")
                {
                    long id = GetId(words[pos]);
                    model.proofSteps[id] = inst;
                    pos++;
                }
                if (words.Length - 1 > pos && words[pos] == ";")
                {
                    ++pos;
                    inst.Z3Generation = int.Parse(words[pos]);
                    ++pos;
                }
                else
                {
                    inst.Z3Generation = -1;
                }
            } break;

            case "[end-of-instance]": lastInst = null; break;


            case "[decide-and-or]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (words.Length >= 2)
                {
                    decideClause = GetTerm(words[1]);
                }
                break;

            // we're getting [assign] anyhow
            case "[decide]": break;

            case "[assign]": {
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions || words.Length < 2)
                {
                    break;
                }
                ScopeDesc d = model.scopes[model.scopes.Count - 1];
                Literal   l = GetLiteral(words[1], false);
                if (d.Literal == null)
                {
                    d.Literal = l;
                }
                else
                {
                    d.Implied.Add(l);
                }
                int pos = 2;
                if (pos < words.Length && words[pos] == "decision")
                {
                    pos++;
                }
                if (pos < words.Length)
                {
                    string kw = words[pos++];
                    switch (kw)
                    {
                    case "clause":
                    case "bin-clause":
                        Term[] expl = new Term[words.Length - pos];
                        for (int i = 0; i < expl.Length; ++i)
                        {
                            expl[i] = GetLiteralTerm(words[pos++]);
                        }
                        l.Explanation = expl;
                        break;

                    case "justification": break;

                    default:
                        if (kw != "axiom")
                        {
                            l.Explanation = new Term[] { GetTerm(kw) }
                        }
                        ;
                        break;
                    }
                }
            } break;

            case "[push]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (!skipDecisions)
                {
                    model.PushScope();
                }
                break;

            case "[pop]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions || words.Length < 2)
                {
                    break;
                }
                model.PopScopes(int.Parse(words[1]), curConfl);
                curConfl = null;
                break;

            case "[begin-check]":
                beginCheckSeen++;
                interestedInCurrentCheck = checkToConsider == beginCheckSeen;
                if (beginCheckSeen > 1 && checkToConsider == 1)
                {
                    Console.WriteLine("More than a single search log in the file, sticking to the first one; use /c:N option to override");
                }
                break;

            case "[query-done]":
                if (interestedInCurrentCheck)
                {
                    eofSeen++;
                }
                break;

            case "[eof]":
                eofSeen++;
                break;

            case "[resolve-process]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions || words.Length < 2)
                {
                    break;
                }
                currResNode      = new ResolutionLiteral();
                currResNode.Term = GetLiteralTerm(words[1], out currResNode.Negated, out currResNode.Id);
                if (currResRoot == null)
                {
                    currResRoot = currResNode;
                }
                else
                {
                    var t = currResRoot.Find(currResNode.Id);
                    if (t != null)
                    {
                        currResNode = t;
                    }
                    else
                    {
                        Console.WriteLine("cannot attach to conflict {0}", words[1]);
                    }
                }
                break;

            case "[resolve-lit]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions || words.Length < 3 || currResNode == null)
                {
                    break;
                }
                {
                    var l = new ResolutionLiteral();
                    l.Term            = GetLiteralTerm(words[2], out l.Negated, out l.Id);
                    l.LevelDifference = int.Parse(words[1]);
                    currResNode.Results.Add(l);
                }
                break;

            case "[conflict]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions)
                {
                    break;
                }
                curConfl                = new Conflict();
                curConfl.Id             = cnflCount++;
                curConfl.ResolutionLits = cnflResolveLits.ToArray();
                cnflResolveLits.Clear();
                curConfl.LineNo = curlineNo;
                curConfl.Cost   = curlineNo;
                if (model.conflicts.Count > 0)
                {
                    curConfl.Cost -= model.conflicts[model.conflicts.Count - 1].LineNo;
                }
                model.conflicts.Add(curConfl);
                curConfl.ResolutionRoot = currResRoot;
                currResRoot             = null;
                currResNode             = null;
                for (int i = 1; i < words.Length; ++i)
                {
                    string  w   = words[i];
                    Literal lit = GetLiteral(w, false);
                    if (lit != null)
                    {
                        curConfl.Literals.Add(lit);
                    }
                }
                break;

            // obsolete
            case "[mk-enode]":
            case "[mk-bool-var]": {
                int generation;

                if (words.Length < 3)
                {
                    break;
                }
                words = StripGeneration(words, out generation);
                Term[] args = GetArgs(3, words);
                Term   t;
                if (lastInst == null &&
                    model.terms.TryGetValue(words[1], out t) &&
                    t.Name == words[2] && t.Args.Length == args.Length &&
                    ForAll2(t.Args, args, delegate(Term x, Term s) { return(x == s); }))
                {
                    // nothing
                }
                else
                {
                    t                     = new Term(words[2], args);
                    t.Responsible         = lastInst;
                    model.terms[words[1]] = t;
                }
            } break;

            // V1 stuff
            case "[create]": {
                if (words.Length < 4)
                {
                    break;
                }
                Term[] args = GetArgs(4, words);
                Term   t    = new Term(args.Length == 0 ? words[3] : words[3].Substring(1), args);
                t.Responsible         = lastInst;
                model.terms[words[1]] = t;
            } break;

            case "[fingerprint]": {
                if (words.Length < 3)
                {
                    break;
                }
                Instantiation inst = new Instantiation();
                inst.Responsible             = GetArgs(3, words);
                model.fingerprints[words[1]] = inst;
            } break;

            case "[mk_const]": if (words.Length < 2)
                {
                    break;
                }
                model.terms.Remove("#" + words[1]); break;

            case "[create_ite]": if (words.Length < 2)
                {
                    break;
                }
                model.terms.Remove("#" + words[1]); break;

            case "[done-instantiate-fp]": lastInst = null; break;

            case "[instantiate-fp]": {
                if (words.Length < 4)
                {
                    break;
                }
                Instantiation inst;
                if (!model.fingerprints.TryGetValue(words[2], out inst))
                {
                    System.Console.WriteLine("fingerprint not found {0}", words[0]);
                    break;
                }
                if (inst.Quant != null)
                {
                    //Console.WriteLine("multi inst");
                    break;
                }
                inst.Quant = CreateQuantifier(words[1], words[1]);
                AddInstance(inst);
                for (int i = 4; i < words.Length; ++i)
                {
                    words[i] = words[i].Substring(words[i].IndexOf(':') + 1);
                }
                inst.Bindings = GetArgs(4, words);
            } break;

            case "[conflict-resolve]":
                if (!interestedInCurrentCheck)
                {
                    break;
                }
                if (skipDecisions)
                {
                    break;
                }
                cnflResolveLits.Add(GetLiteral(words[1], true));
                break;

            case "[conflict-lit]":
                if (curConfl != null && words.Length >= 2)
                {
                    Literal lit = new Literal();
                    curConfl.Literals.Add(lit);
                    int pos = 1;
                    if (words[pos] == "(not")
                    {
                        lit.Negated = true;
                        pos++;
                    }
                    if (words.Length <= pos)
                    {
                        break;
                    }
                    string no = words[pos].Substring(1).Replace(":", "").Replace(")", "");
                    if (!int.TryParse(no, out lit.Id))
                    {
                        Console.WriteLine("cannot get literal number of {0}", no);
                    }
                    pos++;
                    if (words.Length <= pos)
                    {
                        break;
                    }
                    if (words[pos] == "not")
                    {
                        pos++;
                    }
                    if (words.Length <= pos)
                    {
                        break;
                    }
                    string sym = words[pos].Replace("(", "");
                    pos++;
                    if (sym != "FORALL")
                    {
                        for (int i = pos; i < words.Length; i++)
                        {
                            int idx = words[i].IndexOf(":");
                            if (idx > 0)
                            {
                                words[i] = words[i].Substring(0, idx);
                            }
                        }
                        lit.Term = new Term(sym, GetArgs(pos, words));
                    }
                }
                break;

            case "[end-conflict]": break;


            case "[used]": break;

            case "WARNING:": break;

            default:
                Console.WriteLine("wrong line: '{0}'", line);
                break;
            }
        }