public void UpdateNetwork(GameTime gameTime) { // Update the server with our status. timeSinceLastUpdate += gameTime.ElapsedGameTime.TotalSeconds; if (timeSinceLastUpdate > 0.05) { timeSinceLastUpdate = 0; if (CurrentStateType == "Infiniminer.States.MainGameState") propertyBag.SendPlayerUpdate(); } // Recieve messages from the server. NetMessageType msgType; while (propertyBag.netClient.ReadMessage(msgBuffer, out msgType)) { switch (msgType) { case NetMessageType.StatusChanged: { if (propertyBag.netClient.Status == NetConnectionStatus.Disconnected) ChangeState("Infiniminer.States.ServerBrowserState"); } break; case NetMessageType.ConnectionRejected: { string[] reason = msgBuffer.ReadString().Split(";".ToCharArray()); if (reason.Length < 2 || reason[0] == "VER") MessageBox.Show("Error: client/server version incompability!\r\nServer: " + msgBuffer.ReadString() + "\r\nClient: " + INFINIMINER_VERSION); else MessageBox.Show("Error: you are banned from this server!"); ChangeState("Infiniminer.States.ServerBrowserState"); } break; case NetMessageType.Data: { InfiniminerMessage dataType = (InfiniminerMessage)msgBuffer.ReadByte(); switch (dataType) { case InfiniminerMessage.BlockBulkTransfer: { //Decompress the sent data into its origonal size in decompressed stream var compressed = msgBuffer.ReadBytes(msgBuffer.LengthBytes-msgBuffer.Position/8); var compressedstream = new System.IO.MemoryStream(compressed); var decompresser = new System.IO.Compression.GZipStream(compressedstream, System.IO.Compression.CompressionMode.Decompress); byte x = (byte)decompresser.ReadByte(); byte y = (byte)decompresser.ReadByte(); propertyBag.mapLoadProgress[x,y] = true; for (byte dy=0; dy<GlobalVariables.PACKETSIZE; dy++) for (byte z=0; z<GlobalVariables.MAPSIZE; z++) { BlockType blockType = (BlockType)decompresser.ReadByte(); if (blockType != BlockType.None) propertyBag.blockEngine.downloadList[x, y+dy, z] = blockType; } bool downloadComplete = true; for (x=0; x<GlobalVariables.MAPSIZE; x++) for (y=0; y<GlobalVariables.MAPSIZE; y+=GlobalVariables.PACKETSIZE) if (propertyBag.mapLoadProgress[x,y] == false) { downloadComplete = false; break; } if (downloadComplete) { ChangeState("Infiniminer.States.TeamSelectionState"); if (!NoSound) MediaPlayer.Stop(); propertyBag.blockEngine.DownloadComplete(); } } break; case InfiniminerMessage.SetBeacon: { Vector3 position = msgBuffer.ReadVector3(); string text = msgBuffer.ReadString(); PlayerTeam team = (PlayerTeam)msgBuffer.ReadByte(); if (text == "") { if (propertyBag.beaconList.ContainsKey(position)) propertyBag.beaconList.Remove(position); } else { Beacon newBeacon = new Beacon(); newBeacon.ID = text; newBeacon.Team = team; propertyBag.beaconList.Add(position, newBeacon); } } break; case InfiniminerMessage.TriggerConstructionGunAnimation: { propertyBag.constructionGunAnimation = msgBuffer.ReadFloat(); if (propertyBag.constructionGunAnimation <= -0.1) propertyBag.PlaySound(InfiniminerSound.RadarSwitch); } break; case InfiniminerMessage.ResourceUpdate: { // ore, cash, weight, max ore, max weight, team ore, red cash, blue cash, all uint propertyBag.playerOre = msgBuffer.ReadUInt32(); propertyBag.playerCash = msgBuffer.ReadUInt32(); propertyBag.playerWeight = msgBuffer.ReadUInt32(); propertyBag.playerOreMax = msgBuffer.ReadUInt32(); propertyBag.playerWeightMax = msgBuffer.ReadUInt32(); propertyBag.teamOre = msgBuffer.ReadUInt32(); propertyBag.teamRedCash = msgBuffer.ReadUInt32(); propertyBag.teamBlueCash = msgBuffer.ReadUInt32(); } break; case InfiniminerMessage.BlockSet: { // x, y, z, type, all bytes byte x = msgBuffer.ReadByte(); byte y = msgBuffer.ReadByte(); byte z = msgBuffer.ReadByte(); BlockType blockType = (BlockType)msgBuffer.ReadByte(); if (blockType == BlockType.None) { if (propertyBag.blockEngine.BlockAtPoint(new Vector3(x, y, z)) != BlockType.None) propertyBag.blockEngine.RemoveBlock(x, y, z); } else { if (propertyBag.blockEngine.BlockAtPoint(new Vector3(x, y, z)) != BlockType.None) propertyBag.blockEngine.RemoveBlock(x, y, z); propertyBag.blockEngine.AddBlock(x, y, z, blockType); CheckForStandingInLava(); } } break; case InfiniminerMessage.TriggerExplosion: { Vector3 blockPos = msgBuffer.ReadVector3(); // Play the explosion sound. propertyBag.PlaySound(InfiniminerSound.Explosion, blockPos); // Create some particles. propertyBag.particleEngine.CreateExplosionDebris(blockPos); // Figure out what the effect is. float distFromExplosive = (blockPos + 0.5f * Vector3.One - propertyBag.playerPosition).Length(); if (distFromExplosive < 3) propertyBag.KillPlayer("WAS KILLED IN AN EXPLOSION!"); else if (distFromExplosive < 8) { // If we're not in explosion mode, turn it on with the minimum ammount of shakiness. if (propertyBag.screenEffect != ScreenEffect.Explosion) { propertyBag.screenEffect = ScreenEffect.Explosion; propertyBag.screenEffectCounter = 2; } // If this bomb would result in a bigger shake, use its value. propertyBag.screenEffectCounter = Math.Min(propertyBag.screenEffectCounter, (distFromExplosive - 2) / 5); } } break; case InfiniminerMessage.PlayerSetTeam: { uint playerId = msgBuffer.ReadUInt32(); if (propertyBag.playerList.ContainsKey(playerId)) { Player player = propertyBag.playerList[playerId]; player.Team = (PlayerTeam)msgBuffer.ReadByte(); } } break; case InfiniminerMessage.PlayerJoined: { uint playerId = msgBuffer.ReadUInt32(); string playerName = msgBuffer.ReadString(); bool thisIsMe = msgBuffer.ReadBoolean(); bool playerAlive = msgBuffer.ReadBoolean(); propertyBag.playerList[playerId] = new Player(null, (Game)this); propertyBag.playerList[playerId].Handle = playerName; propertyBag.playerList[playerId].ID = playerId; propertyBag.playerList[playerId].Alive = playerAlive; if (thisIsMe) propertyBag.playerMyId = playerId; } break; case InfiniminerMessage.PlayerLeft: { uint playerId = msgBuffer.ReadUInt32(); if (propertyBag.playerList.ContainsKey(playerId)) propertyBag.playerList.Remove(playerId); } break; case InfiniminerMessage.PlayerDead: { uint playerId = msgBuffer.ReadUInt32(); if (propertyBag.playerList.ContainsKey(playerId)) { Player player = propertyBag.playerList[playerId]; player.Alive = false; propertyBag.particleEngine.CreateBloodSplatter(player.Position, player.Team == PlayerTeam.Red ? Color.Red : Color.Blue); if (playerId != propertyBag.playerMyId) propertyBag.PlaySound(InfiniminerSound.Death, player.Position); } } break; case InfiniminerMessage.PlayerAlive: { uint playerId = msgBuffer.ReadUInt32(); if (propertyBag.playerList.ContainsKey(playerId)) { Player player = propertyBag.playerList[playerId]; player.Alive = true; } } break; case InfiniminerMessage.PlayerUpdate: { uint playerId = msgBuffer.ReadUInt32(); if (propertyBag.playerList.ContainsKey(playerId)) { Player player = propertyBag.playerList[playerId]; player.UpdatePosition(msgBuffer.ReadVector3(), gameTime.TotalGameTime.TotalSeconds); player.Heading = msgBuffer.ReadVector3(); player.Tool = (PlayerTools)msgBuffer.ReadByte(); player.UsingTool = msgBuffer.ReadBoolean(); player.Score = (uint)(msgBuffer.ReadUInt16() * 100); } } break; case InfiniminerMessage.GameOver: { propertyBag.teamWinners = (PlayerTeam)msgBuffer.ReadByte(); } break; case InfiniminerMessage.ChatMessage: { ChatMessageType chatType = (ChatMessageType)msgBuffer.ReadByte(); string chatString = msgBuffer.ReadString(); ChatMessage chatMsg = new ChatMessage(chatString, chatType, 10); propertyBag.chatBuffer.Insert(0, chatMsg); propertyBag.PlaySound(InfiniminerSound.ClickLow); } break; case InfiniminerMessage.PlayerPing: { uint playerId = (uint)msgBuffer.ReadInt32(); if (propertyBag.playerList.ContainsKey(playerId)) { if (propertyBag.playerList[playerId].Team == propertyBag.playerTeam) { propertyBag.playerList[playerId].Ping = 1; propertyBag.PlaySound(InfiniminerSound.Ping); } } } break; case InfiniminerMessage.PlaySound: { InfiniminerSound sound = (InfiniminerSound)msgBuffer.ReadByte(); bool hasPosition = msgBuffer.ReadBoolean(); if (hasPosition) { Vector3 soundPosition = msgBuffer.ReadVector3(); propertyBag.PlaySound(sound, soundPosition); } else propertyBag.PlaySound(sound); } break; } } break; } } // Make sure our network thread actually gets to run. Thread.Sleep(1); }
private static bool MessageExpired(ChatMessage msg) { return msg.timestamp <= 0; }
public void addChatMessage(string chatString, ChatMessageType chatType, float timestamp) { string[] text = chatString.Split(' '); string textFull = ""; string textLine = ""; int newlines = 0; float curWidth = 0; for (int i = 0; i < text.Length; i++) {//each(string part in text){ string part = text[i]; if (i != text.Length - 1) part += ' '; //Correct for lost spaces float incr = interfaceEngine.uiFont.MeasureString(part).X; curWidth += incr; if (curWidth > 1024 - 64) //Assume default resolution, unfortunately { if (textLine.IndexOf(' ') < 0) { curWidth = 0; textFull = textFull + "\n" + textLine; textLine = ""; } else { curWidth = incr; textFull = textFull + "\n" + textLine; textLine = part; } newlines++; } else { textLine = textLine + part; } } if (textLine != "") { textFull += "\n" + textLine; newlines++; } if (textFull == "") textFull = chatString; ChatMessage chatMsg = new ChatMessage(textFull, chatType, 10,newlines); chatBuffer.Insert(0, chatMsg); chatFullBuffer.Insert(0, chatMsg); PlaySound(InfiniminerSound.ClickLow); }