Changeset 591

Show
Ignore:
Timestamp:
09/29/07 17:03:56 (1 year ago)
Author:
takkaria
Message:

Refactor the dungeon generation code a bit, so that it reads easier.

Files:

Legend:

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

    r589 r591  
    19981998 
    19991999 
    2000                 /* Erase the old cave */ 
    2001                 wipe_o_list(); 
    2002                 wipe_mon_list(); 
    2003  
    2004  
    20052000                /* XXX XXX XXX */ 
    20062001                message_flush(); 
  • trunk/src/generate.c

    r566 r591  
    50405040 
    50415041/* 
     5042 * Clear the dungeon, ready for generation to begin. 
     5043 */ 
     5044static void clear_cave(void) 
     5045{ 
     5046        int x, y; 
     5047 
     5048        wipe_o_list(); 
     5049        o_max = 1; 
     5050 
     5051        wipe_mon_list(); 
     5052        mon_max = 1; 
     5053 
     5054 
     5055        /* Clear flags and flow information. */ 
     5056        for (y = 0; y < DUNGEON_HGT; y++) 
     5057        { 
     5058                for (x = 0; x < DUNGEON_WID; x++) 
     5059                { 
     5060                        /* No features */ 
     5061                        cave_feat[y][x] = 0; 
     5062 
     5063                        /* No flags */ 
     5064                        cave_info[y][x] = 0; 
     5065                        cave_info2[y][x] = 0; 
     5066 
     5067                        /* No flow */ 
     5068                        cave_cost[y][x] = 0; 
     5069                        cave_when[y][x] = 0; 
     5070                } 
     5071        } 
     5072 
     5073        /* Mega-Hack -- no player in dungeon yet */ 
     5074        cave_m_idx[p_ptr->py][p_ptr->px] = 0; 
     5075        p_ptr->px = p_ptr->py = 0; 
     5076 
     5077        /* Hack -- illegal panel */ 
     5078        Term->offset_y = DUNGEON_HGT; 
     5079        Term->offset_x = DUNGEON_WID; 
     5080 
     5081 
     5082        /* Nothing special here yet */ 
     5083        good_item_flag = FALSE; 
     5084 
     5085        /* Nothing good here yet */ 
     5086        rating = 0; 
     5087} 
     5088 
     5089 
     5090/* 
     5091 * Calculate the level feeling, using a "rating" and the player's depth. 
     5092 */ 
     5093static int calculate_feeling(int rating, int depth) 
     5094{ 
     5095        int feeling; 
     5096 
     5097        /* Town gets no feeling */ 
     5098        if (!depth == 0) return 0; 
     5099 
     5100 
     5101        /* Extract the feeling */ 
     5102        if      (rating > 50 +     depth    ) feeling = 2; 
     5103        else if (rating > 40 + 4 * depth / 5) feeling = 3; 
     5104        else if (rating > 30 + 3 * depth / 5) feeling = 4; 
     5105        else if (rating > 20 + 2 * depth / 5) feeling = 5; 
     5106        else if (rating > 15 + 1 * depth / 3) feeling = 6; 
     5107        else if (rating > 10 + 1 * depth / 5) feeling = 7; 
     5108        else if (rating >  5 + 1 * depth /10) feeling = 8; 
     5109        else if (rating >  0) feeling = 9; 
     5110        else feeling = 10; 
     5111 
     5112        /* Hack -- Have a special feeling sometimes */ 
     5113        if (good_item_flag && OPT(adult_no_preserve)) feeling = 1; 
     5114 
     5115        return feeling; 
     5116} 
     5117 
     5118 
     5119/* 
    50425120 * Generate a random dungeon level 
    5043  * 
    5044  * Hack -- regenerate any "overflow" levels 
    5045  * 
    5046  * Hack -- allow auto-scumming via a gameplay option. 
    5047  * 
    5048  * Note that this function resets flow data and grid flags directly. 
    5049  * Note that this function does not reset features, monsters, or objects.   
    5050  * Features are left to the town and dungeon generation functions, and  
    5051  * "wipe_m_list()" and "wipe_o_list()" handle monsters and objects. 
    50525121 */ 
    50535122void generate_cave(void) 
    50545123{ 
    5055         int y, x, num
     5124        const char *error = "no generation"
    50565125 
    50575126        /* The dungeon is not ready */ 
    50585127        character_dungeon = FALSE; 
    50595128 
    5060         /* Generate */ 
    5061         for (num = 0; TRUE; num++) 
    5062         { 
    5063                 bool okay = TRUE; 
    5064                 cptr why = NULL; 
    5065  
    5066                 /* Reset monsters and objects */ 
    5067                 o_max = 1; 
    5068                 mon_max = 1; 
    5069  
    5070  
    5071                 /* Clear flags and flow information. */ 
    5072                 for (y = 0; y < DUNGEON_HGT; y++) 
    5073                 { 
    5074                         for (x = 0; x < DUNGEON_WID; x++) 
    5075                         { 
    5076                                 /* No flags */ 
    5077                                 cave_info[y][x] = 0; 
    5078                                 cave_info2[y][x] = 0; 
    5079  
    5080                                 /* No flow */ 
    5081                                 cave_cost[y][x] = 0; 
    5082                                 cave_when[y][x] = 0; 
    5083                         } 
    5084                 } 
    5085  
    5086  
    5087                 /* Mega-Hack -- no player in dungeon yet */ 
    5088                 cave_m_idx[p_ptr->py][p_ptr->px] = 0; 
    5089                 p_ptr->px = p_ptr->py = 0; 
    5090  
    5091                 /* Hack -- illegal panel */ 
    5092                 Term->offset_y = DUNGEON_HGT; 
    5093                 Term->offset_x = DUNGEON_WID; 
    5094  
    5095  
    5096                 /* Nothing special here yet */ 
    5097                 good_item_flag = FALSE; 
    5098  
    5099                 /* Nothing good here yet */ 
    5100                 rating = 0; 
    5101  
    5102                 /* Build the town */ 
    5103                 if (!p_ptr->depth) 
    5104                 { 
    5105                         /* Make a town */ 
     5129        while (error) 
     5130        { 
     5131                error = NULL; 
     5132                clear_cave(); 
     5133 
     5134                if (p_ptr->depth == 0) 
    51065135                        town_gen(); 
    5107                 } 
    5108  
    5109                 /* Build a real level */ 
    51105136                else 
    5111                 { 
    5112                         /* Make a dungeon */ 
    51135137                        cave_gen(); 
    5114                 } 
    5115  
    5116                 okay = TRUE; 
    5117  
    5118  
    5119                 /* Extract the feeling */ 
    5120                 if      (rating > 50 +     p_ptr->depth    ) feeling = 2; 
    5121                 else if (rating > 40 + 4 * p_ptr->depth / 5) feeling = 3; 
    5122                 else if (rating > 30 + 3 * p_ptr->depth / 5) feeling = 4; 
    5123                 else if (rating > 20 + 2 * p_ptr->depth / 5) feeling = 5; 
    5124                 else if (rating > 15 + 1 * p_ptr->depth / 3) feeling = 6; 
    5125                 else if (rating > 10 + 1 * p_ptr->depth / 5) feeling = 7; 
    5126                 else if (rating >  5 + 1 * p_ptr->depth /10) feeling = 8; 
    5127                 else if (rating >  0) feeling = 9; 
    5128                 else feeling = 10; 
    5129  
    5130                 /* Hack -- Have a special feeling sometimes */ 
    5131                 if (good_item_flag && adult_no_preserve) feeling = 1; 
    51325138 
    51335139                /* It takes 1000 game turns for "feelings" to recharge */ 
    5134                 if (((turn - old_turn) < 1000) && (old_turn > 1)) feeling = 0; 
    5135  
    5136                 /* Hack -- no feeling in the town */ 
    5137                 if (!p_ptr->depth) feeling = 0; 
    5138  
    5139  
    5140                 /* Prevent object over-flow */ 
     5140                if (((turn - old_turn) < 1000) && (old_turn > 1)) 
     5141                        feeling = 0; 
     5142                else 
     5143                        feeling = calculate_feeling(rating, p_ptr->depth); 
     5144 
     5145                /* Hack -- regenerate any "overflow" levels */ 
    51415146                if (o_max >= z_info->o_max) 
    5142                 { 
    5143                         /* Message */ 
    5144                         why = "too many objects"; 
    5145  
    5146                         /* Message */ 
    5147                         okay = FALSE; 
    5148                 } 
    5149  
    5150                 /* Prevent monster over-flow */ 
     5147                        error = "too many objects"; 
    51515148                if (mon_max >= z_info->m_max) 
    5152                 { 
    5153                         /* Message */ 
    5154                         why = "too many monsters"; 
    5155  
    5156                         /* Message */ 
    5157                         okay = FALSE; 
    5158                 } 
    5159  
    5160                 /* Message */ 
    5161                 if ((cheat_room) && (why))  
    5162                         msg_format("Generation restarted (%s)", why); 
    5163  
    5164                 /* Accept */ 
    5165                 if (okay) break; 
    5166  
    5167                 /* Wipe the objects */ 
    5168                 wipe_o_list(); 
    5169  
    5170                 /* Wipe the monsters */ 
    5171                 wipe_mon_list(); 
     5149                        error = "too many monsters"; 
     5150 
     5151 
     5152                if (OPT(cheat_room) && error) 
     5153                        msg_format("Generation restarted: %s.", error); 
    51725154        } 
    51735155 
     
    51755157        /* The dungeon is ready */ 
    51765158        character_dungeon = TRUE; 
    5177  
    51785159 
    51795160        /* Remember when this level was "created" */