-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proper fix for loot drop bug now that i know the cause
- Loading branch information
jakzo
committed
Jul 19, 2024
1 parent
408ca3c
commit ace7964
Showing
12 changed files
with
211 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ | |
"xd:pre", | ||
"HintPath", | ||
"BonelabPath", | ||
"BoneworksPath" | ||
"BoneworksPath", | ||
"PostBuildEvent" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Found the actual source of the bug and rewrote the mod to patch the broken method. Experimentally verified by breaking 8k+ ammo boxes with a script (included in this mod under the `test` configuration option). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
namespace Sst.LootDropBugfix { | ||
static class AppVersion { public const string Value = "1.3.0"; } | ||
namespace Sst.LootDropBugfix; | ||
|
||
static class AppVersion { | ||
public const string Value = "1.3.0"; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="BuildAll"> | ||
<Target Name="BuildAll"> | ||
<!-- All game/Melon Loader version combinations to build --> | ||
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" | ||
Properties="Configuration=$(Configuration);Patch=3;MelonLoader=5" /> | ||
</Target> | ||
|
||
<PropertyGroup> | ||
<CopyIntoGameAfterBuild>true</CopyIntoGameAfterBuild> | ||
|
||
<ProjectGuid>{EAE1410F-B5CF-47D6-8764-2FCAEE822C9D}</ProjectGuid> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="AppVersion.cs" /> | ||
<Compile Include="src/**/*.cs" /> | ||
<Compile Include="../../../common/Utilities/Metadata.cs" /> | ||
<Compile Include="../../../common/Utilities/Dbg.cs" /> | ||
<Compile Include="../../../common/Boneworks/Il2CppNullable.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,112 @@ | ||
using System.Linq; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using MelonLoader; | ||
using UnityEngine; | ||
using StressLevelZero.Props; | ||
using StressLevelZero.Pool; | ||
using StressLevelZero.Data; | ||
|
||
namespace Sst.LootDropBugfix { | ||
public static class AmmoDebugger { | ||
private static MelonPreferences_Entry<bool> _prefDebugAmmo; | ||
private static float _lastUpdate = 0; | ||
private static ObjectDestructable _toBreak; | ||
private static bool _replacedAmmo = false; | ||
|
||
public static void Initialize() { | ||
var category = MelonPreferences.CreateCategory(BuildInfo.NAME); | ||
_prefDebugAmmo = category.CreateEntry( | ||
"debug_ammo", false, | ||
"Teleports and breaks ammo boxes to test item replacement"); | ||
namespace Sst.LootDropBugfix; | ||
|
||
public class AmmoDebugger { | ||
private static Il2CppSystem.Nullable<bool> _emptyNullableBool = | ||
new Utilities.Il2CppNullable<bool>(null); | ||
private static Il2CppSystem.Nullable<Color> _emptyNullableColor = | ||
new Utilities.Il2CppNullable<Color>(null); | ||
|
||
private MelonPreferences_Entry<bool> _prefTest; | ||
private MelonPreferences_Entry<float> _prefTestSpeed; | ||
|
||
public AmmoDebugger(MelonPreferences_Category prefCategory) { | ||
_prefTest = prefCategory.CreateEntry( | ||
"test", false, "Test", "Spawns and breaks ammo boxes to test the fix"); | ||
_prefTestSpeed = prefCategory.CreateEntry( | ||
"test_speed", 1f, "Test speed", | ||
"Rate at which the test ammo boxes spawn and break"); | ||
_prefTest.OnValueChanged += (a, b) => OnLevelStart(); | ||
} | ||
|
||
public static void OnAmmoReplaced() { _replacedAmmo = true; } | ||
|
||
public static void OnUpdate() { | ||
if (!_prefDebugAmmo.Value || Time.time - _lastUpdate <= 0.5f || | ||
_replacedAmmo) | ||
return; | ||
|
||
_lastUpdate = Time.time; | ||
if (_toBreak) { | ||
_toBreak.TakeDamage(Vector3.back, 100, false, | ||
StressLevelZero.Combat.AttackType.Piercing); | ||
_toBreak = null; | ||
} else { | ||
foreach (var ammo in GameObject | ||
.FindObjectsOfType<StressLevelZero.AmmoPickup>()) | ||
GameObject.Destroy(ammo.transform.parent.gameObject); | ||
var head = GameObject.FindObjectOfType<StressLevelZero.Rig.RigManager>() | ||
.physicsRig.m_head; | ||
var ammoCrates = | ||
GameObject.FindObjectsOfType<ObjectDestructable>() | ||
.Where(obj => | ||
obj.lootTable != null && Mod.IsAmmoCrate(obj) && | ||
(obj.transform.position - head.position).sqrMagnitude > | ||
25) | ||
.ToArray(); | ||
if (ammoCrates.Length > 0) { | ||
_toBreak = ammoCrates[0]; | ||
_toBreak.transform.position = | ||
head.position + head.rotation * new Vector3(0, 0, 2); | ||
public void OnLevelStart() { | ||
if (_prefTest.Value) | ||
MelonCoroutines.Start(SpawnAndBreakAmmoCrates()); | ||
} | ||
|
||
public IEnumerator SpawnAndBreakAmmoCrates() { | ||
var head = Object.FindObjectOfType<StressLevelZero.Rig.RigManager>() | ||
?.physicsRig?.m_head; | ||
// TODO: Could use the hover junkers ship ammo box as a prefab because it | ||
// is always loaded | ||
var cratePrefab = Object.FindObjectsOfType<ObjectDestructable>() | ||
.FirstOrDefault(obj => obj.lootTable?.name.StartsWith( | ||
"AmmoCrateTable_") ?? | ||
false) | ||
?.gameObject; | ||
|
||
var numDestroyed = 0; | ||
var numMissingLootItem = 0; | ||
while (head != null && cratePrefab != null && _prefTest.Value) { | ||
|
||
var crate = Object.Instantiate(cratePrefab.gameObject, | ||
head.position + | ||
head.rotation * new Vector3(0, 0, 2), | ||
Quaternion.identity); | ||
yield return new WaitForSeconds(0.2f / _prefTestSpeed.Value); | ||
|
||
if (crate == null) { | ||
yield return new WaitForSeconds(0.5f / _prefTestSpeed.Value); | ||
continue; | ||
} | ||
|
||
var obj = crate.GetComponent<ObjectDestructable>(); | ||
var prefabNames = | ||
obj.lootTable.items.Select(item => item.spawnable.prefab.name); | ||
var spawnPosition = obj.spawnTarget.position; | ||
obj.TakeDamage(Vector3.back, 100, false, | ||
StressLevelZero.Combat.AttackType.Piercing); | ||
|
||
numDestroyed++; | ||
var spawnedLootItem = | ||
FindSpawnedLootItem(prefabNames, spawnPosition, false); | ||
if (spawnedLootItem == null) | ||
numMissingLootItem++; | ||
MelonLogger.Msg($"Loot bugs: {numMissingLootItem} / {numDestroyed}"); | ||
yield return new WaitForSeconds(0.5f / _prefTestSpeed.Value); | ||
|
||
spawnedLootItem?.GetComponent<Poolee>().Despawn(_emptyNullableBool, | ||
_emptyNullableColor); | ||
yield return new WaitForSeconds(0.2f / _prefTestSpeed.Value); | ||
} | ||
} | ||
|
||
private GameObject FindSpawnedLootItem(IEnumerable<string> prefabNames, | ||
Vector3 spawnPosition, | ||
bool checkExactPosition) { | ||
foreach (var collider in Physics.OverlapSphere(spawnPosition, 0.5f)) { | ||
var topLevelObject = collider.transform; | ||
while (topLevelObject.parent) | ||
topLevelObject = topLevelObject.parent; | ||
var isCorrectPosition = | ||
!checkExactPosition || | ||
(topLevelObject.position - spawnPosition).sqrMagnitude < 1e-9; | ||
if (isCorrectPosition && | ||
prefabNames.Any(topLevelObject.name.StartsWith)) { | ||
return topLevelObject.gameObject; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static bool IsLootGuaranteed(LootTableData lootTable) { | ||
var totalPercentage = | ||
lootTable.items.Aggregate(0f, (total, item) => total + item.percentage); | ||
return totalPercentage >= 100f; | ||
} | ||
|
||
public void OnGetLootItem(LootTableData lootTable, SpawnableObject result) { | ||
if (_prefTest.Value && IsLootGuaranteed(lootTable) && result == null) { | ||
MelonLogger.Warning( | ||
"LootTableData.GetLootItem just returned null when loot chances add up to 100%!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.