Cohadar
master of fugue
- Reaction score
- 209
Ok there will be some ugly math here but don't get frightened.
You don't really need to know math at all to use this tutorial
because i assembled a table of usefull constants
and you can simply copy-paste those
Trigger code is all in 2-3 lines so it is basically really simple
to understand
(there will be a demo map at the end of course)
General Idea:
Lets imagine you want to make some item that gives special ability
to hero when picked up, and looses special ability when dropped.
We will discus 3 template items here:
Orb of Hex - Gives a 33% chance to hero with this item to hex opponents on attack
Orb of Frost - When attacked hero will be protected by Frost Armor
Orb of Shadow - When attacked hero will become invisible
you can make any other items you want that give any ability you want
of course
First idea some people might have is to add the ability directly to hero
But that is bad for several reasons:
#First what if ability needs to be targeted?
#Second it will clutter your icon space
(you can avoid this with spellbook, but that is lots of ugly code and mapping)
#and last to disable this ability you have to disable it for player
there is no way to disable it for single unit, so if you have 2 units
using same items it will fuck things up
So what is the solution?:
Custom Values, but not any kind of custom values
we will use Binary custom values
Basically when hero pick some item we will add some number to his custom value
and when he loses it we will subtract that same number
It is as simple as that
Note: if you don't know what binary numbers are
or don't know how to use them you can simply skip this part,
you don't really need it.
But if you want to understand how this really works read on..
Here are some references for binary numbers:
http://en.wikipedia.org/wiki/Binary_numeral_system
You can also use scientific view of you ordinary windows calculator
to convert binary to ordinary numbers...
Lets do the math:
Custom Values of units are 32 bits long
now one might think that we can use each bit for every special item
we intend to use but there is one small problem
what if hero picks 2 same items and then drops one?
Solution:
Hero can have max of 6 items (that is 110 in binary)
and it takes up 3 bits
So for every item we will use 3 bits
to extract 3 bits we need a Modulo of 8
so every number we add/subtract from hero custom value MUST be a power of 8 (Note 8^0 =1, 8^1=8, 8^2=64 ...)
and since we are using 3 bits per item we can have
32(number of bits in custom vale)/3 = 10 special items
Note: if you just went berserk after reading this don't worry
like I said you don't really need to understand all that
No-Math Section:
Ok so we need to add/subtract some numbers
from our hero to mark his possession/non-possession of certain items
Here are those magic numbers:
Like I said they are all powers of 8
2^0, 2^3, 2^6, 2^9, .... 2^27
Trigger Code:
For every next item instead of 1 you would place 8, 64, 512...
and so on....
Now code for dummy caster:
Note: if you don't know what a dummy caster is there are some really good tutorials on that subject on this forum, search them up
wrf division by 1?
that 1 is a place holder here,
for every special item after this you will place
guess what numbers instead of 1
yes yes 8, 64, 512.. the magic numbers
And finally the demo map:
You don't really need to know math at all to use this tutorial
because i assembled a table of usefull constants
and you can simply copy-paste those
Trigger code is all in 2-3 lines so it is basically really simple
to understand
(there will be a demo map at the end of course)
General Idea:
Lets imagine you want to make some item that gives special ability
to hero when picked up, and looses special ability when dropped.
We will discus 3 template items here:
Orb of Hex - Gives a 33% chance to hero with this item to hex opponents on attack
Orb of Frost - When attacked hero will be protected by Frost Armor
Orb of Shadow - When attacked hero will become invisible
you can make any other items you want that give any ability you want
of course
First idea some people might have is to add the ability directly to hero
Code:
Unit - Add <SomeAbility> to (Triggering unit)
#First what if ability needs to be targeted?
#Second it will clutter your icon space
(you can avoid this with spellbook, but that is lots of ugly code and mapping)
#and last to disable this ability you have to disable it for player
there is no way to disable it for single unit, so if you have 2 units
using same items it will fuck things up
So what is the solution?:
Custom Values, but not any kind of custom values
we will use Binary custom values
Basically when hero pick some item we will add some number to his custom value
and when he loses it we will subtract that same number
It is as simple as that
Note: if you don't know what binary numbers are
or don't know how to use them you can simply skip this part,
you don't really need it.
But if you want to understand how this really works read on..
Here are some references for binary numbers:
http://en.wikipedia.org/wiki/Binary_numeral_system
You can also use scientific view of you ordinary windows calculator
to convert binary to ordinary numbers...
Lets do the math:
Custom Values of units are 32 bits long
now one might think that we can use each bit for every special item
we intend to use but there is one small problem
what if hero picks 2 same items and then drops one?
Solution:
Hero can have max of 6 items (that is 110 in binary)
and it takes up 3 bits
So for every item we will use 3 bits
to extract 3 bits we need a Modulo of 8
so every number we add/subtract from hero custom value MUST be a power of 8 (Note 8^0 =1, 8^1=8, 8^2=64 ...)
and since we are using 3 bits per item we can have
32(number of bits in custom vale)/3 = 10 special items
Note: if you just went berserk after reading this don't worry
like I said you don't really need to understand all that
No-Math Section:
Ok so we need to add/subtract some numbers
from our hero to mark his possession/non-possession of certain items
Here are those magic numbers:
Code:
1, 8, 64, 512, 4096, 32768, 262144, 2097152, 16777216, 134217728
Like I said they are all powers of 8
2^0, 2^3, 2^6, 2^9, .... 2^27
Trigger Code:
Code:
Hex Orb Picked
Events
Unit - A unit Acquires an item
Conditions
(Item-type of (Item being manipulated)) Equal to Orb Of Hex
Actions
Unit - Set the custom value of (Triggering unit) to ((Custom value of (Triggering unit)) + 1)
Code:
Hex Orb Dropped
Events
Unit - A unit Loses an item
Conditions
(Item-type of (Item being manipulated)) Equal to Orb Of Hex
Actions
Unit - Set the custom value of (Triggering unit) to ((Custom value of (Triggering unit)) - 1)
For every next item instead of 1 you would place 8, 64, 512...
Code:
Frost Orb Picked
Events
Unit - A unit Acquires an item
Conditions
(Item-type of (Item being manipulated)) Equal to Orb Of Frost
Actions
Unit - Set the custom value of (Triggering unit) to ((Custom value of (Triggering unit)) + 8)
and so on....
Now code for dummy caster:
Note: if you don't know what a dummy caster is there are some really good tutorials on that subject on this forum, search them up
Code:
Hex Orb
Events
Unit - A unit Is attacked
Conditions
(((Custom value of (Attacking unit)) / 1) mod 8) Greater than 0
(Random integer number between 1 and 100) Less than or equal to 33
Actions
Unit - Add Avatar (Neutral) to (Triggering unit)
Unit - Order Dummy Caster 0006 <gen> to Orc Shadow Hunter - Hex (Attacked unit)
wrf division by 1?
that 1 is a place holder here,
for every special item after this you will place
guess what numbers instead of 1
yes yes 8, 64, 512.. the magic numbers
Code:
Frost Orb
Events
Unit - A unit Is attacked
Conditions
(((Custom value of (Attacked unit)) / 8) mod 8) Greater than 0
((Attacked unit) has buff Frost Armor) Equal to False
Actions
Unit - Order Dummy Caster 0006 <gen> to Undead Lich - Frost Armor (Attacked unit)
Code:
Shadow Orb
Events
Unit - A unit Is attacked
Conditions
(((Custom value of (Attacked unit)) / 64) mod 8) Greater than 0
((Attacked unit) has buff Invisibility) Equal to False
Actions
Wait 1.00 seconds
Unit - Order Dummy Caster 0006 <gen> to Human Sorceress - Invisibility (Attacked unit)
And finally the demo map: