Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instant Teleporter Option & Performance to AutoFuel #752

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified ValheimPlus/Assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ValheimPlus/Configurations/Sections/PlayerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public class PlayerConfiguration : ServerSyncConfig<PlayerConfiguration>
public bool skipTutorials { get; internal set; } = false;
public bool disableEncumbered { get; internal set; } = false;
public bool autoPickUpWhenEncumbered { get; internal set; } = false;
public bool disableEightSecondTeleport { get; internal set; } = false;
}
}
14 changes: 13 additions & 1 deletion ValheimPlus/GameClasses/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,6 @@ public static float GetMaxCarryWeight()
}
}


[HarmonyPatch(typeof(Player), nameof(Player.GetFirstRequiredItem))]
public static class Player_GetFirstRequiredItem_Transpiler
{
Expand Down Expand Up @@ -1168,4 +1167,17 @@ public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction
return instructions;
}
}

[HarmonyPatch(typeof(Player), nameof(Player.UpdateTeleport))]
public static class Player_UpdateTeleport_Patch
{
[HarmonyPrefix]
private static void Prefix(float dt, ref float ___m_teleportTimer, ref bool ___m_teleporting, ref bool ___m_distantTeleport, ref Vector3 ___m_teleportTargetPos)
{
if (Configuration.Current.Player.disableEightSecondTeleport && ZNetScene.instance.IsAreaReady(___m_teleportTargetPos) && ___m_teleporting)
{
___m_teleportTimer += 10f;
}
}
}
}
159 changes: 78 additions & 81 deletions ValheimPlus/GameClasses/Smelter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,102 +151,99 @@ static void Prefix(Smelter __instance)
if (__instance == null || !Player.m_localPlayer || __instance.m_nview == null || !__instance.m_nview.IsOwner())
return;

Smelter smelter = __instance;

Stopwatch delta = GameObjectAssistant.GetStopwatch(smelter.gameObject);
Stopwatch delta = GameObjectAssistant.GetStopwatch(__instance.gameObject);
if (delta.IsRunning && delta.ElapsedMilliseconds < 1000) return;
delta.Restart();

float autoFuelRange = 0f;
bool ignorePrivateAreaCheck = false;
bool isKiln = false;
if (smelter.m_name.Equals(SmelterDefinitions.KilnName))
{
if (!Configuration.Current.Kiln.IsEnabled || !Configuration.Current.Kiln.autoFuel)
return;
isKiln = true;
autoFuelRange = Configuration.Current.Kiln.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Kiln.ignorePrivateAreaCheck;
}
else if (smelter.m_name.Equals(SmelterDefinitions.SmelterName))
{
if (!Configuration.Current.Smelter.IsEnabled || !Configuration.Current.Smelter.autoFuel)
return;
autoFuelRange = Configuration.Current.Smelter.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Smelter.ignorePrivateAreaCheck;
}
else if (smelter.m_name.Equals(SmelterDefinitions.FurnaceName))
{
if (!Configuration.Current.Furnace.IsEnabled || !Configuration.Current.Furnace.autoFuel)
return;
autoFuelRange = Configuration.Current.Furnace.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Furnace.ignorePrivateAreaCheck;
}
else if (__instance.m_name.Equals(SmelterDefinitions.WindmillName))
switch(__instance.m_name)
{
if (!Configuration.Current.Windmill.IsEnabled || !Configuration.Current.Windmill.autoFuel)
case SmelterDefinitions.KilnName:
if (!Configuration.Current.Kiln.IsEnabled || !Configuration.Current.Kiln.autoFuel)
return;
isKiln = true;
autoFuelRange = Configuration.Current.Kiln.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Kiln.ignorePrivateAreaCheck;
break;
case SmelterDefinitions.SmelterName:
if (!Configuration.Current.Smelter.IsEnabled || !Configuration.Current.Smelter.autoFuel)
return;
autoFuelRange = Configuration.Current.Smelter.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Smelter.ignorePrivateAreaCheck;
break;
case SmelterDefinitions.FurnaceName:
if (!Configuration.Current.Furnace.IsEnabled || !Configuration.Current.Furnace.autoFuel)
return;
autoFuelRange = Configuration.Current.Furnace.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Furnace.ignorePrivateAreaCheck;
break;
case SmelterDefinitions.WindmillName:
if (!Configuration.Current.Windmill.IsEnabled || !Configuration.Current.Windmill.autoFuel)
return;
autoFuelRange = Configuration.Current.Windmill.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Windmill.ignorePrivateAreaCheck;
break;
case SmelterDefinitions.SpinningWheelName:
if (!Configuration.Current.SpinningWheel.IsEnabled || !Configuration.Current.SpinningWheel.autoFuel)
return;
autoFuelRange = Configuration.Current.SpinningWheel.autoRange;
ignorePrivateAreaCheck = Configuration.Current.SpinningWheel.ignorePrivateAreaCheck;
break;
default:
return;
autoFuelRange = Configuration.Current.Windmill.autoRange;
ignorePrivateAreaCheck = Configuration.Current.Windmill.ignorePrivateAreaCheck;
}
else if (__instance.m_name.Equals(SmelterDefinitions.SpinningWheelName))
{
if (!Configuration.Current.SpinningWheel.IsEnabled || !Configuration.Current.SpinningWheel.autoFuel)
return;
autoFuelRange = Configuration.Current.SpinningWheel.autoRange;
ignorePrivateAreaCheck = Configuration.Current.SpinningWheel.ignorePrivateAreaCheck;
}

autoFuelRange = Helper.Clamp(autoFuelRange, 1, 50);

int toMaxOre = smelter.m_maxOre - smelter.GetQueueSize();
int toMaxFuel = smelter.m_maxFuel - (int)System.Math.Ceiling(smelter.GetFuel());
int toMaxOre = __instance.m_maxOre - __instance.GetQueueSize();
int toMaxFuel = __instance.m_maxFuel - (int)System.Math.Ceiling(__instance.GetFuel());
int threshold = Configuration.Current.Kiln.stopAutoFuelThreshold < 0 ? 0 : Configuration.Current.Kiln.stopAutoFuelThreshold;

if (smelter.m_fuelItem && toMaxFuel > 0)
if (__instance.m_fuelItem && toMaxFuel > 0)
{
ItemDrop.ItemData fuelItemData = smelter.m_fuelItem.m_itemData;
ItemDrop.ItemData fuelItemData = __instance.m_fuelItem.m_itemData;

// Check for fuel in nearby containers
int addedFuel = InventoryAssistant.RemoveItemInAmountFromAllNearbyChests(smelter.gameObject, autoFuelRange, fuelItemData, toMaxFuel, !ignorePrivateAreaCheck);
int addedFuel = InventoryAssistant.RemoveItemInAmountFromAllNearbyChests(__instance.gameObject, autoFuelRange, fuelItemData, toMaxFuel, !ignorePrivateAreaCheck);
for (int i = 0; i < addedFuel; i++)
{
smelter.m_nview.InvokeRPC("AddFuel", new object[] { });
__instance.m_nview.InvokeRPC("AddFuel", new object[] { });
}
if (addedFuel > 0)
ZLog.Log("Added " + addedFuel + " fuel(" + fuelItemData.m_shared.m_name + ") in " + smelter.m_name);
UnityEngine.Debug.Log("Added " + addedFuel + " fuel(" + fuelItemData.m_shared.m_name + ") in " + __instance.m_name);
}
if (toMaxOre > 0)
{
List<Container> nearbyChests = InventoryAssistant.GetNearbyChests(smelter.gameObject, autoFuelRange);
foreach (Container c in nearbyChests)
foreach (Smelter.ItemConversion itemConversion in __instance.m_conversion)
{
foreach (Smelter.ItemConversion itemConversion in smelter.m_conversion)
if (isKiln)
{
if (isKiln)
{
if (Configuration.Current.Kiln.dontProcessFineWood && itemConversion.m_from.m_itemData.m_shared.m_name.Equals(WoodDefinitions.FineWoodName)) continue;
if (Configuration.Current.Kiln.dontProcessRoundLog && itemConversion.m_from.m_itemData.m_shared.m_name.Equals(WoodDefinitions.RoundLogName)) continue;
if (Configuration.Current.Kiln.dontProcessFineWood && itemConversion.m_from.m_itemData.m_shared.m_name.Equals(WoodDefinitions.FineWoodName)) continue;
if (Configuration.Current.Kiln.dontProcessRoundLog && itemConversion.m_from.m_itemData.m_shared.m_name.Equals(WoodDefinitions.RoundLogName)) continue;

int threshold = Configuration.Current.Kiln.stopAutoFuelThreshold < 0 ? 0 : Configuration.Current.Kiln.stopAutoFuelThreshold;
if (threshold > 0 && InventoryAssistant.GetItemAmountInItemList(InventoryAssistant.GetNearbyChestItemsByContainerList(nearbyChests), itemConversion.m_to.m_itemData) >= threshold) return;
}
if (threshold > 0 && InventoryAssistant.GetItemAmountInItemList(
InventoryAssistant.GetNearbyChestItems(__instance.gameObject, autoFuelRange, !ignorePrivateAreaCheck), itemConversion.m_to.m_itemData) >= threshold
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move the InventoryAssistant.GetNearbyChestItems call out of the foreach loop to avoid calling multiple time this function.

This function calls InventoryAssistant.GetNearbyChests which uses Physics.OverlapSphere. This is a function that has an heavy impact on performance and should only be run once for a given __instance.gameObject.

) return;
}

ItemDrop.ItemData oreItem = itemConversion.m_from.m_itemData;
int addedOres = InventoryAssistant.RemoveItemFromChest(c, oreItem, toMaxOre);
if (addedOres > 0)
ItemDrop.ItemData oreItem = itemConversion.m_from.m_itemData;

int addedOres = InventoryAssistant.RemoveItemInAmountFromAllNearbyChests(__instance.gameObject, autoFuelRange, oreItem, toMaxFuel, !ignorePrivateAreaCheck);
if (addedOres > 0)
{
GameObject orePrefab = ObjectDB.instance.GetItemPrefab(itemConversion.m_from.gameObject.name);

for (int i = 0; i < addedOres; i++)
{
GameObject orePrefab = ObjectDB.instance.GetItemPrefab(itemConversion.m_from.gameObject.name);

for (int i = 0; i < addedOres; i++)
{
smelter.m_nview.InvokeRPC("AddOre", new object[] { orePrefab.name });
}
toMaxOre -= addedOres;
if (addedOres > 0)
ZLog.Log("Added " + addedOres + " ores(" + oreItem.m_shared.m_name + ") in " + smelter.m_name);
if (toMaxOre == 0)
return;
__instance.m_nview.InvokeRPC("AddOre", new object[] { orePrefab.name });
}
toMaxOre -= addedOres;
if (addedOres > 0)
UnityEngine.Debug.Log("Added " + addedOres + " ores(" + oreItem.m_shared.m_name + ") in " + __instance.m_name);
if (toMaxOre == 0)
return;
}
}
}
Expand Down Expand Up @@ -340,24 +337,24 @@ private static float GetPowerOutput(Windmill __instance)

public static class SmelterDefinitions
{
public static readonly string KilnName = "$piece_charcoalkiln";
public static readonly string SmelterName = "$piece_smelter";
public static readonly string FurnaceName = "$piece_blastfurnace";
public static readonly string WindmillName = "$piece_windmill";
public static readonly string SpinningWheelName = "$piece_spinningwheel";
public const string KilnName = "$piece_charcoalkiln";
public const string SmelterName = "$piece_smelter";
public const string FurnaceName = "$piece_blastfurnace";
public const string WindmillName = "$piece_windmill";
public const string SpinningWheelName = "$piece_spinningwheel";
}

public static class FurnaceDefinitions
{
public static readonly string CopperOrePrefabName = "CopperOre";
public static readonly string ScrapIronPrefabName = "IronScrap";
public static readonly string SilverOrePrefabName = "SilverOre";
public static readonly string TinOrePrefabName = "TinOre";

public static readonly string CopperPrefabName = "Copper";
public static readonly string IronPrefabName = "Iron";
public static readonly string SilverPrefabName = "Silver";
public static readonly string TinPrefabName = "Tin";
public const string CopperOrePrefabName = "CopperOre";
public const string ScrapIronPrefabName = "IronScrap";
public const string SilverOrePrefabName = "SilverOre";
public const string TinOrePrefabName = "TinOre";

public const string CopperPrefabName = "Copper";
public const string IronPrefabName = "Iron";
public const string SilverPrefabName = "Silver";
public const string TinPrefabName = "Tin";

public static readonly List<Smelter.ItemConversion> AdditionalConversions = new List<Smelter.ItemConversion>
{
Expand Down
2 changes: 1 addition & 1 deletion ValheimPlus/ValheimPlus.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<PropertyGroup>
<!-- Needs to be your path to the base Valheim folder -->
Expand Down