Critique for final(?) version of GDD + new GDM, please

Weep

Godspeed to the sound of the pounding
Reaction score
400
Preface: these systems are meant to not require JassHelper or any World Editor extensions, so no vJass syntax nor Object Merger nor globals of any kind that cannot be declared in the vanilla World Editor.

I'm hoping to make a release of GUI-Friendly Damage Modification sometime along the line, which requires a compatibility update of GDD in order to make GDM an optional "module" rather than making two releases of GDD, integrated and non-integrated. Some efficiency is lost in order to have this flexibility. :(

I'm pretty sure I haven't broken anything or added any leaks. Here's the RC for GDD 1.2.2, with the only changes being a switch of GDD_LeftMapGroup to an array named GDD_Groups to minimize the global count when adding GDM, and some code to detect whether GDM is loaded and if so, use its code instead of the simpler GDD code for running the GUI event. Since this might be the final GDD release, I'd like it if you could scour it.

JASS:
// GUI-Friendly Damage Detection -- v1.2.2 -- by Weep
//    http://www.thehelper.net/forums/showthread.php?t=137957
//
//    Requires: only this trigger and its variables.
//
// -- What? --
//    This system provides a leak-free, GUI-friendly implementation of an "any unit takes
//    damage" event.  It requires no JASS knowledge to use.
//
//    It uses the Game - Value Of Real Variable event as its method of activating other
//    triggers, and passes the event responses through a few globals.
//
// -- Why? --
//    The traditional GUI method of setting up a trigger than runs when any unit is damaged
//    leaks trigger events.  This system is easy to implement and removes the need to do
//    you own GUI damage detection setup.
//
// -- How To Implement --
//    0. Before you copy triggers that use GDD into a new map, you need to copy over GDD
//       with its GDD Variable Creator trigger, or there will be a problem: the variables
//       won't be automatically created correctly.
//
//    1. Be sure "Automatically create unknown variables while pasting trigger data" is
//       enabled in the World Editor general preferences.
//    2. Copy this trigger category ("GDD") and paste it into your map.
//       (Alternately: create the variables listed in the globals block below, create a
//       trigger named "GUI Friendly Damage Detection", and paste in this entire text.)
//    3. Create your damage triggers using Game - Value Of Real Variable as the event,
//       select GDD_Event as the variable, and leave the rest of the settings to the default
//       "becomes Equal to 0.00".
//       The event responses are the following variables:
//          GDD_Damage is the amount of damage, replacing Event Response - Damage Taken.
//          GDD_DamagedUnit is the damaged unit, replacing Event Response - Triggering Unit.
//              Triggering Unit can still be used, if you need to use waits.
//              Read the -- Notes -- section below for more info.
//          GDD_DamageSource is the damaging unit, replacing Event Response - Damage Source.
//
// -- Notes --
//    GDD's event response variables are not wait-safe; you can't use them after a wait in
//    a trigger.  If you need to use waits, Triggering Unit (a.k.a. GetTriggerUnit()) can
//    be used in place of GDD_DamageSource.  There is no usable wait-safe equivalent to
//    Event Damage or Damage Source; you'll need to save the values yourself.
//
//    Don't write any values to the variables used as the event responses, or it will mess
//    up any other triggers using this system for their triggering.  Only use their values.
//
//    This uses arrays, so can detect damage for a maximum of 8190 units at a time, and
//    cleans up data at a rate of 33.33 per second, by default.  This should be enough for
//    most maps, but if you want to change the rate, change the value returned in the
//    GDD_RecycleRate function at the top of the code, below.
//
//    By default, GDD will not register units that have Locust at the moment of their
//    entering the game, and will not recognize when they take damage (which can only
//    happen if the Locust ability is later removed from the unit.)  To allow a unit to have
//    Locust yet still cause GDD damage events if Locust is removed, you can either design
//    the unit to not have Locust by default and add it via triggers after creation, or
//    edit the GDD_Filter function at the top of the code, below.
//
// -- Credits --
//    Captain Griffin on wc3c.net for the research and concept of GroupRefresh.
//
//    Credit in your map not needed, but please include this README.
//
// -- Version History --
//    1.2.2: 
//    1.2.1: Minor code cleaning.  Added configuration functions.  Updated documentation.
//    1.2.0: Made this system work properly with recursive damage.
//    1.1.1: Added a check in order to not index units with the Locust ability (dummy units).
//           If you wish to check for damage taken by a unit that is unselectable, do not
//           give the unit-type Locust in the object editor; instead, add the Locust ability
//           'Aloc' via a trigger after its creation, then remove it.
//    1.1.0: Added a check in case a unit gets moved out of the map and back.
//    1.0.0: First release.


//===================================================================
// Configurables.
constant function GDD_RecycleRate takes nothing returns real //The rate at which the system checks units to see if they've been removed from the game
	return 0.03
endfunction

function GDD_Filter takes unit u returns boolean //The condition a unit has to pass to have it registered for damage detection
	return GetUnitAbilityLevel(u, 'Aloc') == 0 //By default, the system ignores Locust units, because they normally can't take damage anyway
endfunction

//===================================================================
// This is just for reference.
// If you use JassHelper, you could uncomment this section instead of creating the variables in the trigger editor.

// globals
// 	real udg_GDD_Event = 0.
// 	real udg_GDD_Damage = 0.
// 	unit udg_GDD_DamagedUnit
// 	unit udg_GDD_DamageSource
// 	trigger array udg_GDD__TriggerArray
// 	integer array udg_GDD__Integers
// 	unit array udg_GDD__UnitArray
// 	group array udg_GDD__Groups
// endglobals

//===================================================================
// System code follows.  Don't touch!
function GDD_Run takes nothing returns boolean
	set udg_GDD_Event = -1000.
	set udg_GDD_Event = 0.
	return false
endfunction

function GDD_Event takes nothing returns boolean
	local unit damagedCache = udg_GDD_DamagedUnit
	local unit damagingCache = udg_GDD_DamageSource
	local real damageCache = udg_GDD_Damage
	set udg_GDD_DamagedUnit = GetTriggerUnit()
	set udg_GDD_DamageSource = GetEventDamageSource()
	set udg_GDD_Damage = GetEventDamage()
	set udg_GDD__TriggerArray[0] = GetTriggeringTrigger()
	call TriggerEvaluate(gg_trg_GUI_Friendly_Damage_Detection)
	set udg_GDD_DamagedUnit = damagedCache
	set udg_GDD_DamageSource = damagingCache
	set udg_GDD_Damage = damageCache
	set damagedCache = null
	set damagingCache = null
	return false
endfunction

function GDD_AddDetection takes nothing returns boolean
// 	if(udg_GDD__Integers[0] > 8190) then
// 		call BJDebugMsg("GDD: Too many damage events!  Decrease number of units present in the map or increase recycle rate.")
// 		***Recycle rate is specified in the GDD_RecycleRate function at the top of the code.  Smaller is faster.***
// 		return
// 	endif
	if(IsUnitInGroup(GetFilterUnit(), udg_GDD__Groups[0])) then
		call GroupRemoveUnit(udg_GDD__Groups[0], GetFilterUnit())
	elseif(GDD_Filter(GetFilterUnit())) then
		set udg_GDD__Integers[0] = udg_GDD__Integers[0]+1
		set udg_GDD__UnitArray[udg_GDD__Integers[0]] = GetFilterUnit()
		set udg_GDD__TriggerArray[udg_GDD__Integers[0]] = CreateTrigger()
		call TriggerRegisterUnitEvent(udg_GDD__TriggerArray[udg_GDD__Integers[0]], udg_GDD__UnitArray[udg_GDD__Integers[0]], EVENT_UNIT_DAMAGED)
		call TriggerAddCondition(udg_GDD__TriggerArray[udg_GDD__Integers[0]], Condition(function GDD_Event))
	endif
	return false
endfunction

function GDD_PreplacedDetection takes nothing returns nothing
	local group g = CreateGroup()
	local integer i = 0
	loop
		call GroupEnumUnitsOfPlayer(g, Player(i), Condition(function GDD_AddDetection))
		set i = i+1
		exitwhen i == bj_MAX_PLAYER_SLOTS
	endloop
	call DestroyGroup(g)
	set g = null
endfunction

function GDD_GroupRefresh takes nothing returns nothing
// Based on GroupRefresh by Captain Griffen on wc3c.net
	if (bj_slotControlUsed[5063] == true) then
		call GroupClear(udg_GDD__Groups[0])
		set bj_slotControlUsed[5063] = false
	endif
	call GroupAddUnit(udg_GDD__Groups[0], GetEnumUnit())
endfunction

function GDD_Recycle takes nothing returns nothing
	if(udg_GDD__Integers[0] <= 0) then
		return
	elseif(udg_GDD__Integers[1] <= 0) then
		set udg_GDD__Integers[1] = udg_GDD__Integers[0]
	endif
	if(GetUnitTypeId(udg_GDD__UnitArray[udg_GDD__Integers[1]]) == 0) then
		call DestroyTrigger(udg_GDD__TriggerArray[udg_GDD__Integers[1]])
		set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = null
		set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = udg_GDD__TriggerArray[udg_GDD__Integers[0]]
		set udg_GDD__UnitArray[udg_GDD__Integers[1]] = udg_GDD__UnitArray[udg_GDD__Integers[0]]
		set udg_GDD__UnitArray[udg_GDD__Integers[0]] = null
		set udg_GDD__Integers[0] = udg_GDD__Integers[0]-1
	endif
	set udg_GDD__Integers[1] = udg_GDD__Integers[1]-1
endfunction

function GDD_LeaveMap takes nothing returns boolean
	local boolean Cached = bj_slotControlUsed[5063]
	if(udg_GDD__Integers[2] < 64) then
		set udg_GDD__Integers[2] = udg_GDD__Integers[2]+1
	else
		set bj_slotControlUsed[5063] = true
		call ForGroup(udg_GDD__Groups[0], function GDD_GroupRefresh)
		set udg_GDD__Integers[2] = 0
	endif
	call GroupAddUnit(udg_GDD__Groups[0], GetFilterUnit())
	set bj_slotControlUsed[5063] = Cached
	return false
endfunction

// ===========================================================================
function InitTrig_GUI_Friendly_Damage_Detection takes nothing returns nothing
	local region r = CreateRegion()
	if udg_GDD__Groups[0] == null then
		set udg_GDD__Groups[0] = CreateGroup()
	endif
	
	call RegionAddRect(r, GetWorldBounds())
	call TriggerRegisterEnterRegion(CreateTrigger(), r, Condition(function GDD_AddDetection))
	call TriggerRegisterLeaveRegion(CreateTrigger(), r, Condition(function GDD_LeaveMap))
	call GDD_PreplacedDetection()
	call TimerStart(CreateTimer(), GDD_RecycleRate(), true, function GDD_Recycle)
	set r = null
	
	if gg_trg_GUI_Friendly_Damage_Detection == null then
		set gg_trg_GUI_Friendly_Damage_Detection = CreateTrigger()
		call TriggerAddCondition(gg_trg_GUI_Friendly_Damage_Detection, Condition(function GDD_Run))
	endif
endfunction


Then, there's GDM, which I haven't yet documented thoroughly. It's an event response sequencing and damage modifying addition to GDD, with an innovative(?) way of detecting its corresponding object editor ability without having to change a rawcode every time the user pastes it.

It's been long enough since I first wrote it that I almost don't remember how the code is structured. :p I've been using it a lot and it seems to work properly. Again, a scouring for errors or efficiency improvements (remember the preface) would be appreciated. :)

JASS:
//===================================================================
// Configurables.
constant function GDM_AbilityDeathPrevention takes nothing returns string //The name of the death prevention ability
	return "GDM Death Prevention"
endfunction

constant function GDM_FirstRawcode takes nothing returns integer //The lowest rawcode to check for the death prevention ability
	return 'A000'
endfunction

constant function GDM_LastRawcode takes nothing returns integer //The highest rawcode to check for the death prevention ability
	return 'A500'
endfunction

//===================================================================
// This is just for reference.
// If you use JassHelper, you could uncomment this section instead of creating the variables in the trigger editor.

// globals
// 	real udg_GDM_ToAdd = 0.
// 	real udg_GDM_ToBlock = 0.

// 	real udg_GDM_DamageAdjusted = 0.
// 	real udg_GDM_TotalAdded = 0.
// 	real udg_GDM_TotalBlocked = 0.

//	timer udg_GDM__Timer
// endglobals

//===================================================================
// System code follows.  Don't touch!
function GDM_BlockReset takes nothing returns nothing
	local unit u = GetEnumUnit()
	local real health = GetWidgetLife(u)
	call UnitRemoveAbility(u, udg_GDD__Integers[4])
	if health > 0.405 then
		call SetWidgetLife(u, health)
	endif
	set u = null
endfunction

function GDM_BlockResetCallback takes nothing returns nothing
	call ForGroup(udg_GDD__Groups[1], function GDM_BlockReset)
	call GroupClear(udg_GDD__Groups[1])
endfunction

function GDM_Run takes nothing returns boolean
	local real addCache = udg_GDM_TotalAdded
	local real blockCache = udg_GDM_TotalBlocked
	local real damageCache = udg_GDM_DamageAdjusted
	local integer stepCache = udg_GDD__Integers[6]
	set udg_GDM_TotalAdded = 0.
	set udg_GDM_TotalBlocked = 0.
	set udg_GDD__Integers[6] = 0
	
	set udg_GDD_Event = -1000.
	loop
		if udg_GDM_TotalAdded > 0. then
			set udg_GDM_DamageAdjusted = udg_GDD_Damage + udg_GDM_TotalAdded
		else
			set udg_GDM_DamageAdjusted = udg_GDD_Damage
		endif
		if udg_GDM_TotalBlocked > 0. then
			set udg_GDM_DamageAdjusted = udg_GDM_DamageAdjusted - udg_GDM_TotalBlocked
		endif
		
		set udg_GDD_Event = I2R(udg_GDD__Integers[6])
		exitwhen udg_GDD__Integers[6] == 10
		set udg_GDD__Integers[6] = udg_GDD__Integers[6]+1
	endloop

	if udg_GDM_TotalAdded < 0. then
		set udg_GDM_TotalAdded = 0.
	endif
	if udg_GDM_TotalBlocked < 0. then
		set udg_GDM_TotalBlocked = 0.
	else
		if udg_GDM_TotalBlocked > udg_GDD_Damage + udg_GDM_TotalAdded then
			set udg_GDM_TotalBlocked = udg_GDD_Damage + udg_GDM_TotalAdded
		endif
	endif
	set udg_GDM_DamageAdjusted = udg_GDD_Damage + udg_GDM_TotalAdded - udg_GDM_TotalBlocked
	set udg_GDD__Integers[6] = -1
	set udg_GDD_Event = -1.
	
	if udg_GDM_TotalAdded > udg_GDM_TotalBlocked then
		set udg_GDM_TotalAdded = udg_GDM_TotalAdded - udg_GDM_TotalBlocked
		
		call DisableTrigger(udg_GDD__TriggerArray[0])
		call UnitDamageTarget(udg_GDD_DamageSource, udg_GDD_DamagedUnit, udg_GDM_TotalAdded, true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_UNIVERSAL, WEAPON_TYPE_WHOKNOWS)
		call EnableTrigger(udg_GDD__TriggerArray[0])
	elseif udg_GDD_Damage > 0. and udg_GDM_TotalBlocked > udg_GDM_TotalAdded then
		set udg_GDM_TotalBlocked = udg_GDM_TotalBlocked - udg_GDM_TotalAdded
		if udg_GDM_TotalBlocked > udg_GDD_Damage then
			set udg_GDM_TotalBlocked = udg_GDD_Damage
		endif
	
		set udg_GDM_DamageAdjusted = GetWidgetLife(udg_GDD_DamagedUnit) + udg_GDM_TotalBlocked
		if GetUnitState(udg_GDD_DamagedUnit, UNIT_STATE_MAX_LIFE) < udg_GDM_DamageAdjusted then
			call UnitAddAbility(udg_GDD_DamagedUnit, udg_GDD__Integers[4])
			call GroupAddUnit(udg_GDD__Groups[1], udg_GDD_DamagedUnit)
			call TimerStart(udg_GDM__Timer, 0., false, function GDM_BlockResetCallback)
		endif
		call SetWidgetLife(udg_GDD_DamagedUnit, udg_GDM_DamageAdjusted)
	endif

	set udg_GDM_DamageAdjusted = damageCache
	set udg_GDM_TotalAdded = addCache
	set udg_GDM_TotalBlocked = blockCache
	set udg_GDD__Integers[6] = stepCache
	return false
endfunction

function GDM_AddDamage takes nothing returns boolean
	if udg_GDD__Integers[6] != -1 then
		set udg_GDM_TotalAdded = udg_GDM_TotalAdded + udg_GDM_ToAdd
	endif
	set udg_GDM_ToAdd = 0.
	return false
endfunction

function GDM_AddBlock takes nothing returns boolean
	if udg_GDD__Integers[6] != -1 then
		set udg_GDM_TotalBlocked = udg_GDM_TotalBlocked + udg_GDM_ToBlock
	endif
	set udg_GDM_ToBlock = 0.
	return false
endfunction

function GDM_FindDeathPreventionAbilityNewThread takes nothing returns nothing
	local integer i = udg_GDD__Integers[5]+250
	local string s = GDM_AbilityDeathPrevention()

	loop
		exitwhen udg_GDD__Integers[5] == i
		if GetObjectName(udg_GDD__Integers[5]) == s then
			set udg_GDD__Integers[4] = udg_GDD__Integers[5]
			exitwhen true
		endif
		set udg_GDD__Integers[5] = udg_GDD__Integers[5]+1
	endloop
endfunction

function GDM_FindDeathPreventionAbility takes nothing returns nothing
	local trigger t = CreateTrigger()
	local triggeraction a = TriggerAddAction(t, function GDM_FindDeathPreventionAbilityNewThread)
	
	set udg_GDD__Integers[5] = GDM_FirstRawcode()
	loop
		exitwhen udg_GDD__Integers[4] > 0 or udg_GDD__Integers[5] > GDM_LastRawcode()
		call TriggerExecute(t)
	endloop
	
	if udg_GDD__Integers[4] == 0 then
		call BJDebugMsg("GDM Error: Could not find the death prevention ability!")
	endif

	call TriggerRemoveAction(t, a)
	call DestroyTrigger(t)
	set t = null
	set a = null
endfunction

// ===========================================================================
function InitTrig_GUI_Friendly_Damage_Modification takes nothing returns nothing
	local trigger t = CreateTrigger()
	call TriggerRegisterVariableEvent(t, "udg_GDM_ToAdd", NOT_EQUAL, 0.)
	call TriggerAddCondition(t, Condition(function GDM_AddDamage))
	set t = CreateTrigger()
	call TriggerRegisterVariableEvent(t, "udg_GDM_ToBlock", NOT_EQUAL, 0.)
	call TriggerAddCondition(t, Condition(function GDM_AddBlock))
	set t = null

	set udg_GDD__Groups[1] = CreateGroup()
	call GDM_FindDeathPreventionAbility()
	
	if gg_trg_GUI_Friendly_Damage_Detection == null then
		set gg_trg_GUI_Friendly_Damage_Detection = CreateTrigger()
	else
		call TriggerClearConditions(gg_trg_GUI_Friendly_Damage_Detection)
	endif
	call TriggerAddCondition(gg_trg_GUI_Friendly_Damage_Detection, Condition(function GDM_Run))
endfunction
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
Bump. Is there any additional information needed in order to comment on the code?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
It looks great to me. The only problem is this:


GetWorldBounds() generates a new rect each time. Thus, you need to assign a variable to it and remove it at the end of the script. ;) EDIT: Nvm this, you are right.

Otherwise, great job. I'll definitely be sure to recommend this to any GUI'ers looking for a damage detection system. The damage modification is just awesome. :thup:
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
It looks great to me. The only problem is this:


GetWorldBounds() generates a new rect each time. Thus, you need to assign a variable to it and remove it at the end of the script. ;)

Bah! It's done once at map init and remains current over the game's duration. Harumph and poppycock, so sayeth I. :nuts:

Otherwise, great job. I'll definitely be sure to recommend this to any GUI'ers looking for a damage detection system. The damage modification is just awesome. :thup:
Well, I should officially submit it first. :D Thanks for the feedback.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top