Decoded: OpenTTD (2003) v1.8 (2018) Source file: random_func.hpp Line-by-line code walkthrough by MaiZure random_func.hpp defines Randomizer class and support fucntions Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/random_func.hpp 1 COMMENT (*) 2 BLANK (-) 3 * 4 * 5 * 6 * 7 * 8 * 9 - 10 * 11 - 12 Header guard 13 Header guard 14 - 15 If we're using an Apple system... 16 * 17 Define the flag to use the builtin 18 End check for Apple 19 - 20 * 21 * 22 * 23 BLOCK START - Randomizer, stores one RNG 24 * 25 Declares an array of two 32-bit unsigned ints for state 26 - 27 Declares a Next function for getting the next random number 28 Declares another Next function to limiting the random number 29 Declares a SetSeed function to prime the RNG 30 BLOCK END - Randomizer 31 Imports one global RNG 32 Imports the other global RNG 33 * 34 * 35 BLOCK START - SaveRandomSeeds, holds randomizers 36 Declares one Randomizer storage space 37 Declares the other Randomizer storage space 38 BLOCK END - SaveRandomSeeds 39 - 40 * 41 * 42 * 43 * 44 Defines SaveRandomSeeds with one argument: the storage struct 45 BLOCK START - SaveRandomSeeds, stores the RNG state in a struct 46 Assigns the first global RNG to the storage container (by value!) 47 Assigns the second global RNG, also by value 48 BLOCK END - SaveRandomSeeds 49 - 50 * 51 * 52 * 53 * 54 Defines RestoreRandomSeeds with one argument: the storage container 55 BLOCK START - RestoreRandomSeeds, restores the saved RNG state 56 Restore the first global RNG to a previously saved value 57 Restores the second global RNG to a previously saved value 58 BLOCK END - RestoreRandomSeeds 59 - 60 Forward declares SetRandomSeed with one argument 61 If we're currently debugging the RNG 62 If this is an Apple... 63 Define the built in random function for Apple built in 64 Otherwise this isn't apple so.. 65 Use another random function 66 End check for Apple 67 Declare DoRandom with two arguments, line number and file name 68 Define a RandomRange macro function as DoRandomRange 69 Forward declare DoRandomRange 70 If we're not in debug mode 71 Defines Random with no arguments 72 BLOCK START - Random, pulls a value from RNG 73 Returns a number from the RNG 74 BLOCK START - Random 75 - 76 * 77 * 78 * 79 * 80 * 81 * 82 * 83 Defines RandomRange with one argument: the upper limit 84 BLOCK START - RandomRange, returns a range-restricted number 85 Returns a number from the RNG with a limit 86 BLOCK END -RandomRange 87 End check for debug mode 88 - 89 Defines InteractiveRandom with no arguments 90 BLOCK START - InteractiveRandom, Non-game state RNG 91 Returns a value from the other RNG 92 BLOCK END - InteractiveRandom 93 - 94 Defines InteractiveRandomRange with one argument: a limit 95 BLOCK START - InteractiveRandomRange, range-restricted second RNG 96 Returns a value from the other RNG with a limit 97 BLOCK END - InteractiveRandomRange 98 - 99 * 100 * 101 * 102 * 103 * 104 * 105 * 106 * 107 * 108 * 109 * 110 * 111 * 112 * 113 * 114 Defines Chance16I with three arguments: numerator, denominator and random number 115 BLOCK START - Chance16I, returns true or false based on input probability 116 Checks that b isn't zero (avoid div/0 error) 117 Checks if the random number beat the probability 118 BLOCK END - Chance16I 119 - 120 * 121 * 122 * 123 * 124 * 125 * 126 * 127 * 128 * 129 * 130 If we're debugging RNG.. 131 Defines a macro for Chance16 with a random number (local scope usage) 132 End check for RNG debug mode (use a normal function) 133 Defines Chance16 with two arguments: a probability num/dem 134 BLOCK START - Chance16, returns true or false with given probability 135 Returns the value of Chance16I using RNG for an input 136 BLOCK END - Chance16 137 Ends check for random debug mode 138 - 139 * 140 * 141 * 142 * 143 * 144 * 145 * 146 * 147 * 148 * 149 * 150 * 151 * 152 * 153 * 154 If we're in random debug mode 155 Defines macro for Chance16R in terms of DoRandom 156 Ends check for debug mode 157 Defines Chance16R with with three input values: probability fraction num/dem and a variable to store result 158 BLOCK START - Chance16R, Gets a random number 159 Stores the random number 160 Returns the boolean roll result using the random number 161 BLOCK END - Chance16R 162 Ends check for random debug 163 - 164 Header guard