Skip to content

Commit

Permalink
New API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 2, 2024
1 parent 6249e71 commit c59b975
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Heart_Module.Data.Scripts.HeartModule.Projectiles;
using Heart_Module.Data.Scripts.HeartModule.Projectiles.StandardClasses;
using Heart_Module.Data.Scripts.HeartModule.Weapons;
using Heart_Module.Data.Scripts.HeartModule.Weapons.StandardClasses;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
Expand All @@ -15,6 +16,9 @@

namespace Heart_Module.Data.Scripts.HeartModule.Definitions.ApiHandler
{
/// <summary>
/// Contains every HeartApi method.
/// </summary>
internal class HeartApiMethods
{
internal readonly Dictionary<string, Delegate> ModApiMethods;
Expand All @@ -37,9 +41,15 @@ internal HeartApiMethods()

// Weapon Generics
["BlockHasWeapon"] = new Func<MyEntity, bool>(HasWeapon),
["SubtypeHasDefinition"] = new Func<string, bool>(SubtypeHasDefinition),
["GetWeaponDefinitions"] = new Func<string[]>(GetWeaponDefinitions),
["GetWeaponDefinition"] = new Func<string, byte[]>(GetWeaponDefinition),
["RegisterWeaponDefinition"] = new Func<byte[], bool>(RegisterWeaponDefinition),
["UpdateWeaponDefinition"] = new Func<byte[], bool>(UpdateWeaponDefinition),

// Standard
["LogWriteLine"] = new Action<string>(HeartData.I.Log.Log),
["GetNetworkLoad"] = new Func<int>(GetNetworkLoad),
};
}

Expand Down Expand Up @@ -118,7 +128,7 @@ public int RegisterProjectileDefinition(byte[] serialized)
ProjectileDefinitionBase def = MyAPIGateway.Utilities.SerializeFromBinary<ProjectileDefinitionBase>(serialized);
if (def == null)
return -1;
return ProjectileDefinitionManager.RegisterDefinition(def, true);
return ProjectileDefinitionManager.RegisterModApiDefinition(def);
}
public bool UpdateProjectileDefinition(int definitionId, byte[] serialized)
{
Expand All @@ -136,6 +146,56 @@ public bool HasWeapon(MyEntity block)
{
return block is IMyConveyorSorter && ((IMyConveyorSorter) block).GameLogic is SorterWeaponLogic;
}

public bool SubtypeHasDefinition(string subtype)
{
return WeaponDefinitionManager.HasDefinition(subtype);
}

public string[] GetWeaponDefinitions() => WeaponDefinitionManager.GetAllDefinitions();

public byte[] GetWeaponDefinition(string subtype)
{
if (WeaponDefinitionManager.HasDefinition(subtype))
return MyAPIGateway.Utilities.SerializeToBinary(WeaponDefinitionManager.GetDefinition(subtype));
return null;
}

public bool RegisterWeaponDefinition(byte[] definition)
{
if (definition == null || definition.Length == 0)
return false;

WeaponDefinitionBase weaponDef = MyAPIGateway.Utilities.SerializeFromBinary<WeaponDefinitionBase>(definition);
if (definition == null)
{
SoftHandle.RaiseException("Invalid weapon definition!");
return false;
}

return WeaponDefinitionManager.RegisterModApiDefinition(weaponDef);
}

public bool UpdateWeaponDefinition(byte[] definition)
{
if (definition == null || definition.Length == 0)
return false;

WeaponDefinitionBase weaponDef = MyAPIGateway.Utilities.SerializeFromBinary<WeaponDefinitionBase>(definition);
if (definition == null)
{
SoftHandle.RaiseException("Invalid weapon definition!");
return false;
}

return WeaponDefinitionManager.UpdateDefinition(weaponDef);
}
#endregion

#region Debug Methods

public int GetNetworkLoad() => HeartData.I.Net.NetworkLoad;

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Heart_Module.Data.Scripts.HeartModule.Network
public class HeartNetwork
{
public int NetworkLoadTicks = 240;
public int NetworkLoad { get; private set; } = 0;
public int NetworkLoad { get; private set; } = 0; // TODO: Per-packet type network load

private List<int> networkLoadArray = new List<int>();
private int networkLoadUpdate = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void TickUpdate(float delta)
{
Guidance?.RunGuidance(delta);

CheckHits(delta);
CheckHits();
Velocity += Definition.PhysicalProjectile.Acceleration * delta;
Position += (InheritedVelocity + Direction * Velocity) * delta;
DistanceTravelled += Velocity * delta;
Expand All @@ -169,7 +169,7 @@ public void TickUpdate(float delta)

if (RemainingImpacts > 0)
{
MaxBeamLength = CheckHits(delta); // Set visual beam length
MaxBeamLength = CheckHits(); // Set visual beam length
if (MaxBeamLength == -1)
MaxBeamLength = Definition.PhysicalProjectile.MaxTrajectory;
}
Expand All @@ -178,7 +178,7 @@ public void TickUpdate(float delta)
UpdateAudio();
}

public float CheckHits(float delta)
public float CheckHits()
{
if (NextMoveStep == Vector3D.Zero)
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

namespace Heart_Module.Data.Scripts.HeartModule.Projectiles
{
/// <summary>
/// Collects and distributes all projectile definitions.
/// </summary>
internal class ProjectileDefinitionManager
{
public static ProjectileDefinitionManager I;
private List<ProjectileDefinitionBase> Definitions = new List<ProjectileDefinitionBase>();
private List<ProjectileDefinitionBase> Definitions = new List<ProjectileDefinitionBase>(); // TODO: Store serialized versions of definitions in case of modded functionality
private Dictionary<string, int> DefinitionNamePairs = new Dictionary<string, int>();

/// <summary>
/// Changes the ID of a projectile definition. If the ID is already occupied, swaps the two definitions. DO NOT CALL ON SERVER!
/// Unused.
/// </summary>
/// <param name="name"></param>
/// <param name="newId"></param>
Expand Down Expand Up @@ -67,6 +71,24 @@ public static bool HasDefinition(int id)
return I.Definitions.Count > id && id >= 0;
}

/// <summary>
/// Use this when creating a definiton live.
/// </summary>
/// <param name="definition"></param>
/// <returns></returns>
public static int RegisterModApiDefinition(ProjectileDefinitionBase definition)
{
if (HasDefinition(definition.Name))
throw new System.Exception("Attempted to assign ProjectileDefinition to existing ID!");
return RegisterDefinition(definition, true);
}

/// <summary>
/// Registers a projectile definition.
/// </summary>
/// <param name="definition"></param>
/// <param name="syncToClients"></param>
/// <returns></returns>
public static int RegisterDefinition(ProjectileDefinitionBase definition, bool syncToClients = false)
{
if (I.DefinitionNamePairs.ContainsKey(definition.Name))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Heart_Module.Data.Scripts.HeartModule.Weapons.StandardClasses;
using Heart_Module.Data.Scripts.HeartModule.ErrorHandler;
using Heart_Module.Data.Scripts.HeartModule.Weapons.StandardClasses;
using System.Collections.Generic;
using System.Linq;

namespace Heart_Module.Data.Scripts.HeartModule.Weapons
{
/// <summary>
/// Collects and distributes all weapon definitions.
/// </summary>
internal class WeaponDefinitionManager
{
public static WeaponDefinitionManager I;

private Dictionary<string, WeaponDefinitionBase> Definitions = new Dictionary<string, WeaponDefinitionBase>();
private Dictionary<string, WeaponDefinitionBase> Definitions = new Dictionary<string, WeaponDefinitionBase>(); // TODO: Store serialized versions of definitions in case of modded functionality.

public static WeaponDefinitionBase GetDefinition(string subTypeId)
{
Expand All @@ -22,6 +27,26 @@ public static bool HasDefinition(string subTypeId)
return I.Definitions.ContainsKey(subTypeId);
}

public static bool UpdateDefinition(WeaponDefinitionBase definition)
{
if (!HasDefinition(definition.Assignments.BlockSubtype))
return false;

I.Definitions[definition.Assignments.BlockSubtype] = definition;
return true;
}

public static bool RegisterModApiDefinition(WeaponDefinitionBase definition)
{
if (HasDefinition(definition.Assignments.BlockSubtype))
{
SoftHandle.RaiseException("Attempted to assign WeaponDefinition to existing ID!", callingType: typeof(WeaponDefinitionManager));
return false;
}
RegisterDefinition(definition);
return true;
}

public static void RegisterDefinition(WeaponDefinitionBase definition)
{
if (definition == null)
Expand All @@ -37,11 +62,19 @@ public static void RegisterDefinition(WeaponDefinitionBase definition)

HeartData.I.OrreryBlockCategory.AddBlock(definition.Assignments.BlockSubtype);
HeartData.I.Log.Log($"Registered weapon definition {definition.Assignments.BlockSubtype}.");

if (HeartData.I.IsLoaded)
WeaponManager.I.UpdateLogicOnExistingBlocks(definition);
}

public static int DefinitionCount()
{
return I.Definitions.Count;
}

public static string[] GetAllDefinitions()
{
return I.Definitions.Keys.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public override void LoadData()
HeartData.I.OnGridRemove += OnGridRemove;
}

/// <summary>
/// Check for blocks already in the world without weapon logic.
/// </summary>
/// <param name="definition"></param>
public void UpdateLogicOnExistingBlocks(WeaponDefinitionBase definition)
{
foreach (var grid in GridWeapons.Keys)
{
foreach (var block in grid.GetFatBlocks<IMyConveyorSorter>())
{
if (block.BlockDefinition.SubtypeName != definition.Assignments.BlockSubtype || block.GameLogic?.GetAs<SorterWeaponLogic>() != null)
continue;

AddWeapon(block);
}
}
}

/// <summary>
/// Check if new grids contain valid weapons.
/// </summary>
Expand Down

0 comments on commit c59b975

Please sign in to comment.