Changeset 1415

Show
Ignore:
Timestamp:
05/28/09 14:58:52 (10 months ago)
Author:
magnate
Message:

Order the list of visible monsters by depth, because monster.txt has not been in any meaningful order since JLE's additions circa 2.9.6

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/monster/monster2.c

    r1390 r1415  
    561561void display_monlist(void) 
    562562{ 
    563         int i, max; 
     563        int i, j, k, max; 
    564564        int line = 1, x = 0; 
    565565        int cur_x; 
    566         unsigned total_count = 0, disp_count = 0; 
     566        unsigned total_count = 0, disp_count = 0, type_count = 0; 
    567567 
    568568        byte attr; 
     
    573573        monster_type *m_ptr; 
    574574        monster_race *r_ptr; 
     575        monster_race *r2_ptr; 
    575576 
    576577        u16b *race_count; 
     578        u16b *order; 
    577579 
    578580        bool in_term = (Term != angband_term[0]); 
     
    601603        } 
    602604 
    603         /* Allocate the array */ 
     605        /* Allocate the primary array */ 
    604606        race_count = C_ZNEW(z_info->r_max, u16b); 
    605607 
     
    612614                if (!m_ptr->ml) continue; 
    613615 
     616                /* If this is the first one of this type, count the type */ 
     617                if (!race_count[m_ptr->r_idx]) type_count++; 
     618                 
    614619                /* Bump the count for this race, and the total count */ 
    615620                race_count[m_ptr->r_idx]++; 
     
    636641                total_count, (total_count > 1 ? "s" : "")), 0, 0); 
    637642 
    638  
    639         /* Go over in reverse order (so we show harder monsters first) */ 
    640         for (i = z_info->r_max - 1; (i > 0) && (line < max); i--) 
     643        /* Allocate the secondary array */ 
     644        order = C_ZNEW(type_count, u16b); 
     645 
     646 
     647        /* Sort, because we cannot rely on monster.txt being ordered */ 
     648 
     649        /* Populate the ordered array, starting at 1 to ignore @ */ 
     650        for (i = 1; i < z_info->r_max; i++) 
    641651        { 
    642652                /* No monsters of this race are visible */ 
    643653                if (!race_count[i]) continue; 
    644654 
     655                /* Get the monster info */ 
     656                r_ptr = &r_info[i]; 
     657 
     658                /* Fit this monster into the sorted array */ 
     659                for (j = 0; j < type_count; j++) 
     660                { 
     661                        r2_ptr = &r_info[order[j]]; 
     662 
     663                        /* Monsters are sorted by depth */ 
     664                        /* Monsters of same depth are sorted by power */ 
     665                        if ((r_ptr->level > r2_ptr->level) || 
     666                                ((r_ptr->level == r2_ptr->level) && 
     667                                (r_ptr->power > r2_ptr->power))) 
     668                        { 
     669                                /* Move weaker monsters down the array */ 
     670                                for (k = type_count - 1; k > j; k--) 
     671                                { 
     672                                        order[k] = order[k - 1]; 
     673                                } 
     674 
     675                                /* Put current monster in the right place */ 
     676                                order[j] = i; 
     677                                break; 
     678                        } 
     679                } 
     680        } 
     681                 
     682 
     683        /* Print them out in descending order */ 
     684        for (i = 0; (i < type_count) && (line < max); i++) 
     685        { 
    645686                /* Reset position */ 
    646687                cur_x = x; 
    647688 
    648689                /* Note that these have been displayed */ 
    649                 disp_count += race_count[i]; 
     690                disp_count += race_count[order[i]]; 
    650691 
    651692                /* Get monster race and name */ 
    652                 r_ptr = &r_info[i]; 
     693                r_ptr = &r_info[order[i]]; 
    653694                m_name = r_name + r_ptr->name; 
    654695 
     
    662703 
    663704                /* Build the monster name */ 
    664                 if (race_count[i] == 1) 
     705                if (race_count[order[i]] == 1) 
    665706                        my_strcpy(buf, m_name, sizeof(buf)); 
    666707                else 
    667                         strnfmt(buf, sizeof(buf), "%s (x%d) ", m_name, race_count[i]); 
     708                        strnfmt(buf, sizeof(buf), "%s (x%d) ", m_name,  
     709                                race_count[order[i]]); 
    668710 
    669711                /* Display the pict */ 
     
    711753                Term_addstr(-1, TERM_WHITE, "  (Press any key to continue.)"); 
    712754 
    713         /* Free the race counters */ 
     755        /* Free the arrays */ 
    714756        FREE(race_count); 
     757        FREE(order); 
    715758} 
    716759