Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimized Main Loop, Tizela
#1
I saw there were some other guy that did this, mine might be better, migth not. Also I havn't really looked through the Mirage Engine Source that much so this might not be complete but anyone knowing what I'm talking about should easily get this to work.

Add this under any global scope
Code:
Public   OnlinePlayerID(0 to MAX_PLAYERS) as integer
The OnlinePlayerID array will keep track of every player that's online and is on your map. This simply becasue its used throughout the mainloop.


Add this in the sub GameLoop, before the do while..
Code:
dim c as long

Code:
Do While InGame
        Tick = GetTickCount

Right after this (client side atleast) add this

Code:
For i = 1 To C
         OnlinePlayerId = 0
     Next i

Code:
c  = 0
    For i = 1 To MAX_PLAYERS
         If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex)Then
               OnlinePlayerID(C) = i
               C = C + 1
         End If
     Next i

Now we loop through all the players and find all online, and on our map and we place them in the right order in to our OnlinePlayerID array.

Then replace every
Code:
For i = 1 To MAX_PLAYERS
            If IsPlaying(i) Then
                Call BltPlayer(i)
            End If
        Next i

with

Code:
For i = 1 To MAX_PLAYERS
            If OnlinePlayerID(i) = 0 Then
                Exit For
            End If
            Call BltPlayer(OnlinePlayerID( i ))
        Next i


then

Code:
For i = 1 To MAX_PLAYERS
            If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
                Call BltPlayerName(i)
            End If
        Next i

to

Code:
For i = 1 To MAX_PLAYERS
            If OnlinePlayerID(i) = 0 Then
                Exit For
            End If
            Call BltPlayerName(OnlinePlayerID( i ))
        Next i

Looks like it loops through the entire player array, but it don't. As soon as it find the first empty integer of our OnlinePlayersID it quits the loop.

Also, change all these puppies too
Code:
For i = 1 To MAX_PLAYERS
                If IsPlaying(i) Then
                    If GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
                        If (GetPlayerX(i) = GetPlayerX(MyIndex)) And (GetPlayerY(i) = GetPlayerY(MyIndex) - 1) Then
                            CanMove = False
                        
                            ' Set the new direction if they weren't facing that direction
                            If d  DIR_UP Then
                                Call SendPlayerDir
                            End If
                            Exit Function
                        End If
                    End If
                End If
            Next i


to something like this

Code:
For i = 1 To MAX_PLAYERS
                If OnlinePlayerID(i) = 0 Then
                    Exit For
                End If
                        If (GetPlayerX(OnlinePlayerID(i)) = GetPlayerX(MyIndex)) And (GetPlayerY(OnlinePlayerID(i)) = GetPlayerY(MyIndex) - 1) Then
                            CanMove = False
                        
                            ' Set the new direction if they weren't facing that direction
                            If d  DIR_UP Then
                                Call SendPlayerDir
                            End If
                            Exit Function
                        End If
                    End If
            Next i
Again we can remove 2 if checks, since we know all players in this array is on our map, and is also online. Again exit the for loop if we reach the end.

Note! While I havn't treid this exact code out, I did something simlar with a 3.0.3 source I played around with, this hasn't been compiled there may be typos

Further Optimizing
Instead of looping through the players on your map to check if they still are there every frame then clearing it as my tutorial now does you can simply update it when people joins / enters the map. Thanks Robin for suggesting it. However, the vanilla MSE doesn't have a such function/packet and I'm to lazy to write one for you.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)