diff --git a/map.cpp b/map.cpp index cd66541..a485d48 100755 --- a/map.cpp +++ b/map.cpp @@ -56,104 +56,14 @@ namespace battleship{ } void Map::loadTerrainObject(int id){ - /* - LuaManager *luaManager = LuaManager::getSingleton(); - string terrainTable = (id == -1 ? "terrain" : "waterBodies"); - vector baseIndices = (id == -1 ? vector{Index(terrainTable)} : vector{Index(terrainTable), Index(id + 1)}); - Vector3 pos = Vector3::VEC_ZERO, size = Vector3::VEC_ZERO; - Index terrInd = Index("terrain"), - waterInd = Index("waterBodies"), - idIndex = Index(id + 1), - posInd = Index("pos"), - sizeInd = Index("size"), - xInd = Index("x"), - yInd = Index("y"), - zInd = Index("z"), - nodeInd = Index("nodes"); - - vector sizeBaseInd; - - if(id == -1){ - sizeBaseInd.push_back(terrInd); - } - else{ - float x = luaManager->getFloatFromTable(mapTable, vector{waterInd, idIndex, posInd, xInd}); - float y = luaManager->getFloatFromTable(mapTable, vector{waterInd, idIndex, posInd, yInd}); - float z = luaManager->getFloatFromTable(mapTable, vector{waterInd, idIndex, posInd, zInd}); - pos = Vector3(x, y, z); - - sizeBaseInd = vector{waterInd, idIndex}; - } - - sizeBaseInd.push_back(sizeInd); - vector indVecX = sizeBaseInd; - vector indVecY = sizeBaseInd; - vector indVecZ = sizeBaseInd; - indVecX.push_back(xInd); - indVecY.push_back(yInd); - indVecZ.push_back(zInd); - float x = luaManager->getFloatFromTable(mapTable, indVecX); - float y = luaManager->getFloatFromTable(mapTable, indVecY); - float z = luaManager->getFloatFromTable(mapTable, indVecZ); - size = Vector3(x, y, z); - - vector baseNodeInd = baseIndices; - baseNodeInd.push_back(nodeInd); - - vector ind = baseNodeInd; - ind.push_back(Index("numCells")); - int numCells = luaManager->getIntFromTable(mapTable, ind); - - u32 **weights = new u32*[numCells]; - Cell *cells = new Cell[numCells]; - - for(int i = 0; i < numCells; i++){ - weights[i] = new u32[numCells]; - - for(int j = 0; j < numCells; j++){ - int wid = i * numCells + j; - ind = baseNodeInd; - ind.push_back(Index("weights")); - ind.push_back(Index(wid + 1)); - u32 w = luaManager->getIntFromTable(mapTable, ind); - weights[i][j] = w; - } - - ind = baseNodeInd; - ind.push_back(Index("impassible")); - ind.push_back(Index(i + 1)); - bool impassible = luaManager->getFloatFromTable(mapTable, ind); - - vector posBaseInd = baseNodeInd; - posBaseInd.push_back(posInd); - posBaseInd.push_back(Index(i + 1)); - - vector indVecX = posBaseInd; - vector indVecY = posBaseInd; - vector indVecZ = posBaseInd; - indVecX.push_back(xInd); - indVecY.push_back(yInd); - indVecZ.push_back(zInd); - float x = luaManager->getFloatFromTable(mapTable, indVecX); - float y = luaManager->getFloatFromTable(mapTable, indVecY); - float z = luaManager->getFloatFromTable(mapTable, indVecZ); - cells[i] = Cell(Vector3(x, y, z), (id == -1), impassible); - } - - Node *node = nullptr; + string texPath = "", basePath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/"; Quad *quad = nullptr; - TerrainObject::Type type; - string basePath = GameManager::getSingleton()->getPath(); - string texPath = basePath; + Node *node = nullptr; - //TODO remove this quick fix for landmass model loading if(id == -1){ - type = TerrainObject::LANDMASS; - - string mapPathSuffix = "Models/Maps/" + mapName + "/"; - texPath += mapPathSuffix; - string terrainFile = basePath + mapPathSuffix + luaManager->getStringFromTable(mapTable, vector{terrInd, Index("model")}); + texPath = basePath + (string)SOL_LUA_STATE[mapTable]["terrain"]["albedo"]; + string terrainFile = basePath + (string)SOL_LUA_STATE[mapTable]["terrain"]["model"]; AssetManager::getSingleton()->load(terrainFile); node = (Model*)((new Model(terrainFile))->getChild(0)); @@ -162,25 +72,21 @@ namespace battleship{ delete par; } else{ - type = TerrainObject::RECT_WATERBODY; - node = new Node(); - quad = new Quad(Vector3(size.x, size.z, 1), true); - node->attachMesh(quad); + sol::table waterBodyTable = SOL_LUA_STATE[mapTable]["waterbodies"][id + 1], posTable = waterBodyTable["pos"]; + texPath = basePath + (string)waterBodyTable["albedo"]; - texPath += "Textures/Water/"; + quad = new Quad(Vector3(waterBodyTable["size"]["x"], waterBodyTable["size"]["z"], 1), true); + Vector3 pos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + node = new Node(pos); + node->attachMesh(quad); } - nodeParent->attachChild(node); - node->setPosition(pos); + terrainNode->attachChild(node); Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); mat->addBoolUniform("texturingEnabled", true); mat->addBoolUniform("lightingEnabled", false); - ind = baseIndices; - ind.push_back(Index("albedoMap")); - texPath += luaManager->getStringFromTable(mapTable, ind); - string fr[]{texPath}; AssetManager::getSingleton()->load(fr[0]); Texture *t = new Texture(fr, 1, false); @@ -191,9 +97,6 @@ namespace battleship{ ((Model*)node)->setMaterial(mat); else quad->setMaterial(mat); - - terrainObjects.push_back(TerrainObject(pos, size, Vector3(cellSize.x, (id == -1 ? 0 : cellSize.y), cellSize.z), type, node, numCells, cells, weights)); - */ } void Map::loadSpawnPoints(){ @@ -206,7 +109,6 @@ namespace battleship{ } } - //TODO simplify the dubplicatory field extraction statements void Map::loadPlayers(){ AssetManager *assetManager = AssetManager::getSingleton(); string path = GameManager::getSingleton()->getPath(); @@ -240,15 +142,58 @@ namespace battleship{ } void Map::preprareScene(){ - terrainNode = new Node(); Root *root = Root::getSingleton(); - root->getRootNode()->attachChild(terrainNode); + Node *rootNode = root->getRootNode(); + string libPath = root->getLibPath(); + terrainNode = new Node(); + rootNode->attachChild(terrainNode); + + cellNode = new Node(); + cellNode->setVisible(false); + rootNode->attachChild(cellNode); + landCellMat = new Material(libPath + "texture"); + landCellMat->addBoolUniform("lightingEnabled", false); + landCellMat->addBoolUniform("texturingEnabled", false); + landCellMat->addVec4Uniform("diffuseColor", Vector4(0, 1, 0, 1)); + waterCellMat = new Material(libPath + "texture"); + waterCellMat->addBoolUniform("lightingEnabled", false); + waterCellMat->addBoolUniform("texturingEnabled", false); + waterCellMat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); Camera *cam = root->getCamera(); cam->setPosition(Vector3(1, 1, 1) * 40); cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); } + void Map::loadCells(){ + int numCells = SOL_LUA_STATE[mapTable]["numCells"]; + sol::table cellsTable = SOL_LUA_STATE[mapTable]["cells"]; + + for(int i = 0; i < numCells; i++){ + sol::table cellTable = cellsTable[i + 1], posTable = cellTable["pos"]; + int numEdges = cellTable["numEdges"]; + vector edges; + + for(int j = 0; j < numEdges; j++){ + sol::table edgeTable = cellTable["edges"][j + 1]; + edges.push_back(Edge(edgeTable["weight"], edgeTable["srcCellId"], edgeTable["destCellId"])); + } + + Vector3 cellPos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + Cell::Type cellType = (Cell::Type)cellTable["type"]; + + Quad *quad = new Quad(Vector3(CELL_SIZE.x, CELL_SIZE.z, 0)); + quad->setWireframe(true); + quad->setMaterial(cellType == Cell::Type::LAND ? landCellMat : waterCellMat); + + Node *node = new Node(cellPos + Vector3::VEC_J * .1); + node->attachMesh(quad); + cellNode->attachChild(node); + + cells.push_back(Cell(cellPos, cellType, edges)); + } + } + void Map::load(string mapName, bool empty) { this->mapName = mapName; @@ -267,6 +212,7 @@ namespace battleship{ loadSpawnPoints(); loadPlayers(); loadSkybox(); + loadCells(); loadTerrainObject(-1); for(int i = 0; i < numWaterbodies; i++) diff --git a/map.h b/map.h index fdbfd39..00a574a 100755 --- a/map.h +++ b/map.h @@ -10,6 +10,7 @@ namespace vb01{ class Model; + class Material; class Node; } @@ -55,7 +56,8 @@ namespace battleship{ inline void addSpawnPoint(vb01::Vector3 sp){spawnPoints.push_back(sp);} private: std::string mapTable = "map"; - vb01::Node *terrainNode = nullptr; + vb01::Node *terrainNode = nullptr, *cellNode = nullptr; + vb01::Material *landCellMat = nullptr, *waterCellMat = nullptr; std::string mapName; vb01::Vector3 CELL_SIZE = vb01::Vector3(7, 7, 7); std::vector spawnPoints; @@ -67,6 +69,7 @@ namespace battleship{ void loadSpawnPoints(); void loadPlayers(); void loadSkybox(); + void loadCells(); void loadTerrainObject(int); }; } diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 15691ce..573c64c 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -492,7 +492,7 @@ namespace battleship{ } void MapEditorAppState::MapEditor::generateMapScript(){ - int numTerrObjs = map->getNodeParent()->getNumChildren(); + int numTerrObjs = 0; string mapScript = "map = {\nnumWaterBodies = " + to_string(numTerrObjs) + ",\n"; mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; mapScript += "numPlayers = " + to_string(map->getNumPlayers()) + ",\n"; @@ -537,12 +537,13 @@ namespace battleship{ mapScript += "}\n"; } - mapScript += "},\ncells = {\n"; vector cells = generateMapCells(); + mapScript += "},\nnumCells = " + to_string(cells.size()) + ",\n"; + mapScript += "cells = {\n"; for(Map::Cell cell : cells){ Vector3 p = cell.pos; - mapScript += "{type = '" + to_string((int)cell.type) + "', pos = {x = " + to_string(p.x) + ", y = " + to_string(p.y) + ", z = " + to_string(p.z) + "}, edges = {"; + mapScript += "{type = " + to_string((int)cell.type) + ", pos = {x = " + to_string(p.x) + ", y = " + to_string(p.y) + ", z = " + to_string(p.z) + "}, numEdges = " + to_string(cell.edges.size()) + ", edges = {"; for(Map::Edge edge : cell.edges) mapScript += "{srcCellId = " + to_string(edge.srcCellId) + ", destCellId = " + to_string(edge.destCellId) + ", weight = " + to_string(edge.weight) + "}, "; @@ -566,7 +567,7 @@ namespace battleship{ } mapScript += "skybox = \"" + skyboxPath + "\",\n"; - //mapScript += "terrain = " + parseTerrainObject(map->getTerrainObject(0)); + mapScript += "terrain = {model = \"" + map->getMapName() + ".xml\", albedo = \"" + map->getMapName() + ".jpg\"},\n"; mapScript += "waterbodies = {"; for(int i = 1; i < numTerrObjs; i++){}