#include "defs.h"
#include <stdarg.h>

#define CP_DEFAULT 1
#define CP_ENEMY   2
#define CP_FRIEND  3
#define CP_ITEM    4
#define CP_WATER   5
#define CP_SPECIAL 6
#define CP_INSTALL 7
#define CP_DIM     8

static int term_rows, term_cols;
static int map_origin_y, map_origin_x;

void display_init(void)
{
    initscr();
    if (has_colors()) {
        start_color();
        use_default_colors();
        init_pair(CP_DEFAULT, COLOR_WHITE,   -1);
        init_pair(CP_ENEMY,   COLOR_RED,     -1);
        init_pair(CP_FRIEND,  COLOR_GREEN,   -1);
        init_pair(CP_ITEM,    COLOR_YELLOW,  -1);
        init_pair(CP_WATER,   COLOR_BLUE,    -1);
        init_pair(CP_SPECIAL, COLOR_MAGENTA, -1);
        init_pair(CP_INSTALL, COLOR_CYAN,    -1);
        init_pair(CP_DIM,     COLOR_BLACK,   -1);
    }
    cbreak();
    noecho();
    nonl();
    keypad(stdscr, TRUE);
    curs_set(0);

    getmaxyx(stdscr, term_rows, term_cols);
    if (term_rows < 24) term_rows = 24;
    if (term_cols < 80) term_cols = 80;

    map_origin_y = 0;
    map_origin_x = STAT_W + 1;

    G->win_stat = newwin(MAP_H, STAT_W, 0, 0);
    G->win_map  = newwin(MAP_H, MAP_W, map_origin_y, map_origin_x);
    G->win_msg  = newwin(MSG_LINES, STAT_W + MAP_W + 1, MAP_H, 0);
    G->win_inv  = NULL;

    scrollok(G->win_msg, TRUE);

    if (has_colors()) {
        wattron(G->win_stat, COLOR_PAIR(CP_DEFAULT));
        wattron(G->win_map,  COLOR_PAIR(CP_DEFAULT));
        wattron(G->win_msg,  COLOR_PAIR(CP_DEFAULT));
    }
}

void display_cleanup(void)
{
    if (G->win_stat) delwin(G->win_stat);
    if (G->win_map)  delwin(G->win_map);
    if (G->win_msg)  delwin(G->win_msg);
    if (G->win_inv)  delwin(G->win_inv);
    endwin();
}

static int tile_color(int tile)
{
    switch (tile) {
    case T_DOOR_CLOSED:
    case T_DOOR_OPEN:
    case T_DOOR_LOCKED:
        return CP_WATER;
    case T_WASTE:
        return CP_WATER;
    case T_STAIRS_UP:
    case T_STAIRS_DOWN:
        return CP_SPECIAL;
    case T_FLOATER_PAD:
    case T_JAUNT_PLATE:
    case T_OMNIFLOW:
        return CP_INSTALL;
    case T_HOLE:
        return CP_ENEMY;
    default:
        return CP_DEFAULT;
    }
}

void display_map(void)
{
    WINDOW *w = G->win_map;
    level_t *lev = G->levels[G->cur_level];
    player_t *p = &G->player;

    werase(w);
    if (!lev) { wnoutrefresh(w); return; }

    for (int y = 0; y < MAP_H; y++) {
        for (int x = 0; x < MAP_W; x++) {
            cell_t *c = &lev->map[y][x];
            chtype ch;

            if (!c->explored) {
                if (G->opts.show_unexplored)
                    ch = '.' | COLOR_PAIR(CP_DIM);
                else
                    ch = ' ';
                mvwaddch(w, y, x, ch);
                continue;
            }

            if (!c->visible) {
                int tile = c->tile;
                if (tile >= 0 && tile < T_COUNT)
                    ch = tile_ch[tile] | COLOR_PAIR(CP_DIM) | A_DIM;
                else
                    ch = ' ';
                mvwaddch(w, y, x, ch);
                continue;
            }

            /* Visible cell: check player, monsters, items, then tile */
            if (x == p->x && y == p->y) {
                int attr = COLOR_PAIR(CP_DEFAULT);
                if (G->opts.bold_cursor)    attr |= A_BOLD;
                if (G->opts.reverse_cursor) attr |= A_REVERSE;
                if (G->opts.blink_cursor)   attr |= A_BLINK;
                ch = '@' | attr;
                mvwaddch(w, y, x, ch);
                continue;
            }

            /* Check for visible monster */
            monster_t *mon = NULL;
            for (int i = 0; i < lev->n_monsters; i++) {
                monster_t *m = &lev->monsters[i];
                if (m->active && m->x == x && m->y == y) {
                    mon = m;
                    break;
                }
            }
            if (mon) {
                char glyph = monster_glyph(mon->type);
                int attr;
                if (mon->friendly)
                    attr = COLOR_PAIR(CP_FRIEND);
                else
                    attr = COLOR_PAIR(CP_ENEMY);
                if (G->opts.bold_bems) attr |= A_BOLD;
                if (monster_is_mechanical(mon->type))
                    attr = COLOR_PAIR(CP_INSTALL) | A_BOLD;
                ch = glyph | attr;
                mvwaddch(w, y, x, ch);
                continue;
            }

            /* Check for floor items */
            if (c->items_here > 0) {
                int attr = COLOR_PAIR(CP_ITEM);
                if (G->opts.bold_objects) attr |= A_BOLD;
                if (G->opts.reverse_objects) attr |= A_REVERSE;
                /* Find first item at this location for its glyph */
                char ig = '*';
                for (int i = 0; i < lev->n_floor_items; i++) {
                    if (lev->floor_item_x[i] == x &&
                        lev->floor_item_y[i] == y) {
                        ig = item_glyph(lev->floor_items[i].type);
                        break;
                    }
                }
                ch = ig | attr;
                mvwaddch(w, y, x, ch);
                continue;
            }

            /* Draw tile */
            int tile = c->tile;
            if (tile >= 0 && tile < T_COUNT) {
                int attr = COLOR_PAIR(tile_color(tile));
                int tc = tile_ch[tile];
                if (c->blood)
                    attr = COLOR_PAIR(CP_ENEMY);
                if (c->paint)
                    attr = COLOR_PAIR(CP_SPECIAL);
                if (c->install_type > 0) {
                    attr = COLOR_PAIR(CP_INSTALL);
                    if (G->opts.bold_installs) attr |= A_BOLD;
                    if (G->opts.reverse_installs) attr |= A_REVERSE;
                }
                ch = tc | attr;
            } else {
                ch = ' ';
            }
            mvwaddch(w, y, x, ch);
        }
    }
    wnoutrefresh(w);
}

void display_stats(void)
{
    WINDOW *w = G->win_stat;
    player_t *p = &G->player;

    werase(w);

    /* Name - truncate to 12 displayed chars as per original */
    wattron(w, A_BOLD);
    mvwprintw(w, 0, 0, "%-12.12s", p->name);
    wattroff(w, A_BOLD);

    mvwprintw(w, 2, 0, "LEV: %d", p->level);

    if (p->hp < p->max_hp)
        mvwprintw(w, 3, 0, "HP:  %d/%d", p->hp, p->max_hp);
    else
        mvwprintw(w, 3, 0, "HP:  %d", p->hp);

    mvwprintw(w, 4, 0, "AC:  %d", p->ac);
    mvwprintw(w, 5, 0, "WC:  %d", p->wc);
    mvwprintw(w, 6, 0, "WT:  %d", p->weight_carried);

    mvwprintw(w, 7, 0,  "STR: %d", p->str);
    mvwprintw(w, 8, 0,  "DEX: %d", p->dex);
    mvwprintw(w, 9, 0,  "SPD: %d", p->spd);
    mvwprintw(w, 10, 0, "CON: %d", p->con);
    mvwprintw(w, 11, 0, "INT: %d", p->intel);
    mvwprintw(w, 12, 0, "WIS: %d", p->wis);
    mvwprintw(w, 13, 0, "CHR: %d", p->chr);

    mvwprintw(w, 14, 0, "EPS: %d", p->eps);

    /* Region display */
    if (p->region > 0)
        mvwprintw(w, 16, 0, "Region: %d", p->region);
    else
        mvwprintw(w, 16, 0, "Region: ?");

    mvwprintw(w, 17, 0, "DOOM v%d.%d%c",
              GAME_VERSION_MAJ, GAME_VERSION_MIN, GAME_VERSION_REV);

    /* Status indicators */
    if (p->auto_search)
        mvwprintw(w, 1, 0, "Auto-Search");
    if (G->debug_mode) {
        wattron(w, A_REVERSE);
        mvwprintw(w, 1, 0, " debug ");
        wattroff(w, A_REVERSE);
    }

    wnoutrefresh(w);
}

void display_msg(const char *text)
{
    WINDOW *w = G->win_msg;
    wprintw(w, "%s\n", text);
    wnoutrefresh(w);
}

void display_msgs(void)
{
    WINDOW *w = G->win_msg;
    werase(w);
    int start = G->msg_count - MSG_LINES;
    if (start < 0) start = 0;
    for (int i = start; i < G->msg_count; i++) {
        int idx = i % MAX_MSG_LOG;
        wprintw(w, "%s\n", G->messages[idx]);
    }
    wnoutrefresh(w);
}

void msg(const char *fmt, ...)
{
    char buf[MAX_MSG];
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);

    int idx = G->msg_count % MAX_MSG_LOG;
    strncpy(G->messages[idx], buf, MAX_MSG - 1);
    G->messages[idx][MAX_MSG - 1] = '\0';
    G->msg_count++;

    display_msg(buf);
}

void display_inventory(void)
{
    player_t *p = &G->player;
    if (!G->inv_visible) return;

    int inv_x = G->inv_left_side ? 0 : (STAT_W + MAP_W + 1 - INV_W);
    if (inv_x < 0) inv_x = STAT_W + MAP_W + 1;

    if (!G->win_inv) {
        G->win_inv = newwin(MAP_H, INV_W, 0, inv_x);
    } else {
        mvwin(G->win_inv, 0, inv_x);
        wresize(G->win_inv, MAP_H, INV_W);
    }
    WINDOW *w = G->win_inv;
    werase(w);
    box(w, 0, 0);
    mvwprintw(w, 0, 2, " Inventory ");

    int max_lines = MAP_H - 2;
    int start = G->inv_scroll;
    if (start > p->inv_count - max_lines)
        start = p->inv_count - max_lines;
    if (start < 0) start = 0;

    for (int i = 0; i < max_lines && (start + i) < p->inv_count; i++) {
        int slot = start + i;
        item_t *it = &p->inventory[slot];
        if (it->type < 0) continue;

        char label = (slot < 26) ? ('a' + slot) : ('A' + slot - 26);
        const char *nm = item_name(it);

        char suffix[32] = "";
        if (slot == p->wielded)
            snprintf(suffix, sizeof(suffix), " (wielded)");
        else if (slot == p->reserve)
            snprintf(suffix, sizeof(suffix), " (reserve)");
        else if (slot == p->worn_armor)
            snprintf(suffix, sizeof(suffix), " (worn)");
        else if (slot == p->worn_helmet)
            snprintf(suffix, sizeof(suffix), " (worn)");

        if (it->is_active) {
            size_t len = strlen(suffix);
            if (len == 0)
                snprintf(suffix, sizeof(suffix), " (active)");
            else
                snprintf(suffix + len - 1, sizeof(suffix) - len + 1,
                         "/active)");
        }

        int attr = COLOR_PAIR(CP_DEFAULT);
        if (slot == p->wielded || slot == p->reserve)
            attr = COLOR_PAIR(CP_ITEM) | A_BOLD;
        if (slot == p->worn_armor || slot == p->worn_helmet)
            attr = COLOR_PAIR(CP_INSTALL);

        wattron(w, attr);
        if (it->qty > 1)
            mvwprintw(w, i + 1, 1, "%c. %d %s%s",
                      label, it->qty, nm, suffix);
        else
            mvwprintw(w, i + 1, 1, "%c. %s%s",
                      label, nm, suffix);
        wattroff(w, attr);
    }

    wnoutrefresh(w);
}

void display_clear_inv(void)
{
    G->inv_visible = 0;
    if (G->win_inv) {
        werase(G->win_inv);
        wnoutrefresh(G->win_inv);
        delwin(G->win_inv);
        G->win_inv = NULL;
    }
}

void display_refresh(void)
{
    display_stats();
    display_map();
    display_msgs();
    if (G->inv_visible)
        display_inventory();
    doupdate();
}

static void paged_text(const char **lines, int nlines)
{
    WINDOW *w = newwin(term_rows, term_cols, 0, 0);
    keypad(w, TRUE);
    int page_lines = term_rows - 2;
    int pos = 0;

    for (;;) {
        werase(w);
        for (int i = 0; i < page_lines && (pos + i) < nlines; i++)
            mvwprintw(w, i, 0, "%s", lines[pos + i]);

        if (pos + page_lines >= nlines)
            mvwprintw(w, term_rows - 1, 0,
                      "SPACE to resume play, CANCEL to retain inventory... ");
        else
            mvwprintw(w, term_rows - 1, 0,
                      "SPACE for more, CANCEL to resume play... ");
        wrefresh(w);

        int ch = wgetch(w);
        if (ch == ' ' || ch == KEY_NPAGE) {
            pos += page_lines;
            if (pos >= nlines) break;
        } else if (ch == 'q' || ch == 27 || ch == '\n' || ch == KEY_ENTER) {
            break;
        } else if (ch == KEY_PPAGE) {
            pos -= page_lines;
            if (pos < 0) pos = 0;
        }
    }
    delwin(w);
    werase(stdscr);
    touchwin(stdscr);
    wrefresh(stdscr);
}

void display_intro(void)
{
    static const char *intro[] = {
        "",
        "                                DoomsDay 2026",
        "",
        "It came in the Spring of the year of our Lord 2026 AD, the doom that mankind",
        "had justly feared for so long.  Upon the once great nations of the earth rained",
        "chemical, biological, and nuclear terror so devastating in its effect, that it",
        "laid waste to every strategic, industrial, and cultural center existing on the",
        "face of the globe.  Two billion people died in the holocaust.  One billion more",
        "died soon after from disease, starvation, and hopeless terror.",
        "",
        "And then came the invader.  And it was not Man.  Although the war had mankind's",
        "baser nature (avarice, paranoia, suspicion, fear, and the hunger for power) as",
        "its seedling, it had been nurtured by intruders from a far distant galaxy until",
        "the time proved ripe for conquest.  A conquest against which no force could",
        "stand, for militant forces no longer existed in the aftermath of this",
        "terrible war.  By June the last vestige of human resistance was obliterated.",
        "",
        "But yet there was hope!  For small groups of individuals banded together during",
        "the successive months to carry on the struggle, to reclaim the proud but war",
        "torn world that had once been theirs.  Many such bands were routed out and",
        "slaughtered by the aliens.  Others flourished, silently, as they rebuilt a",
        "technological foundation for a counter offensive ...",
        "",
        "           Your mission, whether or not you decide to accept it ...",
        "",
        "You are a member of Operation Thunderbolt.  Your mission is to recover what is",
        "believed to be a working prototype of a revolutionary leap in offensive weapons",
        "technology: an anti-matter bomb.  Partially documented in a recently recovered",
        "ultra-top-secret Joint Chiefs of Staff report, its light-weight tactical design",
        "was evidently being seriously considered as a means of defeating the invaders,",
        "irrespective of the toll in human life, just three days before the Cheyenne",
        "Mountain stronghold was destroyed by aliens.",
        "",
        "The L.A.M.B. (as it was known by insiders) is thought to be secreted in the",
        "underground research labs of MegaTech International, beneath the southern tip of",
        "the Appalachian mountains near the Tennessee/Georgia border.  Find it and return",
        "to Nova Base Alpha as quickly as possible - a QuickLift will zone you in as you",
        "exit the MegaTech facility.  Our fabrication units will be standing by.",
        "",
        "You MUST succeed in this mission.  The aliens have begun a comprehensive",
        "campaign to exterminate our remaining rebel bases.  But under no, repeat NO",
        "circumstances are you to permit yourself, or any of your squad, to be captured",
        "for interrogation.  Each of you will be given a small black pill.",
        "",
        "          Unfortunately, you encounter an enemy patrol en route ...",
        "",
        "... and only YOU survived.  Retreating with your life, and little else, you",
        "barely manage to locate the shattered remains of the research lab.  After",
        "several days of harried investigation, with interludes of hunting for food and",
        "avoiding alien predators that hunted YOU as food, you finally dig your way into",
        "the basement complex ... and discover that it is inhabited!",
        "",
        "All manner of strangely mutated creatures live within the sheltering ruin,",
        "creatures distorted by the gene-wrenching effects of the war, and creatures",
        "brought to Earth by the alien invaders.",
        "",
        "And as you begin your perilous journey downward, you begin to see signs of",
        "recent exploration by sentient creatures, and begin to suspect that here,",
        "too, the aliens have come - the aliens with their ubiquitous metal servants.",
        "",
        "Then you find the message.  Scrawled with grime and blood across a section of",
        "flooring by an unsteady hand are words that tell you all too clearly that the",
        "invader is here:",
        "",
        "                         Beware of the Zytts!",
    };
    int n = sizeof(intro) / sizeof(intro[0]);
    paged_text(intro, n);
}

void display_death(const char *cause)
{
    werase(stdscr);
    attron(COLOR_PAIR(CP_ENEMY) | A_BOLD);
    mvprintw(2, 20, "You died ...");
    attroff(A_BOLD);
    mvprintw(4, 20, "%s", cause);

    player_t *p = &G->player;
    if (p->region > 0)
        mvprintw(5, 20, " on Region %d", p->region);

    int score = p->eps + p->bonus_pts - p->penalty_pts;
    attroff(COLOR_PAIR(CP_ENEMY));
    attron(COLOR_PAIR(CP_DEFAULT));
    mvprintw(8,  10, "    Experience Points   %10d", p->eps);
    mvprintw(9,  10, "    Bonus Points        %10d", p->bonus_pts);
    mvprintw(10, 10, "    Penalty Points      %10d", p->penalty_pts);
    mvprintw(11, 10, "    Total               %10d", score);
    mvprintw(13, 10, "You accumulated %d points during this session", score);

    mvprintw(16, 20, "Better luck next time ...");
    mvprintw(18, 20, "Press any key to exit");
    attroff(COLOR_PAIR(CP_DEFAULT));
    refresh();
    nodelay(stdscr, FALSE);
    getch();
}

void display_score(void)
{
    player_t *p = &G->player;
    int score = p->eps + p->bonus_pts - p->penalty_pts;

    static const char *lines[12];
    char buf0[80], buf1[80], buf2[80], buf3[80], buf4[80];

    snprintf(buf0, sizeof(buf0), "Your current score is:");
    snprintf(buf1, sizeof(buf1),
             "    Experience Points   %10d", p->eps);
    snprintf(buf2, sizeof(buf2),
             "    Bonus Points        %10d", p->bonus_pts);
    snprintf(buf3, sizeof(buf3),
             "    Penalty Points      %10d", p->penalty_pts);
    snprintf(buf4, sizeof(buf4),
             "    Total               %10d", score);

    lines[0] = "";
    lines[1] = buf0;
    lines[2] = buf1;
    lines[3] = buf2;
    lines[4] = buf3;
    lines[5] = buf4;
    lines[6] = "";
    paged_text(lines, 7);
}

int display_prompt_yn(const char *prompt)
{
    WINDOW *w = G->win_msg;
    wprintw(w, "%s", prompt);
    wrefresh(w);
    for (;;) {
        int ch = wgetch(w);
        if (ch == 'y' || ch == 'Y') {
            wprintw(w, "y\n");
            wrefresh(w);
            return 1;
        }
        if (ch == 'n' || ch == 'N' || ch == 27 ||
            ch == '\n' || ch == KEY_ENTER) {
            wprintw(w, "n\n");
            wrefresh(w);
            return 0;
        }
    }
}

int display_prompt_dir(const char *prompt)
{
    msg("%s", prompt);
    doupdate();
    for (;;) {
        int ch = wgetch(G->win_msg);
        switch (ch) {
        case 'k': case '8': case KEY_UP:    return DIR_N;
        case 'u': case '9':                 return DIR_NE;
        case 'l': case '6': case KEY_RIGHT: return DIR_E;
        case 'n': case '3':                 return DIR_SE;
        case 'j': case '2': case KEY_DOWN:  return DIR_S;
        case 'b': case '1':                 return DIR_SW;
        case 'h': case '4': case KEY_LEFT:  return DIR_W;
        case 'y': case '7':                 return DIR_NW;
        case '<':                            return DIR_UP;
        case '>':                            return DIR_DOWN;
        case 27: case '\n': case KEY_ENTER:  return -1;
        }
    }
}

int display_prompt_item(const char *prompt)
{
    player_t *p = &G->player;
    char range_hi = (p->inv_count <= 26)
        ? ('a' + p->inv_count - 1)
        : ('A' + p->inv_count - 27);

    char pbuf[MAX_MSG];
    if (p->inv_count <= 26)
        snprintf(pbuf, sizeof(pbuf), "%s (a to %c, CANCEL) ", prompt, range_hi);
    else
        snprintf(pbuf, sizeof(pbuf), "%s (a to %c, CANCEL) ", prompt, range_hi);

    msg("%s", pbuf);
    doupdate();

    for (;;) {
        int ch = wgetch(G->win_msg);
        if (ch == 27 || ch == '\n' || ch == KEY_ENTER)
            return -1;
        if (ch == '-') {
            /* '-' means current weapon */
            if (p->wielded >= 0) return p->wielded;
            if (p->reserve >= 0) return p->reserve;
            return -1;
        }
        if (ch == '*') {
            /* toggle inventory */
            G->inv_visible = !G->inv_visible;
            display_refresh();
            continue;
        }
        int slot = -1;
        if (ch >= 'a' && ch <= 'z') slot = ch - 'a';
        else if (ch >= 'A' && ch <= 'Z') slot = ch - 'A' + 26;
        if (slot >= 0 && slot < p->inv_count &&
            p->inventory[slot].type >= 0)
            return slot;
    }
}

int display_prompt_str(const char *prompt, char *buf, int maxlen)
{
    WINDOW *w = G->win_msg;
    wprintw(w, "%s", prompt);
    wrefresh(w);
    echo();
    curs_set(1);
    int ret = wgetnstr(w, buf, maxlen - 1);
    noecho();
    curs_set(0);
    return (ret == OK) ? (int)strlen(buf) : 0;
}

void display_help(void)
{
    static const char *help[] = {
        "a  attack something          r  reload current weapon",
        "A  activate an item           R  remove a worn item",
        "c  close something            s  search vicinity",
        "C  recharge an item           S  toggle auto-search",
        "d  drop an item               t  throw an item",
        "D  deactivate an item         T  toss one or more items",
        "e  eat an item                v  version / score",
        "E  examine an item            w  wield an item",
        "f  fire weapon                W  wear an item",
        "F  fix adjacent item          x  exchange weapons",
        "g  pick up an item            X  checkpoint game",
        "G  get item from container    z  toggle options",
        "i  inspect location",
        "I  toggle inventory           h/j/k/l  movement (vi keys)",
        "m  display message log        y/u/b/n  diagonal movement",
        "M  invoke shell               1-9      numpad movement",
        "o  open something             SPACE    rest in place",
        "O  snapshot screen            <        go up",
        "p  pause until healed         >        go down",
        "P  print screen               .+dir    run",
        "q  drink/quaff from item      ?        this help",
        "Q  Quit and save game         /        general health",
        "",
        "TAB + key for extended commands (see full help file)",
        "",
        "DoomsDay 2026  Copyright (c) 1987  Dave Scheifler",
        "Unix port 2026",
    };
    int n = sizeof(help) / sizeof(help[0]);
    paged_text(help, n);
}

void display_health(void)
{
    player_t *p = &G->player;
    static const char *lines[32];
    int n = 0;
    char header[] = "You are:  ";
    lines[n++] = header;

    struct { unsigned int flag; const char *label; } effects[] = {
        { SF_INJURED,     "injured" },
        { SF_BLINDED,     "blinded" },
        { SF_DIZZY,       "dizzy" },
        { SF_CONFUSED,    "confused" },
        { SF_STUNNED,     "stunned" },
        { SF_POISONED,    "poisoned" },
        { SF_IRRADIATED,  "irradiated" },
        { SF_CLUMSY,      "clumsy" },
        { SF_WEBBED,      "webbed" },
        { SF_SLEEPING,    "sleeping" },
        { SF_VULNERABLE,  "vulnerable" },
        { SF_KEEN_EYED,   "keen eyed" },
        { SF_SHARP_EARED, "sharp eared" },
        { SF_FLOATING,    "floating" },
        { SF_SLOWED,      "slowed" },
        { SF_SPEEDED,     "speeded up" },
        { SF_WEAKENED,    "weakened" },
        { SF_KLUTZY,      "klutzy" },
        { SF_DEBILITATED, "debilitated" },
        { SF_IMPAIRED,    "mentally impaired" },
        { SF_FUZZY,       "fuzzy headed" },
        { SF_DISEASED,    "diseased" },
        { SF_PAINTED,     "paint spattered" },
        { 0, NULL }
    };

    int any = 0;
    for (int i = 0; effects[i].label; i++) {
        if (p->status & effects[i].flag) {
            lines[n++] = effects[i].label;
            any = 1;
            if (n >= 30) break;
        }
    }
    if (!any)
        lines[n++] = "You seem to be perfectly healthy";

    paged_text(lines, n);
}

void display_version(void)
{
    player_t *p = &G->player;
    int score = p->eps + p->bonus_pts - p->penalty_pts;
    msg("DoomsDay 2026    Version %d.%d%c    Copyright (c) 1987    All Rights Reserved",
        GAME_VERSION_MAJ, GAME_VERSION_MIN, GAME_VERSION_REV);
    msg("Current Score: %d  (%d Experience + %d Bonus - %d Penalty)",
        score, p->eps, p->bonus_pts, p->penalty_pts);
}
