Root, anyone?

Discussion in 'General Modification' started by Shiningted, Aug 13, 2013.

Remove all ads!
  1. Shiningted

    Shiningted I changed this damn title, finally! Administrator

    Joined:
    Oct 23, 2004
    Messages:
    12,743
    Likes Received:
    374
    Apparently, obj.distance_to(obj) only measures the distances between, errr, objects.

    So I am trying to find the distance between locations. And barring anyone pointing out to me a simple or obvious way (please do) I have been using the standard geometric stuff - triangulism, imaginary numbers (eleventeen and such), 3rd derivative calculus etc.

    Which brings me to my question - how the heck do you get a square root in Python? The root symbol √ breaks Notepad :( Any ideas?
     
  2. Sitra Achara

    Sitra Achara Senior Member

    Joined:
    Sep 1, 2003
    Messages:
    3,622
    Likes Received:
    538
    If you're just comparing to another distance, you can just compare the square of distances :)

    But if you must, you can use the ** operator

    e.g.

    2**0.5

    Will yield 1.414...


    As for the former question - I gather you're calculating sqrt(DX^2 + DY^2)? Should be ok, but perhaps edge cases could cause headaches. Can you give some context?
     
  3. Shiningted

    Shiningted I changed this damn title, finally! Administrator

    Joined:
    Oct 23, 2004
    Messages:
    12,743
    Likes Received:
    374
    Yeah, exactly right: triangulism like I said.

    I am trying to cast a spell on an NPC but they should not go into combat because of it. I was having no luck though, even if I set up the spell identical to, say, CLW (which won't cause an NPC to attack) they, well, attack. In fact, I found out last night they attack without even compiling the .py file: they go straight off the rules/spells/.txt file, even if, as said, its set as a defensive or healing spell (so the script of the spell is irrelevant).

    So instead I am doing it as an area spell (with a tiny AoE, you just cast t the target's feet). Then I need to identify who is closest to the target spot, so I did a list and sort-by-distance (manually when I couldn't get the script to do othrwise, as is often the case in ToEE).

    The good news? It does work that way: you can get a handle on the NPC in question and do whatever you like to them by script without them attacking you. With your square root suggestion I should be able to do a proper sort. (And if someone casts it at the midpoint of a bunch of NPCs and it doesn't target the one they want, well, tough luck: target their feet and you'll be right).

    Now... since Python doesn't need you to define variable types (int / float etc), how do I make sure it allows a float? My previous efforts seem to default as an int, and return numbers like '1' and '7' rather than the aforementioned 1.414 etc.
     
  4. Sitra Achara

    Sitra Achara Senior Member

    Joined:
    Sep 1, 2003
    Messages:
    3,622
    Likes Received:
    538
    Well, again, if you want to avoid complications, just calculate the square of distance (i.e. without the sqrt at all), and sort by that value - it's equivalent.

    As for the floatgescheft - what do you mean by allow? And what previous efforts? (I hope you weren't using ^ :) )
    Anyway, I know that I tested it in the console and it definitely returned a floating point number. (1.414...)
    If you want to be bulletproof things, you can cast other results to floating point by using float(arg).

    Also, if you're using an AoE targetting, doesn't it return a target list that, given the small radius, necessarily contains only 1 target at most?
     
  5. Shiningted

    Shiningted I changed this damn title, finally! Administrator

    Joined:
    Oct 23, 2004
    Messages:
    12,743
    Likes Received:
    374
    Ahhh, but its the act of 'targeting' the NPC that is causing him to go hostile: so I am axcluding NPCs as targets. Hence fiddly workarounds.
    Fair point, but I also want to make sure its within a certain distance, so people have to actually make an effort to put it on the NPC they want, not just 'nearby and closest'. Because what looks closest on screen and what the engine thinks is closest are not always the same thing, I can tell you from experience ;)

    Otherwise I solved the float thing (I was checking via a global_vars - I checked via .txt output and got a nice long floating point :) )

    Anyways, think I've got it now, thanks Sitra :wave:
     
  6. XVicious

    XVicious Established Member

    Joined:
    Jun 21, 2013
    Messages:
    427
    Likes Received:
    8

    have you tried using other trig funtions instead of root of to find distance?
    (PATHAGRIAM THEORAM)


    say

    tan, cos, atan, acos

    get angle
    point A / point B = tan(angle A)

    then,

    get hypotnuse
    hypotnuse = adjacent / (cos * (angle A) )

    the function might use RAD
    so you need to convert them to DEG if you get the wrongnumbers
    like
    deg = angle * PI / 180

    (some functions only accept RAD parameters)



    or try finding functions like
    hypot ( x, z ) format

    in c/C++ root is define like
    sqrt(x)

    some script languages use simular function proto's
    to standard c/c++ formats

    or you have to create a sqrt function!!
    here is a guide
    http://www.homeschoolmath.net/teaching/square-root-algorithm.php
    logical
    http://www.homeschoolmath.net/teaching/sqr-algorithm-why-works.php
    lol

    using case less then greater
    incremental
    (be a long funtion to create exact replica)
    i.e sqrt of 27
    2*2 =4
    4*4 = 16
    5*5 = 25
    6*6 = 36
    27> 25 yes
    27> 36 no
    ------------
    27-25 = 2
    then 1*1 closest sqrt of 2
    2-1 =1

    approximated answer:
    ~5.1
     
    Last edited: Aug 31, 2013
Our Host!