--圣剑 梅里亚杜克
local s,id,o=GetID()
function s.initial_effect(c)
	if c:GetOriginalCode()==id then
	--adjust
	local e01=Effect.CreateEffect(c)
	e01:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e01:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e01:SetCode(EVENT_ADJUST)
	e01:SetRange(LOCATION_SZONE)
	e01:SetCountLimit(1)
	e01:SetOperation(s.adjustop)
	c:RegisterEffect(e01)
	--reset
	local e02=Effect.CreateEffect(c)
	e02:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e02:SetCode(EVENT_LEAVE_FIELD)
	e02:SetOperation(s.resetop)
	c:RegisterEffect(e02)
	local e03=Effect.CreateEffect(c)
	e03:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e03:SetCode(EVENT_CHAIN_SOLVED)
	e03:SetCondition(s.resetcon)
	e03:SetOperation(s.resetop)
	c:RegisterEffect(e03)
	--
	c:SetUniqueOnField(1,0,id)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_EQUIP)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_CONTINUOUS_TARGET)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--Equip limit
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE)
	e2:SetCode(EFFECT_EQUIP_LIMIT)
	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e2:SetValue(s.eqlimit)
	c:RegisterEffect(e2)
	--get effect
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_EQUIP)
	e3:SetCode(id)
	e3:SetCondition(s.efcon)
	c:RegisterEffect(e3)
	--equip
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,0))
	e4:SetCategory(CATEGORY_EQUIP)
	e4:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e4:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e4:SetCode(EVENT_TO_GRAVE)
	e4:SetCountLimit(1,id)
	e4:SetCondition(s.eqcon)
	e4:SetTarget(s.eqtg)
	e4:SetOperation(s.operation2)
	c:RegisterEffect(e4)
	end
end
function s.eqlimit(e,c)
	return c:IsRace(RACE_WARRIOR)
end
function s.eqfilter1(c)
	return c:IsFaceup() and 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.eqfilter1(chkc) end
	if chk==0 then return Duel.IsExistingTarget(s.eqfilter1,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_EQUIP)
	Duel.SelectTarget(tp,s.eqfilter1,tp,LOCATION_MZONE,LOCATION_MZONE,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_EQUIP,e:GetHandler(),1,0,0)
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if tc and c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) and tc:IsFaceup() and c:CheckUniqueOnField(tp) then
		Duel.Equip(tp,c,tc)
	end
end
function s.efcon(e,c)
	local tc=e:GetHandler():GetEquipTarget()
	return tc:IsSetCard(0x107a) 
end
function s.actarget(e,te,tp)
	--prevent activating
	local tc=te:GetHandler()
	return (te:GetValue()==id and not tc:IsHasEffect(id))  
	--prevent normal activating beside S&T on field
	or (te:GetValue()==id+1 and tc:IsHasEffect(id)) 
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsSetCard(0x107a) 
end
function s.adjustop(e,tp,eg,ep,ev,re,r,rp)
	if not s.globle_check then
		local c=e:GetHandler()
		--
		s.globle_check=true
		local c=e:GetHandler()
		local ge0=Effect.CreateEffect(e:GetHandler())
		ge0:SetType(EFFECT_TYPE_FIELD)
		ge0:SetCode(EFFECT_ACTIVATE_COST)
		ge0:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_CANNOT_DISABLE)
		ge0:SetCost(aux.FALSE)
		ge0:SetTargetRange(1,1)
		ge0:SetTarget(s.actarget)
		Duel.RegisterEffect(ge0,0)
		cregister=Card.RegisterEffect
		table_effect={}
		Card.RegisterEffect=function(card,effect,flag)
			if effect then
				local eff=effect:Clone()
				local con=eff:GetCondition()
				if effect:IsHasType(EFFECT_TYPE_IGNITION) 
				   or (con and effect:IsHasType(EFFECT_TYPE_QUICK_O) and effect:GetCode()==EVENT_FREE_CHAIN) then
					eff:SetValue(id+1)
					--effect edit
					local eff2=effect:Clone()
					--spell speed 2
					if eff2:IsHasType(EFFECT_TYPE_IGNITION) then
						eff2:SetType(EFFECT_TYPE_QUICK_O)
						eff2:SetCode(EVENT_FREE_CHAIN)
						eff2:SetHintTiming(TIMING_DRAW_PHASE+TIMING_END_PHASE+TIMINGS_CHECK_MONSTER,TIMING_DRAW_PHASE+TIMING_END_PHASE+TIMINGS_CHECK_MONSTER)
					end
					--spell speed 2
					if con and eff2:IsHasType(EFFECT_TYPE_QUICK_O) and eff2:GetCode()==EVENT_FREE_CHAIN then
						eff2:SetCondition(
						function(e,tp,eg,ep,ev,re,r,rp)
							Infinitrack_GetCurrentPhase=Duel.GetCurrentPhase
							Duel.GetCurrentPhase=function() return PHASE_MAIN1 end
							local Infinitrack_boolean=con(e,tp,eg,ep,ev,re,r,rp)
							Duel.GetCurrentPhase=Infinitrack_GetCurrentPhase
							return Infinitrack_boolean
						end)
						eff2:SetHintTiming(TIMING_DRAW_PHASE+TIMING_END_PHASE+TIMINGS_CHECK_MONSTER,TIMING_DRAW_PHASE+TIMING_END_PHASE+TIMINGS_CHECK_MONSTER)
					end
					eff2:SetValue(id)
					table.insert(table_effect,eff2)
				end
				table.insert(table_effect,eff)
			end
			return 
		end
		local tc=e:GetHandler():GetEquipTarget()
		table_effect={}
		tc:ReplaceEffect(id,0)
		Duel.CreateToken(0,tc:GetOriginalCode())
		for key,eff in ipairs(table_effect) do
			cregister(tc,eff)
		end
		Card.RegisterEffect=cregister
	end
end
function s.resetop(e,tp,eg,ep,ev,re,r,rp)
	s.globle_check=false
end
function s.resetcon(e,tp,eg,ep,ev,re,r,rp)
	return re:GetHandler():IsCode(30575681) and e:GetHandler():IsHasEffect(30575681)
end
function s.eqcon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousPosition(POS_FACEUP) and c:IsReason(REASON_DESTROY) and c:CheckUniqueOnField(tp)
end
function s.eqfilter2(c)
	return c:IsFaceup() and c:IsSetCard(0x107a) and c:IsRace(RACE_WARRIOR)
end
function s.eqtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.eqfilter2(chkc) end
	if chk==0 then return e:GetHandler():IsRelateToEffect(e) and Duel.GetLocationCount(tp,LOCATION_SZONE)>0
		and Duel.IsExistingTarget(s.eqfilter2,tp,LOCATION_MZONE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_EQUIP)
	Duel.SelectTarget(tp,s.eqfilter2,tp,LOCATION_MZONE,0,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_EQUIP,e:GetHandler(),1,0,0)
	Duel.SetOperationInfo(0,CATEGORY_LEAVE_GRAVE,e:GetHandler(),1,0,0)
end
function s.operation2(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if tc and c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) and tc:IsFaceup()
		and tc:IsControler(tp) and tc:IsSetCard(0x107a) and s.eqlimit(nil,tc) and c:CheckUniqueOnField(tp) then
		Duel.Equip(tp,c,tc)
	end
end