-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.cpp
More file actions
490 lines (425 loc) · 16.1 KB
/
host.cpp
File metadata and controls
490 lines (425 loc) · 16.1 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "host.h"
#include "fish.h"
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <utility>
#include "filelogger.h"
#include "stdoutlogger.h"
#include <algorithm>
#ifdef _DEBUG_VER_
#include <iostream>
#endif
static inline int min(int a, int b){
return (a<b)?a:b;
}
static inline int max(int a, int b){
return (a>b)?a:b;
}
static inline int RandInt(int a, int b){
return static_cast<int>(((static_cast<unsigned>(rand()) << 16) + (static_cast<unsigned>(rand()))) % (b-a+1) + a);
}
static std::pair<int, int> GetRandomCoord(){
static Host& host = RetrieveHost();
int x, y;
do{
x = RandInt(1, N);
y = RandInt(1, M);
} while (host.getMapContent(x, y) != EMPTY);
return std::make_pair(x, y);
}
static inline bool IfCoordValid(int x, int y){
return x>0 && x<=N && y>0 && y<=M;
}
inline bool Host::IfIdValid(int id) const{
return id>=1 && id<=iIdPoolSize && fdpIdTable[id];
}
static inline int ManhattanDistance(int x1, int y1, int x2, int y2){
return abs(x1-x2) + abs(y1-y2);
}
static inline bool ActionPriority(FishData_t* l, FishData_t* r){
static Host &host = RetrieveHost();
return (l->iStatus < r->iStatus) ||
((l->iStatus == r->iStatus) &&
( (l->iSpeed > r->iSpeed) ||
((l->iSpeed == r->iSpeed) &&
( (l->iHP * r->iMaxHP > r->iHP * l->iMaxHP) ||
((l->iHP * r->iMaxHP == r->iHP * l->iMaxHP) &&
(host.getPoint(l->iId) < host.getPoint(r->iId)) )))));
}
static inline bool PointPriority(FishData_t* l, FishData_t* r){
static Host &host = RetrieveHost();
return (host.getPoint(l->iId) > host.getPoint(r->iId));
}
Host::Host():iPlayerCount(0), iDeathCount(0), iHostStatus(BEFOREFIRSTSETUP),
elhLog(){
srand(static_cast<unsigned>(time(0)));
}
Host::~Host(){
Destroy();
}
void Host::SetLogger(){
elhLog.AddLogger(new FileLogger("result.txt"));
#ifdef _ENABLE_STDOUTLOGGER_
elhLog.AddLogger(new StdoutLogger);
#endif
}
void Host::FirstSetup(){
if (iHostStatus == BEFOREFIRSTSETUP) {
SetLogger();
iHostStatus = DESTROYED;
Initialize();
SetupAI();
}
}
void Host::Reset(){
if (iHostStatus == END){
Destroy();
Initialize();
SetupAI();
}
}
void Host::Destroy(){
if (iHostStatus == END){
for (int i = 0; i!=MAX_PLAYER; ++i)
delete fdpFishTable[i];
iHostStatus = DESTROYED;
elhLog.HostDestroyed(time(0));
}
}
void Host::Initialize(){
if (iHostStatus == DESTROYED){
iPlayerCount = 0;
for (int i = 0; i!=iIdPoolSize; ++i){
ipIdPool[i] = i+1;
}
for (int i = 0; i<=iIdPoolSize; ++i){
fdpIdTable[i+1] = 0;
}
iDeathCount = 0;
for (int i = 0; i!=MAX_PLAYER; ++i){
fdpFishTable[i] = 0;
fdpDeathTable[i] = 0;
}
for (int i = 1; i<=N; ++i)
for (int j = 1; j<=M; ++j)
ipMapContent[i][j] = EMPTY;
iHostStatus = INITIALIZED;
elhLog.HostInitialized(time(0));
}
}
FishData_t::FishData_t(int id, fish* fIns):iId(id), iPropertyPoint(INITIAL_PROPERTY_POINT), iHP(0), iMaxHP(0), iStrength(0), iSpeed(0), iExp(0),
iLevel(1), iPosX(INVALID_VALUE), iPosY(INVALID_VALUE), iKillBonus(0), iKillCount(0), iDeadCount(0),
strIdentifier(), iPhase(END), iStatus(DEAD), iRoundSinceDead(0), fishInstance(fIns)
{}
FishData_t::~FishData_t(){
delete fishInstance;
}
int Host::AllocateID(fish* fIns){
if (iPlayerCount == MAX_PLAYER) return INVALID_VALUE; //number of player exceed the limit
//Allocate ID
{
int iChosenIndex = RandInt(iPlayerCount, iIdPoolSize-1);
if (iPlayerCount!=iChosenIndex){
ipIdPool[iPlayerCount]^=ipIdPool[iChosenIndex];
ipIdPool[iChosenIndex]^=ipIdPool[iPlayerCount];
ipIdPool[iPlayerCount]^=ipIdPool[iChosenIndex];
}
}
//Allocate Data Space
fdpFishTable[iPlayerCount] = new FishData_t(ipIdPool[iPlayerCount], fIns);
fdpIdTable[ipIdPool[iPlayerCount]] = fdpFishTable[iPlayerCount];
return ipIdPool[iPlayerCount++];
}
int Host::getPropertyPoint(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iPropertyPoint) : INVALID_VALUE;
}
int Host::getPoint(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iKillBonus + fdpIdTable[id]->iExp) : INVALID_VALUE;
}
int Host::getLevel(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iLevel) : INVALID_VALUE;
}
int Host::getExp(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iExp) : INVALID_VALUE;
}
int Host::getX(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iPosX) : INVALID_VALUE;
}
int Host::getY(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iPosY) : INVALID_VALUE;
}
int Host::getHP(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iHP) : INVALID_VALUE;
}
int Host::getMaxHP(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iMaxHP) : INVALID_VALUE;
}
int Host::getAtt(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iStrength) : INVALID_VALUE;
}
int Host::getSp(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iSpeed) : INVALID_VALUE;
}
int Host::getMapContent(int x, int y) const{
return (IfCoordValid(x,y)) ? (ipMapContent[x][y]) : EMPTY;
}
int Host::getMapContentWithId(int x, int y, int id) const{
return (IfIdValid(id) && fdpIdTable[id]->iStatus != FishData_t::DEAD) ? getMapContent(x,y) : EMPTY;
}
int Host::getDeadCount(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iDeadCount) : INVALID_VALUE;
}
int Host::getKillCount(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iKillCount) : INVALID_VALUE;
}
int Host::getKillBonus(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->iKillBonus) : INVALID_VALUE;
}
int Host::getPlayerCount() const{
return iPlayerCount;
}
std::string Host::getIdentifier(int id) const{
return (IfIdValid(id)) ? (fdpIdTable[id]->strIdentifier) : "INV";
}
bool Host::move(int id, int x, int y){
if (!IfIdValid(id)) {
return false;
} //invalid id
if (!IfCoordValid(x, y)){
elhLog.FishMove(id, getX(id), getY(id), x, y, 1);
return false;
} //invalid coordinate
if (getMapContent(x, y) != EMPTY){
elhLog.FishMove(id, getX(id), getY(id), x, y, 4);
return false;
} //position already occupied
if (fdpIdTable[id]->iPhase!=FishData_t::START){
elhLog.FishMove(id, getX(id), getY(id), x, y, 5);
return false;
} // already moved or attacked
if (fdpIdTable[id]->iSpeed < ManhattanDistance(getX(id), getY(id), x, y)){
elhLog.FishMove(id, getX(id), getY(id), x, y, 3);
return false;
}
int iFormerX = getX(id), iFormerY = getY(id);
if (IfCoordValid(iFormerX, iFormerY))
ipMapContent[iFormerX][iFormerY] = EMPTY; //empty the original grid
ipMapContent[x][y] = id; //fill (x,y) with id
fdpIdTable[id]->iPosX = x; //update coord in FishData
fdpIdTable[id]->iPosY = y;
fdpIdTable[id]->iPhase = FishData_t::AFTER_MOVE;
elhLog.FishMove(id, iFormerX, iFormerY, x, y, 0);
return true;
}
int Host::IncreaseHP(int id, int delta){
if (delta<=0) return 0; //invalid delta
delta = min(delta, fdpIdTable[id]->iMaxHP - fdpIdTable[id]->iHP);
fdpIdTable[id]->iHP += delta;
if (delta) elhLog.FishHPModified(id);
return delta;
}
bool Host::DecreaseHP(int id, int delta){
if (delta<=0) return false; //invalid delta
fdpIdTable[id]->iHP -= delta;
if (fdpIdTable[id]->iHP > 0) {
if (delta) elhLog.FishHPModified(id);
return false;
}
else{
fdpIdTable[id]->iHP = 0;
fdpIdTable[id]->iStatus = FishData_t::DEAD;
fdpIdTable[id]->iPhase = FishData_t::END;
fdpIdTable[id]->iRoundSinceDead = 0;
ipMapContent[getX(id)][getY(id)] = EMPTY;
fdpIdTable[id]->iPosX = INVALID_VALUE;
fdpIdTable[id]->iPosY = INVALID_VALUE;
++fdpIdTable[id]->iDeadCount;
fdpDeathTable[iDeathCount++] = fdpIdTable[id];
elhLog.FishDead(id);
return true;
}
}
int Host::IncreaseExp(int id, int delta){
if (delta<=0) return 0; //invalide delta
fdpIdTable[id]->iExp += delta;
elhLog.FishExpIncreased(id);
int newLevel = trunc((sqrt(9 + 8 * fdpIdTable[id]->iExp) - 1) / 2);
while (((newLevel+3) * newLevel / 2) <= fdpIdTable[id]->iExp) ++newLevel; //in case of float error
while (((newLevel+2) * (newLevel-1) / 2) > fdpIdTable[id]->iExp) --newLevel;
newLevel -= fdpIdTable[id]->iLevel;
fdpIdTable[id]->iPropertyPoint+= 3 * newLevel;
fdpIdTable[id]->iLevel+=newLevel;
if (newLevel) elhLog.FishLevelUp(id);
return newLevel;
}
void Host::RefreshFood(){
for (int i=0; i<N; ++i)
for (int j=0; j<M; ++j)
if (ipMapContent[i][j] == FOOD) ipMapContent[i][j] = EMPTY;
#ifdef _DEBUG_VER_
int ipPosX[MAX_FOOD], ipPosY[MAX_FOOD];
#endif
for (int i=0; i<MAX_FOOD; ++i){
std::pair<int, int> coord = GetRandomCoord();
ipMapContent[coord.first][coord.second] = FOOD;
#ifdef _DEBUG_VER_
ipPosX[i] = coord.first;
ipPosY[i] = coord.second;
#endif
}
#ifdef _DEBUG_VER_
elhLog.FoodRefreshed(ipPosX, ipPosY);
#else
elhLog.FoodRefreshed(0, 0);
#endif
}
bool Host::attack(int id, int x, int y){
if (!IfIdValid(id)){
return false;
} // invalid id
if (!IfCoordValid(x, y)){
elhLog.FishAttack(id, x, y, EMPTY, 1);
return false;
} //invalid coordinate
if (fdpIdTable[id]->iPhase != FishData_t::START && fdpIdTable[id]->iPhase!=FishData_t::AFTER_MOVE){
elhLog.FishAttack(id, x, y, getMapContent(x, y), 5);
return false;
} //attack denied during current phase
if (getMapContent(x, y) == EMPTY){
elhLog.FishAttack(id, x, y, getMapContent(x, y), 4);
return false;
} //nothing to attack
if (getMapContent(x, y) == id){
elhLog.FishAttack(id, x, y, getMapContent(x, y), 6);
return false;
} //are you serious? YOU WANNA ATTACK YOURSELF?
if (ManhattanDistance(getX(id), getY(id), x, y) > 1){
elhLog.FishAttack(id, x, y, getMapContent(x, y), 3);
return false;
} //target is not in range
fdpIdTable[id]->iPhase = FishData_t::AFTER_ATTACK;
if (getMapContent(x, y) == FOOD){
elhLog.FishAttack(id, x, y, getMapContent(x, y), 0);
ipMapContent[x][y] = EMPTY;
IncreaseHP(id, max(2, getMaxHP(id) / 10));
IncreaseExp(id, 1);
return true;
} else {
int target = getMapContent(x, y);
elhLog.FishAttack(id, x, y, getMapContent(x, y), 0);
if (DecreaseHP(target, fdpIdTable[id]->iStrength)){
IncreaseExp(id, max(1, fdpIdTable[target]->iLevel/2));
fdpIdTable[id]->iKillBonus += (max(0, fdpIdTable[target]->iLevel - fdpIdTable[id]->iLevel) << 1);
++fdpIdTable[id]->iKillCount;
}
return true;
}
}
bool Host::increaseHealth(int id, int delta){
if ( (!IfIdValid(id)) //invalid id
|| (delta <= 0) //invalid delta
|| (fdpIdTable[id]->iStatus == FishData_t::DEAD) //dead fish cannot increase propery points
|| (fdpIdTable[id]->iPropertyPoint < delta) //no sufficient property point
) return false;
fdpIdTable[id]->iMaxHP += (delta << 1);
elhLog.FishHealthIncreased(id);
fdpIdTable[id]->iHP += (delta << 1);
elhLog.FishHPModified(id);
fdpIdTable[id]->iPropertyPoint-=delta;
return true;
}
bool Host::increaseStrength(int id, int delta){
if ( (!IfIdValid(id)) //invalid id
|| (delta <= 0) //invalid delta
|| (fdpIdTable[id]->iStatus == FishData_t::DEAD) //dead fish cannot increase propery points
|| (fdpIdTable[id]->iPropertyPoint < delta) //no sufficient property point
) return false;
fdpIdTable[id]->iStrength += delta;
elhLog.FishStrengthIncreased(id);
fdpIdTable[id]->iPropertyPoint-=delta;
return true;
}
bool Host::increaseSpeed(int id, int delta){
if ( (!IfIdValid(id)) //invalid id
|| (delta <= 0) //invalid delta
|| (fdpIdTable[id]->iStatus == FishData_t::DEAD) //dead fish cannot increase propery points
|| (fdpIdTable[id]->iPropertyPoint < delta) //no sufficient property point
) return false;
fdpIdTable[id]->iSpeed += delta;
elhLog.FishSpeedIncreased(id);
fdpIdTable[id]->iPropertyPoint-=delta;
return true;
}
bool Host::Start(){
if (iHostStatus == READY){
iHostStatus = ONGOING;
elhLog.GameStarted(time(0));
//preparation
RefreshFood(); //refresh food
for (int i=0; i<iPlayerCount; ++i){
std::pair<int, int> coord = GetRandomCoord();
fdpFishTable[i]->iPosX = coord.first;
fdpFishTable[i]->iPosY = coord.second;
ipMapContent[coord.first][coord.second] = fdpFishTable[i]->iId;
fdpFishTable[i]->iStatus = FishData_t::ALIVE;
elhLog.FishBorn(fdpFishTable[i]->iId);
} //get fish born
for (int i=0; i<iPlayerCount; ++i){
elhLog.FishInitializing(fdpFishTable[i]->iId);
fdpFishTable[i]->fishInstance->init();
} //invoke init()
//ongoing
for (int iRoundCount=0; iRoundCount<MAX_ROUND; ++iRoundCount){
if (iRoundCount!=0 && iRoundCount % FOOD_ROUND == 0) RefreshFood(); //refresh food every FOOD_ROUND rounds
elhLog.RoundStarted(iRoundCount);
{
int j = 0;
for (int i = 0; i<iDeathCount; ++i){
if (fdpDeathTable[i]->iRoundSinceDead == iReviveAfterRound){
std::pair<int, int> coord;
fdpDeathTable[i]->fishInstance->revive(coord.first, coord.second);
if (!IfCoordValid(coord.first, coord.second) ||
ipMapContent[coord.first][coord.second]!=EMPTY)
coord = GetRandomCoord();
ipMapContent[coord.first][coord.second] = fdpDeathTable[i]->iId;
fdpDeathTable[i]->iPosX = coord.first;
fdpDeathTable[i]->iPosY = coord.second;
fdpDeathTable[i]->iStatus = FishData_t::ALIVE;
fdpDeathTable[i]->iHP = max(fdpDeathTable[i]->iMaxHP / 10, 1);
elhLog.FishRevived(fdpDeathTable[i]->iId);
} else {
++fdpDeathTable[i]->iRoundSinceDead;
fdpDeathTable[j++] = fdpDeathTable[i];
}
}
iDeathCount = j;
} // revive fish
{
std::sort(fdpFishTable, fdpFishTable+iPlayerCount, &ActionPriority);
int ipIdList[MAX_PLAYER];
for (int iCurrentFish = 0; iCurrentFish<iPlayerCount; ++iCurrentFish)
ipIdList[iCurrentFish] = fdpFishTable[iCurrentFish]->iId;
elhLog.SequenceDecided(ipIdList);
} // Decide sequence
// round started
for (int iCurrentFish = 0; iCurrentFish<iPlayerCount; ++iCurrentFish){
if (fdpFishTable[iCurrentFish]->iStatus == FishData_t::DEAD) continue;
fdpFishTable[iCurrentFish]->iPhase = FishData_t::START;
elhLog.FishInAction(fdpFishTable[iCurrentFish]->iId);
fdpFishTable[iCurrentFish]->fishInstance->play();
fdpFishTable[iCurrentFish]->iPhase = FishData_t::END;
}
}
{
std::sort(fdpFishTable, fdpFishTable+iPlayerCount, &PointPriority);
int ipIdList[MAX_PLAYER];
for (int iCurrentFish = 0; iCurrentFish<iPlayerCount; ++iCurrentFish)
ipIdList[iCurrentFish] = fdpFishTable[iCurrentFish]->iId;
elhLog.GameEnded(time(0), ipIdList);
} // report that the game has ended
iHostStatus = END;
return true;
}
return false;
}