yuanshen/Yuanshen.Gameserver/Game/DropTable.cs
wisedev 61cc2055cf First working version of the server
KCP gameserver with packet dispatch, scene streaming, combat, gacha and the
shop, plus a small HTTP server for the login flow and the wish history page.

Game data under assets is pulled out of the client instead of being typed by
hand. The excel tables come from the packed bin container and the spawn points
from the compiled scene scripts.
2026-09-04 02:06:53 +03:00

150 lines
4.9 KiB
C#

using System.Text.Json;
namespace Yuanshen.Gameserver.Game;
internal readonly record struct DropEntry(uint ItemId, uint MinCount, uint MaxCount, double Chance, int Rolls, int Pool);
internal sealed class DropTable
{
private readonly Dictionary<uint, Dictionary<uint, uint>> _monsters = new();
private readonly Dictionary<uint, List<DropEntry>> _drops = new();
private readonly Dictionary<uint, string> _itemTypes = new();
public DropTable()
{
Load("monster_drops.json");
}
public bool IsEmpty => _drops.Count == 0;
public int MonsterCount => _monsters.Count;
public string TypeOf(uint itemId) => _itemTypes.GetValueOrDefault(itemId, "material");
public uint DropIdFor(uint monsterId, uint level)
{
if (!_monsters.TryGetValue(monsterId, out Dictionary<uint, uint>? byLevel) || byLevel.Count == 0)
return 0;
if (byLevel.TryGetValue(level, out uint exact))
return exact;
uint best = 0;
uint bestLevel = 0;
foreach ((uint candidate, uint dropId) in byLevel)
{
if (candidate <= level && candidate >= bestLevel)
{
bestLevel = candidate;
best = dropId;
}
}
return best != 0 ? best : byLevel.OrderBy(pair => pair.Key).First().Value;
}
public Dictionary<uint, uint> Roll(uint dropId, Random random)
{
Dictionary<uint, uint> result = new();
if (!_drops.TryGetValue(dropId, out List<DropEntry>? entries))
return result;
foreach (DropEntry entry in entries.Where(entry => entry.Pool == 0))
{
for (int attempt = 0; attempt < entry.Rolls; attempt++)
{
if (random.NextDouble() < entry.Chance)
Add(result, entry, random);
}
}
foreach (IGrouping<int, DropEntry> group in entries.Where(entry => entry.Pool != 0).GroupBy(entry => entry.Pool))
{
List<DropEntry> members = group.ToList();
for (int attempt = 0; attempt < members[0].Rolls; attempt++)
{
double roll = random.NextDouble();
double accumulated = 0;
foreach (DropEntry member in members)
{
accumulated += member.Chance;
if (roll >= accumulated)
continue;
Add(result, member, random);
break;
}
}
}
return result;
}
private static void Add(Dictionary<uint, uint> result, DropEntry entry, Random random)
{
uint count = entry.MaxCount > entry.MinCount
? (uint)random.Next((int)entry.MinCount, (int)entry.MaxCount + 1)
: entry.MinCount;
if (count != 0)
result[entry.ItemId] = result.GetValueOrDefault(entry.ItemId) + count;
}
private void Load(string fileName)
{
string path = Path.Combine(AppContext.BaseDirectory, "assets", "drops", fileName);
if (!File.Exists(path))
return;
using JsonDocument document = JsonDocument.Parse(File.ReadAllText(path));
JsonElement root = document.RootElement;
foreach (JsonProperty monster in root.GetProperty("monsters").EnumerateObject())
{
if (!uint.TryParse(monster.Name, out uint monsterId))
continue;
Dictionary<uint, uint> byLevel = new();
foreach (JsonProperty level in monster.Value.EnumerateObject())
{
if (uint.TryParse(level.Name, out uint parsedLevel))
byLevel[parsedLevel] = level.Value.GetUInt32();
}
if (byLevel.Count != 0)
_monsters[monsterId] = byLevel;
}
foreach (JsonProperty drop in root.GetProperty("drops").EnumerateObject())
{
if (!uint.TryParse(drop.Name, out uint dropId))
continue;
List<DropEntry> entries = new();
foreach (JsonElement entry in drop.Value.EnumerateArray())
{
entries.Add(new DropEntry(
entry.GetProperty("itemId").GetUInt32(),
entry.GetProperty("minCount").GetUInt32(),
entry.GetProperty("maxCount").GetUInt32(),
entry.GetProperty("chance").GetDouble(),
entry.TryGetProperty("rolls", out JsonElement rolls) ? Math.Max(rolls.GetInt32(), 1) : 1,
entry.TryGetProperty("pool", out JsonElement pool) ? pool.GetInt32() : 0));
}
_drops[dropId] = entries;
}
foreach (JsonProperty item in root.GetProperty("items").EnumerateObject())
{
if (uint.TryParse(item.Name, out uint itemId))
_itemTypes[itemId] = item.Value.GetProperty("type").GetString() ?? "material";
}
}
}