Mirage Source

Free ORPG making software.
It is currently Sat Apr 27, 2024 3:37 pm

All times are UTC


Forum rules


Make sure your tutorials are kept up to date with the latest MS4 releases.



Post new topic Reply to topic  [ 1591 posts ]  Go to page 1, 2, 3, 4, 5 ... 64  Next
Author Message
PostPosted: Tue Sep 23, 2008 5:02 am 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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 :)

_________________
Image


Last edited by JokeofWeek on Fri Oct 10, 2008 8:28 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Oct 10, 2008 7:43 pm 
Offline
Pro
User avatar

Joined: Tue Nov 13, 2007 2:42 pm
Posts: 509
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.


Top
 Profile  
 
PostPosted: Fri Oct 10, 2008 8:27 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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 :)

_________________
Image


Top
 Profile  
 
PostPosted: Wed Dec 01, 2021 7:57 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинйоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфо
инфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоинфоsemiasphalticflux.ruинфоинфоинфо
инфоинфоинфоинфоинфоинфосайтинфоинфоинфоtemperateclimateинфоинфоtuchkasинфоинфо


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:38 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Econ


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:39 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
57.1


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:40 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Bett


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:41 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Bett


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:42 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Sick


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:44 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Forb


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:45 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Intr


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:46 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Emma


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:47 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Homo


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:48 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Wern


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:49 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Wind


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:50 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Prim


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:51 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Extr


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:52 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Exce


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:54 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Gard


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:55 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Anto


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:56 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Brit


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:57 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Riso


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:58 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Shau


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 8:59 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Bren


Top
 Profile  
 
PostPosted: Wed Dec 29, 2021 9:00 am 
Online
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 489441
Supe


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1591 posts ]  Go to page 1, 2, 3, 4, 5 ... 64  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 40 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group