Changeset 632

Show
Ignore:
Timestamp:
12/22/07 17:16:00 (8 months ago)
Author:
takkaria
Message:

Clean up death.c a lot, including removing the annoying center_string() function. (#417)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/death.c

    r631 r632  
    2020 
    2121 
    22  
    23 /* 
    24  * Centers a string within a 31 character string 
    25  */ 
    26 static void center_string(char *buf, size_t len, cptr str) 
    27 { 
    28         int i, j; 
    29  
    30         /* Total length */ 
    31         i = strlen(str); 
    32  
    33         /* Necessary border */ 
    34         j = 15 - i / 2; 
    35  
    36         /* Mega-Hack */ 
    37         strnfmt(buf, len, "%*s%s%*s", j, "", str, 31 - i - j, ""); 
    38 } 
    39  
    40  
    41  
    42  
    43  
    4422/* 
    4523 * Hack - save the time of death 
     
    4826 
    4927 
    50 /* 
    51  * Display a "tomb-stone" 
     28 
     29/* 
     30 * Write formatted string `fmt` on line `y`, centred between points x1 and x2. 
     31 */ 
     32static void put_str_centred(int y, int x1, int x2, const char *fmt, ...) 
     33
     34        va_list vp; 
     35        char *tmp; 
     36        size_t len; 
     37        int x; 
     38 
     39        /* Format into the (growable) tmp */ 
     40        va_start(vp, fmt); 
     41        tmp = vformat(fmt, vp); 
     42        va_end(vp); 
     43 
     44        /* Centre now */ 
     45        len = strlen(tmp); 
     46        x = x1 + ((x2-x1)/2 - len/2); 
     47 
     48        put_str(tmp, y, x); 
     49
     50 
     51 
     52/* 
     53 * Display the tombstone 
    5254 */ 
    5355static void print_tomb(void) 
    5456{ 
    55         cptr p; 
    56  
    57         char tmp[160]; 
     57        ang_file *fp; 
    5858        char buf[1024]; 
    59  
    60         ang_file *fp; 
    61  
    62         int line = 7; 
    63  
    64  
    65         /* Clear screen */ 
     59        int line = 0; 
     60 
     61 
    6662        Term_clear(); 
    6763 
     
    7066        fp = file_open(buf, MODE_READ, -1); 
    7167 
    72         /* Dump */ 
    7368        if (fp) 
    7469        { 
    75                 int i = 0; 
    76  
    77                 /* Dump the file to the screen */ 
    7870                while (file_getl(fp, buf, sizeof(buf))) 
    79                         put_str(buf, i++, 0); 
     71                        put_str(buf, line++, 0); 
    8072 
    8173                file_close(fp); 
    8274        } 
    8375 
    84  
    85         /* Get the right total */ 
     76        line = 7; 
     77 
     78        put_str_centred(line++, 8, 8+31, "%s", op_ptr->full_name); 
     79        put_str_centred(line++, 8, 8+31, "the"); 
    8680        if (p_ptr->total_winner) 
    87                 p = "Magnificent"
     81                put_str_centred(line++, 8, 8+31, "Magnificent")
    8882        else 
    89                 p = c_text + cp_ptr->title[(p_ptr->lev - 1) / 5]; 
    90  
    91         center_string(buf, sizeof(buf), op_ptr->full_name); 
    92         put_str(buf, line++, 8); 
    93  
    94         center_string(buf, sizeof(buf), "the"); 
    95         put_str(buf, line++, 8); 
    96  
    97         center_string(buf, sizeof(buf), p); 
    98         put_str(buf, line++, 8); 
     83                put_str_centred(line++, 8, 8+31, "%s", c_text + cp_ptr->title[(p_ptr->lev - 1) / 5]); 
    9984 
    10085        line++; 
    10186 
    102         center_string(buf, sizeof(buf), c_name + cp_ptr->name); 
    103         put_str(buf, line++, 8); 
    104  
    105         strnfmt(tmp, sizeof(tmp), "Level: %d", (int)p_ptr->lev); 
    106         center_string(buf, sizeof(buf), tmp); 
    107         put_str(buf, line++, 8); 
    108  
    109         strnfmt(tmp, sizeof(tmp), "Exp: %ld", (long)p_ptr->exp); 
    110         center_string(buf, sizeof(buf), tmp); 
    111         put_str(buf, line++, 8); 
    112  
    113         strnfmt(tmp, sizeof(tmp), "AU: %ld", (long)p_ptr->au); 
    114         center_string(buf, sizeof(buf), tmp); 
    115         put_str(buf, line++, 8); 
    116  
    117         strnfmt(tmp, sizeof(tmp), "Killed on Level %d", p_ptr->depth); 
    118         center_string(buf, sizeof(buf), tmp); 
    119         put_str(buf, line++, 8); 
    120  
    121         strnfmt(tmp, sizeof(tmp), "by %s.", p_ptr->died_from); 
    122         center_string(buf, sizeof(buf), tmp); 
    123         put_str(buf, line++, 8); 
     87        put_str_centred(line++, 8, 8+31, "%s", c_name + cp_ptr->name); 
     88        put_str_centred(line++, 8, 8+31, "Level: %d", (int)p_ptr->lev); 
     89        put_str_centred(line++, 8, 8+31, "Exp: %d", (int)p_ptr->exp); 
     90        put_str_centred(line++, 8, 8+31, "AU: %d", (int)p_ptr->au); 
     91        put_str_centred(line++, 8, 8+31, "Killed on Level %d", p_ptr->depth); 
     92        put_str_centred(line++, 8, 8+31, "by %s.", p_ptr->died_from); 
    12493 
    12594        line++; 
    12695 
    127         strnfmt(tmp, sizeof(tmp), "%-.24s", ctime(&death_time)); 
    128         center_string(buf, sizeof(buf), tmp); 
    129         put_str(buf, line++, 8); 
    130 
    131  
    132  
    133 /* 
    134  * Hack - Know inventory and home items upon death 
     96        put_str_centred(line++, 8, 8+31, "by %-.24s", ctime(&death_time)); 
     97
     98 
     99 
     100/* 
     101 * Know inventory and home items upon death 
    135102 */ 
    136103static void death_knowledge(void) 
    137104{ 
     105        store_type *st_ptr = &store[STORE_HOME]; 
     106        object_type *o_ptr; 
     107 
    138108        int i; 
    139109 
    140         object_type *o_ptr; 
    141  
    142         store_type *st_ptr = &store[STORE_HOME]; 
    143  
    144  
    145         /* Hack -- Know everything in the inven/equip */ 
    146110        for (i = 0; i < INVEN_TOTAL; i++) 
    147111        { 
    148112                o_ptr = &inventory[i]; 
    149  
    150                 /* Skip non-objects */ 
    151113                if (!o_ptr->k_idx) continue; 
    152114 
    153                 /* Aware and Known */ 
    154115                object_aware(o_ptr); 
    155116                object_known(o_ptr); 
    156  
    157                 /* Fully known */ 
    158                 o_ptr->ident |= (IDENT_KNOWN); 
    159         } 
    160  
    161         /* Hack -- Know everything in the home */ 
     117        } 
     118 
    162119        for (i = 0; i < st_ptr->stock_num; i++) 
    163120        { 
    164121                o_ptr = &st_ptr->stock[i]; 
    165  
    166                 /* Skip non-objects */ 
    167122                if (!o_ptr->k_idx) continue; 
    168123 
    169                 /* Aware and Known */ 
    170124                object_aware(o_ptr); 
    171125                object_known(o_ptr); 
    172  
    173                 /* Fully known */ 
    174                 o_ptr->ident |= (IDENT_KNOWN); 
    175126        } 
    176127 
    177128        /* Hack -- Recalculate bonuses */ 
    178129        p_ptr->update |= (PU_BONUS); 
    179  
    180         /* Handle stuff */ 
    181130        handle_stuff(); 
    182131} 
     
    185134 
    186135/* 
    187  * Display the winner file 
     136 * Display the winner crown 
    188137 */ 
    189138static void display_winner(void) 
    190139{ 
    191140        char buf[1024]; 
    192         char tmp[1024]; 
    193141        ang_file *fp; 
    194142 
     
    219167        } 
    220168 
    221         strnfmt(buf, sizeof(buf), "All Hail the Mighty %s!", sp_ptr->winner); 
    222         center_string(tmp, sizeof(tmp), buf); 
    223         put_str(tmp, i++, (wid/2) - (31/2)); 
     169        put_str_centred(i, 0, wid, "All Hail the Mighty %s!", sp_ptr->winner); 
    224170 
    225171        flush(); 
     
    228174 
    229175 
    230  
    231  
    232  
    233  
    234  
     176/* 
     177 * Menu command: dump character dump to file. 
     178 */ 
    235179static void death_file(void *unused, const char *title) 
    236180{ 
    237181        char ftmp[80]; 
    238182        strnfmt(ftmp, sizeof(ftmp), "%s.txt", op_ptr->base_name); 
     183 
     184        (void)unused; 
     185        (void)title; 
    239186 
    240187        if (!get_string("File name: ", ftmp, sizeof(ftmp))) 
     
    261208} 
    262209 
     210/* 
     211 * Menu command: view character dump and inventory. 
     212 */ 
    263213static void death_info(void *unused, const char *title) 
    264214{ 
     
    266216        object_type *o_ptr; 
    267217        store_type *st_ptr = &store[STORE_HOME]; 
     218 
     219        (void)unused; 
     220        (void)title; 
    268221 
    269222 
     
    349302} 
    350303 
     304/* 
     305 * Menu command: peruse pre-death messages. 
     306 */ 
    351307static void death_messages(void *unused, const char *title) 
    352308{ 
     309        (void)unused; 
     310        (void)title; 
     311 
    353312        screen_save(); 
    354313        do_cmd_messages(); 
     
    356315} 
    357316 
     317/* 
     318 * Menu command: see top twenty scores. 
     319 */ 
    358320static void death_scores(void *unused, const char *title) 
    359321{ 
     322        (void)unused; 
     323        (void)title; 
     324 
    360325        screen_save(); 
    361326        top_twenty(); 
     
    363328} 
    364329 
     330/* 
     331 * Menu command: examine items in the inventory. 
     332 */ 
    365333static void death_examine(void *unused, const char *title) 
    366334{ 
    367335        int item; 
    368336        cptr q, s; 
     337 
     338        (void)unused; 
     339        (void)title; 
    369340 
    370341 
     
    382353        { 
    383354                object_type *o_ptr = &inventory[item]; 
    384  
    385                 /* "Know" */ 
    386                 o_ptr->ident |= IDENT_KNOWN; 
    387355 
    388356                /* Describe */ 
     
    404372} 
    405373 
    406 menu_type death_menu; 
    407  
     374 
     375/* 
     376 * Menu structures for the death menu. 
     377 */ 
     378static menu_type death_menu; 
    408379static const menu_action death_actions[] = 
    409380{ 
     
    416387}; 
    417388 
    418  
    419  
    420  
    421 static char tag_death_main(menu_type *menu, int oid) 
    422 
    423         if (death_actions[oid].id) 
    424                 return death_actions[oid].id; 
    425  
    426         return 0; 
    427 
    428  
    429 static int valid_death_main(menu_type *menu, int oid) 
    430 
    431         if (death_actions[oid].name) 
    432                 return 1; 
    433  
    434         return 0; 
    435 
    436  
    437 static void display_death_main(menu_type *menu, int oid, bool cursor, int row, int col, int width) 
     389/* Return the tag for a menu entry */ 
     390static char death_menu_tag(menu_type *menu, int oid) 
     391
     392        (void)menu; 
     393        return death_actions[oid].id; 
     394
     395 
     396/* Display a menu entry */ 
     397static void death_menu_display(menu_type *menu, int oid, bool cursor, int row, int col, int width) 
    438398{ 
    439399        byte attr = curs_attrs[CURS_KNOWN][(int)cursor]; 
    440  
    441         if (death_actions[oid].name) 
    442                 c_prt(attr, death_actions[oid].name, row, col); 
    443 
    444  
     400        (void)menu; 
     401        (void)width; 
     402        c_prt(attr, death_actions[oid].name, row, col); 
     403
    445404 
    446405static const menu_iter death_iter = 
    447406{ 
    448407        0, 
    449         tag_death_main
    450         valid_death_main
    451         display_death_main
     408        death_menu_tag
     409        NULL
     410        death_menu_display
    452411        NULL 
    453412}; 
     
    461420void death_screen(void) 
    462421{ 
    463         bool wants_to_quit = FALSE; 
    464  
    465  
     422        menu_type *menu; 
     423        const char cmd_keys[] = { ARROW_LEFT, ARROW_RIGHT, '\0' }; 
     424        const region area = { 51, 2, 0, 6 }; 
     425 
     426        int cursor = 0; 
     427        ui_event_data c = EVENT_EMPTY; 
     428 
     429 
     430        /* Retire in the town in a good state */ 
    466431        if (p_ptr->total_winner) 
    467432        { 
    468                 /* Retire in the town in a good state */ 
    469433                p_ptr->depth = 0; 
    470434                my_strcpy(p_ptr->died_from, "Ripe Old Age", sizeof(p_ptr->died_from)); 
     
    485449        /* Get time of death */ 
    486450        (void)time(&death_time); 
    487  
    488         /* You are dead */ 
    489451        print_tomb(); 
    490  
    491         /* Hack - Know everything upon death */ 
    492452        death_knowledge(); 
    493  
    494         /* Enter player in high score list */ 
    495453        enter_score(&death_time); 
    496454 
    497         /* Flush all input keys */ 
     455        /* Flush all input and output */ 
    498456        flush(); 
    499  
    500         /* Flush messages */ 
    501457        message_flush(); 
    502458 
    503459 
    504         /* Initialize the menus */ 
    505         menu_type *menu; 
    506    const char cmd_keys[] = { ARROW_LEFT, ARROW_RIGHT, '\0' }; 
    507         const region area = { 51, 2, 0, 6 }; 
    508  
    509         int cursor = 0; 
    510         ui_event_data c = EVENT_EMPTY; 
    511  
    512  
    513         /* options screen selection menu */ 
     460        /* Initialize the menu */ 
    514461        menu = &death_menu; 
    515462        WIPE(menu, menu_type); 
     
    537484        } 
    538485} 
    539  
    540