1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <cassert>
#include "UnitHandler.h"
#include "Unit.h"
#include "UnitDefHandler.h"
#include "CommandAI/BuilderCAI.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/MoveTypes/MoveType.h"
#include "Sim/Weapons/Weapon.h"
#include "System/EventHandler.h"
#include "System/Log/ILog.h"
#include "System/myMath.h"
#include "System/TimeProfiler.h"
#include "System/Util.h"
#include "System/Sync/SyncTracer.h"
#include "System/creg/STL_Deque.h"
#include "System/creg/STL_List.h"
#include "System/creg/STL_Set.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CUnitHandler* unitHandler = NULL;
CR_BIND(CUnitHandler, )
CR_REG_METADATA(CUnitHandler, (
CR_MEMBER(units),
CR_MEMBER(unitsByDefs),
CR_MEMBER(activeUnits),
CR_MEMBER(builderCAIs),
CR_MEMBER(idPool),
CR_MEMBER(unitsToBeRemoved),
CR_MEMBER(activeSlowUpdateUnit),
CR_MEMBER(activeUpdateUnit),
CR_MEMBER(maxUnits),
CR_MEMBER(maxUnitRadius)
))
CUnitHandler::CUnitHandler()
:
maxUnits(0),
maxUnitRadius(0.0f)
{
// set the global (runtime-constant) unit-limit as the sum
// of all team unit-limits, which is *always* <= MAX_UNITS
// (note that this also counts the Gaia team)
//
// teams can not be created at runtime, but they can die and
// in that case the per-team limit is recalculated for every
// other team in the respective allyteam
for (unsigned int n = 0; n < teamHandler->ActiveTeams(); n++) {
maxUnits += teamHandler->Team(n)->GetMaxUnits();
}
units.resize(maxUnits, NULL);
unitsByDefs.resize(teamHandler->ActiveTeams(), std::vector<std::vector<CUnit*>>(unitDefHandler->unitDefs.size()));
// id's are used as indices, so they must lie in [0, units.size() - 1]
// (furthermore all id's are treated equally, none have special status)
idPool.Expand(0, units.size());
activeSlowUpdateUnit = 0;
activeUpdateUnit = 0;
}
CUnitHandler::~CUnitHandler()
{
for (CUnit* u: activeUnits) {
// ~CUnit dereferences featureHandler which is destroyed already
u->delayedWreckLevel = -1;
delete u;
}
}
void CUnitHandler::DeleteScripts()
{
// Predelete scripts since they sometimes call models
// which are already gone by KillSimulation.
for (CUnit* u: activeUnits) {
u->DeleteScript();
}
}
void CUnitHandler::InsertActiveUnit(CUnit* unit)
{
const unsigned int insertionPos = gs->randFloat() * activeUnits.size();
idPool.AssignID(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;
}
bool CUnitHandler::AddUnit(CUnit* unit)
{
// LoadUnit should make sure this is true
assert(CanAddUnit(unit->id));
InsertActiveUnit(unit);
teamHandler->Team(unit->team)->AddUnit(unit, CTeam::AddBuilt);
VectorInsertUnique(unitsByDefs[unit->team][unit->unitDef->id], unit, false);
maxUnitRadius = std::max(unit->radius, maxUnitRadius);
return true;
}
void CUnitHandler::DeleteUnit(CUnit* unit)
{
unitsToBeRemoved.push_back(unit);
}
void CUnitHandler::DeleteUnitsNow()
{
if (unitsToBeRemoved.empty())
return;
while (!unitsToBeRemoved.empty()) {
DeleteUnitNow(unitsToBeRemoved.back());
unitsToBeRemoved.pop_back();
}
}
void CUnitHandler::DeleteUnitNow(CUnit* delUnit)
{
// we want to call RenderUnitDestroyed while the unit is still valid
eventHandler.RenderUnitDestroyed(delUnit);
const auto it = std::find(activeUnits.begin(), activeUnits.end(), delUnit);
assert(it != activeUnits.end());
{
const int delTeam = delUnit->team;
const int delType = delUnit->unitDef->id;
teamHandler->Team(delTeam)->RemoveUnit(delUnit, CTeam::RemoveDied);
if (activeSlowUpdateUnit > std::distance(activeUnits.begin(), it)) {
--activeSlowUpdateUnit;
}
activeUnits.erase(it);
VectorErase(unitsByDefs[delTeam][delType], delUnit);
idPool.FreeID(delUnit->id, true);
units[delUnit->id] = nullptr;
CSolidObject::SetDeletingRefID(delUnit->id);
delete delUnit;
CSolidObject::SetDeletingRefID(-1);
}
#ifdef _DEBUG
for (CUnit* u: activeUnits) {
if (u == delUnit) {
LOG_L(L_ERROR, "Duplicated unit found in active units on erase");
}
}
#endif
}
void CUnitHandler::Update()
{
auto UNIT_SANITY_CHECK = [](const CUnit* unit) {
unit->pos.AssertNaNs();
unit->midPos.AssertNaNs();
unit->relMidPos.AssertNaNs();
unit->speed.AssertNaNs();
unit->deathSpeed.AssertNaNs();
unit->rightdir.AssertNaNs();
unit->updir.AssertNaNs();
unit->frontdir.AssertNaNs();
if (unit->unitDef->IsGroundUnit()) {
assert(unit->pos.x >= -(float3::maxxpos * 16.0f));
assert(unit->pos.x <= (float3::maxxpos * 16.0f));
assert(unit->pos.z >= -(float3::maxzpos * 16.0f));
assert(unit->pos.z <= (float3::maxzpos * 16.0f));
}
};
DeleteUnitsNow();
{
SCOPED_TIMER("Unit::MoveType::Update");
for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit* unit = activeUnits[activeUpdateUnit];
AMoveType* moveType = unit->moveType;
UNIT_SANITY_CHECK(unit);
if (moveType->Update()) {
eventHandler.UnitMoved(unit);
}
if (!unit->pos.IsInBounds() && (unit->speed.w > MAX_UNIT_SPEED)) {
// this unit is not coming back, kill it now without any death
// sequence (so deathScriptFinished becomes true immediately)
unit->KillUnit(nullptr, false, true, false);
}
UNIT_SANITY_CHECK(unit);
assert(activeUnits[activeUpdateUnit] == unit);
}
}
{
// Delete dead units
for (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit* unit = activeUnits[activeUpdateUnit];
if (!unit->deathScriptFinished)
continue;
// there are many ways to fiddle with "deathScriptFinished", so a unit
// may arrive here not having been properly killed (with isDead still
// false)
// make sure we always call Killed; no-op if isDead is already true
unit->KillUnit(nullptr, false, true, true);
DeleteUnit(unit);
assert(activeUnits[activeUpdateUnit] == unit);
}
}
{
SCOPED_TIMER("Unit::UpdateLosStatus");
for (CUnit* unit: activeUnits) {
for (int at = 0; at < teamHandler->ActiveAllyTeams(); ++at) {
unit->UpdateLosStatus(at);
}
}
}
{
SCOPED_TIMER("Unit::SlowUpdate");
assert(activeSlowUpdateUnit >= 0);
// reset the iterator every <UNIT_SLOWUPDATE_RATE> frames
if ((gs->frameNum % UNIT_SLOWUPDATE_RATE) == 0) {
activeSlowUpdateUnit = 0;
}
// stagger the SlowUpdate's
unsigned int n = (activeUnits.size() / UNIT_SLOWUPDATE_RATE) + 1;
for (; activeSlowUpdateUnit < activeUnits.size() && n != 0; ++activeSlowUpdateUnit) {
CUnit* unit = activeUnits[activeSlowUpdateUnit];
UNIT_SANITY_CHECK(unit);
unit->SlowUpdate();
unit->SlowUpdateWeapons();
unit->localModel.UpdateBoundingVolume();
UNIT_SANITY_CHECK(unit);
n--;
}
}
{
SCOPED_TIMER("Unit::Update");
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 (activeUpdateUnit = 0; activeUpdateUnit < activeUnits.size();++activeUpdateUnit) {
CUnit* unit = activeUnits[activeUpdateUnit];
if (unit->CanUpdateWeapons()) {
for (CWeapon* w: unit->weapons) {
w->Update();
}
}
assert(activeUnits[activeUpdateUnit] == unit);
}
}
}
void CUnitHandler::AddBuilderCAI(CBuilderCAI* b)
{
// called from CBuilderCAI --> owner is already valid
builderCAIs[b->owner->id] = b;
}
void CUnitHandler::RemoveBuilderCAI(CBuilderCAI* b)
{
// called from ~CUnit --> owner is still valid
assert(b->owner != nullptr);
builderCAIs.erase(b->owner->id);
}
bool CUnitHandler::CanBuildUnit(const UnitDef* unitdef, int team) const
{
if (teamHandler->Team(team)->AtUnitLimit()) {
return false;
}
if (unitsByDefs[team][unitdef->id].size() >= unitdef->maxThisUnit) {
return false;
}
return true;
}
|