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.5 2004/09/20 01:31:45 theoddone33
21 * Revision 1.4 2002/06/17 06:33:11 relnev
22 * ryan's struct patch for gcc 2.95
24 * Revision 1.3 2002/06/09 04:41:29 relnev
25 * added copyright header
27 * Revision 1.2 2002/05/07 03:16:53 theoddone33
28 * The Great Newline Fix
30 * Revision 1.1.1.1 2002/05/03 03:28:11 root
34 * 7 6/23/99 2:23p Mattk
35 * Fixed detail level trail rendering problem.
37 * 6 6/22/99 7:03p Dave
38 * New detail options screen.
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.
44 * 4 2/17/99 2:11p Dave
45 * First full run of squad war. All freespace and tracker side stuff
48 * 3 11/14/98 5:33p Dave
49 * Lots of nebula work. Put in ship contrails.
51 * 2 10/07/98 10:54a Dave
54 * 1 10/07/98 10:51a Dave
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
61 * 8 5/08/98 7:09p Dave
62 * Lots of UI tweaking.
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.
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.
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.
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.
85 * 3 1/15/98 11:13a John
86 * Added code for specifying weapon trail bitmaps in weapons.tbl
88 * 2 12/21/97 6:15p John
89 * Made a seperate system for missile trails
91 * 1 12/21/97 5:30p John
98 #include "freespace.h"
100 #include "linklist.h"
102 #include "3dinternal.h"
107 #define MAX_TRAILS MAX_WEAPONS
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
118 trail_info info; // this is passed when creating a trail
126 trail Trails[MAX_TRAILS];
128 trail Trail_free_list;
129 trail Trail_used_list;
131 // Reset everything between levels
132 void trail_level_init()
137 list_init( &Trail_free_list );
138 list_init( &Trail_used_list );
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] );
146 //returns the number of a free trail
147 //returns -1 if no free trails
148 int trail_create(trail_info info)
153 // standalone server should never create trails
154 if(Game_mode & GM_STANDALONE_SERVER){
158 if ( !Detail.weapon_extras ) {
159 // No trails at slot 0
163 if (Num_trails >= MAX_TRAILS ) {
165 mprintf(("Trail creation failed - too many trails!\n" ));
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
174 // remove trailp from the free list
175 list_remove( &Trail_free_list, trailp );
177 // insert trailp onto the end of used list
178 list_append( &Trail_used_list, trailp );
184 trail_num = trailp-Trails;
186 // Init the trail data
190 trailp->object_died = 0;
191 trailp->trail_stamp = timestamp(trailp->info.stamp);
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 )
204 vm_vec_sub( &rvec, &Eye_position, pos );
205 vm_vec_normalize( &rvec );
207 vm_vec_crossprod(&uvec,fvec,&rvec);
208 vm_vec_normalize(&uvec);
210 vm_vec_scale_add( top, pos, &uvec, w/2.0f );
211 vm_vec_scale_add( bot, pos, &uvec, -w/2.0f );
215 int trail_is_on_ship(int trail_index, ship *shipp)
219 for(idx=0; idx<MAX_SHIP_CONTRAILS; idx++){
220 if(shipp->trail_num[idx] == (short)trail_index){
229 // Render the trail behind a missile.
230 // Basically a queue of points that face the viewer.
231 void trail_render( trail * trailp )
235 if ( trailp->tail == trailp->head ) return;
239 vector topv, botv, *fvec, last_pos, tmp_fvec;
240 vertex last_top, last_bot, top, bot;
242 int sections[NUM_TRAIL_SECTIONS];
243 int num_sections = 0;
245 int n = trailp->tail;
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)) ){
254 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
257 if ( trailp->val[n] > 1.0f ) {
261 sections[num_sections++] = n;
263 } while ( n != trailp->head );
267 for (i=0; i<num_sections; i++ ) {
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);
279 pos = trailp->pos[n];
283 //&objp->orient.fvec;
284 if ( num_sections > 1 ) {
286 vm_vec_sub(&tmp_fvec, &pos, &trailp->pos[sections[i+1]] );
287 vm_vec_normalize_safe(&tmp_fvec);
297 vm_vec_sub(&tmp_fvec, &last_pos, &pos );
298 vm_vec_normalize_safe(&tmp_fvec);
302 trail_calc_facing_pts( &topv, &botv, fvec, &pos, w );
304 g3_rotate_vertex( &top, &topv );
305 g3_rotate_vertex( &bot, &botv );
310 if ( i == num_sections-1 ) {
313 vm_vec_avg( ¢erv, &topv, &botv );
315 g3_rotate_vertex( ¢er, ¢erv );
319 vlist[0] = &last_top;
320 vlist[1] = &last_bot;
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;
327 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f, -1, -1);
328 g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
332 vlist[0] = &last_bot;
335 vlist[3] = &last_top;
337 vlist[0]->u = 0.0f; vlist[0]->v = 0.0f;
338 vlist[1]->u = 1.0f; vlist[1]->v = 0.0f;
339 vlist[2]->u = 1.0f; vlist[2]->v = 1.0f;
340 vlist[3]->u = 0.0f; vlist[3]->v = 1.0f;
342 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f, -1, -1);
343 g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
353 void trail_add_segment( int trail_num, vector *pos )
355 if (trail_num < 0 ) return;
356 if (trail_num >= MAX_TRAILS ) return;
358 trail *trailp = &Trails[trail_num];
360 int next = trailp->tail;
362 if ( trailp->tail >= NUM_TRAIL_SECTIONS )
365 if ( trailp->head == trailp->tail ) {
368 if ( trailp->head >= NUM_TRAIL_SECTIONS )
372 trailp->pos[next] = *pos;
373 trailp->val[next] = 0.0f;
376 void trail_set_segment( int trail_num, vector *pos )
378 if (trail_num < 0 ) return;
379 if (trail_num >= MAX_TRAILS ) return;
381 trail *trailp = &Trails[trail_num];
383 int next = trailp->tail-1;
385 next = NUM_TRAIL_SECTIONS-1;
388 trailp->pos[next] = *pos;
391 void trail_move_all(float frametime)
395 trailp=GET_FIRST(&Trail_used_list);
397 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
399 int num_alive_segments = 0;
401 if ( trailp->tail != trailp->head ) {
402 int n = trailp->tail;
403 float time_delta = frametime / trailp->info.max_life;
406 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
408 trailp->val[n] += time_delta;
410 if ( trailp->val[n] <= 1.0f ) {
411 num_alive_segments++; // Record how many still alive.
414 } while ( n != trailp->head );
417 if ( trailp->object_died && (num_alive_segments < 1) ) {
418 // delete it from the list!
419 trail *next_one = GET_NEXT(trailp);
421 // remove objp from the used list
422 list_remove( &Trail_used_list, trailp);
424 // add objp to the end of the free
425 list_append( &Trail_free_list, trailp );
430 SDL_assert(Num_trails >= 0);
434 trailp=GET_NEXT(trailp);
439 void trail_object_died( int trail_num )
441 if (trail_num < 0 ) return;
442 if (trail_num >= MAX_TRAILS ) return;
444 trail *trailp = &Trails[trail_num];
446 trailp->object_died++;
449 void trail_render_all()
453 if ( !Detail.weapon_extras ) {
454 // No trails at slot 0
458 trailp=GET_FIRST(&Trail_used_list);
460 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
461 trail_render(trailp);
462 trailp=GET_NEXT(trailp);
466 int trail_stamp_elapsed(int trail_num)
468 return timestamp_elapsed(Trails[trail_num].trail_stamp);
471 void trail_set_stamp(int trail_num)
473 Trails[trail_num].trail_stamp = timestamp(Trails[trail_num].info.stamp);