Generate a YGOPro Lua script for the following Yu-Gi-Oh card effect:
Distribute a number of Cubic Counters on face-up monsters your opponent controls, up to the number of "Cubic" monsters you control. (Monsters with a Cubic Counter cannot attack, also negate their effects.) You can banish this card from your GY, then target 1 "Cubic" monster you control; this turn, each time it destroys a monster by battle that has a Cubic Counter, inflict damage to your opponent equal to the original ATK of that destroyed monster. You can only use each effect of "Cubic Causality" once per turn.
--方界縁起
--Cubic Causality
--scripted by Naim
local s,id=GetID()
function s.initial_effect(c)
	--counters
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_COUNTER)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCountLimit(1,id)
	e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER|TIMING_END_PHASE)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--damage
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,1))
	e2:SetCategory(CATEGORY_DISABLE)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e2:SetHintTiming(0,TIMING_BATTLE_START)
	e2:SetCountLimit(1,{id,1})
	e2:SetCost(Cost.SelfBanish) 
	e2:SetTarget(s.damtg)
	e2:SetOperation(s.damact)
	c:RegisterEffect(e2)
end
s.counter_place_list={0x1038}
s.listed_series={SET_CUBIC}
function s.cfilter(c)
	return c:IsFaceup() and c:IsSetCard(SET_CUBIC)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_MZONE,0,1,nil)
		and Duel.IsExistingMatchingCard(Card.IsFaceup,tp,0,LOCATION_MZONE,1,nil) end
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local ct=Duel.GetMatchingGroupCount(s.cfilter,tp,LOCATION_MZONE,0,nil)
	local g=Duel.GetMatchingGroup(Card.IsFaceup,tp,0,LOCATION_MZONE,nil)
	if ct>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FACEUP)
		local sg=g:Select(tp,1,ct,nil)
		local ac=sg:GetFirst()
		for ac in aux.Next(sg) do
			ac:AddCounter(0x1038,1)
			local e1=Effect.CreateEffect(c)
			e1:SetType(EFFECT_TYPE_SINGLE)
			e1:SetCode(EFFECT_CANNOT_ATTACK)
			e1:SetCondition(s.condition)
			e1:SetReset(RESET_EVENT|RESETS_STANDARD)
			ac:RegisterEffect(e1)
			local e2=e1:Clone()
			e2:SetCode(EFFECT_DISABLE)
			ac:RegisterEffect(e2)
		end
	end
end
function s.condition(e)
	return e:GetHandler():GetCounter(0x1038)>0
end
function s.tgfilter(c)
	return c:IsFaceup() and c:IsSetCard(SET_CUBIC)
end
function s.damtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsOnField() and chkc:IsControler(tp) and s.tgfilter(chkc) end
	if chk==0 then return Duel.IsExistingTarget(s.tgfilter,tp,LOCATION_MZONE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FACEUP)
	Duel.SelectTarget(tp,s.tgfilter,tp,LOCATION_MZONE,0,1,1,nil)
end
function s.damact(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) and tc:IsFaceup() then
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
		e1:SetCode(EVENT_LEAVE_FIELD_P)
		e1:SetOperation(s.shchk)
		e1:SetLabelObject(tc)
		e1:SetReset(RESET_PHASE|PHASE_END)
		Duel.RegisterEffect(e1,tp)
		local e2=Effect.CreateEffect(c)
		e2:SetDescription(aux.Stringid(id,2))
		e2:SetCategory(CATEGORY_DAMAGE)
		e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		e2:SetCode(EVENT_BATTLE_DESTROYING)
		e2:SetCondition(s.shcon)
		e2:SetOperation(s.damop)
		e2:SetLabelObject(tc)
		e2:SetReset(RESET_PHASE|PHASE_END)
		Duel.RegisterEffect(e2,tp)
		tc:RegisterFlagEffect(id,RESET_EVENT|RESETS_STANDARD_PHASE_END&~(RESET_LEAVE|RESET_TOGRAVE),0,1)
		tc:CreateEffectRelation(e1)
	end
end
function s.shchk(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetLabelObject()
	if not tc or not tc:IsRelateToEffect(e) then return end
	local bt=tc:GetBattleTarget()
	for tc in aux.Next(eg) do
		if bt==tc and tc:GetCounter(0x1038) > 0 then
			tc:RegisterFlagEffect(id+2,RESET_CHAIN,0,1)
		end
	end
end
function s.shcon(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetLabelObject()
	local bt=tc:GetBattleTarget()
	return tc and tc:GetFlagEffect(id)~=0 and bt and bt:GetFlagEffect(id+2) ~= 0
end
function s.damop(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetLabelObject()
	local bt=tc:GetBattleTarget()
	local atk=bt:GetBaseAttack()
	Duel.Damage(1-tp,atk,REASON_EFFECT)
end