home · contact · privacy
Corrected comment spellings and line lengths in code.
[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 uint16_t, uint32_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
11                       * draw_all_wins()
12                       */
13 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
14                         * draw_log_win()
15                         */
16 #include "keybindings.h" /* for init_keybindings(), get_action_key() */
17 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](),
18                         * try_fopen(), try_fclose(), try_fclose_unlink_rename()
19                         */
20 #include "map_objects.h" /* for structs Monster, Item, Player,
21                           * init_map_object_defs(), read_map_objects(),
22                           * build_map_objects()
23                           */
24 #include "map.h" /* for struct Map, init_map() */
25 #include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
26                    * try_calloc(), check_tempfile(), check_xor_files()
27                    */
28 #include "wincontrol.h" /* for create_winconfs(), init_winconfs(), init_wins(),
29                          * sorted_wintoggle()
30                          */
31 #include "rrand.h" /* for rrand(), rrand_seed() */
32 #include "rexit.h" /* for exit_game(), exit_err() */
33 #include "control.h" /* for meta_control() */
34 #include "command_db.h" /* for init_command_db() */
35
36
37
38 int main(int argc, char *argv[])
39 {
40     char * f_name = "main()";
41     struct World world;
42     world.turn = 0;        /* Turns to 1 when map and objects are initalized. */
43
44     init_command_db(&world);
45     set_cleanup_flag(CLEANUP_COMMAND_DB);
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, &world);
53     check_tempfile(recordfile_tmp, &world);
54     check_tempfile(savefile_tmp, &world);
55     check_tempfile("config/windows/Win_tmp_k", &world);
56     check_tempfile("config/windows/Win_tmp_m", &world);
57     check_tempfile("config/windows/Win_tmp_i", &world);
58     check_tempfile("config/windows/Win_tmp_l", &world);
59     check_tempfile("config/windows/toggle_order_tmp", &world);
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, player, monster/item definitions and monsters/items. */
87     world.score = 0;
88     world.log = try_calloc(1, sizeof(char), &world, f_name);
89     set_cleanup_flag(CLEANUP_LOG);
90     update_log(&world, " ");
91     struct Player player;
92     player.hitpoints = 5;
93     world.player = &player;
94     world.monster = 0;
95     world.item = 0;
96     init_map_object_defs(&world, "config/defs");
97     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
98
99     /* For interactive mode, try to load world state from savefile. */
100     char * err_r = "Trouble loading game (in main()) / "
101                    "reading from opened 'savefile'.";
102     FILE * file;
103     if (1 == world.interactive && 0 == access(savefile, F_OK))
104     {
105         file = try_fopen(savefile, "r", &world, f_name);
106         if (   read_uint32_bigendian(file, &world.seed)
107             || read_uint32_bigendian(file, &world.turn)
108             || read_uint16_bigendian(file, &world.score)
109             || read_uint16_bigendian(file, &player.pos.y)
110             || read_uint16_bigendian(file, &player.pos.x)
111             || read_uint8(file, &player.hitpoints)
112             || read_map_objects(&world, &world.monster, file)
113             || read_map_objects(&world, &world.item,    file))
114         {
115             exit_err(1, &world, err_r);
116         }
117         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
118         try_fclose(file, &world, f_name);
119         player.pos.y--;
120         player.pos.x--;
121     }
122
123     /* For non-interactive mode, try to load world state from record file. */
124     else
125     {
126         err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
127                 "in main()).";
128         if (0 == world.interactive)
129         {
130             file = try_fopen(recordfile, "r", &world, f_name);
131             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
132         }
133
134         /* For interactive-mode in newly started world, generate a start seed
135          * from the current time.
136          */
137         else
138         {
139             world.seed = time(NULL);
140
141             char * err_w = "Trouble recording new seed "
142                            "(write_uint32_bigendian() in main()) / writing to "
143                            "file 'record_tmp'.";
144             file = try_fopen(recordfile_tmp, "w", &world, f_name);
145             exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
146             try_fclose_unlink_rename(file, recordfile_tmp, recordfile,
147                                     &world, f_name);
148         }
149     }
150
151     /* Generate map from seed and, if newly generated world, start positions of
152      * actors.
153      */
154     rrand_seed(world.seed);
155     struct Map map = init_map();
156     world.map = &map;
157     set_cleanup_flag(CLEANUP_MAP);
158     if (0 == world.turn)
159     {
160         player.pos = find_passable_pos(world.map);
161         void * foo;
162         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
163         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
164         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
165         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
166         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
167         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
168         world.turn = 1;
169     }
170
171     /* Initialize window system and windows. */
172     WINDOW * screen = initscr();
173     set_cleanup_flag(CLEANUP_NCURSES);
174     noecho();
175     curs_set(0);
176     keypad(screen, TRUE);
177     raw();
178     init_keybindings(&world);
179     set_cleanup_flag(CLEANUP_KEYBINDINGS);
180     char * err_winmem = "Trouble with init_win_meta() in main ().";
181     exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem);
182     set_cleanup_flag(CLEANUP_WIN_META);
183     init_winconfs(&world);
184     set_cleanup_flag(CLEANUP_WINCONFS);
185     init_wins(&world);
186     set_cleanup_flag(CLEANUP_WINS);
187     sorted_wintoggle(&world);
188     err_winmem = "Trouble with draw_all_wins() in main().";
189
190     /* Replay mode. */
191     int key;
192     if (0 == world.interactive)
193     {
194         int action = 0;
195         if (0 != start_turn)
196         {
197             while (world.turn != start_turn)
198             {
199                 action = getc(file);
200                 if (EOF == action)
201                 {
202                     break;
203                 }
204                 record_control(action, &world);
205             }
206         }
207         while (1)
208         {
209             draw_all_wins(world.wmeta);
210             key = getch();
211             if (   EOF != action
212                 && key == get_action_key(world.keybindings, "wait"))
213             {
214                 action = getc(file);
215                 if (EOF != action)
216                 {
217                     record_control(action, &world);
218                 }
219             }
220             else if (meta_control(key, &world))
221             {
222                 try_fclose(file, &world, f_name);
223                 exit_game(&world);
224             }
225         }
226     }
227
228     /* Interactive mode. */
229     else
230     {
231         while (1)
232         {
233             save_game(&world);
234             draw_all_wins(world.wmeta);
235             key = getch();
236             if (0 != player.hitpoints && 0 == player_control(key, &world))
237             {
238                 continue;
239             }
240             if (meta_control(key, &world))
241             {
242                 exit_game(&world);
243             }
244         }
245     }
246 }