Thursday, January 19, 2017

The Truth Spinner

Make my beak face the skull of truth. The alternate is descent into the deep darkness.
 The "truth spinner" puzzle is designed to stop the player from attempting the Shadow Temple without the Lens of Truth. On the surface, it looks as if there are five possible skulls to pick from, and thus a 20% chance of getting the correct skull first try. But having looked into it deeper, it turns out that there's a particular skull with a 44.4% chance of being the skull of truth.

While all of the actors related to the truth spinner puzzle (the statue, the trap door, the gate, and the five skull torches) are managed by ovl_bg_haka_gate, they are all spawned individually by the room data itself, possibly to make it easy to position these elements with whatever level editor Nintendo came up with. A small problem with doing things this way is that there is no "master" actor (say the spinner for example) that readily has a pointer handle to the other actors (the torches), allowing them to communicate directly.

Instead, the actors act in a more decentralized manner by letting the torch actors determine individually which one will become the skull of truth. Of the five torches, the two closest to the wooden torch will never be selected, perhaps to stop people from "cheesing" through the puzzle by simply turning to the first skull over and over until it works. To eliminate these two torches, the initializer function for the torches start by checking their y-rotation component to see if they should move on to the skull of truth check.

The remaining skull torches continue on by reading the halfword at NTSC 1.0 80A310B0 (which resolves to 801EA5B0 when spawning in from the main entrance), which stores what y-rotation the statue must be spun to in order to solve the puzzle. If this value is not 0x0100 (the initial "dummy" value), the torch generates a random number between 0.0 and 3.0 exclusive. It then reads in a byte value at 80A310B4 or 801EA5B4 (which has an initial value of 1) and checks if the randomly generated number is less than this byte value. If so, that torch becomes the skull of truth, otherwise the byte value is incremented by 1. Thus, the second skull, if checked, tests if it's randomly generated number is < 2, and the third, if checked, tests if it's < 3 (which it should always be). 

Because a new random number is generated at each step, the odds aren't an even 33.3% chance. Instead...
  • The first skull processed has a 33.3% (3/9) chance of being picked
  • The second skull processed has a 44.4% (4/9) chance of being picked
  • The third skull processed has a 22.2% (2/9) chance of being picked
Since I've been curious whether any biases would occur due to how random number generation works in Ocarina of Time, I wrote a program to test all 2^32 possibilities when spawning from the Shadow Temple main entrance. The source can be found here (see TruthSpinner.cs).

The final results weren't too surprising, but it was an interesting test none the less...
  • The first skull processed was chosen 1,431,655,936 times, or 33.33333731%
  • The second skull processed was chosen 1,908,875,264 times, or 44.44446564%
  • The third skull processed was chosen 954,436,096 times, or 22.22219706%
Now that we know the odds, the last thing to figure out is the order in which the skull torch initializer functions are processed. When entering the dungeon, the initializers are called in reverse order in which the actors are spawned. Looking towards the wall leading toward the dungeon entrance and hover boots, the torch on the middle is spawned first, followed by the torch on the right, then lastly the torch on the left. Thus...
  • The skull on the far right has a 44.4% chance
  • The skull in the middle has a 22.2% chance
  • The skull on the left has a 33.3% chance
...at least when coming in from the main entrance. In theory, if the actors end up being spawned across multiple update cycles (perhaps when going through doors), the order in which their initializer functions are called could change... but to tell either way would take a bit more time than I want to spend on this for now. 

Special thanks to Ecksters for egging me on to figure this out, and dotzo for doing 999 trials in-game, which helped a bit in confirming the location of the bit of source that determined the skull of truth.

No comments:

Post a Comment