Before I begin, I would like to thank Andreas for providing me with keys for Texture Packer Pro and Physics Editor ( http://www.texturepacker.com/ ). I will be using and reviewing those tools in future posts.
This week I am going to take a bit of a look at Twelve Months of Doomsday Episode 3: Invasion which was released last week. As I said before, when creating a game that is paying homage to an existing (old by modern standards) game, I like to have my own twists to the game. Anybody familiar with classic arcade games knows the game "Space Invaders". In this game an army of aliens march back and forth across the screen trying to get to the bottom of the screen. The obvious twist for this game is to have a number of formations. This is clearly not an original twist to the game as there are numerous games that did just this. My personal favorite Space Invaders variant would be Gorf.
To me, this is not enough of a twist, so I thought about having a different movement pattern. This got me thinking about another classic arcade game known as centipede. The neat thing about that game was that if you shot the centipede in the middle, half of the centipede would continue on it's existing course while the other half would immediately drop down a row. This would also give me the chance to have lines moving in different directions at the same time.
Implementing this movement logic was a bit more challenging than I originally expected. The core movement logic is easy enough. The aliens are put into "lines" where they follow the head alien which determines the movement for the line. Each alien simply tries to move to the location of the alien ahead of them. Dividing the line into two is simple enough. The new line gets flagged to move down so that is simple to handle. The problem is determining where the aliens can move to and if they have a clear shot. Checking against every alien to see if a collision was going to happen would be very inefficient. Granted, todays computers are more than fast enough that I could probably have gone this route. Instead, I break the play field into a tile grid and mark the location of aliens on the grid with the ability to reserve a tile so you don't end up with multiple aliens trying to go into the same tile.
As I didn't want aliens shooting through each other, this made checking if an alien was in the clear and could take a shot at the player was very simple. The grid is checked to see if there are any aliens in the same column as the alien. If not (and reserved tiles in that column count as aliens) we know that the alien can take a shot without hitting another alien.
Thursday, March 22, 2012
Thursday, March 8, 2012
The future is collapsing.
When I wrote my original version of Color Collapse last millenium, it was in C++ for Windows 95. At that time the shareware scene was still strong and I had noticed that there were a lot of Tetris and Columns clones appearing. My personal design philosophy is that if you are going to "pay hommage" to a game, you should always have something original or new to add to the game. Obviously, there are exceptions to this rule (such as for existing card and board games) but I've always tried to put my own twist on any game that I am developing. With Color Collapse, that twist was to have the game board filled in at the start and the goal of the game was to clear the board. I don't know if I was the first person to come up with this idea, probably not, but I was not aware of any other game that did this when I originally created the game.
When I finally decided to play around with web development a few years later, I ended up having to port the game from C++ to the then relatively new Java platform. Last year I was going through the games I have created for the BlazingGames site with the goal of seeing what games could have enhanced versions created for it and came up with a lot of new play modes and special tiles that could be added to Color Collapse so decided it would be a good upgrade candidate. Unfortunately, before I got that project started properly, my mother was hospitalized and later died of cancer. The fact that my mother really enjoyed this game both has me wanting to finish this enhanced version while making working on it emotionally hard. The fact that the 2012 meme can only be exploited once dictated my plans for 2012 so this years releases are pretty tied up.
For this reason, I have decided that 2013 would be the year I really focused on properly enhancing this game. My 2011 plans were to write the game in Flash. Now that everyone is writing Flash off, even though I personally think it will be at least until 2016 that Flash will remain strong, I am thinking that HTML 5/JavaScript is probably the route to go with this game. The game is not overly complex so there should be no issues moving the game to JavaScript.
The sad thing is that even that decision is not a straightforward decision. I can use my existing (BGLayers) library for the game. I can switch to another open source library which would probably provide me with better performance and more already implemented features. I could also wait and see what Adobe's Edge tool does. From the tiny amount I have played with an early demo of the tool it does seem like a JavaScript version of Flash but different enough that I would probably have a lot of re-learning to do. All this is assuming I want to write JavaScript using JavaScript. As it turns out, that is not the only option.
Alternatives to writing native JavaScript is Google's PlayN which converts Java into JavaScript and has a game library as well. Also from Google is their Dart language which is in early stages. Then there is CoffeeScript which seems to be gaining a fairly large following.
This is one case where having too many options is just as bad as not having enough. About all I can say at this point in time is that the Color Collapse Plus project will be the site focus of 2013 (subject to change due to unforeseen factors or the incredibly unlikely event that the world really does end) and will be in HTML 5. The how is the tough question.
When I finally decided to play around with web development a few years later, I ended up having to port the game from C++ to the then relatively new Java platform. Last year I was going through the games I have created for the BlazingGames site with the goal of seeing what games could have enhanced versions created for it and came up with a lot of new play modes and special tiles that could be added to Color Collapse so decided it would be a good upgrade candidate. Unfortunately, before I got that project started properly, my mother was hospitalized and later died of cancer. The fact that my mother really enjoyed this game both has me wanting to finish this enhanced version while making working on it emotionally hard. The fact that the 2012 meme can only be exploited once dictated my plans for 2012 so this years releases are pretty tied up.
For this reason, I have decided that 2013 would be the year I really focused on properly enhancing this game. My 2011 plans were to write the game in Flash. Now that everyone is writing Flash off, even though I personally think it will be at least until 2016 that Flash will remain strong, I am thinking that HTML 5/JavaScript is probably the route to go with this game. The game is not overly complex so there should be no issues moving the game to JavaScript.
The sad thing is that even that decision is not a straightforward decision. I can use my existing (BGLayers) library for the game. I can switch to another open source library which would probably provide me with better performance and more already implemented features. I could also wait and see what Adobe's Edge tool does. From the tiny amount I have played with an early demo of the tool it does seem like a JavaScript version of Flash but different enough that I would probably have a lot of re-learning to do. All this is assuming I want to write JavaScript using JavaScript. As it turns out, that is not the only option.
Alternatives to writing native JavaScript is Google's PlayN which converts Java into JavaScript and has a game library as well. Also from Google is their Dart language which is in early stages. Then there is CoffeeScript which seems to be gaining a fairly large following.
This is one case where having too many options is just as bad as not having enough. About all I can say at this point in time is that the Color Collapse Plus project will be the site focus of 2013 (subject to change due to unforeseen factors or the incredibly unlikely event that the world really does end) and will be in HTML 5. The how is the tough question.
Thursday, February 23, 2012
There and Back Again
After taking a look at a number of potential cross-platform solutions for my future larger-scale development plans, I was hit by a wave of nostalgia which has me thinking about my programming path. What I find most interesting is that there is a noticeable trend which may just be starting to reverse itself, but first, lets start the journey at the beginning.
When I was a kid, I first got my exposure to the concept of computers from science fiction. Saturday mornings had re-runs of TV shows such as Star Trek and Space 1999. Star Wars was the big movie that had me wanting to build my own R2D2 and Video Games were starting to appear. My cousin had an Atari 2600 a couple of years after they came out but my parents wouldn't get me one. This is when my oldest sister started taking a course in high school called "Computer Science." After looking at her homework I realized that it wasn't anything difficult so I got the brilliant idea that if I got a computer instead of a game machine I could create my own games that would be better than anything my cousin could buy for his 2600 so I changed my plans to try and convince my parents that subsidizing my purchase of a computer would be a good educational opportunity.
To cut the story short, a few years later I managed to get a Commodore 64 with a tape drive that I was able to hook up to an old black and white television set (the Apple II I wanted was simply way too pricy). The language that my sister was learning in school was the same language my computer used. BASIC. As it turns out, the Commodore 64 was great for games and could easily blow away anything the 2600 was capable of (due to the rather limited hardware of the 2600) but it also turned out that creating games was not easy. Still, thanks to magazines like Compute and Commodore as well as books like 101 BASIC Games, I was able to learn a lot about how to create games. The big thing I learned, however, was BASIC was just too slow. Reading the programmer reference manual hinted at the solution to my speed problem. Something called Machine Language.
The Commodore 64 used a RISC-like processor known as the 6510 so my first entry into the world of assembly language was the 65xx dialect. This became my predominant programming language. I loved assembly language as it was so much faster than BASIC though was still not fast enough for what I wanted to do and required a lot more work than BASIC. As many programmers at that time did, I compromised by taking advantage of a 4K block of memory located at $C000 (49152) to store small assembly language routines that could be called from BASIC.
When I reached high school, our school was a test-bed for a new type of computer known as the Macintosh which we were programming using a structured programming language known as Pascal. The BASIC that my sister took was no longer part of the curriculum. This was probably the best thing that could have happened as if nothing else, it taught me the evils of spaghetti code and taught me how to better structure my programs.
When the Amiga 500 came out, I was able to get one as part of a bundle that included a huge suite of software including Deluxe Paint 2. The store owner was able to sell me a discounted copy of Latice C as it had been opened and returned. This lead me to discovering the C programming language which my Computer Science teacher did not care much for as I would constantly tell him that it was way better than pascal as it had all the structure plus was much closer to assembly language. In fact my compiler had an option to compile to assembly language so you could write the program in C and then fine tune the resulting code.
In College my language repertoire was increased and my Amiga started having random reset problems. As our computer science lab used IBM machines anyway, I decided to move to a 386 running DOS 5 and Windows 3. When Visual C++ came out, I switched from C to C++.
The big thing that happened in the mid nineties was something called the internet. As I was living in a small town, it was hard to get internet access but thankfully the phone company decided to give my town free long distance to a nearby larger town so I was able to get online (30 hours a month at 14400 baud initially). I was disappointed by how limited browsers were but in 1996 Java came out. The language was enough like C/C++ that I had no problems using the language and had the huge advantage that it would allow you to create programs that could run on a web page without requiring the user to download any type of plug-in or perform some type of installation which I felt would turn away most average users.
For various reasons, Java never worked out quite the way I had hoped. This was largely due to Microsoft wanting an incompatible version of Java to run on their machines which effectively ended up causing Java to stagnate on the browser. For the best compatibility, Java developers essentially had to stick with Java 1.1 instead of being able to take advantage of the new features available to server side developers. This is when an animation tool called Flash added a scripting language known as ActionScript so I started experimenting with Flash.
Now we are at another interesting transition point. With app stores everywhere and Flash starting it's decline, I am looking at which direction to head. One direction is towards HTML 5 which is far less feature-rich than Flash making it a step backwards. The other is a return to C++ and a shift of priorities to phone/tablet/mac store/windows store development. Looking at the trend, I started with a low level language and went higher and higher level till I ended up with a scripting language. Now as Moore's Law is finally starting to show it's head, I suspect that there will be a shift back to C++ and possibly even hand-written assembly language to optimize the critical parts of a program may even start becoming the trend. Who knows what the future holds. Maybe Adobe CS6 will have Flash development tools that actually produce good code for mobile devices. I suspect, however, that I will be shifting back to C++ with some JavaScript for my smaller games.
When I was a kid, I first got my exposure to the concept of computers from science fiction. Saturday mornings had re-runs of TV shows such as Star Trek and Space 1999. Star Wars was the big movie that had me wanting to build my own R2D2 and Video Games were starting to appear. My cousin had an Atari 2600 a couple of years after they came out but my parents wouldn't get me one. This is when my oldest sister started taking a course in high school called "Computer Science." After looking at her homework I realized that it wasn't anything difficult so I got the brilliant idea that if I got a computer instead of a game machine I could create my own games that would be better than anything my cousin could buy for his 2600 so I changed my plans to try and convince my parents that subsidizing my purchase of a computer would be a good educational opportunity.
To cut the story short, a few years later I managed to get a Commodore 64 with a tape drive that I was able to hook up to an old black and white television set (the Apple II I wanted was simply way too pricy). The language that my sister was learning in school was the same language my computer used. BASIC. As it turns out, the Commodore 64 was great for games and could easily blow away anything the 2600 was capable of (due to the rather limited hardware of the 2600) but it also turned out that creating games was not easy. Still, thanks to magazines like Compute and Commodore as well as books like 101 BASIC Games, I was able to learn a lot about how to create games. The big thing I learned, however, was BASIC was just too slow. Reading the programmer reference manual hinted at the solution to my speed problem. Something called Machine Language.
The Commodore 64 used a RISC-like processor known as the 6510 so my first entry into the world of assembly language was the 65xx dialect. This became my predominant programming language. I loved assembly language as it was so much faster than BASIC though was still not fast enough for what I wanted to do and required a lot more work than BASIC. As many programmers at that time did, I compromised by taking advantage of a 4K block of memory located at $C000 (49152) to store small assembly language routines that could be called from BASIC.
When I reached high school, our school was a test-bed for a new type of computer known as the Macintosh which we were programming using a structured programming language known as Pascal. The BASIC that my sister took was no longer part of the curriculum. This was probably the best thing that could have happened as if nothing else, it taught me the evils of spaghetti code and taught me how to better structure my programs.
When the Amiga 500 came out, I was able to get one as part of a bundle that included a huge suite of software including Deluxe Paint 2. The store owner was able to sell me a discounted copy of Latice C as it had been opened and returned. This lead me to discovering the C programming language which my Computer Science teacher did not care much for as I would constantly tell him that it was way better than pascal as it had all the structure plus was much closer to assembly language. In fact my compiler had an option to compile to assembly language so you could write the program in C and then fine tune the resulting code.
In College my language repertoire was increased and my Amiga started having random reset problems. As our computer science lab used IBM machines anyway, I decided to move to a 386 running DOS 5 and Windows 3. When Visual C++ came out, I switched from C to C++.
The big thing that happened in the mid nineties was something called the internet. As I was living in a small town, it was hard to get internet access but thankfully the phone company decided to give my town free long distance to a nearby larger town so I was able to get online (30 hours a month at 14400 baud initially). I was disappointed by how limited browsers were but in 1996 Java came out. The language was enough like C/C++ that I had no problems using the language and had the huge advantage that it would allow you to create programs that could run on a web page without requiring the user to download any type of plug-in or perform some type of installation which I felt would turn away most average users.
For various reasons, Java never worked out quite the way I had hoped. This was largely due to Microsoft wanting an incompatible version of Java to run on their machines which effectively ended up causing Java to stagnate on the browser. For the best compatibility, Java developers essentially had to stick with Java 1.1 instead of being able to take advantage of the new features available to server side developers. This is when an animation tool called Flash added a scripting language known as ActionScript so I started experimenting with Flash.
Now we are at another interesting transition point. With app stores everywhere and Flash starting it's decline, I am looking at which direction to head. One direction is towards HTML 5 which is far less feature-rich than Flash making it a step backwards. The other is a return to C++ and a shift of priorities to phone/tablet/mac store/windows store development. Looking at the trend, I started with a low level language and went higher and higher level till I ended up with a scripting language. Now as Moore's Law is finally starting to show it's head, I suspect that there will be a shift back to C++ and possibly even hand-written assembly language to optimize the critical parts of a program may even start becoming the trend. Who knows what the future holds. Maybe Adobe CS6 will have Flash development tools that actually produce good code for mobile devices. I suspect, however, that I will be shifting back to C++ with some JavaScript for my smaller games.
Thursday, February 9, 2012
TMoD Episode 2 Moon Run
Moon Run, the second episode of my Twelve Months of Doomsday series, was - like all the episodes of the series - inspired by classic arcade games. The big difference with this game from the other games in the series is that it was inspired by only a single game and I never actually played the game in which it was inspired by. I suppose to be perfectly clear, it wasn't actually the game that inspired me but the advertising for the game. This requires a trip back to my childhood where the stray memories came from.
When I was a kid, arcades where a profitable business with video games being the big draw. The home video game consoles were just starting to emerge with the Atari 2600 being the big thing as it would allow you to play arcade games on your TV. Of course, the quality difference from the arcade machines to the home version was huge. A large part of the decline of the Arcade is a byproduct of video game consoles ever-growing power as after a certain point the home versions of the game were good enough that it was no longer necessary to go to the arcade to get the full experience.
Video games at that time were thought of as kid's things. Far too many politicians still think they are as many politicians are too ignorant to realize that a person might continue to play video games after they have grown up. Sort of like how people learn a sport as a kid and still play it as an adult. The point being that something aimed at kids requires advertising aimed at kids and that meant comic books. Comic books are also like video games in which there are many adults who read them. Even back then. But that is a different pet peeve. One of my aunts was a huge comic book fan. She was also an insomniac who would read dozens of comics before going to bed. This was actually a good thing for me and my cousin as my aunt would give us boxes full of the comics she had read. She was a particular fan of Marvel comics which is probably why I am a Marvel fan.
The comic advertisements for video games where often in the form of mini-comics outlining the story behind the game. The concept presented for Moon Patrol was one of driving a moon buggy jumping over craters while shooting down aliens. As a kid this sounded great. From what my friends told me, however, the concept was much better than the game so I never actually bothered with the game. Advertising didn't always fail, however. The D&D ads in the comics actually got me interested in role-playing games.
So, the game was technically not inspired by a classic arcade game. It was inspired by an advertisement for a classic arcade game. Proof that inspiration can come from unexpected sources.
Thursday, January 26, 2012
What if piracy vanished?
There is a saying that you do not know how important something is until you no longer have it. With thoughts of SOPA and ACTA still in my mind, I wondered what would be the results if Santa Claus came down from the North Pole and gave old media the silver bullet for stopping piracy (hint, it is not SOPA or ACTA as those won't work anyway and is as like to exist as Santa)? If piracy were to become impossible, what would happen? Personally, I try not to pirate anything so for me little would change other then knowing that all the stuff I buy is fully legit. Looking at the excuses I have heard for piracy, lets look at the outcome based on the excuses. Percentages are an estimate of how many times I have heard the excuse and don't add up to 100% because most people have multiple excuses.
1. Can't afford to buy the stuff (80%). After piracy is gone, these people will still not be able to afford it. That means that they will not be buying it because they don't have the money to buy it anyway so it is not lost revenue. No extra sales from these people. Granted, some of these people are young and would likely buy stuff once they started getting money but they would have probably done so anyway the difference is that many of these also fall into point 3 even if they don't realize it so the negative sales would apply to these people as well. Net result is a harm to old media and a gain to new media in the long term.
2. Not worth the price (60%). These are people who think the price is too high. Quite often what they want to pay is much lower than what the price will ever be so the result is no sales. In the cases where the price does drop to the point that these people will be willing to pay for the item, some of them will buy the item but most will have found other (new media) equivalent stuff. Net effect is a big gain for new media. While there is a small gain for old media, it comes at a cost of exposure (see point 3) so a tiny short term gain but long term losses.
3. Wanted to see if it was any good (60%). This is the most interesting category as it is where the arguments against piracy fall apart. When you look at it logically, it is obvious that people do not want to buy something they are not going to like. Unless the price is really low most people won't take the chance. This means that there is no net gain from these people. This leads me to a personal example. Steve Gibson recommended the Honor Harrington series but when I looked at the book covers I took a step back. I know you should never judge a book by the cover and this series is an example of why. I would not have grabbed the series had the first two books not been freely (legally) available at http://www.baen.com/library/ . The free books got me to get past my hesitancy about the series and resulted in much more of my money going to Baen. There are a lot of cases I personally know of where a person has become a fan of something due to a pirated copy. Often these people don't buy copies of the stuff they have pirated (sometimes they do) but they usually end up buying new stuff that comes out from that series/author. If they had never pirated, they would not know they liked the series/author and not buy new stuff. Net result is a huge loss for old media.
4. Already Bought It in different format or DRMed (40%). The result of DRM is often products that don't work as good as the pirated version. Some people don't care for the less-functional version so will find a non-crippled pirate version of the product. These ARE NOT lost sales as a copy was legitimately paid for. Likewise, people who have an old record or tape collection do not want to buy yet another copy of something they already own so will "pirate" the stuff they already bought. I assume Santa's solution would cover both these issues so the net result is no gained sales as the pirated numbers were artificially bumped up anyway.
5. Because I can (10%). These are those people who pirate for the sake of pirating. The few people I know who fall into this category either are also in group 1 or buy an awful lot of stuff legitimately so if they were no longer able to pirate they would simply do something else. Possibly a gain for new media. No change for old media.
After looking at the world after the silver bullet I see old media dying at an even faster rate then they are now and new media growing at a faster rate then they are now. Not exactly the outcome that RIAA/MPAA and other MAFIAA type organizations are after. All I can say to the people from these organizations is to be careful what you wish for because you might actually get it and the results are not what you think they will be.
Friday, January 13, 2012
TMoD Twelve Months of Doomsday
What better day then Friday the Thirteenth to resurrect my Game Development blog. After all, Jason managed to rise from the dead quite frequently. True, a month between posts isn't exactly dying but I had to have a Friday the 13th reference. For 2012 I am planning on updating this blog every other week. To be a bit more precise, weeks that do not have new content on BlazingGames.com will be the weeks that I post articles here. When appropriate, these articles will provide details about whatever was posted on BlazingGames the previous week. If for some reason you want to read more of what I have to say, feel free to follow me on Google+.
The big game series that I am running throughout 2012 is Twelve Months of Doomsday (TMoD) which is my way of having fun with the 2012 end of the world theme. Statistically speaking, there is a small chance that the world will end in 2012. To my understanding, the end of the Mayan calendar didn't actually represent the end of the world but the beginning of a new cycle. Perhaps the 21st of December will be the beginning of the singularity that many science-fiction writers are predicting.
TMoD is a collection of classic-style arcade games tied to a twelve part over-arching story about how the cast from One of those Weeks try to prevent the end of the world. I actually enjoyed bringing back the characters from my 46 episode adventure game. The design of this series not only had to tell a story, but had to fit in arcade games similar to those I played as a child. All the games I write are my original code and I try to make the games as original as possible while still bringing back the feel of the old days (well, at least if you are 30+).
The first game in this series was actually chosen not because it was an arcade game but because it was one of the first games I ever typed into my Commodore 64. Lunar Lander was a text based game found in a book containing 101 BASIC Computer Games. While BASIC was not the greatest of languages, it came with the computer so it was the language of choice for learning to program back then. I probably still harbour some bad programming habits as a result. The game gave you a distance from the surface and you had to type in the amount of fuel you wanted to burn. This determined your speed as you approached the surface. There were a number of arcade versions of this concept which I modeled my game after.
You will notice that a large portion of the games deal with threats from space. This is partly because the main villain is an alien, but more because the childhood arcade games that I played tended to have science-fiction themes. There are a lot of really good games that I am planning on releasing as part of this series so please give the series a chance if you find you don't like the first few games.
The big game series that I am running throughout 2012 is Twelve Months of Doomsday (TMoD) which is my way of having fun with the 2012 end of the world theme. Statistically speaking, there is a small chance that the world will end in 2012. To my understanding, the end of the Mayan calendar didn't actually represent the end of the world but the beginning of a new cycle. Perhaps the 21st of December will be the beginning of the singularity that many science-fiction writers are predicting.
TMoD is a collection of classic-style arcade games tied to a twelve part over-arching story about how the cast from One of those Weeks try to prevent the end of the world. I actually enjoyed bringing back the characters from my 46 episode adventure game. The design of this series not only had to tell a story, but had to fit in arcade games similar to those I played as a child. All the games I write are my original code and I try to make the games as original as possible while still bringing back the feel of the old days (well, at least if you are 30+).
The first game in this series was actually chosen not because it was an arcade game but because it was one of the first games I ever typed into my Commodore 64. Lunar Lander was a text based game found in a book containing 101 BASIC Computer Games. While BASIC was not the greatest of languages, it came with the computer so it was the language of choice for learning to program back then. I probably still harbour some bad programming habits as a result. The game gave you a distance from the surface and you had to type in the amount of fuel you wanted to burn. This determined your speed as you approached the surface. There were a number of arcade versions of this concept which I modeled my game after.
You will notice that a large portion of the games deal with threats from space. This is partly because the main villain is an alien, but more because the childhood arcade games that I played tended to have science-fiction themes. There are a lot of really good games that I am planning on releasing as part of this series so please give the series a chance if you find you don't like the first few games.
Sunday, December 4, 2011
DDTe4 Hours 16 to 18 Optimization
After creating the game, it became clear to me that there are more efficient ways of implementing the game other than using a grid like I did but as I have a short time-limit on the creation of the game, these faster implementations are out of the question. Still, there is some time left for doing an optimization pass. One problem that a lot of programmers have is the desire to prematurely optimize code. While the desire to write efficient code is a good desire, spending a large amount of time on a piece of code that has minimal impact on the performance of the final product is not a good way of spending your time. This is why tools like the profiler that is included in FireBug is such a great feature. The profiler tracks how many times and how much time is spent in every function that is executed while the profiler is running. This lets you know what parts of your program is taking the most time.
In the case of the life game, the code that spent the most time executing due to the huge number of times it was called was the getCell function. The problem with this function is that it needs to support calls outside of the proper bounds in order to properly support the borders. That said, the bounds should only be out by 1 in any given direction which means that if the grid size was slightly bigger and the border was computed along the edges every iteration then we could greatly simplify the getCell function.
To handle this change, the grid size was increased by 2 in both directions, with 1,1 being the top corner internally, though externally 0,0 will be the coordinate passed to get the data. This may not be intuitive, but the downside to optimization is that it can make the code a bit more complicated. The getCell function, on the other hand, becomes a very simple single line of code: return this.grid[y+1][x+1];
This means that if -1 is passed, it goes to the extra bottom or left row and passing the size of the grid as a coordinate will get the extra bottom or right row. To properly fill these rows, the setBorder function has been enhanced. It will fill the border rows and columns with the appropriate data. To make sure the data is correct, it is called at the start of every iteration. To finish up my first optimization pass, the countNeighbors and countCancer functions were quickly optimized by unrolling the loops and replacing the call to getCell with directly obtained cell data as a single line of code is easy to inline.
In JavaScript, there are no explicit types for variables. Variables get converted into whatever type is needed for the current operation. The problem is that this does not always work as planned leading to some real interesting bugs that would not appear in typed languages. In my case, I was using a value from a combo box. The value was a number, but because it is from an html control it's value is stored as a string. When it was passed to my function, 2 gets added to it. The problem is that JavaScript did not convert the string into a number then add two to it, but instead it converted the number into a string and appended it to the string. This resulted in "32" + 2 = "322" which is way off. I solved this bug by flooring the input value forcing it into a number, but finding the bug was a huge problem due to the fact that the code seemed correct (and in a typed language would have been correct). Thank you FireBug.
After fixing the bug and running the profiler again, the getCell, countNeighbors and countCancer drastically dropped in execution time. This optimization pass improved the speed of the simulation quite a bit. The game is quite fun to play with though I am sure there are many features people may want. I can't really think of any features that can be implemented in the few hours left within the allotted development time. For that reason, I am considering the game done. Still, if there is enough interest and suggestions, an enhanced version is something that I would certainly consider working on.
Subscribe to:
Posts (Atom)
