name: Ghostrick
card_text: Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. Once per turn, during your Main Phase 1: You can target 1 "Ghostrick" monster you control; its ATK becomes equal to the combined original ATK of all "Ghostrick" monsters currently on the field, until the end of your opponent's next turn, but if it does, only that monster can attack this turn.
external_ids: local SETCARD_ID_0 = 0x8d --Ghostrick
  
lua: local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--summon limit
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_SUMMON)
  	e1:SetCondition(s.sumcon)
  	c:RegisterEffect(e1)
  	--turn set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  	--atkup
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_ATKCHANGE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.atkcon)
  	e3:SetTarget(s.atktg)
  	e3:SetOperation(s.atkop)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e)
  	return not Duel.IsExistingMatchingCard(s.filter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
  	end
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
  	end
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_CANNOT_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_OATH)
  	e1:SetTargetRange(LOCATION_MZONE, 0)
  	e1:SetTarget(s.ftarget)
  	e1:SetLabel(g:GetFirst():GetFieldID())
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		local atk = 0
  		local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  		local bc = g:GetFirst()
  		while bc do
  			local catk = bc:GetBaseAttack()
  			if catk < 0 then
  				catk = 0
  			end
  			atk = atk + catk
  			bc = g:GetNext()
  		end
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetValue(atk)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 2)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.ftarget(e, c)
  	return e:GetLabel() ~= c:GetFieldID()
  end
  
  
source_text: Ghostrick
  Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. Once per turn, during your Main Phase 1: You can target 1 "Ghostrick" monster you control; its ATK becomes equal to the combined original ATK of all "Ghostrick" monsters currently on the field, until the end of your opponent's next turn, but if it does, only that monster can attack this turn.
target_text: -- Ghostrick
  local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--summon limit
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_SUMMON)
  	e1:SetCondition(s.sumcon)
  	c:RegisterEffect(e1)
  	--turn set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  	--atkup
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_ATKCHANGE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.atkcon)
  	e3:SetTarget(s.atktg)
  	e3:SetOperation(s.atkop)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e)
  	return not Duel.IsExistingMatchingCard(s.filter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
  	end
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
  	end
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_CANNOT_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_OATH)
  	e1:SetTargetRange(LOCATION_MZONE, 0)
  	e1:SetTarget(s.ftarget)
  	e1:SetLabel(g:GetFirst():GetFieldID())
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		local atk = 0
  		local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  		local bc = g:GetFirst()
  		while bc do
  			local catk = bc:GetBaseAttack()
  			if catk < 0 then
  				catk = 0
  			end
  			atk = atk + catk
  			bc = g:GetNext()
  		end
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetValue(atk)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 2)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.ftarget(e, c)
  	return e:GetLabel() ~= c:GetFieldID()
  end
  
  