Need help with this code here..
Blitz3D Forums/Blitz3D Programming/Need help with this code here..
| ||
When my friend tries to connect to my machine.. He always get the memory access violation when connecting.. but when I see it from my server side it says he lagged out.. I'm using BlitzPlay Network Engine.. Here is the code.. Graphics3D 800,600,16,2 SetBuffer BackBuffer() AppTitle "Soul Shooter Online V0.4" AntiAlias enable WBuffer enable Dither enable Include "getfile.bb" Include "gnet.bb" Include "ParticleManager.bb" Include "devilgui.bb" Include "bplite.bb" Include "Inc_TerraEdLoader.bb" Include "Inc_TerraEdCamera.bb" HidePointer Global gameState$="Title Screen" Global body=1 Global scene=2 Collisions body,scene,2,2 Collisions body,body,2,2 Global spell$="Spark" Global copySprite Global chat$ Global key=GetKey() ;-------------------------------------------- Global mb1press Global mb2press Global upkeyPress Global downkeyPress Global leftkeyPress Global rightkeyPress Global enterkeyPress Global spacekeyPress Global tabkeyPress Global chatkeypress ;--------------------------------------------- Global localplayerpointer Global font=LoadFont("Arial",20,1) Global bigfont=LoadFont("Arial",50,1) Global menucount,servercount,serverpointer Global camera Global health# Global serverrefresh# Global shot,shot2,shot3 Global shottimer# Global hosting Global vx#,vy#,vz# Global oldx#,oldy#,oldz# Global centermouse=False Global justdied Global level Dim incoming$(100) Global positionUpdateTimer# Global sparkemitter Global sparktemplate Global scoresend# Global mainlogo=LoadImage("media/ssobanner.png") Global readserver1$ Global server1$ ;member status sounds-------------------------------------------- Global normalsound=LoadSound("media/normal.mp3") Global cpasound=LoadSound("media/cpa.mp3") Global modsound=LoadSound("media/moderator.mp3") Global adminsound=LoadSound("media/admin.mp3") Global creatorsound=LoadSound("media/creator.mp3") Global enteringchat=False ;----------------------------------------------------------------------- ;collisions Collisions(body,scene, 2, 2) Collisions(body,body, 2, 2) ;---------------------------------------------------------------------- ;load spell sounds Global sparksound=LoadSound("media/spark.wav") Global aurorablastsound=LoadSound("media/aurorablast.wav") Global shockwavesound=LoadSound("media/shockwave.wav") Global deadlygassound=LoadSound("media/deadlygas.wav") gui_initgui("skins/cwing.skin") camera=TER_CreateCamera(0,0,0) TER_InitTerraEd(camera,False) Type player Field iD, name$,mesh[10],score,score2 End Type Type info Field txt$ End Type Type pdata Field x#,y#,r#,name$,net_id% Field xvel#,yvel#,rvel# End Type Text 0,0,"Now Loading..." Delay 5000 Cls Global name$=Input("Please enter a username:") .mainmenu Global mainwindow=GUI_CreateWindow(400,300,200,200, "SSO Main Menu") Global btn1 = GUI_CreateButton(mainwindow,5,10,175,25, "Servers") Global btn2 = GUI_CreateButton(mainwindow,5,40,175,25, "Forums") Global btn3 = GUI_CreateButton(mainwindow,5,70,175,25, "Fan Sites") Global btn4 = GUI_CreateButton(mainwindow,5,100,175,25,"Clan Support (Coming Soon)") While Not KeyHit(1) Cls GUI_UpdateGUI() If GUI_AppEvent()=btn1 serverwindow=GUI_CreateWindow(200,300,200,200, "Servers:") btn11 = GUI_CreateButton(serverwindow,5,10,175,25, "SoulEdge Server") btn12 = GUI_CreateButton(serverwindow,5,40,175,25, "") btn13 = GUI_CreateButton(serverwindow,5,70,175,25, "") btn14 = GUI_CreateButton(serverwindow,5,100,175,25,"") btn16 = GUI_CreateButton(serverwindow,5,130,175,25,"") EndIf If GUI_AppEvent()=btn11 Color 255,255,255 Cls My_Port = Rand (3000,4000) IP$=Input$("Please enter the host's IP? ") Select BP_JoinSession(name,My_Port,IP,2080) ;Handle any of the reasons if we couldn't join. Case BP_NOREPLY Cls RuntimeError "No reply from server.." End Case BP_IAMBANNED Cls RuntimeError "You have been banned.. Contact an admin for unban on the forums." End Case BP_GAMEISFULL Cls RuntimeError "The server is full.. exiting" End Case BP_PORTNOTAVAILABLE Cls RuntimeError "Port: " + My_Port + " was not available.. exiting" End Case BP_USERABORT Cls RuntimeError "Connection attempt aborted!" End End Select Goto chatserver Cls EndIf DrawImage mainlogo,0,0 Flip Wend GUI_FreeGUI() End .chatserver ;Oh, we're allowed in? here we go! p.pdata = New pdata p\name = myname p\x = width/2 p\y = height/2 p\r = 90 p\net_id = BP_My_ID While Not KeyHit(1) frames = WaitTimer(tmr) For k = 1 To frames BP_UpdateNetwork() HandleMessages() UpdatePlayers() send_count = send_count - 1 If send_count <= 0 Then send_count = send_freq p.pdata = First pdata msgtosend$ = BP_IntToStr(Int(p\x),2) + BP_IntToStr(Int(p\y),2) + BP_IntToStr(Int(p\r),2) If sendupdates Then BP_UDPMessage (0,1,msgtosend) End If Next RenderAll() Wend BP_EndSession () End ;******************************Functions******************************* Function HandleMessages () For msg.MsgInfo = Each MsgInfo Select msg\msgType Case 255 ;A new player has joined! p.pdata = New pdata p\name = msg\msgData p\x = width/2 p\y = height/2 p\r = 90 p\net_id = msg\msgFrom nInfo.NetInfo = BP_FindID(p\net_id) Info ("**" + p\Name + " has joined!" +extra$) Case 254 ;A player has left p.pdata = Find_pdata(msg\msgFrom) If p<>Null Then If msg\msgData = True Then Info ("**" + p\Name + " leaves.") Else Info ("**" + p\Name + " lagged out!") Delete p End If Case 253 ;The host has disconnected For p.pdata = Each pdata If p\net_id <> BP_My_ID Then Delete p Next If msg\msgData = True Then Info ("**The server has been shutdown by the host!") Else Info ("**No reply from host in " + (BP_TimeoutPeriod / 1000) + " seconds. Exiting game..") Case 252 ;Someone got kicked/banned p.pdata = Find_pdata(msg\msgFrom) If p\net_id = BP_My_ID Then ;It was -me-?? If msg\msgData = False Then Info ("**You have been kicked by Admin/Mod") Else Info ("**You have been banned by Server Admin/Moderator.") For p.pdata = Each pdata If p\net_id <> BP_My_ID Then Delete p End If Next Else ;It wasn't? Ok then. If msg\msgData = False Then Info ("**" + p\Name + " has been kicked!") Else Info ("**" + p\Name + " has been banned!") Delete p End If Case 2 ;a chat packet p.pdata = Find_pdata(msg\msgFrom) Info (p\Name + ":" + msg\msgData) Case 3 ;normal PlaySound normalsound Case 4 ;cpa attributes PlaySound cpasound Case 5;moderator attributes PlaySound modsound Case 6;admin attributes PlaySound adminsound Case 7;creator attributes PlaySound creatorsound End Select Delete msg Next End Function Function Info (t$) i.Info=New info i\txt$=t$ Insert i Before First info End Function Function UpdatePlayers () For p.pdata = Each pdata ;Other input If KeyHit(KF1) Then logging = 1 - logging If logging Then BP_StartLogFile (myname + ".txt") Else BP_StopLogFile () End If End If If BP_My_ID = BP_Host_ID For keycheck = 60 To 68 If KeyHit(keycheck) Then If keycheck - 58 <> My_ID Then ;No kicking yourself! If KeyDown(kShift) Then nInfo.NetInfo = BP_FindID(keycheck - 58) If nInfo<>Null Then Info("**You banned: " + nInfo\Name) BP_KickID(keycheck - 58, True) End If Else nInfo.NetInfo = BP_FindID(keycheck - 58) If nInfo<>Null Then Info("**You kicked: " + nInfo\Name) BP_KickID(keycheck - 58, False) End If End If End If End If Next End If If KeyHit(KF11) Then sendupdates = 1 - sendupdates End If If KeyDown(KTab) Then DebugInfo = 1 Else DebugInfo = 0 ;Chat key=GetKey() If key If key=13 If chat$<>"" Then BP_UDPMessage (0,2,chat$) Info (p\name + ":" + chat) chat$="" End If Else If key=8 If Len(chat$)>0 Then chat$=Left$(chat$,Len(chat$)-1) Else If key>=32 And key<127 chat$=chat$+Chr$(key) EndIf EndIf Next End Function Function RenderAll () Local st$ Local counter% For p.pdata = Each pdata counter = counter + 1 Next Color 100,100,100 Rect 0,FontHeight()*2,width,FontHeight()*(counter) + 1,False Color 10,10,50 Rect 1,(FontHeight()*2)+1,width-2,FontHeight()*(counter) - 1,True textcounter = 2 Color 20,150,150 Text width/2,0,"-=[Soul Shooter Online ChatRoom]=-",True For p.pdata = Each pdata If p\net_id = BP_My_ID Then Text 1,FontHeight()*textcounter, p\net_id + "] " + "**" + p\name + "**" Else Text 1,FontHeight()*textcounter, p\net_id + "] " + p\name End If textcounter = textcounter + 1 Next Color 0,200,50 textcounter = textcounter + 1 Text 0,FontHeight()*textcounter, ">" + chat$ + "_" textcounter = textcounter + 1 counter = 0 Color 150,150,150 For i.Info = Each info counter = counter + 1 If counter > 15 Then Delete i Else Text 0,FontHeight()*textcounter, i\txt textcounter = textcounter + 1 End If Next Color 0,140,160 If logging Then Text 0,height-FontHeight(),"Logging: Enabled (f1 to toggle)" Else Text 0,height-FontHeight(),"Logging: Disabled (f1 to toggle)" If sendupdates Then Text 0,height-FontHeight()*2,"Sending updates: True (f11 to toggle)" Else Text 0,height-FontHeight()*2,"Sending updates: False (f11 to toggle)" Text 0,height-FontHeight()*3,"[TAB] displays debug information",False, False If Host Then Text 0,height-FontHeight()*4,"F2-10 to boot that player, hold [SHIFT] to ban" If DebugInfo Then p.pdata = First pdata Color 40,100,150 Rect (width/2) - WindowCount - 1,(height/2)-51,(WindowCount * 2) + 2, 102,False Color WindowCount/4,WindowCount/3,WindowCount/2 Rect (width/2) - WindowCount,(height/2)-50,WindowCount * 2, 100,True If WindowCount < 210 Then WindowCount = WindowCount + 10 If WindowCount > 210 Then WindowCount = 210 Else If BP_Online Then Color 100,200,100 Text (width/2) - WindowCount,(height/2)-50,"Online",False,False Else Color 200,50,50 Text (width/2) - WindowCount,(height/2)-50,"Offline",False,False End If Color 200,200,200 st$ = "-=[" + p\Name + "]=-" Text (width/2),(height/2) - (FontHeight() * 3),st$,True,True st$ = "GameType: " + BP_GetGameType() Text (width/2) - (WindowCount),(height/2) - FontHeight(),st$,False,True st$ = "TimeoutPeriod: " + BP_GetTimeoutPeriod() + "s." Text (width/2) - (WindowCount),(height/2),st$,False,True st$ = "My IP: " + BP_GetMyIP() Text (width/2) - (WindowCount),(height/2) + FontHeight(),st$,False,True st$ = "Host IP: " + DottedIP$(BP_GetHostIP()) Text (width/2) - (WindowCount),(height/2) + (FontHeight() * 2),st$,False,True st$ = "# Of Players: " + BP_GetNumberOfPlayers() Text (width/2),(height/2) - FontHeight(),st$,False,True st$ = "Max. # Of Players: " + BP_GetMaxPlayers() Text (width/2),(height/2),st$,False,True st$ = "Packets Sent: " + BP_GetPacketsSent() Text (width/2),(height/2) + FontHeight(),st$,False,True st$ = "Packets Received: " + BP_GetPacketsReceived() Text (width/2),(height/2) + (FontHeight() * 2),st$,False,True End If Else If WindowCount > 0 Then Color 40,100,150 Rect (width/2) - WindowCount - 1,(height/2)-51,(WindowCount * 2) + 2, 102,False Color WindowCount/4,WindowCount/3,WindowCount/2 Rect (width/2) - WindowCount,(height/2)-50,WindowCount * 2, 100,True WindowCount = WindowCount - 10 If WindowCount < 0 Then WindowCount = 0 End If End If Flip Cls End Function Function Find_pdata.pdata (ID) For p.pdata = Each pdata If p\net_id = ID Then Return p Next End Function |
| ||
Ok, I do not have the time to analyze that code. If you dont mind some positive criticism allow me to make a few suggestions. First off Indent your code. It will be impossible for you and everyone else to easily decipher exactly what the heck is going on. Secondly network game coding is a sheer nightmare. Might I suggest starting with something a tad easier? Simply creating a client and server on the same machine that does nothing more than a simple chat. Just learn to had send and receive data as well as to build on that and have a good data management structure. Blindly jumping into projects like this always ends poorly. If you cant trap a MAV at this early of a stage, just image what it will be like when there are a lot of clients sending tons of packets everywhere. Just some friendly advise. If you want to continue with this however, might I suggest running in Debug mode and atleast figuring out where it is MAVing on your friends end? -Scott |
| ||
that's what this code is basically is.. a simple chat.. and I have worked on a game with network coding before with my Soul Shooter Online game.. so I'm not new with Network coding.. (TCP) I'm just using this BlitzPlay lite.. which is UDP.. and I'm learning how to use it.. and My fault with the code indenting.. |