Print Page | Close Window

Optimizing Looping (v1 & v2)

Printed From: Mirage Source
Category: Tutorials
Forum Name: Approved Tutorials
Forum Discription: All tutorials shown to actually work with MSE are moved here.
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=47
Printed Date: 20 December 2006 at 6:01pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Optimizing Looping (v1 & v2)
Posted By: Sync
Subject: Optimizing Looping (v1 & v2)
Date Posted: 07 February 2006 at 6:22pm
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:
' Used for logging
Public ServerLog As Boolean


add:

' 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:
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
             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:
             ' Set The High Index
             Call SetHighIndex


this will set the high index whenever anyone connects

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


add
        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

For I = 1 to MAX_PLAYERS
to
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 http://www.miragesource.com/forums/index.php?topic=3784.0 - 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:
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

Call SetHighIndex

Anywhere you find that (There should be 2), add underneith:
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:
' Used for parsing
Public SEP_CHAR As String * 1
Public END_CHAR As String * 1


add:
' Used for HighIndex
Public HighIndex As Long


now in modHandleData, under:
    ' :::::::::::::::::::
    ' :: 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 "<free spells slot>"
             End If
        Next i
       
        frmMirage.lstSpells.ListIndex = 0
    End If


add:

    ' :::::::::::::::::::::::
    ' :: 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
For I = 1 to MAX_PLAYERS
to
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 http://www.miragesource.com/forums/index.php?topic=3784.0 - 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:
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

Call SetHighIndex

Anywhere you find that (There should be 2), add underneith:
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:
' Used for parsing
Public SEP_CHAR As String * 1
Public END_CHAR As String * 1


add:
' Used for HighIndex
Public HighIndex As Long


now in modHandleData, under:
    ' :::::::::::::::::::
    ' :: 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 "<free spells slot>"
             End If
        Next i
       
        frmMirage.lstSpells.ListIndex = 0
    End If


add:

    ' :::::::::::::::::::::::
    ' :: 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
For I = 1 to MAX_PLAYERS
to
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




Replies:
Posted By: Sync
Date Posted: 07 February 2006 at 6:23pm
Approved .. hmm



Posted By: Nevermore
Date Posted: 28 February 2006 at 9:05pm
does this work with greentail?


Posted By: Misunderstood
Date Posted: 28 February 2006 at 9:40pm
We dunno, we dont use greentail, you can always backup and try.


Posted By: Nevermore
Date Posted: 01 March 2006 at 8:29pm
it worked, nice tutorial


Posted By: SoccerPeter
Date Posted: 04 March 2006 at 4:48pm
Couldn't you just a do a find/replace all with

Quote:
For I = 1 to MAX_PLAYERS
to
For I = 1 to HighIndex


I think that would make it alot easier, and yet another way cntrl f is your friend.


Posted By: funkynut
Date Posted: 04 March 2006 at 5:46pm
ctrl F4?

What does that do?  Or do you mean Ctrl F or Alt F4

O and i'm not sure (Might be wrong) but there could be parts where it needs to scan all player spots


Posted By: Dark Dragon
Date Posted: 09 March 2006 at 4:41pm
No, well if you do, it will work, but make shure to exclude the code that sets high index



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info