name: Butterflyoke
card_text: When an opponent's monster declares an attack: Target the attacking monster; change that target to Defense Position, and if you do, equip this card to that monster. Once per turn, during either player's Main Phase or Battle Phase: You can change the equipped monster's Battle Position.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_POSITION + CATEGORY_EQUIP)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--pos
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.poscon)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local c = e:GetHandler()
  	local cid = Duel.GetChainInfo(0, CHAININFO_CHAIN_ID)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_REMAIN_FIELD)
  	e1:SetProperty(EFFECT_FLAG_OATH)
  	e1:SetReset(RESET_CHAIN)
  	c:RegisterEffect(e1)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_DISABLED)
  	e2:SetOperation(s.tgop)
  	e2:SetLabel(cid)
  	e2:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local cid = Duel.GetChainInfo(ev, CHAININFO_CHAIN_ID)
  	if cid ~= e:GetLabel() then
  		return
  	end
  	if e:GetOwner():IsRelateToChain(ev) then
  		e:GetOwner():CancelToGrave(false)
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local tc = Duel.GetAttacker()
  	if chkc then
  		return chkc == tc
  	end
  	if chk == 0 then
  		return tc:IsLocation(LOCATION_MZONE) and tc:IsAttackPos() and tc:IsCanChangePosition() and tc:IsCanBeEffectTarget(e) and e:IsCostChecked()
  	end
  	Duel.SetTargetCard(tc)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsLocation(LOCATION_SZONE) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsAttackable() and not tc:IsStatus(STATUS_ATTACK_CANCELED) then
  		Duel.ChangePosition(tc, POS_FACEUP_DEFENSE)
  		if c:IsRelateToEffect(e) and not c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  			Duel.Equip(tp, c, tc)
  			--Equip limit
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetValue(s.eqlimit)
  			e1:SetLabelObject(tc)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			c:RegisterEffect(e1)
  		end
  	elseif c:IsRelateToEffect(e) and not c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  		c:CancelToGrave(false)
  	end
  end
  function s.eqlimit(e, c)
  	return c == e:GetLabelObject()
  end
  function s.poscon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph >= PHASE_MAIN1 and ph <= PHASE_MAIN2 and e:GetHandler():GetEquipTarget()
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ec = c:GetEquipTarget()
  	if ec then
  		Duel.ChangePosition(ec, POS_FACEUP_DEFENSE, 0, POS_FACEUP_ATTACK, 0)
  	end
  end
  
  
source_text: Butterflyoke
  When an opponent's monster declares an attack: Target the attacking monster; change that target to Defense Position, and if you do, equip this card to that monster. Once per turn, during either player's Main Phase or Battle Phase: You can change the equipped monster's Battle Position.
target_text: -- Butterflyoke
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_POSITION + CATEGORY_EQUIP)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--pos
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.poscon)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local c = e:GetHandler()
  	local cid = Duel.GetChainInfo(0, CHAININFO_CHAIN_ID)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_REMAIN_FIELD)
  	e1:SetProperty(EFFECT_FLAG_OATH)
  	e1:SetReset(RESET_CHAIN)
  	c:RegisterEffect(e1)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_DISABLED)
  	e2:SetOperation(s.tgop)
  	e2:SetLabel(cid)
  	e2:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local cid = Duel.GetChainInfo(ev, CHAININFO_CHAIN_ID)
  	if cid ~= e:GetLabel() then
  		return
  	end
  	if e:GetOwner():IsRelateToChain(ev) then
  		e:GetOwner():CancelToGrave(false)
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local tc = Duel.GetAttacker()
  	if chkc then
  		return chkc == tc
  	end
  	if chk == 0 then
  		return tc:IsLocation(LOCATION_MZONE) and tc:IsAttackPos() and tc:IsCanChangePosition() and tc:IsCanBeEffectTarget(e) and e:IsCostChecked()
  	end
  	Duel.SetTargetCard(tc)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsLocation(LOCATION_SZONE) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsAttackable() and not tc:IsStatus(STATUS_ATTACK_CANCELED) then
  		Duel.ChangePosition(tc, POS_FACEUP_DEFENSE)
  		if c:IsRelateToEffect(e) and not c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  			Duel.Equip(tp, c, tc)
  			--Equip limit
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetValue(s.eqlimit)
  			e1:SetLabelObject(tc)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			c:RegisterEffect(e1)
  		end
  	elseif c:IsRelateToEffect(e) and not c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  		c:CancelToGrave(false)
  	end
  end
  function s.eqlimit(e, c)
  	return c == e:GetLabelObject()
  end
  function s.poscon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph >= PHASE_MAIN1 and ph <= PHASE_MAIN2 and e:GetHandler():GetEquipTarget()
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ec = c:GetEquipTarget()
  	if ec then
  		Duel.ChangePosition(ec, POS_FACEUP_DEFENSE, 0, POS_FACEUP_ATTACK, 0)
  	end
  end
  
  