--炼金釜-混沌提炼
local s,id,o=GetID()
local o=100
function s.initial_effect(c)
	aux.AddCodeList(c,40412012)
	--Activate
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	--search
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(40412012,0))
	e1:SetCategory(CATEGORY_TOHAND+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_SZONE)
	e1:SetCountLimit(1,40412012)
	e1:SetCondition(s.con)
	e1:SetCost(s.cost)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--remove
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetProperty(EFFECT_FLAG_SET_AVAILABLE+EFFECT_FLAG_IGNORE_RANGE+EFFECT_FLAG_IGNORE_IMMUNE)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCode(EFFECT_TO_GRAVE_REDIRECT)
	e2:SetTargetRange(0xff,0xfe)
	e2:SetTarget(s.rmtg)
	e2:SetValue(LOCATION_REMOVED)
	c:RegisterEffect(e2)
	--same effect send this card to grave and spsummon another card check
	local e5=aux.AddThisCardInGraveAlreadyCheck(c)
	--Spsummon
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(40412012,1))
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_REMOVE)
	e3:SetRange(LOCATION_SZONE)
	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY+EFFECT_FLAG_CARD_TARGET)
	e3:SetCountLimit(1)
	e3:SetLabelObject(e5)
	e3:SetCondition(s.spcon)
	e3:SetTarget(s.tdtg)
	e3:SetOperation(s.tdop)
	c:RegisterEffect(e3)
	--immune
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_SINGLE)
	e4:SetCode(EFFECT_IMMUNE_EFFECT)
	e4:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e4:SetRange(LOCATION_SZONE)
	e4:SetCondition(s.imecon)
	e4:SetValue(s.efilter)
	c:RegisterEffect(e4)
	Duel.AddCustomActivityCounter(id,ACTIVITY_CHAIN,s.chainfilter)
	local e7=Effect.CreateEffect(c)
	e7:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e7:SetCode(EVENT_CHAIN_SOLVED)
	e7:SetRange(0x3c)
	e7:SetOperation(s.op)
	c:RegisterEffect(e7)
	if not s.global_check then
		s.global_check=true
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		ge1:SetCode(EVENT_SPSUMMON_SUCCESS)
		ge1:SetOperation(s.checkop)
		Duel.RegisterEffect(ge1,0)
		local ge2=Effect.CreateEffect(c)
		ge2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		ge2:SetCode(EVENT_CHAINING)
		ge2:SetOperation(s.checkop1)
		Duel.RegisterEffect(ge2,0)
	end
end
function s.chainfilter(re,tp,cid)
	if re:GetHandler():IsCode(40412006,40412007,40412008,40412009,40412010,40412011) 
		and re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) then
		local code=re:GetHandler():GetCode()
		Duel.RegisterFlagEffect(tp,code,RESET_PHASE+PHASE_END,0,1)
		Duel.RegisterFlagEffect(tp,40412012,RESET_PHASE+PHASE_END,0,1)
	end
	return not (re:GetHandler():IsCode(40412006,40412007,40412008,40412009,40412010,40412011) 
		and re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL))
end
function s.checkfilter(c)
	return not c:IsSummonableCard()
end
function s.checkop(e,tp,eg,ep,ev,re,r,rp)
	local g=eg:Filter(s.checkfilter,nil)
	local tc=g:GetFirst()
	while tc do
		Duel.RegisterFlagEffect(tc:GetSummonPlayer(),41412012,RESET_PHASE+PHASE_END,0,1)
		tc=g:GetNext()
	end
end
function s.checkop1(e,tp,eg,ep,ev,re,r,rp)
	if re and re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) 
		and re:GetHandler():IsCode(40412006,40412007,40412008,40412009,40412010,40412011) then
		local code=re:GetHandler():GetCode()
		Duel.RegisterFlagEffect(tp,code,RESET_PHASE+PHASE_END,0,1)
		Duel.RegisterFlagEffect(tp,40412012,RESET_PHASE+PHASE_END,0,1)
	end
end
function s.op(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if Duel.GetFlagEffect(tp,40412000)>0 and Duel.GetCustomActivityCount(40412000,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412000) end
	if Duel.GetFlagEffect(tp,40412006)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412000) end
	if Duel.GetFlagEffect(tp,40412001)>0 and Duel.GetCustomActivityCount(40412001,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412001) end
	if Duel.GetFlagEffect(tp,40412007)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412001) end
	if Duel.GetFlagEffect(tp,40412002)>0 and Duel.GetCustomActivityCount(40412002,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412002) end
	if Duel.GetFlagEffect(tp,40412008)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412002) end
	if Duel.GetFlagEffect(tp,40412003)>0 and Duel.GetCustomActivityCount(40412003,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412003) end
	if Duel.GetFlagEffect(tp,40412009)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412003) end
	if Duel.GetFlagEffect(tp,40412004)>0 and Duel.GetCustomActivityCount(40412004,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412004) end
	if Duel.GetFlagEffect(tp,40412010)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412004) end
	if Duel.GetFlagEffect(tp,40412005)>0 and Duel.GetCustomActivityCount(40412005,tp,ACTIVITY_CHAIN)>0 then s.GFE0(e,40412005) end
	if Duel.GetFlagEffect(tp,40412011)>0 and Duel.GetCustomActivityCount(40412012,tp,ACTIVITY_CHAIN)>0 then s.GFE1(e,40412005) end
end
function s.GFE0(e,ide)
	local c=e:GetHandler()
	c:RegisterFlagEffect(ide,RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET+RESET_PHASE+PHASE_END,EFFECT_FLAG_CLIENT_HINT,1,0,aux.Stringid(ide,0))
end
function s.GFE1(e,ide)
	local c=e:GetHandler()
	c:RegisterFlagEffect(ide,RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET+RESET_PHASE+PHASE_END,EFFECT_FLAG_CLIENT_HINT,1,0,aux.Stringid(ide,1))
end
function s.rmtg(e,c) 
	return c:GetOwner()==e:GetHandlerPlayer() 
end
function s.con(e,tp,eg,ep,ev,re,r,rp) 
	return Duel.GetFlagEffect(tp,41412012)>0 
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckLPCost(tp,700) end
	Duel.Hint(HINT_OPSELECTED,1-tp,e:GetDescription())
	Duel.PayLPCost(tp,700)
end
function s.filter(c) 
	return aux.IsCodeListed(c,40412012) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand() 
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_DECK,0,1,nil) end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK)
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local g=Duel.SelectMatchingCard(tp,s.filter,tp,LOCATION_DECK,0,1,1,nil)
	if #g>0 then
		Duel.SendtoHand(g,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,g)
	end
end
function s.cfilter0(c,tp)
	return c:IsRace(RACE_PYRO) and c:IsPreviousLocation(LOCATION_ONFIELD) and not c:IsType(TYPE_TOKEN) and c:IsPreviousControler(tp) 
end
function s.spcon(e,tp,eg,ep,ev,re,r,rp)
	return eg:IsExists(s.cfilter0,1,nil,tp)
end
function s.cfilter(c,e,tp)
	return c:IsRace(RACE_PYRO) and c:IsLocation(LOCATION_REMOVED) and c:IsPreviousControler(tp) 
		 and c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsCanBeEffectTarget(e) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.cfilter2(c,e,tp)
	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_ONFIELD)
		and c:IsRace(RACE_PYRO) and not c:IsType(TYPE_TOKEN) and c:IsCanBeEffectTarget(e) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) 
end
function s.tdtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	--local mg=eg:Filter(s.cfilter1,nil,tp):Filter(s.tgfilter,nil,e,tp)
	local mg=eg:Filter(s.cfilter2,nil,e,tp)
	if chkc then return mg:IsContains(chkc) and s.cfilter2(chkc,e,tp) end
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and mg:GetCount()>0 end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TARGET)
	local tg=mg:FilterSelect(tp,s.cfilter,1,1,nil,e,tp)
	Duel.SetTargetCard(tg)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,tg,1,0,0)
end
function s.tdop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	if tc:IsRelateToEffect(e) and Duel.SpecialSummonStep(tc,0,tp,tp,false,false,POS_FACEUP) then
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetValue(500)
		e1:SetReset(RESET_EVENT+RESETS_STANDARD)
		tc:RegisterEffect(e1)
		local e2=Effect.CreateEffect(e:GetHandler())
		e2:SetType(EFFECT_TYPE_SINGLE)
		e2:SetCode(EFFECT_CANNOT_TRIGGER)
		e2:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
		tc:RegisterEffect(e2)
	end
	Duel.SpecialSummonComplete()
end
function s.cfilter1(c) 
	return c:IsFaceup() and (c:IsRace(RACE_PYRO) or c:IsSetCard(0x3cfe)) 
end
function s.imecon(e,tp,eg,ep,ev,re,r,rp) 
	return Duel.IsExistingMatchingCard(s.cfilter1,tp,LOCATION_MZONE,0,1,nil) 
end
function s.efilter(e,re) 
	return e:GetHandlerPlayer()~=re:GetOwnerPlayer() and re:IsActivated() 
end