-- 巫独少女 莉莉丝
local s,id,o=GetID()
function s.initial_effect(c)
	-- cannot special summon
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,1)
	e1:SetCondition(s.tgcon)
	c:RegisterEffect(e1)
	local e1p=e1:Clone()
	e1p:SetCode(EFFECT_CANNOT_SUMMON)
	c:RegisterEffect(e1p)
	-- enter the field
	local e1q=Effect.CreateEffect(c)
	e1q:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e1q:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e1q:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1q:SetLabelObject(e1)
	e1q:SetOperation(s.flop)
	c:RegisterEffect(e1q)
	-- special summon
	local e2=Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetCondition(s.sscon)
	e2:SetOperation(s.ssop)
	c:RegisterEffect(e2)
end

function s.tgcon(e,tp,eg,ep,ev,re,r,rp,chk)
	return e:GetHandler():GetFlagEffect(id)>0
end
function s.tglimit(e)
	local c=e:GetHandler()
	return true --not c:IsSetCard(0x162)	
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()==69035382 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.filter(c,tp,e)
	return c:IsSetCard(0xf72) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) and c:GetCode()~=id
end
function s.sscon(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	return rp==1-tp and c:IsPreviousControler(tp)
		and Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_GRAVE,0,1,nil,tp,e)
		and e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
end

function s.ssop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectTarget(tp,s.filter,tp,LOCATION_GRAVE,0,1,1,nil,tp,e)
	Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)
end
