home · contact · privacy
Moved map-specific functions into map.h / map.c library.
[plomrogue] / src / roguelike.c
1 #include "roguelike.h"
2 #include <stdlib.h>
3 #include <ncurses.h>
4 #include <string.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include "windows.h"
8 #include "draw_wins.h"
9 #include "keybindings.h"
10 #include "readwrite.h"
11 #include "objects_on_map.h"
12 #include "map.h"
13
14 uint16_t rrand(char use_seed, uint32_t new_seed) {
15 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
16   static uint32_t seed;
17   if (0 != use_seed)
18     seed = new_seed;
19   seed = ((seed * 1103515245) + 12345) % 2147483648;   // Values as recommended by POSIX.1-2001 (see rand(3)).
20   return (seed / 65536); }                         // Ignore least significant 16 bits (they are less random).
21
22 void update_log (struct World * world, char * text) {
23 // Update log by appending text, or by appending a "." if text is the same as the last one.
24   static char * last_msg;
25   if (0 == last_msg)
26     last_msg = calloc(1, sizeof(char));
27   char * new_text;
28   uint16_t len_old = strlen(world->log);
29   if (0 == strcmp(last_msg, text)) {
30     uint16_t len_whole = len_old + 1;
31     new_text = calloc(len_whole + 1, sizeof(char));
32     memcpy(new_text, world->log, len_old);
33     memcpy(new_text + len_old, ".", 1); }
34   else {
35     uint16_t len_new = strlen(text);
36     uint16_t len_whole = len_old + len_new + 1;
37     new_text = calloc(len_whole, sizeof(char));
38     memcpy(new_text, world->log, len_old);
39     memcpy(new_text + len_old, text, len_new);
40     last_msg = calloc(len_new + 1, sizeof(char));
41     memcpy(last_msg, text, len_new); }
42   free(world->log);
43   world->log = new_text; }
44
45 uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
46 // Return the offset for display of a map inside a frame centered on pos.
47   uint16_t offset = 0;
48   if (mapsize > framesize) {
49     if (pos > framesize / 2) {
50       if (pos < mapsize - (framesize / 2))
51         offset = pos - (framesize / 2);
52       else
53         offset = mapsize - framesize; } }
54   return offset; }
55
56 void turn_over (struct World * world, char action) {
57 // Record action in game record file, increment turn and move enemy.
58   if (1 == world->interactive) {
59     FILE * file = fopen("record", "a");
60     fputc(action, file);
61     fclose(file); }
62   world->turn++;
63   rrand(1, world->seed * world->turn);
64   struct Monster * monster;
65   for (monster = world->monster; monster != 0; monster = monster->cmo.next)
66     move_monster(world, monster); }
67
68 void save_game(struct World * world) {
69 // Save game data to game file.
70   FILE * file = fopen("savefile", "w");
71   write_uint32_bigendian(world->seed, file);
72   write_uint32_bigendian(world->turn, file);
73   write_uint16_bigendian(world->player->pos.y + 1, file);
74   write_uint16_bigendian(world->player->pos.x + 1, file);
75   write_map_objects (world->monster, file, write_map_objects_monsterdata);
76   write_map_objects (world->item, file, readwrite_map_objects_dummy);
77   fclose(file); }
78
79 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
80 // Toggle display of window win.
81   if (0 != win->frame.curses_win)
82     suspend_win(win_meta, win);
83   else
84     append_win(win_meta, win); }
85
86 void scroll_pad (struct WinMeta * win_meta, char dir) {
87 // Try to scroll pad left or right.
88   if      ('+' == dir)
89     reset_pad_offset(win_meta, win_meta->pad_offset + 1);
90   else if ('-' == dir)
91     reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
92
93 void growshrink_active_window (struct WinMeta * win_meta, char change) {
94 // Grow or shrink active window horizontally or vertically by one cell size.
95   if (0 != win_meta->active) {
96     struct yx_uint16 size = win_meta->active->frame.size;
97     if      (change == '-') size.y--;
98     else if (change == '+') size.y++;
99     else if (change == '_') size.x--;
100     else if (change == '*') size.x++;
101     resize_active_win (win_meta, size); } }
102
103 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
104                         struct Win * win_map, struct Win * win_info, struct Win * win_log) {
105 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
106   if (key == get_action_key(world->keybindings, "quit"))
107     return 1;
108   else if (key == get_action_key(world->keybindings, "scroll pad right"))
109     scroll_pad (win_meta, '+');
110   else if (key == get_action_key(world->keybindings, "scroll pad left"))
111     scroll_pad (win_meta, '-');
112   else if (key == get_action_key(world->keybindings, "toggle keys window"))
113     toggle_window(win_meta, win_keys);
114   else if (key == get_action_key(world->keybindings, "toggle map window"))
115     toggle_window(win_meta, win_map);
116   else if (key == get_action_key(world->keybindings, "toggle info window"))
117     toggle_window(win_meta, win_info);
118   else if (key == get_action_key(world->keybindings, "toggle log window"))
119     toggle_window(win_meta, win_log);
120   else if (key == get_action_key(world->keybindings, "cycle forwards"))
121     cycle_active_win(win_meta, 'n');
122   else if (key == get_action_key(world->keybindings, "cycle backwards"))
123     cycle_active_win(win_meta, 'p');
124   else if (key == get_action_key(world->keybindings, "shift forwards"))
125     shift_active_win(win_meta, 'f');
126   else if (key == get_action_key(world->keybindings, "shift backwards"))
127     shift_active_win(win_meta, 'b');
128   else if (key == get_action_key(world->keybindings, "grow horizontally"))
129     growshrink_active_window(win_meta, '*');
130   else if (key == get_action_key(world->keybindings, "shrink horizontally"))
131     growshrink_active_window(win_meta, '_');
132   else if (key == get_action_key(world->keybindings, "grow vertically"))
133     growshrink_active_window(win_meta, '+');
134   else if (key == get_action_key(world->keybindings, "shrink vertically"))
135     growshrink_active_window(win_meta, '-');
136   else if (key == get_action_key(world->keybindings, "save keys"))
137     save_keybindings(world);
138   else if (key == get_action_key(world->keybindings, "keys nav up"))
139     keyswin_move_selection (world, 'u');
140   else if (key == get_action_key(world->keybindings, "keys nav down"))
141     keyswin_move_selection (world, 'd');
142   else if (key == get_action_key(world->keybindings, "keys mod"))
143     keyswin_mod_key (world, win_meta);
144   else if (key == get_action_key(world->keybindings, "map up"))
145     map_scroll (world->map, NORTH, win_map->frame.size);
146   else if (key == get_action_key(world->keybindings, "map down"))
147     map_scroll (world->map, SOUTH, win_map->frame.size);
148   else if (key == get_action_key(world->keybindings, "map right"))
149     map_scroll (world->map, EAST, win_map->frame.size);
150   else if (key == get_action_key(world->keybindings, "map left"))
151     map_scroll (world->map, WEST, win_map->frame.size);
152   else if (key == get_action_key(world->keybindings, "map center player"))
153     map_center_player (world->map, world->player, win_map->frame.size);
154   return 0; }
155
156 int main (int argc, char *argv[]) {
157   struct World world;
158
159   // Read in startup options (i.e. replay option and replay start turn).
160   int opt;
161   uint32_t start_turn;
162   world.interactive = 1;
163   while ((opt = getopt(argc, argv, "s::")) != -1) {
164     switch (opt) {
165       case 's':
166         world.interactive = 0;
167         start_turn = 0;
168         if (optarg)
169           start_turn = atoi(optarg);
170         break;
171       default:
172         exit(EXIT_FAILURE); } }
173
174   // Initialize log, player, monsters and items.
175   world.log = calloc(1, sizeof(char));
176   update_log (&world, " ");
177   struct Player player;
178   player.hitpoints = 5;
179   world.player = &player;
180   world.monster = 0;
181   world.item = 0;
182
183   // For interactive mode, try to load world state from savefile.
184   FILE * file;
185   if (1 == world.interactive && 0 == access("savefile", F_OK)) {
186     file = fopen("savefile", "r");
187     world.seed = read_uint32_bigendian(file);
188     world.turn = read_uint32_bigendian(file);
189     player.pos.y = read_uint16_bigendian(file) - 1;
190     player.pos.x = read_uint16_bigendian(file) - 1;
191     read_map_objects (&world.monster, file, sizeof(struct Monster), read_map_objects_monsterdata);
192     read_map_objects (&world.item,    file, sizeof(struct Item),    readwrite_map_objects_dummy);
193     fclose(file); }
194
195   // For non-interactive mode, try to load world state from record file.
196   else {
197     world.turn = 1;
198     if (0 == world.interactive) {
199       file = fopen("record", "r");
200       world.seed = read_uint32_bigendian(file); }
201
202     // For interactive-mode in newly started world, generate a start seed from the current time.
203     else {
204       file = fopen("record", "w");
205       world.seed = time(NULL);
206       write_uint32_bigendian(world.seed, file);
207       fclose(file); } }
208
209   // Generate map from seed and, if newly generated world, start positions of actors.
210   rrand(1, world.seed);
211   struct Map map = init_map();
212   world.map = &map;
213   if (1 == world.turn) {
214     player.pos = find_passable_pos(&map);
215     unsigned char n_monsters = rrand(0, 0) % 16;
216     unsigned char n_items    = rrand(0, 0) % 48;
217     build_map_objects (&world.monster, n_monsters, sizeof(struct Monster), build_map_objects_monsterdata, &map);
218     build_map_objects (&world.item,    n_items,    sizeof(struct Item),    build_map_objects_itemdata,    &map); }
219
220   // Initialize window system and windows.
221   WINDOW * screen = initscr();
222   noecho();
223   curs_set(0);
224   keypad(screen, TRUE);
225   raw();
226   init_keybindings(&world);
227   struct WinMeta win_meta = init_win_meta(screen);
228   struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
229   struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
230   struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
231   struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
232   win_keys.frame.size.x = 29;
233   win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
234   win_info.frame.size.y = 2;
235   win_log.frame.size.y = win_meta.pad.size.y - (2 + win_info.frame.size.y);
236   toggle_window(&win_meta, &win_keys);
237   toggle_window(&win_meta, &win_map);
238   toggle_window(&win_meta, &win_info);
239   toggle_window(&win_meta, &win_log);
240
241   // Replay mode.
242   int key;
243   unsigned char quit_called = 0;
244   unsigned char await_actions = 1;
245   if (0 == world.interactive) {
246     int action;
247     while (1) {
248       if (start_turn == world.turn)
249         start_turn = 0;
250       if (0 == start_turn) {
251         draw_all_wins (&win_meta);
252         key = getch(); }
253       if (1 == await_actions &&
254           (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
255         action = getc(file);
256         if (EOF == action) {
257           start_turn = 0;
258           await_actions = 0; }
259         else if (0 == action)
260           player_wait (&world);
261         else if (NORTH == action)
262           move_player(&world, NORTH);
263         else if (EAST  == action)
264           move_player(&world, EAST);
265         else if (SOUTH == action)
266           move_player(&world, SOUTH);
267         else if (WEST == action)
268           move_player(&world, WEST); }
269       else
270         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
271         if (1 == quit_called)
272           break; } }
273
274   // Interactive mode.
275   else {
276     uint32_t last_turn = 0;
277     while (1) {
278       if (last_turn != world.turn) {
279         save_game(&world);
280         last_turn = world.turn; }
281       if (1 == await_actions && 0 == player.hitpoints)
282         await_actions = 0;
283       draw_all_wins (&win_meta);
284       key = getch();
285       if      (1 == await_actions && key == get_action_key(world.keybindings, "player up"))
286         move_player(&world, NORTH);
287       else if (1 == await_actions && key == get_action_key(world.keybindings, "player right"))
288         move_player(&world, EAST);
289       else if (1 == await_actions && key == get_action_key(world.keybindings, "player down"))
290         move_player(&world, SOUTH);
291       else if (1 == await_actions && key == get_action_key(world.keybindings, "player left"))
292         move_player(&world, WEST);
293       else if (1 == await_actions && key == get_action_key(world.keybindings, "wait / next turn"))
294         player_wait (&world);
295       else
296         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
297         if (1 == quit_called)
298           break; } }
299
300   // Clean up and exit.
301   free(map.cells);
302   for (key = 0; key <= world.keyswindata->max; key++)
303     free(world.keybindings[key].name);
304   free(world.keybindings);
305   free(world.keyswindata);
306   free(world.log);
307   endwin();
308   return 0; }