Decoded: Rogue (1980) by Toy, Arnold, Wichman
DOS version (1983) by Mel Sibony and Jon Lane
Source file: NEW_LEVE.C
Beginner friendly, line-by-line code walkthrough by MaiZure

NEW_LEVE.C is where most of the level generation magic happens

Original code:
https://britzl.github.io/roguearchive/

Original code with line numbers
http://www.maizure.org/projects/decoded-rogue/NEW_LEVE_linenum.txt



1     COMMENT
2     COMMENT
3     COMMENT
4     COMMENT
5     COMMENT
6     COMMENT
7     BLANK
8     Include the game header
9     Include the console management header
10    BLANK
11    Defines the probability of a treasure room (5%)
12    Defines the maximum nunber of treasures in a treasure room (10)
13    Defines the minimum number of treasures in a treasure room (2)
14    BLANK
15    BLANK

GENERATE A NEW LEVEL

16    Defines new_level with no arguments
17    BLOCK START - new_level, generates a new level -- all stored at global
18    Declares a room index and a counter
19    Declares a pointer to a monster
20    Declares a pointer to flags
21    Declares an unused pointer to other pointers
22    Declares an index
23    Declares coordinates to stairs
24    BLANK
25    Remove the 'held' flag from the player since this is a new level
26    COMMENT
27    COMMENT
28    COMMENT
29    COMMENT
30    COMMENT
31    If the level number is greater the max level the player has visited...
32    Update the new max level
33    If the copy protected flag is set (it is -- ROGUE.H)
34    Execute a game tick..
35    If we're past the first level and the checksums don't match...
36    End the game
37    End check for protected flag
38    COMMENT
39    COMMENT
40    COMMENT
41    Remove all the level data, replace with spaces
42    Set all of the level position flags to real (no hidden doors, etc)
43    COMMENT
44    COMMENT
45    COMMENT
46    Loop through all the monsters in the global mlist array
47    Get rid of monster inventories
48    Get rid of all monsters
49    COMMENT
50    COMMENT
51    COMMENT
52    Replace the flytrap damage global
53    COMMENT
54    COMMENT
55    COMMENT
56    Remove all level objects
57    Create all of the rooms
58    If this is the first level...
59    Import the window data segment
60    BLANK
61    If this is a full initialization
62    If the data segment hasn't been defined
63    Move the cursor to the bottom left
64    Clear the row
65    Otherwise there is a full data segment
66    Clear the entire window
67    End check for first level
68    Destroy the level screen
69    Update the status bar
70    Draw all passage
71    Increment the player's food usage
72    Generate potions and scrolls
73    COMMENT
74    COMMENT
75    COMMENT
76    Reset the counter
77    Loop to find a valid position for exit stairs
78    Choose a random room
79    Find a random position in the room and save it to the stairs global
80    Find the index of the stairs in the level data array
81    Increment the counter, and every 100 failed attempts...
82    BLOCK START - new seed
83    Reset counter
84    Generate a new seed
85    Repeat seed loop
86    Repeat loop for exit stairs until a valid position is found
87    Set the position to stairs
88    COMMENT
89    COMMENT
90    COMMENT
91    BLOCK START - Add traps (10% chance per level)
92    The number of traps is at least 1 and up to level/4
93    If the number of traps chosen is greater than allowable...
94    The number of traps is the max number
95    Set counter to match the traps
96    While there are still traps to place...
97    Loop while we find an empty position (clobber stair global)
98    Choose a random room
99    Choose a random point in the room
100   Get the index of the position
101   Retry loop if we didn't find a valid position
102   Get a pointer to the level flags
103   Remove the real flag
104   Add a random trap flag
105   Repeat until all traps are set
106   BLOCK END - Add traps
107   Loop while we find a player starting position
108   Find a random room
109   Find a random position in the room
110   Find the level index of the room
111   Loop while the position has a trap...
112   or has a monster
113   BLANK
114   Reset the message buffer
115   The player enders the room 
116   Draw the player's '@' symbol in position
117   Save the player's position
118   Save the players room
119   If the player has the see monster ability...
120   Remove the ability
121   BLOCK END - new_level
122   BLANK
123   COMMENT
124   COMMENT
125   COMMENT
126   COMMENT

CHOOSE A RANDOM ROOM

127   Define rnd_room with no arguments
128   BLOCK START - rnd_room, returns the index to a random room
129   Declare the return index
130   BLANK
131   Start a loop to find a valid room
132   Choose a random room
133   Repeat loop if it's not actually a room or is a maze
134   Return the selected room
135   BLOCK END - rnd_room
136   BLANK
137   COMMENT
138   COMMENT
139   COMMENT
140   COMMENT

GENERATE POTIONS AND SCROLLS

141   Defines put_things with no arguments
142   BLOCK START - put_things, places potions and scrolls
143   Initialize an iterator to 0
144   Declare a pointer to an object
145   Declare an index for a room
146   Declare a coordinate for objects
147   BLANK
148   COMMENT
149   COMMENT
150   COMMENT
151   COMMENT
152   COMMENT
153   COMMENT
154   If the player has seen the amulate and is on the way out...
155   Reduce the number of allowable objects by 1 (reserved for amulate)
156   Otherwise, the amulet was missed and needs to be generated
157   COMMENT
158   COMMENT
159   COMMENT
160   COMMENT
161   COMMENT
162   COMMENT
163   If the player is beyond the amulet and hasn't seen it...
164   And we can generate an item...make the amulet
165   Add the item to the level
166   No hit bonus
167   No damage bonus
168   Worst armor class / power
169   Set the type to amulet
170   COMMENT
171   COMMENT
172   COMMENT
173   Loop to find a valid position
174   Choose a random room
175   Choose a random position
176   Repeat loop until a valid position is found
177   Draw the amulate
178   Match the position with the amulet
179   End check for available items
180   End placing of the amulate
181   COMMENT
182   COMMENT
183   COMMENT
184   If we roll a random chance for a treasure room
185   Make the treasure room
186   End check for regenerating the amulet
187   COMMENT
188   COMMENT
189   COMMENT
190   Loop through all the possible objects on a level
191   Randomly roll 35% chance to generate an item
192   COMMENT
193   COMMENT
194   COMMENT
195   Generate an item
196   Add it to the level
197   COMMENT
198   COMMENT
199   COMMENT
200   Loop to find a position
201   Get a random room
202   Get a random position in the room
203   Repeat the loop until a valid position is found
204   Add the object to that position
205   Match the object position to the generated position
206   End check for item generation
207   BLOCK END - put_things
208   BLANK
209   COMMENT
210   COMMENT
211   COMMENT
212   COMMENT
213   Define the maximum tries for a monster in the treasure room (10)
214   BLANK

CREATE A TREASURE ROOM

215   Defines treas_room with no arguments
216   BLOCK START - treas_room, create a treasure room
217   Declare a monster counter and an index
218   Delcare a pointer to an object
219   Delcare a pointer to a room
220   Declare counters for treasure spots and more monsters
221   Declare a coordinate for a monster
222   BLANK
223   Get a pointer to a random room
224   The number of treasure spots is based off of the room size
225   If the number of room spots is larger than the maximum number
226   Treasure spots is the maximum number
227   Match the number of numbers to the amount of treasure
228   Loop for each item to generate
229   BLOCK START - Treasure room item loop
230   Loop to find a room position
231   BLOCK START - Position loop
232   Choose a random position
233   Get the index
234   BLOCK END - Position loop (check for validity)
235   Generate a new item
236   Copy the coordinates
237   Add the item to the level
238   Put the item on the map
239   BLOCK END - Treasure room item loop
240   BLANK
241   COMMENT
242   COMMENT
243   COMMENT
244   BLANK
245   Choose a random number of monsters based on treasure count. If its high
246   Clamp the monster count to the max value plus 2
247   Get the count of floor positions
248   If there are more monsters than floor positions...
249   Clamp the number of monsters to the number of floor positions
250   Temporarily increase the level number
251   For each monster we've counted...
252   BLOCK START - Treasure room monsters
253   Loop through all the identified spots
254   BLOCK START - Find a valid position
255   Find a random position
256   Get the index of the position
257   If the floor position is valid with no monster...
258   We've found a spot, break from the search loop
259   BLOCK END - Find a valid position
260   If we haven't busted the attemp count...
261   BLOCK START - Monster placement
262   If we can successfully allocate a monster
263   BLOCK START - Create monster
264   Initialize monster at the empty position with the new level value
265   Check for preprocessor test flag
266   If placing a monster failed...
267   Print debug message
268   End check for preprocessor test flag
269   All treasure room monsters are mean
270   Give all monsters some inventory
271   BLOCK END - Create monster
272   BLOCK END - Monster placement
273   BLOCK END - Treasure room monsters
274   Bring the level back down to this level
275   BLOCK END - treas_room
276   EOF