Ejemplo n.º 1
0
        public static AIModelMetaData Merge(AIModelMetaData oldData, AIModelMetaData newData)
        {
            var oldBehaviourNameMap = oldData.OffsetToBehaviourNameMap;
            var newBehaviourNameMap = newData.OffsetToBehaviourNameMap;

            foreach (var kv in oldBehaviourNameMap)
            {
                if (!newBehaviourNameMap.ContainsKey(kv.Key))
                {
                    newBehaviourNameMap.Add(kv.Key, kv.Value);
                }
            }

            var oldPersoNameMap = oldData.OffsetToPersoNamesMap;
            var newPersoNameMap = newData.OffsetToPersoNamesMap;

            foreach (var kv in oldPersoNameMap)
            {
                if (!newPersoNameMap.ContainsKey(kv.Key))
                {
                    newPersoNameMap.Add(kv.Key, kv.Value);
                }
            }

            var initialRules    = new List <string>();
            var initialReflexes = new List <string>();

            initialRules.AddRange(oldData.InitialRules);
            initialRules.AddRange(newData.InitialRules);

            initialReflexes.AddRange(oldData.InitialReflexes);
            initialReflexes.AddRange(newData.InitialReflexes);

            return(new AIModelMetaData(newData.Offset, newData.Map, newData.Name, initialRules.Distinct().ToArray(), initialReflexes.Distinct().ToArray(), newData.DsgVars, newBehaviourNameMap,
                                       newPersoNameMap));
        }
Ejemplo n.º 2
0
        private void ExportRawAIModels(string path)
        {
            void ExportScript(string path, Behavior b, int i, Script s)
            {
                string filePathRaw = Path.Combine(path, i + ".osb");

                if (File.Exists(filePathRaw))
                {
                    File.Delete(filePathRaw);
                }

                Directory.CreateDirectory(Path.GetDirectoryName(filePathRaw));

                File.WriteAllBytes(filePathRaw, s.GetNodeBytes());
            }

            ExportTextTableJson(path);

            Dictionary <(string model, string family), AIModel> aiModelFamilyCombination = new Dictionary <(string, string), AIModel>();
            Dictionary <AIModel, List <string> > defaultRules    = new Dictionary <AIModel, List <string> >();
            Dictionary <AIModel, List <string> > defaultReflexes = new Dictionary <AIModel, List <string> >();

            var persos = loader.persos;

            //persos.AddRange(loader.globals.spawnablePersos); maybe needed?

            foreach (var perso in persos)
            {
                if (perso?.brain?.mind?.AI_model == null)
                {
                    continue;
                }

                if (!defaultRules.ContainsKey(perso.brain.mind.AI_model))
                {
                    defaultRules.Add(perso.brain.mind.AI_model, new List <string>()
                    {
                        perso.brain.mind.intelligenceNormal?.defaultComport?.NameSubstring ?? ""
                    });
                }
                else
                {
                    defaultRules[perso.brain.mind.AI_model].Add(perso.brain.mind.intelligenceNormal?.defaultComport?.NameSubstring ?? "");
                }
                if (!defaultReflexes.ContainsKey(perso.brain.mind.AI_model))
                {
                    defaultReflexes.Add(perso.brain.mind.AI_model, new List <string>()
                    {
                        perso.brain.mind.intelligenceReflex?.defaultComport?.NameSubstring ?? ""
                    });
                }
                else
                {
                    defaultReflexes[perso.brain.mind.AI_model].Add(perso.brain.mind.intelligenceReflex?.defaultComport?.NameSubstring ?? "");
                }

                var combination = (perso.nameModel, perso.nameFamily);
                if (!aiModelFamilyCombination.ContainsKey(combination))
                {
                    aiModelFamilyCombination.Add(combination, perso.brain.mind.AI_model);
                }
            }

            foreach (var aiModelKV in aiModelFamilyCombination)
            {
                string exportAIModelName = aiModelKV.Key.model + "__" + aiModelKV.Key.family;
                string aiModelDir        = Path.Combine(path, exportAIModelName);
                var    aiModel           = aiModelKV.Value;
                Directory.CreateDirectory(aiModelDir);

                ExportStringPointers(aiModelDir);

                if (aiModel.behaviors_normal != null)
                {
                    foreach (var b in aiModel.behaviors_normal)
                    {
                        int i = 0;

                        string behaviorPath = Path.Combine(aiModelDir, "rule", b.NameSubstring);
                        Directory.CreateDirectory(behaviorPath);

                        if (b.scheduleScript != null && !b.scripts.Contains(b.scheduleScript))
                        {
                            ExportScript(behaviorPath, b, i++, b.scheduleScript);
                        }

                        foreach (var s in b.scripts)
                        {
                            ExportScript(behaviorPath, b, i++, s);
                        }
                    }
                }

                if (aiModel.behaviors_reflex != null)
                {
                    foreach (var b in aiModel.behaviors_reflex)
                    {
                        int i = 0;

                        string behaviorPath = Path.Combine(aiModelDir, "reflex", b.NameSubstring);
                        Directory.CreateDirectory(behaviorPath);

                        if (b.scheduleScript != null && !b.scripts.Contains(b.scheduleScript))
                        {
                            ExportScript(behaviorPath, b, i++, b.scheduleScript);
                        }

                        foreach (var s in b.scripts)
                        {
                            ExportScript(behaviorPath, b, i++, s);
                        }
                    }
                }

                if (aiModel.macros != null)
                {
                    //int i = 0;
                    foreach (var m in aiModel.macros)
                    {
                        var script = m.script;

                        if (script == null)
                        {
                            continue;
                        }

                        string filePathRaw = Path.Combine(aiModelDir, "macros",
                                                          m.NameSubstring + ".osb");
                        if (File.Exists(filePathRaw))
                        {
                            File.Delete(filePathRaw);
                        }

                        Directory.CreateDirectory(Path.GetDirectoryName(filePathRaw));

                        File.WriteAllBytes(filePathRaw, script.GetNodeBytes());
                    }
                }

                var metaFilePath = Path.Combine(aiModelDir, "aimodel.json");

                AIModelMetaData?oldMetaData = null;

                if (File.Exists(metaFilePath))
                {
                    oldMetaData = JsonConvert.DeserializeObject <AIModelMetaData>(File.ReadAllText(metaFilePath));
                }

                var aiModelMetaData = AIModelMetaData.FromAIModel(aiModel, exportAIModelName, defaultRules[aiModel].ToArray(), defaultReflexes[aiModel].ToArray());

                if (oldMetaData != null)
                {
                    aiModelMetaData = AIModelMetaData.Merge(oldMetaData.Value, aiModelMetaData);
                }

                var metaJSON = JsonConvert.SerializeObject(aiModelMetaData, JsonExportSettings);
                File.WriteAllText(metaFilePath, metaJSON);
            }
        }