četvrtak, 1. kolovoza 2013.

Progress report July 2013





As usual there was a lot of under the hood work and some visible changes last three months. As you can see on image above galaxy map looks like something: textured stars, wormholes, star names, selection indicator.
Some new functionality is there too, double clicking the star opens the star system. View is still stub though. A lot of stuff is stub right now but as I'm running out of stuff that can be stubbed real implementation is under way. I'm experimenting with my tool for procedural image generation in order to make better star images (different for explored and unexplored) and to make fair map. Currently I'm tackling the fair distribution of vacant star positions. It may sound weird to use image generation tool for prototyping galaxy generation algorithm but I must say is good visualization tool.
This is how the tool looks like. Parameters on the right and generated image on the left. When I have to change the algorithm, I have to go back to source code yes but when I want to try out the algorithm with various parameter values I can do that on tool's GUI. Images above visualize output of star positioning algorithm. Left image is star map with hidden vacant positions while on the left those are shown as dark grey. Light gray/yellow dots are positions for players' homeworlds.
Also finally, I've found a comfortable way for keeping "to do" notes. With graphs! GraphViz is amazing tool, if only there was good GUI for it. Maybe I'll make one in the future. To do graph is simple, everything starts from special "Features to do" node and branches out from it. In order to complete the game I have to implement this, this and that feature, OK, but feature "Fleets" is too vague and considers a lot of tasks so I'll split it to this, this and that subfeature and so on. Leaves (nodes without following nodes or "children") are either tasks that I can immediately start and finish within few hours or concepts that have to be more elaborated and split into smaller tasks.
I've tried something similar before, called it "Spec tree" but it didn't feel fluid. I almost started in-depth analysis of why but I'll just say that standard Windows tree view feels cluncky (even with drag and drop) and looks noisy. On the other hand editing textual file in Notepad++, running a command in Command Prompt and seeing a result in Photo Viewer on the other screen is somehow easier and more satisfying.

četvrtak, 9. svibnja 2013.

Progress report April 2013

 

Introduction


There was a lot of changes under the hood and even a few visible ones during the past month:
  • Meditation on game data model
  • Programs for creating images
  • Organized external dependencies
  • OpenGL graphics

OpenGL graphics


This is last thing I've implemented last month but it is the root of most work. Also it is only visible progress so I'll explain it first. Last autumn was making a program (called it Grid Pixel Canvas) for creating vector graphics images. It didn't go far because I haven't elaborated how should tools (for drawing dots, lines, curves, polygons) be used but it was a great exercise in learning OpenTK. OpenTK (open toolkit library) is a library for .Net that among the other things wraps OpenGL. Other wrapper libraries mainly translate C functions and constants 1:1 to C# but this one it makes it feel C#-ish. Constants are translated to enums, matrices are proper C# objects instead of plain two-dimensional arrays and so on.

Galaxy map in the Stareater is now rendered with OpenGL provided by OpenTK. So far the rendering only draws red squares in place of stars but it's a foundation I can build upon. Rendering loop is implemented in such way to minimize unnecessary updates on one side and to allow smooth animations on the other. All game engines I've previously tried, including the one shipped with OpenTK, simply redraw a scene as often as possible. Upside of that approach is maximal possible frame count per second but the down side are constant CPU usage and consequently bigger energy usage. Energy usage can be an issue on laptops because an application with 100% CPU usage can drain the battery in less than an hour, maybe even less then half an hour. Stareater is not going to be graphics intensive game or generally constantly computationally demanding so it will not be designed in a way that clogs CPU. Way it done now is with a timer that triggers refresh roughly every 10 ms.

Along with rendering I've implemented map panning and zooming. When zooming, the point under the mouse is fixed. I'm pointing that out because I had to do some matrix calculus on paper for that and I was proud when it worked :). Anyway no more jumbo picture box with scrollbars and glitchy panning.

Old data model


In order to draw something on the map, star locations have to be stored somewhere. Data model used before remake (prior the version 0.5) was clunky. It was classic object composition and it turned out that I needed something that looks like relational database. To explain this, the situation was along this lines:
  • Game object contains collection of stars and collection of players
  • Player object contains collection of fleets and collection technologies
Sometimes the game needs collection of fleets of a certain player and sometimes collection of fleets at certain location. Old data model made one query straightforward and the other complex. Getting players fleet would look like game.players[playerNumber].fleets. Getting fleets at certain star would require looping through all fleets of all players and check if location matches star's location:

Collection fleetsAtStar = new Collection();
foreach(Player player in game.players)
    foreach(Fleet fleet in player.fleets)
        if (fleet.location == star.location)
            fleetsAtStar.Add(fleet);

As you can see, the code is visually complex. Performance is suboptimal but not a big issue. Why I'm complaining about visual part, because the code that is easier to read is easier to fix and build upon. Another big issue with object composition is that temporary data (such as player's total research points) also has to live within objects. Well, it doesn't necessarily have to but it's hard to move it somewhere else.  In properly designed relational database data organization is radically different. There are no nested objects and certain mechanism, called indexing, allows skipping the majority of uninteresting data when searching for objects that satisfy certain condition. Example from above would look like this in a database:
  • Collection of stars
  • Collection of players
  • Collection of fleets with reference to owner (player) and location (star)
  • Collection of technologies with reference to owner (player)
Getting data from database is this simple:

Collection fleetsAtStar = db.Fleets.Where(
    fleet => fleet.location == star.location
);

Code for getting fleets of certain player would look the same but with different "where clause". Easy, isn't it? For those unfamiliar with the syntax of lambda functions, the parameter of where method is a function that takes a fleet object and decides if it satisfies condition (returns bool). Performance-wise this code is by default equal to nested loop from object composition example but as I said, database "tables" (collections of data of certain type) can be indexed.
Simply put index is kind of parallel collection that groups references to objects in primary collection by certain property. If the database query (such as Where method in the code example above) involves property that has been indexed than an the query would be reduced to finding appropriate groups and loop only through elements in those groups. And just to be sure, finding a group is faster (O(log(n))) then checking all groups but that's enough details in this digression.

Custom collections


I was really tempted to use a database to store runtime data. With SQLite and an adapter library the size of game would increase by 3 MB. That may not seam as a problem in post the ADSL era but upload speed at my location is horrible, space on Google Code hosting is limited, code repository get clunkier and it simply can be done with less megabytes. In fact I've found solution that is as big as few C# classes. When translated to machine code it's merely a kB or two. The solution is custom collection implementation for each "database table" that groups elements on their own and exposes those groups. Now the kicker is that I'm using T4 to generate code for those classes. Instead of copy-pasting and modifying same but slightly different code I can write no more then a dozen lines in a text template file and the C# class is generated for me.

T4 is a markup language much like ASP (or PHP and JSP) that allows mixing of content and generation logic. In this case the content is C# code. Generation logic is either VB or C# code that direct the flow of content generation. Sound like inception, using C# to generate C# code :). Another feature I'm using or even exploiting is including one text template file to another. One text template is a base, a set of helper methods and variables and class generator with placeholders. Other templates fill those placeholders and use base template to generate desired code. And they are quite short:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="$(ProjectDir)/Utils/Collections/IndexedCollectionBase.tt" #><#
  
  namespaceName = "Stareater";
  usings.Add("Stareater.Utils.Collections");
  usings.Add("Stareater.Maps");
  usings.Add("Stareater.Players");
  
  Generate("VisitedStarsCollection", "VisitedStars",
   new GroupInfo("Stars","StarData","Star",ViewType.Collection),
   new GroupInfo("Players","Player","Player",ViewType.Collection)
  );
#>

First two lines are overhead, what langauge is used in template code and what extension to append to generated file. Third line includes the base template file. Next four lines fill certain optional parameters in base template and finally, call to Generate method fills placeholders and does all the heavy lifting. Parameters of that method are collection attributes (class name, type of elements) and group descriptors


New data model


After I got a way of storing data it was the time to see what data does the game needs and how exactly to organize it. To do that I went through all classes that contained some kind of dynamic data and plotted it on a graph. A tool specialized for creating class diagrams would be more appropriate but I wanted to learn and test the limits of GraphViz.

Analysis showed that the game operates with four kinds of data:
  • Static (doesn't change over the game, for example technology "tree" or types of ship equipment)
  • State descriptors (describes current turn, for example colony population or ship position)
  • Change descriptors (players' decisions such as spending ratios or fleet orders)
  • Temporary data (temporary values used when calculating the state of the next turn)
This figure show new data organization model where blue nodes are classes and gray nodes their properties. Static data is boring so I skipped it in the deeper analysis. Boring in a sense that they don't interact with or depend on other data, they are for all intents and purposes constants. Among dynamic data, state descriptors are the biggest. What surprised is that change data is small. Don't be fooled by the number of gray nodes in temporary data, effects are very big structures.


This is the graph of old data model. Rectangles are classes a ellipses are their properties. Property colors depict their placement in new model (green = state, orange = change, pink = temporary). Blue properties will be used as grouping keys and red will be either changed, moved to controller (layer between game logic and user interface) or removed entirely. Some nodes have multiple colors which means they multiple roles in new model. Names are copied from source file (plus prefix to make nodes unique since by that time I didn't know how to use a certain feature in GraphViz) so if you don't understand Croatian you may have a hard time to read the graph. That is one issues I'm correcting in current code rewrite, all code is being written in "common trade language" (English).

Separation of state, change and temporary data is perhaps the change with the biggest impact to the code rewrite. Partly because I didn't expected it when planning the rewrite, partly because it allows better separation of concerns and it improves readability.

External dependencies


I touched the subject of external dependencies earlier. Two new external dependencies were introduced to Stareater last month: NGenerics and OpenTK. OpenTK, as mentioned, interface to OpenGL (pretty graphics on user interface). It also provides OpenAL (open audio library) that may come handy in the future. NGenerics provides additional data structures that are needed in the code, namely priority queue, 2D vectors and graph vertices and edges. Those classes are relatively simple to write but I weighted that cost of the existing library is lower then reinventing the wheel. Size of the DLL is acceptable, about 160 kB. In my humble opinion it would be better if they have had split the library to a few smaller because serious stuff is mixed with educational (like gnome sort) and algorithms and data structures can be separated.

To manage external dependencies I learned and started to use NuGet. It's the tool for managing dependencies that integrates well with Visual Studio C# projects. For professional and higher editions of VS there is the NuGet extension. For others there is a command line tool which is fairly simple to use. The benefits of the tool are simplified installation of dependencies, easy updating and separation of library repository and code repository. Separation is neat thing because the versioning system doesn't have to track DLL file which can be quite large compared to the rest of the files.

Conclusion

Last month I learned a lot of new stuff in order to make a progress in the Stareater. I learned how to use GraphViz, NuGet and T4,  i4o and C# Expression Evaluator. Last two weren't used in the project but they were a learning material.

As usual, majority of progress isn't visible but will add it's value along the line. Now on the menu are the cool things: playing with OpenGL, exploitation of the gained knowledge, experimentation with starlane generation algorithms and experimentation with procedural image generation. Oh, now I see I haven't touched the subject of my programs for creating graphics. The post is too lengthy as is. I'll write about it some other time.

In retrospective, for me the blog turned out to be horrible medium for reporting progress. At first there is nothing to write about and then there are either many small subjects or a few hard ones. For instance, this post took me more than a week to write it. It was still April when I started. I could have used that time do work on the game. That's why I've decided to either blog even less often or write simpler posts and use Twitter in the mean time. Hash tag is #stareater4x.

ponedjeljak, 25. ožujka 2013.

New game finished

 
"New game" GUI is almost finished, actually it's in that state for few weeks. Part that currently stands between almost finished and finished is map preview. There are few things under the hood that have to be implemented beforehand so by the time the "New game" GUI is finished, the main game GUI will already be in the development. So I'll take this opportunity to write about new stuff in new "New game" GUI.

Obviously the current design differs from original concept (compare the image above with the image in the post from November). First of all the "New game" is not a single window in the process. Main window provides overview of selected settings and opens other windows for specific settings. I figured it would be cleaner to separate and have larger GUI form each group of settings than cram everything on a one big window.

Quantity and quality (color, organization, human or AI) of players is defined in "Player setup window". Once I get some spare time I'll add default "confirm" button to make it consistent with other windows and when I get sensible drawing tool (or skills) I'll add visual description for a player type in the list of players. Imagine it as human or computer icon inside color rectangle. What is not seen on the image above is support for multiple AI implementations. AI logic is contained in plug-ins, in DLL files that have one or more classes with certain interface. That way it's easy to add and change AI implementations and no implementation receives special treatment including my own.

Starting conditions setup window is for setting up initial population, infrastructure and number of colonies. I was thinking about having options for adding starting fleet and technologies but that is for future work when I'll have too much spare time :) (and when technologies and ship designs are reimplemented). Oh and one sexy thing that is already implemented are magnitude prefixes. Instead of writing 1500000000 simply write 1.5 G. Valid prefixes are k, M, G, T, P, E, Z, Y (as in kilo, mega, giga, tera, peta, exa, zetta, yota).

And finally the map setup window. Parameter assortment depends on map generators which are, like AI implementation, defined in plug-ins. Map are generated in three steps: positioning, connecting and populating stars. Positioning step determines number and positions of the stars. Second step connects stars with wormholes. In the final step star types are decided and populated with planets. Each of these steps is implemented in separate plug-in so it's possible to mod only part of map generation process while reusing other parts. In case there are more then one implementation for a certain step, GUI will show a dropdown menu for choosing one. Big black area is currently a place holder and will be used as map preview once I implement map generation steps (for now there are only interfaces and empty implementations) and draw new images for stars and wormholes.

As you can see there is a good deal of moddable parts. And there is more. As you may have seen on my main blog I've been trying out parser generators. One of result of such work is a parser that could crunch normal infix expressions. In previous versions of Stareater numbers in data files (such as food per farmer) were either constants or expressions in prefix or postfix notation (Polish or reverse Polish notation). Why were they in a text file instead in code in the first place? To make balancing easier (modding support is just a bonus). It's easier to tell a friend which number to change in text file than how to navigate and recompile the source code. And because I felt they are kind of configuration data and should be outside the source code. Now it's even simpler, with infix notation. I admit I overdid some things but I believe it will pay out. Just for the record writing a syntax for infix parser with proper operator precedence and associativity is less than 2 hour job but attributing that grammar (writing C# code for actions when certain terminals or non-terminals are matched) took me almost 20 hours.

Next thing on the list is to complete the map generator and have galaxy map rendered as soon as possible. I'll probably make simple map generator without resource balancing and making sure home systems are "fair" than implement map rendering and do the rest of the map generator afterward.

subota, 19. siječnja 2013.

Map generation pipeline

New game form is almost complete, all that is left is map setup GUI. That would be trivial if it was simple "select size" drop down menu but I want to expand it a little, I want to add more options. By default map is square shaped and I want to add means for defining different shapes such round or spiral. Instead of hardcoding few shapes, I opted for plugin based system where each shape generator is a DLL file containing a class with certain interface. Actually a single DLL file can contain multiple map shapes but that's technical detail point is, map shapes are .Net code that is outside the main executable. That was the idea before I've started implementing new game GUI. Thing I realized yesterday is that code for positioning stars is short compared to the rest of the map generation code (assigning star types, populating stars with planets, ensuring balanced distribution of "good" and "bad" planets) and that I or modder would want to reuse that over multiple map shapes. That made think a little bit more, define the map generation pipeline and decide what would be moddable and how.


Looks simple and trivial and that is the power of documentation (that I can't stress more). In star positions phase, as mentioned positions for the stars are calculated but stars are not yet generated. In the next phase
star types and sizes are decided. There potentially could be more attributes such as space monster lairs or alien relics but at the moment they are not defined. In parallel to that phase can go the phase where "starlanes" (or wormholes) are distributed. Planet distribution phase and planet attributes phase may actually be single phase. Anyway those two phases fill star systems with planets and calculate attributes such as planet size or environmental conditions.

Potentially each phase on the flow chart can be made moddable but at the moment I don't see the need for that extreme. For now I'd group those five phases to two groups: star graph and planet generation.


Odd choice is putting star attribution to planet generation. The original idea was that star type directly influences distribution of planets (both positionwise and typewise) in the star system. Putting star positions and starlanes in the same bucket may reduce the code reuse a bit but it simplifies interfaces for plugin code. I don't know, may I could split them somehow without "void *" arguments and with the fewest presumptions.

četvrtak, 8. studenoga 2012.

New game screen


So, GUI time. To be honest, I didn't do much coding. I've spent some time writing about technologies in design document. For now research and development are covered by the document but those topics also opened some new questions and made me aware of one more topic related to technologies. The new topic is  point spending focus. So far I thought about it as a little detail but it turned out it's bigger than that, big enough to have a chapter for it self. The questions I mentioned are should research scale with the number of players and should researched theories be easy share (traded during diplomatic agreement or stolen through espionage). Problem of scaling is that if theories are easy to share, having an ally could drastically speed up the research. I'm not talking about 10%-20%, I'm talking about 100%-200%. By writing this, I recalled a certain feature of Civilization EVO game. Trading technologies there didn't simply counted as acquiring fully researched tech, instead cost of researching traded tech was halved. I like that approach and maybe I'll do something along that line.

Oh, that picture on the top? It's a concept for a "new game" screen/window/form/dialog. It's much like old one except "Map" and "Starting population" drop down menus are replaced with buttons that will open respective dialogs. Reason for those changes are support for multiple map generation methods and more options for starting population. The concept is by no means final and there are some stuff I'd like to change. For instance, there is no way to select player's color and I'd like to add some way for choosing "faction" and color for the opponents.


Also I'd like to mention that I was a little distracted by making tools for creating graphical content. One such tool is for toying with procedurally generated images (picture above). Old images for stars on the galaxy map were in GIF format and don't contain information about transparency in the so called alpha channel and this tool is designed to produce such images. Also the tool is a framework for implementing different image generators.


Other tool is for making "vector graphics", images defined by shapes rather than by limited number of pixels. As you can guess from the picture, the tool will be used for drawing ships for the Stareater. The thing with this tool is that shapes' vertices are always on the grid. Somehow I find it that my drawing skill work best with that restriction.

And last but not least distraction is the IKON library. I was writing unit test for IKSTON to do the "test app" right, to test the nifty TDD features of Visaul Studio and to easily check whole library when I change something. I'll continue with these distractions, I have to write some more unit test for IKSTON and there is still a lot to do on the Grid Canvas (that air plane on the image above). I hope I'll soon find enough motivation to finish the new game screen for the Stareater and to finally start with the interesting stuff.

nedjelja, 7. listopada 2012.

Localization

I was going write about localization as I was implementing it but the lack of a free time prevented me form finishing the post before the implementation was completed. Now that it's finished and it's weekend with holiday on Monday, I finally have the time to write a few words about it.

I did some polishing on the IKON library, my pet project, to streamline the data loading logic in the Stareater. As I said in the previous post, application settings (language choice, GUI size, game settings) use IKSTON variant of IKON and language files use a custom variant.This is how a simple language file looks like:

:FormMainMenu
FormTitle = Main menu
Title = Stareater
NewGame = New game
Settings = Settings
Save = Save
Load = Load game
Quit = Quit
-
Except the first and the last line, entries are key-value pairs. This is the simplest format that is and will be supported. It's easy to read and easy to edit. At least I thinks so, complaint in the comments if you don't think so :). I'm planning to add support form more complex formats along the way.

If you remember from the previous post, each IKON value starts with a "sign", a character that defines it's format. First sign in the example is ":", a colon. That sign defines what I call a context, a named set of key-value entries, much like IKSTON compound value. Terminating character for contexts is "-", a minus. Context in the example is named FormMainMenu and contains entries for the game's main menu. Sign for other values in the example is "=" and represents the beginning of a single line text value. Text in those values  is interpreted as a literal text with stripped leading and trailing white spaces. Other formats I'm planning to implement are for supporting multiline and variable texts. Both formats will be introduced after (re)implementing technologies and expression in data files.

There is one more thing to say about localization support, folder structure. All language data are stored in {Stareater}/language/ folder (where {Stareater} is thefolder where the Stareater is installed). Subfolders of that folder are named after the language code, so US English texts are located in {Stareater}/language/en-us/ and Croatian in {Stareater}/language/hr/.  Also if the language is intended to be used as "default", suffix(default) is added to it's folder name. Default language is the language that is set initially and used as a fallback. If a certain translation is not found in the language selected by user, text from the default language will be used.

Now that first few bricks are laid, it's time for more game related work: "new game" screen, map generators and organizations. I finally decided what should be organizations used for (except cosmetics), they would provide bonus level for technologies. Bonus levels would affect benefits from that technology but would not increase research cost and requirements for upgrading technology effectively reducing the cost of developing that tech. Sound simple but don't expect it to be done by the end of a month :).

ponedjeljak, 17. rujna 2012.

IKON and IKSTON

As mentioned before, I've designed my own data format for the Stareater data. The language has an abstract foundation and concrete version. Abstract part is called IKON (Ivan Kravarščan's object notation) and on it's own doesn't define any data format and cannot be used as such. What it does define is:
  • Identifier format (sequence of a-z, A-Z, 0-9 and _ characters).
  • Value syntax:  sign data [@ identifier]*
    • The sign is a character associated with the parser for that kind of value. 
    • Identifers after "@" are reference name, "*" means zero or more.
  • Each value should  have a type.
  • Abstract classes, interfaces and helper classes for quick development of concrete data formats.

IKSTON (Ivan Kravarščan's object notation) is concrete IKON. It resembles to JSON without double quotes in keys in key-value pairs. It has five types of values, number, text, array, compound, referenced values. Text is perhaps the simplest. Sign for text is ", the double quote. Text ends with the second double quote that is not escaped. Escape codes are:

Escape sequence Translates to
\\ \ (backslash)
\" " (double quote)
\n new line character
\r "carriage return" character
\t tab character
Standard stuff, escaping escape character, escaping "end of data" character and a few convenience options.

Numbers are slightly more complex, sign is =, equals character and number parser reads (after skipping whitespaces after sign) the input until it finds a character that not either from 0-9, a-z, A-Z or period or minus. Reason for accepting letters is that scientific notation (such as 1.5e3 instead of 1500) and special numbers such as Infinity, -Infinity and NaN should be accepted. Scientific notation is useful especially since a lot of cost values in Stareater tend to be 9 digit values and infinity "symbol" is good to have around. Also it's good to point out that a number is ambiguous terms when it comes to programming. Numeric IKSTON values do not automatically convert textual representation to numeric representation (integer, decimal, floating point) but they offer methods for getting a value as specific numeric representation.

After explaining text and number values, arrays sound are simple, sign is [, left square bracket, data are whatever IKON data there is and closing character is ], right square bracket. For example:

[ "text1" "text2" =1 =2 "text3" =1e6 ] @testData

Now you see the deal with sign characters. In many other data formats numbers are just numbers but in IKSTON numbers require equality sign. It may seen unnatural but the up side for that is there are no type guessing. Text may not accidentally end up being number. Example also shows how references are declared. Before explaining compound type, example first:

{ star
   size = 12
    x = 9
    y = 4
    name "Alpha Centaury"
    planets [
        { Planet size = 100 }
        { Planet size = 120 }
        { Planet size = 10 }
    ]
}

As you can see, the sign in {, left curly bracket. First thing in the compound value is the identifier with the name of the "class". As stated before each value has a type. Numbers, texts and array have the constant type names (IKSTON.Numeric, IKSTON.Text, IKSTON.Array respectively) while compound values have whatever that first identifier is as a type name. After type name, compound value can have key-value pairs (identifier and IKON value) before being closed with }, a right curly bracket.

Shish, that's a lot of explaining for a something that will not be used "as is" in the project. IKSTON will be used for application settings but localization and game data will use different implementations of IKON. Come to think of it, save game format might be the pure IKSTON. I've already started with localization implementation but more about that in the next post.