2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Weapon/Trails.cpp $
15 * Code for missile trails
18 * Revision 1.4 2002/06/17 06:33:11 relnev
19 * ryan's struct patch for gcc 2.95
21 * Revision 1.3 2002/06/09 04:41:29 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:53 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:11 root
31 * 7 6/23/99 2:23p Mattk
32 * Fixed detail level trail rendering problem.
34 * 6 6/22/99 7:03p Dave
35 * New detail options screen.
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.
41 * 4 2/17/99 2:11p Dave
42 * First full run of squad war. All freespace and tracker side stuff
45 * 3 11/14/98 5:33p Dave
46 * Lots of nebula work. Put in ship contrails.
48 * 2 10/07/98 10:54a Dave
51 * 1 10/07/98 10:51a Dave
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
58 * 8 5/08/98 7:09p Dave
59 * Lots of UI tweaking.
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.
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.
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.
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.
82 * 3 1/15/98 11:13a John
83 * Added code for specifying weapon trail bitmaps in weapons.tbl
85 * 2 12/21/97 6:15p John
86 * Made a seperate system for missile trails
88 * 1 12/21/97 5:30p John
95 #include "freespace.h"
99 #include "3dinternal.h"
104 #define MAX_TRAILS MAX_WEAPONS
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
115 trail_info info; // this is passed when creating a trail
123 trail Trails[MAX_TRAILS];
125 trail Trail_free_list;
126 trail Trail_used_list;
128 // Reset everything between levels
129 void trail_level_init()
134 list_init( &Trail_free_list );
135 list_init( &Trail_used_list );
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] );
143 //returns the number of a free trail
144 //returns -1 if no free trails
145 int trail_create(trail_info info)
150 // standalone server should never create trails
151 if(Game_mode & GM_STANDALONE_SERVER){
155 if ( !Detail.weapon_extras ) {
156 // No trails at slot 0
160 if (Num_trails >= MAX_TRAILS ) {
162 mprintf(("Trail creation failed - too many trails!\n" ));
167 // Find next available trail
168 trailp = GET_FIRST(&Trail_free_list);
169 Assert( trailp != &Trail_free_list ); // shouldn't have the dummy element
171 // remove trailp from the free list
172 list_remove( &Trail_free_list, trailp );
174 // insert trailp onto the end of used list
175 list_append( &Trail_used_list, trailp );
181 trail_num = trailp-Trails;
183 // Init the trail data
187 trailp->object_died = 0;
188 trailp->trail_stamp = timestamp(trailp->info.stamp);
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 )
201 vm_vec_sub( &rvec, &Eye_position, pos );
202 vm_vec_normalize( &rvec );
204 vm_vec_crossprod(&uvec,fvec,&rvec);
205 vm_vec_normalize(&uvec);
207 vm_vec_scale_add( top, pos, &uvec, w/2.0f );
208 vm_vec_scale_add( bot, pos, &uvec, -w/2.0f );
212 int trail_is_on_ship(int trail_index, ship *shipp)
216 for(idx=0; idx<MAX_SHIP_CONTRAILS; idx++){
217 if(shipp->trail_num[idx] == (short)trail_index){
226 // Render the trail behind a missile.
227 // Basically a queue of points that face the viewer.
228 void trail_render( trail * trailp )
232 if ( trailp->tail == trailp->head ) return;
236 vector topv, botv, *fvec, last_pos, tmp_fvec;
237 vertex last_top, last_bot, top, bot;
239 int sections[NUM_TRAIL_SECTIONS];
240 int num_sections = 0;
242 int n = trailp->tail;
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)) ){
251 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
254 if ( trailp->val[n] > 1.0f ) {
258 sections[num_sections++] = n;
260 } while ( n != trailp->head );
264 for (i=0; i<num_sections; i++ ) {
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);
276 pos = trailp->pos[n];
280 //&objp->orient.fvec;
281 if ( num_sections > 1 ) {
283 vm_vec_sub(&tmp_fvec, &pos, &trailp->pos[sections[i+1]] );
284 vm_vec_normalize_safe(&tmp_fvec);
294 vm_vec_sub(&tmp_fvec, &last_pos, &pos );
295 vm_vec_normalize_safe(&tmp_fvec);
299 trail_calc_facing_pts( &topv, &botv, fvec, &pos, w );
301 g3_rotate_vertex( &top, &topv );
302 g3_rotate_vertex( &bot, &botv );
307 if ( i == num_sections-1 ) {
310 vm_vec_avg( ¢erv, &topv, &botv );
312 g3_rotate_vertex( ¢er, ¢erv );
316 vlist[0] = &last_top;
317 vlist[1] = &last_bot;
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;
324 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
326 g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
328 g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED );
334 vlist[0] = &last_bot;
337 vlist[3] = &last_top;
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;
344 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
346 g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
348 g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED );
359 void trail_add_segment( int trail_num, vector *pos )
361 if (trail_num < 0 ) return;
362 if (trail_num >= MAX_TRAILS ) return;
364 trail *trailp = &Trails[trail_num];
366 int next = trailp->tail;
368 if ( trailp->tail >= NUM_TRAIL_SECTIONS )
371 if ( trailp->head == trailp->tail ) {
374 if ( trailp->head >= NUM_TRAIL_SECTIONS )
378 trailp->pos[next] = *pos;
379 trailp->val[next] = 0.0f;
382 void trail_set_segment( int trail_num, vector *pos )
384 if (trail_num < 0 ) return;
385 if (trail_num >= MAX_TRAILS ) return;
387 trail *trailp = &Trails[trail_num];
389 int next = trailp->tail-1;
391 next = NUM_TRAIL_SECTIONS-1;
394 trailp->pos[next] = *pos;
397 void trail_move_all(float frametime)
401 trailp=GET_FIRST(&Trail_used_list);
403 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
405 int num_alive_segments = 0;
407 if ( trailp->tail != trailp->head ) {
408 int n = trailp->tail;
409 float time_delta = frametime / trailp->info.max_life;
412 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
414 trailp->val[n] += time_delta;
416 if ( trailp->val[n] <= 1.0f ) {
417 num_alive_segments++; // Record how many still alive.
420 } while ( n != trailp->head );
423 if ( trailp->object_died && (num_alive_segments < 1) ) {
424 // delete it from the list!
425 trail *next_one = GET_NEXT(trailp);
427 // remove objp from the used list
428 list_remove( &Trail_used_list, trailp);
430 // add objp to the end of the free
431 list_append( &Trail_free_list, trailp );
436 Assert(Num_trails >= 0);
440 trailp=GET_NEXT(trailp);
445 void trail_object_died( int trail_num )
447 if (trail_num < 0 ) return;
448 if (trail_num >= MAX_TRAILS ) return;
450 trail *trailp = &Trails[trail_num];
452 trailp->object_died++;
455 void trail_render_all()
459 if ( !Detail.weapon_extras ) {
460 // No trails at slot 0
464 trailp=GET_FIRST(&Trail_used_list);
466 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
467 trail_render(trailp);
468 trailp=GET_NEXT(trailp);
472 int trail_stamp_elapsed(int trail_num)
474 return timestamp_elapsed(Trails[trail_num].trail_stamp);
477 void trail_set_stamp(int trail_num)
479 Trails[trail_num].trail_stamp = timestamp(Trails[trail_num].info.stamp);