Jump to content

Checking if player is within an area


namtatehtmit

Recommended Posts

New member here, mainly c developer, newer to java, looking to make a cow killing script to warm up. I have read the API.

Using state based scripting, my question is how do I get to check if my character is within a define area assuming I have an area defined.

 

psuedo code for attacking:

if( in cow pen area ) {
    if( not in combat ) {
        if( cow is not in combat) {
	    return State.ATTACK;
	}
    }
}
Edited by namtatehtmit
Link to comment
Share on other sites

New member here, mainly c developer, newer to java, looking to make a cow killing script to warm up. I have read the API.

Using state based scripting, my question is how do I get to check if my character is within a define area assuming I have an area defined.

psuedo code for attacking:

if( in cow pen area ) {
    if( not in combat ) {
        if( cow is not in combat) {
	    return State.ATTACK;
	}
    }
}

if (area.contains(myPosition())
Edited by Explv
Link to comment
Share on other sites

if (area.contains(myPosition())

 

That works, but why do I not need to initialize a player object or anything?

 

Like i just need to call the myposition() method and i get my characters position, where does that information come from?

 

Also where do i look in the API to find other methods that pertain to my character?

Edited by namtatehtmit
Link to comment
Share on other sites

That works, but why do I not need to initialize a player object or anything?

 

Like i just need to call the myposition() method and i get my characters position, where does that information come from?

 

Also where do i look in the API to find other methods that pertain to my character?

 

The player object for your own player can be accessed with:

Player player = myPlayer();

The method:

myPosition()

Just does:

myPlayer().getPosition()
Edited by Explv
Link to comment
Share on other sites

That works, but why do I not need to initialize a player object or anything?

 

Like i just need to call the myposition() method and i get my characters position, where does that information come from?

 

Also where do i look in the API to find other methods that pertain to my character?

He's giving you pseudo code, you still need to reference the class that deals with the player object.

Link to comment
Share on other sites

 

I lol'd.

 

Are there benefits doing it your way instead of just doing

if(area.contains(myPlayer()) { 

}

 

tl;dr It's all the same shit.

 

The Area class has several contains methods:

public boolean contains(Entity entity) {
    return contains(entity.getX(), entity.getY()) && plane == entity.getZ();
}
public boolean contains(Position position) {
    return contains(position.getX(), position.getY()) && plane == position.getZ();
}
public boolean contains(int x, int y, int z) {
    return contains(x, y) && plane == z;
}
public boolean contains(int x, int y) {
    return (new java.awt.geom.Area(polygon)).contains((double)x, (double)y);
}

So, when you call:

area.contains(myPlayer())

That will call the contains(Entity entity) method, because the Player is an Entity, which will then call the contains(int x, int y) method with the x coordinate and y coordinate of that entity, and also check if the z vlaues are the same.

 

When you call:

area.contains(myPosition())

That will call the contains(Position position) method, which will then call the contains(int x, int y, int z) method using the x, y and z coordinates of that Position, which then will call the contains(int x, int y) method, and also check the z values are the same.

 

Either way, they both end up calling the same method contains(int x, int y) which checks that the polygon of the Area contains those coordinates, and also check that the z value is the same.

  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...