File: ScriptMoveType.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (342 lines) | stat: -rw-r--r-- 7,844 bytes parent folder | download
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
#include "StdAfx.h"
#include "mmgr.h"

#include "ScriptMoveType.h"

#include "Lua/LuaRules.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Rendering/GroundDecalHandler.h"
#include "Rendering/UnitModels/3DModel.h"
#include "Sim/Misc/Wind.h"
#include "Sim/Misc/AirBaseHandler.h"
#include "Sim/Misc/LosHandler.h"
#include "Sim/Misc/QuadField.h"
#include "Sim/Misc/RadarHandler.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitTypes/Building.h"
#include "GlobalUnsynced.h"
#include "Matrix44f.h"
#include "myMath.h"

CR_BIND_DERIVED(CScriptMoveType, AMoveType, (NULL));
CR_REG_METADATA(CScriptMoveType, (
	CR_MEMBER(tag),
	CR_MEMBER(extrapolate),
	CR_MEMBER(drag),
	CR_MEMBER(vel),
	CR_MEMBER(relVel),
	CR_MEMBER(useRelVel),
	CR_MEMBER(rot),
	CR_MEMBER(rotVel),
	CR_MEMBER(useRotVel),
	CR_MEMBER(trackSlope),
	CR_MEMBER(trackGround),
	CR_MEMBER(groundOffset),
	CR_MEMBER(gravityFactor),
	CR_MEMBER(windFactor),
	CR_MEMBER(mins),
	CR_MEMBER(maxs),
	CR_MEMBER(noBlocking), // copy of CSolidObject::isMarkedOnBlockingMap
	CR_MEMBER(gndStop),
	CR_MEMBER(shotStop),
	CR_MEMBER(slopeStop),
	CR_MEMBER(collideStop),
	CR_MEMBER(leaveTracks),
	CR_MEMBER(hasDecal),
	CR_MEMBER(isBuilding),
	CR_MEMBER(isBlocking), // copy of CSolidObject::blocking (no longer used)
	CR_MEMBER(rotOffset),
	CR_MEMBER(lastTrackUpdate),
	CR_MEMBER(oldPos),
	CR_MEMBER(oldSlowUpdatePos),
	CR_MEMBER(scriptNotify),
	CR_RESERVED(64)
	));


CScriptMoveType::CScriptMoveType(CUnit* owner):
	AMoveType(owner),
	tag(0),
	extrapolate(true),
	drag(0.0f),
	vel(0.0f, 0.0f, 0.0f),
	relVel(0.0f, 0.0f, 0.0f),
	useRelVel(false),
	rot(0.0f, 0.0f, 0.0f),
	rotVel(0.0f, 0.0f, 0.0f),
	useRotVel(false),
	trackSlope(false),
	trackGround(false),
	groundOffset(0.0f),
	gravityFactor(0.0f),
	windFactor(0.0f),
	mins(-1.0e9f, -1.0e9f, -1.0e9f),
	maxs(+1.0e9f, +1.0e9f, +1.0e9f),
	noBlocking(false),
	gndStop(false),
	shotStop(false),
	slopeStop(false),
	collideStop(false),
	leaveTracks(true),
	hasDecal(false),
	isBuilding(false),
	rotOffset(0.0f, 0.0f, 0.0f),
	lastTrackUpdate(0),
	oldPos(owner ? owner->pos:float3(0,0,0)),
	oldSlowUpdatePos(oldPos),
	scriptNotify(0)
{
	useHeading = false; // use the transformation matrix instead of heading

	if (owner) {
		const UnitDef* unitDef = owner->unitDef;
		isBuilding = (unitDef->type == "Building");
	} else {
		isBuilding = false;
	}
}


CScriptMoveType::~CScriptMoveType(void)
{
	// clean up if noBlocking was made true at
	// some point during this script's lifetime
	// and not reset
	owner->Block();
}

#if defined(USE_GML) && defined(__GNUC__) && (__GNUC__ == 4)
// This is supposed to fix some GCC crashbug related to threading
// The MOVAPS SSE instruction is otherwise getting misaligned data
__attribute__ ((force_align_arg_pointer))
#endif
inline void CScriptMoveType::CalcDirections()
{
	CMatrix44f matrix;
	//matrix.Translate(-rotOffset);
	matrix.RotateY(-rot.y);
	matrix.RotateX(-rot.x);
	matrix.RotateZ(-rot.z);
	//matrix.Translate(rotOffset);
	owner->rightdir.x = -matrix[ 0];
	owner->rightdir.y = -matrix[ 1];
	owner->rightdir.z = -matrix[ 2];
	owner->updir.x    =  matrix[ 4];
	owner->updir.y    =  matrix[ 5];
	owner->updir.z    =  matrix[ 6];
	owner->frontdir.x =  matrix[ 8];
	owner->frontdir.y =  matrix[ 9];
	owner->frontdir.z =  matrix[10];

	const shortint2 HandP = GetHAndPFromVector(owner->frontdir);
	owner->heading = HandP.x;
}


void CScriptMoveType::SlowUpdate()
{
	const float3& pos = owner->pos;

	// make sure the unit is in the map
	// pos.CheckInBounds();

	// don't need the rest if the pos hasn't changed
	if (pos == oldSlowUpdatePos) {
		return;
	}
	oldSlowUpdatePos = pos;

	const int newmapSquare = ground->GetSquare(pos);
	if (newmapSquare != owner->mapSquare){
		owner->mapSquare = newmapSquare;

		loshandler->MoveUnit(owner, false);
		if (owner->hasRadarCapacity) {
			radarhandler->MoveUnit(owner);
		}
	}
	qf->MovedUnit(owner);

	owner->isUnderWater = ((owner->pos.y + owner->model->height) < 0.0f);
};


void CScriptMoveType::CheckNotify()
{
	if (scriptNotify) {
		if (luaRules && luaRules->MoveCtrlNotify(owner, scriptNotify)) {
			owner->DisableScriptMoveType();
		}
		scriptNotify = 0;
	}
}


void CScriptMoveType::Update()
{
	if (useRotVel) {
		rot += rotVel;
		CalcDirections();
	}

	owner->speed = vel;
	if (extrapolate) {
		if (drag != 0.0f) {
			vel *= (1.0f - drag); // quadratic drag does not work well here
		}
		if (useRelVel) {
			const float3 rVel = (owner->frontdir *  relVel.z) +
			                    (owner->updir    *  relVel.y) +
			                    (owner->rightdir * -relVel.x); // x is left
			owner->speed += rVel;
		}
		vel.y        += mapInfo->map.gravity * gravityFactor;
		owner->speed += (wind.GetCurrentWind() * windFactor);
		owner->pos   += owner->speed;
	}

	if (trackGround) {
		const float gndMin =
			ground->GetHeight2(owner->pos.x, owner->pos.z) + groundOffset;
		if (owner->pos.y <= gndMin) {
			owner->pos.y = gndMin;
			owner->speed.y = 0.0f;
			if (gndStop) {
				owner->speed.y = 0.0f;
				vel    = ZeroVector;
				relVel = ZeroVector;
				rotVel = ZeroVector;
				scriptNotify = 1;
			}
		}
	}

	// positional clamps
	CheckLimits();

	if (trackSlope) {
		TrackSlope();
	}

	owner->UpdateMidPos();

	// don't need the rest if the pos hasn't changed
	if (oldPos == owner->pos) {
		CheckNotify();
		return;
	}

	oldPos = owner->pos;

	if (!noBlocking) {
		owner->Block();
	}

	if (groundDecals && owner->unitDef->leaveTracks && leaveTracks &&
	    (lastTrackUpdate < (gs->frameNum - 7)) &&
	    ((owner->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectatingFullView)) {
		lastTrackUpdate = gs->frameNum;
		groundDecals->UnitMoved(owner);
	}

	CheckNotify();
};


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; }
}


void CScriptMoveType::SetPhysics(const float3& pos,
                                 const float3& vel,
                                 const float3& rot)
{
	owner->pos = pos;
	owner->speed = vel;
	SetRotation(rot);
	return;
}


void CScriptMoveType::SetPosition(const float3& pos)
{
	owner->pos = pos;
	return;
}


void CScriptMoveType::SetVelocity(const float3& _vel)
{
	vel = _vel;
	return;
}


void CScriptMoveType::SetRelativeVelocity(const float3& _relVel)
{
	relVel = _relVel;
	useRelVel = ((relVel.x != 0.0f) || (relVel.y != 0.0f) || (relVel.z != 0.0f));
	return;
}


void CScriptMoveType::SetRotation(const float3& _rot)
{
	rot = _rot;
	CalcDirections();
}


void CScriptMoveType::SetRotationVelocity(const float3& rvel)
{
	rotVel = rvel;
	useRotVel = ((rotVel.x != 0.0f) || (rotVel.y != 0.0f) || (rotVel.z != 0.0f));
	return;
}


void CScriptMoveType::SetRotationOffset(const float3& rotOff)
{
	rotOffset = rotOff;
}


void CScriptMoveType::SetHeading(short heading)
{
	owner->heading = heading;
	if (!trackSlope) {
		owner->frontdir = GetVectorFromHeading(heading);
		owner->updir = UpVector;
		owner->rightdir = owner->frontdir.cross(UpVector);
	}
}


void CScriptMoveType::SetNoBlocking(bool state)
{
	// if false, forces blocking-map updates
	noBlocking = state;

	if (noBlocking) {
		owner->UnBlock();
	} else {
		owner->Block();
	}
}

void CScriptMoveType::TrackSlope()
{
	owner->frontdir = GetVectorFromHeading(owner->heading);
	owner->updir = ground->GetSmoothNormal(owner->pos.x, owner->pos.z);
	owner->rightdir = owner->frontdir.cross(owner->updir);
	owner->rightdir.Normalize();
	owner->frontdir = owner->updir.cross(owner->rightdir);
}