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
|
/* 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 "Rendering/Models/3DModel.h"
#include "Sim/Misc/AirBaseHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/MoveTypes/MoveType.h"
#include "System/EventHandler.h"
#include "System/EventBatchHandler.h"
#include "System/Log/ILog.h"
#include "System/TimeProfiler.h"
#include "System/myMath.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(maxUnits),
CR_MEMBER(maxUnitRadius),
CR_POSTLOAD(PostLoad)
))
void CUnitHandler::PostLoad()
{
// reset any synced stuff that is not saved
activeSlowUpdateUnit = activeUnits.end();
}
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<CUnitSet>(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 = activeUnits.end();
airBaseHandler = new CAirBaseHandler();
}
CUnitHandler::~CUnitHandler()
{
for (std::list<CUnit*>::iterator usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
// ~CUnit dereferences featureHandler which is destroyed already
(*usi)->delayedWreckLevel = -1;
delete (*usi);
}
delete airBaseHandler;
}
void CUnitHandler::InsertActiveUnit(CUnit* unit)
{
std::list<CUnit*>::iterator ui = activeUnits.begin();
if (ui != activeUnits.end()) {
// randomize this to make the slow-update order random (good if one
// builds say many buildings at once and then many mobile ones etc)
const unsigned int insertionPos = gs->randFloat() * activeUnits.size();
for (unsigned int n = 0; n < insertionPos; ++n) {
++ui;
}
}
idPool.AssignID(unit);
assert(unit->id < units.size());
assert(units[unit->id] == NULL);
activeUnits.insert(ui, unit);
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);
unitsByDefs[unit->team][unit->unitDef->id].insert(unit);
maxUnitRadius = std::max(unit->radius, maxUnitRadius);
return true;
}
void CUnitHandler::DeleteUnit(CUnit* unit)
{
unitsToBeRemoved.push_back(unit);
(eventBatchHandler->GetUnitCreatedDestroyedBatch()).dequeue_synced(unit);
}
void CUnitHandler::DeleteUnitNow(CUnit* delUnit)
{
int delTeam = 0;
int delType = 0;
std::list<CUnit*>::iterator usi;
for (usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
if (*usi == delUnit) {
if (activeSlowUpdateUnit != activeUnits.end() && *usi == *activeSlowUpdateUnit) {
++activeSlowUpdateUnit;
}
delTeam = delUnit->team;
delType = delUnit->unitDef->id;
teamHandler->Team(delTeam)->RemoveUnit(delUnit, CTeam::RemoveDied);
activeUnits.erase(usi);
unitsByDefs[delTeam][delType].erase(delUnit);
idPool.FreeID(delUnit->id, true);
units[delUnit->id] = NULL;
CSolidObject::SetDeletingRefID(delUnit->id);
delete delUnit;
CSolidObject::SetDeletingRefID(-1);
break;
}
}
#ifdef _DEBUG
for (usi = activeUnits.begin(); usi != activeUnits.end(); /* no post-op */) {
if (*usi == delUnit) {
LOG_L(L_ERROR, "Duplicated unit found in active units on erase");
usi = activeUnits.erase(usi);
} else {
++usi;
}
}
#endif
}
void CUnitHandler::Update()
{
{
if (!unitsToBeRemoved.empty()) {
while (!unitsToBeRemoved.empty()) {
eventHandler.DeleteSyncedObjects(); // the unit destructor may invoke eventHandler, so we need to call these for every unit to clear invaild references from the batching systems
eventHandler.DeleteSyncedUnits();
CUnit* delUnit = unitsToBeRemoved.back();
unitsToBeRemoved.pop_back();
DeleteUnitNow(delUnit);
}
}
eventHandler.UpdateUnits();
}
#define MAPPOS_SANITY_CHECK(unit) \
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)); \
}
#define UNIT_SANITY_CHECK(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(); \
MAPPOS_SANITY_CHECK(unit);
{
SCOPED_TIMER("Unit::MoveType::Update");
for (auto usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
CUnit* unit = *usi;
AMoveType* moveType = unit->moveType;
UNIT_SANITY_CHECK(unit);
if (moveType->Update()) {
eventHandler.UnitMoved(unit);
}
if (!unit->pos.IsInBounds() && (Square(unit->speed.w) > (MAX_UNIT_SPEED * MAX_UNIT_SPEED))) {
// this unit is not coming back, kill it now without any death
// sequence (so deathScriptFinished becomes true immediately)
unit->KillUnit(NULL, false, true, false);
}
UNIT_SANITY_CHECK(unit);
}
}
{
// Delete dead units
for (auto usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
CUnit* unit = *usi;
if (unit->deathScriptFinished) {
// there are many ways to fiddle with "deathScriptFinished", so a unit may
// arrive here without having been properly killed (and isDead still false),
// which can result in MT deadlocking -- FIXME verify this
// (KU returns early if isDead)
unit->KillUnit(NULL, false, true);
DeleteUnit(unit);
}
}
}
{
SCOPED_TIMER("Unit::UpdatePieceMatrices");
for (auto usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
// UnitScript only applies piece-space transforms so
// we apply the forward kinematics update separately
// (only if we have any dirty pieces)
CUnit* unit = *usi;
unit->localModel->UpdatePieceMatrices();
}
}
{
SCOPED_TIMER("Unit::Update");
for (auto usi = activeUnits.begin(); usi != activeUnits.end(); ++usi) {
CUnit* unit = *usi;
UNIT_SANITY_CHECK(unit);
unit->Update();
UNIT_SANITY_CHECK(unit);
}
}
{
SCOPED_TIMER("Unit::SlowUpdate");
// reset the iterator every <UNIT_SLOWUPDATE_RATE> frames
if ((gs->frameNum & (UNIT_SLOWUPDATE_RATE - 1)) == 0) {
activeSlowUpdateUnit = activeUnits.begin();
}
// stagger the SlowUpdate's
unsigned int n = (activeUnits.size() / UNIT_SLOWUPDATE_RATE) + 1;
for (; activeSlowUpdateUnit != activeUnits.end() && n != 0; ++activeSlowUpdateUnit) {
CUnit* unit = *activeSlowUpdateUnit;
UNIT_SANITY_CHECK(unit);
unit->SlowUpdate();
UNIT_SANITY_CHECK(unit);
n--;
}
}
}
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 != NULL);
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;
}
|