8 #include "keybindings.h"
10 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
11 // Toggle display of window win.
13 suspend_window(win_meta, win);
15 append_window(win_meta, win); }
17 void growshrink_active_window (struct WinMeta * win_meta, char change) {
18 // Grow or shrink active window horizontally or vertically by one cell size.
19 if (0 != win_meta->active) {
20 uint16_t height = win_meta->active->height;
21 uint16_t width = win_meta->active->width;
24 else if (change == '+')
26 else if (change == '_')
28 else if (change == '*')
30 resize_active_window (win_meta, height, width); } }
32 struct Map init_map () {
33 // Initialize map with some experimental start values.
39 map.cells = malloc(map.width * map.height);
42 for (y = 0; y < map.height; y++)
43 for (x = 0; x < map.width; x++) {
46 if ( 0 == ran % ((x*x) / 3 + 1)
47 || 0 == ran % ((y*y) / 3 + 1)
48 || 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
49 || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1))
51 map.cells[(y * map.width) + x] = terrain; }
54 void map_scroll (struct Map * map, char dir) {
55 // Scroll map into direction dir if possible by changing the offset.
56 if ('n' == dir && map->offset_y > 0)
60 else if ('w' == dir && map->offset_x > 0)
65 void next_turn (struct World * world) {
66 // Increment turn and move enemy.
69 char ty = world->monster->y;
70 char tx = world->monster->x;
79 if (tx == world->player->x && ty == world->player->y)
80 update_log(world, "\nThe monster hits you.");
81 else if (is_passable(world, tx, ty)) {
82 world->monster->y = ty;
83 world->monster->x = tx; } }
85 void update_log (struct World * world, char * text) {
86 // Update log with new text to be appended.
88 uint16_t len_old = strlen(world->log);
89 uint16_t len_new = strlen(text);
90 uint16_t len_whole = len_old + len_new + 1;
91 new_text = calloc(len_whole, sizeof(char));
92 memcpy(new_text, world->log, len_old);
93 memcpy(new_text + len_old, text, len_new);
95 world->log = new_text; }
97 char is_passable (struct World * world, uint16_t x, uint16_t y) {
98 // Check if coordinate on (or beyond) map is accessible to movement.
100 if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
101 if ('.' == world->map->cells[y * world->map->width + x])
105 void move_player (struct World * world, char d) {
106 // Move player in direction d, increment turn counter and update log.
107 static char prev = 0;
110 char ty = world->player->y;
111 char tx = world->player->x;
124 if (ty == world->monster->y && tx == world->monster->x)
126 else if (is_passable(world, tx, ty)) {
128 world->player->y = ty;
129 world->player->x = tx; }
130 if (success * d == prev)
131 update_log (world, ".");
134 update_log (world, "\nYou hit the monster.");
136 char * msg = calloc(25, sizeof(char));
137 char * msg_content = "You fail to move";
139 msg_content = "You move";
140 sprintf(msg, "\n%s %s.", msg_content, dir);
141 update_log (world, msg);
146 void player_wait(struct World * world) {
147 // Make player wait one turn.
149 update_log (world, "\nYou wait."); }
153 init_keybindings(&world);
155 world.log = calloc(1, sizeof(char));
156 update_log (&world, "Start!");
157 struct Map map = init_map();
159 struct Player player;
162 world.player = &player;
163 struct Monster monster;
166 world.monster = &monster;
168 WINDOW * screen = initscr();
171 keypad(screen, TRUE);
173 struct WinMeta win_meta = init_win_meta(screen);
174 struct Win win_keys = init_window(&win_meta, "Keys", &world, draw_keys_win);
175 struct Win win_map = init_window(&win_meta, "Map", &world, draw_map_win);
176 struct Win win_info = init_window(&win_meta, "Info", &world, draw_info_win);
177 struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win);
181 draw_all_windows (&win_meta);
183 if (key == get_action_key(world.keybindings, "quit"))
185 else if (key == get_action_key(world.keybindings, "scroll pad right"))
186 scroll_pad (&win_meta, '+');
187 else if (key == get_action_key(world.keybindings, "scroll pad left"))
188 scroll_pad (&win_meta, '-');
189 else if (key == get_action_key(world.keybindings, "toggle keys window"))
190 toggle_window(&win_meta, &win_keys);
191 else if (key == get_action_key(world.keybindings, "toggle map window"))
192 toggle_window(&win_meta, &win_map);
193 else if (key == get_action_key(world.keybindings, "toggle info window"))
194 toggle_window(&win_meta, &win_info);
195 else if (key == get_action_key(world.keybindings, "toggle log window"))
196 toggle_window(&win_meta, &win_log);
197 else if (key == get_action_key(world.keybindings, "cycle forwards"))
198 cycle_active_window(&win_meta, 'n');
199 else if (key == get_action_key(world.keybindings, "cycle backwards"))
200 cycle_active_window(&win_meta, 'p');
201 else if (key == get_action_key(world.keybindings, "shift forwards"))
202 shift_active_window(&win_meta, 'f');
203 else if (key == get_action_key(world.keybindings, "shift backwards"))
204 shift_active_window(&win_meta, 'b');
205 else if (key == get_action_key(world.keybindings, "grow horizontally"))
206 growshrink_active_window(&win_meta, '*');
207 else if (key == get_action_key(world.keybindings, "shrink horizontally"))
208 growshrink_active_window(&win_meta, '_');
209 else if (key == get_action_key(world.keybindings, "grow vertically"))
210 growshrink_active_window(&win_meta, '+');
211 else if (key == get_action_key(world.keybindings, "shrink vertically"))
212 growshrink_active_window(&win_meta, '-');
213 else if (key == get_action_key(world.keybindings, "save keys"))
214 save_keybindings(&world);
215 else if (key == get_action_key(world.keybindings, "keys nav up"))
216 keyswin_move_selection (&world, 'u');
217 else if (key == get_action_key(world.keybindings, "keys nav down"))
218 keyswin_move_selection (&world, 'd');
219 else if (key == get_action_key(world.keybindings, "keys mod"))
220 keyswin_mod_key (&world, &win_meta);
221 else if (key == get_action_key(world.keybindings, "map up"))
222 map_scroll (&map, 'n');
223 else if (key == get_action_key(world.keybindings, "map down"))
224 map_scroll (&map, 's');
225 else if (key == get_action_key(world.keybindings, "map right"))
226 map_scroll (&map, 'e');
227 else if (key == get_action_key(world.keybindings, "map left"))
228 map_scroll (&map, 'w');
229 else if (key == get_action_key(world.keybindings, "player down"))
230 move_player(&world, 's');
231 else if (key == get_action_key(world.keybindings, "player up"))
232 move_player(&world, 'n');
233 else if (key == get_action_key(world.keybindings, "player right"))
234 move_player(&world, 'e');
235 else if (key == get_action_key(world.keybindings, "player left"))
236 move_player(&world, 'w');
237 else if (key == get_action_key(world.keybindings, "wait") )
238 player_wait (&world); }
241 for (key = 0; key <= world.keyswindata->max; key++)
242 free(world.keybindings[key].name);
243 free(world.keybindings);
244 free(world.keyswindata);