-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
3101 lines (2901 loc) · 83.9 KB
/
window.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 <sys/ioctl.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <string.h>
#include <stdio.h>
#include <ncurses.h>
#include <ctype.h> //for isdigit
#include "world.h"
char *modes[] = {
[MOVE] = "MOVE",
[VIEW] = "VIEW",
};
int current_mode = VIEW;
char *screens[] = {
[START_MENU] = "MENU",
[MAIN_SCREEN] = "GAME: MAIN SCREEN",
[GAME_TIME_DIALOG] = "SET GAME TIME",
[NEW_GAME] = "CREATE NEW GAME",
[MAP_EDITOR] = "MAP EDITOR",
[REGIONS_DIALOG] = "REGIONS",
[ADD_REGION_DIALOG] = "ADD REGION",
[EDIT_REGION_DIALOG] = "EDIT REGION",
[GIVE_REGION_DIALOG] = "GIVE REGION",
[RENAME_REGION_DIALOG] = "RENAME REGION",
[CHARACTERS_DIALOG] = "HEROES",
[ADD_CHARACTER_DIALOG] = "ADD HERO",
[EDIT_CHARACTER_DIALOG] = "EDIT HERO",
[RENAME_CHARACTER_DIALOG] = "RENAME HERO",
[CHARACTER_MONEY_DIALOG] = "CHARACTER MONEY",
[CHARACTER_DATES_DIALOG] = "HERO DATES",
[REGION_CHARACTER_DIALOG] = "REGION TO HERO",
[GIVE_MONEY_DIALOG] = "GIVE MONEY",
[HEIR_DIALOG] = "NAME A HEIR",
[FEUDAL_DIALOG] = "FEUDAL RELATIONS",
[HOMAGE_DIALOG] = "PAY HOMAGE",
[PROMOTE_SOLDIER_DIALOG] = "PROMOTE SOLDIER",
[DIPLOMACY_DIALOG] = "DIPLOMACY",
[VALIDATE_DIALOG] = "VALIDATE GAME DATA",
[HELP_DIALOG] = "HELP",
[INFORMATION] = "INFORMATION",
[SELF_DECLARATION_DIALOG] = "BECOME A KING",
[GAME_OVER] = "GAME OVER",
[CHOOSE_CHARACTER_DIALOG] = "CHOOSE A HERO"
};
region_t *selected_region = NULL;
char const piece_char[] = {
[NOBLE] = 'n', /* ? */
[SOLDIER] = 's',
[SHIP] = 'S'
};
char const noble_char[] = {
[KNIGHT] = 'k',
[BARON] = 'b',
[COUNT] = 'c',
[DUKE] = 'd',
[KING] = 'K'
};
tile_t *cursor = NULL;
int check_termsize()
{
int retval = 0;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if (w.ws_row < 24 || w.ws_col < 80)
retval = 1;
return retval;
}
int get_input(WINDOW * window)
{
int ch = 0, ch2 = 0, input = 0;
ch = wgetch(window);
if (ch == 27) {
ch2 = wgetch(window);
if (ch2 == '[') {
input = wgetch(window);
if (input == 65 || input == 66 || input == 67
|| input == 68)
return input + 1000;
}
else return tolower(ch2);
}
return tolower(ch);
}
int start_menu(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
int sf_exists = savefile_exists();
int i;
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
wprintw(local_win, " [N]ew game\n");
if (world->grid || sf_exists) wprintw(local_win, " [L]oad game\n");
wprintw(local_win, " [Q]uit\n");
int ch;
while (1) {
ch = tolower(wgetch(local_win));
switch (ch) {
case 'l':
if (world->grid || sf_exists) {
return MAIN_SCREEN;
}
break;
case 'n':
return NEW_GAME;
break;
case 'q':
return SHUTDOWN;
break;
default:
break;
}
}
}
void draw_map(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
character_t *character = world->selected_character;
if (!cursor) cursor = get_noble_by_owner(character)->tile;
piece_t *piece = cursor->piece;
int i, j;
/**
uint16_t h_multiplier = world->grid->cursor_height / 24;
uint16_t w_multiplier = world->grid->cursor_width / 48;
**/
int16_t h_offset = cursor->height - 12;
int16_t w_offset = cursor->width - 24;
char tile_char = '.';
int color_nr = 0;
int character_age_mon = 0;
tile_t *current_tile = NULL;
for (i = 24 + h_offset; i > h_offset; i--) {
for (j = w_offset; j < 48 + w_offset; j++) {
/**
for (i = 24 * (h_multiplier + 1) - 1; i >= 24 * h_multiplier; i--) {
if (i >= world->grid->height) {
wprintw(local_win, "\n");
continue;
}
for (j = 48 * w_multiplier; j < (48 * (w_multiplier + 1) < world->grid->width ? 48 * (w_multiplier + 1) : world->grid->width); j++) {
**/
/**
for (i = 0; i < world->grid->height; i++) {
for (j = 0; j < world->grid->width; j++) {
**/
if (i < 0 || i >= world->grid->height || j < 0
|| j >= world->grid->width) {
color_nr = 1;
tile_char = ' ';
} else {
current_tile = world->grid->tiles[i][j];
if (current_tile->piece != NULL) {
color_nr = current_tile->piece->owner->id %
6 + 10;
switch (current_tile->piece->type) {
case NOBLE: /* noble */
tile_char = noble_char[current_tile->piece->owner->rank];
break;
case SOLDIER: /* soldier */
case SHIP: /* ship */
tile_char = piece_char[current_tile->piece->type];
break;
}
}
else if (current_tile->walkable) {
if (current_tile->region != NULL
&& current_tile->region->owner != NULL)
color_nr = current_tile->region->owner->id % 6 + 10;
else
color_nr = 1;
tile_char = '.';
}
else {
color_nr = 1; //blue
tile_char = '~';
}
}
if (cursor->height == i
&& cursor->width == j) {
if (color_nr == 1)
color_nr = 26;
else
color_nr += 10;
}
wcolor_set(local_win, color_nr, NULL);
wprintw(local_win, "%c", tile_char);
}
wprintw(local_win, "\n");
}
wcolor_set(local_win, 1, NULL);
/* global info */
mvwprintw(local_win, 0, 50, "Date: %s of year %d",
months[world->current_time.tm_mon],
world->current_time.tm_year);
mvwprintw(local_win, 1, 50, "Nr. players: %d", count_characters());
mvwprintw(local_win, 2, 50, "Map size: %dx%d", world->grid->height,
world->grid->width);
/* character info */
mvwprintw(local_win, 4, 50, "Hero: %s (%s)", character->name,
rank_name[character->rank]);
character_age_mon =
(world->current_time.tm_year - character->birthdate.tm_year) * 12 +
(world->current_time.tm_mon - character->birthdate.tm_mon);
mvwprintw(local_win, 5, 50, "Age: %d year(s) %d month(s)",
character_age_mon / 12, character_age_mon % 12);
mvwprintw(local_win, 6, 50, "Money: %d", character->money);
mvwprintw(local_win, 7, 50, "Army: %d",
count_pieces_by_owner(character));
mvwprintw(local_win, 8, 50, "Land: %d",
count_tiles_by_owner(character));
mvwprintw(local_win, 9, 50, "Heir: %s",
(character->heir != NULL ? character->heir->name : "none"));
mvwprintw(local_win, 10, 50, "Lord: %s",
(character->lord != NULL ? character->lord->name : "none"));
mvwprintw(local_win, 11, 50, "Moves left: %d", world->moves_left);
/* place info */
mvwprintw(local_win, 12, 50, "Tile: %d, %d", cursor->height,
cursor->width);
mvwprintw(local_win, 13, 50, "Terrain: %s %s",
(cursor->walkable ? "walkable" : "unwalkable"), "land");
mvwprintw(local_win, 14, 50, "Region: %s (%d)",
(cursor->region == NULL ? "none" : cursor->region->name),
(cursor->region == NULL ? 0 : cursor->region->id));
mvwprintw(local_win, 15, 50, "Owned by: %s",
(cursor->region == NULL
|| cursor->region->owner ==
NULL ? "none" : cursor->region->owner->name));
/* piece info */
mvwprintw(local_win, 17, 50, "Unit: %s",
(piece == NULL ? " " : piece_name[piece->type]));
mvwprintw(local_win, 18, 50, "Owned by: %s",
(piece == NULL ? " " : piece->owner->name));
mvwprintw(local_win, 19, 50, "Diplomacy: ");
if (piece != NULL && piece->owner->id != character->id) {
dipstatus_t *diplomacy = get_dipstatus(piece->owner, character);
unsigned char status = get_diplomacy(piece->owner, character);
switch (status) {
case NEUTRAL:
wcolor_set(local_win, 12, NULL);
break;
case ALLIANCE:
wcolor_set(local_win, 11, NULL);
break;
case WAR:
wcolor_set(local_win, 10, NULL);
break;
}
wprintw(local_win, "%s", dipstatus_name[status]);
if (diplomacy && diplomacy->pending_offer)
wprintw(local_win, " *");
wcolor_set(local_win, 1, NULL);
}
int message_len = strlen(world->message);
if (message_len > 0) {
wcolor_set(local_win, 12, NULL);
for (i = 0; i < (message_len - 1) / 29 + 1; i++) {
mvwprintw(local_win, 20 + i, 50, "%.*s", 29,
&world->message[29 * i]);
}
wcolor_set(local_win, 1, NULL);
}
mvwprintw(local_win, 23, 50, "Mode: %s\n", modes[current_mode]);
mvwprintw(local_win, 23, 65, "Help: '?'");
world->message[0] = '\0';
}
int regions_dialog(WINDOW *local_win)
{
int retval = REGIONS_DIALOG;
wclear(local_win);
wattrset(local_win, A_BOLD);
curs_set(FALSE);
noecho();
int i;
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
region_t *current_region = world->regionlist;
character_t *active_character = world->selected_character;
int nr_regions = count_regions_by_owner(active_character);
if (nr_regions > 1)
wprintw(local_win,
" To give a region to another player, press 'g'.\n");
else
wprintw(local_win,
" [You can't give away your last region.]\n");
wprintw(local_win, " To rename a region, press 'e'.\n");
wprintw(local_win, " To scroll, press up/down keys.\n");
wprintw(local_win, " To return to map, press 'q'.\n\n");
/**
* if selected region is not owned by selected character,
* set to first region owned by active character
**/
if (selected_region == NULL
|| selected_region->owner == NULL
|| selected_region->owner !=
world->selected_character) {
current_region = world->regionlist;
while (current_region != NULL) {
if (current_region->owner != NULL
&& current_region->owner ==
world->selected_character) {
selected_region = current_region;
break;
}
current_region = current_region->next;
}
}
uint16_t counter = 0;
uint16_t section = 0;
current_region = world->regionlist;
int regionlist_selector = 0;
while (current_region != NULL) {
if (current_region->owner != NULL
&& current_region->owner == world->selected_character) {
if (current_region == selected_region)
break;
else
regionlist_selector++;
}
current_region = current_region->next;
}
current_region = world->regionlist;
section = regionlist_selector / 10;
while (current_region != NULL) {
if (current_region->owner == NULL
|| current_region->owner != world->selected_character) {
current_region = current_region->next;
;
continue;
}
if (counter >= section * 10 && counter < section * 10 + 10
&& counter < nr_regions) {
if (counter == regionlist_selector)
wattron(local_win, COLOR_PAIR(26));
else
wattron(local_win, COLOR_PAIR(1));
mvwprintw(local_win, 8 + counter % 10, 2,
"%3d. %s (%i tiles)", current_region->id,
current_region->name, current_region->size);
wattron(local_win, COLOR_PAIR(1));
}
counter++;
current_region = current_region->next;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (regionlist_selector > 0) {
regionlist_selector--;
do {
selected_region = selected_region->prev;
} while (selected_region && selected_region->owner != world->selected_character);
}
break;
case 1066:
if (regionlist_selector < nr_regions - 1) {
regionlist_selector++;
do {
selected_region = selected_region->next;
} while (selected_region && selected_region->owner != world->selected_character);
}
break;
case 'e':
retval = EDIT_REGION_DIALOG;
break;
case 'g': /* give to another character */
retval = GIVE_REGION_DIALOG;
break;
case 'q': /* return to map */
retval = MAIN_SCREEN;
break;
default:
break;
}
return retval;
}
int rename_region_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
curs_set(TRUE);
echo();
int i;
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s", screens[current_screen]);
int stage = 0;
int error = 0;
region_t *region = selected_region;
char name[17] = { 0 };
while (1) {
switch (stage) {
case 0:
if (error == 1)
mvwprintw(local_win, 2, 2,
"New name is not unique. Type another one:\n\n ");
else
mvwprintw(local_win, 2, 2,
"Type a new name for the region '%s', or press Enter to return:\n\n ",
region->name);
wgetnstr(local_win, name, 16);
if (strlen(name) == 0) {
return REGIONS_DIALOG;
break;
}
if (get_region_by_name(name) == NULL) {
stage = 1;
error = 0;
} else
error = 1;
break;
case 1:
change_region_name(name, region);
sort_region_list();
return REGIONS_DIALOG;
break;
}
}
}
int give_region_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
curs_set(FALSE);
noecho();
int i;
character_t *active_character = world->selected_character;
character_t *selected_character = world->selected_character;
int characterlist_selector;
region_t *region = selected_region;
int give_region_ok = 0;
while (1) {
characterlist_selector = get_character_order(selected_character);
give_region_ok = 0;
wclear(local_win);
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s", screens[current_screen]);
if (selected_character == active_character) {
mvwprintw(local_win, 2, 2,
"[You can not give region to yourself]");
give_region_ok = 0;
} else {
mvwprintw(local_win, 2, 2,
"To give %s to selected player, press Enter.",
region->name);
give_region_ok = 1;
}
mvwprintw(local_win, 3, 2, "To scroll, press up/down keys.");
mvwprintw(local_win, 4, 2, "To return, press 'q'.");
uint16_t nr_characters = count_characters();
uint16_t counter = 0;
uint16_t section = 0;
character_t *current = world->characterlist;
while (current != NULL) {
section = characterlist_selector / 10;
if (counter >= section * 10
&& counter < section * 10 + 10
&& counter < nr_characters) {
if (counter == characterlist_selector) {
wattron(local_win, COLOR_PAIR(26));
} else
wattron(local_win, COLOR_PAIR(1));
mvwprintw(local_win, 8 + counter % 10, 2,
"%3d. %s", current->id,
current->name);
if (current == active_character)
wprintw(local_win, " (you)");
wattron(local_win, COLOR_PAIR(1));
}
current = current->next;
counter++;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (selected_character->prev)
selected_character = selected_character->prev;
break;
case 1066:
if (selected_character->next)
selected_character = selected_character->next;
break;
case 10:
if (give_region_ok) {
change_region_owner(selected_character, region);
add_to_chronicle("%s %s granted %s to %s %s.\n",
rank_name[active_character->rank],
active_character->name,
region->name,
rank_name[selected_character->rank],
selected_character->name);
return REGIONS_DIALOG;
break;
}
break;
case 'q':
return REGIONS_DIALOG;
break;
}
}
}
int give_money_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
int i;
int stage = 0;
int error = 0;
character_t *active_character = world->selected_character;
character_t *receiving_character = NULL;
int characterlist_selector;
uint16_t money = 0;
char money_ch[MONEY_MAX_DIGITS + 1] = { 0 };
while (1) {
switch (stage) {
case 0:
curs_set(TRUE);
echo();
wclear(local_win);
for (i = 0;
i < (80 - strlen(screens[current_screen])) / 2;
i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
if (error == 1)
wprintw(local_win,
" Error. Try another number (1-%d):\n\n ",
active_character->money);
else
wprintw(local_win,
" Type the amount to be given (1-%d), or press Enter to dismiss:\n\n ",
active_character->money);
wgetnstr(local_win, money_ch, MONEY_MAX_DIGITS);
if (strlen(money_ch) == 0) {
return MAIN_SCREEN;
break;
}
for (i = 0; i < strlen(money_ch); i++) {
if (!isdigit(money_ch[i])) {
error = 1;
break;
}
}
money = atoi(money_ch);
if (money < 1 || money > active_character->money) {
error = 1;
break;
}
stage = 1;
error = 0;
break;
case 1:
receiving_character = world->selected_character;
while (stage == 1) {
curs_set(FALSE);
noecho();
wclear(local_win);
for (i = 0;
i <
(80 - strlen(screens[current_screen])) / 2;
i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n",
screens[current_screen]);
wprintw(local_win,
" To pick a player, press Enter.\n");
wprintw(local_win,
" To scroll, press up/down keys.\n");
wprintw(local_win,
" To return, press 'esc'.\n\n");
uint16_t nr_characters = count_characters();
uint16_t counter = 0;
uint16_t section = 0;
character_t *current = world->characterlist;
characterlist_selector =
get_character_order(receiving_character);
while (current != NULL) {
section = characterlist_selector / 10;
if (counter >= section * 10
&& counter < section * 10 + 10
&& counter < nr_characters) {
if (counter ==
characterlist_selector) {
wattron(local_win,
COLOR_PAIR(26));
} else
wattron(local_win,
COLOR_PAIR(1));
mvwprintw(local_win,
8 + counter % 10, 2,
"%3d. %s",
current->id,
current->name);
if (current == world->selected_character)
wprintw(local_win,
" (you)\n");
else
wprintw(local_win,
"\n");
wattron(local_win,
COLOR_PAIR(1));
}
current = current->next;
counter++;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (receiving_character->prev) receiving_character = receiving_character->prev;
break;
case 1066:
if (receiving_character->next) receiving_character = receiving_character->next;
break;
case 10:
if (receiving_character != active_character) {
error = 0;
stage = 2;
}
break;
case 27:
return MAIN_SCREEN;
break;
}
}
break;
case 2:
error = transfer_money(active_character, receiving_character, money);
if (error) strcpy(world->message, "Failed to transfer money");
return MAIN_SCREEN;
break;
}
}
}
int info_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
curs_set(FALSE);
noecho();
uint16_t i, j;
uint16_t section = 0;
uint16_t nr_characters = count_characters();
struct rankings {
character_t *character;
uint16_t nr_tiles;
uint16_t land_ranking;
uint16_t nr_pieces;
uint16_t army_ranking;
uint16_t money_ranking;
};
struct rankings **characters = malloc(sizeof(struct rankings *) * nr_characters);
if (!characters) exit(EXIT_FAILURE);
for (i = 0; i < nr_characters; i++) {
characters[i] = malloc(sizeof(struct rankings));
if (!characters[i]) exit(EXIT_FAILURE);
}
character_t *current = world->characterlist;
i = 0;
while (i < nr_characters && current != NULL) {
characters[i]->character = current;
characters[i]->nr_tiles = count_tiles_by_owner(characters[i]->character);
characters[i]->land_ranking = 1;
characters[i]->nr_pieces = count_pieces_by_owner(characters[i]->character);
characters[i]->army_ranking = 1;
characters[i]->money_ranking = 1;
current = current->next;
i++;
}
for (i = 0; i < nr_characters; i++) {
for (j = 0; j < i; j++) {
if (characters[i]->nr_tiles < characters[j]->nr_tiles) characters[i]->land_ranking++;
else if (characters[i]->nr_tiles > characters[j]->nr_tiles) characters[j]->land_ranking++;
if (characters[i]->nr_pieces < characters[j]->nr_pieces) characters[i]->army_ranking++;
else if (characters[i]->nr_pieces > characters[j]->nr_pieces) characters[j]->army_ranking++;
if (characters[i]->character->money < characters[j]->character->money) characters[i]->money_ranking++;
else if (characters[i]->character->money > characters[j]->character->money) characters[j]->money_ranking++;
}
}
while (1) {
wclear(local_win);
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
wprintw(local_win, " To scroll, press up/down keys.\n");
wprintw(local_win, " To return, press 'q'.\n\n");
mvwprintw(local_win, 6, 2, " Nr.");
mvwprintw(local_win, 6, 7, "Hero's Name");
mvwprintw(local_win, 6, 44, "Money");
mvwprintw(local_win, 6, 54, "Army");
mvwprintw(local_win, 6, 64, "Land");
i = 0;
while (i < nr_characters) {
if (i >= section * 10
&& i < section * 10 + 10) {
mvwprintw(local_win, 8 + i % 10, 2,
"%3d. %s", characters[i]->character->id,
characters[i]->character->name);
mvwprintw(local_win, 8 + i % 10, 42,
"%3d (#%d)", characters[i]->character->money,
characters[i]->money_ranking);
mvwprintw(local_win, 8 + i % 10, 52,
"%3d (#%d)",
characters[i]->nr_pieces,
characters[i]->army_ranking);
mvwprintw(local_win, 8 + i % 10, 62,
"%3d (#%d)",
characters[i]->nr_tiles,
characters[i]->land_ranking);
}
i++;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (section > 0)
section--;
break;
case 1066:
if (section < (nr_characters - 1) / 10)
section++;
break;
case 'q':
for (i = 0; i < nr_characters; i++) if (characters[i]) free(characters[i]);
free(characters);
return MAIN_SCREEN;
break;
}
}
}
int successor_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
int i;
character_t *current;
character_t *active_character = world->selected_character;
character_t *heir = active_character->heir;
character_t *new_heir = world->selected_character;
int characterlist_selector;
while (1) {
curs_set(FALSE);
noecho();
wclear(local_win);
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
mvwprintw(local_win, 2, 2, "To pick a player, press Enter.\n");
mvwprintw(local_win, 3, 2, "To scroll, press up/down keys.\n");
mvwprintw(local_win, 4, 2, "To remove heir, press 'd'.\n");
mvwprintw(local_win, 5, 2, "To return, press 'q'.\n\n");
uint16_t nr_characters = count_characters();
uint16_t counter = 0;
uint16_t section = 0;
current = world->characterlist;
characterlist_selector = get_character_order(new_heir);
while (current != NULL) {
section = characterlist_selector / 10;
if (counter >= section * 10
&& counter < section * 10 + 10
&& counter < nr_characters) {
if (counter == characterlist_selector) {
wattron(local_win, COLOR_PAIR(26));
} else
wattron(local_win, COLOR_PAIR(1));
mvwprintw(local_win, 8 + counter % 10, 2,
"%3d. %s", current->id,
current->name);
if (current == world->selected_character)
wprintw(local_win, " (you)\n");
else if (heir != NULL
&& current->id == heir->id)
wprintw(local_win, " (current heir)\n");
else
wprintw(local_win, "\n");
wattron(local_win, COLOR_PAIR(1));
}
current = current->next;
counter++;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (new_heir->prev) new_heir = new_heir->prev;
break;
case 1066:
if (new_heir->next) new_heir = new_heir->next;
break;
case 10:
if (new_heir != active_character) {
set_successor(active_character, new_heir);
heir = new_heir;
}
break;
case 'd':
heir = NULL;
set_successor(active_character, heir);
break;
case 'q':
return MAIN_SCREEN;
break;
}
}
}
int feudal_dialog(WINDOW *local_win)
{
wclear(local_win);
wattrset(local_win, A_BOLD);
int i;
character_t *active_character = world->selected_character;
character_t *lord = active_character->lord;
piece_t *current_piece = cursor->piece;
character_t *selected_character = NULL;
int characterlist_selector = 0;
int create_vassal_ok = 0;
int promote_vassal_ok = 0;
while (1) {
create_vassal_ok = 0;
promote_vassal_ok = 0;
curs_set(FALSE);
noecho();
wclear(local_win);
for (i = 0; i < (80 - strlen(screens[current_screen])) / 2; i++)
wprintw(local_win, " ");
wprintw(local_win, "%s\n\n", screens[current_screen]);
int nr_vassals = count_vassals(active_character);
if (lord != NULL)
mvwprintw(local_win, 2, 2, "Your lord is %s.",
lord->name);
else
mvwprintw(local_win, 2, 2, "To pay homage, press 'h'.");
mvwprintw(local_win, 3, 2, "To scroll, press up/down keys.");
if (nr_vassals == 0)
mvwprintw(local_win, 4, 2,
"[You have no vassals to free]");
else
mvwprintw(local_win, 4, 2,
"To free a vassal, press 'f'.");
if (nr_vassals == 0)
mvwprintw(local_win, 5, 2,
"[You have no vassals to promote]");
else if (get_money(active_character) < COST_SOLDIER)
mvwprintw(local_win, 5, 2,
"[Can't promote a vassal. Not enough money.]");
else if (selected_character != NULL
&& active_character->rank - selected_character->rank <= 1)
mvwprintw(local_win, 5, 2,
"[Can't promote a vassal. He is one rank below you.]");
else {
mvwprintw(local_win, 5, 2,
"To promote a vassal, press 'p'.");
promote_vassal_ok = 1;
}
if (active_character->rank <= KNIGHT)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. Your rank is too low.]");
else if (count_regions_by_owner(active_character) < 2)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. Too few regions.]");
else if (get_money(active_character) < COST_SOLDIER)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. Not enough money.]");
else if (current_piece == NULL)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. No piece selected.]");
else if (current_piece->owner->id != active_character->id)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. Selected piece not yours.]");
else if (current_piece->type != 1)
mvwprintw(local_win, 6, 2,
"[Can't create vassal. Selected piece not soldier.]");
else {
mvwprintw(local_win, 6, 2,
"To create a new vassal, press 'v'");
create_vassal_ok = 1;
}
mvwprintw(local_win, 7, 2, "To return, press 'q'.");
uint16_t counter = 0;
uint16_t section = 0;
character_t *current = world->characterlist;
while (current != NULL) {
section = characterlist_selector / 10;
if (counter >= section * 10
&& counter < section * 10 + 10
&& counter < nr_vassals) {
if (counter == characterlist_selector) {
wattron(local_win, COLOR_PAIR(26));
selected_character = current;
} else
wattron(local_win, COLOR_PAIR(1));
if (current->lord != NULL
&& current->lord->id == active_character->id) {
mvwprintw(local_win, 9 + counter % 10,
2, "%3d. %s (%s)",
current->id, current->name,
rank_name[current->rank]);
counter++;
}
wattron(local_win, COLOR_PAIR(1));
}
current = current->next;
}
int user_move = get_input(local_win);
switch (user_move) {
case 1065:
if (characterlist_selector > 0)
characterlist_selector--;
break;
case 1066:
if (characterlist_selector < nr_vassals - 1)
characterlist_selector++;
break;
case 'h':
if (lord == NULL)
return HOMAGE_DIALOG;
break;
case 'f':
if (selected_character != NULL) {
unhomage(selected_character);
add_to_chronicle
("%s %s became a sovereign of his lands.\n",
rank_name[selected_character->rank],
selected_character->name);
characterlist_selector = 0;