protected override void Initialize() { graphicsDeviceManager.IsFullScreen = false; graphicsDeviceManager.PreferredBackBufferWidth = 1024; graphicsDeviceManager.PreferredBackBufferHeight = 768; graphicsDeviceManager.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8; DatafileLoader dataFile = new DatafileLoader("client.config.txt"); if (dataFile.Data.ContainsKey("width")) { graphicsDeviceManager.PreferredBackBufferWidth = int.Parse(dataFile.Data["width"], System.Globalization.CultureInfo.InvariantCulture); } if (dataFile.Data.ContainsKey("height")) { graphicsDeviceManager.PreferredBackBufferHeight = int.Parse(dataFile.Data["height"], System.Globalization.CultureInfo.InvariantCulture); } if (dataFile.Data.ContainsKey("fullscreen")) { graphicsDeviceManager.IsFullScreen = bool.Parse(dataFile.Data["fullscreen"]); } if (dataFile.Data.ContainsKey("handle")) { playerHandle = dataFile.Data["handle"]; } if (dataFile.Data.ContainsKey("showfps")) { DrawFrameRate = bool.Parse(dataFile.Data["showfps"]); } if (dataFile.Data.ContainsKey("yinvert")) { InvertMouseYAxis = bool.Parse(dataFile.Data["yinvert"]); } if (dataFile.Data.ContainsKey("nosound")) { NoSound = bool.Parse(dataFile.Data["nosound"]); } if (dataFile.Data.ContainsKey("pretty")) { RenderPretty = bool.Parse(dataFile.Data["pretty"]); } if (dataFile.Data.ContainsKey("volume")) { volumeLevel = Math.Max(0, Math.Min(1, float.Parse(dataFile.Data["volume"], System.Globalization.CultureInfo.InvariantCulture))); } graphicsDeviceManager.ApplyChanges(); base.Initialize(); }
protected override void Initialize() { graphicsDeviceManager.IsFullScreen = false; graphicsDeviceManager.PreferredBackBufferWidth = 1024; graphicsDeviceManager.PreferredBackBufferHeight = 768; graphicsDeviceManager.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8; DatafileLoader dataFile = new DatafileLoader("client.config.txt"); if (dataFile.Data.ContainsKey("width")) graphicsDeviceManager.PreferredBackBufferWidth = int.Parse(dataFile.Data["width"], System.Globalization.CultureInfo.InvariantCulture); if (dataFile.Data.ContainsKey("height")) graphicsDeviceManager.PreferredBackBufferHeight = int.Parse(dataFile.Data["height"], System.Globalization.CultureInfo.InvariantCulture); if (dataFile.Data.ContainsKey("fullscreen")) graphicsDeviceManager.IsFullScreen = bool.Parse(dataFile.Data["fullscreen"]); if (dataFile.Data.ContainsKey("handle")) playerHandle = dataFile.Data["handle"]; if (dataFile.Data.ContainsKey("showfps")) DrawFrameRate = bool.Parse(dataFile.Data["showfps"]); if (dataFile.Data.ContainsKey("yinvert")) InvertMouseYAxis = bool.Parse(dataFile.Data["yinvert"]); if (dataFile.Data.ContainsKey("nosound")) NoSound = bool.Parse(dataFile.Data["nosound"]); if (dataFile.Data.ContainsKey("pretty")) RenderPretty = bool.Parse(dataFile.Data["pretty"]); if (dataFile.Data.ContainsKey("volume")) volumeLevel = Math.Max(0,Math.Min(1,float.Parse(dataFile.Data["volume"], System.Globalization.CultureInfo.InvariantCulture))); graphicsDeviceManager.ApplyChanges(); base.Initialize(); }
public bool Start() { // Read in from the config file. DatafileLoader dataFile = new DatafileLoader("server.config.txt"); if (dataFile.Data.ContainsKey("winningcash")) winningCashAmount = uint.Parse(dataFile.Data["winningcash"], System.Globalization.CultureInfo.InvariantCulture); if (dataFile.Data.ContainsKey("includelava")) includeLava = bool.Parse(dataFile.Data["includelava"]); if (dataFile.Data.ContainsKey("orefactor")) oreFactor = uint.Parse(dataFile.Data["orefactor"], System.Globalization.CultureInfo.InvariantCulture); if (dataFile.Data.ContainsKey("maxplayers")) maxPlayers = Math.Min(32, uint.Parse(dataFile.Data["maxplayers"], System.Globalization.CultureInfo.InvariantCulture)); if (dataFile.Data.ContainsKey("public")) publicServer = bool.Parse(dataFile.Data["public"]); if (dataFile.Data.ContainsKey("servername")) serverName = dataFile.Data["servername"]; if (dataFile.Data.ContainsKey("sandbox")) sandboxMode = bool.Parse(dataFile.Data["sandbox"]); // Load the ban-list. banList = LoadBanList(); // Create our block world, translating the coordinates out of the cave generator (where Z points down) BlockType[, ,] worldData = CaveGenerator.GenerateCaveSystem(GlobalVariables.MAPSIZE, includeLava, oreFactor); blockList = new BlockType[GlobalVariables.MAPSIZE, GlobalVariables.MAPSIZE, GlobalVariables.MAPSIZE]; blockCreatorTeam = new PlayerTeam[GlobalVariables.MAPSIZE, GlobalVariables.MAPSIZE, GlobalVariables.MAPSIZE]; for (ushort i = 0; i < GlobalVariables.MAPSIZE; i++) for (ushort j = 0; j < GlobalVariables.MAPSIZE; j++) for (ushort k = 0; k < GlobalVariables.MAPSIZE; k++) { blockList[i, (ushort)(GlobalVariables.MAPSIZE - 1 - k), j] = worldData[i, j, k]; if(blockList[i, (ushort)(GlobalVariables.MAPSIZE - 1 - k), j] == BlockType.Lava) LavaBlocks.Add(new InfiniminerServer.Point3D() { X = (ushort)i, Y = (ushort)(GlobalVariables.MAPSIZE - 1 - k), Z = (ushort)j }, 0); blockCreatorTeam[i, j, k] = PlayerTeam.None; } // Initialize the server. NetConfiguration netConfig = new NetConfiguration("InfiniminerPlus"); netConfig.MaxConnections = (int)maxPlayers; netConfig.Port = 5565; netServer = new InfiniminerNetServer(netConfig); netServer.SetMessageTypeEnabled(NetMessageType.ConnectionApproval, true); //netServer.SimulatedMinimumLatency = 0.1f; //netServer.SimulatedLatencyVariance = 0.05f; //netServer.SimulatedLoss = 0.1f; //netServer.SimulatedDuplicates = 0.05f; netServer.Start(); // Initialize variables we'll use. NetBuffer msgBuffer = netServer.CreateBuffer(); NetMessageType msgType; NetConnection msgSender; // Store the last time that we did a flow calculation. DateTime lastFlowCalc = DateTime.Now; // Calculate initial lava flows. ConsoleWrite("CALCULATING INITIAL LAVA FLOWS"); while (DoLavaStuff()) ; ConsoleWrite("TOTAL LAVA BLOCKS = " + LavaBlocks.Count); // Send the initial server list update. PublicServerListUpdate(); // Main server loop! ConsoleWrite("SERVER READY"); while (keepRunning) { // Process any messages that are here. while (netServer.ReadMessage(msgBuffer, out msgType, out msgSender)) { switch (msgType) { case NetMessageType.ConnectionApproval: { Player newPlayer = new Player(msgSender, null); newPlayer.Handle = InfiniminerGame.Sanitize(msgBuffer.ReadString()).Trim(); if (newPlayer.Handle.Length == 0) { newPlayer.Handle = "Player"; } string clientVersion = msgBuffer.ReadString(); if (clientVersion != InfiniminerGame.INFINIMINER_VERSION) { msgSender.Disapprove("VER;" + InfiniminerGame.INFINIMINER_VERSION); } else if (banList.Contains(newPlayer.IP)) { msgSender.Disapprove("BAN;"); } else { playerList[msgSender] = newPlayer; this.netServer.SanityCheck(msgSender); msgSender.Approve(); } } break; case NetMessageType.StatusChanged: { if (!this.playerList.ContainsKey(msgSender)) { break; } Player player = playerList[msgSender]; if (msgSender.Status == NetConnectionStatus.Connected) { ConsoleWrite("CONNECT: " + playerList[msgSender].Handle); SendCurrentMap(msgSender); SendPlayerJoined(player); PublicServerListUpdate(); } else if (msgSender.Status == NetConnectionStatus.Disconnected) { ConsoleWrite("DISCONNECT: " + playerList[msgSender].Handle); SendPlayerLeft(player, player.Kicked ? "WAS KICKED FROM THE GAME!" : "HAS ABANDONED THEIR DUTIES!"); if (playerList.ContainsKey(msgSender)) playerList.Remove(msgSender); PublicServerListUpdate(); } } break; case NetMessageType.Data: { if (!this.playerList.ContainsKey(msgSender)) { break; } Player player = playerList[msgSender]; InfiniminerMessage dataType = (InfiniminerMessage)msgBuffer.ReadByte(); switch (dataType) { case InfiniminerMessage.ChatMessage: { // Read the data from the packet. ChatMessageType chatType = (ChatMessageType)msgBuffer.ReadByte(); string chatString = InfiniminerGame.Sanitize(msgBuffer.ReadString()); ConsoleWrite("CHAT: (" + player.Handle + ") " + chatString); // Append identifier information. if (chatType == ChatMessageType.SayAll) chatString = player.Handle + " (ALL): " + chatString; else chatString = player.Handle + " (TEAM): " + chatString; // Construct the message packet. NetBuffer chatPacket = netServer.CreateBuffer(); chatPacket.Write((byte)InfiniminerMessage.ChatMessage); chatPacket.Write((byte)((player.Team == PlayerTeam.Red) ? ChatMessageType.SayRedTeam : ChatMessageType.SayBlueTeam)); chatPacket.Write(chatString); // Send the packet to people who should recieve it. foreach (Player p in playerList.Values) { if (chatType == ChatMessageType.SayAll || chatType == ChatMessageType.SayBlueTeam && p.Team == PlayerTeam.Blue || chatType == ChatMessageType.SayRedTeam && p.Team == PlayerTeam.Red) if (p.NetConn.Status == NetConnectionStatus.Connected) netServer.SendMessage(chatPacket, p.NetConn, NetChannel.ReliableInOrder3); } } break; case InfiniminerMessage.UseTool: { Vector3 playerPosition = msgBuffer.ReadVector3(); Vector3 playerHeading = msgBuffer.ReadVector3(); PlayerTools playerTool = (PlayerTools)msgBuffer.ReadByte(); BlockType blockType = (BlockType)msgBuffer.ReadByte(); switch (playerTool) { case PlayerTools.Pickaxe: UsePickaxe(player, playerPosition, playerHeading); break; case PlayerTools.ConstructionGun: UseConstructionGun(player, playerPosition, playerHeading, blockType); break; case PlayerTools.DeconstructionGun: UseDeconstructionGun(player, playerPosition, playerHeading); break; case PlayerTools.ProspectingRadar: UseSignPainter(player, playerPosition, playerHeading); break; case PlayerTools.Detonator: UseDetonator(player); break; } } break; case InfiniminerMessage.SelectClass: { PlayerClass playerClass = (PlayerClass)msgBuffer.ReadByte(); ConsoleWrite("SELECT_CLASS: " + player.Handle + ", " + playerClass.ToString()); switch (playerClass) { case PlayerClass.Engineer: player.OreMax = 350; player.WeightMax = 4; break; case PlayerClass.Miner: player.OreMax = 200; player.WeightMax = 8; break; case PlayerClass.Prospector: player.OreMax = 200; player.WeightMax = 4; break; case PlayerClass.Sapper: player.OreMax = 200; player.WeightMax = 4; break; } SendResourceUpdate(player); } break; case InfiniminerMessage.PlayerSetTeam: { PlayerTeam playerTeam = (PlayerTeam)msgBuffer.ReadByte(); ConsoleWrite("SELECT_TEAM: " + player.Handle + ", " + playerTeam.ToString()); player.Team = playerTeam; SendResourceUpdate(player); SendPlayerSetTeam(player); } break; case InfiniminerMessage.PlayerDead: { ConsoleWrite("PLAYER_DEAD: " + player.Handle); player.Ore = 0; player.Cash = 0; player.Weight = 0; player.Alive = false; SendResourceUpdate(player); SendPlayerDead(player); string deathMessage = msgBuffer.ReadString(); if (deathMessage != "") { msgBuffer = netServer.CreateBuffer(); msgBuffer.Write((byte)InfiniminerMessage.ChatMessage); msgBuffer.Write((byte)(player.Team == PlayerTeam.Red ? ChatMessageType.SayRedTeam : ChatMessageType.SayBlueTeam)); msgBuffer.Write(player.Handle + " " + deathMessage); foreach (NetConnection netConn in playerList.Keys) if (netConn.Status == NetConnectionStatus.Connected) netServer.SendMessage(msgBuffer, netConn, NetChannel.ReliableInOrder3); } } break; case InfiniminerMessage.PlayerAlive: { ConsoleWrite("PLAYER_ALIVE: " + player.Handle); player.Ore = 0; player.Cash = 0; player.Weight = 0; player.Alive = true; SendResourceUpdate(player); SendPlayerAlive(player); } break; case InfiniminerMessage.PlayerUpdate: { player.Position = msgBuffer.ReadVector3(); player.Heading = msgBuffer.ReadVector3(); player.Tool = (PlayerTools)msgBuffer.ReadByte(); player.UsingTool = msgBuffer.ReadBoolean(); SendPlayerUpdate(player); } break; case InfiniminerMessage.DepositOre: { DepositOre(player); foreach (Player p in playerList.Values) SendResourceUpdate(p); } break; case InfiniminerMessage.WithdrawOre: { WithdrawOre(player); foreach (Player p in playerList.Values) SendResourceUpdate(p); } break; case InfiniminerMessage.PlayerPing: { SendPlayerPing((uint)msgBuffer.ReadInt32()); } break; case InfiniminerMessage.PlaySound: { InfiniminerSound sound = (InfiniminerSound)msgBuffer.ReadByte(); Vector3 position = msgBuffer.ReadVector3(); PlaySound(sound, position); } break; } } break; } } // Time to send a new server update? TimeSpan updateTimeSpan = DateTime.Now - lastServerListUpdate; if (updateTimeSpan.TotalMinutes > 5) PublicServerListUpdate(); // Check for players who are in the zone to deposit. DepositForPlayers(); // Is it time to do a lava calculation? If so, do it! TimeSpan timeSpan = DateTime.Now - lastFlowCalc; if (timeSpan.TotalMilliseconds > 500) { DoLavaStuff(); lastFlowCalc = DateTime.Now; } // Handle console keypresses. while (Console.KeyAvailable) { ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Enter) ConsoleProcessInput(); else if (keyInfo.Key == ConsoleKey.Backspace) { if (consoleInput.Length > 0) consoleInput = consoleInput.Substring(0, consoleInput.Length - 1); ConsoleRedraw(); } else { consoleInput += keyInfo.KeyChar; ConsoleRedraw(); } } // Is the game over? if (winningTeam != PlayerTeam.None && !restartTriggered) { BroadcastGameOver(); restartTriggered = true; restartTime = DateTime.Now.AddSeconds(10); } // Restart the server? if (restartTriggered && DateTime.Now > restartTime) { SaveLevel("autosave_" + (UInt64)DateTime.Now.ToBinary() + ".lvl"); netServer.Shutdown("The server is restarting."); return true; } // Pass control over to waiting threads. Thread.Sleep(1); } netServer.Shutdown("The server was terminated."); return false; }