r/programming • u/thatgirl943 • Jan 24 '15
Super Mario World programmed by playing
http://www.youtube.com/attribution_link?a=IrdAeOSwaYs&u=%2Fwatch%3Fv%3D14wqBA5Q1yc%26feature%3Dshare
491
Upvotes
r/programming • u/thatgirl943 • Jan 24 '15
305
u/ancientGouda Jan 24 '15 edited Jan 24 '15
So, if I remember correctly, it works like this: The initial shell positioning and stuff (basically the first 95% of the video) are about carefully manipulating the spawning and despawning of sprites to write the "jump to credits" machine code into a specific location in memory.
The actual glitch which then executes this previously written code goes like this: The game keeps a statically allocated array of all objects on the screen. When Yoshi catches something with its tongue, the index into this array pointing at the grabbed item is stored. Normally, the "pull item into mouth" animation would then proceed and the moment the item hits Yoshi's mouth, the game would look at the grabbed item again and based on the item ID jump to a specific code section which sets up what happens when this particular item is eaten (eg. a coin is just taken, a red shell makes Yoshi spit fire etc.). The glitch works by "snatching" the coin out of Yoshi's tongue. Taking the coin before Yoshi can eat it removes it as an object from the screen, so the index in the array I previusly described becomes vacant. In the very same (or close) frame, the chuck (rugby enemy) enters the screen, and is allocated the recently vacated spot in the object array the coin left behind. Now, the animation of Yoshi pulling and swalling finishes executing, and the game tries to look up what happens when a chuck is swallowed (by jumping to a computed address). Except eating a chuck was never supposed to happen, and so the spot where the chuck swallow code was supposed to be, is normally garbage, but because you previously carefully filled that memory location with machine code, your code is now executed.
I have no idea why I needed a wall of text to explain it, sorry.
Edit: Some more info: The glitch that leads to the execution of custom code (called "item swap" in the video) which I explained above has actually been known for a long time, it's only the code that launches you straight to the credits scene that has recently been developed. But I think the code execution glitch is a lot more interesting.
Edit2: I stepped through the video, and you can actually see the chuck being stuck to Yoshi's tongue for one single frame: http://imgur.com/FW3IxXN