name: Shateki
card_text: You can discard 1 card, then target 1 monster your opponent controls that has a Level; roll a six-sided die and apply this effect based on the result.
  ● Higher than the targeted monster's Level: Destroy the targeted monster, then you can add 1 monster with the same original Level from your Deck to your hand, also you cannot activate the effect of "Shateki" for the rest of this turn.
  ● Equal to or lower than that targeted monster's Level: Reduce the targeted monster's Level by 1.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND | CATEGORY_SEARCH | CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.descon)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) == 0
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.desilter(c)
  	return c:IsFaceup() and c:IsLevelAbove(1)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.desilter(chkc) and chkc ~= e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.desilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.desilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  end
  function s.thfilter(c, lv)
  	return c:IsLevel(lv) and c:IsAbleToHand()
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local dc = Duel.TossDice(tp, 1)
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsType(TYPE_MONSTER) then
  		if dc > tc:GetLevel() then
  			if Duel.Destroy(tc, REASON_EFFECT) ~= 0 then
  				local lv = tc:GetOriginalLevel()
  				if lv > 0 and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, lv) and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  					Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  					local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, lv)
  					if g:GetCount() > 0 then
  						Duel.BreakEffect()
  						Duel.SendtoHand(g, nil, REASON_EFFECT)
  						Duel.ConfirmCards(1 - tp, g)
  					end
  				end
  			end
  			Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetDescription(aux.Stringid(id, 2))
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET + EFFECT_FLAG_CLIENT_HINT)
  			e1:SetTargetRange(1, 0)
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  		elseif tc:IsLevelAbove(dc) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_LEVEL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(-1)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  
  
source_text: Shateki
  You can discard 1 card, then target 1 monster your opponent controls that has a Level; roll a six-sided die and apply this effect based on the result.
  ● Higher than the targeted monster's Level: Destroy the targeted monster, then you can add 1 monster with the same original Level from your Deck to your hand, also you cannot activate the effect of "Shateki" for the rest of this turn.
  ● Equal to or lower than that targeted monster's Level: Reduce the targeted monster's Level by 1.
target_text: -- Shateki
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND | CATEGORY_SEARCH | CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.descon)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) == 0
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.desilter(c)
  	return c:IsFaceup() and c:IsLevelAbove(1)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.desilter(chkc) and chkc ~= e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.desilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.desilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  end
  function s.thfilter(c, lv)
  	return c:IsLevel(lv) and c:IsAbleToHand()
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local dc = Duel.TossDice(tp, 1)
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsType(TYPE_MONSTER) then
  		if dc > tc:GetLevel() then
  			if Duel.Destroy(tc, REASON_EFFECT) ~= 0 then
  				local lv = tc:GetOriginalLevel()
  				if lv > 0 and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, lv) and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  					Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  					local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, lv)
  					if g:GetCount() > 0 then
  						Duel.BreakEffect()
  						Duel.SendtoHand(g, nil, REASON_EFFECT)
  						Duel.ConfirmCards(1 - tp, g)
  					end
  				end
  			end
  			Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetDescription(aux.Stringid(id, 2))
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET + EFFECT_FLAG_CLIENT_HINT)
  			e1:SetTargetRange(1, 0)
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  		elseif tc:IsLevelAbove(dc) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_LEVEL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(-1)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  
  