diff --git a/Game/Dungeon/CraftingHandler.cs b/Game/Dungeon/CraftingHandler.cs
index b747dc9..a95d78e 100644
--- a/Game/Dungeon/CraftingHandler.cs
+++ b/Game/Dungeon/CraftingHandler.cs
@@ -179,6 +179,63 @@ public class CraftingHandler
return (recipe, false, null);
}
+ ///
+ /// Consume materials on failed discovery attempt (65% chance to lose materials)
+ ///
+ public async Task<(bool MaterialsLost, List LostItems)> TryConsumeMaterialsOnFailedDiscoveryAsync(
+ GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients)
+ {
+ const float MATERIAL_LOSS_CHANCE = 0.65f;
+ var rng = new Random();
+
+ if (rng.NextDouble() >= MATERIAL_LOSS_CHANCE)
+ {
+ // Lucky! Materials saved
+ return (false, new List());
+ }
+
+ // Load inventory
+ var inventory = await db.DungeonCharacterItems
+ .Where(i => i.CharacterId == characterId && !i.IsEquipped)
+ .ToListAsync();
+
+ var lostItems = new List();
+
+ foreach (var (itemDefId, qty) in ingredients)
+ {
+ int remaining = qty;
+ var stacks = inventory.Where(i => i.ItemDefinitionId == itemDefId).ToList();
+
+ foreach (var stack in stacks)
+ {
+ if (remaining <= 0) break;
+
+ int toRemove = Math.Min(remaining, stack.Quantity);
+ stack.Quantity -= toRemove;
+ remaining -= toRemove;
+
+ if (stack.Quantity <= 0)
+ {
+ db.DungeonCharacterItems.Remove(stack);
+ }
+ }
+
+ // Get item name for message
+ var itemDef = await db.DungeonItemDefinitions.FindAsync(itemDefId);
+ if (itemDef != null)
+ {
+ lostItems.Add($"{qty}x {itemDef.Name}");
+ }
+ }
+
+ await db.SaveChangesAsync();
+
+ _log.LogInformation("Character {CharId} lost materials on failed discovery: {Items}",
+ characterId, string.Join(", ", lostItems));
+
+ return (true, lostItems);
+ }
+
///
/// Craft an item from a known recipe
///
diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs
index 93b7b47..ecc8374 100644
--- a/Game/Dungeon/DungeonHandler.cs
+++ b/Game/Dungeon/DungeonHandler.cs
@@ -2972,9 +2972,25 @@ public sealed class DungeonHandler : IClientMessageHandler
if (recipe == null)
{
+ // Failed discovery - 65% chance to lose materials
+ var (materialsLost, lostItems) = await _craftingHandler.TryConsumeMaterialsOnFailedDiscoveryAsync(
+ db, session.CharacterId, ingredients);
+
+ var errorMsg = error ?? "No recipe found for these ingredients";
+ if (materialsLost)
+ {
+ errorMsg += $"\nMaterials lost: {string.Join(", ", lostItems)}";
+ }
+ else
+ {
+ errorMsg += "\nMaterials saved!";
+ }
+
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
success = false,
- error = error ?? "No recipe found for these ingredients"
+ error = errorMsg,
+ materialsLost = materialsLost,
+ lostItems = lostItems
}, ct);
}
else if (alreadyKnown)