-
Notifications
You must be signed in to change notification settings - Fork 2
/
gameengine.cpp
569 lines (470 loc) · 15.1 KB
/
gameengine.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 <QCoreApplication>
#include <QFileInfo>
#include <QJsonDocument>
#include <QSettings>
#include "gameengine.h"
#include "board.h"
#include "level.h"
#include "player.h"
#include "monster.h"
#include "attack.h"
GameEngine::GameEngine(QObject *parent):
QObject(parent),
m_timer(new QTimer(this)),
m_displayTimer(new QTimer(this)),
m_board(new Board(this)),
m_ticksPerSecond(25),
m_rows(40),
m_columns(70),
m_running(false),
m_newHighScore(false),
m_tunePointEarned(false)
{
// Initialize propertys
m_strengthStepWidth = 0.1;
m_defenseStepWidth = 0.1;
m_reproductionStepWidth = 50;
m_speedStepWidth = 0.25;
m_pillowsModel = new AttackPillowModel(this);
m_levels = new LevelModel(this);
m_playerSettings = new PlayerSettings(this);
m_tickInterval = 1000 / m_ticksPerSecond;
connect(this, &GameEngine::gameFinished, this, &GameEngine::onGameOver);
connect(m_board, &Board::boardChanged, this, &GameEngine::boardChanged);
}
QHash<int, QVariantMap> GameEngine::levelDescriptions() const
{
return m_levelDescriptions;
}
QVariantMap GameEngine::levelDescription(int levelId) const
{
return m_levelDescriptions.value(levelId);
}
Board *GameEngine::board() const
{
return m_board;
}
LevelModel *GameEngine::levels()
{
return m_levels;
}
AttackPillowModel *GameEngine::pillows()
{
return m_pillowsModel;
}
QUrl GameEngine::dataDir() const
{
return m_dataDir;
}
void GameEngine::setDataDir(const QUrl &dataDir)
{
m_dataDir = dataDir;
emit dataDirChanged();
initGameEngine();
}
void GameEngine::start()
{
m_timer->start();
m_displayTimer->start();
m_running = true;
emit runningChanged();
}
void GameEngine::stop()
{
m_timer->stop();
m_displayTimer->stop();
m_running = false;
emit runningChanged();
}
int GameEngine::rows() const
{
return m_rows;
}
int GameEngine::columns() const
{
return m_columns;
}
QString GameEngine::gameTime() const
{
return QTime::fromMSecsSinceStartOfDay(m_totalGameTimeMs).toString("mm:ss.zzz");
}
QString GameEngine::displayGameTime() const
{
return QTime::fromMSecsSinceStartOfDay(m_totalGameTimeMs).toString("mm:ss");
}
bool GameEngine::running() const
{
return m_running;
}
bool GameEngine::newHighScore() const
{
return m_newHighScore;
}
bool GameEngine::tunePointEarned() const
{
return m_tunePointEarned;
}
int GameEngine::winnerId() const
{
return m_winnerId;
}
void GameEngine::startAttack(Attack *attack)
{
qDebug() << "Start attack -> " << attack->sourceIds() << "->" << attack->destinationId();
foreach (int monsterId, attack->sourceIds()) {
Monster* sourceMonster = board()->monster(monsterId);
Monster* destinationMonster = board()->monster(attack->destinationId());
// check attack strength
int attackStrength = 0;
if (sourceMonster->monsterType() == Monster::MonsterTypeStrength) {
attackStrength += 4;
}
attackStrength += sourceMonster->player()->strength();
// check attack speed
int attackSpeed = 0;
if (sourceMonster->monsterType() == Monster::MonsterTypeSpeed) {
attackSpeed += 4;
}
attackSpeed += sourceMonster->player()->speed();
// create pillow
AttackPillow *pillow = new AttackPillow(sourceMonster->player(), sourceMonster, destinationMonster, sourceMonster->split(), attackStrength, attackSpeed, this);
if (pillow->count() == 0) {
pillow->deleteLater();
continue;
}
m_pillowList.insert(pillow->id(), pillow);
m_pillowsModel->addPillow(pillow);
}
}
int GameEngine::ticksPerSecond() const
{
return m_ticksPerSecond;
}
int GameEngine::tickInterval() const
{
return m_tickInterval;
}
PlayerSettings *GameEngine::playerSettings()
{
return m_playerSettings;
}
double GameEngine::strengthStepWidth() const
{
return m_strengthStepWidth;
}
double GameEngine::reproductionStepWidth() const
{
return m_reproductionStepWidth;
}
double GameEngine::defenseStepWidth() const
{
return m_defenseStepWidth;
}
double GameEngine::speedStepWidth() const
{
return m_speedStepWidth;
}
void GameEngine::attackFinished(QString pillowId)
{
AttackPillow *pillow = m_pillowList.take(pillowId);
qDebug() << "Attack" << pillow->sourceMonster()->id() << "->" << pillow->destinationMonster()->id() << "finished";
pillow->destinationMonster()->impact(pillow);
m_pillowsModel->removePillow(pillow);
emit pillowsChanged();
pillow->deleteLater();
}
void GameEngine::resetGameSettings()
{
qDebug() << "Reset game settings...";
m_levels->resetLevelSettings();
m_playerSettings->resetSettings();
}
void GameEngine::startGame(const int &levelId)
{
Level *level = m_levelHash.value(levelId);
m_board->setLevel(level);
foreach (Player *player, m_board->playersList()) {
if (player->playerType() == Player::PlayerTypeAi) {
AiBrain *brain = new AiBrain(m_board, player, m_strengthStepWidth, m_defenseStepWidth, this);
qDebug() << "Create AI for player" << player->id();
m_brains.insert(player, brain);
connect(brain, &AiBrain::startAttack, this, &GameEngine::startAttack);
}
}
qDebug() << "Game: start Level" << levelId;
start();
foreach (AiBrain *brain, m_brains.values()) {
brain->start();
}
// reset engine properties
m_gameOver = false;
m_newHighScore = false;
emit newHighScoreChanged();
m_tunePointEarned = false;
emit tunePointEarnedChanged();
m_totalGameTimeMs = 0;
m_gameTimer.restart();
emit displayGameTimeChanged();
calculateScores();
emit gameStarted();
}
void GameEngine::restartGame()
{
qDebug() << "Game: restart";
int levelId = m_board->level()->levelId();
stopGame();
startGame(levelId);
emit gameRestarted();
}
void GameEngine::stopGame()
{
qDebug() << "Game: stop";
stop();
// clean up brains
foreach (AiBrain *brain, m_brains.values()) {
brain->deleteLater();
}
m_brains.clear();
// stop game timer
m_totalGameTimeMs += m_gameTimer.elapsed();
emit gameTimeChanged();
// clean up existing pillows
m_pillowsModel->clearModel();
foreach (AttackPillow *pillow, m_pillowList.values()) {
pillow->deleteLater();
}
m_pillowList.clear();
// reset the board
m_board->resetBoard();
if (!m_gameOver) {
emit gameStoped();
}
m_gameOver = true;
}
void GameEngine::pauseGame()
{
if (m_gameOver)
return;
// stop AIs
foreach (AiBrain *brain, m_brains.values()) {
brain->stop();
}
// stop GameEngine
stop();
// stop game timer
m_totalGameTimeMs += m_gameTimer.elapsed();
emit gameTimeChanged();
qDebug() << "Game: pause" << m_totalGameTimeMs;
emit gamePaused();
}
void GameEngine::continueGame()
{
// if game over...ignore it
if (m_gameOver)
return;
// start AIs
foreach (AiBrain *brain, m_brains.values()) {
brain->start();
}
// start GameEngine
start();
// continue game timer
m_gameTimer.restart();
emit gameTimeChanged();
qDebug() << "Game: continue";
emit gameContinue();
}
void GameEngine::loadLevels()
{
QDir dir(m_dataDir.path() + "levels");
QStringList levelDirs = dir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::Name);
qDebug() << "searching level data in" << dir.path();
foreach (const QString &levelDir, levelDirs) {
if (!levelDir.startsWith("level")) {
continue;
}
QFileInfo fi(dir.absolutePath() + "/" + levelDir + "/level.json");
if (!fi.exists()) {
qDebug() << "Level directory" << levelDir << "does not contain a level.json file.";
continue;
}
QFile levelFile(fi.absoluteFilePath());
if (!levelFile.open(QFile::ReadOnly)) {
qDebug() << "Cannot open level file for reading:" << fi.absoluteFilePath();
continue;
}
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(levelFile.readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug() << "Cannot parse level file:" << error.errorString();
continue;
}
QVariantMap levelData = jsonDoc.toVariant().toMap();
qDebug() << " -> loading level" << levelData.value("id").toInt() << "...";
Level *level = new Level(this);
level->setName(levelData.value("name").toString());
level->setLevelId(levelData.value("id").toInt());
level->setPlayersVariants(levelData.value("players").toList());
level->setMonstersVariants(levelData.value("monsters").toList());
QSettings settings;
settings.beginGroup(level->name());
level->setTimeStamp(settings.value("timeStamp", 0).toInt());
level->setUnlocked(settings.value("unlocked", false).toBool());
settings.endGroup();
if (level->levelId() == 1) {
level->setUnlocked(true);
}
m_levels->addLevel(level);
m_levelHash.insert(level->levelId(), level);
}
m_levels->sortLevels();
emit levelsChanged();
// now check if there are new levels and make sure the last level is unlocked
foreach (Level *level, m_levels->levels()) {
if (level->timeStamp() != 0) {
// get next level
Level *nextLevel = 0;
nextLevel = m_levelHash.value(level->levelId() + 1);
if (nextLevel) {
QSettings settings;
settings.beginGroup(nextLevel->name());
settings.setValue("unlocked", true);
settings.endGroup();
if (!nextLevel->unlocked()) {
qDebug() << " -> Unlock last level" << nextLevel->levelId();
}
nextLevel->setUnlocked(true);
}
}
}
}
void GameEngine::calculateScores()
{
if (!m_running)
return;
int total = 0;
foreach (Player *player, m_board->playersList()) {
player->setPointCount(0);
foreach (Monster *monster, m_board->monstersList()) {
if (monster->player()->id() == player->id()){
player->addPoints(monster->value());
total += monster->value();
}
}
foreach (AttackPillow *pillow, m_pillowsModel->pillows()) {
if (pillow->player()->id() == player->id()){
player->addPoints(pillow->count());
total += pillow->count();
}
}
}
foreach (Player *player, m_board->playersList()) {
if (player->pointCount() == 0) {
player->setPercentage(0);
}
double percentage = (double)player->pointCount() / total;
player->setPercentage((double)(qRound(percentage * 100) / 100.0));
if (percentage == 1) {
emit gameFinished(player->id());
}
}
}
void GameEngine::initGameEngine()
{
loadLevels();
m_timer->setInterval(1000 / m_ticksPerSecond);
m_timer->setTimerType(Qt::PreciseTimer);
connect(m_timer, &QTimer::timeout, this, &GameEngine::slotTick);
m_totalGameTimeMs = 0;
emit displayGameTimeChanged();
m_displayTimer->setInterval(200);
connect(m_displayTimer, &QTimer::timeout, this, &GameEngine::onDisplayTimerTimeout);
connect(m_board, &Board::startAttack, this, &GameEngine::startAttack);
}
void GameEngine::slotTick()
{
m_board->tick();
calculateScores();
}
void GameEngine::onDisplayTimerTimeout()
{
m_totalGameTimeMs += m_gameTimer.elapsed();
m_gameTimer.restart();
emit displayGameTimeChanged();
}
void GameEngine::onGameOver(const int &winnerId)
{
if (m_gameOver)
return;
if( winnerId == 1) {
qDebug() << "Game Over!! You are the winner!";
} else {
qDebug() << "Game Over!! You lost the game. Player" << winnerId << "won the game.";
}
foreach (AiBrain *brain, m_brains.values()) {
brain->stop();
}
stop();
// stop game timer
m_totalGameTimeMs += m_gameTimer.elapsed();
m_finalTime = m_totalGameTimeMs;
emit gameTimeChanged();
emit displayGameTimeChanged();
if (winnerId == 1) {
if (m_board->level()->timeStamp() > m_finalTime || m_board->level()->timeStamp() == 0) {
// check if we earned a tune point
if (m_board->level()->timeStamp() == 0) {
m_playerSettings->increaseTunePoints();
qDebug() << "Tune points +1";
m_tunePointEarned = true;
emit tunePointEarnedChanged();
}
m_newHighScore = true;
emit newHighScoreChanged();
qDebug() << "New highscore!";
m_board->level()->setTimeStamp(m_finalTime);
QSettings settings;
settings.beginGroup(m_board->level()->name());
settings.setValue("timeStamp", m_finalTime);
settings.setValue("unlocked", true);
settings.endGroup();
Level *nextLevel = 0;
nextLevel = m_levelHash.value(m_board->level()->levelId() + 1);
if (nextLevel) {
settings.beginGroup(nextLevel->name());
settings.setValue("unlocked", true);
settings.endGroup();
if (!nextLevel->unlocked()) {
qDebug() << "Unlock next level" << nextLevel->levelId();
}
nextLevel->setUnlocked(true);
}
} else {
m_newHighScore = false;
emit newHighScoreChanged();
}
}
m_winnerId = winnerId;
emit winnerIdChanged();
m_gameOver = true;
emit gameOver();
}