home · contact · privacy
Use own pseudo-random number generator to ensure portable predictability.
[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 "windows.h"
7 #include "draw_wins.h"
8 #include "roguelike.h"
9 #include "keybindings.h"
10
11 static uint32_t seed = 0;
12
13 uint16_t rrand() {
14 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
15   uint32_t a   = 1103515245;      // Values for a and b as recommended by POSIX.1-2001 (see man page rand(3)).
16   uint16_t b   =      12345;
17   uint32_t mod = 2147483648;
18   seed = ((seed * a) + b) % mod;
19   return (seed / 65536); }        // Ignore least significant 16 bits (they are less random).
20
21 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
22 // Toggle display of window win.
23   if (0 != win->curses)
24     suspend_window(win_meta, win);
25   else
26     append_window(win_meta, win); }
27
28 void growshrink_active_window (struct WinMeta * win_meta, char change) {
29 // Grow or shrink active window horizontally or vertically by one cell size.
30   if (0 != win_meta->active) {
31     uint16_t height = win_meta->active->height;
32     uint16_t width = win_meta->active->width;
33     if      (change == '-')
34       height--;
35     else if (change == '+')
36       height++;
37     else if (change == '_')
38       width--;
39     else if (change == '*')
40       width++;
41     resize_active_window (win_meta, height, width); } }
42
43 struct Map init_map () {
44 // Initialize map with some experimental start values.
45   struct Map map;
46   map.width = 96;
47   map.height = 32;
48   map.offset_x = 0;
49   map.offset_y = 0;
50   map.cells = malloc(map.width * map.height);
51   uint16_t x, y, ran;
52   char terrain;
53   for (y = 0; y < map.height; y++)
54     for (x = 0; x < map.width; x++) {
55       terrain = '.';
56       ran = rrand();
57       if (   0 == ran % ((x*x) / 3 + 1)
58           || 0 == ran % ((y*y) / 3 + 1)
59           || 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
60           || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1))
61         terrain = ' ';
62       map.cells[(y * map.width) + x] = terrain; }
63   return map; }
64
65 void map_scroll (struct Map * map, char dir) {
66 // Scroll map into direction dir if possible by changing the offset.
67   if      ('n' == dir && map->offset_y > 0)
68     map->offset_y--;
69   else if ('s' == dir)
70     map->offset_y++;
71   else if ('w' == dir && map->offset_x > 0)
72     map->offset_x--;
73   else if ('e' == dir)
74     map->offset_x++; }
75
76 void next_turn (struct World * world) {
77 // Increment turn and move enemy.
78   world->turn++;
79   char d = rrand() % 5;
80   char ty = world->monster->y;
81   char tx = world->monster->x;
82   if (1 == d)
83     ty++;
84   else if (2 == d)
85     ty--;
86   else if (3 == d)
87     tx++;
88   else if (4 == d)
89     tx--;
90   if (tx == world->player->x && ty == world->player->y)
91     update_log(world, "\nThe monster hits you.");
92   else if (is_passable(world, tx, ty)) {
93     world->monster->y = ty;
94     world->monster->x = tx; } }
95
96 void update_log (struct World * world, char * text) {
97 // Update log with new text to be appended.
98   char * new_text;
99   uint16_t len_old = strlen(world->log);
100   uint16_t len_new = strlen(text);
101   uint16_t len_whole = len_old + len_new + 1;
102   new_text = calloc(len_whole, sizeof(char));
103   memcpy(new_text, world->log, len_old);
104   memcpy(new_text + len_old, text, len_new);
105   free(world->log);
106   world->log = new_text; }
107
108 char is_passable (struct World * world, uint16_t x, uint16_t y) {
109 // Check if coordinate on (or beyond) map is accessible to movement.
110   char passable = 0;
111   if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
112     if ('.' == world->map->cells[y * world->map->width + x])
113       passable = 1;
114   return passable; }
115
116 void move_player (struct World * world, char d) {
117 // Move player in direction d, increment turn counter and update log.
118   static char prev = 0;
119   char success = 0;
120   char * dir;
121   char ty = world->player->y;
122   char tx = world->player->x;
123   if ('s' == d) {
124     dir = "south";
125     ty++; }
126   if ('n' == d) {
127     dir = "north";
128     ty--; }
129   if ('w' == d) {
130     dir = "west";
131     tx--; }
132   if ('e' == d) {
133     dir = "east";
134     tx++; }
135   if (ty == world->monster->y && tx == world->monster->x)
136     success = 2;
137   else if (is_passable(world, tx, ty)) {
138     success = 1;
139     world->player->y = ty;
140     world->player->x = tx; }
141   if (success * d == prev)
142     update_log (world, ".");
143   else {
144     if (2 == success)
145       update_log (world, "\nYou hit the monster.");
146     else {
147       char * msg = calloc(25, sizeof(char));
148       char * msg_content = "You fail to move";
149       if (1 == success)
150         msg_content = "You move";
151       sprintf(msg, "\n%s %s.", msg_content, dir);
152       update_log (world, msg);
153       free(msg); } }
154   prev = success * d;
155   next_turn (world); }
156
157 void player_wait (struct World * world) {
158 // Make player wait one turn.
159   next_turn (world);
160   update_log (world, "\nYou wait."); }
161
162 void save_map (struct Map * map) {
163 // Save map to file "map".
164   FILE * file = fopen("map", "w");
165   fputc(map->width / CHAR_MAX, file);
166   fputc(map->width % CHAR_MAX, file);
167   uint16_t y, x;
168   for (y = 0; y < map->height; y++)
169     for (x = 0; x < map->width; x++)
170       fputc(map->cells[y * map->width + x], file);
171   fclose(file); }
172
173 struct Map load_map () {
174 // Load map from file.
175   FILE * file = fopen("map", "r");
176   struct Map map;
177   map.width = (fgetc(file) * CHAR_MAX) + fgetc(file);
178   long pos = ftell(file);
179   int c = fgetc(file);
180   uint32_t i = 0;
181   while (EOF != c) {
182     i++;
183     c = fgetc(file); }
184   map.cells = malloc(i * sizeof(char));
185   map.height = i / map.width;
186   fseek(file, pos, SEEK_SET);
187   c = fgetc(file);
188   i = 0;
189   while (EOF != c) {
190     map.cells[i] = (char) c;
191     c = fgetc(file);
192     i++; }
193   fclose(file);
194   return map; }
195
196 int main () {
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();
203   world.map = &map;
204   struct Player player;
205   player.y = 16;
206   player.x = 16;
207   world.player = &player;
208   struct Monster monster;
209   monster.y = 16;
210   monster.x = 80;
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, "load map"))
267       map = load_map();
268     else if (key == get_action_key(world.keybindings, "save map"))
269       save_map(&map);
270     else if (key == get_action_key(world.keybindings, "map up"))
271       map_scroll (&map, 'n');
272     else if (key == get_action_key(world.keybindings, "map down"))
273       map_scroll (&map, 's');
274     else if (key == get_action_key(world.keybindings, "map right"))
275       map_scroll (&map, 'e');
276     else if (key == get_action_key(world.keybindings, "map left"))
277       map_scroll (&map, 'w');
278     else if (key == get_action_key(world.keybindings, "player down"))
279       move_player(&world, 's');
280     else if (key == get_action_key(world.keybindings, "player up"))
281       move_player(&world, 'n');
282     else if (key == get_action_key(world.keybindings, "player right"))
283       move_player(&world, 'e');
284     else if (key == get_action_key(world.keybindings, "player left"))
285       move_player(&world, 'w');
286     else if (key == get_action_key(world.keybindings, "wait") )
287       player_wait (&world); }
288
289   free(map.cells);
290   for (key = 0; key <= world.keyswindata->max; key++)
291     free(world.keybindings[key].name);
292   free(world.keybindings);
293   free(world.keyswindata);
294   free(world.log);
295
296   endwin();
297   return 0; }