name: Magical
card_text: You can Tribute Summon this card face-up by Tributing 1 "Magical Musket" monster. During either player's turn, you can activate "Magical Musket" Spell/Trap Cards from your hand. Once per turn, during your opponent's End Phase: You can draw cards equal to the number of "Magical Musket" Spell/Trap Cards you activated this turn while this card was face-up on your field. You can only use this effect of "Magical Musket Mastermind Zakiel" once per turn.
external_ids: local SETCARD_ID_0 = 0x108 --Magical
  
lua: local SETCARD_ID_0 = 0x108 --Magical
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--normal summon with 1 tribute
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SUMMON_PROC)
  	e1:SetCondition(s.otcon)
  	e1:SetOperation(s.otop)
  	e1:SetValue(SUMMON_TYPE_ADVANCE)
  	c:RegisterEffect(e1)
  	--activate from hand
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_QP_ACT_IN_NTPHAND)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_0))
  	e3:SetTargetRange(LOCATION_HAND, 0)
  	e3:SetValue(32841045)
  	c:RegisterEffect(e3)
  	local e4 = e3:Clone()
  	e4:SetCode(EFFECT_TRAP_ACT_IN_HAND)
  	c:RegisterEffect(e4)
  	--draw
  	local e5 = Effect.CreateEffect(c)
  	e5:SetCategory(CATEGORY_DRAW)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e5:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e5:SetCode(EVENT_PHASE + PHASE_END)
  	e5:SetRange(LOCATION_MZONE)
  	e5:SetCountLimit(1, id)
  	e5:SetCondition(s.drcon)
  	e5:SetTarget(s.drtg)
  	e5:SetOperation(s.drop)
  	c:RegisterEffect(e5)
  	local e6 = Effect.CreateEffect(c)
  	e6:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e6:SetCode(EVENT_CHAINING)
  	e6:SetRange(LOCATION_MZONE)
  	e6:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e6:SetLabelObject(e5)
  	e6:SetOperation(s.regop)
  	c:RegisterEffect(e6)
  	local e7 = e6:Clone()
  	e7:SetCode(EVENT_CHAIN_NEGATED)
  	e7:SetOperation(s.regop2)
  	c:RegisterEffect(e7)
  	local e8 = e6:Clone()
  	e8:SetCode(EVENT_PHASE_START + PHASE_DRAW)
  	e8:SetOperation(s.clearop)
  	c:RegisterEffect(e8)
  end
  function s.otfilter(c, tp)
  	return c:IsSetCard(SETCARD_ID_0) and (c:IsControler(tp) or c:IsFaceup())
  end
  function s.otcon(e, c, minc)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local mg = Duel.GetMatchingGroup(s.otfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, tp)
  	return c:IsLevelAbove(7) and minc <= 1 and Duel.CheckTribute(c, 1, 1, mg)
  end
  function s.otop(e, tp, eg, ep, ev, re, r, rp, c)
  	local mg = Duel.GetMatchingGroup(s.otfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, tp)
  	local sg = Duel.SelectTribute(tp, c, 1, 1, mg)
  	c:SetMaterial(sg)
  	Duel.Release(sg, REASON_SUMMON + REASON_MATERIAL)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	if re:GetHandler():IsSetCard(SETCARD_ID_0) and rp == tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local val = e:GetLabelObject():GetLabel()
  		e:GetLabelObject():SetLabel(val + 1)
  	end
  end
  function s.regop2(e, tp, eg, ep, ev, re, r, rp)
  	if re:GetHandler():IsSetCard(SETCARD_ID_0) and rp == tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local val = e:GetLabelObject():GetLabel()
  		if val == 0 then
  			val = 1
  		end
  		e:GetLabelObject():SetLabel(val - 1)
  	end
  end
  function s.clearop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetLabelObject():SetLabel(0)
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local d = e:GetLabel()
  	if chk == 0 then
  		return d > 0 and Duel.IsPlayerCanDraw(tp, d)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, d)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if not e:GetHandler():IsRelateToEffect(e) then
  		return
  	end
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	local d = e:GetLabel()
  	if d > 0 then
  		Duel.Draw(p, d, REASON_EFFECT)
  	end
  end
  
  
source_text: Magical
  You can Tribute Summon this card face-up by Tributing 1 "Magical Musket" monster. During either player's turn, you can activate "Magical Musket" Spell/Trap Cards from your hand. Once per turn, during your opponent's End Phase: You can draw cards equal to the number of "Magical Musket" Spell/Trap Cards you activated this turn while this card was face-up on your field. You can only use this effect of "Magical Musket Mastermind Zakiel" once per turn.
target_text: -- Magical
  local SETCARD_ID_0 = 0x108 --Magical
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--normal summon with 1 tribute
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SUMMON_PROC)
  	e1:SetCondition(s.otcon)
  	e1:SetOperation(s.otop)
  	e1:SetValue(SUMMON_TYPE_ADVANCE)
  	c:RegisterEffect(e1)
  	--activate from hand
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_QP_ACT_IN_NTPHAND)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_0))
  	e3:SetTargetRange(LOCATION_HAND, 0)
  	e3:SetValue(32841045)
  	c:RegisterEffect(e3)
  	local e4 = e3:Clone()
  	e4:SetCode(EFFECT_TRAP_ACT_IN_HAND)
  	c:RegisterEffect(e4)
  	--draw
  	local e5 = Effect.CreateEffect(c)
  	e5:SetCategory(CATEGORY_DRAW)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e5:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e5:SetCode(EVENT_PHASE + PHASE_END)
  	e5:SetRange(LOCATION_MZONE)
  	e5:SetCountLimit(1, id)
  	e5:SetCondition(s.drcon)
  	e5:SetTarget(s.drtg)
  	e5:SetOperation(s.drop)
  	c:RegisterEffect(e5)
  	local e6 = Effect.CreateEffect(c)
  	e6:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e6:SetCode(EVENT_CHAINING)
  	e6:SetRange(LOCATION_MZONE)
  	e6:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e6:SetLabelObject(e5)
  	e6:SetOperation(s.regop)
  	c:RegisterEffect(e6)
  	local e7 = e6:Clone()
  	e7:SetCode(EVENT_CHAIN_NEGATED)
  	e7:SetOperation(s.regop2)
  	c:RegisterEffect(e7)
  	local e8 = e6:Clone()
  	e8:SetCode(EVENT_PHASE_START + PHASE_DRAW)
  	e8:SetOperation(s.clearop)
  	c:RegisterEffect(e8)
  end
  function s.otfilter(c, tp)
  	return c:IsSetCard(SETCARD_ID_0) and (c:IsControler(tp) or c:IsFaceup())
  end
  function s.otcon(e, c, minc)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local mg = Duel.GetMatchingGroup(s.otfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, tp)
  	return c:IsLevelAbove(7) and minc <= 1 and Duel.CheckTribute(c, 1, 1, mg)
  end
  function s.otop(e, tp, eg, ep, ev, re, r, rp, c)
  	local mg = Duel.GetMatchingGroup(s.otfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, tp)
  	local sg = Duel.SelectTribute(tp, c, 1, 1, mg)
  	c:SetMaterial(sg)
  	Duel.Release(sg, REASON_SUMMON + REASON_MATERIAL)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	if re:GetHandler():IsSetCard(SETCARD_ID_0) and rp == tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local val = e:GetLabelObject():GetLabel()
  		e:GetLabelObject():SetLabel(val + 1)
  	end
  end
  function s.regop2(e, tp, eg, ep, ev, re, r, rp)
  	if re:GetHandler():IsSetCard(SETCARD_ID_0) and rp == tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local val = e:GetLabelObject():GetLabel()
  		if val == 0 then
  			val = 1
  		end
  		e:GetLabelObject():SetLabel(val - 1)
  	end
  end
  function s.clearop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetLabelObject():SetLabel(0)
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local d = e:GetLabel()
  	if chk == 0 then
  		return d > 0 and Duel.IsPlayerCanDraw(tp, d)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, d)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if not e:GetHandler():IsRelateToEffect(e) then
  		return
  	end
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	local d = e:GetLabel()
  	if d > 0 then
  		Duel.Draw(p, d, REASON_EFFECT)
  	end
  end
  
  