Skip to content

Commit

Permalink
use ItemFactory::generateRandomItem to generate Griswold's store items
Browse files Browse the repository at this point in the history
  • Loading branch information
wheybags committed Jun 12, 2020
1 parent 88e58d1 commit d0a6fb9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
14 changes: 8 additions & 6 deletions apps/freeablo/faworld/itemfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ namespace FAWorld
return newItem;
}

std::unique_ptr<Item> ItemFactory::generateRandomItem(int32_t itemLevel) const
std::unique_ptr<Item> ItemFactory::generateRandomItem(int32_t itemLevel, ItemGenerationType generationType) const
{
return generateRandomItem(itemLevel, [](const ItemBase&) { return true; });
return generateRandomItem(itemLevel, generationType, [](const ItemBase&) { return true; });
}

std::unique_ptr<Item> ItemFactory::generateRandomItem(int32_t itemLevel, const ItemFilter& filter) const
std::unique_ptr<Item> ItemFactory::generateRandomItem(int32_t itemLevel, ItemGenerationType generationType, const ItemFilter& filter) const
{
if (DebugSettings::itemGenerationType == DebugSettings::ItemGenerationType::AlwaysMagical)
generationType = ItemGenerationType::AlwaysMagical;

const ItemBase* itemBase = randomItemBase([&](const ItemBase& base) {
bool ok = filter(base) && base.mQualityLevel <= itemLevel;

if (DebugSettings::itemGenerationType != DebugSettings::ItemGenerationType::Normal)
if (generationType != ItemGenerationType::Normal)
ok = ok && base.getEquipType() != ItemEquipType::none;

return ok;
Expand All @@ -41,8 +44,7 @@ namespace FAWorld

if (EquipmentItem* equipmentItem = item->getAsEquipmentItem())
{
bool magical = DebugSettings::itemGenerationType == DebugSettings::ItemGenerationType::AlwaysMagical || mRng.randomInRange(0, 99) <= 10 ||
mRng.randomInRange(0, 99) <= itemLevel;
bool magical = generationType == ItemGenerationType::AlwaysMagical || mRng.randomInRange(0, 99) <= 10 || mRng.randomInRange(0, 99) <= itemLevel;

if (magical)
{
Expand Down
11 changes: 9 additions & 2 deletions apps/freeablo/faworld/itemfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ namespace FAWorld
explicit ItemFactory(const DiabloExe::DiabloExe& exe, Random::Rng& rng);

std::unique_ptr<Item> generateBaseItem(const std::string& id) const;
std::unique_ptr<Item> generateRandomItem(int32_t itemLevel) const;
std::unique_ptr<Item> generateRandomItem(int32_t itemLevel, const ItemFilter& filter) const;

enum class ItemGenerationType
{
Normal,
OnlyBaseItems,
AlwaysMagical,
};
std::unique_ptr<Item> generateRandomItem(int32_t itemLevel, ItemGenerationType generationType) const;
std::unique_ptr<Item> generateRandomItem(int32_t itemLevel, ItemGenerationType generationType, const ItemFilter& filter) const;

const ItemBase* randomItemBase(const ItemFilter& filter) const;
const ItemPrefixOrSuffixBase* randomPrefixOrSuffixBase(const ItemPrefixOrSuffixFilter& filter) const;
Expand Down
2 changes: 1 addition & 1 deletion apps/freeablo/faworld/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace FAWorld
}
else
{
item = mWorld.getItemFactory().generateRandomItem(mStats.mLevel);
item = mWorld.getItemFactory().generateRandomItem(mStats.mLevel, ItemFactory::ItemGenerationType::Normal);
}

getLevel()->dropItemClosestEmptyTile(item, *this, getPos().current(), Misc::Direction(Misc::Direction8::none));
Expand Down
9 changes: 4 additions & 5 deletions apps/freeablo/faworld/storedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ namespace FAWorld
{
StoreData::StoreData(const ItemFactory& itemFactory) : mItemFactory(itemFactory) {}

void StoreData::regenerateGriswoldBasicItems(int32_t ilvl, Random::Rng& rng)
void StoreData::generateGriswoldBasicItems(int32_t itemLevel, Random::Rng& rng)
{
int32_t count = rng.randomInRange(10, 20);
griswoldBasicItems.resize(count);
for (auto& item : griswoldBasicItems)
for (StoreItem& item : griswoldBasicItems)
{
const ItemBase* itemBase = mItemFactory.randomItemBase([&](const ItemBase& base) {
item.item = mItemFactory.generateRandomItem(itemLevel, ItemFactory::ItemGenerationType::OnlyBaseItems, [&](const ItemBase& base) {
static const auto excludedTypes = {ItemType::misc, ItemType::gold, ItemType::staff, ItemType::ring, ItemType::amulet};
return base.mQualityLevel <= ilvl && std::count(excludedTypes.begin(), excludedTypes.end(), base.mType) == 0;
return std::count(excludedTypes.begin(), excludedTypes.end(), base.mType) == 0;
});

item.item = itemBase->createItem();
item.item->init();
item.storeId = mNextItemId;
mNextItemId++;
Expand Down
4 changes: 2 additions & 2 deletions apps/freeablo/faworld/storedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace FAWorld
uint32_t storeId = 0;
};

/// class for storing and regenerating items sold in various stores
/// class for storing items sold in various stores
class StoreData
{
public:
Expand All @@ -33,7 +33,7 @@ namespace FAWorld
void save(FASaveGame::GameSaver& saver) const;
void load(FASaveGame::GameLoader& loader);

void regenerateGriswoldBasicItems(int32_t base, Random::Rng& rng);
void generateGriswoldBasicItems(int32_t itemLevel, Random::Rng& rng);

public:
std::vector<StoreItem> griswoldBasicItems;
Expand Down
4 changes: 2 additions & 2 deletions apps/freeablo/faworld/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace FAWorld
this->setupObjectIdMappers();

if (mDiabloExe.isLoaded())
regenerateStoreItems();
generateStoreItems();
}

void World::regenerateStoreItems() { mStoreData->regenerateGriswoldBasicItems(10 /*placeholder*/, *mRng.get()); }
void World::generateStoreItems() { mStoreData->generateGriswoldBasicItems(10 /*placeholder*/, *mRng.get()); }

void World::load(FASaveGame::GameLoader& loader)
{
Expand Down
2 changes: 1 addition & 1 deletion apps/freeablo/faworld/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace FAWorld
void setLevel(int32_t levelNum, bool upStairsPos = true);
GameLevel* getLevel(size_t level);
void insertLevel(size_t level, GameLevel* gameLevel);
void regenerateStoreItems();
void generateStoreItems();

Actor* getActorAt(const Misc::Point& point);

Expand Down

0 comments on commit d0a6fb9

Please sign in to comment.