Saturday, July 16, 2011

AS3 embedded language virtual machine and compiler beta release

Download my simple, domain specific still-in-beta embedded-in-ActionScript scripting language I am writing for my game! Includes compiler and virtual machine.

I have found that recently some people have been coming to my blog in search of a scripting language that one can embed in ActionScript. I have written a few blog entries about my look into this and my accomplishments but they have been lacking in code and have been nothing more than updates to no one about my own progress. I haven't been updating as often as I'd like and I would really like to show off the actual virtual machine I've created along with the compiler so I have decided since people have been looking for it, I'll bite the bullet and put it up. The scripting language I have written is called CandyScript. It is extremely simple so far and not super powerful, but it does what I need it to do for now. It is also still in "beta" because I am actively working on it; this release isn't really a library, it's just a simple code base people interested can learn from.

The reason why I haven't yet posted anything is because I am also a newbie at creating domain specific languages. I originally thought that I would hold off until I had an actual library but to be honest, library bloat is always annoying and a lot of people don't want some blogger's "library" but just some code they can look at/use/play around with. My scripting language is a secondary project to my game; I am not going to generalize it for everyone's needs and maintain it as such so that it is a true library so why pretend that I will? Instead, I think it is a fairly straight-forward virtual machine and compiler grammar set that I wish I could have looked at when I was figuring out the stuff and that I hope is useful for people who want to see a simple machine in action. It isn't file after file after linked library; if you have an initial grounding in the topic you should be able to step through my implementation in a debugger and read through the code and get an idea of what is happening. Even before then, you can play around and see what I did just by writing scripts and sending them through my VM. There are only two "meaty" classes: an interpreter which loads a compiled script into memory and a virtual machine which executes the script. Seven tiny extra classes provide support to these two main classes (a few are just dummy classes written to represent types such as null). Likewise, the ANTLR grammar is split into two files; one which parses the C-subset scripting language I wrote into an abstract syntax tree and one which turns the tree into a compiled XML file.

I would just like to make one thing clear; I hope I can help people understand better about virtual machines by using and looking at my code. But I am also pretty much a newbie to this field as well. There may be naive mistakes and implementations littering my code; I do not know. All I know is it seems to work for me. It is in beta, however, and I am designing it to my needs, not generalized needs. I can't vouch for it as though I am an expert level DSL writer who knows all the tricks of writing interpreters and compilers; to an expert this may (or may not) look laughably amateurish. All I know, once again, is that it works and seems to work pretty fast for a scripting language literally built on top of another scripting language :) I hope people can learn from this and that someone doesn't come by and telling me I am leading everyone terribly astray; even if I am, my blog certainly doesn't generate enough traffic for me to find that out :)

Friday, July 1, 2011

Adult Swim scheduling bug

As I have mentioned before, I run the single serving site, Tonight, the first of July, as I went to check if Family Guy was indeed playing, I noticed the site was displaying my catchall bug informing the user something went wrong. Nervous that Adult Swim had retooled their scheduling or caught on and blocked me from leeching their scheduling data, I began to investigate.

As it turns out, it was a bug I had never caught in which the first day of the month (which begins at 12 midnight) was looking backwards for the previous day's schedule (which is what late night programming is technically classified under). So, being the first of July, the late night Adult Swim programming is still technically part June 30th's programming block. However, my naive code was simply subtracting 1 from the date so that it was looking for day 0 and not day 30 (the last day of June).

Adult Swim stores schedules in XML files located at URLs such as This link may appear to lead to a blank page but if you look at the source, it is an XML file containing show data. The naming format is fairly simple: the "1" is the date of the current month so that if it is July, then 1 represents July 1st. The "EST" means this is the schedule for Eastern Standard Time (they have all the American time zones). So basically, on the first of the month, my website knew it was first but technically the scheduling for all late night programming on the first is really part of yesterday. Unfortunately, my error resulted in looking for a file named "" instead of "".

Before, I said the format "1.EST.xml" meant the first of the current month for Eastern time zones in the US? Well one catch, apparently if it is the first, then "30.EST.xml" or "31.EST.xml" or any other "last day of month" schedule points to last month's 30th or 31st or what have you. Luckily, in PHP, it is easy to work with dates and times and calling date("t", mktime(0, 0, 0, date("n") - 1, 1, date("y"))); returns the last day of the last month. So, in August this will return 31 (31 days in July) but right now (in July) it returns 30 (30 days in June).

What is interesting is that I went to confirm that the official Adult Swim schedule was working and that the "30.EST.xml" and the like all actually pointed to June 30th's programming and not July's. When loading the page I was greeted with this:

It looks like Adult Swim has a similar issue; the schedule does not load and inspecting the page reveals it is trying to load "undefined.EST.xml". I haven't looked at this page in a while, but I assume it's a similar error of judgement when programming in terms of "it's 2 AM, we need to actually pull the programming from yesterday so let's do some subtraction and forget about how the day before the first isn't the 0th".

Sunday, June 5, 2011

ANTLR translator for embedded scripting language

Update 7/16/2011 I have posted code for this.

Last month, I wrote a post detailing the building of a scripting language interpreter inside of ActionScript. The interpreter worked from abstract syntax trees represented in XML, however the idea of programming in this environment is not too appealing. Therefore, I knew I had to build a translator to take a C-like syntax and translate it into my XML abstract syntax tree format. I chose to use ANTLR as my parser generator and used the related StringTemplate template engine to bridge the gap between parsed AST and XML representation. The Definitive ANTLR Reference by Terrence Parr (the creator of ANTLR) was a lot of help when it came to learning about how to use ANTLR and about non-trivial parsing in general.

So far my language consists of functions that may be called by various aspects of the game engine and that may call each other. Various types are supported, type casting is implementing, scoping is implemented, function returns are implemented and while/for/if statements are all supported. I am probably going to implement array support as well. Now that I can write complicated scripting procedures, a lot more opportunities will open for game design when I begin that phase but the code will remain penetrable since it is compiled from a readable C-subset syntax.

Monday, May 23, 2011

Media company One Line Creative's website launch

I recently had the pleasure of implementing a website for Thomas Byrd and One Line Creative which has recently launched. One Line Creative is a media production company and you can check out their videos on the website and also the One Line Creative blog.

Monday, May 16, 2011

Embedded scripting language in ActionScript

Update 7/16/2011 I have posted code for this.

I have recently been working on various projects both for myself and others but one thing which I have been focused on is creating a scripting language for the game I am working on. A lot of my entries so far have been about spin-off projects relating to the work I am doing for my ActionScript game and this is no different.

I knew that I wanted to create a scripting language that resembled C in it's structure. Ultimately, the idea was to have a function based language, however, where functions are written out in a style similar to C but there are special calls that can be made throughout the script and functions are either called by triggers on the map (handled by the game engine) or specially, such as scripts to run automatically at such points as level start, level end, et cetera.

The actual format of the scripting language is being handled separately. I have an ANTLR grammar that I am working on to parse the actual code and compile it and I will maybe post about that in the future, but this post documents handling and interpreting the intermediate representation which is what my compiler will eventually output from the C-like input.

Friday, April 22, 2011

Build ZDoom from source in Xcode 4

Update 5/8/2011 I have received word that this tutorial works with Xcode 3.2.4 as well as Xcode 4. Thanks to andreb. I'd like to thank him for pointing out I could use the $SRCROOT variable instead of /absolute/path/to/workspace/ZDoom-Xcode.

When I was younger, I used to make maps and other mods for the popular old school game Doom. The community of mappers for the game was already quite established by the time I got into it, due to Doom being one of the original games to develop a large modding community. Even after it's heyday (and still to this day) the community thrives due to two things: the game is classically enjoyable and fun to mod for and the existence of source ports has kept the game relatively up to date. Almost no one runs vanilla Doom these days, due to the slow but steady deprecation of DOS support on many systems but plenty of programmers have kept the game alive by creating modified engines (or source ports) which run on modern operating systems without a hitch and add features to the game to make it more interesting while allowing it to keep it's charm and simplicity. This is thanks to John Carmack and id Software's open sourcing of the Doom engine back in 1997. ZDoom is one of the more popular updated engines and has been around for a long time.

A few years ago, I switched from PC to Mac and although I am happy with this change the one thing I left behind was gaming. Although I don't care too much these days if the latest game is being ported to OS X or not, I did miss some of the old games I used to play. There are so many free maps and add ons from the Doom community that just buying Doom, downloading a source port and getting some mods is enough to keep one entertained indefinitely. It will also only set you back about ten dollars to purchase Doom, making it extremely cheap gaming.

There are plenty of ports for Doom which release OS X binaries and these are all great ports, however ZDoom has such a large number of maps and features that you miss out on a lot of good mods if you aren't running it. I had never gotten around to running it on OS X before because they didn't have any binaries, but I finally decided to dive in and build it myself. Luckily, they support OS X by using CMake, but unluckily, nothing works perfectly and this is what had halted me from getting too deep before. As great as running CMake and then make would be, the build process doesn't work quite that easily. Before I continue however, I discovered that the DRD Team already hosted unofficial OS X builds of ZDoom so if you would like to spare yourself a headache and just download those, you can run ZDoom on OS X without building anything.

Wednesday, April 20, 2011

Space Opera, an x86 assembly arcade game

Screenshot of Space Opera, in action

Back in 2008, as a freshman in my second semester of college, I wrote a game in x86 assembly language as my final project for a core computer science class I was taking. The class was Computer Systems Organization, taught by Professor Nathan Hull and I think it was one of the more important classes that I took. This class came fairly early in the track (once you were out of the introductory Java classes) and it was one of the classes that began to explore the field of programming and computer science on a lower level than contrived OOP simulations of unrealistic problems.

A lot of people do not like learning assembly and it certainly is directly inapplicable in most fields these days, however I am one of the people who believes it is very important to at least have some experience in it because you really start to understand what is happening behind the scenes in any language once you have worked in it. To have such a low level knowledge of what is happening is useful and interesting, and I credit this class with helping to draw a clear line between my high school self and post-high school self in terms of understanding paradigms beyond introduction-to-Java type programming. Not to say that all the work I did in high school was that simplistic or bad, but that the mentality I had was rooted in easy to digest examples of OOP paradigms from introductory books and this starts to become limiting, at a point.

Thursday, April 14, 2011

Vintage 1970s haiku generator by my granddad

Ross Ainsworth Dierdorff, 1922-2011

Back in January, I received a bundle of my grandfather's papers, following his passing, from my uncle. The papers detailed a simple algorithm my grandfather written to randomly generate haikus. My uncle, who also works in the field of math and computer science, had wanted me to take the notes, the BASIC implementation and the hand written word lists and see if I could recreate the program.

In this day and age, writing a haiku generator would not be much of a challenge. It would likely be an extra credit assignment in an Introductory 101 Java class. I felt that porting from BASIC to a modern day language would be a bit backwards, when I could just write a haiku generator in a modern language using modern paradigms. However, if I were to do that, then all I would have is a haiku generator with no relation to the one I was trying to memorialize. The point would be missed, I realized, the more that I thought about it.

Then I realized that the point isn't to recreate a haiku generator, which is something today's technically inclined youth could teach themselves to do thanks to the abundance of freely available programming environments and introductions to programming and mathematics. The point is to put the original haiku generator on display, because what makes it better than a cheesy Javascript implementation* is that back in the 1970s, people like my granddad were programming computers to do things that were cool. Something like a computer program that generated haikus in the 1970s was not abstract mathematical nonsense. It was something real. You could show it off to your friends, enjoy the motivating feeling of tangible success and most importantly, have created something cool that breaks out of the occasionally dry realm of mathematics and science.

Wednesday, April 13, 2011

ActionScript foray into fast Fourier transforms

I am aware that this image matches my blog :) Preview my FFT implementation

Sometime late December/early January of this year, I realized that I would need to learn how to implement a fast Fourier transform in ActionScript and, quite honestly, in general. I was working on my game and as I said in a previous post, my game is partially reliant on a powerful audio engine I am working on.

Some people reading this may be aware that Adobe already has an FFT implementation. In their SoundMixer class, there is a computeSpectrum() function and if you choose true for the FFTMode boolean parameter, the spectrum returned will be a Fourier transform of the current frame of audio. However, this function is only available on the SoundMixer which is Adobe's static global sound control class and so the transform represents the transform of all sound going through the Flash application and not just, say, a single music track. This isn't a problem if all you are playing is the track you want to transform but this isn't always going to be the case. You may want to perform the transform on a music track, while sound effects playing in the background, for example.

I ended up writing an FFT implementation in ActionScript. However, it wasn't super fast and I shelved it for a few months. Recently, I decided to optimize it, so I spent the last few days cleaning it up and making a faster implementation. Although it is not as fast as perfect implementations written in C, I have managed to make it run fairly quickly, however it is optimized for real data such as audio (as opposed to data consisting of complex numbers).

Sunday, April 10, 2011

Gravity tables for side scroller physics

As I mentioned earlier, I am working on a game in ActionScript (in other words, a Flash game but one that is programmed as opposed to developed primarily on the timeline). It is a two dimensional side scrolling game and as expected, one of the main movements of the player (besides going right and left) is jumping (and by extension, falling).

In order to implement jumping and falling, not just for the player but for anything, I needed to build a rudimentary physics engine to handle gravity. Now, since this is just a two dimensional arcade style game the physics do not have to move beyond a high school level but they need to be implemented well.

Single serving website: Is Family Guy Currently Playing On Adult Swim?

Last summer, an idea came to me. I had been staying up late and watching Adult Swim on Cartoon Network and although I enjoy a lot of their programming, it is sometimes hit or miss for me. For starters, I really dislike anime a lot so whenever I turned on Adult Swim and saw anime it would be like a punch to the gut of disappointment. Although I enjoy Aqua Teen Hunger Force and The Venture Brothers, the show that is consistently funny for late night mindless consumption is Family Guy. So whenever Family Guy was on, I didn't have to worry about it being something that I wasn't that into playing on Adult Swim.

However, turning on the television to find out if Family Guy was on was not good enough, I soon realized. The let down from finding out something stupid was playing, quickly following the anticipatory turning on of the television, was too much for me. So I came up with a solution. I would create a website that would quickly tell me if Family Guy was currently playing on Adult Swim. Clearly, this was the easiest and least convoluted solution. And so was born.

Saturday, April 9, 2011

Make your music pump like Parisian house

The Prydzifier* (or Daft Punkifier or Parisian House-ifier)

First of all, welcome to my blog, to whoever may be reading it. This is my first post, as opposed to an introductory post, however in order to properly introduce my blog, I will say a few words about how it came to be. I am Samuel, a computer scientist who majored at NYU and this blog is designed to share my small projects and code that I am working on. It is pretty much a run of the mill software developer blog in which I ramble about things I have worked on and maybe someone will find it interesting or helpful.

That being out of the way, I recently have been working in ActionScript and Flash. I know that the initial reaction of many developers is one of distaste when hearing about Flash development, and I was one of those people fairly recently, myself. However after years and years of putting it off, I took a Flash Programming class my senior year because I knew that I should at least know how it works for my resumé, even if I didn't think I would ever use it.

As it turned out, I really enjoyed it and found it more powerful than I had expected. It still has many of the shortcomings you probably could rattle off to me in a heartbeat but it also is much cleaner these days than what I always imagined it to be like. Plus, the work I do in it is easily accessible inside the browser so that I can show off things I create somewhat easier. You also you have to have good development practices and knowing your application is going to be running inside Flash inside a browser means you really have to make sure that everything is coded tightly.

This post is specifically about a spin off from a larger project (a game actually) that I am working on. My game has an entire audio engine that is being worked on as necessary for the game, and one of the things I needed was the ability to plug in various effects, especially a dynamic range compressor. I hope to talk about the game more in future posts. After creating the compressor for the game, I ported it over to a standalone Flash application so that it could be played with on its own.