Skip to content

Commit

Permalink
Add map ping (Ctrl + Alt + LeftClick) to mark position in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Mar 9, 2024
1 parent 5673272 commit 031e9c6
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 33 deletions.
79 changes: 64 additions & 15 deletions NebulaPatcher/Patches/Dynamic/GameCamera_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NebulaWorld;
using NebulaWorld.GameStates;
using NebulaWorld.MonoBehaviours.Local.Chat;
using UnityEngine;

#endregion

Expand All @@ -28,21 +29,25 @@ public static void Logic_Postfix(GameCamera __instance)
if (!Multiplayer.IsActive || GameStatesManager.DuringReconnect) return;

var observingPlayerId = Multiplayer.Session.Gizmos.ObservingPlayerId;
if (observingPlayerId == 0) return;
var observingPlanetId = Multiplayer.Session.Gizmos.ObservingPlanetId;
if (observingPlayerId == 0 && observingPlanetId == 0) return;

if (VFInput.escape)
{
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
ChatManager.Instance.SendChatMessage(
"Exit tracking mode".Translate(),
ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlanetId = 0;
VFInput.UseEscape();
return;
}

if (observingPlayerId > 0)
{
TrackingPlayer(__instance, observingPlayerId);
}
if (observingPlanetId > 0)
{
TrakcingPlanetPos(__instance, observingPlanetId, Multiplayer.Session.Gizmos.ObservingPos);
}
}

private static void TrackingPlayer(GameCamera cam, ushort playerId)
Expand All @@ -51,23 +56,67 @@ private static void TrackingPlayer(GameCamera cam, ushort playerId)
{
if (!models.TryGetValue(playerId, out var model))
{
ChatManager.Instance.SendChatMessage(
string.Format("Can't find player {0}".Translate(), playerId),
ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
StopTrackingPlayer(string.Format("Can't find player {0}".Translate(), playerId));
return;
}
var planetId = model.PlayerInstance.planetId;
var planetId = model.Movement.localPlanetId;
if (planetId > 0 && planetId != GameMain.mainPlayer.planetId)
{
ChatManager.Instance.SendChatMessage(
string.Format("Player {0} is on a different planet".Translate(), playerId),
ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
StopTrackingPlayer(string.Format("Player {0} is on a different planet".Translate(), playerId));
return;
}
var starId = model.Movement.LocalStarId;
if (planetId == 0 && (starId <= 0 || starId != GameMain.localStar?.id))
{
StopTrackingPlayer(string.Format("Player {0} is too far away".Translate(), playerId));
return;
}
cam.rtsTarget.position = model.PlayerTransform.position;
cam.rtsTarget.eulerAngles = Maths.SphericalRotation(cam.rtsTarget.position, 0f).eulerAngles;
if (GameMain.mainPlayer.planetId > 0)
{
if (planetId == 0 && Multiplayer.Session.IsClient)
{
StopTrackingPlayer(string.Format("Player {0} is too far away".Translate(), playerId));
return;
}
// The local player is on the planet and viewing another player on the same planet or in space
cam.rtsTarget.position = model.PlayerTransform.position;
cam.rtsTarget.eulerAngles = Maths.SphericalRotation(cam.rtsTarget.position, 0f).eulerAngles;
}
else
{
// Both players are in space
StopTrackingPlayer(string.Format("Player {0} is too far away".Translate(), playerId));
}
}
}

private static void StopTrackingPlayer(string text)
{
ChatManager.Instance.SendChatMessage(text, ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
}

private static void TrakcingPlanetPos(GameCamera cam, int planetId, Vector3 pos)
{
if (planetId > 0 && planetId != GameMain.mainPlayer.planetId)
{
var planet = GameMain.galaxy.PlanetById(planetId);
if (planet != null)
{
UIRoot.instance.uiGame.OpenStarmap();
var map = UIRoot.instance.uiGame.starmap;
var starIdx = planetId / 100 - 1;
map.focusStar = map.starUIs[starIdx];
map.focusPlanet = null;
map.OnCursorFunction2Click(0);
map.screenCameraController.SetViewTarget(planet, null, null, null, VectorLF3.zero,
planet.realRadius * 0.00025 * 6.0, planet.realRadius * 0.00025 * 160.0, true, false);
}
Multiplayer.Session.Gizmos.ObservingPlanetId = 0;
return;
}
cam.rtsTarget.position = pos;
cam.rtsTarget.eulerAngles = Maths.SphericalRotation(pos, 0f).eulerAngles;

}
}
104 changes: 100 additions & 4 deletions NebulaWorld/Chat/ChatLinks/NavigateChatLinkHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using System.Linq;
using NebulaModel.DataStructures.Chat;
using NebulaWorld.MonoBehaviours.Local.Chat;
using UnityEngine;
Expand All @@ -21,21 +20,90 @@ public void OnClick(string data)
case 2: // PlayerId
if (!ushort.TryParse(substrings[0], out var playerId)) return;
Multiplayer.Session.Gizmos.SetIndicatorPlayerId(playerId);
ChatManager.Instance.SendChatMessage("Starting navigation to ".Translate() + substrings[1],
ChatManager.Instance.SendChatMessage("Navigate to ".Translate() + substrings[1],
ChatMessageType.CommandOutputMessage);
break;

case 3: // AstroId
if (!int.TryParse(substrings[0], out var astroId)) return;
if (astroId < 0) return;

// Clear the guide line if choosing the same target
var previousAstro = GameMain.mainPlayer.navigation.indicatorAstroId;
GameMain.mainPlayer.navigation.indicatorAstroId = previousAstro != astroId ? astroId : 0;
break;

case 5: // PlanetPos
if (!TryParsePlanetPos(substrings, out var planetId, out var pos)) return;
Multiplayer.Session.Gizmos.SetIndicatorPing(planetId, pos);
break;
}
}

public void OnRightClick(string data)
{
var gizmoManager = Multiplayer.Session.Gizmos;
var substrings = data.Split(SplitSeparator);
switch (substrings.Length)
{
case 2: // PlayerId
if (!ushort.TryParse(substrings[0], out var playerId)) return;
Multiplayer.Session.Gizmos.ObservingPlayerId = playerId;
ChatManager.Instance.SendChatMessage("Start tracking to player (ESC to exit)".Translate(),
if (gizmoManager.ObservingPlayerId == playerId)
{
gizmoManager.ObservingPlayerId = 0;
return;
}
gizmoManager.ObservingPlayerId = playerId;
ChatManager.Instance.SendChatMessage("Tracking mode (ESC or click again to exit)".Translate(),
ChatMessageType.CommandOutputMessage);
break;

case 3: // AstroId
if (!int.TryParse(substrings[0], out var astorId)) return;
if (astorId <= 0) return;

// Open the starmap and focus on the target
UIRoot.instance.uiGame.OpenStarmap();
var starmap = UIRoot.instance.uiGame.starmap;
var planet = GameMain.galaxy.PlanetById(astorId);
var star = GameMain.galaxy.StarById(astorId / 100);
if (planet != null)
{
var starIdx = astorId / 100 - 1;
starmap.focusStar = starmap.starUIs[starIdx];
starmap.focusPlanet = null;
starmap.OnCursorFunction2Click(0);
starmap.screenCameraController.SetViewTarget(planet, null, null, null, VectorLF3.zero,
planet.realRadius * 0.00025 * 6.0, planet.realRadius * 0.00025 * 160.0, true, false);
}
else if (star != null)
{
var starIdx = astorId / 100 - 1;
starmap.focusPlanet = null;
starmap.focusStar = starmap.starUIs[starIdx];
starmap.OnCursorFunction2Click(0);
}
return;

case 5: // PlanetPos
if (!TryParsePlanetPos(substrings, out var planetId, out var pos)) return;
if (gizmoManager.ObservingPlanetId == planetId && gizmoManager.ObservingPos == pos)
{
gizmoManager.ObservingPlanetId = 0;
return;
}
gizmoManager.ObservingPlanetId = planetId;
gizmoManager.ObservingPos = pos;
if (GameCamera.instance.planetMode && planetId == GameMain.localPlanet?.id)
{
// In planet view, move the camera to aim at the target
var toRotate = Quaternion.FromToRotation(Vector3.up, pos);
var vector = toRotate * Vector3.up;
var normalized = Vector3.Cross(vector, Vector3.up).normalized;
toRotate = Quaternion.LookRotation(Vector3.Cross(normalized, vector), vector);
GameCamera.instance.planetPoser.rotationWanted = toRotate;
}
ChatManager.Instance.SendChatMessage("Tracking mode (ESC or click again to exit)".Translate(),
ChatMessageType.CommandOutputMessage);
break;
}
Expand Down Expand Up @@ -109,4 +177,32 @@ public static string FormatNavigateToPlayerString(ushort playerId, string displa
var data = playerId + SplitSeparator.ToString() + displayString;
return FormatNavigateString(data);
}

public static string FormatNavigateToAstro(int astroId, string displayString)
{
var data = astroId + SplitSeparator.ToString()
+ "astroId" + SplitSeparator.ToString() + displayString;
return FormatNavigateString(data);
}

public static string FormatNavigateToPlanetPos(int planetId, Vector3 pos, string displayString)
{
var data = planetId + SplitSeparator.ToString()
+ pos.x + SplitSeparator.ToString() + pos.y + SplitSeparator.ToString() + pos.z + SplitSeparator.ToString()
+ displayString;
return FormatNavigateString(data);
}

private static bool TryParsePlanetPos(string[] substrings, out int planetId, out Vector3 pos)
{
planetId = 0;
pos = Vector3.zero;
if (substrings.Length != 5) return false;
if (!int.TryParse(substrings[0], out planetId)) return false;
if (!float.TryParse(substrings[1], out var x)) return false;
if (!float.TryParse(substrings[2], out var y)) return false;
if (!float.TryParse(substrings[3], out var z)) return false;
pos = new Vector3(x, y, z);
return true;
}
}
4 changes: 2 additions & 2 deletions NebulaWorld/Chat/RichChatLinkRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public static string ParseRichText(string linkString, out string linkData)
{
linkData = "";
var splitStrings = linkString.Split(' ');
if (splitStrings.Length != 2)
if (splitStrings.Length < 2)
{
return "";
}

linkData = splitStrings[1];
linkData = linkString.Substring(splitStrings[0].Length + 1);
return splitStrings[0];
}

Expand Down
11 changes: 11 additions & 0 deletions NebulaWorld/MonoBehaviours/Local/Chat/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public void SendChatMessage(string text, ChatMessageType messageType)
chatWindow.SendLocalChatMessage(text, messageType);
}

public void InsetTextToChatbox(string text, bool forceOpenChatWindow)
{
if (chatWindow == null) return;
if (!chatWindow.IsActive)
{
if (!forceOpenChatWindow) return;
chatWindow.Toggle();
}
chatWindow.InsertText(text);
}

public bool IsPointerIn()
{
return chatWindow.DragTrigger.pointerIn;
Expand Down
8 changes: 8 additions & 0 deletions NebulaWorld/MonoBehaviours/Local/Chat/ChatWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ChatWindow : MonoBehaviour


internal string UserName;
public bool IsActive { get; private set; }


private void Awake()
Expand Down Expand Up @@ -283,6 +284,7 @@ public void Toggle(bool forceClosed = false, bool focusField = true)
var desiredStatus = !forceClosed && !chatWindow.activeSelf;
chatWindow.SetActive(desiredStatus);
notifier.gameObject.SetActive(!desiredStatus);
IsActive = desiredStatus;
if (chatWindow.activeSelf)
{
// when the window is activated we assume user wants to type right away
Expand Down Expand Up @@ -323,6 +325,12 @@ public void InsertSprite()
});
}

public void InsertText(string richText)
{
chatBox.Insert(richText);
FocusInputField();
}

public QueuedMessage GetQueuedMessage()
{
return outgoingMessages.Count > 0 ? outgoingMessages.Dequeue() : null;
Expand Down
1 change: 1 addition & 0 deletions NebulaWorld/MonoBehaviours/Remote/RemotePlayerMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private void MoveInterpolated(Snapshot previous, Snapshot current, float ratio)
var currentAbsolutePosition = GetAbsolutePosition(current);

localPlanetId = current.LocalPlanetId;
if (current.LocalPlanetId > 0) LocalStarId = current.LocalPlanetId / 100;

rootTransform.SetPositionAndRotation(Vector3.Lerp(previousRelativePosition, currentRelativePosition, ratio),
Quaternion.Slerp(previous.Rotation, current.Rotation, ratio));
Expand Down
Loading

0 comments on commit 031e9c6

Please sign in to comment.