Skip to content

Commit

Permalink
Deal with activeUnits reallocation in unitHandler, Fix #4918
Browse files Browse the repository at this point in the history
  • Loading branch information
ashdnazg committed Jul 27, 2015
1 parent 439a848 commit ecf0c51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
29 changes: 22 additions & 7 deletions rts/Sim/Units/UnitHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void CUnitHandler::PostLoad()
{
// reset any synced stuff that is not saved
activeSlowUpdateUnit = 0;
activeUpdateUnit = 0;
}


Expand Down Expand Up @@ -74,6 +75,7 @@ CUnitHandler::CUnitHandler()
idPool.Expand(0, units.size());

activeSlowUpdateUnit = 0;
activeUpdateUnit = 0;
airBaseHandler = new CAirBaseHandler();
}

Expand All @@ -98,10 +100,14 @@ void CUnitHandler::InsertActiveUnit(CUnit* unit)
assert(unit->id < units.size());
assert(units[unit->id] == NULL);

assert(insertionPos >= 0 && insertionPos <= activeUnits.size());
activeUnits.insert(activeUnits.begin() + insertionPos, unit);
if (insertionPos <= activeSlowUpdateUnit) {
++activeSlowUpdateUnit;
}
if (insertionPos <= activeUpdateUnit) {
++activeUpdateUnit;
}
units[unit->id] = unit;
}

Expand Down Expand Up @@ -130,7 +136,6 @@ void CUnitHandler::DeleteUnitNow(CUnit* delUnit)
{
//we want to call RenderUnitDestroyed while the unit is still valid
eventHandler.RenderUnitDestroyed(delUnit);

for (int i = 0; i < activeUnits.size();++i) {
if (activeUnits[i] != delUnit)
continue;
Expand All @@ -146,6 +151,8 @@ void CUnitHandler::DeleteUnitNow(CUnit* delUnit)
--activeSlowUpdateUnit;
}

assert(activeUnits.size() == i || activeUnits[i] != delUnit);

unitsByDefs[delTeam][delType].erase(delUnit);
idPool.FreeID(delUnit->id, true);

Expand Down Expand Up @@ -202,8 +209,9 @@ void CUnitHandler::Update()

{
SCOPED_TIMER("Unit::MoveType::Update");

for (CUnit* unit: activeUnits) {

for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit *unit = activeUnits[activeUpdateUnit];
AMoveType* moveType = unit->moveType;

UNIT_SANITY_CHECK(unit);
Expand All @@ -218,12 +226,14 @@ void CUnitHandler::Update()
}

UNIT_SANITY_CHECK(unit);
assert(activeUnits[activeUpdateUnit] == unit);
}
}

{
// Delete dead units
for (CUnit* unit: activeUnits) {
for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit *unit = activeUnits[activeUpdateUnit];
if (!unit->deathScriptFinished)
continue;

Expand All @@ -233,12 +243,13 @@ void CUnitHandler::Update()
// (KU returns early if isDead)
unit->KillUnit(NULL, false, true);
DeleteUnit(unit);
assert(activeUnits[activeUpdateUnit] == unit);
}
}

{
SCOPED_TIMER("Unit::UpdatePieceMatrices");

//Shouldn't insert new units
for (CUnit* unit: activeUnits) {
// UnitScript only applies piece-space transforms so
// we apply the forward kinematics update separately
Expand Down Expand Up @@ -273,22 +284,26 @@ void CUnitHandler::Update()
{
SCOPED_TIMER("Unit::Update");

for (CUnit* unit: activeUnits) {
for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit *unit = activeUnits[activeUpdateUnit];
UNIT_SANITY_CHECK(unit);
unit->Update();
UNIT_SANITY_CHECK(unit);
assert(activeUnits[activeUpdateUnit] == unit);
}
}

{
SCOPED_TIMER("Unit::Weapon::Update");

for (CUnit* unit: activeUnits) {
for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit *unit = activeUnits[activeUpdateUnit];
if (unit->CanUpdateWeapons()) {
for (CWeapon* w: unit->weapons) {
w->Update();
}
}
assert(activeUnits[activeUpdateUnit] == unit);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/Units/UnitHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CUnitHandler

std::vector<CUnit*> unitsToBeRemoved; ///< units that will be removed at start of next update
size_t activeSlowUpdateUnit; ///< first unit of batch that will be SlowUpdate'd this frame
size_t activeUpdateUnit; ///< first unit of batch that will be SlowUpdate'd this frame

///< global unit-limit (derived from the per-team limit)
///< units.size() is equal to this and constant at runtime
Expand Down

0 comments on commit ecf0c51

Please sign in to comment.