Kürzungen bei UDP

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

Ornos

Betreff: Kürzungen bei UDP

BeitragDi, Apr 05, 2005 9:59
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo
hab mal ne frage zum kürzen beim udpsendungen.
hier ist mal ein abgewandelter beispielcode von netblitz
BlitzBasic: [AUSKLAPPEN]

;Here is a more realistic demo of NetBlitz, this is 3d and created by David Dawkins
;The level was built in Maplet
AppTitle \"NetBlitz 3d!\"
SeedRnd MilliSecs()
Type player
Field x#,y#,z#
Field name$,net_id$
Field ent,cam,piv
Field speed
End Type

;Spawn Points
;1,-3,0
;63,5,21
;-61,3,-19
;-31,-11,2

Print \"NetBlitz Created By David Dawkins\"
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,0,-5,0
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()

play\speed=0
Function Main()
mastertime = MilliSecs()
While Not KeyHit(1)
;---------------------------------Hier ist meine Frage--------
If KeyDown(200) ;Forward
play\speed=play\speed*1.2
If play\speed>3 Then play\speed=3
MoveEntity play\ent,0,0,play\speed
SendNBMsg(2,\"1\",0,mainstream)
ElseIf KeyDown(208) ;Back
MoveEntity play\ent,0,0,-.5
SendNBMsg(2,\"2\",0,mainstream)
EndIf

If KeyDown(203) ;Left
TurnEntity play\ent,0,4,0
SendNBMsg(2,\"3\",0,mainstream)
ElseIf KeyDown(205) ;Right
TurnEntity play\ent,0,-4,0
SendNBMsg(2,\"4\",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 = CreateCone() : RotateMesh(cone,90,0,0)
PositionMesh cone,0,0,2 : ScaleMesh cone,.5,.5,.5 : 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 = 1 : p\y = -3 : p\z = 0
ElseIf spawn = 2
p\x = 63 : p\y = 5 : p\z = 21
ElseIf spawn = 3
p\x = -61 : p\y = 5 : p\z = -19
ElseIf spawn = 4
p\x = -31 : p\y = -11 : p\z = 2
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)
;----------------hier-----------------------------------
If move = 1 ;Forward
p\speed=p\speed*1.2
If p\speed>3 Then p\speed=3
MoveEntity p\ent,0,0,p\speed
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
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


Wie kann ich diesen teil abkürzen, ohne ihn nochmals in den case2 reinschreiben zu müssen? denn doppelt gemoppelt ist immer schlecht!

steht in der main funktion:
BlitzBasic: [AUSKLAPPEN]

play\speed=play\speed*1.2
If play\speed>3 Then play\speed=3


das ist der teil (in case 2) den ich nicht nochmal darstehen haben will,
sondern abgekürzt, aber wie geht das(wenn überhaupt):
BlitzBasic: [AUSKLAPPEN]

p\speed=p\speed*1.2
If p\speed>3 Then p\speed=3


meine originale berechnung für den speed ist noch wesendlich länger
und desshalb will ich ihn nicht doppelt berechnen müssen

Ornos

BeitragDi, Apr 05, 2005 14:10
Antworten mit Zitat
Benutzer-Profile anzeigen
kann mir denn keiner helfen
 

Timo

BeitragDi, Apr 05, 2005 14:14
Antworten mit Zitat
Benutzer-Profile anzeigen
naja, kürzer weis ich jetzt nicht, (hab jetzt auch nicht die lust mich in den ganzen code einzulesen) aber du könntest ihn wesentlich übersichtlicher gestalten, indem du den code einrückst (z.t. hast du es ja gemacht). außerdem solltest du große sinnabschnitte in Funktionen packen, das könnte den Code zugleich kürzer machen

Ornos

BeitragDi, Apr 05, 2005 14:27
Antworten mit Zitat
Benutzer-Profile anzeigen
ok ich versuchs dann mal zu erklären.
wenn ich die feiltaste(oben) drücke, dann berechnet er zuerst das was ich auf dem monitor sehe (also meine play\objekt) und er berechnet zudem den speed (play\speed), wie schnell also mein objket ist.
das gleiche macht er auch für die personen im netzwerk. er zeigt allen clients was mein objekt macht wenn ich die feiltaste(oben) drücke und berechnet...leider...nochmal den speed (p\speed) und sendet den ganzen kram. doch ich will den p\speed nicht nochmal berechnen, sondern mich mit irgend einem befehl auf das bereits berechnete play\speed beziehen.
sowas wie play\speed=p\speed geht nicht. hab ich schon versucht
kennst sich einer mit udp in blitz aus? ich glaube ich muss das irgendwie auf die id's beziehen (glaube ich).
 

Timo

BeitragDi, Apr 05, 2005 14:50
Antworten mit Zitat
Benutzer-Profile anzeigen
naja, also das scheint mir nicht wie ein UDP problem, sondern eher wie eine Programmierproblem Wink
warum funktioniert denn
Code: [AUSKLAPPEN]
play\speed=p\speed

nicht?

Ornos

BeitragMi, Apr 06, 2005 9:07
Antworten mit Zitat
Benutzer-Profile anzeigen
das geht deshalb nicht, weil er dann komischerweise den anderen mitspieler im netzwerk steuert

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group