Crafting change

This commit is contained in:
dingo
2025-12-28 09:36:40 -05:00
parent f1b2749526
commit 8cc54cfafd
9 changed files with 3029 additions and 18 deletions
+104 -12
View File
@@ -376,7 +376,7 @@ public class CraftingHandler
var rollResults = new List<(string Stat, int Value)>();
// Handle stat rolling for equipment
if (recipe.HasStatRolls && recipe.PossibleStats.Count > 0)
if (recipe.HasStatRolls && outputItemDef != null)
{
var rng = new Random();
craftedItem = new DungeonCraftedItem
@@ -389,22 +389,40 @@ public class CraftingHandler
CraftingLevelAtCreation = skill.Level
};
// Roll stats
int rollCount = skill.StatRollCount;
int minRoll = skill.MinStatRoll;
int maxRoll = skill.MaxStatRoll;
// Calculate skill quality bonus
// 2x required level = 50% chance for max on each roll
// Formula: skillBonus = min(1.0, skill.Level / (recipe.RequiredLevel * 2))
float skillBonus = Math.Min(1.0f, (float)skill.Level / (recipe.RequiredLevel * 2));
int totalRolled = 0;
int maxPossibleTotal = 0;
for (int i = 0; i < rollCount; i++)
// Roll each stat that has a range defined on the item
rollResults = RollItemStats(outputItemDef, skillBonus, rng, out totalRolled, out maxPossibleTotal);
// Apply rolled stats to the crafted item
foreach (var (stat, value) in rollResults)
{
var stat = recipe.PossibleStats[rng.Next(recipe.PossibleStats.Count)];
int value = rng.Next(minRoll, maxRoll + 1);
totalRolled += value;
maxPossibleTotal += maxRoll;
ApplyStatRoll(craftedItem, stat, value);
rollResults.Add((stat, value));
}
// If no ranges defined, fall back to old system using recipe's PossibleStats
if (rollResults.Count == 0 && recipe.PossibleStats.Count > 0)
{
int rollCount = skill.StatRollCount;
int minRoll = skill.MinStatRoll;
int maxRoll = skill.MaxStatRoll;
for (int i = 0; i < rollCount; i++)
{
var stat = recipe.PossibleStats[rng.Next(recipe.PossibleStats.Count)];
int value = RollWithSkillBonus(minRoll, maxRoll, skillBonus, rng);
totalRolled += value;
maxPossibleTotal += maxRoll;
ApplyStatRoll(craftedItem, stat, value);
rollResults.Add((stat, value));
}
}
// Calculate quality rating (0-100 based on how good the rolls were)
@@ -541,6 +559,80 @@ public class CraftingHandler
};
}
/// <summary>
/// Roll item stats using the item definition's min/max ranges.
/// Higher skill bonus = higher chance for max values.
/// </summary>
private List<(string Stat, int Value)> RollItemStats(
DungeonItemDefinition item,
float skillBonus,
Random rng,
out int totalRolled,
out int maxPossibleTotal)
{
var results = new List<(string Stat, int Value)>();
int rolled = 0;
int maxTotal = 0;
// Helper to roll a stat
void AddRoll(string statName, int min, int max)
{
if (max > min) // Only roll if there's a range
{
int value = RollWithSkillBonus(min, max, skillBonus, rng);
results.Add((statName, value));
rolled += value;
maxTotal += max;
}
else if (max > 0) // Fixed value
{
results.Add((statName, max));
rolled += max;
maxTotal += max;
}
}
// Stat bonuses
AddRoll("BonusStrength", item.MinBonusStrength, item.MaxBonusStrength);
AddRoll("BonusIntelligence", item.MinBonusIntelligence, item.MaxBonusIntelligence);
AddRoll("BonusWisdom", item.MinBonusWisdom, item.MaxBonusWisdom);
AddRoll("BonusEndurance", item.MinBonusEndurance, item.MaxBonusEndurance);
AddRoll("BonusDexterity", item.MinBonusDexterity, item.MaxBonusDexterity);
AddRoll("BonusHealth", item.MinBonusHealth, item.MaxBonusHealth);
AddRoll("BonusMana", item.MinBonusMana, item.MaxBonusMana);
// Weapon damage
AddRoll("MinDamage", item.MinMinDamage, item.MaxMinDamage);
AddRoll("MaxDamage", item.MinMaxDamage, item.MaxMaxDamage);
// Armor
AddRoll("Armor", item.MinArmor, item.MaxArmor);
totalRolled = rolled;
maxPossibleTotal = maxTotal;
return results;
}
/// <summary>
/// Roll a value between min and max, biased toward max based on skill bonus.
/// skillBonus of 0.5 = 50% chance to get max value
/// skillBonus of 1.0 = always get max value
/// </summary>
private int RollWithSkillBonus(int min, int max, float skillBonus, Random rng)
{
if (min >= max) return max;
// Roll to see if we get max
if (rng.NextDouble() < skillBonus)
{
return max;
}
// Otherwise roll normally
return rng.Next(min, max + 1);
}
private void ApplyStatRoll(DungeonCraftedItem item, string stat, int value)
{
switch (stat)