예제 #1
0
 static void Main(string[] args)
 {
     //InfiniminerMessageBox.Show("Test :)");
     if (Debugger.IsAttached)
     {
         using (InfiniminerGame game = new InfiniminerGame(args))
         {
             game.Run();
         }
     }
     else
     {
         using (InfiniminerGame game = new InfiniminerGame(args))
         {
             try
             {
                 game.Run();
             }
             catch (Exception e)
             {
                 //System.Windows.Forms.MessageBox.Show(e.Message + "\r\n\r\n" + e.StackTrace);
                 InfiniminerMessageBox.Show(e.Message + "\r\n\r\n" + e.StackTrace);
             }
         }
     }
 }
예제 #2
0
        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.
            while ((msgBuffer = propertyBag.netClient.ReadMessage()) != null)
            {
                switch (msgBuffer.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                {
                    if (propertyBag.netClient.ConnectionStatus == NetConnectionStatus.RespondedConnect)
                    {
                        anyPacketsReceived = true;
                    }
                    if (propertyBag.netClient.ConnectionStatus == NetConnectionStatus.Disconnected)
                    {
                        anyPacketsReceived = false;
                        try
                        {
                            string[] reason = msgBuffer.ReadString().Split(";".ToCharArray());
                            if (reason.Length < 2 || reason[0] == "VER")
                            {
                                InfiniminerMessageBox.Show("Error: client/server version incompability!\r\nServer: " + msgBuffer.ReadString() + "\r\nClient: " + Defines.INFINIMINER_VERSION);
                            }
                            else
                            {
                                InfiniminerMessageBox.Show("Error: you are banned from this server!");
                            }
                        }
                        catch { }
                        ChangeState("Infiniminer.States.ServerBrowserState");
                    }
                }
                break;

                case NetIncomingMessageType.Data:
                {
                    try
                    {
                        InfiniminerMessage dataType = (InfiniminerMessage)msgBuffer.ReadByte();
                        switch (dataType)
                        {
                        case InfiniminerMessage.BlockBulkTransfer:
                        {
                            anyPacketsReceived = true;

                            try
                            {
                                //This is either the compression flag or the x coordiante
                                byte isCompressed = msgBuffer.ReadByte();
                                byte x;
                                byte y;

                                //255 was used because it exceeds the map size - of course, bytes won't work anyway if map sizes are allowed to be this big, so this method is a non-issue
                                if (isCompressed == 255)
                                {
                                    var compressed       = msgBuffer.ReadBytes(msgBuffer.LengthBytes - (int)(msgBuffer.Position / 8));
                                    var compressedstream = new System.IO.MemoryStream(compressed);
                                    var decompresser     = new System.IO.Compression.GZipStream(compressedstream, System.IO.Compression.CompressionMode.Decompress);

                                    x = (byte)decompresser.ReadByte();
                                    y = (byte)decompresser.ReadByte();
                                    propertyBag.mapLoadProgress[x, y] = true;
                                    for (byte dy = 0; dy < 16; dy++)
                                    {
                                        for (byte z = 0; z < 64; z++)
                                        {
                                            BlockType blockType = (BlockType)decompresser.ReadByte();
                                            if (blockType != BlockType.None)
                                            {
                                                propertyBag.blockEngine.downloadList[x, y + dy, z] = blockType;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    x = isCompressed;
                                    y = msgBuffer.ReadByte();
                                    propertyBag.mapLoadProgress[x, y] = true;
                                    for (byte dy = 0; dy < 16; dy++)
                                    {
                                        for (byte z = 0; z < 64; z++)
                                        {
                                            BlockType blockType = (BlockType)msgBuffer.ReadByte();
                                            if (blockType != BlockType.None)
                                            {
                                                propertyBag.blockEngine.downloadList[x, y + dy, z] = blockType;
                                            }
                                        }
                                    }
                                }
                                bool downloadComplete = true;
                                for (x = 0; x < 64; x++)
                                {
                                    for (y = 0; y < 64; y += 16)
                                    {
                                        if (propertyBag.mapLoadProgress[x, y] == false)
                                        {
                                            downloadComplete = false;
                                            break;
                                        }
                                    }
                                }
                                if (downloadComplete)
                                {
                                    ChangeState("Infiniminer.States.TeamSelectionState");
                                    if (!NoSound)
                                    {
                                        MediaPlayer.Stop();
                                    }
                                    propertyBag.blockEngine.DownloadComplete();
                                }
                            }
                            catch (Exception e)
                            {
                                Console.OpenStandardError();
                                Console.Error.WriteLine(e.Message);
                                Console.Error.WriteLine(e.StackTrace);
                                Console.Error.Close();
                            }
                        }
                        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(Defines.deathByExpl);                //"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;
                            propertyBag.playerList[playerId].AltColours = customColours;
                            propertyBag.playerList[playerId].redTeam    = red;
                            propertyBag.playerList[playerId].blueTeam   = blue;
                            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 = Defines.Sanitize(msgBuffer.ReadString());
                            //Time to break it up into multiple lines
                            propertyBag.addChatMessage(chatString, chatType, 10);
                        }
                        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;
                        }
                    }
                    catch { }         //Error in a received message
                }
                break;
                }
            }

            // Make sure our network thread actually gets to run.
            Thread.Sleep(1);
        }