Skip to content

Commit

Permalink
Merge pull request #36 from jpw1991/35-hp-not-working-correctly
Browse files Browse the repository at this point in the history
2.1.1: CVL updated to 2.3.1; add HeavyLogging config for optional hea…
  • Loading branch information
jpw1991 authored Sep 11, 2023
2 parents cf98101 + 6df5d99 commit 36b31d4
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 44 deletions.
69 changes: 42 additions & 27 deletions ChebsMercenaries/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class BasePlugin : BaseUnityPlugin
{
public const string PluginGuid = "com.chebgonaz.chebsmercenaries";
public const string PluginName = "ChebsMercenaries";
public const string PluginVersion = "2.1.0";
public const string PluginVersion = "2.1.2";
private const string ConfigFileName = PluginGuid + ".cfg";
private static readonly string ConfigFileFullPath = Path.Combine(Paths.ConfigPath, ConfigFileName);

public readonly System.Version ChebsValheimLibraryVersion = new("2.2.0");
public readonly System.Version ChebsValheimLibraryVersion = new("2.3.1");

private readonly Harmony harmony = new(PluginGuid);

Expand All @@ -40,10 +40,33 @@ public class BasePlugin : BaseUnityPlugin
};

// if set to true, the particle effects that for some reason hurt radeon are dynamically disabled
public static ConfigEntry<bool> RadeonFriendly;
public static ConfigEntry<bool> RadeonFriendly, HeavyLogging;

public static CustomLocalization Localization = LocalizationManager.Instance.GetLocalization();

public static readonly List<string> MercenaryPrefabPaths = new()
{
"ChebGonaz_HumanMiner.prefab",
"ChebGonaz_HumanWoodcutter.prefab",
"ChebGonaz_HumanArcher.prefab",
"ChebGonaz_HumanArcherTier2.prefab",
"ChebGonaz_HumanArcherTier3.prefab",
"ChebGonaz_HumanWarrior.prefab",
"ChebGonaz_HumanWarriorTier2.prefab",
"ChebGonaz_HumanWarriorTier3.prefab",
"ChebGonaz_HumanWarriorTier4.prefab",

"ChebGonaz_HumanMinerFemale.prefab",
"ChebGonaz_HumanWoodcutterFemale.prefab",
"ChebGonaz_HumanArcherFemale.prefab",
"ChebGonaz_HumanArcherTier2Female.prefab",
"ChebGonaz_HumanArcherTier3Female.prefab",
"ChebGonaz_HumanWarriorFemale.prefab",
"ChebGonaz_HumanWarriorTier2Female.prefab",
"ChebGonaz_HumanWarriorTier3Female.prefab",
"ChebGonaz_HumanWarriorTier4Female.prefab",
};

#region ConfigStuff

// Global Config Acceptable Values
Expand Down Expand Up @@ -86,6 +109,9 @@ private void CreateConfigValues()
"which seem to give users with Radeon cards trouble for unknown " +
"reasons. If you have problems with lag it might also help to switch" +
"this setting on."));

HeavyLogging = Config.Bind("General (Client)", "HeavyLogging",
false, new ConfigDescription("Turn this on for debugging. Lots of things will get logged."));

HumanMinion.CreateConfigs(this);
HumanWoodcutterMinion.CreateConfigs(this);
Expand Down Expand Up @@ -172,74 +198,63 @@ private void LoadChebGonazAssetBundle()
#endregion

#region Creatures

var prefabNames = new List<string>();

prefabNames.Add("ChebGonaz_HumanMiner.prefab");
prefabNames.Add("ChebGonaz_HumanWoodcutter.prefab");
prefabNames.Add("ChebGonaz_HumanArcher.prefab");
prefabNames.Add("ChebGonaz_HumanArcherTier2.prefab");
prefabNames.Add("ChebGonaz_HumanArcherTier3.prefab");
prefabNames.Add("ChebGonaz_HumanWarrior.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier2.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier3.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier4.prefab");

prefabNames.Add("ChebGonaz_HumanMinerFemale.prefab");
prefabNames.Add("ChebGonaz_HumanWoodcutterFemale.prefab");
prefabNames.Add("ChebGonaz_HumanArcherFemale.prefab");
prefabNames.Add("ChebGonaz_HumanArcherTier2Female.prefab");
prefabNames.Add("ChebGonaz_HumanArcherTier3Female.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorFemale.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier2Female.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier3Female.prefab");
prefabNames.Add("ChebGonaz_HumanWarriorTier4Female.prefab");

prefabNames.ForEach(prefabName =>
MercenaryPrefabPaths.ForEach(prefabName =>
{
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Loading prefab {prefabName}...");

var prefab = Base.LoadPrefabFromBundle(prefabName, chebgonazAssetBundle, RadeonFriendly.Value);
switch (prefabName)
{
case "ChebGonaz_HumanMiner.prefab":
case "ChebGonaz_HumanMinerFemale.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding HumanMinerMinion component to {prefabName}.");
prefab.AddComponent<HumanMinerMinion>();
break;
case "ChebGonaz_HumanWoodcutter.prefab":
case "ChebGonaz_HumanWoodcutterFemale.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding HumanWoodcutterMinion component to {prefabName}.");
prefab.AddComponent<HumanWoodcutterMinion>();
break;

case "ChebGonaz_HumanWarrior.prefab":
case "ChebGonaz_HumanWarriorFemale.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryWarriorTier1Minion component to {prefabName}.");
prefab.AddComponent<MercenaryWarriorTier1Minion>();
break;
case "ChebGonaz_HumanWarriorTier2.prefab":
case "ChebGonaz_HumanWarriorTier2Female.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryWarriorTier2Minion component to {prefabName}.");
prefab.AddComponent<MercenaryWarriorTier2Minion>();
break;
case "ChebGonaz_HumanWarriorTier3.prefab":
case "ChebGonaz_HumanWarriorTier3Female.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryWarriorTier3Minion component to {prefabName}.");
prefab.AddComponent<MercenaryWarriorTier3Minion>();
break;
case "ChebGonaz_HumanWarriorTier4.prefab":
case "ChebGonaz_HumanWarriorTier4Female.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryWarriorTier4Minion component to {prefabName}.");
prefab.AddComponent<MercenaryWarriorTier4Minion>();
break;

case "ChebGonaz_HumanArcher.prefab":
case "ChebGonaz_HumanArcherFemale.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryArcherTier1Minion component to {prefabName}.");
prefab.AddComponent<MercenaryArcherTier1Minion>();
break;
case "ChebGonaz_HumanArcherTier2.prefab":
case "ChebGonaz_HumanArcherTier2Female.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryArcherTier2Minion component to {prefabName}.");
prefab.AddComponent<MercenaryArcherTier2Minion>();
break;
case "ChebGonaz_HumanArcherTier3.prefab":
case "ChebGonaz_HumanArcherTier3Female.prefab":
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding MercenaryArcherTier3Minion component to {prefabName}.");
prefab.AddComponent<MercenaryArcherTier3Minion>();
break;

default:
if (HeavyLogging.Value) Jotunn.Logger.LogInfo($"Adding HumanMinion component to {prefabName}.");
prefab.gameObject.AddComponent<HumanMinion>();
break;
}
Expand Down
1 change: 1 addition & 0 deletions ChebsMercenaries/ChebsMercenaries.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="Patches\TameablePatches.cs" />
<Compile Include="Patches\VisEquipmentPatches.cs" />
<Compile Include="Patches\WearNTearPatches.cs" />
<Compile Include="Patches\ZNetScenePatches.cs" />
<Compile Include="Properties\IgnoreAccessModifiers.cs" />
<Compile Include="BasePlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
43 changes: 32 additions & 11 deletions ChebsMercenaries/Minions/HumanMinion.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using BepInEx.Configuration;
using ChebsMercenaries.Structure;
using ChebsValheimLibrary.Common;
using ChebsValheimLibrary.Minions;
using UnityEngine;
using Logger = Jotunn.Logger;
using Random = UnityEngine.Random;

namespace ChebsMercenaries.Minions
{
Expand Down Expand Up @@ -222,6 +219,11 @@ protected IEnumerator WaitForZNet()
}

RestoreDrops();

if (BasePlugin.HeavyLogging.Value)
{
Logger.LogInfo($"Health set for {gameObject.name}: humanoid.m_health={humanoid.m_health}");
}
}

public void ScaleEquipment(MercenaryType mercenaryType, ArmorType armorType)
Expand Down Expand Up @@ -314,7 +316,14 @@ public void ScaleEquipment(MercenaryType mercenaryType, ArmorType armorType)

humanoid.m_defaultItems = defaultItems.ToArray();

if (BasePlugin.HeavyLogging.Value)
{
var equipmentStringLog = string.Join(", ", defaultItems.Select(a => a.name));
Logger.LogInfo($"Provided equipment {mercenaryType} {armorType}: {equipmentStringLog}");
}

humanoid.GiveDefaultItems();
humanoid.m_visEquipment.UpdateEquipmentVisuals();
}

public static void Spawn(MercenaryType mercenaryType, ArmorType armorType, Transform spawner)
Expand All @@ -323,13 +332,18 @@ public static void Spawn(MercenaryType mercenaryType, ArmorType armorType, Trans

if (ZNetScene.instance == null)
{
Jotunn.Logger.LogWarning("Spawn: ZNetScene.instance is null, trying again later...");
Logger.LogWarning("Spawn: ZNetScene.instance is null, trying again later...");
return;
}

var female = Random.value < ChanceOfFemale.Value;

var prefabName = female ? PrefabNamesFemale[mercenaryType] : PrefabNames[mercenaryType];
if (BasePlugin.HeavyLogging.Value)
{
var genderLog = female ? "female" : "male";
Logger.LogInfo($"Spawning {genderLog} {mercenaryType} with {armorType} from {prefabName}.");
};
var prefab = ZNetScene.instance.GetPrefab(prefabName);
if (!prefab)
{
Expand All @@ -342,7 +356,7 @@ public static void Spawn(MercenaryType mercenaryType, ArmorType armorType, Trans

if (spawnedChar == null)
{
Jotunn.Logger.LogError("Spawn: spawnedChar is null");
Logger.LogError("Spawn: spawnedChar is null");
return;
}

Expand All @@ -352,7 +366,7 @@ public static void Spawn(MercenaryType mercenaryType, ArmorType armorType, Trans

if (!spawnedChar.TryGetComponent(out Humanoid humanoid))
{
Jotunn.Logger.LogError("Spawn: spawnedChar has no humanoid component");
Logger.LogError("Spawn: spawnedChar has no humanoid component");
return;
}

Expand All @@ -366,19 +380,26 @@ public static void Spawn(MercenaryType mercenaryType, ArmorType armorType, Trans
if (!female)
{
var randomBeard = _beards[Random.Range(0, _beards.Count)].gameObject.name;
if (BasePlugin.HeavyLogging.Value) Logger.LogInfo($"Applying beard {randomBeard}.");
humanoid.SetBeard(randomBeard);
humanoid.m_visEquipment.SetBeardItem(humanoid.m_beardItem);
}

if (BasePlugin.HeavyLogging.Value) Logger.LogInfo($"Applying hair {humanoid.m_hairItem}.");
humanoid.m_visEquipment.SetHairItem(humanoid.m_hairItem);
humanoid.m_visEquipment.UpdateEquipmentVisuals();

var minion = mercenaryType switch
// var minion = mercenaryType switch
// {
// MercenaryType.Miner => spawnedChar.AddComponent<HumanMinerMinion>(),
// MercenaryType.Woodcutter => spawnedChar.AddComponent<HumanWoodcutterMinion>(),
// _ => spawnedChar.AddComponent<HumanMinion>()
// };
if (!spawnedChar.TryGetComponent(out HumanMinion minion))
{
MercenaryType.Miner => spawnedChar.AddComponent<HumanMinerMinion>(),
MercenaryType.Woodcutter => spawnedChar.AddComponent<HumanWoodcutterMinion>(),
_ => spawnedChar.AddComponent<HumanMinion>()
};
Logger.LogError("Spawn: spawnedChar has no HumanMinion component");
return;
}

minion.ScaleEquipment(mercenaryType, armorType);

Expand Down
2 changes: 2 additions & 0 deletions ChebsMercenaries/Package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ You can find the github [here](https://github.com/jpw1991/chebs-mercenaries).

Date | Version | Notes
--- | --- | ---
12/09/2023 | 2.1.2 | Fix issue of armor not displaying properly on mercs; fix issue of skin colors not changing for mercs
10/09/2023 | 2.1.1 | CVL updated to 2.3.1; add HeavyLogging config for optional heavy debugging; optimize adding of components to HumanMinion; add shebang to python scripts
23/08/2023 | 2.1.0 | update for new valheim patch
28/07/2023 | 2.0.0 | Workers should behave more realistically with gradual destruction of rocks, trees, etc.
23/07/2023 | 1.7.0 | Add Russian translation & open untranslated parts of the mod up to future translation; update CVL to 2.1.2; permit tweaks of mercenary health in configs; fix diverse bugs eg. female workers not spawning with their AI attached
Expand Down
Binary file modified ChebsMercenaries/Package/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ChebsMercenaries/Package/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ChebsMercenaries",
"description": "Cheb's Mercenaries adds mercenaries to Valheim that you can purchase with gold and upgrade with materials to fight (warriors, archers) or perform work (lumberjacks, miners).",
"version_number": "2.1.0",
"version_number": "2.1.2",
"website_url": "https://github.com/jpw1991/chebs-mercenaries",
"dependencies": [
"ValheimModding-Jotunn-2.11.0"
Expand Down
67 changes: 67 additions & 0 deletions ChebsMercenaries/Patches/ZNetScenePatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using ChebsMercenaries.Minions;
using HarmonyLib;

// ReSharper disable InconsistentNaming
// ReSharper disable UnusedParameter.Local

// Harmony patching is very sensitive regarding parameter names. Everything in this region should be hand crafted
// and not touched by well-meaning but clueless IDE optimizations.
// eg.
// * __instance MUST be named with exactly two underscores.
// * ___m_drops MUST be named with exactly three underscores.
// * Unused parameters must be left there because they must match the method to override
// * All patch methods need to be static
//
// This is because all of this has a special meaning to Harmony.

namespace ChebsMercenaries.Patches
{
[HarmonyPatch]
public class ZNetScenePatches
{
// Steal the Player's material from the player and give it to the mercenaries so that their armour displays
// properly. Thanks to JustAFrogger for this solution.
[HarmonyPatch(typeof(ZNetScene), nameof(ZNetScene.Awake)), HarmonyPostfix]
public static void ZNetScenePatch(ZNetScene __instance)
{
var playerPrefab = __instance.GetPrefab("Player");
if (playerPrefab == null)
{
Jotunn.Logger.LogError($"ZNetScenePatches: Failed to get player prefab. Armor may not display" +
$"correctly on mercenaries.");
return;
}

if (!playerPrefab.TryGetComponent(out VisEquipment playerVisEquipment))
{
Jotunn.Logger.LogError($"ZNetScenePatches: Failed to get Player's VisEquipment " +
$"component. Armor may not display correctly on mercenaries.");
return;
}

var male = playerVisEquipment.m_models[0].m_baseMaterial;
var female = playerVisEquipment.m_models[1].m_baseMaterial;

BasePlugin.MercenaryPrefabPaths.ForEach(prefabFileName =>
{
var prefabName = prefabFileName.Replace(".prefab", "");
var mercPrefab = __instance.GetPrefab(prefabName);
if (mercPrefab == null)
{
Jotunn.Logger.LogError($"ZNetScenePatches: Failed to get {prefabName}. Armor may not display" +
$"correctly on mercenaries.");
return;
}

if (!mercPrefab.TryGetComponent(out VisEquipment visEquipment))
{
Jotunn.Logger.LogError($"ZNetScenePatches: Failed to get {prefabName}'s VisEquipment " +
$"component. Armor may not display correctly on mercenaries.");
return;
}

visEquipment.m_bodyModel.material = prefabName.Contains("Female") ? female : male;
});
}
}
}
4 changes: 2 additions & 2 deletions ChebsMercenaries/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyVersion("2.1.2.0")]
[assembly: AssemblyFileVersion("2.1.2.0")]
8 changes: 8 additions & 0 deletions ChebsMercenaries/Structure/MercenaryChest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ private void PayForMercenary(HumanMinion.MercenaryType mercenaryType)
HumanMinion.MercenaryType.Woodcutter => HumanWoodcutterMinion.ItemsCost,
_ => null
};
if (BasePlugin.HeavyLogging.Value)
{
var itemsCostLog = itemsCost?.Value == null ? "" : string.Join(", ", itemsCost.Value);
Jotunn.Logger.LogInfo($"Paying for mercenary {mercenaryType} with {itemsCostLog}...");
}
ChebGonazMinion.ConsumeRequirements(itemsCost, _inventory);
}

Expand All @@ -149,6 +154,8 @@ private ChebGonazMinion.ArmorType UpgradeMercenaryEquipment()
ArmorIronRequiredConfig.Value,
ArmorBronzeRequiredConfig.Value,
ArmorLeatherScrapsRequiredConfig.Value);

if (BasePlugin.HeavyLogging.Value) Jotunn.Logger.LogInfo($"Determining mercenary's armour type: {armorType}.");

switch (armorType)
{
Expand Down Expand Up @@ -252,6 +259,7 @@ IEnumerator Recruitment()
Localization.instance.Localize("$chebgonaz_mercenarychest_recruitmentmessage")
.Replace("%1", nextMercLocalized)
.Replace("%2", (RecruitmentInterval.Value - (Time.time - _lastRecruitmentAt)).ToString("0"));
if (BasePlugin.HeavyLogging.Value) Jotunn.Logger.LogInfo(recruitmentMessage);
Chat.instance.SetNpcText(gameObject, Vector3.up, 5f, 4f, "",
recruitmentMessage,
false);
Expand Down
Loading

0 comments on commit 36b31d4

Please sign in to comment.