8 struct KeyBinding * keybindings;
9 struct KeysWinData * keyswindata;
13 struct Player * player; };
35 void draw_with_linebreaks (struct Win *, char *, int);
36 void draw_text_from_bottom (struct Win *, char *);
37 void draw_log (struct Win *);
38 void draw_map (struct Win *);
39 void draw_info (struct Win *);
40 void draw_keys_window (struct Win *);
41 void toggle_window (struct WinMeta *, struct Win *);
42 void init_keybindings(struct World *);
43 struct Map init_map ();
44 void update_info (struct World *);
45 void update_log (struct World *, char *);
46 void save_keybindings(struct World *);
47 int get_action_key (struct KeyBinding *, char *);
48 char * get_keyname(int);
50 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
51 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
56 for (y = start_y; y < win->height; y++) {
59 for (x = 0; x < win->width; x++) {
62 if ('\n' == text[z]) {
66 mvwaddch(win->curses, y, x, text[z]);
67 if ('\n' == text[z+1]) {
70 else if (0 == text[z+1]) {
74 void draw_text_from_bottom (struct Win * win, char * text) {
75 // Draw text from end/bottom to the top.
79 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
80 for (x = 0; x < win->width; x++) { // a window of available width, but infinite height.
82 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
83 break; // the loop. Make sure they don't count as cell space themselves.
84 if ('\n' == text[z+1]) {
87 else if (0 == text[z+1]) {
92 if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
93 start_y = win->height - y;
94 else if (y > win->height) {
95 offset = y - win->height;
96 for (y = 0; y < offset; y++)
97 for (x = 0; x < win->width; x++) {
101 if ('\n' == text[z+1]) {
104 text = text + (sizeof(char) * (z + 1)); }
105 draw_with_linebreaks(win, text, start_y); }
107 void draw_log (struct Win * win) {
108 // Draw log text from world struct in win->data from bottom to top.
109 struct World * world = (struct World *) win->data;
110 draw_text_from_bottom(win, world->log); }
112 void draw_map (struct Win * win) {
113 // Draw map determined by win->data Map struct into window. Respect offset.
114 struct World * world = (struct World *) win->data;
115 struct Map * map = world->map;
116 struct Player * player = world->player;
117 char * cells = map->cells;
118 int width_map_av = map->width - map->offset_x;
119 int height_map_av = map->height - map->offset_y;
121 for (y = 0; y < win->height; y++) {
122 z = map->offset_x + (map->offset_y + y) * (map->width);
123 for (x = 0; x < win->width; x++) {
124 if (y < height_map_av && x < width_map_av) {
125 if (z == (map->width * player->y) + player->x)
126 mvwaddch(win->curses, y, x, '@');
128 mvwaddch(win->curses, y, x, cells[z]);
131 void draw_info (struct Win * win) {
132 // Draw info window by appending win->data integer value to "Turn: " display.
133 struct World * world = (struct World *) win->data;
134 int count = world->turn;
136 snprintf(text, 100, "Turn: %d", count);
137 draw_with_linebreaks(win, text, 0); }
139 void draw_keys_window (struct Win * win) {
140 // Draw keybinding window.
141 struct World * world = (struct World *) win->data;
142 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
143 struct KeyBinding * keybindings = world->keybindings;
145 if (keyswindata->max >= win->height) {
146 if (keyswindata->select > win->height / 2) {
147 if (keyswindata->select < (keyswindata->max - (win->height / 2)))
148 offset = keyswindata->select - (win->height / 2);
150 offset = keyswindata->max - win->height + 1; } }
151 int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
152 char * keydesc = malloc(keydescwidth);
156 for (y = 0; y <= keyswindata->max && y < win->height; y++) {
158 if (y == keyswindata->select - offset) {
160 if (1 == keyswindata->edit)
161 attri = attri | A_BLINK; }
162 keyname = get_keyname(keybindings[y + offset].key);
163 snprintf(keydesc, keydescwidth, "%-9s", keyname);
165 for (x = 0; x < win->width; x++)
166 if (x < strlen(keydesc))
167 mvwaddch(win->curses, y, x, keydesc[x] | attri);
168 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
169 mvwaddch(win->curses, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
171 mvwaddch(win->curses, y, x, ' ' | attri); }
174 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
175 // Toggle display of window win.
176 if (0 != win->curses)
177 suspend_window(win_meta, win);
179 append_window(win_meta, win); }
181 void init_keybindings(struct World * world) {
182 // Initialize keybindings from file "keybindings".
183 FILE * file = fopen("keybindings", "r");
192 if (c_count > linemax)
193 linemax = c_count + 1;
196 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
197 fseek(file, 0, SEEK_SET);
198 char * command = malloc(linemax);
201 while (fgets(command, linemax, file)) {
202 keybindings[commcount].key = atoi(command);
203 cmdptr = strchr(command, ' ') + 1;
204 keybindings[commcount].name = malloc(strlen(cmdptr));
205 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
206 keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
210 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
211 keyswindata->max = lines - 1;
212 keyswindata->select = 0;
213 keyswindata->edit = 0;
214 world->keybindings = keybindings;
215 world->keyswindata = keyswindata; }
217 struct Map init_map () {
218 // Initialize map with some experimental start values.
224 map.cells = malloc(map.width * map.height);
226 for (y = 0; y < map.height; y++)
227 for (x = 0; x < map.width; x++)
228 map.cells[(y * map.width) + x] = '.';
229 map.cells[(5 * map.width) + 5] = 'X';
230 map.cells[(3 * map.width) + 8] = 'X';
231 map.cells[(8 * map.width) + 3] = 'X';
234 void update_info (struct World * world) {
235 // Update info data by incrementing turn value.
238 void update_log (struct World * world, char * text) {
239 // Update log with new text to be appended.
241 int len_old = strlen(world->log);
242 int len_new = strlen(text);
243 int len_whole = len_old + len_new + 1;
244 new_text = calloc(len_whole, sizeof(char));
245 memcpy(new_text, world->log, len_old);
246 memcpy(new_text + len_old, text, len_new);
248 world->log = new_text; }
250 void save_keybindings(struct World * world) {
251 // Write keybindings to keybindings file.
252 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
253 struct KeyBinding * keybindings = world->keybindings;
254 FILE * file = fopen("keybindings", "w");
257 for (i = 0; i <= keyswindata->max; i++)
258 if (strlen(keybindings[i].name) > linemax)
259 linemax = strlen(keybindings[i].name);
260 linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
261 char * line = malloc(linemax);
262 for (i = 0; i <= keyswindata->max; i++) {
263 snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
264 fwrite(line, sizeof(char), strlen(line), file); }
268 int get_action_key (struct KeyBinding * keybindings, char * name) {
269 // Return key matching name in keybindings.
271 while (strcmp(keybindings[i].name, name) )
273 return keybindings[i].key; }
275 char * get_keyname(int keycode) {
276 // Translate some keycodes to readable names of max 9 chars.
278 keyname = malloc(15);
279 if (32 < keycode && keycode < 127)
280 sprintf(keyname, "%c", keycode);
281 else if (keycode == 9)
282 sprintf(keyname, "TAB");
283 else if (keycode == 10)
284 sprintf(keyname, "RETURN");
285 else if (keycode == 27)
286 sprintf(keyname, "ESCAPE");
287 else if (keycode == 32)
288 sprintf(keyname, "SPACE");
289 else if (keycode == KEY_UP)
290 sprintf(keyname, "UP");
291 else if (keycode == KEY_DOWN)
292 sprintf(keyname, "DOWN");
293 else if (keycode == KEY_LEFT)
294 sprintf(keyname, "LEFT");
295 else if (keycode == KEY_RIGHT)
296 sprintf(keyname, "RIGHT");
297 else if (keycode == KEY_HOME)
298 sprintf(keyname, "HOME");
299 else if (keycode == KEY_BACKSPACE)
300 sprintf(keyname, "BACKSPACE");
301 else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
302 int f = keycode - KEY_F0;
303 sprintf(keyname, "F%d", f); }
304 else if (keycode == KEY_DC)
305 sprintf(keyname, "DELETE");
306 else if (keycode == KEY_IC)
307 sprintf(keyname, "INSERT");
308 else if (keycode == KEY_NPAGE)
309 sprintf(keyname, "NEXT PAGE");
310 else if (keycode == KEY_PPAGE)
311 sprintf(keyname, "PREV PAGE");
312 else if (keycode == KEY_END)
313 sprintf(keyname, "END");
315 sprintf(keyname, "(unknown)");
320 init_keybindings(&world);
322 world.log = calloc(1, sizeof(char));
323 update_log (&world, "Start!");
324 struct Map map = init_map();
326 struct Player player;
329 world.player = &player;
331 WINDOW * screen = initscr();
334 keypad(screen, TRUE);
336 struct WinMeta win_meta = init_win_meta(screen);
338 struct Win win_keys = init_window(&win_meta, "Keys");
339 win_keys.draw = draw_keys_window;
340 win_keys.data = &world;
342 struct Win win_map = init_window(&win_meta, "Map");
343 win_map.draw = draw_map;
344 win_map.data = &world;
346 struct Win win_info = init_window(&win_meta, "Info");
347 win_info.draw = draw_info;
348 win_info.data = &world;
350 struct Win win_log = init_window(&win_meta, "Log");
351 win_log.draw = draw_log;
352 win_log.data = &world;
356 draw_all_windows (&win_meta);
358 if (key == get_action_key(world.keybindings, "quit"))
360 else if (key == get_action_key(world.keybindings, "scroll pad right"))
361 win_meta.pad_offset++;
362 else if (key == get_action_key(world.keybindings, "scroll pad left") && win_meta.pad_offset > 0)
363 win_meta.pad_offset--;
364 else if (key == get_action_key(world.keybindings, "toggle keys window"))
365 toggle_window(&win_meta, &win_keys);
366 else if (key == get_action_key(world.keybindings, "toggle map window"))
367 toggle_window(&win_meta, &win_map);
368 else if (key == get_action_key(world.keybindings, "toggle info window"))
369 toggle_window(&win_meta, &win_info);
370 else if (key == get_action_key(world.keybindings, "toggle log window"))
371 toggle_window(&win_meta, &win_log);
372 else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
373 cycle_active_window(&win_meta, 'n');
374 else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
375 cycle_active_window(&win_meta, 'p');
376 else if (key == get_action_key(world.keybindings, "shift forwards") && win_meta.active != 0)
377 shift_window(&win_meta, 'f');
378 else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
379 shift_window(&win_meta, 'b');
380 else if (key == get_action_key(world.keybindings, "grow horizontally") && win_meta.active != 0)
381 resize_window(&win_meta, '*');
382 else if (key == get_action_key(world.keybindings, "shrink horizontally") && win_meta.active != 0)
383 resize_window(&win_meta, '_');
384 else if (key == get_action_key(world.keybindings, "grow vertically") && win_meta.active != 0)
385 resize_window(&win_meta, '+');
386 else if (key == get_action_key(world.keybindings, "shrink vertically") && win_meta.active != 0)
387 resize_window(&win_meta, '-');
388 else if (key == get_action_key(world.keybindings, "save keys"))
389 save_keybindings(&world);
390 else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0)
391 world.keyswindata->select--;
392 else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max)
393 world.keyswindata->select++;
394 else if (key == get_action_key(world.keybindings, "keys mod")) {
395 world.keyswindata->edit = 1;
396 draw_all_windows (&win_meta);
398 if (key < 1000) // ensure maximum of three digits in key code field
399 world.keybindings[world.keyswindata->select].key = key;
400 world.keyswindata->edit = 0; }
401 else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0)
403 else if (key == get_action_key(world.keybindings, "map down"))
405 else if (key == get_action_key(world.keybindings, "map right"))
407 else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0)
409 else if (key == get_action_key(world.keybindings, "player down") && player.y < map.height - 1) {
410 update_info (&world);
411 update_log (&world, "\nYou move south.");
413 else if (key == get_action_key(world.keybindings, "player up") && player.y > 0) {
414 update_info (&world);
415 update_log (&world, "\nYou move north.");
417 else if (key == get_action_key(world.keybindings, "player right") && player.x < map.width - 1) {
418 update_info (&world);
419 update_log (&world, "\nYou move east.");
421 else if (key == get_action_key(world.keybindings, "player left") && player.x > 0) {
422 update_info (&world);
423 update_log (&world, "\nYou move west.");
425 else if (key == get_action_key(world.keybindings, "wait") ) {
426 update_info (&world);
427 update_log (&world, "\nYou wait."); } }
430 for (key = 0; key <= world.keyswindata->max; key++)
431 free(world.keybindings[key].name);
432 free(world.keybindings);
433 free(world.keyswindata);