home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[plomrogue] / src / main.c
index b27cf5d5724bfd446b484e90ba045d95ca019b43..c9e468bbe3aab0d339792f61d66983d0bb6c42b6 100644 (file)
@@ -3,32 +3,32 @@
 #include "main.h" /* for world global */
 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE */
 #include <stdio.h> /* for FILE typedef, F_OK */
-#include <ncurses.h> /* for initscr(), noecho(), curs_set(), keypad(), raw() */
+#include <ncurses.h> /* for noecho(), curs_set(), keypad(), raw() */
 #include <time.h> /* for time() */
 #include <unistd.h> /* for getopt(), optarg */
 #include <stdint.h> /* for uint32_t */
 #include "windows.h" /* for structs WinMeta, Win, init_win_meta(),
                       * draw_all_wins()
                       */
-#include "readwrite.h" /* for read_uint32_bigendian](),
+#include "readwrite.h" /* for try_fgetc(), read_uint32_bigendian(),
                         * write_uint32_bigendian(), try_fopen(), try_fclose(),
-                        * try_fclose_unlink_rename()
+                        * try_fclose_unlink_rename(), try_fgetc_noeof(),
                         */
 #include "map_objects.h" /* for structs MapObj, init_map_object_defs(),
-                          * build_map_objects(), get_player()
+                          * add_map_objects(), get_player()
                           */
 #include "map.h" /* for struct Map, init_map() */
-#include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
-                   * try_calloc(), check_tempfile(), check_xor_files(),
-                   * load_interface_conf(), load_game()
+#include "misc.h" /* for update_log(), save_game(), try_calloc(), load_game(),
+                   * check_tempfile(), check_files_xor(), load_interface_conf(),
+                   * rrand()
                    */
 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
-#include "rrand.h" /* for rrand(), rrand_seed() */
-#include "rexit.h" /* for exit_game(), exit_err() */
+#include "rexit.h" /* for exit_game() */
 #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */
 #include "control.h" /* for control_by_id(), player_control(),
                       * get_available_keycode_to_action()
                       */
+#include "map_object_actions.h" /* for init_map_object_actions() */
 
 
 
@@ -37,8 +37,11 @@ int main(int argc, char *argv[])
     char * f_name = "main()";
     world.turn = 0;        /* Turns to 1 when map and objects are initalized. */
 
+    /* Initialize commands and map object actions. */
     init_command_db();
     set_cleanup_flag(CLEANUP_COMMAND_DB);
+    init_map_object_actions();
+    set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
 
     /* Check for corrupted savefile / recordfile savings. */
     char * recordfile = "record";
@@ -89,8 +92,6 @@ int main(int argc, char *argv[])
     world.map_obj_count = 0;
 
     /* For interactive mode, try to load world state from savefile. */
-    char * err_r = "Trouble loading game (in main()) / "
-                   "reading from opened 'savefile'.";
     FILE * file;
     if (1 == world.interactive && 0 == access(savefile, F_OK))
     {
@@ -101,12 +102,10 @@ int main(int argc, char *argv[])
     /* For non-interactive mode, try to load world state from record file. */
     else
     {
-        err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
-                "in main()).";
         if (0 == world.interactive)
         {
             file = try_fopen(recordfile, "r", f_name);
-            exit_err(read_uint32_bigendian(file, &world.seed), err_r);
+            world.seed = read_uint32_bigendian(file);
         }
 
         /* For interactive-mode in newly started world, generate a start seed
@@ -115,20 +114,18 @@ int main(int argc, char *argv[])
         else
         {
             world.seed = time(NULL);
-
-            char * err_w = "Trouble recording new seed "
-                           "(write_uint32_bigendian() in main()) / writing to "
-                           "file 'record_tmp'.";
             file = try_fopen(recordfile_tmp, "w", f_name);
-            exit_err(write_uint32_bigendian(world.seed, file), err_w);
+            write_uint32_bigendian(world.seed, file);
             try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name);
         }
+        world.mapseed = world.seed;
     }
 
     /* Generate map from seed and, if newly generated world, start positions of
      * actors.
      */
-    rrand_seed(world.seed);
+    uint32_t restore_seed = world.seed;
+    world.seed = world.mapseed;
     struct Map map = init_map();
     world.map = &map;
     set_cleanup_flag(CLEANUP_MAP);
@@ -144,18 +141,17 @@ int main(int argc, char *argv[])
         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
         world.turn = 1;
     }
+    world.seed = restore_seed;
 
     /* Initialize window system and windows. */
-    WINDOW * screen = initscr();
+    init_win_meta();
     set_cleanup_flag(CLEANUP_NCURSES);
     noecho();
     curs_set(0);
-    keypad(screen, TRUE);
+    keypad(world.wmeta->screen, TRUE);
     raw();
-    init_win_meta(screen);
-    set_cleanup_flag(CLEANUP_WIN_META);
     load_interface_conf();
-    set_cleanup_flag(CLEANUP_INTERFACE_CONF);
+    set_cleanup_flag(CLEANUP_INTERFACE);
 
     /* Focus map on player. */
     struct MapObj * player = get_player();
@@ -163,7 +159,7 @@ int main(int argc, char *argv[])
     win_map->center = player->pos;
 
     /* Initialize player's inventory selection index to start position. */
-    world.inventory_select = 0;
+    world.inventory_sel = 0;
 
     /* Replay mode. */
     int key;
@@ -175,14 +171,15 @@ int main(int argc, char *argv[])
         {
             while (world.turn != start_turn)
             {
-                action = getc(file);
+                action = try_fgetc(file, f_name);
                 if (EOF == action)
                 {
                     break;
                 }
-                if (is_command_id_shortdsc(action, "drop"))
+                if (   is_command_id_shortdsc(action, "drop")
+                    || is_command_id_shortdsc(action, "use"))
                 {
-                    world.inventory_select = getc(file);
+                    world.inventory_sel = try_fgetc_noeof(file, f_name);
                 }
                 player_control_by_id(action);
             }
@@ -200,12 +197,13 @@ int main(int argc, char *argv[])
             if (   EOF != action
                 && key == get_available_keycode_to_action("wait"))
             {
-                action = getc(file);
+                action = try_fgetc(file, f_name);
                 if (EOF != action)
                 {
-                    if (is_command_id_shortdsc(action, "drop"))
+                    if (   is_command_id_shortdsc(action, "drop")
+                        || is_command_id_shortdsc(action, "use"))
                     {
-                        world.inventory_select = getc(file);
+                        world.inventory_sel = try_fgetc_noeof(file, f_name);
                     }
                     player_control_by_id(action);
                 }