Title explains all.
If not, is it possilb eot enable distanceTo on items in level editor
It is possible, i believe distanceTo an item was introduced in Forest
@Lydia_Song
Does it work on control points?
Cuz i used this code in Ace of coders
def commandScout():
targets = []
captures = []
friends = hero.findFriends()
points = hero.getControlPoints()
for point in points:
if point.team <> hero.team:
targets.append(point)
captures = []
for point in points:
if point.team == hero.team:
captures.append(point)
friends = hero.findFriends()
for friend in friends:
if friend.type == 'soldier':
target = friend.findNearest(targets)
if target and friend.distanceTo(target) < 11:
capture = friend.findNearest(captures)
if capture:
hero.command(friend, "defend", capture.pos)
elif target:
hero.command(friend, "defend", target.pos)
wdym control points
20
In Ace of Coders.
i dont play ace of coders so idk
You can just use the distance formula to find the distance of two points.
How do I do that?
Also can you please help me with control points?
Still not working.
@Chaboi_3000
It’s the distance formula used in math. (You should’ve learned it in 5th grade)
More detailed explanation:
The distance formula is the following: √((x2-x1)^2+(y2-y1)^2)
Why? It’s because it works using the pythagorean theorem. (a^2+b^2=c^2
)
Visual:
Since C is going to be the distance(Explained later), we want to find what C is in terms of A and B.
That is simply: c=√(a^2+b^2)
Now we know the formula, so the next step would be applying it into the code. Here’s what your situation looks like:
You can now see that c
, the hypotenuse of the triangle, is the distance between the Unit and the Control Point.
To start off, begin by creating an artificial scenario. For the coordinate of “Unit”, let’s say it’s (7,9), and for the coordinate of the control point, let’s say it’s (-1,15).
We already know the distance formula is √((x2-x1)^2+(y2-y1)^2)
, so we can begin to plug in the values to attain the answer.
How to get distance formula
The distance formula is literally the formula to get the hypotenuse of a right-triangle(Given a and b), but with coordinates. (c=√(a^2+b^2)
)
So find what the legs of the triangle are, we need to find the distance between two points on a graph. Imagine a number line with values -5 and 2. You can immediately recognize that the distance between those two points is 7. But how do you get it? You should figure out that the answer is 2 - (-5)
, or 7. So the 2nd value subtracted by the 1st. If you apply this to the X and Y coordinates, we can substitute leg A as (y2-y1), and leg B as (x2-x1).
If you plug in the values, you should get something like: √(((-1)-7)^2+(15-9)^2)
That can be simplified to √((-8)^2+(6)^2)
-8^2 is 64, and 6^2 is 36. So the answer would be √(64+36)
, or √(100)
, or 10.
Now to the code part. How do we get it into Python code? First, it’s important to define a new function. Name it findDistance()
with the arguments, x1, y1, x2, y2. (x1,y1) is the coordinate of the unit, and (x2, y2) is the coordinate of the control point.
Next, you must understand CodeCombat has a built-in math library, with can be used using Math
. (In normal python, you’d use import math
with lowercase math
)
The distance formula involves two math operators: powers and square roots.
Power of a number: Math.pow(num, power)
Square root of a number: Math.sqrt(num)
First, you should get the 2nd power of difference between x2 and x1, which can be done by Math.pow(x2-x1,2)
. Next, you should get the 2nd power of the difference between y2 and y1, which can be done similarly using Math.pow(y2-y1,2)
. Then, add those two together, and get the square root of the sum of the two values using Math.sqrt()
. Since you want to return the value, you’d use return
and return the result.
Function Code
def findDistance(x1,y1,x2,y2):
return Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2))
THANK YOU SO MUCH!!!
I THINK I KNOW HOW T GET IT!
IF THE DISTANCE TO THE NEAREST POINT THAT ISN’T MY TEAM IS MORE THEN 11, CAPTURE IT.
ELSE, LOOK FOR THE NEAREST POINT AND DEFEND IT.
UR THE BEST!!!
complete senetence…