]> icculus.org git repositories - taylor/freespace2.git/blob - src/weapon/trails.cpp
ryan's struct patch for gcc 2.95
[taylor/freespace2.git] / src / weapon / trails.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Weapon/Trails.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code for missile trails
16  *
17  * $Log$
18  * Revision 1.4  2002/06/17 06:33:11  relnev
19  * ryan's struct patch for gcc 2.95
20  *
21  * Revision 1.3  2002/06/09 04:41:29  relnev
22  * added copyright header
23  *
24  * Revision 1.2  2002/05/07 03:16:53  theoddone33
25  * The Great Newline Fix
26  *
27  * Revision 1.1.1.1  2002/05/03 03:28:11  root
28  * Initial import.
29  *
30  * 
31  * 7     6/23/99 2:23p Mattk
32  * Fixed detail level trail rendering problem.
33  * 
34  * 6     6/22/99 7:03p Dave
35  * New detail options screen.
36  * 
37  * 5     2/23/99 8:11p Dave
38  * Tidied up dogfight mode. Fixed TvT ship type problems for alpha wing.
39  * Small pass over todolist items.
40  * 
41  * 4     2/17/99 2:11p Dave
42  * First full run of squad war. All freespace and tracker side stuff
43  * works.
44  * 
45  * 3     11/14/98 5:33p Dave
46  * Lots of nebula work. Put in ship contrails.
47  * 
48  * 2     10/07/98 10:54a Dave
49  * Initial checkin.
50  * 
51  * 1     10/07/98 10:51a Dave
52  * 
53  * 9     5/13/98 3:10p John
54  * made detail slider for weapon rendering change the distance that lasers
55  * become non-textured.  The lowest setting turns off missile trail
56  * rendering.
57  * 
58  * 8     5/08/98 7:09p Dave
59  * Lots of UI tweaking.
60  * 
61  * 7     4/10/98 5:20p John
62  * Changed RGB in lighting structure to be ubytes.  Removed old
63  * not-necessary 24 bpp software stuff.
64  * 
65  * 6     3/31/98 5:19p John
66  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
67  * bunch of debug stuff out of player file.  Made model code be able to
68  * unload models and malloc out only however many models are needed.
69  *  
70  * 
71  * 5     3/23/98 5:00p John
72  * Improved missile trails.  Made smooth alpha under hardware.  Made end
73  * taper.  Made trail touch weapon.
74  * 
75  * 4     1/23/98 5:08p John
76  * Took L out of vertex structure used B (blue) instead.   Took all small
77  * fireballs out of fireball types and used particles instead.  Fixed some
78  * debris explosion things.  Restructured fireball code.   Restructured
79  * some lighting code.   Made dynamic lighting on by default. Made groups
80  * of lasers only cast one light.  Made fireballs not cast light.
81  * 
82  * 3     1/15/98 11:13a John
83  * Added code for specifying weapon trail bitmaps in weapons.tbl
84  * 
85  * 2     12/21/97 6:15p John
86  * Made a seperate system for missile trails
87  * 
88  * 1     12/21/97 5:30p John
89  * Initial version
90  *
91  * $NoKeywords: $
92  */
93
94 #include "pstypes.h"
95 #include "freespace.h"
96 #include "weapon.h"
97 #include "linklist.h"
98 #include "3d.h" 
99 #include "3dinternal.h" 
100 #include "bmpman.h"
101 #include "trails.h"
102 #include "timer.h"
103
104 #define MAX_TRAILS MAX_WEAPONS
105
106 // Stuff for missile trails doesn't need to be saved or restored... or does it?
107 typedef struct trail {
108         int             head, tail;                                             // pointers into the queue for the trail points
109         vector  pos[NUM_TRAIL_SECTIONS];        // positions of trail points
110         float           val[NUM_TRAIL_SECTIONS];        // for each point, a value that tells how much to fade out      
111         int             object_died;                                    // set to zero as long as object        
112         int             trail_stamp;                                    // trail timestamp      
113
114         // trail info
115         trail_info info;                                                        // this is passed when creating a trail
116
117         struct  trail * prev;
118         struct  trail * next;
119 } trail;
120
121
122 int Num_trails = 0;
123 trail Trails[MAX_TRAILS];
124
125 trail Trail_free_list;
126 trail Trail_used_list;
127
128 // Reset everything between levels
129 void trail_level_init()
130 {
131         int i;
132
133         Num_trails = 0;
134         list_init( &Trail_free_list );
135         list_init( &Trail_used_list );
136
137         // Link all object slots into the free list
138         for (i=0; i<MAX_TRAILS; i++)    {
139                 list_append(&Trail_free_list, &Trails[i] );
140         }
141 }
142
143 //returns the number of a free trail
144 //returns -1 if no free trails
145 int trail_create(trail_info info)
146 {
147         int trail_num;
148         trail *trailp;
149
150         // standalone server should never create trails
151         if(Game_mode & GM_STANDALONE_SERVER){
152                 return -1;
153         }
154
155         if ( !Detail.weapon_extras )    {
156                 // No trails at slot 0
157                 return -1;
158         }
159
160         if (Num_trails >= MAX_TRAILS ) {
161                 #ifndef NDEBUG
162                 mprintf(("Trail creation failed - too many trails!\n" ));
163                 #endif
164                 return -1;
165         }
166
167         // Find next available trail
168         trailp = GET_FIRST(&Trail_free_list);
169         Assert( trailp != &Trail_free_list );           // shouldn't have the dummy element
170
171         // remove trailp from the free list
172         list_remove( &Trail_free_list, trailp );
173         
174         // insert trailp onto the end of used list
175         list_append( &Trail_used_list, trailp );
176
177         // increment counter
178         Num_trails++;
179
180         // get objnum
181         trail_num = trailp-Trails;
182
183         // Init the trail data
184         trailp->info = info;
185         trailp->tail = 0;
186         trailp->head = 0;       
187         trailp->object_died = 0;                
188         trailp->trail_stamp = timestamp(trailp->info.stamp);
189
190         return trail_num;
191 }
192
193 // output top and bottom vectors
194 // fvec == forward vector (eye viewpoint basically. in world coords)
195 // pos == world coordinate of the point we're calculating "around"
196 // w == width of the diff between top and bottom around pos
197 void trail_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w )
198 {
199         vector uvec, rvec;
200
201         vm_vec_sub( &rvec, &Eye_position, pos );
202         vm_vec_normalize( &rvec );
203
204         vm_vec_crossprod(&uvec,fvec,&rvec);
205         vm_vec_normalize(&uvec);
206
207         vm_vec_scale_add( top, pos, &uvec, w/2.0f );
208         vm_vec_scale_add( bot, pos, &uvec, -w/2.0f );
209 }
210
211 // trail is on ship
212 int trail_is_on_ship(int trail_index, ship *shipp)
213 {
214         int idx;
215
216         for(idx=0; idx<MAX_SHIP_CONTRAILS; idx++){
217                 if(shipp->trail_num[idx] == (short)trail_index){
218                         return 1;
219                 }
220         }
221
222         // nope
223         return 0;
224 }
225
226 // Render the trail behind a missile.
227 // Basically a queue of points that face the viewer.
228 void trail_render( trail * trailp )
229 {               
230         trail_info *ti; 
231
232         if ( trailp->tail == trailp->head ) return;
233
234         ti = &trailp->info;     
235
236         vector topv, botv, *fvec, last_pos, tmp_fvec;
237         vertex last_top, last_bot, top, bot;
238
239         int sections[NUM_TRAIL_SECTIONS];
240         int num_sections = 0;
241
242         int n = trailp->tail;
243
244         // if this trail is on the player ship, and he's in any padlock view except rear view, don't draw       
245         if((Player_ship != NULL) && trail_is_on_ship(trailp - Trails, Player_ship) && (Viewer_mode & (VM_PADLOCK_UP | VM_PADLOCK_LEFT | VM_PADLOCK_RIGHT)) ){
246                 return;
247         }
248
249         do      {
250                 n--;
251                 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
252
253
254                 if ( trailp->val[n] > 1.0f ) {
255                         break;
256                 }
257
258                 sections[num_sections++] = n;
259
260         } while ( n != trailp->head );
261
262         int i;
263
264         for (i=0; i<num_sections; i++ ) {
265
266                 n = sections[i];
267
268                 float w;
269                 ubyte l;
270
271                 w = trailp->val[n]*(ti->w_end - ti->w_start) + ti->w_start;
272                 l = (ubyte)fl2i((trailp->val[n]*(ti->a_end - ti->a_start) + ti->a_start)*255.0f);
273
274                 vector pos;
275
276                 pos = trailp->pos[n];
277
278                 if ( i == 0 )   {
279                         //fvec = 
280                         //&objp->orient.fvec;
281                         if ( num_sections > 1 ) {
282         
283                                 vm_vec_sub(&tmp_fvec, &pos, &trailp->pos[sections[i+1]] );
284                                 vm_vec_normalize_safe(&tmp_fvec);
285                                 fvec = &tmp_fvec;
286
287                         } else {
288                                 fvec = &tmp_fvec;
289                                 fvec->xyz.x = 0.0f;
290                                 fvec->xyz.y = 0.0f;
291                                 fvec->xyz.z = 1.0f;
292                         }
293                 } else {
294                         vm_vec_sub(&tmp_fvec, &last_pos, &pos );
295                         vm_vec_normalize_safe(&tmp_fvec);
296                         fvec = &tmp_fvec;
297                 }
298                         
299                 trail_calc_facing_pts( &topv, &botv, fvec, &pos, w );
300
301                 g3_rotate_vertex( &top, &topv );
302                 g3_rotate_vertex( &bot, &botv );
303                 top.a = bot.a = l;      
304
305                 if ( i > 0 )    {
306
307                         if ( i == num_sections-1 )      {
308                                 // Last one...
309                                 vector centerv;
310                                 vm_vec_avg( &centerv, &topv, &botv );
311                                 vertex center;
312                                 g3_rotate_vertex( &center, &centerv );
313                                 center.a = l;   
314
315                                 vertex *vlist[3];
316                                 vlist[0] = &last_top;
317                                 vlist[1] = &last_bot;
318                                 vlist[2] = &center;
319
320                                 vlist[0]->u = 0.0f;  vlist[0]->v = 1.0f;
321                                 vlist[1]->u = 0.0f;  vlist[1]->v = 0.0f;
322                                 vlist[2]->u = 1.0f;  vlist[2]->v = 0.5f;
323
324                                 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
325                                 if ( D3D_enabled )      {
326                                         g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
327                                 } else {
328                                         g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED );
329                                 }
330
331
332                         } else {
333                                 vertex *vlist[4];
334                                 vlist[0] = &last_bot;
335                                 vlist[1] = &bot;
336                                 vlist[2] = &top;
337                                 vlist[3] = &last_top;
338
339                                 vlist[0]->u = 0.0f;  vlist[0]->v = 0.0f;
340                                 vlist[1]->u = 1.0f;  vlist[1]->v = 0.0f;
341                                 vlist[2]->u = 1.0f;  vlist[2]->v = 1.0f;
342                                 vlist[3]->u = 0.0f;  vlist[3]->v = 1.0f;
343
344                                 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
345                                 if ( D3D_enabled )      {
346                                         g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
347                                 } else {
348                                         g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED );
349                                 }
350                         }
351                 }
352                 last_pos = pos;
353                 last_top = top;
354                 last_bot = bot;
355         }
356 }
357
358
359 void trail_add_segment( int trail_num, vector *pos )
360 {
361         if (trail_num < 0 ) return;
362         if (trail_num >= MAX_TRAILS ) return;
363
364         trail *trailp = &Trails[trail_num];
365
366         int next = trailp->tail;
367         trailp->tail++;
368         if ( trailp->tail >= NUM_TRAIL_SECTIONS )
369                 trailp->tail = 0;
370
371         if ( trailp->head == trailp->tail )     {
372                 // wrapped!!
373                 trailp->head++;
374                 if ( trailp->head >= NUM_TRAIL_SECTIONS )
375                         trailp->head = 0;
376         }
377         
378         trailp->pos[next] = *pos;
379         trailp->val[next] = 0.0f;
380 }               
381
382 void trail_set_segment( int trail_num, vector *pos )
383 {
384         if (trail_num < 0 ) return;
385         if (trail_num >= MAX_TRAILS ) return;
386
387         trail *trailp = &Trails[trail_num];
388
389         int next = trailp->tail-1;
390         if ( next < 0 ) {
391                 next = NUM_TRAIL_SECTIONS-1;
392         }
393         
394         trailp->pos[next] = *pos;
395 }
396
397 void trail_move_all(float frametime)
398 {
399         trail *trailp;  
400
401         trailp=GET_FIRST(&Trail_used_list);
402
403         while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
404
405                 int num_alive_segments = 0;
406
407                 if ( trailp->tail != trailp->head )     {
408                         int n = trailp->tail;                   
409                         float time_delta = frametime / trailp->info.max_life;
410                         do      {
411                                 n--;
412                                 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
413
414                                 trailp->val[n] += time_delta;
415
416                                 if ( trailp->val[n] <= 1.0f ) {
417                                         num_alive_segments++;   // Record how many still alive.
418                                 }
419
420                         } while ( n != trailp->head );
421                 }               
422         
423                 if ( trailp->object_died && (num_alive_segments < 1) )  {
424                         // delete it from the list!
425                         trail *next_one = GET_NEXT(trailp);
426
427                         // remove objp from the used list
428                         list_remove( &Trail_used_list, trailp);
429
430                         // add objp to the end of the free
431                         list_append( &Trail_free_list, trailp );
432
433                         // decrement counter
434                         Num_trails--;
435
436                         Assert(Num_trails >= 0);
437                         
438                         trailp = next_one;
439                 } else {
440                         trailp=GET_NEXT(trailp);
441                 }
442         }
443 }
444
445 void trail_object_died( int trail_num )
446 {
447         if (trail_num < 0 ) return;
448         if (trail_num >= MAX_TRAILS ) return;
449
450         trail *trailp = &Trails[trail_num];
451         
452         trailp->object_died++;
453 }
454
455 void trail_render_all()
456 {
457         trail *trailp;
458
459         if ( !Detail.weapon_extras )    {
460                 // No trails at slot 0
461                 return;
462         }
463
464         trailp=GET_FIRST(&Trail_used_list);
465
466         while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
467                 trail_render(trailp);
468                 trailp=GET_NEXT(trailp);
469         }
470
471 }
472 int trail_stamp_elapsed(int trail_num)
473 {
474         return timestamp_elapsed(Trails[trail_num].trail_stamp);
475 }
476
477 void trail_set_stamp(int trail_num)
478 {
479         Trails[trail_num].trail_stamp = timestamp(Trails[trail_num].info.stamp);
480 }