forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLaserUpdate.cpp
More file actions
411 lines (359 loc) · 12.6 KB
/
LaserUpdate.cpp
File metadata and controls
411 lines (359 loc) · 12.6 KB
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
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: LaserUpdate.cpp //////////////////////////////////////////////////////////////////////////
// Author: Kris Morness, July 2002
// Desc: Handles laser update processing for render purposes and game control.
///////////////////////////////////////////////////////////////////////////////////////////////////
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/GameUtility.h"
#include "Common/Xfer.h"
#include "Common/DrawModule.h"
#include "GameClient/Drawable.h"
#include "GameClient/GameClient.h"
#include "GameClient/ParticleSys.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Object.h"
#include "GameLogic/Module/LaserUpdate.h"
#include "WWMath/vector3.h"
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
LaserUpdateModuleData::LaserUpdateModuleData()
{
m_parentFireBoneOnTurret = FALSE;
}
//-------------------------------------------------------------------------------------------------
/*static*/ void LaserUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
{
ModuleData::buildFieldParse(p);
static const FieldParse dataFieldParse[] =
{
{ "MuzzleParticleSystem", INI::parseAsciiString, nullptr, offsetof( LaserUpdateModuleData, m_particleSystemName ) },
{ "TargetParticleSystem", INI::parseAsciiString, nullptr, offsetof( LaserUpdateModuleData, m_targetParticleSystemName ) },
{ "ParentFireBoneName", INI::parseAsciiString, nullptr, offsetof( LaserUpdateModuleData, m_parentFireBoneName ) },
{ "ParentFireBoneOnTurret", INI::parseAsciiString, nullptr, offsetof( LaserUpdateModuleData, m_parentFireBoneOnTurret ) },
{ nullptr, nullptr, nullptr, 0 }
};
p.add(dataFieldParse);
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
LaserRadiusUpdate::LaserRadiusUpdate()
{
m_widening = false;
m_widenStartFrame = 0;
m_widenFinishFrame = 0;
m_currentWidthScalar = 1.0f;
m_decaying = false;
m_decayStartFrame = 0;
m_decayFinishFrame = 0;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
LaserUpdate::LaserUpdate( Thing *thing, const ModuleData* moduleData ) : ClientUpdateModule( thing, moduleData )
{
m_dirty = FALSE;
m_endPos.zero();
m_startPos.zero();
m_particleSystemID = INVALID_PARTICLE_SYSTEM_ID;
m_targetParticleSystemID = INVALID_PARTICLE_SYSTEM_ID;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
LaserUpdate::~LaserUpdate( void )
{
if( m_particleSystemID )
TheParticleSystemManager->destroyParticleSystemByID( m_particleSystemID );
if( m_targetParticleSystemID )
TheParticleSystemManager->destroyParticleSystemByID( m_targetParticleSystemID );
}
//-------------------------------------------------------------------------------------------------
/** The update callback. */
//-------------------------------------------------------------------------------------------------
void LaserUpdate::clientUpdate( void )
{
m_dirty |= m_laserRadius.updateRadius();
}
//-------------------------------------------------------------------------------------------------
bool LaserRadiusUpdate::updateRadius()
{
bool updated = false;
if( m_decaying )
{
UnsignedInt now = TheGameLogic->getFrame();
m_currentWidthScalar = 1.0f - (Real)(now - m_decayStartFrame) / (Real)(m_decayFinishFrame - m_decayStartFrame);
updated = true;
if( m_currentWidthScalar <= 0.0f )
{
m_currentWidthScalar = 0.0f;
//m_decaying = false // ?????
}
}
else if( m_widening )
{
//We need to resize our laser width based on the growth ratio completed.
UnsignedInt now = TheGameLogic->getFrame();
m_currentWidthScalar = (Real)(now - m_widenStartFrame) / (Real)(m_widenFinishFrame - m_widenStartFrame);
updated = true;
if( m_currentWidthScalar >= 1.0f )
{
m_currentWidthScalar = 1.0f;
m_widening = false;
}
}
return updated;
}
//-------------------------------------------------------------------------------------------------
void LaserRadiusUpdate::setDecayFrames( UnsignedInt decayFrames )
{
if( decayFrames > 0 )
{
m_decaying = true;
m_decayStartFrame = TheGameLogic->getFrame();
m_decayFinishFrame = m_decayStartFrame + decayFrames;
m_currentWidthScalar = 1.0f;
}
}
//-------------------------------------------------------------------------------------------------
void LaserRadiusUpdate::initRadius( Int sizeDeltaFrames )
{
if( sizeDeltaFrames > 0 )
{
m_widening = true;
m_widenStartFrame = TheGameLogic->getFrame();
m_widenFinishFrame = m_widenStartFrame + sizeDeltaFrames;
m_currentWidthScalar = 0.0f;
}
else if( sizeDeltaFrames < 0 )
{
m_decaying = true;
m_decayStartFrame = TheGameLogic->getFrame();
m_decayFinishFrame = m_decayStartFrame - sizeDeltaFrames;
m_currentWidthScalar = 1.0f;
}
}
//-------------------------------------------------------------------------------------------------
void LaserRadiusUpdate::xfer( Xfer *xfer )
{
// widening
xfer->xferBool( &m_widening );
// decaying
xfer->xferBool( &m_decaying );
// widen start frame
xfer->xferUnsignedInt( &m_widenStartFrame );
// widen finish frame
xfer->xferUnsignedInt( &m_widenFinishFrame );
// current width scalar
xfer->xferReal( &m_currentWidthScalar );
// decay start frame
xfer->xferUnsignedInt( &m_decayStartFrame );
// decay finish frame
xfer->xferUnsignedInt( &m_decayFinishFrame );
}
//-------------------------------------------------------------------------------------------------
void LaserUpdate::initLaser( const Object *parent, const Coord3D *startPos, const Coord3D *endPos, Int sizeDeltaFrames )
{
const LaserUpdateModuleData *data = getLaserUpdateModuleData();
ParticleSystem *system;
m_laserRadius.initRadius( sizeDeltaFrames );
//Compute startPos
if( parent && data->m_parentFireBoneName.isNotEmpty() )
{
// Override startPos with the logic bone position
if( data->m_parentFireBoneOnTurret )
{
if( !parent->getSingleLogicalBonePositionOnTurret( TURRET_MAIN, data->m_parentFireBoneName.str(), &m_startPos, nullptr ) )
{
// failed to find the required bone, so just die
TheGameClient->destroyDrawable( getDrawable() );
return;
}
}
else
{
if( !parent->getSingleLogicalBonePosition( data->m_parentFireBoneName.str(), &m_startPos, nullptr ) )
{
// failed to find the required bone, so just die
TheGameClient->destroyDrawable( getDrawable() );
return;
}
}
}
else if( startPos )
{
// or just use what they gave
m_startPos = *startPos;
}
else
{
// if they gave nothing, then we are screwed
TheGameClient->destroyDrawable( getDrawable() );
return;
}
//Compute endPos
if( endPos != nullptr )
{
// just use what they gave, no override here
m_endPos = *endPos;
}
else
{
// if they gave nothing, then we are screwed
TheGameClient->destroyDrawable( getDrawable() );
return;
}
if( !m_particleSystemID )
{
//If we don't have a particle system for the lense flare (muzzle flare), create it.
if( data->m_particleSystemName.isNotEmpty() )
{
const ParticleSystemTemplate *tmp = TheParticleSystemManager->findTemplate( data->m_particleSystemName );
if( tmp )
{
system = TheParticleSystemManager->createParticleSystem( tmp );
if( system )
{
m_particleSystemID = system->getSystemID();
}
}
}
//If we don't have a particle system for the target effect, create it.
if( data->m_targetParticleSystemName.isNotEmpty() )
{
const ParticleSystemTemplate *tmp = TheParticleSystemManager->findTemplate( data->m_targetParticleSystemName );
if( tmp )
{
system = TheParticleSystemManager->createParticleSystem( tmp );
if( system )
{
m_targetParticleSystemID = system->getSystemID();
}
}
}
}
//Adjust the position of any existing particle system.
if( m_particleSystemID )
{
system = TheParticleSystemManager->findParticleSystem( m_particleSystemID );
if( system )
{
system->setPosition( &m_startPos );
}
else
{
// Particle system no longer exists; clear the stale ID to prevent future access violations.
m_particleSystemID = INVALID_PARTICLE_SYSTEM_ID;
}
}
if( m_targetParticleSystemID )
{
system = TheParticleSystemManager->findParticleSystem( m_targetParticleSystemID );
if( system )
{
system->setPosition( &m_endPos );
}
else
{
// Particle system no longer exists; clear the stale ID to prevent future access violations.
m_targetParticleSystemID = INVALID_PARTICLE_SYSTEM_ID;
}
}
//Important! Set the laser position to the average of both points or else
//it probably won't get rendered!!!
Coord3D avgPos;
avgPos.set( startPos );
avgPos.add( endPos );
avgPos.scale( 0.5 );
getDrawable()->setPosition( &avgPos );
Object *laser = getDrawable()->getObject();
if( laser )
{
laser->setPosition( &avgPos );
}
m_dirty = true;
}
//-------------------------------------------------------------------------------------------------
Real LaserUpdate::getTemplateLaserRadius() const
{
const Drawable *draw = getDrawable();
const LaserDrawInterface* ldi = nullptr;
for( const DrawModule** d = draw->getDrawModules(); *d; ++d )
{
ldi = (*d)->getLaserDrawInterface();
if( ldi )
{
//***NOTE***
//While it appears the logic is accessing client data, it is actually accessing template module
//data from the client. This value is INI constant thus can't change. It's grouped with other
//laser defining attributes and having it there makes it easier for artists.
return ldi->getLaserTemplateWidth();
}
}
return 0.0f;
}
//-------------------------------------------------------------------------------------------------
Real LaserUpdate::getCurrentLaserRadius() const
{
return getTemplateLaserRadius() * getWidthScale();
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void LaserUpdate::crc( Xfer *xfer )
{
// extend base class
ClientUpdateModule::crc( xfer );
}
// ------------------------------------------------------------------------------------------------
/** Xfer method
* Version Info:
* 1: Initial version */
// ------------------------------------------------------------------------------------------------
void LaserUpdate::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// extend base class
ClientUpdateModule::xfer( xfer );
// start pos
xfer->xferCoord3D( &m_startPos );
// end pos
xfer->xferCoord3D( &m_endPos );
// dirty
xfer->xferBool( &m_dirty );
// particle system ID
xfer->xferUser( &m_particleSystemID, sizeof( ParticleSystemID ) );
// target particle system id
xfer->xferUser( &m_targetParticleSystemID, sizeof( ParticleSystemID ) );
m_laserRadius.xfer( xfer );
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void LaserUpdate::loadPostProcess( void )
{
// extend base class
ClientUpdateModule::loadPostProcess();
}