XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
using System;
using Newtonsoft.Json.Linq;
using Sandbox.Engine.Utils;
using Sandbox.Game.Entities;
using Sandbox.Game.World;
using VRage.Game;
using VRage.Game.ModAPI.Interfaces;
using VRageMath;

namespace XFE.SeAgent.Plugin.Game
{
    public sealed partial class GameDebugApi
    {
        private MySession cameraOverrideSession;
        private IMyCameraController savedCameraController;
        private MatrixD savedSpectatorView;
        private bool savedCockpitFirstPerson;
        private float savedCockpitPitch,savedCockpitYaw;

        private JObject CameraView(JObject args)
        {
            foreach(var property in args.Properties()) if(property.Name!="position" && property.Name!="target" && property.Name!="up" && property.Name!="restore" && property.Name!="look" && property.Name!="firstPerson") throw new ArgumentException("Unknown camera option: "+property.Name);
            if(args["firstPerson"]!=null) {
                if(args.Count!=1 || args["firstPerson"].Type!=JTokenType.Boolean)throw new ArgumentException("firstPerson requires a boolean and cannot be combined with other camera options.");
                RequireWritableWorld();
                var cockpit=MySession.Static.CameraController as MyCockpit;
                if(cockpit==null)throw new InvalidOperationException("firstPerson requires the actual cockpit camera controller.");
                cockpit.IsInFirstPersonView=(bool)args["firstPerson"];
            }
            if(args["look"]!=null) {
                if(args.Count!=1)throw new ArgumentException("look cannot be combined with a spectator pose or restore.");
                RequireWritableWorld();
                var cockpit=MySession.Static.CameraController as MyCockpit;
                if(cockpit==null || !(cockpit.IsInFirstPersonView || cockpit.ForceFirstPersonCamera))throw new InvalidOperationException("look requires the actual first-person cockpit camera controller.");
                if(!(args["look"] is JObject look) || look.Count!=2 || look["pitch"]==null || look["yaw"]==null)throw new ArgumentException("look requires pitch and yaw in degrees.");
                foreach(var value in look.Properties())if(value.Value.Type!=JTokenType.Float && value.Value.Type!=JTokenType.Integer)throw new ArgumentException("look angles must be numbers.");
                double pitch=(double)look["pitch"],yaw=(double)look["yaw"];
                if(double.IsNaN(pitch) || double.IsNaN(yaw) || pitch< -60 || pitch>70 || yaw< -90 || yaw>90)throw new ArgumentException("look pitch -60..70, yaw -90..90 degrees.");
                cockpit.HeadLocalXAngle=(float)pitch;cockpit.HeadLocalYAngle=(float)yaw;
            }
            bool restore=(bool?)args["restore"]??false;
            if(restore && (args["position"]!=null || args["target"]!=null || args["up"]!=null)) throw new ArgumentException("restore cannot be combined with a pose.");
            if(restore) {
                RequireWritableWorld();
                if(cameraOverrideSession!=MySession.Static || savedCameraController==null) throw new InvalidOperationException("No camera override exists in this world.");
                if(savedCameraController.Entity!=null && savedCameraController.Entity.Closed) throw new InvalidOperationException("Original camera entity was closed.");
                MySpectatorCameraController.Static.SetViewMatrix(savedSpectatorView);
                MySession.Static.CameraController=savedCameraController;
                if(savedCameraController is MyCockpit cockpit) {
                    cockpit.IsInFirstPersonView=savedCockpitFirstPerson;
                    cockpit.HeadLocalXAngle=savedCockpitPitch;cockpit.HeadLocalYAngle=savedCockpitYaw;
                }
                savedCameraController=null; cameraOverrideSession=null;
            } else if(args["position"]!=null || args["target"]!=null || args["up"]!=null) {
                RequireWritableWorld();
                var position=CameraVector(args["position"],"position");var target=CameraVector(args["target"],"target");
                var up=args["up"]==null?Vector3D.Up:CameraVector(args["up"],"up");
                var forward=target-position;
                if(forward.LengthSquared()<.0001 || up.LengthSquared()<.0001 || Vector3D.Cross(Vector3D.Normalize(forward),Vector3D.Normalize(up)).LengthSquared()<.0001) throw new ArgumentException("Camera target and up must define distinct, nonparallel directions.");
                if(cameraOverrideSession!=MySession.Static || savedCameraController==null) {
                    savedCameraController=MySession.Static.CameraController;cameraOverrideSession=MySession.Static;
                    savedSpectatorView=MySpectatorCameraController.Static.GetViewMatrix();
                    if(savedCameraController is MyCockpit cockpit) {
                        savedCockpitFirstPerson=cockpit.IsInFirstPersonView;
                        savedCockpitPitch=cockpit.HeadLocalXAngle;savedCockpitYaw=cockpit.HeadLocalYAngle;
                    }
                }
                MySession.Static.SetCameraController(MyCameraControllerEnum.SpectatorFixed,null,position);
                MySpectatorCameraController.Static.Velocity=Vector3D.Zero;
                MySpectatorCameraController.Static.SetViewMatrix(MatrixD.CreateLookAt(position,target,up));
            }
            var currentCockpit=MySession.Static.CameraController as MyCockpit;
            return new JObject { ["pose"]=Pose(MatrixD.Invert(MySector.MainCamera.ViewMatrix)),
                ["controller"]=MySession.Static.CameraController?.GetType().FullName,
                ["cockpit"]=currentCockpit==null?null:new JObject { ["entityId"]=currentCockpit.EntityId.ToString(),["firstPerson"]=currentCockpit.IsInFirstPersonView || currentCockpit.ForceFirstPersonCamera,["pitch"]=currentCockpit.HeadLocalXAngle,["yaw"]=currentCockpit.HeadLocalYAngle },
                ["overridden"]=cameraOverrideSession==MySession.Static && savedCameraController!=null,
                ["note"]="Only the rendering camera changes; the player, controlled ship and world entities are not moved. Pose updates on the next rendered frame." };
        }
        private static Vector3D CameraVector(JToken value,string name)
        {
            if(!(value is JObject obj) || obj.Count!=3 || obj["x"]==null || obj["y"]==null || obj["z"]==null) throw new ArgumentException(name+" requires x, y, z.");
            foreach(var property in obj.Properties()) if(property.Value.Type!=JTokenType.Float && property.Value.Type!=JTokenType.Integer) throw new ArgumentException(name+" requires numeric coordinates.");
            var v=new Vector3D((double)obj["x"],(double)obj["y"],(double)obj["z"]);
            if(!v.IsValid() || Math.Max(Math.Abs(v.X),Math.Max(Math.Abs(v.Y),Math.Abs(v.Z)))>1e10) throw new ArgumentException("Invalid camera coordinates.");
            return v;
        }
    }
}