Handling large data with AS3

Posted in Programming on March 30th, 2009 by Manuel
No Gravatar

We decided to do our map generator in Flash. One of the reasons was we can share the render code between game client and generator this way. We are planning on having really big maps in our game. To be on the safe side I quadrupled the requirements made by game design (well, you never know) and ended up with more than 10 million values to stuffed into memory. Each map position has different values such as terrain type, climate zone, humidity and height level, … so this is really a lot of data!

With such large data blocks I was facing three problems: One is data initialization time, another one is access time and the last one is memory footprint.

In the first iteration of my map-data class I used our extensive and comfortable dynamic property system. I thought it would be a good idea if I could add any amount of arbitrary data to any map entry. This freedom came with big price tag: It took forever to initialize and used about one Gigabyte of memory. This was absolutely not acceptable!

Consequently, this was not the way to go. Next, I tried a three-dimensional array. This approach makes the data a bit less flexible but it’s still ok. Initialization time was better but still not great. The memory footprint wasn’t what I would call good either. At least, I was now able to fit the data into memory (we’re still talking about over 100MB, though). To bring down initialization times I refactored the data class to use a linear array. Linear arrays are really fast to create, but then I realized that access times are terrible. So, I did not find a satisfying solution here either.

More or less by accident I stumbled over Flash’s built-in ByteArray class. It is basically works as a stream so I did not expect a great performance gain. I tried it anyway. Well, it really outperformed the three-dimensional array, which is astounding since the ByteArray needs the same offset calculations as the linear array does. The best thing about the ByteArray is it’s memory footprint. There is no class overhead and if you only need values between 0 and 255 (as I did) you can use one byte per value instead of wasting 4 bytes (size of int) each. This brought down the required memory to around 10 MB.

In my next post I will write about a little test suite I wrote to prove the power of the ByteArray. So stay tuned and remember to give ByteArray a chance.

Happy coding! :)

Popularity: 5% [?]

  • Share/Bookmark
Tags: , ,

Mapping databases to objects

Posted in Uncategorized on March 17th, 2009 by Joerg
No Gravatar

Some years ago I worked on an application that made use of some pretty heavy database access. We were using the SQLite database, which is sort of a library that contains a relational database which can be stored locally on disk. As the name suggests, it can perform SQL queries. After some months of coding the database interface was over and over filled with SQL-statements which in some cases were put together from different SQL-statement-segments at runtime and other evil stuff.

At one point I asked myself:
What if we now would have to make some major changes to the database model for whatever reason?

The answer was pretty simple:
We would surely die. Slowly and painfully.

We would be working ourselves through every single SQL statement in the whole project and check if it fits the new database model.

Luckily, humans are able to learn from their mistakes, at least to a certain extent. So what we are using now in our browsergame to access our database is a so called persistence framework that lets you map SQL statements which you define in an xml file directly into objects. You won’t find a single handwritten SQL statement in the whole code. Once you have defined how an object is retrieved from the database (like I said, in an xml file), you will never have to mess around with SQL statements concerning that kind of object again, in whatever context the object might occur.

There are several more or less complex persistance frameworks out there, so you’ll have to decide depending on your project which one suits you best. The one we are using is called iBATIS and is available under the Apache License 2.0.

Getting into iBATIS seemed quite exhausting to me at first, but after a few hours of trial and error and some major support from Jens I got it all together and would not want to miss it in our project.

So persistance frameworks FTW!!!!111one

Popularity: 2% [?]

  • Share/Bookmark

Christopher Columbus a Scotsman?

Posted in Blog, People on March 10th, 2009 by Chris
No Gravatar

Can it be true Christopher Columbus, the 15th century explorer, was actually a Scotsman?
The historian Villalonga was combing the archives in the Genoa region, the Spanish history academy and national library. Villalonga found out Christopher Columbus was actually called Pedro Scotto and he was not Italian but of Scottish origin.
You can read the whole story here: Christopher Columbus a Scotsman.

Popularity: 4% [?]

  • Share/Bookmark
Tags: , , , , , , , , , , , , ,

Black Adam and Eve built Ark?

Posted in Uncategorized on March 9th, 2009 by Manuel
No Gravatar

Recently, I read the book “Jungle Child” by Sabine Kuegler. She tells the story of her childhood in the jungle of Indonesia where she and her family lived with a group of fine young cannibals known as the Fayu tribe, which have lived completely isolated from the outside world up until then.

Because of its unusual content it is sure an interesting read but not quite as remarkable as advertised. It is written in a fairly simplistic style while being boring at times as well. I am probably the worst book critic ever so I’ll stop to rant about it right here. What I want to do instead is tell you about something I found in the book:

About two thirds through the book, after Sabine’s heroic father (a missionary) heroically reunited the hostile clans of the area in a very heroic way (did I mention he was a heroic missionary, yet?), the family went some ways upriver to visit the leader of one of the formerly aggressive clans.

At this meeting the leader talked about the mythological origins of the Fayu. He talked about a big city where all people lived happily together. Then God came and gave each couple in the city a different language (Tower of Babel?). So, the people split up and each couple went in a different direction with one of them being the first Fayus. So the Fayu believe they originated from only one couple: a bit like Adam and Eve, eh? But there is more! After a while, God decided it is about high-tide for a good old-fashioned flood and so He flooded the world, but not without letting Fayu-Adam and Fayu-Eve build an ark to take a pair of all available animals aboard. Now, how odd is that?

The Fayu people were really isolated for all this time! They have not been exposed to any other religion except for the one they made up themselves. Admittedly, they have compressed the story a bit leaving Noah and a lot more completely out of the equation. Nonetheless, the parallels to the Christian story are still amazing. Makes you wonder, doesn’t it?

Popularity: 2% [?]

  • Share/Bookmark

Why Am I Not Cool?

Posted in Uncategorized on March 3rd, 2009 by Rafael
No Gravatar

In music, indie is cool. Of course, blockbuster bands with pretty singers get the mass media exposure and make the big bucks, but there are plenty of bass-playing Starbucks barristas who are convinced that their music is a hundred times better than that mainstream crap. More importantly, they have fans who feel the same way! A hardcore punk or indie rocker sees poor production values as a sign of moral integrity, and believes that any band that other people have heard of sucks by definition. I gather that film works the same way; literature is even more extreme.

Games are different.

Jörg mentioned John Carmack in his last post. John Carmack is what is known as a “rock star programmer“. That doesn’t mean he works for Rockstar Games, but rather that Carmack is rich, arrogant, and takes drugs a glamourous virtuoso with legions of fans.  Up until a few years ago, that’s what every game developer wanted to be¹. There was no distinction between commercial and critical success. There were no poor but virtuous idealists struggling to smash the self-indulgent industry collosi — no punks screaming against seven minute guitar solos and big hair.

That has changed. Indie game developers are proliferating, and they are as elitist, sincere and lo-fi as anyone could wish².

But creators are only one half of a scene: without fans, art house game development is just wanking. And so far, most indie game fans are also indie game developers. There are signs of hope — XBox Live Arcade and its equivalent on other consoles are doing a remarkable job of bringing indie games to the mass market, and Penny Arcade is doing its best to make Indie cool — but so far, I have yet to hear a gamer say “Corporate Games Suck! Cliff Harris was so much better before he sold out.”

¹ Except for the indomitable authors of interactive fiction, God love ‘em.

² In part, this is due to technological change: new programming tools and the Internet have lowered development and distribution costs respectively, making it possible for one or two people with a vision to realize it. Ubiquitous video cameras and youtube have done the same for film. Getting noticed, though, is no easier than it ever was, which is where dedicated fans are important.

Popularity: 2% [?]

  • Share/Bookmark