name: Beast
card_text: Once per turn, at the start of the Damage Step, if your monster battles an opponent's monster in the same column: You can return your monster and any other cards in its column to the hand. You can only activate the effect of "Beast King Unleashed" once per Chain.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	e0:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e0:SetHintTiming(TIMING_DAMAGE_STEP)
  	e0:SetTarget(s.target)
  	c:RegisterEffect(e0)
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BATTLE_START)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE
  	local b2 = Duel.CheckEvent(EVENT_BATTLE_START) and s.thcon(e, tp, eg, ep, ev, re, r, rp) and s.thtg(e, tp, eg, ep, ev, re, r, rp, 0)
  	if chk == 0 then
  		return b1 or b2
  	end
  	if b2 then
  		e:SetCategory(CATEGORY_TOHAND)
  		e:SetOperation(s.thop)
  		s.thtg(e, tp, eg, ep, ev, re, r, rp, 1)
  	else
  		e:SetCategory(0)
  		e:SetOperation(nil)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local b = Duel.GetAttackTarget()
  	if not b then
  		return false
  	end
  	if not a:IsControler(tp) then
  		a, b = b, a
  	end
  	local lg = a:GetColumnGroup()
  	if not lg:IsContains(b) then
  		return false
  	end
  	e:SetLabelObject(a)
  	return a:IsControler(tp) and a:IsRelateToBattle() and b:IsControler(1 - tp) and b:IsRelateToBattle()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local a = e:GetLabelObject()
  	local c = e:GetHandler()
  	if chk == 0 then
  		return a and a:IsAbleToHand() and Duel.GetFlagEffect(tp, id) == 0 and c:GetFlagEffect(50675041) == 0
  	end
  	Duel.RegisterFlagEffect(tp, id, RESET_CHAIN, 0, 1)
  	c:RegisterFlagEffect(50675041, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  	local g = a:GetColumnGroup():Filter(Card.IsAbleToHand, nil)
  	g:AddCard(a)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, #g, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local a = e:GetLabelObject()
  	if a and a:IsRelateToBattle() and a:IsControler(tp) then
  		local g = a:GetColumnGroup()
  		g:AddCard(a)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Beast
  Once per turn, at the start of the Damage Step, if your monster battles an opponent's monster in the same column: You can return your monster and any other cards in its column to the hand. You can only activate the effect of "Beast King Unleashed" once per Chain.
target_text: -- Beast
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	e0:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e0:SetHintTiming(TIMING_DAMAGE_STEP)
  	e0:SetTarget(s.target)
  	c:RegisterEffect(e0)
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BATTLE_START)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE
  	local b2 = Duel.CheckEvent(EVENT_BATTLE_START) and s.thcon(e, tp, eg, ep, ev, re, r, rp) and s.thtg(e, tp, eg, ep, ev, re, r, rp, 0)
  	if chk == 0 then
  		return b1 or b2
  	end
  	if b2 then
  		e:SetCategory(CATEGORY_TOHAND)
  		e:SetOperation(s.thop)
  		s.thtg(e, tp, eg, ep, ev, re, r, rp, 1)
  	else
  		e:SetCategory(0)
  		e:SetOperation(nil)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local b = Duel.GetAttackTarget()
  	if not b then
  		return false
  	end
  	if not a:IsControler(tp) then
  		a, b = b, a
  	end
  	local lg = a:GetColumnGroup()
  	if not lg:IsContains(b) then
  		return false
  	end
  	e:SetLabelObject(a)
  	return a:IsControler(tp) and a:IsRelateToBattle() and b:IsControler(1 - tp) and b:IsRelateToBattle()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local a = e:GetLabelObject()
  	local c = e:GetHandler()
  	if chk == 0 then
  		return a and a:IsAbleToHand() and Duel.GetFlagEffect(tp, id) == 0 and c:GetFlagEffect(50675041) == 0
  	end
  	Duel.RegisterFlagEffect(tp, id, RESET_CHAIN, 0, 1)
  	c:RegisterFlagEffect(50675041, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  	local g = a:GetColumnGroup():Filter(Card.IsAbleToHand, nil)
  	g:AddCard(a)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, #g, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local a = e:GetLabelObject()
  	if a and a:IsRelateToBattle() and a:IsControler(tp) then
  		local g = a:GetColumnGroup()
  		g:AddCard(a)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  		end
  	end
  end
  
  