Scriptname _00E_MQ12c_HarbirSiegeSC extends ObjectReference Conditional  

;=====================================================================================
;                                       EVENTS                                       
;=====================================================================================

Event OnTriggerEnter(ObjectReference akActionRef)

    If akActionRef == PlayerREF
        bInTrigger = True
        RegisterForSingleUpdate(1)
        SpotPlayer()
    EndIf

EndEvent

Event OnTriggerLeave(ObjectReference akActionRef)
	
	If akActionRef == PlayerREF
		bInTrigger = False
		if MQ12c_OpenFire.GetValueInt() == 1
			StopFireOnPlayer()
		EndIf
	EndIf

EndEvent

Event OnUpdate()

    if bInTrigger
        OpenFireOnPlayer()
    EndIf

EndEvent

State KillPlayerState
	
	Event OnBeginState()
		RegisterForSingleUpdate(10)
	EndEvent
	
	Event OnUpdate()
		_00E_MQ12c_BallistaSpell.cast(MQ12c_HarborMage_01, PlayerREF)
		_00E_MQ12c_BallistaSpell.cast(MQ12c_HarborMage_02, PlayerREF)
		_00E_MQ12c_BallistaSpell.cast(MQ12c_HarborMage_03, PlayerREF)
		RegisterForSingleUpdate(10)
	EndEvent
	
EndState

;=====================================================================================
;                                       FUNCTIONS                                    
;=====================================================================================

Function SpotPlayer()

    _00E_MQ12c_CaughtByNehrimese.Show()
    _00E_TavernGames_CaughtCheatingImod.Apply()
    MAGVampireSunlight.Play(PlayerREF)
    MQ12c_NehrimeseScoutREF.Say(MQ12c_D01_NehrimeseScoutBranchTopic)

EndFunction

Function OpenFireOnPlayer()

    _00E_Music_Combat_BonehunterNoCond.Add()
    MQ12c_OpenFire.SetValueInt(1)
    
    int iIndex = _00E_MQ12c_FireNPCs.GetSize()
    
    while iIndex > 0
        iIndex -= 1
        Actor akActor = _00E_MQ12c_FireNPCs.GetAt(iIndex) as Actor
        akActor.EvaluatePackage()
    endwhile
    
    iIndex = _00E_MQ12c_SoldierNPCs.GetSize()

    while iIndex > 0
        iIndex -= 1
        Actor akActor = _00E_MQ12c_SoldierNPCs.GetAt(iIndex) as Actor
        akActor.EvaluatePackage()
        akActor.StartCombat(PlayerREF)
        akActor.SetGhost(False)
    endwhile
    
    Actor akSayActor = _00E_MQ12c_FireNPCs.GetAt(Utility.RandomInt(0, _00E_MQ12c_FireNPCs.GetSize() - 1)) as Actor
    akSayActor.Say(BanditDialogue_NormalToCombat)
	
	GoToState("KillPlayerState")
    
EndFunction

Function StopFireOnPlayer()

    _00E_Music_Combat_BonehunterNoCond.Remove()
    MQ12c_OpenFire.SetValueInt(0)
    
    int iIndex = _00E_MQ12c_FireNPCs.GetSize()
    
    while iIndex > 0
        iIndex -= 1
        Actor akActor = _00E_MQ12c_FireNPCs.GetAt(iIndex) as Actor
        akActor.EvaluatePackage()
        akActor.StopCombat()
        akActor.StopCombatAlarm()
    endwhile

    iIndex = _00E_MQ12c_SoldierNPCs.GetSize()

    while iIndex > 0
        iIndex -= 1
        Actor akActor = _00E_MQ12c_SoldierNPCs.GetAt(iIndex) as Actor
        akActor.EvaluatePackage()
        akActor.StopCombat()
        akActor.StopCombatAlarm()
        akActor.SetGhost(True)
    endwhile
    
	GoToState("")
	UnregisterForUpdate()
	
EndFunction


;=====================================================================================
;                                        PROPERTIES                                      
;=====================================================================================

bool bInTrigger

bool Property bOpenFire Auto Conditional Hidden

Actor Property MQ12c_HarborMage_01 Auto
Actor Property MQ12c_HarborMage_02 Auto
Actor Property MQ12c_HarborMage_03 Auto

Formlist Property _00E_MQ12c_FireNPCs Auto
Formlist Property _00E_MQ12c_SoldierNPCs Auto

Actor Property PlayerREF Auto

ObjectReference Property MQ12c_NehrimeseScoutREF Auto

ImageSpaceModifier Property _00E_TavernGames_CaughtCheatingImod Auto
GlobalVariable Property MQ12c_OpenFire Auto

Message Property _00E_MQ12c_CaughtByNehrimese Auto

MusicType Property _00E_Music_Combat_BonehunterNoCond Auto

Sound Property MAGVampireSunlight Auto

Spell Property _00E_MQ12c_BallistaSpell Auto

Topic Property MQ12c_D01_NehrimeseScoutBranchTopic Auto
Topic Property BanditDialogue_NormalToCombat Auto