--PSY骨 架 装 备 补 丁 ·π
local m=114514843
local cm=_G["c"..m]
function cm.initial_effect(c)
	--splimit
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_SINGLE)
	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e0:SetCode(EFFECT_SPSUMMON_CONDITION)
	e0:SetValue(cm.splimit)
	c:RegisterEffect(e0)
	--pos
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_POSITION)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetRange(LOCATION_HAND)
	e1:SetCountLimit(1,m)
	e1:SetCondition(cm.con)
	e1:SetTarget(cm.target)
	e1:SetOperation(cm.operation)
	c:RegisterEffect(e1)
end
function cm.splimit(e,se,sp,st)
	return se:IsHasType(EFFECT_TYPE_ACTIONS)
end
function cm.filter(c,e,tp)
	return c:IsFaceup() and c:IsCanTurnSet() and c:IsSummonPlayer(1-tp) and (not e or c:IsRelateToEffect(e))
end
function cm.actfilter(c,tp)
	return c:IsSetCard(0xc1) and c:IsType(TYPE_SPELL+TYPE_TRAP) and not c:IsForbidden() and (Duel.GetLocationCount(tp,LOCATION_SZONE)>0 or c:IsType(TYPE_FIELD))
end
function cm.confilter(c)
	return not c:IsSetCard(0xc1)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tp=c:GetControler()
	return Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and
	  not  Duel.IsExistingMatchingCard(cm.confilter,tp,LOCATION_MZONE,0,1,nil) and not Duel.IsExistingMatchingCard(Card.IsFacedown,tp,LOCATION_MZONE,0,1,nil) and Duel.GetFieldGroupCount(tp,LOCATION_MZONE,0)>0
end
function cm.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return c:IsDiscardable() end
	Duel.SendtoGrave(c,REASON_COST+REASON_DISCARD)
end
function cm.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return eg:IsExists(cm.filter,1,nil,nil,tp) and Duel.IsExistingMatchingCard(cm.actfilter,tp,LOCATION_DECK,0,1,nil,tp) and Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false) end
	Duel.SetTargetCard(eg)
	Duel.SetOperationInfo(0,CATEGORY_POSITION,eg,eg:GetCount(),0,0)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,e:GetHandler(),1,0,0)
end
function cm.operation(e,tp,eg,ep,ev,re,r,rp)
	local g=eg:Filter(cm.filter,nil,e,tp)
	if Duel.ChangePosition(g,POS_FACEDOWN_DEFENSE)~=0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOFIELD)
		local tc=Duel.SelectMatchingCard(tp,cm.actfilter,tp,LOCATION_DECK,0,1,1,nil,tp):GetFirst()
		if tc then
			if tc:IsType(TYPE_FIELD) then
				local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
				if fc then
					Duel.SendtoGrave(fc,REASON_RULE)
				end
				if Duel.MoveToField(tc,tp,tp,LOCATION_FZONE,POS_FACEUP,true) then
					local c=e:GetHandler()
					local fid=c:GetFieldID()
					Duel.SpecialSummonStep(c,0,tp,tp,false,false,POS_FACEUP)
					c:RegisterFlagEffect(m,RESET_EVENT+RESETS_STANDARD,0,1,fid)
					Duel.SpecialSummonComplete()
					local e1=Effect.CreateEffect(c)
					e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
					e1:SetCode(EVENT_PHASE+PHASE_END)
					e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
					e1:SetCountLimit(1)
					e1:SetLabel(fid)
					e1:SetLabelObject(c)
					e1:SetCondition(cm.rmcon)
					e1:SetOperation(cm.rmop)
					Duel.RegisterEffect(e1,tp)
				end
			else
				if Duel.MoveToField(tc,tp,tp,LOCATION_SZONE,POS_FACEUP,true) then
					local c=e:GetHandler()
					local fid=c:GetFieldID()
					Duel.SpecialSummonStep(c,0,tp,tp,false,false,POS_FACEUP)
					c:RegisterFlagEffect(m,RESET_EVENT+RESETS_STANDARD,0,1,fid)
					Duel.SpecialSummonComplete()
					local e1=Effect.CreateEffect(c)
					e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
					e1:SetCode(EVENT_PHASE+PHASE_END)
					e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
					e1:SetCountLimit(1)
					e1:SetLabel(fid)
					e1:SetLabelObject(c)
					e1:SetCondition(cm.rmcon)
					e1:SetOperation(cm.rmop)
					Duel.RegisterEffect(e1,tp)
				end
			end
		end
	end
end
function cm.rmcon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetLabelObject()
	if not c:GetFlagEffectLabel(m)==fid then
		g:DeleteGroup()
		e:Reset()
		return false
	else return true end
end
function cm.rmop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetLabelObject()
	if not c:GetFlagEffectLabel(m)==fid then
	Duel.Remove(c,POS_FACEUP,REASON_EFFECT) end
end