--幻蝋館の使者
--Coded by Lee
local s,id,o=GetID()
function s.initial_effect(c)
	--Special Summon (from hand : itself)
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_ATTACK_ANNOUNCE)
	e1:SetRange(LOCATION_HAND)
	e1:SetCountLimit(1,id)
	e1:SetCondition(s.spcon)
	e1:SetTarget(s.sptg)
	e1:SetOperation(s.spop)
	c:RegisterEffect(e1)
	--indes
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
	e2:SetRange(LOCATION_MZONE)
	e2:SetTargetRange(LOCATION_MZONE,LOCATION_MZONE)
	e2:SetTarget(s.indtg)
	e2:SetValue(1)
	c:RegisterEffect(e2)
	--disable
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,1))
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCountLimit(1)
	e3:SetCondition(s.discon)
	e3:SetTarget(s.distg)
	e3:SetOperation(s.disop)
	c:RegisterEffect(e3)
end
function s.spcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetAttacker():IsControler(1-tp)
end
function s.sptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,e:GetHandler(),1,0,0)
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) then 
	Duel.SpecialSummon(c,0,tp,tp,false,false,POS_FACEUP) 
	end
end
function s.indtg(e,c)
	local tc=e:GetHandler()
	return c==tc or c==tc:GetBattleTarget()
end
function s.discon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetTurnPlayer()==1-tp
end
function s.filter(c)
	return c:IsPosition(POS_ATTACK)
end
function s.distg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.filter,tp,0,LOCATION_MZONE,1,nil) end
end
function s.disop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g=Duel.GetMatchingGroup(s.filter,tp,0,LOCATION_MZONE,nil)
	if #g>0 then 
		local tc=g:GetFirst()
			while tc do
			  local e1=Effect.CreateEffect(c)
			  e1:SetType(EFFECT_TYPE_SINGLE)
			  e1:SetCode(EFFECT_CANNOT_CHANGE_POSITION)
			  e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
			  e1:SetRange(LOCATION_MZONE)
			  e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_BATTLE_START+RESET_OPPO_TURN)
			  e1:SetTargetRange(0,LOCATION_MZONE)
			  tc:RegisterEffect(e1)
			  local e2=Effect.CreateEffect(c)
			  e2:SetType(EFFECT_TYPE_SINGLE)
			  e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
			  e2:SetCode(EFFECT_CANNOT_BE_SYNCHRO_MATERIAL)
			  e2:SetRange(LOCATION_MZONE)
			  e2:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_BATTLE_START+RESET_OPPO_TURN)
			  e2:SetValue(1)
			  tc:RegisterEffect(e2)
			  local e3=e2:Clone()
			  e3:SetCode(EFFECT_CANNOT_BE_FUSION_MATERIAL)
			  e3:SetValue(s.fuslimit)
			  tc:RegisterEffect(e3)
			  local e4=e2:Clone()
			  e4:SetCode(EFFECT_CANNOT_BE_XYZ_MATERIAL)
			  tc:RegisterEffect(e4)
			  local e5=e2:Clone()
			  e5:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
			  tc:RegisterEffect(e5)
			  tc=g:GetNext()
			end
		local sg=g:Filter(aux.NegateEffectMonsterFilter,nil)
		local sc=sg:GetFirst()
			while sc do
			  Duel.NegateRelatedChain(sc,RESET_TURN_SET)
			  local e6=Effect.CreateEffect(c)
			  e6:SetType(EFFECT_TYPE_SINGLE)
			  e6:SetCode(EFFECT_DISABLE)
			  e6:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
			  e6:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_BATTLE_START+RESET_OPPO_TURN)
			  sc:RegisterEffect(e6)
			  local e7=Effect.CreateEffect(c)
			  e7:SetType(EFFECT_TYPE_SINGLE)
			  e7:SetCode(EFFECT_DISABLE_EFFECT)
			  e7:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
			  e7:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_BATTLE_START+RESET_OPPO_TURN)
			  e7:SetValue(RESET_TURN_SET)
			  sc:RegisterEffect(e7)
			  sc=sg:GetNext()
			 end
	end
end
function s.fuslimit(e,c,sumtype)
	return sumtype==SUMMON_TYPE_FUSION
end