Tuesday, September 29, 2009

Source Pawn: A journey of gaming discovery

OK, so it's not technically C++ but it's written in and based off C++ and I haven't done anything actually related to C++ yet this week so I'm going to talk about something that combines the schools love of open source and my love of the Source engine.

At the request of one of my Internet friends I have recently downloaded the tools for "Source Pawn" it's a psuedo interpreted, objectless, terribly packaged (It downloads as a.gz (an open source version of zip) that extracts into a .TAR (an open source version of RAR)that extracts into a proper folder) language designed for writing mods and plug ins for the Source gaming engine. It apparently shares attributes with PHP, C++, javascript and a big mess.

That's not to say it's a bad thing. Quite the opposite. While some parts of it seem very messy, this is probably the first time that a language has been written by fans specifically for the purpose of modding a single set of games. As a feat it's quite remarkable and a stellar example of what an open source fan community can do when united by a common love and goal.

No games outside of the source engine's has ever had as numerous a collections of plugins, mods, open source coders as the source games and Source Pawn is the main reason. The recent DOS attacks many servers were facing was fixed in it, and all for free, because (and this is the part you guys will like) in order to keep it all community generated and free the creators have a strict policy that anything created in Source Pawn MUST have it's source code posted online for all to see. There are of course developers who create their own extensions to write tools in allowing them to make a nice profit off their work but for the most part, when Team Fortress 2, Counter Strike Source or Half Life 2 get a new mod it's available for the public, free of charge and with its source code laid bare so that everyone can bug hunt it and improve it for the rest of the community.

So I look forward to writing and testing my first plugins for this strange love child of gamer geekdom and open source philosophy. And when I have nothing C++ based to say in the future I'll fill you all in on how it goes.

Monday, September 21, 2009

OOP344 Challenge 2

Our class was given the challenge "Modify io_display function to the shortest code possible."
After some brainstorming I was able to get each part of the function down to 1 line. The original code was:

void io_display(const char *str, int row, int col, int len){
io_move(row, col);
if(len <= 0){
io_putstr(str);
}
else{
int i;
for(i=0;i<len && str[i];i++){
io_putch(str[i]);
}
for(;i<len;i++){
io_putch(' ');
}
}
}

I moved the variable to the top since I was taking out the if else structure and left the io_move where it was since it was already only one line.
Then I turned the check for a len of 0 or less into a for loop that only executed if the length was less than 1 and had the io_putstr as an increment condition. This saved me 4 lines.
Then I basically just did the same thing for the other two for loops, making the output command part of the increment and closing the for with a semicolon instead of a set of braces. The final code is only 7 lines long compared to the original 15.
Were this a c++ program I could have made the int declaration part of the for loop bringing the line total down to 6 but since it's supposed to work as a standard c program that isn't an option.

Here is the final function.

void io_display(const char *str, int row, int col, int len){
int i;
io_move(row, col);
for(i=len; i <= 0;io_putstr(str),i=1);
for(i=0; i<len && str[i];io_putch(str[i]),i++);
for(;i<len;putch(' '),i++);
}

Tuesday, September 15, 2009