New site

I spent today working on a new site to host information about Vanguard, and I think it came out pretty well. You can check it out here. I’ve almost finished the language standard, and when I finish the compiler I will rewrite the site from scratch in Vanguard.

EDIT: Oh, I came up with a punny logo-type (that pun also intended) thing for the language: “[☺]“. It’s a reference to the structure of expressions; maybe somebody will get it.

The Vanguard Project

I’ve been working with web development for a few years, and have picked up on quite a few inconsistencies and complications with the tools available out there. Lots of people have tried fixing the problems, but it seems in the end they all wind up building on top of a broken foundation, and it comes crumbling down; everyone leaves the “fix” and either returns to the classic methods of web dev or seeks a new remedy for their problems. You’d think, in this case, that the best thing to do would be to start all over and build up a more robust foundation, but with the massive adoption these common “broken” technologies, convincing all the developers and designers out there to adopt a new paradigm and trash the decades of accomplishments we’ve already achieved is nigh impossible. Heck, I’ve seen people try to build new languages and browsers designed specifically for them, but even those efforts have remained fruitless. It would seem we’re stuck with the traditional combination of HTML and CSS for the rest of eternity, gradually revising features until we get something that’s still only marginally better.

That’s why I made Vanguard. I had the idea to create a sort of website-compiler, with 3-4 languages designed for templates, styling, content markup, and configuration, but after I got started with the first language — the one originally going to be used for content markup — I realized I had something special here. I expanded upon the idea and stuck to the idea of flexibility, extensibility, and robustness, and ended up with a very minimal yet practical language, which I named Vanguard. It takes up way less space for markup and styling than HTML and CSS yet provides a ton of functionality that neither language could ever dream of possessing, like property inheritance heirarchies and no explicit end tags (yay! not really functionality, but still a grand feature). I am currently still in the process of defining the language — which is going on over here, by the way — and will soon move on to writing a compiler for the language; if you want to contribute anything, I’m usually on IRC at irc.awfulnet.org in the #vanguard channel as cdh473.

Another goal of mine was to devise a compact language which could easily be interpreted directly by a browser — in case a hobbyist programmer or something came along and wanted to help out a bit — without any historical artifacts carried over from the previous technologies. Of course, the basic application of the language will be quite familiar to those who are already accustomed to HTML and CSS, but the clean-up work that’s been taken care of as well as the new paradigms available in the language, in my opinion, vastly improve the experience.

I chose a syntax similar to s-expressions for multiple reasons. The first was that, in my experience with other languages based on them (such as Lisp and its variants), they provide unrivaled flexibility; code can quite easily be dealt with by other code, and it allows some really neat tricks. With Vanguard, type declarations allowed me to combine multiple [partial] expressions and apply them as new types, which allows some tidy markup choices; it also helps to further separate content from the layout: the document writer can place his/her declarations in a separate file and use them inside the actual document, changing them externally as necessary. The second, more minor (yet still nice) reason was that it, paired with whitespace syntax, totally removed the necessity for ‘end tags’ as in HTML. Now, if you want to create an inline expression, you just enclose the content with the ‘]’, so you really end up typing a lot less than you would in HTML.

Whitespace syntax can be found in languages like Python, where it is essentially required; other languages, such as C and Java, do not require it, yet they still encourage the use of proper whitespace formatting. Vanguard does not force one to use its whitespace syntax, but rather rewards those who make use of it. If I, for instance, write with explicit expression ends (‘]’), I end up with harder-to-read markup (that is still legible, mind you), but no real advantage. If I were to make extensive use of whitespace syntax and properly format my document for it, I get very neat, clean markup that is easy to work with, as it does not have bizarre, unnatural-looking expression ends. This should encourage people to use the special syntax as much as possible, while evading the issue of complaints such as those of Python’s forced whitespace syntax, I mean, whitespace syntax isn’t even in the language after expansion! This brings me to my next point:

Vanguard’s core.
In my efforts to devise a minimal language, I faced the issue of figuring out how to add all these cool, extra features, as well as how to add later features atop such a tiny base language. I decided I would set aside a subset of the bare minimum functionality, define a solid standard for it, never to change it (drastically), and construct everything else in what became the expander. I also separated the types from the core and put them into a sort of class with the expander, being in their own type library and whatnot. I’m certain that this separation would allow use of Vanguard in other areas as well, such as domain-specific content markup and something akin to what XML does.

After all of the work put into Vanguard, I am very confident in its future and will continue to evolve it as a language, putting it to the test in my own projects and (hopefully) watching others use it for theirs. If you have any suggestions or questions, leave me a comment below.

OpenGL Rewrite

Since I’m rewriting the graphics routines, I figured I’d spend some time cleaning up the horrid mess of spaghetti code I had accumulated throughout all these tests; the game is going to be much faster, more efficient, and the code will be easier to read. All of the graphics calls are done in OpenGL now, so I pretty much scrapped all Allegro drawing functionality. This will allow me to get some special optimizations I wouldn’t otherwise have been able to achieve.

The rewrite is coming along quite nicely. After this is all done, I can finally get down to working on building the ragdoll prototypes and implementing inverse kinematics!

Upcoming Lighting Engine

Now that the object primitives and day/night cycles are done, with working colour transitions based on time, I am going to take a little while to rewrite the graphics routines and build a lighting engine in OpenGL. Once this is done, I will upload screenshots of progress from here on out.

I spent some time today building an interactive mode for the game so I could more easily test physics; I’ve reached a point where I think everything’s been tuned properly and so I’m ready to move on.

Day / Night Cycle and Primitives

The sun now moves in an elliptical path throughout the day, and I’m working on a decent sprite for the moon. Later I plan on setting colours for the sky and having different radiosities based on the positioning of the sun and moon. Stars are another planned feature during the night.

Currently I’m working on building all the major primitives used for constructing objects, so I can get to more advanced functionality. I’d like to have certain materials be breakable, such as wooden crates, but it may be too computationally expensive. After I get a few more primitives built I’ll start working on the space shuttle again.

Physics

Last night I finished integrating Chipmunk and had some fun throwing around objects and making rockets with thrusters and whatnot; however, there still remains the problem of having a finite ground plane (it needs to extend infinitely). Tonight I’ll try to build a crude 1-3 stage space shuttle and see how well camera following works (I’ll upload screenshots whenever I finish).

(Re)Introducing: Spacetart!

I decided I’d revive an old project of mine: a 2D physics-based sandbox based around the concept of playing as a stick person, with all movement and control coming directly from muscular impulses and AI assistance. I’m coding it for GNU Linux first, but once it becomes playable, I’ll port it to Windows and likely Mac OS X and iOS (yes, this means you will be able to play it on your iDevice). Spacetart is written in C, using the cross-platform game library Allegro 5 for rendering and user input, as well as Chipmunk for the physics. On the iOS version I will probably switch it over from Allegro to Cocos2D. I’ll keep this blog updated as development progresses.

I also plan to implement a Lua in-game scripting API that allows the player to create his/her own mechanisms and doo-dads.

Python: Grouping Functions

If you’ve ever used Python much (or just about any other high-level language), you’ve probably written functions. They are extremely useful in saving keystrokes, reducing bugs, and writing reusable code. You’d probably also know that it’s sometimes necessary to group functions together to stay organized and write clean code. There are many ways to do this, the most common being making use of objects which contain these functions; however, this may not be the best choice for every scenario.

What is the simplest method for grouping together functions?

The simplest way to categorize your functions is to prepend a common name to similar functions:

def input_getUsername():
    #do stuff

def input_getPassword():
    #do stuff

def output_showAccountInfo():
    #do stuff

def meta_logOut():
    #do stuff

This will provide a sense of organization for you and other programmers, but it is only cosmetic; it doesn’t actually make a difference in the operation of the program. If you’d like to physically group your functions together, you’ll probably end up writing a class to contain these functions:

class UserInput:
    def getUsername(self):
        #do stuff
    def getPassword(self):
        #do stuff

class Output:
    def showAccountInfo(self):
        #do stuff

class Meta:
    def logOut(self):
        #do stuff

userInput = UserInput()
output = Output()
meta = Meta()

But that’s ugly! Can I do this without creating instances of the classes?

A neat way to achieve this without creating objects is to convert these functions into class methods by using the @classmethod decorator. Note that class methods need a cls argument, whereas object methods require self.

class UserInput:
    @classmethod
    def getUsername(cls):
        #do stuff
    @classmethod
    def getPassword(cls):
        #do stuff

class Output:
    @classmethod
    def showAccountInfo(cls):
        #do stuff

class Meta:
    @classmethod
    def logOut(cls):
        #do stuff

Now you can call the methods directly from the class as though it were an object; however, you cannot store values in the class as you would otherwise be able to.

What if I want to do a function look-up using a string?

If you have a string literal and want to use it to call a function, i.e. call function foo() with the string "foo", you can create a class of these functions (not class methods) and convert it into a dictionary:

class UserInput:
    def getUsername(self):
        #do stuff
    def getPassword(self):
        #do stuff

class Output:
    def showAccountInfo(self):
        #do stuff

class Meta:
    def logOut(self):
        #do stuff

userInput = UserInput.__dict__
output = Output.__dict__
meta = Meta.__dict__

Now, if you want to call Output.showAccountInfo(), simply use output["showAccountInfo"]().

Hopefully you now know how to organize your functions neatly and even store them in dictionaries for quick look-up, this is a widely-applicable bit of knowledge and I’m sure you’ll be able to find a use for it somewhere.

Modular Arithmetic: The Power of The Modulo Operator

What Is The Modulo Operator?

The modulo operator is essentially used to obtain the remainder of a division operation. Let’s say I evaluate the equation 10/8.  This will have a remainder of 2 because 8 can only be divided into 10 once, and 2 is left over. Let’s say I change the equation to 10/4. This will have the same result as the previous equation, because 4 goes into 10 twice and leaves 2 as the remainder. Modulo provides an alternative to writing a decrementing loop to find the remainder such as this:

int remainder(int dividend, int divisor) {
    int rem;
    for (int tmp = dividend; tmp >= divisor;) {
        tmp -= divisor;
        rem = tmp;
    }
    return rem;
}

Instead, all you have to do is this (you wouldn’t actually write a function since the modulo operator already does just this):

int remainder(int dividend, int divisor) {
    return dividend % divisor;
}

How Can I Use The Modulo Operator In My Favorite Language?

Many languages have built-in support for the modulo operator. If it is not included as an operator, it is often a standard function. Following are examples of using modulo in many different languages.

C/C++, Java, Python, Lua, PHP, Ruby, etc. (most common)

10 % 4

Pascal

10 mod 4

Haskell

10 `mod` 4

Ruby (alternative example)

10.modulo(4)

These examples should all result in the same value.

What Are Some Practical Uses of The Modulo Operator?

Checking if a number is even or odd:

You can use the modulo operator on any integer value, even negatives, to determine whether it is even or odd. Simply mod it by 2.

string getOddEven(int n) {
    if (n % 2 == 0) { //If `n` is even
        return "even";
    } else { //If `n` is odd
        return "odd";
    };
}

As you can probably see from the above example, an even number will evaluate to 0. If a number is odd, it will evaluate to 1.

Finding the nearest multiple of a given number:

For a given number n you can find the nearest multiple m of any number b using the modulo operator. You can even specify whether you want to round up or down without much effort at all. Here’s the math behind it:

Nearest Multiple Equation

The C equivalent:

int nearestMultiple(int n, int b) {
    if (n % b <= b / 2) {
        return (n - (n % b)); //Round down
    } else {
        return (n + (b - (n % b))); //Round up
    };
}

Multiples of ten:

Multiples of ten are useful because ten is the base number for the decimal number system. You can use modulo to split numbers up digit by digit. If you want to write a function that allows you to return any digit from a number, here’s an example (it goes from left to right starting at 0):

#include <math.h>

int getDigit(int digitIndex, int number) {
    int digit;
    digitIndex = pow(10, digitIndex);
    digit = number - (number % digitIndex); //Remove digits to the right
    digit = digit  % (digitIndex * 10); //Remove digits to the left
    digit = digit / digitIndex; //Remove trailing zeros
    return digit;
}

Creating A Clock:

The classic example of modular arithmetic is in a simple 12-hour clock. When the minute hand hits 60 minutes, it wraps around and starts over again. To emulate this, just mod a minute value by 60 at every tick. The hour hand wraps at 12, so do the same, just mod the hour value by 12 at every tick. The same applies for days of the week, month, and year. You could create a calendar/clock system using this basic knowledge.

Drawing Tiled Graphics:

If you want to draw tiled graphics in, say, a side-scrolling 2d platform game, you can use the modulo operator to find the offset necessary to draw the origin of a tiling background texture. Just mod the x and y values of the current display on the game’s coordinate system by the respective dimensions of the background texture image and draw however many copies of that image is necessary to cover the entirety of the screen. Voila! You have seamless tiling background textures!

What Else Can I Do With The Modulo Operator?

The modulo operator can be used in many different ways not shown here, like checking for primes and sometimes it’s even used in cryptography! When looking for ways to optimize your algorithms, look for places to use the modulo operator, as it can drastically reduce calculation time in many cases.

Python: Daemon-less Server/Client Data Transfer

If you’ve ever tried to set up a server/client connection on a web server with a shared hosting plan, you may have run into the same problem I did this last week. I was writing a server-side Python script that would set up a TCP connection with the client, receive commands, then write/read to/from a MySQL database and send back data to the client. Shortly after having finished the two scripts, I took a look at the ToS of the web host and noticed it said that daemons were not allowed.

Immediately I scrambled around for an alternative and found urllib. Basically what this allows you to do is easily send URL requests and transfer data with the server via POST and GET. For a simple example of what this can do (if it’s still not clear), take a look at these two scripts:

client.py

#!/usr/bin/env python
#client.py
import urllib
print "Enter a string to be hashed.\n"
while 1:
    #Gets input
    toHash = raw_input("String ['q' or 'Q' to exit]: ")
    #If user enters 'q' or 'Q', exits the script
    if (toHash.lower()=='q'): break
    #Prepares POST data
    params = urllib.urlencode({'toHash':toHash})
    #Opens the URL with POST data
    f = urllib.urlopen("http://cdh473.com/test/md5hash.php", params)
    #Prints the hashed string to the console
    print "MD5 Checksum: "+f.read()+'\n'

md5hash.php
<?php
//md5hash.php

//Sends the MD5 hash of the POSTed string back to the python script
echo md5($_POST['toHash']);

?>

client.py receives user input and stores in the string toHash. If toHash equals 'q' or 'Q', then the script breaks of the while loop and exits, otherwise it carries on with the loop. Next, it stores toHash in a dictionary named params(you have to use urllib.urlencode() on the dictionary when creating POST data). The script then queries md5hash.php, which is located on the web server, sending it the POST data located in params. After receiving the data that md5hash.php returns, client.py prints the MD5 hash to the console.

md5hash.php receives POST data from client.py, then converts it to an MD5 hash and echoing the result back to client.py.

You can also perform the same task with GET data by simply changing line 13 of client.py to f = urllib.urlopen("http://cdh473.com/test/md5hash.php?%s" % params) and line 5 of md5hash.php to echo md5($_GET['toHash']);.

Here’s an example of using GET requests on a search engine:

getyahoo.py

#!/usr/bin/env python
import urllib

print 'Yahoo Search Result Grabber'
while 1:
    query = raw_input('Search [\'q\' or \'Q\' to exit]: ')
    if (query.lower() == 'q'): break
    params = urllib.urlencode({'q':query})
    f = urllib.urlopen("http://search.yahoo.com/search?%s" % params)
    print f.read()

What this script does is query Yahoo Search using a string that the user entered, returning the first page of the search results as it would in a web browser.