Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
ObjectReader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fstream>
4 #include <iostream>
5 #include <map>
6 #include <optional>
7 #include <string>
8 
9 #include "ItemReader.hpp"
10 #include "SkillReader.hpp"
11 #include "Utils.hpp"
12 
17 {
18  unsigned id;
19  std::string instanceId;
20  int x;
21  int y;
22  int z;
23  unsigned rotation;
24 };
25 
32 std::string generateObjectInstanceId(ObjectLocation objectLocation);
33 
39 const std::vector<ObjectLocation> readObjectLocations();
40 
42 {
43  int id;
44  std::string name;
45  std::string description;
46  std::vector<std::vector<std::string>> tileMap;
47  std::string textureFilename;
48  std::string objFilename;
49 };
50 
52 {
53  int itemId;
55 };
56 
58 {
59  std::vector<YieldableItem> yieldableItemList;
61 };
62 
64 {
65  int itemId;
69 };
70 
72 {
74  std::vector<ItemTransform> itemTransformList;
79 };
80 
82 {
83  std::vector<YieldableItem> yieldableItemList;
85 };
86 
87 namespace reader
88 {
89  enum class ObjectType
90  {
91  GENERAL,
92  RESOURCE,
93  LOOT
94  };
95 }
96 
101 {
102  std::map<int, GeneralObjectStruct> generalObjects;
103  std::map<int, ResourceObjectStruct> resourceObjects;
104  std::map<int, LootObjectStruct> lootObjects;
105 
106  bool containsId(int id)
107  {
108  return generalObjects.contains(id) || resourceObjects.contains(id) || lootObjects.contains(id);
109  }
110 
111  // Returns object type for given object id. Call this first and then use the appropriate getter
112  const std::optional<reader::ObjectType> getObjectType(int id)
113  {
114  if (generalObjects.contains(id))
115  {
117  }
118  else if (resourceObjects.contains(id))
119  {
121  }
122  else if (lootObjects.contains(id))
123  {
125  }
126  else
127  {
128  return std::nullopt;
129  }
130  }
131 
132  // Get object tile map for given object id (returns empty vector if not found)
133  std::vector<std::vector<std::string>> getTileMap(int id)
134  {
135  std::optional<reader::ObjectType> type = getObjectType(id);
136 
137  if (type == std::nullopt)
138  {
139  return {};
140  }
141 
142  switch (type.value())
143  {
145  return generalObjects[id].tileMap;
147  return resourceObjects[id].tileMap;
149  return lootObjects[id].tileMap;
150  default:
151  return {};
152  }
153  }
154 
156  {
157  return generalObjects.at(id);
158  }
159 
161  {
162  return resourceObjects.at(id);
163  }
164 
166  {
167  return lootObjects.at(id);
168  }
169 
174  const std::vector<int> getObjectIds() const
175  {
176  std::vector<int> objectIds;
177 
178  for (const auto object : generalObjects)
179  {
180  objectIds.push_back(object.second.id);
181  }
182  for (const auto object : resourceObjects)
183  {
184  objectIds.push_back(object.second.id);
185  }
186  for (const auto object : lootObjects)
187  {
188  objectIds.push_back(object.second.id);
189  }
190 
191  return objectIds;
192  }
193 };
194 
202 GameObjects fetchObjects(GameItems& gameItems, std::map<int, GameSkill>& gameSkills);
GameObjects fetchObjects(GameItems &gameItems, std::map< int, GameSkill > &gameSkills)
Reads objects defined in the objects.json file and returns them as a GameObjects struct.
Definition: ObjectReader.cpp:93
std::string generateObjectInstanceId(ObjectLocation objectLocation)
Generates a unique id for an object instance.
Definition: ObjectReader.cpp:16
const std::vector< ObjectLocation > readObjectLocations()
Reads all the objects on the map. Server can use this to spawn objects on the map.
Definition: ObjectReader.cpp:29
static uint32_t id
Definition: Server.cpp:19
Definition: ObjectReader.hpp:88
ObjectType
Definition: ObjectReader.hpp:90
Contains all the item data in the game. This is used to load item assets into memory and create item ...
Definition: ItemReader.hpp:87
Contains all the objects defined in the objects.json file.
Definition: ObjectReader.hpp:101
std::vector< std::vector< std::string > > getTileMap(int id)
Definition: ObjectReader.hpp:133
const std::vector< int > getObjectIds() const
Returns all the object ids that exist.
Definition: ObjectReader.hpp:174
const GeneralObjectStruct & getGeneralObject(int id)
Definition: ObjectReader.hpp:155
const std::optional< reader::ObjectType > getObjectType(int id)
Definition: ObjectReader.hpp:112
const ResourceObjectStruct & getResourceObject(int id)
Definition: ObjectReader.hpp:160
const LootObjectStruct & getLootObject(int id)
Definition: ObjectReader.hpp:165
std::map< int, GeneralObjectStruct > generalObjects
Definition: ObjectReader.hpp:102
bool containsId(int id)
Definition: ObjectReader.hpp:106
std::map< int, ResourceObjectStruct > resourceObjects
Definition: ObjectReader.hpp:103
std::map< int, LootObjectStruct > lootObjects
Definition: ObjectReader.hpp:104
Definition: ObjectReader.hpp:42
std::vector< std::vector< std::string > > tileMap
Definition: ObjectReader.hpp:46
std::string description
Definition: ObjectReader.hpp:45
std::string name
Definition: ObjectReader.hpp:44
std::string textureFilename
Definition: ObjectReader.hpp:47
int id
Definition: ObjectReader.hpp:43
std::string objFilename
Definition: ObjectReader.hpp:48
Definition: ObjectReader.hpp:64
int resultItemId
Definition: ObjectReader.hpp:66
int resultItemQuantity
Definition: ObjectReader.hpp:67
int xpPerTransform
Definition: ObjectReader.hpp:68
int itemId
Definition: ObjectReader.hpp:65
Definition: ObjectReader.hpp:82
std::vector< YieldableItem > yieldableItemList
Definition: ObjectReader.hpp:83
std::string characterInteractAnimation
Definition: ObjectReader.hpp:84
Describes the location of an object on the map. This is used to spawn object instances on the map.
Definition: ObjectReader.hpp:17
unsigned id
Definition: ObjectReader.hpp:18
int x
Definition: ObjectReader.hpp:20
int z
Definition: ObjectReader.hpp:22
std::string instanceId
Definition: ObjectReader.hpp:19
unsigned rotation
Definition: ObjectReader.hpp:23
int y
Definition: ObjectReader.hpp:21
Definition: ObjectReader.hpp:72
std::vector< ItemTransform > itemTransformList
Definition: ObjectReader.hpp:74
int xpRequirement
Definition: ObjectReader.hpp:77
YieldableItems yieldableItems
Definition: ObjectReader.hpp:73
int relatedSkillId
Definition: ObjectReader.hpp:76
std::string characterInteractAnimation
Definition: ObjectReader.hpp:78
int depleteChance
Definition: ObjectReader.hpp:75
Definition: ObjectReader.hpp:52
int itemId
Definition: ObjectReader.hpp:53
int yieldPercentage
Definition: ObjectReader.hpp:54
Definition: ObjectReader.hpp:58
int xpPerYield
Definition: ObjectReader.hpp:60
std::vector< YieldableItem > yieldableItemList
Definition: ObjectReader.hpp:59