home · contact · privacy
Sorted out library dependencies / includes. Include every header file needed by each...
[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     uint16_t height = win_meta->active->frame.size.y;
114     uint16_t width = win_meta->active->frame.size.x;
115     if      (change == '-')
116       height--;
117     else if (change == '+')
118       height++;
119     else if (change == '_')
120       width--;
121     else if (change == '*')
122       width++;
123     resize_active_win (win_meta, height, width); } }
124
125 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
126                         struct Win * win_map, struct Win * win_info, struct Win * win_log) {
127 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
128   if (key == get_action_key(world->keybindings, "quit"))
129     return 1;
130   else if (key == get_action_key(world->keybindings, "scroll pad right"))
131     scroll_pad (win_meta, '+');
132   else if (key == get_action_key(world->keybindings, "scroll pad left"))
133     scroll_pad (win_meta, '-');
134   else if (key == get_action_key(world->keybindings, "toggle keys window"))
135     toggle_window(win_meta, win_keys);
136   else if (key == get_action_key(world->keybindings, "toggle map window"))
137     toggle_window(win_meta, win_map);
138   else if (key == get_action_key(world->keybindings, "toggle info window"))
139     toggle_window(win_meta, win_info);
140   else if (key == get_action_key(world->keybindings, "toggle log window"))
141     toggle_window(win_meta, win_log);
142   else if (key == get_action_key(world->keybindings, "cycle forwards"))
143     cycle_active_win(win_meta, 'n');
144   else if (key == get_action_key(world->keybindings, "cycle backwards"))
145     cycle_active_win(win_meta, 'p');
146   else if (key == get_action_key(world->keybindings, "shift forwards"))
147     shift_active_win(win_meta, 'f');
148   else if (key == get_action_key(world->keybindings, "shift backwards"))
149     shift_active_win(win_meta, 'b');
150   else if (key == get_action_key(world->keybindings, "grow horizontally"))
151     growshrink_active_window(win_meta, '*');
152   else if (key == get_action_key(world->keybindings, "shrink horizontally"))
153     growshrink_active_window(win_meta, '_');
154   else if (key == get_action_key(world->keybindings, "grow vertically"))
155     growshrink_active_window(win_meta, '+');
156   else if (key == get_action_key(world->keybindings, "shrink vertically"))
157     growshrink_active_window(win_meta, '-');
158   else if (key == get_action_key(world->keybindings, "save keys"))
159     save_keybindings(world);
160   else if (key == get_action_key(world->keybindings, "keys nav up"))
161     keyswin_move_selection (world, 'u');
162   else if (key == get_action_key(world->keybindings, "keys nav down"))
163     keyswin_move_selection (world, 'd');
164   else if (key == get_action_key(world->keybindings, "keys mod"))
165     keyswin_mod_key (world, win_meta);
166   else if (key == get_action_key(world->keybindings, "map up"))
167     map_scroll (world->map, NORTH);
168   else if (key == get_action_key(world->keybindings, "map down"))
169     map_scroll (world->map, SOUTH);
170   else if (key == get_action_key(world->keybindings, "map right"))
171     map_scroll (world->map, EAST);
172   else if (key == get_action_key(world->keybindings, "map left"))
173     map_scroll (world->map, WEST);
174   return 0; }
175
176 int main (int argc, char *argv[]) {
177   struct World world;
178
179   // Read in startup options (i.e. replay option and replay start turn).
180   int opt;
181   uint32_t start_turn;
182   world.interactive = 1;
183   while ((opt = getopt(argc, argv, "s::")) != -1) {
184     switch (opt) {
185       case 's':
186         world.interactive = 0;
187         start_turn = 0;
188         if (optarg)
189           start_turn = atoi(optarg);
190         break;
191       default:
192         exit(EXIT_FAILURE); } }
193
194   // Initialize log, player and monsters.
195   world.log = calloc(1, sizeof(char));
196   update_log (&world, " ");
197   struct Player player;
198   world.player = &player;
199   struct Monster monster1;
200   struct Monster monster2;
201   struct Monster monster3;
202   world.monster = &monster1;
203   monster1.next = &monster2;
204   monster2.next = &monster3;
205   monster3.next = 0;
206   monster1.name = 'A';
207   monster2.name = 'B';
208   monster3.name = 'C';
209
210   // For interactive mode, try to load world state from savefile.
211   FILE * file;
212   if (1 == world.interactive && 0 == access("savefile", F_OK)) {
213     file = fopen("savefile", "r");
214     world.seed = read_uint32_bigendian(file);
215     world.turn = read_uint32_bigendian(file);
216     player.pos.y = read_uint16_bigendian(file);
217     player.pos.x = read_uint16_bigendian(file);
218     monster1.pos.y = read_uint16_bigendian(file);
219     monster1.pos.x = read_uint16_bigendian(file);
220     monster2.pos.y = read_uint16_bigendian(file);
221     monster2.pos.x = read_uint16_bigendian(file);
222     monster3.pos.y = read_uint16_bigendian(file);
223     monster3.pos.x = read_uint16_bigendian(file);
224     fclose(file); }
225
226   // For non-interactive mode, try to load world state from frecord file.
227   else {
228     world.turn = 1;
229     if (0 == world.interactive) {
230       file = fopen("record", "r");
231       world.seed = read_uint32_bigendian(file); }
232
233     // For interactive-mode in newly started world, generate a start seed from the current time.
234     else {
235       file = fopen("record", "w");
236       world.seed = time(NULL);
237       write_uint32_bigendian(world.seed, file);
238       fclose(file); } }
239
240   // Generate map from seed and, if newly generated world, start positions of actors.
241   rrand(1, world.seed);
242   struct Map map = init_map();
243   world.map = &map;
244   if (1 == world.turn) {
245     for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos.y, player.pos.x);) {
246       player.pos.y = rrand(0, 0) % map.size.y;
247       player.pos.x = rrand(0, 0) % map.size.x; }
248     struct Monster * monster;
249     for (monster = world.monster; monster != 0; monster = monster->next)
250       for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos.y, monster->pos.x);) {
251         monster->pos.y = rrand(0, 0) % map.size.y;
252         monster->pos.x = rrand(0, 0) % map.size.x; } }
253
254   // Initialize window system and windows.
255   WINDOW * screen = initscr();
256   noecho();
257   curs_set(0);
258   keypad(screen, TRUE);
259   raw();
260   init_keybindings(&world);
261   struct WinMeta win_meta = init_win_meta(screen);
262   struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
263   struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
264   struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
265   struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
266   win_keys.frame.size.x = 29;
267   win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
268   win_info.frame.size.y = 1;
269   win_log.frame.size.y = win_meta.pad.size.y - 3;
270   toggle_window(&win_meta, &win_keys);
271   toggle_window(&win_meta, &win_map);
272   toggle_window(&win_meta, &win_info);
273   toggle_window(&win_meta, &win_log);
274
275   // Replay mode.
276   int key;
277   unsigned char quit_called = 0;
278   if (0 == world.interactive) {
279     unsigned char still_reading_file = 1;
280     int action;
281     while (1) {
282       if (start_turn == world.turn)
283         start_turn = 0;
284       if (0 == start_turn) {
285         draw_all_wins (&win_meta);
286         key = getch(); }
287       if (1 == still_reading_file &&
288           (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
289         action = getc(file);
290         if (EOF == action) {
291           start_turn = 0;
292           still_reading_file = 0; }
293         else if (0 == action)
294           player_wait (&world);
295         else if (NORTH == action)
296           move_player(&world, NORTH);
297         else if (EAST  == action)
298           move_player(&world, EAST);
299         else if (SOUTH == action)
300           move_player(&world, SOUTH);
301         else if (WEST == action)
302           move_player(&world, WEST); }
303       else
304         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
305         if (1 == quit_called)
306           break; } }
307
308   // Interactive mode.
309   else {
310     uint32_t last_turn = 0;
311     while (1) {
312       if (last_turn != world.turn) {
313         save_game(&world);
314         last_turn = world.turn; }
315       draw_all_wins (&win_meta);
316       key = getch();
317       if      (key == get_action_key(world.keybindings, "player up"))
318         move_player(&world, NORTH);
319       else if (key == get_action_key(world.keybindings, "player right"))
320         move_player(&world, EAST);
321       else if (key == get_action_key(world.keybindings, "player down"))
322         move_player(&world, SOUTH);
323       else if (key == get_action_key(world.keybindings, "player left"))
324         move_player(&world, WEST);
325       else if (key == get_action_key(world.keybindings, "wait / next turn"))
326         player_wait (&world);
327       else
328         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
329         if (1 == quit_called)
330           break; } }
331
332   // Clean up and exit.
333   free(map.cells);
334   for (key = 0; key <= world.keyswindata->max; key++)
335     free(world.keybindings[key].name);
336   free(world.keybindings);
337   free(world.keyswindata);
338   free(world.log);
339   endwin();
340   return 0; }