-
Notifications
You must be signed in to change notification settings - Fork 605
/
g_roff.cpp
693 lines (575 loc) · 16.7 KB
/
g_roff.cpp
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
===========================================================================
Copyright (C) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
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/>.
===========================================================================
*/
#include "g_headers.h"
#include "g_local.h"
#include "g_roff.h"
#include "g_icarus.h"
#include "../code/qcommon/ojk_saved_game_helper.h"
// The list of precached ROFFs
roff_list_t roffs[MAX_ROFFS];
int num_roffs = 0;
extern void Q3_TaskIDComplete( gentity_t *ent, taskID_t taskType );
void G_RoffNotetrackCallback( gentity_t *cent, const char *notetrack)
{
int i = 0, r = 0, r2 = 0, objectID = 0, anglesGathered = 0, posoffsetGathered = 0;
char type[256];
char argument[512];
char addlArg[512];
char errMsg[256];
char t[64];
char teststr[256];
int addlArgs = 0;
vec3_t parsedAngles, parsedOffset, useAngles, useOrigin, forward, right, up;
if (!cent || !notetrack)
{
return;
}
//notetrack = "effect effects/explosion1.efx 0+0+64 0-0-1";
while (notetrack[i] && notetrack[i] != ' ')
{
type[i] = notetrack[i];
i++;
}
type[i] = '\0';
if (notetrack[i] != ' ')
{ //didn't pass in a valid notetrack type, or forgot the argument for it
return;
}
i++;
while (notetrack[i] && notetrack[i] != ' ')
{
if (notetrack[i] != '\n' && notetrack[i] != '\r')
{ //don't read line ends for an argument
argument[r] = notetrack[i];
r++;
}
i++;
}
argument[r] = '\0';
if (!r)
{
return;
}
if (notetrack[i] == ' ')
{ //additional arguments...
addlArgs = 1;
i++;
r = 0;
while (notetrack[i])
{
addlArg[r] = notetrack[i];
r++;
i++;
}
addlArg[r] = '\0';
}
if (strcmp(type, "effect") == 0)
{
if (!addlArgs)
{
VectorClear(parsedOffset);
goto defaultoffsetposition;
}
i = 0;
while (posoffsetGathered < 3)
{
r = 0;
while (addlArg[i] && addlArg[i] != '+' && addlArg[i] != ' ')
{
t[r] = addlArg[i];
r++;
i++;
}
t[r] = '\0';
i++;
if (!r)
{ //failure..
VectorClear(parsedOffset);
i = 0;
goto defaultoffsetposition;
}
parsedOffset[posoffsetGathered] = atof(t);
posoffsetGathered++;
}
if (posoffsetGathered < 3)
{
sprintf(errMsg, "Offset position argument for 'effect' type is invalid.");
goto functionend;
}
i--;
if (addlArg[i] != ' ')
{
addlArgs = 0;
}
defaultoffsetposition:
r = 0;
if (argument[r] == '/')
{
r++;
}
while (argument[r] && argument[r] != '/')
{
teststr[r2] = argument[r];
r2++;
r++;
}
teststr[r2] = '\0';
if (r2 && strstr(teststr, "effects"))
{ //get rid of the leading "effects" since it's auto-inserted
r++;
r2 = 0;
while (argument[r])
{
teststr[r2] = argument[r];
r2++;
r++;
}
teststr[r2] = '\0';
Q_strncpyz(argument, teststr, sizeof(argument));
}
objectID = G_EffectIndex(argument);
r = 0;
if (objectID)
{
if (addlArgs)
{ //if there is an additional argument for an effect it is expected to be XANGLE-YANGLE-ZANGLE
i++;
while (anglesGathered < 3)
{
r = 0;
while (addlArg[i] && addlArg[i] != '-')
{
t[r] = addlArg[i];
r++;
i++;
}
t[r] = '\0';
i++;
if (!r)
{ //failed to get a new part of the vector
anglesGathered = 0;
break;
}
parsedAngles[anglesGathered] = atof(t);
anglesGathered++;
}
if (anglesGathered)
{
VectorCopy(parsedAngles, useAngles);
}
else
{ //failed to parse angles from the extra argument provided..
VectorCopy(cent->s.apos.trBase, useAngles);
}
}
else
{ //if no constant angles, play in direction entity is facing
VectorCopy(cent->s.apos.trBase, useAngles);
}
AngleVectors(useAngles, forward, right, up);
VectorCopy(cent->s.pos.trBase, useOrigin);
//forward
useOrigin[0] += forward[0]*parsedOffset[0];
useOrigin[1] += forward[1]*parsedOffset[0];
useOrigin[2] += forward[2]*parsedOffset[0];
//right
useOrigin[0] += right[0]*parsedOffset[1];
useOrigin[1] += right[1]*parsedOffset[1];
useOrigin[2] += right[2]*parsedOffset[1];
//up
useOrigin[0] += up[0]*parsedOffset[2];
useOrigin[1] += up[1]*parsedOffset[2];
useOrigin[2] += up[2]*parsedOffset[2];
G_PlayEffect(objectID, useOrigin, useAngles);
}
}
else if (strcmp(type, "sound") == 0)
{
objectID = G_SoundIndex(argument);
cgi_S_StartSound(cent->s.pos.trBase, cent->s.number, CHAN_BODY, objectID);
}
//else if ...
else
{
if (type[0])
{
Com_Printf("Warning: \"%s\" is an invalid ROFF notetrack function\n", type);
}
else
{
Com_Printf("Warning: Notetrack is missing function and/or arguments\n");
}
}
return;
functionend:
Com_Printf("Type-specific notetrack error: %s\n", errMsg);
return;
}
qboolean G_ValidRoff( roff_hdr2_t *header )
{
if ( !strncmp( header->mHeader, "ROFF", 4 ))
{
if ( LittleLong(header->mVersion) == ROFF_VERSION2 && LittleLong(header->mCount) > 0 )
{
return qtrue;
}
else if ( LittleLong(header->mVersion) == ROFF_VERSION && LittleFloat(((roff_hdr_t*)header)->mCount) > 0.0f )
{ // version 1 defines the count as a float, so we best do the count check as a float or we'll get bogus results
return qtrue;
}
}
return qfalse;
}
qboolean G_InitRoff( char *file, unsigned char *data )
{
roff_hdr_t *header = (roff_hdr_t *)data;
int count;
int i;
roffs[num_roffs].fileName = G_NewString( file );
if ( LittleLong(header->mVersion) == ROFF_VERSION )
{
count = (int)LittleFloat(header->mCount);
// We are Old School(tm)
roffs[num_roffs].data = (void *) G_Alloc( count * sizeof( move_rotate_t ) );
move_rotate_t *mem = (move_rotate_t *)roffs[num_roffs].data;
roffs[num_roffs].mFrameTime = 100; // old school ones have a hard-coded frame time
roffs[num_roffs].mLerp = 10;
if ( mem )
{
// The allocation worked, so stash this stuff off so we can reference the data later if needed
roffs[num_roffs].frames = count;
// Step past the header to get to the goods
move_rotate_t *roff_data = ( move_rotate_t *)&header[1];
// Copy all of the goods into our ROFF cache
for ( i = 0; i < count; i++, roff_data++, mem++ )
{
// Copy just the delta position and orientation which can be applied to anything at a later point
#ifdef Q3_BIG_ENDIAN
mem->origin_delta[0] = LittleFloat(roff_data->origin_delta[0]);
mem->origin_delta[1] = LittleFloat(roff_data->origin_delta[1]);
mem->origin_delta[2] = LittleFloat(roff_data->origin_delta[2]);
mem->rotate_delta[0] = LittleFloat(roff_data->rotate_delta[0]);
mem->rotate_delta[1] = LittleFloat(roff_data->rotate_delta[1]);
mem->rotate_delta[2] = LittleFloat(roff_data->rotate_delta[2]);
#else
VectorCopy( roff_data->origin_delta, mem->origin_delta );
VectorCopy( roff_data->rotate_delta, mem->rotate_delta );
#endif
}
}
else
{
return qfalse;
}
}
else
{
// Version 2.0, heck yeah!
roff_hdr2_t *hdr = (roff_hdr2_t *)data;
count = LittleLong(hdr->mCount);
roffs[num_roffs].frames = count;
roffs[num_roffs].data = (void *) G_Alloc( count * sizeof( move_rotate2_t ));
move_rotate2_t *mem = (move_rotate2_t *)roffs[num_roffs].data;
if ( mem )
{
roffs[num_roffs].mFrameTime = LittleLong(hdr->mFrameRate);
roffs[num_roffs].mLerp = 1000 / LittleLong(hdr->mFrameRate);
roffs[num_roffs].mNumNoteTracks = LittleLong(hdr->mNumNotes);
// Step past the header to get to the goods
move_rotate2_t *roff_data = ( move_rotate2_t *)&hdr[1];
roffs[num_roffs].type = 2; //rww - any reason this wasn't being set already?
// Copy all of the goods into our ROFF cache
for ( i = 0; i < count; i++ )
{
#ifdef Q3_BIG_ENDIAN
mem[i].origin_delta[0] = LittleFloat(roff_data[i].origin_delta[0]);
mem[i].origin_delta[1] = LittleFloat(roff_data[i].origin_delta[1]);
mem[i].origin_delta[2] = LittleFloat(roff_data[i].origin_delta[2]);
mem[i].rotate_delta[0] = LittleFloat(roff_data[i].rotate_delta[0]);
mem[i].rotate_delta[1] = LittleFloat(roff_data[i].rotate_delta[1]);
mem[i].rotate_delta[2] = LittleFloat(roff_data[i].rotate_delta[2]);
#else
VectorCopy( roff_data[i].origin_delta, mem[i].origin_delta );
VectorCopy( roff_data[i].rotate_delta, mem[i].rotate_delta );
#endif
mem[i].mStartNote = LittleLong(roff_data[i].mStartNote);
mem[i].mNumNotes = LittleLong(roff_data[i].mNumNotes);
}
if ( LittleLong(hdr->mNumNotes) )
{
int size;
char *ptr, *start;
ptr = start = (char *)&roff_data[i];
size = 0;
for( i = 0; i < LittleLong(hdr->mNumNotes); i++ )
{
size += strlen(ptr) + 1;
ptr += strlen(ptr) + 1;
}
// ? Get rid of dynamic memory ?
roffs[num_roffs].mNoteTrackIndexes = new char *[LittleLong(hdr->mNumNotes)];
ptr = roffs[num_roffs].mNoteTrackIndexes[0] = new char[size];
memcpy(roffs[num_roffs].mNoteTrackIndexes[0], start, size);
for( i = 1; i < LittleLong(hdr->mNumNotes); i++ )
{
ptr += strlen(ptr) + 1;
roffs[num_roffs].mNoteTrackIndexes[i] = ptr;
}
}
}
else
{
return qfalse;
}
}
return qtrue;
}
//-------------------------------------------------------
// G_LoadRoff
//
// Does the fun work of loading and caching a roff file
// If the file is already cached, it just returns an
// ID to the cached file.
//-------------------------------------------------------
int G_LoadRoff( const char *fileName )
{
char file[MAX_QPATH];
byte *data;
int len, i, roff_id = 0;
// Before even bothering with all of this, make sure we have a place to store it.
if ( num_roffs >= MAX_ROFFS )
{
Com_Printf( S_COLOR_RED"MAX_ROFFS count exceeded. Skipping load of .ROF '%s'\n", fileName );
return roff_id;
}
// The actual path
sprintf( file, "%s/%s.rof", Q3_SCRIPT_DIR, fileName );
// See if I'm already precached
for ( i = 0; i < num_roffs; i++ )
{
if ( Q_stricmp( file, roffs[i].fileName ) == 0 )
{
// Good, just return me...avoid zero index
return i + 1;
}
}
#ifdef _DEBUG
Com_Printf( S_COLOR_GREEN"Caching ROF: '%s'\n", file );
#endif
// Read the file in one fell swoop
len = gi.FS_ReadFile( file, (void**) &data);
if ( len <= 0 )
{
Com_Printf( S_COLOR_RED"Could not open .ROF file '%s'\n", fileName );
return roff_id;
}
// Now let's check the header info...
roff_hdr2_t *header = (roff_hdr2_t *)data;
// ..and make sure it's reasonably valid
if ( !G_ValidRoff( header ))
{
Com_Printf( S_COLOR_RED"Invalid roff format '%s'\n", fileName );
}
else
{
G_InitRoff( file, data );
// Done loading this roff, so save off an id to it..increment first to avoid zero index
roff_id = ++num_roffs;
}
gi.FS_FreeFile( data );
return roff_id;
}
//-------------------------------------------------------
// G_Roff
//
// Handles applying the roff data to the specified ent
//-------------------------------------------------------
void G_Roff( gentity_t *ent )
{
if ( !ent->next_roff_time )
{
return;
}
if ( ent->next_roff_time > level.time )
{// either I don't think or it's just not time to have me think yet
return;
}
const int roff_id = G_LoadRoff( ent->roff );
if ( !roff_id )
{ // Couldn't cache this rof
return;
}
// The ID is one higher than the array index
const roff_list_t * roff = &roffs[ roff_id - 1 ];
vec3_t org, ang;
if ( roff->type == 2 )
{
move_rotate2_t *data = &((move_rotate2_t *)roff->data)[ ent->roff_ctr ];
VectorCopy( data->origin_delta, org );
VectorCopy( data->rotate_delta, ang );
if (data->mStartNote != -1 || data->mNumNotes)
{
G_RoffNotetrackCallback(ent, roffs[roff_id - 1].mNoteTrackIndexes[data->mStartNote]);
}
}
else
{
move_rotate_t *data = &((move_rotate_t *)roff->data)[ ent->roff_ctr ];
VectorCopy( data->origin_delta, org );
VectorCopy( data->rotate_delta, ang );
}
#ifdef _DEBUG
if ( g_developer->integer )
{
Com_Printf( S_COLOR_GREEN"ROFF dat: num: %d o:<%.2f %.2f %.2f> a:<%.2f %.2f %.2f>\n",
ent->roff_ctr,
org[0], org[1], org[2],
ang[0], ang[1], ang[2] );
}
#endif
if ( ent->client )
{
// Set up the angle interpolation
//-------------------------------------
VectorAdd( ent->s.apos.trBase, ang, ent->s.apos.trBase );
ent->s.apos.trTime = level.time;
ent->s.apos.trType = TR_INTERPOLATE;
// Store what the next apos->trBase should be
VectorCopy( ent->s.apos.trBase, ent->client->ps.viewangles );
VectorCopy( ent->s.apos.trBase, ent->currentAngles );
VectorCopy( ent->s.apos.trBase, ent->s.angles );
if ( ent->NPC )
{
//ent->NPC->desiredPitch = ent->s.apos.trBase[PITCH];
ent->NPC->desiredYaw = ent->s.apos.trBase[YAW];
}
// Set up the origin interpolation
//-------------------------------------
VectorAdd( ent->s.pos.trBase, org, ent->s.pos.trBase );
ent->s.pos.trTime = level.time;
ent->s.pos.trType = TR_INTERPOLATE;
// Store what the next pos->trBase should be
VectorCopy( ent->s.pos.trBase, ent->client->ps.origin );
VectorCopy( ent->s.pos.trBase, ent->currentOrigin );
//VectorCopy( ent->s.pos.trBase, ent->s.origin );
}
else
{
// Set up the angle interpolation
//-------------------------------------
VectorScale( ang, roff->mLerp, ent->s.apos.trDelta );
VectorCopy( ent->pos2, ent->s.apos.trBase );
ent->s.apos.trTime = level.time;
ent->s.apos.trType = TR_LINEAR;
// Store what the next apos->trBase should be
VectorAdd( ent->pos2, ang, ent->pos2 );
// Set up the origin interpolation
//-------------------------------------
VectorScale( org, roff->mLerp, ent->s.pos.trDelta );
VectorCopy( ent->pos1, ent->s.pos.trBase );
ent->s.pos.trTime = level.time;
ent->s.pos.trType = TR_LINEAR;
// Store what the next apos->trBase should be
VectorAdd( ent->pos1, org, ent->pos1 );
//make it true linear... FIXME: sticks around after ROFF is done, but do we really care?
ent->alt_fire = qtrue;
if ( !ent->e_ThinkFunc
&& ent->s.eType != ET_MISSILE
&& ent->s.eType != ET_ITEM
&& ent->s.eType != ET_MOVER )
{//will never set currentAngles & currentOrigin itself
EvaluateTrajectory( &ent->s.apos, level.time, ent->currentAngles );
EvaluateTrajectory( &ent->s.pos, level.time, ent->currentOrigin );
}
}
// See if the ROFF playback is done
//-------------------------------------
if ( ++ent->roff_ctr >= roff->frames )
{
// We are done, so let me think no more, then tell the task that we're done.
ent->next_roff_time = 0;
// Stop any rotation or movement.
VectorClear( ent->s.pos.trDelta );
VectorClear( ent->s.apos.trDelta );
Q3_TaskIDComplete( ent, TID_MOVE_NAV );
return;
}
// Lock me to a 20hz update rate
ent->next_roff_time = level.time + roff->mFrameTime;
assert( roff->mFrameTime >= 50 );//HAS to be at least 50
}
//-------------------------------------------------------
// G_SaveCachedRoffs
//
// Really fun savegame stuff
//-------------------------------------------------------
void G_SaveCachedRoffs()
{
int i, len;
ojk::SavedGameHelper saved_game(
::gi.saved_game);
// Write out the number of cached ROFFs
saved_game.write_chunk<int32_t>(
INT_ID('R', 'O', 'F', 'F'),
::num_roffs);
// Now dump out the cached ROFF file names in order so they can be loaded on the other end
for ( i = 0; i < num_roffs; i++ )
{
// Dump out the string length to make things a bit easier on the other end...heh heh.
len = strlen( roffs[i].fileName ) + 1;
saved_game.write_chunk<int32_t>(
INT_ID('S', 'L', 'E', 'N'),
len);
saved_game.write_chunk(
INT_ID('R', 'S', 'T', 'R'),
::roffs[i].fileName,
len);
}
}
//-------------------------------------------------------
// G_LoadCachedRoffs
//
// Really fun loadgame stuff
//-------------------------------------------------------
void G_LoadCachedRoffs()
{
int i, count = 0, len = 0;
char buffer[MAX_QPATH];
ojk::SavedGameHelper saved_game(
::gi.saved_game);
// Get the count of goodies we need to revive
saved_game.read_chunk<int32_t>(
INT_ID('R', 'O', 'F', 'F'),
count);
// Now bring 'em back to life
for ( i = 0; i < count; i++ )
{
saved_game.read_chunk<int32_t>(
INT_ID('S', 'L', 'E', 'N'),
len);
if (len < 0 || static_cast<size_t>(len) >= sizeof(buffer))
{
::G_Error("invalid length for RSTR string in save game: %d bytes\n", len);
}
saved_game.read_chunk(
INT_ID('R', 'S', 'T', 'R'),
buffer,
len);
G_LoadRoff( buffer );
}
}