name: Mekk-Knight
card_text: If 2 or more cards are in the same column, you can Special Summon this card (from your hand) in that column. You can only Special Summon "Mekk-Knight Purple Nightfall" once per turn this way. (Quick Effect): You can target 1 "Mekk-Knight" monster you control; banish it (until the Standby Phase of your next turn), and if you do, add 1 "Mekk-Knight" monster from your Deck to your hand, except "Mekk-Knight Purple Nightfall". You can only use this effect of "Mekk-Knight Purple Nightfall" once per turn.
external_ids: local SETCARD_ID_0 = 0x10c --Mekk-Knight
  
lua: local SETCARD_ID_0 = 0x10c --Mekk-Knight
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.hspcon)
  	e1:SetValue(s.hspval)
  	c:RegisterEffect(e1)
  	--banish & search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_REMOVE + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:GetColumnGroupCount() > 0
  end
  function s.hspcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local zone = 0
  	local lg = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	for tc in aux.Next(lg) do
  		zone = bit.bor(zone, tc:GetColumnZone(LOCATION_MZONE, tp))
  	end
  	return Duel.GetLocationCount(tp, LOCATION_MZONE, tp, LOCATION_REASON_TOFIELD, zone) > 0
  end
  function s.hspval(e, c)
  	local tp = c:GetControler()
  	local zone = 0
  	local lg = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	for tc in aux.Next(lg) do
  		zone = bit.bor(zone, tc:GetColumnZone(LOCATION_MZONE, tp))
  	end
  	return 0, zone
  end
  function s.rmfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemove()
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsCode(id) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.rmfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Remove(tc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 and tc:IsLocation(LOCATION_REMOVED) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetDescription(aux.Stringid(id, 1))
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_STANDBY)
  		e1:SetLabelObject(tc)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		if Duel.GetTurnPlayer() == tp and Duel.GetCurrentPhase() <= PHASE_STANDBY then
  			e1:SetReset(RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 2)
  			e1:SetValue(Duel.GetTurnCount())
  			tc:RegisterFlagEffect(id, RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 0, 2)
  		else
  			e1:SetReset(RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN)
  			e1:SetValue(0)
  			tc:RegisterFlagEffect(id, RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 0, 1)
  		end
  		Duel.RegisterEffect(e1, tp)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() ~= tp or Duel.GetTurnCount() == e:GetValue() then
  		return false
  	end
  	return e:GetLabelObject():GetFlagEffect(id) ~= 0
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	Duel.ReturnToField(tc)
  end
  
  
source_text: Mekk-Knight
  If 2 or more cards are in the same column, you can Special Summon this card (from your hand) in that column. You can only Special Summon "Mekk-Knight Purple Nightfall" once per turn this way. (Quick Effect): You can target 1 "Mekk-Knight" monster you control; banish it (until the Standby Phase of your next turn), and if you do, add 1 "Mekk-Knight" monster from your Deck to your hand, except "Mekk-Knight Purple Nightfall". You can only use this effect of "Mekk-Knight Purple Nightfall" once per turn.
target_text: -- Mekk-Knight
  local SETCARD_ID_0 = 0x10c --Mekk-Knight
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.hspcon)
  	e1:SetValue(s.hspval)
  	c:RegisterEffect(e1)
  	--banish & search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_REMOVE + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:GetColumnGroupCount() > 0
  end
  function s.hspcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local zone = 0
  	local lg = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	for tc in aux.Next(lg) do
  		zone = bit.bor(zone, tc:GetColumnZone(LOCATION_MZONE, tp))
  	end
  	return Duel.GetLocationCount(tp, LOCATION_MZONE, tp, LOCATION_REASON_TOFIELD, zone) > 0
  end
  function s.hspval(e, c)
  	local tp = c:GetControler()
  	local zone = 0
  	local lg = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	for tc in aux.Next(lg) do
  		zone = bit.bor(zone, tc:GetColumnZone(LOCATION_MZONE, tp))
  	end
  	return 0, zone
  end
  function s.rmfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemove()
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsCode(id) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.rmfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Remove(tc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 and tc:IsLocation(LOCATION_REMOVED) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetDescription(aux.Stringid(id, 1))
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_STANDBY)
  		e1:SetLabelObject(tc)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		if Duel.GetTurnPlayer() == tp and Duel.GetCurrentPhase() <= PHASE_STANDBY then
  			e1:SetReset(RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 2)
  			e1:SetValue(Duel.GetTurnCount())
  			tc:RegisterFlagEffect(id, RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 0, 2)
  		else
  			e1:SetReset(RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN)
  			e1:SetValue(0)
  			tc:RegisterFlagEffect(id, RESET_PHASE + PHASE_STANDBY + RESET_SELF_TURN, 0, 1)
  		end
  		Duel.RegisterEffect(e1, tp)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() ~= tp or Duel.GetTurnCount() == e:GetValue() then
  		return false
  	end
  	return e:GetLabelObject():GetFlagEffect(id) ~= 0
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	Duel.ReturnToField(tc)
  end
  
  