Mirage Source
http://www.miragesource.net/forums/

[Misc] Some useful functions...
http://www.miragesource.net/forums/viewtopic.php?f=183&t=4434
Page 1 of 64

Author:  JokeofWeek [ Tue Sep 23, 2008 5:02 am ]
Post subject:  [Misc] Some useful functions...

Hey guys :) Here are just a few useful functions I've made lately :) Enjoy :D

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 :)

Author:  Jacob [ Fri Oct 10, 2008 7:43 pm ]
Post subject:  Re: [Misc] Some useful functions...

Quick question

Shouldn't the AddInt and AddLong be like this:
Code:
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

Code:
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


Otherwise:
Code:
Public Sub AddIntegerToSetBuffer(ByRef Buffer() As Byte, vData As Integer, ByRef WriteHead As Integer)
  Dim tBytes() As Byte

  ReDim tBytes(UBound(Buffer))
 
  Call CopyMemory(tBytes(WriteHead), vData, 2)
 
  Buffer = tBytes

  WriteHead = WriteHead + 2
End Sub


You'd be overwriting anything you've written to the Buffer() before that call because it's setting Buffer = tBytes. tBytes is never given the data from Buffer.

Author:  JokeofWeek [ Fri Oct 10, 2008 8:27 pm ]
Post subject:  Re: [Misc] Some useful functions...

Dugor wrote:
Quick question

Shouldn't the AddInt and AddLong be like this:
Code:
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

Code:
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


Otherwise:
Code:
Public Sub AddIntegerToSetBuffer(ByRef Buffer() As Byte, vData As Integer, ByRef WriteHead As Integer)
  Dim tBytes() As Byte

  ReDim tBytes(UBound(Buffer))
 
  Call CopyMemory(tBytes(WriteHead), vData, 2)
 
  Buffer = tBytes

  WriteHead = WriteHead + 2
End Sub


You'd be overwriting anything you've written to the Buffer() before that call because it's setting Buffer = tBytes. tBytes is never given the data from Buffer.


Ah true, fixed :) Thanks Dugor :)

Author:  wanai [ Wed Dec 01, 2021 7:57 am ]
Post subject:  Re: [Misc] Some useful functions...

инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинйоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоsemiasphalticflux.ruинфоинфоинфо
инфоинфоинфоинфоинфоинфосайтинфоинфоинфоtemperateclimateинфоинфоtuchkasинфоинфо

Author:  wanai [ Wed Dec 29, 2021 8:38 am ]
Post subject:  Re: [Misc] Some useful functions...

Econ

Author:  wanai [ Wed Dec 29, 2021 8:39 am ]
Post subject:  Re: [Misc] Some useful functions...

57.1

Author:  wanai [ Wed Dec 29, 2021 8:40 am ]
Post subject:  Re: [Misc] Some useful functions...

Bett

Author:  wanai [ Wed Dec 29, 2021 8:41 am ]
Post subject:  Re: [Misc] Some useful functions...

Bett

Author:  wanai [ Wed Dec 29, 2021 8:42 am ]
Post subject:  Re: [Misc] Some useful functions...

Sick

Author:  wanai [ Wed Dec 29, 2021 8:44 am ]
Post subject:  Re: [Misc] Some useful functions...

Forb

Author:  wanai [ Wed Dec 29, 2021 8:45 am ]
Post subject:  Re: [Misc] Some useful functions...

Intr

Author:  wanai [ Wed Dec 29, 2021 8:46 am ]
Post subject:  Re: [Misc] Some useful functions...

Emma

Author:  wanai [ Wed Dec 29, 2021 8:47 am ]
Post subject:  Re: [Misc] Some useful functions...

Homo

Author:  wanai [ Wed Dec 29, 2021 8:48 am ]
Post subject:  Re: [Misc] Some useful functions...

Wern

Author:  wanai [ Wed Dec 29, 2021 8:49 am ]
Post subject:  Re: [Misc] Some useful functions...

Wind

Author:  wanai [ Wed Dec 29, 2021 8:50 am ]
Post subject:  Re: [Misc] Some useful functions...

Prim

Author:  wanai [ Wed Dec 29, 2021 8:51 am ]
Post subject:  Re: [Misc] Some useful functions...

Extr

Author:  wanai [ Wed Dec 29, 2021 8:52 am ]
Post subject:  Re: [Misc] Some useful functions...

Exce

Author:  wanai [ Wed Dec 29, 2021 8:54 am ]
Post subject:  Re: [Misc] Some useful functions...

Gard

Author:  wanai [ Wed Dec 29, 2021 8:55 am ]
Post subject:  Re: [Misc] Some useful functions...

Anto

Author:  wanai [ Wed Dec 29, 2021 8:56 am ]
Post subject:  Re: [Misc] Some useful functions...

Brit

Author:  wanai [ Wed Dec 29, 2021 8:57 am ]
Post subject:  Re: [Misc] Some useful functions...

Riso

Author:  wanai [ Wed Dec 29, 2021 8:58 am ]
Post subject:  Re: [Misc] Some useful functions...

Shau

Author:  wanai [ Wed Dec 29, 2021 8:59 am ]
Post subject:  Re: [Misc] Some useful functions...

Bren

Author:  wanai [ Wed Dec 29, 2021 9:00 am ]
Post subject:  Re: [Misc] Some useful functions...

Supe

Page 1 of 64 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/