optimized cell selection for blocking

This commit is contained in:
devZoGok
2025-06-04 16:59:03 +03:00
parent b8051f24df
commit 1238b3741c
2 changed files with 28 additions and 13 deletions
+27 -13
View File
@@ -382,26 +382,39 @@ namespace battleship{
return spawnPointsTbl.size();
}
//TODO improve for underwater cells
vector<int> Map::getSurroundingCells(Vector3 p, int numRings){
int numHorCells = int(mapSize.x / CELL_SIZE.x);
int numVertCells = int(mapSize.z / CELL_SIZE.z);
int horId = int((.5 * mapSize.x + p.x) / CELL_SIZE.x);
int vertId = int((.5 * mapSize.z + p.z) / CELL_SIZE.z);
vector<int> cellIds;
for(int i = vertId - numRings; i <= vertId + numRings; i++)
for(int j = horId - numRings; j <= horId + numRings; j++)
cellIds.push_back(numHorCells * i + j);
return cellIds;
}
void Map::blockCells(Unit *unit){
for(Cell &cell : cells){
if(cell.blockedBy && cell.blockedBy != unit) continue;
vector<int> surroundingCellIds = getSurroundingCells(unit->getPos(), 0);
Vector3 cellDir = (cell.pos - unit->getPos()), dirVec = unit->getDirVec(), leftVec = unit->getLeftVec();
float forwAngle = std::min(dirVec.getAngleBetween(cellDir.norm()), (-dirVec).getAngleBetween(cellDir.norm()));
float forwDist = cellDir.getLength() * cos(forwAngle);
for(int scid : surroundingCellIds){
if(cells[scid].blockedBy && cells[scid].blockedBy != unit) continue;
float leftAngle = std::min(leftVec.getAngleBetween(cellDir.norm()), (-leftVec).getAngleBetween(cellDir.norm()));
float leftDist = cellDir.getLength() * cos(leftAngle);
bool within = (forwDist < .5 * (unit->getLength() + CELL_SIZE.x) && leftDist < .5 * (unit->getWidth() + CELL_SIZE.z));
cell.blockedBy = (within ? unit : nullptr);
bool within = unit->pointWithinObj(cells[scid].pos);
cells[scid].blockedBy = (within ? unit : nullptr);
}
}
void Map::unblockCells(Unit *unit){
for(Cell &cell : cells)
if(cell.blockedBy == unit)
cell.blockedBy = nullptr;
vector<int> surroundingCellIds = getSurroundingCells(unit->getPos(), 0);
for(int scid : surroundingCellIds)
if(cells[scid].blockedBy == unit)
cells[scid].blockedBy = nullptr;
}
void Map::loadSpawnPoints(){
@@ -689,6 +702,7 @@ namespace battleship{
}
//TODO replace search with binary search
//TODO optimize horizontal and vertical id calculcation
int Map::getCellId(Vector3 pos, bool checkUnderwaterCells){
int numHorCells = int(mapSize.x / CELL_SIZE.x), horId = -1;
+1
View File
@@ -78,6 +78,7 @@ namespace battleship{
bool isPointWithinTerrainObject(vb01::Vector3, int);
void loadPlayerGameObjects();
int getNumMapSpawnPoints(std::string = "");
std::vector<int> getSurroundingCells(vb01::Vector3, int);
void blockCells(Unit*);
void unblockCells(Unit*);
inline std::string getMapName(){return mapName;}