How does server work in simulation games?

Hi

In games like Clash or other similar games with resource management and simulation elements in which player has his own map and can give some tasks (like building,…) to NPCs (for example) and leave the game, and when coming back the next day he sees the job is done (i.e things are created, resources are used,…)

Does the server keep running the simulation even when the player has left the game or are there some other mechanisms to it?

1 Like

I think you have two options:

  • The “simulation” (as you called it) keeps running
  • On any action that happens, the server checks how much time has passed since the last action, and executes everything that happened in that time (I think this is how the old browser games (where you had a village, could build knights, send them to other villages, then they would arrive in a few hours etc.)) worked - Most of them were written in stuff like PHP and just checked at the start of the page how much time has passed since the last page load and executed everything in between)

That said, for a new game, you should probably go with the first option as a permanently running application that receives connects/disconnects from clients is not really a big deal to setup anymore. And you will save a lot of tricky stuff and workarounds, that you would have to do for the 2nd solution to work at a proper scale. At least that’s my opinion.

2 Likes

i dont played Clash, but based on what you said, i can tell its exactly like destroflyer said.

it is: do same like browser mmo games do.

Server just know time in database or somewhere when action was started and when should end.
Then client only receive once from server “time left” of that action to show timer that is calculated in client side. Once action is done, client decide to auto-update visuals or just wait for user to click any button/menu.

1 Like

Yeah, I agree… a lot of this stuff is predictable. That they let you watch it is because they’ve injected you into the calculation. But (usually) this stuff is independent and doesn’t require any special calculus to determine when it ends, what the state will be, etc…

1 Like

Guys, thanks so much for the inputs. :slightly_smiling_face:

The “time left” approach seems good and fits well with client hosted servers as well.

By the way, does Minecraft also take the same approach? (have not played it before)

Minecraft runs the simulation for every loaded chunk. Now a chunk is mostly only loaded when a player is nearby. That means if you never visit your farm again, trees and crops don’t even grow.

While ticking all the time is possible, it’s minecrafts biggest performance issue, because once every X ms, the whole world is simulated. At some point, the simulation is too big to happen in time.
It’s a bad comparison, but simulating with a bigger timespan (the browsergame approach) is a lot less computation, at least if your game can run while the client isn’t there.
Ultimatively, when the “chunks are loaded”, you need to fall back to method 1 though. Like when there’s always at least one player around.

But that depends on the use case. Think of a browser game: You get attacked. The server calculates how much troops you have gained since the last login. They fight. The simulation sleeps again.
It doesn’t work for games like minecraft when someone is waiting for your trees to grow.

Edit: For Minecraft Plugin NPCs, where possible, the approach of time passed is used as well for performance and to force the player to login and collect the goods.

1 Like

Note that sometimes the calculations depend on a correct order of events.
Darkchaos gave one example: If your village produces new troops at a fixed rate, you first have to run the production simulation only until the time you get attacked. Otherwise the defender will have too many troops.

Or if attacks allow looting of resources, only simulate resource production up until the time of the attack to prevent stealing too much.

And if the looted stuff increases an attacker’s production, run his simulation first until the time when his troops return. Then increase his production and run the simulation up until the next event (consider attacking/being attacked multiple times).
Same applies when a building is completed which increases production.

I wrote a browser game a while ago in which a user also gained points from resources in stock (because you can only attack players with at least 50% of your points[*] and I wanted to reduce the advantage of the already massive production difference). And initially I ran the resource simulation only while a player was online or during interaction (attack) events. There was a Top100 ranking list and since players only consistently gained points while they’re online, it allowed players to see who is offline (and therefore a more defenseless target)!
I then changed it so the simulation of other users also runs (sometimes, random chance) when I see them listed anywhere.

[*] This btw lead players to dump their resources into trade offerings. Gamers will find every exploit, it’s so funny.

1 Like