How to build a 4x game

I’m trying to study how succefully implement a 4x game.

Area of interest:

  1. map data: how to store stellars systems (graphs?), how to generate them and so on… 2) multiplayer: how to organize code in a non graphical server and a client to display it 3) command system: what are patters to catch user and ai decisions and handle them, adding at first “explore” and “colonize” then “combat”, “research”, “spy” and so on (commands can affect ships, planets, research, etc…) 4) ai system: ai can use commands to expand, upgrade planets and ship

I know is a big questions, so help is appreciated :smiley:

  1. Map data

Best choice is have a graph to model a galaxy. A node is a stellar system and every system have a list of planets. Ship cannot travel outside of predefined paths, like in Ascendancy:

Every connection between two stellar systems have a cost, in turns.

Generate a galaxy is only a matter of: - dimension: number of stellar systems, - variety: randomize number of planets and types (desertic, earth, etc…), - positions of each stellar system on game space - connections: assure that exist a path between every node, so graph is “connected” (not sure if this a matematically correct term)

  1. Multiplayer

Game is organized in turns: player 1, player 2, ai1, ai2. Server take care of all data and clients just diplay it and collect data change. Because is a turn game, latency is not a problem :smiley:

  1. Command system

I would like to design a hierarchy of commands to take care of this aspect:

abstract Genericcommand (target) ExploreCommand (Ship) extends genericcommand colonizeCommand (Ship) buildcommand(planet, object)

and so on.

In my head all this commands are stored in a queue for every planets, ships or reasearch center or spy, and each turn a command is sent to a server to apply command and change data state

  1. ai system

I don’t have any idea about this. Is a big topic and what I want is a simple ai. Something like “expand and fight against everyone”. I think about a behaviour tree to control ai moves, so I can develop an ai that try to build ships to expand and then colonize planets, upgrade them throught science and combat enemies. Could be done with a finite state machine too ?

any ideas, resources, article are welcome!

How much Java do you know?

I have good knowledge of Java

Don’t start with a 4X. Start with just the first X. Get the first bit working without multiplayer. Don’t worry about things like command details at the start. You are going to be wrong every time. You will only get a good idea of what is needed once you have something up and running.

Basically start with a very small playable core. Get that playable. Then go from playable state to playable state.

Trust me. I have been writing a RTS for years. Now i have started again and got something playable in the first weekend. It is what i should have done in the first place. Then you focus on the game, not the code.

Okay just checking because your question is similar to one that asks how do I create an MMO?

delt0r’s advice is good and so I have nothing else to add.

thanks for tips :smiley:

Explore is for sure first thing to do!

Now keeping on creating a graph with a decent galaxy to explore!

For generating a galaxy, I found it very difficult to generate a galaxy of a specific shape. One approach that worked quite well was to use a “probability map” kind of approach.

To get an evenly distributed starmap I’d use the following approach:

  1. Select a random position on the map
  2. Check if any other star is too close
  3. If not, place the star, otherwise go to 1
    Repeat until you got enough stars.

Very simple, but usually works just fine. Now, the “probability map” makes this a bit more complicated. For this you need an image file of the shape of the galaxy you want. For example, a 128x128 image with a black background and the galaxy shape in shades of grey and white. Each pixel of the map then represents the probability that a star will be present in the area of the map covered by the pixel (i.e. black pixels = no stars, white pixels = many stars, grey pixels = some stars).

Generating a star map would then rougly go as follows:

  1. Select a random position on the map
  2. Get a normalized R,G or B value (i.e. 0f to 1f) of the pixel that corresponds to the selected position. We’ll call this the “pixel value”
  3. If (random.nextFloat() < pixelValue) check for close stars and place the star
    Repeat until you got enough stars.

Ofcourse this is not an efficient algorithm at all, but map generators do not need to be. And you’ll still need to add space lanes and stuff. But I don’t really like wormholes or space lanes anyway (is it a coïncidence that the Master of Orion series went down the drain as soon as space lanes appeared?), so that’s not a problem for me

Anyway, using this approach (and some toying with paint.net) it was quite easy to generate all kind of complex galaxy shapes, from clusters to spirals. And it didn’t requite too fancy mathematics. Maybe it gives you some ideas.

Was considering taking how I made my game into my grave, though as I know it’s nothing new I guess I can share the basics I’ve figured out when creating a 4x.

1, Map data

As to have the quickest generation I think a HashMap<Point, Star> is the best data set. It’s slow to add new stars in though it’s never a problem if you generate the stars only once. The O(1) complexity to pick up a star from a position is so worth it.

The graph can be a abstract graph within each star, though picking out for example a LinkedList just as to symbolize the graph is too slow to be properly used.

Generating a galaxy depends on what kind of galaxy you wish to create. I prefer randomized galaxies and then add weights which gathers the galaxy into clusters.

Randomization generation is usually as simple as:


Random rand = new Random();
while(starMap.getValues().size()<STARS_MAX){
Point p = new Point(rand.nextInt(UNIVERSE_WIDTH), rand.nextInt(UNIVERSE_HEIGHT));
if(!starMap.containsKey(p)){
starMap.add(p, new Star());
}
}

The graph creation is just a “Breadth-first search” with weights problem.

2, There exist thousands of different suggestions.

If going turn based then going the easiest fashion with mailboxes make each player send in orders, make server do orders and then let server send out results.

If RTS then if someone do something, send said order to everyone. When everyone has said that they have recieved said order they do said order. All random factors should be set to a SEED. Making sure everyone’s games are constantly identical.

3, Your idea of a functional approach isn’t how you should do it in Java. Just do it OOP as you should do it in Java.

4, I preferred a randomized there events (colonization, research &:c) affects the options and then let the AI go nuts.

In my game it became quite nicely lethal but still simple.

What do you mean by a core game? I also want to create a 4x game, but i don’t know where to start… :frowning: :frowning: :frowning:

A core game meant that you should implement one simplest functionality to be considered a game (a starship following the mouse on the screen or whatever) and then add functionality from there until you got a whole game. Kind of a simple agile production.

If 4x sci-fi then I’d say to begin by randomizing a bunch of points into a data structure.

Then paint said points as JLabels on a JPanel.

And voila, you got the basics for the simplest kind of 4x game! :slight_smile:

If you want some other kind of 4x then you need to specify which to let me help you.

For generating galaxies I worked out a pretty basic algorithm but gives good results.

I have a 2 dimensional array. The distance between two adjacent elements is 1.
I pass in a minimum distance to the algorithm, meaning the minimum distance between the stars. If it’s 2, then at least 2 spaces around a star must remain empty.

I take the first row, pick a random position, place the star and fill in the surrounding minDistance cells with “occupied”. I also fill in on the Y axis.
Pick a random position from the remaining available positions and randomly select one available space, fill in the occupied and continue until there is no more space left. Do this for each row.

For shaping the galaxy, use a probability map as mentioned before.

The distances after is finished are calculated by using simple 2D math and the graph can be built.

Hope it helps someone.

What should i use for a 4x game. Swing or Applet

Another question i have is for a 4x game is it more GUI or UI…

Now you are just trolling right?

1, Doesn’t matter. I prefer desktop platforms in Swing as I consider such games more real, others prefer Applets.

2, Isn’t GUI and UI the same thing? Or do you mean text based versus graphical? In such a case it’s whatever you prefer. I’d say graphical as a text based 4x feels quite a few decades old.

same problem for me, need to fine some gui tool to design a good gui for my 4x game without doing by myself

Where did you place that code. Did you place it in the constructor or the paint method… Never mind i found another way.