using Audio; using Harmony; using Platform; using System; using System.Collections; using UAI; using UnityEngine; using static SleeperVolume; public class XUiC_HiredNPC : XUiController { public string name; public int EntityID = -1; private float distance; public Vector3 position; private EntityNPCRebirth hiredNPC { get; set; } private readonly CachedStringFormatterXuiRgbaColor itemicontintcolorFormatter = new CachedStringFormatterXuiRgbaColor(); private readonly CachedStringFormatter distanceFormatter = new CachedStringFormatter(delegate(float _f) { if (_f > 1000f) { return (_f / 1000f).ToCultureInvariantString("0.0") + " KM"; } return _f.ToCultureInvariantString("0.0") + " M"; }); private XUiC_SimpleButton btnDismiss; private XUiC_SimpleButton btnTeleport; //private XUiC_SimpleButton btnTeleportTo; private XUiC_SimpleButton btnWaypoint; private XUiC_SimpleButton btnBedroll; private float updateLimiter; public override void Init() { base.Init(); IsDirty = true; btnDismiss = (XUiC_SimpleButton)GetChildById("btnDismiss"); if (btnDismiss != null) btnDismiss.OnPressed += BtnDismiss_Controller_OnPress; btnTeleport = (XUiC_SimpleButton)GetChildById("btnTeleport"); if (btnTeleport != null) btnTeleport.OnPressed += BtnTeleport_Controller_OnPress; /*btnTeleportTo = (XUiC_SimpleButton)GetChildById("btnTeleportTo"); if (btnTeleportTo != null) btnTeleportTo.OnPressed += BtnTeleportTo_Controller_OnPress;*/ btnWaypoint = (XUiC_SimpleButton)GetChildById("btnWaypoint"); if (btnWaypoint != null) btnWaypoint.OnPressed += BtnWaypoint_Controller_OnPress; btnBedroll = (XUiC_SimpleButton)GetChildById("btnBedroll"); if (btnBedroll != null) btnBedroll.OnPressed += BtnBedroll_Controller_OnPress; } private void BtnTeleport_Controller_OnPress(XUiController _sender, int _mousebutton) { if (hiredNPC != null) { hiredNPC.HideNPC(false); hiredNPC.guardPosition = Vector3.zero; hiredNPC.attackTarget = null; RebirthManager.UpdateHireInfo(hiredNPC.entityId, "order", "follow"); hiredNPC.Buffs.SetCustomVar("CurrentOrder", (int)EntityUtilities.Orders.Follow); RebirthUtilities.toggleCollisions(false, hiredNPC); hiredNPC.SetPosition(xui.playerUI.entityPlayer.position); } } private void BtnWaypoint_Controller_OnPress(XUiController _sender, int _mousebutton) { Waypoint _wp = new Waypoint(); _wp.pos = new Vector3i(position.x, position.y, position.z); _wp.icon = "ui_game_symbol_npc"; _wp.name.Update(name, PlatformManager.MultiPlatform.User.PlatformUserId); _wp.ownerId = (PlatformUserIdentifierAbs)null; //_wp.entityId = -1; _wp.bIsAutoWaypoint = true; _wp.bUsingLocalizationId = true; //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress _wp.name: " + _wp.name); //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress _wp.pos: " + _wp.pos); //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress _wp.icon: " + _wp.icon); for (int index = xui.playerUI.entityPlayer.Waypoints.Collection.list.Count - 1; index >= 0; --index) { Waypoint waypoint = xui.playerUI.entityPlayer.Waypoints.Collection.list[index]; //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress waypoint.name: " + waypoint.name); //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress waypoint.pos: " + waypoint.pos); //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress waypoint.icon: " + waypoint.icon); if (xui.playerUI.entityPlayer.Waypoints.ContainsWaypoint((Waypoint)_wp)) { //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress 1"); Manager.PlayInsidePlayerHead("misc/missingitemtorepair"); GameManager.ShowTooltip((EntityPlayerLocal)xui.playerUI.entityPlayer, Localization.Get("ttWaypointAlreadyExists")); return; } if (waypoint.pos == _wp.pos && waypoint.name == _wp.name ) { //Log.Out("XUiC_HiredNPC-BtnWaypoint_Controller_OnPress 2"); Manager.PlayInsidePlayerHead("misc/missingitemtorepair"); GameManager.ShowTooltip((EntityPlayerLocal)xui.playerUI.entityPlayer, Localization.Get("ttWaypointAlreadyExists")); return; } } NavObject navObject = NavObjectManager.Instance.RegisterNavObject("waypoint", (Vector3)_wp.pos, _wp.icon, true); navObject.UseOverrideColor = true; navObject.OverrideColor = Color.white; navObject.IsActive = true; navObject.name = _wp.name.Text; navObject.usingLocalizationId = true; _wp.navObject = navObject; xui.playerUI.entityPlayer.Waypoints.Collection.Add(_wp); } /*private void BtnTeleportTo_Controller_OnPress(XUiController _sender, int _mousebutton) { bool isClient = SingletonMonoBehaviour.Instance.IsClient; if (isClient) { SingletonMonoBehaviour.Instance.SendToServer(NetPackageManager.GetPackage().Setup(xui.playerUI.entityPlayer.entityId, position), false); } else { //Log.Out("XUiC_HiredNPC-BtnTeleportTo_Controller_OnPress position: " + position); //float height1 = GameManager.Instance.World.GetTerrainHeight((int)position.x, (int)position.z); float height1 = GameManager.Instance.World.GetHeight((int)position.x, (int)position.z); float height2 = GameManager.Instance.World.GetHeight((int)position.x, (int)position.z); float height = ((height1 + height2) / 2f) + 1.5f; //Log.Out("XUiC_HiredNPC-BtnTeleportTo_Controller_OnPress height1: " + height1); //Log.Out("XUiC_HiredNPC-BtnTeleportTo_Controller_OnPress height2: " + height2); //Log.Out("XUiC_HiredNPC-BtnTeleportTo_Controller_OnPress height: " + height); if (height <= position.y) { xui.playerUI.entityPlayer.SetPosition(position); } else { xui.playerUI.entityPlayer.SetPosition(new Vector3(position.x, height, position.x)); } } }*/ private void BtnDismiss_Controller_OnPress(XUiController _sender, int _mousebutton) { var uiforPlayer = LocalPlayerUI.GetUIForPlayer(xui.playerUI.entityPlayer); if (uiforPlayer != null) { xui.playerUI.entityPlayer.Buffs.SetCustomVar("CurrentNPC", this.EntityID); uiforPlayer.windowManager.Open("DismissEntity", true, false, true); RebirthVariables.NPCName = this.name; } this.ViewComponent.IsVisible = false; } private void BtnBedroll_Controller_OnPress(XUiController _sender, int _mousebutton) { SpawnPosition spawnPoint = RebirthUtilities.GetSpawnPoint(xui.playerUI.entityPlayer); if (!spawnPoint.IsUndef()) { bool isClient = SingletonMonoBehaviour.Instance.IsClient; if (isClient) { SingletonMonoBehaviour.Instance.SendToServer(NetPackageManager.GetPackage().Setup(EntityID, spawnPoint.position), false); } else { if (hiredNPC != null) { hiredNPC.HideNPC(false); hiredNPC.guardPosition = Vector3.zero; hiredNPC.attackTarget = null; RebirthManager.UpdateHireInfo(hiredNPC.entityId, "order", "stay", spawnPoint.position.ToString(), new Vector3(0, hiredNPC.rotation.y, 0).ToString()); hiredNPC.Buffs.SetCustomVar("CurrentOrder", (int)EntityUtilities.Orders.Stay); RebirthManager.UpdateHireInfo(hiredNPC.entityId, "reSpawnPosition", "", Vector3.zero.ToString(), new Vector3(0, hiredNPC.rotation.y, 0).ToString()); RebirthManager.UpdateHireInfo(hiredNPC.entityId, "spawnPosition", "", spawnPoint.position.ToString(), new Vector3(0, hiredNPC.rotation.y, 0).ToString()); RebirthManager.UpdateHireInfo(hiredNPC.entityId, "order", "guard", Vector3.zero.ToString(), Vector3.zero.ToString()); hiredNPC.SetPosition(new Vector3(spawnPoint.position.x, spawnPoint.position.y + 1f, spawnPoint.position.z)); GameManager.Instance.StartCoroutine(RebirthUtilities.pauseSleep(hiredNPC, 1f)); } } } else { Manager.PlayInsidePlayerHead("misc/missingitemtorepair"); GameManager.ShowTooltip((EntityPlayerLocal)xui.playerUI.entityPlayer, Localization.Get("ttMissingBedroll")); } } public override void Update(float _dt) { base.Update(_dt); updateLimiter -= _dt; if (!(updateLimiter < 0f)) return; updateLimiter = 1f; UpdateStatus(); RefreshBindings(true); } public void SetCompanion(EntityNPCRebirth entity) { hiredNPC = entity; if (hiredNPC == null) { RefreshBindings(true); return; } UpdateStatus(); RefreshBindings(true); IsDirty = true; } private void UpdateStatus() { if (hiredNPC == null) return; var entityPlayer = xui.playerUI.entityPlayer; var magnitude = (hiredNPC.GetPosition() - entityPlayer.GetPosition()).magnitude; distance = magnitude; } public override void OnOpen() { base.OnOpen(); IsDirty = true; RefreshBindings(true); } public override bool GetBindingValue(ref string value, string bindingName) { if (!XUi.IsGameRunning() || xui.playerUI.entityPlayer == null || !LocalPlayerUI.GetUIForPlayer(xui.playerUI.entityPlayer).windowManager.IsWindowOpen("ManageNPCs")) { return true; } switch (bindingName) { case "name" when hiredNPC == null: value = this.name; btnTeleport.IsVisible = false; return true; case "name": value = hiredNPC.EntityName; if (hiredNPC.Buffs.GetCustomVar("$FR_NPC_Respawn") == 0f && !(hiredNPC.bMine || hiredNPC.bRepair)) { btnTeleport.IsVisible = true; } return true; case "currentorder": if (hiredNPC == null) { value = ""; //Log.Out("XUiC_HiredNPC-GetBindingValue value: " + value); return true; } string currentOrder = "tt" + (EntityUtilities.Orders)hiredNPC.Buffs.GetCustomVar("CurrentOrder"); //Log.Out("XUiC_HiredNPC-GetBindingValue currentOrder: " + currentOrder); //Log.Out("XUiC_HiredNPC-GetBindingValue hiredNPC.bMine: " + hiredNPC.bMine); if (hiredNPC.Buffs.GetCustomVar("$FR_NPC_Respawn") == 1f) { value = "[cf8888]" + Localization.Get("ttInactive") + "[-]"; } else if (hiredNPC.bMine) { value = "[cf8888]" + Localization.Get("ttMining") + "[-]"; } else if (hiredNPC.bRepair) { value = "[cf8888]" + Localization.Get("ttRepairing") + "[-]"; } else { value = Localization.Get(currentOrder); } //Log.Out("XUiC_HiredNPC-GetBindingValue value: " + value); return true; case "distancecolor": { var color2 = Color.white; if (hiredNPC == null) { value = ""; return true; } var leader = EntityUtilities.GetLeaderOrOwner(hiredNPC.entityId); if (leader == null) { value = ""; return true; } if (distance > 50f) { color2 = new Color(207f / 255f, 136f / 255f, 136f / 255f); } value = itemicontintcolorFormatter.Format(color2); return true; } case "distance": { if (hiredNPC == null) { value = ""; return true; } value = distanceFormatter.Format(this.distance); return true; } case "position": { value = ""; if (hiredNPC == null) { value = "[cf8888]" + Localization.Get("ttCannotBeFound") + "[-]\n" + position.ToString(); btnWaypoint.IsVisible = true; btnBedroll.IsVisible = false; return true; } if (hiredNPC.bMine || hiredNPC.bRepair) { return true; } btnWaypoint.IsVisible = false; btnBedroll.IsVisible = true; value = hiredNPC.position.ToString(); return true; } case "type": { if (hiredNPC == null) { value = ""; return true; } if (hiredNPC.HasAnyTags(FastTags.Parse("survivor"))) { value = Localization.Get("ttSurvivor"); } else { value = Localization.Get(hiredNPC.EntityClass.entityClassName); } return true; } case "level": { if (hiredNPC == null) { value = ""; return true; } value = hiredNPC.Buffs.GetCustomVar("$FR_NPC_Level").ToString(); return true; } case "kills": { if (hiredNPC == null) { value = ""; return true; } value = hiredNPC.Buffs.GetCustomVar("$varNumKills").ToString(); return true; } case "weapon": { if (hiredNPC == null) { value = ""; return true; } if (hiredNPC.EntityClass.Properties.Values.ContainsKey("BagItems")) { value = Localization.Get(hiredNPC.EntityClass.Properties.Values["BagItems"]); } else { value = ""; } return true; } default: return false; } } }