Asteroids, my exam assignment
May 17, 2008 Flash, Games, Programming, ade's stuff No CommentsLazy reader’s link to the current version of the game
I went to a short flash-based videogame course a few weeks back. The exam is in form of a homework assignment where you select one of three games to implement. I chose to make a asteroids-style game clone. The platform is Flash CS3 and Actionscript 3.
I started out with the ship, drawing it as a movieclip with 4 frames, for idling, turning left and right, and moving forward. Then i made a SpaceShip-class that handled as much as possible of the ship and its movement. First, I made it turn, with some basic turning-speed acceleration and deceleration, using the three different frames to animate it. Click to try, use left and right arrows to turn.
Then there was some coding involved with making the ship move. The ship uses a vector for movement and modifying its course uses vector addition, which creates a pretty cool skidding effect. When the ship moves toward the edge of the game area, it needs to warp the opposide side of the screen, and there should be visual evidence before the transfer is complete that the spaceship is going to show up on the other end. So, there needed to be a “clone” involved that acts as a copy of the spaceship until the original ship is outside the screen, at which point the clone will take over. I ended up using a border of movieclips and using hit-testing to see if the ship was getting close to the edge. Try version two. Use left, right and up arrows.
When the ship movement was complete, i started working on the “asteroids”. So I put some rocks from my plant in my scanner and put them as a single movieclip in flash. Then i made the Rock class, to handle the rocks movement across the screen. They use a similar edge-detection and clone method, as well as movement. They start with a random position, speed and apperance. Click here to see.
Then of course, I needed to make a end-game screen when the ship hit an asteroid. So i had this vision in my head, how i would call rockMovieClip.hitTest(shipMovieClip) and just find out at once if the ship was hitting the rock. But a quick test revals that flash uses boxes for hit detection and a not-so-fast googling of the situation revealed that there was not really a practical way to do this. I tried to make the pixel-level hitdetection that was described in the BitmapData class but it just wasn’t a very clean solution, so i just made the objects check for their distance instead, since the rocks are kinda round and the ship too. Try here.
Finally, the ship obviously needs to shoot something. I made a green little round particle thingy and copied the movement code from the other classes and wired everything together in the flash file (first frame). Tanja decided my green projectiles were unsatisfactory and suggested I made them pink instead which turned out quite fabulous. Use spacebar to shoot. You may notice that the shots are aware of the ship’s movement.
There’s a slight bug still, where asteroids can “leak” outside the playing area. Need to check my edge-handling code a bit. The “A:” counter shows how many rock instances you are currently dealing with. Get it to 0 and you win (but I haven’t made the win-screen yet.)
Update: The final version is complete.
Duke Nukem Ventrilo Guild Prank
May 17, 2008 Fun, Internet, Uncategorized No CommentsThere’s alot of people playing mmorpg’s these days, many of them taking their gaming very seriously. This particular videoclip deals with a guild in Lord of the rings online, where the mother of a player, also active in the guild, get’s a bit mad at someone playing some sound clips in their ventrilo from Duke Nukem 3D, a classic first person shooter game. Viewer discretion is advised
parsing script files…
April 14, 2008 C#, Programming, ade's stuff No Commentsthis week I’ve been working on a single function for cakepawn (a script editor for the pawn language made in C#) that parses script files. originally it was meant as a bugfix for the previous sloppy method that grabbed functions from files in the “include” directory (for “method insight”, the little tooltip that comes up when you type a function name and a parentheses) but I ended up spending tons of work on it, so now I’m using the same function to parse the current edited document in realtime and show the user’s own functions in a little window.
the old function would lazily search for keywords like “native” “stock” or “public” and grab the function definition from that. however, pawn does not require you to use those words, so you can just define your function with its name and the parameters in parentheses, followed by a block ( { to } ) or one line. the language syntax isn’t following any strict syntax really, which created alot of work. it has a c-style syntax.
so anyway, the first few days i tinkered with some methods to make the parser read the functions like I wanted to, mainly using regular expressions and replacing stuff in the file. what i’d do was to first make a regexp search for the block comments, for example, and just remove them from the string. like this:
string parseStr = textBox1.Text; //remove block comments
string blockComments = “\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/”;
Regex re1 = new Regex(blockComments);
parseStr = re1.Replace(parseStr, “”);
Then i’d do the same for line comments, string/char literals etc. It took me a good while to figure out the expressions for that stuff, but I finally got it working.
Here’s what the function finder looks like.
functionDefs = “[\\r\\n]\\s*(native |stock |public |static |forward |.+:)*\\s*@?\\w+?\\s*\\([^\\)]*\\)”;
urgh… by the way, before I searched for functions, i removed all the text inside compound blocks ({…}), since method definitions are outside any blocks. i didn’t use regexp’s for that though, i just nested two while-loops and removed the text as I went along, removing top-level blocks one by one.
ok so I had all this working, there was only one problem, it was waaaay to slow. after I added the real-time parsing I loaded up my own main script, it’s about 4000 lines long, and the slowdown of the interface was noticeable. so I installed a HighPerformanceTimer and did some benchmarking. it took about 0.88 seconds to parse the file.
the next sessions of worktime I spent optimizing the algorithm and fixing bugs. there was alot of little bugs with the parsing, due to the complexity of parsing such a loosely formatted file. the first thing I did to optimize the algorithm was to make a list (System.Collections.Generic.List) of two-integer objects that kept track of the start and stop positions of removed text, instead of actually removing the text (and thus rebuilding the string tons of times). I later changed the list to a simple bool-array, one bool for every character position that said True if the character was marked as removed.
then I made a custom indexOf (simple string search) function that also checked if characters were removed. then I rewrote some regexps to custom iterations instead, because it was faster.
my first test run gave a whopping total time of 18 seconds (hrrm) but I believed in my code and fixed alot of bugs and stuff and soon it was below one second
then I added in some more timers to check and optimize every part of the function and kept making it faster and faster. the result was good!
old time: 0.88 seconds – new time: 11 milliseconds! I could spend more time making it even better, but I don’t need it to right now. a file of 20000+ lines takes about 75 millisec to parse and spit out all method/#define definiton names and positions, and I don’t need to parse the file too often, so it should be OK.
here’s the class as it looks at the time of writing, in case you need any part of it. LibraryFile.cs
you can also download the cakepawn source to get all files (new version not up yet). http://ade.se/projects/cakepawn
Doug Stanhope
April 3, 2008 Fun 1 Commentjust watched a decent stand up by a guy called Doug Stanhope, from a club somewhere in NYC. it has some of my favorite comedy ingredients, like intelligence, religion-bashing, and extreme obscenity
cakepawn
April 1, 2008 Games, Programming, ade's stuff No Comments
i’ve made a script editor for the sa-mp scripting platform, for the pawn language. kalle made the cool pic for it
it’s my first larger open-source project, and it’s released under gpl3. it’s a multiple document interface editor with syntax highlighting of course, integrated compiler messages parsing and viewing, and has some nice bonus stuff like reading the includes for method insight (the little tooltip that tells you the arguments expected when you type the name of the function) and a color picker with transparency support. still working on it and have many great ideas to complete yet, and alot of ugly code (not mine originally) to refactor. the textarea itself is another opensource project, made by digitalrune, who extended the editor control from the sharpdevelop project.
screenshots:
if you happen to be a sa-mp pawn script maker or want to download the app for some other reason it’s available at http://ade.se/projects/cakepawn
San Andreas: Multiplayer
March 18, 2008 Games, Internet, Programming, ade's stuff 3 Commentswhen the community-made multiplayer mod for original GTA 3 came out years ago, I checked it out and soon realized it wasn’t all that much fun. there was one made for the sequel vice city too, and it had alot more features, and was actually quite fun for a while, but in the end you’d get sick of all the client crashes and lag problems. then a few years passed and I kinda forgot about the whole thing. since then there has been yet another GTA release (for PC), san andreas. so now I checked out the multiplayer mod for that, and wow, I was really impressed. the stability is so much better, and if you avoid certain weapons you can actually have a pretty fair gunfight on the streets without feeling that you’ve unloaded 4 magazines of tec9 in someones head and nothing happened. remember, this is done by hacking the internals of a single-player only game… it’s pretty much not been done before on such a scale.
the most interesting feature with the “SA-MP” mod is that you can run scripts server side to make unique rules for your server. the server’s behavior will be completely different from server to server things like weapons allowed, spawn points, text/UI on your screen, classes, objectives and special scriptable functions like banks, shops, houses…. there’s a good amount of API functions to use. from some of these servers, whole communities take shape.
I played around on servers a bit (but only for like 4-5 hours total) and soon realized that I could be writing my very own gta-mini-mmorpg variant. so I wanted too take a look at how the scripting worked. It’s done in a language called Pawn (formerly SmallC), familiar from things like the amxmod in the half-life engine (specifically, counter-strike 1.6 and below) servers. the general syntax is familiar from C or Java and it’s pretty easy to get into. then I found the MySQL plugin for the script engine, and pretty much just started coding and coding….
I never really looked into the existing scripts that were available in the community. most of them were just variants of one original huge script some guy made anyway, and I thought it would be more interesting to make everything from scratch with my own ideas. as it turned out, there was alot of work involved with this, doing basic stuff like registering, loggin in, setting spawn points, making car spawn locations, etc, especially since I wanted everything to be dynamic and stored in the mysql database
after about two weekends of work, I have a pretty nice foundation (~4000 lines of code), including user account system, a persistent player housing system with a couple of interesting features, dynamic administration of the server with adding/removing cars, spawnpoints, houses, etc. I also made a inventory system and raw-material gathering from various places around the map, with things like harvesting on field with a combine harvester, fishing, and more. one of the goals is to avoid making this rich 3d environment into a zork-style text adventure seen on some servers, where you have to type commands in text every time you want to do something. Instead, the script provides for on-screen menus and visualization, and events triggered upon things you do in the game environment such as entering a certain type of car in a certain area. my friend is helping me with some content adding stuff like making player houses, placing cars and spawnpoints etc.
an interesting side-effect of the mysql architecture is that you can really easily dump information out into webpages, and even allow for some web-based playing if you don’t have access to the real server. more info on the project later
Flash upload system
February 22, 2008 Flash, Programming, ade's stuff No CommentsI’ve started work on a new Flash/PHP web upload system… to make sharing files easier when MSN is too slow or when i’m at school/work and want to send something straight home (without having to re-download it when I get home)
i’ve thought of some stuff i want to make but i started out with the most important feature, uploading files. i looked around on the web for a suitable solution matching my criteria,
- be able to show percentage progress bar
- show upload speed
- be reasonably compatible
turns out it’s a pain making a progress bar with AJAX & PHP. while certainly possible, apparently requires a recompiling php with a patch and stuff, and that’s going to create problems in the long run. however, you can do it in flash pretty easily. now, not all systems have a flash player, but that’s likely to change. this also allows for some funky animations and stuff
so, building on the source from the papervision logo i made earlier, i extended my cube class and converted it to a plane, with texturing. then i chopped up a default file icon from vista in parts of 16×16 pixels (with actionscript code) and assigned them to a grid of 11×14 planes. i also added the target rotation angles instead of just target coordinates. as the file upload progresses, planes come out from the lower left corner and build up the file icon. so it’s like a progressbar (but way cooler)…
oh, and the back end on the server is just a simple php script that eats the transferred file and puts it in the right place. btw, i had to mod the php config a bit to make it accept files larger than 2mb, and run for longer than 30sec. the current system has been tested and works with 700mb+ files.
i won’t post the real thing here because i dont want my server clogged with random files, but here’s a demo of how it looks with the uploading code removed.
again, this is made with Flash CS3 + the papervision3d lib.
click it to start it or reset progress!
papervision logo
February 17, 2008 Flash, Programming, ade's stuff 1 Commentmade a new flash thing today… it’s made with Flash CS3, Papervision3d in Actionscript 3.
concept: make a logo in flash with the 3d library papervision… the idea was to take a small image with the text “ade.se” and make every pixel into a cube in a 3d scene, where the cubes or pixels would come flying into the view, and eventually form the textual image in a nice perspective.
i started with the basics (haven’t made anything other than a few easy tutorials in flash/papervision/actionscript 3 before) and created the scene with a placeholder rectangle (to show the final position of the cubes). then I made a class to handle the cubes, which has a few important functions, setPos, setTargetPos, and moveCloser(speed). the thought is to have the first frame in the movie set up the cubes at random positions and then invoke moveCloser at every rendered frame which causes the cubes to move one step closer to their target position.
the first problem was to make the cubes move in a nice manner toward their goal.
you need to make them move at different speeds in every dimension, as they will be at a different distance in X, Y and Z from their target. here’s a cube moving to pixel position 0, 0 with the reference rectangle visible: movement.htm
also, I needed to make sure that i could actually handle having so many cubes rendered at one time… I created a image in photoshop to act as the target, each (colored) pixel representing a cube and its final position. I settled for a image size of 80×19. selecting the colored pixels with the wand and quick look at the histogram window reveals that there will be 790 cubes needed:
remember that every cube has 6 faces, and 2 triangles for each face. that means 9480 triangles to render in total. this would be a laugh for every computer today in a normal 3d-accelerated environment, but working with it here in a high level unaccelerated state is a bit different. i created a basic benchmark to see how it ran. as you can see, the FPS is acceptable but not very good… benchmark.htm
as creating 790 objects by hand and giving them the right properties would take a long time and be immensely boring, I made a small app in visual basic 6 to read the image and spit out a finished script.
here’s the result! (click it to randomize cube positions)





