File: AAirMoveType.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (287 lines) | stat: -rw-r--r-- 8,692 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "AAirMoveType.h"

#include "Game/GlobalUnsynced.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Rendering/Env/Particles/Classes/SmokeProjectile.h"
#include "Sim/Misc/QuadField.h"
#include "Sim/Misc/SmoothHeightMesh.h"
#include "Sim/Projectiles/ExplosionGenerator.h"
#include "Sim/Projectiles/ProjectileMemPool.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "System/SpringMath.h"

CR_BIND_DERIVED_INTERFACE(AAirMoveType, AMoveType)

CR_REG_METADATA(AAirMoveType, (
	CR_MEMBER(aircraftState),
	CR_MEMBER(collisionState),

	CR_MEMBER(oldGoalPos),
	CR_MEMBER(reservedLandingPos),

	CR_MEMBER(landRadiusSq),
	CR_MEMBER(wantedHeight),
	CR_MEMBER(orgWantedHeight),

	CR_MEMBER(accRate),
	CR_MEMBER(decRate),
	CR_MEMBER(altitudeRate),

	CR_MEMBER(collide),
	CR_MEMBER(autoLand),
	CR_MEMBER(dontLand),
	CR_MEMBER(useSmoothMesh),
	CR_MEMBER(canSubmerge),
	CR_MEMBER(floatOnWater),

	CR_MEMBER(lastCollidee),

	CR_MEMBER(crashExpGenID)
))



static inline float AAMTGetGroundHeightAW(float x, float z) { return CGround::GetHeightAboveWater(x, z); }
static inline float AAMTGetGroundHeight  (float x, float z) { return CGround::GetHeightReal      (x, z); }
static inline float AAMTGetSmoothGroundHeightAW(float x, float z) { return smoothGround.GetHeightAboveWater(x, z); }
static inline float AAMTGetSmoothGroundHeight  (float x, float z) { return smoothGround.GetHeight          (x, z); }
static inline float HAMTGetMaxGroundHeight(float x, float z) { return std::max(smoothGround.GetHeight(x, z), CGround::GetApproximateHeight(x, z)); }
static inline float SAMTGetMaxGroundHeight(float x, float z) { return std::max(smoothGround.GetHeight(x, z), CGround::GetHeightAboveWater(x, z)); }

static inline void AAMTEmitEngineTrail(CUnit* owner, unsigned int) {
	projMemPool.alloc<CSmokeProjectile>(owner, owner->midPos, guRNG.NextVector() * 0.08f, (100.0f + guRNG.NextFloat() * 50.0f), 5.0f, 0.2f, 0.4f);
}
static inline void AAMTEmitCustomTrail(CUnit* owner, unsigned int id) {
	explGenHandler.GenExplosion(id, owner->midPos, owner->frontdir, 1.0f, 0.0f, 1.0f, owner, nullptr);
}


AAirMoveType::GetGroundHeightFunc amtGetGroundHeightFuncs[6] = {
	AAMTGetGroundHeightAW,       // canSubmerge=0 useSmoothMesh=0
	AAMTGetGroundHeight  ,       // canSubmerge=1 useSmoothMesh=0
	AAMTGetSmoothGroundHeightAW, // canSubmerge=0 useSmoothMesh=1
	AAMTGetSmoothGroundHeight,   // canSubmerge=1 useSmoothMesh=1
	HAMTGetMaxGroundHeight,      // HoverAirMoveType::UpdateFlying
	SAMTGetMaxGroundHeight,      // StrafeAirMoveType::UpdateFlying
};

AAirMoveType::EmitCrashTrailFunc amtEmitCrashTrailFuncs[2] = {
	AAMTEmitEngineTrail,
	AAMTEmitCustomTrail,
};



AAirMoveType::AAirMoveType(CUnit* unit): AMoveType(unit)
{
	// creg
	if (unit == nullptr)
		return;

	const UnitDef* ud = owner->unitDef;

	oldGoalPos = unit->pos;

	// same as {Ground, HoverAir}MoveType::accRate
	accRate = std::max(0.01f, ud->maxAcc);
	decRate = std::max(0.01f, ud->maxDec);
	altitudeRate = std::max(0.01f, ud->verticalSpeed);
	landRadiusSq = Square(BrakingDistance(maxSpeed, decRate));

	collide = ud->collide;
	dontLand = ud->DontLand();
	useSmoothMesh = ud->useSmoothMesh;
	canSubmerge = ud->canSubmerge;
	floatOnWater = ud->floatOnWater;

	UseHeading(false);

	if (ud->GetCrashExpGenCount() > 0) {
		crashExpGenID = guRNG.NextInt(ud->GetCrashExpGenCount());
		crashExpGenID = ud->GetCrashExpGenID(crashExpGenID);
	}
}


bool AAirMoveType::UseSmoothMesh() const {
	if (!useSmoothMesh)
		return false;

	const CCommandAI* cai = owner->commandAI;
	const CCommandQueue& cq = cai->commandQue;
	const Command& fc = (cq.empty())? Command(CMD_STOP): cq.front();

	const bool closeGoalPos = (goalPos.SqDistance2D(owner->pos) < Square(landRadiusSq * 2.0f));
	const bool transportCmd = ((fc.GetID() == CMD_LOAD_UNITS) || (fc.GetID() == CMD_UNLOAD_UNIT));
	const bool forceDisable = ((transportCmd && closeGoalPos) || (aircraftState != AIRCRAFT_FLYING && aircraftState != AIRCRAFT_HOVERING));

	return !forceDisable;
}

void AAirMoveType::DependentDied(CObject* o) {
	if (o == lastCollidee) {
		lastCollidee = nullptr;
		collisionState = COLLISION_NOUNIT;
	}
}

bool AAirMoveType::Update() {
	// NOTE: useHeading is never true by default for aircraft (AAirMoveType
	// forces it to false, while only CUnit::{Attach,Detach}Unit manipulate
	// it specifically for HoverAirMoveType's)
	if (UseHeading()) {
		SetState(AIRCRAFT_TAKEOFF);
		UseHeading(false);
	}

	// prevent UnitMoved event spam
	return false;
}

void AAirMoveType::UpdateLanded()
{
	// while an aircraft is being built we do not adjust its
	// position, because the builder might be a tall platform
	if (owner->beingBuilt)
		return;

	// when an aircraft transitions to the landed state it
	// is on the ground, but the terrain can be raised (or
	// lowered) later and we do not want to end up hovering
	// in mid-air or sink below it
	// let gravity do the job instead of teleporting
	const float minHeight = amtGetGroundHeightFuncs[owner->unitDef->canSubmerge](owner->pos.x, owner->pos.z);
	const float curHeight = owner->pos.y;

	if (curHeight > minHeight) {
		if (curHeight > 0.0f) {
			owner->speed.y += mapInfo->map.gravity;
		} else {
			owner->speed.y = mapInfo->map.gravity;
		}
	} else {
		owner->speed.y = 0.0f;
	}

	owner->SetVelocityAndSpeed(owner->speed + owner->GetDragAccelerationVec(float4(0.0f, 0.0f, 0.0f, 0.1f)));
	owner->Move(UpVector * (std::max(curHeight, minHeight) - owner->pos.y), true);
	owner->Move(owner->speed, true);
	// match the terrain normal
	owner->UpdateDirVectors(owner->IsOnGround(), false);
	owner->UpdateMidAndAimPos();
}

void AAirMoveType::LandAt(float3 pos, float distanceSq)
{
	if (distanceSq < 0.0f)
		distanceSq = Square(BrakingDistance(maxSpeed, decRate));

	if (aircraftState != AIRCRAFT_LANDING)
		SetState(AIRCRAFT_LANDING);

	landRadiusSq = std::max(distanceSq, Square(std::max(owner->radius, 10.0f)));
	reservedLandingPos = pos;
	const float3 originalPos = owner->pos;
	owner->Move(reservedLandingPos, false);
	owner->Block();
	owner->Move(originalPos, false);

	wantedHeight = reservedLandingPos.y - amtGetGroundHeightFuncs[owner->unitDef->canSubmerge](reservedLandingPos.x, reservedLandingPos.z);
}


void AAirMoveType::UpdateLandingHeight(float newWantedHeight)
{
	wantedHeight = newWantedHeight;
	reservedLandingPos.y = wantedHeight + amtGetGroundHeightFuncs[owner->unitDef->canSubmerge](reservedLandingPos.x, reservedLandingPos.z);
}


void AAirMoveType::UpdateLanding()
{
	const float3& pos = owner->pos;

	const float radius = std::max(owner->radius, 10.0f);
	const float radiusSq = radius * radius;
	const float distSq = reservedLandingPos.SqDistance(pos);


	const float localAltitude = pos.y - amtGetGroundHeightFuncs[owner->unitDef->canSubmerge](owner->pos.x, owner->pos.z);

	if (distSq <= radiusSq || (distSq < landRadiusSq && localAltitude < wantedHeight + radius)) {
		SetState(AIRCRAFT_LANDED);
		owner->SetVelocityAndSpeed(UpVector * owner->speed);
	}
}


void AAirMoveType::CheckForCollision()
{
	if (!collide)
		return;

	const SyncedFloat3& pos = owner->midPos;
	const SyncedFloat3& forward = owner->frontdir;

	float dist = 200.0f;

	QuadFieldQuery qfQuery;
	quadField.GetUnitsExact(qfQuery, pos + forward * 121.0f, dist);

	if (lastCollidee != nullptr) {
		DeleteDeathDependence(lastCollidee, DEPENDENCE_LASTCOLWARN);

		lastCollidee = nullptr;
		collisionState = COLLISION_NOUNIT;
	}

	// find closest potential collidee
	for (CUnit* unit: *qfQuery.units) {
		if (unit == owner || !unit->unitDef->canfly)
			continue;

		const SyncedFloat3& op = unit->midPos;
		const float3 dif = op - pos;
		const float3 forwardDif = forward * (forward.dot(dif));

		if (forwardDif.SqLength() >= (dist * dist))
			continue;

		const float3 ortoDif = dif - forwardDif;
		const float frontLength = forwardDif.Length();
		// note: radii are multiplied by two
		const float minOrtoDif = (unit->radius + owner->radius) * 2.0f + frontLength * 0.1f + 10.0f;

		if (ortoDif.SqLength() < (minOrtoDif * minOrtoDif)) {
			dist = frontLength;
			lastCollidee = const_cast<CUnit*>(unit);
		}
	}

	if (lastCollidee != nullptr) {
		collisionState = COLLISION_DIRECT;
		AddDeathDependence(lastCollidee, DEPENDENCE_LASTCOLWARN);
		return;
	}

	for (CUnit* u: *qfQuery.units) {
		if (u == owner)
			continue;

		if ((u->midPos - pos).SqLength() > Square((owner->radius + u->radius) * 2.0f))
			continue;

		lastCollidee = u;
	}

	if (lastCollidee != nullptr) {
		collisionState = COLLISION_NEARBY;
		AddDeathDependence(lastCollidee, DEPENDENCE_LASTCOLWARN);
		return;
	}
}