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