name: Hinezumi
card_text: Cannot be used as a material for a Fusion, Synchro, Xyz, or Link Summon. If this card is Normal or Special Summoned: Place 6 counters on it. Once per turn, during your End Phase: Give control of this card to your opponent. Once per turn, if control of this face-up card with a counter(s) changes: Roll a six-sided die, remove as many counters from this card as possible, up to the result, and if all of this card's counters are removed by this effect, destroy this card, and if you do, take 2000 damage.
external_ids: local COUNTER_ID_0 = 0x58 --Counter(Nezumihanabi)
  
lua: local COUNTER_ID_0 = 0x58 --Counter(Nezumihanabi)
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x58)
  	--cannot be material
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_CANNOT_BE_FUSION_MATERIAL)
  	e1:SetValue(s.fuslimit)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EFFECT_CANNOT_BE_SYNCHRO_MATERIAL)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_CANNOT_BE_XYZ_MATERIAL)
  	c:RegisterEffect(e3)
  	local e4 = e2:Clone()
  	e4:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
  	c:RegisterEffect(e4)
  	--add counter
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 0))
  	e5:SetCategory(CATEGORY_COUNTER)
  	e5:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e5:SetCode(EVENT_SUMMON_SUCCESS)
  	e5:SetProperty(EFFECT_FLAG_DELAY)
  	e5:SetOperation(s.countop)
  	c:RegisterEffect(e5)
  	local e6 = e5:Clone()
  	e6:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e6)
  	--control
  	local e7 = Effect.CreateEffect(c)
  	e7:SetDescription(aux.Stringid(id, 1))
  	e7:SetCategory(CATEGORY_CONTROL)
  	e7:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e7:SetCode(EVENT_PHASE + PHASE_END)
  	e7:SetRange(LOCATION_MZONE)
  	e7:SetCountLimit(1)
  	e7:SetCondition(s.ctrcon)
  	e7:SetTarget(s.ctrtg)
  	e7:SetOperation(s.ctrop)
  	c:RegisterEffect(e7)
  	--dice
  	local e8 = Effect.CreateEffect(c)
  	e8:SetDescription(aux.Stringid(id, 2))
  	e8:SetCategory(CATEGORY_DICE + CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e8:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e8:SetCode(EVENT_CONTROL_CHANGED)
  	e8:SetCountLimit(1)
  	e8:SetCondition(s.dicecon)
  	e8:SetTarget(s.dicetg)
  	e8:SetOperation(s.diceop)
  	c:RegisterEffect(e8)
  end
  function s.fuslimit(e, c, sumtype)
  	return sumtype & SUMMON_TYPE_FUSION == SUMMON_TYPE_FUSION
  end
  function s.countop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		c:AddCounter(COUNTER_ID_0, 6)
  	end
  end
  function s.ctrcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.ctrtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_CONTROL, e:GetHandler(), 1, 0, 0)
  end
  function s.ctrop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.GetControl(c, 1 - tp)
  	end
  end
  function s.dicecon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetCounter(COUNTER_ID_0) > 0
  end
  function s.dicetg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.diceop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() and c:IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_EFFECT) then
  		local dc = Duel.TossDice(tp, 1)
  		if dc > c:GetCounter(COUNTER_ID_0) then
  			dc = c:GetCounter(COUNTER_ID_0)
  		end
  		c:RemoveCounter(tp, COUNTER_ID_0, dc, REASON_EFFECT)
  		if c:GetCounter(COUNTER_ID_0) == 0 and Duel.Destroy(c, REASON_EFFECT) ~= 0 then
  			Duel.Damage(tp, 2000, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Hinezumi
  Cannot be used as a material for a Fusion, Synchro, Xyz, or Link Summon. If this card is Normal or Special Summoned: Place 6 counters on it. Once per turn, during your End Phase: Give control of this card to your opponent. Once per turn, if control of this face-up card with a counter(s) changes: Roll a six-sided die, remove as many counters from this card as possible, up to the result, and if all of this card's counters are removed by this effect, destroy this card, and if you do, take 2000 damage.
target_text: -- Hinezumi
  local COUNTER_ID_0 = 0x58 --Counter(Nezumihanabi)
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x58)
  	--cannot be material
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_CANNOT_BE_FUSION_MATERIAL)
  	e1:SetValue(s.fuslimit)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EFFECT_CANNOT_BE_SYNCHRO_MATERIAL)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_CANNOT_BE_XYZ_MATERIAL)
  	c:RegisterEffect(e3)
  	local e4 = e2:Clone()
  	e4:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
  	c:RegisterEffect(e4)
  	--add counter
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 0))
  	e5:SetCategory(CATEGORY_COUNTER)
  	e5:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e5:SetCode(EVENT_SUMMON_SUCCESS)
  	e5:SetProperty(EFFECT_FLAG_DELAY)
  	e5:SetOperation(s.countop)
  	c:RegisterEffect(e5)
  	local e6 = e5:Clone()
  	e6:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e6)
  	--control
  	local e7 = Effect.CreateEffect(c)
  	e7:SetDescription(aux.Stringid(id, 1))
  	e7:SetCategory(CATEGORY_CONTROL)
  	e7:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e7:SetCode(EVENT_PHASE + PHASE_END)
  	e7:SetRange(LOCATION_MZONE)
  	e7:SetCountLimit(1)
  	e7:SetCondition(s.ctrcon)
  	e7:SetTarget(s.ctrtg)
  	e7:SetOperation(s.ctrop)
  	c:RegisterEffect(e7)
  	--dice
  	local e8 = Effect.CreateEffect(c)
  	e8:SetDescription(aux.Stringid(id, 2))
  	e8:SetCategory(CATEGORY_DICE + CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e8:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e8:SetCode(EVENT_CONTROL_CHANGED)
  	e8:SetCountLimit(1)
  	e8:SetCondition(s.dicecon)
  	e8:SetTarget(s.dicetg)
  	e8:SetOperation(s.diceop)
  	c:RegisterEffect(e8)
  end
  function s.fuslimit(e, c, sumtype)
  	return sumtype & SUMMON_TYPE_FUSION == SUMMON_TYPE_FUSION
  end
  function s.countop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		c:AddCounter(COUNTER_ID_0, 6)
  	end
  end
  function s.ctrcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.ctrtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_CONTROL, e:GetHandler(), 1, 0, 0)
  end
  function s.ctrop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.GetControl(c, 1 - tp)
  	end
  end
  function s.dicecon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetCounter(COUNTER_ID_0) > 0
  end
  function s.dicetg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.diceop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() and c:IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_EFFECT) then
  		local dc = Duel.TossDice(tp, 1)
  		if dc > c:GetCounter(COUNTER_ID_0) then
  			dc = c:GetCounter(COUNTER_ID_0)
  		end
  		c:RemoveCounter(tp, COUNTER_ID_0, dc, REASON_EFFECT)
  		if c:GetCounter(COUNTER_ID_0) == 0 and Duel.Destroy(c, REASON_EFFECT) ~= 0 then
  			Duel.Damage(tp, 2000, REASON_EFFECT)
  		end
  	end
  end
  
  