Skip to content

Commit

Permalink
Merge pull request #3 from Snarkki/LootTesting
Browse files Browse the repository at this point in the history
Loot testing
  • Loading branch information
Snarkki committed Dec 31, 2021
2 parents df23cec + 9e71ee9 commit af2ded4
Show file tree
Hide file tree
Showing 16 changed files with 12,740 additions and 653 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS0649: Field 'UnusedLootItem.Cost' is never assigned to, and will always have its default value 0
dotnet_diagnostic.CS0649.severity = none
9 changes: 7 additions & 2 deletions RandomEnemies.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ VisualStudioVersion = 17.0.31825.309
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomEnemies", "RandomEnemies\RandomEnemies.csproj", "{DDFAF76E-2A49-4A05-9198-7A80613B1794}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{36F63F26-09E2-4D31-A3AC-4AE2774520BC}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ModMaker", "..\WrathModMaker\ModMaker\ModMaker.shproj", "{7D3F9428-C134-47FE-9D04-305B4D330401}"
EndProject
Global
Expand All @@ -19,8 +24,8 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Release|Any CPU.Build.0 = Release|Any CPU
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{DDFAF76E-2A49-4A05-9198-7A80613B1794}.Release|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
47 changes: 47 additions & 0 deletions RandomEnemies/Loot/Loot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;


namespace RandomEnemies.Mechanics
{
[Serializable]
internal class UnusedLootItem
{
[JsonProperty("Name")]
public string Name;

[JsonProperty("File")]
public string File;

[JsonProperty("Cost")]
public int Cost;

[JsonProperty("CR")]
public int CR;

[JsonProperty("Description")]
public string Description;
}

internal class UnusedLootDict
{
[JsonProperty("Dict")]
public Dictionary<string, UnusedLootItem> LootDict = new Dictionary<string, UnusedLootItem>();
}

internal static class LootJSON
{
public static UnusedLootDict LoadJSON(string json)
{
return JsonConvert.DeserializeObject<UnusedLootDict>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
}
}
}
106 changes: 106 additions & 0 deletions RandomEnemies/Loot/LootHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static RandomEnemies.Main;
using Kingmaker.PubSubSystem;
using Kingmaker;
using Kingmaker.EntitySystem;
using Kingmaker.View.MapObjects;
using Kingmaker.Utility;
using Kingmaker.EntitySystem.Entities;
using Owlcat.Runtime.Core.Utils;
using Kingmaker.Blueprints.Area;
using RandomEnemies.Utilities;
using UnityEngine;
using System.IO;
using Kingmaker.Blueprints.Items;
using Kingmaker.Blueprints;
using Kingmaker.Items;
using Kingmaker.Blueprints.Loot;
using Kingmaker.RuleSystem;
using Kingmaker.Blueprints.Items.Equipment;
using Kingmaker.Dungeon.Blueprints;
using Kingmaker.Blueprints.Root;

namespace RandomEnemies.Mechanics
{
class LootHandler
{

public static UnusedLootDict RegLoot = LootJSON.LoadJSON(File.ReadAllText($"{ModPath}/regular.json"));

public static int Level2Cost(int level)
{
return (int)(Math.Pow(level, 2) * 160);
}

public static void GiveRandom(UnitEntityData entityData, int level)
{
SetWrap.EnsureItemsGiven();
var blueprint = RegLoot.LootDict.Where(p => p.Value.CR >= (level - 5 >= 1 ? level - 5 : 1) && p.Value.CR <= level && p.Value.Cost <= Level2Cost(level))?.Random().Key;
if (blueprint.IsNullOrEmpty() || SetWrap.ItemsGiven[Game.Instance.Player.GameId].Contains(blueprint))
{
if (3 < level && level < 20)
GiveRandom(entityData, level + 1);
return;
}
if (!SetWrap.ItemsGiven[Game.Instance.Player.GameId].Contains(blueprint))
SetWrap.ItemsGiven[Game.Instance.Player.GameId].Add(blueprint);
Main.LogDebug($"Gave: {blueprint} to entity {entityData}");
entityData.Inventory.Add(ResourcesLibrary.TryGetBlueprint<BlueprintItem>(blueprint));
}

public static int CalculateLevel()
{
var roll = RulebookEvent.Dice.D100;
var level = Game.Instance.Player.PartyLevel;
if (0 <= roll && roll < 10)
{
level -= 3;
}
else if (10 <= roll && roll < 25)
{
level -= 2;
}
else if (25 <= roll && roll < 40)
{
level -= 1;
}
else if (40 <= roll && roll < 80)
{
//no change
}
else if (80 <= roll && roll < 95)
{
level += 1;
}
else if (95 <= roll && roll < 99)
{
level += 2;
}
else if (roll == 100)
{
level += 3;
}
else
{
//no change
}

if (level < 1)
level = 1;

return level;
}
public static bool RollForLoot()
{
int roll = UnityEngine.Random.Range(1, 100);
if (roll <= (int)Main.settings.ChanceForLootDrop)
{ return true; }
else return false;
}
}
}

53 changes: 53 additions & 0 deletions RandomEnemies/Loot/LootHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Kingmaker;
using Kingmaker.EntitySystem;
using Kingmaker.Utility;
using Kingmaker.View.MapObjects;
using RandomEnemies.Utilities;
using Owlcat.Runtime.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.Rendering;
using Kingmaker.EntitySystem.Entities;

namespace RandomEnemies.Mechanics
{
public static class LootHelper
{
public static IEnumerable<LootWrapper> GetEntitiesCurrentArea()
{
SetWrap.EnsureContainersChecked();
var lootWrapperList = new List<LootWrapper>();

var interactionLootParts = Game.Instance.State.Units
.Where<UnitEntityData>(e => e.IsPlayersEnemy)
.Where<UnitEntityData>(e => !e.IsDeadAndHasLoot)
.Select<EntityDataBase, InteractionLootPart>(i => i.Get<InteractionLootPart>())
.Where<InteractionLootPart>(i => i?.Loot != Game.Instance.Player.SharedStash)
.NotNull<InteractionLootPart>();

var source = TempList.Get<InteractionLootPart>();

foreach (var interactionLootPart in interactionLootParts)
{
if (!interactionLootPart.IsViewed)
source.Add(interactionLootPart);
}

var collection = source.Distinct<InteractionLootPart>((IEqualityComparer<InteractionLootPart>)new MassLootHelper.LootDuplicateCheck()).Select<InteractionLootPart, LootWrapper>((Func<InteractionLootPart, LootWrapper>)(i => new LootWrapper()
{
InteractionLoot = i
}));

lootWrapperList.AddRange(collection);
return (IEnumerable<LootWrapper>)lootWrapperList;
}

public static int Worth(this LootWrapper container)
{
return container.InteractionLoot.Loot.Items.Select(i => i.Cost).Sum();
}
}
}
47 changes: 47 additions & 0 deletions RandomEnemies/Loot/UnusedLootItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;


namespace RandomEnemies.Loot
{
[Serializable]
internal class UnusedLootItem
{
[JsonProperty("Name")]
public string Name;

[JsonProperty("File")]
public string File;

[JsonProperty("Cost")]
public int Cost;

[JsonProperty("CR")]
public int CR;

[JsonProperty("Description")]
public string Description;
}

internal class UnusedLootDict
{
[JsonProperty("Dict")]
public Dictionary<string, UnusedLootItem> LootDict = new Dictionary<string, UnusedLootItem>();
}

internal static class LootJSON
{
public static UnusedLootDict LoadJSON(string json)
{
return JsonConvert.DeserializeObject<UnusedLootDict>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
}
}
}
11 changes: 10 additions & 1 deletion RandomEnemies/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ namespace RandomEnemies
{
public class Main
{
private const string CombatSpeed = "Change Combat Speed";
public static string ModPath;

private static bool Load(UnityModManager.ModEntry modEntry)
{
modEntry.OnToggle = new Func<UnityModManager.ModEntry, bool, bool>(Main.OnToggle);
var harmony = new Harmony(modEntry.Info.Id);

Main.settings = UnityModManager.ModSettings.Load<Settings>(modEntry);
ModSettings.ModEntry = modEntry;
ModPath = modEntry.Path;
ModSettings.LoadAllSettings();
modEntry.OnGUI = new Action<UnityModManager.ModEntry>(Main.OnGUI);
modEntry.OnSaveGUI = new Action<UnityModManager.ModEntry>(Main.OnSaveGUI);
Expand Down Expand Up @@ -65,6 +67,12 @@ private static void OnGUI(UnityModManager.ModEntry modEntry)
settings.ChanceForLootDrop = GUILayout.HorizontalSlider(settings.ChanceForLootDrop, 0.0f, 100.0f, GUILayout.Width(100f));
GUILayout.Label("Chance for additional loot: " + Math.Round(settings.ChanceForLootDrop, 0));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
Settings.UseRandomEncounters = GUILayout.Toggle(Settings.UseRandomEncounters, "Enable random encounters");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
Settings.UseRandomLoot = GUILayout.Toggle(Settings.UseRandomLoot, "Enable random loot");
GUILayout.EndHorizontal();


}
Expand All @@ -83,6 +91,7 @@ public static bool GetInteger(string stringToConvert, out int intOutValue, int m
public static List<BlueprintUnit> unitBlackListBP = new List<BlueprintUnit>();
public static List<string> SpawnedUnitId = new List<string>();
public static Dictionary<string, BlueprintItem> SpawnedUnitsLoots = new Dictionary<string, BlueprintItem>();

public static void Log(string msg)
{
ModSettings.ModEntry.Logger.Log(msg);
Expand Down

0 comments on commit af2ded4

Please sign in to comment.