Ticket #1386: sort-slays.diff

File sort-slays.diff, 2.1 KB (added by ycombinator, 10 years ago)
  • src/object/obj-info.c

    From aee81d914bf194006beb1c86c609e0cbfc411bfe Mon Sep 17 00:00:00 2001
    From: Rooslan S. Khayrov <khayrov@gmail.com>
    Date: Fri, 1 Apr 2011 09:57:40 +0000
    Subject: [PATCH] Sort and group slays in weapon description.
    
    Sort and group slays in weapon description according to the damage
    multiplier.
    ---
     src/object/obj-info.c |   32 ++++++++++++++++++++++++++++++--
     1 files changed, 30 insertions(+), 2 deletions(-)
    
    diff --git a/src/object/obj-info.c b/src/object/obj-info.c
    index 0b26891..1cf45f2 100644
    a b static bool describe_combat(textblock *tb, const object_type *o_ptr, 
    704704        flags_init(mask, OF_SIZE, OF_ALL_SLAY_MASK, FLAG_END); 
    705705 
    706706        cnt = list_slays(f, mask, desc, NULL, mult, TRUE); 
    707         for (i = 0; i < cnt; i++) 
     707        /* Sort slays and brands by multiplier. Their total number is likely to  
     708           be very small, so insertion sort suits fine. */ 
     709        for (i = 1; i < cnt; ++i) 
    708710        { 
     711                int j; 
     712                int key = mult[i]; 
     713                const char *keydesc = desc[i]; 
     714                /* reverse sort order */ 
     715                for (j = i - 1; j >= 0 && mult[j] < key; --j) 
     716                { 
     717                        mult[j + 1] = mult[j]; 
     718                        desc[j + 1] = desc[j]; 
     719                } 
     720                mult[j + 1] = key; 
     721                desc[j + 1] = keydesc; 
     722        } 
     723        i = 0; 
     724        while (i < cnt) 
     725        { 
     726                int last_mult = mult[i]; 
     727                int group_length = 1; 
    709728                int melee_adj_mult = ammo ? 0 : 1; /* ammo mult adds fully, melee mult is times 1, so adds 1 less */ 
    710729                /* Include bonus damage and slay in stated average */ 
    711730                total_dam = dam * (multiplier + mult[i] - melee_adj_mult) + xtra_precrit; 
    static bool describe_combat(textblock *tb, const object_type *o_ptr, 
    726745                        textblock_append_c(tb, TERM_L_GREEN, "%d", total_dam / 10); 
    727746 
    728747 
    729                 textblock_append(tb, " vs. %s, ", desc[i]); 
     748                textblock_append(tb, " vs. %s", desc[i]); 
     749                for (++i; i < cnt && mult[i] == last_mult; ++i) 
     750                { 
     751                        bool last_in_group = (i == cnt - 1) || (mult[i + 1] != last_mult); 
     752                        ++group_length; 
     753                        textblock_append(tb, 
     754                                last_in_group ? (group_length > 2 ? ", and %s" : " and %s") : ", %s", 
     755                                desc[i]); 
     756                } 
     757                textblock_append(tb, ", "); 
    730758        } 
    731759 
    732760        if (cnt) textblock_append(tb, "and ");