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