name: Koa'ki
card_text: During each of your End Phases, destroy this card unless you send 1 "Iron Core of Koa'ki Meiru" from your hand to the Graveyard or reveal 1 Warrior-Type monster in your hand. Once per turn: You can target 1 Level 3 or lower Warrior-Type monster in your Graveyard; equip that target to this card. This card gains ATK equal to half the combined ATK of the monsters equipped to it by this effect. If this card would be destroyed by battle, destroy all monsters equipped to it by this effect instead.
external_ids: local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  
lua: local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--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)
  	--equip
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 3))
  	e2:SetCategory(CATEGORY_LEAVE_GRAVE + CATEGORY_EQUIP)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.eqtg)
  	e2:SetOperation(s.eqop)
  	c:RegisterEffect(e2)
  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:IsRace(RACE_WARRIOR) and not c:IsPublic()
  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_CONFIRM)
  		local g = g2:Select(tp, 1, 1, nil)
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.Destroy(c, REASON_COST)
  	end
  end
  function s.filter(c)
  	return c:IsLevelBelow(3) and c:IsRace(RACE_WARRIOR) and not c:IsForbidden()
  end
  function s.eqtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, g, 1, 0, 0)
  end
  function s.eqop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsRace(RACE_WARRIOR) then
  		if not Duel.Equip(tp, tc, c, false) then
  			return
  		end
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_OWNER_RELATE)
  		e1:SetCode(EFFECT_EQUIP_LIMIT)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e1:SetValue(s.eqlimit)
  		tc:RegisterEffect(e1)
  		local atk = tc:GetAttack()
  		if atk < 0 then
  			atk = 0
  		end
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_EQUIP)
  		e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e2:SetCode(EFFECT_UPDATE_ATTACK)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e2:SetValue(math.ceil(atk / 2))
  		tc:RegisterEffect(e2)
  		local e3 = Effect.CreateEffect(c)
  		e3:SetType(EFFECT_TYPE_EQUIP)
  		e3:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e3:SetCode(EFFECT_DESTROY_SUBSTITUTE)
  		e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e3:SetValue(s.subval)
  		tc:RegisterEffect(e3)
  	end
  end
  function s.eqlimit(e, c)
  	return e:GetOwner() == c
  end
  function s.subval(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  
source_text: Koa'ki
  During each of your End Phases, destroy this card unless you send 1 "Iron Core of Koa'ki Meiru" from your hand to the Graveyard or reveal 1 Warrior-Type monster in your hand. Once per turn: You can target 1 Level 3 or lower Warrior-Type monster in your Graveyard; equip that target to this card. This card gains ATK equal to half the combined ATK of the monsters equipped to it by this effect. If this card would be destroyed by battle, destroy all monsters equipped to it by this effect instead.
target_text: -- Koa'ki
  local CARD_ID_0 = 36623431 --Iron Core of Koa'ki Meiru
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--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)
  	--equip
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 3))
  	e2:SetCategory(CATEGORY_LEAVE_GRAVE + CATEGORY_EQUIP)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.eqtg)
  	e2:SetOperation(s.eqop)
  	c:RegisterEffect(e2)
  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:IsRace(RACE_WARRIOR) and not c:IsPublic()
  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_CONFIRM)
  		local g = g2:Select(tp, 1, 1, nil)
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.Destroy(c, REASON_COST)
  	end
  end
  function s.filter(c)
  	return c:IsLevelBelow(3) and c:IsRace(RACE_WARRIOR) and not c:IsForbidden()
  end
  function s.eqtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, g, 1, 0, 0)
  end
  function s.eqop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsRace(RACE_WARRIOR) then
  		if not Duel.Equip(tp, tc, c, false) then
  			return
  		end
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_OWNER_RELATE)
  		e1:SetCode(EFFECT_EQUIP_LIMIT)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e1:SetValue(s.eqlimit)
  		tc:RegisterEffect(e1)
  		local atk = tc:GetAttack()
  		if atk < 0 then
  			atk = 0
  		end
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_EQUIP)
  		e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e2:SetCode(EFFECT_UPDATE_ATTACK)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e2:SetValue(math.ceil(atk / 2))
  		tc:RegisterEffect(e2)
  		local e3 = Effect.CreateEffect(c)
  		e3:SetType(EFFECT_TYPE_EQUIP)
  		e3:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e3:SetCode(EFFECT_DESTROY_SUBSTITUTE)
  		e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  		e3:SetValue(s.subval)
  		tc:RegisterEffect(e3)
  	end
  end
  function s.eqlimit(e, c)
  	return e:GetOwner() == c
  end
  function s.subval(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  