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