name: Meklord
card_text: The first time this card would be destroyed by battle each turn, it is not destroyed. You can only use each of the following effects of "Meklord Nucleus Infinity Core" once per turn. If this card is Normal or Special Summoned: You can add 1 "Meklord" Spell/Trap from your Deck to your hand. If this card is destroyed by card effect: You can Special Summon 1 "Meklord Emperor" monster from your hand or Deck with a different Attribute from the monsters you control, ignoring its Summoning conditions, also for the rest of this turn, you can only declare an attack with 1 monster.
external_ids: local SETCARD_ID_0 = 0x3013 --Meklord
  local SETCARD_ID_1 = 0x13 --Meklord
  
lua: local SETCARD_ID_0 = 0x3013 --Meklord
  local SETCARD_ID_1 = 0x13 --Meklord
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--tohand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	--indes
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e3:SetCountLimit(1)
  	e3:SetValue(s.valcon)
  	c:RegisterEffect(e3)
  	--special summon
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetCountLimit(1, id + 100)
  	e4:SetCondition(s.spcon2)
  	e4:SetTarget(s.sptg2)
  	e4:SetOperation(s.spop2)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.filter(c, att)
  	return c:IsFaceup() and c:IsAttribute(att)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_1)
  		and c:IsType(TYPE_MONSTER)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, true, false)
  		and not Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil, c:GetAttribute())
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp)
  		if g:GetCount() > 0 then
  			Duel.SpecialSummon(g, 0, tp, tp, true, false, POS_FACEUP)
  		end
  	end
  	local c = e:GetHandler()
  	--cannot attack
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e2:SetCode(EFFECT_CANNOT_ATTACK_ANNOUNCE)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.atkcon)
  	e2:SetTarget(s.atktg)
  	Duel.RegisterEffect(e2, tp)
  	--check
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	e3:SetOperation(s.checkop)
  	e3:SetLabelObject(e2)
  	Duel.RegisterEffect(e3, tp)
  end
  function s.atkcon(e)
  	return e:GetLabel() ~= 0
  end
  function s.atktg(e, c)
  	return c:GetFieldID() ~= e:GetLabel()
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local fid = eg:GetFirst():GetFieldID()
  	e:GetLabelObject():SetLabel(fid)
  end
  
  
source_text: Meklord
  The first time this card would be destroyed by battle each turn, it is not destroyed. You can only use each of the following effects of "Meklord Nucleus Infinity Core" once per turn. If this card is Normal or Special Summoned: You can add 1 "Meklord" Spell/Trap from your Deck to your hand. If this card is destroyed by card effect: You can Special Summon 1 "Meklord Emperor" monster from your hand or Deck with a different Attribute from the monsters you control, ignoring its Summoning conditions, also for the rest of this turn, you can only declare an attack with 1 monster.
target_text: -- Meklord
  local SETCARD_ID_0 = 0x3013 --Meklord
  local SETCARD_ID_1 = 0x13 --Meklord
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--tohand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	--indes
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e3:SetCountLimit(1)
  	e3:SetValue(s.valcon)
  	c:RegisterEffect(e3)
  	--special summon
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetCountLimit(1, id + 100)
  	e4:SetCondition(s.spcon2)
  	e4:SetTarget(s.sptg2)
  	e4:SetOperation(s.spop2)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.filter(c, att)
  	return c:IsFaceup() and c:IsAttribute(att)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_1)
  		and c:IsType(TYPE_MONSTER)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, true, false)
  		and not Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil, c:GetAttribute())
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp)
  		if g:GetCount() > 0 then
  			Duel.SpecialSummon(g, 0, tp, tp, true, false, POS_FACEUP)
  		end
  	end
  	local c = e:GetHandler()
  	--cannot attack
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e2:SetCode(EFFECT_CANNOT_ATTACK_ANNOUNCE)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.atkcon)
  	e2:SetTarget(s.atktg)
  	Duel.RegisterEffect(e2, tp)
  	--check
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	e3:SetOperation(s.checkop)
  	e3:SetLabelObject(e2)
  	Duel.RegisterEffect(e3, tp)
  end
  function s.atkcon(e)
  	return e:GetLabel() ~= 0
  end
  function s.atktg(e, c)
  	return c:GetFieldID() ~= e:GetLabel()
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local fid = eg:GetFirst():GetFieldID()
  	e:GetLabelObject():SetLabel(fid)
  end
  
  