• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Get a random value from the unit attack range

Status
Not open for further replies.
Level 1
Joined
Jun 11, 2017
Messages
2
I want to get the attack range of a unit and make an action generating a random number from the unit attack range.

Example: I have a footman with 15-42 attack range
From that attack range i wanna generate a random integer from 15-42 without putting it manually on the values.
 
Level 39
Joined
Feb 27, 2007
Messages
5,019
Damage ranges are calculated as such:

Min = Base + (Num Dice)*1
Max = Base + (Num Dice)*(Sides per Die)

These values are now available in the trigger editor as Unit - Base Damage, Unit - Dice Number, and Unit - Dice Sides. They are 0-indexed not 1-indexed which means "weapon index 0" is for attack1 and "weapon index 1" is for attack2.
  • Set UNIT = (Triggering Unit) //or whatever
  • Set DMG = (Base Damage of UNIT for weapon index 0)
  • Set DICE = (Dice Number of UNIT for weapon index 0)
  • Set SIDES = (Dice Sides of UNIT for weapon index 0)
  • Set MIN = (BASE + DICE)
  • Set MAX = (BASE + (DICE x SIDES))
 
Level 1
Joined
Jun 11, 2017
Messages
2
Damage ranges are calculated as such:

Min = Base + (Num Dice)*1
Max = Base + (Num Dice)*(Sides per Die)

These values are now available in the trigger editor as Unit - Base Damage, Unit - Dice Number, and Unit - Dice Sides. They are 0-indexed not 1-indexed which means "weapon index 0" is for attack1 and "weapon index 1" is for attack2.
  • Set UNIT = (Triggering Unit) //or whatever
  • Set DMG = (Base Damage of UNIT for weapon index 0)
  • Set DICE = (Dice Number of UNIT for weapon index 0)
  • Set SIDES = (Dice Sides of UNIT for weapon index 0)
  • Set MIN = (BASE + DICE)
  • Set MAX = (BASE + (DICE x SIDES))


Thanks mate, i will put it on work
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
One should be able to use the recently added natives to get the unit attack range. One can then perform the maths as normal.
JASS:
// This function can be used to lookup the attack range of a specific weapon.
native BlzGetUnitWeaponRealField takes unit whichUnit,unitweaponrealfield whichField,integer index returns real

// This is the constant for the attack range field.
constant unitweaponrealfield UNIT_WEAPON_RF_ATTACK_RANGE=ConvertUnitWeaponRealField('ua1r')
The term "attack range" in Warcraft III represents how far a unit can attack. For example an attack range of 1280 would be able to attack anything up to 10 tiles away. Looking at your response you probably meant to ask for attack damage instead.
From that attack range i wanna generate a random integer from 15-42 without putting it manually on the values.
If you are trying to roll a number similar to the damage number which Warcraft III generates when a unit attacks, then the maths might not be as simple as rolling a random number in range between minimum and maximum. Due to the use of multiple dice the probability distribution is not constant across the range.

For example a unit with 2 dice of 3 sides has the following chances for particular damage outcomes.
1/9 -> 2
2/9 -> 3
3/9 -> 4
2/9 -> 5
1/9 -> 6
So despite it showing in the UI as 2-6 damage, it will mostly land within the middle of the range.
 
Level 7
Joined
Jul 4, 2007
Messages
249
For example a unit with 2 dice of 3 sides has the following chances for particular damage outcomes.
1/9 -> 2
2/9 -> 3
3/9 -> 4
2/9 -> 5
1/9 -> 6
So despite it showing in the UI as 2-6 damage, it will mostly land within the middle of the range.
This is very interesting, haven't thought of that before. This actually makes a huge difference, I always felt like there was something off with this calculation
Min = Base + (Num Dice)*1
Max = Base + (Num Dice)*(Sides per Die)
Is it worth doing the extra calculations for it though? I kinda feel that it is but I'm not sure
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
Is it worth doing the extra calculations for it though? I kinda feel that it is but I'm not sure
If you want to simulate the damage accurately, it is.

The following shows the shape of the distribution as dice number increases in the Summing Dice section.
Why convolution regularize functions?

As number of dice increase the probability distribution goes towards the normal distribution.
Normal distribution - Wikipedia

The reason it is important to simulate is due to how the rolls are distributed. It is unlikely that extreme values are rolled as shown in the image below.
Normal distribution - Wikipedia
 
Level 7
Joined
Jul 4, 2007
Messages
249
If you want to simulate the damage accurately, it is.

The following shows the shape of the distribution as dice number increases in the Summing Dice section.
Why convolution regularize functions?

As number of dice increase the probability distribution goes towards the normal distribution.
Normal distribution - Wikipedia

The reason it is important to simulate is due to how the rolls are distributed. It is unlikely that extreme values are rolled as shown in the image below.
Normal distribution - Wikipedia
This is very useful information, thank you!
 
Level 39
Joined
Feb 27, 2007
Messages
5,019
To randomize this distribution easily just literally imagine rolling dice.
  • Set UNIT = (Triggering Unit) //or whatever
  • Set DMG = (Base Damage of UNIT for weapon index 0)
  • Set DICE = (Dice Number of UNIT for weapon index 0)
  • Set SIDES = (Dice Sides of UNIT for weapon index 0)
  • -------- --------
  • Set Total = DMG
  • For each (Integer A) from 1 to DICE do (Actions)
    • Loop - Actions
      • Set Total = (Total + (Random integer between 1 and SIDES))
  • -------- use Total as desired --------
 
Status
Not open for further replies.
Top