name: Koa'ki
card_text: This card cannot be Normal Summoned or Set. This card can only be Special Summoned by removing from play 1 "Iron Core of Koa'ki Meiru" from your hand. During each of your End Phases, destroy this card unless you send 1 "Iron Core of Koa'ki Meiru" or 1 "Koa'ki Meiru" monster from your hand to the Graveyard. Once per turn, during your Main Phase, you can select and destroy 1 card your opponent controls.
external_ids: local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  local SETCARD_ID_0 = 0x1d --Koa'ki
  
lua: local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  local SETCARD_ID_0 = 0x1d --Koa'ki
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	c:EnableReviveLimit()
  	--cost
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.mtcon)
  	e1:SetOperation(s.mtop)
  	c:RegisterEffect(e1)
  	--spsummon proc
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_SPSUMMON_PROC)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e2:SetRange(LOCATION_HAND)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 3))
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.mtcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.cfilter1(c)
  	return c:IsCode(CARD_ID_0) and c:IsAbleToGraveAsCost()
  end
  function s.cfilter2(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGraveAsCost()
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	Duel.HintSelection(Group.FromCards(c))
  	local g1 = Duel.GetMatchingGroup(s.cfilter1, tp, LOCATION_HAND, 0, nil)
  	local g2 = Duel.GetMatchingGroup(s.cfilter2, tp, LOCATION_HAND, 0, nil)
  	local select = 2
  	if g1:GetCount() > 0 and g2:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif g1:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 2))
  		if select == 1 then
  			select = 2
  		end
  	elseif g2:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2)) + 1
  	else
  		select = Duel.SelectOption(tp, aux.Stringid(id, 2))
  		select = 2
  	end
  	if select == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local g = g1:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(g, REASON_COST)
  	elseif select == 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local g = g2:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(g, REASON_COST)
  	else
  		Duel.Destroy(c, REASON_COST)
  	end
  end
  function s.spfilter(c)
  	return c:IsCode(CARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_HAND, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
  	if tc then
  		e:SetLabelObject(tc)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.Remove(g, POS_FACEUP, REASON_SPSUMMON)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  
  
source_text: Koa'ki
  This card cannot be Normal Summoned or Set. This card can only be Special Summoned by removing from play 1 "Iron Core of Koa'ki Meiru" from your hand. During each of your End Phases, destroy this card unless you send 1 "Iron Core of Koa'ki Meiru" or 1 "Koa'ki Meiru" monster from your hand to the Graveyard. Once per turn, during your Main Phase, you can select and destroy 1 card your opponent controls.
target_text: -- Koa'ki
  local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  local SETCARD_ID_0 = 0x1d --Koa'ki
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	c:EnableReviveLimit()
  	--cost
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.mtcon)
  	e1:SetOperation(s.mtop)
  	c:RegisterEffect(e1)
  	--spsummon proc
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_SPSUMMON_PROC)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e2:SetRange(LOCATION_HAND)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 3))
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.mtcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.cfilter1(c)
  	return c:IsCode(CARD_ID_0) and c:IsAbleToGraveAsCost()
  end
  function s.cfilter2(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGraveAsCost()
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	Duel.HintSelection(Group.FromCards(c))
  	local g1 = Duel.GetMatchingGroup(s.cfilter1, tp, LOCATION_HAND, 0, nil)
  	local g2 = Duel.GetMatchingGroup(s.cfilter2, tp, LOCATION_HAND, 0, nil)
  	local select = 2
  	if g1:GetCount() > 0 and g2:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif g1:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 2))
  		if select == 1 then
  			select = 2
  		end
  	elseif g2:GetCount() > 0 then
  		select = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2)) + 1
  	else
  		select = Duel.SelectOption(tp, aux.Stringid(id, 2))
  		select = 2
  	end
  	if select == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local g = g1:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(g, REASON_COST)
  	elseif select == 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local g = g2:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(g, REASON_COST)
  	else
  		Duel.Destroy(c, REASON_COST)
  	end
  end
  function s.spfilter(c)
  	return c:IsCode(CARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_HAND, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
  	if tc then
  		e:SetLabelObject(tc)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.Remove(g, POS_FACEUP, REASON_SPSUMMON)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  
  