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(),
12 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
15 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
16 #include "readwrite.h" /* for read_uint16_bigendian, read_uint32_bigendian,
17 * write_uint32_bigendian
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20 * init_map_object_defs(), read_map_objects(),
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()
28 #include "yx_uint16.h" /* for dir enum */
29 #include "rrand.h" /* for rrand(), rrand_seed() */
30 #include "rexit.h" /* for exit_game() */
32 int main(int argc, char *argv[])
36 /* Read in startup options (i.e. replay option and replay start turn). */
39 world.interactive = 1;
40 while ((opt = getopt(argc, argv, "s::")) != -1)
45 world.interactive = 0;
48 start_turn = atoi(optarg);
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, " ");
61 world.player = &player;
64 init_map_object_defs(&world, "defs");
66 /* For interactive mode, try to load world state from savefile. */
68 if (1 == world.interactive && 0 == access("savefile", F_OK))
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);
81 /* For non-interactive mode, try to load world state from record file. */
85 if (0 == world.interactive)
87 file = fopen("record", "r");
88 world.seed = read_uint32_bigendian(file);
91 /* For interactive-mode in newly started world, generate a start seed
92 * from the current time.
96 file = fopen("record", "w");
97 world.seed = time(NULL);
98 write_uint32_bigendian(world.seed, file);
103 /* Generate map from seed and, if newly generated world, start positions of
106 rrand_seed(world.seed);
107 struct Map map = init_map();
109 set_cleanup_flag(CLEANUP_MAP);
112 player.pos = find_passable_pos(world.map);
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);
121 /* Initialize window system and windows. */
122 WINDOW * screen = initscr();
123 set_cleanup_flag(CLEANUP_NCURSES);
126 keypad(screen, TRUE);
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);
150 unsigned char quit_called = 0;
151 unsigned char await_actions = 1;
152 if (0 == world.interactive)
157 if (start_turn == world.turn)
163 draw_all_wins (&win_meta);
166 if (1 == await_actions
167 && (world.turn < start_turn
168 || key == get_action_key(world.keybindings,
169 "wait / next turn")) )
177 else if (0 == action)
179 player_wait (&world);
181 else if (NORTH == action)
183 move_player(&world, NORTH);
185 else if (EAST == action)
187 move_player(&world, EAST);
189 else if (SOUTH == action)
191 move_player(&world, SOUTH);
193 else if (WEST == action)
195 move_player(&world, WEST);
200 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
201 &win_map, &win_info, &win_log);
202 if (1 == quit_called)
210 /* Interactive mode. */
213 uint32_t last_turn = 0;
216 if (last_turn != world.turn)
219 last_turn = world.turn;
221 if (1 == await_actions && 0 == player.hitpoints)
225 draw_all_wins (&win_meta);
227 if (1 == await_actions
228 && key == get_action_key(world.keybindings,
231 move_player(&world, NORTH);
233 else if (1 == await_actions
234 && key == get_action_key(world.keybindings,
237 move_player(&world, EAST);
239 else if (1 == await_actions
240 && key == get_action_key(world.keybindings,
243 move_player(&world, SOUTH);
245 else if (1 == await_actions
246 && key == get_action_key(world.keybindings,
249 move_player(&world, WEST);
251 else if (1 == await_actions
252 && key == get_action_key(world.keybindings,
255 player_wait (&world);
259 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
260 &win_map, &win_info, &win_log);
261 if (1 == quit_called)