Dear forum members,
I have a problem with my multiplayer game and since people on these forums seem to know everything, I decided to try my luck here, and hope someone can help me with it.
I'm trying to create a simple 2p multiplayer RTS game using DarkBasic Pro.
The code works as follows: first a pier to pier connection is set up between the two computers, then 2 objects per person are drawn to the screen, the X,Y,Z and a health stat of these objects are known to both computers. You can move the objects with the mouse, if your object gets close enough to enemy objects, they start to damage each other.
When an ojbects health gets below (or at) 0 the program will remove the object.
Simplified version of the main loop of the code here:
Do
Gosub _Controls
Gosub _AI
XRotate camera 90
Position camera CX#,CY#,CZ#
Gosub _Sync
For n = 1 to MaxObject
If Object exist(n) = 1
If ObjHealth(n) <= 0
Delete object n
SelectObj(n) = 0
Endif
Endif
Next
rem Note that the enemy object number in this case is n (a 3 or 4) while the EnemyHealth stat belonging to those objects is n - MaxObject (a 1 or 2)
For n = MaxObject + 1 to MaxObject + MaxEnemy
If EnemyHealth(n - MaxObject) <= 0 then Delete object n
Next
Sync
Loop
_Sync:
Rem This part will send the object X and Z position of all of your objects, and the health stat of both enemy objects
For n = 1 to MaxObject
If object exist(n) = 1
Send net message float 0 , Object position x(n)
Send net message float 0 , Object position z(n)
endif
next
For n = MaxObject + 1 to MaxObject + MaxEnemy
If object exist(n) = 1
Send net message integer 0 , EnemyHealth(n - MaxObject)
Endif
Next
Rem This part will recieve those messages send by the other computer, which are the X and Z of both enemy objects, and the health stats of both your objects.
For n = 1 + MaxObject to MaxObject + MaxEnemy
If Object exist(n) = 1
Repeat
Get net message
EnemyX# = Net message float()
Until EnemyX# > 0
Repeat
Get net message
EnemyZ# = Net message float()
Until EnemyZ# > 0
Position object n , EnemyX#, object position y(n) , EnemyZ#
EnemyX# = 0
EnemyZ# = 0
Endif
next
For n = 1 to MaxObject
If Object exist(n) = 1
Repeat
Get net message
Sub = net message integer()
Until Sub <> 0
ObjHealth(n) = Sub
Sub = 0
Endif
Next
If Net message exists() = 1
loops = 0
Repeat
Get net message
Until Net message exists() = 0
Endif
Return
This code works fine, if an object is moved on one screen, it will also move on the other screen, until one of the health stats gets below(or at) 0. If it happens, the object should be deleted at both computers at the same time, however this doesn't happen, and the result is the computer waiting for information it will never recieve and thus a deadlock occurs.
If anybody knows what exactly the problem is (it appears the healthstats of both computers is not completely in sync), or knows how to fix it, any help is much appreciated.
Gaia