udp server client game

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

MrJoney

Betreff: udp server client game

BeitragDo, Mai 24, 2007 20:26
Antworten mit Zitat
Benutzer-Profile anzeigen
hallo leute ich bins mal wieder habe diesen code gefuden und wollte mal fragen

1. ob es jemanden gib bei euch der das programm getrentschreiben kann also eine datei in 2d wie wow und eine nur mit dem spiel

2. ob eseinen 3d moddeler gib nur für terains

3. ob es eine möglichkeit gib den ping abzufragen mitgrün root und so das von bin 1 bis 100 grünes feld ist bei ping 100 bis 150 gelb von 150 bis 250 orage und von 250 bis 1000 rot das wäre cool Smile

so hir der code Code: [AUSKLAPPEN]
;

SeedRnd MilliSecs()
Type player
Field x#,y#,z#
Field name$,net_id$
Field ent,cam,piv
End Type

loca = Input("Local Game?(1 - Yes,0 - No) ")
host = Input("Hosting Or Joining:(0 = Join,1 = Host) ")
If host = 0 And loca  = 0
ip$ = Input("Select IP: ")
port = Input("Select Port: ")
ElseIf host = 0 And loca = 1
port = Input("Select Port: ")
ip = ""
ElseIf host = 1 And loca = 0
ip = ""
port = 80
ElseIf host = 1 And loca = 1
ip = ""
port = 80
EndIf

Include "nb.bb"
Global mainstream = startnbgame(port)
If mainstream = 0 Then RuntimeError("Port Occupied") : End

name$ = Input("Enter Name: ")

Graphics3D 640,480,0,2
Global level = LoadMesh("test3d.b3d")
EntityFX level,1 : MoveEntity level,1,-3,0 : ScaleMesh level,5,5,5
CreateLight()

EntityType level,2

Global play.player
Global mastertime ;This timer is so that all the positions are right
id$ = CreatenbPlayer( name$,ip,mainstream,host )
play = createplayer(name,id)
If host = 0
For n.nbplayer = Each nbplayer
If n\id <> nbuserid
pack$ = play\x+" "+play\y+" "+play\z
SendNBMsg(1,pack,n\id,mainstream)
p.player = createplayer(n\name,n\id)
FreeEntity p\cam
EndIf
Next
EndIf

Collisions 1,2,2,2

Main()

Function Main()
mastertime = MilliSecs()
While Not KeyHit(1)
If KeyDown(17) ;Forward
MoveEntity play\ent,0,0,.5
SendNBMsg(2,"1",0,mainstream)
ElseIf KeyDown(31) ;Back
MoveEntity play\ent,0,0,-.5
SendNBMsg(2,"2",0,mainstream)
EndIf

If KeyDown(30) ;Left
TurnEntity play\ent,0,4,0
SendNBMsg(2,"3",0,mainstream)
ElseIf KeyDown(32) ;Right
TurnEntity play\ent,0,-4,0
SendNBMsg(2,"4",0,mainstream)
EndIf
If KeyDown(57) ; Sprung
MoveEntity play\ent ,0,0.8,0
SendNBMSg(2,"5",0,mainstream)
EndIf

TranslateEntity play\ent,0,-.2,0

dx#=EntityX( play\piv,True )-EntityX( play\cam,1 )
dy#=EntityY( play\piv,True )-EntityY( play\cam,1 )
dz#=EntityZ( play\piv,True )-EntityZ( play\cam,1 )
TranslateEntity play\cam,dx*.1,dy*.1,dz*.1
PointEntity play\cam,play\ent

If KeyHit(2)
DebugLog(EntityX(play\ent))
DebugLog(EntityY(play\ent))
DebugLog(EntityZ(play\ent))
EndIf

UpdateNetwork()

UpdateWorld
RenderWorld

play\x = EntityX(play\ent) : play\y = EntityY(play\ent) : play\z = EntityZ(play\ent)

If MilliSecs() - mastertime > 15000
pack$ = play\x+" "+play\y+" "+play\z
SendNBMsg(1,pack,0,mainstream)
mastertime = MilliSecs()
EndIf

For p.player = Each player
CameraProject(play\cam,EntityX(p\ent),EntityY(p\ent),EntityZ(p\ent))
If ProjectedZ() > 0 Then Text(ProjectedX(),ProjectedY(),p\name)
Next
Flip
Wend
deletenbplayer(mainstream)
Stopnbgame(mainstream)
End
End Function

Function CreatePlayer.player(name$,id$)
spawn = Rand(1,4)
p.player = New player
p\x = 0 : p\y = 0 : p\z = 0
p\name = name$
p\ent = CreateSphere():UpdateNormals p\ent
cone = LoadMesh("hanfopa.3ds") : RotateMesh(cone,270,180,0)
PositionMesh cone,0,-4,1 : ScaleMesh cone,0.2,0.2,0.2 : AddMesh cone,p\ent
FreeEntity cone
p\cam = CreateCamera()
p\piv = CreatePivot(p\ent) : MoveEntity p\piv,0,5,-10
EntityColor p\ent,Rnd(255),Rnd(255),Rnd(255)
EntityType p\ent,1:EntityType p\cam,1
p\net_id = id
If spawn = 1
p\x = 0 : p\y = 0 : p\z = 0
ElseIf spawn = 2
p\x = 0 : p\y = 0 : p\z = 0
ElseIf spawn = 3
p\x = 0 : p\y = 0 : p\z = 0
ElseIf spawn = 4
p\x = 0 : p\y = 0 : p\z = 0
EndIf
PositionEntity p\ent,p\x,p\y,p\z : PositionEntity p\cam,p\x,p\y,p\z
ResetEntity p\ent : ResetEntity p\cam
Return p
End Function

Function UpdateNetwork()
While RecvNBMsg(mainstream)
   Select NBMsgType(mainstream)
      Case 100 ;New Player
         serverid$ = converttoid(serverip,serverport)
         If nbuserid = serverid
         checknbdatabase(mainstream) ;Make sure the host only does this
         ElseIf nbuserid <> serverid ;If you are not the server
         i.nbplayer = New nbplayer
         i\name = nbmsgdata(mainstream)
         i\ip = nbmsgip(mainstream)
         i\port = nbmsgport(mainstream)
         i\id = converttoid(i\ip,i\port)
         EndIf
         p.player = CreatePlayer(nbmsgdata(mainstream),nbmsgfrom(mainstream))
         FreeEntity p\cam
         pack$ = play\x+" "+play\y+" "+play\z
         SendNBMsg(1,pack,nbmsgfrom(mainstream),mainstream)
         pitch# = EntityPitch(play\ent)
         roll# = EntityRoll(play\ent)
         pack$=LSet$( pitch,7 )+LSet$( EntityYaw(play\ent),7 )+LSet$( roll,7 )
         SendNBMsg(3,pack,nbmsgfrom(mainstream),mainstream)
      Case 101 ;Player Left
         For p.player = Each player
         If p\net_id = nbmsgfrom(mainstream)
         FreeEntity p\ent
            For n.nbplayer = Each nbplayer
            If n\id = p\net_id Then Delete n
            Next
         Delete p : Exit
         EndIf
         Next
      Case 1 ;Position File
         For p.player = Each player
         If p\net_id = nbmsgfrom(mainstream)
         dat$ = nbmsgdata$(mainstream)
         Local num1$,num2$,num3$,count
         For wc = 1 To Len(dat)
         let$ = Mid(dat,wc,1)
         If let = " " Then count = count + 1
         If let <> " "
            If count = 0
            num1 = num1 + let
            ElseIf count = 1
            num2 = num2 + let
            ElseIf count = 2
            num3 = num3 + let
            EndIf
         EndIf
         Next
         p\x = Float(num1) : p\y = Float(num2) : p\z = Float(num3)
         HideEntity p\ent
         PositionEntity p\ent,p\x,p\y,p\z : ShowEntity p\ent
         Exit
         EndIf
         Next
      Case 2 ;Movement File
         For p.player = Each player
         If p\net_id = nbmsgfrom(mainstream)
         move = nbmsgdata$(mainstream)
         If move = 1 ;Forward
         MoveEntity p\ent,0,0,.5
         ElseIf move = 2;Back
         MoveEntity p\ent,0,0,-.5
         ElseIf move = 3;Left
         TurnEntity p\ent,0,4,0
         ElseIf move = 4;Right
         TurnEntity p\ent,0,-4,0
         ElseIf move = 5; Sprung
      MoveEntity  p\ent,0,0.8,0
         EndIf
         Exit
         EndIf
         Next
      Case 3 ;Rotation File
         For p.player = Each player
         If p\net_id = nbmsgfrom(mainstream)
         dat$ = nbmsgdata$(mainstream)
         rotx# = Float(Mid(dat,1,7)) :roty#=Float(Mid(dat,8,7)) : rotz#=Float(Mid(dat,15,7))
         RotateEntity p\ent,rotx,roty,rotz
         Exit
         EndIf
         Next
   End Select
Wend

For p.player = Each player
If p\net_id <> nbuserid
TranslateEntity p\ent,0,-.2,0
EndIf
Next
End Function
         


das ist noich eine bb datei die heißt nb.bb

Code: [AUSKLAPPEN]
Type nbplayer
Field name$,id$
Field host,ip,port
End Type

Global serverip,nbmessage$,nbname$,serverport,nbuserid$

Function StartNBGame(port=80)
c = CreateUDPStream(port)
dport = UDPStreamPort(c)
If dport <> port Then CloseUDPStream(c):c = 0
DebugLog "Created New NetBlitz Game"
Return c
End Function

Function CreateNBPlayer$(name$,sip$,udpstream,hosting=0)
DebugLog "Created NetBlitz Player: "+name
go = CountHostIPs(sip)
ip = HostIP(1)
nbname = name
n.nbplayer = New nbplayer
n\name = name:n\port=nbgameport(udpstream)
If hosting = 1
serverip = ip:n\host= 1
serverport = nbgameport(udpstream)
EndIf
If hosting = 0
serverip = ip
WriteString(udpstream,"100:"+name)
SendUDPMsg(udpstream,ip,80)
;Delay 1000
CheckNBDataBase(udpstream,0)
EndIf
CountHostIPs("")
n\id= converttoid(HostIP(1),nbgameport(udpstream))
n\ip=HostIP(1)
nbuserid = converttoid(HostIP(1),nbgameport(udpstream))
Return nbuserid
End Function

Function DeleteNBPlayer(udpstream)
DebugLog "Deleted NetBlitz Player"
For i.nbplayer = Each nbplayer
If i\id <> nbuserid
WriteString(udpstream,"101:")
SendUDPMsg(udpstream,i\ip,i\port)
EndIf
Next
End Function

Function StopNBGame(udpstream)
DebugLog "NetBlitz Game Stopped"
CloseUDPStream(udpstream)
End Function

Function NBPlayerName$(id$)
For n.nbplayer= Each nbplayer
If n\id = id Then Return n\name
Next
End Function

Function NBPlayerLocal(ip)
CountHostIPs("")
If HostIP(1) = ip
Return 1
Else
Return HostIP(1)
EndIf
End Function

Function RecvNBMsg(udpstream,readm=1)
g = RecvUDPMsg(udpstream)
If g <> 0
DebugLog "Recieved NetBlitz Message"
If readm = 1
nbmessage = ReadString(udpstream)
EndIf
Return 1
EndIf
Return 0
End Function

Function NBMsgType(udpstream)
typ = Left(nbmessage,3)
Return typ
End Function

Function NBMsgFrom$(udpstream)
ip = UDPMsgIP(udpstream)
port = UDPMsgPort(udpstream)
tempid$=converttoid(ip,port)
For n.nbplayer = Each nbplayer
If n\id = tempid Then Return n\id
Next
End Function

Function NBMsgData$(udpstream)
msg$ = nbmessage
index=Instr( msg,":" )
msg = Mid(msg$,index+1)
Return msg
End Function

Function SendNBMsg(typ,msg$,idto$,udpstream)
If idto = 0
For i.nbplayer = Each nbplayer
If i\id <> nbuserid
WriteString(udpstream,typ+":"+msg)
SendUDPMsg(udpstream,i\ip,i\port)
EndIf
Next
Else
For i.nbplayer = Each nbplayer
If i\id = idto
WriteString(udpstream,typ+":"+msg)
SendUDPMsg(udpstream,i\ip,i\port)
EndIf
Next
EndIf
DebugLog "Sent Message: "+msg
End Function

;Extra NetBlitz Commands To Make The Internet Easier
Function NBMsgTimeOut(milliseconds)
UDPTimeouts(milliseconds)
End Function

Function WriteNBString(typ,texts$,udpstream)
WriteString(udpstream,typ+":"+texts)
End Function

Function SendNBPacket(idto$,udpstream)
If idto = 0
For i.nbplayer = Each nbplayer
If i\id <> nbuserid
SendUDPMsg(udpstream,i\ip,i\port)
EndIf
Next
Else
For i.nbplayer = Each nbplayer
If i\id = idto
SendUDPMsg(udpstream,i\ip,i\port)
EndIf
Next
EndIf
DebugLog "Sent Message Packet"
End Function

Function NBCopyPacket(udpstream,deststream,buffer)
CopyStream udpstream,deststream,buffer
End Function

Function CheckNBDatabase(udpstream,msgtype=1)
If msgtype = 0
time = MilliSecs()
Repeat
x = recvnbmsg(udpstream,1)
If x > 0
If nbmsgtype(udpstream) <> 102
x = 0
EndIf
EndIf
If MilliSecs() - time > 10000
RuntimeError "Timeout Error"+Chr(10)+"Could Not Connect To Server"
EndIf
Until x > 0
If nbmsgtype(udpstream) = 102
serverport = nbmsgport(udpstream)
serverip = nbmsgip(udpstream)
;i.nbplayer = New nbplayer
;i\ip = nbmsgfrom(udpstream)
;i\port = nbmsgport(udpstream)
;readnbpacket(udpstream)
;i\name = nbmsgdata(udpstream)
;i\id = Int(Str$(serverip)+Str$(serverport))
serverid$=converttoid(serverip,serverport)

n.nbplayer = New nbplayer
n\name = nbmsgdata(udpstream)
n\ip = ReadInt(udpstream)
n\port = ReadInt(udpstream)
n\id = converttoid(n\ip,n\port)
While Not Eof(udpstream)
n.nbplayer = New nbplayer
msg$ = ReadString(udpstream)
index=Instr( msg,":" )
msg = Mid(msg$,index+1)
n\name = msg
n\ip = ReadInt(udpstream)
n\port = ReadInt(udpstream)
n\id = converttoid(n\ip,n\port)
Wend

For i.nbplayer = Each nbplayer
If i\id = serverid Then i\host=1 : Exit
Next

For n.nbplayer = Each nbplayer
If n\host = 0 And n\id <> nbuserid
WriteString(udpstream,"100:"+nbname)
SendUDPMsg(udpstream,n\ip,n\port)
EndIf
Next

DebugLog countnbplayers()+" Playing"
Return 1
EndIf
ElseIf msgtype = 1
;WriteString(udpstream,"102:TempMessage")
;WriteString(udpstream,"102:"+nbplayerlocal()) Experimenting
;WriteString(udpstream,"102:"+nbname)
;WriteString(udpstream,"102:"+nbgameport(udpstream))
For i.nbplayer = Each nbplayer
WriteString(udpstream,"102:"+i\name)
WriteInt(udpstream,i\ip)
WriteInt(udpstream,i\port)
Next
i.nbplayer = New nbplayer
i\name = nbmsgdata(udpstream)
i\ip = nbmsgip(udpstream)
i\port = nbmsgport(udpstream)
i\id = converttoid(i\ip,i\port)
sendnbpacket(i\id,udpstream)
Return 1
EndIf
End Function

Function CountNBPlayers()
For i.nbplayer = Each nbplayer
bcount = bcount + 1
Next
Return bcount
End Function

Function ReadNBPacket(udpstream)
nbmessage = ReadString(udpstream)
Return nbmessage
End Function

Function NBGamePort(udpstream)
port = UDPStreamPort(udpstream)
Return port
End Function

Function NBMsgPort(udpstream)
port = UDPMsgPort(udpstream)
Return port
End Function

Function NBMsgIP(udpstream)
ip = UDPMsgIP(udpstream)
Return ip
End Function

Function CheckAvailPorts()
For br = 1 To 10000
c = CreateUDPStream(br)
If c <> 0
Exit
EndIf
Next
CloseUDPStream(c)
Return br
End Function

Function NBPlayerPort(id$)
For i.nbplayer = Each nbplayer
If i\id = id
Return i\port
EndIf
Next
End Function

Function NBPlayerIP(id$)
For i.nbplayer = Each nbplayer
If i\id = id
Return i\ip
EndIf
Next
End Function

Function ConvertToID$(ip,port)
s$ = Str$(ip)+"-"+Str$(port)
Return s
End Function


so hoffe mir kann da einer helfen

Tankbuster

BeitragDo, Mai 24, 2007 20:34
Antworten mit Zitat
Benutzer-Profile anzeigen
Zitat:
1. ob es jemanden gib bei euch der das programm getrentschreiben kann also eine datei in 2d wie wow und eine nur mit dem spiel

2. ob eseinen 3d moddeler gib nur für terains

3. ob es eine möglichkeit gib den ping abzufragen mitgrün root und so das von bin 1 bis 100 grünes feld ist bei ping 100 bis 150 gelb von 150 bis 250 orage und von 250 bis 1000 rot das wäre cool


1. Eine Datei wie World Of Warcraft ??
Naja.. ich glaube nicht, das das jemand machen wird. Wenn du ein Spiel proggen willst, dann mach das besser von vorne bis hinten alleine! Wink
Ich persöhnlich finde den Code auch unübersichtlich...

2. Du kannst mit Blender Meshs als Terrain Modellieren! Oder eine Heigthmap verwenden! Ich würde das letztere nehmen, denn du hast dann schon eine eingebaute LOD-Funktion

3. Klar! Du schickst dem Server etwas, und misst die Zeit, wie lange es braucht, um zurückzukommen!
Twitter
Download Jewel Snake!
Windows|Android

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group