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