Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
ItemReader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <map>
5 #include <optional>
6 #include <string>
7 
8 struct ItemStats
9 {
10  ItemStats(int acc = 0, int dam = 0, int aspd = 0, int rng = 0, int arm = 0, int dod = 0) : accuracy(acc), damage(dam), attackSpeed(aspd), range(rng), armor(arm), dodge(dod){};
11 
12  int accuracy; // accuracy bonus
13 
14  int damage; // damage bonus
15 
16  int attackSpeed; // attack speed in milliseconds, non-zero only for weapons
17 
18  int range; // range in tiles, non-zero only for weapons
19 
20  int armor; // armor bonus
21 
22  int dodge; // dodge change bonus
23 
25  {
26  return ItemStats(accuracy + other.accuracy, damage + other.damage, std::max(attackSpeed, other.attackSpeed), std::max(range, other.range), armor + other.armor, dodge + other.dodge);
27  }
28 
29  friend std::ostream& operator<<(std::ostream& os, const ItemStats& stats);
30 };
31 
32 inline std::ostream& operator<<(std::ostream& os, const ItemStats& stats)
33 {
34  os << "ItemStats: accuracy = " << stats.accuracy << ", damage = " << stats.damage << ", attackSpeed = " << stats.attackSpeed << ", range = " << stats.range << ", armor = " << stats.armor << ", dodge = " << stats.dodge << std::endl;
35  return os;
36 };
37 
38 enum class Slot
39 {
40  HEAD,
41  NECK,
42  TOP,
43  MAIN_HAND,
44  OFF_HAND,
45  BOTTOM,
46  GLOVES,
47  BOOTS,
48  RING,
49 };
50 
52 {
53  int id;
54  std::string name;
55  std::string description;
57  std::string textureFilename;
58  std::string objFilename;
59  std::string iconFilename;
60 };
61 
63 {
67 };
68 
70 {
72 };
73 
74 enum class ItemType
75 {
76  GENERAL,
77  EQUIPPABLE,
78  FOOD
79 };
80 
86 struct GameItems
87 {
88  std::map<int, GeneralItemStruct> generalItems;
89  std::map<int, EquippableItemStruct> equippableItems;
90  std::map<int, FoodItemStruct> foodItems;
91 
92  const bool containsId(const int id) const
93  {
94  return generalItems.contains(id) || equippableItems.contains(id) || foodItems.contains(id);
95  }
96 
97  const std::vector<int> getAllIds() const
98  {
99  std::vector<int> ids;
100  for (const auto& [id, _] : generalItems)
101  {
102  ids.push_back(id);
103  }
104  for (const auto& [id, _] : equippableItems)
105  {
106  ids.push_back(id);
107  }
108  for (const auto& [id, _] : foodItems)
109  {
110  ids.push_back(id);
111  }
112  return ids;
113  }
114 
115  std::optional<ItemType> getItemType(int id)
116  {
117  if (generalItems.contains(id))
118  {
119  return ItemType::GENERAL;
120  }
121  else if (equippableItems.contains(id))
122  {
123  return ItemType::EQUIPPABLE;
124  }
125  else if (foodItems.contains(id))
126  {
127  return ItemType::FOOD;
128  }
129  else
130  {
131  return std::nullopt;
132  }
133  }
134 
136  {
137  return generalItems.at(id);
138  }
139 
141  {
142  return equippableItems.at(id);
143  }
144 
146  {
147  return foodItems.at(id);
148  }
149 };
150 
std::ostream & operator<<(std::ostream &os, const ItemStats &stats)
Definition: ItemReader.hpp:32
GameItems fetchItems()
Reads items defined in the items.json file and returns them as a GameItems struct....
Definition: ItemReader.cpp:14
ItemType
Definition: ItemReader.hpp:75
Slot
Definition: ItemReader.hpp:39
@ BOTTOM
@ OFF_HAND
@ GLOVES
@ MAIN_HAND
Definition: ItemReader.hpp:63
bool isStackable
Definition: ItemReader.hpp:66
Slot slot
Definition: ItemReader.hpp:64
ItemStats stats
Definition: ItemReader.hpp:65
Definition: ItemReader.hpp:70
int healAmount
Definition: ItemReader.hpp:71
Contains all the item data in the game. This is used to load item assets into memory and create item ...
Definition: ItemReader.hpp:87
const GeneralItemStruct & getGeneralItem(int id)
Definition: ItemReader.hpp:135
const EquippableItemStruct & getEquippableItem(int id)
Definition: ItemReader.hpp:140
std::map< int, EquippableItemStruct > equippableItems
Definition: ItemReader.hpp:89
const std::vector< int > getAllIds() const
Definition: ItemReader.hpp:97
const FoodItemStruct & getFoodItem(int id)
Definition: ItemReader.hpp:145
std::map< int, GeneralItemStruct > generalItems
Definition: ItemReader.hpp:88
const bool containsId(const int id) const
Definition: ItemReader.hpp:92
std::optional< ItemType > getItemType(int id)
Definition: ItemReader.hpp:115
std::map< int, FoodItemStruct > foodItems
Definition: ItemReader.hpp:90
Definition: ItemReader.hpp:52
std::string description
Definition: ItemReader.hpp:55
int id
Definition: ItemReader.hpp:53
std::string name
Definition: ItemReader.hpp:54
std::string iconFilename
Definition: ItemReader.hpp:59
std::string objFilename
Definition: ItemReader.hpp:58
bool isStackable
Definition: ItemReader.hpp:56
std::string textureFilename
Definition: ItemReader.hpp:57
Definition: ItemReader.hpp:9
friend std::ostream & operator<<(std::ostream &os, const ItemStats &stats)
Definition: ItemReader.hpp:32
int armor
Definition: ItemReader.hpp:20
int attackSpeed
Definition: ItemReader.hpp:16
ItemStats operator+(ItemStats other)
Definition: ItemReader.hpp:24
ItemStats(int acc=0, int dam=0, int aspd=0, int rng=0, int arm=0, int dod=0)
Definition: ItemReader.hpp:10
int dodge
Definition: ItemReader.hpp:22
int damage
Definition: ItemReader.hpp:14
int accuracy
Definition: ItemReader.hpp:10
int range
Definition: ItemReader.hpp:18