home · contact · privacy
Actually save player's hitpoints to savefile (instead of, bug, resetting them on...
[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   fputc(world->player->hitpoints, file);
76   write_map_objects (world->monster, file, write_map_objects_monsterdata);
77   write_map_objects (world->item, file, readwrite_map_objects_dummy);
78   fclose(file); }
79
80 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
81 // Toggle display of window win.
82   if (0 != win->frame.curses_win)
83     suspend_win(win_meta, win);
84   else
85     append_win(win_meta, win); }
86
87 void scroll_pad (struct WinMeta * win_meta, char dir) {
88 // Try to scroll pad left or right.
89   if      ('+' == dir)
90     reset_pad_offset(win_meta, win_meta->pad_offset + 1);
91   else if ('-' == dir)
92     reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
93
94 void growshrink_active_window (struct WinMeta * win_meta, char change) {
95 // Grow or shrink active window horizontally or vertically by one cell size.
96   if (0 != win_meta->active) {
97     struct yx_uint16 size = win_meta->active->frame.size;
98     if      (change == '-') size.y--;
99     else if (change == '+') size.y++;
100     else if (change == '_') size.x--;
101     else if (change == '*') size.x++;
102     resize_active_win (win_meta, size); } }
103
104 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
105                         struct Win * win_map, struct Win * win_info, struct Win * win_log) {
106 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
107   if (key == get_action_key(world->keybindings, "quit"))
108     return 1;
109   else if (key == get_action_key(world->keybindings, "scroll pad right"))
110     scroll_pad (win_meta, '+');
111   else if (key == get_action_key(world->keybindings, "scroll pad left"))
112     scroll_pad (win_meta, '-');
113   else if (key == get_action_key(world->keybindings, "toggle keys window"))
114     toggle_window(win_meta, win_keys);
115   else if (key == get_action_key(world->keybindings, "toggle map window"))
116     toggle_window(win_meta, win_map);
117   else if (key == get_action_key(world->keybindings, "toggle info window"))
118     toggle_window(win_meta, win_info);
119   else if (key == get_action_key(world->keybindings, "toggle log window"))
120     toggle_window(win_meta, win_log);
121   else if (key == get_action_key(world->keybindings, "cycle forwards"))
122     cycle_active_win(win_meta, 'n');
123   else if (key == get_action_key(world->keybindings, "cycle backwards"))
124     cycle_active_win(win_meta, 'p');
125   else if (key == get_action_key(world->keybindings, "shift forwards"))
126     shift_active_win(win_meta, 'f');
127   else if (key == get_action_key(world->keybindings, "shift backwards"))
128     shift_active_win(win_meta, 'b');
129   else if (key == get_action_key(world->keybindings, "grow horizontally"))
130     growshrink_active_window(win_meta, '*');
131   else if (key == get_action_key(world->keybindings, "shrink horizontally"))
132     growshrink_active_window(win_meta, '_');
133   else if (key == get_action_key(world->keybindings, "grow vertically"))
134     growshrink_active_window(win_meta, '+');
135   else if (key == get_action_key(world->keybindings, "shrink vertically"))
136     growshrink_active_window(win_meta, '-');
137   else if (key == get_action_key(world->keybindings, "save keys"))
138     save_keybindings(world);
139   else if (key == get_action_key(world->keybindings, "keys nav up"))
140     keyswin_move_selection (world, 'u');
141   else if (key == get_action_key(world->keybindings, "keys nav down"))
142     keyswin_move_selection (world, 'd');
143   else if (key == get_action_key(world->keybindings, "keys mod"))
144     keyswin_mod_key (world, win_meta);
145   else if (key == get_action_key(world->keybindings, "map up"))
146     map_scroll (world->map, NORTH, win_map->frame.size);
147   else if (key == get_action_key(world->keybindings, "map down"))
148     map_scroll (world->map, SOUTH, win_map->frame.size);
149   else if (key == get_action_key(world->keybindings, "map right"))
150     map_scroll (world->map, EAST, win_map->frame.size);
151   else if (key == get_action_key(world->keybindings, "map left"))
152     map_scroll (world->map, WEST, win_map->frame.size);
153   else if (key == get_action_key(world->keybindings, "map center player"))
154     map_center_player (world->map, world->player, win_map->frame.size);
155   return 0; }
156
157 int main (int argc, char *argv[]) {
158   struct World world;
159
160   // Read in startup options (i.e. replay option and replay start turn).
161   int opt;
162   uint32_t start_turn;
163   world.interactive = 1;
164   while ((opt = getopt(argc, argv, "s::")) != -1) {
165     switch (opt) {
166       case 's':
167         world.interactive = 0;
168         start_turn = 0;
169         if (optarg)
170           start_turn = atoi(optarg);
171         break;
172       default:
173         exit(EXIT_FAILURE); } }
174
175   // Initialize log, player, monsters and items.
176   world.log = calloc(1, sizeof(char));
177   update_log (&world, " ");
178   struct Player player;
179   player.hitpoints = 5;
180   world.player = &player;
181   world.monster = 0;
182   world.item = 0;
183
184   // For interactive mode, try to load world state from savefile.
185   FILE * file;
186   if (1 == world.interactive && 0 == access("savefile", F_OK)) {
187     file = fopen("savefile", "r");
188     world.seed = read_uint32_bigendian(file);
189     world.turn = read_uint32_bigendian(file);
190     player.pos.y = read_uint16_bigendian(file) - 1;
191     player.pos.x = read_uint16_bigendian(file) - 1;
192     player.hitpoints = fgetc(file);
193     read_map_objects (&world.monster, file, sizeof(struct Monster), read_map_objects_monsterdata);
194     read_map_objects (&world.item,    file, sizeof(struct Item),    readwrite_map_objects_dummy);
195     fclose(file); }
196
197   // For non-interactive mode, try to load world state from record file.
198   else {
199     world.turn = 1;
200     if (0 == world.interactive) {
201       file = fopen("record", "r");
202       world.seed = read_uint32_bigendian(file); }
203
204     // For interactive-mode in newly started world, generate a start seed from the current time.
205     else {
206       file = fopen("record", "w");
207       world.seed = time(NULL);
208       write_uint32_bigendian(world.seed, file);
209       fclose(file); } }
210
211   // Generate map from seed and, if newly generated world, start positions of actors.
212   rrand(1, world.seed);
213   struct Map map = init_map();
214   world.map = &map;
215   if (1 == world.turn) {
216     player.pos = find_passable_pos(&map);
217     unsigned char n_monsters = rrand(0, 0) % 16;
218     unsigned char n_items    = rrand(0, 0) % 48;
219     build_map_objects (&world.monster, n_monsters, sizeof(struct Monster), build_map_objects_monsterdata, &map);
220     build_map_objects (&world.item,    n_items,    sizeof(struct Item),    build_map_objects_itemdata,    &map); }
221
222   // Initialize window system and windows.
223   WINDOW * screen = initscr();
224   noecho();
225   curs_set(0);
226   keypad(screen, TRUE);
227   raw();
228   init_keybindings(&world);
229   struct WinMeta win_meta = init_win_meta(screen);
230   struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
231   struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
232   struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
233   struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
234   win_keys.frame.size.x = 29;
235   win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
236   win_info.frame.size.y = 2;
237   win_log.frame.size.y = win_meta.pad.size.y - (2 + win_info.frame.size.y);
238   toggle_window(&win_meta, &win_keys);
239   toggle_window(&win_meta, &win_map);
240   toggle_window(&win_meta, &win_info);
241   toggle_window(&win_meta, &win_log);
242
243   // Replay mode.
244   int key;
245   unsigned char quit_called = 0;
246   unsigned char await_actions = 1;
247   if (0 == world.interactive) {
248     int action;
249     while (1) {
250       if (start_turn == world.turn)
251         start_turn = 0;
252       if (0 == start_turn) {
253         draw_all_wins (&win_meta);
254         key = getch(); }
255       if (1 == await_actions &&
256           (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
257         action = getc(file);
258         if (EOF == action) {
259           start_turn = 0;
260           await_actions = 0; }
261         else if (0 == action)
262           player_wait (&world);
263         else if (NORTH == action)
264           move_player(&world, NORTH);
265         else if (EAST  == action)
266           move_player(&world, EAST);
267         else if (SOUTH == action)
268           move_player(&world, SOUTH);
269         else if (WEST == action)
270           move_player(&world, WEST); }
271       else
272         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
273         if (1 == quit_called)
274           break; } }
275
276   // Interactive mode.
277   else {
278     uint32_t last_turn = 0;
279     while (1) {
280       if (last_turn != world.turn) {
281         save_game(&world);
282         last_turn = world.turn; }
283       if (1 == await_actions && 0 == player.hitpoints)
284         await_actions = 0;
285       draw_all_wins (&win_meta);
286       key = getch();
287       if      (1 == await_actions && key == get_action_key(world.keybindings, "player up"))
288         move_player(&world, NORTH);
289       else if (1 == await_actions && key == get_action_key(world.keybindings, "player right"))
290         move_player(&world, EAST);
291       else if (1 == await_actions && key == get_action_key(world.keybindings, "player down"))
292         move_player(&world, SOUTH);
293       else if (1 == await_actions && key == get_action_key(world.keybindings, "player left"))
294         move_player(&world, WEST);
295       else if (1 == await_actions && key == get_action_key(world.keybindings, "wait / next turn"))
296         player_wait (&world);
297       else
298         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
299         if (1 == quit_called)
300           break; } }
301
302   // Clean up and exit.
303   free(map.cells);
304   for (key = 0; key <= world.keyswindata->max; key++)
305     free(world.keybindings[key].name);
306   free(world.keybindings);
307   free(world.keyswindata);
308   free(world.log);
309   endwin();
310   return 0; }