Author Topic: [PI][TUT]Clipped player following  (Read 950 times)

Offline ~Dylan~

  • Steel Member
  • *
  • Posts: 37
  • Rep 0
    • View Profile
[PI][TUT]Clipped player following
« on: March 27, 2011, 01:51:19 pm »
**CLIENT SIDED**

In Client.java add this method

Code: [Select]
public int followPlayer = 0;

    public int followNPC = 0;

    public int followDistance = 1;

Now under "private void resetLogout() {" add
 
Code: [Select]
followPlayer = 0;

        followNPC = 0;

        followDistance = 1;


Now add this void in Client.java

Code: [Select]
public int canWalkDelay = 0;
     public int getDis(int coordX1, int coordY1, int coordX2, int coordY2)
    {
        int deltaX = coordX2 - coordX1;
        int deltaY = coordY2 - coordY1;
        return ((int)Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
    }
public int random(int range)
    {
        return (int)(Math.random() * range);
    }
   public boolean withinDistance(int x1, int y1, int x2, int y2, int dis)
    {
        for (int i = 0; i <= dis; i++)
        {
try{
            if ((x1 + i) == x2 && ((y1 + i) == y2 || (y1 - i) == y2 || y1 == y2))
                return true;
            else
            if ((x1 - i) == x2 && ((x1 + i) == y2 || (y1 - i) == y2 || y1 == y2))
                return true;
            else
            if (x1 == x2 && ((x1 + i) == y2 || (y1 - i) == y2 || y1 == y2))
                return true;
} catch(Exception ex){
System.out.println("Exception in following, method : WithingDistance");
}
        }
        return false;
    }

Add this under "private void mainGameProcessor() below if(!loggedIn) return;"
Code: [Select]
try{
canWalkDelay--;
        if (followNPC > 0)
        {
            NPC n = npcArray[followNPC];
            if (n != null)
            {
                if (!withinDistance(myPlayer.smallX[0], myPlayer.smallY[0], n.smallX[0], n.smallY[0], followDistance))
                {
                    doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], 1, 0, n.smallY[0], myPlayer.smallX[0], false, n.smallX[0]);
                }
            }
        }
        else if (followPlayer > 0 && canWalkDelay <= 0)
        {
            Player p = playerArray[followPlayer];
            if (p != null)
            {
                int dis = getDis(myPlayer.smallX[0], myPlayer.smallY[0], p.smallX[0], p.smallY[0]);
                if (dis > followDistance)
                {
                    doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], followDistance, 0, p.smallY[0], myPlayer.smallX[0], false, p.smallX[0]);
                    canWalkDelay = 30;
                }
                else if (dis == 0)
                {
                    int rnd = random(4);
                    if (rnd == 0)
                    {
                        doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], 1, 0, p.smallY[0], myPlayer.smallX[0], false, p.smallX[0] - 2);
                    }
                    else if (rnd == 1)
                    {
                        doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], 1, 0, p.smallY[0], myPlayer.smallX[0], false, p.smallX[0] + 2);
                    }
                    else if (rnd == 2)
                    {
                        doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], 1, 0, p.smallY[0] - 2, myPlayer.smallX[0], false, p.smallX[0]);
                    }
                    else if (rnd == 3)
                    {
                        doWalkTo(2, 0, 1, 0, myPlayer.smallY[0], 1, 0, p.smallY[0] + 2, myPlayer.smallX[0], false, p.smallX[0]);
                    }
                    canWalkDelay = 60;
                }
            }
        }
} catch(Exception ex){
System.out.println("Exception in following, method : in process.)");
}

Now replace "case 174:" with this
Code: [Select]
case 174:

/*Empty Following Packet*/

                followPlayer = 0;

                followNPC = 0;

                int l11z = inStream.readUnsignedWord();

                int iq = inStream.readUnsignedByte();

                followDistance = inStream.readUnsignedWord();

                if (iq == 0)

                {

                    followNPC = l11z;

                }

                else if (iq == 1)

                {

                    followPlayer = l11z;

                }

                pktType = -1;

                return true;

**SERVER SIDED**

In Client.java add this
Code: [Select]
public void applyFollowing()
{
if (follow2 > 0)
{
//Client p = Server.playerHandler.client[followId];
Client p = (Client) Server.playerHandler.players[follow2];     
if (p != null)
{
if (isDead)
{
follow(0, 3, 1);
return;
}
if (!goodDistance(p.absX, p.absY, absX, absY, 25))
{
follow(0, 3, 1);
return;
}
}
else if (p == null)
{
follow(0, 3, 1);
}
}
else if (follow2 > 0)
{
//Server.npcHandler.npcs.NPC npc = Server.npcHandler.npcs[followId2];
if (Server.npcHandler.npcs[followId2] != null)
{
if (Server.npcHandler.npcs[followId2].isDead)
{
follow(0, 3, 1);
return;
}
if (!goodDistance(Server.npcHandler.npcs[followId2].absX, Server.npcHandler.npcs[followId2].absY, absX, absY, 25))
{
follow(0, 3, 1);
return;
}
}
else if (Server.npcHandler.npcs[followId2] == null)
{
follow(0, 3, 1);
}
}
}

public int followDistance = 0;

public void follow(int slot, int type, int distance)
{
if (slot > 0 && slot == follow2 && type == 1 && follow2 > 0 && followDistance != distance && (/*usingOtherRangeWeapons || */usingBow || usingMagic))
                    return;
else if (slot > 0 && slot == followId2 && type == 0 && followId2 > 0 && followDistance >= distance && distance != 1)
                    return;
//else if (type == 3 && followId2 == 0 && follow2 == 0)
                    //return;
outStream.createFrame(174);
if (freezeTimer > 0) {
outStream.writeWord(0);
} else {
outStream.writeWord(slot);
if (type == 0) {
follow2 = 0;
followId2 = slot;
faceUpdate(followId2);
} else if (type == 1) {
followId2 = 0;
follow2 = slot;
faceUpdate(32768 + follow2);
} else if (type == 3) {
followId2 = 0;
follow2 = 0;
followDistance = 0;
faceUpdate(65535);
}
followDistance = distance;
}
outStream.writeByte(type);
outStream.writeWord(distance);
}

Now search for
Code: [Select]
getPA().resetFollow();and delete it!

Now search for "process()" and add these two methods underneath it
Code: [Select]
applyFollowing();

if(followId > 0) {

getPA().followPlayer();

} else if (followId2 > 0) {

getPA().followNpc();

}
Now open up Player.java and add this
Code: [Select]
public int follow2 = 0;

followId,

followDistance,

followId2,

Now replace your Followplayer.java with this
Code: [Select]
package server.model.players.packets;

import server.Server;
import server.model.players.Client;
import server.model.players.PacketType;

/**
 * Follow Player by Tyluur.
 **/
public class FollowPlayer implements PacketType {

@Override
public void processPacket(Client c, int packetType, int packetSize) {
/*int followPlayer = c.getInStream().readUnsignedWordBigEndian();
if(Server.playerHandler.players[followPlayer] == null) {
return;
}
c.playerIndex = 0;
c.npcIndex = 0;
c.mageFollow = false;
c.usingBow = false;
c.usingRangeWeapon = false;
c.followDistance = 1;
c.followId = followPlayer;*/
int toFollow = c.getInStream().readUnsignedWordBigEndian();
c.follow2 = toFollow;
if (Server.playerHandler.players[toFollow] == null)
return;
c.faceUpdate(32768+toFollow);
c.follow(toFollow, 1, 1);
}}
and your Walking.java with this!
Code: [Select]
package server.model.players.packets;

import server.Server;
import server.model.players.Client;
import server.model.players.PacketType;

/**
 * Walking packet
 **/
public class Walking implements PacketType {

@Override
public void processPacket(Client c, int packetType, int packetSize) {
if (c.isBanking)
c.isBanking = false;

if (packetType == 248 || packetType == 164) {
c.faceUpdate(0);
c.npcIndex = 0;
c.playerIndex = 0;
if (c.followId > 0 || c.followId2 > 0 || c.follow2 > 0)
c.getPA().resetFollow();
}
c.getPA().removeAllWindows();
if(c.duelRule[1] && c.duelStatus == 5) {
if(Server.playerHandler.players[c.duelingWith] != null) {
if(!c.goodDistance(c.getX(), c.getY(), Server.playerHandler.players[c.duelingWith].getX(), Server.playerHandler.players[c.duelingWith].getY(), 1) || c.attackTimer == 0) {
c.sendMessage("Walking has been disabled in this duel!");
}
}
c.playerIndex = 0;
return;
}

if(c.freezeTimer > 0) {
if(Server.playerHandler.players[c.playerIndex] != null) {
if(c.goodDistance(c.getX(), c.getY(), Server.playerHandler.players[c.playerIndex].getX(), Server.playerHandler.players[c.playerIndex].getY(), 1) && packetType != 98) {
c.playerIndex = 0;
return;
}
}
if (packetType != 98) {
c.sendMessage("A magical force stops you from moving.");
c.playerIndex = 0;
}
return;
}

if (System.currentTimeMillis() - c.lastSpear < 4000) {
c.sendMessage("You have been stunned.");
c.playerIndex = 0;
return;
}

if (packetType == 98) {
c.mageAllowed = true;
}

if((c.duelStatus >= 1 && c.duelStatus <= 4) || c.duelStatus == 6) {
if(c.duelStatus == 6) {
c.getTradeAndDuel().claimStakedItems();
}
return;
}


if(c.respawnTimer > 3) {
return;
}
if(c.inTrade) {
return;
}
if(packetType == 248) {
packetSize -= 14;
}
c.newWalkCmdSteps = (packetSize - 5)/2;
if(++c.newWalkCmdSteps > c.walkingQueueSize) {
c.newWalkCmdSteps = 0;
return;
}

c.getNewWalkCmdX()[0] = c.getNewWalkCmdY()[0] = 0;

int firstStepX = c.getInStream().readSignedWordBigEndianA()-c.getMapRegionX()*8;
for(int i = 1; i < c.newWalkCmdSteps; i++) {
c.getNewWalkCmdX()[i] = c.getInStream().readSignedByte();
c.getNewWalkCmdY()[i] = c.getInStream().readSignedByte();
}

int firstStepY = c.getInStream().readSignedWordBigEndian()-c.getMapRegionY()*8;
c.setNewWalkCmdIsRunning(c.getInStream().readSignedByteC() == 1);
for(int i1 = 0; i1 < c.newWalkCmdSteps; i1++) {
c.getNewWalkCmdX()[i1] += firstStepX;
c.getNewWalkCmdY()[i1] += firstStepY;
}
}

}

Share on Bluesky Share on Facebook


Offline Sajon

  • Rune Member
  • *
  • Posts: 166
  • Rep -1
  • Never Back down
  • Location: United Kingdom
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #1 on: March 28, 2011, 05:25:07 pm »
i seen this problem in alot of server

Offline Holy

  • Adamant Member
  • *
  • Posts: 98
  • Rep 0
  • The Next Lionel Messi.
  • Location: Written in the stars,Million miles away...
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #2 on: March 28, 2011, 06:09:11 pm »
This isn't problem,more like solution mate :)
Anyways GJ Dylan  ;;)
-Holy

Offline Amoridan

  • Mithril Member
  • *
  • Posts: 66
  • Rep 0
  • <<>><<>> I see you
  • Location: Seattle, WA // Tokyo, Japan
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #3 on: March 28, 2011, 11:38:19 pm »
dont do so many } it just wastes memory and storage just do one per each saves a lot of time coding i would know.
Owner and Coder of Project Life,
Amoridan

To apply for Project Life,
http://www.projectlifersps.smfnew.com/
we are a team who release clients and servers to those people who dont know how to do it themselves. And we are non prophit team of programmers, GFX designers, and 3D artists.
To check out are 562 project which we be putting online soon and also we will be releasing after we feel that we dont want it anymore is at: http://www.projectlife562.smfnew.com/

Offline ~Dylan~

  • Steel Member
  • *
  • Posts: 37
  • Rep 0
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #4 on: March 28, 2011, 11:45:51 pm »
There is reasons I do { and }
It is simply because I use eclipse to check all my voids and methods to see if they are correct and will give me any errors and by using the "{" and "}" symbols I can make my tutorials quicker :)

Offline Povskill

  • Iron Member
  • *
  • Posts: 15
  • Rep 0
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #5 on: April 04, 2011, 05:21:40 pm »
Thanx for that

Offline RawK

  • Bronze Member
  • *
  • Posts: 14
  • Rep 0
    • View Profile
Re: [PI][TUT]Clipped player following
« Reply #6 on: April 09, 2011, 01:38:48 am »
Nice Tut, is this so people dont walk through walls etc.?


Unless i say it is my work it isnt <3