forked from pjbroad/other-life
/
hud.c
2392 lines (2122 loc) · 70.5 KB
/
hud.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "hud.h"
#include "asc.h"
#include "buddy.h"
#include "consolewin.h"
#include "context_menu.h"
#include "cursors.h"
#include "draw_scene.h"
#include "elconfig.h"
#include "elwindows.h"
#include "events.h"
#include "gamewin.h"
#include "gl_init.h"
#include "global.h"
#include "hud_indicators.h"
#include "hud_timer.h"
#include "icon_window.h"
#include "init.h"
#include "interface.h"
#include "items.h"
#include "item_info.h"
#include "keys.h" //Avoid problems with SHIFT, ALT, CTRL
#include "knowledge.h"
#include "manufacture.h"
#include "mapwin.h"
#include "minimap.h"
#include "missiles.h"
#include "multiplayer.h"
#include "new_character.h"
#include "notepad.h"
#include "platform.h"
#include "questlog.h"
#include "sound.h"
#include "spells.h"
#include "stats.h"
#include "storage.h"
#include "tabs.h"
#include "textures.h"
#include "trade.h"
#include "translate.h"
#ifdef ECDEBUGWIN
#include "eye_candy_debugwin.h"
#endif
#include "user_menus.h"
#include "url.h"
#include "emotes.h"
#define WALK 0
#define SIT 1
#define LOOK 2
#define TRADE 3
#define ATTACK 4
#define USE 5
#ifdef OTHER_LIFE
Uint64 exp_lev[MAX_EXP_LEVEL];
#else
Uint32 exp_lev[MAX_EXP_LEVEL];
#endif
#ifdef NEW_NEW_CHAR_WINDOW
hud_interface last_interface = HUD_INTERFACE_NEW_CHAR; //Current interface (game or new character)
#endif
int display_stats_bar_handler(window_info *win);
int display_misc_handler(window_info *win);
int click_misc_handler(window_info *win, int mx, int my, Uint32 flags);
int mouseover_misc_handler(window_info *win, int mx, int my);
int display_quickbar_handler(window_info *win);
int click_quickbar_handler(window_info *win, int mx, int my, Uint32 flags);
int mouseover_quickbar_handler(window_info *win, int mx, int my);
int mouseover_stats_bar_handler(window_info *win, int mx, int my);
void init_hud_frame();
void init_stats_display();
void draw_exp_display();
void draw_stats();
void init_misc_display(hud_interface type);
void init_quickbar();
void toggle_quickbar_draggable();
void flip_quickbar();
void reset_quickbar();
void change_flags(int win_id, Uint32 flags);
Uint32 get_flags(int win_id);
int get_quickbar_y_base();
static int context_hud_handler(window_info *win, int widget_id, int mx, int my, int option);
static size_t cm_hud_id = CM_INIT_VALUE;
static size_t cm_quickbar_id = CM_INIT_VALUE;
static size_t cm_id = CM_INIT_VALUE;
static int cm_quickbar_enabled = 0;
static int cm_sound_enabled = 0;
static int cm_music_enabled = 0;
static int cm_minimap_shown = 0;
static int cm_rangstats_shown = 0;
enum { CMH_STATS=0, CMH_STATBARS, CMH_KNOWBAR, CMH_TIMER, CMH_DIGCLOCK, CMH_ANACLOCK,
CMH_SECONDS, CMH_FPS, CMH_INDICATORS, CMH_QUICKBM, CMH_SEP1, CMH_MINIMAP, CMH_RANGSTATS,
CMH_SEP2, CMH_SOUND, CMH_MUSIC, CMH_SEP3, CMH_LOCATION };
enum { CMQB_RELOC=0, CMQB_DRAG, CMQB_RESET, CMQB_FLIP, CMQB_ENABLE };
int hud_x= 64;
int hud_y= 48;
int hud_text;
int view_analog_clock= 1;
int view_digital_clock= 0;
int view_knowledge_bar = 1;
int view_hud_timer = 1;
int copy_next_LOCATE_ME = 0;
int stats_bar_win= -1;
int misc_win= -1;
int quickbar_win= -1;
int quickspell_win= -1;
int show_help_text=1;
int always_enlarge_text=1;
int qb_action_mode=ACTION_USE;
int show_stats_in_hud=0;
int show_statbars_in_hud=0;
static int first_disp_stat = 0; /* first skill that will be display */
static int num_disp_stat = NUM_WATCH_STAT-1; /* number of skills to be displayed */
static int statbar_start_y = 0; /* y coord in window of top if stats bar */
static int stat_mouse_is_over = -1; /* set to stat of the is mouse over that bar */
static int mouse_over_clock = 0; /* 1 if mouse is over digital or analogue clock */
static int mouse_over_compass = 0; /* 1 if mouse is over the compass */
static int mouse_over_knowledge_bar = 0; /* 1 if mouse is over the knowledge bar */
static const int knowledge_bar_height = SMALL_FONT_Y_LEN + 6;
static const int stats_bar_height = SMALL_FONT_Y_LEN;
static int mouseover_quickbar_item_pos = -1;
/* called on client exit to free resources */
void cleanup_hud(void)
{
destroy_timer();
destroy_hud_indicators();
destroy_window(misc_win);
destroy_window(stats_bar_win);
destroy_window(quickbar_win);
stats_bar_win = quickbar_win = misc_win = -1;
}
/* #exp console command, display current exp information */
int show_exp(char *text, int len)
{
int thestat;
char buf[256];
for (thestat=0; thestat<NUM_WATCH_STAT-1; thestat++)
{
safe_snprintf(buf, sizeof(buf), "%s: level %u, %u/%u exp (%u to go)",
statsinfo[thestat].skillnames->name, statsinfo[thestat].skillattr->base,
*statsinfo[thestat].exp, *statsinfo[thestat].next_lev,
*statsinfo[thestat].next_lev - *statsinfo[thestat].exp );
LOG_TO_CONSOLE(c_green1, buf);
}
return 1;
}
// initialize anything related to the hud
void init_hud_interface (hud_interface type)
{
#ifndef NEW_NEW_CHAR_WINDOW
static hud_interface last_interface = HUD_INTERFACE_NEW_CHAR;
#endif
if (type == HUD_INTERFACE_LAST)
type = last_interface;
init_hud_frame ();
#ifndef NEW_NEW_CHAR_WINDOW
init_misc_display (type);
#else
if(type == HUD_INTERFACE_GAME)
init_misc_display (type);
#endif
if (type == HUD_INTERFACE_NEW_CHAR)
{
#ifdef NEW_NEW_CHAR_WINDOW
hud_x=270;
resize_root_window();
#endif
init_icon_window (NEW_CHARACTER_ICONS);
}
else
{
#ifdef NEW_NEW_CHAR_WINDOW
if (hud_x>0)
hud_x=HUD_MARGIN_X;
resize_root_window();
#endif
init_icon_window (MAIN_WINDOW_ICONS);
init_stats_display ();
init_quickbar ();
init_quickspell ();
init_hud_indicators ();
ready_for_user_menus = 1;
if (enable_user_menus)
display_user_menus();
if ((minimap_win < 0) && open_minimap_on_start)
view_window (&minimap_win, 0);
}
last_interface = type;
}
void show_hud_windows ()
{
if (icons_win >= 0) show_window (icons_win);
if (stats_bar_win >= 0) show_window (stats_bar_win);
if (misc_win >= 0) show_window (misc_win);
if (quickbar_win >= 0) show_window (quickbar_win);
if (quickspell_win >= 0) show_window (quickspell_win);
show_hud_indicators_window();
}
void hide_hud_windows ()
{
if (icons_win >= 0) hide_window (icons_win);
if (stats_bar_win >= 0) hide_window (stats_bar_win);
if (misc_win >= 0) hide_window (misc_win);
if (quickbar_win >= 0) hide_window (quickbar_win);
if (quickspell_win >= 0) hide_window (quickspell_win);
hide_hud_indicators_window();
}
// draw everything related to the hud
void draw_hud_interface()
{
glColor3f(1.0f, 1.0f, 1.0f);
draw_hud_frame();
}
// check to see if a mouse click was on the hud
// used in non-standard modes
int check_hud_interface()
{
return click_in_windows(mouse_x, mouse_y, 0); // temporarily here for testing
}
// hud frame section
float vertical_bar_u_start = (float)192/256;
float vertical_bar_u_end = 1.0f;
float vertical_bar_v_end = 0.0f;
float vertical_bar_v_start = 0.0f;
float horizontal_bar_u_start = (float)144/256;
float horizontal_bar_u_end = (float)191/256;
float horizontal_bar_v_start = 0.0f;
float horizontal_bar_v_end = 0.0f;
void init_hud_frame()
{
#ifdef NEW_TEXTURES
vertical_bar_v_end = (float)window_height/256;
horizontal_bar_v_start = (float)(window_width-hud_x)/256;
#else /* NEW_TEXTURES */
vertical_bar_v_start = (float)window_height/256;
horizontal_bar_v_end = (float)(window_width-hud_x)/256;
#endif /* NEW_TEXTURES */
}
#ifdef NEW_TEXTURES
float logo_u_start = (float)64/256;
float logo_v_start = (float)128/256;
float logo_u_end = (float)127/256;
float logo_v_end = (float)191/256;
#else /* NEW_TEXTURES */
float logo_u_start=(float)64/256;
float logo_v_start=1.0f-(float)128/256;
float logo_u_end=(float)127/256;
float logo_v_end=1.0f-(float)191/256;
#endif /* NEW_TEXTURES */
void draw_hud_frame()
{
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
#ifdef NEW_TEXTURES
bind_texture(hud_text);
#else /* NEW_TEXTURES */
get_and_set_texture_id(hud_text);
#endif /* NEW_TEXTURES */
glBegin(GL_QUADS);
#ifndef NEW_NEW_CHAR_WINDOW
draw_2d_thing(vertical_bar_u_start, vertical_bar_v_start, vertical_bar_u_end, vertical_bar_v_end,window_width-hud_x, 0, window_width, window_height);
draw_2d_thing_r(horizontal_bar_u_start, horizontal_bar_v_start, horizontal_bar_u_end, horizontal_bar_v_end,0,window_height,window_width-hud_x , window_height-hud_y);
#else
draw_2d_thing_r(horizontal_bar_u_start, horizontal_bar_v_start, horizontal_bar_u_end, horizontal_bar_v_end,0,window_height,window_width, window_height-hud_y);
#endif
#ifdef NEW_NEW_CHAR_WINDOW
if(last_interface == HUD_INTERFACE_GAME)
{
draw_2d_thing(vertical_bar_u_start, vertical_bar_v_start, vertical_bar_u_end, vertical_bar_v_end,window_width-hud_x, 0, window_width, window_height);
#endif
//draw the logo
draw_2d_thing(logo_u_start, logo_v_start, logo_u_end, logo_v_end,window_width-hud_x, 0, window_width, 64);
#ifdef NEW_NEW_CHAR_WINDOW
}
#endif
glEnd();
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
void switch_action_mode(int * mode, int id)
{
item_action_mode=qb_action_mode=action_mode=*mode;
}
void view_console_win (int *win, int id)
{
if ( get_show_window (console_root_win) && !locked_to_console )
{
hide_window (console_root_win);
show_window (game_root_win);
// Undo stupid quickbar hack
if ( !get_show_window (quickbar_win) )
show_window (quickbar_win);
if ( !get_show_window (quickspell_win) )
show_window (quickspell_win);
}
else
{
if ( get_show_window (game_root_win) )
hide_window (game_root_win);
if ( get_show_window (map_root_win) )
{
switch_from_game_map ();
hide_window (map_root_win);
}
show_window (console_root_win);
}
}
void view_map_win (int * win, int id)
{
if ( get_show_window (map_root_win) && !locked_to_console )
{
switch_from_game_map ();
hide_window (map_root_win);
show_window (game_root_win);
// Undo stupid quickbar hack
if ( !get_show_window (quickbar_win) )
show_window (quickbar_win);
}
else if ( switch_to_game_map () && !locked_to_console )
{
if ( get_show_window (game_root_win) )
hide_window (game_root_win);
if ( get_show_window (console_root_win) )
hide_window (console_root_win);
show_window (map_root_win);
}
}
typedef struct
{
char name[20];
int *id;
} windowid_by_name;
int* get_winid(const char *name)
{
static windowid_by_name win_ids[] = {
{ "invent", &items_win },
{ "spell", &sigil_win },
{ "manu", &manufacture_win },
{ "emotewin", &emotes_win },
{ "quest", &questlog_win },
{ "map", &map_root_win },
{ "info", &tab_info_win },
{ "buddy", &buddy_win },
{ "stats", &tab_stats_win },
{ "console", &console_root_win },
{ "help", &tab_help_win },
{ "opts", &elconfig_win },
{ "range", &range_win },
{ "minimap", &minimap_win },
{ "name_pass", &namepass_win },
{ "customize", &color_race_win } };
size_t i;
if (name == NULL)
return NULL;
for (i=0; i<sizeof(win_ids)/sizeof(windowid_by_name); i++)
if (strcmp(win_ids[i].name, name) == 0)
return win_ids[i].id;
return NULL;
}
void view_window(int * window, int id)
{
if (window == NULL)
return;
if (window == &map_root_win)
{
view_map_win(window, id);
return;
}
if (window == &console_root_win)
{
view_console_win(window, id);
return;
}
if(window==&sigil_win||window==&manufacture_win)
{
if(get_show_window(trade_win))
{
LOG_TO_CONSOLE(c_red2,no_open_on_trade);
return;
}
}
if(*window < 0)
{
//OK, the window has not been created yet - use the standard functions
if(window==&items_win)display_items_menu();
else if(window==&sigil_win) display_sigils_menu();
else if(window==&manufacture_win) display_manufacture_menu();
else if(window==&emotes_win) display_emotes_menu();
else if(window==&elconfig_win) display_elconfig_win();
else if(window==&buddy_win) display_buddy();
else if(window==&trade_win) display_trade_menu();
else if(window==&tab_info_win) display_tab_info();
else if(window==&minimap_win) display_minimap();
#ifdef ECDEBUGWIN
else if(window==&ecdebug_win) display_ecdebugwin();
#endif
else if(window==&storage_win) display_storage_menu();
else if(window==&tab_stats_win) display_tab_stats();
else if(window==&tab_help_win) display_tab_help();
#ifndef NEW_NEW_CHAR_WINDOW
else if(window==&namepass_win) show_account_win();
else if(window==&color_race_win) show_color_race_win();
#endif
else if(window==&questlog_win) display_questlog();
else if(window==&range_win) display_range_win();
}
else toggle_window(*window);
}
void view_tab (int *window, int *col_id, int tab)
{
if (get_show_window (*window))
{
if (tab_collection_get_tab (*window, *col_id) == tab)
{
hide_window (*window);
}
else
{
tab_collection_select_tab (*window, *col_id, tab);
}
}
else
{
view_window (window, 0);
tab_collection_select_tab (*window, *col_id, tab);
}
}
int enlarge_text(void)
{
if (always_enlarge_text)
return 1;
return ((SDL_GetModState() & (KMOD_CTRL|KMOD_ALT)));
}
void show_help(const char *help_message, int x, int y)
{
show_sized_help_coloured(help_message, x, y, 1.0f, 1.0f, 1.0f, 0);
}
void show_sized_help(const char *help_message, int x, int y, int big)
{
show_sized_help_coloured(help_message, x, y, 1.0f, 1.0f, 1.0f, big);
}
void show_help_coloured(const char *help_message, int x, int y, float r, float g, float b)
{
show_sized_help_coloured(help_message, x, y, r, g, b, 0);
}
void show_sized_help_coloured(const char *help_message, int x, int y, float r, float g, float b, int big)
{
float y_font_len = (big) ?DEFAULT_FONT_Y_LEN: SMALL_FONT_Y_LEN;
int len=strlen(help_message)*((big) ?DEFAULT_FONT_X_LEN :SMALL_FONT_X_LEN)+1;
int width=window_width-80;
if(x+len>width) x-=(x+len)-width;
glColor4f(0.0f,0.0f,0.0f,0.5f);
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glBegin(GL_QUADS);
glVertex3i(x-1,y+y_font_len,0);
glVertex3i(x-1,y,0);
glVertex3i(x+len,y,0);
glVertex3i(x+len,y+y_font_len,0);
glEnd();
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glColor3f(r,g,b);
if (big)
draw_string(x, y, (unsigned char*)help_message, 1);
else
draw_string_small(x, y, (unsigned char*)help_message, 1);
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
// Stats bars in the bottom HUD .....
int show_action_bar = 0;
int watch_this_stats[MAX_WATCH_STATS]={NUM_WATCH_STAT -1, 0, 0, 0, 0}; // default to only watching overall
int max_food_level=45;
static int exp_bar_text_len = 9.5*SMALL_FONT_X_LEN;
static const int stats_bar_text_len = 4.5 * SMALL_FONT_X_LEN;
// return the number of watched stat bars, the number displayed in the botton HUD
static int get_num_statsbar_exp(void)
{
size_t i;
int num_skills_bar = 0;
for (i=0; i<MAX_WATCH_STATS; i++)
if ((watch_this_stats[i] > 0) && statsinfo[watch_this_stats[i]-1].is_selected)
num_skills_bar++;
return num_skills_bar;
}
// calculate the number of digits for the specified Uint32
static Uint32 Uint32_digits(Uint32 number)
{
Uint32 digits = 1;
long step = 10;
while ((step <= number) && (digits<11))
{
digits++;
step *= 10;
}
return digits;
}
// check if we need to adjust exp_bar_text_len due to an exp change
static int recalc_exp_bar_text_len(void)
{
static int init_flag = 1;
static Uint32 last_exp[NUM_WATCH_STAT-1];
static Uint32 last_to_go_len[NUM_WATCH_STAT-1];
static Uint32 last_selected[NUM_WATCH_STAT-1];
int recalc = 1;
int i;
if (init_flag)
{
for (i=0; i<NUM_WATCH_STAT-1; i++)
{
last_exp[i] = *statsinfo[i].exp;
last_to_go_len[i] = Uint32_digits(*statsinfo[i].next_lev - *statsinfo[i].exp);
last_selected[i] = 0;
}
init_flag = 0;
}
for (i=0; i<NUM_WATCH_STAT-1; i++)
{
/* if any exp changes, recalculate the number of digits for next level value */
if (last_exp[i] != *statsinfo[i].exp)
{
unsigned int curr = Uint32_digits(*statsinfo[i].next_lev - *statsinfo[i].exp);
/* if the number of digit changes, we need to recalulate exp_bar_text_len */
if (last_to_go_len[i] != curr)
{
last_to_go_len[i] = curr;
recalc = 1;
}
last_exp[i] = *statsinfo[i].exp;
}
/* if the selected status of any skill changed, we need to recalulate exp_bar_text_len */
if (last_selected[i] != statsinfo[i].is_selected)
{
last_selected[i] = statsinfo[i].is_selected;
recalc = 1;
}
}
/* recalc based only the skills being watched */
if (recalc)
{
int max_len = 0;
for (i=0; i<MAX_WATCH_STATS; i++)
if ((watch_this_stats[i] > 0) && statsinfo[watch_this_stats[i]-1].is_selected &&
(last_to_go_len[watch_this_stats[i]-1] > max_len))
max_len = last_to_go_len[watch_this_stats[i]-1];
return SMALL_FONT_X_LEN*(max_len+1.5);
}
else
return exp_bar_text_len;
}
// return the optimal length for stat bars for the botton HUD
static int calc_stats_bar_len(int num_exp)
{
// calculate the maximum length for stats bars given the current number of both bar types
int num_stat = (show_action_bar) ?5: 4;
int prosed_len = (window_width-HUD_MARGIN_X-1) - (num_stat * stats_bar_text_len) - (num_exp * exp_bar_text_len);
prosed_len /= num_stat + num_exp;
// constrain the maximum and minimum length of the skills bars to reasonable size
if (prosed_len < 50)
prosed_len = 50;
else if (prosed_len > 100)
prosed_len = 100;
return prosed_len;
}
// calculate the maximum number of exp bars
static int calc_max_disp_stats(int suggested_stats_bar_len)
{
int exp_offset = ((show_action_bar)?5:4) * (suggested_stats_bar_len + stats_bar_text_len);
int preposed_max_disp_stats = (window_width - HUD_MARGIN_X - exp_offset) / (suggested_stats_bar_len + exp_bar_text_len);
if (preposed_max_disp_stats > MAX_WATCH_STATS)
preposed_max_disp_stats = MAX_WATCH_STATS;
return preposed_max_disp_stats;
}
static int statbar_cursor_x;
static int stats_bar_len;
static int max_disp_stats=1;
static int health_bar_start_x;
static int health_bar_start_y;
static int mana_bar_start_x;
static int mana_bar_start_y;
static int food_bar_start_x;
static int food_bar_start_y;
static int load_bar_start_x;
static int load_bar_start_y;
static int action_bar_start_x;
static int action_bar_start_y;
static int exp_bar_start_x;
static int exp_bar_start_y;
static int free_statbar_space;
// clear the context menu regions for all stats bars and set up again
static void reset_statsbar_exp_cm_regions(void)
{
size_t i;
cm_remove_regions(stats_bar_win);
for (i=0; i<max_disp_stats; i++)
if (watch_this_stats[i] > 0)
cm_add_region(cm_id, stats_bar_win, exp_bar_start_x+i*(stats_bar_len+exp_bar_text_len), exp_bar_start_y, stats_bar_len, 12);
}
static int cm_statsbar_handler(window_info *win, int widget_id, int mx, int my, int option)
{
int i;
int add_bar = 0;
// selecting the same stat more than once, removing the last bar
// or adding too many is not possible as options are greyed out.
for (i=0; i<max_disp_stats;i++)
{
if ((mx >= exp_bar_start_x+i*(stats_bar_len+exp_bar_text_len)) && (mx <= exp_bar_start_x+i*(stats_bar_len+exp_bar_text_len)+stats_bar_len))
{
// if deleting the bar, close any gap
if (option == NUM_WATCH_STAT+1)
{
statsinfo[watch_this_stats[i]-1].is_selected=0;
if (i<max_disp_stats-1)
memmove(&(watch_this_stats[i]), &(watch_this_stats[i+1]), (max_disp_stats-i-1) * sizeof(int));
watch_this_stats[max_disp_stats-1] = 0;
init_stats_display();
}
else if (option == NUM_WATCH_STAT)
add_bar = 1;
else
{
statsinfo[watch_this_stats[i]-1].is_selected=0;
watch_this_stats[i] = option+1;
statsinfo[option].is_selected=1;
}
break;
}
}
// if we want another bar, assigning it to the first unwatched stat
if (add_bar)
{
int proposed_max_disp_stats = calc_max_disp_stats(calc_stats_bar_len(get_num_statsbar_exp()+1));
int next_free = -1;
for (i=0; i<NUM_WATCH_STAT-1; i++)
if (statsinfo[i].is_selected == 0)
{
next_free = i;
break;
}
for (i=0; next_free>=0 && i<proposed_max_disp_stats; i++)
if (watch_this_stats[i] == 0)
{
watch_this_stats[i] = next_free + 1;
statsinfo[next_free].is_selected =1 ;
break;
}
init_stats_display();
}
reset_statsbar_exp_cm_regions();
return 1;
}
static void cm_statsbar_pre_show_handler(window_info *win, int widget_id, int mx, int my, window_info *cm_win)
{
size_t i;
int proposed_max_disp_stats = calc_max_disp_stats(calc_stats_bar_len(get_num_statsbar_exp()+1));
for (i=0; i<NUM_WATCH_STAT-1; i++)
cm_grey_line(cm_id, i, 0);
for (i=0; i<max_disp_stats; i++)
if (watch_this_stats[i] > 0)
cm_grey_line(cm_id, watch_this_stats[i]-1, 1);
cm_grey_line(cm_id, NUM_WATCH_STAT, ((get_num_statsbar_exp() < proposed_max_disp_stats) ?0 :1));
cm_grey_line(cm_id, NUM_WATCH_STAT+1, ((watch_this_stats[1]==0)?1:0));
}
// the stats display
void init_stats_display()
{
int num_exp = get_num_statsbar_exp();
int actual_num_exp = 0;
//create the stats bar window
if(stats_bar_win < 0)
{
static size_t cm_id_ap = CM_INIT_VALUE;
stats_bar_win= create_window("Stats Bar", -1, 0, 0, window_height-44, window_width-HUD_MARGIN_X, 12, ELW_TITLE_NONE|ELW_SHOW_LAST);
set_window_handler(stats_bar_win, ELW_HANDLER_DISPLAY, &display_stats_bar_handler);
set_window_handler(stats_bar_win, ELW_HANDLER_MOUSEOVER, &mouseover_stats_bar_handler);
// context menu to enable/disable the action points bar
cm_id_ap = cm_create(cm_action_points_str, NULL);
cm_add_window(cm_id_ap, stats_bar_win);
cm_bool_line(cm_id_ap, 0, &show_action_bar, "show_action_bar");
}
else
init_window(stats_bar_win, -1, 0, 0, window_height-44, window_width-HUD_MARGIN_X, 12);
// calculate the statsbar len given curent config
stats_bar_len = calc_stats_bar_len(num_exp);
// calculate the maximum number of exp bars we can have
max_disp_stats = calc_max_disp_stats(stats_bar_len);
// if we need to reduce the number of bars, recalculate the optimum stats bar len
if (num_exp > max_disp_stats)
stats_bar_len = calc_stats_bar_len(max_disp_stats);
// all the bars are at the top of the window
mana_bar_start_y = food_bar_start_y = health_bar_start_y = load_bar_start_y = action_bar_start_y = exp_bar_start_y = 0;
// calculate the stats bar x position
mana_bar_start_x = stats_bar_text_len;
food_bar_start_x = stats_bar_len + 2 * stats_bar_text_len;
health_bar_start_x = 2 * stats_bar_len + 3 * stats_bar_text_len;
load_bar_start_x = 3 * stats_bar_len + 4 * stats_bar_text_len;
if (show_action_bar)
action_bar_start_x = 4 * stats_bar_len + 5 * stats_bar_text_len;
// the x position of the first exp bar
exp_bar_start_x = ((show_action_bar)?5:4) * (stats_bar_len + stats_bar_text_len) + exp_bar_text_len;
// clear any unused slots in the watch list and recalc how many are being displayed
if (max_disp_stats < MAX_WATCH_STATS)
{
size_t i;
for (i=max_disp_stats;i<MAX_WATCH_STATS;i++)
if (watch_this_stats[i] > 0)
{
statsinfo[watch_this_stats[i]-1].is_selected=0;
watch_this_stats[i]=0;
}
}
actual_num_exp = get_num_statsbar_exp();
free_statbar_space = (window_width-HUD_MARGIN_X-1) -
(exp_bar_start_x - exp_bar_text_len + actual_num_exp * (exp_bar_text_len + stats_bar_len));
// apologise if we had to reduce the number of exp bars
if (num_exp > actual_num_exp)
LOG_TO_CONSOLE(c_red2, remove_bar_message_str);
// create the exp bars context menu, used by all ative exp bars
if (!cm_valid(cm_id))
{
int thestat;
cm_id = cm_create(NULL, cm_statsbar_handler);
for (thestat=0; thestat<NUM_WATCH_STAT-1; thestat++)
cm_add(cm_id, (char *)statsinfo[thestat].skillnames->name, NULL);
cm_add(cm_id, cm_stats_bar_base_str, NULL);
cm_set_pre_show_handler(cm_id,cm_statsbar_pre_show_handler);
}
reset_statsbar_exp_cm_regions();
}
void draw_stats_bar(int x, int y, int val, int len, float r, float g, float b, float r2, float g2, float b2)
{
char buf[32];
int i; // i deals with massive bars by trimming at 110%
if(len>stats_bar_len*1.1)
i=stats_bar_len*1.1;
else
i=len;
glDisable(GL_TEXTURE_2D);
if(i >= 0){
glBegin(GL_QUADS);
//draw the colored section
glColor3f(r2, g2, b2);
glVertex3i(x, y+8, 0);
glColor3f(r, g, b);
glVertex3i(x, y, 0);
glColor3f(r, g, b);
glVertex3i(x+i, y, 0);
glColor3f(r2, g2, b2);
glVertex3i(x+i, y+8, 0);
glEnd();
}
// draw the bar frame
glColor3f(newcol_r, newcol_g, newcol_b);
glBegin(GL_LINE_LOOP);
glVertex3i(x, y, 0);
glVertex3i(x+stats_bar_len, y, 0);
glVertex3i(x+stats_bar_len, y+8, 0);
glVertex3i(x, y+8, 0);
glEnd();
glEnable(GL_TEXTURE_2D);
// handle the text
safe_snprintf(buf, sizeof(buf), "%d", val);
//glColor3f(0.8f, 0.8f, 0.8f); moved to next line
draw_string_small_shadowed(x-(1+8*strlen(buf))-1, y-2, (unsigned char*)buf, 1,0.8f, 0.8f, 0.8f,0.0f,0.0f,0.0f);
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
static void draw_side_stats_bar(const int x, const int y, const int baselev, const int cur_exp, const int nl_exp, size_t colour)
{
int len = 58-58.0f/(float)((float)(nl_exp-exp_lev[baselev])/(float)(nl_exp-cur_exp));
GLfloat colours[2][2][3] = { { {0.11f, 0.11f, 0.11f}, {0.3f, 0.5f, 0.2f} },
{ {0.10f,0.10f,0.80f}, {0.40f,0.40f,1.00f} } };
if (colour > 1)
colour = 0;
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
glDisable(GL_TEXTURE_2D);
if(len >= 0){
glBegin(GL_QUADS);
//draw the colored section
glColor3fv(colours[colour][0]);
glVertex3i(x, y+13, 0);
glColor3fv(colours[colour][1]);
glVertex3i(x, y, 0);
glColor3fv(colours[colour][1]);
glVertex3i(x+len, y, 0);
glColor3fv(colours[colour][0]);
glVertex3i(x+len, y+13, 0);
glEnd();
}
// draw the bar frame
glColor3f(newcol_r, newcol_g, newcol_b);
glBegin(GL_LINE_LOOP);
glVertex3i(x, y, 0);
glVertex3i(x+58, y, 0);
glVertex3i(x+58, y+13, 0);
glVertex3i(x, y+13, 0);
glEnd();
glEnable(GL_TEXTURE_2D);
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
static struct { int d; int h; Uint32 dt; Uint32 ht; } my_last_health = { 0, 0, 0, 0 };
/* called when our actor receives damage, displayed as hover over health bar */
void set_last_damage(int quantity)
{
my_last_health.d = quantity;
my_last_health.dt = SDL_GetTicks();
}
/* called when our actor heals, displayed as hover over health bar */
void set_last_heal(int quantity)
{
my_last_health.h = quantity;
my_last_health.ht = SDL_GetTicks();
}
/* draws damage and heal above the health bar */
static void draw_last_health_change(void)
{
char str[20];
static const Uint32 timeoutms = 2*60*1000;
static const int yoff = (int)(-(SMALL_FONT_Y_LEN+5));
/* damage in red */
if (my_last_health.d != 0)
{
if (abs(SDL_GetTicks() - my_last_health.dt) > timeoutms)
my_last_health.d = 0;
else
{
safe_snprintf(str, sizeof(str), " %d ", my_last_health.d);
show_help_coloured(str, health_bar_start_x+stats_bar_len/2-strlen(str)*SMALL_FONT_X_LEN, yoff, 1.0f, 0.0f, 0.0f);
}
}
/* heal in green */
if (my_last_health.h != 0)
{
if (abs(SDL_GetTicks() - my_last_health.ht) > timeoutms)
my_last_health.h = 0;
else
{
safe_snprintf(str, sizeof(str), " %d ", my_last_health.h);
show_help_coloured(str, health_bar_start_x+stats_bar_len/2, yoff, 0.0f, 1.0f, 0.0f);
}
}
}
int display_stats_bar_handler(window_info *win)
{
static Uint32 last_time = 0;
float health_adjusted_x_len;
float food_adjusted_x_len;
float mana_adjusted_x_len;
float load_adjusted_x_len;
float action_adjusted_x_len;
int over_health_bar;
// the space taken up by the exp bar text is minimised, but may change
// don't have to check often but this is an easy place to do it and its quick anyway
if (abs(SDL_GetTicks()-last_time) > 250)
{
int proposed_len = recalc_exp_bar_text_len();
if (proposed_len != exp_bar_text_len) // it will very rarely change
{
exp_bar_text_len = proposed_len;
init_stats_display();
}
last_time = SDL_GetTicks();
}
over_health_bar = statbar_cursor_x>health_bar_start_x && statbar_cursor_x < health_bar_start_x+stats_bar_len;
//get the adjusted length
if(!your_info.material_points.cur || !your_info.material_points.base)
health_adjusted_x_len=0;//we don't want a div by 0
else
health_adjusted_x_len=stats_bar_len/((float)your_info.material_points.base/(float)your_info.material_points.cur);
if(your_info.food_level<=0)
food_adjusted_x_len=0;//we don't want a div by 0
else
food_adjusted_x_len=stats_bar_len/((float)max_food_level/(float)your_info.food_level);
if(food_adjusted_x_len>stats_bar_len) food_adjusted_x_len=stats_bar_len;
if(!your_info.ethereal_points.cur || !your_info.ethereal_points.base)
mana_adjusted_x_len=0;//we don't want a div by 0
else
mana_adjusted_x_len=stats_bar_len/((float)your_info.ethereal_points.base/(float)your_info.ethereal_points.cur);
if(!your_info.carry_capacity.cur || !your_info.carry_capacity.base)
load_adjusted_x_len=0;//we don't want a div by 0
else