Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Resources.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <ostream>
5#include <stdexcept>
6#include <string>
7#include <string_view>
8
13
17class Resources {
18 public:
19 int food = 0;
20 int linemate = 0;
21 int deraumere = 0;
22 int sibur = 0;
23 int mendiane = 0;
24 int phiras = 0;
25 int thystame = 0;
26
27 bool isEmpty() const
28 {
29 return food == 0 && linemate == 0 && deraumere == 0 && sibur == 0 && mendiane == 0 &&
30 phiras == 0 && thystame == 0;
31 }
32
33 static std::string get_name(int index)
34 {
35 switch (index) {
36 case 0:
37 return "Food";
38 case 1:
39 return "Linemate";
40 case 2:
41 return "Deraumere";
42 case 3:
43 return "Sibur";
44 case 4:
45 return "Mendiane";
46 case 5:
47 return "Phiras";
48 case 6:
49 return "Thystame";
50 default:
51 throw std::out_of_range("Invalid resource index");
52 }
53 }
54
55 static std::optional<ResourceType> fromName(std::string_view name)
56 {
57 if (name == "food") return ResourceType::FOOD;
58 if (name == "linemate") return ResourceType::LINEMATE;
59 if (name == "deraumere") return ResourceType::DERAUMERE;
60 if (name == "sibur") return ResourceType::SIBUR;
61 if (name == "mendiane") return ResourceType::MENDIANE;
62 if (name == "phiras") return ResourceType::PHIRAS;
63 if (name == "thystame") return ResourceType::THYSTAME;
64 return std::nullopt;
65 }
66
67 static std::string get_name(ResourceType type) { return get_name(static_cast<int>(type)); }
68
69 int& operator[](size_t index)
70 {
71 switch (index) {
72 case 0:
73 return food;
74 case 1:
75 return linemate;
76 case 2:
77 return deraumere;
78 case 3:
79 return sibur;
80 case 4:
81 return mendiane;
82 case 5:
83 return phiras;
84 case 6:
85 return thystame;
86 default:
87 throw std::out_of_range("Invalid resource index");
88 }
89 }
90
91 const int& operator[](size_t index) const
92 {
93 switch (index) {
94 case 0:
95 return food;
96 case 1:
97 return linemate;
98 case 2:
99 return deraumere;
100 case 3:
101 return sibur;
102 case 4:
103 return mendiane;
104 case 5:
105 return phiras;
106 case 6:
107 return thystame;
108 default:
109 throw std::out_of_range("Invalid resource index");
110 }
111 }
112
113 int& operator[](ResourceType type) { return (*this)[static_cast<size_t>(type)]; }
114
115 const int& operator[](ResourceType type) const { return (*this)[static_cast<size_t>(type)]; }
116
117 static float density(ResourceType type)
118 {
119 switch (type) {
121 return 0.5f;
123 return 0.3f;
125 return 0.15f;
127 return 0.1f;
129 return 0.1f;
131 return 0.08f;
133 return 0.05f;
134 default:
135 throw std::out_of_range("Invalid resource type");
136 }
137 }
138 static constexpr int TYPE_COUNT = static_cast<int>(ResourceType::COUNT);
139};
140
141inline std::ostream& operator<<(std::ostream& os, const Resources& res)
142{
143 os << "Food: " << res.food << ", Linemate: " << res.linemate << ", Deraumere: " << res.deraumere
144 << ", Sibur: " << res.sibur << ", Mendiane: " << res.mendiane << ", Phiras: " << res.phiras
145 << ", Thystame: " << res.thystame;
146 return os;
147}
ResourceType
Enumerate the different types of resources in the game.
Definition Resources.hpp:12
std::ostream & operator<<(std::ostream &os, const Resources &res)
static std::string res(const Resources &r)
Definition Serializer.cpp:5
Represents the resources available in the game, including food and various minerals.
Definition Resources.hpp:17
static float density(ResourceType type)
static std::optional< ResourceType > fromName(std::string_view name)
Definition Resources.hpp:55
bool isEmpty() const
Definition Resources.hpp:27
int & operator[](size_t index)
Definition Resources.hpp:69
const int & operator[](ResourceType type) const
static std::string get_name(ResourceType type)
Definition Resources.hpp:67
static std::string get_name(int index)
Definition Resources.hpp:33
const int & operator[](size_t index) const
Definition Resources.hpp:91
static constexpr int TYPE_COUNT
int & operator[](ResourceType type)
int deraumere
Definition Resources.hpp:21