-- Shut up! 
local s,id,o=GetID()
function s.initial_effect(c)
    -- special summon
	local e1=Effect.CreateEffect(c) 
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCondition(s.condition)
	e1:SetTarget(s.target)
	e1:SetCost(s.cost)
	e1:SetOperation(s.op)
    c:RegisterEffect(e1)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetCurrentPhase()==PHASE_MAIN1 and not Duel.CheckPhaseActivity() and Duel.IsAbleToEnterBP()
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	local bc=e:GetLabelObject()
	if chk==0 then return Duel.CheckLPCost(tp,100,true) 
		and Duel.IsPlayerCanSpecialSummonMonster(tp,id+1,0,TYPES_TOKEN_MONSTER,0,0,3,RACE_WARRIOR,ATTRIBUTE_EARTH) end
	local maxc=Duel.GetLP(tp)
	if maxc>25500 then maxc=25500 end
	maxc=math.floor(maxc/100)*100
	local t={}
	for i=1,maxc/100 do
		t[i]=i*100
	end
	local cost=Duel.AnnounceNumber(tp,table.unpack(t))
	Duel.PayLPCost(tp,cost,true)
	e:SetLabel(cost)
	-- only token can attack
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_CANNOT_ATTACK_ANNOUNCE)
	e1:SetTargetRange(LOCATION_MZONE,0)
	e1:SetReset(RESET_PHASE+PHASE_END)
	e1:SetTarget(s.antarget)
	Duel.RegisterEffect(e1,tp)
end
function s.antarget(e,c)
	return not c:IsType(TYPE_TOKEN)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chk==0 then return true end
	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
		Duel.SetChainLimit(aux.FALSE)
	end
end
function s.op(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0
		or not Duel.IsPlayerCanSpecialSummonMonster(tp,id+1,0,TYPES_TOKEN_MONSTER,0,0,3,RACE_WARRIOR,ATTRIBUTE_EARTH) then return end
	local c=e:GetHandler()

	-- silent until get into Battle step.
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetCode(EFFECT_CANNOT_ACTIVATE)
	e1:SetReset(RESET_PHASE+PHASE_BATTLE_START)
	e1:SetTargetRange(1,1)
	e1:SetValue(1)
	Duel.RegisterEffect(e1,tp)

	-- can't goto ep, must bp
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetCode(EFFECT_CANNOT_EP)
	e2:SetTargetRange(1,0)
	e2:SetReset(RESET_PHASE+PHASE_MAIN2+PHASE_BATTLE+RESET_SELF_TURN+RESET_OPPO_TURN)
	Duel.RegisterEffect(e2,tp)

	-- Create token
	local token=Duel.CreateToken(tp,id+1)
	Duel.SpecialSummon(token,0,tp,tp,false,false,POS_FACEUP_ATTACK)
	s.initial_token_effect(e,tp,token)
	Duel.BreakEffect()
	Duel.SkipPhase(Duel.GetTurnPlayer(),PHASE_MAIN1,RESET_PHASE+PHASE_END,1)
end

function s.disop(e)
	return function(e2,...)
		e:Reset()
		e2:Reset()
	end
end

function s.initial_token_effect(e,tp,token)
	local c=e:GetHandler()
	-- atk
	local te0=Effect.CreateEffect(c)
	te0:SetType(EFFECT_TYPE_SINGLE)
	te0:SetCode(EFFECT_SET_ATTACK)
	te0:SetValue(e:GetLabel())
	te0:SetReset(RESET_EVENT+RESETS_STANDARD-RESET_TOFIELD)
	token:RegisterEffect(te0)
	-- can't release
	local te1=Effect.CreateEffect(c)
	te1:SetType(EFFECT_TYPE_SINGLE)
	te1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	te1:SetRange(LOCATION_MZONE)
	te1:SetCode(EFFECT_UNRELEASABLE_SUM)
	te1:SetValue(1)
	token:RegisterEffect(te1)
	-- cannot be material
	local te2=te1:Clone()
	te2:SetCode(EFFECT_UNRELEASABLE_NONSUM)
	token:RegisterEffect(te2)
	local te3=te1:Clone()
	te3:SetCode(EFFECT_CANNOT_BE_FUSION_MATERIAL)
	te3:SetValue(aux.TRUE)
	token:RegisterEffect(te3)
	local te4=te1:Clone()
	te4:SetCode(EFFECT_CANNOT_BE_SYNCHRO_MATERIAL)
	token:RegisterEffect(te4)
	local te5=te1:Clone()
	te5:SetCode(EFFECT_CANNOT_BE_XYZ_MATERIAL)
	token:RegisterEffect(te5)
	local te6=te1:Clone()
	te6:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
	token:RegisterEffect(te6)
	-- must attack
	local te7=Effect.CreateEffect(c)
	te7:SetType(EFFECT_TYPE_SINGLE)
	te7:SetCode(EFFECT_MUST_ATTACK)
	te7:SetReset(RESET_EVENT+RESETS_STANDARD)
	token:RegisterEffect(te7)
	-- shut up! 
	local te8=Effect.CreateEffect(c)
	te8:SetType(EFFECT_TYPE_FIELD)
	te8:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	te8:SetCode(EFFECT_CANNOT_ACTIVATE)
	te8:SetRange(LOCATION_MZONE)
	te8:SetTargetRange(1,1)
	te8:SetValue(1)
	te8:SetCondition(s.shutupcon)
	token:RegisterEffect(te8)
	-- chain attack
	local te9=Effect.CreateEffect(c)
	te9:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	te9:SetCode(EVENT_BATTLE_DESTROYING)
	te9:SetCondition(s.chainacon)
	te9:SetOperation(s.chainaop)
	te9:SetReset(RESET_EVENT+RESETS_STANDARD)
	te9:SetLabelObject(token)
	--token:RegisterEffect(te9)
	Duel.RegisterEffect(te9,tp)
	local te10=Effect.CreateEffect(c)
	te10:SetType(EFFECT_TYPE_FIELD)
	te10:SetRange(LOCATION_MZONE)
	te10:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	te10:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	te10:SetTargetRange(1,0)
	token:RegisterEffect(te10)
end

function s.shutupcon(e)
	return Duel.GetAttacker()==e:GetHandler() or Duel.GetAttackTarget()==e:GetHandler()
end
function s.chainacon(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetLabelObject()
	local bc=tc:GetBattleTarget()
	return tc==eg:GetFirst() and tc:IsRelateToBattle() and bc and bc:IsPreviousControler(1-tp) and tc:IsChainAttackable(0)
end
function s.chainaop(e,tp,eg,ep,ev,re,r,rp)
	if Duel.CheckLPCost(tp,2000) and Duel.SelectYesNo(tp,aux.Stringid(id,0)) then 
		Duel.PayLPCost(tp,2000)
		Duel.ChainAttack()
	end
end
