Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Game Server Structure  (Read 19418 times)

0 Members and 1 Guest are viewing this topic.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Game Server Structure
« on: October 18, 2014, 11:04:06 am »
Hello, after long time  ;D .

I want help because i've got problem with my game-server code structure (c++) VS2013.

#I'm getting quite pissed off when:
 - i want to let ent/npc get some data from map,
 - exchanging data between two maps

@ because the structure looks like this:

Main - calls funtion Server::run() to prepare an initialize enviroment.
   |
Server - everything what this does is controling FPS and calls MapMgr::update()
   |
MapMgr - there is std::map<enum, std::unique_ptr<Map>> to store maps & updates them.
   |
Map - std::deque<std::unique_ptr<EntActor>> storing npcs & in update() calling ptr -> update();
   |
EntActor - whole interface, move, goto, health, shield, speed .....

EntActor::update()
moves/rotates entity to specified location, or try attack any near player by data from Map.

and this whole system is one-way, should i make some sort of a global-message-bus ?
Please tell me if you have better idea :) .

# Problem begins when i want to port/move EntActor from map1 to map2.
« Last Edit: October 18, 2014, 11:47:38 am by SDH »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Game Server Structure
« Reply #1 on: October 18, 2014, 11:34:14 am »
So you really think these few keywords will explain everything to us?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #2 on: October 18, 2014, 11:35:26 am »
okay, i'll try to "explain" more

EDIT: i hope it's better now :) .
« Last Edit: October 18, 2014, 11:49:32 am by SDH »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: Game Server Structure
« Reply #3 on: October 18, 2014, 05:21:46 pm »
A simple solution could be to access the map directly - for example, by creating a Service Locator as a singleton, which would hold a pointer to the map (or an array of maps).

Also, I have two questions after looking at how you store your maps and entities.
  • Why are you using std::map for storing maps? Your keys are enum values, which can be directly casted to integers - you could just use std::vector, resized for the total amount of enum values and then just access the maps directly with the access operator. This way you could get rid of the overhead of using the hashing algorithm every time you access the element in the map (although, in case of integers it's negligible... but still).
  • Is there a particular reason to store entities in std::deque? Are you adding or removing the entities from the front? If not, then using std::vector instead would have less overhead too, and would be more efficient for CPU cache.
« Last Edit: October 18, 2014, 05:26:26 pm by Xornand »

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #4 on: October 19, 2014, 11:31:15 am »
Xornand: thanks :) it helped a lot.

And that std::vector, well i never used it because it reallocate everything when added something new but when i read your post then i reallized this in my mind :D : (f**k, i use only static amount of maps and npcs....., he's right)  ;D
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Game Server Structure
« Reply #5 on: October 19, 2014, 05:14:29 pm »
You should always use std::vector unless you have a reason for a different data structure. It performs well in a wide variety of scenarios.

std::array is a possibility if the number of elements is known at compile time, such as the number of enumerators in an enum. You can then use the idiom of a separate Count enumerator to avoid magic constants:
namespace Scope
{
    enum MyEnum
    {
        Enumerator1,
        Enumerator2,
        ...
        Count // <- always last one
    };
}

std::array<MyElem, Scope::Count> array;

Alternatively, you can use C++11 scoped enums (enum struct or enum class), but then you need an explicit type cast to integers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #6 on: October 20, 2014, 06:29:28 am »
I've already done this couple of minutes ago before i read your post :D :

managerMaps.cpp
Maps::Maps()
{
        m_maps.reserve(global::MAP::MapsAmount);
}

m_maps is a std::vector

but thanks :) .
« Last Edit: October 20, 2014, 06:33:00 am by SDH »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Game Server Structure
« Reply #7 on: October 20, 2014, 07:55:37 am »
You need a form of persistance to keep data from the server stored between sessions. When you change map, generally you will be starting a new local server which (obviously) will not have the same variables and stuff as the previous one. This is easily solved by writing the relevant persistant data (health, armour, weapons, etc) to a file before the map changes, then reading it when the next map is loaded.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #8 on: October 20, 2014, 04:11:23 pm »
It's not "local" game, it's multiplayer, so maps are paused except map/s where is/are player/s.
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Game Server Structure
« Reply #9 on: October 20, 2014, 09:19:44 pm »
Its still the same thing. You save persistent data, close the server, open a new one, load the new map, load the persistent data. This is how most games do it like Half Life and Counter Strike (even Quake did it this way).

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #10 on: October 20, 2014, 09:56:50 pm »
i'm not quite sure what do you want to tell me with that :D
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Game Server Structure
« Reply #11 on: October 21, 2014, 08:44:07 am »
I'm simply pointing out that its a common method and it works. You could even use a database depending on which way you want to go but a file is probably safer and maybe faster in the long run.

Simply put, persistent data is data that will not change between sessions. If you have 10 health and leave the game, restart the server or change the map, when you come back to that game you will still have 10 health.

I'm suggesting that you do it the way that Quake and Valve's games do it which works and its a simple approach.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #12 on: October 21, 2014, 09:10:39 am »
i dont use any DB/File, intead i use computer memory:

databaseNPC, databaseMap: (example)
enum iType
{
        A = 0,
        B,
        C
}

struct iStruct
{
        int a;
        int b;
        int c;
}

static std::map<enum, const iStruct> m_db;

funcs:
static void initialize(); // to add all npcs .or. maps to db
static const iStruct get(const enum);
 

i use this for npcs & maps that's the reason of std::map
« Last Edit: October 21, 2014, 09:15:08 am by SDH »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: Game Server Structure
« Reply #13 on: October 21, 2014, 09:54:03 am »
I think you didn't understand what Gambit was talking about. Storing everything in RAM doesn't make your data persistent - it'll be gone the moment your std::map is destroyed (either by going out of scope or by closing your application, for example due to a server reboot).

Now, depending on your game, this may be or not a desired behavior. If your game is like an MMO or needs to keep players' stats, then you'll want state persistence, so saving the game state to a file or a database is the way to do it. However, if each multiplayer session is unique and you don't care about tracking any information about the players (and thus everyone always starts from the beginning and the entire map is reloaded every time a new game is hosted), then I guess you could get away without the database.

From your vague description, it seems like you're making a persistent world (at least that's the impression I got), so if that's the case, you'll need some persistent storage for storing your persistent data and storing it in RAM doesn't solve the problem.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Game Server Structure
« Reply #14 on: October 21, 2014, 10:46:40 am »
now i understand.

Well, static things like Hitpoints,Shield,Wapons are in application/ram, and (as you said) stats will be in database (persistent)
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.