The galaxy generation code bothered me a bit. In a previous post, I talked about optimizing it so it's somewhat faster. But if I do 800x800 random galaxy, it takes my computer over a minute to generate the galaxy (it's a quad core with 4 gig of memory), and there were only like 4,000 stars when it was done. Surely my computer can place 4,000 stars randomly faster than that, so I brainstormed for a faster algorithm. Then it hit me, I can use the quadtree concept I used for galaxy drawing to generate the galaxy. Here's how it works:
Parent node encompass the whole galaxy, and have subnodes that divide the galaxy. This continue to branch until each node are only 1x1 in size. I then tell it to remove nodes that are in illegal areas (for shaped galaxies). Then I tell it to randomly find a node and give me its location. After getting the node, I place a star there, then remove the adjacent nodes. This continue until there's no more nodes.
The end result? The same 800x800 random galaxy that took over a minute to generate now generates it in about 2 1/2 seconds! This is a major optimization! As a result, I'm satisified with how it works, and its speed. I seriously doubt anyone will play in those huge galaxies, but just in case they do, they won't laugh at my generation code.
I've started importing some stuff from the old code (before I started refactoring the whole thing), and did some code improvements and cleaning up. Planets are back in, and each star system can have up to 6 planets. The "IsExplored" is added, and the function "SetHomeworld" is implemented. Starting a game from the new game screen now creates homeworld for each of the player.
I also added an EmpireManager that will manage all the empires. When a player hits end turn, Empire Manager will iterate through each empire, and if it's computer player, execute the AI code, otherwise halt and let the next human player play. When all empires are done, then end of turn will commence. There's no "End Turn" yet, but the Empire Manager will help organize the code for me.
The exploration functionality is there, so when you start a new game, you only see your own home system's name. All other stars don't have their names displayed.
Now that my goals as stated in previous post are accomplished, my tasks now are to import all the old code and re-implement them in the new code. This includes colonization, exploration, fleet stuff, end of turn processing, and other stuff. When those are done, you'll be able to start a new game, select the galaxy shape/size, select players, and start it, then explore/colonize. I might throw in planetary development, but no promises!
No comments:
Post a Comment