-- Experimental: move the real player to a place through the game's own teleport. -- The game's teleporter device (BP_Teleport, DoTeleport) calls -- PBZSaveLoadSubsystem:TeleportTo(NewLevelName, MakeTransform(Location, Rotation, 1), None) -- for a place in the same level (NewLevelName = GetCurrentLevelName) and for one in another level alike; the same -- call, with the same empty bell id, is made here. It brings the game's own loading with it, which a plain location -- write does not (the destination is not streamed in yet and the player falls through it). -- No save call, no bell and no flow state are written here. What the game itself saves after its teleport is the -- game's own behaviour and has not been read back (another teleport mod's notes say the native teleport forces a -- save at the destination); the app tells the user to back the save up first. -- Before the native call a "即将" line goes to the log; afterwards the player's level and position are read back -- for up to READBACK_SECONDS. After the arrival the only other write is the fall watch: a player who drops below -- the target is stopped and put back at it (StopMovementImmediately + K2_TeleportTo), each time announced and read back. local U=require('ue_observe') local Teleport={} Teleport.SUBSYSTEM='PBZSaveLoadSubsystem' Teleport.MAX_COORD=2e7 -- cm; the last arena stands at z = 4.5e6 Teleport.READBACK_SECONDS=40 Teleport.ARRIVED_CM=5000 -- within this of the target horizontally, in the target level, counts as arrived -- The native call reloads the world even for a nearby point on the same map. A new pawn or controller, -- or walking into the target radius, is not evidence that the world reloaded. -- After the arrival the floor may not be streamed in yet (another teleport mod added "fall protection during -- loading" to this same native route). For HOLD_SECONDS the player's height is watched every tick: FALL_CM below the -- target, the movement is stopped and the player is put back at the target, at most MAX_PULLS times. Teleport.HOLD_SECONDS=15 Teleport.FALL_CM=600 Teleport.MAX_PULLS=60 -- 12 seconds of ticks: most of the watch Teleport.TICK_MS=200 local WORLD='^[%a_][%w_]*$' local function finite(v)return type(v)=='number'and v==v and v>-Teleport.MAX_COORD and v, x=, y=, z= (cm), yaw= (degrees, optional), name= (optional)}. function Teleport.check(place) if type(place)~='table'then return false,'地点无效'end if type(place.world)~='string'or #place.world>64 or not place.world:match(WORLD)then return false,'地图名无效'end if not(finite(place.x)and finite(place.y)and finite(place.z))then return false,'坐标无效'end if place.yaw~=nil and not(type(place.yaw)=='number'and place.yaw==place.yaw and place.yaw>=-360 and place.yaw<=360)then return false,'朝向无效'end return true end function Teleport.quat(yaw) local half=math.rad(yaw or 0)/2 return {X=0,Y=0,Z=math.sin(half),W=math.cos(half)} end local function call(o,m,...) if not U.valid(o)then return false end local a,n={...},select('#',...);local ok,value=pcall(function()return o[m](o,table.unpack(a,1,n))end) return ok,U.unwrap(value) end local function num(v)return type(v)=='number'and string.format('%.0f',v)or'?'end local function text(p)return p and(tostring(p.world)..' ('..num(p.x)..','..num(p.y)..','..num(p.z)..')')or'不可读'end function Teleport.new(o) local self={alive=true,last=nil} local pending local find=o.find or FindFirstOf local fname=o.fname or FName local clock=o.clock or os.clock local function say(s)(o.log or function()end)('[Teleport] '..s)end -- Where the real player is now: level name and position (read only). function self:here() local playerRead,player=pcall(o.player) if not playerRead or not U.valid(player)then return nil,'玩家不可读'end local worldRead,world=pcall(o.worldName) if not worldRead or type(world)~='string'or not world:match(WORLD)then return nil,'当前地图名不可读'end local got,location=call(player,'K2_GetActorLocation') local x,y,z=U.struct(location,'X'),U.struct(location,'Y'),U.struct(location,'Z') if not got or not(finite(x)and finite(y)and finite(z))then return nil,'玩家位置不可读'end local _,rotation=call(player,'K2_GetActorRotation') local yaw=U.struct(rotation,'Yaw') return {world=world,x=x,y=y,z=z,yaw=type(yaw)=='number'and yaw==yaw and yaw or 0},nil,U.key(player) end function self:busy()return pending~=nil end local function worldKey() if not o.worldKey then return nil end local ok,value=pcall(o.worldKey) return ok and type(value)=='string' and value~='' and value or nil end local function nearTarget(now,place) local dx,dy=now.x-place.x,now.y-place.y return dx*dx+dy*dy<=Teleport.ARRIVED_CM^2 end -- A fast timer of its own while a teleport is being read back or held; the support loop's tick is the fallback. local timer,lastTick local function stopTimer() local old=timer;timer=nil;lastTick=nil if old and old.handle and o.cancel then pcall(o.cancel,old.handle) end end local function startTimer() if timer~=nil or type(o.loop)~='function'then return end local ticket={} timer=ticket local ok,handle=pcall(o.loop,Teleport.TICK_MS,function() if timer~=ticket or not ticket.handle or not self.alive or not pending then return end local t=clock() if lastTick and t>=lastTick and t-lastTick=.5 end -- Stops the fall and puts the player back at the target; returns whether the position read back near it. local function pull(p,now) local got,player=pcall(o.player) local hold=p.hold if not got or U.key(player)~=hold.pawn or worldKey()~=hold.world then return end hold.pulls=hold.pulls+1 say('即将 防坠拉回 第'..hold.pulls..'次 '..p.label..' 当前高度='..num(now.z)..' 目标高度='..num(p.place.z) ..' StopMovementImmediately + K2_TeleportTo(目标)') local _,movement=call(player,'GetMovementComponent') call(movement,'StopMovementImmediately') if pending~=p or U.key(player)~=hold.pawn or worldKey()~=hold.world then return end local called,result=call(player,'K2_TeleportTo',{X=p.place.x,Y=p.place.y,Z=p.place.z},{Pitch=0,Yaw=p.place.yaw or 0,Roll=0}) local after,_,pawn=self:here() local back=after~=nil and pawn==hold.pawn and worldKey()==hold.world and after.world==p.place.world and nearTarget(after,p.place) and math.abs(after.z-p.place.z)=hold.untilTime or moved if not done and now and pawn and pawn==hold.pawn and observed and observed==hold.world and now.z0 and ('已到达(落下去过,拉回'..hold.recovered..'次)') or '已到达' self.last={ok=not hold.failed,message=message,place=p.label} end return end local waited=clock()-p.started local reloaded=now and (now.world~=p.before.world or p.from and observed and observed~=p.from) if reloaded and not p.reloaded then p.reloaded=true say('地图已重新载入 '..p.label..' 用时='..string.format('%.1f',waited)..'秒 当前='..text(now)) end if pending~=p then return end local destinationLoaded=reloaded and now.world==p.place.world if destinationLoaded then self.back=p.before end if destinationLoaded and nearTarget(now,p.place) then p.hold={untilTime=clock()+Teleport.HOLD_SECONDS,pulls=0,recovered=0,world=observed,pawn=pawn} say('传送读回 到位 '..p.label..' 目标='..text(p.place)..' 当前='..text(now)..' 重新载入='..tostring(p.reloaded) ..' 用时='..string.format('%.1f',waited)..'秒;接下来' ..Teleport.HOLD_SECONDS..'秒观察高度,掉下去就拉回') self.last={ok=true,message='已到达',place=p.label} elseif waited>=Teleport.READBACK_SECONDS then pending=nil;stopTimer() say('传送读回 超时 '..p.label..' 目标='..text(p.place)..' 当前='..text(now)..'('..Teleport.READBACK_SECONDS..'秒内没有读到到位)') self.last={ok=false,message='没有读到到位,当前在 '..text(now),place=p.label} end end function self:relocating()return pending~=nil end function self:status() return {pending=pending~=nil and pending.hold==nil,can_return=self.back~=nil,last=self.last} end function self:silence()self.alive=false;pending=nil;stopTimer()end return self end return Teleport