[Math] I need a math formula for my trigger

Level 7
Joined
Oct 16, 2018
Messages
87
salutations.
let me explain what kind of formula i need.

i want to say that when distance between two units is high, then variableX becomes lower (not under 0).
and when is low, then variableX becomes higher.
i dont want values lower than 0.
and note that there is only "- + x /"

im waiting for your assistance :ugly:
 
Last edited:
Level 7
Joined
Oct 16, 2018
Messages
87
Simplest one to fulfil your requirements would be something like C / distance where C is some constant and distance is greater than 1 or equal to 1.

What is this for?
hey Doctor, thanks for your answer.

i made a trigger that makes Accuracy for my Archer unit, so when Archer attacks, his arrow will shoots somewhere near the target.


but there is two integers that i want them to involve in my trigger, cur_Accuracy and upg_Accuracy.
so theres a normal stat like unit damage and hp which is called "cur_Accuracy" (this is a percentage between 1% and 100%)
and a skill or tech (something upgradable) which is called "upg_Accuracy" (this is 0 by default until the player upgrades it)


so i want them both to depend on where the arrow will falls.
and btw i want the Distance to involve, so When the distance is higher, arrow will falls more away from the target position and when is lower, the arrow will falls closer.

here is my current formula which is so simple but IS MUCH FAR than what i really want :ugly:
take a look:

  • Set VariableSet Formula = ((Distance between (Position of (Attacking unit)) and (Position of (Attacked unit))) / (((Real((cur_Accuracy for (Attacking unit)))) x 0.20) + (((Real((Level of upg_Accuracy for (Attacking unit)))) x 1.00) + 1.00)))
  • Unit - Order (Attacking unit) to Attack Ground (Random point in (Region centered at (Position of (Attacked unit)) with size (Formula, Formula)))
 
Last edited:

Uncle

Warcraft Moderator
Level 42
Joined
Aug 10, 2018
Messages
4,189
I don't think you want to create a Region:
  • Unit - Order (Attacking unit) to Attack Ground (Random point in (Region centered at (Position of (Attacked unit)) with size (Formula, Formula)))
Point with Polar Offset will get the job done. Something like this:
  • Actions
    • -------- Get the Accuracy: --------
    • Set VariableSet AttackPoint = (Position of (Attacking unit))
    • Set VariableSet TargetPoint = (Position of (Attacked unit))
    • Set VariableSet AccuracyUpgrade = (Real((Current research level of Accuracy Upgrade for (Owner of (Attacking unit)))))
    • Set VariableSet AccuracyDistance = (Distance between AttackPoint and TargetPoint)
    • Set VariableSet AccuracyRoll = (AccuracyDistance / (4.00 + AccuracyUpgrade))
    • Custom script: call RemoveLocation(udg_AttackPoint)
    • -------- --------
    • -------- Use the Accuracy to determine where the attack projectile will hit: --------
    • Set VariableSet AttackPoint = (TargetPoint offset by (Random real number between 0.00 and AccuracyRoll) towards (Random angle) degrees.)
    • Unit - Order (Attacking unit) to Attack Ground AttackPoint
    • -------- --------
    • Custom script: call RemoveLocation(udg_AttackPoint)
    • Custom script: call RemoveLocation(udg_TargetPoint)
It's not exactly what you wanted but it creates a similar effect. The further away the two units are the greater the AccuracyRoll will be which is used to calculate the offset point where the projectile will hit.

So for example if you're 600 distance away from the target then the AccuracyRoll will be 150 (600 / 4), which means that AttackPoint will be set to some random point between 0 and 150 distance away from the target. If you increase the distance then AccuracyRoll will increase as well, creating a larger range for the offset.

The Accuracy Upgrade tech will increase the divisor during the calculation for AccuracyRoll which will decrease the random offset range.
600 / 5 = 0 to 120 offset range
600 / 6 = 0 to 100 offset range
etc...
 
Last edited:
Level 7
Joined
Oct 16, 2018
Messages
87
I don't think you want to create a Region:
  • Unit - Order (Attacking unit) to Attack Ground (Random point in (Region centered at (Position of (Attacked unit)) with size (Formula, Formula)))
Point with Polar Offset will get the job done. Something like this:
  • Actions
    • -------- Get the Accuracy: --------
    • Set VariableSet AttackPoint = (Position of (Attacking unit))
    • Set VariableSet TargetPoint = (Position of (Attacked unit))
    • Set VariableSet AccuracyUpgrade = (Real((Current research level of Accuracy Upgrade for (Owner of (Attacking unit)))))
    • Set VariableSet AccuracyDistance = (Distance between AttackPoint and TargetPoint)
    • Set VariableSet AccuracyRoll = (AccuracyDistance / (4.00 + AccuracyUpgrade))
    • Custom script: call RemoveLocation(udg_AttackPoint)
    • -------- --------
    • -------- Use the Accuracy to determine where the attack projectile will hit: --------
    • Set VariableSet AttackPoint = (TargetPoint offset by (Random real number between 0.00 and AccuracyRoll) towards (Random angle) degrees.)
    • Unit - Order (Attacking unit) to Attack Ground AttackPoint
    • -------- --------
    • Custom script: call RemoveLocation(udg_AttackPoint)
    • Custom script: call RemoveLocation(udg_TargetPoint)
It's not exactly what you wanted but it creates a similar effect. The further away the two units are the greater the AccuracyRoll will be which is used to calculate the offset point where the projectile will hit.

So for example if you're 600 distance away from the target then the AccuracyRoll will be 150 (600 / 4), which means that AttackPoint will be set to some random point between 0 and 150 distance away from the target. If you increase the distance then AccuracyRoll will increase as well, creating a larger range for the offset.

The Accuracy Upgrade tech will increase the divisor during the calculation for AccuracyRoll which will decrease the random offset range.
600 / 5 = 0 to 120 offset range
600 / 6 = 0 to 100 offset range
etc...

thanks for the formula:grin:
ill test it tomrow.


but there is a problem.
if the player accuracy tech level is 0 (not researched/upgraded) then 600 / 0 = 0 to unlimit offset range? :huh:
 

Uncle

Warcraft Moderator
Level 42
Joined
Aug 10, 2018
Messages
4,189
thanks for the formula:grin:
ill test it tomrow.


but there is a problem.
if the player accuracy tech level is 0 (not researched/upgraded) then 600 / 0 = 0 to unlimit offset range? :huh:
It'd be 600 / (4 + tech level). The divisor is increased by the tech level and has a base value of 4. You can adjust this base value to whatever you'd like.
 
Level 28
Joined
Feb 27, 2007
Messages
4,109
In order to properly bound the accuracy rolls to your satisfaction it is relevant to know a few things:
  • At what distance is accuracy maximized? Is there some range inside of which a unit should be 100% accurate, or should the unit be 100% accurate only if its (x,y) position is explicitly equal to the target's position?
  • How many accuracy tech upgrade levels are there? How much accuracy should this add at max level?
  • What should the relationship between accuracy techs and range-based accuracy be? Basically: are they additive, multiplicative, or something else entirely? For example if my range makes me 40% accurate and I have 1 tech upgrade for +20% accuracy, should that be the same as ranged-based accuracy of 60% with 0 tech level? What about 20% accuracy with +2 tech level? Those were all additive examples but it could be multiplicative such that 40% accurate with +1 tech level of +20% gives you 48% accurate.
  • What's the furthest distance a unit could attack another unit with and need this accuracy check? 600? 2500? 12000? Unlimited? etc.
  • At 100% accuracy, does the attack always land on the target or is there inherent variance? Should anything happen if accuracy goes above 100%?
Answering these questions will help define some boundary conditions and behavior for your accuracy formula, which you can use to determine what the mathematical form of it should be.
 
Level 7
Joined
Oct 16, 2018
Messages
87
In order to properly bound the accuracy rolls to your satisfaction it is relevant to know a few things:
  • At what distance is accuracy maximized? Is there some range inside of which a unit should be 100% accurate, or should the unit be 100% accurate only if its (x,y) position is explicitly equal to the target's position?
  • How many accuracy tech upgrade levels are there? How much accuracy should this add at max level?
  • What should the relationship between accuracy techs and range-based accuracy be? Basically: are they additive, multiplicative, or something else entirely? For example if my range makes me 40% accurate and I have 1 tech upgrade for +20% accuracy, should that be the same as ranged-based accuracy of 60% with 0 tech level? What about 20% accuracy with +2 tech level? Those were all additive examples but it could be multiplicative such that 40% accurate with +1 tech level of +20% gives you 48% accurate.
  • What's the furthest distance a unit could attack another unit with and need this accuracy check? 600? 2500? 12000? Unlimited? etc.
  • At 100% accuracy, does the attack always land on the target or is there inherent variance? Should anything happen if accuracy goes above 100%?
Answering these questions will help define some boundary conditions and behavior for your accuracy formula, which you can use to determine what the mathematical form of it should be.


@[email protected]
youre lighting some bulb lamps above my head.


1. at 100% accuracy unit must attack-ground where the target exactly is. (however if the target moves before arrow touches it, the hit will be unsuccessful and this is what i do want to happen. attack-ground is something dodge-able as i know, people may move and dodge the projectile.)
max range of each unit is the max distance.

2. 10 levels, 3% per level must be ok (30% when maxed).

3. um, what? :ugly:

4. 1800 for archers, 1200 for crossbows, 3200 for catapults and 4800 for trebuchets and... (ballista and others), there is a way to get unit max range and min range. min for the archers is 600... (and other numbers for the other units)

5. yes attack-ground on the exact position of the target. i do want to make it fit 100% not more, theres something called "cur_Accuracy" which is the accuracy of the archer and has 100 levels (0% - 100%). and upgrades may affect this.



this one is something that must be ignored:
im trying my best to dont let such happens using cur_Accuracy and a limit accuracy for archers and crossbows and etc.
 
Level 28
Joined
Feb 27, 2007
Messages
4,109
I see that English isn't your main language, so some of what I said was probably lost in translation. I will try to be more clear.


1. Imagine two circles around your target: one much bigger than the other. The big circle's radius is the max attack range for the attacker (let's it's an archer so its radius is 1800), and if the attacker was standing at the edge of this circle its attack should have some minimum accuracy. What should that minimum be? 0%? 1%? 10%? For now, ignore the accuracy upgrades and imagine that they don't exist.

The smaller circle's radius is the distance at which the attacker has maximum accuracy. If the attacker is at the edge of (or inside of) this circle when making its attack, the attack should have some maximum accuracy. What should that maximum accuracy be? 100% 99% 90%? How big should the radius of this small circle be? Should its radius be the minimum attack distance for the attacker (in this example with archers that number seems to be 600)? Should its radius be the same size for all units no matter what their min attack range is? Should its radius be 0?


3. Now we include the accuracy upgrades and you should consider this scenario: the archer attacking its target has 60% accuracy from its distance to the target, and has 30% accuracy bonus from upgrades. "Additive" means that the two accuracy numbers are added together: 0.60+0.30 = 0.90. "Multiplicative" means the two accuracy numbers are multiplied together: 0.60*0.30 = 0.18, then 0.60+0.18 = 0.78. (In either case it's possible to have accuracy > 100% but we ignore this for now.) Why does this matter?
  • If bonus accuracy is additive, then the accuracy bonus upgrades will function kind of like a floor for accuracy: you can never be less accurate than whatever your tech upgrade level is--this effectively changes the minimum accuracy for the big circle described in 1 above. It also sets a cap on how accurate you have to be to achieve 100% accuracy--this effectively increases the size of the smaller circle described in 1 above.

  • If bonus accuracy is multiplicative, then accuracy bonus upgrades will change the shape of the accuracy function and how it scales. For example: A 10% accurate shot with a 30% multiplicative bonus only gains 3% more accuracy (total of 13% accurate), but an 80% accurate shot with a 30% multiplicative bonus gains 24% more accuracy (for a total of 104%). In this example the accuracy upgrades do almost nothing for you if you are far away, but will help you more the more accurate you were to begin with (maybe because the attacker was closer or whatever). This would change a straight, diagonal line shape into half an upwards facing parabola shape.

Again ignoring bonus accuracy for now, you will probably want your accuracy function to look something like this:

Accuracy Formula.png


The red section is a minimum accuracy that is achieved when the attacker is outside/on the big circle. The green section is max accuracy that is achieved when the attacker is inside/on the small circle. You have to decide what d_max and d_min are (the radii of the big and small circles) but they can be constants or depend on the attacking unit's attack range/unit-type/time-of-day/etc. Same applies to a_min and a_max: they can be constants or defined arbitrarily per accuracy check. Technically there is still the possibility of a divide-by-zero here but that can be worked around.

Finally you need to cap/floor the input value of d (which is x in the above formula), the distance to the target, and the output values of the function.
 
Level 7
Joined
Oct 16, 2018
Messages
87
I see that English isn't your main language, so some of what I said was probably lost in translation. I will try to be more clear.


1. Imagine two circles around your target: one much bigger than the other. The big circle's radius is the max attack range for the attacker (let's it's an archer so its radius is 1800), and if the attacker was standing at the edge of this circle its attack should have some minimum accuracy. What should that minimum be? 0%? 1%? 10%? For now, ignore the accuracy upgrades and imagine that they don't exist.

The smaller circle's radius is the distance at which the attacker has maximum accuracy. If the attacker is at the edge of (or inside of) this circle when making its attack, the attack should have some maximum accuracy. What should that maximum accuracy be? 100% 99% 90%? How big should the radius of this small circle be? Should its radius be the minimum attack distance for the attacker (in this example with archers that number seems to be 600)? Should its radius be the same size for all units no matter what their min attack range is? Should its radius be 0?


3. Now we include the accuracy upgrades and you should consider this scenario: the archer attacking its target has 60% accuracy from its distance to the target, and has 30% accuracy bonus from upgrades. "Additive" means that the two accuracy numbers are added together: 0.60+0.30 = 0.90. "Multiplicative" means the two accuracy numbers are multiplied together: 0.60*0.30 = 0.18, then 0.60+0.18 = 0.78. (In either case it's possible to have accuracy > 100% but we ignore this for now.) Why does this matter?
  • If bonus accuracy is additive, then the accuracy bonus upgrades will function kind of like a floor for accuracy: you can never be less accurate than whatever your tech upgrade level is--this effectively changes the minimum accuracy for the big circle described in 1 above. It also sets a cap on how accurate you have to be to achieve 100% accuracy--this effectively increases the size of the smaller circle described in 1 above.

  • If bonus accuracy is multiplicative, then accuracy bonus upgrades will change the shape of the accuracy function and how it scales. For example: A 10% accurate shot with a 30% multiplicative bonus only gains 3% more accuracy (total of 13% accurate), but an 80% accurate shot with a 30% multiplicative bonus gains 24% more accuracy (for a total of 104%). In this example the accuracy upgrades do almost nothing for you if you are far away, but will help you more the more accurate you were to begin with (maybe because the attacker was closer or whatever). This would change a straight, diagonal line shape into half an upwards facing parabola shape.

Again ignoring bonus accuracy for now, you will probably want your accuracy function to look something like this:

View attachment 398376

The red section is a minimum accuracy that is achieved when the attacker is outside/on the big circle. The green section is max accuracy that is achieved when the attacker is inside/on the small circle. You have to decide what d_max and d_min are (the radii of the big and small circles) but they can be constants or depend on the attacking unit's attack range/unit-type/time-of-day/etc. Same applies to a_min and a_max: they can be constants or defined arbitrarily per accuracy check. Technically there is still the possibility of a divide-by-zero here but that can be worked around.

Finally you need to cap/floor the input value of d (which is x in the above formula), the distance to the target, and the output values of the function.

1. i could understand the english of "1" this time, but couldnt understand what you actually mean.
i dont want them to attack the enemies at their minimum range, and i really cannot understand your meaning...
i do want the archers to attack-ground the target, but there must be a cricle as its accuracy and more accuracy make this cricle smaller and lower accuracy makes it bigger so theres more chance to attack-ground a far point when accuracy is low.



3. i think i have to give up and read this section later again and again to understand :ugly:
its night here and im tired, ill read this again tomrow.
 
Last edited:
Top