--九鼎
local s,id,o=GetID()
function s.initial_effect(c)
    --Activate
    local e1=Effect.CreateEffect(c)
    e1:SetCategory(CATEGORY_EQUIP)
    e1:SetType(EFFECT_TYPE_ACTIVATE)
    e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_CONTINUOUS_TARGET)
    e1:SetTarget(s.target)
    e1:SetOperation(s.operation)
    c:RegisterEffect(e1)
    --immune
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_IMMUNE_EFFECT)
	e2:SetRange(LOCATION_SZONE)
	e2:SetTargetRange(LOCATION_ONFIELD,LOCATION_ONFIELD)
	e2:SetTarget(s.immunetg)
	e2:SetValue(s.immunefilter)
	c:RegisterEffect(e2)
	--Equip limit
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_SINGLE)
	e3:SetCode(EFFECT_EQUIP_LIMIT)
	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e3:SetValue(s.eqlimit)
	c:RegisterEffect(e3)
	--redirect
	local e5=Effect.CreateEffect(c)
	e5:SetType(EFFECT_TYPE_SINGLE)
	e5:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
	e5:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e5:SetCondition(aux.TRUE)
	e5:SetValue(LOCATION_REMOVED)
	c:RegisterEffect(e5)
end

function s.filter(c)
	return c:IsFaceup() and c:IsRace(RACE_WARRIOR)
end
function s.eqlimit(e,c)
	return c:IsRace(RACE_WARRIOR)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE) and s.filter(chkc) end
	if chk==0 then return Duel.IsExistingTarget(s.filter,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_EQUIP)
	Duel.SelectTarget(tp,s.filter,tp,LOCATION_MZONE,LOCATION_MZONE,1,1,nil)
	local c=e:GetHandler()
	local s=c:GetSequence()
	local seq=Duel.SelectField(tp,9,LOCATION_ONFIELD,0,0xe0+(0x100<<s),id)
	e:SetLabel(seq)
	
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) and tc:IsFaceup() then
		local seq=e:GetLabel()
		Duel.Hint(HINT_ZONE,0,seq)
		Duel.Equip(tp,c,tc)
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,1,seq)
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetCode(EFFECT_DISABLE)
		e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
		c:RegisterEffect(e1)
	end
end

function s.immunetg(e,c)
	local sc=e:GetHandler()
	local tzone=sc:GetFlagEffectLabel(id)
	if tzone==nil then return false end
	local zone=s.getzone(c)
	--print(seq,loc,tzone,zone)
	return bit.band(tzone,zone)>0
end
function s.immunefilter(e,te)
	local c=e:GetHandler()
	local tc=c:GetEquipTarget()
	return te:GetOwner()~=c
end

function s.getzone(c)
	local seq=c:GetSequence()
	local loc=c:GetLocation()
	local zone=0
	if loc==LOCATION_MZONE then
		zone=0x1<<seq
	elseif loc==LOCATION_SZONE then
		zone=0x100<<seq
	elseif loc==LOCATION_FZONE then
		zone=0x1000
	elseif loc==LOCATION_PZONE then
		zone=0 -- ????
	end
	return zone
end