home · contact · privacy
Enemies have hitpoints (5 at start), disappear when those reach 0.
[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 "objects_on_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 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 + 1, file);
98   write_uint16_bigendian(world->player->pos.x + 1, file);
99   struct Monster * monster;
100   for (monster = world->monster; monster != 0; monster = monster->next) {
101     write_uint16_bigendian(monster->pos.y + 1, file);
102     write_uint16_bigendian(monster->pos.x + 1, file);
103     fputc(monster->name, file);
104     fputc(monster->hitpoints, file); }
105   write_uint16_bigendian(0, file);
106   struct Item * item;
107   for (item = world->item; item != 0; item = item->next) {
108     write_uint16_bigendian(item->pos.y + 1, file);
109     write_uint16_bigendian(item->pos.x + 1, file);
110     fputc(item->name, file); }
111   write_uint16_bigendian(0, file);
112   fclose(file); }
113
114 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
115 // Toggle display of window win.
116   if (0 != win->frame.curses_win)
117     suspend_win(win_meta, win);
118   else
119     append_win(win_meta, win); }
120
121 void scroll_pad (struct WinMeta * win_meta, char dir) {
122 // Try to scroll pad left or right.
123   if      ('+' == dir)
124     reset_pad_offset(win_meta, win_meta->pad_offset + 1);
125   else if ('-' == dir)
126     reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
127
128 void growshrink_active_window (struct WinMeta * win_meta, char change) {
129 // Grow or shrink active window horizontally or vertically by one cell size.
130   if (0 != win_meta->active) {
131     struct yx_uint16 size = win_meta->active->frame.size;
132     if      (change == '-') size.y--;
133     else if (change == '+') size.y++;
134     else if (change == '_') size.x--;
135     else if (change == '*') size.x++;
136     resize_active_win (win_meta, size); } }
137
138 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
139                         struct Win * win_map, struct Win * win_info, struct Win * win_log) {
140 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
141   if (key == get_action_key(world->keybindings, "quit"))
142     return 1;
143   else if (key == get_action_key(world->keybindings, "scroll pad right"))
144     scroll_pad (win_meta, '+');
145   else if (key == get_action_key(world->keybindings, "scroll pad left"))
146     scroll_pad (win_meta, '-');
147   else if (key == get_action_key(world->keybindings, "toggle keys window"))
148     toggle_window(win_meta, win_keys);
149   else if (key == get_action_key(world->keybindings, "toggle map window"))
150     toggle_window(win_meta, win_map);
151   else if (key == get_action_key(world->keybindings, "toggle info window"))
152     toggle_window(win_meta, win_info);
153   else if (key == get_action_key(world->keybindings, "toggle log window"))
154     toggle_window(win_meta, win_log);
155   else if (key == get_action_key(world->keybindings, "cycle forwards"))
156     cycle_active_win(win_meta, 'n');
157   else if (key == get_action_key(world->keybindings, "cycle backwards"))
158     cycle_active_win(win_meta, 'p');
159   else if (key == get_action_key(world->keybindings, "shift forwards"))
160     shift_active_win(win_meta, 'f');
161   else if (key == get_action_key(world->keybindings, "shift backwards"))
162     shift_active_win(win_meta, 'b');
163   else if (key == get_action_key(world->keybindings, "grow horizontally"))
164     growshrink_active_window(win_meta, '*');
165   else if (key == get_action_key(world->keybindings, "shrink horizontally"))
166     growshrink_active_window(win_meta, '_');
167   else if (key == get_action_key(world->keybindings, "grow vertically"))
168     growshrink_active_window(win_meta, '+');
169   else if (key == get_action_key(world->keybindings, "shrink vertically"))
170     growshrink_active_window(win_meta, '-');
171   else if (key == get_action_key(world->keybindings, "save keys"))
172     save_keybindings(world);
173   else if (key == get_action_key(world->keybindings, "keys nav up"))
174     keyswin_move_selection (world, 'u');
175   else if (key == get_action_key(world->keybindings, "keys nav down"))
176     keyswin_move_selection (world, 'd');
177   else if (key == get_action_key(world->keybindings, "keys mod"))
178     keyswin_mod_key (world, win_meta);
179   else if (key == get_action_key(world->keybindings, "map up"))
180     map_scroll (world->map, NORTH);
181   else if (key == get_action_key(world->keybindings, "map down"))
182     map_scroll (world->map, SOUTH);
183   else if (key == get_action_key(world->keybindings, "map right"))
184     map_scroll (world->map, EAST);
185   else if (key == get_action_key(world->keybindings, "map left"))
186     map_scroll (world->map, WEST);
187   return 0; }
188
189 int main (int argc, char *argv[]) {
190   struct World world;
191
192   // Read in startup options (i.e. replay option and replay start turn).
193   int opt;
194   uint32_t start_turn;
195   world.interactive = 1;
196   while ((opt = getopt(argc, argv, "s::")) != -1) {
197     switch (opt) {
198       case 's':
199         world.interactive = 0;
200         start_turn = 0;
201         if (optarg)
202           start_turn = atoi(optarg);
203         break;
204       default:
205         exit(EXIT_FAILURE); } }
206
207   // Initialize log, player, monsters and items.
208   world.log = calloc(1, sizeof(char));
209   update_log (&world, " ");
210   struct Player player;
211   world.player = &player;
212   struct Monster * monster;
213   struct Item * item;
214   uint16_t test;
215   char start;
216
217   // For interactive mode, try to load world state from savefile.
218   FILE * file;
219   if (1 == world.interactive && 0 == access("savefile", F_OK)) {
220     file = fopen("savefile", "r");
221     world.seed = read_uint32_bigendian(file);
222     world.turn = read_uint32_bigendian(file);
223     player.pos.y = read_uint16_bigendian(file) - 1;
224     player.pos.x = read_uint16_bigendian(file) - 1;
225     start = 1;
226     world.monster = 0;
227     while (1) {
228       test = read_uint16_bigendian(file);
229       if (0 == test)
230         break;
231       if (start) {
232         monster = malloc(sizeof(struct Monster));
233         world.monster = monster;
234         start = 0; }
235       else {
236         monster->next = malloc(sizeof(struct Monster));
237         monster = monster->next; }
238       monster->pos.y = test - 1;
239       monster->pos.x = read_uint16_bigendian(file) - 1;
240       monster->name = fgetc(file);
241       monster->hitpoints = fgetc(file); }
242     if (!start)
243       monster->next = 0;
244     start = 1;
245     world.item = 0;
246     while (1) {
247       test = read_uint16_bigendian(file);
248       if (0 == test)
249         break;
250       if (start) {
251         item = malloc(sizeof(struct Item));
252         world.item = item;
253         start = 0; }
254       else {
255         item->next = malloc(sizeof(struct Item));
256         item = item->next; }
257       item->pos.y = test - 1;
258       item->pos.x = read_uint16_bigendian(file) - 1;
259       item->name = fgetc(file); }
260     if (!start)
261       item->next = 0;
262     fclose(file); }
263
264   // For non-interactive mode, try to load world state from record file.
265   else {
266     world.turn = 1;
267     if (0 == world.interactive) {
268       file = fopen("record", "r");
269       world.seed = read_uint32_bigendian(file); }
270
271     // For interactive-mode in newly started world, generate a start seed from the current time.
272     else {
273       file = fopen("record", "w");
274       world.seed = time(NULL);
275       write_uint32_bigendian(world.seed, file);
276       fclose(file); } }
277
278   // Generate map from seed and, if newly generated world, start positions of actors.
279   rrand(1, world.seed);
280   struct Map map = init_map();
281   world.map = &map;
282   if (1 == world.turn) {
283     player.pos = find_passable_pos(&map);
284     unsigned char n_monsters = rrand(0, 0) % 16;
285     unsigned char n_items    = rrand(0, 0) % 48;
286     unsigned char i;
287     start = 1;
288     world.monster = 0;
289     for (i = 0; i < n_monsters; i++) {
290       if (start) {
291         monster = malloc(sizeof(struct Monster));
292         world.monster = monster;
293         start = 0; }
294       else {
295         monster->next = malloc(sizeof(struct Monster));
296         monster = monster->next; }
297       monster->pos = find_passable_pos(&map);
298       monster->name = 'A' + (rrand(0, 0) % 8);
299       monster->hitpoints = 5; }
300     if (!start)
301       monster->next = 0;
302     start = 1;
303     world.item = 0;
304     for (i = 0; i < n_items; i++) {
305       if (start) {
306         item = malloc(sizeof(struct Item));
307         world.item = item;
308         start = 0; }
309       else {
310         item->next = malloc(sizeof(struct Item));
311         item = item->next; }
312       item->pos = find_passable_pos(&map);
313       item->name = '#' + (rrand(0, 0) % 4); }
314     if (!start)
315       item->next = 0; }
316
317   // Initialize window system and windows.
318   WINDOW * screen = initscr();
319   noecho();
320   curs_set(0);
321   keypad(screen, TRUE);
322   raw();
323   init_keybindings(&world);
324   struct WinMeta win_meta = init_win_meta(screen);
325   struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
326   struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
327   struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
328   struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
329   win_keys.frame.size.x = 29;
330   win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
331   win_info.frame.size.y = 1;
332   win_log.frame.size.y = win_meta.pad.size.y - 3;
333   toggle_window(&win_meta, &win_keys);
334   toggle_window(&win_meta, &win_map);
335   toggle_window(&win_meta, &win_info);
336   toggle_window(&win_meta, &win_log);
337
338   // Replay mode.
339   int key;
340   unsigned char quit_called = 0;
341   if (0 == world.interactive) {
342     unsigned char still_reading_file = 1;
343     int action;
344     while (1) {
345       if (start_turn == world.turn)
346         start_turn = 0;
347       if (0 == start_turn) {
348         draw_all_wins (&win_meta);
349         key = getch(); }
350       if (1 == still_reading_file &&
351           (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
352         action = getc(file);
353         if (EOF == action) {
354           start_turn = 0;
355           still_reading_file = 0; }
356         else if (0 == action)
357           player_wait (&world);
358         else if (NORTH == action)
359           move_player(&world, NORTH);
360         else if (EAST  == action)
361           move_player(&world, EAST);
362         else if (SOUTH == action)
363           move_player(&world, SOUTH);
364         else if (WEST == action)
365           move_player(&world, WEST); }
366       else
367         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
368         if (1 == quit_called)
369           break; } }
370
371   // Interactive mode.
372   else {
373     uint32_t last_turn = 0;
374     while (1) {
375       if (last_turn != world.turn) {
376         save_game(&world);
377         last_turn = world.turn; }
378       draw_all_wins (&win_meta);
379       key = getch();
380       if      (key == get_action_key(world.keybindings, "player up"))
381         move_player(&world, NORTH);
382       else if (key == get_action_key(world.keybindings, "player right"))
383         move_player(&world, EAST);
384       else if (key == get_action_key(world.keybindings, "player down"))
385         move_player(&world, SOUTH);
386       else if (key == get_action_key(world.keybindings, "player left"))
387         move_player(&world, WEST);
388       else if (key == get_action_key(world.keybindings, "wait / next turn"))
389         player_wait (&world);
390       else
391         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
392         if (1 == quit_called)
393           break; } }
394
395   // Clean up and exit.
396   free(map.cells);
397   for (key = 0; key <= world.keyswindata->max; key++)
398     free(world.keybindings[key].name);
399   free(world.keybindings);
400   free(world.keyswindata);
401   free(world.log);
402   endwin();
403   return 0; }