Skip to content

Commit

Permalink
MISSILE GUIDANCE IN MULTIPLAYER!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 26, 2024
1 parent 8844659 commit 2db4fbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ internal void PreformRaycast(Guidance currentstage)

internal bool IsTargetAllowed(IMyEntity target, Guidance? currentStage)
{
if (currentStage == null)
if (currentStage == null || target == null)
return false;
if (projectile.Firer == 0)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ public n_SerializableFireEvents(List<Projectile> projectiles)
directionX = new float[projectiles.Count];
directionY = new float[projectiles.Count];
directionZ = new float[projectiles.Count];
targetEntities = new long[projectiles.Count];

bool needsTargetEntities = false;

for (int i = 0; i < projectiles.Count; i++)
{
Expand All @@ -226,8 +229,17 @@ public n_SerializableFireEvents(List<Projectile> projectiles)
directionX[i] = (float)projectiles[i].Direction.X;
directionY[i] = (float)projectiles[i].Direction.Y;
directionZ[i] = (float)projectiles[i].Direction.Z;

if (projectiles[i].Guidance != null && projectiles[i].Guidance.GetTarget() != null)
{
needsTargetEntities = true;
targetEntities[i] = projectiles[i].Guidance.GetTarget().EntityId;
}
}

if (!needsTargetEntities)
targetEntities = null;

MillisecondsFromMidnight = (int)DateTime.UtcNow.TimeOfDay.TotalMilliseconds;
}

Expand All @@ -253,6 +265,7 @@ public n_SerializableFireEvents(long[] firerWeaponId, uint[] uniqueProjectileId,
[ProtoMember(23)] private float[] directionX;
[ProtoMember(24)] private float[] directionY;
[ProtoMember(25)] private float[] directionZ;
[ProtoMember(26)] private long[] targetEntities;
//[ProtoMember(26)] private byte[] muzzleIdx; // TODO: Sync muzzle index for fireevents.

public Vector3[] Direction() // just using a Vector3 adds 2 extra bytes (!!!)
Expand All @@ -270,7 +283,7 @@ public Vector3 Direction(int index)
return new Vector3(directionX[index], directionY[index], directionZ[index]);
}

[ProtoMember(26)] public int MillisecondsFromMidnight;
[ProtoMember(27)] public int MillisecondsFromMidnight;

public Projectile ToProjectile(int index)
{
Expand All @@ -293,12 +306,9 @@ public Projectile ToProjectile(int index)
LastUpdate = DateTime.UtcNow.Date.AddMilliseconds(MillisecondsFromMidnight - HeartData.I.Net.ServerTimeOffset).Ticks
};

if (p.Guidance != null) // Assign target for self-guided projectiles
if (targetEntities != null && p.Guidance != null) // Assign target for self-guided projectiles
{
if (weapon is SorterTurretLogic)
p.Guidance.SetTarget(((SorterTurretLogic)weapon).TargetEntity);
else
p.Guidance.SetTarget(WeaponManagerAi.I.GetTargeting(weapon.SorterWep.CubeGrid)?.PrimaryGridTarget);
p.Guidance.SetTarget(MyAPIGateway.Entities.GetEntityById(targetEntities[index]));
}

// TODO: Look into engine tick based syncing? Server time can vary DRASTICALLY.
Expand Down

0 comments on commit 2db4fbd

Please sign in to comment.