Network Game

Blitz3D Forums/Blitz3D Programming/Network Game

I've got it working so that you can create players and others stuff. My positioning system isn't working. The enemy will appear 0,0,0 but won't moveafter that.
AppTitle "Internet Game"

SetBuffer BackBuffer()

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

Type EnemyType
	Field Name$
	Field mesh
	Field Id
End Type

Type BulletType
	Field mesh
End Type

SeedRnd MilliSecs()

Global Game = StartNetGame()	

If Game = 0 Then
	End
EndIf

Graphics3D 1024,768,32,1

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

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

Dim sight(2)

;Load Required Data
Global BulletMesh = LoadMesh("Bullet.3ds")
Global GunFire = LoadSound("gunfire.wav")
sight(0) = LoadImage("Sight2.bmp")
Building = LoadMesh("Building2-6.b3d")
Global enemy = LoadMesh("Person.3ds")

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

id = CreateNetPlayer(Name$)

If Game > 0 Then
	SendNetMsg(1,Name$,id)
ElseIf Game = 1 Then
	SendNetMsg(4,"Request: Host Player",id)
	Repeat
		RecvNetMsg()
		If NetMsgType() = 4 Then
			thisEnemy.EnemyType = New EnemyType
			thisEnemy\Name$ = Mid$(NetMsgData$(),9,Len(NetMsgData$()))
			thisEnemy\Id = NetMsgFrom()
			thisEnemy\mesh = CopyMesh(enemy)
		EndIf
	Until NetMsgType() = 4
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

Global player = CreatePivot()
PositionEntity player,0,1,0
EntityRadius player,.3
EntityType player,1

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

Randomize()		

Global camera = CreateCamera(player)
CameraRange camera,.1,200

Global light = CreateLight()
LightColor light,32,32,32
TurnEntity light,45,-45,0

EntityFX Building,1
EntityType Building,2

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

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)
	
	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
			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
	
	Locate 0,0
	Print EntityDistance(player,box)
	Print "Frame: " + frame
	
	RecvNetMsg()
	
	If NetMsgType() = 1
		Locate 0,0
		Cls
		Print NetMsgData() + " has joined"
		players = players + 1
		Print "Increase player amount"
		thisEnemy.EnemyType = New EnemyType
		Print "Created new player Type"
		thisEnemy\mesh = CopyMesh(enemy)
		thisEnemy\Name$ = NetMsgData$()
		thisEnemy\Id = NetMsgFrom()
		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 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

	If EntityDistance(player,box) < 1 Then
		Cls
		Locate 0,0
		EndGraphics
		Print "You Won!!!"
		SendNetMsg 99,Name$,id
		Delay 500
		EndGame()
	EndIf
	
	UpdateWorld
	RenderWorld
	
	If sight(1) = 1 Then
		DrawImage sight(0),GraphicsWidth() / 2,GraphicsHeight() / 2,0
	EndIf
	Flip
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()

RecvNetMsg()

If NetMsgType() = 4 And NetMsgData$() = "Request: Host Player" Then
	SendNetMsg(4,"Return: " + Name$,id)
EndIf

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

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

End Function

Function Randomize()

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

End Function

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

This is where the pos code is
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
			EndIf
		Next
	EndIf

P.S. This is my 100th post!!!