-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
519 lines (452 loc) · 19 KB
/
Copy pathmain.c
File metadata and controls
519 lines (452 loc) · 19 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
/*
*
* main.c for Tavern
*
* Copyright 2026 terra2o and contributors
*
* Licensed under GPLv3
*
*/
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#include <string.h>
#include "include/game_state.h"
#include "include/sim.h"
#include "include/log.h"
#include "include/ui.h"
#include "include/save.h"
#include "include/event.h"
#include "include/version.h"
#ifdef _WIN32
#include <windows.h>
#endif
#define VERSION_STRING "Tavern - Version: " GAME_VERSION
#ifdef _WIN32
/* GetCurrentConsoleFontEx/SetCurrentConsoleFontEx were added in Vista and
don't exist in XP's kernel32.dll. Linking against them directly makes the
whole executable fail to load on XP with "procedure entry point ... could
not be located", so they're resolved dynamically and skipped if absent. */
typedef BOOL (WINAPI *GetCurrentConsoleFontEx_t)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
typedef BOOL (WINAPI *SetCurrentConsoleFontEx_t)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
static void windows_shrink_console_font(void)
{
HMODULE k32 = GetModuleHandleA("kernel32.dll");
GetCurrentConsoleFontEx_t pGetCurrentConsoleFontEx;
SetCurrentConsoleFontEx_t pSetCurrentConsoleFontEx;
HANDLE con;
CONSOLE_FONT_INFOEX font;
if (!k32)
return;
pGetCurrentConsoleFontEx =
(GetCurrentConsoleFontEx_t)GetProcAddress(k32, "GetCurrentConsoleFontEx");
pSetCurrentConsoleFontEx =
(SetCurrentConsoleFontEx_t)GetProcAddress(k32, "SetCurrentConsoleFontEx");
if (!pGetCurrentConsoleFontEx || !pSetCurrentConsoleFontEx)
return;
con = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (con == INVALID_HANDLE_VALUE)
return;
memset(&font, 0, sizeof(font));
font.cbSize = sizeof(font);
if (pGetCurrentConsoleFontEx(con, FALSE, &font)) {
if (font.dwFontSize.X > 8) font.dwFontSize.X = 8;
if (font.dwFontSize.Y > 12) font.dwFontSize.Y = 12;
pSetCurrentConsoleFontEx(con, FALSE, &font);
}
CloseHandle(con);
}
/* Keep the console screen buffer sized to whatever the window can actually
grow to on this screen/font, instead of padding it out to a large fixed
size. The window can never be dragged bigger than the buffer, so some
headroom is needed for resizing to work at all, but a buffer much bigger
than what the screen can show creates scrollbars that the legacy Windows
10 console host (conhost.exe) redraws very badly, causing visible
glitching/stutter that doesn't happen on older console hosts (Vista) or
Windows Terminal, which don't share that renderer. Bounding the buffer to
GetLargestConsoleWindowSize() gives exactly enough room to resize up to
full screen without any headroom beyond that. */
static void windows_grow_console_buffer(void)
{
HANDLE con;
CONSOLE_SCREEN_BUFFER_INFO csbi;
con = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (con == INVALID_HANDLE_VALUE)
return;
if (GetConsoleScreenBufferInfo(con, &csbi)) {
COORD size = GetLargestConsoleWindowSize(con);
SHORT win_x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
SHORT win_y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
if (size.X < win_x) size.X = win_x;
if (size.Y < win_y) size.Y = win_y;
/* SetConsoleScreenBufferSize() makes the console enqueue a fresh
WINDOW_BUFFER_SIZE_EVENT even when called with the size it
already has. Since this function runs from the KEY_RESIZE
handler, calling it unconditionally turns one resize event
into an infinite loop: handle resize -> call this -> new
(spurious) resize event -> handle resize -> ... which starves
real keyboard input for as long as it keeps going. Only call
it when the size is actually changing. */
if (size.X > 0 && size.Y > 0 &&
(size.X != csbi.dwSize.X || size.Y != csbi.dwSize.Y))
SetConsoleScreenBufferSize(con, size);
}
CloseHandle(con);
}
/* True only if the console window's actual size disagrees with what
PDCurses thinks LINES/COLS are. Used to tell a real resize apart from
a spurious WINDOW_BUFFER_SIZE_EVENT (including ones caused by our own
resize handling calling SetConsoleScreenBufferSize/
SetConsoleActiveScreenBuffer), since acting on a spurious one just
re-triggers another such event and loops forever, starving keyboard
input for as long as it keeps going.
Must open "CONOUT$" rather than use GetStdHandle(STD_OUTPUT_HANDLE):
PDCurses creates its own console screen buffer on startup and makes
it the active (visible) one via SetConsoleActiveScreenBuffer, but
STD_OUTPUT_HANDLE keeps pointing at the original buffer, which is now
invisible and never resized. Querying it reads a permanently stale
window rect, so the resize is never seen, regardless of Windows
version. "CONOUT$" always refers to whichever buffer is currently
active. */
static int windows_console_size_changed(void)
{
HANDLE con;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int win_x, win_y;
con = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (con == INVALID_HANDLE_VALUE)
return 1;
if (!GetConsoleScreenBufferInfo(con, &csbi)) {
CloseHandle(con);
return 1;
}
win_x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
win_y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
CloseHandle(con);
return win_x != COLS || win_y != LINES;
}
/* KEY_RESIZE only reaches PDCurses via a WINDOW_BUFFER_SIZE_EVENT from the
console subsystem. Modern conhost.exe (Vista and later) reliably posts
that event for a plain window-drag resize, but XP's older csrss-hosted
console (and possibly other pre-conhost hosts) does not, so a dragged
resize can go completely unnoticed there. Polling the actual console size
once per frame sidesteps the event entirely and works the same on every
Windows version. */
static void windows_poll_resize(void)
{
if (windows_console_size_changed()) {
resize_term(0, 0);
windows_grow_console_buffer();
}
}
/* The game never uses the mouse, so PDCurses just leaves QuickEdit Mode
at whatever the console already had (see pdc_quick_edit in
vendor/pdcurses/wincon/pdcscrn.c and pdckbd.c). QuickEdit is on by
default in classic Windows consoles (cmd.exe), and with it on, clicking
into the window to focus it starts a text selection that blocks
ReadConsoleInput (what getch() relies on) until Escape or a right click
cancels it. That looks exactly like the game "not taking input", so
force it off explicitly instead of relying on the user's console
defaults. */
static void windows_disable_quick_edit(void)
{
HANDLE con = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (con == INVALID_HANDLE_VALUE)
return;
if (GetConsoleMode(con, &mode)) {
mode &= ~ENABLE_QUICK_EDIT_MODE;
mode |= ENABLE_EXTENDED_FLAGS;
SetConsoleMode(con, mode);
}
}
#endif
static void event_handler(Tavern* b, Town* t, Kingdom* k, World* w, UiState* ui_state, int actions_per_day, UiMode mode, int* resolved)
{
w->pending_event = EVENT_NONE;
ui_state->mode = mode;
*resolved = 0;
while (!*resolved) {
int ch;
draw_ui(b, w->day, 0, actions_per_day, t, k, w, ui_state, &ui_state->war);
ch = getch();
napms(16);
#ifdef _WIN32
windows_poll_resize();
if (ch != KEY_RESIZE && ch != ERR)
ui_handle_input(ch, ui_state, b, t, k, w);
#else
if (ch == KEY_RESIZE)
resize_term(0, 0);
else if (ch != ERR)
ui_handle_input(ch, ui_state, b, t, k, w);
#endif
}
ui_state->mode = UI_MODE_NORMAL;
}
/* Fresh tavern with default starting stats, supplied by merchant_id.
day is only used to schedule the first rent payment. */
static Tavern make_starter_tavern(int day, int merchant_id, const Merchant* m)
{
Tavern b = {0};
b.money = 700.0f;
b.drinks[DRINK_ALE].price = 5.0f;
b.drinks[DRINK_WINE_APPLE].price = 120.0f;
b.drinks[DRINK_WINE_GRAPE].price = 120.0f;
b.drinks[DRINK_ALE].inventory.amount = 10;
b.drinks[DRINK_WINE_APPLE].inventory.amount = 2;
b.drinks[DRINK_WINE_GRAPE].inventory.amount = 2;
b.last_drink_price[DRINK_ALE] = 1.0f;
b.last_drink_price[DRINK_WINE_APPLE] = 1.0f;
b.last_drink_price[DRINK_WINE_GRAPE] = 1.0f;
b.fruits[FRUIT_APPLE].inventory.expiration_date = 30;
b.fruits[FRUIT_GRAPE].inventory.expiration_date = 30;
b.fruits[FRUIT_APPLE].inventory.amount = 1;
b.fruits[FRUIT_GRAPE].inventory.amount = 1;
b.quality_actual = m->quality;
b.quality_perceived = 0.5f;
b.rumor = 0.5f;
b.consistency = 1.0f;
b.handsomeness = 0.6f;
b.reputation = 0.5f;
b.supplier_id = merchant_id;
b.last_pathway_clean_day = 0;
b.rent.pay_period = 30;
b.rent.next_payment_day = day + b.rent.pay_period;
b.rent.rent_amount = 1500;
b.rent.base_rent = 1500;
b.rent.next_wage_day = day + b.rent.pay_period;
b.employees = 0;
b.employees_wage = 500.0f;
b.tavern_size = 1;
return b;
}
static void init_new_game(World* w)
{
Kingdom kingdom = {0};
Town town = {0};
Merchant m_init = {0};
Merchant m_rival = {0};
Tavern b_init;
Tavern rival;
int merchant_id;
int rival_merchant_id;
int i;
w->day = 0;
world_kingdoms_init(w, MAX_KINGDOMS);
kingdom.inflation_rate = 1.0f;
kingdom.money_supply_prev = 0.0f;
kingdom_towns_init(&kingdom, MAX_TOWNS);
town.last_advertised_day = 0;
town_merchants_init(&town, MAX_MERCHANTS);
town_taverns_init(&town, MAX_TAVERNS);
town_cats_init(&town, ANIMALS_DEFAULT_CAPACITY);
/* Seed a small starting colony - cats_tick() only makes kittens from
existing mature pairs, so the town needs a handful to start with
or it would never have any cats at all. */
for (i = 0; i < 4; i++) cat_spawn(&town.cats);
population_init(&town.population, 100000);
for (i = 0; i < 150; i++) citizen_spawn(&town.population);
m_init.drink_price[DRINK_ALE] = 5.0f;
m_init.drink_price[DRINK_WINE_APPLE] = 90.0f;
m_init.drink_price[DRINK_WINE_GRAPE] = 90.0f;
m_init.quality = 0.7f;
m_init.instability = 0.2f;
merchant_init_default_stock(&m_init);
merchant_id = town_add_merchant(&town, m_init);
b_init = make_starter_tavern(w->day, merchant_id, &m_init);
town.player_tavern_id = town_add_tavern(&town, b_init);
/* Rival's supplier: cheaper on ale but riskier and lower quality,
so the two starter taverns draw from genuinely different
merchants instead of sharing one. */
m_rival.drink_price[DRINK_ALE] = 4.5f;
m_rival.drink_price[DRINK_WINE_APPLE] = 90.0f;
m_rival.drink_price[DRINK_WINE_GRAPE] = 90.0f;
m_rival.quality = 0.6f;
m_rival.instability = 0.35f;
merchant_init_default_stock(&m_rival);
rival_merchant_id = town_add_merchant(&town, m_rival);
rival = make_starter_tavern(w->day, rival_merchant_id, &m_rival);
rival.money = 500.0f;
town_add_tavern(&town, rival);
town_relink_suppliers(&town);
kingdom.player_town_id = kingdom_add_town(&kingdom, town);
w->player_kingdom_id = world_add_kingdom(w, kingdom);
}
int main(void)
{
World w = {0};
Kingdom* k;
Town* t;
Tavern* b;
int game_running;
char version[64];
char pool_buf[64];
UiState ui_state;
srand(time(NULL));
if (!load_game(SAVE_PATH, &w)) {
init_new_game(&w);
save_game(SAVE_PATH, &w);
}
k = world_player_kingdom(&w);
t = world_player_town(&w);
b = world_player_tavern(&w);
initscr();
#ifdef _WIN32
windows_shrink_console_font();
windows_grow_console_buffer();
windows_disable_quick_edit();
#endif
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
#ifndef PDCURSES
/* ncurses holds a lone ESC for up to ESCDELAY ms (often ~1000) in
case it's the start of an escape sequence, which makes every ESC
cancel/close in this game feel like it needs a second press to
actually register. PDCurses reads console key events directly
and has no such delay, so it doesn't need (or have) this call. */
set_escdelay(25);
#endif
init_colors();
curs_set(0);
game_running = 1;
tavern_snprintf(version, sizeof(version), "%s", VERSION_STRING);
log_message(&w.log, version, LOG_IMPORTANT);
log_message(&w.log, "Welcome! Press a key to start the best tavern simulation ever...", LOG_IMPORTANT);
tavern_snprintf(pool_buf, sizeof(pool_buf), "Taverns in town: %d | Merchants: %d",
t->tavern_count, t->merchant_count);
log_message(&w.log, pool_buf, LOG_INFO);
ui_state_init(&ui_state);
ui_state.war.our_kingdom_attack = k->our_kingdom_attack;
while (game_running) {
/* Recomputed each day since hiring can change it mid-game */
int actions_per_day = tavern_actions_per_day(b);
int action_num;
int sales;
int total_wine;
char buf_l[256];
/* Allow multiple actions per day */
for (action_num = 1;
action_num <= actions_per_day && game_running;
action_num++) {
while (1) {
int ch;
Action choice;
draw_ui(b, w.day, action_num, actions_per_day, t, k, &w, &ui_state, &ui_state.war);
ch = getch();
napms(16);
#ifdef _WIN32
windows_poll_resize();
if (ch != KEY_RESIZE && ch != ERR)
ui_handle_input(ch, &ui_state, b, t, k, &w);
#else
if (ch == KEY_RESIZE)
resize_term(0, 0);
else if (ch != ERR)
ui_handle_input(ch, &ui_state, b, t, k, &w);
#endif
if (ui_state.number_input.is_confirmed != 0) {
ui_process_action(&ui_state, b, t, k, &w);
break;
}
if (ui_state.mode != UI_MODE_NORMAL)
continue;
if (ch == 's' || ch == 'S') {
ui_state.mode = UI_MODE_SUPPLIER;
ui_state.supplier.selected = b->supplier_id;
continue;
}
if (ch == 'd' || ch == 'D') {
ui_state.mode = UI_MODE_DETAIL;
continue;
}
choice = read_action(ch);
if (choice == (Action)-1) {
game_running = 0;
break;
}
else if (choice == (Action)-2)
continue;
else if (choice == ACT_BUY_WINE || choice == ACT_ADJUST_WINE_PRICE) {
ui_state.pending_action = choice;
ui_state.mode = UI_MODE_WINE_VARIETY;
}
else if (find_action_input_spec(choice) != NULL) {
const ActionInputSpec* spec = find_action_input_spec(choice);
ui_state.pending_action = choice;
ui_start_number_input(&ui_state, spec->prompt, spec->min_val, spec->max_val, spec->is_float);
}
else if (choice == ACT_CLEAN_PATHWAY) {
apply_action(b, choice, t, k, &w, 0);
log_message(&w.log, "Cleaned pathway.", LOG_INFO);
break;
}
else if (choice == ACT_COLLECT_FRUIT) {
int cmax_x, cmax_y;
getmaxyx(stdscr, cmax_y, cmax_x);
collect_state_start(&ui_state.collect, cmax_x, cmax_y);
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_COLLECT, &ui_state.collect.resolved);
log_message(&w.log, "Went out to pick fruit.", LOG_INFO);
break;
}
else {
apply_action(b, choice, t, k, &w, 0);
/* hire/expand already log their own outcome (success or
failure) inside apply_action, so logging a generic
"completed" here would contradict a failure message */
if (choice != ACT_HIRE_EMPLOYEES && choice != ACT_EXPAND_TAVERN)
log_message(&w.log, "Action completed.", LOG_INFO);
break;
}
}
}
if (!game_running)
break;
/* End of day simulation (ADVANCES w.day) */
sales = simulate_day(&w);
/* Resolve any pending event before the next day */
/* War events fire additionally when at war */
if (k->at_war && w.pending_event == EVENT_NONE)
random_war_event(k, &w);
/* Check if war ends */
if (k->at_war && w.day >= k->war_end_day) {
k->at_war = 0;
log_message(&w.log, "The war has ended. Peace returns to the land.", LOG_IMPORTANT);
}
if (w.pending_event == EVENT_FIGHT)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_FIGHT, &ui_state.fight.resolved);
else if (w.pending_event == EVENT_VOMIT)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_VOMIT, &ui_state.vomit.resolved);
else if (w.pending_event == EVENT_STEAL)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_STEAL, &ui_state.steal.resolved);
else if (w.pending_event == EVENT_CAT_TROUBLE)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_CAT_TROUBLE, &ui_state.cat_trouble.resolved);
else if (w.pending_event == EVENT_WAR) {
ui_state.war.our_kingdom_attack = k->our_kingdom_attack;
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_WAR, &ui_state.war.resolved);
} else if (w.pending_event == EVENT_WAR_SOLDIERS)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_WAR_SOLDIERS, &ui_state.war_soldiers.resolved);
else if (w.pending_event == EVENT_WAR_REFUGEES)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_WAR_REFUGEES, &ui_state.war_refugees.resolved);
else if (w.pending_event == EVENT_WAR_ATTACK)
event_handler(b, t, k, &w, &ui_state, actions_per_day, UI_MODE_WAR_ATTACK, &ui_state.war_attack.resolved);
save_game(SAVE_PATH, &w);
total_wine = b->drinks[DRINK_WINE_APPLE].inventory.amount + b->drinks[DRINK_WINE_GRAPE].inventory.amount;
tavern_snprintf(buf_l, sizeof(buf_l),
"End of day %d: %d sales | Money: $%.2f | Ale: %d | Wine: %d | Rep: %.2f",
w.day, sales, b->money, b->drinks[DRINK_ALE].inventory.amount,
total_wine, b->reputation);
log_message(&w.log, buf_l, LOG_IMPORTANT);
}
world_kingdoms_free(&w);
endwin();
return 0;
}