Mirage Source
Clear Player - Printable Version

+- Mirage Source (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Resources (https://mirage-engine.uk/forums/forumdisplay.php?fid=49)
+---- Thread: Clear Player (/showthread.php?tid=428)



Clear Player - William - 25-11-2006

If you have a unedited ClearPlayer, replace this:
Code:
Sub ClearPlayer(ByVal Index As Long)
Dim i As Long
Dim n As Long

    Player(Index).Login = ""
    Player(Index).Password = ""
    
    For i = 1 To MAX_CHARS
        Player(Index).Char(i).Name = ""
        Player(Index).Char(i).Class = 0
        Player(Index).Char(i).Level = 0
        Player(Index).Char(i).Sprite = 0
        Player(Index).Char(i).Exp = 0
        Player(Index).Char(i).Access = 0
        Player(Index).Char(i).PK = NO
        Player(Index).Char(i).POINTS = 0
        Player(Index).Char(i).Guild = 0
        
        Player(Index).Char(i).HP = 0
        Player(Index).Char(i).MP = 0
        Player(Index).Char(i).SP = 0
        
        Player(Index).Char(i).STR = 0
        Player(Index).Char(i).DEF = 0
        Player(Index).Char(i).SPEED = 0
        Player(Index).Char(i).MAGI = 0
        
        For n = 1 To MAX_INV
            Player(Index).Char(i).Inv(n).Num = 0
            Player(Index).Char(i).Inv(n).Value = 0
            Player(Index).Char(i).Inv(n).Dur = 0
        Next n
        
        For n = 1 To MAX_PLAYER_SPELLS
            Player(Index).Char(i).Spell(n) = 0
        Next n
        
        Player(Index).Char(i).ArmorSlot = 0
        Player(Index).Char(i).WeaponSlot = 0
        Player(Index).Char(i).HelmetSlot = 0
        Player(Index).Char(i).ShieldSlot = 0
        
        Player(Index).Char(i).Map = 0
        Player(Index).Char(i).x = 0
        Player(Index).Char(i).y = 0
        Player(Index).Char(i).Dir = 0
        
        ' Temporary vars
        Player(Index).Buffer = ""
        Player(Index).IncBuffer = ""
        Player(Index).CharNum = 0
        Player(Index).InGame = False
        Player(Index).AttackTimer = 0
        Player(Index).DataTimer = 0
        Player(Index).DataBytes = 0
        Player(Index).DataPackets = 0
        Player(Index).PartyPlayer = 0
        Player(Index).InParty = 0
        Player(Index).Target = 0
        Player(Index).TargetType = 0
        Player(Index).CastedSpell = NO
        Player(Index).PartyStarter = NO
        Player(Index).GettingMap = NO
    Next i
End Sub
With this:
Code:
Sub ClearPlayer(ByVal Index As Long)
Dim i As Long
Dim n As Long

    Player(Index).Login = ""
    Player(Index).Password = ""
    
    For i = 1 To MAX_CHARS
        Player(Index).Char(i).Name = ""
        Player(Index).Char(i).Class = 0
        Player(Index).Char(i).Level = 0
        Player(Index).Char(i).Sprite = 0
        Player(Index).Char(i).Exp = 0
        Player(Index).Char(i).Access = 0
        Player(Index).Char(i).PK = NO
        Player(Index).Char(i).POINTS = 0
        Player(Index).Char(i).Guild = 0
        
        Player(Index).Char(i).HP = 0
        Player(Index).Char(i).MP = 0
        Player(Index).Char(i).SP = 0
        
        Player(Index).Char(i).STR = 0
        Player(Index).Char(i).DEF = 0
        Player(Index).Char(i).SPEED = 0
        Player(Index).Char(i).MAGI = 0
        
        For n = 1 To MAX_INV
            Player(Index).Char(i).Inv(n).Num = 0
            Player(Index).Char(i).Inv(n).Value = 0
            Player(Index).Char(i).Inv(n).Dur = 0
        Next n
        
        For n = 1 To MAX_PLAYER_SPELLS
            Player(Index).Char(i).Spell(n) = 0
        Next n
        
        Player(Index).Char(i).ArmorSlot = 0
        Player(Index).Char(i).WeaponSlot = 0
        Player(Index).Char(i).HelmetSlot = 0
        Player(Index).Char(i).ShieldSlot = 0
        
        Player(Index).Char(i).Map = 0
        Player(Index).Char(i).x = 0
        Player(Index).Char(i).y = 0
        Player(Index).Char(i).Dir = 0
    Next i
    ' Temporary vars
        Player(Index).Buffer = ""
        Player(Index).IncBuffer = ""
        Player(Index).CharNum = 0
        Player(Index).InGame = False
        Player(Index).AttackTimer = 0
        Player(Index).DataTimer = 0
        Player(Index).DataBytes = 0
        Player(Index).DataPackets = 0
        Player(Index).PartyPlayer = 0
        Player(Index).InParty = 0
        Player(Index).Target = 0
        Player(Index).TargetType = 0
        Player(Index).CastedSpell = NO
        Player(Index).PartyStarter = NO
        Player(Index).GettingMap = NO
End Sub

The change is that you put this:
Code:
' Temporary vars
        Player(Index).Buffer = ""
        Player(Index).IncBuffer = ""
        Player(Index).CharNum = 0
        Player(Index).InGame = False
        Player(Index).AttackTimer = 0
        Player(Index).DataTimer = 0
        Player(Index).DataBytes = 0
        Player(Index).DataPackets = 0
        Player(Index).PartyPlayer = 0
        Player(Index).InParty = 0
        Player(Index).Target = 0
        Player(Index).TargetType = 0
        Player(Index).CastedSpell = NO
        Player(Index).PartyStarter = NO
        Player(Index).GettingMap = NO
Outside the For Loop, because that part of code doesn't have a 'i' in it Tongue


- Spodi - 25-11-2006

How about this:

Code:
Sub ClearPlayer(ByVal Index As Long)
Dim EmptyPlayer As Player 'Not sure if Player is the right variable here - you want the same variable type as used for Player()

Player(Index) = EmptyPlayer
End Sub

Done. :wink:


- William - 25-11-2006

Code:
Dim EmptyPlayer as PlayerRec

Does that really work? Seems logical though Tongue


- Spodi - 25-11-2006

Works just fine. It is how I do it in vbGORE. :wink:


- William - 25-11-2006

Seems cool Smile Ill use it Tongue


- Obsidian - 26-11-2006

Yeah that works. It's how i've been doing it for a while... after i rewrote all the MS stuff in binary i realized it was easier to clear everything that way than it was to go back and do it all by hand. Smile


- Dragoons Master - 27-11-2006

I use a diferent way. I can't remember who did it, but it was someone from here(I think it was Verrigan):
Declare this:
Code:
Public Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
And use it like this(example, npc):
Code:
Call FillMemory(ByVal VarPtr(Npc(Index)), LenB(Npc(Index)), 0)
I think it's much faster...

EDIT: Btw, I'm not using it only for Players, and I don't know why... Tongue


- Spodi - 27-11-2006

Probably is faster, yeah. Thats a good idea, Verrigan. Why don't you just use ZeroMemory then, though? ZeroMemory is faster then FillMemory.


- Matt - 28-11-2006

Spodi, you realize, that was DM that posted that, not Verrigan. Lol.

Please don't edit your post, it will seriously make me look stupid. >.


- William - 28-11-2006

DM said he got it from Verrigan.


- Matt - 28-11-2006

He said he thinks he got it from Ver. And be that so, it was still DM that said it in this topic, not Ver.


- Spodi - 28-11-2006

Quote:but it was someone from here(I think it was Verrigan):

:wink:

That sounds like something Verrigan would say, too. :wink:


- Spodi - 28-11-2006

Your testing environment is pretty unrealistic, though. You're not going to be clearing 50000000 byte UDTs.

Using my speed template:

1 = Zero, 2 = Fill
Quote:%Faster -2.5| -3.7| -3.7| -3.7| -1.2| -2.5| -1.2| -1.2| -1.2| -1.2
Test1 79| 78| 78| 78| 79| 79| 79| 79| 79| 79
Test2 81| 81| 81| 81| 80| 81| 80| 80| 80| 80

1 = Fill, 2 = Zero
Quote:%Faster 1.3| 2.5| 2.5| 2.5| 1.3| 0| 2.5| 1.3| 2.5| 1.3
Test1 80| 81| 81| 81| 80| 80| 81| 80| 81| 80
Test2 79| 79| 79| 79| 79| 80| 79| 79| 79| 79

Specs:
Code:
Public t(0 To 999) As Byte

Code:
ZeroMemory t(0), 1000
Code:
FillMemory t(0), 1000, 0
Code:
Private Const lngIterations As Long = 1000000

The difference is hardly even noteable, though, like you said. Just nit-picking, though. :wink:


- Dragoons Master - 28-11-2006

Well, I'm having a problem with the FillMemory being used w/ the player array. When it clears a fixed-size string is does not set its text to vbnullstring, it changes to a lot of boxes, you know? so I need to use a diferent sub, like this one:
Code:
Sub ClearPlayer(ByVal Index As Long)
    Call FillMemory(ByVal VarPtr(Player(Index)), LenB(Player(Index)), 0)
    Player(Index).Login = vbNullString
End Sub



- Spodi - 28-11-2006

Verrigan Wrote:I wonder why your ZeroMemory() calls are almost always faster, when mine are not.

Maybe it's faster with smaller bits of memory.. or maybe our systems are just completely different.. Do you have an AMD, or Intel processor?

AMD.

I also have a lot more loops going, while yours is just a few loops on a huge chunk of memory. FillMemory probably has a bit more of an overhead, but they both work at nearly the same speed, so since yours only has 50 calls and all the time went to the actual routine, mine has a lot more so the overhead is much more distinguishable.

*shrugs*


- Xlithan - 21-01-2007

Spodi Wrote:How about this:

Code:
Sub ClearPlayer(ByVal Index As Long)
Dim EmptyPlayer As Player 'Not sure if Player is the right variable here - you want the same variable type as used for Player()

Player(Index) = EmptyPlayer
End Sub

Done. :wink:

That's all we need for the entire sub?


- William - 22-01-2007

That should work, just test it.


- Xlithan - 22-01-2007

Can the same method be used for the other Clear subs, like ClearNPC?


- William - 22-01-2007

GameBoy Wrote:Can the same method be used for the other Clear subs, like ClearNPC?
yes it can.


- Xlithan - 22-01-2007

Cool, I can cut down on some lines then Tongue

Also, I get a type mismatch on Player(Index), should it be PlayerRec(Index) or something?


- William - 22-01-2007

Then try the fillmemory instead.


- Spodi - 22-01-2007

I was giving a pseudo-code, not exactly how it should be.

And yes, using ZeroMemory (or FillMemory) would be better and faster.

Code:
ZeroMemory Player(Index), Len(Player(Index))

Something like that.


- grimsk8ter11 - 22-01-2007

If PlayerRec is public them Dim EmptyPlayer as PlayerRec player(index) = emptyplayer would work