0
\$\begingroup\$

There is a browser game called Soccerstar where users play in a soccer team and compete in matches against other teams. Matches are scheduled at specific times of the day and played automatically and when the user logs in he can watch an animation representing the last played match.

Now, I want to implement a similar feature: a match is simulated on the server based on the players statistics and tactical choices and the information about the match progression is stored in the database. Please note that I want the outcome of the players actions (tackles, shots etc.) not to be completely deterministic because I want to introduce some randomness. Once the match has been simulated and stored, the client can retrieve the information and generate the animation at any time.

My main doubt is how to represent and store the information about the match progression: what would be the smartest way to do it?

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't think we have enough information about a concrete problem in your game to narrow down specific solutions. The storage can literally be whatever you want it to be - from a complete frame-by-frame description of where every entity on the field should be at each moment, to no storage at all, if the complete sequence can be generated deterministically from the team's stats and a seed based on the match start time. What kinds of behaviour are you currently having trouble storing or playing back / re-generating in your game? \$\endgroup\$ – DMGregory Jan 18 at 13:04
  • \$\begingroup\$ Actually it was a theoretical question, I am not implementing anything for now. I was just curious about what are the possibilities for a problem like this. By the way, I don't think that the whole sequence can be generated deterministically. \$\endgroup\$ – firion Jan 18 at 13:13
  • \$\begingroup\$ Questions asking "How might existing game X have implemented feature Y?" are not considered on-topic here — we're not the right people to ask. You can ask the developers of the original game directly; they'll know. All we can do is speculate, which leads to opinion: I think there's nothing to prevent those devs from generating their matches deterministically, you think there is, and only the original devs can tell us whether either is right. What we can answer for you here is "How can I implement feature Y in my game/mod?" If that's not your question, you may want to look elsewhere. \$\endgroup\$ – DMGregory Jan 18 at 13:47
  • \$\begingroup\$ A Pseudo-Random-Number-Generator is a deterministic algorithm. You give the generator a seed, then it will produce a sequence of pseudo-random numbers. If you give it the same seed, it will give you the same sequence of numbers. You can thus generate a seed before a match, then use the generator to play a match that "has randomness" to it, then store the events and the seed, and use the same seed to replay the same match. With the same seed, the sequence is deterministic :) \$\endgroup\$ – Alexandre Vaillancourt Jan 18 at 15:01
1
\$\begingroup\$

Most pseudorandom number generators can be initialized with a seed value. When you initialize it with the same seed, you get the same sequence of random numbers. That means even an algorithm which uses pseudorandom numbers can be reproduced exactly if you seed the RNG with the same value.

That means you just need to store the seed value in your database and you can exactly reproduce the whole match with all of its random events.

However, there is an additional problem in your case: The game is played asynchronously. When a player watches a match a few hours after the others played it, then some variables which affect the outcome of the match might have changed. For example, the opponent might have changed their team composition. So you need to make sure the match is always simulated with the game state at the point in time where the match was scheduled, not the current game state.

If your data model has a build-in history (which can also be useful for a lot of other reasons), that's trivial to do. If your data model does not keep a history, then you need to store all parts of the game state which are relevant for the outcome of the match together with the RNG seed.

Another problem occurs when you decide to change the algorithm which is used to decide matches when your game is already public. When one player watches the game before you release the update and another player watches it after the release, they will get different results. You can avoid that problem by keeping your old match algorithms around and always decide matches according to the algorithm which was the current one when the match was scheduled.

\$\endgroup\$
  • \$\begingroup\$ A couple things to add here: 1) ensure your die rolls aren't dependent on a variable frame rate - you can use techniques like a fixed timestep to ensure that playing back the match at a different rendering rate doesn't change the outcome. 2) you might want to flip this around and generate the final score outcome first, then generate a sequence of plays that produces that outcome. Then your server only has to run the former calcluation to update team stats & standings, and the more detailed sim can be run on the client only when watching the replay. You can also orchestrate the plays for drama. \$\endgroup\$ – DMGregory Jan 18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.