Scriptname _00E_TeleportationMEScript extends activemagiceffect  
{Attach this script to all teleportation scripts, and change the teleportation target by changing the "TeleportTargetMarker" property.}

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

Event OnEffectStart (Actor akTarket, Actor akCaster)

	bool bHasCompanion = false
	int i = NoTeleportCompanions.GetSize()
	while i > 0 && ! bHasCompanion
		i -= 1
		bHasCompanion = (NoTeleportCompanions.GetAt(i) as Actor).IsPlayerTeammate()
	endwhile

	If bHasCompanion || (_00E_TeleportGlobal.GetValueInt() == 1) || (PlayerREF.GetWorldSpace() == Akropolis)
		_00E_Teleport_sTeleportNotPossible.Show()
		MAGVampireSunlight.Play(PlayerREF)
		MagicEffect CastedMGEF = Self.GetBaseObject()

		int nItems = _00E_TeleportScrollMGEF_List.GetSize()
		Int iIndex = 0
		While iIndex < nItems && !bScrollAdded
			MagicEffect ScrollMGEF = _00E_TeleportScrollMGEF_List.GetAt(iIndex) as MagicEffect
			If ScrollMGEF == CastedMGEF
				Scroll ScrollToAdd = _00E_TeleportScroll_List.GetAt(iIndex) as Scroll
				PlayerREF.AddItem(ScrollToAdd, 1)
				bScrollAdded = true
			EndIf
			iIndex += 1
		EndWhile
		
		bScrollAdded = false
		
	Else
		If __Config_TeleportTargetMarker.GetParentCell() == ability_container
			_00E_Teleport_sNoTargetMarked.Show()
		Else
			_00E_PlayerFunctions.GetTeleportControl().TeleportPlayer(__Config_TeleportTargetMarker)
		EndIf
	EndIf

EndEvent

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

bool bScrollAdded

WorldSpace Property Akropolis Auto

Cell Property ability_container Auto

Formlist Property _00E_TeleportScroll_List Auto
Formlist Property _00E_TeleportScrollMGEF_List Auto
Formlist Property NoTeleportCompanions Auto

Actor Property PlayerREF Auto

GlobalVariable Property _00E_TeleportGlobal Auto

Sound Property _00E_MagicProtectionSpellM Auto
Sound Property MAGVampireSunlight Auto

ObjectReference Property __Config_TeleportTargetMarker Auto
{Set this to be the XMarkerHeading where the player arrives}

Message Property _00E_Teleport_sNoTargetMarked Auto
Message Property _00E_Teleport_sTeleportNotPossible Auto