refactored visible game object detection

checking whether the game object frame is on the right type of a cell
This commit is contained in:
devZoGok
2026-03-15 15:21:11 +02:00
parent 268ea14f4a
commit 580659aad5
5 changed files with 63 additions and 66 deletions
+9 -63
View File
@@ -476,73 +476,19 @@ namespace battleship{
return (numAboveEdges > 0 && numBelowEdges > 0); return (numAboveEdges > 0 && numBelowEdges > 0);
} }
//TODO fix fog of war for hostile units
void ActiveGameState::renderUnits() { void ActiveGameState::renderUnits() {
ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); vector<Unit*> friendlyUnits = mainPlayer->getFriendlyUnits();
vector<Listbox*> listboxes{};
vector<Checkbox*> checkboxes{};
vector<Slider*> sliders{};
vector<Textbox*> textboxes{};
vector<Node*> guiRects = guiManager->getGuiRectangles();
vector<Text*> texts{
guiManager->getText("depth"),
guiManager->getText("refineds"),
guiManager->getText("wealth"),
guiManager->getText("research")
};
vector<Unit*> units; for (Player *p : Game::getSingleton()->getPlayers(true)){
vector<Unit*> units = p->getUnits();
for (Player *p : Game::getSingleton()->getPlayers(true)) for (Unit *u : units) {
for (Unit *u : p->getUnits()) if(u->isVehicle() && ((Vehicle*)u)->getGarrisonable()) continue;
units.push_back(u);
bool unitVisible = mainPlayer->isObjectVisible((GameObject*)u, friendlyUnits);
vector<Unit*> selUnits = mainPlayer->getSelectedUnits(); u->getModel()->setVisible(unitVisible);
for (Unit *u : units) {
if(u->isVehicle() && ((Vehicle*)u)->getGarrisonable()) continue;
Node *model = u->getModel();
if (u->getPlayer()->getTeam() == mainPlayer->getTeam()){
if(!u->getLosLightNode()) u->initLosLight();
model->setVisible(true);
}
else{
if(u->getLosLightNode()) u->destroyLosLight();
model->setVisible(false);
for (int i = 0; i < units.size(); i++)
if(units[i]->getPlayer() == mainPlayer){
Vector3 obsUnitPos = units[i]->getPos();
obsUnitPos.y = 0;
Vector3 compUnitPos = u->getPos();
compUnitPos.y = 0;
float dist = compUnitPos.getDistanceFrom(obsUnitPos);
if(dist <= units[i]->getLineOfSight()){
model->setVisible(true);
break;
}
}
} }
} }
}
//TODO improve this for greater accuracy
bool ActiveGameState::isInLineOfSight(Vector3 center, float radius, Unit *u) {
bool inside = false;
for (int i = 0; i < 4 && !inside; i++)
if (center.getDistanceFrom(u->getCorner(i)) <= radius){
inside = true;
break;
}
return inside;
} }
void ActiveGameState::updateDragBox() { void ActiveGameState::updateDragBox() {
-1
View File
@@ -73,7 +73,6 @@ namespace battleship{
void updateStructureFrames(); void updateStructureFrames();
void castRayToTerrain(); void castRayToTerrain();
void issueOrder(Order::TYPE, std::vector<Order::Target>, bool); void issueOrder(Order::TYPE, std::vector<Order::Target>, bool);
bool isInLineOfSight(vb01::Vector3, float, Unit*);
void enableUnitState(Unit::State); void enableUnitState(Unit::State);
void addUnitGui(); void addUnitGui();
inline bool canSelectHoveredOnGameObj(){return gameObjHoveredOn && gameObjHoveredOn->isSelectable() && gameObjHoveredOn->getPlayer() == mainPlayer;} inline bool canSelectHoveredOnGameObj(){return gameObjHoveredOn && gameObjHoveredOn->isSelectable() && gameObjHoveredOn->getPlayer() == mainPlayer;}
+19 -2
View File
@@ -73,18 +73,35 @@ namespace battleship{
} }
} }
//TODO fix which unit frames light green or red //TODO include checking if frame is outside of line of sight
void GameObjectFrameController::checkPlacement(GameObjectFrame &s){ void GameObjectFrameController::checkPlacement(GameObjectFrame &s){
s.status = GameObjectFrame::PLACEABLE; s.status = GameObjectFrame::PLACEABLE;
Map *map = Map::getSingleton(); Map *map = Map::getSingleton();
Vector3 mapSize = map->getMapSize(); Vector3 mapSize = map->getMapSize();
Vector3 cellSize = map->getCellSize();
int dirMult[][2]{{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int dirMult[][2]{{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
vector<Map::Cell> &cells = map->getCells();
sol::table tbl = generateView()["units"][s.getId() + 1];
UnitType ut = (UnitType)tbl["unitType"];
//vector<Unit*> friendlyUnits = s.getPlayer()->getFriendlyUnits();
for(int i = 0; i < 4; i++){ for(int i = 0; i < 4; i++){
Vector3 cornerPos = (s.getPos() + s.getDirVec() * .5 * dirMult[i][0] * s.getLength() + s.getLeftVec() * .5 * dirMult[i][1] * s.getWidth()); Vector3 cornerPos = (s.getPos() + s.getDirVec() * .5 * dirMult[i][0] * s.getLength() + s.getLeftVec() * .5 * dirMult[i][1] * s.getWidth());
if(!(fabs(cornerPos.x) <= .5 * mapSize.x && fabs(cornerPos.z) <=.5 * mapSize.z)){ if(!(fabs(cornerPos.x) <= .5 * mapSize.x && fabs(cornerPos.z) <= .5 * mapSize.z)){
s.status = GameObjectFrame::NOT_PLACEABLE;
return;
}
Map::Cell cell = cells[map->getCellId(cornerPos, false)];
bool landUnitOnWater = (ut == UnitType::LAND && cell.type == Map::Cell::Type::WATER);
bool waterUnitOnLand = ((ut == UnitType::SEA_LEVEL || ut == UnitType::UNDERWATER) && cell.type == Map::Cell::Type::LAND);
bool withinCell = (fabs(cell.pos.x - cornerPos.x) < .5 * cellSize.x && fabs(cell.pos.z - cornerPos.z) < .5 * cellSize.z);
if((landUnitOnWater || waterUnitOnLand) && withinCell){
s.status = GameObjectFrame::NOT_PLACEABLE; s.status = GameObjectFrame::NOT_PLACEABLE;
return; return;
} }
+33
View File
@@ -286,4 +286,37 @@ namespace battleship{
} }
} }
} }
vector<Unit*> Player::getFriendlyUnits(bool includeOwn){
vector<Unit*> friendlyUnits;
for (Player *pl : Game::getSingleton()->getPlayers(true))
for (Unit *u : pl->getUnits())
if((includeOwn && pl == this) || pl->getTeam() == getTeam())
friendlyUnits.push_back(u);
return friendlyUnits;
}
//TODO improve this for greater accuracy
bool Player::isObjectVisible(GameObject *object, std::vector<Unit*> friendlyUnits) {
Player *objPlayer = object->getPlayer();
for(Unit *friendlyUnit : friendlyUnits){
if(objPlayer == this || objPlayer->getTeam() == getTeam())
return true;
Vector3 obsUnitPos = object->getPos();
obsUnitPos.y = 0;
Vector3 compUnitPos = friendlyUnit->getPos();
compUnitPos.y = 0;
float dist = compUnitPos.getDistanceFrom(obsUnitPos);
if(dist <= friendlyUnit->getLineOfSight())
return true;
}
return false;
}
} }
+2
View File
@@ -39,6 +39,8 @@ namespace battleship{
void deselectUnit(Unit*); void deselectUnit(Unit*);
std::vector<GameObject*> getDestructables(); std::vector<GameObject*> getDestructables();
void updateGameStats(Unit*); void updateGameStats(Unit*);
std::vector<Unit*> getFriendlyUnits(bool = true);
bool isObjectVisible(GameObject*, std::vector<Unit*>);
inline int getResource(ResourceType rt){return resources[(int)rt];} inline int getResource(ResourceType rt){return resources[(int)rt];}
inline void updateResource(ResourceType rt, int amount, bool add){resources[(int)rt] = (add ? resources[(int)rt] + amount : amount);} inline void updateResource(ResourceType rt, int amount, bool add){resources[(int)rt] = (add ? resources[(int)rt] + amount : amount);}
inline Trader* getTrader(){return trader;} inline Trader* getTrader(){return trader;}