local cm,m = GetID()
cm.name = "狂风龙 绿宝石龙" 
if not Lanpa_Dragon_Effect then Lanpa_Dragon_Effect = {} end
local lge = Lanpa_Dragon_Effect
function cm.initial_effect(c)
    local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(m,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_CUSTOM+m)
	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e1:SetRange(LOCATION_HAND+LOCATION_GRAVE)
	e1:SetCondition(cm.con)
	e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	e1:SetCountLimit(1,m)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(m,1))
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCost(cm.cost)
	e2:SetOperation(cm.op2)
	c:RegisterEffect(e2)
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e3:SetCode(EVENT_ADJUST)
	e3:SetRange(LOCATION_DECK+LOCATION_HAND)
	e3:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_ADJUST)
	e1:SetCondition(cm.con3)
	e1:SetOperation(cm.opp)
	Duel.RegisterEffect(e1,tp)
	e:Reset()
	end)
	c:RegisterEffect(e3)
	table.insert(lge,{c,e2})
	if not cm.global_check then
		cm.global_check=true
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_DESTROYED)
		ge1:SetCondition(cm.con1)
		ge1:SetOperation(cm.op1)
		Duel.RegisterEffect(ge1,0)
	end
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return ev==1-tp or ev==PLAYER_ALL
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and c:IsCanBeSpecialSummoned(e,0,tp,false,false) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,c,1,0,0)
end
function cm.op(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 cm.con1(e,tp,eg,ep,ev,re,r,rp)
	local v=0
	if eg:IsExists(cm.pf,1,nil,0) then v=v+1 end
	if eg:IsExists(cm.pf,1,nil,1) then v=v+2 end
	if v==0 then return false end
	e:SetLabel(({0,1,PLAYER_ALL})[v])
	return true
end
function cm.op1(e,tp,eg,ep,ev,re,r,rp)
	Duel.RaiseEvent(eg,EVENT_CUSTOM+m,re,r,rp,ep,e:GetLabel())
end
function cm.pf(c,tp)
	return c:IsReason(REASON_BATTLE+REASON_EFFECT) and c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_ONFIELD)
end
function cm.f(c)
	return c:IsAbleToRemoveAsCost() and c:IsRace(RACE_DRAGON) and not c:IsAttribute(ATTRIBUTE_LIGHT)
end
function cm.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(cm.f,tp,LOCATION_GRAVE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,cm.f,tp,LOCATION_GRAVE,0,1,1,nil)
	Duel.Remove(g,POS_FACEUP,REASON_COST)
	e:SetLabelObject(g:GetFirst())
end
function cm.op2(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) then
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetRange(LOCATION_MZONE)
		e1:SetCode(EFFECT_IMMUNE_EFFECT)
		e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
		e1:SetValue(cm.ef)
		e1:SetLabelObject(e:GetLabelObject())
		c:RegisterEffect(e1)
	end
end
function cm.ef(e,te)
	return te:GetHandler():IsAttribute(e:GetLabelObject():GetAttribute())
end
function cm.ff(c,mc)
    return c:IsRace(RACE_DRAGON) and c:IsSummonType(SUMMON_TYPE_FUSION) and c:GetMaterial():IsContains(mc) and c:GetFlagEffect(m)==0
end
function cm.con3(e,tp,eg,ep,ev,re,r,rp)
    return Duel.IsExistingMatchingCard(cm.ff,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil,e:GetHandler())
end
function cm.get(c)
    for k,v in ipairs(lge) do
        if v[1]==c then return v[2] end
    end
    return false
end
function cm.opp(e,tp,eg,ep,ev,re,r,rp)
    local c,g = e:GetHandler(),Duel.GetMatchingGroup(cm.ff,tp,LOCATION_MZONE,LOCATION_MZONE,nil,e:GetHandler())
    for rc in aux.Next(g) do
        local e1 = cm.get(c):Clone()
        e1:SetReset(RESET_EVENT+RESETS_STANDARD)
        rc:RegisterEffect(e1)
        rc:RegisterFlagEffect(m,RESET_EVENT+RESETS_STANDARD,EFFECT_FLAG_CLIENT_HINT,0,0,aux.Stringid(m,3))
    end
end