• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Struggling with maths...

Level 14
Joined
Oct 16, 2010
Messages
749
Hi,

I'm adding some "AI" autocasting to my map, thought it was time to improve it from "IF THEY ATTACK SOMETHING THEN CAST THE ABILITY!!"

I've got 2 different units with Frost Nova and Shadow Strike each, I thought it would make some sense to make them cast these as somewhat "execution" abilities.

I completely understand how to pick the enemy with the highest health, lowest health, group of enemies (within reason), etc. But to stop the AI wasting the spell on something with VERY low health I was thinking I would make them use it enemies closest to 25% health for example, that way they will still use it on a healthy unit and also use the spell to finish an enemy off.

However I'm struggling to work out what maths I need to use to work out these differences? My maths example below -

Enemy with 10% health is 15% from 25%, an enemy with 80% health is 55% from 25%, however to get to these two answers you needed to do different sums...

How do I do this maths to detect what number is closer to the 25%?


NOTE - I don't want to have them "save" the spell for a better target, once they have full mana (this matches the cast cost) I want them to use the spell on the best thing that is nearby to them at the time




P.S. I would also like to do something with autocasting "line" based damage like shockwave and carrion wave, if anyone knows how to check if enemies are in a line that would be great too!
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,873
Pseudo-code from your pal ChatGPT after feeding it the middle chunk of your post:
Code:
targetHealth = 25.00
closestEnemy = null
smallestDistance = infinity

for each enemy in enemies:
    currentHealth = enemy.healthPercentage
    distance = abs(currentHealth - targetHealth)
 
    if distance < smallestDistance:
        smallestDistance = distance
        closestEnemy = enemy

return closestEnemy
In Warcraft 3 GUI terms:

The For Each Loop becomes a Pick Every Unit action, enemies is a Unit Group variable setup beforehand with your nearby targets, smallestDistance is a Real equal to 99999.00, closestEnemy is set to No unit by default, and you wouldn't "return" anything since it's not a function call. Whatever closestEnemy is Set to after the Loop finishes is your best target. Abs is a math function that converts a potentially negative number to a positive - this exists already in the Math functions list.

Regarding the line stuff, this works great:
 
Last edited:
Top