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