-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.hpp
229 lines (163 loc) · 4.76 KB
/
game.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef GAME_HPP
#define GAME_HPP
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <stdexcept>
#include <thread>
#include <cstdlib>
#include <ostream>
#include "executable.hpp"
#include "ui.hpp"
#include "config.hpp"
class Game;
class GameError;
class League;
class Unit;
struct UnitKind;
class Executable;
/*
* Game.
*/
class Game {
friend Unit;
private:
UIDisplay display;
const Config &config;
typedef std::unordered_map<std::string, League> LeagueMap;
LeagueMap leagues;
std::thread thread;
volatile bool threadCont;
std::vector<Unit *> board;
public:
Game(const Config &config);
~Game();
SpriteID registerSprite(const Sprite &sprite) {return display.registerSprite(sprite);}
const Config &getConfig() const {return config;}
const LeagueMap &getLeagues() const {return leagues;}
void placeUnit(Unit &unit);
void removeUnit(const Unit &unit);
bool isValidPosition(int x, int y) const;
bool isFreePosition(int x, int y) const;
void getRandomLocation(int &x, int &y);
void start();
};
/*
* League.
*/
class League {
private:
std::unordered_map<std::string, UnitKind> unitKinds;
std::size_t nextUnitIndex = 0;
std::vector<Unit> staging;
public:
League() {}
League(Game &game, const LeagueInfo &info);
std::vector<Unit> units;
Unit *getNextUnit();
std::uint64_t getTotalBiomass() const;
};
static inline bool operator<(const League &a, const League &b) {
return a.getTotalBiomass() < b.getTotalBiomass();
}
/*
* Unit.
*/
class Unit {
public:
enum class Direction {
North = 0,
East = 1,
South = 2,
West = 3,
Max = West
};
class Position {
private:
int x, y;
public:
Position(int x, int y) : x(x), y(y) {};
int getX() const {return x;}
int getY() const {return y;}
void move(const Game &game, Direction dir);
void move(Direction dir);
std::string stringValue();
};
static Direction getRandomDirection();
enum class InsnRep {
Eat,
Go,
Str
};
typedef long Weight;
Unit(/*Game &game, const League &league,*/ SpriteID sprite, Executable *exec, int x, int y, bool newborn = false)
: /*game(game), league(league),*/ sprite(sprite), exec(exec), position(x, y), weight(newborn? 2 : 5) {}
/*Unit(const Unit &src) :
position(src.position), direction(src.direction),
insnRep(src.insnRep), insnRepCnt(src.insnRepCnt),
weight(src.weight),
sprite(src.sprite), exec(src.exec),
pc(src.pc) {}*/
//const Unit &operator=(const Unit &src) {return *this = Unit(src);}
void execInsn(Game &game, League &league);
const Position getPosition() const {return position;}
Weight getWeight() const {return weight;}
bool isDead() const {return weight <= 0;}
SpriteID getSpriteID() const {return sprite;}
private:
/*Game &game;
const League &league;*/
Position position;
Unit *findEnemy(Game &game);
Direction direction = getRandomDirection();
InsnRep insnRep;
int insnRepCnt = 0;
Weight weight;
bool loseWeight(Game &game, Weight loss = 1);
void damage(Game &game, Weight attackerWeight) {
loseWeight(game, GetRandom(static_cast<std::uint32_t>(3 + attackerWeight / 2)));
}
SpriteID sprite;
Executable *exec;
Executable::Word pc = 0;
};
static inline Unit::Direction operator+(Unit::Direction dir, int offs) {
return static_cast<Unit::Direction>((static_cast<int>(dir) + offs) % 4);
}
static inline Unit::Direction &operator+=(Unit::Direction &dir, int offs) {
return dir = dir + 1;
}
static inline Unit::Direction &operator++(Unit::Direction &dir) {
return dir += 1;
}
static inline Unit::Direction operator++(Unit::Direction &dir, int) {
auto ret = dir;
dir += 1;
return ret;
}
static inline Unit::Direction operator-(Unit::Direction dir, int offs) {
return static_cast<Unit::Direction>(std::abs((static_cast<int>(dir) - offs) % 4));
}
static inline Unit::Direction &operator-=(Unit::Direction &dir, int offs) {
return dir = dir - 1;
}
static inline Unit::Direction &operator--(Unit::Direction &dir) {
return dir -= 1;
}
static inline Unit::Direction operator--(Unit::Direction &dir, int) {
auto ret = dir;
dir -= 1;
return ret;
}
static inline Unit::Direction operator~(Unit::Direction dir) {
return static_cast<Unit::Direction>((static_cast<int>(dir) + 2) % 4);
}
/*
* UnitKind.
*/
struct UnitKind {
SpriteID sprite;
Executable exec;
};
#endif