Thread: [PI]Object distance check

Results 1 to 7 of 7
  1. #1 [PI]Object distance check 
    Registered Member
    Join Date
    Jan 2010
    Posts
    65
    Thanks given
    6
    Thanks received
    1
    Rep Power
    1
    Hi guys!

    I've looked on google but couldnt find the right answer.
    Basicly what i have tried to do for the last hour is to add a distance check to my cooking range (id 3039)
    What have i actually tried?

    I tried this but couldnt figure it out:
    [Only registered and activated users can see links. ]

    I also tried this but it didnt work:
    [Only registered and activated users can see links. ]

    What do i want? I want it so the players walks up to the cooking range first and then starts cooking, now he starts cooking no matter what..

    Can someone help me with this? or aleast get me started..

    Thanks in advance!
    Luukwa
    Reply With Quote  
     

  2. #2  
    Donator
    BongoProd's Avatar
    Join Date
    Oct 2012
    Posts
    812
    Thanks given
    1
    Thanks received
    68
    Rep Power
    0
    Bump
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Jan 2010
    Posts
    65
    Thanks given
    6
    Thanks received
    1
    Rep Power
    1
    Bump
    Reply With Quote  
     

  4. #4  
    Registered Member BryceTheCoder's Avatar
    Join Date
    Aug 2008
    Posts
    740
    Thanks given
    21
    Thanks received
    24
    Rep Power
    27
    Here is a function I like called goodDistance(int, int, int, int, int);
    However, if you have a PI base you probably have it already inside your Player class.

    Here is the function:
    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX,
    			int playerY, int distance) {
    		for (int i = 0; i <= distance; i++) {
    			for (int j = 0; j <= distance; j++) {
    				if ((objectX + i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if ((objectX - i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if (objectX == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    So, now you can imagine how you use it.
    For example, you want the players to be close to the range before executing "Cooking".
    On the packet of where the player uses the item on the range, do a simple if statement like:
    Code:
    if (goodDistance(objectX, objectY, c.absX, c.absY, DistanceYouDesire)) {
    //DoCooking();
    }
    This will do a simple if statement check to see if the player is within "DistanceYouDesire" tiles with the object.

    Hope I helped

    Advertise your RSPS cheap on my website: [Only registered and activated users can see links. ]

    ^ PM me and I will give you a few dollars for FREE on my website for advertisements ^
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Jan 2010
    Posts
    65
    Thanks given
    6
    Thanks received
    1
    Rep Power
    1
    Quote Originally Posted by BryceTheCoder View Post
    Here is a function I like called goodDistance(int, int, int, int, int);
    However, if you have a PI base you probably have it already inside your Player class.

    Here is the function:
    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX,
    			int playerY, int distance) {
    		for (int i = 0; i <= distance; i++) {
    			for (int j = 0; j <= distance; j++) {
    				if ((objectX + i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if ((objectX - i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if (objectX == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    So, now you can imagine how you use it.
    For example, you want the players to be close to the range before executing "Cooking".
    On the packet of where the player uses the item on the range, do a simple if statement like:
    Code:
    if (goodDistance(objectX, objectY, c.absX, c.absY, DistanceYouDesire)) {
    //DoCooking();
    }
    This will do a simple if statement check to see if the player is within "DistanceYouDesire" tiles with the object.

    Hope I helped
    And distance i desire is just the amount of tiles away?
    Reply With Quote  
     

  6. #6  
    Android/Java/Meteor/Node
    Orbyt's Avatar
    Join Date
    Oct 2013
    Posts
    335
    Thanks given
    25
    Thanks received
    21
    Rep Power
    11
    Quote Originally Posted by BryceTheCoder View Post
    Here is a function I like called goodDistance(int, int, int, int, int);
    However, if you have a PI base you probably have it already inside your Player class.

    Here is the function:
    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX,
    			int playerY, int distance) {
    		for (int i = 0; i <= distance; i++) {
    			for (int j = 0; j <= distance; j++) {
    				if ((objectX + i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if ((objectX - i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if (objectX == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    This will do a simple if statement check to see if the player is within "DistanceYouDesire" tiles with the object.

    Hope I helped
    Couldn't you just replace that whole method with 1 if statement?

    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
       if (((playerX + playerY) - (objectX + objectY)) <= distance && ((objectX + objectY) - (playerX + playerY) <= distance)) {
          return true;
       } else {
             return false;
          }
    }
    Of course this might be a bit cleaner:
    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
       
       int objSum = objectX + objectY;
       int playerSum = playerX + playerY;
       if (playerSum - objSum <= distance && objSum - playerSum <= distance) {
          return true;
       } else {
             return false;
          }   
    }
    maybe ternary?

    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
       int firstDif = (playerX + playerY) - (objectX + objectY);
       int secDif = (objectX + objectY) - (playerX + playerY);
          boolean result = (firstDif <= distance && secDif <= distance) ? true : false;
          return result;
    }
    These are untested though, may not work lol.
    Reply With Quote  
     

  7. #7  
    Registered Member
    Join Date
    Jan 2010
    Posts
    65
    Thanks given
    6
    Thanks received
    1
    Rep Power
    1
    Quote Originally Posted by BryceTheCoder View Post
    Here is a function I like called goodDistance(int, int, int, int, int);
    However, if you have a PI base you probably have it already inside your Player class.

    Here is the function:
    Code:
    public boolean goodDistance(int objectX, int objectY, int playerX,
    			int playerY, int distance) {
    		for (int i = 0; i <= distance; i++) {
    			for (int j = 0; j <= distance; j++) {
    				if ((objectX + i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if ((objectX - i) == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				} else if (objectX == playerX
    						&& ((objectY + j) == playerY
    						|| (objectY - j) == playerY || objectY == playerY)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    So, now you can imagine how you use it.
    For example, you want the players to be close to the range before executing "Cooking".
    On the packet of where the player uses the item on the range, do a simple if statement like:
    Code:
    if (goodDistance(objectX, objectY, c.absX, c.absY, DistanceYouDesire)) {
    //DoCooking();
    }
    This will do a simple if statement check to see if the player is within "DistanceYouDesire" tiles with the object.

    Hope I helped
    Getting a error when compiling..

    src\server\model\players\packets\ItemOnObject.java :55: error: cannot find symbol

    if (goodDistance(objectX, objectY, c.absX, c.absY, 2)) {

    ^
    symbol: method goodDistance(int,int,int,int,int)
    location: class ItemOnObject
    1 error
    Press any key to continue . . .
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI]Stop useless object distance check
    By Aleksandr in forum Snippets
    Replies: 21
    Last Post: 04-11-2013, 12:41 AM
  2. Object Distance checks
    By Nando in forum Tutorials
    Replies: 34
    Last Post: 03-21-2013, 10:27 AM
  3. [PI] Long-distance Object Click [HELP]
    By Compaq in forum Help
    Replies: 5
    Last Post: 03-20-2013, 06:21 PM
  4. [PI] Basic Use Item on Object Distance Check
    By Aleksandr in forum Snippets
    Replies: 1
    Last Post: 09-18-2011, 12:56 AM
  5. PI Object Distance
    By Jason in forum Help
    Replies: 3
    Last Post: 05-22-2011, 05:18 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •