using System.Collections; using System.Collections.Generic; using UnityEngine; using Gaia; using SiegeSong; namespace SiegeSong { public class ActorLoader : MonoBehaviour { public string Input; public GameObject Species; public GameObject Instance; public InstanceManager InstanceManager; public Transform StartLocation; public GameObject PlayerTracker; public bool IsPlayer; public int ActorID; void Start() { if (Input.Length > 0) LoadActor2(); } void Update() { } public void LoadActor2(bool despawnOldInstance = false) { var stats = JsonUtility.FromJson(Input); if (Instance != null && despawnOldInstance) GameObject.Destroy(Instance); Instance = GameObject.Instantiate(Species); Instance.transform.position = StartLocation.position; if (Instance.GetComponent() == null) Instance.AddComponent(); var actor = Instance.GetComponent(); if (actor.Stats == null) actor.Stats = new Statistics(); actor.Stats.ID = ActorID; actor.ActorLoader = this; if (!Instance.active) Instance.active = true; if (stats.IsPlayer) { for (var j = 0; j < Instance.transform.childCount; j++) { if (Instance.transform.GetChild(j).name == "PlayerAgent") { Instance.transform.GetChild(j).gameObject.active = true; PlayerTracker.transform.parent = Instance.transform; } } } actor.Stats = stats; Instance.transform.eulerAngles = new Vector3(stats.RotationX, stats.RotationY, stats.RotationZ); Instance.transform.position = new Vector3(stats.LocationX, stats.LocationY, stats.LocationZ); for (var i = 0; i < stats.InventoryItemIDs.Count; i++) { actor.Inventory.SelectedItemID = stats.InventoryItemIDs[i]; actor.Inventory.SelectedAmount = stats.InventoryItemQuantities[i]; actor.Inventory.AddItem(); actor.Inventory.SelectedItemID = 0; actor.Inventory.SelectedAmount = 0; actor.Inventory.SelectedItemIndex = 0; } foreach (var itemId in stats.ActiveInventoryItemIDs) { if (actor.Inventory.GetItemByID(itemId).GetComponent() != null) actor.Inventory.EquipmentManager.AddEquipmentToSlots(actor.Inventory.GetItemByID(itemId)); } actor.Inventory.ApparrelManager.CurrentlyActiveIDs = stats.ActiveInventoryItemIDs.ToArray(); actor.Inventory.ApparrelManager.UpdateApparrel(); actor.Stats.InstanceID = InstanceManager.GenerateInstanceID(); Instance.gameObject.name = $"{actor.Stats.FirstName} {actor.Stats.LastName}"; InstanceManager.AddInstance(actor.Stats.InstanceID, Instance); if (stats.IsPlayer) { //InstanceManager.CurrentPlayerInstanceID = actor.Stats.InstanceID; //InstanceManager.RuntimeManager.PlayerInstance = Instance; //InstanceManager.RuntimeManager.PopulateReferences(); } } public void SaveActor2() { var stats = Instance.GetComponentInChildren().Stats; for (var j = 0; j < Instance.transform.childCount; j++) { if (Instance.transform.GetChild(j).name == "PlayerAgent" && Instance.transform.GetChild(j).gameObject.active) stats.IsPlayer = true; } var statsString = JsonUtility.ToJson(stats); Input = statsString; } public void SaveActor() { var actorDataString = "A-"; var value = ""; if (Instance.GetComponentInChildren().Stats.SkillValueCategories == null) Instance.GetComponentInChildren().Stats.SkillValueCategories = new Dictionary>(); if (Instance.GetComponentInChildren().Stats.RespectForActor == null) Instance.GetComponentInChildren().Stats.RespectForActor = new Dictionary(); if (Instance.GetComponentInChildren().Stats.AdmirationForActor == null) Instance.GetComponentInChildren().Stats.AdmirationForActor = new Dictionary(); if (Instance.GetComponentInChildren().Stats.SuspicionOfActor == null) Instance.GetComponentInChildren().Stats.SuspicionOfActor = new Dictionary(); if (Instance.GetComponentInChildren().Stats.FactionRank == null) Instance.GetComponentInChildren().Stats.FactionRank = new Dictionary(); if (Instance.GetComponentInChildren().Stats.RegionalBounty == null) Instance.GetComponentInChildren().Stats.RegionalBounty = new Dictionary(); if (Instance.GetComponentInChildren().Stats.RegionalFame == null) Instance.GetComponentInChildren().Stats.RegionalFame = new Dictionary(); if (Instance.GetComponentInChildren().Blendshapes == null) Instance.GetComponentInChildren().Blendshapes = new Dictionary(); if (Instance.GetComponentInChildren().Inventory.Contents == null) Instance.GetComponentInChildren().Inventory.Contents = new Dictionary(); foreach (var skillCategory in Instance.GetComponentInChildren().Stats.SkillValueCategories.Keys) { if (Instance.GetComponentInChildren().Stats.SkillValueCategories[skillCategory] == null) Instance.GetComponentInChildren().Stats.SkillValueCategories[skillCategory] = new Dictionary(); foreach (var skill in Instance.GetComponentInChildren().Stats.SkillValueCategories[skillCategory]) { actorDataString += $"sc{skillCategory.ToString("00")}_{skill.Key.ToString("00")}_{skill.Value.ToString("00")}/"; } } foreach (var id in Instance.GetComponentInChildren().Stats.RespectForActor.Keys) { actorDataString += $"ra{id}_{Instance.GetComponentInChildren().Stats.RespectForActor[id]}/"; value = Instance.GetComponentInChildren().Stats.RespectForActor[id] != null ? Instance.GetComponentInChildren().Stats.RespectForActor[id].ToString() : "0"; actorDataString += $"ra{id}_{value}/"; } foreach (var id in Instance.GetComponentInChildren().Stats.AdmirationForActor.Keys) { value = Instance.GetComponentInChildren().Stats.AdmirationForActor[id] != null ? Instance.GetComponentInChildren().Stats.AdmirationForActor[id].ToString() : "0"; actorDataString += $"aa{id}_{value}/"; } foreach (var id in Instance.GetComponentInChildren().Stats.SuspicionOfActor.Keys) { actorDataString += $"sa{id}_{Instance.GetComponentInChildren().Stats.SuspicionOfActor[id]}/"; value = Instance.GetComponentInChildren().Stats.SuspicionOfActor[id] != null ? Instance.GetComponentInChildren().Stats.SuspicionOfActor[id].ToString() : "0"; actorDataString += $"sa{id}_{value}/"; } foreach (var id in Instance.GetComponentInChildren().Stats.FactionRank.Keys) { actorDataString += $"fr{id}_{Instance.GetComponentInChildren().Stats.FactionRank[id]}/"; value = Instance.GetComponentInChildren().Stats.FactionRank[id] != null ? Instance.GetComponentInChildren().Stats.FactionRank[id].ToString() : "0"; actorDataString += $"fr{id}_{value}/"; } foreach (var id in Instance.GetComponentInChildren().Stats.RegionalBounty.Keys) { actorDataString += $"rb{id}_{Instance.GetComponentInChildren().Stats.RegionalBounty[id]}/"; value = Instance.GetComponentInChildren().Stats.RegionalBounty[id] != null ? Instance.GetComponentInChildren().Stats.RegionalBounty[id].ToString() : "0"; actorDataString += $"rb{id}_{value}/"; } foreach (var id in Instance.GetComponentInChildren().Stats.RegionalFame.Keys) { actorDataString += $"rf{id}_{Instance.GetComponentInChildren().Stats.RegionalFame[id]}/"; value = Instance.GetComponentInChildren().Stats.RegionalFame[id] != null ? Instance.GetComponentInChildren().Stats.RegionalFame[id].ToString() : "0"; actorDataString += $"rf{id}_{value}/"; } foreach (var blendshape in Instance.GetComponentInChildren().Blendshapes.Keys) { actorDataString += $"bs{blendshape}_{Instance.GetComponentInChildren().Blendshapes[blendshape]}/"; value = Instance.GetComponentInChildren().Blendshapes[blendshape] != null ? Instance.GetComponentInChildren().Blendshapes[blendshape].ToString() : "0"; actorDataString += $"bs{blendshape}_{value}/"; } actorDataString += $"pl{IsPlayer}/"; actorDataString += $"lx{Instance.transform.position.x.ToString("00000")}/"; actorDataString += $"ly{Instance.transform.position.y.ToString("00000")}/"; actorDataString += $"lz{Instance.transform.position.z.ToString("00000")}/"; actorDataString += $"hs{Instance.GetComponentInChildren().HairstyleID.ToString("00")}/"; actorDataString += $"fh{Instance.GetComponentInChildren().FacialHairID.ToString("00")}/"; actorDataString += $"fd{Instance.GetComponentInChildren().FacialDetailID.ToString("00")}/"; actorDataString += $"hr{Instance.GetComponentInChildren().HairColorR.ToString("000")}/"; actorDataString += $"hg{Instance.GetComponentInChildren().HairColorG.ToString("000")}/"; actorDataString += $"hb{Instance.GetComponentInChildren().HairColorB.ToString("000")}/"; actorDataString += $"ha{Instance.GetComponentInChildren().HairColorA.ToString("000")}/"; actorDataString += $"mm{Instance.GetComponentInChildren().Stats.MaxMagic}/"; actorDataString += $"ms{Instance.GetComponentInChildren().Stats.MaxStamina}/"; actorDataString += $"mh{Instance.GetComponentInChildren().Stats.MaxHealth}/"; foreach (var item in Instance.GetComponentInChildren().Inventory.Inventory.Contents) { if (item.Key != 0) { actorDataString += $"iid{item.Key.ToString("00")}"; //actorDataString += $"-ius{Instance.GetComponentInChildren().Inventory.GetItemByID(item.Key).GetComponent().Using}"; actorDataString += $"-icn{Instance.GetComponentInChildren().Inventory.GetItemByID(item.Key).GetComponent().PhysicalCondition.ToString("00")}"; actorDataString += $"-/"; } } Input = actorDataString; } public void LoadActor(bool despawnOldInstance = false) { if (IsPlayer) { PlayerTracker.transform.parent = null; } if (Instance != null && despawnOldInstance) GameObject.Destroy(Instance); Instance = GameObject.Instantiate(Species); Instance.transform.position = StartLocation.position; if (Instance.GetComponent().Stats == null) Instance.GetComponent().Stats = new Statistics(); Instance.GetComponent().Stats.ID = ActorID; Instance.GetComponent().ActorLoader = this; if (!Instance.active) Instance.active = true; if (IsPlayer) { PlayerTracker.transform.parent = Instance.transform; } var parsedText = ""; var parseStartIndex = 2; var id = -1; var key = ""; var subId = 0; var valueStartIndex = -1; var integerValue = 0; var floatValue = 0.0f; var itemId = -1; for (var i = parseStartIndex; i < Input.Length; i++) { if (Input[i] == '/') { parsedText = Input.Substring(parseStartIndex, i - parseStartIndex); parseStartIndex = i + 1; if (parsedText.Length > 2) { var parsedTextPrefix = parsedText.Substring(0, 2); switch (parsedTextPrefix) { case "sc": id = int.Parse(parsedText.Substring(2, 2)); subId = int.Parse(parsedText.Substring(5, 2)); integerValue = int.Parse(parsedText.Substring(8, 2)); if (Instance.GetComponent().Stats.SkillValueCategories == null) Instance.GetComponent().Stats.SkillValueCategories = new Dictionary>(); if (!Instance.GetComponent().Stats.SkillValueCategories.ContainsKey(id)) Instance.GetComponent().Stats.SkillValueCategories[id] = new Dictionary(); if (!Instance.GetComponent().Stats.SkillValueCategories[id].ContainsKey(subId)) Instance.GetComponent().Stats.SkillValueCategories[id].Add(subId, integerValue); else Instance.GetComponent().Stats.SkillValueCategories[id][subId] = integerValue; break; case "ra": valueStartIndex = parsedText.Substring(3, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.RespectForActor[id] = integerValue; break; case "aa": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.AdmirationForActor[id] = integerValue; break; case "sa": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.SuspicionOfActor[id] = integerValue; break; case "fr": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.FactionRank[id] = integerValue; break; case "rb": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.RegionalBounty[id] = integerValue; break; case "rf": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); id = int.Parse(parsedText.Substring(2, valueStartIndex)); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Stats.RegionalFame[id] = integerValue; break; case "bs": valueStartIndex = parsedText.Substring(2, 2).IndexOf('_'); key = parsedText.Substring(2, valueStartIndex); integerValue = int.Parse(parsedText.Substring(valueStartIndex)); Instance.GetComponent().Blendshapes[key] = floatValue; break; case "pl": IsPlayer = parsedText.Substring(2, 4) == "True"; for (var j = 0; j < Instance.transform.childCount; j++) { if (Instance.transform.GetChild(j).name == "PlayerAgent") Instance.transform.GetChild(j).gameObject.active = IsPlayer; } break; case "hs": id = int.Parse(parsedText.Substring(2, 2)); Instance.GetComponent().HairstyleID = id; break; case "fh": id = int.Parse(parsedText.Substring(2, 2)); Instance.GetComponent().FacialHairID = id; break; case "fd": id = int.Parse(parsedText.Substring(2, 2)); Instance.GetComponent().FacialDetailID = id; break; case "hr": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().HairColorR = id; break; case "hg": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().HairColorG = id; break; case "hb": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().HairColorB = id; break; case "ha": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().HairColorA = id; break; case "mm": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().Stats.MaxMagic = id; break; case "nm": Instance.GetComponent().Stats.FirstName = parsedText.Substring(2).Contains("_") ? parsedText.Substring(2, parsedText.IndexOf("_") - 2) : parsedText.Substring(2); if (parsedText.Substring(2).Contains("_")) Instance.GetComponent().Stats.LastName = parsedText.Substring(parsedText.IndexOf("_") + 1); Instance.name = parsedText.Substring(2).Replace("_", " "); break; case "ms": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().Stats.MaxStamina = id; break; case "mh": id = int.Parse(parsedText.Substring(2, 3)); Instance.GetComponent().Stats.MaxHealth = id; break; //case "lx": // if (parsedText[2] == '-') // integerValue = int.Parse(parsedText.Substring(2, 6)); // else // integerValue = int.Parse(parsedText.Substring(2, 5)); // Instance.transform.position = new Vector3((float)integerValue, Instance.transform.position.y, Instance.transform.position.z); // break; //case "ly": // if (parsedText[2] == '-') // integerValue = int.Parse(parsedText.Substring(2, 6)); // else // integerValue = int.Parse(parsedText.Substring(2, 5)); // Instance.transform.position = new Vector3(Instance.transform.position.x, (float)integerValue, Instance.transform.position.z); // break; //case "lz": // if (parsedText[2] == '-') // integerValue = int.Parse(parsedText.Substring(2, 6)); // else // integerValue = int.Parse(parsedText.Substring(2, 5)); // Instance.transform.position = new Vector3(Instance.transform.position.x, Instance.transform.position.y, (float)integerValue); break; case "ii": var parsedSubText = ""; var parseSubStartIndex = 0; itemId = int.Parse(parsedText.Substring(3, 2)); for (var j = parseSubStartIndex; j < parsedText.Length - 2; j++) { if (parsedText[j] == '-') { parsedSubText = parsedText.Substring(parseSubStartIndex, j - parseSubStartIndex); parseSubStartIndex = j + 1; if (parsedSubText.Length > 2) { var parsedSubTextPrefix = parsedSubText.Substring(0, 3); var subKey = ""; var subSubId = 0; var subValueStartIndex = -1; var subIntegerValue = 0; var subFloatValue = 0.0f; switch (parsedSubTextPrefix) { case "iid": itemId = int.Parse(parsedText.Substring(3, 2)); Instance.GetComponent().Inventory.SelectedItemID = itemId; Instance.GetComponent().Inventory.SelectedAmount = 1; Instance.GetComponent().Inventory.AddItem(); Instance.GetComponent().Inventory.SelectedItemID = 0; Instance.GetComponent().Inventory.SelectedAmount = 0; Instance.GetComponent().Inventory.SelectedItemIndex = 0; break; case "icn": Instance.GetComponent().Inventory.GetItemByID(itemId).GetComponentInChildren().PhysicalCondition = integerValue; integerValue = int.Parse(parsedSubText.Substring(3, 2)); break; case "ius": var boolValue = parsedSubText.Substring(3, 4) == "True"; //Instance.GetComponent().Inventory.GetItemByID(itemId).GetComponentInChildren().Using = boolValue; if (boolValue) { var newArray = new int[Instance.GetComponent().Inventory.ApparrelManager.CurrentlyActiveIDs.Length + 1]; Instance.GetComponent().Inventory.ApparrelManager.CurrentlyActiveIDs.CopyTo(newArray, 1); newArray[0] = itemId; Instance.GetComponent().Inventory.ApparrelManager.CurrentlyActiveIDs = newArray; Instance.GetComponent().Inventory.ApparrelManager.UpdateApparrel(); if (Instance.GetComponent().Inventory.GetItemByID(itemId).GetComponent() != null) Instance.GetComponent().Inventory.EquipmentManager.AddEquipmentToSlots(Instance.GetComponent().Inventory.GetItemByID(itemId)); } break; } } } } break; } } } } Instance.GetComponent().Stats.InstanceID = InstanceManager.GenerateInstanceID(); InstanceManager.AddInstance(Instance.GetComponent().Stats.InstanceID, Instance); if (IsPlayer) { InstanceManager.CurrentPlayerInstanceID = Instance.GetComponent().Stats.InstanceID; //InstanceManager.RuntimeManager.PlayerInstance = Instance; //InstanceManager.RuntimeManager.PopulateReferences(); } } } }