-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterface.cpp
More file actions
4948 lines (4252 loc) · 161 KB
/
interface.cpp
File metadata and controls
4948 lines (4252 loc) · 161 KB
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 "interface.h"
#include "window.h"
#include "cuss.h"
#include "city.h"
#include "player_city.h"
#include "stringfunc.h"
#include "keys.h"
#include "building.h"
#include "animal.h"
#include "rng.h"
#include "globals.h"
#include "files.h"
#include "help.h"
#include <sstream>
#include <fstream>
#include <cstdarg> // For the variadic function below
#include <map>
Interface::Interface()
{
cur_menu = MENU_NULL;
cur_mode = IMODE_NULL;
cur_data_mode = DATA_MODE_CITIZENS;
message_offset = 0;
next_menu_posx = 2;
sel = Point(4, 4);
city_radius = true;
show_terrain = false;
current_area = AREA_NULL;
temp_text = false;
}
Interface::~Interface()
{
}
bool Interface::init()
{
bool errors = false;
std::stringstream ss_errors;
ss_errors << "Interface initialized with ";
if (errors) {
return false;
}
if (!i_main.load_from_file("cuss/interface.cuss")) {
debugmsg("Failed to load critical interface file cuss/interface.cuss!");
return false;
}
w_main.init(0, 0, 80, 24);
// Menu setup! Put menus here.
add_menu(MENU_GAME, "Game",
"Save & Quit",
"Quit without saving",
"About",
0
);
add_menu(MENU_MINISTERS, "Ministers",
"Finance",
"Farms & Food",
"Hunting",
"Livestock",
"Mines & Minerals",
"Morale",
"Trade",
0
);
add_menu(MENU_BUILDINGS, "Buildings",
"Status",
"Build",
0
);
add_menu(MENU_WORLD, "World",
"View Map",
0
);
add_menu(MENU_HELP, "Help",
"Index",
"Search",
0
);
set_menu_str();
pl_city = GAME->city;
return true;
}
bool Interface::starting_screen()
{
Window w_start(0, 0, 80, 24);
cuss::interface i_start;
if (!i_start.load_from_file("cuss/start.cuss")) {
return false;
}
std::ifstream fin;
std::string motd;
std::vector<std::string> art;
fin.open("motd.txt");
if (fin.is_open()) {
std::string tmp;
while (!fin.eof()) {
getline(fin, tmp);
if (tmp.empty()) {
motd += "\n";
} else {
motd += tmp;
}
}
fin.close();
}
i_start.set_data("text_motd", motd);
fin.open("art.txt");
if (fin.is_open()) {
std::string tmpart, tmpline;
while (!fin.eof()) {
getline(fin, tmpline);
if (!tmpline.empty() && tmpline[0] == '#' &&
(tmpline.length() == 1 || tmpline[1] == ' ')) {
continue; // It's a comment
} else if (tmpline.length() == 1 && tmpline[0] == '%') {
art.push_back(tmpart);
tmpart = "";
} else {
tmpart += tmpline;
tmpart += "\n";
tmpline = "";
}
}
if (!tmpart.empty()) {
tmpart = tmpart.substr( 0, tmpart.length() - 1 ); // Strip off \n
art.push_back(tmpart);
}
fin.close();
}
if (!art.empty()) {
i_start.set_data("text_art", art[ rng(0, art.size() - 1) ]);
}
if (GAME->load_world()) {
i_start.set_data("text_world_name", GAME->world->get_name());
i_start.set_data("text_world_name", c_yellow);
} else {
i_start.set_data("text_world_name", "<c=ltred>No world exists! Press \
<c=pink>G<c=ltred> to generate one.<c=/>");
}
i_start.draw(&w_start);
w_start.refresh();
long ch;
while (true) { // Loop until we exit the function via L N or Q
ch = input();
switch (ch) {
case 'h':
case 'H':
help_index();
i_start.draw(&w_start);
w_start.refresh();
break;
case 'l':
case 'L':
// Load city
if (load_game()) {
return true;
}
i_start.draw(&w_start);
w_start.refresh();
break;
case 'n':
case 'N':
// New city
if (GAME->start_new_game()) {
return true;
}
i_start.draw(&w_start);
w_start.refresh();
break;
case 'g':
case 'G':
// Generate new world
if (!GAME->is_world_ready() ||
query_yn("Overwrite %s with a new world?",
GAME->world->get_name().c_str())) {
World_design design;
if (world_design_screen(design)) {
GAME->generate_world(&design);
i_start.set_data("text_world_name", GAME->world->get_name());
i_start.set_data("text_world_name", c_yellow);
}
}
i_start.draw(&w_start);
w_start.refresh();
break;
case 'q':
case 'Q':
return false;
}
} // while (true)
return true;
}
bool Interface::world_design_screen(World_design& design)
{
cuss::interface i_design;
if (!i_design.load_from_file("cuss/world_design.cuss")) {
return false;
}
Window w_design(0, 0, 80, 24);
/* Set up vectors containing the names of world paraments (size, temp, etc)
* These are used for populating the popup menus when we change these values; by
* setting up vectors now, we avoid having to build them every time we change
* them.
*/
std::vector<std::string> world_size_names, world_temp_names, world_rain_names,
world_mountain_names, race_names;
for (int i = 0; i < WORLD_SIZE_MAX; i++) {
world_size_names.push_back( world_size_name( World_size(i) ) );
}
for (int i = 0; i < WORLD_TEMP_MAX; i++) {
world_temp_names.push_back( world_temperature_name( World_temperature(i) ));
}
for (int i = 0; i < WORLD_RAIN_MAX; i++) {
world_rain_names.push_back( world_rainfall_name( World_rainfall(i) ) );
}
for (int i = 0; i < WORLD_MOUNTAIN_MAX; i++) {
world_mountain_names.push_back( world_mountain_name( World_mountain(i) ) );
}
// Start at 1 to skip RACE_NULL
for (int i = 1; i < RACE_MAX; i++) {
race_names.push_back( capitalize_all_words(Race_data[i]->plural_name) );
}
i_design.ref_data("entry_name", &(design.name));
std::vector<std::string> races;
i_design.ref_data("list_kingdoms", &races);
i_design.set_data("text_size", world_size_name(design.size) );
i_design.set_data("text_size", c_yellow);
i_design.set_data("num_kingdoms_recommended",
world_size_kingdoms( design.size ) );
i_design.set_data("num_kingdoms_recommended", c_ltgreen);
i_design.set_data("text_temperature",
world_temperature_name(design.temperature) );
i_design.set_data("text_temperature",
world_temperature_color(design.temperature) );
i_design.set_data("text_rainfall", world_rainfall_name(design.rainfall) );
i_design.set_data("text_rainfall", world_rainfall_color(design.rainfall) );
i_design.set_data("text_mountains", world_mountain_name(design.mountain) );
i_design.set_data("text_mountains", world_mountain_color(design.mountain) );
i_design.select("entry_name");
while (true) {
i_design.draw(&w_design);
w_design.refresh();
long ch = getch();
switch (ch) {
case '!':
design.name = get_random_world_name();
break;
case '@': {
design.size = World_size( menu_vec("World Size", world_size_names) );
i_design.set_data("text_size", world_size_name( design.size ) );
int num_kingdoms = world_size_kingdoms( design.size );
i_design.set_data("num_kingdoms_recommended", num_kingdoms);
// Colorize num_kingdoms_recommended
if (num_kingdoms > design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_ltgreen);
} else if (num_kingdoms == design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_yellow);
} else { // Too many kingdoms!
i_design.set_data("num_kingdoms_recommended", c_red);
}
} break;
case '#':
design.temperature =
World_temperature( menu_vec("World Temperature", world_temp_names) );
i_design.set_data("text_temperature",
world_temperature_name ( design.temperature ) );
i_design.set_data("text_temperature",
world_temperature_color( design.temperature ) );
break;
case '$':
design.rainfall =
World_rainfall( menu_vec("World Rainfall", world_rain_names) );
i_design.set_data("text_rainfall",
world_rainfall_name ( design.rainfall ) );
i_design.set_data("text_rainfall",
world_rainfall_color( design.rainfall ) );
break;
case '%':
design.mountain =
World_mountain( menu_vec("World Mountains", world_mountain_names) );
i_design.set_data("text_mountains",
world_mountain_name ( design.mountain ) );
i_design.set_data("text_mountains",
world_mountain_color( design.mountain ) );
break;
case '\n':
if (design.name.empty()) {
popup("<c=red>You need to name this world!<c=/>");
i_design.select("entry_name");
} else if (design.kingdoms.empty()) {
popup("<c=red>You need at least one kingdom!<c=/>");
i_design.select("list_kingdoms");
} else {
return true;
}
break;
case KEY_ESC:
return false;
default:
// First, check if we've selected list_kingdoms; if so, we handle a couple keys
if (i_design.selected()->name == "list_kingdoms" &&
(ch == 'a' || ch == 'A' || ch == 'd' || ch == 'D')) {
if (ch == 'a' || ch == 'A') {
// Add 1 since menu_vec() returns 0 for the first choice, and 0 == RACE_NULL
Race new_race = Race( 1 + menu_vec("Kingdom Race", race_names) );
design.kingdoms.push_back( new_race );
std::stringstream ss_race;
ss_race << "<c=" << color_tag( Race_data[new_race]->color ) <<
">" <<
capitalize_all_words(Race_data[new_race]->plural_name) <<
"<c=/>";
races.push_back( ss_race.str() );
// Colorize num_kingdoms_recommended
int num_kingdoms = world_size_kingdoms( design.size );
if (num_kingdoms > design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_ltgreen);
} else if (num_kingdoms == design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_yellow);
} else { // Too many kingdoms!
i_design.set_data("num_kingdoms_recommended", c_red);
}
} else if (ch == 'd' || ch == 'D') {
int index = i_design.get_int("list_kingdoms");
if (index >= 0 && index < design.kingdoms.size()) {
design.kingdoms.erase( design.kingdoms.begin() + index );
races.erase( races.begin() + index );
// Colorize num_kingdoms_recommended
int num_kingdoms = world_size_kingdoms( design.size );
if (num_kingdoms > design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_ltgreen);
} else if (num_kingdoms == design.kingdoms.size()) {
i_design.set_data("num_kingdoms_recommended", c_yellow);
} else { // Too many kingdoms!
i_design.set_data("num_kingdoms_recommended", c_red);
}
}
}
} else {
// This handles both TAB to move between name entry and the kingdom list, and
// typing to enter in a world name.
i_design.handle_keypress(ch);
}
break;
} // switch (ch)
} // while (true)
}
void Interface::main_loop()
{
sel = Point(4, 4);
city_radius = true;
// All the truly constant data.
i_main.set_data("text_menu_bar", menu_str);
std::stringstream ss_city_race;
Race_datum* race_dat = Race_data[pl_city->get_race()];
ss_city_race << "<c=white>" << pl_city->get_name() << " - <c=" <<
color_tag(race_dat->color) << ">" <<
capitalize(race_dat->plural_name) << "<c=/>";
i_main.set_data("text_city_race", ss_city_race.str());
set_mode(IMODE_VIEW_MAP);
set_data_mode(DATA_MODE_CITIZENS);
int date_size = i_main.element_width("text_date");
bool done = false;
while (!done) {
i_main.set_data("text_date", GAME->get_date_str(date_size));
pl_city->draw_map(i_main.find_by_name("draw_map"), sel, city_radius,
show_terrain);
if (cur_mode != IMODE_MENU) {
print_message_alert();
print_data();
}
i_main.set_data("num_population", pl_city->get_total_population());
i_main.set_data("num_gold", pl_city->get_resource_amount(RES_GOLD) );
i_main.set_data("num_food", pl_city->get_resource_amount(RES_FOOD) );
i_main.set_data("num_wood", pl_city->get_resource_amount(RES_WOOD) );
i_main.set_data("num_stone", pl_city->get_resource_amount(RES_STONE));
if (!temp_text) {
if (cur_mode == IMODE_VIEW_MAP && current_area != AREA_NULL) {
display_area_stats(current_area);
} else {
i_main.set_data("text_map_info", pl_city->get_map_info(sel));
}
}
i_main.draw(&w_main);
w_main.refresh();
long ch = input();
// Mark all but 10 most recent messages as read if we're on the message tab
if (cur_data_mode == DATA_MODE_MESSAGES &&
pl_city->unread_messages >= 10) {
pl_city->unread_messages = 10;
}
restore_info_text(); // Undo temporary change to text_map_info
if (ch == KEY_ESC) {
set_menu(MENU_NULL);
set_mode(IMODE_VIEW_MAP);
} else if (ch == '!') {
set_mode(IMODE_MENU);
set_menu(MENU_NULL);
} else {
handle_key(ch);
}
if (game_state == GAME_QUIT) {
done = true;
}
}
}
void Interface::handle_key(long ch)
{
//debugmsg("handle_key");
if (ch >= '1' && ch <= '9') { // Accessing or using a menu!
/* We start counting at 1 because this is tied to the interface. All menus and
* items in menus start counting at 1, so we do too.
*/
int menu_index = ch - '0';
if (cur_menu == MENU_NULL) { // We're not in a menu - so open one
set_menu( Menu_id( menu_index ) );
} else {
do_menu_action(cur_menu, menu_index);
set_menu( MENU_NULL );
set_mode( IMODE_VIEW_MAP ); // Get out of menu mode
}
} else { // Not a menu; thus, the action taken depends on our mode.
if (ch == '*') {
debugmsg("mode %d", cur_mode);
}
switch (cur_mode) {
case IMODE_MENU:
// Most menu stuff is handled above (keys '1' - '9'). But Q can close the menu.
if (ch == 'q' || ch == 'Q') {
set_menu(MENU_NULL);
set_mode(IMODE_VIEW_MAP);
}
break;
case IMODE_VIEW_MAP: {
Point p = input_direction(ch);
// Move the cursor
if (p.x != -2 && (p.x != 0 || p.y != 0)) {
sel += p;
if (sel.x < 0) {
sel.x = 0;
} else if (sel.x >= CITY_MAP_SIZE) {
sel.x = CITY_MAP_SIZE - 1;
}
if (sel.y < 0) {
sel.y = 0;
} else if (sel.y >= CITY_MAP_SIZE) {
sel.y = CITY_MAP_SIZE - 1;
}
// Get info on currently-selected tile
} else if (ch == '\n') {
if (current_area != AREA_NULL) {
if (pl_city->area_at(sel)) {
set_temp_info("There is already an area there!");
} else {
enqueue_area();
}
} else {
popup( pl_city->get_map_info(sel).c_str() );
}
// Revert to normal mode (not building an area, VIEW_MAP mode)
} else if (ch == 'q' || ch == 'Q') {
current_area = AREA_NULL;
set_mode(IMODE_VIEW_MAP);
i_main.clear_data("text_data");
// Toggle hiding areas (i.e. only show terrain)
} else if (ch == 't' || ch == 'T') {
show_terrain = !show_terrain;
// Toggled grayed-out tiles outside of our city's radius
} else if (ch == 'r' || ch == 'R') {
city_radius = !city_radius;
// Build a new area
} else if (ch == 'a' || ch == 'A') {
current_area = pick_area();
display_area_stats(current_area);
set_mode(IMODE_VIEW_MAP);
Building_datum* build = get_building_for(current_area);
if (current_area != AREA_NULL && build) {
i_main.set_data("text_data", build->get_short_description());
}
// Close an area
} else if (ch == 'c' || ch == 'C') {
Area* area_selected = pl_city->area_at(sel);
if (!area_selected) {
set_temp_info("No area there.");
} else if (!area_selected->is_open()) {
set_temp_info("That area is already closed.");
} else if (query_yn("Really close your %s?",
area_selected->get_name().c_str())) {
area_selected->close(pl_city);
}
// Open a closed area
} else if (ch == 'o' || ch == 'O') {
Area* area_selected = pl_city->area_at(sel);
if (!area_selected) {
set_temp_info("No area there.");
} else if (area_selected->is_open()) {
set_temp_info("That area is already open.");
} else {
int cost = area_selected->get_reopen_cost();
if (pl_city->get_resource_amount(RES_GOLD) < cost) {
std::stringstream ss_mes;
ss_mes << "You do not have enough gold to re-open your " <<
area_selected->get_name() << ". (Cost: " << cost <<
" You: " << pl_city->get_resource_amount(RES_GOLD);
set_temp_info(ss_mes.str());
} else if (query_yn("Open your %s at a cost of %d gold?",
area_selected->get_name().c_str(), cost)) {
pl_city->expend_resource(RES_GOLD, cost);
area_selected->auto_hire(pl_city);
area_selected->building.open = true;
}
}
// Destroy an area
} else if (ch == 'd' || ch == 'D') {
Area* area_selected = pl_city->area_at(sel);
if (area_selected) {
int cost = area_selected->get_destroy_cost();
int gold = pl_city->get_resource_amount(RES_GOLD);
// Areas under construction are free to "destroy" (but we won't get the build
// costs back).
if (area_selected->under_construction()) {
if (query_yn("Cancel %s construction? You will lose all \
resources spent to build it.", area_selected->get_name().c_str())) {
pl_city->destroy_area_at(sel);
}
} else if (gold < cost) {
popup("Destroying that %s costs <c=ltred>%d<c=/> gold. You have \
<c=ltred>%d<c=/> gold.", area_selected->get_name().c_str(), cost, gold);
} else if (query_yn("Destroy the %s?\nCost: %d gold\nYou: %d gold",
area_selected->get_name().c_str(), cost, gold)) {
pl_city->destroy_area_at(sel);
}
}
} else if (ch == '[') {
shift_data_mode(-1);
} else if (ch == ']') {
shift_data_mode(1);
// Move time forward by 1 day
} else if (ch == '.') {
GAME->advance_time(1, pl_city);
// Move time forward by 1 week
} else if (ch == '>') {
GAME->advance_time(7, pl_city);
}
if (cur_data_mode == DATA_MODE_MESSAGES) { // It has a few special keys
if (ch == '\'' && cur_data_mode == DATA_MODE_MESSAGES) {
pl_city->unread_messages = 0;
message_offset = 0;
} else if (ch == '+' || ch == '=') {
message_offset++;
} else if (ch == '-') {
message_offset--;
}
}
} break; // case IMODE_VIEW_MAP
default:
break;
// TODO: Fill this in with all keybindings for all modes.
}
}
}
void Interface::set_mode(Interface_mode mode)
{
cur_mode = mode;
i_main.clear_data("text_map_info"); // TODO: No?
switch (mode) {
case IMODE_NULL:
case IMODE_MENU:
i_main.clear_data("text_commands");
break;
case IMODE_VIEW_MAP: {
std::stringstream commands;
commands << "<c=pink>Movement keys<c=/>: Scroll" << std::endl;
if (current_area != AREA_NULL) {
commands << "<c=pink>Enter<c=/>: Place " <<
Area_data[current_area]->name << std::endl <<
"<c=pink>Q<c=/>: Cancel " <<
Area_data[current_area]->name << " placement" << std::endl;
} else {
commands << "<c=pink>Enter<c=/>: Get info on tile" << std::endl;
}
commands << "<c=pink>T<c=/>: Toggle terrain view" << std::endl;
commands << "<c=pink>R<c=/>: Toggle control radius" << std::endl;
commands << "<c=pink>A<c=/>: Build ";
if (current_area != AREA_NULL) {
commands << "a different ";
}
commands << "area" << std::endl;
commands << "<c=pink>C<c=/>: Close area" << std::endl;
commands << "<c=pink>O<c=/>: Re-open area" << std::endl;
commands << "<c=pink>D<c=/>: Destroy area" << std::endl;
i_main.set_data("text_commands", commands.str());
} break;
} // switch (mode)
}
void Interface::set_data_mode(Data_mode mode)
{
cur_data_mode = mode;
message_offset = 0; // Reset any scrolling we've done
// Set up text_data_help
Data_mode last = Data_mode( mode - 1 );
if (last == DATA_MODE_NULL) {
last = Data_mode( DATA_MODE_MAX - 1 );
}
std::stringstream ss_last;
ss_last << "<c=pink>[ <c=white>" << data_mode_name(last) << "<c=/>";
i_main.set_data("text_data_last", ss_last.str());
Data_mode next = Data_mode( mode + 1 );
if (next == DATA_MODE_MAX) {
next = Data_mode( DATA_MODE_NULL + 1 );
}
std::stringstream ss_next;
ss_next << "<c=white>" << data_mode_name(next) << " <c=pink>]<c=/>";
i_main.set_data("text_data_next", ss_next.str());
print_data();
}
void Interface::shift_data_mode(int offset)
{
int result = cur_data_mode + offset;
while (result <= DATA_MODE_NULL) {
result += (DATA_MODE_MAX - 1);
}
if (result >= DATA_MODE_MAX) {
result = result % (DATA_MODE_MAX - 1);
}
set_data_mode( Data_mode(result) );
}
void Interface::print_message_alert()
{
i_main.clear_data("text_messages");
std::vector<int> msg_count = pl_city->get_unread_message_count();
// Get a total message count
int total_messages = 0;
for (int i = 0; i < msg_count.size(); i++) {
total_messages += msg_count[i];
}
if (total_messages == 0) {
i_main.set_data("text_messages", "<c=dkgray>No new messages.<c=/>");
return;
}
std::stringstream ss_msgs;
ss_msgs << total_messages << " messages (";
// Print a number for each type, color-coded
for (int i = MESSAGE_MINOR; i < MESSAGE_MAX; i++) {
Message_type mtype = Message_type(i);
if (i > MESSAGE_MINOR) {
ss_msgs << " ";
}
ss_msgs << "<c=" << color_tag( message_type_color( mtype ) ) << ">" <<
msg_count[i] << "<c=/>";
}
ss_msgs << ")";
i_main.set_data("text_messages", ss_msgs.str());
}
void Interface::print_data()
{
std::stringstream ss_data;
if (cur_data_mode != DATA_MODE_NULL) {
i_main.clear_data("text_data_header");
i_main.clear_data("text_data");
}
switch (cur_data_mode) {
case DATA_MODE_NULL:
break; // Do nothing.
case DATA_MODE_CITIZENS:
i_main.set_data("text_data_header",
" <c=ltblue>Citizens<c=/>");
// First, population & unemployment data
// Header
ss_data << " Population Unemployed" << std::endl;
for (int i = CIT_PEASANT; i < CIT_MAX; i++) {
Citizen_type cit_type = Citizen_type(i);
// TODO: huh?
if (true || pl_city->population[i].count > 0) {
// We use "true" in citizen_type_name() to indicate pluralization
std::string cit_name = capitalize(citizen_type_name(cit_type, true));
ss_data << "<c=yellow>" << cit_name << "<c=/>";
// Add spaces for alignment
for (int n = 0; n < 14 - cit_name.length(); n++) {
ss_data << " ";
}
// Add spaces for alignment
int spaces = 6 - digits_in(pl_city->population[i].count);
for (int n = 0; n < spaces; n++) {
ss_data << " ";
}
ss_data << pl_city->population[i].count;
ss_data << " "; // Align under "Unemployed"
int unemployed = pl_city->population[i].get_unemployed();
spaces = 6 - digits_in(unemployed);
for (int n = 0; n < spaces; n++) {
ss_data << " ";
}
ss_data << unemployed << std::endl;
} // if (pl_city->population[i].count > 0)
} // for (int i = 0; i < CIT_MAX; i++)
ss_data << std::endl;
// Housing data
for (int i = CIT_PEASANT; i < CIT_MAX; i++) {
Citizen_type cit_type = Citizen_type(i);
std::string cit_name = capitalize( citizen_type_name(cit_type) );
ss_data << cit_name;
for (int n = 0; n < 9 - cit_name.length(); n++) {
ss_data << " ";
}
ss_data << "housing available:";
int housing = pl_city->get_total_housing(cit_type);
int spaces = 6 - digits_in(housing);
for (int n = 0; n < spaces; n++) {
ss_data << " ";
}
if (housing <= pl_city->get_total_population(cit_type)) {
ss_data << "<c=red>";
} else if (housing * 0.8 <= pl_city->get_total_population(cit_type)){
ss_data << "<c=yellow>";
}
ss_data << housing << "<c=/>" << std::endl;
}
ss_data << std::endl;
// Finally, population limits.
ss_data << "<c=yellow>Population limits:<c=/>" << std::endl;
for (int i = CIT_PEASANT; i < CIT_MAX; i++) {
Citizen_type cit_type = Citizen_type(i);
std::string cit_name = capitalize( citizen_type_name(cit_type) );
ss_data << cit_name << ":";
// Insert spacing for alignment.
for (int n = 0; n < 9 - cit_name.length(); n++) {
ss_data << " ";
}
int cap = pl_city->get_population_cap(cit_type);
if (cap == -1) { // No cap.
ss_data << " <c=blue>N/A<c=/>" << std::endl;
} else {
// Insert spacing for alignment.
for (int n = 0; n < 4 - digits_in(cap); n++) {
ss_data << " ";
}
ss_data << cap << std::endl;
}
}
break;
case DATA_MODE_RESOURCES: {
i_main.set_data("text_data_header",
" <c=ltblue>Resources<c=/>");
// We do two columns; resources on the left, minerals on the right.
// We do them in the same loop and check to see if they're valid for both.
// First loop figures out what we've got.
std::vector<Resource> resource_list;
std::vector<Mineral> mineral_list;
for (int i = 0; i < RES_MAX || i < MINERAL_MAX; i++) {
if (i < RES_MAX) {
Resource res = Resource(i);
if (!Resource_data[res]->meta &&
pl_city->get_resource_amount(res) > 0) {
resource_list.push_back(res);
}
}
if (i < MINERAL_MAX) {
Mineral min = Mineral(i);
if (pl_city->get_mineral_amount(min) > 0) {
mineral_list.push_back(min);
}
}
}
// Now actually print.
for (int i = 0; i < resource_list.size() || i < mineral_list.size(); i++){
if (i < resource_list.size()) {
Resource res = resource_list[i];
std::string res_name = capitalize( Resource_data[res]->name );
int amount = pl_city->get_resource_amount(res);
// Colorize it
ss_data << "<c=" << color_tag( Resource_data[res]->color ) << ">";
ss_data << res_name << "<c=/>:";
// Insert spaces for alignment. length() + 1 because of the :
if (res_name.length() < 14) {
for (int n = 0; n < 15 - (res_name.length() + 1); n++) {
ss_data << " ";
}
}
// More spaces for number alignment.
for (int n = 0; n < 5 - digits_in(amount); n++) {
ss_data << " ";
}
ss_data << amount;
} else if (i < mineral_list.size()) {
// If we DON'T have a resource to print in this line, but we DO have a mineral
// to print, we need to put in black spaces so the mineral starts at the right.
for (int n = 0; n < 20; n++) { // 15 for the name, 5 for the amount
ss_data << " ";
}
}
// Now, print mineral if we are still in range.
if (i < mineral_list.size()) {
// Spacing to seperate us from resources (even if there wasn't a resource)
ss_data << " <c=white>|<c=/> ";
Mineral min = mineral_list[i];
std::string min_name = capitalize( Mineral_data[min]->name );
int amount = pl_city->get_mineral_amount(min);
ss_data << min_name << ":";
// Insert spaces for alignment. length() + 1 because of the :
for (int n = 0; n < 10 - (min_name.length() + 1); n++) {
ss_data << " ";
}
// More spaces for number alignment.
for (int n = 0; n < 5 - digits_in(amount); n++) {
ss_data << " ";
}
ss_data << amount;
} // if (i < mineral_list.size())
ss_data << std::endl;
} // for (int i = 0; i < res_list.size() || i < min_list.size(); i++)
} break;
case DATA_MODE_MESSAGES: {
i_main.set_data("text_data_header",
"<c=ltblue>Messages<c=/> (<c=pink>'<c=/>: Clear \
<c=pink>-<c=/>/<c=pink>+<c=/>: Scroll)");
// If there's no new messages, let us know that.
if (pl_city->unread_messages == 0 || pl_city->messages.empty()) {
ss_data << "<c=dkgray>No new messages.<c=/>";
}
// We show the last N messages, where N = pl_city->unread_messages.
// Ensure that unread_messages is safe.
if (pl_city->unread_messages > pl_city->messages.size()) {
pl_city->unread_messages = pl_city->messages.size();
}
// Ensure that message_offset is safe.
int mess_size = pl_city->messages.size();
if (mess_size - pl_city->unread_messages + message_offset < 0) {
message_offset = pl_city->unread_messages - mess_size;
}
if (pl_city->unread_messages + message_offset > mess_size) {
message_offset = mess_size - pl_city->unread_messages;
}
Date last_date(1, 1, 1); // Make sure the date check fails
for (int i = pl_city->messages.size() - pl_city->unread_messages +
message_offset;
i < pl_city->messages.size();
i++) {
Message* mes = &(pl_city->messages[i]);
if (mes->date != last_date) { // Print the date
last_date = mes->date;
ss_data << "<c=ltblue>" << last_date.get_text() << ":<c=/>" <<
std::endl;
}
// Print color-coding.
ss_data << "<c=" << color_tag( message_type_color( mes->type ) ) << ">";
ss_data << mes->text << "<c=/>" << std::endl;
} // for (i from first message to last message)
} break;
} // switch (cur_data_mode)
i_main.set_data("text_data", ss_data.str());
i_main.set_data("text_data", 999999); // Scroll to the bottom
}
void Interface::set_menu(Menu_id item)
{
std::string menu_name;
int posx = -1;