Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Trimming Some Code


balliztik1
 Share

Recommended Posts

Well, there's some things in the Eclipse source that I feel could be altered so that it does the same thing, without requiring as much code. I will add any changes that I find to this list.

**Function CanMove**
It was bad. This function had 26 (!) checks for which direction that the player is facing and 4 completely different sets of code for each button that could be pushed (up, down, left or right). I completely got rid of the direction checks and had it run the same code, no matter which button is pushed.

Client side, find _Function CanMove() As Boolean_ and change the entire function to this:
```
Function CanMove() As Boolean
Dim i As Long
Dim x As Long
Dim y As Long

CanMove = True

If Player(MyIndex).Moving <> 0 Then
  CanMove = False
  Exit Function
End If

' Make sure they haven't just casted a spell

If Player(MyIndex).CastedSpell = YES Then
  If GetTickCount > Player(MyIndex).AttackTimer + 1000 Then
    Player(MyIndex).CastedSpell = NO
    Else
    CanMove = False
    Exit Function
  End If
End If

x = GetPlayerX(MyIndex)
y = GetPlayerY(MyIndex)

If DirUp Then
  Call SetPlayerDir(MyIndex, DIR_UP)
  y = y - 1
ElseIf DirDown Then
  Call SetPlayerDir(MyIndex, DIR_DOWN)
  y = y + 1
ElseIf DirLeft Then
  Call SetPlayerDir(MyIndex, DIR_LEFT)
  x = x - 1
Else
  Call SetPlayerDir(MyIndex, DIR_RIGHT)
  x = x + 1
End If

Call SendPlayerDir

If y < 0 Then
  If Map(GetPlayerMap(MyIndex)).Up > 0 Then
    Call SendPlayerRequestNewMap(0)
    GettingMap = True
  End If
  CanMove = False
  Exit Function
ElseIf y > MAX_MAPY Then
  If Map(GetPlayerMap(MyIndex)).Down > 0 Then
    Call SendPlayerRequestNewMap(0)
    GettingMap = True
  End If
  CanMove = False
  Exit Function
ElseIf x < 0 Then
  If Map(GetPlayerMap(MyIndex)).Left > 0 Then
    Call SendPlayerRequestNewMap(0)
    GettingMap = True
  End If
  CanMove = False
  Exit Function
ElseIf x > MAX_MAPX Then
  If Map(GetPlayerMap(MyIndex)).Right > 0 Then
    Call SendPlayerRequestNewMap(0)
    GettingMap = True
  End If
  CanMove = False
  Exit Function
End If

If Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_BLOCKED Or Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_SIGN Or Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_ROOFBLOCK Or Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_SKILL Then
  CanMove = False
  Exit Function
End If

If Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_CBLOCK Then
  If Map(GetPlayerMap(MyIndex)).Tile(x, y).Data1 = Player(MyIndex).Class Then Exit Function
  If Map(GetPlayerMap(MyIndex)).Tile(x, y).Data2 = Player(MyIndex).Class Then Exit Function
  If Map(GetPlayerMap(MyIndex)).Tile(x, y).Data3 = Player(MyIndex).Class Then Exit Function
  CanMove = False
End If

If Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_GUILDBLOCK And Map(GetPlayerMap(MyIndex)).Tile(x, y).String1 <> GetPlayerGuild(MyIndex) Then
  CanMove = False
End If

If Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_KEY Or Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_DOOR Then
  If TempTile(x, y).DoorOpen = NO Then
  CanMove = False
  Exit Function
  End If
End If

If Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_WALKTHRU Then
  Exit Function
Else
  For i = 1 To MAX_PLAYERS
    If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
      If (GetPlayerX(i) = x) And (GetPlayerY(i) = y) Then
        CanMove = False
        Exit Function
      End If
    End If
  Next i
End If

For i = 1 To MAX_MAP_NPCS
  If MapNpc(i).num > 0 Then
    If (MapNpc(i).x = x) And (MapNpc(i).y = y) Then
      CanMove = False
      Exit Function
    End If
  End If
Next i

If CanAttributeNPCMove(DIR_UP) = False Then
  CanMove = False
  Exit Function
End If

End Function
```

> Was: 554 lines of code
> Changed to: 124 lines of code
> Percent Change: ~77.6% reduction

**Sub SetSpeed**
A simple edit. The original sub was doing exponents backwards by means of division with remainders (eeew) and do/loop, when using a logarithmic function can cut it down to a single if-statement.

Server side, find _Sub SetSpeed_ and find this chunk of code in it:
```
        div = 2
        Do While div <= 32
            If Not (Speed / div) = 1 Then
                If (Speed / div) Mod 2 = 0 Then
                    div = div * 2
                Else
                    Exit Sub 'NOT a power of 2!
                End If
            Else
                Exit Do 'WAS a power of 2!
            End If
        Loop
```
Replace that with this:
```
        If Int(Log(Speed) / Log(2)) <> Log(Speed) / Log(2) Then
        Exit Sub
        End If
```
Finally, you no longer need the variable _div_, so delete this line:
```
  Dim div As Integer 'Divisor for power of 2 loop
```

> Was: 35 lines of code
> Changed to: 21 lines of code
> Percent Change: ~40% reduction

**Sub PlayerMove**
Another case of unneeded directional checks. This one isn't as bad as before, but is still in need of changing.

Server side, find _Sub PlayerMove_ and find this chunk of code in it:
```
    Select Case Dir
     Case DIR_UP
        ' Check to make sure not outside of boundries

        If GetPlayerY(index) > 0 Then
            ' Check to make sure that the tile is walkable

            If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type <> TILE_TYPE_BLOCKED And Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type <> TILE_TYPE_ROOFBLOCK Then
                If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type = TILE_TYPE_GUILDBLOCK And Trim(Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).String1) <> Trim(GetPlayerGuild(index)) Then
                    Exit Sub
                End If

                ' Check to see if the tile is a skill tile

                If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type <> TILE_TYPE_SKILL Then
                    ' Check to see if the tile is a key and if it is check if its opened

                    If (Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type <> TILE_TYPE_KEY Or Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type <> TILE_TYPE_DOOR) Or ((Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type = TILE_TYPE_DOOR Or Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) - 1).Type = TILE_TYPE_KEY) And TempTile(GetPlayerMap(index)).DoorOpen(GetPlayerX(index), GetPlayerY(index) - 1) = YES) Then
                        Call SetPlayerY(index, GetPlayerY(index) - 1)

                        packet = PacketID.PlayerMove & SEP_CHAR & index & SEP_CHAR & GetPlayerX(index) & SEP_CHAR & GetPlayerY(index) & SEP_CHAR & GetPlayerDir(index) & SEP_CHAR & Movement & SEP_CHAR & END_CHAR
                        Call SendDataToMapBut(index, GetPlayerMap(index), packet)
                        Moved = YES
                    End If

                End If
            End If
         Else
            ' Check to see if we can move them to the another map

            If Map(GetPlayerMap(index)).Up > 0 Then
                Call PlayerWarp(index, Map(GetPlayerMap(index)).Up, GetPlayerX(index), MAX_MAPY)
                Moved = YES
            End If

        End If

     Case DIR_DOWN
        ' Check to make sure not outside of boundries

        If GetPlayerY(index) < MAX_MAPY Then
            ' Check to make sure that the tile is walkable

            If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type <> TILE_TYPE_BLOCKED And Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type <> TILE_TYPE_ROOFBLOCK Then
                If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type = TILE_TYPE_GUILDBLOCK And Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).String1 <> GetPlayerGuild(index) Then
                    Exit Sub
                End If

                If Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type <> TILE_TYPE_SKILL Then
                    ' Check to see if the tile is a key and if it is check if its opened

                    If (Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type <> TILE_TYPE_KEY Or Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type <> TILE_TYPE_DOOR) Or ((Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type = TILE_TYPE_DOOR Or Map(GetPlayerMap(index)).tile(GetPlayerX(index), GetPlayerY(index) + 1).Type = TILE_TYPE_KEY) And TempTile(GetPlayerMap(index)).DoorOpen(GetPlayerX(index), GetPlayerY(index) + 1) = YES) Then
                        Call SetPlayerY(index, GetPlayerY(index) + 1)

                        packet = PacketID.PlayerMove & SEP_CHAR & index & SEP_CHAR & GetPlayerX(index) & SEP_CHAR & GetPlayerY(index) & SEP_CHAR & GetPlayerDir(index) & SEP_CHAR & Movement & SEP_CHAR & END_CHAR
                        Call SendDataToMapBut(index, GetPlayerMap(index), packet)
                        Moved = YES
                    End If

                End If
            End If
         Else
            ' Check to see if we can move them to the another map

            If Map(GetPlayerMap(index)).Down > 0 Then
                Call PlayerWarp(index, Map(GetPlayerMap(index)).Down, GetPlayerX(index), 0)
                Moved = YES
            End If

        End If

     Case DIR_LEFT
        ' Check to make sure not outside of boundries

        If GetPlayerX(index) > 0 Then
            ' Check to make sure that the tile is walkable

            If Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type <> TILE_TYPE_BLOCKED And Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type <> TILE_TYPE_ROOFBLOCK Then
                If Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type = TILE_TYPE_GUILDBLOCK And Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).String1 <> GetPlayerGuild(index) Then
                    Exit Sub
                End If

                'Check to see if the tile is a skill tile

                If Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type <> TILE_TYPE_SKILL Then
                    ' Check to see if the tile is a key and if it is check if its opened

                    If (Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type <> TILE_TYPE_KEY Or Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type <> TILE_TYPE_DOOR) Or ((Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type = TILE_TYPE_DOOR Or Map(GetPlayerMap(index)).tile(GetPlayerX(index) - 1, GetPlayerY(index)).Type = TILE_TYPE_KEY) And TempTile(GetPlayerMap(index)).DoorOpen(GetPlayerX(index) - 1, GetPlayerY(index)) = YES) Then
                        'BARON DEBUG TIME - CAUSE OF RTE 9'S ABOVE ?
                        Call SetPlayerX(index, GetPlayerX(index) - 1)

                        packet = PacketID.PlayerMove & SEP_CHAR & index & SEP_CHAR & GetPlayerX(index) & SEP_CHAR & GetPlayerY(index) & SEP_CHAR & GetPlayerDir(index) & SEP_CHAR & Movement & SEP_CHAR & END_CHAR
                        Call SendDataToMapBut(index, GetPlayerMap(index), packet)
                        Moved = YES
                    End If

                End If
            End If
         Else
            ' Check to see if we can move them to the another map

            If Map(GetPlayerMap(index)).left > 0 Then
                Call PlayerWarp(index, Map(GetPlayerMap(index)).left, MAX_MAPX, GetPlayerY(index))
                Moved = YES
            End If

        End If

     Case DIR_RIGHT
        ' Check to make sure not outside of boundries

        If GetPlayerX(index) < MAX_MAPX Then
            ' Check to make sure that the tile is walkable

            If Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type <> TILE_TYPE_BLOCKED And Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type <> TILE_TYPE_ROOFBLOCK Then
                If Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type = TILE_TYPE_GUILDBLOCK And Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).String1 <> GetPlayerGuild(index) Then
                    Exit Sub
                End If

                ' Check for skill tile

                If Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type <> TILE_TYPE_SKILL Then
                    ' Check to see if the tile is a key and if it is check if its opened

                    If (Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type <> TILE_TYPE_KEY Or Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type <> TILE_TYPE_DOOR) Or ((Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type = TILE_TYPE_DOOR Or Map(GetPlayerMap(index)).tile(GetPlayerX(index) + 1, GetPlayerY(index)).Type = TILE_TYPE_KEY) And TempTile(GetPlayerMap(index)).DoorOpen(GetPlayerX(index) + 1, GetPlayerY(index)) = YES) Then
                        Call SetPlayerX(index, GetPlayerX(index) + 1)

                        packet = PacketID.PlayerMove & SEP_CHAR & index & SEP_CHAR & GetPlayerX(index) & SEP_CHAR & GetPlayerY(index) & SEP_CHAR & GetPlayerDir(index) & SEP_CHAR & Movement & SEP_CHAR & END_CHAR
                        Call SendDataToMapBut(index, GetPlayerMap(index), packet)
                        Moved = YES
                    End If

                End If
            End If
         Else
            ' Check to see if we can move them to the another map

            If Map(GetPlayerMap(index)).Right > 0 Then
                Call PlayerWarp(index, Map(GetPlayerMap(index)).Right, 0, GetPlayerY(index))
                Moved = YES
            End If

        End If
    End Select
```
Change that to this:
```
x = GetPlayerX(index)
y = GetPlayerY(index)

Select Case GetPlayerDir(index)
Case 0
  y = y - 1
Case 1
  y = y + 1
Case 2
  x = x - 1
Case 3
  x = x + 1
End Select

'Check for edge of map
If y < 0 Then
  If Map(GetPlayerMap(index)).Up > 0 Then
    Call PlayerWarp(index, Map(GetPlayerMap(index)).Up, GetPlayerX(index), MAX_MAPY)
    Moved = YES
  End If
ElseIf y > MAX_MAPY Then
  If Map(GetPlayerMap(index)).Down > 0 Then
    Call PlayerWarp(index, Map(GetPlayerMap(index)).Down, GetPlayerX(index), 0)
    Moved = YES
  End If
ElseIf x < 0 Then
  If Map(GetPlayerMap(index)).Left > 0 Then
    Call PlayerWarp(index, Map(GetPlayerMap(index)).Left, MAX_MAPX, GetPlayerY(index))
    Moved = YES
  End If
ElseIf x > MAX_MAPX Then
  If Map(GetPlayerMap(index)).Right > 0 Then
    Call PlayerWarp(index, Map(GetPlayerMap(index)).Right, 0, GetPlayerY(index))
    Moved = YES
  End If
End If

' Check to make sure that the tile is walkable
If Map(GetPlayerMap(index)).tile(x, y).Type <> TILE_TYPE_BLOCKED And Map(GetPlayerMap(index)).tile(x, y).Type <> TILE_TYPE_ROOFBLOCK Then
  If Map(GetPlayerMap(index)).tile(x, y).Type = TILE_TYPE_GUILDBLOCK And Trim(Map(GetPlayerMap(index)).tile(x, y).String1) <> Trim(GetPlayerGuild(index)) Then
    Exit Sub
  End If

' Check to see if the tile is a skill tile
If Map(GetPlayerMap(index)).tile(x, y).Type <> TILE_TYPE_SKILL Then
  ' Check to see if the tile is a key and if it is check if its opened
  If (Map(GetPlayerMap(index)).tile(x, y).Type <> TILE_TYPE_KEY Or Map(GetPlayerMap(index)).tile(x, y).Type <> TILE_TYPE_DOOR) Or ((Map(GetPlayerMap(index)).tile(x, y).Type = TILE_TYPE_DOOR Or Map(GetPlayerMap(index)).tile(x, y).Type = TILE_TYPE_KEY) And TempTile(GetPlayerMap(index)).DoorOpen(x, y) = YES) Then
    Call SetPlayerY(index, y)
    Call SetPlayerX(index, x)
    packet = "playermove" & SEP_CHAR & index & SEP_CHAR & GetPlayerX(index) & SEP_CHAR & GetPlayerY(index) & SEP_CHAR & GetPlayerDir(index) & SEP_CHAR & Movement & SEP_CHAR & END_CHAR
    Call SendDataToMapBut(index, GetPlayerMap(index), packet)
    Moved = YES
  End If
End If
```

> Was: 572 lines of code
> Changed to: 481 lines of code
> Percent Change: ~15.9% reduction
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...