;BEGIN FRAGMENT CODE - Do not edit anything between this and the end comment
;NEXT FRAGMENT INDEX 6
Scriptname dcur_cursedCollarQuestScript Extends Quest Hidden

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_teleport_abandonedprison
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_teleport_abandonedprison Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_sheogorathssack
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_sheogorathssack Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_trapdoorlocation_5
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_trapdoorlocation_5 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_jaildoor
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_jaildoor Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_deadmage
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_deadmage Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h6
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h6 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_jailkeylocation_3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_jailkeylocation_3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_trapdoorlocation_2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_trapdoorlocation_2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h5
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h5 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_9
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_9 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_10
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_10 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_magechest_roadsideruins
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_magechest_roadsideruins Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_ladder
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_ladder Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_trapdoorlocation_1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_trapdoorlocation_1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_teleport_anisescabin
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_teleport_anisescabin Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_8
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_8 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_teleport_falkreath
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_teleport_falkreath Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_19
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_19 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_5
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_5 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquesttrapdoor
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquesttrapdoor Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d4
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d4 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d5
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d5 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_13
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_13 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_sheogorath
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_sheogorath Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_9
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_9 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_jailkeylocation_2
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_jailkeylocation_2 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_6
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_6 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_teleport_solitude
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_teleport_solitude Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_trapdoor
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_trapdoor Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_7
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_7 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_prisondoor
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_prisondoor Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_teleport_winterhold
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_teleport_winterhold Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_20
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_20 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_8
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_8 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_18
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_18 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_6
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_6 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarmarker_1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarmarker_1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_maingate
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_maingate Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_14
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_14 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_trapdoorlocation_3
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_trapdoorlocation_3 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_16
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_16 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_4
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_4 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_h4
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_h4 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_11
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_11 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_10
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_10 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_7
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_7 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_15
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_15 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_12
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_12 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_cursedcollaractivator
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_cursedcollaractivator Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_17
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_17 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_crystal
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_crystal Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_jailkeylocation_1
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_jailkeylocation_1 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_collarquestchest_d6
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_collarquestchest_d6 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_5
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_5 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_ddd_keylocation_4
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_ddd_keylocation_4 Auto
;END ALIAS PROPERTY

;BEGIN ALIAS PROPERTY dcur_alias_ddd_trapdoorlocation_4
;ALIAS PROPERTY TYPE ReferenceAlias
ReferenceAlias Property Alias_dcur_alias_ddd_trapdoorlocation_4 Auto
;END ALIAS PROPERTY

;BEGIN FRAGMENT Fragment_2
Function Fragment_2()
;BEGIN CODE
SetObjectiveDisplayed(10)
;END CODE
EndFunction
;END FRAGMENT

;END FRAGMENT CODE - Do not edit anything between this and the begin comment

dcur_library Property dclibs  Auto  

;SexLabFramework Property SexLab Auto
Quest Property SexLabQuestFramework  Auto  
Quest Property sla_Framework  Auto  

SPELL Property dcur_electricblastspell  Auto
MagicEffect Property dcur_collareffect  Auto  

Armor Property dcur_cursedcollar  Auto  
Armor Property dcur_cursedCollarRendered  Auto  
Armor Property dcur_cursedbelt  Auto  
Armor Property dcur_cursedBeltRendered  Auto  
Armor Property dcur_cursedbra  Auto  
Armor Property dcur_cursedBraRendered  Auto  
Armor Property dcur_cursedArmCuffs  Auto  
Armor Property dcur_cursedArmCuffsRendered  Auto  
Armor Property dcur_cursedLegCuffs  Auto  
Armor Property dcur_cursedLegCuffsRendered  Auto  
Armor Property dcur_cursedBlindfold  Auto  
Armor Property dcur_cursedBlindfoldRendered  Auto  
Armor Property dcur_cursedArmbinder  Auto  
Armor Property dcur_cursedArmbinderRendered  Auto  
Armor Property dcur_cursedBoots  Auto  
Armor Property dcur_cursedBootsRendered  Auto  
Armor Property dcur_cursedHarness  Auto  
Armor Property dcur_cursedHarnessRendered  Auto 
Armor Property dcur_cursedBallGag  Auto  
Armor Property dcur_cursedBallGagRendered  Auto  
Armor Property dcur_cursedPanelGag  Auto  
Armor Property dcur_cursedPanelGagRendered  Auto   

MiscObject Property Bucket01  Auto  
Key Property dcur_jailkey  Auto  
Key Property dcur_prisonkey  Auto  
Key Property dcur_magicKey  Auto  
Book Property dcur_collarletter  Auto  
Book Property dcur_collarwizarddiary  Auto  
Book Property dcur_collarletterhint1  Auto  
Book Property dcur_collarletterhint2  Auto  
Book Property dcur_collarletterhint3  Auto  
Book Property dcur_collarletterhint4  Auto  
Book Property dcur_collarletterhint5  Auto  
Book Property dcur_collarletterhint6  Auto  
Book Property dcur_collarletterhint7  Auto  
Book Property dcur_collarletterhint8  Auto  
Key Property dcur_cursedmasterkey  Auto  

Container Property dcur_collarquestchest  Auto  

Keyword Property ArmorBoots  Auto  
Keyword Property ArmorClothing  Auto  
Keyword Property ArmorCuirass  Auto  
Keyword Property ArmorDarkBrotherhood  Auto  
Keyword Property ArmorGauntlets  Auto  
Keyword Property ArmorHeavy  Auto  
Keyword Property ArmorHelmet  Auto  
Keyword Property ArmorJewelry  Auto  
Keyword Property ArmorLight  Auto  

ImageSpaceModifier Property black  Auto  
MiscObject Property gold01  Auto  

Key Property dcur_trapdoorkey  Auto  

dcur_mcmconfig Property dcumenu Auto
zadlibs Property libs Auto
zadxlibs Property xlibs Auto

; this variable will be polled by the CollarEffect to determine if it should disable fast travel
bool property nofasttravel = True Auto

float property collarqueststartedtime = 0.0 Auto
; when did the last collar event trigger?
float property lastminorevent = 0.0 Auto

; Variables for the main quest
bool property cursedcollarquestrunning = false Auto

; some pseudo constants
int desttype_surface = 1
int desttype_hideout = 2
int desttype_dungeon = 3

bool function removecursedgag(bool nomutex = false, bool destroydevice = true)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousGag) && libs.PlayerRef.isequipped(dcur_cursedBallGag)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedBallGag, dcur_cursedBallGagRendered, Libs.zad_DeviousGag, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousGag) && libs.PlayerRef.isequipped(dcur_cursedPanelGag)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedPanelGag, dcur_cursedPanelGagRendered, Libs.zad_DeviousGag, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedbelt(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBelt) && libs.PlayerRef.isequipped(dcur_cursedBelt)					
		libs.removeDevice(Libs.PlayerRef, dcur_cursedBelt, dcur_cursedBeltRendered, Libs.zad_DeviousBelt, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		;if libs.GetWornDevice(libs.playerref, Libs.zad_DeviousPlugVaginal) == dcumenu.dcur_customVagPlug
		;	libs.removeDevice(Libs.PlayerRef, dcumenu.dcur_customVagPlug, dcumenu.dcur_customVagPlugRendered, Libs.zad_DeviousPlugVaginal, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		;else
			libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousPlugVaginal, false, false, false)
		;endif
		;if libs.GetWornDevice(libs.playerref, Libs.zad_DeviousPlugVaginal) == dcumenu.dcur_customAnPlug
		;	libs.removeDevice(Libs.PlayerRef, dcumenu.dcur_customAnPlug, dcumenu.dcur_customAnPlugRendered, Libs.zad_DeviousPlugAnal, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)		
		;else
		;	libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousPlugAnal, false, false, false)
		;endif
		return true
	endif
	return false
endfunction

bool function removecursedbra(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBra) && libs.PlayerRef.isequipped(dcur_cursedBra)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedBra, dcur_cursedBraRendered, Libs.zad_DeviousBra, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedlegcuffs(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousLegCuffs) && libs.PlayerRef.isequipped(dcur_cursedLegCuffs)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedLegCuffs, dcur_cursedLegCuffsRendered, Libs.zad_DeviousLegCuffs, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedarmcuffs(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmCuffs) && libs.PlayerRef.isequipped(dcur_cursedArmCuffs)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedArmCuffs, dcur_cursedArmCuffsRendered, Libs.zad_DeviousArmCuffs, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedblindfold(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBlindfold) && libs.PlayerRef.isequipped(dcur_cursedBlindfold)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedBlindfold, dcur_cursedBlindfoldRendered, Libs.zad_DeviousBlindfold, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedboots(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBoots) && libs.PlayerRef.isequipped(dcur_cursedBoots)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedBoots, dcur_cursedBootsRendered, Libs.zad_DeviousBoots, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedarmbinder(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmbinder) && libs.PlayerRef.isequipped(dcur_cursedArmbinder)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedArmbinder, dcur_cursedArmbinderRendered, Libs.zad_DeviousArmbinder, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedharness(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousHarness) && libs.PlayerRef.isequipped(dcur_cursedHarness)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedHarness, dcur_cursedHarnessRendered, Libs.zad_DeviousHarness, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function removecursedcollar(bool nomutex = false, bool destroydevice = true)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_Deviouscollar) && libs.PlayerRef.isequipped(dcur_cursedcollar)		
		libs.removeDevice(Libs.PlayerRef, dcur_cursedcollar, dcur_cursedcollarRendered, Libs.zad_Deviouscollar, destroyDevice = destroyDevice, skipevents = false, skipmutex = nomutex)
		return true
	endif
	return false
endfunction

bool function equipcursedgag(bool nomutex = false)
	; remove present gag, if it's a generic one
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousGag)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousgag, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedBallGag, dcur_cursedBallGagRendered, Libs.zad_DeviousGag, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedbelt(bool nomutex = false)
	; remove present belt, if it's a generic one
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBelt)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousBelt, false, false)
			return false
		endif
	endif	
	libs.ManipulateGenericDeviceByKeyword(libs.playerref, Libs.zad_DeviousPlugVaginal, false, false)
	libs.ManipulateGenericDeviceByKeyword(libs.playerref, Libs.zad_DeviousPlugAnal, false, false)
	libs.equipDevice(libs.PlayerRef, libs.plugSoulgemVag, libs.plugSoulgemVagRendered, libs.Zad_Deviousplugvaginal, skipevents = false, skipmutex = nomutex)	  
	libs.equipDevice(libs.PlayerRef, libs.plugSoulgemAn, libs.plugSoulgemAnRendered, libs.Zad_Deviouspluganal, skipevents = false, skipmutex = nomutex)
	utility.wait(0.1)
	libs.equipDevice(Libs.PlayerRef, dcur_cursedBelt, dcur_cursedBeltRendered, Libs.zad_DeviousBelt, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedbra(bool nomutex = false)
	; remove present bra, if it's a generic one
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBra)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousBra, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedBra, dcur_cursedBraRendered, Libs.zad_DeviousBra, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedlegcuffs(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousLegCuffs)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousLegCuffs, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedLegCuffs, dcur_cursedLegCuffsRendered, Libs.zad_DeviousLegCuffs, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedarmcuffs(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmCuffs)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousArmCuffs, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedArmCuffs, dcur_cursedArmCuffsRendered, Libs.zad_DeviousArmCuffs, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedblindfold(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBlindfold)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousBlindfold, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedBlindfold, dcur_cursedBlindfoldRendered, Libs.zad_DeviousBlindfold, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedarmbinder(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmbinder)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousArmbinder, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedArmbinder, dcur_cursedArmbinderRendered, Libs.zad_DeviousArmbinder, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedboots(bool nomutex = false)	
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBoots)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousBoots, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedBoots, dcur_cursedBootsRendered, Libs.zad_DeviousBoots, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedHarness(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousHarness)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousHarness, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedHarness, dcur_cursedHarnessRendered, Libs.zad_DeviousHarness, skipevents = false, skipmutex = nomutex)
	return true
endfunction

bool function equipcursedcollar(bool nomutex = false)
	if Libs.PlayerRef.WornHasKeyword(Libs.zad_Deviouscollar)
		if !libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviouscollar, false, false)
			return false
		endif
	endif	
	libs.equipDevice(Libs.PlayerRef, dcur_cursedCollar, dcur_cursedCollarRendered, Libs.zad_DeviousCollar, skipevents = false, skipmutex = nomutex)
	return true
endfunction

function unequipcursedset(bool nomutex = true)	
	removecursedarmbinder(nomutex)
	removecursedboots(nomutex)
	removecursedHarness(nomutex)
	;removecursedblindfold(nomutex = false)
	removecursedarmcuffs(nomutex)
	removecursedlegcuffs(nomutex)
	removecursedbra(nomutex)
	removecursedbelt(nomutex)
	removecursedgag(nomutex)
endFunction

Function Punish(int escapeattempts)
	float currentHealth = libs.playerref.GetActorValuePercentage("Health") * 100		
	if escapeattempts == 1
		Utility.Wait(0.1)
		Game.ForceThirdPerson()
		libs.Notify("あなたの頭の中で首輪の声がします: '俺を不正にいじろうなんて思うな！", messageBox=true)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStart")		
		libs.PlayerRef.DamageActorValue("Health",  currentHealth* 0.25)
		libs.PlayerRef.DamageActorValue("Stamina", currentHealth* 0.25)
		libs.PlayerRef.DamageActorValue("Magicka", currentHealth* 0.25)
		Utility.Wait(2.0)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStop")
	endif
	if escapeattempts == 2
		Utility.Wait(0.1)
		Game.ForceThirdPerson()
		libs.Notify("あなたの頭の中で声がします: '前の調教はお前には不十分だったようだ。これでも喰らえ!'", messageBox=true)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStart")		
		Utility.Wait(1)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		libs.PlayerRef.DamageActorValue("Health",  currentHealth* 0.5)
		libs.PlayerRef.DamageActorValue("Stamina", currentHealth* 0.5)
		libs.PlayerRef.DamageActorValue("Magicka", currentHealth* 0.5)
		Utility.Wait(2.0)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStop")
	endif
	if escapeattempts == 3
		Utility.Wait(0.1)
		Game.ForceThirdPerson()
		libs.Notify("あなたの頭の中で声がします: '俺に逆らえないよう、お前には辛い目を遭わせないといけないようだ。手を休むと縛られるぞ！'", messageBox=true)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStart")		
		Utility.Wait(1)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		libs.PlayerRef.DamageActorValue("Health",  currentHealth* 0.75)
		libs.PlayerRef.DamageActorValue("Stamina", currentHealth* 0.75)
		libs.PlayerRef.DamageActorValue("Magicka", currentHealth* 0.75)
		Utility.Wait(2.0)		
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStop")
		if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmbinder)
			libs.equipDevice(libs.PlayerRef, libs.Armbinder, libs.ArmbinderRendered, libs.Zad_DeviousArmbinder, skipevents = false, skipmutex = false)			
		endif
	endif
	if escapeattempts > 3
		Utility.Wait(0.1)
		Game.ForceThirdPerson()
		libs.Notify("あなたの頭の中で声がします: 'そうだな、お前は本当に反抗的なようだ。俺はお前の注意を何か他に縛り付けてやる。文字通りな。'", messageBox=true)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStart")		
		Utility.Wait(1)
		dcur_electricblastspell.remoteCast(libs.playerRef, libs.playerRef, libs.playerRef)
		libs.PlayerRef.DamageActorValue("Health",  currentHealth* 0.75)
		libs.PlayerRef.DamageActorValue("Stamina", currentHealth* 0.75)
		libs.PlayerRef.DamageActorValue("Magicka", currentHealth* 0.75)
		Utility.Wait(2.0)		
		Debug.SendAnimationEvent(libs.playerRef, "BleedOutStop")
		equipfullset()
	endif
Endfunction

int Function dcur_removekeys()
int reskeys = Libs.PlayerRef.GetItemCount(libs.restraintsKey)
int chaskeys = Libs.PlayerRef.GetItemCount(libs.ChastityKey)
	libs.PlayerRef.RemoveItem(libs.restraintsKey, reskeys) 
	libs.PlayerRef.RemoveItem(libs.ChastityKey, chaskeys)  
	return reskeys + chaskeys  
EndFunction

Function equipfullset(bool noblindfold = false, bool noarmbinder = false, bool nogag = false, bool noboots = false, bool restrictiveonly = false)
	(SexLabQuestFramework As SexLabFramework).StripActor(libs.playerref, doanimate = true, leadIn = false) 	
	If !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousPlugVaginal) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.plugSoulgemVag, libs.plugSoulgemVagRendered, libs.Zad_Deviousplugvaginal, skipevents = false, skipmutex = true)
	endif
	If !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousPlugAnal) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.plugSoulgemAn, libs.plugSoulgemAnRendered, libs.Zad_Deviouspluganal, skipevents = false, skipmutex = true)		
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBelt) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.beltPadded, libs.beltPaddedRendered, libs.Zad_DeviousBelt, skipevents = false, skipmutex = true)
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBra) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.braPadded, libs.braPaddedRendered, libs.Zad_DeviousBra, skipevents = false, skipmutex = true)	
	endif
	
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousLegCuffs) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.CuffsPaddedLegs, libs.CuffsPaddedLegsRendered, libs.Zad_DeviousLegCuffs, skipevents = false, skipmutex = true)
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmCuffs) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, libs.CuffsPaddedArms, libs.CuffsPaddedArmsRendered, libs.Zad_DeviousArmCuffs, skipevents = false, skipmutex = true)			
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousCollar) && !restrictiveonly
		libs.equipDevice(libs.PlayerRef, xlibs.eboniteHarnessCollar, xlibs.eboniteHarnessCollarRendered, libs.Zad_DeviousCollar, skipevents = false, skipmutex = true)			
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBlindfold) && !noblindfold
		libs.equipDevice(libs.PlayerRef, libs.blindfold, libs.blindfoldRendered, libs.Zad_DeviousBlindfold, skipevents = false, skipmutex = true)			
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousArmbinder) && !noarmbinder
		libs.equipDevice(libs.PlayerRef, libs.Armbinder, libs.ArmbinderRendered, libs.Zad_DeviousArmbinder, skipevents = false, skipmutex = true)			
	endif
	if !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousGag) && !nogag
		libs.equipDevice(Libs.PlayerRef, libs.gagBall, libs.gagBallRendered, Libs.zad_DeviousGag, skipevents = false, skipmutex = true)
	endif	
	If !Libs.PlayerRef.WornHasKeyword(Libs.zad_DeviousBoots) && !noboots
		libs.equipDevice(libs.PlayerRef, xlibs.Bootslocking, xlibs.BootslockingRendered, libs.Zad_DeviousBoots, skipevents = false, skipmutex = true)		
	endif
	dcur_removekeys()
EndFunction

function terminatequest(bool suppressendgift = false)	
	
	cursedcollarquestrunning = false
	libs.PlayerRef.RemoveItem(dcur_collarletterhint1, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint2, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint3, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint4, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint5, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint6, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint7, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletterhint8, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarletter, 1, true)
	libs.PlayerRef.RemoveItem(dcur_collarwizarddiary, 1, true)
	libs.PlayerRef.RemoveItem(dcur_jailkey, 1, true)
	libs.PlayerRef.RemoveItem(dcur_prisonkey, 1, true)
	libs.PlayerRef.RemoveItem(dcur_trapdoorkey, 1, true)
	
	self.SetStage(1000)
	self.SetObjectiveDisplayed(1000)
	self.CompleteQuest()
	self.reset()	
	if dcumenu.debuglogenabled
		debug.trace("DCUR Ending Cursed Collar quest")
	endif
	lastminorevent = 0	
	Utility.Wait(0.5)
	unequipcursedset(nomutex = true)
	
	Utility.Wait(0.5)
	removecursedcollar(nomutex = true)
		
	libs.ManipulateGenericDeviceByKeyword(libs.playerref, libs.zad_deviousBlindfold, false, false, false)
	
	if dcumenu.cursedcollarquestendgift && !suppressendgift
		Utility.Wait(1)
		libs.Notify("あなたの頭の中でシェオゴラスの声がします: '俺は感銘している。お前、やったな！誰がそう考えた？俺は非常に失望したぞ！だが確かに、俺は体を動かすのを得意にして、お前の身を自由にするという約束を守るぞ。しかし、お前に餞別をやらんとは言ってない、そうだよな？俺はお前が楽しんでくれることを期待するぞ。それはお前にピッタリのはずだ。全てありうる観点からしてな！'", messagebox = true)	
		Utility.Wait(1)
		libs.equipDevice(libs.playerref, dcumenu.dcur_customVagplug, dcumenu.dcur_customVagplugRendered, libs.Zad_DeviousPlugVaginal, skipevents = false, skipmutex = true)				
		libs.equipDevice(libs.playerref, dcumenu.dcur_customAnplug, dcumenu.dcur_customAnplugRendered, libs.Zad_DeviousPlugAnal, skipevents = false, skipmutex = true)				
		Utility.Wait(0.2)
		libs.equipDevice(libs.playerref, dcumenu.dcur_hisecbelt, dcumenu.dcur_hisecbeltRendered, libs.Zad_DeviousBelt, skipevents = false, skipmutex = false)				
		libs.equipDevice(libs.playerref, dcumenu.dcur_hisecbra, dcumenu.dcur_hisecbraRendered, libs.Zad_DeviousBra, skipevents = false, skipmutex = false)				
		libs.equipDevice(libs.playerref, dcumenu.dcur_hiseccollar, dcumenu.dcur_hiseccollarRendered, libs.Zad_Deviouscollar, skipevents = false, skipmutex = false)						
	endif
endFunction

armor function getnextcurseditem()
	if !libs.PlayerRef.isequipped(dcur_cursedBelt)
		return dcur_cursedbelt				
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedBra)
		return dcur_cursedbra
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedHarness)
		return dcur_cursedHarness
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedarmcuffs)
		return dcur_cursedarmcuffs
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedlegcuffs)
		return dcur_cursedlegcuffs
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedBoots)
		return dcur_cursedBoots
	endif
	if !libs.PlayerRef.isequipped(dcur_cursedBallGag) && !libs.PlayerRef.isequipped(dcur_cursedPanelGag)
		if dcumenu.cursedcollarusepanelgag
			return dcur_cursedPanelGag
		else
			return dcur_cursedBallGag
		endif
	endif
	;if !libs.PlayerRef.isequipped(dcur_cursedblindfold)
	;	return dcur_cursedblindfold
	;endif
	if !libs.PlayerRef.isequipped(dcur_cursedarmbinder)
		return dcur_cursedarmbinder
	endif
	return none
endFunction

form function getmatchingbook(armor dditem)
	if dditem == dcur_cursedBelt
		return dcur_collarletterhint1
	endif	
	if dditem == dcur_cursedBra
		return dcur_collarletterhint2
	endif	
	if dditem == dcur_cursedArmCuffs
		return dcur_collarletterhint3
	endif	
	if dditem == dcur_cursedLegCuffs
		return dcur_collarletterhint4
	endif	
	if dditem == dcur_cursedBoots
		return dcur_collarletterhint5
	endif	
	if dditem == dcur_cursedBallGag || dditem == dcur_cursedPanelGag
		return dcur_collarletterhint6
	endif	
	if dditem == dcur_cursedArmbinder
		return dcur_collarletterhint7
	endif	
	if dditem == dcur_cursedHarness
		return dcur_collarletterhint8
	endif	
	return none
endfunction

int function collarquest_getactualstage()
	; we need this because Papyrus is silly and GetStage() always returns the highest completed stage, no matter which one is actually active
	int teststage = 50	
	while teststage < 90
		if self.IsObjectiveDisplayed(teststage)
			return teststage
		else
			teststage += 1
		endif
	endwhile
	return 0
endfunction

function closecurrentstage(int currentstage)	
	; disable the currently active chest	
	if currentstage == 61
		Alias_dcur_alias_collarquestchest_1.GetReference().disable(true)
	elseif currentstage == 62
		Alias_dcur_alias_collarquestchest_2.GetReference().disable(true)
	elseif currentstage == 63
		Alias_dcur_alias_collarquestchest_3.GetReference().disable(true)
	elseif currentstage == 64
		Alias_dcur_alias_collarquestchest_4.GetReference().disable(true)
	elseif currentstage == 65
		Alias_dcur_alias_collarquestchest_5.GetReference().disable(true)
	elseif currentstage == 66
		Alias_dcur_alias_collarquestchest_6.GetReference().disable(true)
	elseif currentstage == 67
		Alias_dcur_alias_collarquestchest_7.GetReference().disable(true)
	elseif currentstage == 68
		Alias_dcur_alias_collarquestchest_8.GetReference().disable(true)
	elseif currentstage == 69
		Alias_dcur_alias_collarquestchest_9.GetReference().disable(true)
	elseif currentstage == 70
		Alias_dcur_alias_collarquestchest_10.GetReference().disable(true)
		
	elseif currentstage == 71
		Alias_dcur_alias_collarquestchest_h1.GetReference().disable(true)
	elseif currentstage == 72
		Alias_dcur_alias_collarquestchest_h2.GetReference().disable(true)
	elseif currentstage == 73
		Alias_dcur_alias_collarquestchest_h3.GetReference().disable(true)
	elseif currentstage == 74
		Alias_dcur_alias_collarquestchest_h4.GetReference().disable(true)
	elseif currentstage == 75
		Alias_dcur_alias_collarquestchest_h5.GetReference().disable(true)
	elseif currentstage == 76
		Alias_dcur_alias_collarquestchest_h6.GetReference().disable(true)
	
	elseif currentstage == 81
		Alias_dcur_alias_collarquestchest_d1.GetReference().disable(true)
	elseif currentstage == 82
		Alias_dcur_alias_collarquestchest_d2.GetReference().disable(true)
	elseif currentstage == 83
		Alias_dcur_alias_collarquestchest_d3.GetReference().disable(true)
	elseif currentstage == 84
		Alias_dcur_alias_collarquestchest_d4.GetReference().disable(true)
	elseif currentstage == 85
		Alias_dcur_alias_collarquestchest_d5.GetReference().disable(true)
	elseif currentstage == 86
		Alias_dcur_alias_collarquestchest_d6.GetReference().disable(true)
	endif
	self.SetObjectiveCompleted(currentstage, true)	
	self.SetObjectiveDisplayed(currentstage, false)	
endfunction

function stuffchest(ObjectReference chestref, armor dditem)
	if dcumenu.debuglogenabled
		debug.trace("DCUR Cursed Collar Quest: Setting up a chest")
	endif
	libs.notify("ある場所のイメージがあなたの頭の中に現れます...")
	chestref.enable(true)
	Utility.Wait(1.0)
	; empty whatever is currently in the chest
	Int i = chestref.GetNumItems()
	While (i > 0)
		i -= 1
		Form Item = chestref.GetNthForm(i)
		chestref.RemoveItem(Item, chestref.GetItemCount(Item), True)
	EndWhile
	; add next cursed item
	chestref.additem(dditem)	
	if getmatchingbook(dditem)
		chestref.additem(getmatchingbook(dditem))	
	endif
	if dditem == dcur_cursedBelt
		chestref.additem(dcumenu.dcur_customVagPlug)
		chestref.additem(dcumenu.dcur_customAnPlug)
		chestref.additem(libs.piercingVSoul)
	endif	
endfunction

int function pickdestinationtype()
	int chance = 0
	int grandtotalweight = dcumenu.cursedcollarsurfaceweight + dcumenu.cursedcollarhideoutweight + dcumenu.cursedcollardungeonweight
	bool done = false
	int bailout = 0
		while !done
		chance = Utility.RandomInt(1, grandtotalweight)		
		if chance < dcumenu.cursedcollarsurfaceweight
			done = true
			return desttype_surface			
		else
			chance = chance - dcumenu.cursedcollarsurfaceweight
			if chance < 1 
				chance = 1
			endif
		endIf
		if !done && chance < dcumenu.cursedcollarhideoutweight
			done = true
			return desttype_hideout
		else
			chance = chance - dcumenu.cursedcollarhideoutweight
			if chance < 1 
				chance = 1
			endif
		endIf
		if !done && chance < dcumenu.cursedcollardungeonweight
			done = true
			return desttype_dungeon
		endIf
		if !done
			bailout += 1
			if bailout > 20 
				return desttype_surface
			endif
		endif
	endwhile
endfunction

function picknextstage()
	; randomly select a chest location
	int rnd
	int desttype = pickdestinationtype()
	if desttype == desttype_surface
		rnd = Utility.RandomInt(61, 70)
	elseif desttype == desttype_hideout
		rnd = Utility.RandomInt(71, 76)
	elseif desttype == desttype_dungeon
		rnd = Utility.RandomInt(81, 86)
	endif
	armor dditem = getnextcurseditem()
	if !dditem
		if dcumenu.debuglogenabled
			debug.trace("[DCUR] All Cursed items are equipped. Triggering last stage.")
		endif
		Alias_dcur_alias_collarquesttrapdoor.GetReference().enable(true)
		Alias_dcur_alias_ddd_jaildoor.GetReference().lock(false)
		Alias_dcur_ddd_prisondoor.GetReference().lock(false)
		Alias_dcur_alias_ddd_sheogorathssack.GetReference().disable(true)
		Alias_dcur_ddd_trapdoor.GetReference().lock()
		Alias_dcur_ddd_trapdoor.GetReference().SetLockLevel(255)		
		Alias_dcur_alias_ddd_ladder.GetReference().disable(true)
		rnd = 90				
	endif
	ObjectReference ChestRef 
	if rnd == 61
		chestRef = Alias_dcur_alias_collarquestchest_1.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 62
		chestRef = Alias_dcur_alias_collarquestchest_2.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 63
		chestRef = Alias_dcur_alias_collarquestchest_3.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 64
		chestRef = Alias_dcur_alias_collarquestchest_4.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 65
		chestRef = Alias_dcur_alias_collarquestchest_5.GetReference()
		stuffchest(chestref, dditem)		
	elseif rnd == 66
		chestRef = Alias_dcur_alias_collarquestchest_6.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 67
		chestRef = Alias_dcur_alias_collarquestchest_7.GetReference()
		stuffchest(chestref, dditem)	
	elseif rnd == 68
		chestRef = Alias_dcur_alias_collarquestchest_8.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 69
		chestRef = Alias_dcur_alias_collarquestchest_9.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 70
		chestRef = Alias_dcur_alias_collarquestchest_10.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 71
		chestRef = Alias_dcur_alias_collarquestchest_h1.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 72
		chestRef = Alias_dcur_alias_collarquestchest_h2.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 73
		chestRef = Alias_dcur_alias_collarquestchest_h3.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 74
		chestRef = Alias_dcur_alias_collarquestchest_h4.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 75
		chestRef = Alias_dcur_alias_collarquestchest_h5.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 76
		chestRef = Alias_dcur_alias_collarquestchest_h6.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 81
		chestRef = Alias_dcur_alias_collarquestchest_d1.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 82
		chestRef = Alias_dcur_alias_collarquestchest_d2.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 83
		chestRef = Alias_dcur_alias_collarquestchest_d3.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 84
		chestRef = Alias_dcur_alias_collarquestchest_d4.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 85
		chestRef = Alias_dcur_alias_collarquestchest_d5.GetReference()
		stuffchest(chestref, dditem)
	elseif rnd == 86
		chestRef = Alias_dcur_alias_collarquestchest_d6.GetReference()
		stuffchest(chestref, dditem)
	endif
	self.SetStage(rnd)
	self.SetObjectiveDisplayed(rnd, true)
endfunction

Function SetNextStage()		
	if dcumenu.debuglogenabled
		debug.trace("DCUR Cursed Collar Quest: Stage triggered: " + self.GetStage())
	endif
	if self.GetStage() == 10
		self.SetObjectiveCompleted(self.GetStage())
		self.SetStage(20)
		self.SetObjectiveDisplayed(20)
		Alias_dcur_alias_magechest_roadsideruins.GetReference().enable(true)
		Alias_dcur_alias_magechest_roadsideruins.GetReference().additem(dcur_collarwizarddiary)
		return
	endif	
	if self.GetStage() == 20
		self.SetObjectiveCompleted(self.GetStage())
		picknextstage()		
		return
	endif	
	if self.GetStage() >= 50 && self.GetStage() < 90
		closecurrentstage(collarquest_getactualstage())
		picknextstage()		
		return
	endif	
endfunction  
