Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing Looping (v1 & v2)
#1
Originally posted by Dark Dragon

Name: Optimize Looping
By: Dark//Dragon
Difficulty: 3/5
Version: 1

Notes:
Ok, what this will do is add a variable HighIndex, that is equal to the highest assigned index. With that we can greatly optimize looping, because if you have IOCP for example and you're MAX_PLAYERS is 500, 4 players were playing then the player with index 3 left, using the old system, you would waste 497 loops every time there is a "1 To MAX_PLAYERS", but once you have added this system only 1 loop will be wasted.
In This version (1), only the server will have the variable, projected in version 2 i will parse the variable to client to opti that too.
This was programmed in 3.03, i know many of the MSE changes so i'll try to change my code to be complient, but if there is a mistake, figure it out :wink:

All Server Side

++++++++++++++
Part1 : Atcuall Code
++++++++++++++

at the top of modGlobals, under:
Code:
' Used for logging
Public ServerLog As Boolean

add:
Code:
' Used for Player looping
Public HighIndex As Long

this is our variable, and we will store stuff here! :wink:

At the bottom of modGameLogic, add:
Code:
Sub SetHighIndex()
Dim I As Integer
Dim X As Integer

    For I = 0 To MAX_PLAYERS
        X = MAX_PLAYERS - I
        
        If IsConnected(X) = True Then
             HighIndex = X
             Exit Sub
        End If

    Next I
    
    HighIndex = 0
    
End Sub

this will set the variable = to the highest assigned index.

now! in modServerTCP, in the sub SocketConnected, under
Code:
If Not IsBanned(GetPlayerIP(index)) Then
                 Call TextAdd(frmServer.txtText, "Received connection from " & GetPlayerIP(index) & ".", True)
             Else
                 Call AlertMsg(index, "You have been banned from " & GAME_NAME & ", and can no longer play.")
             End If

add:
Code:
' Set The High Index
             Call SetHighIndex

this will set the high index whenever anyone connects

also in ModServerTCP, in the sub "CloseSocket", under
Code:
Call UpdateCaption

add
Code:
Call SetHighIndex

this will recalculate the High index whenver anyone disconnects.

Ok, Part1 (The Real Code) is done, the next part is mindless :wink:

+++++++++++++++
Part2: Mindlessness!!
+++++++++++++++
Ok, this entire Part is me telling you where to change

Code:
For I = 1 to MAX_PLAYERS
to
Code:
For I = 1 to HighIndex
, so heres what im going to do, im going to say the module and the sub (and possebly notes that include like do 4 times) and you do it :wink:

ModDatabase
=============
Sub SaveAllPlayersOnline()

ModGameLogic
=============
Function TotalOnlinePlayers() As Long
Function FindPlayer(ByVal Name As String) As Long
Function CanNpcMove(ByVal MapNum As Long, ByVal MapNpcNum As Long, ByVal Dir) As Boolean [4 times for the 4 dirs]
Function GetTotalMapPlayers(ByVal MapNum As Long) As Long

ModGeneral
=============
Sub GameAI()
Sub CheckGiveHP()
Sub PlayerSaveTimer()

ModServerTCP
=============
Function IsMultiAccounts(ByVal Login As String) As Boolean
Function IsMultiIPOnline(ByVal IP As String) As Boolean
Sub SendDataToAll(ByVal Data As String)
Sub SendDataToAllBut(ByVal index As Long, ByVal Data As String)
Sub SendDataToMap(ByVal MapNum As Long, ByVal Data As String)
Sub SendDataToMapBut(ByVal index As Long, ByVal MapNum As Long, ByVal Data As String)
Sub AdminMsg(ByVal Msg As String, ByVal Color As Byte)
Sub SendWhosOnline(ByVal index As Long)
Sub SendJoinMap(ByVal index As Long)

modHandleData
==============
Player attack packet
Map data packet
Search packet

some of the Subs I might have forgotten, (And not all of them), you need to figure it out, like some of them needed to be left, howver, it will work if they are left out

You're done!!! ;_; :lol:


Name: Optimize Looping
By: Dark//Dragon
Difficulty: 3/5
Version: 2

Notes:
Ok, in this tutorial, I assume you have already completed V1 of the tutorial, available HERE. What this tutorial is, is the extention onto that tutorial, where i extend the HighIndex variable over to the client, alowing for much optimised looping client side :wink:

As Always:
Back up your source before starting ANY tutorial!

Server Side
What will be added server side is a sub to send the variable, and calling that sub wherever the HighIndex is calculated

so,
At the bottom of modTCP, add:
Code:
Sub SendHighIndex()
Dim I As Long
    For I = 1 To HighIndex
        Call SendDataTo(I, "HighIndex" & SEP_CHAR & HighIndex & SEP_CHAR & END_CHAR)
    Next I
End Sub

i know it LOOKs like it should be SendDataToAll, but for reasons God only knows, it did not work for me, so i did this instead...

now, do a search for

Code:
Call SetHighIndex
Anywhere you find that (There should be 2), add underneith:
Code:
Call SendHighIndex

Server Side Done!

Client Side
Basically, all the client side is, is a HandleData packet to recive the packet the server is sending, and the mindless replaceing of MAX_PLAYERS is most but not all places..., so without further adu...

in modGlobals, under:
Code:
' Used for parsing
Public SEP_CHAR As String * 1
Public END_CHAR As String * 1

add:
Code:
' Used for HighIndex
Public HighIndex As Long

now in modHandleData, under:
Code:
' :::::::::::::::::::
    ' :: Spells packet ::
    ' :::::::::::::::::::
    If (LCase(Parse(0)) = "spells") Then
        
        frmMirage.picPlayerSpells.Visible = True
        frmMirage.lstSpells.Clear
        
        ' Put spells known in player record
        For i = 1 To MAX_PLAYER_SPELLS
             Player(MyIndex).Spell(i) = Val(Parse(i))
             If Player(MyIndex).Spell(i)  0 Then
                 frmMirage.lstSpells.AddItem i & ": " & Trim(Spell(Player(MyIndex).Spell(i)).Name)
             Else
                 frmMirage.lstSpells.AddItem ""
             End If
        Next i
        
        frmMirage.lstSpells.ListIndex = 0
    End If

add:

Code:
' :::::::::::::::::::::::
    ' :: High Index Packet ::
    ' :::::::::::::::::::::::
    If LCase(Parse(0)) = "highindex" Then
        HighIndex = Val(Parse(1))
        Exit Sub
    End If

this recives the highindex packet.

Now in all the folowing places, you will have to change
Code:
For i = 1 to MAX_PLAYERS
to
Code:
For i = 1 to HighIndex


ModHandleData
===========
Check for map packet

modGameLogic
===========
GameLoop [all of them]
Function CanMove [4]
FindPlayer

there may be more... but it wont hurt if you left them out

Name: Optimize Looping
By: Dark//Dragon
Difficulty: 3/5
Version: 2

Notes:
Ok, in this tutorial, I assume you have already completed V1 of the tutorial, available HERE. What this tutorial is, is the extention onto that tutorial, where i extend the HighIndex variable over to the client, alowing for much optimised looping client side :wink:

As Always:
Back up your source before starting ANY tutorial!

Server Side
What will be added server side is a sub to send the variable, and calling that sub wherever the HighIndex is calculated

so,
At the bottom of modTCP, add:
Code:
Sub SendHighIndex()
Dim I As Long
    For I = 1 To HighIndex
        Call SendDataTo(I, "HighIndex" & SEP_CHAR & HighIndex & SEP_CHAR & END_CHAR)
    Next I
End Sub

i know it LOOKs like it should be SendDataToAll, but for reasons God only knows, it did not work for me, so i did this instead...

now, do a search for

Code:
Call SetHighIndex
Anywhere you find that (There should be 2), add underneith:
Code:
Call SendHighIndex

Server Side Done!

Client Side
Basically, all the client side is, is a HandleData packet to recive the packet the server is sending, and the mindless replaceing of MAX_PLAYERS is most but not all places..., so without further adu...

in modGlobals, under:
Code:
' Used for parsing
Public SEP_CHAR As String * 1
Public END_CHAR As String * 1

add:
Code:
' Used for HighIndex
Public HighIndex As Long

now in modHandleData, under:
Code:
' :::::::::::::::::::
    ' :: Spells packet ::
    ' :::::::::::::::::::
    If (LCase(Parse(0)) = "spells") Then
        
        frmMirage.picPlayerSpells.Visible = True
        frmMirage.lstSpells.Clear
        
        ' Put spells known in player record
        For i = 1 To MAX_PLAYER_SPELLS
             Player(MyIndex).Spell(i) = Val(Parse(i))
             If Player(MyIndex).Spell(i)  0 Then
                 frmMirage.lstSpells.AddItem i & ": " & Trim(Spell(Player(MyIndex).Spell(i)).Name)
             Else
                 frmMirage.lstSpells.AddItem ""
             End If
        Next i
        
        frmMirage.lstSpells.ListIndex = 0
    End If

add:

Code:
' :::::::::::::::::::::::
    ' :: High Index Packet ::
    ' :::::::::::::::::::::::
    If LCase(Parse(0)) = "highindex" Then
        HighIndex = Val(Parse(1))
        Exit Sub
    End If

this recives the highindex packet.

Now in all the folowing places, you will have to change
Code:
For i = 1 to MAX_PLAYERS
to
Code:
For i = 1 to HighIndex


ModHandleData
===========
Check for map packet

modGameLogic
===========
GameLoop [all of them]
Function CanMove [4]
FindPlayer

there may be more... but it wont hurt if you left them out
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)