+#include <stdlib.h>
+#include <limits.h>
+#include <stdint.h>
#include <ncurses.h>
#include <string.h>
-#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
#include "windows.h"
#include "draw_wins.h"
#include "roguelike.h"
+#include "keybindings.h"
+
+uint16_t rrand(char use_seed, uint32_t new_seed) {
+// Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
+ static uint32_t seed;
+ if (0 != use_seed)
+ seed = new_seed;
+ seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
+ return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
+
+uint16_t read_uint16_bigendian(FILE * file) {
+// Read uint16 from file in big-endian order.
+ const uint16_t nchar = UCHAR_MAX + 1;
+ unsigned char a = fgetc(file);
+ unsigned char b = fgetc(file);
+ return (a * nchar) + b; }
+
+void write_uint16_bigendian(uint16_t x, FILE * file) {
+// Write uint16 to file in beg-endian order.
+ const uint16_t nchar = UCHAR_MAX + 1;
+ unsigned char a = x / nchar;
+ unsigned char b = x % nchar;
+ fputc(a, file);
+ fputc(b, file); }
+
+uint32_t read_uint32_bigendian(FILE * file) {
+// Read uint32 from file in big-endian order.
+ const uint16_t nchar = UCHAR_MAX + 1;
+ unsigned char a = fgetc(file);
+ unsigned char b = fgetc(file);
+ unsigned char c = fgetc(file);
+ unsigned char d = fgetc(file);
+ return (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d; }
+
+void write_uint32_bigendian(uint32_t x, FILE * file) {
+// Write uint32 to file in beg-endian order.
+ const uint16_t nchar = UCHAR_MAX + 1;
+ unsigned char a = x / (nchar * nchar * nchar);
+ unsigned char b = (x - (a * nchar * nchar * nchar)) / (nchar * nchar);
+ unsigned char c = (x - ((a * nchar * nchar * nchar) + (b * nchar * nchar))) / nchar;
+ unsigned char d = x % nchar;
+ fputc(a, file);
+ fputc(b, file);
+ fputc(c, file);
+ fputc(d, file); }
+
+void load_game(struct World * world) {
+// Load game data from game file.
+ FILE * file = fopen("savefile", "r");
+ world->seed = read_uint32_bigendian(file);
+ world->turn = read_uint32_bigendian(file);
+ world->player->y = read_uint16_bigendian(file);
+ world->player->x = read_uint16_bigendian(file);
+ world->monster->y = read_uint16_bigendian(file);
+ world->monster->x = read_uint16_bigendian(file);
+ fclose(file); }
+
+void save_game(struct World * world) {
+// Save game data to game file.
+ FILE * file = fopen("savefile", "w");
+ write_uint32_bigendian(world->seed, file);
+ write_uint32_bigendian(world->turn, file);
+ write_uint16_bigendian(world->player->y, file);
+ write_uint16_bigendian(world->player->x, file);
+ write_uint16_bigendian(world->monster->y, file);
+ write_uint16_bigendian(world->monster->x, file);
+ fclose(file); }
void toggle_window (struct WinMeta * win_meta, struct Win * win) {
// Toggle display of window win.
void growshrink_active_window (struct WinMeta * win_meta, char change) {
// Grow or shrink active window horizontally or vertically by one cell size.
if (0 != win_meta->active) {
- int height = win_meta->active->height;
- int width = win_meta->active->width;
+ uint16_t height = win_meta->active->height;
+ uint16_t width = win_meta->active->width;
if (change == '-')
height--;
else if (change == '+')
width++;
resize_active_window (win_meta, height, width); } }
-void init_keybindings(struct World * world) {
-// Initialize keybindings from file "keybindings".
- FILE * file = fopen("keybindings", "r");
- int lines = 0;
- int c = 0;
- int linemax = 0;
- int c_count = 0;
- while (EOF != c) {
- c_count++;
- c = getc(file);
- if ('\n' == c) {
- if (c_count > linemax)
- linemax = c_count + 1;
- c_count = 0;
- lines++; } }
- struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
- fseek(file, 0, SEEK_SET);
- char * command = malloc(linemax);
- int commcount = 0;
- char * cmdptr;
- while (fgets(command, linemax, file)) {
- keybindings[commcount].key = atoi(command);
- cmdptr = strchr(command, ' ') + 1;
- keybindings[commcount].name = malloc(strlen(cmdptr));
- memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
- keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
- commcount++; }
- free(command);
- fclose(file);
- struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
- keyswindata->max = lines - 1;
- keyswindata->select = 0;
- keyswindata->edit = 0;
- world->keybindings = keybindings;
- world->keyswindata = keyswindata; }
-
struct Map init_map () {
// Initialize map with some experimental start values.
struct Map map;
- map.width = 96;
- map.height = 32;
+ map.width = 64;
+ map.height = 64;
map.offset_x = 0;
map.offset_y = 0;
map.cells = malloc(map.width * map.height);
- int x, y, ran;
+ uint16_t x, y, ran;
char terrain;
for (y = 0; y < map.height; y++)
for (x = 0; x < map.width; x++) {
terrain = '.';
- ran = rand();
+ ran = rrand(0, 0);
if ( 0 == ran % ((x*x) / 3 + 1)
|| 0 == ran % ((y*y) / 3 + 1)
|| 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
void next_turn (struct World * world) {
// Increment turn and move enemy.
world->turn++;
- char d = rand() % 5;
- char ty = world->monster->y;
- char tx = world->monster->x;
+ rrand(1, world->seed * world->turn);
+ char d = rrand(0, 0) % 5;
+ uint16_t ty = world->monster->y;
+ uint16_t tx = world->monster->x;
if (1 == d)
ty++;
else if (2 == d)
void update_log (struct World * world, char * text) {
// Update log with new text to be appended.
char * new_text;
- int len_old = strlen(world->log);
- int len_new = strlen(text);
- int len_whole = len_old + len_new + 1;
+ uint16_t len_old = strlen(world->log);
+ uint16_t len_new = strlen(text);
+ uint16_t len_whole = len_old + len_new + 1;
new_text = calloc(len_whole, sizeof(char));
memcpy(new_text, world->log, len_old);
memcpy(new_text + len_old, text, len_new);
free(world->log);
world->log = new_text; }
-void save_keybindings(struct World * world) {
-// Write keybindings to keybindings file.
- struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
- struct KeyBinding * keybindings = world->keybindings;
- FILE * file = fopen("keybindings", "w");
- int linemax = 0;
- int i;
- for (i = 0; i <= keyswindata->max; i++)
- if (strlen(keybindings[i].name) > linemax)
- linemax = strlen(keybindings[i].name);
- linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
- char * line = malloc(linemax);
- for (i = 0; i <= keyswindata->max; i++) {
- snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
- fwrite(line, sizeof(char), strlen(line), file); }
- free(line);
- fclose(file); }
-
-int get_action_key (struct KeyBinding * keybindings, char * name) {
-// Return key matching name in keybindings.
- int i = 0;
- while (strcmp(keybindings[i].name, name) )
- i++;
- return keybindings[i].key; }
-
-char * get_keyname(int keycode) {
-// Translate some keycodes to readable names of max 9 chars.
- char * keyname;
- keyname = malloc(15);
- if (32 < keycode && keycode < 127)
- sprintf(keyname, "%c", keycode);
- else if (keycode == 9)
- sprintf(keyname, "TAB");
- else if (keycode == 10)
- sprintf(keyname, "RETURN");
- else if (keycode == 27)
- sprintf(keyname, "ESCAPE");
- else if (keycode == 32)
- sprintf(keyname, "SPACE");
- else if (keycode == KEY_UP)
- sprintf(keyname, "UP");
- else if (keycode == KEY_DOWN)
- sprintf(keyname, "DOWN");
- else if (keycode == KEY_LEFT)
- sprintf(keyname, "LEFT");
- else if (keycode == KEY_RIGHT)
- sprintf(keyname, "RIGHT");
- else if (keycode == KEY_HOME)
- sprintf(keyname, "HOME");
- else if (keycode == KEY_BACKSPACE)
- sprintf(keyname, "BACKSPACE");
- else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
- int f = keycode - KEY_F0;
- sprintf(keyname, "F%d", f); }
- else if (keycode == KEY_DC)
- sprintf(keyname, "DELETE");
- else if (keycode == KEY_IC)
- sprintf(keyname, "INSERT");
- else if (keycode == KEY_NPAGE)
- sprintf(keyname, "NEXT PAGE");
- else if (keycode == KEY_PPAGE)
- sprintf(keyname, "PREV PAGE");
- else if (keycode == KEY_END)
- sprintf(keyname, "END");
- else
- sprintf(keyname, "(unknown)");
- return keyname; }
-
-void keyswin_mod_key (struct World * world, struct WinMeta * win_meta) {
-// In keybindings window, mark selection modifiable, modify key. Ensure max of three digits in key code field.
- world->keyswindata->edit = 1;
- draw_all_windows (win_meta);
- int key = getch();
- if (key < 1000)
- world->keybindings[world->keyswindata->select].key = key;
- world->keyswindata->edit = 0; }
-
-void keyswin_move_selection (struct World * world, char dir) {
-// In keybindings window, move selection upwards or downwards (if within limits of list length).
- if ('u' == dir && world->keyswindata->select > 0)
- world->keyswindata->select--;
- else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
- world->keyswindata->select++; }
-
-char is_passable (struct World * world, int x, int y) {
+char is_passable (struct World * world, uint16_t x, uint16_t y) {
// Check if coordinate on (or beyond) map is accessible to movement.
char passable = 0;
if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
passable = 1;
return passable; }
+void record_action (char action) {
+// Append action to game record file.
+ FILE * file = fopen("record", "a");
+ fputc(action, file);
+ fclose(file); }
+
void move_player (struct World * world, char d) {
// Move player in direction d, increment turn counter and update log.
static char prev = 0;
char success = 0;
char * dir;
- char ty = world->player->y;
- char tx = world->player->x;
+ uint16_t ty = world->player->y;
+ uint16_t tx = world->player->x;
if ('s' == d) {
dir = "south";
ty++; }
update_log (world, msg);
free(msg); } }
prev = success * d;
+ if (1 == world->interactive)
+ record_action(d);
next_turn (world); }
-void player_wait(struct World * world) {
+void player_wait (struct World * world) {
// Make player wait one turn.
+ if (1 == world->interactive)
+ record_action(0);
next_turn (world);
update_log (world, "\nYou wait."); }
-int main () {
+void startpos(struct World * world) {
+// Initialize some default starting values.
+ world->turn = 1;
+ world->player->y = 8;
+ world->player->x = 8;
+ world->monster->y = 55;
+ world->monster->x = 55; }
+
+unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
+ struct Win * win_map, struct Win * win_info, struct Win * win_log) {
+// Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
+ if (key == get_action_key(world->keybindings, "quit"))
+ return 1;
+ else if (key == get_action_key(world->keybindings, "scroll pad right"))
+ scroll_pad (win_meta, '+');
+ else if (key == get_action_key(world->keybindings, "scroll pad left"))
+ scroll_pad (win_meta, '-');
+ else if (key == get_action_key(world->keybindings, "toggle keys window"))
+ toggle_window(win_meta, win_keys);
+ else if (key == get_action_key(world->keybindings, "toggle map window"))
+ toggle_window(win_meta, win_map);
+ else if (key == get_action_key(world->keybindings, "toggle info window"))
+ toggle_window(win_meta, win_info);
+ else if (key == get_action_key(world->keybindings, "toggle log window"))
+ toggle_window(win_meta, win_log);
+ else if (key == get_action_key(world->keybindings, "cycle forwards"))
+ cycle_active_window(win_meta, 'n');
+ else if (key == get_action_key(world->keybindings, "cycle backwards"))
+ cycle_active_window(win_meta, 'p');
+ else if (key == get_action_key(world->keybindings, "shift forwards"))
+ shift_active_window(win_meta, 'f');
+ else if (key == get_action_key(world->keybindings, "shift backwards"))
+ shift_active_window(win_meta, 'b');
+ else if (key == get_action_key(world->keybindings, "grow horizontally"))
+ growshrink_active_window(win_meta, '*');
+ else if (key == get_action_key(world->keybindings, "shrink horizontally"))
+ growshrink_active_window(win_meta, '_');
+ else if (key == get_action_key(world->keybindings, "grow vertically"))
+ growshrink_active_window(win_meta, '+');
+ else if (key == get_action_key(world->keybindings, "shrink vertically"))
+ growshrink_active_window(win_meta, '-');
+ else if (key == get_action_key(world->keybindings, "save keys"))
+ save_keybindings(world);
+ else if (key == get_action_key(world->keybindings, "keys nav up"))
+ keyswin_move_selection (world, 'u');
+ else if (key == get_action_key(world->keybindings, "keys nav down"))
+ keyswin_move_selection (world, 'd');
+ else if (key == get_action_key(world->keybindings, "keys mod"))
+ keyswin_mod_key (world, win_meta);
+ else if (key == get_action_key(world->keybindings, "map up"))
+ map_scroll (world->map, 'n');
+ else if (key == get_action_key(world->keybindings, "map down"))
+ map_scroll (world->map, 's');
+ else if (key == get_action_key(world->keybindings, "map right"))
+ map_scroll (world->map, 'e');
+ else if (key == get_action_key(world->keybindings, "map left"))
+ map_scroll (world->map, 'w');
+ return 0; }
+
+int main (int argc, char *argv[]) {
struct World world;
+ world.interactive = 1;
+ int opt;
+ uint32_t start_turn;
+ while ((opt = getopt(argc, argv, "s::")) != -1) {
+ switch (opt) {
+ case 's':
+ world.interactive = 0;
+ start_turn = 0;
+ if (optarg)
+ start_turn = atoi(optarg);
+ break;
+ default:
+ exit(EXIT_FAILURE); } }
+
init_keybindings(&world);
- world.turn = 0;
world.log = calloc(1, sizeof(char));
- update_log (&world, "Start!");
- struct Map map = init_map();
- world.map = ↦
+ update_log (&world, " ");
struct Player player;
- player.y = 16;
- player.x = 16;
world.player = &player;
struct Monster monster;
- monster.y = 16;
- monster.x = 80;
world.monster = &monster;
+ FILE * file;
+
+ if (0 == world.interactive) {
+ startpos(&world);
+ file = fopen("record", "r");
+ world.seed = read_uint32_bigendian(file); }
+ else {
+ if (0 == access("savefile", F_OK))
+ load_game(&world);
+ else {
+ startpos(&world);
+ world.seed = time(NULL);
+ file = fopen("record", "w");
+ write_uint32_bigendian(world.seed, file);
+ fclose(file); } }
+ rrand(1, world.seed);
+ struct Map map = init_map();
+ world.map = ↦
WINDOW * screen = initscr();
noecho();
struct Win win_map = init_window(&win_meta, "Map", &world, draw_map_win);
struct Win win_info = init_window(&win_meta, "Info", &world, draw_info_win);
struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win);
+ win_keys.width = 29;
+ win_map.width = win_meta.width - win_keys.width - win_log.width - 2;
+ win_info.height = 1;
+ win_log.height = win_meta.height - 3;
+ toggle_window(&win_meta, &win_keys);
+ toggle_window(&win_meta, &win_map);
+ toggle_window(&win_meta, &win_info);
+ toggle_window(&win_meta, &win_log);
int key;
- while (1) {
- draw_all_windows (&win_meta);
- key = getch();
- if (key == get_action_key(world.keybindings, "quit"))
- break;
- else if (key == get_action_key(world.keybindings, "scroll pad right"))
- scroll_pad (&win_meta, '+');
- else if (key == get_action_key(world.keybindings, "scroll pad left"))
- scroll_pad (&win_meta, '-');
- else if (key == get_action_key(world.keybindings, "toggle keys window"))
- toggle_window(&win_meta, &win_keys);
- else if (key == get_action_key(world.keybindings, "toggle map window"))
- toggle_window(&win_meta, &win_map);
- else if (key == get_action_key(world.keybindings, "toggle info window"))
- toggle_window(&win_meta, &win_info);
- else if (key == get_action_key(world.keybindings, "toggle log window"))
- toggle_window(&win_meta, &win_log);
- else if (key == get_action_key(world.keybindings, "cycle forwards"))
- cycle_active_window(&win_meta, 'n');
- else if (key == get_action_key(world.keybindings, "cycle backwards"))
- cycle_active_window(&win_meta, 'p');
- else if (key == get_action_key(world.keybindings, "shift forwards"))
- shift_active_window(&win_meta, 'f');
- else if (key == get_action_key(world.keybindings, "shift backwards"))
- shift_active_window(&win_meta, 'b');
- else if (key == get_action_key(world.keybindings, "grow horizontally"))
- growshrink_active_window(&win_meta, '*');
- else if (key == get_action_key(world.keybindings, "shrink horizontally"))
- growshrink_active_window(&win_meta, '_');
- else if (key == get_action_key(world.keybindings, "grow vertically"))
- growshrink_active_window(&win_meta, '+');
- else if (key == get_action_key(world.keybindings, "shrink vertically"))
- growshrink_active_window(&win_meta, '-');
- else if (key == get_action_key(world.keybindings, "save keys"))
- save_keybindings(&world);
- else if (key == get_action_key(world.keybindings, "keys nav up"))
- keyswin_move_selection (&world, 'u');
- else if (key == get_action_key(world.keybindings, "keys nav down"))
- keyswin_move_selection (&world, 'd');
- else if (key == get_action_key(world.keybindings, "keys mod"))
- keyswin_mod_key (&world, &win_meta);
- else if (key == get_action_key(world.keybindings, "map up"))
- map_scroll (&map, 'n');
- else if (key == get_action_key(world.keybindings, "map down"))
- map_scroll (&map, 's');
- else if (key == get_action_key(world.keybindings, "map right"))
- map_scroll (&map, 'e');
- else if (key == get_action_key(world.keybindings, "map left"))
- map_scroll (&map, 'w');
- else if (key == get_action_key(world.keybindings, "player down"))
- move_player(&world, 's');
- else if (key == get_action_key(world.keybindings, "player up"))
- move_player(&world, 'n');
- else if (key == get_action_key(world.keybindings, "player right"))
- move_player(&world, 'e');
- else if (key == get_action_key(world.keybindings, "player left"))
- move_player(&world, 'w');
- else if (key == get_action_key(world.keybindings, "wait") )
- player_wait (&world); }
+ unsigned char quit_called;
+ if (0 == world.interactive) {
+ unsigned char still_reading_file = 1;
+ int action;
+ while (1) {
+ if (start_turn == world.turn)
+ start_turn = 0;
+ if (0 == start_turn) {
+ draw_all_windows (&win_meta);
+ key = getch(); }
+ if (1 == still_reading_file &&
+ (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
+ action = getc(file);
+ if (EOF == action) {
+ start_turn = 0;
+ still_reading_file = 0; }
+ else if (0 == action)
+ player_wait (&world);
+ else if ('s' == action)
+ move_player(&world, 's');
+ else if ('n' == action)
+ move_player(&world, 'n');
+ else if ('e' == action)
+ move_player(&world, 'e');
+ else if ('w' == action)
+ move_player(&world, 'w'); }
+ else
+ quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
+ if (1 == quit_called)
+ break; } }
+ else {
+ uint32_t last_turn = 0;
+ while (1) {
+ if (last_turn != world.turn) {
+ save_game(&world);
+ last_turn = world.turn; }
+ draw_all_windows (&win_meta);
+ key = getch();
+ if (key == get_action_key(world.keybindings, "player down"))
+ move_player(&world, 's');
+ else if (key == get_action_key(world.keybindings, "player up"))
+ move_player(&world, 'n');
+ else if (key == get_action_key(world.keybindings, "player right"))
+ move_player(&world, 'e');
+ else if (key == get_action_key(world.keybindings, "player left"))
+ move_player(&world, 'w');
+ else if (key == get_action_key(world.keybindings, "wait / next turn"))
+ player_wait (&world);
+ else
+ quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
+ if (1 == quit_called)
+ break; } }
free(map.cells);
for (key = 0; key <= world.keyswindata->max; key++)