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

INIT.C initializes game globals to starting values

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

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




1     COMMENT
2     COMMENT
3     COMMENT
4     COMMENT
5     COMMENT
6     BLANK
7     Include the game header
8     Include the console management header
9     BLANK
10    Create a global object pointer
11    Create a pointer to hold temporary memory allocations
12    BLANK
13    Declare a function that returns a pointer to new memory (custom malloc!)
14    BLANK
15    COMMENT
16    COMMENT
17    COMMENT
18    COMMENT

INITIALIZE PLAYER

19    Defines init_player with no arguments
20    BLOCK START - init_player, sets starting variables for the player
21    Declare a local object pointer
22    Copy the player maximum stats to the current player stats
23    Set the player's food level (this is random)
24    COMMENT
25    COMMENT
26    COMMENT
27    Initialize temporary object holder to zeros
28    Initialize temporary memory to zeroes
29    COMMENT
30    COMMENT
31    COMMENT
32    Create a new item
33    New item is a weapon
34    New item is a mace
35    Initialize the new mace to base statistics
36    Adds a bonus of 1 to hit with the new mace
37    Adds a bonus of 1 to damage with the new mace
38    The player knows about this mace
39    There is only 1 mace
40    Item belongs to the base group
41    Add the mace to the player's inventory
42    The player is using the mace
43    COMMENT
44    COMMENT
45    COMMENT
46    Create a new item
47    The new item is a weapon
48    The new weapon is a a bow
49    Set the base stats for the bow
50    The bow has a hit bonus of 1
51    The bow has a damage bonus of 1
52    There is only 1 bow
53    Add the bow to the base item group
54    The player knows about the bow
55    Add the bow to the player's backpack
56    COMMENT
57    COMMENT
58    COMMENT
59    Create a new item
60    The item is a wepaon
61    The weapon is an arrow
62    Set the arrow's base stats
63    Set a random number of arrows between 25 and 40
64    Arrows don't have a damage bonus
65    The player knows about the arrows
66    Add the arrows to the player inventory
67    COMMENT
68    COMMENT
69    COMMENT
70    Create a new item
71    The new item is armor
72    The armor is ring mail
73    Give the new ring mail a 1 AC bonus over standard
74    The player knows about the ring mail
75    There is only 1 ring mail
76    The ring mail is part of the base group
77    The player is currently wearing the ring mail
78    Add it to the player's inventory
79    COMMENT
80    COMMENT
81    COMMENT
82    Create a nw item
83    The item is food
84    There is only 1 food
85    The food is the base subtype
86    It belongs to the base gorup
87    Add the food to the player's inventory
88    BLOCK END - init_player
89    BLANK
90    COMMENT
91    COMMENT
92    COMMENT
93    COMMENT
94    BLANK

INITIALIZE NAMES

95    Declares a global array of color strings
96    A random color
97    A random color
98    A random color
99    A random color
100   A random color
101   A random color
102   A random color
103   A random color
104   A random color
105   A random color
106   A random color
107   A random color
108   A random color
109   A random color
110   A random color
111   A random color
112   A random color
113   A random color
114   A random color
115   A random color
116   A random color
117   A random color
118   A random color
119   A random color
120   A random color
121   A random color
122   A random color
123   End of the color array
124   BLANK
125   Create a macro of the number of colors (auto updates as the list grows)
126   BLANK
127   Declares a global array of the English consonants 
128   Declares a global array for vowels
129   BLANK
130   Declares a struct for gemstones
131   The first element is the name of the stone
132   The second element is the value of the stone
133   End declaration of the gemstone array
134   BLANK
135   Declare the gemstone struct values of gemstone/value pairs
136   A random gemstone
137   A random gemstone
138   A random gemstone
139   A random gemstone
140   A random gemstone
141   A random gemstone
142   A random gemstone
143   A random gemstone
144   A random gemstone
145   A random gemstone
146   A random gemstone
147   A random gemstone
148   A random gemstone
149   A random gemstone
150   A random gemstone
151   A random gemstone
152   A random gemstone
153   A random gemstone
154   A random gemstone
155   A random gemstone
156   A random gemstone
157   A random gemstone
158   A random gemstone
159   A random gemstone
160   A random gemstone
161   A random gemstone
162   End of the gemstone name list
163   BLANK
164   Define a macro for the number of gemstones. Index boundary
165   BLANK
166   Declares a global array for the possible wood material type strings
167   A random wood material
168   A random wood material
169   A random wood material
170   A random wood material
171   A random wood material
172   A random wood material
173   A random wood material
174   A random wood material
175   A random wood material
176   A random wood material
177   A random wood material
178   A random wood material
179   A random wood material
180   A random wood material
181   A random wood material
182   A random wood material
183   A random wood material
184   A random wood material
185   A random wood material
186   A random wood material
187   A random wood material
188   A random wood material
189   A random wood material
190   A random wood material
191   A random wood material
192   A random wood material
193   A random wood material
194   A random wood material
195   A random wood material
196   A random wood material
197   A random wood material
198   A random wood material
199   A random wood material
200   End of the wood materal array
201   BLANK
202   Defines a macro for the number of wood material types. Index boundary
203   BLANK
204   Declares a global array for metal material type names
205   A random metal material string
206   A random metal material string
207   A random metal material string
208   A random metal material string
209   A random metal material string
210   A random metal material string
211   A random metal material string
212   A random metal material string
213   A random metal material string
214   A random metal material string
215   A random metal material string
216   A random metal material string
217   A random metal material string
218   A random metal material string
219   A random metal material string
220   A random metal material string
221   A random metal material string
222   A random metal material string
223   A random metal material string
224   A random metal material string
225   A random metal material string
226   A random metal material string
227   A random metal material string
228   BLANK
229   Defines a macro for the number of metal materials
230   BLANK
231   COMMENT
232   COMMENT
233   COMMENT
234   COMMENT

INITIALIZE ITEM PROBABILITIES

235   Defines init_things with no arguments
236   BLOCK START - init_things, sets up a cumulative probability table
237   Declare a pointer to an item
238   BLANK
239   Loop from second element to the final element of the things table
240   Add the previous element's probability to the current (converts from 
      absolute to cumulative probability)
241   BLOCK END - init_things
242   BLANK
243   COMMENT
244   COMMENT
245   COMMENT
246   COMMENT

INITIALIZE POTION COLORS

247   Define init_colors with no arguments
248   BLOCK START - init_colors, sets up a palette table for this game run
249   Declare local integer iterators/indicies
250   Declare an array of booleans (this C compiler apparently supported bool
      as a native data type in the early 1980s. I suspect it was typed to int
	  but the code doesn't specifically say)
251   BLANK
252   Loop through all booleans based on color used
253   Disable all usage masks
254   Loop through all potion types
255   BLOCK START - Potion init loop
256   Start a loop (find an unused color)
257   Choose a random color
258   Continue loop if color has already been used
259   Mark the chosen color as used
260   Set the current postion to the chosen random color
261   Player doesn't know the potion type yet
262   Associate the player's guess with the master array of guesses
263   If this isn't first potion...
264   Convert the potion's appearance probability from absolute to cumulative
265   BLOCK END - potion init loop
266   BLOCK END - init_colors
267   BLANK
268   COMMENT
269   COMMENT
270   COMMENT
271   COMMENT
272   BLANK

INITIALIZE SCROLL NAMES

273   Defines init_names with no arguments
274   BLOCK START - init_names, sets the randomized unknown scroll names
275   Declare an integer to index the random syllable table
276   Declare two temporary pointers
277   Declare an integer iterator and an integer counter
278   BLANK
279   Loop through all scroll types
280   BLOCK START - scroll type loop
281   Point to the global character buffer
282   Set the number of words based on terse mode
283   For each syllabus
284   BLOCK START - word loop
285   Choose a number of syllables
286   Loop on number of syllables
287   BLOCK START - syllable loop
288   Get a random syllable
289   If the total length so far is larger than the word buffer
290   BLOCK START - check for valid size
291   Stop adding new words
292   End the loop
293   BLOCK END - check for valid size
294   If there is a non-null character
295   Copy the characters
296   BLOCK END - syllable loop
297   Add a space between words
298   BLOCK END - word loop
299   End of phrase - null-term it
300   COMMENT
301   COMMENT
302   COMMENT
303   Null-term the last element in the print buffer, just in case.
304   Player doesn't know any scroll that has a random name
305   Point between the scroll guesses to the master guess list
306   Copy the ranom name from the buffer to the scroll name
307   If this isn't the first scroll...
308   Convert the scroll's appearance probability from absolute to cumulative
309   BLOCK END - scroll type loop
310   BLOCK END - init_names
311   BLANK
312   COMMENT
313   COMMENT
314   COMMENT
315   COMMENT

RANDOM SYLLABLE

316   Define getsyl with no arguments
317   BLOCK START - getsyl, generates a syllable for a random name
318   Declare a temporary character array with 4 elements
319   BLANK
320   The final character is always null
321   The third character is a random consonant
322   The second character is a random vowel
323   The first characer is a random consonant
324   Return a pointer to the temporary array
325   BLOCK END - getsyl
326   BLANK
327   COMMENT
328   COMMENT
329   COMMENT
330   COMMENT

RANDOM CHARACTER

331   Define rchr with one argument
332   Argument 1 is a random string
333   BLOCK START - rchr, returns a random character from an input string
334   Return a random character within the string
335   BLOCK END - rchr
336   BLANK
337   COMMENT
338   COMMENT
339   COMMENT
340   COMMENT

INITIALIZE STONES

341   Define init_stones with no arguments
342   BLOCK START - init_stones, matches a random mix of rings and stones
343   Declare temporary iterator integers
344   Declare a boolean array for each stone type
345   BLANK
346   Loop through each stone
347   Set each stone usage to false
348   Loop through each ring type
349   BLOCK START - ring init loop
350   Choose a random stone
351   Repeat choosing a stone if it's alread been used
352   Set the unused stone to used
353   Set the ring name to the stone name
354   Player doesn't know the ring yet
355   Associate the ring guess with the master guess list
356   If this isn't the first ring...
357   Convert the ring's appearance probability from absolute to cumulative
358   Add the source stone's base value to the ring's current value
359   BLOCK END - ring init loop
360   BLOCK END - init_stones
361   BLANK
362   COMMENT
363   COMMENT
364   COMMENT
365   COMMENT

INITIALIZE MATERIALS

366   Define init_materials with no arguments
367   BLOCK START - init_materials, randomly matches wood and staff types
368   Declare integer iterators
369   Declare a pointer to a character
370   Declare a boolean array for usage of both wood and metal materials
371   BLANK
372   Loop through all wood materials
373   Set the used array to false
374   Loop through all metal materials
375   Set the used array to false
376   Loop through all wands/staves
377   BLOCK START - Wand/staff init loop
378   Loop forever (until code break within the loop)
379   If a random number is 0 (1/3rd chance)...
380   BLOCK START - Init a wand
381   Choose a random metal index
382   If the metal hasn't been used...
383   BLOCK START - Unused metal
384   Item type is wand
385   Set temp string to the name of the metal
386   Set the metal as used
387   End the loop
388   BLOCK END - Unused metal
389   BLOCK END - Init a wand
390   otherwise...
391   BLOCK START - Init a staff
392   Choose a random wood metal
393   IF the wood type hasn't been used
394   BLOCK START - Unused wood
395   Set the item type name to staff
396   Set the temp string to the name of the wood
397   Set the wood type to used
398   End the loop
399   BLOCK END - Unused wood
400   BLOCK END - Init a staff
401   Set the name of the item
402   Player doesn't know the item yet
403   Associate the wand/staff guess with the master guess array
404   If this isn't the first item
405   Convert the item appearance probability from absolute to cumulative
406   BLOCK END - Wand/staff init loop
407   BLOCK END - init_materials
408   BLANK
409   COMMENT
410   COMMENT
411   COMMENT
412   Declare a pointer to the character levels
413   Declare a pointer to the fight message buffer
414   Declare a pointer to a general message buffer
415   Declare a pointer to the printing buffer
416   Declare a pointer to the end of the use dmemory
417   Delcare a pointer to the start of the memory block
418   Declare a pointer to the start of heap (After code/compile time vars)
419   Declare a string buffer used for holding ring stat text
420   BLANK
421   COMMENT
422   COMMENT
423   COMMENT
424   Declare a pointer to the level data
425   Declare a pointer to the flags for level data
426   BLANK
427   COMMENT
428   COMMENT
429   COMMENT
430   COMMENT

INITIALIZE DATA SEGMENT

431   Define init_ds with 1 argument
432   Argument 1 is not used
433   BLOCK START - init_ds, sets up the game heap (single static allocation)
434   Declare a 32-bit pointer 
435   BLANK
436   Allocate memory for level flags
437   Allocate memory for level data
438   Allocate memory for items
439   Allocate memory for temporary strings
440   BLANK
441   Allocate memory for all static strings
442   Allocate memory for a message buffer
443   Allocate memory for a print buffer
444   Allocate 6 bytes for ring stat printouts
445   Allocate memory for experience levels
446   Loop through each experience levels
447   Each level requires double the experience pointers
448   Null out the experience level pointer
449   BLOCK END - init_ds
450   EOF 