# How to reduce the CPU usage during the state preservation phase in my MMO?

I'm writing small MMO using Boost library. I want to make it non-target game, but I'm having trouble saving the previous game state. I don't know how to implement the game world, NPCs', and players' states. Currently, I'm copying all my NPCs, players and map in main game cycle, but it consumes too much CPU usage, about 60-80%. Obviously it too much.

Example of code in my save game method:

void GameHistory::saveGame(game* g)
{
if (player_history.size() == 64) {
player_history.erase(player_history.begin());
}
if (map_history.size() == 64) {
map_history.erase(map_history.begin());
}
if (npc_history.size() == 64) {
npc_history.erase(npc_history.begin());
}
long long t = lib::GET_SERVER_TIME();
players_obj_hash players;
game::players_hash::iterator itp = g->__players_by_id__.begin();
game::players_hash::iterator endp = g->__players_by_id__.end();
for (; itp != endp; itp++) {
players[itp->second->getId()] = (*itp->second);
}
player_history[t] = players;
map_history[t] = g->map;

npcs_obj_hash npcs;
game::npc_hash::iterator it = g->npcs_by_id.begin();
game::npc_hash::iterator end = g->npcs_by_id.end();
for (; it != end; it++) {
npcs[it->second->getId()] = (*it->second);
}
npc_history[t] = npcs;
}


I'm using map as grid, where every NPC or player "register" on which cell it's on for fast lookup of nearest entities.

Maybe there is another more light approach to save the game state?

• MMOs usually don't have lag compensation - MMO players are usually familiar with lag issues: likely because Blizzard do nothing for it in WoW. Are you sure you are not solving a non-problem? – Jonathan Dickinson Feb 21 '12 at 12:15
• I have though about it, but my game is more like action MMO game, players will cast non target spells, and fight by casting many projectiles (bullets, mini spells etc.) Is it ok that it will be no lag compensation? Wow is target-based game and it can use no lag compensation at all. – Denis Ermolin Feb 21 '12 at 12:27
• @DenisErmolin: so you would be doing something like spiral-knights or magicka? – o0'. Feb 21 '12 at 12:42
• Yeah more like magicka, more action without focusing target spells – Denis Ermolin Feb 21 '12 at 12:48
• @JonathanDickinson - Why do you say MMOs don't usually use lag compensation? WoW obviously has at least some position interpolation (haven't you ever seen someone "lag out" and everyone else sees them running in circles?), and so have all three commercial MMO codebases I've worked on/with. – justinian Feb 21 '12 at 18:59

Additionally, you can be intelligent about which entities you choose to lag compensate. If you know your skill has a maximum range of x and players can move at a maximum speed of y then don't bother lag compensating any entities farther than x+y*dt distance away, since you know that they won't be hit by the skill.