-- 天极辉星 巨熊舰长
local s,id,o=GetID()
function s.initial_effect(c)
	-- pendulum summon
	aux.EnablePendulumAttribute(c,false)
	-- add ritual type
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(1160)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetRange(LOCATION_HAND)
	e1:SetOperation(s.scaleop)
	c:RegisterEffect(e1)
	local e1p=Effect.CreateEffect(c)
	e1p:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_SINGLE)
	e1p:SetCode(EVENT_LEAVE_FIELD)
	e1p:SetRange(LOCATION_PZONE)
	e1p:SetOperation(s.leaveop)
	c:RegisterEffect(e1p)
	Duel.AddCustomActivityCounter(id,ACTIVITY_SPSUMMON,aux.NOT(s.isLegalUrsarcticCard))
    -- level up
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_UPDATE_LEVEL)
	e2:SetRange(LOCATION_PZONE)
	e2:SetTargetRange(LOCATION_HAND,0)
	e2:SetTarget(s.hlvfilter)
	e2:SetValue(6)
	c:RegisterEffect(e2)
	-- call ship
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_IGNITION)
	e3:SetRange(LOCATION_PZONE)
	e3:SetCountLimit(1)
	e3:SetCost(s.cscost)
	e3:SetCondition(s.cscon)
	e3:SetOperation(s.csop)
	c:RegisterEffect(e3)
	
	-- force special summon
	local e5=Effect.CreateEffect(c)
	e5:SetDescription(aux.Stringid(id,0))
	e5:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e5:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_F)
	e5:SetCode(EVENT_SUMMON_SUCCESS)
	e5:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e5:SetOperation(s.ssop)
	c:RegisterEffect(e5)
	local e5p=e5:Clone()
	e5p:SetCode(EVENT_SPSUMMON_SUCCESS)
	c:RegisterEffect(e5p)
	-- destroy
	local e6=Effect.CreateEffect(c)
	e6:SetDescription(aux.Stringid(id,1))
	e6:SetCategory(CATEGORY_DESTROY)
	e6:SetCode(EVENT_FREE_CHAIN)
	e6:SetType(EFFECT_TYPE_IGNITION)
	e6:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e6:SetRange(LOCATION_MZONE)
	e6:SetCost(s.descost)
	e6:SetTarget(s.destg)
	e6:SetOperation(s.desop)
	c:RegisterEffect(e6)
end
-- Lua is evil and the one who write these sh&t must be devil.
function s.isLegalUrsarcticCard(c)
	return c:IsType(TYPE_MONSTER) and c:IsSetCard(0x163) and Duel.GetFlagEffectLabel(0,id)~=nil
end
function s.DrytronSpSummonLimit(e,c,sump,sumtype,sumpos,targetp,se)
	return s.originDrytronSpSummonLimit(e,c,sump,sumtype,sumpos,targetp,se) and not s.isLegalUrsarcticCard(c)
end
function s.DrytronCostFilter(c,tp)
	return (c:IsSetCard(0x154) or (c:IsSetCard(0x163) and Duel.GetFlagEffectLabel(0,id)~=nil) or c:IsType(TYPE_RITUAL)) 
		and c:IsType(TYPE_MONSTER) and Duel.GetMZoneCount(tp,c)>0
		and (c:IsControler(tp) or c:IsFaceup())
end
function s.GetCustomActivityCount(oid,tp,act)
	if oid==97148796 and act==ACTIVITY_SPSUMMON and Duel.GetFlagEffectLabel(0,id)~=nil then
		return math.max(0,s.originGetCustomActivityCount(oid,tp,act)-s.originGetCustomActivityCount(id,tp,act))
	end
return s.originGetCustomActivityCount(oid,tp,act) 
end
function s.scaleop(e,tp,eg,ep,ev,re,r,rp)
	local count=Duel.GetFlagEffectLabel(0,id)
	if count~=nil then
		Duel.SetFlagEffectLabel(0,id,count+1)
	else
		Duel.RegisterFlagEffect(0,id,nil,0,1,1)
	end
	if not s.dry then
		s.originDrytronSpSummonLimit = aux.DrytronSpSummonLimit
		aux.DrytronSpSummonLimit = s.DrytronSpSummonLimit
		s.originDrytronCostFilter = aux.DrytronCostFilter
		aux.DrytronCostFilter = s.DrytronCostFilter
		s.originGetCustomActivityCount = Duel.GetCustomActivityCount
		Duel.GetCustomActivityCount = s.GetCustomActivityCount
		
		-- 右枢可以拿北极天熊
		if c97148796 then
			c97148796.thfilter = function(c)
				return c:IsAbleToHand() and ((c:IsType(TYPE_RITUAL) and c:IsType(TYPE_MONSTER)) or (Duel.GetFlagEffectLabel(0,id) and c:IsSetCard(0x163) and c:IsType(TYPE_MONSTER) ))
			end
		end
		-- 天厨一可以展示北极天熊
		if c22420202 then
			c22420202.drfilter = function(c)
				return (c:IsType(TYPE_RITUAL) and c:IsType(TYPE_MONSTER+TYPE_SPELL) and not c:IsPublic()) or (Duel.GetFlagEffectLabel(0,id) and c:IsSetCard(0x163) and c:IsType(TYPE_MONSTER) )
			end
		end
		--扶筐增二在北极天熊在场时可以康
		if c1174075 then
			c1174075.cfilter = function(c)
				return c:IsFaceup() and c:IsRace(RACE_MACHINE) and (c:IsType(TYPE_RITUAL) or (Duel.GetFlagEffectLabel(0,id) and c:IsSetCard(0x163) and c:IsType(TYPE_MONSTER) ))
			end
		end
		s.dry = true
	end
end
function s.leaveop(e,tp,eg,ep,ev,re,r,rp)
	local count=Duel.GetFlagEffectLabel(0,id)
	if count<=1 then
		Duel.ResetFlagEffect(0,id)
	else
		Duel.SetFlagEffectLabel(0,id,count-1)
	end
end

function s.hlvfilter(e,c)
	return c:IsLevelAbove(1) and c:IsLevelBelow(6) and c:IsSetCard(0x154)
end

function s.cscon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetLocationCount(tp,LOCATION_SZONE)>0
end
function s.cscost(e,tp,eg,ep,ev,re,r,rp,chk)
	local g=Duel.GetMatchingGroup(Card.IsCode,tp,LOCATION_EXTRA,0,nil,33250142)
	if chk==0 then return #g>=3 end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local cg=g:Select(tp,2,2,nil)
	Duel.Remove(cg,POS_FACEUP,REASON_EFFECT)
end

function s.csop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOFIELD)
	local g=Duel.SelectMatchingCard(tp,Card.IsCode,tp,LOCATION_EXTRA,0,1,1,nil,33250142)
	if #g==0 then return end
	local c=e:GetHandler()
	local tc=g:GetFirst()
	Duel.MoveToField(tc,tp,tp,LOCATION_SZONE,POS_FACEUP,true)
	local te0=Effect.CreateEffect(c)
	te0:SetCode(EFFECT_CHANGE_TYPE)
	te0:SetType(EFFECT_TYPE_SINGLE)
	te0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	te0:SetReset(RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET)
	te0:SetValue(TYPE_TRAP+TYPE_CONTINUOUS)
	tc:RegisterEffect(te0)
	-- gain effect
	s.originRegisterEffect = Card.RegisterEffect
	Card.RegisterEffect = s.RegisterEffect1
	c33250142.initial_effect(tc)	
	Card.RegisterEffect = s.originRegisterEffect
	-- scale up
	Duel.BreakEffect()
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_UPDATE_LSCALE)
	e1:SetValue(1)
	e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_DISABLE)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EFFECT_UPDATE_RSCALE)
	c:RegisterEffect(e2)
end

function s.ssop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local ss=false
	if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_SZONE,0,1,nil,33250142) then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		-- special summon starts here
		local g=Duel.SelectMatchingCard(tp,Card.IsCode,tp,LOCATION_SZONE,0,1,1,nil,e,tp,33250142)
		local tc=g:GetFirst()
		if Duel.SpecialSummon(tc,0,tp,tp,true,true,POS_FACEUP)~=0 then
			ss=true
			-- destroy starts here
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
			local g=Duel.SelectMatchingCard(tp,nil,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,nil)
			if g:GetCount()~=0 then
				Duel.HintSelection(g)
				Duel.Destroy(g,REASON_EFFECT)
			end
		end
	end
	-- to pen zone
	if not ss and (Duel.CheckLocation(tp,LOCATION_PZONE,0) or Duel.CheckLocation(tp,LOCATION_PZONE,1)) then
		Duel.MoveToField(c,tp,tp,LOCATION_PZONE,POS_FACEUP,true)
		-- go to pzone so gain effect
		s.scaleop(e,tp,eg,ep,ev,re,r,rp)
	end
end

function s.desfilter(c)
	return (c:IsType(TYPE_RITUAL) or c:IsType(TYPE_SYNCHRO)) and c:IsType(TYPE_MONSTER)
end
function s.descost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.desfilter,tp,LOCATION_GRAVE+LOCATION_MZONE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,s.desfilter,tp,LOCATION_GRAVE+LOCATION_MZONE,0,1,1,nil)
	Duel.Remove(g,POS_FACEUP,REASON_COST)
end
function s.destg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsOnField() end
	if chk==0 then return Duel.IsExistingTarget(aux.TRUE,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectTarget(tp,aux.TRUE,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
end
function s.desop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc and tc:IsRelateToEffect(e) then
		Duel.Destroy(tc,REASON_EFFECT)
	end
end

function s.RegisterEffect1(c,e)
	e:SetReset(RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET)
	e:SetRange(LOCATION_SZONE)
	s.originRegisterEffect(c,e)
end
