From f04ed1acf6d75066a8f5f2f18ccffde1753985f6 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 23 Dec 2025 13:41:07 -0500 Subject: [PATCH] Crafting System v2 --- Game/Dungeon/CraftingHandler.cs | 126 ++++++++++++++++++++++---------- Game/Dungeon/DungeonHandler.cs | 12 ++- 2 files changed, 97 insertions(+), 41 deletions(-) diff --git a/Game/Dungeon/CraftingHandler.cs b/Game/Dungeon/CraftingHandler.cs index 6f22792..b747dc9 100644 --- a/Game/Dungeon/CraftingHandler.cs +++ b/Game/Dungeon/CraftingHandler.cs @@ -135,8 +135,9 @@ public class CraftingHandler /// Try to discover a recipe by combining ingredients /// Returns the recipe if discovered, null otherwise /// - public async Task<(DungeonCraftingRecipe? Recipe, bool AlreadyKnown)> TryDiscoverRecipeAsync( - GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients) + public async Task<(DungeonCraftingRecipe? Recipe, bool AlreadyKnown, string? Error)> TryDiscoverRecipeAsync( + GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients, + CraftingSkillType? skillTypeFilter = null) { // Build hash from ingredients var sortedIngredients = ingredients.OrderBy(i => i.ItemDefinitionId).ThenBy(i => i.Quantity).ToList(); @@ -144,18 +145,24 @@ public class CraftingHandler if (!_ingredientHashToRecipeId.TryGetValue(hash, out var recipeId)) { - return (null, false); + return (null, false, "No recipe found for these ingredients"); } var recipe = _recipes[recipeId]; + // Check skill type filter + if (skillTypeFilter.HasValue && recipe.SkillType != skillTypeFilter.Value) + { + return (null, false, $"This combination creates something in {recipe.SkillType}, not {skillTypeFilter.Value}"); + } + // Check if already known var alreadyKnown = await db.DungeonCharacterKnownRecipes .AnyAsync(kr => kr.CharacterId == characterId && kr.RecipeId == recipeId); if (alreadyKnown) { - return (recipe, true); + return (recipe, true, null); } // Learn the recipe @@ -169,7 +176,7 @@ public class CraftingHandler _log.LogInformation("Character {CharId} discovered recipe: {Recipe}", characterId, recipe.Name); - return (recipe, false); + return (recipe, false, null); } /// @@ -336,46 +343,87 @@ public class CraftingHandler db.DungeonCraftedItems.Add(craftedItem); } - // Find empty inventory slot - var usedSlots = inventory.Where(i => i.CharacterId == characterId && !i.IsEquipped).Select(i => i.SlotIndex).ToHashSet(); - int emptySlot = -1; - for (int i = 0; i < 30; i++) + // Check if we can stack with existing items (only for non-crafted items without stat rolls) + int finalSlotIndex = -1; + bool addedToStack = false; + + if (!recipe.HasStatRolls && outputItemDef != null && outputItemDef.Stackable) { - if (!usedSlots.Contains(i)) + // Find existing stack of same item + var existingStack = inventory.FirstOrDefault(i => + i.ItemDefinitionId == finalOutputItemId && + !i.IsEquipped && + i.Quantity < (outputItemDef.MaxStack > 0 ? outputItemDef.MaxStack : 99)); + + if (existingStack != null) { - emptySlot = i; - break; + int canAdd = (outputItemDef.MaxStack > 0 ? outputItemDef.MaxStack : 99) - existingStack.Quantity; + int toAdd = Math.Min(canAdd, finalOutputQuantity); + existingStack.Quantity += toAdd; + finalSlotIndex = existingStack.SlotIndex; + addedToStack = true; + + // If we couldn't add all, we need a new slot for the rest + if (toAdd < finalOutputQuantity) + { + finalOutputQuantity -= toAdd; + addedToStack = false; // Need to create new item for remainder + } + else + { + finalOutputQuantity = 0; + } } } - if (emptySlot == -1) + // If not stacked (or remainder), find empty slot and create new item + if (!addedToStack || finalOutputQuantity > 0) { - return new CraftingResult { Success = false, ErrorMessage = "Inventory full" }; - } - - // Create the inventory item - var newItem = new DungeonCharacterItem - { - CharacterId = characterId, - ItemDefinitionId = finalOutputItemId, - Quantity = finalOutputQuantity, - SlotIndex = emptySlot, - IsEquipped = false, - AcquiredAt = DateTime.UtcNow - }; - db.DungeonCharacterItems.Add(newItem); - await db.SaveChangesAsync(); - - // Link crafted item if applicable - if (craftedItem != null) - { - db.DungeonCharacterItemCraftedLinks.Add(new DungeonCharacterItemCraftedLink + var usedSlots = inventory.Where(i => i.CharacterId == characterId && !i.IsEquipped).Select(i => i.SlotIndex).ToHashSet(); + int emptySlot = -1; + for (int i = 0; i < 30; i++) { - CharacterItemId = newItem.Id, - CraftedItemId = craftedItem.Id - }); + if (!usedSlots.Contains(i)) + { + emptySlot = i; + break; + } + } + + if (emptySlot == -1 && finalOutputQuantity > 0) + { + return new CraftingResult { Success = false, ErrorMessage = "Inventory full" }; + } + + if (finalOutputQuantity > 0) + { + var newItem = new DungeonCharacterItem + { + CharacterId = characterId, + ItemDefinitionId = finalOutputItemId, + Quantity = finalOutputQuantity, + SlotIndex = emptySlot, + IsEquipped = false, + AcquiredAt = DateTime.UtcNow + }; + db.DungeonCharacterItems.Add(newItem); + finalSlotIndex = emptySlot; + + // Link crafted item if applicable + if (craftedItem != null) + { + await db.SaveChangesAsync(); // Save to get newItem.Id + db.DungeonCharacterItemCraftedLinks.Add(new DungeonCharacterItemCraftedLink + { + CharacterItemId = newItem.Id, + CraftedItemId = craftedItem.Id + }); + } + } } + await db.SaveChangesAsync(); + // Award XP int xpGained = DungeonCharacterCraftingSkill.GetCraftingXp(recipe.Tier, skill.Level); skill.Experience += xpGained; @@ -412,9 +460,10 @@ public class CraftingHandler XpGained = xpGained, NewSkillLevel = skill.Level, LeveledUp = leveledUp, - SlotIndex = emptySlot, + SlotIndex = finalSlotIndex, GotBonusOutput = gotBonusOutput, - OutputQuantity = finalOutputQuantity + OutputQuantity = recipe.OutputQuantity, + AddedToStack = addedToStack }; } @@ -470,4 +519,5 @@ public class CraftingResult public int SlotIndex { get; set; } public bool GotBonusOutput { get; set; } public int OutputQuantity { get; set; } + public bool AddedToStack { get; set; } } diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index a8e3ce5..93b7b47 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -2961,15 +2961,20 @@ public sealed class DungeonHandler : IClientMessageHandler if (request.Discover && request.Ingredients != null && request.Ingredients.Count > 0) { - // Recipe discovery attempt + // Recipe discovery attempt with optional skill type filter var ingredients = request.Ingredients.Select(i => (i.ItemDefinitionId, i.Quantity)).ToList(); - var (recipe, alreadyKnown) = await _craftingHandler.TryDiscoverRecipeAsync(db, session.CharacterId, ingredients); + CraftingSkillType? skillFilter = request.SkillType.HasValue + ? (CraftingSkillType)request.SkillType.Value + : null; + + var (recipe, alreadyKnown, error) = await _craftingHandler.TryDiscoverRecipeAsync( + db, session.CharacterId, ingredients, skillFilter); if (recipe == null) { await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new { success = false, - error = "No recipe found for these ingredients" + error = error ?? "No recipe found for these ingredients" }, ct); } else if (alreadyKnown) @@ -3132,6 +3137,7 @@ public class CraftingAttemptRequest public int RecipeId { get; set; } public List? Ingredients { get; set; } public bool Discover { get; set; } + public int? SkillType { get; set; } } public class CraftingIngredientInput