HelpMakingOnlineGame

Blitz3D Forums/Blitz3D Programming/HelpMakingOnlineGame

I've made a fair number of small single player games using B3D but I'd really like to learn how to make some that're online (not just local network online, but like for everyone). However, I've had trouble running BlitzPlay, and have not been able to discern much from the examples that come with B3D. If anyone could point me in the direction of a tutorial, just to help me set my feet, it would be very appreciated.

Well some will say RakNet. I never could figure that out.
The only way I've been able to figure this out is with something like this...

This code is the actual code I used for one of my games derived from something I found in code archives, I added my own stuff like ban lists and such...
This is the server coding...
Global max_player=8,nunbers_of_players=0,sendchat,chatcont$,typechat,newchat$
Global typecom,newcom$,com$,prm$,hituser$,hitfrom$,sendhit
Global sendadmin,sendgernade,sendteamchat,teamnum,teamchatcont$
Global gpos_x,gpos_y,gpos_z,grot_y,grot_x,gvel,gkind,gnetref
Type user
	Field username$,team,kills,deaths
	Field stream
	Field xpos
	Field ypos
	Field Xmouse
	Field Ymouse
	Field packetdelay
	Field TTS ;Time to send value. each 25 millisec minimum.
	Field RTS ;Ready to send.
End Type
Type banned
	Field name$,ip$
End Type
Global b.banned
Type mesg
	Field txt$
End Type
Global m.mesg

Include "Include\gnet_inc.bb"

server=CreateTCPServer(81)
If Not server Then Print "Failed to create server at port 81" : WaitKey: End
Print "Main core mode.. waiting for a users"


gnet=GNET_AddServer("World Warfare","Alpha Test")
;If Not gnet Then RuntimeError "Unable to list server in GNET"
gnetref=MilliSecs()

IPS=CountHostIPs("")
TIP=HostIP(1)
DotIP$=DottedIP(TIP)


Graphics 550,220,32,2
SetBuffer BackBuffer()
AppTitle "Dedicated Server - Version 1.13"
ClsColor 0,0,255
Color 255,255,0
Cls

LoadBanned()
initime=MilliSecs()
onlinetime#=0
slog=OpenFile("serverlog.log")
Repeat
	junk=ReadLine(slog)
Until Eof(slog)
WriteLine slog,"*****"+CurrentTime()+" ** "+CurrentDate()+"*****"

While Not KeyHit(1)
	starttime=MilliSecs()
	If timetaken>maxtime Then maxtime=timetaken
	upd=upd+1
If upd=50
	Cls
	upd=0
	onlinetime#=(MilliSecs()-initime)/1000
	Text 10,0,"Server online ["+DotIP$+"]"
	Text 10,10,"-------------"
	Text 10,20,"Users logged in "+nunbers_of_players
	If onlinetime#<60
		Text 10,30,"Time Online "+onlinetime#+"/secs"
	Else
		Text 10,30,"Time Online "+(onlinetime#/60)+"/mins"
	EndIf
	Text 10,40,"Packets recived "+packets
	Text 10,50,"Time to execute: "+timetaken+"ms ["+maxtime+"]"
	Text 10,60,"Total Logins since start "+logins
	Text 300,0,"USERS AND SCORES"
	Text 300,10,"------------------------------"
	Text 300,20,"|   NAME   |TEAM|KILLS|DEATHS|"
	ucnt=0
	For u.user=Each user
		Text 300,(12*ucnt)+30,Left(LSet(u\username$,11),11)+" "+LSet(u\team,4)+" "+LSet(u\kills,5)+" "+u\deaths
		ucnt=ucnt+1
	Next
	Color 0,0,128
	Rect 5,75,295,120
	Rect 5,197,490,17
	Color 255,255,0
	cnt=0
	For m.mesg=Each mesg
		Text 10,(12*cnt)+75,m\txt$
		cnt=cnt+1
		If deletem>0
			Delete m
			cnt=cnt-1
			deletem=deletem-1
		EndIf
		If cnt>10 deletem=cnt-10
	Next
	If typechat=True 
		Text 10,200,"ADMIN> "+newchat$
	Else If typecom=True
		Text 10,200,"COMMAND> "+newcom$
	EndIf
	Locate 0,30
	
	Flip
End If

	;************* New Stream handing ************** 
	stream=AcceptTCPStream(server)
	If nunbers_of_players<max_player 
		If stream
			logit "New stream detected"
			message$=ReadLine$(stream)
			If message$="Loging into network"
				logit "User logins"
				message$=ReadLine$(stream)
				failed=False
				For users.user=Each user
					If message$=users\username$ Then failed=True
				Next
				For b.banned=Each banned
					If message$=b\name$
						failed=True
						forban=True
					End If
					If TCPStreamIP(stream)=b\ip$
						failed=True
						forban=True
					EndIf
				Next
				If failed=True
				  If forban=False
					WriteLine(stream,"Username already taken")
					logit "user login failed"
				  Else
					WriteLine(stream,"You where banned from this server")
					AddMesg message$+" tried to log in, but has been banned."
				  End If
				Else ;Login OK
					AddMesg "Welcome "+message$+"!"
					WriteLine(slog,message$+" logged-in ("+CurrentTime()+")")
					WriteLine(stream,"You have a connection to server")
					users.user = New user
					users\username$=message$
					users\stream=stream
					logins=logins+1
					nunbers_of_players=nunbers_of_players+1
				End If
			End If
		End If
	End If
	;**** End of New stream handling ****

	;General stream handling
	For users.user = Each user
		users\TTS=users\TTS-1 ;Decrese time to send.
		If Not Eof(users\stream) ;Check to see so user stream is up.
				If ReadAvail(users\stream)>1 ;Check if data has arrived.
					message$=ReadLine$(users\stream) ;read some
					packets=packets+1
					If message$="Player Cords" ;
						logit "reading user cordinates"
						users\xpos=ReadLine(users\stream) ;Read xpos
						users\ypos=ReadLine(users\stream) ;Read ypos
						users\XMouse=ReadLine(users\stream) ;read xmouse
						users\YMouse=ReadLine(users\stream) ;Read ymouse
						users\RTS=True
					Else If message$="broadcast chat"
						sendchat=True
						chatcont$=users\username$+": "+ReadLine(users\stream)
						AddMesg chatcont$
					Else If message$="team chat"
						sendteamchat=True
						teamnum=ReadLine(users\stream)
						teamchatcont$=users\username$+": "+ReadLine(users\stream)
						AddMesg "[Team #"+teamnum+"] "+chatcont$
					Else If message$="gernade"
						sendgernade=True
						gpos_x=ReadLine(users\stream)
						gpos_y=ReadLine(users\stream)
						gpos_z=ReadLine(users\stream)
						grot_x=ReadLine(users\stream)
						grot_y=ReadLine(users\stream)
						gvel=ReadLine(users\stream)
						gkind=ReadLine(users\stream)
						AddMesg "Throwing Gernade("+gkind+")!"
					Else If message$="user hit"
						hituser$=ReadLine(users\stream)
						hitfrom$=users\username$
						sendhit=True
						AddMesg hitfrom$+" hit player "+hituser$
					Else If message$="killed"
						from$=ReadLine(users\stream)
						sendchat=True
						chatcont$=users\username$+" was killed by "+from$
						For u.user=Each user
							If u\username$=from$
								u\kills=u\kills+1
								Exit
							EndIf
						Next
						users\deaths=users\deaths+1
						AddMesg from$+" killed "+users\username$
						WriteLine(slog,users\username$+" was killed by "+from$+" ("+CurrentTime()+")")
					End If
				End If
		Else ;User disconected .. ditch that user.
			AddMesg users\username$+" has left the server."
			WriteLine(slog,users\username$+" left the server ("+CurrentTime()+")")
			Delete users
			nunbers_of_players=nunbers_of_players-1:Exit ;Exits loop so it wont be a MAV error.
		End If
		If users\TTS<=0 And users\RTS=True
			users\rts=False
			users\TTS=25
			For others.user=Each user
				logit "sending user cordinates"
				WriteLine(users\stream,"user")
				WriteLine(users\stream,others\username$)
				WriteLine(users\stream,others\xpos)
				WriteLine(users\stream,others\ypos)
				WriteLine(users\stream,others\xmouse)
				WriteLine(users\stream,others\ymouse)
				If sendchat=True
					WriteLine(users\stream,"chat")
					WriteLine(users\stream,chatcont$)
				EndIf
				If sendteamchat=True
					WriteLine(users\stream,"teamchat")
					WriteLine(users\stream,teamnum)
					WriteLine(users\stream,teamchatcont$)
				EndIf
				If sendadmin=True
					WriteLine(users\stream,"admin chat")
					WriteLine(users\stream,newchat$)
				EndIf
				If sendgernade=True
					WriteLine(users\stream,"throw gernade")
					WriteLine(users\stream,gpos_x)
					WriteLine(users\stream,gpos_y)
					WriteLine(users\stream,gpos_z)
					WriteLine(users\stream,grot_x)
					WriteLine(users\stream,grot_y)
					WriteLine(users\stream,gvel)
					WriteLine(users\stream,gkind)
				EndIf
			Next
			sendchat=False
			sendadmin=False
			sendgernade=False
			sendteamchat=False
		End If
		If users\username$=hituser$ And sendhit=True
			WriteLine(users\stream,"hit")
			WriteLine(users\stream,hitfrom$)
			sendhit=False
		EndIf
	Next
	
	If KeyHit(28) And typecom=False
		FlushKeys()
		If typechat=False
			typechat=True
			newchat$=""
		Else
			typechat=False
			sendadmin=True
			AddMesg ">"+newchat$
		EndIf
	EndIf
	If typechat=True
		letter=GetKey()
		If letter<>0
			If letter=8
				newchat$=Left(newchat$,Len(newchat$)-1)
			Else
				newchat$=newchat$+Chr$(letter)
			EndIf
		EndIf
		letter=0
	EndIf
	
	If KeyHit(157) And typechat=False
		FlushKeys()
		If typecom=False
			typecom=True
			newcom$=""
		Else
			typecom=False
			space=Instr(newcom$," ")
			comd$=Left(newcom$,space)
			param$=Mid(newcom$,space+1,Len(newcom$)-space)
			CheckCommand(comd$,param$)
		EndIf
	EndIf
	If typecom=True
		letter=GetKey()
		If letter<>0
			If letter=8
				newcom$=Left(newcom$,Len(newcom$)-1)
			Else If letter<>13
				newcom$=newcom$+Chr$(letter)
			EndIf
		EndIf
		letter=0
	EndIf
	
	
	If MilliSecs()>=gnetref+(1000*60)
		gnetref=MilliSecs()
		GNET_RefreshServer("World Warfare","Alpha Test")
	EndIf
	
	;Delay 1
	timetaken=MilliSecs()-starttime
Wend
For users.user=Each user
	WriteLine(users\stream,"kill server")
	CloseTCPStream(users\stream)
Next
SaveBanned()
If Not GNET_Exec( "rem","World Warfare","Alpha Test") Then RuntimeError "Unable to remove server!"
WriteLine(slog,"Server Exiting ("+CurrentTime$()+")")
CloseFile(slog)
End

Function AddMesg(a$)
	m.mesg=New mesg
	m\txt$=a$
End Function

Function logit(a$)
	Text 0,100,a$
End Function

Function CheckCommand(com$,prm$)
	com$=Lower(Trim(com$))
	prm$=Trim(prm$)
	Select com$
		Case "kick"
			check=False
			If prm$="all"
				For users.user=Each user
					WriteLine(users\stream,"forcekick")
					Delete users
					nunbers_of_players=nunbers_of_players-1
				Next
				check=True
				WriteLine(slog,"All users where force kicked out. ("+CurrentTime()+")")
			Else If prm$<>"all"
				For users.user=Each user
					If Lower(users\username$)=prm$
						WriteLine(users\stream,"forcekick")
						check=True
						AddMesg ">"+users\username$+" has been force kicked."
						WriteLine(slog,users\username$+" was force kicked out. ("+CurrentTime()+")")
						Delete users
						nunbers_of_players=nunbers_of_players-1
						Exit
					EndIf
				Next
			EndIf
			If check=False AddMesg ">Player '"+prm$+"' does Not exist!"
		Case "teamchat"
			space=Instr(prm$," ")
			prm1$=Trim(Left(prm$,space))
			prm2$=Trim(Mid(prm$,space+1,Len(prm$)-space))
			sendteamchat=True
			teamnum=prm1
			teamchatcont$=prm2$
			AddMesg "[Team #"+teamnum+"] "+teamchatcont$
		Case "ban"
			For users.user=Each user
				If Lower(users\username$)=prm$
					WriteLine(users\stream,"forceban")
					check=True
					AddMesg ">"+users\username$+" has been banned."
					WriteLine(slog,users\username$+"was banned from this server ("+CurrentTime()+")")
					AddBanned users\username$,TCPStreamIP(users\stream)
					Delete users
					nunbers_of_players=nunbers_of_players-1
					Exit
				EndIf
			Next
		Default
			AddMesg ">No such command '"+com$+"'!"
	End Select
End Function

Function AddBanned(name$,ip$)
	b.banned=New banned
	b\name$=name$
	b\ip$=ip$
End Function

Function SaveBanned()
	file=WriteFile("banlist.dat")
	For b.banned=Each banned
		WriteString(file,b\name$)
		WriteString(file,b\ip$)
	Next
	CloseFile(file)
End Function

Function LoadBanned()
	file=ReadFile("banlist.dat")
	Repeat
		b.banned=New banned
		b\name$=ReadString(file)
		b\ip$=ReadString(file)
	Until Eof(file)
	CloseFile(file)
End Function

Function CheckBanned(uname$,uip$)
	For b.banned=Each banned
		If b\name$=uname$
			Return True
		EndIf
		If b\ip$=uip$
			Return True
		EndIf
	Next
End Function


And this is the client codes I use...
Global server_ip$="localhost",message$,stream,username$,teamnum%=0,TIP,DotIP$
Global xpos=100,ypos=100,zpos,prot ;position of player
Global max_player=8,nunbers_of_players=0,gnetref
Global newchat$,typechat,letter,packets,packetsent
Type user
	Field username$,m
	Field stream
	Field xpos
	Field ypos
	Field zpos
	Field prot
End Type
Type mesg
	Field txt$,red,green
End Type
Global m.mesg

IPS=CountHostIPs("")
TIP=HostIP(1)
DotIP$=DottedIP(TIP)
Print "Your IP: "+DotIP$

Print "Finding GNET Servers."
gnet=GNET_ListServers("World_Warfare")
If Not gnet Then RuntimeError "Unable to contact GNET"
For s.GNET_Server=Each GNET_Server
	If s\game$="World_Warfare"
		Print LSet(s\server$,50)+" | "+s\ip$
	EndIf
Next
Print "IP CHOICE: "+server_ip$
LoadClient()

;MAIN LOOP
;//////////
;//////////
;//////////

Function LoadClient()
	While  Len(username$)<4
		username$=Input$("Enter your username: ")
	Wend
	stream=OpenTCPStream(server_ip$,81)
	If Not stream Then Print "Failed to connect to server "+server_ip$:WaitKey:End
	Print "Connected, logging in..."
	WriteLine(stream,"Loging into network")
	WriteLine(stream,username$)
	message$=ReadLine$(stream)
	If message$<>"You have a connection to server" Then Print message$ : WaitKey :End ;somthing went wrong.
	Print "Login ok starting the game"
	WriteLine(stream,"Player Cords")	
	WriteLine(stream,xpos)
	WriteLine(stream,ypos)
	WriteLine(stream,zpos)
	WriteLine(stream,prot)
	Print "Sucessful, moving on."
	FlushKeys()
	FlushMouse()
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
End Function

Function SendMesg(kind,param1=0,param2=0)
	Select kind
		Case 2
			WriteLine(stream,"gernade")
			WriteLine(stream,EntityX(cam))
			WriteLine(stream,EntityY(cam))
			WriteLine(stream,EntityZ(cam))
			WriteLine(stream,EntityPitch(cam)/2)
			WriteLine(stream,EntityYaw(pnt))
			WriteLine(stream,param1)
			WriteLine(stream,param2)
	End Select
End Function

Function UpdateClient()
	.more	
	hitt=False
	;Time to check for new players
	If ReadAvail(stream)
		hitt=True ;a variable to be set each time packets reading
		message$=ReadLine$(stream)
		packets=packets+1
		Select message$
			Case "user"
				newuser=True
				userN$=ReadLine$(stream)
				If userN$<>username$ 
					For users.user=Each user
						 If users\username$=userN$ Then newuser=False : Exit ;exit and read in cords
					Next
					If newuser=True
						users.user = New user
						users\username$=userN$
						users\m=CopyEntity(snowman)
					End If
					users\xpos=ReadLine(stream)
					users\ypos=ReadLine(stream)
					users\zpos=ReadLine(stream)
					users\prot=ReadLine(stream)
					nunbers_of_players=nunbers_of_players+1
					PositionEntity users\m,users\xpos,users\ypos,users\zpos
					RotateEntity users\m,0,users\prot,0
				End If
				send=True
			Case "hit"
				health=health-Rand(10,20)
				from$=ReadLine(stream)
				AddMesg "You where hit by "+from$
				
				If health<=0
					AddMesg from$+" has killed you."
					WriteLine(stream,"killed")
					WriteLine(stream,from$)
				EndIf
			Case "chat"
				AddMesg ReadLine(stream)
			Case "teamchat
				num%=Int(ReadLine(stream))
				If teamnum%=num%
					AddMesg ReadLine(stream),False,True
				EndIf
			Case "admin chat"
				AddMesg ReadLine(stream),True
			Case "kill server"
				AddMesg "--------------------------------------------------------",True
				AddMesg "Server has been shut down. Please exit on your own terms",True
				AddMesg "--------------------------------------------------------",True
			Case "forcekick"
				AddMesg "Admin has kicked you out of this server.",True
			Case "forceban"
				AddMesg "You where banned and kicked from the server.",True
		End Select
	End If
	
	If hitt=True Then Goto more
	
	If KeyHit(28)
		FlushKeys()
		Locate 10,110
		out$=Input("BROADCAST >")
		WriteLine(stream,"broadcast chat")
		WriteLine(stream,out$)
		FlushKeys()
	EndIf
	If KeyHit(43)
		FlushKeys()
		Locate 10,110
		out$=Input("TEAM >")
		WriteLine(stream,"team chat")
		WriteLine(stream,teamnum)
		WriteLine(stream,out$)
		FlushKeys()
	EndIf
	
	If send=True
		WriteLine(stream,"Player Cords")
		WriteLine(stream,EntityX(pnt))
		WriteLine(stream,EntityY(pnt))
		WriteLine(stream,EntityZ(pnt))
		WriteLine(stream,EntityYaw(pnt))
		packetsent=packetsent+1	
		send=False
		sended=True
	End If
	If sended=True
		sended=False
		Rect 0,0,30,10
	End If
	
	
	cnt=0
	For m.mesg=Each mesg
		If m\green=True Color 0,255,0
		If m\red=True Color 255,0,0
		Text 10,(12*cnt)+120,m\txt$
		If m\red=True Color 255,255,255
		If m\green=True Color 255,255,255
		
		cnt=cnt+1
		If deletem>0
			Delete m
			cnt=cnt-1
			deletem=deletem-1
		EndIf
		If cnt>10 deletem=cnt-10
	Next
End Function


I've got a web site with hosting and everything, so I assume that to incorperate that I just change the CreateServer to OpenServer, with the server name and port #?

being that your server is writing the code.

Look thru it before you use it and change it to your liking and maybe some credit?

"Look thru it before you use it and change it to your liking and maybe some credit? "

I didn't have much time yesterday, but today I'll defenitely try it out, and of course score you some credit if I ever finish a game that uses it.

PCD Guy:

I'm getting a lot of Function Not Founds....what are the specifics for file names/placement etc?