2 * $Logfile: /Freespace2/code/Hud/HUDshield.cpp $
7 * C file for the display and management of the HUD shield
10 * Revision 1.2 2002/05/07 03:16:45 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 12 8/27/99 10:36a Dave
18 * Impose a 2% penalty for hitting the shield balance key.
20 * 11 8/23/99 11:34a Dave
21 * Fixed shield intensity rendering problems.
23 * 10 8/01/99 12:39p Dave
24 * Added HUD contrast control key (for nebula).
26 * 9 7/22/99 4:00p Dave
27 * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
29 * 8 6/10/99 3:43p Dave
30 * Do a better job of syncing text colors to HUD gauges.
32 * 7 1/07/99 9:06a Jasen
35 * 6 12/30/98 8:57a Jasen
36 * updated coords for hi res
38 * 5 12/28/98 3:17p Dave
39 * Support for multiple hud bitmap filenames for hi-res mode.
41 * 4 12/21/98 5:02p Dave
42 * Modified all hud elements to be multi-resolution friendly.
44 * 3 12/14/98 1:15p Jasen
45 * added new HUD shield gauges
47 * 2 10/07/98 10:53a Dave
50 * 1 10/07/98 10:49a Dave
52 * 35 9/19/98 3:11p Adam
53 * Added new hardcoded values for Hud_shield_filenames
55 * 34 8/25/98 1:48p Dave
56 * First rev of EMP effect. Player side stuff basically done. Next comes
59 * 33 5/17/98 3:32p Lawrance
60 * Make shield gauge more readable when flashing
62 * 32 4/25/98 5:39p Dave
63 * Removed an unneeded assert.
65 * 31 4/25/98 3:56p Mike
66 * Make player's shield icon flash when Fred tells it to.
68 * 30 4/25/98 2:00p Dave
69 * Installed a bunch of multiplayer context help screens. Reworked ingame
70 * join ship select screen. Fix places where network timestamps get hosed.
72 * 29 4/21/98 12:19a Allender
73 * only play equalize shield sound if player equalizes shields
75 * 28 3/26/98 5:26p John
76 * added new paging code. nonfunctional.
78 * 27 3/21/98 3:35p Lawrance
79 * Tweak position of numeric integrity for target
81 * 26 3/14/98 4:59p Lawrance
82 * Flash shield/ship icons when ships are hit
84 * 25 3/02/98 5:42p John
85 * Removed WinAVI stuff from Freespace. Made all HUD gauges wriggle from
86 * afterburner. Made gr_set_clip work good with negative x &y. Made
87 * model_caching be on by default. Made each cached model have it's own
88 * bitmap id. Made asteroids not rotate when model_caching is on.
90 * 24 2/22/98 12:19p John
91 * Externalized some strings
93 * 23 2/12/98 4:58p Lawrance
94 * Change to new flashing method.
96 * 22 1/12/98 11:16p Lawrance
97 * Wonderful HUD config.
99 * 21 1/08/98 4:36p Lawrance
100 * Fix bug in shield drawing code.
102 * 20 1/05/98 9:38p Lawrance
103 * Implement flashing HUD gauges.
105 * 19 1/02/98 9:10p Lawrance
106 * Big changes to how colors get set on the HUD.
108 * 18 12/29/97 9:48p Mike
109 * Prevent indexing before array start when quadrant_num = -1.
111 * 17 12/01/97 12:27a Lawrance
112 * redo default alpha color for HUD, make it easy to modify in the future
114 * 16 11/18/97 5:58p Lawrance
115 * flash escort view info when that ship is taking hits
117 * 15 11/18/97 1:21p Mitri
118 * ALAN: be sure to only draw shield icons for targets that are ships
120 * 14 11/17/97 6:37p Lawrance
121 * new gauges: extended target view, new lock triangles, support ship view
123 * 13 11/13/97 10:46p Lawrance
124 * implemented new escort view, damage view and weapons
126 * 12 11/12/97 9:42a Lawrance
127 * show player ship integrity above shield icon
129 * 11 11/11/97 5:06p Lawrance
130 * fix bug with flashing frequency of hull
132 * 10 11/09/97 11:27p Lawrance
133 * move target shield icon closer to center
135 * 9 11/09/97 4:39p Lawrance
136 * don't draw mini ship icon anymore
138 * 8 11/08/97 11:08p Lawrance
139 * implement new "mini-shield" view that sits near bottom of reticle
141 * 7 11/05/97 11:21p Lawrance
142 * implement dynamic alpha on the shields
144 * 6 11/04/97 8:34p Lawrance
145 * fix warning: remove unused variable
147 * 5 11/04/97 7:49p Lawrance
148 * integrating new HUD reticle and shield icons
150 * 4 10/24/97 5:51p Lawrance
151 * don't show shield % if ship has no shields
153 * 3 9/03/97 4:32p John
154 * changed bmpman to only accept ani and pcx's. made passing .pcx or .ani
155 * to bm_load functions not needed. Made bmpman keep track of palettes
156 * for bitmaps not mapped into game palettes.
158 * 2 8/25/97 12:24a Lawrance
159 * implemented HUD shield management
161 * 1 8/24/97 10:31p Lawrance
169 #include "hudtarget.h"
170 #include "hudtargetbox.h"
174 #include "freespace.h"
177 #include "hudshield.h"
178 #include "hudescort.h"
182 #define NUM_SHIELD_LEVELS 8
184 #define SHIELD_TRANSFER_PERCENT 0.083f // 1/12 total shield strength
186 #define SHIELD_HIT_DURATION_SHORT 300 // time a shield quadrant flashes after being hit
187 #define SHIELD_FLASH_INTERVAL_FAST 200 // time between shield quadrant flashes
189 // now read in from hud.tbl
190 #define MAX_SHIELD_ICONS 40
191 int Hud_shield_filename_count = 0;
192 char Hud_shield_filenames[MAX_SHIELD_ICONS][MAX_FILENAME_LEN];
194 char Shield_mini_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
199 hud_frames Shield_gauges[MAX_SHIELD_ICONS];
201 static int Player_shield_coords[GR_NUM_RESOLUTIONS][2] =
211 static int Target_shield_coords[GR_NUM_RESOLUTIONS][2] =
221 static int Hud_shield_inited = 0;
223 int Shield_mini_coords[GR_NUM_RESOLUTIONS][2] = {
232 // draw on the mini shield icon what the ship integrity is
233 int Hud_mini_3digit[GR_NUM_RESOLUTIONS][3] = {
241 int Hud_mini_2digit[GR_NUM_RESOLUTIONS][3] = {
249 int Hud_mini_1digit[GR_NUM_RESOLUTIONS][3] = {
257 int Hud_mini_base[GR_NUM_RESOLUTIONS][2] = {
266 int Shield_mini_loaded = 0;
267 hud_frames Shield_mini_gauge;
269 #define SHIELD_HIT_PLAYER 0
270 #define SHIELD_HIT_TARGET 1
271 static shield_hit_info Shield_hit_data[2];
273 // translate between clockwise-from-top shield quadrant ordering to way quadrants are numbered in the game
274 ubyte Quadrant_xlate[4] = {1,0,2,3};
276 void hud_shield_game_init()
278 char name[MAX_FILENAME_LEN+1] = "";
281 read_file_text("hud.tbl");
284 Hud_shield_filename_count = 0;
285 required_string("#Shield Icons Begin");
286 while(!optional_string("#End")){
287 required_string("$Shield:");
289 stuff_string(name, F_NAME, NULL);
292 Assert(Hud_shield_filename_count < MAX_SHIELD_ICONS);
293 if(Hud_shield_filename_count < MAX_SHIELD_ICONS){
294 strcpy(Hud_shield_filenames[Hud_shield_filename_count++], name);
299 // called at the start of each level from HUD_init. Use Hud_shield_init so we only init Shield_gauges[] once.
300 void hud_shield_level_init()
304 hud_shield_hit_reset(1); // reset for the player
306 if ( Hud_shield_inited ) {
310 for ( i = 0; i < MAX_SHIELD_ICONS; i++ ) {
311 Shield_gauges[i].first_frame = -1;
312 Shield_gauges[i].num_frames = 0;
315 Hud_shield_inited = 1;
317 if ( !Shield_mini_loaded ) {
318 Shield_mini_gauge.first_frame = bm_load_animation(Shield_mini_fname[gr_screen.res], &Shield_mini_gauge.num_frames);
319 if ( Shield_mini_gauge.first_frame == -1 ) {
320 Warning(LOCATION, "Could not load in the HUD shield ani: Shield_mini_fname[gr_screen.res]\n");
323 Shield_mini_loaded = 1;
327 int hud_shield_maybe_flash(int gauge, int target_index, int shield_offset)
330 shield_hit_info *shi;
332 shi = &Shield_hit_data[target_index];
334 if ( !timestamp_elapsed(shi->shield_hit_timers[shield_offset]) ) {
335 if ( timestamp_elapsed(shi->shield_hit_next_flash[shield_offset]) ) {
336 shi->shield_hit_next_flash[shield_offset] = timestamp(SHIELD_FLASH_INTERVAL_FAST);
337 shi->shield_show_bright ^= (1<<shield_offset); // toggle between default and bright frames
340 if ( shi->shield_show_bright & (1<<shield_offset) ) {
341 // hud_set_bright_color();
342 hud_set_gauge_color(gauge, HUD_C_BRIGHT);
345 hud_set_gauge_color(gauge, HUD_C_NORMAL);
346 // hud_set_default_color();
353 // ------------------------------------------------------------------
356 // Show the players shield strength and integrity
358 void hud_shield_show(object *objp)
361 int hud_color_index, range;
367 if ( objp->type != OBJ_SHIP )
370 sp = &Ships[objp->instance];
371 sip = &Ship_info[sp->ship_info_index];
373 if ( sip->shield_icon_index == 255 ) {
377 if (objp == Player_obj) {
378 hud_set_gauge_color(HUD_PLAYER_SHIELD_ICON);
380 hud_set_gauge_color(HUD_TARGET_SHIELD_ICON);
383 // load in shield frames if not already loaded
384 Assert(sip->shield_icon_index >= 0 && sip->shield_icon_index < Hud_shield_filename_count);
385 sgp = &Shield_gauges[sip->shield_icon_index];
387 if ( sgp->first_frame == -1 ) {
388 sgp->first_frame = bm_load_animation(Hud_shield_filenames[sip->shield_icon_index], &sgp->num_frames);
389 if ( sgp->first_frame == -1 ) {
390 Warning(LOCATION, "Could not load in the HUD shield ani: %s\n", Hud_shield_filenames[sip->shield_icon_index]);
395 if ( objp == Player_obj ) {
396 sx = Player_shield_coords[gr_screen.res][0];
397 sy = Player_shield_coords[gr_screen.res][1];
399 sx = Target_shield_coords[gr_screen.res][0];
400 sy = Target_shield_coords[gr_screen.res][1];
403 sx += fl2i(HUD_offset_x);
404 sy += fl2i(HUD_offset_y);
406 // draw the ship first
407 if ( objp == Player_obj ) {
408 hud_shield_maybe_flash(HUD_PLAYER_SHIELD_ICON, SHIELD_HIT_PLAYER, HULL_HIT_OFFSET);
410 hud_shield_maybe_flash(HUD_TARGET_SHIELD_ICON, SHIELD_HIT_TARGET, HULL_HIT_OFFSET);
413 GR_AABITMAP(sgp->first_frame, sx, sy);
415 // draw the four quadrants
417 // Draw shield quadrants at one of NUM_SHIELD_LEVELS
418 max_shield = sip->shields/4.0f;
420 for ( i = 0; i < 4; i++ ) {
422 if ( objp->flags & OF_NO_SHIELDS ) {
426 if ( objp->shields[Quadrant_xlate[i]] < 0.1f ) {
430 range = max(HUD_COLOR_ALPHA_MAX, HUD_color_alpha + 4);
431 hud_color_index = fl2i( (objp->shields[Quadrant_xlate[i]] / max_shield) * range + 0.5);
432 Assert(hud_color_index >= 0 && hud_color_index <= range);
434 if ( hud_color_index < 0 ) {
437 if ( hud_color_index >= HUD_NUM_COLOR_LEVELS ) {
438 hud_color_index = HUD_NUM_COLOR_LEVELS - 1;
442 if ( objp == Player_obj ) {
443 flash = hud_shield_maybe_flash(HUD_PLAYER_SHIELD_ICON, SHIELD_HIT_PLAYER, i);
445 flash = hud_shield_maybe_flash(HUD_TARGET_SHIELD_ICON, SHIELD_HIT_TARGET, i);
449 // gr_set_color_fast(&HUD_color_defaults[hud_color_index]);
450 if ( objp == Player_obj ) {
451 hud_set_gauge_color(HUD_PLAYER_SHIELD_ICON, hud_color_index);
453 hud_set_gauge_color(HUD_TARGET_SHIELD_ICON, hud_color_index);
456 GR_AABITMAP(sgp->first_frame+i+1, sx, sy);
460 // hud_set_default_color();
463 // called at beginning of level to page in all ship icons
464 // used in this level
465 void hud_ship_icon_page_in(ship_info *sip)
469 if ( sip->shield_icon_index == 255 ) {
473 // load in shield frames if not already loaded
474 Assert(sip->shield_icon_index >= 0 && sip->shield_icon_index < Hud_shield_filename_count);
475 sgp = &Shield_gauges[sip->shield_icon_index];
477 if ( sgp->first_frame == -1 ) {
478 sgp->first_frame = bm_load_animation(Hud_shield_filenames[sip->shield_icon_index], &sgp->num_frames);
479 if ( sgp->first_frame == -1 ) {
480 Warning(LOCATION, "Could not load in the HUD shield ani: %s\n", Hud_shield_filenames[sip->shield_icon_index]);
486 for (i=0; i<sgp->num_frames; i++ ) {
487 bm_page_in_aabitmap(sgp->first_frame+i);
492 // ------------------------------------------------------------------
493 // hud_shield_equalize()
495 // Equalize the four shield quadrants for an object
497 void hud_shield_equalize(object *objp, player *pl)
503 Assert(objp != NULL);
511 Assert(objp->type == OBJ_SHIP);
512 if(objp->type != OBJ_SHIP){
516 // are all quadrants equal?
517 for(idx=0; idx<MAX_SHIELD_SECTIONS-1; idx++){
518 if(objp->shields[idx] != objp->shields[idx+1]){
526 strength = get_shield_strength(objp);
527 if ( strength != 0 ) {
528 // maybe impose a 2% penalty - server side and single player only
529 if(!MULTIPLAYER_CLIENT && (pl->shield_penalty_stamp < 0) || timestamp_elapsed_safe(pl->shield_penalty_stamp, 1000) ){
532 // reset the penalty timestamp
533 pl->shield_penalty_stamp = timestamp(1000);
536 set_shield_strength(objp, strength);
541 if ( objp == Player_obj ){
542 snd_play( &Snds[SND_SHIELD_XFER_OK] );
546 // ------------------------------------------------------------------
547 // hud_augment_shield_quadrant()
549 // Transfer shield energy to a shield quadrant from the three other
550 // quadrants. Works by trying to transfer a fixed amount of shield
551 // energy from the other three quadrants, taking the same percentage
552 // from each quadrant.
554 // input: objp => object to perform shield transfer on
555 // direction => which quadrant to augment:
561 void hud_augment_shield_quadrant(object *objp, int direction)
563 float full_shields, xfer_amount, energy_avail, percent_to_take, delta;
564 float max_quadrant_val;
567 Assert(direction >= 0 && direction < 4);
568 Assert(objp->type == OBJ_SHIP);
569 full_shields = Ship_info[Ships[objp->instance].ship_info_index].shields;
571 xfer_amount = full_shields * SHIELD_TRANSFER_PERCENT;
572 max_quadrant_val = full_shields/4.0f;
574 if ( (objp->shields[direction] + xfer_amount) > max_quadrant_val )
575 xfer_amount = max_quadrant_val - objp->shields[direction];
577 Assert(xfer_amount >= 0);
578 if ( xfer_amount == 0 ) {
579 // TODO: provide a feedback sound
583 snd_play( &Snds[SND_SHIELD_XFER_OK] );
587 for ( i = 0; i < MAX_SHIELD_SECTIONS; i++ ) {
588 if ( i == direction )
590 energy_avail += objp->shields[i];
593 percent_to_take = xfer_amount/energy_avail;
594 if ( percent_to_take > 1.0f )
595 percent_to_take = 1.0f;
597 for ( i = 0; i < MAX_SHIELD_SECTIONS; i++ ) {
598 if ( i == direction )
600 delta = percent_to_take * objp->shields[i];
601 objp->shields[i] -= delta;
602 Assert(objp->shields[i] >= 0 );
603 objp->shields[direction] += delta;
604 if ( objp->shields[direction] > max_quadrant_val )
605 objp->shields[direction] = max_quadrant_val;
609 // Try to find a match between filename and the names inside
610 // of Hud_shield_filenames. This will provide us with an
611 // association of ship class to shield icon information.
612 void hud_shield_assign_info(ship_info *sip, char *filename)
616 for ( i = 0; i < Hud_shield_filename_count; i++ ) {
617 if ( !stricmp(filename, Hud_shield_filenames[i]) ) {
618 sip->shield_icon_index = i;
623 void hud_show_mini_ship_integrity(object *objp, int x_force, int y_force)
625 char text_integrity[64];
626 int numeric_integrity;
627 float p_target_integrity,initial_hull;
630 initial_hull = Ship_info[Ships[objp->instance].ship_info_index].initial_hull_strength;
631 if ( initial_hull <= 0 ) {
632 Int3(); // illegal initial hull strength
633 p_target_integrity = 0.0f;
635 p_target_integrity = objp->hull_strength / initial_hull;
636 if (p_target_integrity < 0){
637 p_target_integrity = 0.0f;
641 numeric_integrity = fl2i(p_target_integrity*100 + 0.5f);
642 if(numeric_integrity > 100){
643 numeric_integrity = 100;
645 // Assert(numeric_integrity <= 100);
648 nx = (x_force == -1) ? Hud_mini_base[gr_screen.res][0] : x_force;
649 ny = (y_force == -1) ? Hud_mini_base[gr_screen.res][1] : y_force;
651 // 3 digit hull strength
652 if ( numeric_integrity == 100 ) {
653 nx += Hud_mini_3digit[gr_screen.res][2];
655 // 2 digit hull strength
656 else if ( numeric_integrity < 10 ) {
657 nx += Hud_mini_1digit[gr_screen.res][2];
659 // 1 digit hull strength
661 nx += Hud_mini_2digit[gr_screen.res][2];
664 if ( numeric_integrity == 0 ) {
665 if ( p_target_integrity > 0 ) {
666 numeric_integrity = 1;
670 nx += fl2i( HUD_offset_x );
671 ny += fl2i( HUD_offset_y );
673 sprintf(text_integrity, "%d", numeric_integrity);
674 if ( numeric_integrity < 100 ) {
675 hud_num_make_mono(text_integrity);
678 gr_string(nx, ny, text_integrity);
681 // Draw the miniature shield icon that is drawn near the reticle
682 void hud_shield_show_mini(object *objp, int x_force, int y_force, int x_hull_offset, int y_hull_offset)
685 int hud_color_index, range, frame_offset;
689 shield_hit_info *shi;
691 shi = &Shield_hit_data[SHIELD_HIT_TARGET];
693 if ( objp->type != OBJ_SHIP ) {
697 sp = &Ships[objp->instance];
698 sip = &Ship_info[sp->ship_info_index];
700 hud_set_gauge_color(HUD_TARGET_MINI_ICON);
702 if (!Shield_mini_loaded)
705 sx = (x_force == -1) ? Shield_mini_coords[gr_screen.res][0]+fl2i(HUD_offset_x) : x_force;
706 sy = (y_force == -1) ? Shield_mini_coords[gr_screen.res][1]+fl2i(HUD_offset_y) : y_force;
708 // draw the ship first
709 hud_shield_maybe_flash(HUD_TARGET_MINI_ICON, SHIELD_HIT_TARGET, HULL_HIT_OFFSET);
710 hud_show_mini_ship_integrity(objp,x_force + x_hull_offset,y_force + y_hull_offset);
712 // draw the four quadrants
713 // Draw shield quadrants at one of NUM_SHIELD_LEVELS
714 max_shield = sip->shields/4.0f;
716 for ( i = 0; i < 4; i++ ) {
718 if ( objp->flags & OF_NO_SHIELDS ) {
722 if ( objp->shields[Quadrant_xlate[i]] < 0.1f ) {
726 if ( hud_shield_maybe_flash(HUD_TARGET_MINI_ICON, SHIELD_HIT_TARGET, i) ) {
732 range = HUD_color_alpha;
733 hud_color_index = fl2i( (objp->shields[Quadrant_xlate[i]] / max_shield) * range + 0.5);
734 Assert(hud_color_index >= 0 && hud_color_index <= range);
736 if ( hud_color_index < 0 ) {
739 if ( hud_color_index >= HUD_NUM_COLOR_LEVELS ) {
740 hud_color_index = HUD_NUM_COLOR_LEVELS - 1;
743 if ( hud_gauge_maybe_flash(HUD_TARGET_MINI_ICON) == 1) {
744 // hud_set_bright_color();
745 hud_set_gauge_color(HUD_TARGET_MINI_ICON, HUD_C_BRIGHT);
747 // gr_set_color_fast(&HUD_color_defaults[hud_color_index]);
748 hud_set_gauge_color(HUD_TARGET_MINI_ICON, hud_color_index);
751 GR_AABITMAP(Shield_mini_gauge.first_frame + frame_offset, sx, sy);
754 // hud_set_default_color();
757 // reset the shield_hit_info data structure
758 void shield_info_reset(shield_hit_info *shi)
762 shi->shield_hit_status = 0;
763 shi->shield_show_bright = 0;
764 for ( i = 0; i < NUM_SHIELD_HIT_MEMBERS; i++ ) {
765 shi->shield_hit_timers[i] = 1;
766 shi->shield_hit_next_flash[i] = 1;
770 // reset the timers and hit flags for the shield gauges
772 // This needs to be called whenever the player selects a new target
774 // input: player => optional parameter (default value 0). This is to indicate that player shield hit
775 // info should be reset. This is normally not the case.
776 // is for the player's current target
777 void hud_shield_hit_reset(int player)
779 shield_hit_info *shi;
782 shi = &Shield_hit_data[SHIELD_HIT_PLAYER];
784 shi = &Shield_hit_data[SHIELD_HIT_TARGET];
787 shield_info_reset(shi);
790 // called once per frame to update the state of Shield_hit_status based on the Shield_hit_timers[]
791 void hud_shield_hit_update()
796 if ( Player_ai->target_objnum >= 0 ) {
800 for ( i = 0; i < limit; i++ ) {
801 for ( j = 0; j < NUM_SHIELD_HIT_MEMBERS; j++ ) {
802 if ( timestamp_elapsed(Shield_hit_data[i].shield_hit_timers[j]) ) {
803 Shield_hit_data[i].shield_hit_status &= ~(1<<j);
804 Shield_hit_data[i].shield_show_bright &= ~(1<<j);
810 // called when a shield quadrant is struct, so we can update the timer that will draw the quadrant
814 // objp => object pointer for ship that has been hit
815 // quadrant => quadrant of shield getting hit (-1 if no shield is present)
816 void hud_shield_quadrant_hit(object *objp, int quadrant)
818 shield_hit_info *shi;
821 if ( objp->type != OBJ_SHIP )
824 hud_escort_ship_hit(objp, quadrant);
825 hud_gauge_popup_start(HUD_TARGET_MINI_ICON);
827 if ( OBJ_INDEX(objp) == Player_ai->target_objnum ) {
828 shi = &Shield_hit_data[SHIELD_HIT_TARGET];
829 } else if ( objp == Player_obj ) {
830 shi = &Shield_hit_data[SHIELD_HIT_PLAYER];
835 if ( quadrant >= 0 ) {
836 num = Quadrant_xlate[quadrant];
837 shi->shield_hit_timers[num] = timestamp(300);
839 shi->shield_hit_timers[HULL_HIT_OFFSET] = timestamp(SHIELD_HIT_DURATION_SHORT);
840 hud_targetbox_start_flash(TBOX_FLASH_HULL);
845 void hudshield_page_in()
847 bm_page_in_aabitmap( Shield_mini_gauge.first_frame, Shield_mini_gauge.num_frames );