balliztik1 Posted July 16, 2007 Author Share Posted July 16, 2007 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 BooleanDim i As LongDim x As LongDim y As LongCanMove = TrueIf Player(MyIndex).Moving <> 0 Then CanMove = False Exit FunctionEnd If' Make sure they haven't just casted a spellIf Player(MyIndex).CastedSpell = YES Then If GetTickCount > Player(MyIndex).AttackTimer + 1000 Then Player(MyIndex).CastedSpell = NO Else CanMove = False Exit Function End IfEnd Ifx = GetPlayerX(MyIndex)y = GetPlayerY(MyIndex)If DirUp Then Call SetPlayerDir(MyIndex, DIR_UP) y = y - 1ElseIf DirDown Then Call SetPlayerDir(MyIndex, DIR_DOWN) y = y + 1ElseIf DirLeft Then Call SetPlayerDir(MyIndex, DIR_LEFT) x = x - 1Else Call SetPlayerDir(MyIndex, DIR_RIGHT) x = x + 1End IfCall SendPlayerDirIf y < 0 Then If Map(GetPlayerMap(MyIndex)).Up > 0 Then Call SendPlayerRequestNewMap(0) GettingMap = True End If CanMove = False Exit FunctionElseIf y > MAX_MAPY Then If Map(GetPlayerMap(MyIndex)).Down > 0 Then Call SendPlayerRequestNewMap(0) GettingMap = True End If CanMove = False Exit FunctionElseIf x < 0 Then If Map(GetPlayerMap(MyIndex)).Left > 0 Then Call SendPlayerRequestNewMap(0) GettingMap = True End If CanMove = False Exit FunctionElseIf x > MAX_MAPX Then If Map(GetPlayerMap(MyIndex)).Right > 0 Then Call SendPlayerRequestNewMap(0) GettingMap = True End If CanMove = False Exit FunctionEnd IfIf 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 FunctionEnd IfIf 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 = FalseEnd IfIf Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_GUILDBLOCK And Map(GetPlayerMap(MyIndex)).Tile(x, y).String1 <> GetPlayerGuild(MyIndex) Then CanMove = FalseEnd IfIf 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 IfEnd IfIf Map(GetPlayerMap(MyIndex)).Tile(x, y).Type = TILE_TYPE_WALKTHRU Then Exit FunctionElse 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 iEnd IfFor 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 IfNext iIf CanAttributeNPCMove(DIR_UP) = False Then CanMove = False Exit FunctionEnd IfEnd 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 + 1End Select'Check for edge of mapIf y < 0 Then If Map(GetPlayerMap(index)).Up > 0 Then Call PlayerWarp(index, Map(GetPlayerMap(index)).Up, GetPlayerX(index), MAX_MAPY) Moved = YES End IfElseIf y > MAX_MAPY Then If Map(GetPlayerMap(index)).Down > 0 Then Call PlayerWarp(index, Map(GetPlayerMap(index)).Down, GetPlayerX(index), 0) Moved = YES End IfElseIf x < 0 Then If Map(GetPlayerMap(index)).Left > 0 Then Call PlayerWarp(index, Map(GetPlayerMap(index)).Left, MAX_MAPX, GetPlayerY(index)) Moved = YES End IfElseIf x > MAX_MAPX Then If Map(GetPlayerMap(index)).Right > 0 Then Call PlayerWarp(index, Map(GetPlayerMap(index)).Right, 0, GetPlayerY(index)) Moved = YES End IfEnd If' Check to make sure that the tile is walkableIf 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 tileIf 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 IfEnd If```> Was: 572 lines of code> Changed to: 481 lines of code> Percent Change: ~15.9% reduction Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now