[UDP]Mehr als 2 Spieler

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

N0X

Betreff: [UDP]Mehr als 2 Spieler

BeitragSo, Dez 27, 2009 22:33
Antworten mit Zitat
Benutzer-Profile anzeigen
Hi!
Ich 'hab mich mal wieder an das (für mich) leidige Thema Netzwerk gemacht.
Ich habe ein 2D-RPG und da einen UDP-Austausch der Koordinaten eingebaut.
Klappt soweit alles wunderbar, bis darauf das ich wenn sich mehr als 2 Spieler im Spiel befinden, und einer disconnected, alle anderen Clients die Meldung "Image does not exist" bekommen. (lokal getestet)
Ich denke es liegt daran, dass ich sobald ein Spieler rausgeht, das Spieler-Bild mit FreeImage lösche.
Wie kann ich das anders lösen?

Hier der Server Code (wohl eher uninteressant)
Code: [AUSKLAPPEN]
;SERVER-FILE

;UDP
Global timer=CreateTimer(40)

Type Player
   Field ip,port%
      Field x#,y#
End Type

Type Gegner
   Field x#,y#
End Type

Global svr = CreateUDPStream(7142)
Global write_udp  = CreateUDPStream(7143)
Global send_milli#,read_milli#,readsvr,readav,rbyte,plycount,maxply

While Not (KeyHit(1))
   WaitTimer timer
   
   UDP()
Wend
   CloseUDPStream(write_udp)
   CloseUDPStream(svr)
End


Function UDP()
      readsvr = RecvUDPMsg(svr)
      If readsvr <> 0 Then
         readav = ReadAvail(svr)
         If readav <> 0 Then
            port = UDPMsgPort(svr)
            ip = UDPMsgIP(svr)   
            rbyte = ReadByte(svr)
         Select rbyte
               Case 1
              name$ = ReadString(svr)
                  For p.Player = Each Player
                     WriteByte svr,1
                     WriteInt svr,ip
                     WriteInt svr,port
                     SendUDPMsg svr,p\ip,p\port
                     WriteByte svr,1
                     WriteInt svr,p\ip
                     WriteInt svr,p\port
                WriteString svr,name$
                     SendUDPMsg svr,ip,port
                  Next
                  p.Player = New Player
                  p\ip$ = ip
                  p\port = port
                  Print "Player Connected... "+name$
               Case 2
                  For p.Player = Each Player
                     If ip = p\ip And port = p\port Then
                        Delete p
                     End If
                  Next
                  For p.Player = Each Player
                     WriteByte svr,2
                     WriteInt svr,ip
                     WriteInt svr,port
                     SendUDPMsg svr,p\ip,p\port
                  Next
                  Print "Player Disconnected..."
               Case 3
            While ReadAvail(svr)
                  For p.Player = Each Player
                     If p\port = port And p\ip = ip Then
                        p\x = ReadFloat(svr)
                        p\y = ReadFloat(svr)
                     End If
                  Next
            Wend
            End Select
           
         End If
      End If
      read_milli = MilliSecs()+64

   If send_milli < MilliSecs() Then
      For s.Player = Each Player
         For p.Player = Each Player
            If p\port <> s\port Then
               WriteByte svr,3
               WriteInt svr,s\ip
               WriteInt svr,s\port
               WriteFloat svr,s\x
               WriteFloat svr,s\y
               SendUDPMsg svr,p\ip,p\port
            End If
         Next
      Next
      send_milli = MilliSecs()+64
   End If
End Function


Und hier der Client Code
Code: [AUSKLAPPEN]
Type player
   Field ip,port
   Field gfx
   Field x#,y#
End Type

Global timer=CreateTimer(80)

Graphics 640,480,32,2
SetBuffer BackBuffer()

SeedRnd MilliSecs()

;UDP
Global i_udp = CreateUDPStream()
Global o_udp = CreateUDPStream(Rnd(100,8000))

If Not (i_udp Or o_udp) Then
   RuntimeError("Fehler im Netzwerk!")
EndIf

Global ppx=10*32,ppy=7*32,show_radius=100
Global py=7,px=10
Global scrolly,scrollx
Global send_milli#,read_milli#,readsvr,readav,rbyte

Global char    = LoadImage("char.bmp"):MaskImage char,255,0,220
Global chha    = CopyImage(char):MaskImage chha,255,0,220
Global Tileset = LoadAnimImage("tileset.bmp",32,32,0,6)

Dim map(40,40)

load()

Global Ip% = INT_IP(Input("IP: "))
name$ = Input("Name; ")

WriteByte o_udp,1
WriteString o_udp,name$
SendUDPMsg o_udp,Ip,7142

While Not KeyHit(1)
   WaitTimer(timer)

   Cls

   UDP()

   For x = 0 To 40
      For y = 0 To 40
         DrawBlock tileset,x*32-scrollx,y*32-scrolly,map(x,y)
      Next
   Next

   DrawImage char,ppx-scrollx,ppy-scrolly
   ;Oval ppx,ppy,show_radius,show_radius,0
   For p.Player = Each Player
         DrawImage p\gfx,p\x#-scrollx,p\y#-scrolly
      Next

   Text 0,0,py
   Text 0,20,px

   If move = 1 Then
      If aa > 0
         scrolly = scrolly + 2
         ppy = ppy + 2
         xo = xo + 1
         If xo = 16 Then aa = 0:py = py + 1:xo = 0
      EndIf
   EndIf

   If move = 2 Then
      If aa > 0
         scrolly = scrolly - 2
         ppy = ppy - 2
         xo = xo + 1
         If xo = 16 Then aa = 0:py = py - 1:xo = 0
      EndIf
   EndIf

   If move = 3 Then
      If aa > 0
         scrollx = scrollx - 2
         ppx = ppx - 2
         xo = xo + 1
         If xo = 16 Then aa = 0:px = px - 1:xo = 0
      EndIf
   EndIf

   If move = 4 Then
      If aa > 0
         scrollx = scrollx + 2
         ppx = ppx + 2
         xo = xo + 1
         If xo = 16 Then aa = 0:px = px + 1:xo = 0
      EndIf
   EndIf

   If KeyDown(205) Then
      If map(px+1,py) < 1 Then
         If aa < 1 Then
            move = 4
            aa = 1
         EndIf
      EndIf
   EndIf

   If KeyDown(203) Then
      If map(px-1,py) < 1 Then
         If aa < 1 Then
            move = 3
            aa = 1
         EndIf
      EndIf
   EndIf

   If KeyDown(200) Then
      If map(px,py-1) < 1 Then
         If aa < 1 Then
            move = 2
            aa = 1
         EndIf
      EndIf
   EndIf

   If KeyDown(208) Then
      If map(px,py+1) < 1 Then
         If aa < 1 Then
            move = 1
            aa = 1
         EndIf
      EndIf
   EndIf

   Flip 0
Wend
   WriteByte o_udp,2
   SendUDPMsg o_udp,Ip,7142
   FreeImage tileset
   FreeImage char
   CloseUDPStream(i_udp)
   CloseUDPStream(o_udp)
End

Function load()
   karte$ = ReadFile("map1.map")
   For x = 0 To 40
      For y = 0 To 40
         tile = ReadLine(karte$)
         map(x,y) = ReadInt(karte$)
      Next
   Next
   CloseFile(karte$)
End Function

Function UDP()
   readsvr = RecvUDPMsg(o_udp)
     If readsvr <> 0 Then
         readav = ReadAvail(o_udp)
        If readav <> 0 Then
            rbyte = ReadByte(o_udp)
            Select rbyte
               Case 1
                  ;con
                  p.Player = New Player
                  p\ip = ReadInt(o_udp)
                  p\port = ReadInt(o_udp)
                  p\gfx = chha
              name2$  = ReadString(o_udp)
              ;p\id = ReadInt(o_udp)
               Case 2
                  ;dis
                  rIp = ReadInt(o_udp)
                  port = ReadInt(o_udp)
                  For p.Player = Each Player
                     If p\ip = rIp And p\port = port Then
                        FreeImage p\gfx
                        Delete p
                     End If
                  Next
               Case 3
            While ReadAvail(o_udp)
                  rIp = ReadInt(o_udp)
                  port = ReadInt(o_udp)
                  For p.Player = Each Player
                     If p\ip = rIp And p\port = port Then
                        p\x# = ReadFloat(o_udp)
                        p\y# = ReadFloat(o_udp)
                     End If
                  Next
            Wend
            End Select
           
        End If
     End If
     read_milli = MilliSecs()+64

   ;Send
   If send_milli < MilliSecs() Then
      WriteByte o_udp,3
      WriteFloat o_udp,ppx
      WriteFloat o_udp,ppy
      SendUDPMsg o_udp,Ip,7142
      send_milli = MilliSecs()+64
   End If
End Function

Function INT_IP(IP$)
   a1=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
   a2=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
   a3=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
   a4=Int(IP$)
   
   Return (a1 Shl 24) + (a2 Shl 16) + (a3 Shl 8 ) +a4
End Function


Mfg,
N0X

//EDIT:
Man bin ich doof, einfach weglassen Rolling Eyes
Kann geclosed werden..
Projekte: |Tibario| http://www.blitzforum.de/worklogs/325/ | 5%

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group