home · contact · privacy
Made game exiting and cleaning up more flexible. Provided so far unused exit-on-error...
[plomrogue] / src / main.c
1 /* main.c */
2
3 #include "main.h"
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE, calloc() */
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 "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
10                       * draw_all_wins()
11                       */
12 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
13                         * draw_log_win()
14                         */
15 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
16 #include "readwrite.h" /* for read_uint16_bigendian, read_uint32_bigendian,
17                         * write_uint32_bigendian
18                         */
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20                           * init_map_object_defs(), read_map_objects(),
21                           * build_map_objects()
22                           */
23 #include "map_object_actions.h" /* for player_wait(), move_player() */
24 #include "map.h" /* for struct Map, init_map() */
25 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
26                    * meta_keys(), save_game()
27                    */
28 #include "yx_uint16.h" /* for dir enum */
29 #include "rrand.h" /* for rrand(), rrand_seed() */
30 #include "rexit.h" /* for exit_game() */
31
32 int main(int argc, char *argv[])
33 {
34     struct World world;
35
36     /* Read in startup options (i.e. replay option and replay start turn). */
37     int opt;
38     uint32_t start_turn;
39     world.interactive = 1;
40     while ((opt = getopt(argc, argv, "s::")) != -1)
41     {
42         switch (opt)
43         {
44             case 's':
45                 world.interactive = 0;
46                 start_turn = 0;
47                 if (optarg)
48                     start_turn = atoi(optarg);
49                  break;
50             default:
51                 exit(EXIT_FAILURE);
52         }
53     }
54
55     /* Initialize log, player, monster/item definitions and monsters/items. */
56     world.log = calloc(1, sizeof(char));
57     set_cleanup_flag(CLEANUP_LOG);
58     update_log (&world, " ");
59     struct Player player;
60     player.hitpoints = 5;
61     world.player = &player;
62     world.monster = 0;
63     world.item = 0;
64     init_map_object_defs(&world, "defs");
65
66     /* For interactive mode, try to load world state from savefile. */
67     FILE * file;
68     if (1 == world.interactive && 0 == access("savefile", F_OK))
69     {
70         file = fopen("savefile", "r");
71         world.seed = read_uint32_bigendian(file);
72         world.turn = read_uint32_bigendian(file);
73         player.pos.y = read_uint16_bigendian(file) - 1;
74         player.pos.x = read_uint16_bigendian(file) - 1;
75         player.hitpoints = fgetc(file);
76         read_map_objects(&world, &world.monster, file);
77         read_map_objects(&world, &world.item,    file);
78         fclose(file);
79     }
80
81     /* For non-interactive mode, try to load world state from record file. */
82     else
83     {
84         world.turn = 1;
85         if (0 == world.interactive)
86         {
87             file = fopen("record", "r");
88             world.seed = read_uint32_bigendian(file);
89         }
90
91       /* For interactive-mode in newly started world, generate a start seed
92        * from the current time.
93        */
94       else
95       {
96           file = fopen("record", "w");
97           world.seed = time(NULL);
98           write_uint32_bigendian(world.seed, file);
99           fclose(file);
100       }
101   }
102
103     /* Generate map from seed and, if newly generated world, start positions of
104      * actors.
105      */
106     rrand_seed(world.seed);
107     struct Map map = init_map();
108     world.map = &map;
109     set_cleanup_flag(CLEANUP_MAP);
110     if (1 == world.turn)
111     {
112         player.pos = find_passable_pos(&map);
113         void * foo;
114         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
115         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
116         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
117         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
118         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
119     }
120
121     /* Initialize window system and windows. */
122     WINDOW * screen = initscr();
123     set_cleanup_flag(CLEANUP_NCURSES);
124     noecho();
125     curs_set(0);
126     keypad(screen, TRUE);
127     raw();
128     init_keybindings(&world);
129     set_cleanup_flag(CLEANUP_KEYBINDINGS);
130     struct WinMeta win_meta = init_win_meta(screen);
131     struct Win win_keys = init_win(&win_meta, "Keys",
132                                    0, 29, &world, draw_keys_win);
133     struct Win win_info = init_win(&win_meta, "Info",
134                                    2, 20, &world, draw_info_win);
135     uint16_t height_logwin = win_meta.padframe.size.y
136                              - (2 + win_info.frame.size.y);
137     struct Win win_log = init_win(&win_meta, "Log",
138                                   height_logwin, 20, &world, draw_log_win);
139     uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
140                              - win_log.frame.size.x - 2;
141     struct Win win_map = init_win(&win_meta, "Map",
142                                   0, width_mapwin, &world, draw_map_win);
143     toggle_window(&win_meta, &win_keys);
144     toggle_window(&win_meta, &win_map);
145     toggle_window(&win_meta, &win_info);
146     toggle_window(&win_meta, &win_log);
147
148     /* Replay mode. */
149     int key;
150     unsigned char quit_called = 0;
151     unsigned char await_actions = 1;
152     if (0 == world.interactive)
153     {
154         int action;
155         while (1)
156         {
157             if (start_turn == world.turn)
158             {
159                 start_turn = 0;
160             }
161             if (0 == start_turn)
162             {
163                 draw_all_wins (&win_meta);
164                key = getch();
165             }
166             if (1 == await_actions
167                 && (world.turn < start_turn
168                     || key == get_action_key(world.keybindings,
169                                              "wait / next turn")) )
170             {
171                 action = getc(file);
172                 if (EOF == action)
173                 {
174                     start_turn = 0;
175                     await_actions = 0;
176                 }
177                 else if (0 == action)
178                 {
179                     player_wait (&world);
180                 }
181                 else if (NORTH == action)
182                 {
183                     move_player(&world, NORTH);
184                 }
185                 else if (EAST  == action)
186                 {
187                     move_player(&world, EAST);
188                 }
189                 else if (SOUTH == action)
190                 {
191                     move_player(&world, SOUTH);
192                 }
193                 else if (WEST == action)
194                 {
195                     move_player(&world, WEST);
196                 }
197             }
198             else
199             {
200                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
201                                              &win_map, &win_info, &win_log);
202                 if (1 == quit_called)
203                 {
204                     exit_game(&world);
205                 }
206             }
207         }
208     }
209
210     /* Interactive mode. */
211     else
212     {
213         uint32_t last_turn = 0;
214         while (1)
215         {
216             if (last_turn != world.turn)
217             {
218                 save_game(&world);
219                 last_turn = world.turn;
220             }
221             if (1 == await_actions && 0 == player.hitpoints)
222             {
223                 await_actions = 0;
224             }
225             draw_all_wins (&win_meta);
226             key = getch();
227             if      (1 == await_actions
228                      && key == get_action_key(world.keybindings,
229                                               "player up"))
230             {
231                 move_player(&world, NORTH);
232             }
233             else if (1 == await_actions
234                      && key == get_action_key(world.keybindings,
235                                               "player right"))
236             {
237                 move_player(&world, EAST);
238             }
239             else if (1 == await_actions
240                      && key == get_action_key(world.keybindings,
241                                               "player down"))
242             {
243                 move_player(&world, SOUTH);
244             }
245             else if (1 == await_actions
246                      && key == get_action_key(world.keybindings,
247                                               "player left"))
248             {
249                 move_player(&world, WEST);
250             }
251             else if (1 == await_actions
252                      && key == get_action_key(world.keybindings,
253                                               "wait / next turn"))
254             {
255                 player_wait (&world);
256             }
257             else
258             {
259                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
260                                              &win_map, &win_info, &win_log);
261                 if (1 == quit_called)
262                 {
263                     exit_game(&world);
264                 }
265             }
266         }
267     }
268 }