From 06175446fcf58bb086d555903a3f2a12f8fd261a Mon Sep 17 00:00:00 2001 From: captainurist <73941350+captainurist@users.noreply.github.com> Date: Sun, 15 Oct 2023 09:51:39 +0100 Subject: [PATCH] Moved gold generation code into ItemGen --- src/Engine/Graphics/Indoor.cpp | 22 +------------------- src/Engine/Objects/Chest.cpp | 35 +------------------------------- src/Engine/Objects/Items.cpp | 37 ++++++++++++++++++++++++++++++++++ src/Engine/Objects/Items.h | 1 + 4 files changed, 40 insertions(+), 55 deletions(-) diff --git a/src/Engine/Graphics/Indoor.cpp b/src/Engine/Graphics/Indoor.cpp index 5cd59f118753..716f0124a9b8 100644 --- a/src/Engine/Graphics/Indoor.cpp +++ b/src/Engine/Graphics/Indoor.cpp @@ -2024,29 +2024,9 @@ void SpawnRandomTreasure(MapInfo *mapInfo, SpawnPoint *a2) { return; } - if (a2->uItemIndex == ITEM_TREASURE_LEVEL_1) { - a1a.containing_item.uItemID = ITEM_GOLD_SMALL; - v34 = grng->random(51) + 50; - } else if (a2->uItemIndex == ITEM_TREASURE_LEVEL_2) { - a1a.containing_item.uItemID = ITEM_GOLD_SMALL; - v34 = grng->random(101) + 100; - } else if (a2->uItemIndex == ITEM_TREASURE_LEVEL_3) { - a1a.containing_item.uItemID = ITEM_GOLD_MEDIUM; - v34 = grng->random(301) + 200; - } else if (a2->uItemIndex == ITEM_TREASURE_LEVEL_4) { - a1a.containing_item.uItemID = ITEM_GOLD_MEDIUM; - v34 = grng->random(501) + 500; - } else if (a2->uItemIndex == ITEM_TREASURE_LEVEL_5) { - a1a.containing_item.uItemID = ITEM_GOLD_LARGE; - v34 = grng->random(1001) + 1000; - } else if (a2->uItemIndex == ITEM_TREASURE_LEVEL_6) { - a1a.containing_item.uItemID = ITEM_GOLD_LARGE; - v34 = grng->random(3001) + 2000; - } + a1a.containing_item.generateGold(a2->uItemIndex); a1a.uType = pItemTable->pItems[a1a.containing_item.uItemID].uSpriteID; - a1a.containing_item.SetIdentified(); a1a.uObjectDescID = pObjectList->ObjectIDByItemID(a1a.uType); - a1a.containing_item.goldAmount = v34; } else { if (!a1a.containing_item.GenerateArtifact()) return; diff --git a/src/Engine/Objects/Chest.cpp b/src/Engine/Objects/Chest.cpp index 2577b40fdd68..4156aefc0953 100644 --- a/src/Engine/Objects/Chest.cpp +++ b/src/Engine/Objects/Chest.cpp @@ -560,40 +560,7 @@ void GenerateItemsInChest() { if (whatToGenerateProb < 20) { currItem->Reset(); } else if (whatToGenerateProb < 60) { // generate gold - int goldAmount = 0; - currItem->Reset(); - // TODO(captainurist): merge with the other implementation? - switch (resultTreasureLevel) { - case ITEM_TREASURE_LEVEL_1: - goldAmount = grng->random(51) + 50; - currItem->uItemID = ITEM_GOLD_SMALL; - break; - case ITEM_TREASURE_LEVEL_2: - goldAmount = grng->random(101) + 100; - currItem->uItemID = ITEM_GOLD_SMALL; - break; - case ITEM_TREASURE_LEVEL_3: - goldAmount = grng->random(301) + 200; - currItem->uItemID = ITEM_GOLD_MEDIUM; - break; - case ITEM_TREASURE_LEVEL_4: - goldAmount = grng->random(501) + 500; - currItem->uItemID = ITEM_GOLD_MEDIUM; - break; - case ITEM_TREASURE_LEVEL_5: - goldAmount = grng->random(1001) + 1000; - currItem->uItemID = ITEM_GOLD_LARGE; - break; - case ITEM_TREASURE_LEVEL_6: - goldAmount = grng->random(3001) + 2000; - currItem->uItemID = ITEM_GOLD_LARGE; - break; - default: - assert(false); - break; - } - currItem->SetIdentified(); - currItem->goldAmount = goldAmount; + currItem->generateGold(resultTreasureLevel); } else { pItemTable->generateItem(resultTreasureLevel, RANDOM_ITEM_ANY, currItem); } diff --git a/src/Engine/Objects/Items.cpp b/src/Engine/Objects/Items.cpp index 6779ef761b17..058a6e9aeb0b 100644 --- a/src/Engine/Objects/Items.cpp +++ b/src/Engine/Objects/Items.cpp @@ -261,6 +261,43 @@ bool ItemGen::GenerateArtifact() { } } +void ItemGen::generateGold(ItemTreasureLevel treasureLevel) { + assert(isRandomTreasureLevel(treasureLevel)); + + Reset(); + SetIdentified(); + + switch (treasureLevel) { + case ITEM_TREASURE_LEVEL_1: + goldAmount = grng->random(51) + 50; + uItemID = ITEM_GOLD_SMALL; + break; + case ITEM_TREASURE_LEVEL_2: + goldAmount = grng->random(101) + 100; + uItemID = ITEM_GOLD_SMALL; + break; + case ITEM_TREASURE_LEVEL_3: + goldAmount = grng->random(301) + 200; + uItemID = ITEM_GOLD_MEDIUM; + break; + case ITEM_TREASURE_LEVEL_4: + goldAmount = grng->random(501) + 500; + uItemID = ITEM_GOLD_MEDIUM; + break; + case ITEM_TREASURE_LEVEL_5: + goldAmount = grng->random(1001) + 1000; + uItemID = ITEM_GOLD_LARGE; + break; + case ITEM_TREASURE_LEVEL_6: + goldAmount = grng->random(3001) + 2000; + uItemID = ITEM_GOLD_LARGE; + break; + default: + assert(false); + break; + } +} + template static void AddToMap(std::map> &map, ActualKey key, CharacterAttributeType subkey, int bonusValue = 0, CharacterSkillType skill = CHARACTER_SKILL_INVALID) { diff --git a/src/Engine/Objects/Items.h b/src/Engine/Objects/Items.h index a3399928631e..1714388be455 100644 --- a/src/Engine/Objects/Items.h +++ b/src/Engine/Objects/Items.h @@ -51,6 +51,7 @@ struct ItemGen { // 0x24 inline void SetStolen() { uAttributes |= ITEM_STOLEN; } bool GenerateArtifact(); + void generateGold(ItemTreasureLevel treasureLevel); unsigned int GetValue() const; std::string GetDisplayName(); std::string GetIdentifiedName();