Browse Source

made neural structure

meemknight 2 years ago
parent
commit
6de35ccedf

+ 12 - 0
Pika/core/pikaRuntime/containerManager/containerManager.cpp

@@ -228,6 +228,7 @@ pika::containerId_t pika::ContainerManager::createContainer
 	container.requestedContainerInfo.requestedImguiIds =
 		imguiIDsManager.getImguiIds(containerInformation.containerStaticInfo.requestImguiIds);
 	container.requestedContainerInfo.imguiTotalRequestedIds = containerInformation.containerStaticInfo.requestImguiIds;
+	container.requestedContainerInfo.pushImguiIdForMe = imguiIDsManager.getImguiIds(containerInformation.containerStaticInfo.pushAnImguiIdForMe);
 
 	container.requestedContainerInfo.consoleWindow = consoleWindow;
 #pragma endregion
@@ -436,6 +437,11 @@ void pika::ContainerManager::update(pika::LoadedDll &loadedDll, pika::PikaWindow
 				c.second.requestedContainerInfo.bonusAllocators = &c.second.bonusAllocators;
 
 
+				if (c.second.requestedContainerInfo.pushImguiIdForMe)
+				{
+					ImGui::PushID(c.second.requestedContainerInfo.pushImguiIdForMe);
+				}
+
 				auto t1 = std::chrono::high_resolution_clock::now();
 
 				loadedDll.bindAllocatorDllRealm(&c.second.allocator);
@@ -444,6 +450,12 @@ void pika::ContainerManager::update(pika::LoadedDll &loadedDll, pika::PikaWindow
 
 				auto t2 = std::chrono::high_resolution_clock::now();
 				
+				if (c.second.requestedContainerInfo.pushImguiIdForMe)
+				{
+					ImGui::PopID();
+				}
+
+
 				auto milliseconds = (std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1)).count()/1000.f;
 
 				c.second.frameTimer += milliseconds;

+ 3 - 0
Pika/core/sharedRuntime/baseContainer.h

@@ -44,6 +44,7 @@ struct RequestedContainerInfo
 
 	int requestedImguiIds = 0;
 	int imguiTotalRequestedIds = 0;
+	int pushImguiIdForMe = 0;
 
 	pika::ConsoleWindow *consoleWindow = nullptr;
 
@@ -309,6 +310,7 @@ struct ContainerStaticInfo
 	bool andInputWithWindowHasFocus = 1;
 	bool andInputWithWindowHasFocusLastFrame = 1;
 	bool openOnApplicationStartup = 0;
+	bool pushAnImguiIdForMe = 0;
 
 	bool _internalNotImplemented = 0;
 
@@ -325,6 +327,7 @@ struct ContainerStaticInfo
 			this->useDefaultAllocator == other.useDefaultAllocator &&
 			this->andInputWithWindowHasFocus == other.andInputWithWindowHasFocus &&
 			this->andInputWithWindowHasFocusLastFrame == other.andInputWithWindowHasFocusLastFrame &&
+			this->pushAnImguiIdForMe == other.pushAnImguiIdForMe &&
 			this->openOnApplicationStartup == other.openOnApplicationStartup;
 	}
 

BIN
Pika/engineResources/engineSaves/windowPos1.bin


BIN
Pika/engineResources/engineSaves/windowPos2.bin


+ 10 - 8
Pika/gameplay/containers.h

@@ -17,18 +17,19 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 #include "containers/threedtest.h"
 #include "containers/mario/mario.h"
 #include "containers/mario/marioEditor.h"
+#include "containers/mario/marioNeuralVizualizer.h"
 #include "pluggins/immageviewer.h"
 #include "pluggins/threeDEditor.h"
 #include "pluggins/pikatextEditor.h"
-#include "containers/minecraftDungeons/mcDungeonsEditor.h"
-#include "containers/minecraftDungeons/mcDungeonsgameplay.h"
-#include "containers/minecraftDungeons/mcDungeonsMenu.h"
+//#include "containers/minecraftDungeons/mcDungeonsEditor.h"
+//#include "containers/minecraftDungeons/mcDungeonsgameplay.h"
+//#include "containers/minecraftDungeons/mcDungeonsMenu.h"
 
 #if PIKA_PRODUCTION == 1
 
 #define PIKA_ALL_CONTAINERS() \
-	PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
-	PIKA_DECLARE_CONTAINER(McDungeonsMenu) 
+	//PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
+	//PIKA_DECLARE_CONTAINER(McDungeonsMenu) 
 
 #else
 
@@ -40,8 +41,9 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 	PIKA_DECLARE_CONTAINER(PikaTextEditor) \
 	PIKA_DECLARE_CONTAINER(Mario) \
 	PIKA_DECLARE_CONTAINER(MarioEditor) \
-	PIKA_DECLARE_CONTAINER(McDungeonsEditor) \
-	PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
-	PIKA_DECLARE_CONTAINER(McDungeonsMenu) 
+	PIKA_DECLARE_CONTAINER(MarioNeuralVizualizer)// \
+	//PIKA_DECLARE_CONTAINER(McDungeonsEditor) \
+	//PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
+	//PIKA_DECLARE_CONTAINER(McDungeonsMenu) 
 
 #endif

+ 10 - 13
Pika/gameplay/containers/mario/mario.cpp

@@ -182,36 +182,32 @@ void Player::updatePhisics(float deltaTime)
 
 }
 
-void Player::checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
+void Player::checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, glm::ivec2 mapSize, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
 {
 	glm::vec2 delta = pos - lastPos;
 	const float BLOCK_SIZE = 1.f;
 
 	glm::vec2 &dimensions = position.size;
 
-	glm::ivec2 mapSize = {100,100};
-
 	if (
 		(pos.y < -dimensions.y)
 		|| (pos.x < -dimensions.x)
-		|| (pos.y > mapSize.x * BLOCK_SIZE)
-		|| (pos.x > mapSize.y * BLOCK_SIZE)
+		|| (pos.y > mapSize.y * BLOCK_SIZE)
+		|| (pos.x > mapSize.x * BLOCK_SIZE)
 		)
 	{
 		return;
 	}
 
-	glm::vec2 newPos = performCollision(map, {pos.x, lastPos.y}, {dimensions.x, dimensions.y}, {delta.x, 0},
+	glm::vec2 newPos = performCollision(map, mapSize, {pos.x, lastPos.y}, {dimensions.x, dimensions.y}, {delta.x, 0},
 		upTouch, downTouch, leftTouch, rightTouch);
-	pos = performCollision(map, {newPos.x, pos.y}, {dimensions.x, dimensions.y}, {0, delta.y},
+	pos = performCollision(map, mapSize, {newPos.x, pos.y}, {dimensions.x, dimensions.y}, {0, delta.y},
 		upTouch, downTouch, leftTouch, rightTouch);
 
 }
 
-glm::vec2 Player::performCollision(Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
+glm::vec2 Player::performCollision(Block *map, glm::ivec2 mapSize, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
 {
-	glm::ivec2 mapSize = {100,100};
-
 	int minX = 0;
 	int minY = 0;
 	int maxX = mapSize.x;
@@ -285,10 +281,8 @@ end:
 
 }
 
-void Player::resolveConstrains(Block *map)
+void Player::resolveConstrains(Block *map, glm::ivec2 mapSize)
 {
-	glm::ivec2 mapSize = {100,100};
-
 	bool upTouch = 0;
 	bool downTouch = 0;
 	bool leftTouch = 0;
@@ -304,6 +298,7 @@ void Player::resolveConstrains(Block *map)
 		checkCollisionBrute(pos,
 			lastPos,
 			map,
+			mapSize,
 			upTouch,
 			downTouch,
 			leftTouch,
@@ -324,6 +319,7 @@ void Player::resolveConstrains(Block *map)
 			checkCollisionBrute(newPos,
 				lastPos,
 				map,
+				mapSize,
 				upTouch,
 				downTouch,
 				leftTouch,
@@ -340,6 +336,7 @@ void Player::resolveConstrains(Block *map)
 		checkCollisionBrute(pos,
 			lastPos,
 			map,
+			mapSize,
 			upTouch,
 			downTouch,
 			leftTouch,

+ 12 - 1
Pika/gameplay/containers/mario/mario.h

@@ -85,16 +85,27 @@ struct Mario: public Container
 				simulator.jump = false;
 			}
 
-			simulator.updateFrame(input.deltaTime);
+			if (!simulator.updateFrame(input.deltaTime))
+			{
+				simulator.player.position.position = {1,1};
+				simulator.player.lastPos = {1,1};
+			}
 
 		
 		}
 		
 		renderer.update(input, windowState, simulator);
+		renderer.render();
 
 		return true;
 	}
 
+	void destruct() override
+	{
+		renderer.cleanup();
+		simulator.cleanup();
+	}
+
 };
 
 //todo flag to clear screen from engine

+ 105 - 7
Pika/gameplay/containers/mario/marioCommon.h

@@ -81,12 +81,12 @@ struct Player
 	void updatePhisics(float deltaTime);
 
 
-	void resolveConstrains(Block *map);
+	void resolveConstrains(Block *map, glm::ivec2 mapSize);
 
 	void checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos,
-		Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch);
+		Block *map, glm::ivec2 mapSize, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch);
 
-	glm::vec2 Player::performCollision(Block *map, glm::vec2 pos, glm::vec2 size,
+	glm::vec2 Player::performCollision(Block *map, glm::ivec2 mapSize, glm::vec2 pos, glm::vec2 size,
 		glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch);
 
 	int input = 0;
@@ -118,12 +118,20 @@ struct GameplaySimulation
 		return map[x + y * mapSize.x];
 	}
 
+	Block getMapBlockSafe(int x, int y)
+	{
+		if (x < 0 || y < 0 || x >= mapSize.x || y >= mapSize.y)
+		{
+			return {27};
+		}
+
+		return map[x + y * mapSize.x];
+	}
 
 	int moveDelta = 0;
 	bool jump = 0;
 
-	
-	void updateFrame(float deltaTime)
+	bool updateFrame(float deltaTime)
 	{
 		player.input = moveDelta;
 		if(jump)player.jump(50);
@@ -137,7 +145,7 @@ struct GameplaySimulation
 			player.updatePhisics(deltaTime);
 
 			player.grounded = false;
-			player.resolveConstrains(map);
+			player.resolveConstrains(map, mapSize);
 
 			//player.playerAnimation.grounded = i.second.grounded;
 
@@ -146,9 +154,19 @@ struct GameplaySimulation
 			//player.playerAnimation.update(input.deltaTime);
 		}
 
+		//check fall out
+		if (player.position.getTopLeftCorner().y > mapSize.y)
+		{
+			return 0;
+		}
 
+		return true;
 	}
 
+	void cleanup()
+	{
+		delete[] map;
+	}
 
 };
 
@@ -183,7 +201,6 @@ struct GameplayRenderer
 		//todo push pop or sthing
 		pika::memory::setGlobalAllocatorToStandard();
 		marioTexture.loadFromFile(PIKA_RESOURCES_PATH "/mario/mario.png", true, false);
-		marioTexture.loadFromFile(PIKA_RESOURCES_PATH "/mario/mario.png", true, false);
 		pika::memory::setGlobalAllocator(requestedInfo.mainAllocator);
 
 
@@ -251,12 +268,93 @@ struct GameplayRenderer
 			simulator.player.movingRight ? glm::vec4(0, 1, 1, 0) : glm::vec4(1, 1, 0, 0));
 
 
+	}
+
+	void render()
+	{
 		renderer.flush();
 
+	}
 
+	void cleanup()
+	{
+		renderer.clear();
+		marioTexture.cleanup();
+		tiles.cleanup();
 	}
 
+};
+
+struct NeuralSimulator
+{
+	mario::GameplaySimulation simulator;
+
+	float maxFit = 0;
+	float currentPos = 0;
+	float killTimer = 0;
+
+	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] = {};
+
+	bool updateFrame(float deltaTime)
+	{
+
+		if (simulator.player.position.position.x > maxFit)
+		{
+			maxFit = simulator.player.position.position.x;
+			killTimer = 0;
+		}
+		else
+		{
+			killTimer += deltaTime;
+			if (killTimer > 3)
+			{
+			//	return 0;
+			}
+		}
+
+		//simulator.moveDelta = 1;
+		//simulator.jump = 0;
+
+		memset(vision, 0, sizeof(vision));
+		for (int y = 0; y < visionSizeY; y++)
+		{
+			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();
+			}
+		}
+
+		if (!simulator.updateFrame(deltaTime))
+		{
+			return 0;
+		}
+		else
+		{
+			return 1;
+		}
+
+	}
+
+	void cleanup()
+	{
+		simulator.cleanup();
+	}
 
 };
 

+ 39 - 1
Pika/gameplay/containers/mario/marioEditor.h

@@ -17,6 +17,9 @@ struct MarioEditor: public Container
 	gl2d::TextureAtlasPadding atlas;
 	glm::ivec2 mapSize = {100, 100};
 
+	glm::ivec2 mapSizeEditor = {100, 100};
+
+
 	char path[257] = {};
 
 	glm::vec2 pos = {};
@@ -39,6 +42,7 @@ struct MarioEditor: public Container
 
 		info.requestImguiFbo = true;
 		info.requestImguiIds = 1;
+		info.pushAnImguiIdForMe = true;
 
 		info.extensionsSuported = {".mario"};
 
@@ -61,6 +65,7 @@ struct MarioEditor: public Container
 		atlas = gl2d::TextureAtlasPadding(8, 10, 8*8, 8*10);
 		
 		bool rez = mario::loadMap(requestedInfo, commandLineArgument.to_string(), &map, mapSize);
+		mapSizeEditor = mapSize;
 
 		if (rez)
 		{
@@ -175,6 +180,10 @@ struct MarioEditor: public Container
 
 		}
 
+		if (input.buttons[pika::Button::F].released())
+		{
+			flip = !flip;
+		}
 
 		ImGui::Begin("Block picker");
 		{
@@ -186,6 +195,29 @@ struct MarioEditor: public Container
 			ImGui::Checkbox("Flip", &flip);
 			ImGui::Text("MousePos: %d, %d", blockPosition.x, blockPosition.y);
 
+			ImGui::InputInt2("Map size", &mapSizeEditor[0]); ImGui::SameLine();
+
+			if (ImGui::Button("Set size"))
+			{
+				mario::Block *newMap = 0;
+				newMap = new mario::Block[mapSizeEditor.x * mapSizeEditor.y];
+				mario::Block d{27,0};
+				memset(newMap, *(int *)(&d), mapSizeEditor.x *mapSizeEditor.y);
+
+				for (int y = 0; y < std::min(mapSize.y, mapSizeEditor.y); y++)
+				{
+					for (int x = 0; x < std::min(mapSize.x, mapSizeEditor.x); x++)
+					{
+						newMap[x + y * mapSizeEditor.x] = map[x + y * mapSize.x];
+					}
+				}
+				
+				delete[] map;
+				map = newMap;
+				mapSize = mapSizeEditor;
+			}
+
+
 			ImGui::InputText("Save file", path, sizeof(path));
 			
 			if (ImGui::Button("save"))
@@ -288,7 +320,6 @@ struct MarioEditor: public Container
 
 		}
 		ImGui::End();
-
 	
 
 		if (input.hasFocus && input.lMouse.held() && blockPosition.x >= 0)
@@ -315,6 +346,13 @@ struct MarioEditor: public Container
 		return true;
 	}
 
+	void destruct() override
+	{
+		tiles.cleanup();
+		renderer.clear();
+		delete[] map;
+	}
+
 };
 
 //todo flag to clear screen from engine

+ 163 - 0
Pika/gameplay/containers/mario/marioNeuralVizualizer.h

@@ -0,0 +1,163 @@
+#pragma once
+
+#include <gl2d/gl2d.h>
+#include <imgui.h>
+#include <baseContainer.h>
+#include <shortcutApi/shortcutApi.h>
+#include <pikaSizes.h>
+#include "marioCommon.h"
+#include <fileChanged.h>
+
+
+
+struct MarioNeuralVizualizer: public Container
+{
+
+	mario::GameplayRenderer renderer;
+	mario::NeuralSimulator simulator;
+	//gl2d::FrameBuffer visionFBO;
+
+	//todo user can request imgui ids; shortcut manager context; allocators
+	static ContainerStaticInfo containerInfo()
+	{
+		ContainerStaticInfo info = {};
+		info.defaultHeapMemorySize = pika::MB(10);
+
+		info.extensionsSuported = {".mario"};
+
+		info.requestImguiFbo = true;
+		info.pushAnImguiIdForMe = true;
+
+		return info;
+	}
+
+	std::string mapFile = PIKA_RESOURCES_PATH "/mario/map1.mario";
+
+	const int blockSizePreview = 50;
+
+	bool create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument)
+	{
+		if (commandLineArgument.size() != 0)
+		{
+			mapFile = commandLineArgument.to_string();
+		}
+
+		bool rez = simulator.create(requestedInfo, mapFile);
+
+		rez &= renderer.init(requestedInfo);
+
+		//visionFBO.create(blockSizePreview * mario::NeuralSimulator::visionSizeX, blockSizePreview * mario::NeuralSimulator::visionSizeY);
+
+		return rez;
+	}
+
+	float getFract(float v)
+	{
+		return v - (int)v;
+	}
+
+	bool update(pika::Input input, pika::WindowState windowState,
+		RequestedContainerInfo &requestedInfo)
+	{
+
+		//simulator.moveDelta = 0;
+		//simulator.jump = false;
+
+		if (!simulator.updateFrame(input.deltaTime))
+		{
+			return false;
+		}
+
+		renderer.update(input, windowState, simulator.simulator);
+
+
+		{
+			int delta = 0;
+
+			if (input.hasFocus)
+			{
+				if (input.buttons[pika::Button::A].held())
+				{
+					delta -= 1;
+				}
+				if (input.buttons[pika::Button::D].held())
+				{
+					delta += 1;
+				}
+
+			}
+
+			simulator.simulator.moveDelta = delta;
+
+			if (input.buttons[pika::Button::Space].pressed())
+			{
+				simulator.simulator.jump = true;
+			}
+			else
+			{
+				simulator.simulator.jump = false;
+			}
+
+		}
+
+		//visionFBO.clear();
+		renderer.renderer.pushCamera();
+		renderer.renderer.renderRectangle(
+			glm::vec4(0, 0, mario::NeuralSimulator::visionSizeX * (float)blockSizePreview, mario::NeuralSimulator::visionSizeY * (float)blockSizePreview)
+			, {0.5,0.5,0.5,0.5});
+		for (int y = 0; y < mario::NeuralSimulator::visionSizeY; y++)
+		{
+			for (int x = 0; x < mario::NeuralSimulator::visionSizeX; x++)
+			{
+				auto b = simulator.vision[x + y * mario::NeuralSimulator::visionSizeX];
+				if (b == 1)
+				{
+					renderer.renderer.renderRectangle(glm::vec4(x, y, 0.95, 0.95) * (float)blockSizePreview, {0,1,0,0.5});
+				}
+				//else
+				//{
+				//	renderer.renderer.renderRectangle(glm::vec4(x, y, 0.95, 0.95) * (float)blockSizePreview, {0.5,0.5,0.5,0.5});
+				//}
+			}
+		}
+		renderer.renderer.renderRectangle(
+			glm::vec4(
+			1 , 
+			simulator.visionSizeY - 4,
+			PLAYER_SIZE) *
+			(float)blockSizePreview, {0,0,1,0.5});
+
+		renderer.renderer.popCamera();
+		//renderer.renderer.flushFBO(visionFBO);
+		
+
+		//renderer.renderer.renderRectangle({10,10, blockSizePreview * mario::NeuralSimulator::visionSizeX,blockSizePreview * mario::NeuralSimulator::visionSizeY},
+		//	{}, 0, visionFBO.texture);
+
+
+
+		glBindFramebuffer(GL_FRAMEBUFFER, requestedInfo.requestedFBO.fbo);
+		renderer.render();
+
+		//ImGui::Begin("Neural vision");
+		//{
+		//
+		//
+		//
+		//}
+
+
+		return true;
+	}
+
+	void destruct() override
+	{
+		renderer.cleanup();
+		simulator.cleanup();
+		//visionFBO.cleanup();
+	}
+};
+
+//todo flag to clear screen from engine
+//todo error popup
+//todo error popup disable in release

+ 11 - 2
Pika/resources/logs.txt

@@ -1,2 +1,11 @@
-#2023-04-25 10:00:19: Created container: Mario
-#2023-04-25 10:00:22: Destroyed continer: Mario #1
+#2023-04-25 12:54:47: Created container: MarioNeuralVizualizer
+#2023-04-25 12:55:04: Reloaded dll
+#2023-04-25 13:01:24: Reloaded dll
+#2023-04-25 13:02:53: Reloaded dll
+#2023-04-25 13:03:46: Reloaded dll
+#2023-04-25 13:04:30: Reloaded dll
+#2023-04-25 13:04:53: Reloaded dll
+#2023-04-25 13:05:27: Reloaded dll
+#2023-04-25 13:09:04: Reloaded dll
+#2023-04-25 13:09:48: Reloaded dll
+#2023-04-25 13:10:16: Destroyed continer: MarioNeuralVizualizer #1

BIN
Pika/resources/mario/test1.mario