- Joined
- Aug 16, 2012
- Messages
- 15
SetUnitDamage
This system allows you to add or subtract damage from a unit. No green numbers following a plus sign, no background code, just simple damage addition (or subtraction).
WARNING: Tomes leak. This snippet uses tomes, and there have been suggestions that this snippet may leak. To deal with this problem, please use ItemCleanup found at http://www.hiveworkshop.com/forums/jass-resources-412/system-item-cleanup-175663/
Usage Example:
You want to add 10 damage to a peasant.
If you use a bonus style mod you will get: Damage: 5 - 6 + 10
(Note: 2 of the 3 most common bonus systems require unit indexing systems and have functionality unrelated to damage.)
If you use this function you will get: Damage: 15 - 16
The functional difference is that other systems use abilities that can be added to units to increase their damage with a nice green plus and the damage amount. This library/function creates several items that function as tomes of damage and add directly to, or subtract from the unit's damage. You can also use other systems with this function. The above peasant could easily have 15 - 16 + 10 damage.
Lua:
Jass:
Thank you to PurgeandFire111 for his Lua tutorial here on THW.
Edit: Changed post to reflect functionality.
This system allows you to add or subtract damage from a unit. No green numbers following a plus sign, no background code, just simple damage addition (or subtraction).
WARNING: Tomes leak. This snippet uses tomes, and there have been suggestions that this snippet may leak. To deal with this problem, please use ItemCleanup found at http://www.hiveworkshop.com/forums/jass-resources-412/system-item-cleanup-175663/
Usage Example:
You want to add 10 damage to a peasant.
If you use a bonus style mod you will get: Damage: 5 - 6 + 10
(Note: 2 of the 3 most common bonus systems require unit indexing systems and have functionality unrelated to damage.)
If you use this function you will get: Damage: 15 - 16
The functional difference is that other systems use abilities that can be added to units to increase their damage with a nice green plus and the damage amount. This library/function creates several items that function as tomes of damage and add directly to, or subtract from the unit's damage. You can also use other systems with this function. The above peasant could easily have 15 - 16 + 10 damage.
Lua:
JASS:
//! externalblock extension=lua ObjectMerger $FILENAME$
//! i function createdamagemodifier (id, i)
//! i setobjecttype ("abilities")
//! i createobject ("AIaa", "adb" .. id)
//! i makechange (current, "ansf", "+" .. i)
//! i makechange (current, "anam", "Damage Modifier")
//! i makechange (current, "Iaa1", 1, i)
//! i makechange (current, "acat", "")
//! i setobjecttype ("items")
//! i createobject ("tdex", "idb" .. id)
//! i makechange (current, "unsf", "+" .. i)
//! i makechange (current, "unam", "Damage Modifier")
//! i makechange (current, "iabi", "adb" .. id)
//! i makechange (current, "ifil", "Doodads\Terrain\LOSBlocker\IntentionallyLeftBlank.mdl")
//! i makechange (current, "ipow", 0)
//! i end
//! i local i = 0
//! i while (i < 5) do
//! i createdamagemodifier (i, 10 ^ i)
//! i createdamagemodifier (i + 5, -1 * 10 ^ i)
//! i i = i + 1
//! i end
//! endexternalblock
Jass:
JASS:
library SetUnitDamage uses /*
*/optional RegisterPlayerUnitEvent // [url]www.hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/[/url]
/*
Version 1.0 by Contradictator
Thanks to Magtheridon96 and PurgeandFire111 for providing
compatability with RegisterPlayerUnitEvent.
This library allows the damage of units to be manipulated
in-game without the use of specific upgrades, or green number
bonuses. Note: this can only change the base damage of a unit.
For this system to work you must copy the provided lua script
into your map, save the map, and close the map. Thereafter,
it is suggested that you either delete the lua script or
atleast disable the trigger containing it.
WARNING: Unit damage can go negative, but will display as 0.
This means that a unit that shows 0-0 damage may actually
have -12 damage. This unit will deal 0 damage on attack. If
you give that unit an item to do +10 damage, it would show
in game as doing 0-0 +10, but would still do -2 and deal no
damage on attack.
ADDITIONAL WARNING: This system will fire events for units
dropping and picking up items unless the optional library
by Magtheridon96 is also used in which case it will not fire
such events.
For your use:
function AddUnitDamage
takes unit u, integer d
returns nothing
function SetUnitDamage
takes unit u, integer currentDamage, integer newDamage
returns nothing
*/
globals
// Damage modifying items.
private item array dm
// The amount that each modifier adds to damage.
private integer array dmVal
endglobals
// returns the amount of damage added with each modifier used.
private function UnitUseDamageModifier takes unit u, integer index, integer times returns integer
local item i = dm[index]
local integer amtAdded = dmVal[index] * times
// make sure there is still a charge left afterward.
call SetItemCharges (i, times + 1)
static if LIBRARY_RegisterPlayerUnitEvent then
call DisableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_PICKUP_ITEM))
endif
call UnitAddItem (u, i)
static if LIBRARY_RegisterPlayerUnitEvent then
call EnableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_PICKUP_ITEM))
endif
loop
exitwhen times == 0
call UnitUseItem (u, i)
set times = times - 1
endloop
static if LIBRARY_RegisterPlayerUnitEvent then
call DisableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_DROP_ITEM))
endif
call UnitRemoveItem (u, i)
static if LIBRARY_RegisterPlayerUnitEvent then
call EnableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_DROP_ITEM))
endif
call SetItemVisible (i, false)
set i = null
return amtAdded
endfunction
function AddUnitDamage takes unit u, integer d returns nothing
local integer i
local integer iEnd
local item it
local boolean inv = false
if UnitInventorySize (u) > 0 then
// The unit has an inventory. Remove the item in the
// first slot and hide it so we can put damage
// modifying items in that slot.
set inv = true
set it = UnitItemInSlot (u, 0)
static if LIBRARY_RegisterPlayerUnitEvent then
call DisableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_DROP_ITEM))
endif
call UnitRemoveItem (u, it)
static if LIBRARY_RegisterPlayerUnitEvent then
call EnableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_DROP_ITEM))
endif
call SetItemVisible (it, false)
else
// The unit has no inventory, so we'll give it one now
// and remove it later.
call UnitAddAbility (u, 'AInv')
endif
if d < 0 then
// The unit needs the negative damage modifiers which
// are stored in indicies 9 - 5.
set i = 9
set iEnd = 4
else
// The unit needs the positive damage modifiers which
// are stored in indicies 4 - 0.
set i = 4
set iEnd = -1
endif
loop
exitwhen i == iEnd
// Have the unit use the damage modifier the whole
// number of times is needs to (R2I rounds down).
set d = d - UnitUseDamageModifier (u, i, R2I (d / dmVal[i]))
set i = i - 1
endloop
if inv then
// The unit had an inventory, so give it it's item back,
// and don't remove the inventory.
static if LIBRARY_RegisterPlayerUnitEvent then
call DisableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_PICKUP_ITEM))
endif
call UnitAddItem (u, it)
static if LIBRARY_RegisterPlayerUnitEvent then
call EnableTrigger (GetPlayerUnitEventTrigger (EVENT_PLAYER_UNIT_PICKUP_ITEM))
endif
else
// The unit had no inventory, so remove the one it was
// given.
call UnitRemoveAbility (u, 'AInv')
endif
set it = null
endfunction
function SetUnitDamage takes unit u, integer currentDamage, integer newDamage returns nothing
call AddUnitDamage (u, newDamage - currentDamage)
endfunction
private module mod
private static method onInit takes nothing returns nothing
local integer i = 0
local integer x = 1
loop
exitwhen i == 10
set dm[i] = CreateItem ('idb0' + i, 0, 0)
call SetItemVisible (dm[i], false)
set i = i + 1
endloop
set i = 0
loop
exitwhen i == 5
set dmVal[i] = x
set dmVal[i + 5] = -x
set i = i + 1
set x = x * 10
endloop
endmethod
endmodule
private struct init
implement mod
endstruct
endlibrary
Thank you to PurgeandFire111 for his Lua tutorial here on THW.
Edit: Changed post to reflect functionality.
Last edited: