-void draw_keys_window (struct Win * win) {
-// Draw keybinding window.
- struct World * world = (struct World *) win->data;
- struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
- struct KeyBinding * keybindings = world->keybindings;
- int height_av = win->height - 1;
- int width_av = win->width - 1;
- int offset = 0;
- if (keyswindata->max >= height_av) {
- if (keyswindata->select > height_av / 2)
- if (keyswindata->select < (keyswindata->max - (height_av / 2)))
- offset = keyswindata->select - (height_av / 2);
- else
- offset = keyswindata->max - height_av + 1; }
- int keydescwidth = 3;
- char * keydesc = malloc(keydescwidth + 1);
- attr_t attri;
- int y, x;
- for (y = 0; 0 != keybindings[offset + y].name && y < height_av; y++) {
- attri = 0;
- if (y == keyswindata->select - offset) {
- attri = A_REVERSE;
- if (1 == keyswindata->edit)
- attri = attri | A_BLINK; }
- snprintf(keydesc, keydescwidth + 1, "%3d ", keybindings[y + offset].key);
- for (x = 0; x < width_av; x++)
- if (strlen(keydesc) > x)
- mvwaddch(win->curses_win, y + 1, x + win->border_left, keydesc[x] | attri);
- else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
- mvwaddch(win->curses_win, y + 1, x + win->border_left, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
- else
- mvwaddch(win->curses_win, y + 1, x + win->border_left, ' ' | attri); }
- free(keydesc);
- if (y < height_av)
- for (; y < height_av; y++)
- for (x = 0; x < width_av; x++)
- mvwaddch(win->curses_win, y + 1, x + win->border_left, ' '); }
-
-void init_keybindings(struct World * world) {
-// Initialize keybindings from file "keybindings".
- FILE * file = fopen("keybindings", "r");
- int lines = 0;
- int c = 0;
- int linemax = 0;
- int c_count = 0;
- while (EOF != c) {
- c_count++;
- c = getc(file);
- if ('\n' == c) {
- if (c_count > linemax)
- linemax = c_count;
- c_count = 0;
- lines++; } }
- struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
- fseek(file, 0, SEEK_SET);
- char * command = malloc(linemax);
- char commcount = 0;
- char * digits = malloc(3);
- char digicount = 0;
- int key, digimax;
- int keycount = 0;
- c = getc(file);
- while (EOF != c) {
- if ('\n' == c) {
- keybindings[keycount].name = calloc(commcount, sizeof(char));
- memcpy(keybindings[keycount].name, command, commcount);
- keybindings[keycount].key = key;
- keycount++;
- digicount = 0;
- commcount = 0; }
- else if (-1 != digicount)
- if (' ' == c) {
- key = 0;
- digimax = digicount - 1;
- while (digicount > 0) {
- digicount--;
- key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
- digicount = -1; }
- else {
- digits[digicount] = c;
- digicount++; }
+void move_player (struct World * world, char d) {
+// Move player in direction d, increment turn counter and update log.
+ static char prev = 0;
+ char success = 0;
+ char * dir;
+ char ty = world->player->y;
+ char tx = world->player->x;
+ if ('s' == d) {
+ dir = "south";
+ ty++; }
+ if ('n' == d) {
+ dir = "north";
+ ty--; }
+ if ('w' == d) {
+ dir = "west";
+ tx--; }
+ if ('e' == d) {
+ dir = "east";
+ tx++; }
+ if (ty == world->monster->y && tx == world->monster->x)
+ success = 2;
+ else if (is_passable(world, tx, ty)) {
+ success = 1;
+ world->player->y = ty;
+ world->player->x = tx; }
+ if (success * d == prev)
+ update_log (world, ".");
+ else {
+ if (2 == success)
+ update_log (world, "\nYou hit the monster.");