Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
AI Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzun committed Jul 10, 2018
1 parent bd15b5b commit a67ba5b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
100 changes: 92 additions & 8 deletions source/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ SOFTWARE.
#include "Physics/Physics.h"
#include "Sound/Sound.h"

#ifndef AI_DISABLED
#include "AI/AI.h"
#endif

Game::Game(View *GraphicsView, class QSettings *cfg, class QSettings *settings, int windowWidth, int windowHeight)
: graphicsView(GraphicsView), screenWidth(windowWidth), screenHeight(windowHeight), config(cfg), registry(settings)
{
Expand All @@ -46,6 +50,11 @@ Game::Game(View *GraphicsView, class QSettings *cfg, class QSettings *settings,
gameStarted = 0;
gameActuallyStarted = 0;

birdClosestPipe = 1;

aiPlays = false;
ai = nullptr;

scaleFactor = GAME_DEFAULT_SCALEFACTOR;
soundEnabled = GAME_DEFAULT_SOUND_ENABLED;

Expand All @@ -60,11 +69,11 @@ Game::Game(View *GraphicsView, class QSettings *cfg, class QSettings *settings,
scene = new Scene(this, QRectF(0, 0, screenWidth, screenHeight));

physics = new Physics(this, physicsTickRate, physicsComplexAnalysis, true, physicsSpeedFactor, physicsDisableCollisionDetection);

}

Game::~Game()
{
AIDisable();
delete physics;
delete scene;

Expand All @@ -73,7 +82,6 @@ Game::~Game()
delete sound_point;
delete sound_swooshing;
delete sound_wing;

}

void Game::loadConfiguration()
Expand All @@ -93,6 +101,18 @@ void Game::loadConfiguration()
physicsSpeedFactor = config->value(CONFIG_PHYSICS_SPEEDFACTOR, PHYSICS_DEFAULT_SPEEDFACTOR).toReal();
physicsDisableCollisionDetection = config->value(CONFIG_PHYSICS_DISABLECOLLISIONDETECTION, PHYSICS_DEFAULT_DISABLECOLLISIONDETECTION).toBool();
config->endGroup();

config->beginGroup(CONFIG_AI);
aiNeuronCount = config->value(CONFIG_AI_NEURONCOUNT, AI_DEFAULT_NEURONCOUNT).toInt();
aiBatchSize = config->value(CONFIG_AI_BATCHSIZE, AI_DEFAULT_BATCHSIZE).toInt();
aiEpochs = config->value(CONFIG_AI_EPOCHS, AI_DEFAULT_EPOCHS).toInt();
// aiRealtimeLearn = config->value(CONFIG_AI_REALTIMELEARNING, AI_DEFAULT_RealtimeLearning).toBool();
aiRealtimeLearn = AI_DEFAULT_RealtimeLearning;
aiUpdateInterval = config->value(CONFIG_AI_UPDATEINTERVAL, AI_DEFAULT_UPDATEINTERVAL).toInt();
// aiSelfTrain = config->value(CONFIG_AI_SELFTRAIN, AI_DEFAULT_SELFTRAIN).toBool();
aiSelfTrain = AI_DEFAULT_SELFTRAIN; // Not implemented yet
aiClickThreshold = config->value(CONFIG_AI_CLICKTHRESHOLD, AI_DEFAULT_CLICKTHRESHOLD).toFloat();
config->endGroup();
}

int Game::getScore()
Expand Down Expand Up @@ -132,12 +152,17 @@ void Game::clickEvent()
if(physics)
physics->switchOnlyGroundMove();

gameStarted = 1;
gameStarted = true;
}

if(scene->isGroupVisible(GROUP_NEWROUND))
scene->fadeGroup(GROUP_NEWROUND, false, 5);

#ifndef AI_DISABLED
if(ai)
ai->birdTriggered();
#endif

scene->bird->rise();

sound_wing->playIfEnabled();
Expand All @@ -163,11 +188,59 @@ void Game::soundEnable()
registry->setValue(CONFIG_SOUNDENABLED, soundEnabled);
}

bool Game::isAIEnabled()
{
if(ai == nullptr)
return false;
else
return true;
}

void Game::AIEnable()
{
if(!isAIEnabled())
{
#ifndef AI_DISABLED
ai = new AI(this, aiNeuronCount, aiBatchSize, aiEpochs, aiUpdateInterval, aiClickThreshold);

for(int k = 0; k < GROUP_MAX_ITEM_COUNT; k++) // This method should change in future!
{
if(scene->group_item[GROUP_ROUNDEND][k] == nullptr)
{
scene->group_item[GROUP_ROUNDEND][k] = scene->item_button_AIPlay;
scene->group_item[GROUP_ROUNDEND][k + 1] = nullptr;
break;
}
}
#endif
}
}

void Game::AIDisable()
{
#ifndef AI_DISABLED
if(isAIEnabled())
{
delete ai;
ai = nullptr;
}
#endif
}

void Game::prepareNewRound()
{
scene->flash(Qt::black, 500, QEasingCurve::Linear);

QTimer::singleShot(550, [this]() {
#ifndef AI_DISABLED
if(ai)
{
if(!ai->trainingFinished)
ai->stopTrain();
ai->clearData();
}
#endif

int random1 = Physics::randInt(0, 2);
int random2 = Physics::randInt(0, 1);

Expand Down Expand Up @@ -196,7 +269,7 @@ void Game::prepareNewRound()
score = -1;
updateScore();

gameStarted = 0;
gameStarted = false;

scene->bird->setPos(scene->bird->boundingRect().width() * 2.75, POS_Y_LOGO(screenHeight) + QPixmap(IMG_BIRD_YELLOW_UP).height() * 5);

Expand All @@ -213,8 +286,8 @@ void Game::prepareFirstGame()
{
scene->bird->startOscillate();

gameFinished = 0;
gameActuallyStarted = 1;
gameFinished = false;
gameActuallyStarted = true;
}


Expand All @@ -236,8 +309,19 @@ void Game::gameOver()

scene->gameOver(score, scoreRecord);

gameFinished = 1;
gameActuallyStarted = 0;
gameFinished = true;
gameActuallyStarted = false;
birdClosestPipe = 1;

#ifndef AI_DISABLED
if(ai)
{
aiPlays = false;
QTimer::singleShot(500, [this]() {
ai->train();
});
}
#endif
}


Expand Down
16 changes: 16 additions & 0 deletions source/Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ class Game
void prepareNewRound();
void soundEnable();
void soundDisable();
void AIEnable();
void AIDisable();

bool isGameFinished();
bool isGameStarted();
bool isGameActuallyStarted();
bool isSoundEnabled();
bool isAIEnabled();

int getScore();
int getScoreRecord();
int getScreenWidth();
int getScreenHeight();

int birdClosestPipe; // Move from here!

qreal getScaleFactor();

Sound *sound_die;
Expand All @@ -60,6 +65,8 @@ class Game
class Scene *scene;
class View *graphicsView;

bool aiPlays;

private:
bool soundEnabled;
bool gameFinished, gameStarted;
Expand All @@ -71,6 +78,14 @@ class Game
int screenWidth, screenHeight;
int physicsTickRate;

int aiNeuronCount;
int aiBatchSize;
int aiEpochs;
int aiUpdateInterval;
bool aiRealtimeLearn;
bool aiSelfTrain;
float aiClickThreshold;

qreal physicsSpeedFactor;
qreal scaleFactor;

Expand All @@ -82,6 +97,7 @@ class Game

class Physics *physics;

class AI *ai;
};

#endif // GAME_H

0 comments on commit a67ba5b

Please sign in to comment.