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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "ScriptMoveType.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Sim/Misc/Wind.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitTypes/Building.h"
#include "System/EventHandler.h"
#include "System/Matrix44f.h"
#include "System/myMath.h"
CR_BIND_DERIVED(CScriptMoveType, AMoveType, (NULL))
CR_REG_METADATA(CScriptMoveType, (
CR_MEMBER(tag),
CR_MEMBER(extrapolate),
CR_MEMBER(useRelVel),
CR_MEMBER(useRotVel),
CR_MEMBER(drag),
CR_MEMBER(velVec),
CR_MEMBER(relVel),
CR_MEMBER(rot),
CR_MEMBER(rotVel),
CR_MEMBER(mins),
CR_MEMBER(maxs),
CR_MEMBER(trackSlope),
CR_MEMBER(trackGround),
CR_MEMBER(groundOffset),
CR_MEMBER(gravityFactor),
CR_MEMBER(windFactor),
CR_MEMBER(noBlocking), // copy of CSolidObject::PSTATE_BIT_BLOCKING
CR_MEMBER(gndStop),
CR_MEMBER(shotStop),
CR_MEMBER(slopeStop),
CR_MEMBER(collideStop),
CR_MEMBER(scriptNotify),
CR_RESERVED(64)
))
CScriptMoveType::CScriptMoveType(CUnit* owner):
AMoveType(owner),
tag(0),
extrapolate(true),
useRelVel(false),
useRotVel(false),
drag(0.0f),
velVec(ZeroVector),
relVel(ZeroVector),
rot(ZeroVector),
rotVel(ZeroVector),
mins(-1.0e9f, -1.0e9f, -1.0e9f),
maxs(+1.0e9f, +1.0e9f, +1.0e9f),
trackSlope(false),
trackGround(false),
groundOffset(0.0f),
gravityFactor(0.0f),
windFactor(0.0f),
noBlocking(false),
gndStop(false),
shotStop(false),
slopeStop(false),
collideStop(false),
scriptNotify(0)
{
useHeading = false; // use the transformation matrix instead of heading
oldPos = owner? owner->pos: ZeroVector;
oldSlowUpdatePos = oldPos;
}
CScriptMoveType::~CScriptMoveType()
{
// clean up if noBlocking was made true at
// some point during this script's lifetime
// and not reset
owner->UnBlock();
}
inline void CScriptMoveType::CalcDirections()
{
CMatrix44f matrix;
matrix.RotateY(-rot.y);
matrix.RotateX(-rot.x);
matrix.RotateZ(-rot.z);
owner->SetDirVectors(matrix);
owner->UpdateMidAndAimPos();
owner->SetHeadingFromDirection();
}
void CScriptMoveType::CheckNotify()
{
if (scriptNotify) {
if (eventHandler.MoveCtrlNotify(owner, scriptNotify)) {
// NOTE: deletes \<this\>
owner->DisableScriptMoveType();
} else {
scriptNotify = 0;
}
}
}
bool CScriptMoveType::Update()
{
if (useRotVel) {
rot += rotVel;
CalcDirections();
}
if (extrapolate) {
// NOTE: only gravitational acc. is allowed to build up velocity
// NOTE: strong wind plus low gravity can cause substantial drift
const float3 gravVec = UpVector * (mapInfo->map.gravity * gravityFactor);
const float3 windVec = (wind.GetCurrentWind() * windFactor);
const float3 unitVec = useRelVel?
(owner->frontdir * relVel.z) +
(owner->updir * relVel.y) +
(owner->rightdir * -relVel.x):
ZeroVector;
owner->Move(gravVec + velVec, true);
owner->Move(windVec, true);
owner->Move(unitVec, true);
// quadratic drag does not work well here
velVec += gravVec;
velVec *= (1.0f - drag);
owner->SetVelocityAndSpeed(velVec);
}
if (trackGround) {
const float gndMin = CGround::GetHeightReal(owner->pos.x, owner->pos.z) + groundOffset;
if (owner->pos.y <= gndMin) {
owner->Move(UpVector * (gndMin - owner->pos.y), true);
owner->speed.y = 0.0f;
if (gndStop) {
velVec = ZeroVector;
relVel = ZeroVector;
rotVel = ZeroVector;
scriptNotify = 1;
}
}
}
// positional clamps
CheckLimits();
if (trackSlope) {
owner->UpdateDirVectors(true);
owner->UpdateMidAndAimPos();
}
// don't need the rest if the pos hasn't changed
if (oldPos == owner->pos) {
CheckNotify();
return false;
}
oldPos = owner->pos;
if (!noBlocking) {
owner->Block();
}
CheckNotify();
return true;
}
void CScriptMoveType::CheckLimits()
{
if (owner->pos.x < mins.x) { owner->pos.x = mins.x; owner->speed.x = 0.0f; }
if (owner->pos.x > maxs.x) { owner->pos.x = maxs.x; owner->speed.x = 0.0f; }
if (owner->pos.y < mins.y) { owner->pos.y = mins.y; owner->speed.y = 0.0f; }
if (owner->pos.y > maxs.y) { owner->pos.y = maxs.y; owner->speed.y = 0.0f; }
if (owner->pos.z < mins.z) { owner->pos.z = mins.z; owner->speed.z = 0.0f; }
if (owner->pos.z > maxs.z) { owner->pos.z = maxs.z; owner->speed.z = 0.0f; }
owner->UpdateMidAndAimPos();
}
void CScriptMoveType::SetPhysics(const float3& _pos, const float3& _vel, const float3& _rot)
{
SetPosition(_pos);
SetVelocity(_vel);
SetRotation(_rot);
}
void CScriptMoveType::SetPosition(const float3& _pos) { owner->Move(_pos, false); }
void CScriptMoveType::SetVelocity(const float3& _vel) { owner->SetVelocityAndSpeed(velVec = _vel); }
void CScriptMoveType::SetRelativeVelocity(const float3& _relVel)
{
relVel = _relVel;
useRelVel = ((relVel.x != 0.0f) || (relVel.y != 0.0f) || (relVel.z != 0.0f));
}
void CScriptMoveType::SetRotation(const float3& _rot)
{
rot = _rot;
CalcDirections();
}
void CScriptMoveType::SetRotationVelocity(const float3& _rotVel)
{
rotVel = _rotVel;
useRotVel = ((rotVel.x != 0.0f) || (rotVel.y != 0.0f) || (rotVel.z != 0.0f));
}
void CScriptMoveType::SetHeading(short heading)
{
owner->heading = heading;
if (!trackSlope) {
owner->UpdateDirVectors(false);
owner->UpdateMidAndAimPos();
}
}
void CScriptMoveType::SetNoBlocking(bool state)
{
// if false, forces blocking-map updates
noBlocking = state;
if (noBlocking) {
owner->UnBlock();
} else {
owner->Block();
}
}
|