home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[plomrogue] / src / main.c
1 /* main.c */
2
3 #include "main.h" /* for world global */
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE */
5 #include <stdio.h> /* for FILE typedef, F_OK */
6 #include <ncurses.h> /* for noecho(), curs_set(), keypad(), raw() */
7 #include <time.h> /* for time() */
8 #include <unistd.h> /* for getopt(), optarg */
9 #include <stdint.h> /* for uint32_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win_meta(),
11                       * draw_all_wins()
12                       */
13 #include "readwrite.h" /* for try_fgetc(), read_uint32_bigendian(),
14                         * write_uint32_bigendian(), try_fopen(), try_fclose(),
15                         * try_fclose_unlink_rename(), try_fgetc_noeof(),
16                         */
17 #include "map_objects.h" /* for structs MapObj, init_map_object_defs(),
18                           * add_map_objects(), get_player()
19                           */
20 #include "map.h" /* for struct Map, init_map() */
21 #include "misc.h" /* for update_log(), save_game(), try_calloc(), load_game(),
22                    * check_tempfile(), check_files_xor(), load_interface_conf(),
23                    * rrand()
24                    */
25 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
26 #include "rexit.h" /* for exit_game() */
27 #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */
28 #include "control.h" /* for control_by_id(), player_control(),
29                       * get_available_keycode_to_action()
30                       */
31 #include "map_object_actions.h" /* for init_map_object_actions() */
32
33
34
35 int main(int argc, char *argv[])
36 {
37     char * f_name = "main()";
38     world.turn = 0;        /* Turns to 1 when map and objects are initalized. */
39
40     /* Initialize commands and map object actions. */
41     init_command_db();
42     set_cleanup_flag(CLEANUP_COMMAND_DB);
43     init_map_object_actions();
44     set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
45
46     /* Check for corrupted savefile / recordfile savings. */
47     char * recordfile = "record";
48     char * savefile = "savefile";
49     char * recordfile_tmp = "record_tmp";
50     char * savefile_tmp   = "savefile_tmp";
51     check_files_xor(savefile, recordfile);
52     check_tempfile(recordfile_tmp);
53     check_tempfile(savefile_tmp);
54     check_tempfile("config/windows/Win_tmp_k");
55     check_tempfile("config/windows/Win_tmp_m");
56     check_tempfile("config/windows/Win_tmp_i");
57     check_tempfile("config/windows/Win_tmp_l");
58     check_tempfile("config/windows/toggle_order_tmp");
59
60     /* Read in startup options (i.e. replay option and replay start turn). */
61     int opt;
62     uint32_t start_turn;
63     world.interactive = 1;
64     while ((opt = getopt(argc, argv, "s::")) != -1)
65     {
66         switch (opt)
67         {
68             case 's':
69             {
70                 world.interactive = 0;
71                 start_turn = 0;
72                 if (optarg)
73                 {
74                     start_turn = atoi(optarg);
75                 }
76                 break;
77             }
78             default:
79             {
80                 exit(EXIT_FAILURE);
81             }
82         }
83     }
84
85     /* Initialize log and map object definitions. */
86     world.score = 0;
87     world.log = try_calloc(1, sizeof(char), f_name);
88     set_cleanup_flag(CLEANUP_LOG);
89     update_log(" ");
90     init_map_object_defs("config/defs");
91     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
92     world.map_obj_count = 0;
93
94     /* For interactive mode, try to load world state from savefile. */
95     FILE * file;
96     if (1 == world.interactive && 0 == access(savefile, F_OK))
97     {
98         load_game();
99         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
100     }
101
102     /* For non-interactive mode, try to load world state from record file. */
103     else
104     {
105         if (0 == world.interactive)
106         {
107             file = try_fopen(recordfile, "r", f_name);
108             world.seed = read_uint32_bigendian(file);
109         }
110
111         /* For interactive-mode in newly started world, generate a start seed
112          * from the current time.
113          */
114         else
115         {
116             world.seed = time(NULL);
117             file = try_fopen(recordfile_tmp, "w", f_name);
118             write_uint32_bigendian(world.seed, file);
119             try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name);
120         }
121         world.mapseed = world.seed;
122     }
123
124     /* Generate map from seed and, if newly generated world, start positions of
125      * actors.
126      */
127     uint32_t restore_seed = world.seed;
128     world.seed = world.mapseed;
129     struct Map map = init_map();
130     world.map = &map;
131     set_cleanup_flag(CLEANUP_MAP);
132     if (0 == world.turn)
133     {
134         world.map_objs = NULL;
135         add_map_objects(0, 1);
136         add_map_objects(1, 1 + rrand() % 27);
137         add_map_objects(2, 1 + rrand() % 9);
138         add_map_objects(3, 1 + rrand() % 3);
139         add_map_objects(4, 1 + rrand() % 3);
140         add_map_objects(5, 1 + rrand() % 3);
141         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
142         world.turn = 1;
143     }
144     world.seed = restore_seed;
145
146     /* Initialize window system and windows. */
147     init_win_meta();
148     set_cleanup_flag(CLEANUP_NCURSES);
149     noecho();
150     curs_set(0);
151     keypad(world.wmeta->screen, TRUE);
152     raw();
153     load_interface_conf();
154     set_cleanup_flag(CLEANUP_INTERFACE);
155
156     /* Focus map on player. */
157     struct MapObj * player = get_player();
158     struct Win * win_map = get_win_by_id('m');
159     win_map->center = player->pos;
160
161     /* Initialize player's inventory selection index to start position. */
162     world.inventory_sel = 0;
163
164     /* Replay mode. */
165     int key;
166     struct WinConf * wc;
167     if (0 == world.interactive)
168     {
169         int action = 0;
170         if (0 != start_turn)
171         {
172             while (world.turn != start_turn)
173             {
174                 action = try_fgetc(file, f_name);
175                 if (EOF == action)
176                 {
177                     break;
178                 }
179                 if (   is_command_id_shortdsc(action, "drop")
180                     || is_command_id_shortdsc(action, "use"))
181                 {
182                     world.inventory_sel = try_fgetc_noeof(file, f_name);
183                 }
184                 player_control_by_id(action);
185             }
186         }
187         while (1)
188         {
189             draw_all_wins();
190             key = getch();
191             wc = get_winconf_by_win(world.wmeta->active);
192             if  (   (1 == wc->view && wingeom_control(key))
193                  || (2 == wc->view && winkeyb_control(key)))
194             {
195                 continue;
196             }
197             if (   EOF != action
198                 && key == get_available_keycode_to_action("wait"))
199             {
200                 action = try_fgetc(file, f_name);
201                 if (EOF != action)
202                 {
203                     if (   is_command_id_shortdsc(action, "drop")
204                         || is_command_id_shortdsc(action, "use"))
205                     {
206                         world.inventory_sel = try_fgetc_noeof(file, f_name);
207                     }
208                     player_control_by_id(action);
209                 }
210             }
211             else if (meta_control(key))
212             {
213                 try_fclose(file, f_name);
214                 exit_game();
215             }
216         }
217     }
218
219     /* Interactive mode. */
220     else
221     {
222         while (1)
223         {
224             save_game();
225             draw_all_wins();
226             key = getch();
227             wc = get_winconf_by_win(world.wmeta->active);
228             if  (   (1 == wc->view && wingeom_control(key))
229                  || (2 == wc->view && winkeyb_control(key))
230                  || (0 != player->lifepoints && player_control_by_key(key)))
231             {
232                 continue;
233             }
234             if (meta_control(key))
235             {
236                 exit_game();
237             }
238         }
239     }
240 }