Hey guys
Here are just a few useful functions I've made lately
Enjoy
Code:
Public Function IsInRange(ByVal TestX As Integer, ByVal TestY As Integer, ByVal X As Byte, ByVal Y As Byte, ByVal Range As Byte) As Boolean
IsInRange = False
TestX = Sqr(((TestX - X) * (TestX - X)) + ((TestY - Y) * (TestY - Y)))
If TestX <= Range Then
IsInRange = True
Exit Function
End If
End Function
What this does is checks whether a tile is within a defined range from another tile. Usage :
Quote:
boolTest = IsInRange(GetPlayerX(index),GetPlayerY(index),HealX, HealY,3)
What this would do is check whether the player was within 3 squares (also includes diagonally and such
) from the Heal X/Y location
-------------------------------------------Code:
Public Function GetRange(ByVal TestX As Long, ByVal TestY As Long, ByVal X As Long, ByVal Y As Long, ByVal Range As Long) As Byte
GetRange = Range + 1
TestX = Sqr(((TestX - X) * (TestX - X)) + ((TestY - Y) * (TestY - Y)))
If TestX <= Range Then
GetRange = TestX
Exit Function
End If
End Function
This is similar to the above function, what it does is just returns how many squares away the Tested values are. Same usage as previous function.
-------------------------------------------Code:
Public Function EnDecryptData(ByVal oldData As String) As String
Dim tmpByte() As Byte
Dim i As Byte
If LenB(oldData) = 0 Then Exit Function
tmpByte = StrConv((oldData), vbFromUnicode)
For i = 0 To UBound(tmpByte)
If i Mod 2 = 1 Then
tmpByte(i) = tmpByte(i) Xor 5
Else
tmpByte(i) = tmpByte(i) Xor 10
End If
Next i
EnDecryptData = StrConv(tmpByte, vbUnicode)
Erase tmpByte
End Function
All this does is encrypts a string using simple XOR encryption, and adds abit of security by xoring every second letter with a different number. Make sure you change the 5 and the 10 if you use this in your source
Usage :
Quote:
OriginalString = EnDecryptData(EnDecryptData(ByVal oldString As String))
What's useful about this function is you don't need a seperate decrypting function as you can just re-use the encrypting function to reverse it
-------------------------------------------Code:
Public Sub PreAllocate(ByRef Buffer() As Byte, ByVal ByteLen As Byte)
ReDim Buffer(ByteLen) As Byte
End Sub
Public Sub AddByteToSetBuffer(ByRef Buffer() As Byte, ByVal vData As Byte, ByRef WriteHead As Integer)
Call CopyMemory(Buffer(WriteHead), vData, 1)
WriteHead = WriteHead + 1
End Sub
Public Sub AddIntegerToSetBuffer(ByRef Buffer() As Byte, vData As Integer, ByRef WriteHead As Integer)
Call CopyMemory(Buffer(WriteHead), vData, 2
WriteHead = WriteHead + 2
End Sub
Public Sub AddLongToSetBuffer(ByRef Buffer() As Byte, vData As Long, ByRef WriteHead As Integer)
Call CopyMemory(Buffer(WriteHead), vData, 4)
WriteHead = WriteHead + 4
End Sub
Now what this is is basically an optimized version of Verrigan's byte buffer system. This is mainly for numeric-only packets. What you do is define a variable, I for example, which will keep the current location to add the bytes, and pass it along with your buffer in one of the functions. Make sure you pre-allocate enough room before though. This saves you from constantly redimming every time you add something, and is very useful when you know the size of the packet. For example :
Quote:
Dim Buffer() as byte, I as integer
PreAllocate Buffer, 3
AddByteToBuffer Buffer, 5, I
AddByteToBuffer Buffer, 32, I
AddIntegerToBuffer Buffer, 20000, I
Now, you might have oticed in PreAllocate, we only used 3 as the size of the buffer. Now remember that the Buffer starts at 0, so make sure you do ( Size in Bytes - 1 ), and keep passing I as a variable as the writing location without modifying it
------------------------------------------- I'll add more every once in a while