-- 天极辉工 机巧副官
local s,id,o=GetID()
o=1e9
function s.initial_effect(c)
	-- xyz summon & pendulum summon
	aux.AddXyzProcedure(c,nil,1,2)
	aux.EnablePendulumAttribute(c)
	c:EnableReviveLimit()
	-- this card has level
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_SINGLE)
	e0:SetCode(EFFECT_SYNCHRO_LEVEL)
	e0:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e0:SetRange(~0)
	e0:SetValue(1)
	c:RegisterEffect(e0)
	local e0p=e0:Clone()
	e0p:SetCode(EFFECT_XYZ_LEVEL)
	c:RegisterEffect(e0p)
	-- pzone, give xyz level
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(id)
	e1:SetRange(LOCATION_PZONE)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,1)
	c:RegisterEffect(e1)
	-- pzone, don't take effect from Ursarctic Septentrion
	local e1p=Effect.CreateEffect(c)
	e1p:SetType(EFFECT_TYPE_FIELD)
	e1p:SetCode(EFFECT_IMMUNE_EFFECT)
	e1p:SetRange(LOCATION_PZONE)
	e1p:SetTarget(s.xyzfilter)
	e1p:SetValue(s.immunefilter)
	c:RegisterEffect(e1p)
	-- pzone, Ursarctic level down
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_UPDATE_LEVEL)
	e2:SetRange(LOCATION_PZONE)
	e2:SetTargetRange(LOCATION_MZONE,0)
	e2:SetCondition(s.hlvcon)
	e2:SetTarget(s.hlvfilter)
	e2:SetValue(-6)
	c:RegisterEffect(e2)
	-- pzone, Xyz level gain
	-- local e2p=Effect.CreateEffect(c)
	-- e2p:SetType(EFFECT_TYPE_FIELD)
	-- e2p:SetCode(EFFECT_SYNCHRO_LEVEL)
	-- e2p:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	-- e2p:SetRange(LOCATION_PZONE)
	-- e2p:SetTargetRange(LOCATION_MZONE,0)
	-- e2p:SetCondition(s.hlvcon)
	-- e2p:SetTarget(s.xyzfilter)
	-- e2p:SetValue(s.xyzlevel)
	-- c:RegisterEffect(e2)	
	-- pzone, gain atk
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,2))
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	e3:SetRange(LOCATION_PZONE)
	e3:SetCondition(s.atkcon)
	e3:SetOperation(s.atkop)
	c:RegisterEffect(e3)
	-- pzone, pendulum
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_IGNITION)
	e4:SetRange(LOCATION_PZONE)
	e4:SetCountLimit(1)
	e4:SetCondition(s.pencon)
	e4:SetOperation(s.penop)
	c:RegisterEffect(e4)
	-- force move ship
	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_SPSUMMON_SUCCESS)
	e5:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e5:SetOperation(s.ssop)
	c:RegisterEffect(e5)
	-- destroy
	local e6=Effect.CreateEffect(c)
	e6:SetDescription(aux.Stringid(id,1))
	e6:SetCategory(CATEGORY_RELEASE)
	e6:SetCode(EVENT_FREE_CHAIN)
	e6:SetType(EFFECT_TYPE_QUICK_O)
	e6:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e6:SetRange(LOCATION_MZONE)
	e6:SetCondition(s.descon)
	e6:SetCost(s.descost)
	e6:SetOperation(s.desop)
	c:RegisterEffect(e6)
	-- hijack
	if not s.fy then
		s.originRegisterEffect = Card.RegisterEffect
		s.originUrsarcticSpSummonCost = aux.UrsarcticSpSummonCost
		aux.UrsarcticSpSummonCost = s.UrsarcticSpSummonCost
		s.originUrsarcticSpSummonLimit = aux.UrsarcticSpSummonLimit
		aux.UrsarcticSpSummonLimit = s.UrsarcticSpSummonLimit
		s.originDrytronSpSummonCost = aux.DrytronSpSummonCost
		aux.DrytronSpSummonCost = s.DrytronSpSummonCost
		s.fy = true
	end
end

function s.hlvcon(e,tp,eg,ep,ev,re,r,rp)
	return not Duel.IsExistingMatchingCard(Card.IsType,e:GetHandlerPlayer(),LOCATION_MZONE,0,1,nil,TYPE_SYNCHRO)
end
function s.hlvfilter(e,c)
	return c:IsLevelAbove(7) and c:IsSetCard(0x163)
end
function s.xyzfilter(e,c)
	return c:IsType(TYPE_XYZ)
end
function s.xyzlevel(e,c)
end
function s.immunefilter(e,te)
	return te:GetHandler():GetCode()==53087962
end
function s.atkfilter(c,tp)
	return c:IsFaceup() and (c:IsSetCard(0x154) or c:IsSetCard(0x163)) and c:IsControler(tp)
end
function s.atkcon(e,tp,eg,ep,ev,re,r,rp)
	local scale=e:GetHandler():GetLeftScale()
	return eg:IsExists(s.atkfilter,1,nil,tp) and scale>0 and Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_SZONE,0,1,nil,33250142)
end
function s.atkop(e,tp,eg,ep,ev,re,r,rp)
	local g=eg:Filter(s.atkfilter,nil,tp)
	local tc=g:GetFirst()
	while tc do
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetValue(1000)
		e1:SetReset(RESET_EVENT+RESETS_STANDARD)
		tc:RegisterEffect(e1)
		tc=g:GetNext()
	end
	Duel.BreakEffect()
	local c=e:GetHandler()
	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)
	e2=e1:Clone()
	e2:SetCode(EFFECT_UPDATE_RSCALE)
	c:RegisterEffect(e2)
end

function s.penfilter(c,lsc,rsc)
	local lv=c:GetLevel()
	return lv>lsc and lv<rsc and not c:IsPublic() and (c:IsSetCard(0x154) or c:IsSetCard(0x163))
end

function s.pencon(e,tp,eg,ep,ev,re,r,rp)
	local tc1=Duel.GetFieldCard(tp,LOCATION_PZONE,0)
	local tc2=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
	if not tc1 or not tc2 then return false end
	local lsc=Duel.GetFieldCard(tp,LOCATION_PZONE,0):GetLeftScale()
	local rsc=Duel.GetFieldCard(tp,LOCATION_PZONE,1):GetRightScale()
	if lsc>rsc then lsc,rsc=rsc,lsc end
	return Duel.IsExistingMatchingCard(s.penfilter,tp,LOCATION_HAND,0,1,nil,lsc,rsc) and Auxiliary.PendulumChecklist&(0x1<<tp)==0 -- Haven't pedulum summoned
end
function s.penop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local lsc=Duel.GetFieldCard(tp,LOCATION_PZONE,0):GetLeftScale()
	local rsc=Duel.GetFieldCard(tp,LOCATION_PZONE,1):GetRightScale()
	if lsc>rsc then lsc,rsc=rsc,lsc end
	local g=Duel.SelectMatchingCard(tp,s.penfilter,tp,LOCATION_HAND,0,1,999,nil,lsc,rsc)
	if #g>0 then
		local tc=g:GetFirst()
		while tc do
			local fid=tc:GetFieldID()
			tc:RegisterFlagEffect(id+o,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_OPPO_TURN,EFFECT_FLAG_CLIENT_HINT,1,fid,66)
			local e1=Effect.CreateEffect(c)
			e1:SetType(EFFECT_TYPE_SINGLE)
			e1:SetCode(EFFECT_PUBLIC)
			e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_OPPO_TURN)
			tc:RegisterEffect(e1)
			Card.RegisterEffect = s.RegisterEffect2
			_G['c'..tc:GetCode()].initial_effect(tc)
			Card.RegisterEffect = s.originRegisterEffect
			tc=g:GetNext()
		end
	end
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,0)
	e1:SetReset(RESET_PHASE+PHASE_END)
	e1:SetTarget(s.psplimit)
	Duel.RegisterEffect(e1,tp)
end

function s.psplimit(e,c,tp,sumtp,sumpos)
	return not bit.band(sumtp,SUMMON_TYPE_PENDULUM)==SUMMON_TYPE_PENDULUM
end

function s.ssop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local ss=false
	if Duel.GetLocationCount(tp,LOCATION_SZONE)>0 and Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_MZONE,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_MZONE,0,1,1,nil,e,tp,33250142)
		local tc=g:GetFirst()
		if Duel.MoveToField(tc,tp,tp,LOCATION_SZONE,POS_FACEUP,true) then
			ss=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
			Card.RegisterEffect = s.RegisterEffect1
			c33250142.initial_effect(tc)	
			Card.RegisterEffect = s.originRegisterEffect
		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)
	end
end


function s.desfilter(c)
	return Duel.IsPlayerCanRelease(c:GetOwner(),c) and c:IsType(TYPE_MONSTER)
end
function s.descon(e,tp,eg,ep,ev,re,r,rp,chk)
	return Duel.IsExistingMatchingCard(s.desfilter,tp,LOCATION_MZONE+LOCATION_HAND,0,1,nil) and
		Duel.IsExistingMatchingCard(s.desfilter,tp,0,LOCATION_MZONE,1,nil)	
end
function s.descost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():GetOverlayGroup():IsExists(Card.IsSetCard,1,nil,0x163) end
	local tg=e:GetHandler():GetOverlayGroup():FilterSelect(tp,Card.IsSetCard,1,1,nil,0x163)
	Duel.SendtoGrave(tg,REASON_COST)
end
function s.desop(e,tp,eg,ep,ev,re,r,rp)
	local g1=Duel.SelectMatchingCard(tp,s.desfilter,tp,LOCATION_MZONE+LOCATION_HAND,0,1,1,nil)
	local g2=Duel.SelectMatchingCard(tp,s.desfilter,tp,0,LOCATION_MZONE,0,1,1,nil)
	if #g1>0 and #g2>0 then
		g1:Merge(g2)
		Duel.Release(g1,REASON_COST)
	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

function s.RegisterEffect2(c,e)
	e:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_OPPO_TURN)
	e:SetDescription(aux.Stringid(id,3))
	if e:GetCost() == s.DrytronSpSummonCost then
		e:SetRange(LOCATION_HAND)
		s.originRegisterEffect(c,e)
	end
	if e:GetCost() == s.UrsarcticSpSummonCost then
		s.originRegisterEffect(c,e)
	end
end

function s.UrsarcticSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if c:GetFlagEffect(id+o)>0 then
		if chk==0 then return true end
	else
		return s.originUrsarcticSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
	end
end

function s.UrsarcticSpSummonLimit(e,c)
	return s.originUrsarcticSpSummonLimit(e,c) and not c:IsCode(id) and not
		(Duel.IsPlayerAffectedByEffect(c:GetControler(),id) and c:IsType(TYPE_XYZ))
end
function s.DrytronSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if c:GetFlagEffect(id+o)>0 then
		if chk==0 then 
			return Duel.GetCustomActivityCount(97148796,tp,ACTIVITY_SPSUMMON)<=0 
		end 
		e:SetLabel(100)
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
		e1:SetTargetRange(1,0)
		e1:SetTarget(Auxiliary.DrytronSpSummonLimit)
		e1:SetReset(RESET_PHASE+PHASE_END)
		Duel.RegisterEffect(e1,tp)
		local e2=Effect.CreateEffect(e:GetHandler())
		e2:SetType(EFFECT_TYPE_FIELD)
		e2:SetCode(97148796)
		e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
		e2:SetTargetRange(1,0)
		e2:SetReset(RESET_PHASE+PHASE_END)
		Duel.RegisterEffect(e2,tp)
	else
		return s.originDrytronSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
	end
end
