forked from SharifAIChallenge/AIC16-Client-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
203 lines (167 loc) · 4.16 KB
/
Game.cpp
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
#include "Game.h"
#include <ctime>
#include <x86_64-linux-gnu/sys/time.h>
#include "util.h"
long long getTimeInMilliSeconds() {
timeval tv;
gettimeofday(&tv,NULL);
return (1000000 * tv.tv_sec + tv.tv_usec)/1000;
}
Game::Game()
{
turnStartTime = -1;
map = NULL;
lossRate1 = 0;
lossRate2 = 0;
escape = 0;
totalTurns = 0;
turn = 0;
turnTimeout = 0;
myID = 0;
firstlvl = 0;
secondlvl = 0;
nodeBonus = 0;
edgeBonus = 0;
}
Game::~Game()
{
if(map != NULL)
delete map;
}
void Game::setConstants(Json::Value &msg) {
this->totalTurns = msg["turns"].asInt();
this->turnTimeout = msg["turnTimeout"].asInt();
this->escape = msg["escape"].asInt();
this->nodeBonus = msg["nodeBonus"].asInt();
this->edgeBonus = msg["edgeBonus"].asInt();
this->firstlvl = msg["firstlvl"].asInt();
this->secondlvl = msg["secondlvl"].asInt();
this->lossRate1 = msg["lossRate1"].asInt();
this->lossRate2 = msg["lossRate2"].asInt();
}
int Game::getTotalTurns() {
return this->totalTurns;
}
int Game::getEscapeConstant() {
return this->escape;
}
int Game::getNodeBonusConstant() {
return this->nodeBonus;
}
int Game::getEdgeBonusConstant() {
return this->edgeBonus;
}
int Game::getLowArmyBound() {
return this->firstlvl;
}
int Game::getMediumArmyBound() {
return this->secondlvl;
}
double Game::getMediumCasualtyCoefficient() {
return this->lossRate1;
}
double Game::getLowCasualtyCoefficient() {
return this->lossRate2;
}
void Game::handleInitMessage(Message msg)
{
Json::Value &argsArray = msg.getArray("args");
Json::UInt I=0;
Json::Value &constants = argsArray[I++];
this->setConstants(constants);
this->myID = argsArray[I++].asInt();
// graph deserialization
Json::Value &adjListInt = argsArray[I++];
std::vector<Node*> nodes;
for (int i = 0; i < (int)adjListInt.size(); i++)
{
nodes.push_back(new Node(i));
}
for (int i = 0; i < (int)adjListInt.size(); i++)
{
Json::Value &neighboursInt = adjListInt[i];
std::vector<Node*> neighbours;
for (int j = 0; j < (int)neighboursInt.size(); j++)
{
neighbours.push_back(nodes[neighboursInt[j].asInt()]);
}
nodes[i]->setNeighbours(neighbours);
}
Json::Value &graphDiff = argsArray[I++];
for (int i = 0; i < (int)graphDiff.size(); i++)
{
Json::Value &nodeDiff = graphDiff[i];
int node = nodeDiff[(Json::UInt)0].asInt();
int owner = nodeDiff[1].asInt();
int armyCount = nodeDiff[2].asInt();
nodes[node]->setOwner(owner);
nodes[node]->setArmyCount(armyCount);
}
map = new Graph(nodes);
updateNodesList();
}
void Game::handleTurnMessage(Message msg)
{
turnStartTime = getTimeInMilliSeconds();
Json::Value &argsArray = msg.getArray("args");
Json::UInt I=0;
turn = argsArray[I++].asInt(); /** EDITED BY MEHRAN : USED TO BE ++I **/
Json::Value &graphDiff = argsArray[I++];
for (int i = 0; i < (int)graphDiff.size(); i++)
{
Json::Value &nodeDiff = graphDiff[i];
Json::UInt J=0;
int nodeIndex = nodeDiff[J++].asInt(); /** EDITED BY MEHRAN : USED TO BE ++J **/
map->getNode(nodeIndex)->setOwner(nodeDiff[J++].asInt());
map->getNode(nodeIndex)->setArmyCount(nodeDiff[J++].asInt());
}
updateNodesList();
}
void Game::updateNodesList()
{
for(int i = 0; i < 3; i++)
this->nodes[i].clear();
for (Node* n : this->map->getNodes())
{
nodes[n->getOwner() + 1].push_back(n);
}
}
long long Game::getTurnTimePassed() {
return getTimeInMilliSeconds() - turnStartTime;
}
long long Game::getTurnRemainingTime() {
return turnTimeout - getTurnTimePassed();
}
int Game::getMyId() {
return myID;
}
Graph* Game::getMap() {
return map;
}
std::vector<Node*>& Game::getMyNodes() {
return this->nodes[this->myID + 1];
}
std::vector<Node*>& Game::getOpponentNodes() {
return this->nodes[2 - this->myID];
}
std::vector<Node*>& Game::getFreeNodes() {
return this->nodes[0];
}
int Game::getTurnNumber() {
return this->turn;
}
long long Game::getTotalTurnTime() {
return this->turnTimeout;
}
void Game::moveArmy(Node* src, Node* dst, int count)
{
this->moveArmy(src->getIndex(), dst->getIndex(), count);
}
void Game::moveArmy(int src, int dst, int count)
{
GameEvent* gameEvent = new GameEvent(Constants::TYPE_MOVE);
gameEvent->addArg(src);
gameEvent->addArg(dst);
gameEvent->addArg(count);
eventHandler->addEvent(gameEvent);
}