Skip to content

Commit

Permalink
Merge pull request #47 from 133794m3r/quick-fix
Browse files Browse the repository at this point in the history
BUG FIXED! WINDOWS IS WEIRD AGAIN!

Not waiting needs to be done.
  • Loading branch information
133794m3r authored May 1, 2021
2 parents 2b882ba + 07e5eec commit ef0c791
Show file tree
Hide file tree
Showing 29 changed files with 426 additions and 199 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ add_executable(save_demo
demos/load_save_demo.cpp
)

add_executable(save_test
add_executable(test_save
$<TARGET_OBJECTS:includes_lib>
$<TARGET_OBJECTS:items_lib>
$<TARGET_OBJECTS:inventory_lib>
Expand Down Expand Up @@ -239,6 +239,11 @@ if(MSVC)
#shop Module
add_test(NAME "Shop_Module"
COMMAND test_shop.exe)

#save system
add_test(NAME "Shop_Module"
COMMAND test_save)

else()
#Test the battle system
add_test(NAME "Battle_Module"
Expand All @@ -255,4 +260,8 @@ else()

add_test(NAME "Shop_Module"
COMMAND ./test_shop)

add_test(NAME "Save_System"
COMMAND ./test_save)

endif()
16 changes: 10 additions & 6 deletions classes/containers/inventory.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Inventory {
//The list of items contained in this object.
std::unordered_map<unsigned short, Item*> items;
//the number of items that are contained.
std::unordered_map<unsigned char, unsigned int> item_quantity;
std::unordered_map<unsigned short, unsigned char> item_quantity;
//the indexes of the items as they are held in memory
std::deque<unsigned short> item_indexes;
unsigned short num_items;
Expand Down Expand Up @@ -76,13 +76,17 @@ class Inventory {
* @param item the item to add
* @param number The number to add
*/
void add_item(Item &item, unsigned short number=1){
void add_item(Item &item, unsigned char number=1){
//if it already exists
if(this->items.count(item.get_id()) != 0){
//add to it that number
this->item_quantity[item.get_id()]+=number;
}
else {
//shouldn't be able to have more than 255 items. type is short due to std::cout issues.
if(this->num_items > 255)
return;

//otherwise add an item to the hashtables/such.
this->items[item.get_id()] = &item;
this->item_quantity[item.get_id()] = number;
Expand Down Expand Up @@ -182,17 +186,17 @@ class Inventory {
/**
* Get an item based upon an index
* @param idx
* @return
* @return get the id of the item in that index
*/
unsigned short get_item_id(unsigned short idx) const{
return this->item_indexes[idx];
}

/**
* Get an item based upon it's id
* Get an item's quantity based upon it's id
*
* @param item_id
* @return
* @return the amount of the item they have
*/
unsigned int get_quantity(unsigned short item_id) const{
return this->item_quantity.at(item_id);
Expand Down Expand Up @@ -226,7 +230,7 @@ class Inventory {
ss << "Inventory [";
unsigned int i=0;
for(auto el: this->items){
ss << "{" << el.second->get_name() << ", " << this->item_quantity.at(el.first) << "}";
ss << "{" << el.second->get_name() << ", " << std::to_string(this->item_quantity.at(el.first)) << "}";
if(++i < this->num_items){
ss << ", ";
}
Expand Down
27 changes: 12 additions & 15 deletions classes/containers/loot_table.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@

class LootTable:public Inventory {
private:
std::vector<unsigned int> chances;
std::vector<double> chances;
public:
explicit LootTable(std::vector<Item*> items={}, std::vector<unsigned char> counts={},const std::vector<double>& probs = {})
:Inventory(std::move(items),std::move(counts)){
this->chances.reserve(probs.size());
for(double prob:probs){
this->chances.push_back(std::lround(prob*4294967295));
}
explicit LootTable(const std::vector<Item*>& items={}, std::vector<unsigned char> counts={},const std::vector<double>& probs = {})
:Inventory(items,std::move(counts)){
this->chances = probs;
}

std::vector <std::pair<Item*,unsigned int>> award_items(){
std::vector <std::pair<Item*,unsigned short>> award_items(){
//create a temporary vector
std::vector<std::pair<Item *,unsigned int>> rewards;
unsigned int res = 0;
unsigned short item_id = 0;
std::vector<std::pair<Item *,unsigned short>> rewards;
double res;
unsigned short item_id;
//see which items we'll give them
for(unsigned int i=0;i<this->item_indexes.size();i++){
res = xorshift128(0,4294967295);
res = xorshift128(0.00,1.00);
//if it's less than the probability we award it.
if(res <= this->chances[i]){
//first have to get the item's id.
item_id = this->item_indexes[i];
//then we add a std::pair to the vector
rewards.push_back(std::make_pair(this->items[item_id],this->item_quantity[item_id]));
rewards.emplace_back(this->items[item_id],this->item_quantity[item_id]);
}
}
return rewards;
}

void add_item(Item &item,unsigned int quantity,double probability){
this->chances.push_back(std::lround(probability*4294967295));
void add_item(Item &item,unsigned short quantity,double probability){
this->chances.push_back(probability);
Inventory::add_item(item,quantity);
}
};
Expand Down
1 change: 1 addition & 0 deletions classes/containers/shop_inventory.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ShopInventory: public Inventory {
const std::vector<unsigned char> &quantity = {}):Inventory(items,quantity){

}

/**
* The cost to buy amount of the items.
* @param item_id The item we're going to be using.
Expand Down
24 changes: 15 additions & 9 deletions classes/fighters/actor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Actor {
unsigned int base_str_;
unsigned int base_def_;
//then we store the actual values. Since they may be debuffed or if they're damaged. This way we can have stats reset.
unsigned int hp_;
unsigned int str_;
unsigned int def_;
unsigned int hp_{};
unsigned int str_{};
unsigned int def_{};
//level can never be <0 so unsigned value.
unsigned short lvl_ = 0;

Expand All @@ -42,6 +42,7 @@ class Actor {
Armor *armor_equipped;
//the constructors, public methods, and getters.
public:

explicit Actor(std::string name="Actor", unsigned short level=1, double bonus_hp = 0.0,
double bonus_str = 0.0, double bonus_def = 0.0,unsigned int hp = 15,
unsigned int str = 5, unsigned int def = 3,unsigned short type=0)
Expand All @@ -52,6 +53,9 @@ class Actor {
this->base_hp_ = hp;
this->base_str_ = str;
this->base_def_ = def;
this->hp_ = hp;
this->str_ = str;
this->def_ = def;
//these are used when leveling up to give a bonus upon that levelup beyond just their level.
this->bonus_hp_ = bonus_hp;
this->bonus_str_ = bonus_str;
Expand Down Expand Up @@ -159,7 +163,7 @@ class Actor {
* @param the target actor to damage. Uses this object's strength.
* @return int the amount of damage carried out.
*/
unsigned int attack(Actor &target){
unsigned int attack(Actor &target) const{
return target.damage(this->base_str_);
}

Expand All @@ -169,7 +173,7 @@ class Actor {
*/
virtual void set_level(unsigned short level){

int dif = 0;
int dif;
//if it's the same just do nothing.
if(level == this->lvl_)
dif = this->lvl_ - 1;
Expand All @@ -188,7 +192,7 @@ class Actor {
}

//Check if the actor is still alive. If HP is 0 then it means they're dead.
bool is_alive(){
bool is_alive() const{
return this->hp_ > 0;
}

Expand All @@ -200,6 +204,7 @@ class Actor {
// }

void equip_weapon(Weapon &weapon){

this->weapon_held = &weapon;
this->str_ += weapon.get_damage();
}
Expand All @@ -219,14 +224,15 @@ class Actor {
this->def_ += armor.get_defense();
}

Item *equipped_weapon(){
Item *equipped_weapon() const{
return this->weapon_held;
}

Item *equipped_armor(){
Item *equipped_armor() const{
return this->armor_equipped;
}
virtual operator std::string() const{

virtual explicit operator std::string() const{
std::stringstream ss;
ss << "id: " << this->id << " " << this->name_ << " hp:" <<this->hp_ << "/" << this->base_hp_ << " str:" << this->str_ << "/" << this->base_str_ << " def:" << this->def_ << "/" << this->base_def_;
return ss.str();
Expand Down
49 changes: 25 additions & 24 deletions classes/fighters/mob.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
struct MobRewards{
unsigned int gold = 0;
unsigned int xp = 0;
std::vector<std::pair<Item *,unsigned int>> items;
std::vector<std::pair<Item *,unsigned short>> items;
};

#include "actor.hxx"
#include "../containers/loot_table.hxx"
class Mob : public Actor {
private:
//the xp to be awarded and gold be awarded upon death.
unsigned long xp_;
unsigned int xp_;
unsigned int gold_;
//the tier strings
const static std::string tier_str[7];
Expand Down Expand Up @@ -48,49 +48,50 @@ class Mob : public Actor {
*/
void set_gold(){
unsigned int dl = this->lvl_ + 1;
double gm = 1.9;
double gm = 1.90;
double glm;
double bvm;
if(dl < 10){
gm = 5.9;
glm = 1.9;
bvm = 6.8;
gm = 5.90;
glm = 1.90;
bvm = 6.80;
}
else if(dl < 40){
gm = 6.0;
gm = 6.00;
glm = 1.85;
bvm = 7.0;
bvm = 7.00;
}
else if(dl < 91){
gm = 1.95;
glm = 2.0;
bvm = 50;
bvm = 50.00;
}
else if(dl < 151){
glm = 2.2;
bvm = 70.0;
glm = 2.20;
bvm = 70.00;
}
else if(dl < 199){
glm = 2.4;
bvm = 100.0;
glm = 2.40;
bvm = 100.00;
}
else{
glm = 4.0;
bvm = 120.0;
glm = 4.00;
bvm = 120.00;
}
this->gold_ = static_cast<unsigned int>((((this->xp_*glm)-((dl / gm) * bvm))) * 0.5);
this->gold_ = static_cast<unsigned int>((this->xp_ * glm - ((dl / gm) * bvm)) * 0.50);
}
public:

//initialize the Mob class. Explicit since it can be called with just 1 parameter. Also initialize properties with parent class' constructor.
explicit Mob(std::string name="Mob",unsigned short tier=1, unsigned short level=1,
double bonus_hp=0.00, double bonus_str=0.0, double bonus_def=0.0)
:Actor(std::move(name),level,bonus_hp+((tier-1.0)/20),
bonus_str+((tier-1.0)/100),bonus_def+((tier-1.0)/80),16,6,3,1) {
:Actor(std::move(name),level,bonus_hp+((tier-1.0)/20.00),
bonus_str+((tier-1.0)/100.00),bonus_def+((tier-1.0)/80.00),16,6,3,1) {
unsigned int tmp = this->lvl_ + 1;
//based on other formulas this should make the curve OK.
//tier will modify the two formulas below eventually
this->xp_ = std::lround( tmp* ((tmp*0.79 )+1.2));
this->xp_ = std::lround((tmp * (tmp * 0.79) *(1+ (tier - 1.00 / 3.25))+ 2.00));
this->gold_ = 0;
this->set_gold();
this->tier_ = tier;
this->set_level(level);
Expand All @@ -111,11 +112,11 @@ class Mob : public Actor {
dif = level - this->lvl_;
else
dif = this->lvl_ - level;
dif += (this->tier_-1)/6;
dif += (this->tier_-1.00)/6.00;
//when they modify the level change the stats to the proper values.
this->base_hp_ += std::lround( (this->bonus_hp_+1.0)*13.1*modifier*dif)+(this->tier_-1);
this->base_str_ += std::lround( ((this->bonus_str_+1.0)*4.0*modifier*dif)+((this->tier_)*2));
this->base_def_ += std::lround( ((this->bonus_def_+1.0)*3.125*modifier*dif)+((this->tier_-1)/3));
this->base_hp_ += std::lround( (this->bonus_hp_+1.0)*13.1*modifier*dif)+(this->tier_-1.00);
this->base_str_ += std::lround( ((this->bonus_str_+1.0)*4.0*modifier*dif)+((this->tier_)*2.00));
this->base_def_ += std::lround( ((this->bonus_def_+1.0)*3.125*modifier*dif)+((this->tier_-1.00)/3.00));
//then set the current stats from the base.
this->hp_ = this->base_hp_;
this->str_ = this->base_str_;
Expand Down Expand Up @@ -171,7 +172,7 @@ class Mob : public Actor {
*
* @return The object stringified.
*/
operator std::string(){
explicit operator std::string(){
std::stringstream ss;
ss << "id: " << this->id << " " << this->name_ << " hp:" <<this->hp_ << "/" << this->base_hp_ <<
" str:" << this->str_ << "/" << this->base_str_ << " def:" << this->def_ << "/"
Expand Down
Loading

0 comments on commit ef0c791

Please sign in to comment.