Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MMORPG development and server side logic, update all Game Objects?

Tags:

c#

.net

I have designed Multiplayers Games before, but now I wanted to create an MMORPG Architecture for learning/challenge purpose. I want to go as far as simulate hundreds(or a couple thousands) of concurrent players on a single server.

So far so good except that right now I am facing a problem to figure out a good way to update all game objects on the Server as often and as fast as possible.

By Game Objects I mean all Players, Mobs, Bullets.

The problem is that all Players, Mobs, Bullets are stored in a Collections on the Server Side Memory for faster processing, and iterating throught all of them to check for collisions, update health, update movement, etc... is taking way too long.

Lets say I have 1000 players and there is 10000 mobs in the whole world and all players and creatures are responsible for creating 5 other game objects(no more no less) such as bullets.

That would give (1000 + 10000) * 5 = 55000 game objects in a collection.

Iterating throught all objects, to update them, take forever(a couples minutes) on a Dual-Core HT i5 with 4gb RAM. This is wrong.

When iterating, the code look like this(pseudo):

for(int i = 0; i < gameobjects.Count; i++) {
  for(int j = 0; j < gameobjects.Count; j++) {
      // logic to verify if gameobjects[i] is in range of
      // gameobjects[j]
  }
}

As an optimization, I am thinking about dividing my game objects in different zones and collections but that wouldn't fix the problem where I need to update all objects several times per seconds.

How should i proceed to update all game objects on the server side? I did heavy search to find interesting game design patterns but no results so far. :(

Thanks in advance!

like image 395
SlashJ Avatar asked Apr 28 '13 14:04

SlashJ


2 Answers

I would change the design completely and implement an event base design. This has many advantages, the obvious one is that you will only need to update the objects that are actually being interacted with. As you will always have a majority of the game objects in an MMO game being idle, or not seen at all.

There is no reason why you should calculate objects that are not visible on any players screen. That would be insane and require a server farm that you most likely cannot afford. Instead you can try to predict movement. Or store a list of all objects that are currently not interacted with and update these less frequently.

If a player can't see an object you can teleport the unit over large distances instead of having it travel smoothly. Essentially moving the unit over huge distances within the confined area that the object is allowed to move. Making it look like the object is moving freely even when the object is not visible to the players. Usually this would be triggered as an event when a new player enters, or leaves a zone.

You can achieve this by simply calculating the time since the last update and predict how far the object would have traveled, as if it was visible to the player. This is especially useful for objects or NPCs that has a set route, as it makes the calculations much simpler.

like image 65
eandersson Avatar answered Oct 21 '22 10:10

eandersson


Your code is running that slow, not just because it is checking all N objects, but because it checking all possible interactions of objects, and that takes N^2 calculations = 3 025 000 000 in your sample.

One way to reduce this number of checks would be to put the object in your game world into a grid, so that objects that are not in the same or aligned cells cannot interact with each other.

Also, your current code checks each interaction twice, you can easily fix this by starting loop from i in your inner cycle:

for(int i = 0; i < gameobjects.Count; i++) 
  for(int j = i; j < gameobjects.Count; j++) 
like image 2
alex Avatar answered Oct 21 '22 10:10

alex