local s,id,o=GetID()
function s.initial_effect(c)
    c:SetSPSummonOnce(id)
    c:EnableReviveLimit()
	aux.AddFusionProcFun2(c,s.mfilter1,s.mfilter2,true)
	--special summon condition
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetCode(EFFECT_SPSUMMON_CONDITION)
	e1:SetValue(aux.fuslimit)
	c:RegisterEffect(e1)
	--special summon rule
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e2:SetCode(EFFECT_SPSUMMON_PROC)
	e2:SetRange(LOCATION_EXTRA)
	e2:SetCondition(s.sprcon)
	e2:SetOperation(s.sprop)
	c:RegisterEffect(e2)
    --replace
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,0))
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e3:SetCode(EVENT_CHAINING)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCondition(s.condition)
	e3:SetTarget(s.target)
	e3:SetOperation(s.operation)
	c:RegisterEffect(e3)
    --Negate Activation
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,1))
	e4:SetCategory(CATEGORY_NEGATE)
	e4:SetType(EFFECT_TYPE_QUICK_O)
	e4:SetCode(EVENT_CHAINING)
	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
	e4:SetRange(LOCATION_MZONE+LOCATION_GRAVE)
	e4:SetCountLimit(1)
    e4:SetCost(aux.bfgcost)
	e4:SetCondition(s.negcon)
	e4:SetTarget(s.negtg)
	e4:SetOperation(s.negop)
	c:RegisterEffect(e4)
end
function s.mfilter1(c)
	return c:IsSetCard(0x47)
end
function s.mfilter2(c)
	return c:IsRace(RACE_ROCK)
end
function s.sprfilter1(c,sc)
	return c:IsAttribute(ATTRIBUTE_EARTH) and c:IsAbleToRemoveAsCost() and c:IsCanBeFusionMaterial(sc,SUMMON_TYPE_SPECIAL)
end
function s.sprfilter2(c,tp,sc)
	return c:IsSetCard(0x47) and Duel.GetLocationCountFromEx(tp,tp,c,sc)>0 and c:IsCanBeFusionMaterial(sc,SUMMON_TYPE_SPECIAL) and Duel.CheckReleaseGroupEx(tp,s.sprfilter3,1,REASON_SPSUMMON,true,c,tp,sc)
end
function s.sprfilter3(c,tp,ssc)
	return c:IsRace(RACE_ROCK) and Duel.GetLocationCountFromEx(tp,tp,c,ssc)>0 and c:IsCanBeFusionMaterial(ssc,SUMMON_TYPE_SPECIAL)
end
function s.spfilter(c,tp)
    return (c:IsSetCard(0x47) or c:IsRace(RACE_ROCK)) and Duel.GetLocationCountFromEx(tp,tp,c,sc)>0 and c:IsCanBeFusionMaterial(sc,SUMMON_TYPE_SPECIAL)
end
function s.spfilter1(c)
    return c:IsSetCard(0x47) and c:IsType(TYPE_MONSTER) and c:IsCanBeFusionMaterial(sc,SUMMON_TYPE_SPECIAL)
end
function s.sprcon(e,c)
	if c==nil then return true end
	local tp=c:GetControler()
	return Duel.IsExistingMatchingCard(s.sprfilter1,tp,LOCATION_GRAVE,0,1,nil,c) and Duel.CheckReleaseGroupEx(tp,s.sprfilter2,1,REASON_SPSUMMON,true,nil,tp,c)
end
function s.sprop(e,tp,eg,ep,ev,re,r,rp,c)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
    local g1=Duel.SelectMatchingCard(tp,s.sprfilter1,tp,LOCATION_GRAVE,0,1,1,nil,c)
    c:SetMaterial(g1)
    Duel.Remove(g1,POS_FACEUP,REASON_COST)
    local ft=Duel.GetLocationCountFromEx(tp,tp,nil,c)
	if ft>0 then
		local g1=Duel.SelectReleaseGroupEx(tp,s.sprfilter2,1,1,REASON_SPSUMMON,nil,tp,c)
		local g2=Duel.SelectReleaseGroupEx(tp,s.sprfilter3,1,1,REASON_SPSUMMON,g1,tp,c)
		g1:Merge(g2)
		c:SetMaterial(g1)
		Duel.Release(g1,REASON_COST)
    elseif ft==0 then
		local g1=Duel.SelectReleaseGroup(tp,s.spfilter,1,1,nil,tp)
        local a=g1:GetFirst():IsRace(RACE_ROCK)
        local b=g1:GetFirst():IsSetCard(0x47)
		if a and b then
            local g2=Duel.SelectReleaseGroupEx(tp,s.spfilter,1,1,REASON_SPSUMMON,g1,tp)
        elseif not a and b then
            local g2=Duel.SelectReleaseGroupEx(tp,Card.IsRace,1,1,REASON_SPSUMMON,g1,RACE_ROCK)
		else
            local g2=Duel.SelectReleaseGroupEx(tp,s.spfilter1,1,1,REASON_SPSUMMON,g1) end
		g1:Merge(g2)
		c:SetMaterial(g1)
		Duel.Release(g1,REASON_COST)
    else
        local g1=Duel.SelectReleaseGroup(tp,s.sprfilter2,1,1,REASON_SPSUMMON,nil,tp,c)
		local g2=Duel.SelectReleaseGroup(tp,s.sprfilter3,1,1,REASON_SPSUMMON,g1,tp,c)
		g1:Merge(g2)
        c:SetMaterial(g1)
		Duel.Release(g1,REASON_COST)
    end
end
function s.cfilter(c)
    return c:IsSetCard(0x1047)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	if e==re or not re:IsHasProperty(EFFECT_FLAG_CARD_TARGET) then return false end
	local g=Duel.GetChainInfo(ev,CHAININFO_TARGET_CARDS)
	if not g or g:GetCount()~=1 then return false end
	local tc=g:GetFirst()
	e:SetLabelObject(tc)
	return tc:IsOnField() and Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_MZONE,0,1,e:GetHandler())
end
function s.filter(c,ct)
	return Duel.CheckChainTarget(ct,c)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	local ct=ev
	local label=Duel.GetFlagEffectLabel(0,id)
	if label then
		if ev==bit.rshift(label,16) then ct=bit.band(label,0xffff) end
	end
	if chkc then return chkc:IsOnField() and s.filter(chkc,ct) end
	if chk==0 then return Duel.IsExistingTarget(s.filter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,e:GetLabelObject(),ct) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TARGET)
	Duel.SelectTarget(tp,s.filter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,e:GetLabelObject(),ct)
	local val=ct+bit.lshift(ev+1,16)
	if label then
		Duel.SetFlagEffectLabel(0,id,val)
	else
		Duel.RegisterFlagEffect(0,id,RESET_CHAIN,0,1,val)
	end
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) then
		Duel.ChangeTargetCard(ev,Group.FromCards(tc))
	end
end
function s.nfilter(c)
	return c:IsSetCard(0x1047) and c:IsType(TYPE_FUSION) and c:IsFaceup()
end
function s.negcon(e,tp,eg,ep,ev,re,r,rp)
	return rp==1-tp and Duel.IsChainNegatable(ev) and not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) and (Duel.GetFieldGroupCount(tp,0,LOCATION_MZONE)>0 or Duel.GetTurnPlayer()~=tp) and Duel.IsExistingMatchingCard(s.nfilter,tp,LOCATION_MZONE,0,1,nil)
end
function s.negtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_NEGATE,eg,1,0,0)
end
function s.negop(e,tp,eg,ep,ev,re,r,rp)
    Duel.NegateActivation(ev)
end