Network Game

Blitz3D Forums/Blitz3D Programming/Network Game

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I'm having problems with freezing when another person joins. It will freeze one computer (the joiner). I think it is because it isn't get the host player correctly.
AppTitle "Internet Game"

SetBuffer BackBuffer()

Print "Dial-up:"
Print "  Click Modem Connection for DirectPlay"
Print "  Wait until DirectPlay connects"
Print "  Then connect to or create a game"
Print "                -or-"
Print "  Use your ISPs program to connect"
Print "Broadband:"
Print "  Click TCP/IP Connection for DirectPlay"
Print "  Type the host's IP address"
Print "  Click OK"
Print "  The join or create a game"
Print "LAN/Network:"
Print "  Click OK"
Print "  Then join or create the game"
Print "Serial Connection (direct) - Not Reccomended:"
Print "  Select the COM port"
Print "  Set any other settings"
Print "  Click OK"
Print "  Then Join or connect to the game"

;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;Types;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;

Type EnemyType
	Field Name$
	Field mesh
	Field Id
	Field Team
End Type

Type BulletType
	Field mesh
End Type

SeedRnd MilliSecs()

Global Game = StartNetGame()

If Game = 0 Then
	End
EndIf

If Game = 1 Then
	DebugLog "Joined the Game"
Else
	DebugLog "Hosted the Game"
EndIf

Graphics3D 1024,768,32,1

DebugLog "Graphics: " + GraphicsWidth() + "x" + GraphicsHeight() + "x" + GraphicsDepth()

Global Name$ = Input$("Enter Your Name: ")

DebugLog "Name: " + Name$

Print "Teams"
Print "1. Blue"
Print "2. Red"
Print
Global Team = Input("")

DebugLog "Team: " + Team

;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;Arrays;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;

Dim sight(2)
Dim boxes(5,2)

;Load Required Data
Global BulletMesh = LoadMesh("Bullet.3ds")
Global GunFire = LoadSound("gunfire.wav")
Global Hum = LoadSound("hum.wav")
sight(0) = LoadImage("Sight.bmp")
Building = LoadMesh("Building2-7.b3d")
Sub = LoadMesh("Submarine.b3d")
Global enemy = LoadMesh("Person.3ds")

SoundVolume Hum,2

LoopSound Hum

If BulletMesh = 0 Then RuntimeError "Bullet.3ds Not Found"
If GunFire = 0 Then RuntimeError "gunfire.wav Not Found"
If sight(0) = 0 Then RuntimeError "Sight.bmp Not Found"

;Clear all button history
FlushKeys()
FlushMouse()
FlushJoy()

Global id = CreateNetPlayer(Name$)

DebugLog "ID: " + id

SendNetMsg(1,Name$ + "," + Team,id)

DebugLog "Sent join Data"

If Game = 1 Then ;If game was joined
	SendNetMsg(4,"Host Player",id)
	DebugLog "Sent Request for Host"
	loop = 0
	While loop = 0
		If NetMsgType() = 5 And RecvNetMsg() Then
			thisEnemy.EnemyType = New EnemyType
			DebugLog "Host Type Created"
			start = Instr(NetMsgData$(),",")
			thisEnemy\Name$ = Left(NetMsgData$(),start - 1)
			DebugLog "Host Name: " + thisEnemy\Name$
			thisEnemy\Team = Mid$(NetMsgData$(),start + 1,Len(NetMsgData$()))
			DebugLog "Host Team: " + thisEnemy\Team
			;thisEnemy\Name$ = Mid$(NetMsgData$(),7,Len(NetMsgData$()))
			thisEnemy\Id = NetMsgFrom()
			DebugLog "Host Id: " + NetMsgFrom()
			thisEnemy\mesh = CopyMesh(enemy)
			loop = 1
			Goto EndHostGetLoop
		EndIf
	Wend
		.EndHostGetLoop
		SendNetMsg(4,"Box",id)
		DebugLog "Sent Request for Boxes"
		For i = 0 To 4
			RecvNetMsg()
			If NetMsgType() = 5 Then
				start = Instr(NetMsgData$(),",")
				x = Left(NetMsgData$(),start - 1)
				start2 = Instr(NetMsgData$(),"|")
				y = Mid(NetMsgData$(),start + 1,start2 - 1)
				z = Right(NetMsgData$(),Len(NetMsgData$()) - start2)
				PositionEntity boxes(i,0),x,y,z
				i = i + 1
			EndIf
		Next
EndIf

Cls

HideEntity BulletMesh

sight(1) = 0

MidHandle sight(0)

;RotateEntity Building,Rand(0,360),Rand(0,360),Rand(0,360)

HideEntity enemy

EntityType enemy,3
EntityType bulletMesh,4

If Building = 0 Then
	RuntimeError "File: 'Building.b3d' Not Found"
EndIf

Collisions 1,2,2,2

Global players

For i = 0 To 4
	boxes(i,0) = CreateCube()
	ResizeMesh(boxes(i,0),1,1,1)
	Randomize(boxes(i,0))	
Next

Global SubPiv = CreatePivot()
PositionEntity SubPiv,38.5,-5,15.5

Global SubPiv2 = CreatePivot()
PositionEntity SubPiv2,43.5,-5,15.5

Global player = CreatePivot()
If player = 0 Then RuntimeError "Failed to create the player pivot"
PositionEntity player,0,1,0
EntityRadius player,.3
EntityType player,1

Global box = CreateCube()
ResizeMesh(box,1,1,1)		

HideEntity box

Global camera = CreateCamera(player)
If camera = 0 Then RuntimeError "Failed to create camera"
CameraRange camera,.1,200

Global Mike = CreateListener(camera)

Global light = CreateLight()
If light = 0 Then RuntimeError "Failed to create light"
LightColor light,32,32,32
TurnEntity light,45,-45,0

EntityFX Building,1
EntityFX Sub,1
EntityType Building,2
EntityType Sub,2

PositionEntity Sub,100,0,0

sp#=.05
ey#=EntityY(player)

If Team = 1 Then
	PositionEntity player,21.5,2,43.5
ElseIf Team = 2 Then
	PositionEntity player,30.5,-4,-20.5
EndIf

While Not KeyHit(1)
	
	yv#=EntityY(player)-ey
	ey=EntityY(player)
	
	If KeyHit(57) yv=.1

	MoveEntity player,0,yv-.005,0

	If KeyHit(31) sight(1) = Not sight(1)
	
	If KeyDown(30) TurnEntity camera,-2,0,0
	If KeyDown(44) TurnEntity camera,+2,0,0
	
	If KeyDown(203) TurnEntity player,0,2,0
	If KeyDown(205) TurnEntity player,0,-2,0
	
	If MouseDown(1) And sight(1)
		thisbullet.BulletType = New BulletType
		thisbullet\mesh = CopyMesh(bulletMesh)
		RotateEntity thisBullet\mesh,EntityPitch(player,1),EntityYaw(player,1),EntityRoll(player,1)
		PlaySound GunFire
		PositionEntity thisBullet\Mesh,EntityX(player),EntityY(player),EntityZ(player)
		EntityType thisbullet\Mesh,3
	EndIf
	
	If KeyDown(200) MoveEntity player,0,0,sp
	If KeyDown(208) MoveEntity player,0,0,-sp
	
	Distance = EntityDistance(player,box)
	
	EmitSound(Hum,SubPiv)
	EmitSound(Hum,SubPiv2)
	
	For biter.BulletType = Each BulletType
		MoveEntity biter\mesh,0,0,1
		If Abs(EntityX(biter\mesh,1)) > 1000
			FreeEntity biter\mesh
			Delete biter
		ElseIf Abs(EntityY(biter\mesh,1)) > 1000
			FreeEntity biter\mesh
			Delete biter
		ElseIf Abs(EntityZ(biter\mesh,1)) > 1000
			FreeEntity biter\mesh
			Delete biter
		EndIf
	Next
	
	If frame = 25 Then
		SendNetMsg 2,EntityX(player) + "," + EntityY(player) + "|" + EntityZ(player),id
		frame = 0
	EndIf
	
	frame = frame + 1
	
	RecvNetMsg()
	
	If NetMsgType() = 2 Then
		start = Instr(NetMsgData$(),",")
		x = Left(NetMsgData$(),start - 1)
		start2 = Instr(NetMsgData$(),"|")
		y = Mid(NetMsgData$(),start + 1,start2 - 1)
		z = Right(NetMsgData$(),Len(NetMsgData$()) - start2)
		For thisEnemy.EnemyType = Each EnemyType
			If thisEnemy\Id = NetMsgFrom() Then
				PositionEntity thisEnemy\mesh,x,y,z
				LabelEntity(camera,thisEnemy\mesh,thisEnemy\Name$)
			EndIf
		Next
	EndIf
	
	TurnEntity player,0,-MouseXSpeed(),0
		
	CheckMail()
	
	If KeyDown(54) Then
		sp# = 2
	ElseIf KeyDown(42) Then
		sp# = .2
	Else
		sp# = .05
	EndIf
	
	If KeyDown(17) Then
		WireFrame True
	Else
		WireFrame False
	EndIf
	
	If NetMsgType() = 1
		players = players + 1
		thisEnemy.EnemyType = New EnemyType
		thisEnemy\mesh = CopyMesh(enemy)
		thisEnemy\Name$ = Left$(NetMsgData$(),Instr(NetMsgData$(),",") - 1)
		thisEnemy\Team = Mid$(NetMsgData$(),Instr(NetMsgData$(),",") + 1,Len(NetMsgData$()))
		thisEnemy\Id = NetMsgFrom()
		Print "Team: " + thisEnemy\Team
		Delay 1000
	EndIf
	
	If NetMsgType() = 98 Then
		Print NetMsgData$() + " has quit the game"
		For thisEnemy.EnemyType = Each EnemyType
			If thisEnemy\Id = NetMsgFrom() Then
				FreeEntity thisEnemy\mesh
				Delete thisEnemy
			EndIf
		Next
	EndIf
	
	If NetMsgType() = 4 And NetMsgData$() = "Host Player" Then
		SendNetMsg(5,Name$ + "," + Team,id,NetMsgFrom())
	EndIf
	
	If Game = 2 And NetMsgType() = 4 And NetMsgData$() = "Get" Then
		SendNetMsg(4,"Send: " + Players,NetMsgFrom())
	EndIf
	
	If NetMsgType() = 99 Then
		Cls
		Locate 0,0
		EndGraphics
		Print "You lost. " +  NetMsgData() + " Won!"
		Delay 500
		EndGame()
	EndIf

	For i = 0 To 4
		If EntityDistance(player,boxes(i,0)) < 1 And boxes(i,1) <> Team Then
			;FreeEntity boxes(i,0)
			If Team = 1 Then
				PositionEntity boxes(i,0),21.5,2,43.5
			ElseIf Team = 2 Then
				PositionEntity boxes(i,0),30.5,-4,-20.5
			EndIf
			boxes(i,1) = Team
			Print "You found a box"
			Delay 500
		EndIf
	Next
	
	UpdateWorld
	RenderWorld
	
	If sight(1) = 1 Then
		DrawImage sight(0),GraphicsWidth() / 2,GraphicsHeight() / 2,0
	EndIf
	
	For thisEnemy.EnemyType = Each EnemyType
		LabelEntity(camera,thisEnemy\mesh,thisEnemy\Name$)
	Next
	
	Flip
	
	;Print Infomation
	Locate 0,0
	Print "Frame: " + frame
	Print "Triangles Rendered: " + TrisRendered()
	Print "X: " + EntityX(player)
	Print "Y: " + EntityY(player)
	Print "Z: " + EntityZ(player)
	
Wend

SendNetMsg 98,Name$,id

EndGame()

End

Function ResizeMesh(mesh,width#,height#,depth#)
   
ScaleMesh mesh,1/MeshWidth#(mesh)*width#,1/MeshHeight#(mesh)*height#,(1/MeshDepth#(mesh))*depth#

End Function

Function EndGame()

FreeEntity Building
FreeImage sight(0)
DeleteNetPlayer id

EndGraphics

End

End Function

Function CheckMail()

If NetMsgType() = 4 And NetMsgData$() = "Box" Then
	For i = 0 To 4
		SendNetMsg(5,EntityX(Boxes(i,0)) + "," + EntityY(Boxes(i,0)) + "|" + EntityZ(Boxes(i,0)),id)
	Next
EndIf

If NetMsgType() = 97 Then
	If NetMsgData = Name$ Then
		PositionEntity player,0,1,0
	EndIf
EndIf

If NetMsgType() = 5 Then
	Repeat
		RecvNetMsg()
		If NetMsgType() = 5 Then
			start = Instr(NetMsgData$(),",")
			x = Left(NetMsgData$(),start - 1)
			start2 = Instr(NetMsgData$(),"|")
			y = Mid(NetMsgData$(),start + 1,start2 - 1)
			z = Right(NetMsgData$(),Len(NetMsgData$()) - start2)
			PositionEntity boxes(i,0),x,y,z
			i = i + 1
		EndIf
	Until i = 4
EndIf

If NetMsgType() = 200 Then
	Locate 0,0
	DeleteNetPlayer id
	RuntimeError "A Fatal Error Occurred"
	EndGame()
EndIf

End Function

Function Randomize(boxmesh)

Select Rand(0,6) + 1
	Case 1
		PositionEntity boxmesh,-7,0,-7
	Case 2
		PositionEntity boxmesh,19,0,-18
	Case 3
		PositionEntity boxmesh,18,4,-18
	Case 4
		PositionEntity boxmesh,25,-3,-10
	Case 5
		PositionEntity boxmesh,5,-2,22
	Case 6
		PositionEntity boxmesh,Rand(-8,30),0,Rand(-22,26)
	Case 7
		PositionEntity boxmesh,27,6,3
End Select	

End Function

Function LabelEntity (camera, entity, label$)
	If EntityInView (entity, camera)
		CameraProject camera, EntityX (entity), EntityY (entity), EntityZ (entity)
		w = StringWidth (label$)
		h = StringHeight (label$)
		x = ProjectedX () - (w / 2) - 1
		y = ProjectedY () - (h / 2) - 1
		Color 0, 0, 0
		Rect x, y, w + 2, h + 2, 1
		Color 255, 255, 255
		Text x, y, label$
	EndIf
End Function

Function WaitNetMsg()

Repeat

Until RecvNetMsg()

End Function

;Msg Types
;1 = Join
;2 = Position
;3 = Mail (Send a msg)
;4 = Request
;97 = Reset Player
;98 = Quit
;99 = Win/Lose