name: Magistus
card_text: Target 1 "Magistus" monster you control; equip it with 1 "Magistus" monster, except a Level 4 monster, from your Extra Deck, GY, or face-up field. If you have at least 1 each "Magistus" Fusion, Synchro, Xyz, and Link Monsters in your GY, you can equip it with 1 non-"Magistus" Fusion, Synchro, Xyz, or Link Monster from your Extra Deck, instead. You can only activate "Magistus Theurgy" once per turn.
external_ids: local SETCARD_ID_0 = 0x150 --Magistus
  
lua: local SETCARD_ID_0 = 0x150 --Magistus
  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)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, tp, check)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.eqfilter, tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE, 0, 1, c, check)
  end
  function s.eqfilter(c, check)
  	return (c:IsFaceup() or not c:IsLocation(LOCATION_MZONE)) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsLevel(4)
  		or check and c:IsLocation(LOCATION_EXTRA) and not c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ + TYPE_LINK)
  end
  function s.gfilter(c, type)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(type)
  end
  function s.check(tp, type)
  	return Duel.IsExistingMatchingCard(s.gfilter, tp, LOCATION_GRAVE, 0, 1, nil, type)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local check = s.check(tp, TYPE_FUSION) and s.check(tp, TYPE_SYNCHRO) and s.check(tp, TYPE_XYZ) and s.check(tp, TYPE_LINK)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc, tp, check)
  	end
  	local b = e:IsHasType(EFFECT_TYPE_ACTIVATE) and not e:GetHandler():IsLocation(LOCATION_SZONE)
  	local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  	if b then
  		ft = ft - 1
  	end
  	if chk == 0 then
  		return ft > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, tp, check)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp, check)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, nil, 1, tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 then
  		local check = s.check(tp, TYPE_FUSION) and s.check(tp, TYPE_SYNCHRO) and s.check(tp, TYPE_XYZ) and s.check(tp, TYPE_LINK)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.eqfilter), tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE, 0, 1, 1, tc, check)
  		local ec = g:GetFirst()
  		if ec then
  			if not Duel.Equip(tp, ec, tc) then
  				return
  			end
  			--equip limit
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetLabelObject(tc)
  			e1:SetValue(s.eqlimit)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			ec:RegisterEffect(e1)
  		end
  	end
  end
  function s.eqlimit(e, c)
  	return c == e:GetLabelObject()
  end
  
  
source_text: Magistus
  Target 1 "Magistus" monster you control; equip it with 1 "Magistus" monster, except a Level 4 monster, from your Extra Deck, GY, or face-up field. If you have at least 1 each "Magistus" Fusion, Synchro, Xyz, and Link Monsters in your GY, you can equip it with 1 non-"Magistus" Fusion, Synchro, Xyz, or Link Monster from your Extra Deck, instead. You can only activate "Magistus Theurgy" once per turn.
target_text: -- Magistus
  local SETCARD_ID_0 = 0x150 --Magistus
  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)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, tp, check)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.eqfilter, tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE, 0, 1, c, check)
  end
  function s.eqfilter(c, check)
  	return (c:IsFaceup() or not c:IsLocation(LOCATION_MZONE)) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsLevel(4)
  		or check and c:IsLocation(LOCATION_EXTRA) and not c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ + TYPE_LINK)
  end
  function s.gfilter(c, type)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(type)
  end
  function s.check(tp, type)
  	return Duel.IsExistingMatchingCard(s.gfilter, tp, LOCATION_GRAVE, 0, 1, nil, type)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local check = s.check(tp, TYPE_FUSION) and s.check(tp, TYPE_SYNCHRO) and s.check(tp, TYPE_XYZ) and s.check(tp, TYPE_LINK)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc, tp, check)
  	end
  	local b = e:IsHasType(EFFECT_TYPE_ACTIVATE) and not e:GetHandler():IsLocation(LOCATION_SZONE)
  	local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  	if b then
  		ft = ft - 1
  	end
  	if chk == 0 then
  		return ft > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, tp, check)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp, check)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, nil, 1, tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 then
  		local check = s.check(tp, TYPE_FUSION) and s.check(tp, TYPE_SYNCHRO) and s.check(tp, TYPE_XYZ) and s.check(tp, TYPE_LINK)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.eqfilter), tp, LOCATION_EXTRA + LOCATION_MZONE + LOCATION_GRAVE, 0, 1, 1, tc, check)
  		local ec = g:GetFirst()
  		if ec then
  			if not Duel.Equip(tp, ec, tc) then
  				return
  			end
  			--equip limit
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e1:SetLabelObject(tc)
  			e1:SetValue(s.eqlimit)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			ec:RegisterEffect(e1)
  		end
  	end
  end
  function s.eqlimit(e, c)
  	return c == e:GetLabelObject()
  end
  
  