home · contact · privacy
Improved (and simplified) pseudo-randomness.
[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(), rrand()
24                    */
25 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
26 #include "rexit.h" /* for exit_game(), exit_err() */
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_MAPOBJACTS);
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     char * err_r = "Trouble loading game (in main()) / "
96                    "reading from opened 'savefile'.";
97     FILE * file;
98     if (1 == world.interactive && 0 == access(savefile, F_OK))
99     {
100         load_game();
101         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
102     }
103
104     /* For non-interactive mode, try to load world state from record file. */
105     else
106     {
107         err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
108                 "in main()).";
109         if (0 == world.interactive)
110         {
111             file = try_fopen(recordfile, "r", f_name);
112             exit_err(read_uint32_bigendian(file, &world.seed), err_r);
113         }
114
115         /* For interactive-mode in newly started world, generate a start seed
116          * from the current time.
117          */
118         else
119         {
120             world.seed = time(NULL);
121
122             char * err_w = "Trouble recording new seed "
123                            "(write_uint32_bigendian() in main()) / writing to "
124                            "file 'record_tmp'.";
125             file = try_fopen(recordfile_tmp, "w", f_name);
126             exit_err(write_uint32_bigendian(world.seed, file), err_w);
127             try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name);
128         }
129         world.mapseed = world.seed;
130     }
131
132     /* Generate map from seed and, if newly generated world, start positions of
133      * actors.
134      */
135     uint32_t restore_seed = world.seed;
136     world.seed = world.mapseed;
137     struct Map map = init_map();
138     world.map = &map;
139     set_cleanup_flag(CLEANUP_MAP);
140     if (0 == world.turn)
141     {
142         world.map_objs = NULL;
143         add_map_objects(0, 1);
144         add_map_objects(1, 1 + rrand() % 27);
145         add_map_objects(2, 1 + rrand() % 9);
146         add_map_objects(3, 1 + rrand() % 3);
147         add_map_objects(4, 1 + rrand() % 3);
148         add_map_objects(5, 1 + rrand() % 3);
149         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
150         world.turn = 1;
151     }
152     world.seed = restore_seed;
153
154     /* Initialize window system and windows. */
155     WINDOW * screen = initscr();
156     set_cleanup_flag(CLEANUP_NCURSES);
157     noecho();
158     curs_set(0);
159     keypad(screen, TRUE);
160     raw();
161     init_win_meta(screen);
162     set_cleanup_flag(CLEANUP_WIN_META);
163     load_interface_conf();
164     set_cleanup_flag(CLEANUP_INTERFACE_CONF);
165
166     /* Focus map on player. */
167     struct MapObj * player = get_player();
168     struct Win * win_map = get_win_by_id('m');
169     win_map->center = player->pos;
170
171     /* Initialize player's inventory selection index to start position. */
172     world.inventory_select = 0;
173
174     /* Replay mode. */
175     int key;
176     struct WinConf * wc;
177     if (0 == world.interactive)
178     {
179         int action = 0;
180         if (0 != start_turn)
181         {
182             while (world.turn != start_turn)
183             {
184                 action = getc(file);
185                 if (EOF == action)
186                 {
187                     break;
188                 }
189                 if (   is_command_id_shortdsc(action, "drop")
190                     || is_command_id_shortdsc(action, "use"))
191                 {
192                     world.inventory_select = getc(file);
193                 }
194                 player_control_by_id(action);
195             }
196         }
197         while (1)
198         {
199             draw_all_wins();
200             key = getch();
201             wc = get_winconf_by_win(world.wmeta->active);
202             if  (   (1 == wc->view && wingeom_control(key))
203                  || (2 == wc->view && winkeyb_control(key)))
204             {
205                 continue;
206             }
207             if (   EOF != action
208                 && key == get_available_keycode_to_action("wait"))
209             {
210                 action = getc(file);
211                 if (EOF != action)
212                 {
213                     if (   is_command_id_shortdsc(action, "drop")
214                         || is_command_id_shortdsc(action, "use"))
215                     {
216                         world.inventory_select = getc(file);
217                     }
218                     player_control_by_id(action);
219                 }
220             }
221             else if (meta_control(key))
222             {
223                 try_fclose(file, f_name);
224                 exit_game();
225             }
226         }
227     }
228
229     /* Interactive mode. */
230     else
231     {
232         while (1)
233         {
234             save_game();
235             draw_all_wins();
236             key = getch();
237             wc = get_winconf_by_win(world.wmeta->active);
238             if  (   (1 == wc->view && wingeom_control(key))
239                  || (2 == wc->view && winkeyb_control(key))
240                  || (0 != player->lifepoints && player_control_by_key(key)))
241             {
242                 continue;
243             }
244             if (meta_control(key))
245             {
246                 exit_game();
247             }
248         }
249     }
250 }