summaryrefslogtreecommitdiff
path: root/src/cgame/cg_attachment.c
blob: a04bbd5b2627253583c584da40f532aa82e98bbb (plain)
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
/*
===========================================================================
Copyright (C) 2000-2006 Tim Angus

This file is part of Tremulous.

Tremulous 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 2 of the License,
or (at your option) any later version.

Tremulous 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 Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// cg_attachment.c -- an abstract attachment system

#include "cg_local.h"

/*
===============
CG_AttachmentPoint

Return the attachment point
===============
*/
qboolean CG_AttachmentPoint( attachment_t *a, vec3_t v )
{
  centity_t   *cent;

  if( !a )
    return qfalse;

  // if it all breaks, then use the last point we know was correct
  VectorCopy( a->lastValidAttachmentPoint, v );

  switch( a->type )
  {
    case AT_STATIC:
      if( !a->staticValid )
        return qfalse;

      VectorCopy( a->origin, v );
      break;

    case AT_TAG:
      if( !a->tagValid )
        return qfalse;

      AxisCopy( axisDefault, a->re.axis );
      CG_PositionRotatedEntityOnTag( &a->re, &a->parent,
                                     a->model, a->tagName );
      VectorCopy( a->re.origin, v );
      break;

    case AT_CENT:
      if( !a->centValid )
        return qfalse;

      if( a->centNum == cg.predictedPlayerState.clientNum )
      {
        // this is smoother if it's the local client
        VectorCopy( cg.predictedPlayerState.origin, v );
      }
      else
      {
        cent = &cg_entities[ a->centNum ];
        VectorCopy( cent->lerpOrigin, v );
      }
      break;

    case AT_PARTICLE:
      if( !a->particleValid )
        return qfalse;

      if( !a->particle->valid )
      {
        a->particleValid = qfalse;
        return qfalse;
      }
      else
        VectorCopy( a->particle->origin, v );
      break;

    default:
      CG_Printf( S_COLOR_RED "ERROR: Invalid attachmentType_t in attachment\n" );
      break;
  }

  if( a->hasOffset )
    VectorAdd( v, a->offset, v );

  VectorCopy( v, a->lastValidAttachmentPoint );

  return qtrue;
}

/*
===============
CG_AttachmentDir

Return the attachment direction
===============
*/
qboolean CG_AttachmentDir( attachment_t *a, vec3_t v )
{
  vec3_t      forward;
  centity_t   *cent;

  if( !a )
    return qfalse;

  switch( a->type )
  {
    case AT_STATIC:
      return qfalse;
      break;

    case AT_TAG:
      if( !a->tagValid )
        return qfalse;

      VectorCopy( a->re.axis[ 0 ], v );
      break;

    case AT_CENT:
      if( !a->centValid )
        return qfalse;

      cent = &cg_entities[ a->centNum ];
      AngleVectors( cent->lerpAngles, forward, NULL, NULL );
      VectorCopy( forward, v );
      break;

    case AT_PARTICLE:
      if( !a->particleValid )
        return qfalse;

      if( !a->particle->valid )
      {
        a->particleValid = qfalse;
        return qfalse;
      }
      else
        VectorCopy( a->particle->velocity, v );
      break;

    default:
      CG_Printf( S_COLOR_RED "ERROR: Invalid attachmentType_t in attachment\n" );
      break;
  }

  VectorNormalize( v );
  return qtrue;
}

/*
===============
CG_AttachmentAxis

Return the attachment axis
===============
*/
qboolean CG_AttachmentAxis( attachment_t *a, vec3_t axis[ 3 ] )
{
  centity_t   *cent;

  if( !a )
    return qfalse;

  switch( a->type )
  {
    case AT_STATIC:
      return qfalse;
      break;

    case AT_TAG:
      if( !a->tagValid )
        return qfalse;

      AxisCopy( a->re.axis, axis );
      break;

    case AT_CENT:
      if( !a->centValid )
        return qfalse;

      cent = &cg_entities[ a->centNum ];
      AnglesToAxis( cent->lerpAngles, axis );
      break;

    case AT_PARTICLE:
      return qfalse;
      break;

    default:
      CG_Printf( S_COLOR_RED "ERROR: Invalid attachmentType_t in attachment\n" );
      break;
  }

  return qtrue;
}

/*
===============
CG_AttachmentVelocity

If the attachment can have velocity, return it
===============
*/
qboolean CG_AttachmentVelocity( attachment_t *a, vec3_t v )
{
  if( !a )
    return qfalse;

  if( a->particleValid && a->particle->valid )
  {
    VectorCopy( a->particle->velocity, v );
    return qtrue;
  }
  else if( a->centValid )
  {
    centity_t *cent = &cg_entities[ a->centNum ];

    VectorCopy( cent->currentState.pos.trDelta, v );
    return qtrue;
  }

  return qfalse;
}

/*
===============
CG_AttachmentCentNum

If the attachment has a centNum, return it
===============
*/
int CG_AttachmentCentNum( attachment_t *a )
{
  if( !a || !a->centValid )
    return -1;

  return a->centNum;
}

/*
===============
CG_Attached

If the attachment is valid, return qtrue
===============
*/
qboolean CG_Attached( attachment_t *a )
{
  if( !a )
    return qfalse;

  return a->attached;
}

/*
===============
CG_AttachToPoint

Attach to a point in space
===============
*/
void CG_AttachToPoint( attachment_t *a )
{
  if( !a || !a->staticValid )
    return;

  a->type = AT_STATIC;
  a->attached = qtrue;
}

/*
===============
CG_AttachToCent

Attach to a centity_t
===============
*/
void CG_AttachToCent( attachment_t *a )
{
  if( !a || !a->centValid )
    return;

  a->type = AT_CENT;
  a->attached = qtrue;
}

/*
===============
CG_AttachToTag

Attach to a model tag
===============
*/
void CG_AttachToTag( attachment_t *a )
{
  if( !a || !a->tagValid )
    return;

  a->type = AT_TAG;
  a->attached = qtrue;
}

/*
===============
CG_AttachToParticle

Attach to a particle
===============
*/
void CG_AttachToParticle( attachment_t *a )
{
  if( !a || !a->particleValid )
    return;

  a->type = AT_PARTICLE;
  a->attached = qtrue;
}

/*
===============
CG_SetAttachmentPoint
===============
*/
void CG_SetAttachmentPoint( attachment_t *a, vec3_t v )
{
  if( !a )
    return;

  VectorCopy( v, a->origin );
  a->staticValid = qtrue;
}

/*
===============
CG_SetAttachmentCent
===============
*/
void CG_SetAttachmentCent( attachment_t *a, centity_t *cent )
{
  if( !a || !cent )
    return;

  a->centNum = cent->currentState.number;
  a->centValid = qtrue;
}

/*
===============
CG_SetAttachmentTag
===============
*/
void CG_SetAttachmentTag( attachment_t *a, refEntity_t parent,
    qhandle_t model, char *tagName )
{
  if( !a )
    return;

  a->parent = parent;
  a->model = model;
  strncpy( a->tagName, tagName, MAX_STRING_CHARS );
  a->tagValid = qtrue;
}

/*
===============
CG_SetAttachmentParticle
===============
*/
void CG_SetAttachmentParticle( attachment_t *a, particle_t *p )
{
  if( !a )
    return;

  a->particle = p;
  a->particleValid = qtrue;
}

/*
===============
CG_SetAttachmentOffset
===============
*/
void CG_SetAttachmentOffset( attachment_t *a, vec3_t v )
{
  if( !a )
    return;

  VectorCopy( v, a->offset );
  a->hasOffset = qtrue;
}