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