Randomize stats implemented with a correct integer distribution (equal probability for all numbers).

This commit is contained in:
sigonasr2 2023-12-31 15:47:18 -06:00
parent c2d9178de3
commit 73b72a8838
4 changed files with 15 additions and 7 deletions

View File

@ -216,6 +216,7 @@ bool Crawler::OnUserCreate(){
Inventory::AddItem("Wooden Sword"); Inventory::AddItem("Wooden Sword");
Inventory::AddItem("Laser Sword"); Inventory::AddItem("Laser Sword");
Inventory::AddItem("Shell Sword"); Inventory::AddItem("Shell Sword");
Inventory::AddItem("Ring of the Slime King");
LoadLevel(LEVEL_NAMES["starting_map"_S]); LoadLevel(LEVEL_NAMES["starting_map"_S]);
ChangePlayerClass(WARRIOR); ChangePlayerClass(WARRIOR);

View File

@ -380,16 +380,19 @@ Item::Item(uint32_t amt,IT item,uint8_t enhancementLevel)
void Inventory::AddItem(IT it,uint32_t amt,bool monsterDrop){ void Inventory::AddItem(IT it,uint32_t amt,bool monsterDrop){
if(!ITEM_DATA.count(it))ERR("Item "<<std::quoted(it)<<" does not exist in Item Database!"); if(!ITEM_DATA.count(it))ERR("Item "<<std::quoted(it)<<" does not exist in Item Database!");
std::shared_ptr<Item>newItem=(*_inventory.insert({it,std::make_shared<Item>(amt,it)})).second;
newItem->RandomizeStats();
if(ITEM_DATA[it].IsEquippable()){ //Do not stack equips! if(ITEM_DATA[it].IsEquippable()){ //Do not stack equips!
for(uint32_t i=0;i<amt;i++){ for(uint32_t i=0;i<amt;i++){
InsertIntoSortedInv((*_inventory.insert({it,std::make_shared<Item>(amt,it)})).second); InsertIntoSortedInv(newItem);
} }
goto SkipAddingStackableItem; goto SkipAddingStackableItem;
} }
else else
//There are two places to manipulate items in (Both the sorted inventory and the actual inventory) //There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
if(!_inventory.count(it)){ if(!_inventory.count(it)){
InsertIntoSortedInv((*_inventory.insert({it,std::make_shared<Item>(amt,it)})).second); InsertIntoSortedInv(newItem);
}else{ }else{
auto inventory=_inventory.equal_range(it); auto inventory=_inventory.equal_range(it);
if(std::accumulate(inventory.first,inventory.second,0, if(std::accumulate(inventory.first,inventory.second,0,
@ -1050,8 +1053,8 @@ const uint8_t EnhancementInfo::AvailableChapter()const{
const Stats&Item::RandomStats()const{ const Stats&Item::RandomStats()const{
return randomizedStats; return randomizedStats;
}; };
void Item::SetRandomStat(const ItemAttribute attr,const float val){ void Item::RandomizeStats(){
randomizedStats.A(attr)=val; randomizedStats=it->RandomizeStats();
}; };
const Stats ItemInfo::GetMinStats()const{ const Stats ItemInfo::GetMinStats()const{
@ -1064,7 +1067,11 @@ Stats ItemInfo::RandomizeStats(){
Stats randomRolls; Stats randomRolls;
for(auto&[attr,minVal]:minStats){ for(auto&[attr,minVal]:minStats){
float diff=maxStats.A(attr)-minVal; float diff=maxStats.A(attr)-minVal;
randomRolls.A(attr)=util::random(diff)+minVal; if(attr.ShowAsDecimal()){
randomRolls.A(attr)=util::random(diff)+minVal;
}else{
randomRolls.A(attr)=floor(util::random(diff+1)+minVal); //We must add 1 because we are going to floor the value to prevent the edges from having half as likely probability as all other numbers.
}
} }
return randomRolls; return randomRolls;
} }

View File

@ -202,7 +202,7 @@ public:
const bool CanBeSold()const; const bool CanBeSold()const;
const bool CanBePurchased()const; const bool CanBePurchased()const;
const Stats&RandomStats()const; const Stats&RandomStats()const;
void SetRandomStat(const ItemAttribute attr,const float val); void RandomizeStats();
const EnhancementInfo&GetEnhancementInfo()const; const EnhancementInfo&GetEnhancementInfo()const;
//Use ISBLANK macro instead!! This should not be called directly!! //Use ISBLANK macro instead!! This should not be called directly!!
static bool IsBlank(std::shared_ptr<Item>item); static bool IsBlank(std::shared_ptr<Item>item);

View File

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 1 #define VERSION_PATCH 1
#define VERSION_BUILD 5119 #define VERSION_BUILD 5122
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a