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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "UnitLoader.h"
#include "Unit.h"
#include "UnitDef.h"
#include "UnitDefHandler.h"
#include "UnitHandler.h"
#include "CommandAI/AirCAI.h"
#include "CommandAI/BuilderCAI.h"
#include "CommandAI/CommandAI.h"
#include "CommandAI/FactoryCAI.h"
#include "CommandAI/MobileCAI.h"
#include "Game/GameHelper.h"
#include "Map/Ground.h"
#include "Map/MapDamage.h"
#include "Map/ReadMap.h"
#include "Sim/Features/FeatureDef.h"
#include "Sim/Features/FeatureDefHandler.h"
#include "Sim/Features/FeatureHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "System/Exceptions.h"
#include "System/Log/ILog.h"
#include "System/Platform/Watchdog.h"
CUnitLoader* CUnitLoader::GetInstance()
{
// NOTE: UnitLoader has no internal state, so this is fine wrt. reloading
static CUnitLoader instance;
return &instance;
}
CCommandAI* CUnitLoader::NewCommandAI(CUnit* u, const UnitDef* ud)
{
static_assert(sizeof(CFactoryCAI) <= sizeof(u->caiMemBuffer), "");
static_assert(sizeof(CBuilderCAI) <= sizeof(u->caiMemBuffer), "");
static_assert(sizeof( CAirCAI) <= sizeof(u->caiMemBuffer), "");
static_assert(sizeof( CMobileCAI) <= sizeof(u->caiMemBuffer), "");
static_assert(sizeof( CCommandAI) <= sizeof(u->caiMemBuffer), "");
if (ud->IsFactoryUnit())
return (new (u->caiMemBuffer) CFactoryCAI(u));
if (ud->IsMobileBuilderUnit() || ud->IsStaticBuilderUnit())
return (new (u->caiMemBuffer) CBuilderCAI(u));
// non-hovering fighter or bomber aircraft; coupled to StrafeAirMoveType
if (ud->IsStrafingAirUnit())
return (new (u->caiMemBuffer) CAirCAI(u));
// all other aircraft; coupled to HoverAirMoveType
if (ud->IsAirUnit())
return (new (u->caiMemBuffer) CMobileCAI(u));
if (ud->IsGroundUnit() || ud->IsTransportUnit())
return (new (u->caiMemBuffer) CMobileCAI(u));
return (new (u->caiMemBuffer) CCommandAI(u));
}
CUnit* CUnitLoader::LoadUnit(const std::string& name, const UnitLoadParams& params)
{
const_cast<UnitLoadParams&>(params).unitDef = unitDefHandler->GetUnitDefByName(name);
if (params.unitDef == nullptr)
throw content_error("Couldn't find unittype " + name);
return (LoadUnit(params));
}
CUnit* CUnitLoader::LoadUnit(const UnitLoadParams& params)
{
CUnit* unit = nullptr;
{
const UnitDef* ud = params.unitDef;
if (ud == nullptr)
return unit;
// need to check this BEFORE creating the instance
if (!unitHandler.CanAddUnit(params.unitID))
return unit;
if (params.teamID < 0) {
if (teamHandler.GaiaTeamID() < 0) {
LOG_L(L_WARNING, "[%s] invalid team %d and no Gaia-team", __func__, params.teamID);
return unit;
}
const_cast<UnitLoadParams&>(params).teamID = teamHandler.GaiaTeamID();
}
unit = CUnitHandler::NewUnit(ud);
unit->PreInit(params);
unit->PostInit(params.builder);
}
if (params.flattenGround)
FlattenGround(unit);
return unit;
}
void CUnitLoader::ParseAndExecuteGiveUnitsCommand(const std::vector<std::string>& args, int team)
{
if (args.size() < 2) {
LOG_L(L_WARNING, "[%s] not enough arguments (\"/give [amount] <objectName | 'all'> [team] [@x, y, z]\")", __FUNCTION__);
return;
}
float3 pos;
if (sscanf(args[args.size() - 1].c_str(), "@%f, %f, %f", &pos.x, &pos.y, &pos.z) != 3) {
LOG_L(L_WARNING, "[%s] invalid position argument (\"/give [amount] <objectName | 'all'> [team] [@x, y, z]\")", __FUNCTION__);
return;
}
int amount = 1;
int amountArgIdx = -1;
int teamArgIdx = -1;
if (args.size() == 4) {
amountArgIdx = 0;
teamArgIdx = 2;
}
else if (args.size() == 3) {
if (args[0].find_first_not_of("0123456789") == std::string::npos) {
amountArgIdx = 0;
} else {
teamArgIdx = 1;
}
}
if (amountArgIdx >= 0) {
amount = atoi(args[amountArgIdx].c_str());
if ((amount < 0) || (args[amountArgIdx].find_first_not_of("0123456789") != std::string::npos)) {
LOG_L(L_WARNING, "[%s] invalid amount argument: %s", __FUNCTION__, args[amountArgIdx].c_str());
return;
}
}
int featureAllyTeam = -1;
if (teamArgIdx >= 0) {
team = atoi(args[teamArgIdx].c_str());
if ((!teamHandler.IsValidTeam(team)) || (args[teamArgIdx].find_first_not_of("0123456789") != std::string::npos)) {
LOG_L(L_WARNING, "[%s] invalid team argument: %s", __FUNCTION__, args[teamArgIdx].c_str());
return;
}
featureAllyTeam = teamHandler.AllyTeam(team);
}
const std::string& objectName = (amountArgIdx >= 0) ? args[1] : args[0];
if (objectName.empty()) {
LOG_L(L_WARNING, "[%s] invalid object-name argument", __FUNCTION__);
return;
}
GiveUnits(objectName, pos, amount, team, featureAllyTeam);
}
void CUnitLoader::GiveUnits(const std::string& objectName, float3 pos, int amount, int team, int featureAllyTeam)
{
const CTeam* receivingTeam = teamHandler.Team(team);
if (objectName == "all") {
unsigned int numRequestedUnits = unitDefHandler->NumUnitDefs();
unsigned int currentNumUnits = receivingTeam->GetNumUnits();
// make sure team unit-limit is not exceeded
if ((currentNumUnits + numRequestedUnits) > receivingTeam->GetMaxUnits())
numRequestedUnits = receivingTeam->GetMaxUnits() - currentNumUnits;
// make sure square is entirely on the map
const int sqSize = math::ceil(math::sqrt((float) numRequestedUnits));
const float sqHalfMapSize = sqSize / 2 * 10 * SQUARE_SIZE;
pos.x = Clamp(pos.x, sqHalfMapSize, float3::maxxpos - sqHalfMapSize - 1);
pos.z = Clamp(pos.z, sqHalfMapSize, float3::maxzpos - sqHalfMapSize - 1);
for (int a = 1; a <= numRequestedUnits; ++a) {
Watchdog::ClearTimers(false, true);
const float px = pos.x + (a % sqSize - sqSize / 2) * 10 * SQUARE_SIZE;
const float pz = pos.z + (a / sqSize - sqSize / 2) * 10 * SQUARE_SIZE;
const UnitDef* ud = unitDefHandler->GetUnitDefByID(a);
const UnitLoadParams unitParams = {
ud,
nullptr,
float3(px, CGround::GetHeightReal(px, pz), pz),
ZeroVector,
-1,
team,
FACING_SOUTH,
false,
true,
};
LoadUnit(unitParams);
}
} else {
unsigned int numRequestedUnits = amount;
unsigned int currentNumUnits = receivingTeam->GetNumUnits();
if (receivingTeam->AtUnitLimit()) {
LOG_L(L_WARNING,
"[%s] unable to give more units to team %d (current: %u, team limit: %u, global limit: %u)",
__FUNCTION__, team, currentNumUnits, receivingTeam->GetMaxUnits(), unitHandler.MaxUnits()
);
return;
}
// make sure team unit-limit is not exceeded
if ((currentNumUnits + numRequestedUnits) > receivingTeam->GetMaxUnits())
numRequestedUnits = receivingTeam->GetMaxUnits() - currentNumUnits;
const UnitDef* unitDef = unitDefHandler->GetUnitDefByName(objectName);
const FeatureDef* featureDef = featureDefHandler->GetFeatureDef(objectName, false);
if (unitDef == nullptr && featureDef == nullptr) {
LOG_L(L_WARNING, "[%s] %s is not a valid object-name", __FUNCTION__, objectName.c_str());
return;
}
if (unitDef != nullptr) {
const int xsize = unitDef->xsize;
const int zsize = unitDef->zsize;
const int squareSize = math::ceil(math::sqrt((float) numRequestedUnits));
const float3 squarePos = float3(
pos.x - (((squareSize - 1) * xsize * SQUARE_SIZE) / 2),
pos.y,
pos.z - (((squareSize - 1) * zsize * SQUARE_SIZE) / 2)
);
int unitsLoaded = numRequestedUnits;
for (int z = 0; z < squareSize; ++z) {
for (int x = 0; x < squareSize && (unitsLoaded-- > 0); ++x) {
const float px = squarePos.x + x * xsize * SQUARE_SIZE;
const float pz = squarePos.z + z * zsize * SQUARE_SIZE;
Watchdog::ClearTimers(false, true);
const UnitLoadParams unitParams = {
unitDef,
nullptr,
float3(px, CGround::GetHeightReal(px, pz), pz),
ZeroVector,
-1,
team,
FACING_SOUTH,
false,
true,
};
LoadUnit(unitParams);
}
}
LOG("[%s] spawned %i %s unit(s) for team %i",
__FUNCTION__, numRequestedUnits, objectName.c_str(), team);
}
if (featureDef != nullptr) {
if (featureAllyTeam < 0)
team = -1; // default to world features
const int xsize = featureDef->xsize;
const int zsize = featureDef->zsize;
const int squareSize = math::ceil(math::sqrt((float) numRequestedUnits));
const float3 squarePos = float3(
pos.x - (((squareSize - 1) * xsize * SQUARE_SIZE) / 2),
pos.y,
pos.z - (((squareSize - 1) * zsize * SQUARE_SIZE) / 2)
);
int total = amount; // FIXME -- feature count limit?
for (int z = 0; z < squareSize; ++z) {
for (int x = 0; x < squareSize && total > 0; ++x) {
const float px = squarePos.x + x * xsize * SQUARE_SIZE;
const float pz = squarePos.z + z * zsize * SQUARE_SIZE;
const float3 featurePos = float3(px, CGround::GetHeightReal(px, pz), pz);
Watchdog::ClearTimers(false, true);
FeatureLoadParams params = {
nullptr,
nullptr,
featureDef,
featurePos,
ZeroVector,
team,
featureAllyTeam,
0, // rotation
FACING_SOUTH,
0, // wreckLevels
0, // smokeTime
};
featureHandler.LoadFeature(params);
--total;
}
}
LOG("[%s] spawned %i %s feature(s) for team %i",
__FUNCTION__, numRequestedUnits, objectName.c_str(), team);
}
}
}
void CUnitLoader::FlattenGround(const CUnit* unit)
{
const UnitDef* unitDef = unit->unitDef;
// const MoveDef* moveDef = unit->moveDef;
if (mapDamage->Disabled())
return;
if (!unitDef->levelGround)
return;
if (unitDef->IsAirUnit())
return;
if (!unitDef->IsImmobileUnit())
return;
if (unit->FloatOnWater() && unit->IsInWater())
return;
// if we are float-capable, only flatten
// if the terrain here is above sea level
BuildInfo bi(unitDef, unit->pos, unit->buildFacing);
bi.pos = CGameHelper::Pos2BuildPos(bi, true);
const float hss = 0.5f * SQUARE_SIZE;
const int tx1 = (int) std::max(0.0f ,(bi.pos.x - (bi.GetXSize() * hss)) / SQUARE_SIZE);
const int tz1 = (int) std::max(0.0f ,(bi.pos.z - (bi.GetZSize() * hss)) / SQUARE_SIZE);
const int tx2 = std::min(mapDims.mapx, tx1 + bi.GetXSize());
const int tz2 = std::min(mapDims.mapy, tz1 + bi.GetZSize());
for (int z = tz1; z <= tz2; z++) {
for (int x = tx1; x <= tx2; x++) {
readMap->SetHeight(z * mapDims.mapxp1 + x, bi.pos.y);
}
}
mapDamage->RecalcArea(tx1, tx2, tz1, tz2);
}
void CUnitLoader::RestoreGround(const CUnit* unit)
{
const UnitDef* unitDef = unit->unitDef;
if (mapDamage->Disabled())
return;
if (!unitDef->levelGround)
return;
if (unitDef->IsAirUnit())
return;
if (!unitDef->IsImmobileUnit())
return;
if (unit->FloatOnWater() && unit->IsInWater())
return;
BuildInfo bi(unitDef, unit->pos, unit->buildFacing);
bi.pos = CGameHelper::Pos2BuildPos(bi, true);
const float hss = 0.5f * SQUARE_SIZE;
const int tx1 = (int) std::max(0.0f ,(bi.pos.x - (bi.GetXSize() * hss)) / SQUARE_SIZE);
const int tz1 = (int) std::max(0.0f ,(bi.pos.z - (bi.GetZSize() * hss)) / SQUARE_SIZE);
const int tx2 = std::min(mapDims.mapx, tx1 + bi.GetXSize());
const int tz2 = std::min(mapDims.mapy, tz1 + bi.GetZSize());
const float* heightmap = readMap->GetCornerHeightMapSynced();
int num = 0;
float heightdiff = 0.0f;
for (int z = tz1; z <= tz2; z++) {
for (int x = tx1; x <= tx2; x++) {
int index = z * mapDims.mapxp1 + x;
heightdiff += heightmap[index] - readMap->GetOriginalHeightMapSynced()[index];
++num;
}
}
// adjust the terrain profile to match orgheightmap
heightdiff /= (float)num;
heightdiff += unit->pos.y - bi.pos.y;
for (int z = tz1; z <= tz2; z++) {
for (int x = tx1; x <= tx2; x++) {
int index = z * mapDims.mapxp1 + x;
readMap->SetHeight(index, heightdiff + readMap->GetOriginalHeightMapSynced()[index]);
}
}
// but without affecting the build height
heightdiff = bi.pos.y - CGameHelper::Pos2BuildPos(bi, true).y;
for (int z = tz1; z <= tz2; z++) {
for (int x = tx1; x <= tx2; x++) {
int index = z * mapDims.mapxp1 + x;
readMap->SetHeight(index, heightdiff + heightmap[index]);
}
}
mapDamage->RecalcArea(tx1, tx2, tz1, tz2);
}
|