[Small-Release] Re-written walk method.

02/04/2009 20:06 _Emme_#1
Im pretty sure everyone have seen the walk code in LOTF. Well I rewrote one because I was really really bored, so I decided to do this shit. Its probably the best walk code released so far and yeah, enjoy. Small release as I said, should work in any source with a small adjustment.

Edit : Removed the old one because it wasnt working as I thought ( didnt tested )
Heres one that works almost perfect:


Quote:
public void Walk(byte Dir)
{
sbyte x = 0, y = 0;
if (Dir == 0 || Dir == 1 || Dir == 5 || Dir == 7)
x = 1;
if (Dir >= 1 && Dir <= 3)
x = -1;
if (Dir == 1 || Dir == 6 || Dir == 7)
y = 1;
if (Dir >= 3 && Dir <= 5)
y = -1;
X = (ushort)(X + x); Y = (ushort)(Y + y);
}
02/04/2009 21:25 HellCo#2
yet you flame EVERYONE for using lotf, u idiot.
02/04/2009 21:31 taylor2846#3
um... i have never seen him flame peple for using lotf is seen him flame alot yes but not for using lotf.. or mabe im rong i dont no im just a noob having fun xD
02/04/2009 21:42 InfamousNoone#4
Fyi the *lotf* one will run faster than yours. You also forget that the client does not send a pre-modulated direction, so unless the caller mods it in the argument, you've missed this as well.

Further more you can see several what I'd assume vital things done;
Code:
// notify other people to move? your function fails to do this
World.PlayerMoves(MyChar, Data);

// update the old location, your function fails to do this
MyChar.PrevX = MyChar.LocX;
MyChar.PrevY = MyChar.LocY;

// update the current location, your function *does* do this
MyChar.LocX = (ushort)(MyChar.LocX + AddX);
MyChar.LocY = (ushort)(MyChar.LocY + AddY);
Longer code, does not neccicarly mean slower code.
02/04/2009 21:45 HellCo#5
Quote:
Originally Posted by InfamousNoone View Post
Fyi the *lotf* one will run faster than yours. You also forget that the client does not send a pre-modulated direction, so unless the caller mods it in the argument, you've missed this as well.

Further more you can see several what I'd assume vital things done;
Code:
// notify other people to move? your function fails to do this
World.PlayerMoves(MyChar, Data);

// update the old location, your function fails to do this
MyChar.PrevX = MyChar.LocX;
MyChar.PrevY = MyChar.LocY;

// update the current location, your function *does* do this
MyChar.LocX = (ushort)(MyChar.LocX + AddX);
MyChar.LocY = (ushort)(MyChar.LocY + AddY);
Longer code, does not neccicarly mean slower code.
True dat! Emme just released any random shit to get thanks, go look at he/she posts, EVERY post says press thanks, even though its already in the sig, just stupid ya kno.
02/05/2009 00:24 Korvacs#6
You also have a line of redundant code in your method, if you insist on using yours you should use this:

Code:
public void Walk(byte Dir)
        {
            sbyte x = 0, y = 0;
            if (Dir == 0 || Dir == 1 || Dir == 7) y = 1;
            if (Dir >= 1 && Dir <= 4) x = -1;
            if (Dir >= 3 && Dir <= 5) y = -1;
            X = (ushort)(X + x); Y = (ushort)(Y + y);
         }
I dont see why this was in their at all since its covered by the second if statement......

Code:
            if (Dir >= 1 && Dir <= 3) x = -1;
Edit: Also i just notice you dont ever handle moving in dir 6, so what...are you not allowed to move in that direction on your server?
02/05/2009 00:40 kinshi88#7
Quote:
Originally Posted by Korvacs View Post
Edit: Also i just notice you dont ever handle moving in dir 6, so what...are you not allowed to move in that direction on your server?
Yup, its against the law to move in that direction in Sweden =P
02/05/2009 00:45 zane203#8
Quote:
Originally Posted by HellCo View Post
True dat! Emme just released any random shit to get thanks, go look at he/she posts, EVERY post says press thanks, even though its already in the sig, just stupid ya kno.
=] and what have you coded thats actually up to that level?

honestly i mean, the fact that hybrid helped the code, and even taught emme something is a good thing... the fact that you're being a "hater" and trying to laugh at "knowledge" is the blissful act of ignorance...

=] next time, think b4 you post
-Zane Blalock-
02/05/2009 06:19 _Emme_#9
Quote:
Originally Posted by InfamousNoone View Post
Fyi the *lotf* one will run faster than yours. You also forget that the client does not send a pre-modulated direction, so unless the caller mods it in the argument, you've missed this as well.

Further more you can see several what I'd assume vital things done;
Code:
// notify other people to move? your function fails to do this
World.PlayerMoves(MyChar, Data);

// update the old location, your function fails to do this
MyChar.PrevX = MyChar.LocX;
MyChar.PrevY = MyChar.LocY;

// update the current location, your function *does* do this
MyChar.LocX = (ushort)(MyChar.LocX + AddX);
MyChar.LocY = (ushort)(MyChar.LocY + AddY);
Longer code, does not neccicarly mean slower code.

Yeah, it will move, im calling the method at case 1005, with the players facing as a direction. Oh well I might have missed / done something wrong, but as I said adjust it a little and it should work, and it is better than LOTF one. And no; I do NOT use LOTF, I used LOTF as a reference to show you the difference, to show people LOTF is actually shit coded. Anyone who planning on quitting LOTF, I respect and advice you to Hybrids source.

No, I do not expect thanks for this, as I said its a small release and only to show people.
02/05/2009 06:23 felipeboladao#10
you have codes of Npc says of extremely Basic Source?
02/05/2009 06:38 unknownone#11
The original LOTF one is fine. There's really no need to change it.

There are several reasons it performs better:
Switches can be optimised at compile-time, allowing a direct jump to the relevant case for known values, and a break will exit the selection altogether.
If statements however, all need to be executed each time. On top of that, you have calculations within your selection statmenets (logical OR) which creates additional instructions that must also be executed.

As inf said. Short code doesn't mean fast code.

You're also missing a vital calculation. That being, a mod 8. This is necessary because directions are random powers of 8 added onto the correct value.

And, final complaint: Your code isn't self descriptive. You'd need to comment if for some outsider to understand what it's doing. With previous one, it's obvious.

If you're going to change anything, the only improvement you could make on it would be an enum, which would make it even more readable, descriptive and maintainable

Code:
enum Direction : byte
{
    South, SouthWest, West, NorthWest, North, NorthEast, East, SouthEast,
};

case Direction.South:
    {
        AddY = 1;
        break;
    }
...etc.
02/05/2009 12:14 _Emme_#12
Thank you all for your view of this. Mark, if I would do something like that, I would probably do:
switch (Dir)
{
case Dir.South: y = 1; break;
case Dir.West: x = -1; y = -1; break;
etc..
}

Well just that I like small codes and I understand them more easily, well thanks I'll look into it.

I call the thread closed, no need for more opinions :p
02/05/2009 21:44 InfamousNoone#13
Quote:
Originally Posted by EmmeTheCoder View Post
Yeah, it will move, im calling the method at case 1005, with the players facing as a direction. Oh well I might have missed / done something wrong, but as I said adjust it a little and it should work, and it is better than LOTF one. no it's not, C# is *smart* enough to convert a case statement to an if-else statement believes the if-else will run faster than if it was ran in a case environment if it And no; I do NOT use LOTF, I used LOTF as a reference to show you the difference, to show people LOTF is actually shit coded. Anyone who planning on quitting LOTF, I respect and advice you to Hybrids source.

No, I do not expect thanks for this, as I said its a small release and only to show people.
that is if your compiling with "optimize my code" which everyone obviously needs to have on, lol.