Files
GladiatorIdle/Game/Items.cs

187 lines
8.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace GladiatorIdle
{
public enum ItemSlot { Weapon, Shield, Helmet, Armor, Greaves }
public enum Rarity { Common, Good, Rare, Epic, Legendary }
public enum Affix { Damage, Health, Crit, Agility, Gold, Xp, Crowd }
/// <summary>
/// Grundtyp eines Gegenstands: Slot, Name-Key, Bild und eine kleine eigene Note
/// (z. B. Sica = mehr Krit, Dreizack = mehr Geschick).
/// </summary>
internal sealed record ItemBase(string Id, ItemSlot Slot, double DmgK = 0, double HpK = 0, double CritAdd = 0, double AgiAdd = 0)
{
public string Key => "item." + Id;
public string Icon => "item_" + Id;
public static readonly ItemBase[] All =
{
// Waffen: Schaden
new("gladius", ItemSlot.Weapon, DmgK: 0.80),
new("spatha", ItemSlot.Weapon, DmgK: 0.86, AgiAdd: -0.5),
new("sica", ItemSlot.Weapon, DmgK: 0.76, CritAdd: 0.04),
new("trident", ItemSlot.Weapon, DmgK: 0.76, AgiAdd: 1.0),
// Schilde: Abwehr (Stufe = Block)
new("parma", ItemSlot.Shield, AgiAdd: 0.5),
new("scutum", ItemSlot.Shield, HpK: 0.08),
new("clipeus", ItemSlot.Shield),
new("manica", ItemSlot.Shield, DmgK: 0.06), // Armschutz statt Schild (Retiarius)
// Helme: Leben
new("galea", ItemSlot.Helmet, HpK: 0.30),
new("crested", ItemSlot.Helmet, HpK: 0.27, CritAdd: 0.01),
new("visored", ItemSlot.Helmet, HpK: 0.33, AgiAdd: -0.3),
new("fishhelm", ItemSlot.Helmet, HpK: 0.30, AgiAdd: 0.2),
// Rüstungen: Leben
new("leather", ItemSlot.Armor, HpK: 0.58, AgiAdd: 0.6),
new("hamata", ItemSlot.Armor, HpK: 0.64),
new("squamata", ItemSlot.Armor, HpK: 0.67, AgiAdd: -0.3),
new("segmentata", ItemSlot.Armor, HpK: 0.70, AgiAdd: -0.6),
// Beinschienen: Geschick und etwas Schaden
new("wraps", ItemSlot.Greaves, DmgK: 0.16, AgiAdd: 1.0),
new("ocreae", ItemSlot.Greaves, DmgK: 0.18, HpK: 0.05),
new("greaves", ItemSlot.Greaves, DmgK: 0.22),
new("sandals", ItemSlot.Greaves, DmgK: 0.12, AgiAdd: 1.4),
};
static readonly Dictionary<string, ItemBase> _byId = All.ToDictionary(b => b.Id);
public static ItemBase Get(string id) => _byId[id];
}
/// Ein Zufallsbonus (eigener Typ statt Tupel: System.Text.Json speichert Tupel-Felder nicht).
internal sealed record AffixRoll(Affix Kind, double Value);
/// <summary>Ein Gegenstand: Grundtyp, Seltenheit, Stufe (verbesserbar beim Schmied), Zufallsboni.</summary>
internal sealed class Item
{
public long Id { get; set; }
public string Base { get; set; } = "gladius";
public Rarity Rarity { get; set; }
public int Level { get; set; }
public List<AffixRoll> Affixes { get; set; } = new();
public ItemBase Def => ItemBase.Get(Base);
public ItemSlot Slot => Def.Slot;
/// Wirksame Stufe in einem Slot mit Verstärkung <paramref name="slotLevel"/> (Schmied):
/// Gegenstandsstufe + Seltenheit + halbe Slot-Verstärkung.
public double EffLevel(int slotLevel) => Level + Items.RarityBonus[(int)Rarity] + Items.SLOT_SHARE * slotLevel;
public double AffixSum(Affix a) => Affixes.Where(x => x.Kind == a).Sum(x => x.Value);
}
/// <summary>Summe aller angelegten Gegenstände (Faktoren und Zuschläge).</summary>
internal readonly record struct GearStats(double DmgMult, double HpMult, double Block, double Agility, double Crit,
double GoldMult, double XpMult, double CrowdMult);
/// <summary>Zahlen und Zufall rund um Gegenstände (Beute, Händler, Schmied).</summary>
internal static class Items
{
public static readonly int[] RarityBonus = { 0, 1, 2, 4, 6 };
public static readonly int[] AffixCount = { 0, 1, 2, 3, 4 };
/// Würfel-Gewichte der Seltenheit: normale Beute, Champion-Beute (mindestens selten).
static readonly int[] DropWeights = { 55, 28, 12, 4, 1 };
static readonly int[] ChampionWeights = { 0, 0, 60, 30, 10 };
public const double DROP_CHANCE = 0.25;
public const int INVENTORY = 30;
public const int MERCHANT_OFFERS = 6;
public const double MERCHANT_REFRESH = 600; // Sekunden
/// Schaden/Leben je wirksamer Stufe (wie früher die Ausrüstungs-Stufen).
public const double LEVEL_GROWTH = 1.17;
/// Gegenstandsstufe, die eine Gegnerstufe fallen lässt (etwas unter dem, was man schmieden kann).
public static int DropLevel(int tier, Random rng) => Math.Max(0, (int)Math.Round(tier * 0.35 + rng.Next(-2, 3)));
/// Anteil der Slot-Verstärkung (Schmied), der zur Stufe jedes Gegenstands im Slot zählt.
public const double SLOT_SHARE = 0.45;
public static GearStats Stats(Item?[] equipped, int[] slotLevels)
{
double dmg = 1, hp = 1, block = 0, agi = 0, crit = 0, gold = 1, xp = 1, crowd = 1;
for (int i = 0; i < equipped.Length; i++)
{
var it = equipped[i];
if (it == null) continue;
var d = it.Def;
double lv = it.EffLevel(slotLevels[i]);
if (d.Slot == ItemSlot.Shield) block += lv;
if (d.DmgK > 0) dmg *= Math.Pow(LEVEL_GROWTH, lv * d.DmgK);
if (d.HpK > 0) hp *= Math.Pow(LEVEL_GROWTH, lv * d.HpK);
agi += d.AgiAdd * (1 + lv * 0.08);
crit += d.CritAdd;
dmg *= 1 + it.AffixSum(Affix.Damage);
hp *= 1 + it.AffixSum(Affix.Health);
crit += it.AffixSum(Affix.Crit);
agi += it.AffixSum(Affix.Agility);
gold += it.AffixSum(Affix.Gold);
xp += it.AffixSum(Affix.Xp);
crowd += it.AffixSum(Affix.Crowd);
}
return new GearStats(dmg, hp, block, agi, crit, gold, xp, crowd);
}
/// Wert eines Zufallsbonus nach Seltenheit (Prozentwerte als Anteil, Geschick als Punkte).
static double AffixValue(Affix a, Rarity r, Random rng)
{
double k = 1 + 0.35 * (int)r, roll = 0.6 + 0.4 * rng.NextDouble();
return a switch
{
Affix.Damage or Affix.Health => 0.04 * k * roll,
Affix.Crit => 0.015 * k * roll,
Affix.Agility => Math.Round(3 * k * roll, 1),
Affix.Gold or Affix.Xp => 0.06 * k * roll,
_ => 0.08 * k * roll
};
}
static Rarity RollRarity(Random rng, int[] weights)
{
int roll = rng.Next(weights.Sum());
for (int i = 0; i < weights.Length; i++)
if ((roll -= weights[i]) < 0) return (Rarity)i;
return Rarity.Common;
}
public static Item Create(Random rng, int level, Rarity rarity, ItemSlot? slot = null, long id = 0)
{
var bases = slot is ItemSlot s ? ItemBase.All.Where(b => b.Slot == s).ToArray() : ItemBase.All;
var item = new Item { Id = id, Base = bases[rng.Next(bases.Length)].Id, Rarity = rarity, Level = level };
var kinds = Enum.GetValues<Affix>().OrderBy(_ => rng.Next()).Take(AffixCount[(int)rarity]);
foreach (var a in kinds) item.Affixes.Add(new AffixRoll(a, AffixValue(a, rarity, rng)));
return item;
}
/// Startausrüstung je Klasse (gewöhnlich, Stufe 0).
public static string[] StartKit(GladiatorClass c) => c switch
{
GladiatorClass.Murmillo => new[] { "gladius", "scutum", "fishhelm" },
GladiatorClass.Retiarius => new[] { "trident", "manica", "wraps" },
GladiatorClass.Thraex => new[] { "sica", "parma", "crested" },
_ => new[] { "gladius", "scutum", "visored" },
};
/// Beute nach einem Sieg (null = nichts gefallen).
public static Item? Drop(Random rng, int tier, bool champion, long id)
{
if (!champion && rng.NextDouble() > DROP_CHANCE) return null;
return Create(rng, DropLevel(tier, rng) + (champion ? 3 : 0), RollRarity(rng, champion ? ChampionWeights : DropWeights), id: id);
}
public static Item MerchantItem(Random rng, int tier, long id) =>
Create(rng, DropLevel(tier, rng) + 1, RollRarity(rng, new[] { 30, 40, 22, 7, 1 }), id: id);
/// Grundwert in Gold (Händler-Preis; Verkauf bringt ein Viertel).
public static double Value(Item it) =>
25 * Math.Pow(1.5, it.EffLevel(0)) * (1 + 0.25 * it.Affixes.Count);
public static double SellValue(Item it) => Value(it) / 4;
/// Schmied: Kosten für eine Verstärkungsstufe eines Slots (wie früher die Ausrüstungs-Stufen).
public static double SlotCost(int level) => 20 * Math.Pow(1.5, level);
}
}