-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparavia.c
More file actions
1641 lines (1420 loc) · 49.5 KB
/
paravia.c
File metadata and controls
1641 lines (1420 loc) · 49.5 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
/******************************************************************************
** **
** Santa Paravia & Fiumaccio. Translated from the original TRS-80 BASIC **
** source code into C by Thomas Knox <tknox@mac.com>. **
** **
** Original program (C) 1979 by George Blank **
** <gwblank@postoffice.worldnet.att.net> **
** **
** Curses TUI, bug fixes, and modernisation by Tom Knox, 2000/2026. **
** **
******************************************************************************/
/*
Copyright (C) 2000 Thomas Knox
Portions Copyright (C) 1979 by George Blank, used with permission.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
*/
#include <ncurses.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* ---------------------------------------------------------------------------
* Layout constants — all positions derived from LINES/COLS at runtime.
* We divide the screen into:
* Left panel: map (MAP_COLS wide)
* Right panel: stats (COLS - MAP_COLS wide)
* Bottom strip: message log (MSG_LINES tall)
* --------------------------------------------------------------------------*/
#define MIN_COLS 80
#define MIN_LINES 24
#define MSG_LINES 8 /* lines reserved for the message area */
#define MAP_COLS_FRAC 0.55 /* map takes ~55% of screen width */
#define LOG_ROWS \
(MSG_LINES - 4) /* visible log lines (excl borders+divider+input) */
#define LOG_MAX 64 /* ring buffer capacity */
/* Color pair IDs */
#define CP_NORMAL 1
#define CP_WALL 2
#define CP_TOWER 3
#define CP_FIELDS 4
#define CP_BUILDINGS 5
#define CP_STATS_HDR 6
#define CP_STATS_VAL 7
#define CP_MSG 8
#define CP_TITLE 9
#define CP_WARNING 10
/* ---------------------------------------------------------------------------
* Player struct
* --------------------------------------------------------------------------*/
struct Player {
int Cathedral, Clergy, CustomsDuty, CustomsDutyRevenue, DeadSerfs;
int Difficulty, FleeingSerfs, GrainDemand, GrainPrice, GrainReserve;
int Harvest, IncomeTax, IncomeTaxRevenue, RatsAte;
int Justice, JusticeRevenue, Land, Marketplaces, MarketRevenue;
int Merchants, MillRevenue, Mills, NewSerfs, Nobles, OldTitle, Palace;
int Rats, SalesTax, SalesTaxRevenue, Serfs, SoldierPay, Soldiers, TitleNum;
int TransplantedSerfs, Treasury, WhichPlayer, Year, YearOfDeath;
char City[15], Name[25], Title[15];
float PublicWorks, LandPrice;
bool InvadeMe, IsBankrupt, IsDead, IWon, MaleOrFemale, NewTitle;
};
typedef struct Player player;
/* ---------------------------------------------------------------------------
* Global curses windows
* --------------------------------------------------------------------------*/
static WINDOW *w_map = NULL; /* left panel: the city map */
static WINDOW *w_stats = NULL; /* right panel: player statistics */
static WINDOW *w_msg = NULL; /* bottom strip: message log */
static bool g_color = false; /* whether color is available */
/* Cached terminal dimensions (set in InitWindows, refreshed on resize) */
static int g_lines = 0;
static int g_cols = 0;
static int g_map_cols = 0;
static int g_map_lines = 0;
static int g_stat_cols = 0;
/* ---------------------------------------------------------------------------
* City / title tables
* --------------------------------------------------------------------------*/
static const char CityList[7][15] = {"Santa Paravia", "Fiumaccio", "Torricella",
"Molinetto", "Fontanile", "Romanga",
"Monterana"};
static const char MaleTitles[8][15] = {"Sir", "Baron", "Count",
"Marquis", "Duke", "Grand Duke",
"Prince", "* H.R.H. King"};
static const char FemaleTitles[8][15] = {
"Lady", "Baroness", "Countess", "Marquise",
"Duchess", "Grand Duchess", "Princess", "* H.R.H. Queen"};
/* ---------------------------------------------------------------------------
* Prototypes
* --------------------------------------------------------------------------*/
/* TUI management */
void InitWindows(void);
void TeardownWindows(void);
void ResizeWindows(void);
void MsgPrint(const char *fmt, ...);
void MsgClear(void);
int MsgGetInt(const char *prompt, int lo, int hi);
void MsgGetStr(const char *prompt, char *buf, int maxlen);
int MsgGetGrain(int minimum, int maximum);
void MsgWaitEnter(void);
void DrawMap(player *Me);
void DrawStats(player *Me);
void SetColor(WINDOW *w, int pair, bool on);
/* Game logic */
int main(void);
int Random(int hi);
void InitializePlayer(player *Me, int year, int city, int level,
const char *name, bool MorF);
void AddRevenue(player *Me);
int AttackNeighbor(player *Me, player *Him);
void BuyCathedral(player *Me);
void BuyGrain(player *Me);
void BuyLand(player *Me);
void BuyMarket(player *Me);
void BuyMill(player *Me);
void BuyPalace(player *Me);
void BuySoldiers(player *Me);
int limit10(int num, int denom);
bool CheckNewTitle(player *Me);
void GenerateHarvest(player *Me);
void GenerateIncome(player *Me);
void ChangeTitle(player *Me);
void NewLandAndGrainPrices(player *Me);
void PrintGrain(player *Me);
int ReleaseGrain(player *Me);
void SeizeAssets(player *Me);
void SellGrain(player *Me);
void SellLand(player *Me);
void SerfsDecomposing(player *Me, float scale);
void SerfsProcreating(player *Me, float scale);
void PrintInstructions(void);
void PlayGame(player players[], int n);
void NewTurn(player *Me, int howMany, player players[], player *Baron);
void BuySellGrain(player *Me);
void AdjustTax(player *Me);
void StatePurchases(player *Me, int howMany, player players[]);
void ShowStats(player players[], int howMany);
void ImDead(player *Me);
/* ===========================================================================
* TUI — window management
* =========================================================================*/
void InitWindows(void) {
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
g_color = has_colors();
if (g_color) {
start_color();
use_default_colors();
init_pair(CP_NORMAL, COLOR_WHITE, -1);
init_pair(CP_WALL, COLOR_YELLOW, -1);
init_pair(CP_TOWER, COLOR_CYAN, -1);
init_pair(CP_FIELDS, COLOR_GREEN, -1);
init_pair(CP_BUILDINGS, COLOR_MAGENTA, -1);
init_pair(CP_STATS_HDR, COLOR_CYAN, -1);
init_pair(CP_STATS_VAL, COLOR_WHITE, -1);
init_pair(CP_MSG, COLOR_WHITE, -1);
init_pair(CP_TITLE, COLOR_YELLOW, -1);
init_pair(CP_WARNING, COLOR_RED, -1);
}
ResizeWindows();
}
void ResizeWindows(void) {
getmaxyx(stdscr, g_lines, g_cols);
g_map_cols = (int)(g_cols * MAP_COLS_FRAC);
g_map_lines = g_lines - MSG_LINES;
g_stat_cols = g_cols - g_map_cols;
if (w_map) {
delwin(w_map);
w_map = NULL;
}
if (w_stats) {
delwin(w_stats);
w_stats = NULL;
}
if (w_msg) {
delwin(w_msg);
w_msg = NULL;
}
w_map = newwin(g_map_lines, g_map_cols, 0, 0);
w_stats = newwin(g_map_lines, g_stat_cols, 0, g_map_cols);
w_msg = newwin(MSG_LINES, g_cols, g_map_lines, 0);
box(w_map, 0, 0);
box(w_stats, 0, 0);
box(w_msg, 0, 0);
wrefresh(w_map);
wrefresh(w_stats);
wrefresh(w_msg);
}
void TeardownWindows(void) {
if (w_map)
delwin(w_map);
if (w_stats)
delwin(w_stats);
if (w_msg)
delwin(w_msg);
endwin();
}
void SetColor(WINDOW *w, int pair, bool on) {
if (!g_color)
return;
if (on)
wattron(w, COLOR_PAIR(pair));
else
wattroff(w, COLOR_PAIR(pair));
}
/* ---------------------------------------------------------------------------
* Message window helpers
*
* Layout inside w_msg (MSG_LINES rows tall, g_cols wide):
*
* row 0 : box top border
* rows 1..LOG_ROWS : scrolling log lines (LOG_ROWS = MSG_LINES - 4)
* row MSG_LINES-3 : box-interior divider (ACS_HLINE)
* row MSG_LINES-2 : dedicated input/prompt line <-- NEVER scrolled
* row MSG_LINES-1 : box bottom border
*
* We manage scrolling ourselves with a simple ring buffer of strings so
* that wscrl() — which fights with box borders — is never called.
* --------------------------------------------------------------------------*/
#define LOG_ROWS (MSG_LINES - 4) /* number of visible log lines */
#define LOG_MAX 64 /* ring buffer capacity */
static char g_log[LOG_MAX][512]; /* ring buffer of log strings */
static int g_log_head = 0; /* index of oldest entry */
static int g_log_count = 0; /* number of valid entries */
/* Repaint the entire message window from the ring buffer. */
static void MsgRepaint(void) {
int r, entry;
werase(w_msg);
box(w_msg, 0, 0);
/* Divider above the input line */
mvwhline(w_msg, MSG_LINES - 3, 1, ACS_HLINE, g_cols - 2);
/* Paint log lines, most-recent at bottom */
for (r = 0; r < LOG_ROWS; r++) {
int age = LOG_ROWS - 1 - r; /* 0 = most recent */
if (age >= g_log_count)
continue; /* no entry that old yet */
entry = (g_log_head + g_log_count - 1 - age) % LOG_MAX;
SetColor(w_msg, CP_MSG, true);
mvwaddnstr(w_msg, r + 1, 1, g_log[entry], g_cols - 2);
SetColor(w_msg, CP_MSG, false);
}
wrefresh(w_msg);
}
void MsgClear(void) {
g_log_head = 0;
g_log_count = 0;
MsgRepaint();
}
/* Append a line to the log ring buffer and repaint. */
void MsgPrint(const char *fmt, ...) {
va_list ap;
int slot;
va_start(ap, fmt);
if (g_log_count < LOG_MAX) {
slot = (g_log_head + g_log_count) % LOG_MAX;
g_log_count++;
} else {
/* Buffer full: overwrite oldest entry and advance head */
slot = g_log_head;
g_log_head = (g_log_head + 1) % LOG_MAX;
}
vsnprintf(g_log[slot], sizeof(g_log[slot]), fmt, ap);
va_end(ap);
MsgRepaint();
}
/* Write prompt to the dedicated input line, read and validate an integer. */
int MsgGetInt(const char *prompt, int lo, int hi) {
char buf[64];
int val;
while (true) {
/* Clear and write prompt on the input line */
wmove(w_msg, MSG_LINES - 2, 1);
wclrtoeol(w_msg);
/* Restore right border erased by clrtoeol */
mvwaddch(w_msg, MSG_LINES - 2, g_cols - 1, ACS_VLINE);
SetColor(w_msg, CP_MSG, true);
mvwaddnstr(w_msg, MSG_LINES - 2, 1, prompt, g_cols - 4);
SetColor(w_msg, CP_MSG, false);
/* Position cursor right after the prompt text */
int prompt_len = (int)strlen(prompt);
if (prompt_len > g_cols - 4)
prompt_len = g_cols - 4;
wmove(w_msg, MSG_LINES - 2, 1 + prompt_len);
wrefresh(w_msg);
echo();
curs_set(1);
wgetnstr(w_msg, buf, (int)sizeof(buf) - 1);
curs_set(0);
noecho();
val = atoi(buf);
if (val >= lo && val <= hi)
return val;
MsgPrint("Enter a value between %d and %d.", lo, hi);
}
}
/* Write prompt to the input line, read a raw string. */
void MsgGetStr(const char *prompt, char *out, int maxlen) {
wmove(w_msg, MSG_LINES - 2, 1);
wclrtoeol(w_msg);
mvwaddch(w_msg, MSG_LINES - 2, g_cols - 1, ACS_VLINE);
SetColor(w_msg, CP_MSG, true);
mvwaddnstr(w_msg, MSG_LINES - 2, 1, prompt, g_cols - 4);
SetColor(w_msg, CP_MSG, false);
int prompt_len = (int)strlen(prompt);
if (prompt_len > g_cols - 4)
prompt_len = g_cols - 4;
wmove(w_msg, MSG_LINES - 2, 1 + prompt_len);
wrefresh(w_msg);
echo();
curs_set(1);
wgetnstr(w_msg, out, maxlen - 1);
curs_set(0);
noecho();
out[maxlen - 1] = '\0';
}
/* Grain-release prompt: accepts 1=min, 2=max, or a typed value.
* Shows min/max inline so the player can see them without scrolling up. */
int MsgGetGrain(int minimum, int maximum) {
char buf[64];
int val;
while (true) {
/* Build prompt with live min/max values */
char prompt[128];
snprintf(prompt, sizeof(prompt),
"Release grain 1=Min(%d) 2=Max(%d) or enter amount: ", minimum,
maximum);
wmove(w_msg, MSG_LINES - 2, 1);
wclrtoeol(w_msg);
mvwaddch(w_msg, MSG_LINES - 2, g_cols - 1, ACS_VLINE);
SetColor(w_msg, CP_MSG, true);
mvwaddnstr(w_msg, MSG_LINES - 2, 1, prompt, g_cols - 4);
SetColor(w_msg, CP_MSG, false);
int prompt_len = (int)strlen(prompt);
if (prompt_len > g_cols - 4)
prompt_len = g_cols - 4;
wmove(w_msg, MSG_LINES - 2, 1 + prompt_len);
wrefresh(w_msg);
echo();
curs_set(1);
wgetnstr(w_msg, buf, (int)sizeof(buf) - 1);
curs_set(0);
noecho();
if (buf[0] == '1' && buf[1] == '\0')
return minimum;
if (buf[0] == '2' && buf[1] == '\0')
return maximum;
val = atoi(buf);
if (val >= minimum && val <= maximum)
return val;
MsgPrint("Enter 1 (min), 2 (max), or a value between %d and %d.", minimum,
maximum);
}
}
/* "Press ENTER to continue" — uses the input line, not the log. */
void MsgWaitEnter(void) {
const char *msg = "[ Press ENTER to continue ]";
wmove(w_msg, MSG_LINES - 2, 1);
wclrtoeol(w_msg);
mvwaddch(w_msg, MSG_LINES - 2, g_cols - 1, ACS_VLINE);
SetColor(w_msg, CP_TITLE, true);
mvwaddnstr(w_msg, MSG_LINES - 2, 1, msg, g_cols - 2);
SetColor(w_msg, CP_TITLE, false);
wrefresh(w_msg);
wgetch(w_msg);
/* Clear the input line after the keypress */
wmove(w_msg, MSG_LINES - 2, 1);
wclrtoeol(w_msg);
mvwaddch(w_msg, MSG_LINES - 2, g_cols - 1, ACS_VLINE);
wrefresh(w_msg);
}
/* ===========================================================================
* DrawStats — right-hand panel
* =========================================================================*/
void DrawStats(player *Me) {
int row = 1;
werase(w_stats);
box(w_stats, 0, 0);
/* Title bar */
SetColor(w_stats, CP_TITLE, true);
mvwprintw(w_stats, row++, 1, " %-*s", g_stat_cols - 3, Me->City);
mvwprintw(w_stats, row++, 1, " %s %s", Me->Title, Me->Name);
SetColor(w_stats, CP_TITLE, false);
mvwhline(w_stats, row++, 1, ACS_HLINE, g_stat_cols - 2);
/* Helper macro to print a labelled stat row */
#define STAT_ROW(lbl, fmt, val) \
do { \
SetColor(w_stats, CP_STATS_HDR, true); \
mvwprintw(w_stats, row, 1, "%-14s", (lbl)); \
SetColor(w_stats, CP_STATS_HDR, false); \
SetColor(w_stats, CP_STATS_VAL, true); \
mvwprintw(w_stats, row, 15, fmt, (val)); \
SetColor(w_stats, CP_STATS_VAL, false); \
row++; \
} while (0)
STAT_ROW("Year", "%d", Me->Year);
STAT_ROW("Treasury", "%d fl", Me->Treasury);
STAT_ROW("Land", "%d ha", Me->Land);
STAT_ROW("Grain Rsrv", "%d st", Me->GrainReserve);
STAT_ROW("Grain Dmnd", "%d st", Me->GrainDemand);
STAT_ROW("Grain Price", "%d/1000", Me->GrainPrice);
STAT_ROW("Land Price", "%.2f/ha", Me->LandPrice);
row++;
mvwhline(w_stats, row++, 1, ACS_HLINE, g_stat_cols - 2);
STAT_ROW("Serfs", "%d", Me->Serfs);
STAT_ROW("Soldiers", "%d", Me->Soldiers);
STAT_ROW("Nobles", "%d", Me->Nobles);
STAT_ROW("Clergy", "%d", Me->Clergy);
STAT_ROW("Merchants", "%d", Me->Merchants);
row++;
mvwhline(w_stats, row++, 1, ACS_HLINE, g_stat_cols - 2);
STAT_ROW("Customs", "%d%%", Me->CustomsDuty);
STAT_ROW("Sales Tax", "%d%%", Me->SalesTax);
STAT_ROW("Income Tax", "%d%%", Me->IncomeTax);
const char *justiceStr;
switch (Me->Justice) {
case 1:
justiceStr = "Very Fair";
break;
case 2:
justiceStr = "Moderate";
break;
case 3:
justiceStr = "Harsh";
break;
default:
justiceStr = "Outrageous";
break;
}
STAT_ROW("Justice", "%s", justiceStr);
row++;
mvwhline(w_stats, row++, 1, ACS_HLINE, g_stat_cols - 2);
STAT_ROW("Cathedrals", "%d", Me->Cathedral);
STAT_ROW("Palaces", "%d", Me->Palace);
STAT_ROW("Markets", "%d", Me->Marketplaces);
STAT_ROW("Mills", "%d", Me->Mills);
STAT_ROW("Pub. Works", "%.2f", Me->PublicWorks);
#undef STAT_ROW
/* Bankruptcy / invasion warnings */
if (Me->IsBankrupt) {
SetColor(w_stats, CP_WARNING, true);
mvwprintw(w_stats, row++, 1, "** BANKRUPT **");
SetColor(w_stats, CP_WARNING, false);
}
if (Me->InvadeMe) {
SetColor(w_stats, CP_WARNING, true);
mvwprintw(w_stats, row++, 1, "** UNDER THREAT **");
SetColor(w_stats, CP_WARNING, false);
}
wrefresh(w_stats);
}
/* ===========================================================================
* DrawMap — left-hand panel
*
* The map is divided into three horizontal bands:
*
* [ guard tower | sky / title bar ] <- top ~25%
* [ | walled city (buildings inside) ] <- middle ~50%
* [ fields / plowman ] <- bottom ~25%
*
* Scaling rules (faithful to original description):
* Wall width = scales with Land (min 10, max map_inner_cols-2)
* Wall height = scales with Land (min 4, max city_band_height-2)
* Tower height = scales with Soldiers vs Land/1000 adequacy ratio
* (tall = well defended, short = vulnerable)
* Tower width = always 4 chars
* Plowman pos = at top of field band if Serfs >= Land/10 (all in production)
* otherwise descends proportionally
* Buildings = cathedrals (+), palaces (P), markets (M), mills (~)
* drawn left-to-right inside the wall
* =========================================================================*/
void DrawMap(player *Me) {
werase(w_map);
box(w_map, 0, 0);
int inner_cols = g_map_cols - 2; /* inside box border */
int inner_lines = g_map_lines - 2;
if (inner_cols < 20 || inner_lines < 12) {
mvwprintw(w_map, 1, 1, "Terminal too small.");
wrefresh(w_map);
return;
}
/* --- Band heights ---------------------------------------------------- */
int sky_lines = inner_lines / 5; /* sky/title: 20% */
int city_lines = (inner_lines * 2) / 4; /* city: 50% */
int field_lines = inner_lines - sky_lines - city_lines; /* fields: ~30% */
int sky_top = 1;
int city_top = sky_top + sky_lines;
int field_top = city_top + city_lines;
/* --- Title in sky band ----------------------------------------------- */
SetColor(w_map, CP_TITLE, true);
mvwprintw(w_map, sky_top, 2, "%s, %d AD", Me->City, Me->Year);
SetColor(w_map, CP_TITLE, false);
/* --- City wall dimensions -------------------------------------------- */
/* Land ranges roughly 5000–50000; wall width scales across inner_cols */
int land_clamped = Me->Land;
if (land_clamped < 5000)
land_clamped = 5000;
if (land_clamped > 50000)
land_clamped = 50000;
int wall_w = (int)((float)(land_clamped - 5000) / (float)(50000 - 5000) *
(float)(inner_cols - 12)) +
10;
if (wall_w > inner_cols - 2)
wall_w = inner_cols - 2;
int wall_h = (city_lines * wall_w) / (inner_cols - 2);
if (wall_h < 4)
wall_h = 4;
if (wall_h > city_lines - 1)
wall_h = city_lines - 1;
int wall_left = (inner_cols - wall_w) / 2 + 1;
int wall_top = city_top + (city_lines - wall_h) / 2;
/* --- Draw city walls ------------------------------------------------- */
SetColor(w_map, CP_WALL, true);
/* Top and bottom wall */
for (int c = wall_left; c < wall_left + wall_w; c++) {
mvwaddch(w_map, wall_top, c, ACS_HLINE);
mvwaddch(w_map, wall_top + wall_h - 1, c, ACS_HLINE);
}
/* Left and right wall */
for (int r = wall_top + 1; r < wall_top + wall_h - 1; r++) {
mvwaddch(w_map, r, wall_left, ACS_VLINE);
mvwaddch(w_map, r, wall_left + wall_w - 1, ACS_VLINE);
}
/* Corners */
mvwaddch(w_map, wall_top, wall_left, ACS_ULCORNER);
mvwaddch(w_map, wall_top, wall_left + wall_w - 1, ACS_URCORNER);
mvwaddch(w_map, wall_top + wall_h - 1, wall_left, ACS_LLCORNER);
mvwaddch(w_map, wall_top + wall_h - 1, wall_left + wall_w - 1, ACS_LRCORNER);
/* Gate in the middle of the bottom wall */
int gate_col = wall_left + wall_w / 2;
mvwaddch(w_map, wall_top + wall_h - 1, gate_col, '[');
mvwaddch(w_map, wall_top + wall_h - 1, gate_col + 1, ']');
SetColor(w_map, CP_WALL, false);
/* --- Guard tower (upper-left of wall) -------------------------------- */
/*
* Tower adequacy: soldiers should be >= land/1000.
* ratio = soldiers / (land/1000). Clamp 0.0–2.0.
* Tower height scales from 1 (ratio=0) to sky_lines+2 (ratio>=1.5).
*/
float tower_ratio =
(float)Me->Soldiers / (float)((Me->Land > 0 ? Me->Land : 1) / 1000 + 1);
if (tower_ratio > 2.0f)
tower_ratio = 2.0f;
int tower_max_h = sky_lines + 2; /* can extend up into sky band */
int tower_h = (int)(tower_ratio / 2.0f * (float)tower_max_h);
if (tower_h < 1)
tower_h = 1;
int tower_w = 5;
int tower_left = wall_left;
int tower_base = wall_top; /* tower sits on top of wall */
int tower_top_r = tower_base - tower_h;
if (tower_top_r < sky_top)
tower_top_r = sky_top;
SetColor(w_map, CP_TOWER, true);
/* Tower sides */
for (int r = tower_top_r; r < tower_base; r++) {
mvwaddch(w_map, r, tower_left, '|');
mvwaddch(w_map, r, tower_left + tower_w - 1, '|');
/* Fill interior */
for (int c = tower_left + 1; c < tower_left + tower_w - 1; c++)
mvwaddch(w_map, r, c, ' ');
}
/* Battlements on top */
if (tower_top_r >= sky_top) {
for (int c = tower_left; c < tower_left + tower_w; c++)
mvwaddch(w_map, tower_top_r, c, (c % 2 == 0) ? 'n' : '_');
}
SetColor(w_map, CP_TOWER, false);
/* --- Buildings inside the walls -------------------------------------- */
/*
* We draw symbols left-to-right on the row just above the bottom wall:
* Cathedral -> '+' Palace -> 'P' Market -> 'M' Mill -> '~'
*/
SetColor(w_map, CP_BUILDINGS, true);
int brow = wall_top + wall_h - 2; /* one row above gate */
int bcol = wall_left + 2;
int bmax = wall_left + wall_w - 2;
#define DRAW_BUILDINGS(sym, count) \
do { \
for (int _i = 0; _i < (count) && bcol < bmax; _i++, bcol++) \
mvwaddch(w_map, brow, bcol, (sym)); \
} while (0)
DRAW_BUILDINGS('+', Me->Cathedral);
DRAW_BUILDINGS('P', Me->Palace);
DRAW_BUILDINGS('M', Me->Marketplaces);
DRAW_BUILDINGS('~', Me->Mills);
#undef DRAW_BUILDINGS
/* A simple keep/castle in the centre of the wall interior */
int keep_col = wall_left + wall_w / 2 - 1;
int keep_row = wall_top + wall_h / 2;
if (keep_row < wall_top + 1)
keep_row = wall_top + 1;
if (keep_row > wall_top + wall_h - 2)
keep_row = wall_top + wall_h - 2;
if (keep_col > 1 && keep_col + 3 < g_map_cols - 1) {
mvwprintw(w_map, keep_row - 1, keep_col, "^n^");
mvwprintw(w_map, keep_row, keep_col, "[H]");
}
SetColor(w_map, CP_BUILDINGS, false);
/* --- Fields and plowman --------------------------------------------- */
/*
* The plowman is at the TOP of the field band when all land is in
* production (serfs >= land/10). Otherwise he descends proportionally.
*/
SetColor(w_map, CP_FIELDS, true);
/* Draw field rows with crop symbols */
for (int r = field_top; r < field_top + field_lines - 1; r++) {
for (int c = 1; c < inner_cols + 1; c++) {
char ch = ((r + c) % 4 == 0) ? '"' : '.';
mvwaddch(w_map, r, c, ch);
}
}
/* Plowman position */
int serfs_needed = Me->Land / 10;
if (serfs_needed < 1)
serfs_needed = 1;
float prod_ratio = (float)Me->Serfs / (float)serfs_needed;
if (prod_ratio > 1.0f)
prod_ratio = 1.0f;
/* prod_ratio==1 → row 0 of field band; ratio==0 → last row */
int plow_row =
field_top + (int)((1.0f - prod_ratio) * (float)(field_lines - 2));
if (plow_row < field_top)
plow_row = field_top;
if (plow_row > field_top + field_lines - 2)
plow_row = field_top + field_lines - 2;
int plow_col = inner_cols / 3;
/* Clear a little space around the plowman */
mvwprintw(w_map, plow_row, plow_col, " ");
SetColor(w_map, CP_FIELDS, false);
SetColor(w_map, CP_BUILDINGS, true);
mvwprintw(w_map, plow_row, plow_col, "o-HH-8>"); /* horse & plowman */
SetColor(w_map, CP_BUILDINGS, false);
/* Grain reserve indicator — a simple bar at the very bottom */
int max_grain = 20000;
int grain_bar =
(int)((float)Me->GrainReserve / (float)(max_grain > 0 ? max_grain : 1) *
(float)(inner_cols));
if (grain_bar > inner_cols)
grain_bar = inner_cols;
if (grain_bar < 0)
grain_bar = 0;
SetColor(w_map, CP_FIELDS, true);
for (int c = 1; c <= grain_bar; c++)
mvwaddch(w_map, field_top + field_lines - 1, c, ACS_BLOCK);
mvwprintw(w_map, field_top + field_lines - 1, grain_bar + 2, "grain:%d",
Me->GrainReserve);
SetColor(w_map, CP_FIELDS, false);
wrefresh(w_map);
}
/* ===========================================================================
* Random
* =========================================================================*/
int Random(int hi) {
if (hi <= 0)
return 0;
return rand() % (hi + 1);
}
/* ===========================================================================
* InitializePlayer
* =========================================================================*/
void InitializePlayer(player *Me, int year, int city, int level,
const char *name, bool MorF) {
Me->Cathedral = 0;
strncpy(Me->City, CityList[city], sizeof(Me->City) - 1);
Me->City[sizeof(Me->City) - 1] = '\0';
Me->Clergy = 5;
Me->CustomsDuty = 25;
Me->Difficulty = level;
Me->GrainPrice = 25;
Me->GrainReserve = 5000;
Me->IncomeTax = 5;
Me->IsBankrupt = false;
Me->IsDead = false;
Me->IWon = false;
Me->Justice = 2;
Me->Land = 10000;
Me->LandPrice = 10.0f;
Me->MaleOrFemale = MorF;
Me->Marketplaces = 0;
Me->Merchants = 25;
Me->Mills = 0;
strncpy(Me->Name, name, sizeof(Me->Name) - 1);
Me->Name[sizeof(Me->Name) - 1] = '\0';
Me->Nobles = 4;
Me->OldTitle = 1;
Me->Palace = 0;
Me->PublicWorks = 1.0f;
Me->SalesTax = 10;
Me->Serfs = 2000;
Me->Soldiers = 25;
Me->TitleNum = 1;
if (Me->MaleOrFemale)
strncpy(Me->Title, MaleTitles[0], sizeof(Me->Title) - 1);
else
strncpy(Me->Title, FemaleTitles[0], sizeof(Me->Title) - 1);
Me->Title[sizeof(Me->Title) - 1] = '\0';
if (city == 6)
strncpy(Me->Title, "Baron", sizeof(Me->Title) - 1);
Me->Treasury = 1000;
Me->WhichPlayer = city;
Me->Year = year;
Me->YearOfDeath = year + 20 + Random(35);
}
/* ===========================================================================
* AddRevenue
* =========================================================================*/
void AddRevenue(player *Me) {
Me->Treasury += Me->JusticeRevenue + Me->CustomsDutyRevenue +
Me->IncomeTaxRevenue + Me->SalesTaxRevenue;
if (Me->Treasury < 0)
Me->Treasury = (int)((float)Me->Treasury * 1.5f);
if (Me->Treasury < (-10000 * Me->TitleNum))
Me->IsBankrupt = true;
}
/* ===========================================================================
* AttackNeighbor
* =========================================================================*/
int AttackNeighbor(player *Me, player *Him) {
int LandTaken, deadsoldiers;
if (Me->WhichPlayer == 7)
LandTaken = Random(9000) + 1000;
else
LandTaken = (Me->Soldiers * 1000) - (Me->Land / 3);
if (LandTaken > (Him->Land - 5000))
LandTaken = (Him->Land - 5000) / 2;
Me->Land += LandTaken;
Him->Land -= LandTaken;
beep();
MsgPrint("%s %s of %s invades and seizes %d hectares!", Me->Title, Me->Name,
Me->City, LandTaken);
deadsoldiers = Random(40);
if (deadsoldiers > (Him->Soldiers - 15))
deadsoldiers = Him->Soldiers - 15;
Him->Soldiers -= deadsoldiers;
MsgPrint("%s %s loses %d soldiers in battle.", Him->Title, Him->Name,
deadsoldiers);
DrawMap(Him);
DrawStats(Him);
return LandTaken;
}
/* ===========================================================================
* Buy/sell helpers
* =========================================================================*/
void BuyCathedral(player *Me) {
Me->Cathedral++;
Me->Clergy += Random(6);
Me->Treasury -= 5000;
Me->PublicWorks += 1.0f;
}
void BuyGrain(player *Me) {
int howmuch =
MsgGetInt("How much grain to buy (0=specify total)? ", 0, 999999);
if (howmuch == 0) {
int total =
MsgGetInt("Desired total grain reserve? ", Me->GrainReserve, 999999);
howmuch = total - Me->GrainReserve;
}
Me->Treasury -= howmuch * Me->GrainPrice / 1000;
Me->GrainReserve += howmuch;
}
void BuyLand(player *Me) {
int howmuch = MsgGetInt("How many hectares to buy? ", 0, 999999);
Me->Land += howmuch;
Me->Treasury -= (int)((float)howmuch * Me->LandPrice);
}
void BuyMarket(player *Me) {
Me->Marketplaces++;
Me->Merchants += 5;
Me->Treasury -= 1000;
Me->PublicWorks += 1.0f;
}
void BuyMill(player *Me) {
Me->Mills++;
Me->Treasury -= 2000;
Me->PublicWorks += 0.25f;
}
void BuyPalace(player *Me) {
Me->Palace++;
Me->Nobles += Random(2);
Me->Treasury -= 3000;
Me->PublicWorks += 0.5f;
}
void BuySoldiers(player *Me) {
Me->Soldiers += 20;
Me->Serfs -= 20;
Me->Treasury -= 500;
}
void SellGrain(player *Me) {
int howmuch = MsgGetInt("How much grain to sell? ", 0, Me->GrainReserve);
Me->Treasury += howmuch * Me->GrainPrice / 1000;
Me->GrainReserve -= howmuch;
}
void SellLand(player *Me) {
int maxsell = Me->Land - 5000;
if (maxsell <= 0) {
MsgPrint("You have no land to sell.");
return;
}
int howmuch = MsgGetInt("How many hectares to sell? ", 0, maxsell);
Me->Land -= howmuch;
Me->Treasury += (int)((float)howmuch * Me->LandPrice);
}
/* ===========================================================================
* limit10
* =========================================================================*/
int limit10(int num, int denom) {
int val = num / denom;
return val > 10 ? 10 : val;
}
/* ===========================================================================
* CheckNewTitle
* =========================================================================*/
bool CheckNewTitle(player *Me) {
int Total = limit10(Me->Marketplaces, 1) + limit10(Me->Palace, 1) +
limit10(Me->Cathedral, 1) + limit10(Me->Mills, 1) +
limit10(Me->Treasury, 5000) + limit10(Me->Land, 6000) +
limit10(Me->Merchants, 50) + limit10(Me->Nobles, 5) +
limit10(Me->Soldiers, 50) + limit10(Me->Clergy, 10) +
limit10(Me->Serfs, 2000) +
limit10((int)(Me->PublicWorks * 100.0f), 500);
Me->TitleNum = (Total / Me->Difficulty) - Me->Justice;
if (Me->TitleNum > 7)
Me->TitleNum = 7;
if (Me->TitleNum < 0)
Me->TitleNum = 0;
if (Me->TitleNum > Me->OldTitle) {
Me->OldTitle = Me->TitleNum;
ChangeTitle(Me);
beep();
MsgPrint("Good news! %s has achieved the rank of %s!", Me->Name, Me->Title);
MsgWaitEnter();
return true;
}
Me->TitleNum = Me->OldTitle;
return false;
}
/* ===========================================================================
* GenerateHarvest
* =========================================================================*/
void GenerateHarvest(player *Me) {
Me->Harvest = (Random(5) + Random(6)) / 2;
Me->Rats = Random(50);
Me->GrainReserve =
((Me->GrainReserve * 100) - (Me->GrainReserve * Me->Rats)) / 100;
}
/* ===========================================================================
* GenerateIncome
* =========================================================================*/
void GenerateIncome(player *Me) {
const char *justStr;
float y;
Me->JusticeRevenue = (Me->Justice * 300 - 500) * Me->TitleNum;
switch (Me->Justice) {
case 1:
justStr = "Very Fair";
break;
case 2: