Tree warp, or Dynamic Sprite Spawn Overflow (DSSO) for the glitch in general, is a strong contender for the coolest glitch. I've explained it before using the Agahnim 2 stal, but for this explanation, I'll be focusing on tree warp.
There's a lot to discuss with how sprites spawn, but let's focus on the basics for this. The basics are all we really need. There are 16 active sprite slots in WRAM, and there are 3 ways to spawn sprites:
When a sprite is spawned, no matter what method, it undergoes certain preparations to set its coordinates, AI pointers, etc. This happens exactly once, but who performs this prep depends on the method the sprite is spawned with.
As mentioned before, there are only 16 slots for active sprites to be in. To make sure there's room,
If there was no room, then our index register hits −1, which for an 8-bit two's-complement is
And handle that they do. For the most part. Almost every call to this routine is followed by the instruction
The same can't be said for whatever intern coded the talking tree sprite. These guys break so many rules. First off, they use 3 sprite slots. 1 for the mouth, 2 for the eyes. Secondly, they have the persistence bit set. You see, on the overworld, when most sprites go off screen, they have the decency to deactivate themselves and return to the buffer. This frees up space for other sprites to spawn. Not talking trees. Once those 3 sprites are in their slots, they grasp onto them until you transition. If you visit both trees in the Village of Outcasts, you now have tree rudely hogging 40% of the slots.
And finally (for the sake of time), trees spawn their eyeballs recklessly. Only the mouth is a semi-static spawn. When it's spawned into the main list, its prep routine performs 2 calls of
Tree warp specifically happens when there is exactly 1 free sprite slot; i.e. the mouth takes the last available slot, leaving no space for either of the eyeballs.
What exactly happens when this assumption is made depends on where it was made, but in every case, just spawning a sprite is not good enough. Now that the sprite is spawned, subsequent routines continue prepping it in whatever ways are needed. There's just one itsy-bitsy problem: our index is out of whack. Remember: failure is indicated by an index of
That failsafe is important. Most sprite data is near other sprite data, so a value intended for one property, if using an out-of-bounds index, will be written to an unrelated property. These are mostly inconsequential places, really. Mostly. Offsetting 255 into most of the properties gives us a write to some timer, or the stripes DMA buffer. You might cause a jerk in some sprite's behavior, but nothing that big. The DMA buffer is just going to be updated before it's needed, so it doesn't cause any issues whatsoever.
The interesting write we care about is the X-coordinate low byte, which writes to the array at
The X-coordinate is loaded from the address
Tracing backwards, the last time these were modified was during the sprite prep routine for the tree's mouth. It took the sprite ID and called
In the case of tree warp, the routine we jumped to is
We are writing
Let's quickly explain how wallmaster grabbing works: If the wallmaster is alive, it doesn't let go of Link. If it's active, it can execute its code. If the address
Simply by how overworld spawning works, it is very likely that a tree will be in sprite slot 15. This means sprite slot 15 will already be set as active, permanently at that. For some reason that I'm too scared to learn, the routine outlined above also stores each coordinate in another address. In the case of the X-coordinate, this address is
So that means we're not just spawning a wallmaster. We are spawning a super wallmaster. One from which there is no escape.
In cases where there is not an eyeball as the last sprite, it seems the wallmaster just behaves normally. If it's offscreen, it will despawn to free up space, but, being a dynamically spawned bastard, it will find no home in the buffer. If it's onscreen, you may catch a glimpse of it flying away. I've seen it only once, but it was a magical moment.
Tree warp works when only 1 sprite slot is free before spawning the mouth. When 2 slots are free, things go really really wrong and the game crashes.
This crash is purely bad luck with ROM data. If 2 slots are free, that means the mouth takes one and eyeball 0 takes the other. So eyeball 0 spawns successfully, in the correct slot, and due to the prep that happens in
When it comes to sprite IDs, 3 is particularly evil. There isn't actually a sprite with that ID. That slot in the sprite routines table just contains a pointer to
And that's where Mr. Smith hits the fan.
You're in for a challenge if you want to overflow any of your other spawns. I will cover the ones that are reasonably doable.
In the Village of Outcasts, where the most famous trees reside, you can spawn a vulture by pulling the Thieves' Town gate with every sprite slot filled. This one may be slightly easier than tree warp, but all it spawns is a really ugly bird.
The next easiest DSSO to perform is the digging game. Not even a perfect TAS can dig up enough prizes fast enough to fill the sprite list unassisted. I've tried. Although, with a little help from Qirn, you can bring a cucco down to the digging game and smack it silly until it summons a swarm. For this DSSO, the sprite spawned is based on your X-coordinate. You'll get sprite
With perfect timing, you can mess with the clone animations in the Agahnim 2 fight and have them fill every slot. If the main Agahnim spawns the phantom Ganon then, he'll end up creating a hopping bush stal instead (sprite
I've not actually seen it done, but it should be possible to bring enough sprites on screen on Death Mountain so that when the old man tagalong turns into a sprite, he DSSOs himself. In this case, you just softlock your game. The old man sprite is what gives you control of Link by ending the cutscene. It's sort of like returning him in the rain state without an overlord, but slightly less funny, because there's no geezer wandering off into infinity.
Except for the tree warp, everywhere else that a dynamic sprite spawn overflow can occur is pretty difficult to pull off. For the most part, it's a fair assumption that there would be room somewhere. After all, if that assumption were bad, it wouldn't have taken 27 years for this glitch to be discovered. Actually it was just under 27 years. I found this on 12 November 2018, 9 days before LTTP's birthday. It also only happened during trial and error optimization of a TAS, and it was difficult to reproduce at first.
For some stupid reason, the talking tree is composed of 3 permanently active sprites—1 mouth, 2 eyes. The eyes are spawned dynamically by the mouth. When spawning sprites dynamically, the caller of the routine is left with the responsibility for adding a failsafe when all slots are full. Trees don't.
If the mouth spawns into the last available sprite slot, the eyes will end up in slot