Page 1 of 1

need help with 2d network game

Posted: Thu Feb 21, 2008 4:42 pm
by treebolt
I've made some 2d PureBasic games before, now Im trying to make one that works across a network.

I created a network server but when I try to use OpenScreen() or OpenWindowedScreen() the program crashes. Any thoughts / suggestions?

Posted: Thu Feb 21, 2008 5:43 pm
by Foz
Post up your example of what you're doing that causes a crash.

Posted: Thu Feb 21, 2008 5:46 pm
by treebolt

Code: Select all

If InitNetwork()
Else
  MessageRequester("Error!","Failed to initialize network!")
EndIf
OpenConsole()

port = 6878
If CreateNetworkServer(0,port)
  PrintN("Network Egg server started!")
  ExamineIPAddresses()
  PrintN("IP: "+IPString(NextIPAddress()))
Else
  PrintN("failed to create server! press Enter to terminate")
  crap.s = Input()
  End
EndIf

CloseConsole()
OpenScreen(800,600,32,"game") ;<<<<<<<<<<< This causes the crash

Repeat
  ClearScreen(RGB(0,30,90))
  FlipBuffers()
 
  ;-Server commands
  Nevent = NetworkServerEvent()
  Select Nevent
    Case #PB_NetworkEvent_Data
     ReceiveNetworkData(Connection,buffer,1000)
    Case #PB_NetworkEvent_Disconnect
      MessageRequester("Game Over","Player 2 give up!")
      End
  EndSelect

 ;-Keyboard Controls
 ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  Delay(1)
ForEver

Posted: Thu Feb 21, 2008 5:49 pm
by Kaeru Gaman
InitSprite() is missing.
but causing a crash? phew...

Posted: Thu Feb 21, 2008 5:50 pm
by Foz
Why on earth are you combining a console app and a window app in the same app?

Posted: Thu Feb 21, 2008 5:50 pm
by treebolt
ok thanks I did forget the initsprite and initkeyboard.... thanks and sorry for the wasteful thread

It was causing a crash, not an error.... wierd

The console was just to display the server setting up

Posted: Thu Feb 21, 2008 9:46 pm
by treebolt
I have another question.

when something like this happens

Code: Select all

*buffer = AllocateMemory(1000)
PokeS(*buffer,stringToBeSent)
SendNetworkData(ClientID,*buffer,1000)
how do I figure out how much memory I need to use for allocating / sending? if I use too much will it slow down the program?

Posted: Thu Feb 21, 2008 10:22 pm
by Foz
This is where you need to sit down and work out what *needs* to be sent, in as little as possible.

Basically, you need to send as little information as possible - but possibly at 60 frames per second, this quickly mounts up.

Take a multiplayer pacman game for instance. To display everything correctly, you need, per player, their position (x and y as word values - 4 bytes) and the orientation (up down, left and right - 1 byte). So that's a total of 5 bytes, every frame. At a locked 60 frames per second, then you are sending 300 bytes per second. Not a lot, I have to admit, but then that is what ONE person will be sending. Now lets up the scale to 10 people playing at the same time - that's now 3000 bytes of information being sent to the server every second.

Now what about the information of the rest of the opponents? Well, that will be 9 people's worth of information. So, 5 bytes * 9 people is 45 bytes. So every second, you will be receiving 45 x 60 = 2700 bytes per second. So that isn't a lot for what the client will be receiving.

BUT, the server will be doing most of the sending and receiving. We've already established that it's receiving 3000 bytes per second, but it then has to pump out 2700 bytes to each person - thats then 27,000 bytes every second.

So on modern hardware - not a lot. Heck you should be able to (in theory) use a modem for that. But that is a very simple game.

Now if you upgrade that to a shooter, then the math explodes. You need to cater for *every* bullet position, type, power, as well as the player position, instructions (raise shield, powerup, etc). If you worked all that into 100 bytes per frame, then that's 6000 bytes per second to send and 54,000 bytes to receive on the client. On the server, it would need to be able to handle receiving 60,000 bytes per second and to send 540,000 bytes every second.

Obviously at this rate you will be needing heavy duty servers. Or you start to trim back to what you need and let the clients work out what was done as much as possible. You have Ghz of processing power but limited bandwidth - so retreating to math projections would be far more useful when you are going to be sending a stream of bullets all going in the same direction. You just send each bullet as it appears and it's trajectory and then all the clients will then know that the bullet will be moving per frame in a particular speed and angle. This massively reduces traffic.

I hope this helps :)

Posted: Thu Feb 21, 2008 11:20 pm
by treebolt
These tips are great! Very useful for the program I am trying to create! =)

so what you are saying is, one character = 1 byte, so if i wanted to send a string I would use something like...

Code: Select all

*buffer = AllocateMemory(4)
PokeS(*buffer,"test")
SendNetworkData(ClientID,*buffer,4) ; << 4 letters in the word 'test'

Posted: Thu Feb 21, 2008 11:28 pm
by milan1612
treebolt wrote:These tips are great! Very useful for the program I am trying to create! =)

so what you are saying is, one character = 1 byte, so if i wanted to send a string I would use something like...

Code: Select all

*buffer = AllocateMemory(4)
PokeS(*buffer,"test")
SendNetworkData(ClientID,*buffer,4) ; << 4 letters in the word 'test'
You can use StringByteLength() for this,
and btw. "test" would be 5 bytes as PB strings
are zero-terminated.

Re: need help with 2d network game

Posted: Mon Nov 14, 2011 2:39 pm
by zefiro_flashparty
u need add

If InitSprite() = 0
MessageRequester("Error", "Can't open screen & sprite enviroment!", 0)
End
EndIf
If InitKeyboard() = 0
MessageRequester("Error", "Can't open keyboard!", 0)
End
EndIf
to create windows :p and activate keyboard

iff add this u see a blue screen

Re: need help with 2d network game

Posted: Thu Nov 17, 2011 1:59 pm
by Nituvious
These are really old topics.