X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/move_up?a=blobdiff_plain;f=src%2Froguelike.c;h=f3da2566bbce59cf45463e469dafc22a8bfc0071;hb=6d5705b43adbc2a4b522f971206007c9073c7144;hp=f0057e295884edbae3ad10dc1819dcd9d07d3c8d;hpb=e9d8b1aca776341c9cdaa2ea6406336661d82a76;p=plomrogue diff --git a/src/roguelike.c b/src/roguelike.c index f0057e2..f3da256 100644 --- a/src/roguelike.c +++ b/src/roguelike.c @@ -18,46 +18,6 @@ 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). -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. - if (0 != win->frame.curses_win) - suspend_win(win_meta, win); - else - append_win(win_meta, win); } - -void scroll_pad (struct WinMeta * win_meta, char dir) { -// Try to scroll pad left or right. - if ('+' == dir) - reset_pad_offset(win_meta, win_meta->pad_offset + 1); - else if ('-' == dir) - reset_pad_offset(win_meta, win_meta->pad_offset - 1); } - -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) { - uint16_t height = win_meta->active->frame.size.y; - uint16_t width = win_meta->active->frame.size.x; - if (change == '-') - height--; - else if (change == '+') - height++; - else if (change == '_') - width--; - else if (change == '*') - width++; - resize_active_win (win_meta, height, width); } } - struct Map init_map () { // Initialize map with some experimental start values. struct Map map; @@ -86,37 +46,51 @@ struct Map init_map () { map.cells[y * map.width + x] = '.'; } return map; } -void map_scroll (struct Map * map, char dir) { -// Scroll map into direction dir if possible by changing the offset. - if ('n' == dir && map->offset_y > 0) - map->offset_y--; - else if ('s' == dir) - map->offset_y++; - else if ('w' == dir && map->offset_x > 0) - map->offset_x--; - else if ('e' == dir) - map->offset_x++; } +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); + write_uint16_bigendian(world->monster->next->y, file); + write_uint16_bigendian(world->monster->next->x, file); + write_uint16_bigendian(world->monster->next->next->y, file); + write_uint16_bigendian(world->monster->next->next->x, file); + fclose(file); } + +void record_action (char action) { +// Append action to game record file. + FILE * file = fopen("record", "a"); + fputc(action, file); + fclose(file); } 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; - if (1 == d) - ty++; - else if (2 == d) - ty--; - else if (3 == d) - tx++; - else if (4 == d) - tx--; - if (tx == world->player->x && ty == world->player->y) - update_log(world, "\nThe monster hits you."); - else if (is_passable(world->map, ty, tx)) { - world->monster->y = ty; - world->monster->x = tx; } } + char d; + struct Monster * monster; + uint16_t ty, tx; + for (monster = world->monster; monster != 0; monster = monster->next) { + d = rrand(0, 0) % 5; + ty = monster->y; + tx = monster->x; + if (1 == d) + ty++; + else if (2 == d) + ty--; + else if (3 == d) + tx++; + else if (4 == d) + tx--; + if (tx == world->player->x && ty == world->player->y) + update_log(world, "\nThe monster hits you."); + else if (is_passable(world->map, ty, tx)) { + monster->y = ty; + monster->x = tx; } } } void update_log (struct World * world, char * text) { // Update log with new text to be appended. @@ -130,20 +104,6 @@ void update_log (struct World * world, char * text) { free(world->log); world->log = new_text; } -char is_passable (struct Map * map, uint16_t y, uint16_t x) { -// Check if coordinate on (or beyond) map is accessible to movement. - char passable = 0; - if (0 <= x && x < map->width && 0 <= y && y < map->height) - if ('.' == map->cells[y * map->width + x]) - 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; @@ -163,9 +123,12 @@ void move_player (struct World * world, char d) { if ('e' == d) { dir = "east"; tx++; } - if (ty == world->monster->y && tx == world->monster->x) - success = 2; - else if (is_passable(world->map, ty, tx)) { + struct Monster * monster; + for (monster = world->monster; monster != 0; monster = monster->next) + if (ty == monster->y && tx == monster->x) { + success = 2; + break; } + if (2 != success && is_passable(world->map, ty, tx)) { success = 1; world->player->y = ty; world->player->x = tx; } @@ -187,6 +150,14 @@ void move_player (struct World * world, char d) { record_action(d); next_turn (world); } +char is_passable (struct Map * map, uint16_t y, uint16_t x) { +// Check if coordinate on (or beyond) map is accessible to movement. + char passable = 0; + if (0 <= x && x < map->width && 0 <= y && y < map->height) + if ('.' == map->cells[y * map->width + x]) + passable = 1; + return passable; } + void player_wait (struct World * world) { // Make player wait one turn. if (1 == world->interactive) @@ -194,6 +165,46 @@ void player_wait (struct World * world) { next_turn (world); update_log (world, "\nYou wait."); } +void toggle_window (struct WinMeta * win_meta, struct Win * win) { +// Toggle display of window win. + if (0 != win->frame.curses_win) + suspend_win(win_meta, win); + else + append_win(win_meta, win); } + +void scroll_pad (struct WinMeta * win_meta, char dir) { +// Try to scroll pad left or right. + if ('+' == dir) + reset_pad_offset(win_meta, win_meta->pad_offset + 1); + else if ('-' == dir) + reset_pad_offset(win_meta, win_meta->pad_offset - 1); } + +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) { + uint16_t height = win_meta->active->frame.size.y; + uint16_t width = win_meta->active->frame.size.x; + if (change == '-') + height--; + else if (change == '+') + height++; + else if (change == '_') + width--; + else if (change == '*') + width++; + resize_active_win (win_meta, height, width); } } + +void map_scroll (struct Map * map, char dir) { +// Scroll map into direction dir if possible by changing the offset. + if ('n' == dir && map->offset_y > 0) + map->offset_y--; + else if ('s' == dir) + map->offset_y++; + else if ('w' == dir && map->offset_x > 0) + map->offset_x--; + else if ('e' == dir) + map->offset_x++; } + 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. @@ -265,8 +276,16 @@ int main (int argc, char *argv[]) { update_log (&world, " "); struct Player player; world.player = &player; - struct Monster monster; - world.monster = &monster; + struct Monster monster1; + struct Monster monster2; + struct Monster monster3; + world.monster = &monster1; + monster1.next = &monster2; + monster2.next = &monster3; + monster3.next = 0; + monster1.name = 'A'; + monster2.name = 'B'; + monster3.name = 'C'; FILE * file; if (1 == world.interactive && 0 == access("savefile", F_OK)) { file = fopen("savefile", "r"); @@ -274,8 +293,12 @@ int main (int argc, char *argv[]) { world.turn = read_uint32_bigendian(file); player.y = read_uint16_bigendian(file); player.x = read_uint16_bigendian(file); - monster.y = read_uint16_bigendian(file); - monster.x = read_uint16_bigendian(file); + monster1.y = read_uint16_bigendian(file); + monster1.x = read_uint16_bigendian(file); + monster2.y = read_uint16_bigendian(file); + monster2.x = read_uint16_bigendian(file); + monster3.y = read_uint16_bigendian(file); + monster3.x = read_uint16_bigendian(file); fclose(file); } else { world.turn = 1; @@ -294,9 +317,11 @@ int main (int argc, char *argv[]) { for (player.y = player.x = 0; 0 == is_passable(&map, player.y, player.x);) { player.y = rrand(0, 0) % map.height; player.x = rrand(0, 0) % map.width; } - for (monster.y = monster.x = 0; 0 == is_passable(&map, monster.y, monster.x);) { - monster.y = rrand(0, 0) % map.height; - monster.x = rrand(0, 0) % map.width; } } + struct Monster * monster; + for (monster = world.monster; monster != 0; monster = monster->next) + for (monster->y = monster->x = 0; 0 == is_passable(&map, monster->y, monster->x);) { + monster->y = rrand(0, 0) % map.height; + monster->x = rrand(0, 0) % map.width; } } WINDOW * screen = initscr(); noecho();