From e044d272a7d4db1755a0393403733b4639b061f8 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 22 Jan 2023 17:18:49 +0200 Subject: [PATCH] weights and cell generation reimplemented in C++ --- defConfigs.h | 5 +- mapEditorAppState.cpp | 129 +++++++++++++++++++++++++++++++++++++++--- mapEditorAppState.h | 10 +++- 3 files changed, 131 insertions(+), 13 deletions(-) diff --git a/defConfigs.h b/defConfigs.h index da1c38e..10ac7aa 100755 --- a/defConfigs.h +++ b/defConfigs.h @@ -4,6 +4,8 @@ #include #include +#include + #include #include @@ -16,8 +18,9 @@ namespace battleship{ using namespace gameBase; const std::string DEFAULT_TEXTURE = GameManager::getSingleton()->getPath() + "Textures/defaultTexture.jpg"; - const double camPanSpeed = .1; + const double camPanSpeed = .1, cellLength = 14, cellWidth = 14, cellDepth = 7; const int maxNumGroups = 10; + const vb01::u32 IMPASS_NODE_VAL = 65535; const static int numAppStates = 4; const static int numStaticBinds[numAppStates]{6, 0, 5, 18}; diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 3cda6ab..2b6ecd2 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -19,6 +19,7 @@ #include #include +#include #include @@ -49,7 +50,7 @@ namespace battleship{ MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size, bool newMap){ this->newMap = newMap; - this->size = size; + this->mapSize = size; map = Map::getSingleton(); map->load(name, newMap); @@ -108,6 +109,7 @@ namespace battleship{ } } + void MapEditorAppState::MapEditor::createWaterbody(){ Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); mat->addBoolUniform("lightingEnabled", false); @@ -123,7 +125,8 @@ namespace battleship{ node->attachMesh(quad); Map *map = Map::getSingleton(); - map->addTerrainObject(TerrainObject(pos, size, Vector3(14, 7, 14), TerrainObject::RECT_WATERBODY, node, 0, nullptr, nullptr)); + TerrainObject::Type type = TerrainObject::RECT_WATERBODY; + map->addTerrainObject(TerrainObject(pos, size, Vector3(14, 7, 14), type, node, 0, nullptr, nullptr)); toggleSelection(&map->getTerrainObject(map->getNumTerrainObjects() - 1), true); } @@ -177,7 +180,9 @@ namespace battleship{ Node *node = new Node(); node->attachMesh(mesh); - map->addTerrainObject(TerrainObject(Vector3(0, 0, 0), Vector3(size.x, 0, size.y), Vector3(14, 6, 14), TerrainObject::LANDMASS, node, 0, new Cell, new vb01::u32*)); + TerrainObject::Type type = TerrainObject::LANDMASS; + Vector3 pos = Vector3::VEC_ZERO, s = Vector3(size.x, 0, size.y); + map->addTerrainObject(TerrainObject(pos, s, Vector3(14, 6, 14), type, node, 0, nullptr, nullptr)); } void MapEditorAppState::MapEditor::prepareGui(){ @@ -367,16 +372,122 @@ namespace battleship{ weightsGenerated = false; } - void MapEditorAppState::MapEditor::generateWeights(){ - if(weightsGenerated) - deleteWeights(); + void MapEditorAppState::MapEditor::prepareTerrainObject(u32 **weights, Cell *cells, int cellsByDim[3], float height, bool land){ + const int numCells = cellsByDim[0] * cellsByDim[1] * cellsByDim[2]; + Vector3 initPos = -.5 * Vector3(mapSize.x, -height, mapSize.y); + + for(int i = 0; i < numCells; i++){ + int xId = i % cellsByDim[0]; + int yId = int(i / (cellsByDim[0] * cellsByDim[2])); + int zId = (int(i / cellsByDim[0])) % cellsByDim[2]; + cells[i].pos = initPos + Vector3(xId * cellLength, -yId * cellDepth, zId * cellWidth); + } - weightsGenerated = true; + MeshData meshData = map->getTerrainObject(0).node->getChild(0)->getMesh(0)->getMeshBase(); + + for(int i = 0; i < numCells; i++){ + int waterbodyId = -1; + bool impassible = false; + TerrainObject waterbody; + + for(int j = 0; j < 3 * meshData.numTris; j++){ + Vector3 point = Vector3(float(meshData.vertices[j].pos.x), float(meshData.vertices[j].pos.y), float(meshData.vertices[j].pos.z)); + + for(int k = 1; k < map->getNumTerrainObjects(); k++){ + TerrainObject wb = map->getTerrainObject(k); + bool diffX = (abs(point.x - wb.pos.x) < .5 * wb.size.x); + bool diffY = (abs(point.y - wb.pos.z) < .5 * wb.size.z); + bool diffZ = (abs(point.z + wb.pos.y) < .5 * wb.size.y); + + if(diffX && diffZ){ + waterbodyId = k; + break; + } + } + + if(waterbodyId != -1) + waterbody = map->getTerrainObject(waterbodyId); + + bool withinX = (abs(point.x - cells[i].pos.x) < 0.5 * cellLength); + bool withinY = (!land ? (abs(point.y - cells[i].pos.y) < 0.5 * cellDepth) : true); + bool withinZ = (abs(point.z - cells[i].pos.z) < 0.5 * cellWidth); + bool pointWithin = (withinX && withinY && withinZ); + + if(land && pointWithin && (waterbodyId != -1 && point.y < waterbody.pos.z)) + impassible = true; + else if(!land && pointWithin && waterbodyId != -1) + impassible = true; + } + } + + for(int i = 0; i < numCells; i++){ + int xId = i % cellsByDim[0]; + int yId = i / (cellsByDim[0] * cellsByDim[2]); + int zId = (i / cellsByDim[0]) % cellsByDim[2]; + + for(int j = 0; j < numCells; j++){ + bool adjacent = false; + + if(xId == 0 && j - i == 1) + adjacent = true; + else if(xId == cellsByDim[0] - 1 && j - i == -1) + adjacent = true; + else if(0 < xId && xId < cellsByDim[0] - 1 && abs(j - i) == 1) + adjacent = true; + + if(yId == 0 && j - i == cellsByDim[0] * cellsByDim[2]) + adjacent = true; + else if(0 < yId && yId < cellsByDim[1] - 1 && abs(j - i) == cellsByDim[0] * cellsByDim[2]) + adjacent = true; + else if(yId == cellsByDim[1] - 1 && j - i == -(cellsByDim[0] * cellsByDim[2])) + adjacent = true; + + if(zId == 0 && j - i == cellsByDim[0]) + adjacent = true; + else if(zId == cellsByDim[2] - 1 && j - i == -cellsByDim[0]) + adjacent = true; + else if(0 < zId && zId < cellsByDim[2] - 1 && abs(j - i) == cellsByDim[0]) + adjacent = true; + + weights[i][j] = IMPASS_NODE_VAL; + + if(i == j) + weights[i][j] = 0; + else if(adjacent) + weights[i][j] = 1; + } + } + } + + void MapEditorAppState::MapEditor::prepareTerrainObjects(){ + for(int i = 0; i < map->getNumTerrainObjects(); i++){ + TerrainObject obj = map->getTerrainObject(i); + Vector3 size = obj.size; + int cellsByDim[]{ + int(size.x / cellLength), + cellDepth == 0 ? 1 : int(size.y / cellDepth), + int(size.z / cellWidth) + }; + const int numCells = cellsByDim[0] * cellsByDim[1] * cellsByDim[2]; + + Cell *cells = new Cell[numCells]; + + u32 **weights = new u32*[numCells]; + + for(int i = 0; i < numCells; i++) + weights[i] = new u32[numCells]; + + prepareTerrainObject(weights, cells, cellsByDim, (i > 0 ? obj.pos.y : 0), i == 0); + } + } + + void MapEditorAppState::MapEditor::parseMapScript(){ } void MapEditorAppState::MapEditor::exportMap(){ parseLandmass(); - generateWeights(); + prepareTerrainObjects(); + parseMapScript(); } MapEditorAppState::MapEditorAppState(string name, Vector2 size, bool newMap) : AbstractAppState( @@ -501,7 +612,7 @@ namespace battleship{ break; case Bind::GENERATE_WEIGHTS: - mapEditor->generateWeights(); + mapEditor->prepareTerrainObjects(); break; } } diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 9efa0c0..6d28805 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -1,11 +1,14 @@ #ifndef MAP_EDITOR_APP_STATE_H #define MAP_EDITOR_APP_STATE_H +#include "map.h" + #include #include #include +#include #include @@ -15,7 +18,6 @@ namespace vb01{ namespace battleship{ class MapEditor; - class Map; class TerrainObject; class MapEditorAppState : public gameBase::AbstractAppState{ @@ -42,7 +44,7 @@ namespace battleship{ void createWaterbody(); void moveTerrainObject(float); void exportMap(); - void generateWeights(); + void prepareTerrainObjects(); inline TerrainObject* getSelectedTerrainObject(){return selectedTerrainObject;} inline float getGuiThreshold(){return guiThreshold;} inline float getCircleRadius(){return circleRadius;} @@ -60,7 +62,9 @@ namespace battleship{ void prepareTextures(std::string, bool, std::vector&); void toggleSelection(TerrainObject*, bool); void parseLandmass(); + void parseMapScript(); void deleteWeights(); + void prepareTerrainObject(vb01::u32**, Cell*, int[3], float, bool); inline vb01::Texture* getSkyTexture(int i){return skyTextures[i];} inline vb01::Texture* getLandmassTexture(int i){return landmassTextures[i];} @@ -72,7 +76,7 @@ namespace battleship{ const int NUM_SUBDIVS = 100; float circleRadius = MIN_RADIUS, guiThreshold = 200; vb01::Vector3 pushPos = vb01::Vector3::VEC_ZERO; - vb01::Vector2 size; + vb01::Vector2 mapSize; std::vector skyTextures, landmassTextures, waterTextures; UnitListbox *vehicleListbox = nullptr, *structureListbox = nullptr; };