Example #1
0
 /// <summary>
 /// Start up and run the server.
 /// </summary>
 public void StartUp(Action loaded = null)
 {
     CurThread = Thread.CurrentThread;
     SysConsole.Written += OnConsoleWritten;
     SysConsole.Output(OutputType.INIT, "Launching as new server, this is " + (this == Central ? "" : "NOT ") + "the Central server.");
     SysConsole.Output(OutputType.INIT, "Loading console input handler...");
     ConsoleHandler.Init();
     ConsoleHandler.OnCommandInput += CommandInputHandle;
     SysConsole.Output(OutputType.INIT, "Loading command engine...");
     Commands = new ServerCommands();
     Commands.Init(new ServerOutputter(this), this);
     SysConsole.Output(OutputType.INIT, "Loading CVar engine...");
     CVars = new ServerCVar();
     CVars.Init(this, Commands.Output);
     SysConsole.Output(OutputType.INIT, "Loading default settings...");
     Config = new FDSSection(Files.ReadText("server_config.fds"));
     if (Files.Exists("serverdefaultsettings.cfg"))
     {
         string contents = Files.ReadText("serverdefaultsettings.cfg");
         Commands.ExecuteCommands(contents);
     }
     if (Files.Exists("server_eid.txt"))
     {
         cID = long.Parse(Files.ReadText("server_eid.txt") ?? "1");
     }
     SysConsole.Output(OutputType.INIT, "Loading player command engine...");
     PCEngine = new PlayerCommandEngine();
     SysConsole.Output(OutputType.INIT, "Loading item registry...");
     ItemInfos = new ItemInfoRegistry();
     Items = new ItemRegistry(this);
     Recipes = new RecipeRegistry() { TheServer = this };
     SysConsole.Output(OutputType.INIT, "Loading model handler...");
     Models = new ModelEngine(this);
     SysConsole.Output(OutputType.INIT, "Loading animation handler...");
     Animations = new AnimationEngine();
     SysConsole.Output(OutputType.INIT, "Preparing networking...");
     Networking = new NetworkBase(this);
     Networking.Init();
     SysConsole.Output(OutputType.INIT, "Loading plugins...");
     Plugins = new PluginManager(this);
     Plugins.Init();
     SysConsole.Output(OutputType.INIT, "Loading scripts...");
     AutorunScripts();
     SysConsole.Output(OutputType.INIT, "Building initial world(s)...");
     foreach (string str in Config.GetStringList("server.worlds") ?? new List<string>())
     {
         LoadWorld(str.ToLowerFast());
     }
     SysConsole.Output(OutputType.INIT, "Preparing block image system...");
     BlockImages = new BlockImageManager();
     BlockImages.Init(this);
     if (loaded != null)
     {
         loaded.Invoke();
     }
     SysConsole.Output(OutputType.INIT, "Ticking...");
     // Tick
     double TARGETFPS = 30d;
     Stopwatch Counter = new Stopwatch();
     Stopwatch DeltaCounter = new Stopwatch();
     DeltaCounter.Start();
     double TotalDelta = 0;
     double CurrentDelta = 0d;
     double TargetDelta = 0d;
     int targettime = 0;
     try
     {
         while (true)
         {
             // Update the tick time usage counter
             Counter.Reset();
             Counter.Start();
             // Update the tick delta counter
             DeltaCounter.Stop();
             // Delta time = Elapsed ticks * (ticks/second)
             CurrentDelta = ((double)DeltaCounter.ElapsedTicks) / ((double)Stopwatch.Frequency);
             // Begin the delta counter to find out how much time is /really/ slept+ticked for
             DeltaCounter.Reset();
             DeltaCounter.Start();
             // How much time should pass between each tick ideally
             TARGETFPS = CVars.g_fps.ValueD;
             if (TARGETFPS < 1 || TARGETFPS > 600)
             {
                 CVars.g_fps.Set("30");
                 TARGETFPS = 30;
             }
             TargetDelta = (1d / TARGETFPS);
             // How much delta has been built up
             TotalDelta += CurrentDelta;
             while (TotalDelta > TargetDelta * 3)
             {
                 // Lagging - cheat to catch up!
                 TargetDelta *= 2;
             }
             // As long as there's more delta built up than delta wanted, tick
             while (TotalDelta > TargetDelta)
             {
                 if (NeedShutdown)
                 {
                     CurThread = Thread.CurrentThread;
                     ShutDown(shutdownCallback);
                     return;
                 }
                 lock (TickLock)
                 {
                     Tick(TargetDelta);
                 }
                 TotalDelta -= TargetDelta;
             }
             // The tick is done, stop measuring it
             Counter.Stop();
             // Only sleep for target milliseconds/tick minus how long the tick took... this is imprecise but that's okay
             targettime = (int)((1000d / TARGETFPS) - Counter.ElapsedMilliseconds);
             // Only sleep at all if we're not lagging
             if (targettime > 0)
             {
                 // Try to sleep for the target time - very imprecise, thus we deal with precision inside the tick code
                 Thread.Sleep(targettime);
             }
         }
     }
     catch (ThreadAbortException)
     {
         return;
     }
 }
Example #2
0
 /// <summary>
 /// Called when the window is loading, only to be used by the startup process.
 /// </summary>
 void Window_Load(object sender, EventArgs e)
 {
     SysConsole.Output(OutputType.INIT, "Window generated!");
     DPIScale = Window.Width / CVars.r_width.ValueF;
     SysConsole.Output(OutputType.INIT, "DPIScale is " + DPIScale + "!");
     SysConsole.Output(OutputType.INIT, "Loading base textures...");
     PreInitRendering();
     Textures = new TextureEngine();
     Textures.InitTextureSystem(this);
     ItemFrame = Textures.GetTexture("ui/hud/item_frame");
     SysConsole.Output(OutputType.INIT, "Loading shaders...");
     Shaders = new ShaderEngine();
     GLVendor = GL.GetString(StringName.Vendor);
     CVars.s_glvendor.Value = GLVendor;
     GLVersion = GL.GetString(StringName.Version);
     CVars.s_glversion.Value = GLVersion;
     GLRenderer = GL.GetString(StringName.Renderer);
     CVars.s_glrenderer.Value = GLRenderer;
     SysConsole.Output(OutputType.INIT, "Vendor: " + GLVendor + ", GLVersion: " + GLVersion + ", Renderer: " + GLRenderer);
     if (GLVendor.ToLowerFast().Contains("intel"))
     {
         SysConsole.Output(OutputType.INIT, "Disabling good graphics (Appears to be Intel: '" + GLVendor + "')");
         Shaders.MCM_GOOD_GRAPHICS = false;
     }
     Shaders.InitShaderSystem(this);
     View3D.CheckError("Load - Shaders");
     SysConsole.Output(OutputType.INIT, "Loading rendering helper...");
     Rendering = new Renderer(Textures, Shaders);
     Rendering.Init();
     SysConsole.Output(OutputType.INIT, "Preparing load screen...");
     Texture load_screen = Textures.GetTexture("ui/menus/loadscreen");
     load_screen.Bind();
     Shaders.ColorMultShader.Bind();
     Establish2D();
     Rendering.RenderRectangle(0, 0, Window.Width, Window.Height);
     Window.SwapBuffers();
     SysConsole.Output(OutputType.INIT, "Loading block textures...");
     TBlock = new TextureBlock();
     TBlock.Generate(this, CVars, Textures);
     View3D.CheckError("Load - Textures");
     SysConsole.Output(OutputType.INIT, "Loading fonts...");
     Fonts = new GLFontEngine(Shaders);
     Fonts.Init(this);
     FontSets = new FontSetEngine(Fonts);
     FontSets.Init(this);
     View3D.CheckError("Load - Fonts");
     SysConsole.Output(OutputType.INIT, "Loading animation engine...");
     Animations = new AnimationEngine();
     SysConsole.Output(OutputType.INIT, "Loading model engine...");
     Models = new ModelEngine();
     Models.Init(Animations, this);
     SysConsole.Output(OutputType.INIT, "Loading general graphics settings...");
     CVars.r_vsync.OnChanged += onVsyncChanged;
     onVsyncChanged(CVars.r_vsync, null);
     CVars.r_cloudshadows.OnChanged += onCloudShadowChanged;
     View3D.CheckError("Load - General Graphics");
     SysConsole.Output(OutputType.INIT, "Loading UI engine...");
     UIConsole.InitConsole(); // TODO: make this non-static
     InitChatSystem();
     View3D.CheckError("Load - UI");
     SysConsole.Output(OutputType.INIT, "Preparing rendering engine...");
     InitRendering();
     View3D.CheckError("Load - Rendering");
     SysConsole.Output(OutputType.INIT, "Loading particle effect engine...");
     Particles = new ParticleHelper(this) { Engine = new ParticleEngine(this) };
     SysConsole.Output(OutputType.INIT, "Preparing mouse, keyboard, and gamepad handlers...");
     KeyHandler.Init();
     GamePadHandler.Init();
     View3D.CheckError("Load - Keyboard/mouse");
     SysConsole.Output(OutputType.INIT, "Building the sound system...");
     Sounds = new SoundEngine();
     Sounds.Init(this, CVars);
     View3D.CheckError("Load - Sound");
     SysConsole.Output(OutputType.INIT, "Building game world...");
     BuildWorld();
     View3D.CheckError("Load - World");
     SysConsole.Output(OutputType.INIT, "Preparing networking...");
     Network = new NetworkBase(this);
     RegisterDefaultEntityTypes();
     View3D.CheckError("Load - Net");
     SysConsole.Output(OutputType.INIT, "Playing background music...");
     BackgroundMusic();
     CVars.a_musicvolume.OnChanged += onMusicVolumeChanged;
     CVars.a_musicpitch.OnChanged += onMusicPitchChanged;
     CVars.a_music.OnChanged += onMusicChanged;
     CVars.a_echovolume.OnChanged += OnEchoVolumeChanged;
     OnEchoVolumeChanged(null, null);
     SysConsole.Output(OutputType.INIT, "Setting up screens...");
     TheMainMenuScreen = new MainMenuScreen(this);
     TheGameScreen = new GameScreen(this);
     TheSingleplayerMenuScreen = new SingleplayerMenuScreen(this);
     SysConsole.Output(OutputType.INIT, "Preparing inventory...");
     InitInventory();
     SysConsole.Output(OutputType.INIT, "Showing main menu...");
     ShowMainMenu();
     SysConsole.Output(OutputType.INIT, "Trying to grab RawGamePad...");
     try
     {
         RawGamePad = new XInput();
     }
     catch (Exception ex)
     {
         SysConsole.Output(OutputType.INIT, "Failed to grab RawGamePad: " + ex.Message);
     }
     View3D.CheckError("Load - Final");
     SysConsole.Output(OutputType.INIT, "Ready and looping!");
 }
Example #3
0
 /// <summary>
 /// Prepares the model system.
 /// </summary>
 public void Init(AnimationEngine engine, Client tclient)
 {
     TheClient = tclient;
     AnimEngine = engine;
     Handler = new ModelHandler();
     LoadedModels = new List<Model>();
     Cube = GetModel("cube");
     Cylinder = GetModel("cylinder");
     Sphere = GetModel("sphere");
 }
Example #4
0
 void PopulateChildren(ModelNode node, Model3DNode orin, Model model, AnimationEngine engine, List<ModelNode> allNodes)
 {
     allNodes.Add(node);
     if (engine.HeadBones.Contains(node.Name))
     {
         node.Mode = 0;
     }
     else if (engine.LegBones.Contains(node.Name))
     {
         node.Mode = 2;
     }
     else
     {
         node.Mode = 1;
     }
     for (int i = 0; i < orin.Children.Count; i++)
     {
         ModelNode child = new ModelNode() { Parent = node, Name = orin.Children[i].Name.ToLowerFast() };
         PopulateChildren(child, orin.Children[i], model, engine, allNodes);
         node.Children.Add(child);
     }
 }
Example #5
0
 public Model FromScene(Model3D scene, string name, AnimationEngine engine)
 {
     if (scene.Meshes.Count == 0)
     {
         throw new Exception("Scene has no meshes! (" + name + ")");
     }
     Model model = new Model(name);
     model.Engine = this;
     model.Original = scene;
     model.Root = convert(scene.MatrixA);
     foreach (Model3DMesh mesh in scene.Meshes)
     {
         if (mesh.Name.ToLowerFast().Contains("collision") || mesh.Name.ToLowerFast().Contains("norender"))
         {
             continue;
         }
         ModelMesh modmesh = new ModelMesh(mesh.Name);
         modmesh.vbo.Prepare();
         bool hastc = mesh.TexCoords.Count == mesh.Vertices.Count;
         bool hasn = mesh.Normals.Count == mesh.Vertices.Count;
         if (!hasn)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has no normals! (" + name + ")");
         }
         if (!hastc)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has no texcoords! (" + name + ")");
         }
         for (int i = 0; i < mesh.Vertices.Count; i++)
         {
             BEPUutilities.Vector3 vertex = mesh.Vertices[i];
             modmesh.vbo.Vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z));
             if (!hastc)
             {
                 modmesh.vbo.TexCoords.Add(new Vector3(0, 0, 0));
             }
             else
             {
                 BEPUutilities.Vector2 texCoord = mesh.TexCoords[i];
                 modmesh.vbo.TexCoords.Add(new Vector3((float)texCoord.X, 1 - (float)texCoord.Y, 0));
             }
             if (!hasn)
             {
                 modmesh.vbo.Normals.Add(new Vector3(0, 0, 1));
             }
             else
             {
                 modmesh.vbo.Normals.Add(new Vector3((float)mesh.Normals[i].X, (float)mesh.Normals[i].Y, (float)mesh.Normals[i].Z));
             }
             modmesh.vbo.Colors.Add(new Vector4(1, 1, 1, 1)); // TODO: From the mesh?
         }
         for (int i = 0; i < mesh.Indices.Count; i++)
         {
             modmesh.vbo.Indices.Add((uint)mesh.Indices[i]);
         }
         int bc = mesh.Bones.Count;
         if (bc > 200)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has " + bc + " bones! (" + name + ")");
             bc = 200;
         }
         modmesh.vbo.BoneIDs = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneWeights = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneIDs2 = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneWeights2 = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         int[] pos = new int[modmesh.vbo.Vertices.Count];
         for (int i = 0; i < bc; i++)
         {
             for (int x = 0; x < mesh.Bones[i].Weights.Count; x++)
             {
                 int IDa = mesh.Bones[i].IDs[x];
                 float Weighta = (float)mesh.Bones[i].Weights[x];
                 int spot = pos[IDa]++;
                 if (spot > 7)
                 {
                     //SysConsole.Output(OutputType.WARNING, "Too many bones influencing " + vw.VertexID + "!");
                     ForceSet(modmesh.vbo.BoneWeights, IDa, 3, modmesh.vbo.BoneWeights[IDa][3] + Weighta);
                 }
                 else if (spot > 3)
                 {
                     ForceSet(modmesh.vbo.BoneIDs2, IDa, spot - 4, i);
                     ForceSet(modmesh.vbo.BoneWeights2, IDa, spot - 4, Weighta);
                 }
                 else
                 {
                     ForceSet(modmesh.vbo.BoneIDs, IDa, spot, i);
                     ForceSet(modmesh.vbo.BoneWeights, IDa, spot, Weighta);
                 }
             }
         }
         model.Meshes.Add(modmesh);
     }
     model.RootNode = new ModelNode() { Parent = null, Name = scene.RootNode.Name.ToLowerFast() };
     List<ModelNode> allNodes = new List<ModelNode>();
     PopulateChildren(model.RootNode, scene.RootNode, model, engine, allNodes);
     for (int i = 0; i < model.Meshes.Count; i++)
     {
         for (int x = 0; x < scene.Meshes[i].Bones.Count; x++)
         {
             ModelNode nodet = null;
             string nl = scene.Meshes[i].Bones[x].Name.ToLowerFast();
             for (int n = 0; n < allNodes.Count; n++)
             {
                 if (allNodes[n].Name == nl)
                 {
                     nodet = allNodes[n];
                     break;
                 }
             }
             ModelBone mb = new ModelBone() { Offset = convert(scene.Meshes[i].Bones[x].MatrixA) };
             nodet.Bones.Add(mb);
             model.Meshes[i].Bones.Add(mb);
         }
     }
     return model;
 }