From d3969c2df734f8021ffd462d8a4273d8e81148c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Champeimont?= Date: Sat, 14 Dec 2013 18:49:26 +0100 Subject: [PATCH] Better AI (now uses snakes) --- project/src/AI.cpp | 108 ++++++++++++------ project/src/AI.h | 8 +- project/src/Game.cpp | 2 +- project/src/Program.cpp | 2 +- project/src/Sound.cpp | 1 + project/{rundir => unused libs}/libFLAC-8.dll | Bin .../{rundir => unused libs}/libmikmod-2.dll | Bin project/{rundir => unused libs}/smpeg.dll | Bin 8 files changed, 82 insertions(+), 39 deletions(-) rename project/{rundir => unused libs}/libFLAC-8.dll (100%) rename project/{rundir => unused libs}/libmikmod-2.dll (100%) rename project/{rundir => unused libs}/smpeg.dll (100%) diff --git a/project/src/AI.cpp b/project/src/AI.cpp index 488099a..f7e524f 100644 --- a/project/src/AI.cpp +++ b/project/src/AI.cpp @@ -27,11 +27,13 @@ #include "Program.h" -AI::AI(int p, Maze *m) : dijkstraQ(m->width * m->height * 4) { +AI::AI(int p, Maze *m, Game *g) : dijkstraQ(m->width * m->height * 4) { player = p; - maze = m; + maze = m; + game = g; homeI = 0; - homeJ = 0; + homeJ = 0; + snakeMode = false; } AI::~AI() { @@ -74,13 +76,36 @@ void AI::updateGraph() { } } - - // Where is the player's house? - for (unsigned k=0; khouses.size(); k++) { - if (maze->houses[k].player == player) { - homeI = maze->houses[k].i; - homeJ = maze->houses[k].j; - } + + if (snakeMode) { + // choose a victim + int victim = -1; + int bestScore = 0; + for (int k=0; kplayers; k++) { + if (k == player) { + // Do not choose myself as the victim + continue; + } + if (victim < 0 || game->scores[k] > bestScore) { + victim = k; + bestScore = game->scores[k]; + } + } + // Where is the victim's house? + for (unsigned k=0; khouses.size(); k++) { + if (maze->houses[k].player == victim) { + homeI = maze->houses[k].i; + homeJ = maze->houses[k].j; + } + } + } else { + // Where is the player's house? + for (unsigned k=0; khouses.size(); k++) { + if (maze->houses[k].player == player) { + homeI = maze->houses[k].i; + homeJ = maze->houses[k].j; + } + } } // We cannot cross walls @@ -169,7 +194,7 @@ void AI::updateGraph() { // Take in account other's players houses: - // A mouse can't "escape" such a cell. + // An animal can't "escape" such a cell. for (unsigned k=0; khouses.size(); k++) { House *house = &maze->houses[k]; if (house->player != player) { @@ -316,35 +341,50 @@ void AI::computeDistances() { // Allow AI to place one arrow -void AI::play() { +void AI::play() { + // Decide between two strategies: gathering mice or sending snakes on others + snakeMode = maze->snakes.size() >= maze->mice.size()/10; + // Make a graph that represents the maze updateGraph(); // Compute distances to house from vertex in the graph computeDistances(); + + vector sortedAnimals; + if (snakeMode) { + sortedAnimals.reserve(maze->snakes.size()); + for (unsigned m=0; msnakes.size(); m++) { + Animal *animal = &maze->snakes[m]; + int i, j; + animal->getFutureCell(&i, &j); + int vertexIndex = vertexIndexFromCoords(i, j, animal->direction); + int dist = getDistance(vertexIndex); + sortedAnimals.push_back(AnimalWithDistance(animal, dist)); + } + } else { + // Sort mice by their distance + sortedAnimals.reserve(maze->mice.size()); + for (unsigned m=0; mmice.size(); m++) { + Animal *animal = &maze->mice[m]; + int i, j; + animal->getFutureCell(&i, &j); + int vertexIndex = vertexIndexFromCoords(i, j, animal->direction); + int dist = getDistance(vertexIndex); + sortedAnimals.push_back(AnimalWithDistance(animal, dist)); + // debug: color mouse by distance + /* + { + float rdist = dist / 50.0f; + if (rdist > 1) rdist = 1; + mouse->color.r = rdist; + mouse->color.g = 1 - rdist; + mouse->color.b = 0; + } + */ + } + } - // Sort mice by their distance - vector sortedAnimals; - sortedAnimals.reserve(maze->mice.size()); - for (unsigned m=0; mmice.size(); m++) { - Mouse *mouse = &maze->mice[m]; - int i, j; - mouse->getFutureCell(&i, &j); - int vertexIndex = vertexIndexFromCoords(i, j, mouse->direction); - int dist = getDistance(vertexIndex); - sortedAnimals.push_back(AnimalWithDistance(mouse, dist)); - - // debug: color mouse by distance - /* - { - float rdist = dist / 50.0f; - if (rdist > 1) rdist = 1; - mouse->color.r = rdist; - mouse->color.g = 1 - rdist; - mouse->color.b = 0; - } - */ - } sort(sortedAnimals.begin(), sortedAnimals.end()); for (unsigned m=0; m graph; - int homeI, homeJ; + int homeI, homeJ; + bool snakeMode; // Dijkstra's algo stuff vector pathToHome; vector dijkstraVisited; AIVertexHeap dijkstraQ; vector sortedAnimals; - AI(int, Maze*); + AI(int, Maze*, Game*); ~AI(); void init(); void play(); diff --git a/project/src/Game.cpp b/project/src/Game.cpp index 541c4df..7bb791b 100644 --- a/project/src/Game.cpp +++ b/project/src/Game.cpp @@ -194,7 +194,7 @@ void Game::run() { for (int p=0; p<4; p++) { if (MenuPlayers::playerControls[p] == 2) { - AIs.push_back(AI(p, &maze)); + AIs.push_back(AI(p, &maze, this)); } } diff --git a/project/src/Program.cpp b/project/src/Program.cpp index 5ad8198..752c458 100644 --- a/project/src/Program.cpp +++ b/project/src/Program.cpp @@ -362,7 +362,7 @@ void Program::run() { sound = new Sound(); sound->init(); - // Set FPS limit depending if battery/AC + // Set FPS limit fps = new FPS(); fps->decideLimitFPS(); diff --git a/project/src/Sound.cpp b/project/src/Sound.cpp index 49f5828..e506741 100644 --- a/project/src/Sound.cpp +++ b/project/src/Sound.cpp @@ -58,6 +58,7 @@ Mix_Music *Sound::loadMusic(string filename) { void Sound::playMusic(int musicIndex) { if (Program::getInstance()->config.music) { if (musicPlaying != musicIndex) { + Mix_VolumeMusic(64); if (Mix_FadeInMusic(music[musicIndex], -1, 1000) != 0) { Functions::error(Mix_GetError()); } diff --git a/project/rundir/libFLAC-8.dll b/project/unused libs/libFLAC-8.dll similarity index 100% rename from project/rundir/libFLAC-8.dll rename to project/unused libs/libFLAC-8.dll diff --git a/project/rundir/libmikmod-2.dll b/project/unused libs/libmikmod-2.dll similarity index 100% rename from project/rundir/libmikmod-2.dll rename to project/unused libs/libmikmod-2.dll diff --git a/project/rundir/smpeg.dll b/project/unused libs/smpeg.dll similarity index 100% rename from project/rundir/smpeg.dll rename to project/unused libs/smpeg.dll