Skip to content

Commit

Permalink
Merge pull request #275 from InvalidArgument3/the-questening
Browse files Browse the repository at this point in the history
The questening multiplayerening
  • Loading branch information
InvalidArgument3 authored Dec 2, 2024
2 parents 80b7421 + 920aec3 commit db35c92
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
86 changes: 64 additions & 22 deletions DeltaVFactionQuestLog/Data/Scripts/PersistentFactionObjectives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
using System.Collections.Generic;
using System;
using VRage.Game;
using ProtoBuf;

namespace DeltaVQuestLog
namespace Invalid.DeltaVQuestLog
{
[ProtoContract]
public class QuestLogMessage
{
[ProtoMember(1)]
public long FactionId { get; set; }
[ProtoMember(2)]
public List<string> Objectives { get; set; }
[ProtoMember(3)]
public string Title { get; set; }
[ProtoMember(4)]
public int Duration { get; set; }
}

[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
public class PersistentFactionObjectives : MySessionComponentBase
Expand All @@ -24,31 +37,24 @@ public class PersistentFactionObjectives : MySessionComponentBase

public override void LoadData()
{
isServer = MyAPIGateway.Multiplayer.IsServer; // True for server instances (single-player, listen server host, dedicated server)
bool isDedicated = MyAPIGateway.Utilities.IsDedicated; // True only for dedicated servers
isServer = MyAPIGateway.Multiplayer.IsServer;
bool isDedicated = MyAPIGateway.Utilities.IsDedicated;

if (isServer && !isDedicated)
{
// Single-player or listen server host
LoadObjectives();
}
else if (isDedicated)
{
// Dedicated server
LoadObjectives();
}

// Register chat commands for all instances
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered;

// Register player connect events on the server side (for dedicated and listen servers)
if (isServer)
{
MyVisualScriptLogicProvider.PlayerConnected += OnPlayerConnected;
}
MyAPIGateway.Multiplayer.RegisterMessageHandler(QuestLogMessageId, OnQuestLogMessageReceived);
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered;

}

protected override void UnloadData()
Expand Down Expand Up @@ -123,6 +129,9 @@ private void OnMessageEntered(string messageText, ref bool sendToOthers)
{
if (!messageText.StartsWith("/obj") && !messageText.StartsWith("/objective")) return;

// Only process commands on the client side
if (isServer && !MyAPIGateway.Multiplayer.IsServer) return;

sendToOthers = false;

var args = messageText.Split(' ');
Expand Down Expand Up @@ -202,16 +211,25 @@ private void HandleAddObjective(string[] args, long factionId, long playerId)
factionObjectives[factionId] = new List<string>();
}

if (!factionObjectives[factionId].Contains(objectiveText)) // Avoid duplicates
if (!factionObjectives[factionId].Contains(objectiveText))
{
factionObjectives[factionId].Add(objectiveText);
SaveObjectives();

// Get the player's name
string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown";
string title = $"Faction Objectives [Added by {playerName}: {objectiveText}]";

// Show updated quest log to all faction members
ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Added by {playerName}: {objectiveText}]");
// In singleplayer or on client, show directly
if (!MyAPIGateway.Utilities.IsDedicated)
{
DisplayQuestLog(factionObjectives[factionId], title, playerId);
}

// If server, broadcast to all faction members
if (isServer)
{
ShowQuestLogToFaction(factionId, 30, title);
}

MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} added objective: {objectiveText}");
}
Expand Down Expand Up @@ -286,11 +304,20 @@ private void HandleRemoveObjective(string[] args, long factionId, long playerId)
factionObjectives[factionId].RemoveAt(index - 1);
SaveObjectives();

// Get the player's name
string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown";
string title = $"Faction Objectives [Removed by {playerName}: {removedObjective}]";

// In singleplayer or on client, show directly
if (!MyAPIGateway.Utilities.IsDedicated)
{
DisplayQuestLog(factionObjectives[factionId], title, playerId);
}

// Show updated quest log to all faction members
ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Removed by {playerName}: {removedObjective}]");
// If server, broadcast to all faction members
if (isServer)
{
ShowQuestLogToFaction(factionId, 30, title);
}

MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} removed objective: {removedObjective}");
}
Expand All @@ -313,8 +340,17 @@ private void HandleBroadcast(string[] args, long factionId)
var playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown";
string customTitle = $"Faction Objectives [Broadcasted by {playerName}, {duration}s]";

// Broadcast to faction members
ShowQuestLogToFaction(factionId, duration, customTitle);
// In singleplayer or on client, show directly
if (!MyAPIGateway.Utilities.IsDedicated)
{
DisplayQuestLog(factionObjectives[factionId], customTitle, MyAPIGateway.Session.Player.IdentityId);
}

// If server, broadcast to all faction members
if (isServer)
{
ShowQuestLogToFaction(factionId, duration, customTitle);
}

MyAPIGateway.Utilities.ShowMessage("Objectives", $"Broadcasting objectives for {duration} seconds by {playerName}.");
}
Expand Down Expand Up @@ -382,11 +418,10 @@ private void HandleClearObjectives(long factionId, long playerId)
factionObjectives[factionId].Clear();
SaveObjectives();

// Get the player's name
string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown";

// Notify all faction members about the cleared objectives
ShowQuestLogToFaction(factionId, 10, $"[Cleared by {playerName}]");
// Update to show empty quest log immediately after clearing
ShowQuestLogToFaction(factionId, 30, $"Faction Objectives [Cleared by {playerName}]");

MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} cleared all objectives.");
}
Expand Down Expand Up @@ -443,8 +478,15 @@ private void ShowQuestLogForPlayer(long factionId, long playerId, int duration,

private void DisplayQuestLog(List<string> objectives, string title, long playerId)
{
if (objectives == null || objectives.Count == 0)
{
MyVisualScriptLogicProvider.SetQuestlog(false, "", playerId);
return;
}

MyVisualScriptLogicProvider.SetQuestlog(true, title, playerId);
MyVisualScriptLogicProvider.RemoveQuestlogDetails(playerId);

foreach (var objective in objectives)
{
MyVisualScriptLogicProvider.AddQuestlogObjective(objective, false, true, playerId);
Expand Down
22 changes: 0 additions & 22 deletions DeltaVFactionQuestLog/Data/Scripts/QuestLogMessage.cs

This file was deleted.

0 comments on commit db35c92

Please sign in to comment.