Skip to content

Commit

Permalink
n_MagazineUpdate, n_TargetingUpdate, and HeartNetwork.SendToEveryoneI…
Browse files Browse the repository at this point in the history
…nSync
  • Loading branch information
ari-steas committed Feb 28, 2024
1 parent 0058d1a commit 19bdd58
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using VRage.Game.ModAPI;
using VRageMath;

namespace Heart_Module.Data.Scripts.HeartModule.Network
{
Expand Down Expand Up @@ -84,6 +85,23 @@ public void SendToEveryone(PacketBase packet, byte[] serialized = null)
RelayToClients(packet, HeartData.I.SteamId, serialized);
}

public void SendToEveryoneInSync(PacketBase packet, Vector3D position, byte[] serialized = null)
{
List<ulong> toSend = new List<ulong>();
foreach (var player in HeartData.I.Players)
if (Vector3D.DistanceSquared(player.GetPosition(), position) <= HeartData.I.SyncRangeSq)
toSend.Add(player.SteamUserId);

if (toSend.Count == 0)
return;

if (serialized == null)
serialized = MyAPIGateway.Utilities.SerializeToBinary(packet);

foreach (var clientSteamId in toSend)
RelayToClient(packet, clientSteamId, HeartData.I.SteamId, serialized);
}

public void SendToServer(PacketBase packet, byte[] serialized = null)
{
RelayToServer(packet, HeartData.I.SteamId, serialized);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Heart_Module.Data.Scripts.HeartModule.Network
[ProtoInclude(9, typeof(n_SerializableFireEvents))]
[ProtoInclude(10, typeof(n_TimeSyncPacket))]
[ProtoInclude(11, typeof(Heart_Settings))]
[ProtoInclude(12, typeof(n_TargetingUpdate))]
[ProtoInclude(13, typeof(n_MagazineUpdate))]
[ProtoContract(UseProtoMembersOnly = true)]
public abstract partial class PacketBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal Projectile AddProjectile(Projectile projectile, bool shouldSync = true)
while (!IsIdAvailable(NextId))
NextId++;
projectile.SetId(NextId);
HeartLog.Log("SpawnProjectile " + projectile.Id + $" | [{projectile.Definition.Name}] " + projectile.Definition.Networking.NetworkingMode);
//HeartLog.Log("SpawnProjectile " + projectile.Id + $" | [{projectile.Definition.Name}] " + projectile.Definition.Networking.NetworkingMode);
ActiveProjectiles.Add(projectile.Id, projectile);
if (MyAPIGateway.Session.IsServer && shouldSync)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using VRageMath;
using YourName.ModName.Data.Scripts.HeartModule.Weapons.Setup.Adding;

Expand Down Expand Up @@ -63,6 +64,11 @@ private void Grid_OnBlockAdded(IMySlimBlock obj)
// Unused for now
}

public void SetPrimaryTarget(IMyCubeGrid entity)
{
PrimaryGridTarget = entity;
}

public void UpdateTargeting()
{
try
Expand All @@ -78,8 +84,6 @@ public void UpdateTargeting()
manualTarget = keenTargeting.GetTarget(Grid);
if (manualTarget is IMyCubeGrid)
PrimaryGridTarget = (IMyCubeGrid)manualTarget;
else
PrimaryGridTarget = null;
}

//MyAPIGateway.Utilities.ShowNotification("Grids: " + TargetedGrids.Count, 1000/60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
using ProtoBuf;
using Sandbox.ModAPI;
using System;
using VRageMath;

namespace YourName.ModName.Data.Scripts.HeartModule.Weapons
{
[ProtoContract(UseProtoMembersOnly = true)]
public class Heart_Settings : PacketBase // this will ABSOLUTELY bite me in the ass later.
{
public void Sync()
public void Sync(Vector3D turretPosition)
{
//HeartLog.Log("Sync called!");
if (MyAPIGateway.Session.IsServer)
{
HeartData.I.Net.SendToEveryone(this);
HeartData.I.Net.SendToEveryoneInSync(this, turretPosition);
//HeartLog.Log("Sent settings to all.\n" + ToString() + "\n---------------------------");
}
else
Expand Down Expand Up @@ -53,7 +54,7 @@ public override void Received(ulong SenderSteamId)

if (IsSyncRequest)
{
weapon.Settings.Sync();
weapon.Settings.Sync(weapon.SorterWep.GetPosition());
return;
}

Expand All @@ -62,7 +63,7 @@ public override void Received(ulong SenderSteamId)
//HeartLog.Log("UPDATED Id: " + weapon.Magazines.SelectedAmmoId + " | Idx: " + weapon.Magazines.SelectedAmmoIndex);
//HeartLog.Log("SHOULD BE Idx: " + AmmoLoadedIdx);
if (MyAPIGateway.Session.IsServer)
weapon.Settings.Sync();
weapon.Settings.Sync(weapon.SorterWep.GetPosition());
}

[ProtoMember(1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public float AiRange
set
{
Settings.AiRange = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -345,7 +345,7 @@ public bool PreferUniqueTargetsState
set
{
Settings.PreferUniqueTargetState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -359,7 +359,7 @@ public bool TargetGridsState
set
{
Settings.TargetGridsState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());

}
}
Expand All @@ -374,7 +374,7 @@ public bool TargetProjectilesState
set
{
Settings.TargetProjectilesState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -388,7 +388,7 @@ public bool TargetCharactersState
set
{
Settings.TargetCharactersState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -402,7 +402,7 @@ public bool TargetLargeGridsState
set
{
Settings.TargetLargeGridsState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -416,7 +416,7 @@ public bool TargetSmallGridsState
set
{
Settings.TargetSmallGridsState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -430,7 +430,7 @@ public bool TargetFriendliesState
set
{
Settings.TargetFriendliesState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -444,7 +444,7 @@ public bool TargetNeutralsState
set
{
Settings.TargetNeutralsState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -458,7 +458,7 @@ public bool TargetEnemiesState
set
{
Settings.TargetEnemiesState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -472,7 +472,7 @@ public bool TargetUnownedState
set
{
Settings.TargetUnownedState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public bool MouseShootState
set
{
Settings.MouseShootState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -370,7 +370,7 @@ public bool ShootState
set
{
Settings.ShootState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -386,7 +386,7 @@ public int AmmoComboBox
SetAmmoByIdx(value);

Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand All @@ -413,7 +413,7 @@ public bool HudBarrelIndicatorState
set
{
Settings.HudBarrelIndicatorState = value;
Settings.Sync();
Settings.Sync(SorterWep.GetPosition());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Heart_Module.Data.Scripts.HeartModule.ExceptionHandler;
using Heart_Module.Data.Scripts.HeartModule.Network;
using ProtoBuf;
using System;

namespace Heart_Module.Data.Scripts.HeartModule.Weapons.StandardClasses
{
/// <summary>
/// Triggered whenever a weapon's magazine is emptied by the server.
/// </summary>
[ProtoContract]
internal class n_MagazineUpdate : PacketBase
{
[ProtoMember(1)] internal long WeaponEntityId;
[ProtoMember(2)] internal int MillisecondsFromMidnight;
[ProtoMember(3)] internal int MagazinesLoaded;

public override void Received(ulong SenderSteamId)
{
var magazine = WeaponManager.I.GetWeapon(WeaponEntityId)?.Magazines;
if (magazine == null)
return;

float timeDelta = (float) ((DateTime.UtcNow.TimeOfDay.TotalMilliseconds - MillisecondsFromMidnight)/1000);

magazine.EmptyMagazines();
magazine.MagazinesLoaded = MagazinesLoaded;
magazine.UpdateReload(timeDelta);

HeartLog.Log($"Magazine updated for weapon {WeaponEntityId}! Delta: " + timeDelta);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Heart_Module.Data.Scripts.HeartModule.Network;
using Heart_Module.Data.Scripts.HeartModule.Weapons.AiTargeting;
using ProtoBuf;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using YourName.ModName.Data.Scripts.HeartModule.Weapons.Setup.Adding;

namespace Heart_Module.Data.Scripts.HeartModule.Weapons.StandardClasses
{
// TODO complete
[ProtoContract]
internal class n_TargetingUpdate : PacketBase
{
[ProtoMember(1)] long EntityId;
[ProtoMember(2)] long TargetEntityId;

public override void Received(ulong SenderSteamId)
{
IMyEntity thisEntity = MyAPIGateway.Entities.GetEntityById(EntityId);
if (thisEntity == null)
return;

IMyEntity targetEntity = MyAPIGateway.Entities.GetEntityById(TargetEntityId);

if (thisEntity is IMyCubeGrid)
{
WeaponManagerAi.I.GetTargeting((IMyCubeGrid) thisEntity).SetPrimaryTarget((IMyCubeGrid) targetEntity);
}
else if (thisEntity is IMyConveyorSorter)
{
SorterTurretLogic weapon = WeaponManager.I.GetWeapon(EntityId) as SorterTurretLogic;

weapon?.SetTarget(targetEntity);
}
}
}
}
Loading

0 comments on commit 19bdd58

Please sign in to comment.