WEBVTT
Kind: captions
Language: en

00:00:00.320 --> 00:00:05.360
“Sugar, sugar” is a classic Flash game all&nbsp;
about getting grains of sugar into coffee cups,&nbsp;&nbsp;

00:00:05.360 --> 00:00:10.080
by drawing arbitrary lines with your mouse.&nbsp;
The concept is so simple that by level 2,&nbsp;&nbsp;

00:00:10.080 --> 00:00:12.400
you’re already familiar with the core mechanic.

00:00:12.400 --> 00:00:16.320
As a developer, games like these&nbsp;
often make me wonder how it was done,&nbsp;&nbsp;

00:00:16.320 --> 00:00:20.160
or how I would go about making something&nbsp;
like this. In the case of “Sugar, sugar”,&nbsp;&nbsp;

00:00:20.160 --> 00:00:25.920
the game is simulating up to 3000 grains of&nbsp;
sugar every frame. Not only that, it has to check&nbsp;&nbsp;

00:00:25.920 --> 00:00:31.280
collision against hand-drawn obstacles, and it&nbsp;
has to run decently on the computers of the time.

00:00:31.280 --> 00:00:36.560
My instincts told me that there’s no way you&nbsp;
could achieve this by actually iterating over 3000&nbsp;&nbsp;

00:00:36.560 --> 00:00:42.080
individual objects. So, I decompiled the code,&nbsp;
expecting to find some clever trick, perhaps even&nbsp;&nbsp;

00:00:42.080 --> 00:00:48.160
a simple fluid simulation, only to find some of&nbsp;
the most spaghetti-filled code I have ever seen.

00:00:48.160 --> 00:00:51.360
The entire game logic sits in a single class,&nbsp;&nbsp;

00:00:51.360 --> 00:00:57.200
which, when decompiled, takes up over&nbsp;
3700 lines of code. On top of that,&nbsp;&nbsp;

00:00:57.200 --> 00:01:03.840
it turns out that the sugar logic really is just&nbsp;
one big, dumb loop. Let me break it down for you.

00:01:03.840 --> 00:01:08.240
Every grain of sugar moves down at a&nbsp;
constant speed of 1 pixel per frame,&nbsp;&nbsp;

00:01:08.240 --> 00:01:11.040
with no acceleration from gravity. In most games,&nbsp;&nbsp;

00:01:11.040 --> 00:01:15.280
the lack of acceleration on a falling&nbsp;
object would make it feel unnatural. But,&nbsp;&nbsp;

00:01:15.280 --> 00:01:19.760
since these are grains of sugar, I suppose&nbsp;
the implied air resistance makes up for that.

00:01:19.760 --> 00:01:23.840
On top of this downwards motion,&nbsp;
there’s also a random horizontal sway,&nbsp;&nbsp;

00:01:23.840 --> 00:01:29.440
up to 1 pixel in either direction. These two are&nbsp;
the primary factors that determine where the sugar&nbsp;&nbsp;

00:01:29.440 --> 00:01:34.720
is going to end up on the next frame. And that,&nbsp;
of course, is used for predicting collisions.

00:01:34.720 --> 00:01:39.280
This is where the game does, admittedly, come up&nbsp;
with a pretty clever trick. In order to detect a&nbsp;&nbsp;

00:01:39.280 --> 00:01:44.480
collision, the code checks the color value of&nbsp;
the pixel at the candidate position. If it’s&nbsp;&nbsp;

00:01:44.480 --> 00:01:49.920
below a certain level of brightness, the sugar&nbsp;
is allowed to pass. If it’s above that threshold,&nbsp;&nbsp;

00:01:49.920 --> 00:01:54.160
a collision is flagged. That’s why every&nbsp;
level has a dark background with brighter&nbsp;&nbsp;

00:01:54.160 --> 00:01:59.280
highlights. In fact, if I just switch to a&nbsp;
brighter color palette, the sugar never drops.

00:01:59.280 --> 00:02:03.920
To resolve this collision, the game drops&nbsp;
the candidate position, and instead checks&nbsp;&nbsp;

00:02:03.920 --> 00:02:08.800
for free space on either side of the current&nbsp;
position. The sugar grain then moves in the&nbsp;&nbsp;

00:02:08.800 --> 00:02:14.080
direction with at least 2 pixels of clearance.&nbsp;
And, with that being the whole algorithm,&nbsp;&nbsp;

00:02:14.080 --> 00:02:19.200
I guess it is simple enough to make it viable&nbsp;
for the average school computer in 2011.

00:02:19.200 --> 00:02:21.280
There are some fancier mechanics in the game,&nbsp;&nbsp;

00:02:21.280 --> 00:02:25.120
such as a button to invert gravity, or&nbsp;
holes that magically teleport the sugar,&nbsp;&nbsp;

00:02:25.120 --> 00:02:29.760
but those are relatively simple features that&nbsp;
don’t change the core “physics” of the game.

00:02:29.760 --> 00:02:34.800
With that in mind, in spite of the rancid codebase&nbsp;
full of duplicated code that requires a map to&nbsp;&nbsp;

00:02:34.800 --> 00:02:39.840
navigate, I can’t really blame “Sugar, sugar”&nbsp;
because a simple one-off Flash game doesn’t&nbsp;&nbsp;

00:02:39.840 --> 00:02:45.040
need clean code, and I guess it was still&nbsp;
cool to see those bitmap hacks for collision.

00:02:45.040 --> 00:02:49.520
Now, this got me wondering about other Flash&nbsp;
games with technically impressive mechanics,&nbsp;&nbsp;

00:02:49.520 --> 00:02:54.560
so I put out a YouTube community post asking&nbsp;
you all for ideas on where to look next.

00:02:54.560 --> 00:02:58.480
Of course, right off the bat, there were a&nbsp;
few mentions of “Portal: The Flash Version”.&nbsp;&nbsp;

00:02:58.480 --> 00:03:03.920
Given the... branding of this channel, that&nbsp;
does make a lot of sense. So, let’s take a look.

00:03:03.920 --> 00:03:08.080
Now, the teleportation mechanic isn’t that&nbsp;
interesting on its own. The simplest way&nbsp;&nbsp;

00:03:08.080 --> 00:03:11.440
to teleport something in a game is&nbsp;
to just set its coordinates. But,&nbsp;&nbsp;

00:03:11.440 --> 00:03:16.160
of course, when implementing portals,&nbsp;
things aren’t that straightforward.

00:03:16.160 --> 00:03:21.840
At the very least, you have to be able to redirect&nbsp;
velocity. After all, when speedy thing goes in,&nbsp;&nbsp;

00:03:21.840 --> 00:03:26.960
speedy thing comes out. Valve implements this&nbsp;
by translating your velocity vector to the new&nbsp;&nbsp;

00:03:26.960 --> 00:03:31.840
reference frame. This is universally applicable&nbsp;
and keeps the behavior of portals consistent.

00:03:31.840 --> 00:03:36.720
The Flash version however, has hard-coded&nbsp;
behaviors for each of the four cardinal&nbsp;&nbsp;

00:03:36.720 --> 00:03:41.760
directions. This is why you won’t find a slanted&nbsp;
portal surface – the engine simply doesn’t support&nbsp;&nbsp;

00:03:41.760 --> 00:03:46.880
it. Yes, this simplifies the math a bit, but&nbsp;
that comes at the cost of less flexibility and&nbsp;&nbsp;

00:03:46.880 --> 00:03:52.960
more duplicated code. Now, it’s not really that&nbsp;
hard to rotate a vector, especially in 2D, but I&nbsp;&nbsp;

00:03:52.960 --> 00:03:57.360
get the feeling that the developers of this game&nbsp;
just weren’t as familiar with the math involved.

00:03:57.360 --> 00:04:02.640
Also, notice how in Valve’s implementation,&nbsp;
objects near a portal are duplicated on&nbsp;&nbsp;

00:04:02.640 --> 00:04:06.320
the other side, making you feel like&nbsp;
you’re actually moving between portals,&nbsp;&nbsp;

00:04:06.320 --> 00:04:09.040
not just teleporting. This is&nbsp;
what lets you have two halves&nbsp;&nbsp;

00:04:09.040 --> 00:04:13.280
of a cube in different locations,&nbsp;
despite there only being one cube.

00:04:13.280 --> 00:04:18.240
The Flash version doesn’t bother with this.&nbsp;
Instead, as you step through a portal, the&nbsp;&nbsp;

00:04:18.240 --> 00:04:23.520
collision for the respective wall is overridden,&nbsp;
and your jump key is disabled, until you’ve fully&nbsp;&nbsp;

00:04:23.520 --> 00:04:28.320
gone through the wall, at which point, you&nbsp;
simply get ejected out on the other side.

00:04:28.320 --> 00:04:33.920
That said, the other portal does react somewhat&nbsp;
by rendering a blurry figure of the player as&nbsp;&nbsp;

00:04:33.920 --> 00:04:38.800
they approach. This is an imitation of how&nbsp;
Valve’s portals render what’s on the other side.&nbsp;&nbsp;

00:04:38.800 --> 00:04:42.800
Valve does this by masking a separate&nbsp;
camera onto the face of the portal.

00:04:42.800 --> 00:04:48.160
But the Flash version’s approach is a lot more&nbsp;
primitive. This is just a blurry copy of the&nbsp;&nbsp;

00:04:48.160 --> 00:04:51.600
player sprite. I went ahead and unblurred it&nbsp;
in the game code to better show what’s going&nbsp;&nbsp;

00:04:51.600 --> 00:04:56.480
on. In many cases, the sprite is actually&nbsp;
upside-down, and in other cases it’s also&nbsp;&nbsp;

00:04:56.480 --> 00:05:02.240
moving the wrong way. Notice how here I’m&nbsp;
jumping up, yet the sprite is moving down.

00:05:02.240 --> 00:05:06.720
Granted, this is normally just subtle visual&nbsp;
flair, but it just goes to show how hard it&nbsp;&nbsp;

00:05:06.720 --> 00:05:10.720
is to make portals the way Valve did it.&nbsp;
After all, one of the main reasons they&nbsp;&nbsp;

00:05:10.720 --> 00:05:15.920
hired the Narbacular Drop team is because they had&nbsp;
already figured out all of the complicated math.

00:05:15.920 --> 00:05:19.600
I'd say that at least a part of these&nbsp;
discrepancies can likely be attributed&nbsp;&nbsp;

00:05:19.600 --> 00:05:23.760
to the Flash version team’s inexperience&nbsp;
in developing something like this. There’s&nbsp;&nbsp;

00:05:23.760 --> 00:05:28.880
a lot of duplicated code here, which&nbsp;
is a red flag in almost any code base.&nbsp;&nbsp;

00:05:28.880 --> 00:05:32.560
And, actually, here’s a good&nbsp;
example of why that’s the case.

00:05:32.560 --> 00:05:34.880
This is the code that handles collision near&nbsp;&nbsp;

00:05:34.880 --> 00:05:39.040
portals. Feel free to pause the video&nbsp;
and see if you can spot the mistake.

00:05:39.040 --> 00:05:43.680
Got your answers ready? Alright. Now, the&nbsp;
word “addition” is misspelled in all of&nbsp;&nbsp;

00:05:43.680 --> 00:05:48.080
these variables, but that on its own isn’t&nbsp;
really a problem. Code mostly doesn’t care&nbsp;&nbsp;

00:05:48.080 --> 00:05:51.920
about English grammar as long as you&nbsp;
keep the naming consistent. But here,&nbsp;&nbsp;

00:05:51.920 --> 00:05:57.840
on this one line, the misspelling is&nbsp;
further misspelled, missing the letter A.

00:05:57.840 --> 00:06:01.680
One consequence of this is that you&nbsp;
can jump on the edge of a right-facing&nbsp;&nbsp;

00:06:01.680 --> 00:06:04.880
portal. I always thought that this&nbsp;
was intended behavior, but if you&nbsp;&nbsp;

00:06:04.880 --> 00:06:09.360
try to do the same thing on a left-facing&nbsp;
portal, you’ll just fall right down. And,&nbsp;&nbsp;

00:06:09.360 --> 00:06:15.520
if I patch the code to fix this bug, the same&nbsp;
thing now happens on both types of portals.

00:06:15.520 --> 00:06:19.280
While this ultimately is just a silly&nbsp;
typo, it would’ve been easier to spot&nbsp;&nbsp;

00:06:19.280 --> 00:06:24.480
if these calculations weren’t duplicated and&nbsp;
hard-coded to specific angles. In that case,&nbsp;&nbsp;

00:06:24.480 --> 00:06:27.920
it would’ve been obvious if something&nbsp;
didn’t work quite right. But, with&nbsp;&nbsp;

00:06:27.920 --> 00:06:34.160
the way it's implemented now, the developers&nbsp;
only have a 1 in 2 chance to spot the issue.

00:06:34.160 --> 00:06:37.200
Now, if it seems like I’m being&nbsp;
overly critical of these games,&nbsp;&nbsp;

00:06:37.200 --> 00:06:42.320
you’re probably right. Every game has nasty&nbsp;
code – there’s a reason why developers almost&nbsp;&nbsp;

00:06:42.320 --> 00:06:46.960
never make it public. As long as it’s fun&nbsp;
to play, the rest is just a means to an end.

00:06:46.960 --> 00:06:51.600
Keep in mind that Flash was at its peak during an&nbsp;
era of the internet where programming knowledge&nbsp;&nbsp;

00:06:51.600 --> 00:06:57.200
was rare, yet it often took just one passionate&nbsp;
developer to create what we now regard a classic.

00:06:57.200 --> 00:07:01.360
Things have changed since then, and programming&nbsp;
has become more accessible. Of course,&nbsp;&nbsp;

00:07:01.360 --> 00:07:07.120
for creativity, you’ve still got your&nbsp;
heart. But for code, there’s Coddy.Tech.

00:07:07.120 --> 00:07:11.600
Coddy is the sponsor of this video, and has&nbsp;
been for a few other videos. Since then,&nbsp;&nbsp;

00:07:11.600 --> 00:07:17.360
commenters have been quick to point out that it’s&nbsp;
basically just Duolingo, but for code. And yeah,&nbsp;&nbsp;

00:07:17.360 --> 00:07:21.040
essentially, that is what you get:&nbsp;
structured and interactive lessons&nbsp;&nbsp;

00:07:21.040 --> 00:07:25.760
on whichever programming language you want to&nbsp;
learn. But unlike Duolingo, where a year-long&nbsp;&nbsp;

00:07:25.760 --> 00:07:30.320
streak means you’ve learned basically nothing,&nbsp;
Coddy teaches you how to write real programs&nbsp;&nbsp;

00:07:30.320 --> 00:07:35.440
by like lesson 10. I’ve actually been using&nbsp;
Coddy to learn the Rust programming language,&nbsp;&nbsp;

00:07:35.440 --> 00:07:39.920
and I’m now looking for a project in which to&nbsp;
apply that. They’re not paying me to use Coddy,&nbsp;&nbsp;

00:07:39.920 --> 00:07:44.800
and they’re not paying me to say these exact&nbsp;
words, they’re just genuinely that chill.&nbsp;&nbsp;

00:07:44.800 --> 00:07:48.160
So if you’ve been planning on getting into&nbsp;
programming or learning a new programming&nbsp;&nbsp;

00:07:48.160 --> 00:07:51.760
language, consider giving Coddy a shot&nbsp;
through the link in the description.

00:07:51.760 --> 00:07:55.680
Oh, and one more thing – Coddy is&nbsp;
doing a sale at the end of this month,&nbsp;&nbsp;

00:07:55.680 --> 00:07:58.800
but since this video is coming out&nbsp;
way before that, they’re straight&nbsp;&nbsp;

00:07:58.800 --> 00:08:02.080
up just giving y’all a discount code&nbsp;
so that you wouldn’t have to wait. So,&nbsp;&nbsp;

00:08:02.080 --> 00:08:07.680
to get 20% off Coddy Pro, use the coupon&nbsp;
code on screen or in the description.

00:08:07.680 --> 00:08:13.520
Coming back to that community post, the most&nbsp;
popular suggestion by far was “Run” – a series&nbsp;&nbsp;

00:08:13.520 --> 00:08:20.160
of 3D first-person platformers, with&nbsp;
one simple question: “how was it 3D?”

00:08:20.160 --> 00:08:25.040
I mean, if you think back to most Flash&nbsp;
games of the era, the vast majority used&nbsp;&nbsp;

00:08:25.040 --> 00:08:32.720
simple 2D graphics, or at best faked their 3D with&nbsp;
prerendered scenes. For the longest time, Flash&nbsp;&nbsp;

00:08:32.720 --> 00:08:38.240
didn’t natively support 3D rendering. It does now,&nbsp;
but that was only introduced in Flash 10, which&nbsp;&nbsp;

00:08:38.240 --> 00:08:45.040
came out on October 2008, but the very first Run&nbsp;
game was released a couple of months before that.

00:08:45.040 --> 00:08:49.840
To get to the bottom of this, I decompiled&nbsp;
that first Run game. While Run 3 is the&nbsp;&nbsp;

00:08:49.840 --> 00:08:54.800
community favorite, that one does take full&nbsp;
advantage of the built-in 3D. I also figured&nbsp;&nbsp;

00:08:54.800 --> 00:08:59.280
that starting with the less ambitious entry&nbsp;
would make it easier to reverse-engineer. But,&nbsp;&nbsp;

00:08:59.280 --> 00:09:04.080
regardless of which version I picked,&nbsp;
it was by no means going to be simple.

00:09:04.080 --> 00:09:09.760
You see, Run isn’t actually developed in&nbsp;
Flash ActionScript. It’s written in Haxe,&nbsp;&nbsp;

00:09:09.760 --> 00:09:14.880
a very different programming language capable&nbsp;
of compiling down to a lot of other platforms,&nbsp;&nbsp;

00:09:14.880 --> 00:09:18.720
Flash included. This is probably&nbsp;
an awesome tool for developers,&nbsp;&nbsp;

00:09:18.720 --> 00:09:22.080
but it makes decompilation a nightmare.

00:09:22.080 --> 00:09:26.880
Haxe outputs bytecode, avoiding the&nbsp;
need for verbose ActionScript. This&nbsp;&nbsp;

00:09:26.880 --> 00:09:31.040
means that in the decompilation, a lot&nbsp;
of variables are missing their names,&nbsp;&nbsp;

00:09:31.040 --> 00:09:35.440
and a lot of code ends up being mangled.&nbsp;
It’s a lot harder to understand how&nbsp;&nbsp;

00:09:35.440 --> 00:09:38.800
something works when you’re dealing&nbsp;
with nothing but cryptic symbols and&nbsp;&nbsp;

00:09:38.800 --> 00:09:44.080
numbers. If I had to guess, this is what&nbsp;
non-programmers think code looks like.

00:09:44.080 --> 00:09:48.560
Still, I was able to track down the class&nbsp;
responsible for drawing the tiles and fully&nbsp;&nbsp;

00:09:48.560 --> 00:09:52.640
cleaned it up, with comments and everything.&nbsp;
But before we get into how this game works,&nbsp;&nbsp;

00:09:52.640 --> 00:09:56.720
I want to give you a glimpse of&nbsp;
what that cleanup process is like.

00:09:56.720 --> 00:10:01.280
When I first opened this file, I skimmed it&nbsp;
to look for any resemblance of rendering code.&nbsp;&nbsp;

00:10:01.280 --> 00:10:05.840
There are only three functions here that retain&nbsp;
their names, that being “display”, “getColor”,&nbsp;&nbsp;

00:10:05.840 --> 00:10:10.320
and “getMultiplier”. Those first two sounded&nbsp;
promising, and that’s what got me to look at this&nbsp;&nbsp;

00:10:10.320 --> 00:10:16.160
class in the first place. So, with nothing more to&nbsp;
go off of, I started at the “getColor” function.

00:10:16.160 --> 00:10:20.800
Right away, the function takes in a&nbsp;
parameter of an unknown class. But,&nbsp;&nbsp;

00:10:20.800 --> 00:10:25.040
right below that, I see that this&nbsp;
parameter has a “z” property.&nbsp;&nbsp;

00:10:25.040 --> 00:10:29.520
That not only tells me that this is a&nbsp;
vector, but also that it’s a 3D vector,&nbsp;&nbsp;

00:10:29.520 --> 00:10:35.200
and that this function is using the Z axis for&nbsp;
depth. That’s exactly what I was looking for.

00:10:35.200 --> 00:10:38.320
Then there’s some arithmetic that I have&nbsp;
no chance of understanding just yet,&nbsp;&nbsp;

00:10:38.320 --> 00:10:41.920
but right after that there’s a&nbsp;
call to another unnamed function.

00:10:41.920 --> 00:10:46.000
Upon first glance, this might seem just as&nbsp;
indecipherable as everything else, especially with&nbsp;&nbsp;

00:10:46.000 --> 00:10:52.960
all of those random numerical constants. But- hold&nbsp;
on, I recognize those numbers. Those are powers of&nbsp;&nbsp;

00:10:52.960 --> 00:10:58.320
two! And, more specifically, they’re bitmasks&nbsp;
for the lower 3 bytes of a 32-bit integer. In&nbsp;&nbsp;

00:10:58.320 --> 00:11:03.920
the context of color, these masks are often&nbsp;
used to filter out individual color channels.

00:11:03.920 --> 00:11:08.640
With that little bit of insight, we now&nbsp;
have our first fully deobfuscated function,&nbsp;&nbsp;

00:11:08.640 --> 00:11:11.920
which I’ll call “scaleColor”.&nbsp;
It takes an RGB color value&nbsp;&nbsp;

00:11:11.920 --> 00:11:15.760
as an integer and scales each&nbsp;
channel by the given factor.

00:11:15.760 --> 00:11:19.200
From here, we just have to move&nbsp;
backwards. In the “getColor” function,&nbsp;&nbsp;

00:11:19.200 --> 00:11:23.440
we now know what this last call is, along&nbsp;
with reasonable guesses for the parameters&nbsp;&nbsp;

00:11:23.440 --> 00:11:27.840
going into it. And now that we know what&nbsp;
“getColor” does, we can continue stepping&nbsp;&nbsp;

00:11:27.840 --> 00:11:33.280
backwards to find out more from the code&nbsp;
that references it. It’s a lot like a puzzle.

00:11:33.280 --> 00:11:36.640
Anyway, that’s how I got my&nbsp;
deobfuscated copy of the code,&nbsp;&nbsp;

00:11:36.640 --> 00:11:42.000
and that’s why I can tell you that this pretty&nbsp;
much sums up all the magic. The 4 corners of&nbsp;&nbsp;

00:11:42.000 --> 00:11:48.080
each tile are projected onto the camera plane,&nbsp;
turning 3D coordinates into 2D points. And,&nbsp;&nbsp;

00:11:48.080 --> 00:11:53.040
after that, it’s really just a matter of&nbsp;
drawing a flat shape from those 2D points.

00:11:53.040 --> 00:11:56.160
That’s why the walls have this&nbsp;
blocky gradient going on – it’s&nbsp;&nbsp;

00:11:56.160 --> 00:12:00.320
not as much a stylistic choice as it is a&nbsp;
limitation of the fact that these are 2D&nbsp;&nbsp;

00:12:00.320 --> 00:12:05.440
shapes without any real 3D shading.&nbsp;
The player is also not a 3D object,&nbsp;&nbsp;

00:12:05.440 --> 00:12:10.560
but rather an animated 2D sprite. Though I&nbsp;
guess that one was probably more obvious.

00:12:10.560 --> 00:12:15.840
But, if it’s all just fake 3D drawn with&nbsp;
2D shapes, then how did they do the screen&nbsp;&nbsp;

00:12:15.840 --> 00:12:21.440
rotations and collisions? Well, while the&nbsp;
rendering code is faking the third dimension,&nbsp;&nbsp;

00:12:21.440 --> 00:12:25.680
the rest of the non-rendering code&nbsp;
doesn’t have to. As far as I can tell,&nbsp;&nbsp;

00:12:25.680 --> 00:12:30.800
most of the gameplay-focused stuff uses 3D&nbsp;
vector math and trigonometry extensively,&nbsp;&nbsp;

00:12:30.800 --> 00:12:35.680
so in that regard it’s not much different from&nbsp;
a game engine today. The math doesn’t care how&nbsp;&nbsp;

00:12:35.680 --> 00:12:42.560
many dimensions you use, so as long as you have&nbsp;
a way to render in 3D, the rest becomes trivial.

00:12:42.560 --> 00:12:47.840
Another game that was brought up in the comments&nbsp;
is “Interactive Buddy” – a weird kind of sandbox&nbsp;&nbsp;

00:12:47.840 --> 00:12:51.840
where you get to play God with this boxed-in&nbsp;
sphere creature. I’m not exactly sure what&nbsp;&nbsp;

00:12:51.840 --> 00:12:56.160
the ethics of this whole situation are, but&nbsp;
either way, that’s not we’re here to discuss.

00:12:56.160 --> 00:12:59.040
The commenter who brought up this&nbsp;
game mentioned that it features&nbsp;&nbsp;

00:12:59.040 --> 00:13:02.480
its own scripting language!&nbsp;
I have seen this game before,&nbsp;&nbsp;

00:13:02.480 --> 00:13:08.640
but I had no idea that you could write code in&nbsp;
it. So, naturally, I had to take a closer look.

00:13:08.640 --> 00:13:13.920
Once again, the entire game code&nbsp;
decompiles to a single 9200-line&nbsp;&nbsp;

00:13:13.920 --> 00:13:18.080
file. Though, because this is only a&nbsp;
reconstruction of ActionScript 2 code,&nbsp;&nbsp;

00:13:18.080 --> 00:13:21.280
there’s no way of knowing whether the&nbsp;
game was actually developed this way.

00:13:21.280 --> 00:13:25.920
True to the claims, one of the first things&nbsp;
we find is an initialization for the scripting&nbsp;&nbsp;

00:13:25.920 --> 00:13:29.520
engine. But, while digging through the&nbsp;
code does provide a rough reference for&nbsp;&nbsp;

00:13:29.520 --> 00:13:34.160
the language, the best way to get a&nbsp;
feel for it is to write some code.

00:13:34.160 --> 00:13:38.720
I wanted to find out whether this silly&nbsp;
feature is Turing-complete. If it is,&nbsp;&nbsp;

00:13:38.720 --> 00:13:41.920
that would mean that the engine&nbsp;
can perform any computation,&nbsp;&nbsp;

00:13:41.920 --> 00:13:48.880
given enough time and resources. So, to prove&nbsp;
this, I decided to write a Brainfuck interpreter.

00:13:48.880 --> 00:13:53.120
Now, I’ve already made a whole entire video&nbsp;
covering this cursed language, but here’s&nbsp;&nbsp;

00:13:53.120 --> 00:13:59.120
the gist of it. Brainfuck is an incredibly simple&nbsp;
language, consisting of just 8 valid characters.&nbsp;&nbsp;

00:13:59.120 --> 00:14:03.600
This simplicity also makes it difficult&nbsp;
to use, hence the name. But fundamentally,&nbsp;&nbsp;

00:14:03.600 --> 00:14:09.040
its design mimics that of a Turing machine,&nbsp;
which, by definition, is Turing-complete.

00:14:09.040 --> 00:14:14.960
In most programming languages, writing a Brainfuck&nbsp;
interpreter is often a trivial exercise. But in&nbsp;&nbsp;

00:14:14.960 --> 00:14:20.400
the case of ShockScript – yes, that’s actually&nbsp;
what it’s called – it took substantial effort.

00:14:20.400 --> 00:14:24.800
Most operations in ShockScript are kind&nbsp;
of like Excel functions – there’s a name,&nbsp;&nbsp;

00:14:24.800 --> 00:14:29.520
a pair of brackets, and a few comma-separated&nbsp;
arguments. Even an “if“ statement follows this&nbsp;&nbsp;

00:14:29.520 --> 00:14:33.840
same structure, which is pretty non-standard&nbsp;
for a programming language. There’s also no&nbsp;&nbsp;

00:14:33.840 --> 00:14:38.080
way to define your own functions, aside from&nbsp;
writing code into a string and evaluating&nbsp;&nbsp;

00:14:38.080 --> 00:14:43.360
that. And to top it all off, there are&nbsp;
several bugs in this scripting engine.

00:14:43.360 --> 00:14:49.040
For example, the order of operations is&nbsp;
completely messed up. In this “if” statement,&nbsp;&nbsp;

00:14:49.040 --> 00:14:53.600
the “and” operator applies not to the&nbsp;
pair of comparisons as you’d expect it to,&nbsp;&nbsp;

00:14:53.600 --> 00:14:59.520
but to the operands adjacent to it. Even if&nbsp;
you add brackets, it still doesn’t work. I&nbsp;&nbsp;

00:14:59.520 --> 00:15:05.120
found that the only way around this was to create&nbsp;
named variables with the value of each condition.

00:15:05.120 --> 00:15:10.240
But, in the end, I did manage to get a simple&nbsp;
“Hello, world” program to work from within the&nbsp;&nbsp;

00:15:10.240 --> 00:15:15.360
Brainfuck interpreter. So hurray, this&nbsp;
silly Flash game is Turing-complete.&nbsp;&nbsp;

00:15:15.360 --> 00:15:20.080
Given enough resources and patience, you&nbsp;
could eventually use it to run GTA 6.

00:15:20.080 --> 00:15:23.520
Now, that’s all pretty cool, but&nbsp;
while looking through the game’s code,&nbsp;&nbsp;

00:15:23.520 --> 00:15:27.840
I spotted something else – something&nbsp;
arguably more impressive than a hidden&nbsp;&nbsp;

00:15:27.840 --> 00:15:34.160
scripting engine. This game…&nbsp;
implements its own physics.

00:15:34.160 --> 00:15:39.120
In most Flash games, the physics are handled by a&nbsp;
ready-made physics library that you can just throw&nbsp;&nbsp;

00:15:39.120 --> 00:15:44.720
into your code, configure a bit, and then forget&nbsp;
about. The heavy-lifting is done by someone else.

00:15:44.720 --> 00:15:48.720
What you’re currently looking at is Home&nbsp;
Sheep Home. I used to speedrun this game,&nbsp;&nbsp;

00:15:48.720 --> 00:15:53.120
and in my prime I dissected the code through&nbsp;
and through to hunt for glitches and movement&nbsp;&nbsp;

00:15:53.120 --> 00:15:57.680
tricks. And that’s when I learned that many&nbsp;
such Flash games use a robust physics engine&nbsp;&nbsp;

00:15:57.680 --> 00:16:01.920
called Box2D. For my glitch-hunting,&nbsp;
that meant that it’s unlikely I’d&nbsp;&nbsp;

00:16:01.920 --> 00:16:06.640
find many physics-related bugs, given that&nbsp;
engines like these are often fairly stable.

00:16:06.640 --> 00:16:12.160
But coming back to Interactive Buddy, this&nbsp;
doesn’t use Box2D. In fact, this doesn’t use&nbsp;&nbsp;

00:16:12.160 --> 00:16:18.080
any general purpose physics engine. It’s all&nbsp;
in-house code, written specifically for this&nbsp;&nbsp;

00:16:18.080 --> 00:16:23.360
game, which certainly does some work in&nbsp;
explaining the 9000 line code footprint.&nbsp;&nbsp;

00:16:23.360 --> 00:16:27.920
If there’s any takeaway to be had from&nbsp;
digging through the code, it’s that this game,&nbsp;&nbsp;

00:16:27.920 --> 00:16:32.480
and frankly all of the games we’ve&nbsp;
talked about, are a work of passion.

00:16:32.480 --> 00:16:39.040
Thanks again to Coddy for sponsoring&nbsp;
this video, and thank you for watching.

