home · contact · privacy
Always load seed file if it exists. Don't provide a default one.
[plomrogue] / roguelike.c
1 #include <stdlib.h>
2 #include <limits.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 "roguelike.h"
11 #include "keybindings.h"
12
13 uint16_t rrand(char use_seed, uint32_t new_seed) {
14 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
15   static uint32_t seed;
16   if (0 != use_seed)
17     seed = new_seed;
18   seed = ((seed * 1103515245) + 12345) % 2147483648;   // Values as recommended by POSIX.1-2001 (see rand(3)).
19   return (seed / 65536); }                         // Ignore least significant 16 bits (they are less random).
20
21 uint32_t load_seed() {
22 // Load seed integer from seed file.
23   uint32_t seed;
24   const uint16_t nchar = UCHAR_MAX + 1;
25   FILE * file = fopen("seed", "r");
26   unsigned char a = fgetc(file);
27   unsigned char b = fgetc(file);
28   unsigned char c = fgetc(file);
29   unsigned char d = fgetc(file);
30   seed = (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d;
31   fclose(file);
32   return seed; }
33
34 void save_seed(uint32_t seed) {
35 // Save seed integer to seed file.
36   const uint16_t nchar = UCHAR_MAX + 1;
37   unsigned char a = seed / (nchar * nchar * nchar);
38   unsigned char b = (seed - (a * nchar * nchar * nchar)) / (nchar * nchar);
39   unsigned char c = (seed - ((a * nchar * nchar * nchar) + (b * nchar * nchar))) / nchar;
40   unsigned char d = seed % nchar;
41   FILE * file = fopen("seed", "w");
42   fputc(a, file);
43   fputc(b, file);
44   fputc(c, file);
45   fputc(d, file);
46   fclose(file); }
47
48 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
49 // Toggle display of window win.
50   if (0 != win->curses)
51     suspend_window(win_meta, win);
52   else
53     append_window(win_meta, win); }
54
55 void growshrink_active_window (struct WinMeta * win_meta, char change) {
56 // Grow or shrink active window horizontally or vertically by one cell size.
57   if (0 != win_meta->active) {
58     uint16_t height = win_meta->active->height;
59     uint16_t width = win_meta->active->width;
60     if      (change == '-')
61       height--;
62     else if (change == '+')
63       height++;
64     else if (change == '_')
65       width--;
66     else if (change == '*')
67       width++;
68     resize_active_window (win_meta, height, width); } }
69
70 struct Map init_map (uint32_t seed) {
71 // Initialize map with some experimental start values.
72   struct Map map;
73   map.width = 64;
74   map.height = 64;
75   map.offset_x = 0;
76   map.offset_y = 0;
77   map.cells = malloc(map.width * map.height);
78   uint16_t x, y, ran;
79   char terrain;
80   for (y = 0; y < map.height; y++)
81     for (x = 0; x < map.width; x++) {
82       terrain = '.';
83       ran = rrand(0, 0);
84       if (   0 == ran % ((x*x) / 3 + 1)
85           || 0 == ran % ((y*y) / 3 + 1)
86           || 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
87           || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1))
88         terrain = ' ';
89       map.cells[(y * map.width) + x] = terrain; }
90   return map; }
91
92 void map_scroll (struct Map * map, char dir) {
93 // Scroll map into direction dir if possible by changing the offset.
94   if      ('n' == dir && map->offset_y > 0)
95     map->offset_y--;
96   else if ('s' == dir)
97     map->offset_y++;
98   else if ('w' == dir && map->offset_x > 0)
99     map->offset_x--;
100   else if ('e' == dir)
101     map->offset_x++; }
102
103 void next_turn (struct World * world) {
104 // Increment turn and move enemy.
105   world->turn++;
106   char d = rrand(0, 0) % 5;
107   uint16_t ty = world->monster->y;
108   uint16_t tx = world->monster->x;
109   if (1 == d)
110     ty++;
111   else if (2 == d)
112     ty--;
113   else if (3 == d)
114     tx++;
115   else if (4 == d)
116     tx--;
117   if (tx == world->player->x && ty == world->player->y)
118     update_log(world, "\nThe monster hits you.");
119   else if (is_passable(world, tx, ty)) {
120     world->monster->y = ty;
121     world->monster->x = tx; } }
122
123 void update_log (struct World * world, char * text) {
124 // Update log with new text to be appended.
125   char * new_text;
126   uint16_t len_old = strlen(world->log);
127   uint16_t len_new = strlen(text);
128   uint16_t len_whole = len_old + len_new + 1;
129   new_text = calloc(len_whole, sizeof(char));
130   memcpy(new_text, world->log, len_old);
131   memcpy(new_text + len_old, text, len_new);
132   free(world->log);
133   world->log = new_text; }
134
135 char is_passable (struct World * world, uint16_t x, uint16_t y) {
136 // Check if coordinate on (or beyond) map is accessible to movement.
137   char passable = 0;
138   if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
139     if ('.' == world->map->cells[y * world->map->width + x])
140       passable = 1;
141   return passable; }
142
143 void move_player (struct World * world, char d) {
144 // Move player in direction d, increment turn counter and update log.
145   static char prev = 0;
146   char success = 0;
147   char * dir;
148   uint16_t ty = world->player->y;
149   uint16_t tx = world->player->x;
150   if ('s' == d) {
151     dir = "south";
152     ty++; }
153   if ('n' == d) {
154     dir = "north";
155     ty--; }
156   if ('w' == d) {
157     dir = "west";
158     tx--; }
159   if ('e' == d) {
160     dir = "east";
161     tx++; }
162   if (ty == world->monster->y && tx == world->monster->x)
163     success = 2;
164   else if (is_passable(world, tx, ty)) {
165     success = 1;
166     world->player->y = ty;
167     world->player->x = tx; }
168   if (success * d == prev)
169     update_log (world, ".");
170   else {
171     if (2 == success)
172       update_log (world, "\nYou hit the monster.");
173     else {
174       char * msg = calloc(25, sizeof(char));
175       char * msg_content = "You fail to move";
176       if (1 == success)
177         msg_content = "You move";
178       sprintf(msg, "\n%s %s.", msg_content, dir);
179       update_log (world, msg);
180       free(msg); } }
181   prev = success * d;
182   next_turn (world); }
183
184 void player_wait (struct World * world) {
185 // Make player wait one turn.
186   next_turn (world);
187   update_log (world, "\nYou wait."); }
188
189 int main (int argc, char *argv[]) {
190   uint32_t seed;
191   if (0 == access("seed", F_OK))
192     seed = load_seed();
193   else
194     seed = time(NULL);
195   rrand(1, seed);
196
197   struct World world;
198   init_keybindings(&world);
199   world.turn = 0;
200   world.log = calloc(1, sizeof(char));
201   update_log (&world, "Start!");
202   struct Map map = init_map(seed);
203   world.map = &map;
204   struct Player player;
205   player.y = 8;
206   player.x = 8;
207   world.player = &player;
208   struct Monster monster;
209   monster.y = 55;
210   monster.x = 55;
211   world.monster = &monster;
212
213   WINDOW * screen = initscr();
214   noecho();
215   curs_set(0);
216   keypad(screen, TRUE);
217   raw();
218   struct WinMeta win_meta = init_win_meta(screen);
219   struct Win win_keys = init_window(&win_meta, "Keys", &world, draw_keys_win);
220   struct Win win_map = init_window(&win_meta, "Map", &world, draw_map_win);
221   struct Win win_info = init_window(&win_meta, "Info", &world, draw_info_win);
222   struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win);
223
224   int key;
225   while (1) {
226     draw_all_windows (&win_meta);
227     key = getch();
228     if      (key == get_action_key(world.keybindings, "quit"))
229       break;
230     else if (key == get_action_key(world.keybindings, "scroll pad right"))
231       scroll_pad (&win_meta, '+');
232     else if (key == get_action_key(world.keybindings, "scroll pad left"))
233       scroll_pad (&win_meta, '-');
234     else if (key == get_action_key(world.keybindings, "toggle keys window"))
235       toggle_window(&win_meta, &win_keys);
236     else if (key == get_action_key(world.keybindings, "toggle map window"))
237       toggle_window(&win_meta, &win_map);
238     else if (key == get_action_key(world.keybindings, "toggle info window"))
239       toggle_window(&win_meta, &win_info);
240     else if (key == get_action_key(world.keybindings, "toggle log window"))
241       toggle_window(&win_meta, &win_log);
242     else if (key == get_action_key(world.keybindings, "cycle forwards"))
243       cycle_active_window(&win_meta, 'n');
244     else if (key == get_action_key(world.keybindings, "cycle backwards"))
245       cycle_active_window(&win_meta, 'p');
246     else if (key == get_action_key(world.keybindings, "shift forwards"))
247       shift_active_window(&win_meta, 'f');
248     else if (key == get_action_key(world.keybindings, "shift backwards"))
249       shift_active_window(&win_meta, 'b');
250     else if (key == get_action_key(world.keybindings, "grow horizontally"))
251       growshrink_active_window(&win_meta, '*');
252     else if (key == get_action_key(world.keybindings, "shrink horizontally"))
253       growshrink_active_window(&win_meta, '_');
254     else if (key == get_action_key(world.keybindings, "grow vertically"))
255       growshrink_active_window(&win_meta, '+');
256     else if (key == get_action_key(world.keybindings, "shrink vertically"))
257       growshrink_active_window(&win_meta, '-');
258     else if (key == get_action_key(world.keybindings, "save keys"))
259       save_keybindings(&world);
260     else if (key == get_action_key(world.keybindings, "keys nav up"))
261       keyswin_move_selection (&world, 'u');
262     else if (key == get_action_key(world.keybindings, "keys nav down"))
263       keyswin_move_selection (&world, 'd');
264     else if (key == get_action_key(world.keybindings, "keys mod"))
265       keyswin_mod_key (&world, &win_meta);
266     else if (key == get_action_key(world.keybindings, "save seed"))
267       save_seed(seed);
268     else if (key == get_action_key(world.keybindings, "map up"))
269       map_scroll (&map, 'n');
270     else if (key == get_action_key(world.keybindings, "map down"))
271       map_scroll (&map, 's');
272     else if (key == get_action_key(world.keybindings, "map right"))
273       map_scroll (&map, 'e');
274     else if (key == get_action_key(world.keybindings, "map left"))
275       map_scroll (&map, 'w');
276     else if (key == get_action_key(world.keybindings, "player down"))
277       move_player(&world, 's');
278     else if (key == get_action_key(world.keybindings, "player up"))
279       move_player(&world, 'n');
280     else if (key == get_action_key(world.keybindings, "player right"))
281       move_player(&world, 'e');
282     else if (key == get_action_key(world.keybindings, "player left"))
283       move_player(&world, 'w');
284     else if (key == get_action_key(world.keybindings, "wait") )
285       player_wait (&world); }
286
287   free(map.cells);
288   for (key = 0; key <= world.keyswindata->max; key++)
289     free(world.keybindings[key].name);
290   free(world.keybindings);
291   free(world.keyswindata);
292   free(world.log);
293
294   endwin();
295   return 0; }