forked from naev/naev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.c
1654 lines (1461 loc) · 52.9 KB
/
info.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
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file info.h
*
* @brief Handles the info menu.
*/
/** @cond */
#include "naev.h"
/** @endcond */
#include "info.h"
#include "array.h"
#include "dialogue.h"
#include "equipment.h"
#include "gui.h"
#include "gui_osd.h"
#include "hook.h"
#include "land.h"
#include "log.h"
#include "map.h"
#include "menu.h"
#include "mission.h"
#include "ndata.h"
#include "nlua.h"
#include "nlua_tk.h"
#include "nstring.h"
#include "ntime.h"
#include "pilot.h"
#include "player.h"
#include "player_fleet.h"
#include "player_gui.h"
#include "player_inventory.h"
#include "shiplog.h"
#include "space.h"
#include "tk/toolkit_priv.h"
#include "toolkit.h"
#define BUTTON_WIDTH 135 /**< Button width, standard across menus. */
#define BUTTON_HEIGHT 30 /**< Button height, standard across menus. */
#define menu_Open(f) (menu_open |= (f)) /**< Marks a menu as opened. */
#define menu_Close(f) (menu_open &= ~(f)) /**< Marks a menu as closed. */
#define INFO_WINDOWS 7 /**< Amount of windows in the tab. */
#define INFO_WIN_MAIN 0
#define INFO_WIN_SHIP 1
#define INFO_WIN_WEAP 2
#define INFO_WIN_CARGO 3
#define INFO_WIN_MISN 4
#define INFO_WIN_STAND 5
#define INFO_WIN_SHIPLOG 6
static const char *info_names[INFO_WINDOWS] = {
N_("Main"),
N_("Ship"),
N_("Weapons"),
N_("Cargo"),
N_("Missions"),
N_("Standings"),
N_("Ship log"),
}; /**< Name of the tab windows. */
/**
* @brief For use with registered info buttons.
*/
typedef struct InfoButton_s {
int id; /**< Unique ID. */
char *caption; /**< Button caption. */
char *button; /**< Button widget name. */
int priority; /**< Button priority. */
/* Lua stuff .*/
nlua_env env; /**< Runtime environment. */
int func; /**< Function to call. */
SDL_Keycode key; /**< Hotkey (or SDLK_UNKNOWN==0 if none). */
} InfoButton_t;
static InfoButton_t *info_buttons = NULL;
static unsigned int info_wid = 0;
static unsigned int *info_windows = NULL;
static int info_lastTab; /**< Last open tab. */
static CstSlotWidget info_eq;
static CstSlotWidget info_eq_weaps;
static int *info_factions;
static int selectedMission = 0; /**< Current index in the missions list-box. */
static int selectedLog = 0;
static int selectedLogType = 0;
static char **logTypes=NULL;
static int ntypes=0;
static int nlogs=0;
static char **logs=NULL;
static int *logIDs=NULL;
static int logWidgetsReady=0;
/*
* prototypes
*/
/* information menu */
static void info_close( unsigned int wid, const char *str );
static void info_openMain( unsigned int wid );
static void info_openShip( unsigned int wid );
static void info_openWeapons( unsigned int wid );
static void info_openCargo( unsigned int wid );
static void info_openMissions( unsigned int wid );
static void info_getDim( unsigned int wid, int *w, int *h, int *lw );
static void info_buttonClick( unsigned int wid, const char *str );
static void standings_close( unsigned int wid, const char *str );
static void ship_update( unsigned int wid );
static void weapons_genList( unsigned int wid );
static void weapons_updateList( unsigned int wid, const char *str );
static void weapons_autoweap( unsigned int wid, const char *str );
static void weapons_fire( unsigned int wid, const char *str );
static void weapons_inrange( unsigned int wid, const char *str );
static void weapons_manual( unsigned int wid, const char *str );
static void weapons_volley( unsigned int wid, const char *str );
static void aim_lines( unsigned int wid, const char *str );
static void weapons_renderLegend( double bx, double by, double bw, double bh, void* data );
static void info_openStandings( unsigned int wid );
static void info_shiplogView( unsigned int wid, const char *str );
static void standings_update( unsigned int wid, const char *str );
static void cargo_genList( unsigned int wid );
static void cargo_update( unsigned int wid, const char *str );
static void cargo_jettison( unsigned int wid, const char *str );
static void mission_menu_chk_hide( unsigned int wid, const char *str );
static void mission_menu_chk_priority( unsigned int wid, const char *str );
static void mission_menu_abort( unsigned int wid, const char *str );
static void mission_menu_genList( unsigned int wid, int first );
static void mission_menu_update( unsigned int wid, const char *str );
static void info_openShipLog( unsigned int wid );
static const char* info_getLogTypeFilter( int lstPos );
static void info_changeTab( unsigned int wid, const char *str, int old, int new );
static int sort_buttons( const void *p1, const void *p2 )
{
const InfoButton_t *b1 = p1;
const InfoButton_t *b2 = p2;
if (b1->priority < b2->priority)
return -1;
else if (b1->priority > b2->priority)
return +1;
return strcmp(b1->caption,b2->caption);
}
static void info_buttonFree( InfoButton_t *btn )
{
free( btn->caption );
free( btn->button );
luaL_unref( naevL, LUA_REGISTRYINDEX, btn->func );
}
static void info_buttonRegen (void)
{
int wid, w, h, rows, cols;
if (info_wid == 0)
return;
wid = info_windows[ INFO_WIN_MAIN ];
window_dimWindow( wid, &w, &h );
cols = (w-20) / (20+BUTTON_WIDTH);
rows = 1 + (array_size(info_buttons)) / cols;
for (int i=0; i<array_size(info_buttons); i++) {
InfoButton_t *btn = &info_buttons[i];
int r = (i+1)/cols, c = (i+1)%cols;
if (widget_exists( wid, btn->button ))
window_destroyWidget( wid, btn->button );
window_addButtonKey( wid, -20 - c*(20+BUTTON_WIDTH), 20 + r*(20+BUTTON_HEIGHT),
BUTTON_WIDTH, BUTTON_HEIGHT,
btn->button, btn->caption, info_buttonClick, btn->key );
}
window_resizeWidget( wid, "lstInventory", w-80-240-40-40, h-90 - rows*(20+BUTTON_HEIGHT) );
window_moveWidget( wid, "lstInventory", -20, -70 );
}
/**
* @brief Registers a button in the info menu.
*
* @param caption Caption to give the button.
* @param priority Button priority, lower is more important.
* @param key Hotkey for using the button without it being focused (or SDLK_UNKNOWN or 0 if none).
* @return Newly created button ID.
*/
int info_buttonRegister( const char *caption, int priority, SDL_Keycode key )
{
static int button_idgen = 0;
int id;
InfoButton_t *btn;
if (info_buttons == NULL)
info_buttons = array_create( InfoButton_t );
btn = &array_grow( &info_buttons );
btn->id = ++button_idgen;
btn->caption= strdup( caption );
SDL_asprintf( &btn->button, "btnExtra::%s", caption );
btn->priority = priority;
btn->env = __NLUA_CURENV;
btn->func = luaL_ref( naevL, LUA_REGISTRYINDEX );
btn->key = key;
id = btn->id;
qsort( info_buttons, array_size(info_buttons), sizeof(InfoButton_t), sort_buttons );
info_buttonRegen();
return id;
}
/**
* @brief Unregisters a button in the info menu.
*
* @param id ID of the button to unregister and previously created by info_buttonRegister.
* @return 0 on success.
*/
int info_buttonUnregister( int id )
{
for (int i=0; i<array_size(info_buttons); i++) {
InfoButton_t *btn = &info_buttons[i];
if (btn->id != id)
continue;
if (info_wid != 0) {
int wid = info_windows[ INFO_WIN_MAIN ];
if (widget_exists( wid, btn->button ))
window_destroyWidget( wid, btn->button );
}
info_buttonFree( btn );
array_erase( &info_buttons, btn, btn+1 );
info_buttonRegen();
return 0;
}
return -1;
}
/**
* @brief Clears all the registered buttons.
*/
void info_buttonClear (void)
{
for (int i=0; i<array_size(info_buttons); i++) {
InfoButton_t *btn = &info_buttons[i];
if (info_wid != 0) {
int wid = info_windows[ INFO_WIN_MAIN ];
if (widget_exists( wid, btn->button ))
window_destroyWidget( wid, btn->button );
}
info_buttonFree( btn );
}
array_free( info_buttons );
info_buttons = NULL;
info_buttonRegen();
}
static void info_buttonClick( unsigned int wid, const char *str )
{
(void) wid;
for (int i=0; i<array_size(info_buttons); i++) {
InfoButton_t *btn = &info_buttons[i];
if (strcmp( btn->button, str )!=0)
continue;
lua_rawgeti( naevL, LUA_REGISTRYINDEX, btn->func );
if (nlua_pcall( btn->env, 0, 0 )) {
WARN( _("Failure to run info button with id '%d':\n%s"), btn->id, lua_tostring( naevL, -1 ) );
lua_pop( naevL, 1 );
}
land_needsTakeoff( 1 ); /* Script said so? */
return;
}
}
/**
* @brief Opens the information menu.
*/
void menu_info( int window )
{
int w, h;
const char *names[INFO_WINDOWS];
/* Not under manual control. */
if (pilot_isFlag( player.p, PILOT_MANUAL_CONTROL ))
return;
/* Open closes when previously opened. */
if (menu_isOpen(MENU_INFO) || dialogue_isOpen()) {
if ((info_wid > 0) && !window_isTop(info_wid))
return;
info_close( 0, NULL );
return;
}
/* Dimensions. */
w = 640;
h = 680;
/* Create the window. */
info_wid = window_create( "wdwInfo", _("Info"), -1, -1, w, h );
window_setCancel( info_wid, info_close );
/* Create tabbed window. */
for (size_t i=0; i<INFO_WINDOWS; i++)
names[i] = _(info_names[i]);
info_windows = window_addTabbedWindow( info_wid, -1, -1, -1, -1, "tabInfo",
INFO_WINDOWS, names, 0 );
/* Open the subwindows. */
info_openMain( info_windows[ INFO_WIN_MAIN ] );
info_openShip( info_windows[ INFO_WIN_SHIP ] );
info_openWeapons( info_windows[ INFO_WIN_WEAP ] );
info_openCargo( info_windows[ INFO_WIN_CARGO ] );
info_openMissions( info_windows[ INFO_WIN_MISN ] );
info_openStandings( info_windows[ INFO_WIN_STAND ] );
info_openShipLog( info_windows[ INFO_WIN_SHIPLOG ] );
menu_Open(MENU_INFO);
/* Opening hooks. */
hooks_run("info");
/* Set active window. */
window_tabWinOnChange( info_wid, "tabInfo", info_changeTab );
if (window == INFO_DEFAULT)
window = info_lastTab;
window_tabWinSetActive( info_wid, "tabInfo", CLAMP( 0, 6, window ) );
}
/**
* @brief Closes the information menu.
* @param str Unused.
*/
static void info_close( unsigned int wid, const char *str )
{
(void) wid;
if (info_wid > 0) {
info_lastTab = window_tabWinGetActive( info_wid, "tabInfo" );
/* Copy weapon sets over if changed. */
ws_copy( player.ps.weapon_sets, player.p->weapon_sets );
window_close( info_wid, str );
info_wid = 0;
info_windows = NULL;
logs = NULL;
menu_Close(MENU_INFO);
}
}
/**
* @brief Updates the info windows.
*/
void info_update (void)
{
if (info_windows != NULL)
weapons_genList( info_windows[ INFO_WIN_WEAP ] );
}
/**
* @brief Opens the main info window.
*/
static void info_openMain( unsigned int wid )
{
const char **lic;
char str[STRMAX_SHORT], creds[ECON_CRED_STRLEN];
char **inventory;
char *nt;
int w, h, cargo_used, cargo_total, n;
unsigned int destroyed;
const PlayerItem *inv;
size_t k = 0, l = 0;
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Compute ships destroyed. */
destroyed = 0;
for (int i=0; i<SHIP_CLASS_TOTAL; i++)
destroyed += player.ships_destroyed[i];
/* pilot generics */
nt = ntime_pretty( ntime_get(), 2 );
k += scnprintf( &str[k], sizeof(str)-k, "%s", _("Pilot:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Date:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n\n%s", _("Money:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Current Ship:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Fuel:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", (player.fleet_capacity > 0) ? _("Cargo (fleet):") : _("Cargo:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n\n%s", _("Time played:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Times died:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Times jumped:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Times landed:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Damage done:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Damage taken:") );
k += scnprintf( &str[k], sizeof(str)-k, "\n%s", _("Ships destroyed:") );
window_addText( wid, 20, 20, 120, h-80, 0, "txtDPilot", &gl_smallFont, &cFontGrey, str );
credits2str( creds, player.p->credits, 2 );
l += scnprintf( &str[l], sizeof(str)-l, "%s", player.name );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", nt );
l += scnprintf( &str[l], sizeof(str)-l, "\n\n%s", creds );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", player.p->name );
l += scnprintf( &str[l], sizeof(str)-l, "\n%.0f (%d %s)",
player.p->fuel, pilot_getJumps(player.p), n_( "jump", "jumps", pilot_getJumps(player.p) ) );
cargo_used = pfleet_cargoUsed();
cargo_total = cargo_used + pfleet_cargoFree();
l += scnprintf( &str[l], sizeof(str)-l, "\n%d / %d %s", cargo_used, cargo_total, n_( "tonne", "tonnes", cargo_total ) );
l += scnprintf( &str[l], sizeof(str)-l, "%s", "\n\n" );
l += scnprintf( &str[l], sizeof(str)-l, _("%.1f hours"), player.time_played / 3600. );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", num2strU((double)player.death_counter,0) );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", num2strU((double)player.jumped_times, 0) );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s\n", num2strU((double)player.landed_times, 0) );
l += scnprintf( &str[l], sizeof(str)-l, _("%s MJ"), num2strU(player.dmg_done_shield + player.dmg_done_armour, 0) );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", "" );
l += scnprintf( &str[l], sizeof(str)-l, _("%s MJ"), num2strU(player.dmg_taken_shield + player.dmg_taken_armour, 0) );
l += scnprintf( &str[l], sizeof(str)-l, "\n%s", num2strU(destroyed, 0) );
window_addText( wid, 160, 20,
w-80-160-40+20-180, h-80,
0, "txtPilot", &gl_smallFont, NULL, str );
free(nt);
/* menu */
window_addButton( wid, -20, 20,
BUTTON_WIDTH, BUTTON_HEIGHT,
"btnClose", _("Close"), info_close );
/* TODO probably add alt text with descriptions. */
lic = player_getLicenses();
inv = player_inventory();
n = array_size(lic) + array_size(inv);
/* List. */
if (n == 0) {
inventory = malloc(sizeof(char*));
inventory[0] = strdup(_("None"));
n = 1;
}
else {
int nlic = array_size(lic);
int ninv = array_size(inv);
inventory = malloc(sizeof(char*) * n);
for (int i=0; i<nlic; i++)
SDL_asprintf( &inventory[i], "#n%s#0%s", _("License: "), _(lic[i]) );
qsort( inventory, nlic, sizeof(char*), strsort );
for (int i=0; i<ninv; i++) {
const PlayerItem *pi = &inv[i];
if (pi->quantity == 0)
SDL_asprintf( &inventory[nlic+i], "%s", _(pi->name) );
else
SDL_asprintf( &inventory[nlic+i], _("%s (%d)"), _(pi->name), pi->quantity );
}
qsort( &inventory[nlic], ninv, sizeof(char*), strsort );
}
window_addText( wid, -20, -40, w-80-240-40-40, 20, 1, "txtList",
NULL, NULL, _("Inventory") );
window_addList( wid, -20, -70, w-80-240-40-40, h-110-BUTTON_HEIGHT,
"lstInventory", inventory, n, 0, NULL, NULL );
window_setFocus( wid, "lstInventory" );
info_buttonRegen();
}
/**
* @brief Shows the player what outfits he has.
*
* @param str Unused.
*/
static void info_openShip( unsigned int wid )
{
int w, h;
char buf[STRMAX];
size_t l = 0;
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Buttons */
window_addButton( wid, -20, 20,
BUTTON_WIDTH, BUTTON_HEIGHT,
"closeOutfits", _("Close"), info_close );
/* Text. */
l += scnprintf( &buf[l], sizeof(buf)-l, "%s", _("Name:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Model:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Class:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Crew:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Mass:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Jump Time:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Thrust:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Speed:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Turn:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Time Constant:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Absorption:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Shield:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Armour:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Energy:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Cargo Space:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Fuel:") );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _("Stats:") );
window_addText( wid, 20, -40, 100, h-60, 0, "txtSDesc", &gl_smallFont, &cFontGrey, buf );
window_addText( wid, 160, -40, w-20-20-20-160-180., h-60, 0, "txtDDesc", &gl_smallFont,
NULL, NULL );
/* Custom widget. */
equipment_slotWidget( wid, -20, -40, 180, h-60, &info_eq );
info_eq.selected = &player.ps;
info_eq.canmodify = 0;
/* Update ship. */
ship_update( wid );
}
/**
* @brief Updates the ship stuff.
*/
static void ship_update( unsigned int wid )
{
char buf[STRMAX_SHORT], *hyp_delay;
size_t l = 0;
int cargo = pilot_cargoUsed( player.p ) + pilot_cargoFree( player.p );
hyp_delay = ntime_pretty( pilot_hyperspaceDelay( player.p ), 2 );
/* Generic */
l += scnprintf( &buf[l], sizeof(buf)-l, "%s", player.p->name );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _(player.p->ship->name) );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", _(ship_class(player.p->ship)) );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%d", (int)floor(player.p->crew) );
/* Movement. */
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%.0f %s", player.p->solid.mass, n_( "tonne", "tonnes", player.p->solid.mass ) );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%s average"), hyp_delay );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f kN/tonne"), player.p->thrust / player.p->solid.mass );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f m/s (max %.0f m/s)"),
player.p->speed, solid_maxspeed( &player.p->solid, player.p->speed, player.p->thrust ) );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f deg/s"), player.p->turn*180./M_PI );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%.0f%%", player.p->stats.time_mod * player.p->ship->dt_default * 100. );
/* Health. */
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%.0f%%", player.p->dmg_absorb * 100. );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f / %.0f MJ (%.1f MW)"), player.p->shield, player.p->shield_max, player.p->shield_regen );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f / %.0f MJ (%.1f MW)"), player.p->armour, player.p->armour_max, player.p->armour_regen );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%s", "" );
l += scnprintf( &buf[l], sizeof(buf)-l, _("%.0f / %.0f MJ (%.1f MW)"), player.p->energy, player.p->energy_max, player.p->energy_regen );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%d / %d %s", pilot_cargoUsed( player.p ), cargo, n_( "tonne", "tonnes", cargo ) );
l += scnprintf( &buf[l], sizeof(buf)-l, "\n%.0f / %.0f %s (%d %s)",
player.p->fuel, player.p->fuel_max, n_( "unit", "units", player.p->fuel_max ),
pilot_getJumps(player.p), n_( "jump", "jumps", pilot_getJumps(player.p) ) );
l += scnprintf( &buf[l], sizeof(buf)-l, "%s", "\n\n" );
equipment_shipStats( &buf[l], sizeof(buf)-l, player.p, 0, 0 );
window_modifyText( wid, "txtDDesc", buf );
free( hyp_delay );
}
/**
* @brief Opens the weapons window.
*/
static void info_openWeapons( unsigned int wid )
{
int w, h, x, y, wlen;
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Custom widget. */
equipment_slotWidget( wid, 20, -40, 180, h-60, &info_eq_weaps );
info_eq_weaps.selected = &player.ps;
info_eq_weaps.weapons = 0;
info_eq_weaps.canmodify = 0;
/* Custom widget for legend. */
y = -220;
window_addCust( wid, 220, y, w-200-60, 100, "cstLegend", 0,
weapons_renderLegend, NULL, NULL, NULL, NULL );
/* Checkboxes. */
wlen = w - 220 - 20;
x = 220;
y -= 100;
window_addText( wid, x, y, wlen, 20, 0, "txtLocal", NULL, NULL,
_("Current Weapon Set Settings"));
y -= 20;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkFire", _("Enable instant Mode"), weapons_fire,
(pilot_weapSetTypeCheck( player.p, info_eq_weaps.weapons )==WEAPSET_TYPE_WEAPON) );
y -= 30;
window_addText( wid, x+10, y, wlen, 20, 0, "txtSInstant", NULL, NULL, _("(Weapons fire when this weapon set key is pressed)"));
y -= 20;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkInrange", _("Only shoot weapons that are in range"), weapons_inrange,
pilot_weapSetInrangeCheck( player.p, info_eq_weaps.weapons ) );
y -= 30;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkManual", _("Enable manual aiming mode."), weapons_manual,
pilot_weapSetManualCheck( player.p, info_eq_weaps.weapons ) );
y -= 30;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkVolley", _("Enable volley mode."), weapons_volley,
pilot_weapSetVolleyCheck( player.p, info_eq_weaps.weapons ) );
y -= 40;
window_addText( wid, x, y, wlen, 20, 0, "txtGlobal", NULL, NULL,
_("Global Settings"));
y -= 20;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkAutoweap", _("Automatically handle weapons"), weapons_autoweap, player.p->autoweap );
y -= 30;
window_addCheckbox( wid, x+10, y, wlen, BUTTON_HEIGHT,
"chkHelper", _("Dogfight aiming helper"), aim_lines, player.p->aimLines );
/* List. Has to be generated after checkboxes. */
weapons_genList( wid );
/* Buttons */
window_addButton( wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"closeCargo", _("Close"), info_close );
}
/**
* @brief Generates the weapons list.
*/
static void weapons_genList( unsigned int wid )
{
char **buf, tbuf[STRMAX_SHORT];
int n, w, h;
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Destroy widget if needed. */
if (widget_exists( wid, "lstWeapSets" )) {
window_destroyWidget( wid, "lstWeapSets" );
n = toolkit_getListPos( wid, "lstWeapSets" );
}
else
n = -1;
/* List */
buf = malloc( sizeof(char*) * PILOT_WEAPON_SETS );
for (int i=0; i<PILOT_WEAPON_SETS; i++) {
const char *str = pilot_weapSetName( info_eq_weaps.selected->p, i );
if (str == NULL)
snprintf( tbuf, sizeof(tbuf), "%d - ??", (i+1)%10 );
else
snprintf( tbuf, sizeof(tbuf), "%d - %s", (i+1)%10, str );
buf[i] = strdup( tbuf );
}
window_addList( wid, 20+180+20, -40,
w - (20+180+20+20), 180,
"lstWeapSets", buf, PILOT_WEAPON_SETS,
0, weapons_updateList, NULL );
window_setFocus( wid, "lstWeapSets" );
/* Restore position. */
if (n >= 0)
toolkit_setListPos( wid, "lstWeapSets", n );
}
/**
* @brief Updates the weapon sets.
*/
static void weapons_updateList( unsigned int wid, const char *str )
{
(void) str;
int pos;
/* Update the position. */
pos = toolkit_getListPos( wid, "lstWeapSets" );
if (pos < 0)
return;
info_eq_weaps.weapons = pos;
/* Update fire mode. */
window_checkboxSet( wid, "chkFire",
(pilot_weapSetTypeCheck( player.p, pos ) == WEAPSET_TYPE_WEAPON) );
/* Update inrange. */
window_checkboxSet( wid, "chkInrange",
pilot_weapSetInrangeCheck( player.p, pos ) );
/* Update manual aiming. */
window_checkboxSet( wid, "chkManual",
pilot_weapSetManualCheck( player.p, pos ) );
/* Update autoweap. */
window_checkboxSet( wid, "chkAutoweap", player.p->autoweap );
}
/**
* @brief Toggles autoweap for the ship.
*/
static void weapons_autoweap( unsigned int wid, const char *str )
{
/* Set state. */
int state = window_checkboxState( wid, str );
/* Run autoweapons if needed. */
if (state) {
int sure = dialogue_YesNoRaw( _("Enable autoweapons?"),
_("Are you sure you want to enable automatic weapon groups for the "
"ship?\n\nThis will overwrite all manually-tweaked weapons groups.") );
if (!sure) {
window_checkboxSet( wid, str, 0 );
return;
}
player.p->autoweap = 1;
pilot_weaponAuto( player.p );
weapons_genList( wid );
}
else
player.p->autoweap = 0;
}
/**
* @brief Sets the fire mode.
*/
static void weapons_fire( unsigned int wid, const char *str )
{
int i, state, t, c;
/* Set state. */
state = window_checkboxState( wid, str );
/* See how to handle. */
t = pilot_weapSetTypeCheck( player.p, info_eq_weaps.weapons );
if (t == WEAPSET_TYPE_ACTIVE)
return;
if (state)
c = WEAPSET_TYPE_WEAPON;
else
c = WEAPSET_TYPE_CHANGE;
pilot_weapSetType( player.p, info_eq_weaps.weapons, c );
/* Check to see if they are all fire groups. */
for (i=0; i<PILOT_WEAPON_SETS; i++)
if (!pilot_weapSetTypeCheck( player.p, i ))
break;
/* Not able to set them all to fire groups. */
if (i >= PILOT_WEAPON_SETS) {
dialogue_alert( _("You can not set all your weapon sets to fire groups!") );
pilot_weapSetType( player.p, info_eq_weaps.weapons, WEAPSET_TYPE_CHANGE );
window_checkboxSet( wid, str, 0 );
}
/* Set default if needs updating. */
pilot_weaponSetDefault( player.p );
/* Must regen. */
weapons_genList( wid );
}
/**
* @brief Sets the inrange property.
*/
static void weapons_inrange( unsigned int wid, const char *str )
{
int state = window_checkboxState( wid, str );
pilot_weapSetInrange( player.p, info_eq_weaps.weapons, state );
}
/**
* @brief Sets the manual aim property.
*/
static void weapons_manual( unsigned int wid, const char *str )
{
int state = window_checkboxState( wid, str );
pilot_weapSetManual( player.p, info_eq_weaps.weapons, state );
}
/**
* @brief Sets the volley aim property.
*/
static void weapons_volley( unsigned int wid, const char *str )
{
int state = window_checkboxState( wid, str );
pilot_weapSetVolley( player.p, info_eq_weaps.weapons, state );
}
/**
* @brief Sets the aim lines property.
*/
static void aim_lines( unsigned int wid, const char *str )
{
int state = window_checkboxState( wid, str );
player.p->aimLines = state;
}
/**
* @brief Renders the legend.
*/
static void weapons_renderLegend( double bx, double by, double bw, double bh, void* data )
{
(void) data;
(void) bw;
(void) bh;
double y;
y = by+bh-20;
gl_print( &gl_defFont, bx, y, &cFontWhite, p_("info", "Legend") );
y -= 20.;
toolkit_drawRect( bx, y, 10, 10, &cFontBlue, NULL );
gl_print( &gl_smallFont, bx+20, y, &cFontWhite, _("Outfit that can be activated") );
y -= 20.;
toolkit_drawRect( bx, y, 10, 10, &cFontYellow, NULL );
gl_print( &gl_smallFont, bx+20, y, &cFontWhite, _("Secondary Weapon (Right click toggles)") );
y -= 20.;
toolkit_drawRect( bx, y, 10, 10, &cFontRed, NULL );
gl_print( &gl_smallFont, bx+20, y, &cFontWhite, _("Primary Weapon (Left click toggles)") );
}
/**
* @brief Shows the player their cargo.
*
* @param str Unused.
*/
static void info_openCargo( unsigned int wid )
{
int w, h;
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Buttons */
window_addButton( wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"closeCargo", _("Close"), info_close );
window_addButton( wid, -20-BUTTON_WIDTH-10, 20,
BUTTON_WIDTH, BUTTON_HEIGHT, "btnJettisonCargo", _("Jettison"),
cargo_jettison );
window_disableButton( wid, "btnJettisonCargo" );
/* Description. */
window_addText( wid, 20, -40-200-20,
w - 40, h - BUTTON_HEIGHT - 260, 0,
"txtCargoDesc", NULL, NULL, NULL );
/* Generate the list. */
cargo_genList( wid );
}
/**
* @brief Generates the cargo list.
*/
static void cargo_genList( unsigned int wid )
{
char **buf;
int nbuf;
int w, h;
PilotCommodity *pclist = pfleet_cargoList();
/* Get the dimensions. */
window_dimWindow( wid, &w, &h );
/* Destroy widget if needed. */
if (widget_exists( wid, "lstCargo" ))
window_destroyWidget( wid, "lstCargo" );
/* List */
if (array_size(pclist)==0) {
/* No cargo */
buf = malloc(sizeof(char*));
buf[0] = strdup(_("None"));
nbuf = 1;
}
else {
/* List the player fleet's cargo. */
buf = malloc( sizeof(char*) * array_size(pclist) );
for (int i=0; i<array_size(pclist); i++) {
PilotCommodity *pc = &pclist[i];
int misn = (pc->id != 0);
int illegal = (array_size(pc->commodity->illegalto)>0);
SDL_asprintf(&buf[i], "%s %d%s%s",
_(pc->commodity->name),
pc->quantity,
misn ? _(" [#bMission#0]") : "",
illegal ? _(" (#rillegal#0)") : "" );
}
nbuf = array_size(pclist);
}
array_free(pclist);
window_addList( wid, 20, -40, w - 40, 200,
"lstCargo", buf, nbuf, 0, cargo_update, NULL );
window_setFocus( wid, "lstCargo" );
}
/**
* @brief Updates the player's cargo in the cargo menu.
* @param str Unused.
*/
static void cargo_update( unsigned int wid, const char *str )
{
(void) str;
char desc[STRMAX];
int pos, l;
const Commodity *com;
PilotCommodity *pclist = pfleet_cargoList();
if (array_size(pclist) <= 0) {
window_modifyText( wid, "txtCargoDesc", NULL );
array_free(pclist);
return; /* No cargo, redundant check */
}
/* Can jettison all but mission cargo when not landed*/
if (landed)
window_disableButton( wid, "btnJettisonCargo" );
else
window_enableButton( wid, "btnJettisonCargo" );
pos = toolkit_getListPos( wid, "lstCargo" );
com = pclist[pos].commodity;
if (!com->description)
l = scnprintf( desc, sizeof(desc), "%s", _(com->name) );
else
l = scnprintf( desc, sizeof(desc), "%s\n\n%s", _(com->name), _(com->description) );
/* Only add fleet information with fleet capacity. */
if ((player.fleet_capacity > 0) && (pclist[pos].quantity > 0)) {
l += scnprintf( &desc[l], sizeof(desc)-l, "\n\n%s", _("Carried by the following ships in your fleet:\n") );
PFleetCargo *plist = pfleet_cargoListShips( com );
for (int i=0; i<array_size(plist); i++)
l += scnprintf( &desc[l], sizeof(desc)-l, _("\n - %s (%d)"), plist[i].p->name, plist[i].q );
array_free(plist);
}
/* Add message on illegal outfits. */
if (array_size(com->illegalto) > 0) {
l += scnprintf( &desc[l], sizeof(desc)-l, "\n\n%s", _("Illegalized by the following factions:\n") );
for (int i=0; i<array_size(com->illegalto); i++) {
int f = com->illegalto[i];
if (!faction_isKnown(f))
continue;
l += scnprintf( &desc[l], sizeof(desc)-l, _("\n - %s"), _(faction_name(f)) );
}
}
window_modifyText( wid, "txtCargoDesc", desc );
array_free(pclist);
}
/**
* @brief Makes the player jettison the currently selected cargo.
* @param str Unused.
*/
static void cargo_jettison( unsigned int wid, const char *str )
{
(void)str;
int pos, ret;
Mission *misn;
HookParam hparam[3];
PilotCommodity *pclist = pfleet_cargoList();
if (array_size(pclist) <= 0) {
array_free(pclist);
return; /* No cargo, redundant check */
}
pos = toolkit_getListPos( wid, "lstCargo" );
/* Special case mission cargo. */
if (pclist[pos].id != 0) {
int f;
if (!dialogue_YesNo( _("Abort Mission"),
_("Are you sure you want to abort this mission?") )) {
array_free(pclist);
return;
}
/* Get the mission. */
f = -1;
for (int i=0; i<array_size(player_missions); i++) {
for (int j=0; j<array_size(player_missions[i]->cargo); j++) {
if (player_missions[i]->cargo[j] == pclist[pos].id) {
f = i;
break;
}
}
if (f >= 0)
break;
}
if (f < 0) {
WARN(_("Cargo '%d' does not belong to any active mission."),
pclist[pos].id);
array_free( pclist );
return;
}
misn = player_missions[f];
/* We run the "abort" function if it's found. */