How to make Node.js update world in a MUD or MMORPG

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

How to make Node.js update world in a MUD or MMORPG

Post by Jackolantern »

This is something that has been bugging me about using Node for MUD or MMO type games. You need a lot of things happening in the background to actually run the world, handle basic AI, etc. But the fully asynchronous style of Node seems to be a difficult fit with this. Of course, anything having to do with events, such as the players making any kind of input, will work no problem and be easy to implement. But what about enemies attacking players first, moving enemies around, and others? There have been a few things suggested, but I see problems in each one:

1. Suggestion: Just require() in the code that controls the world to run it in the same process. Problem: This is going to steal a lot of CPU cycles from the event loop. Ryan Dahl suggested anything happening in the event loop should not take more than 2ms to complete. I can't guarantee iterating through every room and every mob and acting on them is going to fit under that time.

2. Suggestion: Using setInterval() to continuously call the world-updating code. Problem: Essentially the same as #1. Plus, you want the world updating code to run essentially in a loop, but you would essentially be guessing on how long to make the timer to make it run in a virtual loop while leaving enough time between cycles to handle all event listeners.

3. Suggestion: Use Web Workers to run the world updating in another thread. Problem: The other thread cannot access the same in-memory values as the main thread because that threatens thread safety. And it could be even worse if you were accessing a database rather than in-memory values, since db access is even slower, basically setting you up to corrupt the state of your game.

4. Suggestion: Create a separate Node program to run the world updating. Problem: This is essentially the same thing as #3 if they are on different threads or processes, or #1 or #2 if they are on the same process.

So what other way is there to do it, or are there ways to fix the above suggestions? Thanks!
The indelible lord of tl;dr
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by OldRod »

I haven't worked with Node yet... only watched some tutorial videos... so I probably have no idea what I'm talking about here (but I will talk anyway :lol: )

With Node, don't you have a socket server that would send updates to client as needed? So if there was a world change near a player, the server would notify him automatically... then the client just needs a way to handle that update as needed. Or am I way off?
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by Jackolantern »

The problem isn't in notifying the player (that is extremely easy with Node), but rather, the issue is in how your program knows that an update is needed to be processed. Most traditional game servers have a loop that runs constantly through all of the world updating routines, and then all of the player-specific stuff, such as sending and receiving data to clients, happen on separate threads. This design doesn't work in Node. So I am kind of stumped on the "Node way" to do this :P Thank you though!
The indelible lord of tl;dr
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by OldRod »

Hmmm... doesn't the server have a listen function? I've seen code like this example on http://nodejs.org/

Like I said, I've never tried using it, but I remember seeing stuff like this in the tutorials I've looked at and it made me think it would be fairly easy.

The client does an action... that action gets sent to the server. The server is listening on that port, gets the action, processes it, then sees what other clients it affects, so it knows who to notify.

Or am I not understanding the problem?
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by Jackolantern »

I had actually never seen that. Thanks! :)

However, the issue isn't about responding to things the player does. That is all very easy (and that is also as far as every tutorial I have seen goes; none of them implement mob AI). The real problem is making things happen without player interaction. For example, even if every player on the game server went AFK, mobs still need to move around, aggressive mobs need to attack unwilling players, mobs need to respawn after so long, any over-time effects need to tick, etc. There is a lot of up-keep going on in a game server, and that is typically what is done in the main loop of a server. However, that isn't possible in node by any way I know.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by hallsofvallhalla »

If a tree falls in the forest no one hears it.....

I do not see a point in bring a world to life if there is no life there to see it. Let the players notify the server that they are in this room and the server then fires up the room. The loop can run but only fires the data if it is needed. Mob places can be tracked by time past and positioned when someone enters the room.

On another note I too was scared of the amount of data being sent with all the mobs and players to account for. The object is to cut the data to the smallest amount possible. Let the client unload it all but Node really can throw a ton of data around.

Using simple strings and simple rules so you can populate a map with little overhead.... use IDs to name creatures then have the client pull the data behind the creature. 3/10/100/200 would be a creature ID of 3 with HP of 10 in location of X100, Y100...the rest is pulled by the client.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by Jackolantern »

I guess what I am wondering though, is what happens when players stop doing things, stop flipping events, but they are still there, logged in? Maybe they are reading something, or talking to a RL friend in the room. At some point surely in a MUD or MMO you will want something to happen to the player, and not always just react to them. I would love to allow mobs to move around, from room to room on their own, and attack players they find, even if no player-driven events are firing. But how can I do this in Node?
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by hallsofvallhalla »

the server will store the players location regardless if no events are firing. I would think you would have mobs check location routinely with an array of players locations. The player only sends data when it does something but your array stack on the server side will still hold its data until it is deleted from the sockets disconnect function.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make Node.js update world in a MUD or MMORPG

Post by Jackolantern »

Oh, I know they would still be there even if no events are firing. Rather, I want the game world to keep moving even if the players aren't sending events. Adapting the "on page view" method of faking world activity when the players react I don't think will work, since, unlike in PHP games, the players can still be watching the world behave even when they aren't doing anything (in stark contrast to PHP games where the player's window into the game world is closed when they aren't submitting pages). This seems to be a problem in Node. I believe someone, somewhere has tackled it due to the usage of Node as a full-blown commercial MMORPG server.
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”