Skip to content

Commit

Permalink
Updated zap functionality (cleaned)
Browse files Browse the repository at this point in the history
Updated PrawnSelfDefenseModule
Added a bunch of patches for the PrawnSelfDefenseModule
Ready for release. WAITING FOR A SMLHELPER UPDATE !!!
  • Loading branch information
VELD-Dev committed Apr 23, 2023
1 parent 25dba22 commit be2d9fb
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 38 deletions.
3 changes: 3 additions & 0 deletions AlterraWeaponry/AlterraWeaponry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@
<Compile Include="behaviours\ZapFunctionalityBehaviour.cs" />
<Compile Include="items\PrawnSelfDefenseModule.cs" />
<Compile Include="patches\ExosuitTorpedoArm_OpenTorpedoStorageExternal_Patch.cs" />
<Compile Include="patches\GameSettings_SaveAsync_Patch.cs" />
<Compile Include="patches\SeamothTorpedo_OnEnergyDepleted_Patch.cs" />
<Compile Include="patches\Vehicle_ChargeModule_Patch.cs" />
<Compile Include="patches\Vehicle_GetSlotCharge_Patch.cs" />
<Compile Include="patches\Vehicle_OnUpgradeModuleChange_Patch.cs" />
<Compile Include="patches\Vehicle_OnUpgradeModuleUse_Patch.cs" />
<Compile Include="utils\ExplosiveTorpedoInitializer.cs" />
Expand Down
50 changes: 15 additions & 35 deletions AlterraWeaponry/behaviours/ZapFunctionalityBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UWE;
using static VehicleUpgradeConsoleInput;
using static VFXParticlesPool;

namespace VELD.AlterraWeaponry.behaviours;

Expand All @@ -14,25 +17,10 @@ internal class ZapFunctionalityBehaviour : MonoBehaviour // Thanks to ECM and Pr

public static GameObject ElectricalDefensePrefab => seamothElectricalDefensePrefab;

private const float EnergyCostPerZap = 5;
private const float ZapPower = 6f;
private const float BaseCharge = 2f;
private const float BaseRadius = 1f;
public const float ZapCooldown = 5f;

public const float ZapCooldown = 10f;
public static float timeNextZap = 0;
private static float DamageMultiplier => 1f;
private static float DirectZapDamage = (BaseRadius + ZapPower * BaseCharge) * DamageMultiplier * 0.5f;
// Calculations and initial values based off ElectricalDefense component

public static bool AbleToZap(Vehicle vehicle)
{
vehicle.energyInterface.GetValues(out float charge, out float capacity);
if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower) && charge < EnergyCostPerZap)
return false;

return true;
}
public float Overcharge { get; private set; }
public float OverchargeScalar { get; private set; }

public static IEnumerator UpdateDefensePrefab()
{
Expand All @@ -44,35 +32,27 @@ public static IEnumerator UpdateDefensePrefab()

seamothElectricalDefensePrefab = prefab?.GetComponent<SeaTruckUpgrades>().electricalDefensePrefab;
}

public bool Zap(Vehicle vehicle)
public bool Zap(Vehicle vehicle, int usedSlotID)
{
CoroutineHost.StartCoroutine(UpdateDefensePrefab());
if (Time.time < timeNextZap)
return true;

if (!AbleToZap(vehicle))
return false;
float charge = vehicle.quickSlotCharge[usedSlotID];
float slotCharge = vehicle.GetSlotCharge(usedSlotID);
this.Overcharge = charge;
this.OverchargeScalar = slotCharge;
CoroutineHost.StartCoroutine(UpdateDefensePrefab());

ZapRadius(vehicle);

timeNextZap = Time.time + ZapCooldown;
return true;
}

private static void ZapRadius(Vehicle vehicle)
private void ZapRadius(Vehicle vehicle)
{
if (vehicle == null)
return;

GameObject gameObject = Utils.SpawnZeroedAt(ElectricalDefensePrefab, vehicle.transform, false);
ElectricalDefense defenseComponent = gameObject.GetComponent<ElectricalDefense>();
defenseComponent.charge = ZapPower;
defenseComponent.chargeScalar = ZapPower;
defenseComponent.radius *= ZapPower;
defenseComponent.chargeRadius *= ZapPower;

if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower))
vehicle.energyInterface.ConsumeEnergy(EnergyCostPerZap);
defenseComponent.charge = this.Overcharge;
defenseComponent.chargeScalar = this.OverchargeScalar;
}
}
2 changes: 2 additions & 0 deletions AlterraWeaponry/items/PrawnSelfDefenseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace VELD.AlterraWeaponry.items;
public class PrawnSelfDefenseModule : Equipable
{
public static GameObject prefab;
public static float maxCharge = 10f;
public static float energyCost = 5f;
public static TechType techType { get; private set; } = 0;

public PrawnSelfDefenseModule() : base("PrawnSelfDefenseModule", "PrawnSelfDefenseModule", "Tooltip_PrawnSelfDefenseModule")
Expand Down
19 changes: 19 additions & 0 deletions AlterraWeaponry/patches/GameSettings_SaveAsync_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.patches;

[HarmonyPatch(typeof(GameSettings))]
public class GameSettings_SaveAsync_Patch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(GameSettings.SaveAsync))]
public static void SaveAsync(GameSettings.OnSaveDelegate onSave)
{
LanguagesHandler.LanguagePatch();
}
}
45 changes: 45 additions & 0 deletions AlterraWeaponry/patches/Vehicle_ChargeModule_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.patches;

[HarmonyPatch(typeof(Vehicle))]
public class Vehicle_ChargeModule_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(Vehicle.ChargeModule))]
public static bool ChargeModule(Vehicle __instance, TechType techType, int slotID)
{
float num = __instance.quickSlotCharge[slotID];
float maxCharge = TechData.GetMaxCharge(techType);

// TEMP CODE WAITING FOR SMLHELPER UPDATE
if(techType == PrawnSelfDefenseModule.techType)
maxCharge = PrawnSelfDefenseModule.maxCharge;

float num2;
TechData.GetEnergyCost(techType, out num2);

// TEMP CODE WAITING FOR SMLHELPER UPDATE
if (techType == PrawnSelfDefenseModule.techType)
num2 = PrawnSelfDefenseModule.energyCost;

float num3 = num2 * Time.deltaTime;
float num4 = maxCharge - num;
bool flag = num3 >= num4;
float b = flag ? Mathf.Max(0f, num4) : num3;
int num6;
float num5 = Mathf.Min(__instance.energyInterface.TotalCanProvide(out num6), b);
__instance.ConsumeEnergy(num5);
__instance.quickSlotCharge[slotID] = __instance.quickSlotCharge[slotID] + num5;
if (__instance.quickSlotCharge[slotID] > 0f && (flag || num5 == 0f))
{
__instance.OnUpgradeModuleUse(techType, slotID);
__instance.quickSlotCharge[slotID] = 0f;
}
return false;
}
}
43 changes: 43 additions & 0 deletions AlterraWeaponry/patches/Vehicle_GetSlotCharge_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.patches;


[HarmonyPatch(typeof(Vehicle))]
internal class Vehicle_GetSlotCharge_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(Vehicle.GetSlotCharge))]
public static bool GetSlotCharge(Vehicle __instance, int slotID, ref float __result)
{
if (slotID < 0 || slotID >= __instance.slotIDs.Length)
{
__result = 1f;
return false;
}
TechType techType;
QuickSlotType quickSlotType = __instance.GetQuickSlotType(slotID, out techType);
if (quickSlotType == QuickSlotType.Chargeable || quickSlotType == QuickSlotType.SelectableChargeable)
{
float maxCharge = TechData.GetMaxCharge(techType);

// TEMPORARY PATCH, WAITING FOR AN SMLHELPER UPDATE
bool flag = techType == PrawnSelfDefenseModule.techType;
if (flag)
maxCharge = PrawnSelfDefenseModule.maxCharge;

if (maxCharge > 0f)
{
__result = __instance.quickSlotCharge[slotID] / maxCharge;
Main.logger.LogInfo($"Slot charge: {__result}");
return false;
}
}
__result = 1f;
return false;
}
}
17 changes: 14 additions & 3 deletions AlterraWeaponry/patches/Vehicle_OnUpgradeModuleUse_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ public class Vehicle_OnUpgradeModuleUse_Patch
[HarmonyPatch(nameof(Vehicle.OnUpgradeModuleUse))]
public static void OnUpgradeModuleUse(TechType techType, int slotID, Vehicle __instance)
{
if(techType == PrawnSelfDefenseModule.techType)
if(__instance is not Exosuit)
return;


bool flag = true;
float num = 0f;
if (techType == PrawnSelfDefenseModule.techType)
{
Main.logger.LogInfo($"OnUpgradeModuleUse input received on slot {slotID}");
if (!__instance.TryGetComponent(out ZapFunctionalityBehaviour defenseMono))
return;
defenseMono.Zap(__instance);
defenseMono.Zap(__instance, slotID);
num = ZapFunctionalityBehaviour.ZapCooldown;
}
if (flag)
{
__instance.quickSlotTimeUsed[slotID] = Time.time;
__instance.quickSlotCooldown[slotID] = num;
}
}
}

0 comments on commit be2d9fb

Please sign in to comment.