-- 巫独少女 爱丽丝
local s,id,o=GetID()
function s.initial_effect(c)
	-- must center
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_SINGLE)
	e0:SetCode(EFFECT_MUST_USE_MZONE)
	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e0:SetValue(0x4)
	c:RegisterEffect(e0)
	-- disable area
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCode(EFFECT_DISABLE_FIELD)
	e1:SetCondition(s.discon)
	e1:SetValue(s.disval)
	c:RegisterEffect(e1)
	-- enter the field
	local e1p=Effect.CreateEffect(c)
	e1p:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e1p:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e1p:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1p:SetLabelObject(e1)
	e1p:SetOperation(s.flop)
	c:RegisterEffect(e1p)
	--adjust
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e2:SetCode(EVENT_ADJUST)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCondition(s.discon)
	e2:SetOperation(s.adjustop)
	c:RegisterEffect(e2)
	--- special summon
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetTarget(s.sstg1)
	e3:SetCost(s.sscost1)
	e3:SetOperation(s.ssop1)
	c:RegisterEffect(e3)
end

function s.discon(e,tp,eg,ep,ev,re,r,rp,chk)
	return e:GetHandler():GetFlagEffect(id)>0
end
function s.disfilter(c)
	return c:GetSequence()~=2
end
function s.disval(e)
	return 0xfb00fb
end
function s.adjustop(e,tp)
	local g=Duel.GetFieldGroup(tp,LOCATION_MZONE,LOCATION_MZONE)
	local tg=g:Filter(s.disfilter,nil)
	Duel.SendtoGrave(tg,REASON_RULE)
end

function s.flop(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	local rc=re:GetHandler()
	if c:IsSummonType(SUMMON_TYPE_RITUAL) and rc:GetCode()==172017010 then
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,1)
	else
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN,0,1)
	end
end

function s.ssfilter1(c,e,tp)
	return c:IsLocation(LOCATION_GRAVE) and c:IsType(TYPE_MONSTER) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.sstg1(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		return eg:IsExists(s.ssfilter1,1,nil,e,tp)
	end
end
function s.sscost1(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SendtoGrave(e:GetHandler(),REASON_COST)
end
function s.ssop1(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=nil
	if #eg>1 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		tc=eg:Filter(s.ssfilter1,nil,e,tp):Select(tp,1,1,nil):GetFirst()
	else
		tc=eg:GetFirst()
	end
	if Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP,0x4) then
		tc:CopyEffect(id,RESET_EVENT+RESETS_STANDARD)
		tc:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN,0,1)
		local e1=Effect.CreateEffect(tc)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_CHANGE_CODE)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetReset(RESET_EVENT+RESETS_STANDARD)
		e1:SetValue(id)
		tc:RegisterEffect(e1)
		local e2=Effect.CreateEffect(tc)
		e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
		e2:SetCode(EVENT_LEAVE_FIELD)
		e2:SetOperation(s.desop)
		tc:RegisterEffect(e2)
	end
end

function s.desop(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetHandler()
	aux.EnableChangeCode(tc,id+1,~0)
end
