Short update, I am now using a socket connection for updating player positions. I can then send only 6 bytes for positions instead of 16. X posititons are always < 1280 and Y positions are always < 720. In theory I could have X and Y positions up to 256 * 256 without combining X and Y byte 1.
TCP packets:
Packet operation, 4 bytes
Player X integer, 4 bytes
Player Y integer, 4 bytes
Player direction, 4 bytes
= 16 bytes
Socket connection:
No packet operation ID, socket is only used for positions
Player number byte (4 players)
Player X byte 1 (player X in range 0 to 255 = 0, player X in range 256 to 511 = 1, player X in range 512 to 767 = 2, and so on)
Player X byte 2(player X in range 0 to 255 = player X byte 1 = player X - 128, player X in range 256 to 511 = player X byte 2 = player X - 256 - 128 and so on), this will send a byte in the range -128 to 127.
The same with Y as with X positions.
Player Y byte 1
Player Y byte 2
Player direction byte (8 directions, angle 0 = 0, angle 45 = 1, angle 90 = 2, and so on)
= 6 bytes
I can further compress it by combining player number and player direction into 1 byte and player X byte 1 and player Y byte 1 into 1 byte.
The send code:
if player_moved
player_moved = 0
if player_x <= 255
player_x_1 = 0
player_x_2 = player_x - 128
endif
if player_x > 255 and player_x <= 511
player_x_1 = 1
player_x_2 = player_x - 256 - 128
endif
if player_x > 511 and player_x <= 767
player_x_1 = 2
player_x_2 = player_x - 512 - 128
endif
if player_x > 767 and player_x <= 1023
player_x_1 = 3
player_x_2 = player_x - 768 - 128
endif
if player_x > 1023 and player_x <= 1279
player_x_1 = 4
player_x_2 = player_x - 1024 - 128
endif
if player_y <= 255
player_y_1 = 0
player_y_2 = player_y - 128
endif
if player_y > 255 and player_y <= 511
player_y_1 = 1
player_y_2 = player_y - 256 - 128
endif
if player_y > 511 and player_y <= 767
player_y_1 = 2
player_y_2 = player_y - 512 - 128
endif
`make packet with player position data
SendSocketByte(socket_connection,player_number)
SendSocketByte(socket_connection,player_x_1)
SendSocketByte(socket_connection,player_x_2)
SendSocketByte(socket_connection,player_y_1)
SendSocketByte(socket_connection,player_y_2)
SendSocketByte(socket_connection,player_direction_1)
FlushSocket(socket_connection)
endif
Recieve code:
opponent_player_number = GetSocketByte(socket_connection)
opponent_player_x_1 = GetSocketByte(socket_connection)
opponent_player_x_2 = GetSocketByte(socket_connection)
opponent_player_y_1 = GetSocketByte(socket_connection)
opponent_player_y_2 = GetSocketByte(socket_connection)
opponent_player_direction = GetSocketByte(socket_connection)
opponent_player_x = opponent_player_x_2 + (256 * opponent_player_x_1) + 128
opponent_player_y = opponent_player_y_2 + (256 * opponent_player_y_1) + 128
opponent_player_direction_1 = 45 * opponent_player_direction
EDIT: Lag is noticeable less now. The smallest data type I can send with the multiplayer messages is a 4 byte integer, and with sockets I can send 1 byte.
13/0