--海晶乙女の泡撃
--Marincess Bubble Blast
--scripted by Larry126
local s,id=GetID()
function s.initial_effect(c)
	c:SetUniqueOnField(1,0,id)
	aux.AddPersistentProcedure(c,0,s.filter,nil,nil,nil,nil,s.condition,s.cost)
	--Inflict damage based on the Link Rating
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e1:SetCode(EVENT_SUMMON_SUCCESS)
	e1:SetRange(LOCATION_SZONE)
	e1:SetTarget(s.damtg)
	e1:SetOperation(s.damop)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
	c:RegisterEffect(e2)
	--Self destruction
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_SINGLE)
	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e3:SetCode(EFFECT_SELF_DESTROY)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCondition(s.descon)
	c:RegisterEffect(e3)
end
s.listed_series={0x12b}
function s.descon(e)
	return Duel.GetLP(1-e:GetHandlerPlayer())<=Duel.GetLP(e:GetHandlerPlayer()) or Duel.GetCurrentPhase()==PHASE_END
end
function s.filter(c)
	return c:IsSetCard(SET_MARINCESS) and c:IsLinkMonster() and c:IsLinkBelow(2)
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsLinkMonster() and c:IsLinkAbove(3)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.IsExistingMatchingCard(s.cfilter,tp,0,LOCATION_MZONE,1,nil)
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetActivityCount(tp,ACTIVITY_ATTACK)==0 end
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_CANNOT_ATTACK_ANNOUNCE)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
	e1:SetTargetRange(1,0)
	e1:SetReset(RESET_PHASE+PHASE_END)
	Duel.RegisterEffect(e1,tp)
end
function s.sfilter(c,g)
	return c:IsFaceup() and c:IsSetCard(SET_MARINCESS) and g:IsContains(c)
end
function s.damtg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	local tc=c:GetFirstCardTarget()
	local lg=nil
	if tc then lg=tc:GetLinkedGroup() end
	if chk==0 then return tc and lg and eg:IsExists(s.sfilter,1,nil,lg) end
	local dam=(tc:GetLink()+lg:GetSum(Card.GetLink))*500
	Duel.SetTargetPlayer(1-tp)
	Duel.SetTargetParam(dam)
	Duel.SetOperationInfo(0,CATEGORY_DAMAGE,nil,0,1-tp,dam)
end
function s.damop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if not c:IsRelateToEffect(e) then return end
	local tc=c:GetFirstCardTarget()
	if not tc then return false end
	local lg=tc:GetLinkedGroup()
	local dam=(tc:GetLink()+lg:GetSum(Card.GetLink))*500
	if dam>0 then
		local p=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER)
		Duel.Damage(p,dam,REASON_EFFECT)
	end
end