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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <boost/lexical_cast.hpp>
#include "System/mmgr.h"
#include "MoveInfo.h"
#include "Game/Game.h"
#include "Lua/LuaParser.h"
#include "Map/ReadMap.h"
#include "Map/MapInfo.h"
#include "MoveMath/MoveMath.h"
#include "MoveMath/GroundMoveMath.h"
#include "MoveMath/HoverMoveMath.h"
#include "MoveMath/ShipMoveMath.h"
#include "System/creg/STL_Deque.h"
#include "System/creg/STL_Map.h"
#include "System/Exceptions.h"
#include "System/CRC.h"
#include "System/myMath.h"
#include "System/Util.h"
CR_BIND(MoveData, (0));
CR_BIND(CMoveInfo, );
CR_REG_METADATA(MoveData, (
CR_MEMBER(name),
CR_ENUM_MEMBER(moveType),
CR_ENUM_MEMBER(moveFamily),
CR_ENUM_MEMBER(terrainClass),
CR_MEMBER(xsize),
CR_MEMBER(xsizeh),
CR_MEMBER(zsize),
CR_MEMBER(zsizeh),
CR_MEMBER(depth),
CR_MEMBER(depthModParams),
CR_MEMBER(maxSlope),
CR_MEMBER(slopeMod),
CR_MEMBER(crushStrength),
CR_MEMBER(speedModMults),
CR_MEMBER(pathType),
CR_MEMBER(unitDefRefCount),
CR_MEMBER(followGround),
CR_MEMBER(subMarine),
CR_MEMBER(avoidMobilesOnPath),
CR_MEMBER(heatMapping),
CR_MEMBER(heatMod),
CR_MEMBER(heatProduced),
CR_MEMBER(moveMath),
CR_MEMBER(tempOwner),
CR_RESERVED(16)
));
CR_REG_METADATA(CMoveInfo, (
CR_MEMBER(moveData),
CR_MEMBER(name2moveData),
CR_MEMBER(moveInfoChecksum),
CR_RESERVED(16)
));
CMoveInfo* moveinfo;
// FIXME: do something with these magic numbers
static const float MAX_ALLOWED_WATER_DAMAGE_GMM = 1e3f;
static const float MAX_ALLOWED_WATER_DAMAGE_HMM = 1e4f;
static float DegreesToMaxSlope(float degrees)
{
// Prevent MSVC from inlining stuff that would break the
// PE checksum compatibility between debug and release
static const float degToRad = PI / 180.0f;
const float deg = Clamp(degrees, 0.0f, 60.0f) * 1.5f;
const float rad = deg * degToRad;
return (1.0f - cos(rad));
}
CMoveInfo::CMoveInfo()
{
const LuaTable rootTable = game->defsParser->GetRoot().SubTable("MoveDefs");
if (!rootTable.IsValid()) {
throw content_error("Error loading movement definitions");
}
groundMoveMath = new CGroundMoveMath();
hoverMoveMath = new CHoverMoveMath();
seaMoveMath = new CShipMoveMath();
CRC crc;
for (int tt = 0; tt < CMapInfo::NUM_TERRAIN_TYPES; ++tt) {
const CMapInfo::TerrainType& terrType = mapInfo->terrainTypes[tt];
crc << terrType.tankSpeed << terrType.kbotSpeed;
crc << terrType.hoverSpeed << terrType.shipSpeed;
}
for (size_t num = 1; /* no test */; num++) {
const LuaTable moveTable = rootTable.SubTable(num);
if (!moveTable.IsValid()) {
break;
}
MoveData* md = new MoveData(this, moveTable, num);
moveData.push_back(md);
name2moveData[md->name] = md->pathType;
switch (md->moveType) {
case MoveData::Ship_Move: { md->moveMath = seaMoveMath; } break;
case MoveData::Hover_Move: { md->moveMath = hoverMoveMath; } break;
case MoveData::Ground_Move: { md->moveMath = groundMoveMath; } break;
}
crc << md->GetCheckSum();
}
CHoverMoveMath::noWaterMove = (mapInfo->water.damage >= MAX_ALLOWED_WATER_DAMAGE_HMM);
CGroundMoveMath::waterDamageCost = (mapInfo->water.damage >= MAX_ALLOWED_WATER_DAMAGE_GMM)?
0.0f: (1.0f / (1.0f + mapInfo->water.damage * 0.1f));
crc << CGroundMoveMath::waterDamageCost;
crc << CHoverMoveMath::noWaterMove;
moveInfoChecksum = crc.GetDigest();
}
CMoveInfo::~CMoveInfo()
{
while (!moveData.empty()) {
delete moveData.back();
moveData.pop_back();
}
delete groundMoveMath;
delete hoverMoveMath;
delete seaMoveMath;
}
MoveData* CMoveInfo::GetMoveDataFromName(const std::string& name)
{
map<string, int>::const_iterator it = name2moveData.find(name);
if (it == name2moveData.end()) {
return NULL;
}
return moveData[it->second];
}
MoveData::MoveData() {
name = "";
moveType = MoveData::Ground_Move;
moveFamily = MoveData::Tank;
terrainClass = MoveData::Mixed;
xsize = 0;
zsize = 0;
xsizeh = 0;
zsizeh = 0;
depth = 0.0f;
maxSlope = 1.0f;
slopeMod = 0.0f;
depthModParams[DEPTHMOD_MIN_HEIGHT] = 0.0f;
depthModParams[DEPTHMOD_MAX_HEIGHT] = std::numeric_limits<float>::max();
depthModParams[DEPTHMOD_MAX_SCALE ] = std::numeric_limits<float>::max();
depthModParams[DEPTHMOD_QUA_COEFF ] = 0.0f;
depthModParams[DEPTHMOD_LIN_COEFF ] = 0.1f;
depthModParams[DEPTHMOD_CON_COEFF ] = 1.0f;
speedModMults[SPEEDMOD_MOBILE_BUSY_MULT] = 0.10f;
speedModMults[SPEEDMOD_MOBILE_IDLE_MULT] = 0.35f;
speedModMults[SPEEDMOD_MOBILE_MOVE_MULT] = 0.65f;
speedModMults[SPEEDMOD_MOBILE_NUM_MULTS] = 0.0f;
crushStrength = 0.0f;
pathType = 0;
unitDefRefCount = 0;
followGround = true;
subMarine = false;
avoidMobilesOnPath = true;
heatMapping = true;
heatMod = 0.05f;
heatProduced = GAME_SPEED;
moveMath = NULL;
tempOwner = NULL;
}
MoveData::MoveData(CMoveInfo* moveInfo, const LuaTable& moveTable, int moveDefID) {
*this = MoveData();
name = StringToLower(moveTable.GetString("name", ""));
pathType = moveDefID - 1;
crushStrength = moveTable.GetFloat("crushStrength", 10.0f);
const LuaTable& depthModTable = moveTable.SubTable("depthModParams");
const LuaTable& speedModMultsTable = moveTable.SubTable("speedModMults");
const float minWaterDepth = moveTable.GetFloat("minWaterDepth", 10.0f);
const float maxWaterDepth = moveTable.GetFloat("maxWaterDepth", 0.0f);
if ((name.find("boat") != string::npos) ||
(name.find("ship") != string::npos)) {
moveType = MoveData::Ship_Move;
depth = minWaterDepth;
moveFamily = MoveData::Ship;
subMarine = moveTable.GetBool("subMarine", false);
} else if (name.find("hover") != string::npos) {
moveType = MoveData::Hover_Move;
maxSlope = DegreesToMaxSlope(moveTable.GetFloat("maxSlope", 15.0f));
moveFamily = MoveData::Hover;
} else {
moveType = MoveData::Ground_Move;
depth = maxWaterDepth;
depthModParams[DEPTHMOD_MIN_HEIGHT] = std::max(0.00f, depthModTable.GetFloat("minHeight", 0.0f ));
depthModParams[DEPTHMOD_MAX_HEIGHT] = ( depthModTable.GetFloat("maxHeight", std::numeric_limits<float>::max() ));
depthModParams[DEPTHMOD_MAX_SCALE ] = std::max(0.01f, depthModTable.GetFloat("maxScale", std::numeric_limits<float>::max() ));
depthModParams[DEPTHMOD_QUA_COEFF ] = std::max(0.00f, depthModTable.GetFloat("quadraticCoeff", 0.0f ));
depthModParams[DEPTHMOD_LIN_COEFF ] = std::max(0.00f, depthModTable.GetFloat("linearCoeff", moveTable.GetFloat("depthMod", 0.1f)));
depthModParams[DEPTHMOD_CON_COEFF ] = std::max(0.00f, depthModTable.GetFloat("constantCoeff", 1.0f ));
// ensure [depthModMinHeight, depthModMaxHeight] is a valid range
depthModParams[DEPTHMOD_MAX_HEIGHT] = std::max(depthModParams[DEPTHMOD_MIN_HEIGHT], depthModParams[DEPTHMOD_MAX_HEIGHT]);
maxSlope = DegreesToMaxSlope(moveTable.GetFloat("maxSlope", 60.0f));
if (name.find("tank") != string::npos) {
moveFamily = MoveData::Tank;
} else {
moveFamily = MoveData::KBot;
}
}
speedModMults[SPEEDMOD_MOBILE_BUSY_MULT] = std::max(0.01f, speedModMultsTable.GetFloat("mobileBusyMult", 0.10f));
speedModMults[SPEEDMOD_MOBILE_IDLE_MULT] = std::max(0.01f, speedModMultsTable.GetFloat("mobileIdleMult", 0.35f));
speedModMults[SPEEDMOD_MOBILE_MOVE_MULT] = std::max(0.01f, speedModMultsTable.GetFloat("mobileMoveMult", 0.65f));
avoidMobilesOnPath = moveTable.GetBool("avoidMobilesOnPath", false);
heatMapping = moveTable.GetBool("heatMapping", false);
heatMod = moveTable.GetFloat("heatMod", 50.0f);
heatProduced = moveTable.GetInt("heatProduced", GAME_SPEED * 2);
// <maxSlope> ranges from 0.0 to 60 * 1.5 degrees, ie. from 0.0 to
// 0.5 * PI radians, ie. from 1.0 - cos(0.0) to 1.0 - cos(0.5 * PI)
// = [0, 1] --> DEFAULT <slopeMod> values range from (4 / 0.001) to
// (4 / 1.001) = [4000.0, 3.996]
//
// speedMod values for a terrain-square slope in [0, 1] are given by
// (1.0 / (1.0 + slope * slopeMod)) and therefore have a MAXIMUM at
// <slope=0, slopeMod=...> and a MINIMUM at <slope=1, slopeMod=4000>
// (of 1.0 / (1.0 + 0.0 * ...) = 1.0 and 1.0 / (1.0 + 1.0 * 4000.0)
// = 0.00025 respectively)
//
slopeMod = moveTable.GetFloat("slopeMod", 4.0f / (maxSlope + 0.001f));
// ground units hug the ocean floor when in water,
// ships stay at a "fixed" level (their waterline)
followGround =
(moveFamily == MoveData::Tank ||
moveFamily == MoveData::KBot);
// tank or bot that cannot get its threads / feet
// wet, or hovercraft (which doesn't touch ground
// or water)
const bool b0 = ((followGround && maxWaterDepth <= 0.0) || moveFamily == MoveData::Hover);
// ship (or sub) that cannot crawl onto shore, OR tank or
// kbot restricted to snorkling (strange but possible)
const bool b1 = ((moveFamily == MoveData::Ship && minWaterDepth > 0.0) || ((followGround) && minWaterDepth > 0.0));
// tank or kbot that CAN go skinny-dipping (amph.),
// or ship that CAN sprout legs when at the beach
const bool b2 = ((followGround) && maxWaterDepth > 0.0) || (moveFamily == MoveData::Ship && minWaterDepth < 0.0);
if (b0) { terrainClass = MoveData::Land; }
if (b1) { terrainClass = MoveData::Water; }
if (b2) { terrainClass = MoveData::Mixed; }
const int xsizeDef = std::max(1, moveTable.GetInt("footprintX", 1));
const int zsizeDef = std::max(1, moveTable.GetInt("footprintZ", xsizeDef));
const int scale = 2;
// make all mobile footprints point-symmetric in heightmap space
// (meaning that only non-even dimensions are possible and each
// footprint always has a unique center square)
xsize = xsizeDef * scale;
zsize = zsizeDef * scale;
xsize -= ((xsize & 1)? 0: 1);
zsize -= ((zsize & 1)? 0: 1);
// precalculated data for MoveMath
xsizeh = xsize >> 1;
zsizeh = zsize >> 1;
assert((xsize & 1) == 1);
assert((zsize & 1) == 1);
}
bool MoveData::TestMoveSquare(const int hmx, const int hmz) const {
bool ret = true;
// test the entire footprint
for (int i = hmx - xsizeh; i <= hmx + xsizeh; i++) {
for (int j = hmz - zsizeh; j <= hmz + zsizeh; j++) {
const float speedMod = moveMath->GetPosSpeedMod(*this, hmx + i, hmz + j);
const CMoveMath::BlockType blockBits = moveMath->IsBlocked(*this, hmx + i, hmz + j);
// check both terrain and the blocking-map
ret &= ((speedMod > 0.0f) && ((blockBits & CMoveMath::BLOCK_STRUCTURE) == 0));
}
}
return ret;
}
float MoveData::GetDepthMod(const float height) const {
// [DEPTHMOD_{MIN, MAX}_HEIGHT] are always >= 0,
// so we return early for positive height values
// only negative heights ("depths") are allowed
if (height > -depthModParams[DEPTHMOD_MIN_HEIGHT]) { return 1.0f; }
if (height < -depthModParams[DEPTHMOD_MAX_HEIGHT]) { return 0.0f; }
const float a = depthModParams[DEPTHMOD_QUA_COEFF];
const float b = depthModParams[DEPTHMOD_LIN_COEFF];
const float c = depthModParams[DEPTHMOD_CON_COEFF];
const float minScale = 0.01f;
const float maxScale = depthModParams[DEPTHMOD_MAX_SCALE];
const float depth = -height;
const float scale = Clamp((a * depth * depth + b * depth + c), minScale, maxScale);
// NOTE:
// <maxScale> is guaranteed to be >= 0.01, so the
// depth-mod range is [1.0 / 0.01, 1.0 / +infinity]
//
// if minScale <= scale < 1.0, speedup
// if 1.0 < scale <= maxScale, slowdown
return (1.0f / scale);
}
unsigned int MoveData::GetCheckSum() const {
unsigned int sum = 0;
const unsigned char* minByte = reinterpret_cast<const unsigned char*>(&moveType);
const unsigned char* maxByte = reinterpret_cast<const unsigned char*>(&heatProduced) + sizeof(heatProduced);
assert(minByte < maxByte);
// NOTE:
// safe so long as MoveData has no virtuals and we
// make sure we do not checksum the pointer-members
for (const unsigned char* byte = minByte; byte != maxByte; byte++) {
sum ^= ((((byte + 1) - minByte) << 8) * (*byte));
}
return sum;
}
|