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(),
21 * read_map_objects_monsterdata,
22 * read_map_objects_itemdata, build_map_objects()
24 #include "map_object_actions.h" /* for player_wait(), move_player() */
25 #include "map.h" /* for struct Map, init_map() */
26 #include "misc.h" /* for rrand(), update_log(), toggle_window(), exit_game(),
27 * find_passable_pos(), meta_keys(), save_game()
29 #include "yx_uint16.h" /* for dir enum */
31 int main(int argc, char *argv[])
35 /* Read in startup options (i.e. replay option and replay start turn). */
38 world.interactive = 1;
39 while ((opt = getopt(argc, argv, "s::")) != -1)
44 world.interactive = 0;
47 start_turn = atoi(optarg);
54 /* Initialize log, player, monster/item definitions and monsters/items. */
55 world.log = calloc(1, sizeof(char));
56 update_log (&world, " ");
59 world.player = &player;
62 init_map_object_defs(&world, "defs");
64 /* For interactive mode, try to load world state from savefile. */
66 if (1 == world.interactive && 0 == access("savefile", F_OK))
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.monster, file, sizeof(struct Monster),
75 read_map_objects_monsterdata);
76 read_map_objects(&world.item, file, sizeof(struct Item), NULL);
80 /* For non-interactive mode, try to load world state from record file. */
84 if (0 == world.interactive)
86 file = fopen("record", "r");
87 world.seed = read_uint32_bigendian(file);
90 /* For interactive-mode in newly started world, generate a start seed
91 * from the current time.
95 file = fopen("record", "w");
96 world.seed = time(NULL);
97 write_uint32_bigendian(world.seed, file);
102 /* Generate map from seed and, if newly generated world, start positions of
105 rrand(1, world.seed);
106 struct Map map = init_map();
110 player.pos = find_passable_pos(&map);
111 void * foo = build_map_objects(&world, &world.monster,
112 0, 1 + rrand(0,0) % 27,
113 sizeof(struct Monster),
114 build_map_objects_monsterdata);
115 foo = build_map_objects(&world, foo, 1, 1 + rrand(0,0) % 9,
116 sizeof(struct Monster),
117 build_map_objects_monsterdata);
118 build_map_objects(&world, foo, 2, 1 + rrand(0,0) % 3,
119 sizeof(struct Monster),
120 build_map_objects_monsterdata);
121 foo = build_map_objects(&world, &world.item, 3, 1 + rrand(0,0) % 3,
123 build_map_objects_itemdata);
124 build_map_objects(&world, foo, 4, 1 + rrand(0,0) % 3,
125 sizeof(struct Item), build_map_objects_itemdata);
128 /* Initialize window system and windows. */
129 WINDOW * screen = initscr();
132 keypad(screen, TRUE);
134 init_keybindings(&world);
135 struct WinMeta win_meta = init_win_meta(screen);
136 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
137 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
138 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
139 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
140 win_keys.frame.size.x = 29;
141 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x
142 - win_log.frame.size.x - 2;
143 win_info.frame.size.y = 2;
144 win_log.frame.size.y = win_meta.pad.size.y - (2 + win_info.frame.size.y);
145 toggle_window(&win_meta, &win_keys);
146 toggle_window(&win_meta, &win_map);
147 toggle_window(&win_meta, &win_info);
148 toggle_window(&win_meta, &win_log);
152 unsigned char quit_called = 0;
153 unsigned char await_actions = 1;
154 if (0 == world.interactive)
159 if (start_turn == world.turn)
165 draw_all_wins (&win_meta);
168 if (1 == await_actions
169 && (world.turn < start_turn
170 || key == get_action_key(world.keybindings,
171 "wait / next turn")) )
179 else if (0 == action)
181 player_wait (&world);
183 else if (NORTH == action)
185 move_player(&world, NORTH);
187 else if (EAST == action)
189 move_player(&world, EAST);
191 else if (SOUTH == action)
193 move_player(&world, SOUTH);
195 else if (WEST == action)
197 move_player(&world, WEST);
202 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
203 &win_map, &win_info, &win_log);
204 if (1 == quit_called)
206 exit_game(&world, &map);
212 /* Interactive mode. */
215 uint32_t last_turn = 0;
218 if (last_turn != world.turn)
221 last_turn = world.turn;
223 if (1 == await_actions && 0 == player.hitpoints)
227 draw_all_wins (&win_meta);
229 if (1 == await_actions
230 && key == get_action_key(world.keybindings,
233 move_player(&world, NORTH);
235 else if (1 == await_actions
236 && key == get_action_key(world.keybindings,
239 move_player(&world, EAST);
241 else if (1 == await_actions
242 && key == get_action_key(world.keybindings,
245 move_player(&world, SOUTH);
247 else if (1 == await_actions
248 && key == get_action_key(world.keybindings,
251 move_player(&world, WEST);
253 else if (1 == await_actions
254 && key == get_action_key(world.keybindings,
257 player_wait (&world);
261 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
262 &win_map, &win_info, &win_log);
263 if (1 == quit_called)
265 exit_game(&world, &map);