I am currently building a game that has a "City" entity. These cities generate and consume resources such as food variably over a period of time. I need to be able query the server often to find exactly how much food the city at any given point. These queries can take place multiple times per minute. There could also be 400,000 cities to track at a given time.

How would you handle tracking these resources? Would you do it in real time, keeping an instance of the city in memory on the server, with some sort of a snapshot in time of the resources, then computing the growth/consumption from that snapshot time for subsequent queries? Would you work exclusively with a database, using a similar "snapshoting" scheme? Maybe a mixture of the 2, caching recently queried cities in memory for a period of time?

There is also a lot of other data that each city needs to track. A player can queue units to build in a barrack. The armies available in the city will need to be updated as units complete.

I'm interested in everyone's input on where/when/how you'd manage the real time data.

  • \$\begingroup\$ I should mention that this is an mmorts \$\endgroup\$ – Darthg8r Jun 14 '12 at 21:26
  • \$\begingroup\$ What's the time resolution that needs to be saved - what if the player unexpectedly looses connection? What type of game - is this like the 'player cities' in Age of Empires online, or like the individual battles players engage in? \$\endgroup\$ – Clockwork-Muse Jun 14 '12 at 22:45
  • \$\begingroup\$ Player cities. Players build a city, build up it's resources, buildings, armies, etc. Then they can deploy those armies to lay siege on another player city. There are also barbarian cities. So, to answer your question, it's a static world, so all content needs to be saved upon disconnection. \$\endgroup\$ – Darthg8r Jun 15 '12 at 12:42
  • \$\begingroup\$ For these battles, what happens if they lose? Do they lose all those units? What happens if they're disconnected during a battle? \$\endgroup\$ – Clockwork-Muse Jun 15 '12 at 15:30
  • \$\begingroup\$ @Darthg8r then maybe it's a game like travian? \$\endgroup\$ – Ali1S232 Jun 16 '12 at 1:41

For a system this large it would be wise to use a combination of strategies. Here are a few strategies that I would employ:

  • Only process a city when absolutely required
    • Update values only when they're needed. Like being viewed by the user or needed in another calculation
    • Use formulas that take time as an input. For example, the population of a city should be defined by a few variables and the time span since it was last updated. This allows you to only update the population values when they're needed and you're not required to continually update them to "keep up".
  • Use timed events to trigger calculations instead of checking to see if they need updating every iteration. For example, when building units.
    • Even if the timer is only a few seconds, this is better than checking or updating a value every frame in the interim.
  • Simplify the data for each city to the absolute minimum needed. Try to base other values off of existing values.
    • For example, instead of calculating and storing the productivity of a city every time it's updated, calculate the productivity based off the population the their moral. This allows you to get the productivity when you need it, without needing to save it or calculate it unnecessarily every update.

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.