Skip to content

Commit

Permalink
Fixed projectile setting syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 26, 2024
1 parent ce005e0 commit 8844659
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ namespace Heart_Module.Data.Scripts.HeartModule.Network
[ProtoContract(UseProtoMembersOnly = true)]
public abstract partial class PacketBase
{
/// <summary>
/// Called whenever your packet is recieved.
/// </summary>
/// <param name="SenderSteamId"></param>
public abstract void Received(ulong SenderSteamId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static void RequestSync(long weaponEntityId)
public override void Received(ulong SenderSteamId)
{
//HeartLog.Log("Recieve called: Sender: " + SenderSteamId + " | Self: " + HeartData.I.SteamId + "\n" + ToString());
if (!IsSyncRequest)
HeartLog.Log("Recieved: " + ToString());

var weapon = WeaponManager.I.GetWeapon(WeaponEntityId);
if (weapon == null)
Expand All @@ -56,16 +58,18 @@ public override void Received(ulong SenderSteamId)
}

weapon.Settings = this;
weapon.Magazines.SelectedAmmoId = AmmoLoadedId;
weapon.Magazines.SelectedAmmoIndex = AmmoLoadedIdx;
HeartLog.Log("UPDATED Id: " + weapon.Magazines.SelectedAmmoId + " | Idx: " + weapon.Magazines.SelectedAmmoIndex);
HeartLog.Log("SHOULD BE Idx: " + AmmoLoadedIdx);
if (MyAPIGateway.Session.IsServer)
weapon.Settings.Sync();
}

[ProtoMember(1)]
internal int ShootStateContainer;
internal short ShootStateContainer;

[ProtoMember(2)]
public int AmmoLoadedId;
public int AmmoLoadedIdx;

[ProtoMember(3)]
public float AiRange;
Expand Down Expand Up @@ -255,6 +259,10 @@ public bool PreferUniqueTargetState

#endregion

public override string ToString()
{
return $"ShootState: {ShootState}\nAmmoLoadedIdx: {AmmoLoadedIdx}";
}

private bool ExpandValue(int bitwise, int enumValue)
{
Expand All @@ -269,6 +277,19 @@ private void CompressValue(ref int bitwise, int enumValue, bool state)
bitwise &= ~enumValue; // AND with negated enumValue
}

private bool ExpandValue(short bitwise, int enumValue)
{
return (bitwise & enumValue) == enumValue;
}

private void CompressValue(ref short bitwise, int enumValue, bool state)
{
if (state)
bitwise |= (short) enumValue;
else
bitwise &= (short) ~enumValue; // AND with negated enumValue
}

private static class TargetingSettingStates
{
public const int TargetGrids = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static void CreateControls()
var logic = b?.GameLogic?.GetAs<SorterWeaponLogic>();
if (logic != null)
{
return logic.Magazines.AmmoIndex;
return logic.Magazines.SelectedAmmoIndex;
}
return -1; // Return a default value (e.g., -1) when the index is out of bounds
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Heart_Module.Data.Scripts.HeartModule;
using Heart_Module.Data.Scripts.HeartModule.ErrorHandler;
using Heart_Module.Data.Scripts.HeartModule.ExceptionHandler;
using Heart_Module.Data.Scripts.HeartModule.Projectiles;
using Heart_Module.Data.Scripts.HeartModule.Projectiles.StandardClasses;
using Heart_Module.Data.Scripts.HeartModule.ResourceSystem;
Expand Down Expand Up @@ -56,7 +57,7 @@ public SorterWeaponLogic(IMyConveyorSorter sorterWeapon, WeaponDefinitionBase de
Func<IMyInventory> getInventoryFunc = () => sorterWeapon.GetInventory();

// You need to provide the missing arguments for WeaponLogic_Magazines constructor here
Magazines = new WeaponLogic_Magazines(definition.Loading, definition.Audio, getInventoryFunc, AmmoComboBox);
Magazines = new WeaponLogic_Magazines(this, getInventoryFunc, AmmoComboBox);

// Initialize the WeaponResourceSystem
_resourceSystem = new WeaponResourceSystem(definition, this);
Expand Down Expand Up @@ -330,20 +331,17 @@ public virtual MatrixD CalcMuzzleMatrix(int id, bool local = false)
public void SetAmmo(int AmmoId)
{
Magazines.SelectedAmmoId = AmmoId;
Settings.AmmoLoadedId = Magazines.SelectedAmmoId;

Magazines.EmptyMagazines();
Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
HeartLog.Log("Ammo: " + ProjectileDefinitionManager.GetDefinition(Magazines.SelectedAmmoId).Name);
}

public void SetAmmoByIdx(int AmmoIdx)
{
if (AmmoIdx < 0 || AmmoIdx >= Definition.Loading.Ammos.Length)
return;

Magazines.AmmoIndex = AmmoIdx;
Settings.AmmoLoadedId = Magazines.SelectedAmmoId;

Magazines.EmptyMagazines();
Magazines.SelectedAmmoIndex = AmmoIdx;
Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
}

#region Terminal controls
Expand Down Expand Up @@ -380,29 +378,29 @@ public int AmmoComboBox
{
get
{
return Settings.AmmoLoadedId;
return Settings.AmmoLoadedIdx;
}

set
{
SetAmmoByIdx(value);

Settings.AmmoLoadedId = Magazines.SelectedAmmoId;
Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
Settings.Sync();
}
}

public void CycleAmmoType(bool forward)
{
if (forward)
Magazines.AmmoIndex = (Magazines.AmmoIndex + 1) % Definition.Loading.Ammos.Length;
Magazines.SelectedAmmoIndex = (Magazines.SelectedAmmoIndex + 1) % Definition.Loading.Ammos.Length;
else
Magazines.AmmoIndex = (Magazines.AmmoIndex - 1 + Definition.Loading.Ammos.Length) % Definition.Loading.Ammos.Length;
Magazines.SelectedAmmoIndex = (Magazines.SelectedAmmoIndex - 1 + Definition.Loading.Ammos.Length) % Definition.Loading.Ammos.Length;

Settings.AmmoLoadedId = Magazines.SelectedAmmoId;
Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
Magazines.EmptyMagazines();

AmmoComboBox = Magazines.AmmoIndex;
AmmoComboBox = Magazines.SelectedAmmoIndex;
}

public bool HudBarrelIndicatorState
Expand Down Expand Up @@ -449,7 +447,7 @@ internal virtual void LoadDefaultSettings()
return;

Settings.ShootState = false;
Settings.AmmoLoadedId = Magazines.SelectedAmmoId;
Settings.AmmoLoadedIdx = Magazines.SelectedAmmoIndex;
Settings.HudBarrelIndicatorState = false;
}

Expand Down Expand Up @@ -477,8 +475,8 @@ internal virtual bool LoadSettings()
{
Settings.ShootState = loadedSettings.ShootState;

Settings.AmmoLoadedId = loadedSettings.AmmoLoadedId;
Magazines.AmmoIndex = Array.IndexOf(Definition.Loading.Ammos, ProjectileDefinitionManager.GetDefinition(Settings.AmmoLoadedId).Name);
Settings.AmmoLoadedIdx = loadedSettings.AmmoLoadedIdx;
Magazines.SelectedAmmoIndex = loadedSettings.AmmoLoadedIdx;

Settings.ControlTypeState = loadedSettings.ControlTypeState;
Settings.HudBarrelIndicatorState = loadedSettings.HudBarrelIndicatorState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public class WeaponLogic_Magazines
{
Loading Definition;
Audio DefinitionAudio;
SorterWeaponLogic Weapon;
private readonly Func<IMyInventory> GetInventoryFunc;

private int _ammoIndex = 0;
private int _selectedAmmoIndex = 0;
private int _selectedAmmo = 0;
private int shotsPerMag = 0;

Expand All @@ -27,38 +28,40 @@ public int SelectedAmmoId
set
{
int idx = Array.IndexOf(Definition.Ammos, value);
if (idx == -1)
if (idx == -1 || _selectedAmmo == value)
return;
_selectedAmmo = value;
_ammoIndex = idx;
_selectedAmmoIndex = idx;
shotsPerMag = ProjectileDefinitionManager.GetDefinition(SelectedAmmoId).Ungrouped.ShotsPerMagazine;
EmptyMagazines();
}
}

public int AmmoIndex
public int SelectedAmmoIndex
{
get
{
return _ammoIndex;
return _selectedAmmoIndex;
}
set
{
if (Definition.Ammos.Length <= value || value < 0)
if (Definition.Ammos.Length <= value || value < 0 || _selectedAmmoIndex == value)
return;
_ammoIndex = value;
_selectedAmmo = ProjectileDefinitionManager.GetId(Definition.Ammos[value]);
_selectedAmmoIndex = value;
shotsPerMag = ProjectileDefinitionManager.GetDefinition(SelectedAmmoId).Ungrouped.ShotsPerMagazine;
EmptyMagazines();
}
}

public WeaponLogic_Magazines(Loading definition, Audio definitionaudio, Func<IMyInventory> getInventoryFunc, int ammoIdx, bool startLoaded = false)
public WeaponLogic_Magazines(SorterWeaponLogic weapon, Func<IMyInventory> getInventoryFunc, int ammoIdx, bool startLoaded = false)
{
Definition = definition;
DefinitionAudio = definitionaudio;
Definition = weapon.Definition.Loading;
DefinitionAudio = weapon.Definition.Audio;
GetInventoryFunc = getInventoryFunc;
RemainingReloads = Definition.MaxReloads;
NextReloadTime = Definition.ReloadTime;
AmmoIndex = ammoIdx;
SelectedAmmoIndex = ammoIdx;
if (startLoaded)
{
MagazinesLoaded = Definition.MagazinesToLoad;
Expand Down

0 comments on commit 8844659

Please sign in to comment.