-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apparently vanilla just checked the id starts with "ice-block", so we'll just make durable different from thawable.
- Loading branch information
Showing
13 changed files
with
260 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Winch.Examples/ExampleItems/Assets/Items/Durable/exampleitems.durable.json
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,19 @@ | ||
{ | ||
"itemNameKey": "exampleitems.durable.name", | ||
"itemDescriptionKey": "exampleitems.durable.desc", | ||
"sprite": "exampleitems.durable", | ||
"itemColor": { | ||
"r": 58, | ||
"g": 40, | ||
"b": 32, | ||
"a": 255 | ||
}, | ||
"squishFactor": 1, | ||
"value": 50, | ||
"dimensions": [ | ||
"X" | ||
], | ||
"maxDurabilityDays": 1, | ||
"displayDurabilityAsPercentage": true | ||
} | ||
|
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
Binary file added
BIN
+229 Bytes
Winch.Examples/ExampleItems/Assets/Textures/exampleitems.durable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,191 @@ | ||
using HarmonyLib; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using UnityEngine; | ||
using Winch.Core; | ||
|
||
namespace Winch.Patches.API | ||
{ | ||
/// <summary> | ||
/// DREDGE devs sure do love to not check item data types. | ||
/// They do this a lot. Especially with DeployableItemData. Instead they check damageMode == DamageMode.DURABILITY. | ||
/// So lets fix that while we add the durable items. | ||
/// </summary> | ||
[HarmonyPatch] | ||
internal static class DurableThawableItemDataPatcher | ||
{ | ||
[HarmonyPrefix] | ||
[HarmonyPriority(Priority.First)] | ||
[HarmonyPatch(typeof(SpatialItemInstance), nameof(SpatialItemInstance.ChangeDurability))] | ||
public static bool SpatialItemInstance_ChangeDurability_Prefix(SpatialItemInstance __instance, float changeAmount) | ||
{ | ||
var itemData = __instance.GetItemData<SpatialItemData>(); | ||
if (itemData.damageMode == DamageMode.DURABILITY) | ||
{ | ||
__instance.durability += changeAmount; | ||
if (itemData is DurableItemData durable && durable.IsDurable()) | ||
{ | ||
__instance.durability = Mathf.Clamp(__instance.durability, 0f, durable.MaxDurabilityDays); | ||
} | ||
else if (itemData is DeployableItemData deployable) | ||
{ | ||
__instance.durability = Mathf.Clamp(__instance.durability, 0f, deployable.MaxDurabilityDays); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
[HarmonyPrefix] | ||
[HarmonyPriority(Priority.First)] | ||
[HarmonyPatch(typeof(SpatialItemInstance), nameof(SpatialItemInstance.GetMissingDurabilityAmount))] | ||
public static bool SpatialItemInstance_GetMissingDurabilityAmount_Prefix(SpatialItemInstance __instance, ref float __result) | ||
{ | ||
__result = 0; | ||
var itemData = __instance.GetItemData<SpatialItemData>(); | ||
if (itemData.damageMode == DamageMode.DURABILITY) | ||
{ | ||
if (itemData is DurableItemData durable && durable.IsDurable()) | ||
{ | ||
__result = durable.MaxDurabilityDays - __instance.durability; | ||
} | ||
else if (itemData is DeployableItemData deployable) | ||
{ | ||
__result = deployable.MaxDurabilityDays - __instance.durability; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static IEnumerable<CodeInstruction> ItemManager_GetItemValue_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
var matcher = new CodeMatcher(instructions, generator).MatchForward(true, | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(SpatialItemData), nameof(SpatialItemData.damageMode))), | ||
new CodeMatch(OpCodes.Ldc_I4_1), | ||
new CodeMatch(OpCodes.Bne_Un) | ||
); | ||
var end = matcher.Instruction.operand; | ||
return matcher.Advance(1).Insert( | ||
new CodeInstruction(OpCodes.Ldloc_0), | ||
new CodeInstruction(OpCodes.Isinst, typeof(DeployableItemData)), | ||
new CodeInstruction(OpCodes.Ldnull), | ||
new CodeInstruction(OpCodes.Beq, end) | ||
).CreateLabel(out Label check).Insert( | ||
new CodeInstruction(OpCodes.Ldloc_0), | ||
new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(WinchExtensions), nameof(WinchExtensions.IsDurable), new System.Type[] { typeof(SpatialItemData) })), | ||
new CodeInstruction(OpCodes.Brfalse, check), | ||
new CodeInstruction(OpCodes.Nop), | ||
new CodeInstruction(OpCodes.Br, end) | ||
).LogInstructions("").InstructionEnumeration(); | ||
} | ||
|
||
public static IEnumerable<CodeInstruction> GridManager_AddDamageToInventory_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
var matcher = new CodeMatcher(instructions, generator).MatchForward(false, | ||
new CodeMatch(OpCodes.Ldloc_S), | ||
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(SpatialItemData), nameof(SpatialItemData.damageMode))), | ||
new CodeMatch(OpCodes.Ldc_I4_1), | ||
new CodeMatch(OpCodes.Bne_Un) | ||
); | ||
var loc = matcher.Instruction.operand; | ||
var label = matcher.Advance(3).Instruction.operand; | ||
matcher.Advance(1) | ||
.Insert( | ||
new CodeInstruction(OpCodes.Ldloc_S, loc), | ||
new CodeInstruction(OpCodes.Isinst, typeof(DeployableItemData)), | ||
new CodeInstruction(OpCodes.Ldnull), | ||
new CodeInstruction(OpCodes.Beq, label) | ||
); | ||
return matcher.InstructionEnumeration(); | ||
} | ||
|
||
[HarmonyPatch] | ||
public static class ItemManager_CreateItem | ||
{ | ||
public static MethodBase TargetMethod() | ||
{ | ||
return AccessTools.Method(typeof(ItemManager), nameof(ItemManager.CreateItem), new System.Type[] { typeof(ItemData) }).MakeGenericMethod(typeof(ItemInstance)); | ||
} | ||
|
||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
return new CodeMatcher(instructions, generator).MatchForward(false, | ||
new CodeMatch(OpCodes.Ldloc_1), | ||
new CodeMatch(OpCodes.Isinst, typeof(DurableItemData)) | ||
).CreateLabel(out Label durable) | ||
.Start().MatchForward(true, | ||
new CodeMatch(OpCodes.Ldloc_1), | ||
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(SpatialItemData), nameof(SpatialItemData.damageMode))), | ||
new CodeMatch(OpCodes.Ldc_I4_1), | ||
new CodeMatch(OpCodes.Bne_Un) | ||
).Advance(1) | ||
.Insert( | ||
new CodeInstruction(OpCodes.Ldloc_1), | ||
new CodeInstruction(OpCodes.Isinst, typeof(DeployableItemData)), | ||
new CodeInstruction(OpCodes.Ldnull), | ||
new CodeInstruction(OpCodes.Beq, durable) | ||
).InstructionEnumeration(); | ||
} | ||
} | ||
|
||
[HarmonyPrefix] | ||
[HarmonyPriority(Priority.First)] | ||
[HarmonyPatch(typeof(SpatialItemInstance), nameof(SpatialItemInstance.RepairToFullDurability))] | ||
public static bool SpatialItemInstance_RepairToFullDurability_Prefix(SpatialItemInstance __instance) | ||
{ | ||
var itemData = __instance.GetItemData<SpatialItemData>(); | ||
if (itemData.damageMode == DamageMode.DURABILITY) | ||
{ | ||
float maxDurabilityDays = 0; | ||
if (itemData is DurableItemData durable && durable.IsDurable()) | ||
{ | ||
maxDurabilityDays = durable.MaxDurabilityDays; | ||
} | ||
else if (itemData is DeployableItemData deployable) | ||
{ | ||
maxDurabilityDays = deployable.MaxDurabilityDays; | ||
} | ||
if (maxDurabilityDays > 0 && __instance.durability < maxDurabilityDays) | ||
{ | ||
__instance.durability = maxDurabilityDays; | ||
if (__instance.OnDurabilityRepaired != null) __instance.OnDurabilityRepaired(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static bool FreshnessCoroutine_AdjustFreshnessForGrid_Prefix(SerializableGrid grid, float proportionOfDayJustElapsed) | ||
{ | ||
var instances = grid.spatialItems.Where(WinchExtensions.IsThawable); | ||
int cooledCells = instances.GetNumberOfCells(); | ||
float coolingChange; | ||
float fishChange; | ||
if (cooledCells == 0) | ||
{ | ||
fishChange = proportionOfDayJustElapsed * GameManager.Instance.GameConfigData.FreshnessLossPerDay; | ||
coolingChange = 0; | ||
WinchCore.Log.Debug($"[FreshnessCoroutine] AdjustFreshnessForGrid({grid.GridConfiguration}, {proportionOfDayJustElapsed}) fishChange: {fishChange}"); | ||
} | ||
else | ||
{ | ||
float propOfMaxReduction = Mathf.InverseLerp(0f, GameManager.Instance.GameConfigData.CellsForMaxFreshnessLossReduction, cooledCells); | ||
float reductionVal = GameManager.Instance.GameConfigData.FreshnessLossReductionCurve.Evaluate(propOfMaxReduction) * GameManager.Instance.GameConfigData.MaxFreshnessLossReduction; | ||
float actualVal = 1 - reductionVal; | ||
coolingChange = proportionOfDayJustElapsed * actualVal; | ||
fishChange = coolingChange * GameManager.Instance.GameConfigData.FreshnessLossPerDay; | ||
WinchCore.Log.Debug($"[FreshnessCoroutine] AdjustFreshnessForGrid({grid.GridConfiguration}, {proportionOfDayJustElapsed}) cooledCells: {cooledCells} | propOfMaxReduction: {propOfMaxReduction} | reductionVal: {reductionVal} | actualVal: {actualVal} | coolingChange: {coolingChange} | fishChange: {fishChange}"); | ||
} | ||
instances.ForEach(i => i.durability -= coolingChange); | ||
instances.Where(WinchExtensions.IsBroken).ForEach(i => grid.RemoveObjectFromGridData(i, true)); | ||
grid.GetAllItemsOfType<FishItemInstance>(ItemType.GENERAL, ItemSubtype.FISH).ForEach((FishItemInstance fi) => | ||
{ | ||
fi.freshness = Mathf.Max(fi.freshness - fishChange, 0); | ||
if (fi.freshness <= 0) GameManager.Instance.ItemManager.ReplaceFishWithRot(fi, grid, false); | ||
}); | ||
return false; | ||
} | ||
} | ||
} |
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
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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using Winch.Util; | ||
|
||
namespace Winch.Serialization.Item | ||
{ | ||
public class ThawableItemData : DurableItemData | ||
{ | ||
} | ||
} |
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,16 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Winch.Serialization.Item; | ||
|
||
public class ThawableItemDataConverter : DurableItemDataConverter | ||
{ | ||
private readonly Dictionary<string, FieldDefinition> _definitions = new() | ||
{ | ||
{ "damageMode", new(DamageMode.DESTROY, null) } | ||
}; | ||
|
||
public ThawableItemDataConverter() | ||
{ | ||
AddDefinitions(_definitions); | ||
} | ||
} |
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