From fa3b43c4b7a23503bbcf689e374e4eb7be108208 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 04:30:11 +0200 Subject: [PATCH 01/16] As init_map() doesn't need the seed anymore, it's no longer a parameter. --- roguelike.c | 4 ++-- roguelike.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/roguelike.c b/roguelike.c index cba46ad..ebf440d 100644 --- a/roguelike.c +++ b/roguelike.c @@ -67,7 +67,7 @@ void growshrink_active_window (struct WinMeta * win_meta, char change) { width++; resize_active_window (win_meta, height, width); } } -struct Map init_map (uint32_t seed) { +struct Map init_map () { // Initialize map with some experimental start values. struct Map map; map.width = 64; @@ -199,7 +199,7 @@ int main (int argc, char *argv[]) { world.turn = 0; world.log = calloc(1, sizeof(char)); update_log (&world, "Start!"); - struct Map map = init_map(seed); + struct Map map = init_map(); world.map = ↦ struct Player player; player.y = 8; diff --git a/roguelike.h b/roguelike.h index 7bbe5c3..45abf77 100644 --- a/roguelike.h +++ b/roguelike.h @@ -27,7 +27,7 @@ uint32_t load_seed(); void save_seed(uint32_t); void toggle_window (struct WinMeta *, struct Win *); void growshrink_active_window (struct WinMeta *, char); -struct Map init_map (uint32_t); +struct Map init_map (); void map_scroll (struct Map *, char); void next_turn (struct World *); void update_log (struct World *, char *); -- 2.30.2 From c1a11c82ca5dd7aa7b48e79b72fcb0112565e1bc Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:13:03 +0200 Subject: [PATCH 02/16] Replaced seed file system with a game save file system. --- keybindings | 2 +- roguelike.c | 96 +++++++++++++++++++++++++++++++++++------------------ roguelike.h | 11 ++++-- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/keybindings b/keybindings index 6d0b52e..920c588 100644 --- a/keybindings +++ b/keybindings @@ -17,7 +17,7 @@ 95 shrink horizontally 43 grow vertically 45 shrink vertically -115 save seed +115 save game 119 map up 120 map down 97 map left diff --git a/roguelike.c b/roguelike.c index ebf440d..0de96ee 100644 --- a/roguelike.c +++ b/roguelike.c @@ -18,31 +18,62 @@ uint16_t rrand(char use_seed, uint32_t 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). -uint32_t load_seed() { -// Load seed integer from seed file. - uint32_t seed; +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; - FILE * file = fopen("seed", "r"); unsigned char a = fgetc(file); unsigned char b = fgetc(file); unsigned char c = fgetc(file); unsigned char d = fgetc(file); - seed = (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d; - fclose(file); - return seed; } + return (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d; } -void save_seed(uint32_t seed) { -// Save seed integer to seed file. +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 = seed / (nchar * nchar * nchar); - unsigned char b = (seed - (a * nchar * nchar * nchar)) / (nchar * nchar); - unsigned char c = (seed - ((a * nchar * nchar * nchar) + (b * nchar * nchar))) / nchar; - unsigned char d = seed % nchar; - FILE * file = fopen("seed", "w"); + 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); + fputc(d, file); } + +void load_seed(struct World * world) { +// Load seed integer from seed 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_seed(struct World * world) { +// Save seed integer to seed 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) { @@ -187,28 +218,27 @@ void player_wait (struct World * world) { update_log (world, "\nYou wait."); } int main (int argc, char *argv[]) { - uint32_t seed; - if (0 == access("seed", F_OK)) - seed = load_seed(); - else - seed = time(NULL); - rrand(1, seed); - struct World world; + struct Player player; + world.player = &player; + struct Monster monster; + world.monster = &monster; + if (0 == access("savefile", F_OK)) + load_seed(&world); + else { + player.y = 8; + player.x = 8; + monster.y = 55; + monster.x = 55; + world.seed = time(NULL); + world.turn = 0; } + rrand(1, world.seed); + init_keybindings(&world); - world.turn = 0; world.log = calloc(1, sizeof(char)); update_log (&world, "Start!"); struct Map map = init_map(); world.map = ↦ - struct Player player; - player.y = 8; - player.x = 8; - world.player = &player; - struct Monster monster; - monster.y = 55; - monster.x = 55; - world.monster = &monster; WINDOW * screen = initscr(); noecho(); @@ -263,8 +293,8 @@ int main (int argc, char *argv[]) { 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, "save seed")) - save_seed(seed); + else if (key == get_action_key(world.keybindings, "save game")) + save_seed(&world); else if (key == get_action_key(world.keybindings, "map up")) map_scroll (&map, 'n'); else if (key == get_action_key(world.keybindings, "map down")) diff --git a/roguelike.h b/roguelike.h index 45abf77..2bfa449 100644 --- a/roguelike.h +++ b/roguelike.h @@ -1,7 +1,8 @@ struct World { struct KeyBinding * keybindings; struct KeysWinData * keyswindata; - uint16_t turn; + uint32_t seed; + uint32_t turn; char * log; struct Map * map; struct Monster * monster; @@ -23,8 +24,12 @@ struct Monster { uint16_t x; }; uint16_t rrand(char, uint32_t); -uint32_t load_seed(); -void save_seed(uint32_t); +uint16_t read_uint16_bigendian(FILE * file); +void write_uint16_bigendian(uint16_t x, FILE * file); +uint32_t read_uint32_bigendian(FILE * file); +void write_uint32_bigendian(uint32_t x, FILE * file); +void load_seed(struct World *); +void save_seed(struct World *); void toggle_window (struct WinMeta *, struct Win *); void growshrink_active_window (struct WinMeta *, char); struct Map init_map (); -- 2.30.2 From 98588def4ac3ab05cb815814de67a44f506ae569 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:17:14 +0200 Subject: [PATCH 03/16] Re-seed rrand() with a new but predictable seed each new turn. --- roguelike.c | 1 + 1 file changed, 1 insertion(+) diff --git a/roguelike.c b/roguelike.c index 0de96ee..6217fec 100644 --- a/roguelike.c +++ b/roguelike.c @@ -134,6 +134,7 @@ void map_scroll (struct Map * map, char dir) { void next_turn (struct World * world) { // Increment turn and move enemy. world->turn++; + rrand(1, world->seed * world->turn); char d = rrand(0, 0) % 5; uint16_t ty = world->monster->y; uint16_t tx = world->monster->x; -- 2.30.2 From 9f43e263fd48bf840d3b6225bf3367752a5575b5 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:26:45 +0200 Subject: [PATCH 04/16] Working towards roguelike permadeath conventions: Saving happens automatically now at each new turn. --- keybindings | 1 - roguelike.c | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/keybindings b/keybindings index 920c588..cb285c7 100644 --- a/keybindings +++ b/keybindings @@ -17,7 +17,6 @@ 95 shrink horizontally 43 grow vertically 45 shrink vertically -115 save game 119 map up 120 map down 97 map left diff --git a/roguelike.c b/roguelike.c index 6217fec..c68e983 100644 --- a/roguelike.c +++ b/roguelike.c @@ -232,7 +232,7 @@ int main (int argc, char *argv[]) { monster.y = 55; monster.x = 55; world.seed = time(NULL); - world.turn = 0; } + world.turn = 1; } rrand(1, world.seed); init_keybindings(&world); @@ -253,7 +253,11 @@ int main (int argc, char *argv[]) { struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win); int key; + uint32_t last_turn = 0; while (1) { + if (last_turn != world.turn) { + save_seed(&world); + last_turn = world.turn; } draw_all_windows (&win_meta); key = getch(); if (key == get_action_key(world.keybindings, "quit")) @@ -294,8 +298,6 @@ int main (int argc, char *argv[]) { 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, "save game")) - save_seed(&world); else if (key == get_action_key(world.keybindings, "map up")) map_scroll (&map, 'n'); else if (key == get_action_key(world.keybindings, "map down")) -- 2.30.2 From 70219003e949b884ceaf1215bfc13f5814aacdbe Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:28:28 +0200 Subject: [PATCH 05/16] Renamed functions to reflect change from seed files system to game save files system. --- roguelike.c | 8 ++++---- roguelike.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/roguelike.c b/roguelike.c index c68e983..c5c1309 100644 --- a/roguelike.c +++ b/roguelike.c @@ -54,7 +54,7 @@ void write_uint32_bigendian(uint32_t x, FILE * file) { fputc(c, file); fputc(d, file); } -void load_seed(struct World * world) { +void load_game(struct World * world) { // Load seed integer from seed file. FILE * file = fopen("savefile", "r"); world->seed = read_uint32_bigendian(file); @@ -65,7 +65,7 @@ void load_seed(struct World * world) { world->monster->x = read_uint16_bigendian(file); fclose(file); } -void save_seed(struct World * world) { +void save_game(struct World * world) { // Save seed integer to seed file. FILE * file = fopen("savefile", "w"); write_uint32_bigendian(world->seed, file); @@ -225,7 +225,7 @@ int main (int argc, char *argv[]) { struct Monster monster; world.monster = &monster; if (0 == access("savefile", F_OK)) - load_seed(&world); + load_game(&world); else { player.y = 8; player.x = 8; @@ -256,7 +256,7 @@ int main (int argc, char *argv[]) { uint32_t last_turn = 0; while (1) { if (last_turn != world.turn) { - save_seed(&world); + save_game(&world); last_turn = world.turn; } draw_all_windows (&win_meta); key = getch(); diff --git a/roguelike.h b/roguelike.h index 2bfa449..0d0ce86 100644 --- a/roguelike.h +++ b/roguelike.h @@ -28,8 +28,8 @@ uint16_t read_uint16_bigendian(FILE * file); void write_uint16_bigendian(uint16_t x, FILE * file); uint32_t read_uint32_bigendian(FILE * file); void write_uint32_bigendian(uint32_t x, FILE * file); -void load_seed(struct World *); -void save_seed(struct World *); +void load_game(struct World *); +void save_game(struct World *); void toggle_window (struct WinMeta *, struct Win *); void growshrink_active_window (struct WinMeta *, char); struct Map init_map (); -- 2.30.2 From bfbd452c40d252715f0622e67e93028a35d6dd42 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:33:55 +0200 Subject: [PATCH 06/16] Updated README to reflect last hours' changes. --- README | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/README b/README index 7880897..224b7b8 100644 --- a/README +++ b/README @@ -4,8 +4,9 @@ plomrogue plomlompom tries to build his own roguelike. Currently, it doesn't do much, apart from managing some example ncurses windows in a bizarre fashion. You can move around a player and meet an enemy, but they don't -really fight. A map gets generated randomly; it's seed can be saved and -later reloaded. +really fight. The map gets generated randomly. There is only one save +file (named "savefile"), and it gets overwritten each new turn. To start +over with a new world, delete it. Install/run ----------- @@ -17,8 +18,6 @@ cd plomrogue make ./roguelike -Use option -l to load an existing map seed. - Default keybindings ------------------- @@ -39,8 +38,6 @@ _ shrink window horizontally + grow window vertically - shrink window vertically -s save seed of map - w scroll map up x scroll map down a scroll map left -- 2.30.2 From b5bccd803afb866258eef95abd10ddb096b6c9e3 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 29 May 2013 05:46:38 +0200 Subject: [PATCH 07/16] Corrected comments. --- roguelike.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roguelike.c b/roguelike.c index c5c1309..3b92c11 100644 --- a/roguelike.c +++ b/roguelike.c @@ -55,7 +55,7 @@ void write_uint32_bigendian(uint32_t x, FILE * file) { fputc(d, file); } void load_game(struct World * world) { -// Load seed integer from seed file. +// Load game data from game file. FILE * file = fopen("savefile", "r"); world->seed = read_uint32_bigendian(file); world->turn = read_uint32_bigendian(file); @@ -66,7 +66,7 @@ void load_game(struct World * world) { fclose(file); } void save_game(struct World * world) { -// Save seed integer to seed file. +// Save game data to game file. FILE * file = fopen("savefile", "w"); write_uint32_bigendian(world->seed, file); write_uint32_bigendian(world->turn, file); -- 2.30.2 From c9b91ea7cb25ac97e736935b1f5f26d7f0d58ef6 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 30 May 2013 00:27:04 +0200 Subject: [PATCH 08/16] Restructured main() start. Start with empty log. --- roguelike.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/roguelike.c b/roguelike.c index 3b92c11..0521ae9 100644 --- a/roguelike.c +++ b/roguelike.c @@ -220,6 +220,10 @@ void player_wait (struct World * world) { int main (int argc, char *argv[]) { struct World world; + init_keybindings(&world); + + world.log = calloc(1, sizeof(char)); + update_log (&world, " "); struct Player player; world.player = &player; struct Monster monster; @@ -234,10 +238,6 @@ int main (int argc, char *argv[]) { world.seed = time(NULL); world.turn = 1; } rrand(1, world.seed); - - init_keybindings(&world); - world.log = calloc(1, sizeof(char)); - update_log (&world, "Start!"); struct Map map = init_map(); world.map = ↦ -- 2.30.2 From 231090b75bf54933f8016781c9c3f83e6ba3d669 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 30 May 2013 00:39:20 +0200 Subject: [PATCH 09/16] Start with windows toggled on and put into a sensible starting configuration. --- roguelike.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/roguelike.c b/roguelike.c index 0521ae9..0279043 100644 --- a/roguelike.c +++ b/roguelike.c @@ -251,6 +251,14 @@ int main (int argc, char *argv[]) { 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; uint32_t last_turn = 0; -- 2.30.2 From cc54db8c41c3d0e00248422b6dc1a67187cd4e16 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 30 May 2013 02:00:08 +0200 Subject: [PATCH 10/16] Added a gameplay recording system and restructured everything to make it fit in. --- README | 2 +- keybindings | 2 +- roguelike.c | 211 ++++++++++++++++++++++++++++++++++------------------ roguelike.h | 4 + 4 files changed, 145 insertions(+), 74 deletions(-) diff --git a/README b/README index 224b7b8..2793e98 100644 --- a/README +++ b/README @@ -46,7 +46,7 @@ t move player up b move player down f move player left h move player rifght -. wait one turn +. next turn / wait S save current keybindings UP navigate selection upwards in keybinding editing window diff --git a/keybindings b/keybindings index cb285c7..5a10cc3 100644 --- a/keybindings +++ b/keybindings @@ -25,4 +25,4 @@ 98 player down 102 player left 104 player right -46 wait +46 wait / next turn diff --git a/roguelike.c b/roguelike.c index 0279043..23a686a 100644 --- a/roguelike.c +++ b/roguelike.c @@ -172,6 +172,12 @@ char is_passable (struct World * world, uint16_t x, uint16_t y) { passable = 1; return passable; } +void record_action (char action) { +// + 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; @@ -211,32 +217,110 @@ void move_player (struct World * world, char d) { 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) { // Make player wait one turn. + if (1 == world->interactive) + record_action(0); next_turn (world); update_log (world, "\nYou wait."); } +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; + 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; - init_keybindings(&world); + world.interactive = 1; + int opt; + while ((opt = getopt(argc, argv, "s")) != -1) { + switch (opt) { + case 's': + world.interactive = 0; + break; + default: + exit(EXIT_FAILURE); } } + init_keybindings(&world); world.log = calloc(1, sizeof(char)); update_log (&world, " "); struct Player player; world.player = &player; struct Monster monster; world.monster = &monster; - if (0 == access("savefile", F_OK)) - load_game(&world); + FILE * file; + + if (0 == world.interactive) { + startpos(&world); + file = fopen("record", "r"); + world.seed = read_uint32_bigendian(file); } else { - player.y = 8; - player.x = 8; - monster.y = 55; - monster.x = 55; - world.seed = time(NULL); - world.turn = 1; } + 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 = ↦ @@ -261,69 +345,52 @@ int main (int argc, char *argv[]) { toggle_window(&win_meta, &win_log); int key; - 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, "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 result; + if (0 == world.interactive) { + int action; + while (1) { + draw_all_windows (&win_meta); + key = getch(); + if (key == get_action_key(world.keybindings, "wait / next turn") ) { + action = getc(file); + if (EOF == action) + break; + 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 + result = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log); + if (1 == result) + 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 + result = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log); + if (1 == result) + break; } } free(map.cells); for (key = 0; key <= world.keyswindata->max; key++) diff --git a/roguelike.h b/roguelike.h index 0d0ce86..26ca0a4 100644 --- a/roguelike.h +++ b/roguelike.h @@ -1,4 +1,5 @@ struct World { + char interactive; struct KeyBinding * keybindings; struct KeysWinData * keyswindata; uint32_t seed; @@ -37,5 +38,8 @@ void map_scroll (struct Map *, char); void next_turn (struct World *); void update_log (struct World *, char *); char is_passable (struct World *, uint16_t, uint16_t); +void record_action (char); void move_player (struct World *, char); void player_wait(struct World *); +void startpos(struct World *); +unsigned char meta_keys(int, struct World *, struct WinMeta *, struct Win *, struct Win *, struct Win *, struct Win *); -- 2.30.2 From 549e29447bb7b090580888d9286afd5d871b40d6 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 30 May 2013 02:02:51 +0200 Subject: [PATCH 11/16] Added info on new recording system to README. --- README | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README b/README index 2793e98..a395d4b 100644 --- a/README +++ b/README @@ -52,3 +52,11 @@ S save current keybindings UP navigate selection upwards in keybinding editing window DOWN navigate selection downwards in keybinding editing window RETURN modify keybinding selected in keybinding editing window + +Replay game recording +--------------------- + +Run "./roguelike -s" to watch a recording of the current game from the +beginning. Hit the "next turn / wait" key to increment turns. Keys to +manage windows, scroll on the map and quit the program are active; keys +to perform player actions are inactive. -- 2.30.2 From 5412e81a7b206b84e82c73ff322e0f34db71dcf3 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 3 Jun 2013 19:02:51 +0200 Subject: [PATCH 12/16] Corrected minor inconsistencies. --- roguelike.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roguelike.c b/roguelike.c index 23a686a..aef863e 100644 --- a/roguelike.c +++ b/roguelike.c @@ -173,7 +173,7 @@ char is_passable (struct World * world, uint16_t x, uint16_t y) { return passable; } void record_action (char action) { -// +// Append action to game record file. FILE * file = fopen("record", "a"); fputc(action, file); fclose(file); } @@ -241,7 +241,7 @@ unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta // 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; - if (key == get_action_key(world->keybindings, "scroll pad right")) + 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, '-'); -- 2.30.2 From 3663e5190d818c51d9b8e8107bf3cc713e0ece03 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 3 Jun 2013 19:06:29 +0200 Subject: [PATCH 13/16] Use more descriptive variable name. --- roguelike.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/roguelike.c b/roguelike.c index aef863e..d734726 100644 --- a/roguelike.c +++ b/roguelike.c @@ -345,7 +345,7 @@ int main (int argc, char *argv[]) { toggle_window(&win_meta, &win_log); int key; - unsigned char result; + unsigned char quit_called; if (0 == world.interactive) { int action; while (1) { @@ -366,8 +366,8 @@ int main (int argc, char *argv[]) { else if ('w' == action) move_player(&world, 'w'); } else - result = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log); - if (1 == result) + 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; @@ -388,8 +388,8 @@ int main (int argc, char *argv[]) { else if (key == get_action_key(world.keybindings, "wait / next turn")) player_wait (&world); else - result = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log); - if (1 == result) + quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log); + if (1 == quit_called) break; } } free(map.cells); -- 2.30.2 From 2f4691b20aaf36f0eb5bc9e4830eabcf74fabed2 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 3 Jun 2013 19:10:47 +0200 Subject: [PATCH 14/16] Don't abort replay on last turn. --- roguelike.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/roguelike.c b/roguelike.c index d734726..5d42071 100644 --- a/roguelike.c +++ b/roguelike.c @@ -347,14 +347,15 @@ int main (int argc, char *argv[]) { int key; unsigned char quit_called; if (0 == world.interactive) { + unsigned char still_reading_file = 1; int action; while (1) { draw_all_windows (&win_meta); key = getch(); - if (key == get_action_key(world.keybindings, "wait / next turn") ) { + if (1 == still_reading_file && key == get_action_key(world.keybindings, "wait / next turn") ) { action = getc(file); if (EOF == action) - break; + still_reading_file = 0; else if (0 == action) player_wait (&world); else if ('s' == action) -- 2.30.2 From 7f1b0fb8bbd4f8c4f07c7d86fb8de0050d925f46 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 3 Jun 2013 19:58:57 +0200 Subject: [PATCH 15/16] -s takes optional argument: number of turn from which to start replay. --- README | 3 ++- roguelike.c | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README b/README index a395d4b..beadb16 100644 --- a/README +++ b/README @@ -59,4 +59,5 @@ Replay game recording Run "./roguelike -s" to watch a recording of the current game from the beginning. Hit the "next turn / wait" key to increment turns. Keys to manage windows, scroll on the map and quit the program are active; keys -to perform player actions are inactive. +to perform player actions are inactive. Append a number to the -s option +(like "-s100") to start the recording at the respective turn number. diff --git a/roguelike.c b/roguelike.c index 5d42071..9a1849c 100644 --- a/roguelike.c +++ b/roguelike.c @@ -291,10 +291,14 @@ int main (int argc, char *argv[]) { struct World world; world.interactive = 1; int opt; - while ((opt = getopt(argc, argv, "s")) != -1) { + 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); } } @@ -350,12 +354,17 @@ int main (int argc, char *argv[]) { unsigned char still_reading_file = 1; int action; while (1) { - draw_all_windows (&win_meta); - key = getch(); - if (1 == still_reading_file && key == get_action_key(world.keybindings, "wait / next turn") ) { + 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) - still_reading_file = 0; + if (EOF == action) { + start_turn = 0; + still_reading_file = 0; } else if (0 == action) player_wait (&world); else if ('s' == action) -- 2.30.2 From 18a698f3107109f8cd6ba0d89e41386ac394cf02 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 3 Jun 2013 21:28:05 +0200 Subject: [PATCH 16/16] Improved map generation algorithm. --- roguelike.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/roguelike.c b/roguelike.c index 9a1849c..9139cb3 100644 --- a/roguelike.c +++ b/roguelike.c @@ -105,19 +105,25 @@ struct Map init_map () { map.height = 64; map.offset_x = 0; map.offset_y = 0; - map.cells = malloc(map.width * map.height); - uint16_t x, y, ran; - char terrain; + uint32_t size = map.width * map.height; + map.cells = malloc(size); + uint16_t y, x; for (y = 0; y < map.height; y++) - for (x = 0; x < map.width; x++) { - terrain = '.'; - 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) - || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1)) - terrain = ' '; - map.cells[(y * map.width) + x] = terrain; } + for (x = 0; x < map.width; x++) + map.cells[(y * map.width) + x] = '~'; + map.cells[size / 2 + (map.width / 2)] = '.'; + uint32_t repeats, root, curpos; + for (root = 0; root * root * root < size; root++); + for (repeats = 0; repeats < size * root; repeats++) { + y = rrand(0, 0) % map.height; + x = rrand(0, 0) % map.width; + curpos = y * map.width + x; + if ('~' == map.cells[curpos] && + ( (curpos >= map.width && '.' == map.cells[curpos - map.width]) + || (curpos < map.width * (map.height - 1) && '.' == map.cells[curpos + map.width]) + || (curpos > 0 && '.' == map.cells[curpos - 1] && curpos % map.width != 0) + || (curpos < (map.width * map.height) && '.' == map.cells[curpos + 1]) && (curpos + 1) % map.width != 0)) + map.cells[y * map.width + x] = '.'; } return map; } void map_scroll (struct Map * map, char dir) { -- 2.30.2