scriptname BetterVampiresBloodMeterUpdate extends Quest


; -------------------------------------------------------------------------------------------------
; VARIABLES ---------------------------------------------------------------------------------------


BetterVampiresBloodMeter property ExposureMeter auto
GlobalVariable Property EnableVampireBloodPoints Auto
GlobalVariable Property EnableVampireBloodPointsMeter Auto
GlobalVariable Property VampireBloodPoints Auto
GlobalVariable Property VampireDynamicStages Auto
GlobalVariable property BetterVampiresBloodMeterDisplay_Contextual auto
GlobalVariable property BetterVampiresBloodMeter_Opacity auto
GlobalVariable Property BetterVampiresBloodMeterScale Auto
GlobalVariable property BetterVampiresBloodMeterX auto
GlobalVariable property BetterVampiresBloodMeterY auto
GlobalVariable property BetterVampiresBloodMeterDisplayTime auto
 
float fLastBloodPointValue
int iDisplayIterationsRemaining


; -------------------------------------------------------------------------------------------------
; EVENTS ------------------------------------------------------------------------------------------


Event OnInit()

	ExposureMeter.HAnchor = "left"
	ExposureMeter.VAnchor = "bottom"
	ExposureMeter.X = BetterVampiresBloodMeterX.GetValue() ; Default is 67
	ExposureMeter.Y = BetterVampiresBloodMeterY.GetValue() ; Default is 640
	
	StartUpdating()
	
endEvent


Event OnGameReload()

	StartUpdating()
	
endEvent


function StartUpdating()

	RegisterForSingleUpdate(2)
	
endFunction


Event OnUpdate()

	if EnableVampireBloodPoints.GetValue() == 0 || EnableVampireBloodPointsMeter.GetValue() == 0
		ExposureMeter.Alpha = 0.0
		;UnRegisterForUpdate()
	else
		UpdateMeter()
	endif

	if EnableVampireBloodPoints.GetValue() != 0 && EnableVampireBloodPointsMeter.GetValue() == 10000
		RegisterForSingleUpdate(2)
	endif	
	
endEvent


; -------------------------------------------------------------------------------------------------
; FUNCTIONS ---------------------------------------------------------------------------------------


function UpdateMeter(bool bSkipDisplayHandling = false)



	float fThisBloodPointValue = VampireBloodPoints.GetValue()
	
	if EnableVampireBloodPoints.GetValue() != 0
		UpdateBlood(fThisBloodPointValue, bSkipDisplayHandling)
	endif
	
	fLastBloodPointValue = fThisBloodPointValue

	if iDisplayIterationsRemaining > 0
		iDisplayIterationsRemaining -= 1
		if iDisplayIterationsRemaining <= 0
			iDisplayIterationsRemaining = 0
			if BetterVampiresBloodMeterDisplay_Contextual.GetValueInt() != 1
				ExposureMeter.FadeTo(0.0, 3.0)
			endif
		endif
	elseif iDisplayIterationsRemaining == 0
		if ExposureMeter.Alpha == BetterVampiresBloodMeter_Opacity.GetValue()
			if BetterVampiresBloodMeterDisplay_Contextual.GetValueInt() != 1
				ExposureMeter.FadeTo(0.0, 3.0)
			endif
		endif
	else
	endif	
	
endFunction


function UpdateBlood(float fThisBloodPointValue, bool bSkipDisplayHandling = false)

	if BetterVampiresBloodMeterDisplay_Contextual.GetValueInt() == 1
		ExposureMeter.Alpha = BetterVampiresBloodMeter_Opacity.GetValue()
	elseif BetterVampiresBloodMeterDisplay_Contextual.GetValueInt() == 2 || bSkipDisplayHandling
		ContextualDisplay(fThisBloodPointValue)
	elseif BetterVampiresBloodMeterDisplay_Contextual.GetValueInt() == 0 && iDisplayIterationsRemaining == 0
		ExposureMeter.Alpha = 0.0
		return
	endif
	if fLastBloodPointValue <= 0
		ExposureMeter.ForcePercent(0.0)
	endif

	ExposureMeter.HAnchor = "left" 
	ExposureMeter.VAnchor = "bottom" 
	ExposureMeter.X = BetterVampiresBloodMeterX.GetValue() ; Default is 67
	ExposureMeter.Y = BetterVampiresBloodMeterY.GetValue() ; Default is 640
	Exposuremeter.Height = ((BetterVampiresBloodMeterScale.GetValue()/100)*25.2) ; Default Scale is 100 with Height of 25.2
	Exposuremeter.Width = ((BetterVampiresBloodMeterScale.GetValue()/100)*292.8) ; Default Scale is 100 with Width of 292.8
	
	If VampireDynamicStages.GetValue() == 20000
		ExposureMeter.SetPercent((fThisBloodPointValue) / 100.0)
	ElseIf VampireDynamicStages.GetValue() != 20000
		ExposureMeter.SetPercent((fThisBloodPointValue) / 300.0)
	EndIf

	Int _primaryColor = 4259840
	
	If VampireDynamicStages.GetValue() == 20000
		If VampireBloodPoints.GetValue() > 67
			_primaryColor	= 11141120
		ElseIf VampireBloodPoints.GetValue() > 37
			_primaryColor	= 6553600
		ElseIf VampireBloodPoints.GetValue() > 0
			_primaryColor	= 3276800
		EndIf
	ElseIf VampireDynamicStages.GetValue() < 20000
		If VampireBloodPoints.GetValue() > 200
			_primaryColor	= 11141120
		ElseIf VampireBloodPoints.GetValue() > 100
			_primaryColor	= 6553600
		ElseIf VampireBloodPoints.GetValue() > 0
			_primaryColor	= 3276800
		EndIf	
	EndIf		
	
	ExposureMeter.SetColors(_primaryColor, 3276800)
	
endFunction


function ContextualDisplay(float fThisBloodPointValue, bool bSkipDisplayHandling = false)

	if bSkipDisplayHandling
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()
		return
	endif

	bool BloodPointsChanged = fLastBloodPointValue != fThisBloodPointValue	
	
	if BloodPointsChanged && VampireBloodPoints.GetValue() > 290 && VampireBloodPoints.GetValue() <= 300
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 280 && VampireBloodPoints.GetValue() <= 290
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 270 && VampireBloodPoints.GetValue() <= 280
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 260 && VampireBloodPoints.GetValue() <= 270
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 250 && VampireBloodPoints.GetValue() <= 260
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 240 && VampireBloodPoints.GetValue() <= 250
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 230 && VampireBloodPoints.GetValue() <= 240
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 220 && VampireBloodPoints.GetValue() <= 230
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 210 && VampireBloodPoints.GetValue() <= 220
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 200 && VampireBloodPoints.GetValue() <= 210
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 190 && VampireBloodPoints.GetValue() <= 200
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 180 && VampireBloodPoints.GetValue() <= 190
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 170 && VampireBloodPoints.GetValue() <= 180
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 160 && VampireBloodPoints.GetValue() <= 170
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 150 && VampireBloodPoints.GetValue() <= 160
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 140 && VampireBloodPoints.GetValue() <= 150
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 130 && VampireBloodPoints.GetValue() <= 140
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 120 && VampireBloodPoints.GetValue() <= 130
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 110 && VampireBloodPoints.GetValue() <= 120
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 100 && VampireBloodPoints.GetValue() <= 110
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 90 && VampireBloodPoints.GetValue() <= 100
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()		
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 80 && VampireBloodPoints.GetValue() <= 90
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 70 && VampireBloodPoints.GetValue() <= 80
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 60 && VampireBloodPoints.GetValue() <= 70
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 50 && VampireBloodPoints.GetValue() <= 60
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 40 && VampireBloodPoints.GetValue() <= 50
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()			
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 30 && VampireBloodPoints.GetValue() <= 40
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()
		ExposureMeter.StartFlash()		
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 20 && VampireBloodPoints.GetValue() <= 30
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()	
		ExposureMeter.StartFlash()			
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() > 10 && VampireBloodPoints.GetValue() <= 20
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()
		ExposureMeter.StartFlash()			
	elseif BloodPointsChanged && VampireBloodPoints.GetValue() <= 10
		ExposureMeter.FadeTo(BetterVampiresBloodMeter_Opacity.GetValue(), 1.0)
		iDisplayIterationsRemaining = BetterVampiresBloodMeterDisplayTime.GetValueInt()
		ExposureMeter.StartFlash()			
		;iDisplayIterationsRemaining = -1			
	EndIf

endFunction
