--高速世界-JINB
function c87593200.initial_effect(c)
	c:SetUniqueOnField(1,1,87593200)
	c:EnableCounterPermit(0x871)
	c:EnableCounterPermit(0x872)
	--add counter
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(87593200,0))
	e2:SetCategory(CATEGORY_COUNTER)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetRange(LOCATION_FZONE)
	e2:SetCountLimit(1)
	e2:SetCode(EVENT_PHASE+PHASE_STANDBY)
	e2:SetOperation(c87593200.ctop)
	c:RegisterEffect(e2)
	
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(20374520,0))
	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e3:SetRange(LOCATION_FZONE)
	e3:SetCode(EVENT_DAMAGE)
	e3:SetCondition(c87593200.condition)
	e3:SetOperation(c87593200.operation)
	c:RegisterEffect(e3)
	--activate cost
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD)
	e4:SetCode(EFFECT_ACTIVATE_COST)
	e4:SetRange(LOCATION_FZONE)
	e4:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e4:SetTargetRange(1,0)
	e4:SetLabel(0x871)
	e4:SetTarget(c87593200.actarget)
	e4:SetCost(c87593200.costchk)
	e4:SetOperation(c87593200.costop)
	c:RegisterEffect(e4)
	--activate cost
	local e5=e4:Clone()
	e5:SetTargetRange(0,1)
	e5:SetLabel(0x872)
	c:RegisterEffect(e5)
	--cannot activate
	local e6=Effect.CreateEffect(c)
	e6:SetType(EFFECT_TYPE_FIELD)
	e6:SetCode(EFFECT_CANNOT_ACTIVATE)
	e6:SetRange(LOCATION_FZONE)
	e6:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e6:SetTargetRange(1,0)
	e6:SetValue(c87593200.cafilter)
	c:RegisterEffect(e6)
	--SYsummon
	local e7=Effect.CreateEffect(c)
	e7:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e7:SetRange(LOCATION_FZONE)
	e7:SetCode(EVENT_SPSUMMON_SUCCESS)
	e7:SetTarget(c87593200.addct)
	e7:SetCondition(c87593200.drcon)
	e7:SetOperation(c87593200.ctop2)
	c:RegisterEffect(e7)
	--draw
	local e8=Effect.CreateEffect(c)
	e8:SetDescription(aux.Stringid(87593200,1))
	e8:SetCategory(CATEGORY_TOHAND+CATEGORY_SEARCH)
	e8:SetType(EFFECT_TYPE_IGNITION)
	e8:SetRange(LOCATION_FZONE)
	e8:SetCountLimit(1)
	e8:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_BOTH_SIDE)
	e8:SetLabel(6)
	e8:SetCost(c87593200.cost)
	e8:SetTarget(c87593200.drtg)
	e8:SetOperation(c87593200.drop)
	c:RegisterEffect(e8)
	--_DAMAGE
	local e9=Effect.CreateEffect(c)
	e9:SetDescription(aux.Stringid(87593200,0))
	e9:SetCategory(CATEGORY_DAMAGE)
	e9:SetType(EFFECT_TYPE_IGNITION)
	e9:SetRange(LOCATION_FZONE)
	e9:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_BOTH_SIDE)
	e9:SetCountLimit(1)
	e9:SetLabel(3)
	e9:SetCost(c87593200.cost)
	e9:SetTarget(c87593200.datg)
	e9:SetOperation(c87593200.daop)
	c:RegisterEffect(e9)
	--cannot set
	local e10=Effect.CreateEffect(c)
	e10:SetType(EFFECT_TYPE_FIELD)
	e10:SetCode(EFFECT_CANNOT_SSET)
	e10:SetRange(LOCATION_FZONE)
	e10:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e10:SetTargetRange(1,0)
	e10:SetTarget(c87593200.sfilter)
	c:RegisterEffect(e10)
	--add counter
	local e11=Effect.CreateEffect(c)
	e11:SetDescription(aux.Stringid(87593200,2))
	e11:SetCategory(CATEGORY_COUNTER)
	e11:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_BOTH_SIDE)
	e11:SetType(EFFECT_TYPE_IGNITION)
	e11:SetCountLimit(1)
	e11:SetRange(LOCATION_FZONE)
	e11:SetCost(c87593200.ctcost)
	e11:SetTarget(c87593200.cttg)
	e11:SetOperation(c87593200.ctop3)
	c:RegisterEffect(e11)
	
	local e12=Effect.CreateEffect(c)
	e12:SetType(EFFECT_TYPE_SINGLE)
	e12:SetCode(EFFECT_CANNOT_USE_AS_COST)
	e12:SetRange(LOCATION_FZONE)
	e12:SetValue(1)
	c:RegisterEffect(e12)

	--[[local e13=Effect.CreateEffect(c)
	e13:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e13:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e13:SetRange(LOCATION_FZONE)
	e13:SetCountLimit(1)
	e13:SetCondition(c87593200.racon)
	e13:SetOperation(c87593200.raop)
	c:RegisterEffect(e13)]]
end
require("ai.JINB.JINB")
function c87593200.racon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:GetControler()==Duel.GetTurnPlayer() then cou1=0x871 cou2=0x872 else cou1=0x872 cou2=0x871 end
	return c:GetCounter(cou1)>c:GetCounter(cou2)
end
function c87593200.raop(e,tp,eg,ep,ev,re,r,rp)
	local tp=Duel.GetTurnPlayer()
	local dc=Duel.TossDice(tp,1)
	if dc==Duel.GetFieldGroupCount(tp,LOCATION_HAND,0) or dc==Duel.GetFieldGroupCount(tp,0,LOCATION_HAND) then
  	  	j=Group.FromCards(Duel.CreateToken(tp,ra[math.random(#ra)]))
		Duel.SendtoHand(j,tp,REASON_RULE)
	end
end

function c87593200.filter2(c)
	return c:IsSetCard(0x874) and c:IsAbleToGraveAsCost()
end
function c87593200.ctcost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(c87593200.filter2,tp,LOCATION_HAND,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
	local g=Duel.SelectMatchingCard(tp,c87593200.filter2,tp,LOCATION_HAND,0,1,63,nil)
	Duel.SendtoGrave(g,REASON_COST)
	e:SetLabel(g:GetCount())
end
function c87593200.cttg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if c:GetControler()==Duel.GetTurnPlayer() then cou=0x871 else cou=0x872 end
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_COUNTER,nil,e:GetLabel(),0,cou)
end
function c87593200.ctop3(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:GetControler()==Duel.GetTurnPlayer() then cou=0x871 else cou=0x872 end
	local ct=e:GetLabel()
	e:GetHandler():AddCounter(cou,ct)
end

function c87593200.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if c:GetControler()==Duel.GetTurnPlayer() then cou=0x871 else cou=0x872 end
	if chk==0 then return c:IsCanRemoveCounter(ep,cou,e:GetLabel(),REASON_COST) end
	c:RemoveCounter(ep,cou,e:GetLabel(),REASON_COST)
end

function c87593200.datg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(Card.IsSetCard,tp,LOCATION_HAND,0,1,nil,0x874) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CONFIRM)
	local g=Duel.SelectMatchingCard(tp,Card.IsSetCard,tp,LOCATION_HAND,0,1,3,nil,0x874)
	Duel.ConfirmCards(1-tp,g)
	Duel.ShuffleHand(tp)
	Duel.SetTargetPlayer(1-tp)
	Duel.SetTargetParam(g:GetCount()*800)
	Duel.SetOperationInfo(0,CATEGORY_DAMAGE,nil,0,1-tp,g:GetCount()*800)
end
function c87593200.daop(e,tp,eg,ep,ev,re,r,rp)
	local p,d=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER,CHAININFO_TARGET_PARAM)
	Duel.Damage(p,d,REASON_EFFECT)
end

function c87593200.filter(c)
	return c:IsSetCard(0x874) and c:IsAbleToHand()
end
function c87593200.drtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		local g=Duel.GetMatchingGroup(c87593200.filter,tp,LOCATION_DECK,0,nil)
		return g:GetClassCount(Card.GetCode)>=2
	end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,0,LOCATION_DECK)
end
function c87593200.drop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(c87593200.filter,tp,LOCATION_DECK,0,nil)
	if g:GetClassCount(Card.GetCode)>=2 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CONFIRM)
		local sg1=g:Select(tp,1,1,nil)
		g:Remove(Card.IsCode,nil,sg1:GetFirst():GetCode())
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CONFIRM)
		local sg2=g:Select(tp,1,1,nil)
		sg1:Merge(sg2)
		Duel.ConfirmCards(1-tp,sg1)
		Duel.ShuffleDeck(tp)
		Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_ATOHAND)
		local tg=sg1:Select(1-tp,1,1,nil)
		local tc=tg:GetFirst()
		Duel.SendtoHand(tc,nil,REASON_EFFECT)
	end
end

function c87593200.condition(e,tp,eg,ep,ev,re,r,rp)
	return ev>=2000
end
function c87593200.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:GetControler()==ep then cou=0x871 else cou=0x872 end
	if c:IsCanRemoveCounter(ep,cou,1,REASON_EFFECT) then
	c:RemoveCounter(ep,cou,1,REASON_EFFECT)
	end
end

function c87593200.addct(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_COUNTER,nil,1,0,nil)
end
function c87593200.drcon(e,tp,eg,ep,ev,re,r,rp)
	local tg=eg:GetFirst()
	local sm=tg:GetMaterial()
	return eg:GetCount()==1 and tg:GetSummonType()==SUMMON_TYPE_SYNCHRO and sm and not sm:IsExists(c87593200.scfil,1,nil)
end
function c87593200.ctop2(e,tp,eg,ep,ev,re,r,rp)
	local tc=eg:GetFirst()
	local sc=tc:GetMaterialCount()
	if tc:GetControler()==tp then cou=0x871 else cou=0x872 end
		if tc:IsCode(74892653,89474727,24696097) or sc==2 then 
		AI.Chat("聚集的梦想结晶将开启全新的进化之门，成为光芒闪耀的道路吧。加速同调！")
		--"无限的力量，现在在此释放，冲向次元的彼岸吧。加速同调！"
		elseif tc:IsCode(35952884,26268488) or sc>3 then 
		AI.Chat("聚集的群星化为一体时，新的羁绊将照亮未来，化作光芒闪耀的道路吧。超极限加速同调！")
		elseif sc==3 then
		AI.Chat("无限的力量，穿破时空，开启未知的世界吧。三角加速同调！")
		end
		e:GetHandler():AddCounter(cou,sc)
end
function c87593200.scfil(c)
  return not c:IsType(TYPE_SYNCHRO)
end

function c87593200.ctop(e,tp,eg,ep,ev,re,r,rp)
	e:GetHandler():AddCounter(0x871,1)
	e:GetHandler():AddCounter(0x872,1)
end


function c87593200.cafilter(e,re,tp)
	return re:GetHandler():IsType(TYPE_FIELD) and re:IsHasType(EFFECT_TYPE_ACTIVATE)
end
function c87593200.sfilter(e,c,tp)
	return c:IsType(TYPE_FIELD)
end

function c87593200.actarget(e,te,tp)
	return te:GetHandler():IsType(TYPE_SPELL) and te:GetHandler()~=e:GetHandler() and te:IsHasType(EFFECT_TYPE_ACTIVATE) and not te:GetHandler():IsSetCard(0x874)
end
function c87593200.costchk(e,tp)
	return e:GetHandler():IsCanRemoveCounter(tp,e:GetLabel(),2,REASON_COST)
end
function c87593200.costop(e,tp,eg,ep,ev,re,r,rp)
	e:GetHandler():RemoveCounter(ep,e:GetLabel(),2,REASON_COST)
end
