name: 宝玉の樹
card_text: 1:このカードが魔法&罠ゾーンに存在する限り、「宝玉獣」モンスターカードが魔法&罠ゾーンに置かれる度に、このカードにジェムカウンターを1つ置く。
  2:ジェムカウンターが置かれているこのカードを墓地へ送って発動できる。
  このカードに置かれていたジェムカウンターの数だけ、デッキから「宝玉獣」モンスターを選んで永続魔法カード扱いで自分の魔法&罠ゾーンに表側表示で置く。
external_ids: local SETCARD_ID_0 = 0x1034 --宝玉獣
  local COUNTER_ID_0 = 0x6 --ジェムカウンター
  
lua: local SETCARD_ID_0 = 0x1034 --宝玉獣
  local COUNTER_ID_0 = 0x6 --ジェムカウンター
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x6)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_MOVE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.ctcon1)
  	e2:SetOperation(s.ctop1)
  	c:RegisterEffect(e2)
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e0:SetCode(EVENT_MOVE)
  	e0:SetRange(LOCATION_SZONE)
  	e0:SetCondition(s.regcon)
  	e0:SetOperation(s.regop)
  	c:RegisterEffect(e0)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EVENT_CHAIN_SOLVED)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.ctcon2)
  	e3:SetOperation(s.ctop2)
  	c:RegisterEffect(e3)
  	--place
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetType(EFFECT_TYPE_IGNITION)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCost(s.plcost)
  	e4:SetTarget(s.pltg)
  	e4:SetOperation(s.plop)
  	c:RegisterEffect(e4)
  end
  function s.cfilter(c)
  	local type = c:GetOriginalType()
  	if c:IsPreviousLocation(LOCATION_ONFIELD) then
  		type = c:GetPreviousTypeOnField()
  	end
  	return c:IsLocation(LOCATION_SZONE) and c:GetSequence() < 5 and c:IsSetCard(SETCARD_ID_0) and bit.band(type, TYPE_MONSTER) ~= 0
  end
  function s.ctcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil) and not Duel.IsChainSolving()
  end
  function s.ctop1(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil) and Duel.IsChainSolving()
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_CHAIN, 0, 1)
  end
  function s.ctcon2(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.ctop2(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():ResetFlagEffect(id)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.plcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	e:SetLabel(e:GetHandler():GetCounter(COUNTER_ID_0))
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.pltg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local c = e:GetHandler()
  		local ct = c:GetCounter(COUNTER_ID_0)
  		return ct > 0 and Duel.GetSZoneCount(tp, c) >= ct and Duel.IsExistingMatchingCard(s.plfilter, tp, LOCATION_DECK, 0, ct, nil)
  	end
  end
  function s.plfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsForbidden()
  end
  function s.plop(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  	if ft < e:GetLabel() then
  		return
  	end
  	if ft > e:GetLabel() then
  		ft = e:GetLabel()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
  	local g = Duel.SelectMatchingCard(tp, s.plfilter, tp, LOCATION_DECK, 0, ft, ft, nil)
  	if g:GetCount() > 0 then
  		local tc = g:GetFirst()
  		while tc do
  			Duel.MoveToField(tc, tp, tp, LOCATION_SZONE, POS_FACEUP, true)
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetCode(EFFECT_CHANGE_TYPE)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET)
  			e1:SetValue(TYPE_SPELL + TYPE_CONTINUOUS)
  			tc:RegisterEffect(e1)
  			tc = g:GetNext()
  		end
  	end
  end
  
  
source_text: 宝玉の樹
  1:このカードが魔法&罠ゾーンに存在する限り、「宝玉獣」モンスターカードが魔法&罠ゾーンに置かれる度に、このカードにジェムカウンターを1つ置く。
  2:ジェムカウンターが置かれているこのカードを墓地へ送って発動できる。
  このカードに置かれていたジェムカウンターの数だけ、デッキから「宝玉獣」モンスターを選んで永続魔法カード扱いで自分の魔法&罠ゾーンに表側表示で置く。
target_text: -- 宝玉の樹
  local SETCARD_ID_0 = 0x1034 --宝玉獣
  local COUNTER_ID_0 = 0x6 --ジェムカウンター
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x6)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_MOVE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.ctcon1)
  	e2:SetOperation(s.ctop1)
  	c:RegisterEffect(e2)
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e0:SetCode(EVENT_MOVE)
  	e0:SetRange(LOCATION_SZONE)
  	e0:SetCondition(s.regcon)
  	e0:SetOperation(s.regop)
  	c:RegisterEffect(e0)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EVENT_CHAIN_SOLVED)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.ctcon2)
  	e3:SetOperation(s.ctop2)
  	c:RegisterEffect(e3)
  	--place
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetType(EFFECT_TYPE_IGNITION)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCost(s.plcost)
  	e4:SetTarget(s.pltg)
  	e4:SetOperation(s.plop)
  	c:RegisterEffect(e4)
  end
  function s.cfilter(c)
  	local type = c:GetOriginalType()
  	if c:IsPreviousLocation(LOCATION_ONFIELD) then
  		type = c:GetPreviousTypeOnField()
  	end
  	return c:IsLocation(LOCATION_SZONE) and c:GetSequence() < 5 and c:IsSetCard(SETCARD_ID_0) and bit.band(type, TYPE_MONSTER) ~= 0
  end
  function s.ctcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil) and not Duel.IsChainSolving()
  end
  function s.ctop1(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil) and Duel.IsChainSolving()
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_CHAIN, 0, 1)
  end
  function s.ctcon2(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.ctop2(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():ResetFlagEffect(id)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.plcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	e:SetLabel(e:GetHandler():GetCounter(COUNTER_ID_0))
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.pltg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local c = e:GetHandler()
  		local ct = c:GetCounter(COUNTER_ID_0)
  		return ct > 0 and Duel.GetSZoneCount(tp, c) >= ct and Duel.IsExistingMatchingCard(s.plfilter, tp, LOCATION_DECK, 0, ct, nil)
  	end
  end
  function s.plfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsForbidden()
  end
  function s.plop(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  	if ft < e:GetLabel() then
  		return
  	end
  	if ft > e:GetLabel() then
  		ft = e:GetLabel()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
  	local g = Duel.SelectMatchingCard(tp, s.plfilter, tp, LOCATION_DECK, 0, ft, ft, nil)
  	if g:GetCount() > 0 then
  		local tc = g:GetFirst()
  		while tc do
  			Duel.MoveToField(tc, tp, tp, LOCATION_SZONE, POS_FACEUP, true)
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetCode(EFFECT_CHANGE_TYPE)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET)
  			e1:SetValue(TYPE_SPELL + TYPE_CONTINUOUS)
  			tc:RegisterEffect(e1)
  			tc = g:GetNext()
  		end
  	end
  end
  
  