bugfix for crashing when referencing destroyed units

This commit is contained in:
devZoGok
2023-12-09 11:29:49 +02:00
parent a408a40090
commit 0af2aac93c
2 changed files with 16 additions and 13 deletions
+12 -13
View File
@@ -275,21 +275,20 @@ namespace battleship{
}
void Unit::attack(Order order){
if(orders[0].targets[0].unit){
bool unitFound = false;
vector<Unit*> units;
for(Player *pl : Game::getSingleton()->getPlayers()){
vector<Unit*> units = pl->getUnits();
if(find(units.begin(), units.end(), orders[0].targets[0].unit) != units.end()){
unitFound = true;
break;
}
}
if(!unitFound)
removeOrder(0);
for(Player *pl : Game::getSingleton()->getPlayers()){
vector<Unit*> un = pl->getUnits();
units.insert(units.end(), un.begin(), un.end());
}
for(Order::Target &target : orders[0].targets)
if(target.unit){
if(find(units.begin(), units.end(), target.unit) != units.end())
break;
removeOrder(0);
}
}
void Unit::setOrder(Order order) {
+4
View File
@@ -271,7 +271,11 @@ namespace battleship{
}
void Vehicle::attack(Order order){
int prevNumOrders = orders.size();
Unit::attack(order);
int currNumOrders = orders.size();
if(prevNumOrders != currNumOrders) return;
Order::Target target = order.targets[0];
Vector3 targVec = (target.unit ? target.unit->getPos() : target.pos) - pos;