Online game and player movement
I was wondering how should I do player movement in my 2D game? The worlds are topdown tile based maps. But I want the player movement not to be restricted to the tiles. If they only hold down the arrow key for a second the player should only move for a second. The player only has 4 directions he can go. Is there a better way...a less bandwidth consuming way? The game is only going to hold a max of 20-30 people.
This is what I'm thinking...
Player pressed arrow key
Client -> server "I want to start moving in x direction"
Server -> client "The next space is not being blocked, you can move at x speed"
Server keeps sending that to the client until he gets a message that: the player stopped moving, player has disconnected, or the next tile is blocked.
if the player lets go of the arrow key
client -> server "Ok, I'm done moving."
You're nearly there, but if you just transmit velocity (which is essentially what you're proposing) then you will get out of sync fairly quickly. A better approach is to transmit both position and velocity, both from the controlling client to the server, and from the server to all the other clients. Clients and servers can then (A) be sure that they're not hugely out of sync, and (B) do some primitive prediction, using the last known velocity and last known position to figure out the likely location of the object.
In your case you can most likely get away with only transmitting the velocity and position when the velocity changes. If you notice that you're getting out of sync a lot because of timing problems, you could also transmit a "heartbeat" position/velocity update every X seconds while the object is moving, just to make sure everyone's still in sync.
(In a more physics-heavy game you might also transmit acceleration, but that's probably not necessary in your case.)
You might end up with ugly "snapping" effects resulting from the prediction being slightly incorrect. You can fix this by gradually interpolating the player's position from "what we thought it was" to "what it is". There's a good article about the best way of doing this on gamedev.net, but it's down at the moment so I can't find it for you.
If you're worried about cheating (like packet editing your position to teleport and such), you should sanity-check the position data on the server end.
I'm getting UO flashbacks here
UO seemed to have a little bit of client prediction, sometimes you would be running and then stop, the server would correct your final location and you would get a bit of a jump to the next tile. with lag, you could tell it would let you take about 4 steps on the client side before it stopped you (waiting for server to come back or respond). which it would then 'rubber-band' you back to the last location the server had.
I'm thinking it might be easier with a point and click movement system, but im not sure how that would change your gameplay. Basically click your player and click a location, let pathfinding navigate him there. The server would send that player and location to the other clients and have them work it out client-side. You would probably need to make sure your pathfinding re-creates it the same way everytime for it to work..