From 84d9333a4ac4b9d60dc9b525b8006456d6f614dc Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Tue, 1 Sep 2015 19:47:58 +0200 Subject: Fixed memory leak --- src/brain/neural_link.cpp | 4 ++-- src/game_state.cpp | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/brain/neural_link.cpp b/src/brain/neural_link.cpp index f8d2b29..c61feb4 100644 --- a/src/brain/neural_link.cpp +++ b/src/brain/neural_link.cpp @@ -1,11 +1,11 @@ #include "brain/neural_link.h" -NeuralLink::NeuralLink(std::shared_ptr input, double weight) +NeuralLink::NeuralLink(std::weak_ptr input, double weight) :_input(input), _weight(weight) { } double NeuralLink::weightedActivation() const { - return _input->activation()*_weight; + return _input.lock()->activation()*_weight; } diff --git a/src/game_state.cpp b/src/game_state.cpp index eb02005..c6e5d70 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -104,7 +104,7 @@ void GameState::logState() const } } -int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int resultIfRight) +int getPyramidOffset(int bottomWidth, int maxWidth, int x, int y, int resultIfLeft, int resultIfRight) { int currentRowWidth = bottomWidth; int currentRowOffset = 0; @@ -112,6 +112,10 @@ int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int result { currentRowOffset += currentRowWidth; currentRowWidth += 2; + if (currentRowWidth > maxWidth) + { + currentRowWidth = maxWidth; + } } if (x > currentRowWidth/2) @@ -136,15 +140,15 @@ int getRectangularOffset(int width, int x, int y) std::vector GameState::toBitArray() const { - std::vector result(172); + std::vector result(102); - int alienOffset = 0; //Aliens are 0 to 100 - int bulletOffset = 101; //Bullets are 101 to 135 - int shieldOffset = 136; //Shields are 136 to 150 - int missileOffset = 151; //Missile info is 151 to 152 - int positionOffset = 153; //Position is 153 to 168 - int existingBuildingsOffset = 169; //Existing buildings is 169 to 170 - int canBuildOffset = 171; //Can build here is 171 + int alienOffset = 0; //Aliens are 0 to 70 + int bulletOffset = 71; //Bullets are 71 to 90 + int shieldOffset = 91; //Shields are 91 to 93 + int missileOffset = 94; //Missile info is 94 to 95 + int positionOffset = 96; //Position is 96 to 98 + int existingBuildingsOffset = 99; //Existing buildings is 99 to 100 + int canBuildOffset = 101; //Can build here is 101 int playerX = GAME_WIDTH/2; int playerY = GAME_AREA_LINES-3; @@ -161,27 +165,27 @@ std::vector GameState::toBitArray() const continue; } - result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true; + result.at(alienOffset + getPyramidOffset(3, 9, alien.x()-playerX, playerY-1-alien.y(), 69, 70)) = true; } for (auto const& bullet : _bullets) { - if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3) + if (bullet.y() >= playerY || bullet.y() < playerY-4 || bullet.x() > playerX+2 || bullet.x() < playerX-2) { continue; } - result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true; + result.at(bulletOffset + getRectangularOffset(5, bullet.x()-playerX, playerY-1-bullet.y())) = true; } for (auto const& shield : _shields) { - if (shield.y() < playerY-3 || shield.x() < playerX-2 || shield.x() > playerX+2) + if (shield.y() < playerY-3 || shield.x() < playerX-1 || shield.x() > playerX+1) { continue; } - result.at(shieldOffset + getRectangularOffset(5, shield.x()-playerX, playerY-1-shield.y())) = true; + result.at(shieldOffset + shield.x()-playerX+1) = true; } if (_missiles.size() > 0) @@ -195,7 +199,18 @@ std::vector GameState::toBitArray() const if (_playerSpaceship) { - result.at(positionOffset + playerX-2) = true; + if (playerX <= GAME_WIDTH/3) + { + result.at(positionOffset) = true; + } + else if (playerX >= GAME_WIDTH*2/3) + { + result.at(positionOffset+2) = true; + } + else + { + result.at(positionOffset+1) = true; + } } result.at(canBuildOffset) = true; -- cgit v1.2.3