-
Notifications
You must be signed in to change notification settings - Fork 2
/
aibrain.cpp
363 lines (302 loc) · 12.6 KB
/
aibrain.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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <stuerz.simon@gmail.com> *
* *
* This file is part of Monster Wars. *
* *
* Monster Wars is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 3 of the License. *
* *
* Monster Wars is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Monster Wars. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "aibrain.h"
#include "math.h"
AiBrain::AiBrain(Board *board, Player *player, double strengthStepWidth, double defenseStepWidth, QObject *parent) :
QObject(parent),
m_board(board),
m_player(player),
m_strengthStepWidth(strengthStepWidth),
m_defenseStepWidth(defenseStepWidth)
{
m_attack = new Attack(this);
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &AiBrain::timeout);
}
void AiBrain::start()
{
qsrand(qrand());
m_timer->setInterval(qrand() % ((5000 + 1) - 4000) + 4000);
m_timer->start();
}
void AiBrain::stop()
{
m_timer->stop();
}
void AiBrain::calculateAttack()
{
QList<Monster *> myMonsters = m_board->myMonsters(m_player);
QList<Monster *> enemyMonsters = m_board->enemyMonsters(m_player);
QList<Monster *> freeMonsters = m_board->freeMonsters();
QList<VirtualAttack *> possibleAttacks;
// check all possible single attacks
foreach (Monster *myMonster, myMonsters) {
foreach (Monster *enemyMonster, enemyMonsters) {
possibleAttacks.append(new VirtualAttack(m_board, myMonster, enemyMonster, m_strengthStepWidth, m_defenseStepWidth));
}
foreach (Monster *freeMonster, freeMonsters) {
possibleAttacks.append(new VirtualAttack(m_board, myMonster, freeMonster, m_strengthStepWidth, m_defenseStepWidth));
}
}
// check multiple attacks
if (myMonsters.count() >= 2) {
QList<Monster *> multipleAttackMonsters;
// take the two monsters with the highest count
qSort(myMonsters.begin(), myMonsters.end(), compareMonsterValue);
multipleAttackMonsters.append(myMonsters.at(0));
multipleAttackMonsters.append(myMonsters.at(1));
foreach (Monster *enemyMonster, enemyMonsters) {
possibleAttacks.append(new VirtualAttack(m_board, multipleAttackMonsters, enemyMonster, m_strengthStepWidth, m_defenseStepWidth));
}
}
qDebug() << QString("Player %1: possible attacks -> %2").arg(m_player->id()).arg(possibleAttacks.count());
if (possibleAttacks.isEmpty())
return;
// get the best attack of each heuristic
QList<VirtualAttack *> bestOfEachHeuristic;
// get shortest distance attack
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareDistance);
VirtualAttack *shortestDistanceAttack = possibleAttacks.first();
bestOfEachHeuristic.append(shortestDistanceAttack);
// get attack with highest pillow value
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareSourceValue);
VirtualAttack *highestSourceAttack = possibleAttacks.first();
bestOfEachHeuristic.append(highestSourceAttack);
// weakest enemy attack
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareDestinationValue);
VirtualAttack *weakestTargetAttack = possibleAttacks.first();
bestOfEachHeuristic.append(weakestTargetAttack);
// smallest destination value after attack
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareDestinationValueAfterImpact);
VirtualAttack *smallestTargetValueAttack = possibleAttacks.first();
bestOfEachHeuristic.append(smallestTargetValueAttack);
// highest percentage after attack
qSort(possibleAttacks.begin(), possibleAttacks.end(), comparePercentageAfterImpact);
VirtualAttack *highestPercentageValueAttack = possibleAttacks.first();
bestOfEachHeuristic.append(highestPercentageValueAttack);
// size of target monster
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareDestinationSize);
VirtualAttack *biggestTargetAttack = possibleAttacks.first();
bestOfEachHeuristic.append(biggestTargetAttack);
// monster can be conquerd
qSort(possibleAttacks.begin(), possibleAttacks.end(), compareConquere);
VirtualAttack *canConquerAttack = possibleAttacks.first();
// add it twice to because conquer is verry important
bestOfEachHeuristic.append(canConquerAttack);
bestOfEachHeuristic.append(canConquerAttack);
// check which attack is the best in most heuristics
int totalHeuristicCount = bestOfEachHeuristic.count();
int heuristicCount = 0;
VirtualAttack *bestAttack = 0;
while (!bestOfEachHeuristic.isEmpty()) {
VirtualAttack *attack = bestOfEachHeuristic.first();
int count = bestOfEachHeuristic.removeAll(attack);
if (count >= heuristicCount) {
bestAttack = attack;
heuristicCount = count;
}
}
if (!bestAttack) {
qDeleteAll(possibleAttacks);
return;
}
qDebug() << QString("Player %1: Best turn heuristic ratio: %2%").arg(m_player->id()).arg(qRound(heuristicCount * 100.0 / totalHeuristicCount));
// create attack
m_attack->reset();
foreach (Monster *sourceMonster, bestAttack->sourceMonsters()) {
m_attack->addMonsterId(sourceMonster->id());
}
m_attack->endAttack(bestAttack->destinationMonster()->id());
// perform attack
emit startAttack(m_attack);
qDeleteAll(possibleAttacks);
}
int AiBrain::calculateIntervall()
{
qsrand(qrand());
int reactionValue = (8 - m_player->reaction()) * 500;
int min = 4000 + reactionValue;
int max = 8000 + reactionValue;
return qrand() % ((max + 1) - min) + min;
}
void AiBrain::timeout()
{
calculateAttack();
m_timer->setInterval(calculateIntervall());
qDebug() << QString("Player %1: Next turn in: %2 [ms]").arg(m_player->id()).arg(m_timer->interval());
}
VirtualAttack::VirtualAttack(Board *board, Monster *sourceMonster, Monster *destinationMonster, double strengthStepWidth, double defenseStepWidth) :
m_board(board),
m_sourceMonsters(QList<Monster *>() << sourceMonster),
m_destinationMonster(destinationMonster),
m_strengthStepWidth(strengthStepWidth),
m_defenseStepWidth(defenseStepWidth)
{
m_distance = calculateDistance();
m_valueAfterImpact = calculateDestinationPointsAfterAttack();
m_percentageAfterImpact = calculatePercentageAfterAttack();
}
VirtualAttack::VirtualAttack(Board *board, QList<Monster *> sourceMonsters, Monster *destinationMonster, double strengthStepWidth, double defenseStepWidth) :
m_board(board),
m_sourceMonsters(sourceMonsters),
m_destinationMonster(destinationMonster),
m_strengthStepWidth(strengthStepWidth),
m_defenseStepWidth(defenseStepWidth)
{
m_distance = calculateDistance();
m_valueAfterImpact = calculateDestinationPointsAfterAttack();
m_percentageAfterImpact = calculatePercentageAfterAttack();
}
QList<Monster *> VirtualAttack::sourceMonsters()
{
return m_sourceMonsters;
}
Monster *VirtualAttack::destinationMonster()
{
return m_destinationMonster;
}
int VirtualAttack::sourcePoints()
{
int points = 0;
foreach (Monster *sourceMonster, m_sourceMonsters) {
points += sourceMonster->value();
}
return points;
}
int VirtualAttack::valueAfterImpact()
{
return m_valueAfterImpact;
}
int VirtualAttack::percentageAfterImpact()
{
return m_percentageAfterImpact;
}
double VirtualAttack::distance()
{
return m_distance;
}
int VirtualAttack::canConquer()
{
return m_canConquer;
}
double VirtualAttack::calculateDistance()
{
double distance = 0;
foreach (Monster * sourceMonster, m_sourceMonsters) {
double dx = sourceMonster->position().x() - m_destinationMonster->position().x();
double dy = sourceMonster->position().y() - m_destinationMonster->position().y();
distance += sqrt(pow(dx,2) + pow(dy,2));
}
return distance / m_sourceMonsters.count();
}
int VirtualAttack::calculateDestinationPointsAfterAttack()
{
int totalAttackPoints = 0;
foreach (Monster *sourceMonster, m_sourceMonsters) {
int attackPoints = sourceMonster->value() / 2;
double strengthMultiplicator = 1 + (sourceMonster->player()->strength() * m_strengthStepWidth);
// check if source monster is a strength monster
if (sourceMonster->monsterType() == Monster::MonsterTypeStrength) {
strengthMultiplicator += 4 * m_strengthStepWidth;
}
double defenseMultiplicator = 1 - (destinationMonster()->player()->defense() * m_defenseStepWidth);
// check if destination monster is a defense monster
if (destinationMonster()->monsterType() == Monster::MonsterTypeDefense) {
defenseMultiplicator -= 4 * m_defenseStepWidth;
}
double finalMiltiplicator = (strengthMultiplicator + defenseMultiplicator) / 2;
totalAttackPoints += qRound(attackPoints * finalMiltiplicator);
}
int valueAfterImpact = destinationMonster()->value() - totalAttackPoints;
if(valueAfterImpact < 0) {
//qDebug() << "This attack (" << sourceMonster()->id() << "->" << destinationMonster()->id() << ") would conquer the monster.";
m_canConquer = 100;
} else {
m_canConquer = 0;
}
return valueAfterImpact;
}
int VirtualAttack::calculatePercentageAfterAttack()
{
QHash<Player *, int> points;
// get all player points
foreach (Player *player, m_board->playersList()) {
int playerPoints = 0;
foreach (Monster *monster, m_board->monstersList()) {
if (monster->player()->id() == player->id()) {
playerPoints += monster->value();
}
}
points.insert(player, playerPoints);
}
Player *myPlayer = sourceMonsters().first()->player();
Player *enemyPlayer = destinationMonster()->player();
// manipulate points for new percentage
int myPoints = points.take(myPlayer);
int enemyPoints = points.take(enemyPlayer);
myPoints -= m_attackPoints;
// if conquer...
if (m_attackPoints < 0) {
myPoints += abs(m_valueAfterImpact);
enemyPoints -= destinationMonster()->value();
} else {
enemyPoints = enemyPoints - destinationMonster()->value() + m_valueAfterImpact;
}
points.insert(myPlayer, myPoints);
points.insert(enemyPlayer, enemyPoints);
int totalPoints = 0;
foreach (int value, points) {
totalPoints += value;
}
// calculate new percentage
return qRound((double)points.value(myPlayer) * 100 / totalPoints);
}
bool compareDistance(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->distance() < attack2->distance();
}
bool compareSourceValue(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->sourcePoints() > attack2->sourcePoints();
}
bool compareDestinationValue(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->destinationMonster()->value() < attack2->destinationMonster()->value();
}
bool compareDestinationValueAfterImpact(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->valueAfterImpact() < attack2->valueAfterImpact();
}
bool comparePercentageAfterImpact(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->percentageAfterImpact() > attack2->percentageAfterImpact();
}
bool compareConquere(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->canConquer() > attack2->canConquer();
}
bool compareDestinationSize(VirtualAttack *attack1, VirtualAttack *attack2)
{
return attack1->destinationMonster()->size() > attack2->destinationMonster()->size();
}
bool compareMonsterValue(Monster *monster1, Monster *monster2)
{
return monster1->value() > monster2->value();
}