scope ItemFieldConditioningKit initializer init
	import Units
	import Players
	import Table
	import Statistics
	import TextTag

	native UnitAlive takes unit u returns boolean

	constant integer DUST_ID     = 'I004'
	constant integer COMBAT_TIME = 5

	timer time = CreateTimer()
	public Table damageTimings = new Table()

	function checkCombatDuration takes nothing returns nothing
		integer index = 0
		real life

		loop
			exitwhen index >= Players.FIRST_BEAST

			if UnitAlive(Units.hero[index]) and UnitHasItemOfTypeBJ(Units.hero[index], DUST_ID) then
				life = GetWidgetLife(Units.hero[index])
				if not damageTimings.hasInt(Units.hero[index].getHandleId()) then
					damageTimings.saveInt(Units.hero[index].getHandleId(), Statistics.secondsElapsed)
				elseif (Statistics.secondsElapsed - damageTimings.loadInt(Units.hero[index].getHandleId())) > COMBAT_TIME and life < GetUnitState(Units.hero[index], UNIT_STATE_MAX_LIFE) then
					SetWidgetLife(Units.hero[index], life + 1)
					TextTag.regeneration(Units.hero[index].getX(), Units.hero[index].getY(), Units.hero[index].getOwner())
				end
			end

			index++
		end
	end

	function init takes nothing returns nothing
		TimerStart(CreateTimer(), 1., true, function checkCombatDuration)
	end
endscope
