|
|
@@ -101,15 +101,13 @@ struct GameplaySimulation
|
|
|
|
|
|
bool create(RequestedContainerInfo &requestedInfo, std::string file)
|
|
|
{
|
|
|
- player.position.position = {1,1};
|
|
|
+ //player.position.position = {1,1};
|
|
|
|
|
|
bool rez = loadMap(requestedInfo, file, &map, mapSize);
|
|
|
|
|
|
return rez;
|
|
|
}
|
|
|
|
|
|
- Player player;
|
|
|
-
|
|
|
Block *map;
|
|
|
glm::ivec2 mapSize = {100, 100};
|
|
|
|
|
|
@@ -131,7 +129,7 @@ struct GameplaySimulation
|
|
|
int moveDelta = 0;
|
|
|
bool jump = 0;
|
|
|
|
|
|
- bool updateFrame(float deltaTime)
|
|
|
+ bool updateFrame(float deltaTime, Player &player)
|
|
|
{
|
|
|
player.input = moveDelta;
|
|
|
if(jump)player.jump(50);
|
|
|
@@ -212,7 +210,7 @@ struct GameplayRenderer
|
|
|
}
|
|
|
|
|
|
|
|
|
- void update(pika::Input input, pika::WindowState windowState,
|
|
|
+ void update(pika::Input &input, pika::WindowState &windowState,
|
|
|
GameplaySimulation &simulator)
|
|
|
{
|
|
|
{
|
|
|
@@ -232,10 +230,7 @@ struct GameplayRenderer
|
|
|
renderer.currentCamera.zoom = std::min(renderer.currentCamera.zoom, 70.f);
|
|
|
renderer.currentCamera.zoom = std::max(renderer.currentCamera.zoom, 50.f);
|
|
|
|
|
|
- //todo update gl2d this function
|
|
|
- renderer.currentCamera.follow(simulator.player.position.getCenter(), input.deltaTime * 3, 0.0001, 0.2, windowState.w, windowState.h);
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
auto viewRect = renderer.getViewRect();
|
|
|
|
|
|
glm::ivec2 minV;
|
|
|
@@ -260,14 +255,28 @@ struct GameplayRenderer
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- glm::vec4 pos(simulator.player.position.position, 1, 1);
|
|
|
- //pos.y -= 1 / 8.f;
|
|
|
- pos.x -= 1 / 8.f;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ void followPlayer(mario::Player &p, pika::Input &input, pika::WindowState &windowState)
|
|
|
+ {
|
|
|
+ //todo update gl2d this function
|
|
|
+ renderer.currentCamera.follow(p.position.getCenter(), input.deltaTime * 3, 0.0001, 0.2, windowState.w, windowState.h);
|
|
|
|
|
|
- renderer.renderRectangle(pos, {}, {}, marioTexture,
|
|
|
- simulator.player.movingRight ? glm::vec4(0, 1, 1, 0) : glm::vec4(1, 1, 0, 0));
|
|
|
|
|
|
+ }
|
|
|
|
|
|
+ void drawPlayer(mario::Player &p)
|
|
|
+ {
|
|
|
+ glm::vec4 pos(p.position.position, 1, 1);
|
|
|
+ //pos.y -= 1 / 8.f;
|
|
|
+ pos.x -= 1 / 8.f;
|
|
|
+ renderer.renderRectangle(pos, {}, {}, marioTexture,
|
|
|
+ p.movingRight ? glm::vec4(0, 1, 1, 0) : glm::vec4(1, 1, 0, 0));
|
|
|
}
|
|
|
|
|
|
void render()
|
|
|
@@ -285,77 +294,97 @@ struct GameplayRenderer
|
|
|
|
|
|
};
|
|
|
|
|
|
-struct NeuralSimulator
|
|
|
-{
|
|
|
- mario::GameplaySimulation simulator;
|
|
|
+static constexpr int visionSizeX = 6;
|
|
|
+static constexpr int visionSizeY = 9;
|
|
|
|
|
|
- float maxFit = 0;
|
|
|
- float currentPos = 0;
|
|
|
- float killTimer = 0;
|
|
|
+static constexpr int visionTotal = visionSizeX * visionSizeY;
|
|
|
|
|
|
- bool create(RequestedContainerInfo &requestedInfo, std::string_view file)
|
|
|
- {
|
|
|
- bool rez = simulator.create(requestedInfo, std::string(file));
|
|
|
-
|
|
|
- currentPos = simulator.player.position.position.x;
|
|
|
-
|
|
|
- return rez;
|
|
|
- }
|
|
|
-
|
|
|
- static constexpr int visionSizeX = 6;
|
|
|
- static constexpr int visionSizeY = 9;
|
|
|
- char vision[visionSizeX * visionSizeY] = {};
|
|
|
+struct NeuralNetork
|
|
|
+{
|
|
|
+ float weights[3][visionTotal] = {};
|
|
|
|
|
|
- bool updateFrame(float deltaTime)
|
|
|
+ void compute(int &moveDirection, bool &jump, char input[visionTotal])
|
|
|
{
|
|
|
-
|
|
|
- if (simulator.player.position.position.x > maxFit)
|
|
|
- {
|
|
|
- maxFit = simulator.player.position.position.x;
|
|
|
- killTimer = 0;
|
|
|
- }
|
|
|
- else
|
|
|
+ float rezult[3] = {};
|
|
|
+ for (int i = 0; i < 3; i++)
|
|
|
{
|
|
|
- killTimer += deltaTime;
|
|
|
- if (killTimer > 3)
|
|
|
+ for (int j = 0; j < visionTotal; j++)
|
|
|
{
|
|
|
- // return 0;
|
|
|
+ rezult[i] += input[j] * weights[i][j];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //simulator.moveDelta = 1;
|
|
|
- //simulator.jump = 0;
|
|
|
-
|
|
|
- memset(vision, 0, sizeof(vision));
|
|
|
- for (int y = 0; y < visionSizeY; y++)
|
|
|
+ float dir = rezult[2] - rezult[1];
|
|
|
+ if (std::abs(dir) < 1) { moveDirection = 0; }
|
|
|
+ else if(dir > 0)
|
|
|
{
|
|
|
- for (int x = 0; x < visionSizeX; x++)
|
|
|
- {
|
|
|
- auto b = simulator.
|
|
|
- getMapBlockSafe(
|
|
|
- x + simulator.player.position.getCenter().x - 1,
|
|
|
- y + simulator.player.position.getCenter().y - visionSizeY + 4);
|
|
|
-
|
|
|
- vision[x + y * visionSizeX] = b.isCollidable();
|
|
|
- }
|
|
|
+ moveDirection = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ moveDirection = -1;
|
|
|
}
|
|
|
|
|
|
- if (!simulator.updateFrame(deltaTime))
|
|
|
+ if (rezult[0] > 1)
|
|
|
{
|
|
|
- return 0;
|
|
|
+ jump = 1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return 1;
|
|
|
+ jump = 0;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- void cleanup()
|
|
|
+ float getRandomFloat(std::mt19937 &rng, float min, float max)
|
|
|
+ {
|
|
|
+ std::uniform_real_distribution<float> dist(min, max);
|
|
|
+ return dist(rng);
|
|
|
+ }
|
|
|
+
|
|
|
+ int getRandomInt(std::mt19937 &rng, int min, int max)
|
|
|
+ {
|
|
|
+ std::uniform_int_distribution<int> dist(min, max);
|
|
|
+ return dist(rng);
|
|
|
+ }
|
|
|
+
|
|
|
+ void addRandomNeuron(std::mt19937 &rng)
|
|
|
{
|
|
|
- simulator.cleanup();
|
|
|
+ std::vector<glm::ivec2> positions;
|
|
|
+ positions.reserve(visionTotal*3);
|
|
|
+ for (int i = 0; i < 3; i++)
|
|
|
+ for (int j = 0; j < visionTotal; j++)
|
|
|
+ {
|
|
|
+ if (weights[i][j] == 0)
|
|
|
+ {
|
|
|
+ positions.push_back({i,j});
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!positions.empty())
|
|
|
+ {
|
|
|
+ auto index = getRandomInt(rng, 0, positions.size() - 1);
|
|
|
+ weights[positions[index].x][positions[index].y] = getRandomFloat(rng, -1.5, 1.5);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
+struct PlayerSimulation
|
|
|
+{
|
|
|
+ mario::Player p;
|
|
|
+ float maxFit = 0;
|
|
|
+ float killTimer = 0;
|
|
|
+};
|
|
|
+
|
|
|
+bool performNeuralSimulation(PlayerSimulation &p, float deltaTime, mario::GameplaySimulation &simulator, mario::NeuralNetork
|
|
|
+ &network);
|
|
|
+
|
|
|
+void renderNeuralNetwork(gl2d::Renderer2D &renderer, char vision[mario::visionSizeX * mario::visionSizeY],
|
|
|
+ float blockSizePreview, mario::NeuralNetork &network);
|
|
|
+
|
|
|
+void getVision(char vision[visionSizeX * visionSizeY], mario::GameplaySimulation &simulator, PlayerSimulation &p);
|
|
|
+
|
|
|
+
|
|
|
};
|