コード例 #1
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);
        }
コード例 #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.
            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.ConnectionApproval:
                        anyPacketsReceived = true;
                        break;
                    case NetMessageType.ConnectionRejected:
                        {
                            anyPacketsReceived = false;
                            try
                            {
                                string[] reason = msgBuffer.ReadString().Split(";".ToCharArray());
                                if (reason.Length < 2 || reason[0] == "VER")
                                    System.Windows.Forms.MessageBox.Show("Error: client/server version incompability!\r\nServer: " + msgBuffer.ReadString() + "\r\nClient: " + Defines.INFINIMINER_VERSION);
                                else
                                    System.Windows.Forms.MessageBox.Show("Error: you are banned from this server!");
                            }
                            catch { }
                            ChangeState("Infiniminer.States.ServerBrowserState");
                        }
                        break;

                    case NetMessageType.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 - 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);
        }
コード例 #3
0
        public void SetBlock(ushort x, ushort y, ushort z, BlockType blockType, PlayerTeam team)
        {
            if (x <= 0 || y <= 0 || z <= 0 || (int)x >= MAPSIZE - 1 || (int)y >= MAPSIZE - 1 || (int)z >= MAPSIZE - 1)
                return;

            if (blockType == BlockType.BeaconRed || blockType == BlockType.BeaconBlue)
            {
                Beacon newBeacon = new Beacon();
                newBeacon.ID = GenerateBeaconID();
                newBeacon.Team = blockType == BlockType.BeaconRed ? PlayerTeam.Red : PlayerTeam.Blue;
                beaconList[new Vector3(x, y, z)] = newBeacon;
                SendSetBeacon(new Vector3(x, y + 1, z), newBeacon.ID, newBeacon.Team);
            }

            if (blockType == BlockType.None && (blockList[x, y, z] == BlockType.BeaconRed || blockList[x, y, z] == BlockType.BeaconBlue))
            {
                if (beaconList.ContainsKey(new Vector3(x, y, z)))
                    beaconList.Remove(new Vector3(x, y, z));
                SendSetBeacon(new Vector3(x, y + 1, z), "", PlayerTeam.None);
            }

            blockList[x, y, z] = blockType;
            blockCreatorTeam[x, y, z] = team;

            // x, y, z, type, all bytes
            NetBuffer msgBuffer = netServer.CreateBuffer();
            msgBuffer.Write((byte)InfiniminerMessage.BlockSet);
            msgBuffer.Write((byte)x);
            msgBuffer.Write((byte)y);
            msgBuffer.Write((byte)z);
            msgBuffer.Write((byte)blockType);
            foreach (NetConnection netConn in playerList.Keys)
                if (netConn.Status == NetConnectionStatus.Connected)
                    netServer.SendMessage(msgBuffer, netConn, NetChannel.ReliableUnordered);

            if (blockType == BlockType.Lava)
                lavaBlockCount += 1;

            //ConsoleWrite("BLOCKSET: " + x + " " + y + " " + z + " " + blockType.ToString());
        }
コード例 #4
0
        //dont forget duplicate function SetBlock
        public void SetBlockDebris(ushort x, ushort y, ushort z, BlockType blockType, PlayerTeam team)
        {
            if (x <= 0 || y <= 0 || z <= 0 || (int)x > MAPSIZE - 1 || (int)y > MAPSIZE - 1 || (int)z > MAPSIZE - 1)
                return;

            if (blockType == BlockType.None)//block removed, we must unsleep liquids nearby
            {
                Disturb(x, y, z);
            }

            blockListContent[x, y, z, 0] = 0;//dangerous stuff can happen if we dont set this

            if (blockType == BlockType.BeaconRed || blockType == BlockType.BeaconBlue)
            {
                Beacon newBeacon = new Beacon();
                newBeacon.ID = GenerateBeaconID();
                newBeacon.Team = blockType == BlockType.BeaconRed ? PlayerTeam.Red : PlayerTeam.Blue;
                beaconList[new Vector3(x, y, z)] = newBeacon;
                SendSetBeacon(new Vector3(x, y + 1, z), newBeacon.ID, newBeacon.Team);
            }
            else if (blockType == BlockType.Pipe)
            {
                blockListContent[x, y, z, 1] = 0;//Is pipe connected? [0-1]
                blockListContent[x, y, z, 2] = 0;//Is pipe a source? [0-1]
                blockListContent[x, y, z, 3] = 0;//Pipes connected
                blockListContent[x, y, z, 4] = 0;//Is pipe destination?
                blockListContent[x, y, z, 5] = 0;//src x
                blockListContent[x, y, z, 6] = 0;//src y
                blockListContent[x, y, z, 7] = 0;//src z
                blockListContent[x, y, z, 8] = 0;//pipe must not contain liquid
            }
            else if (blockType == BlockType.Barrel)
            {
                blockListContent[x, y, z, 1] = 0;//containtype
                blockListContent[x, y, z, 2] = 0;//amount
                blockListContent[x, y, z, 3] = 0;
            }
            else if (blockType == BlockType.Pump)
            {
                blockListContent[x, y, z, 1] = 0;//direction
                blockListContent[x, y, z, 2] = 0;//x input
                blockListContent[x, y, z, 3] = -1;//y input
                blockListContent[x, y, z, 4] = 0;//z input
                blockListContent[x, y, z, 5] = 0;//x output
                blockListContent[x, y, z, 6] = 1;//y output
                blockListContent[x, y, z, 7] = 0;//z output
            }

            if (blockType == BlockType.None && (blockList[x, y, z] == BlockType.BeaconRed || blockList[x, y, z] == BlockType.BeaconBlue))
            {
                if (beaconList.ContainsKey(new Vector3(x, y, z)))
                    beaconList.Remove(new Vector3(x, y, z));
                SendSetBeacon(new Vector3(x, y + 1, z), "", PlayerTeam.None);
            }

            if (blockType == blockList[x, y, z])//duplicate block, no need to send players data
            {
                blockList[x, y, z] = blockType;
                blockCreatorTeam[x, y, z] = team;
                flowSleep[x, y, z] = false;
            }
            else
            {

                blockList[x, y, z] = blockType;
                blockCreatorTeam[x, y, z] = team;
                flowSleep[x, y, z] = false;

                // x, y, z, type, all bytes
                NetBuffer msgBuffer = netServer.CreateBuffer();
                msgBuffer.Write((byte)InfiniminerMessage.BlockSetDebris);
                msgBuffer.Write((byte)x);
                msgBuffer.Write((byte)y);
                msgBuffer.Write((byte)z);
                if (blockType == BlockType.Vacuum)
                {
                    msgBuffer.Write((byte)BlockType.None);
                }
                else
                {
                    msgBuffer.Write((byte)blockType);
                }
                foreach (NetConnection netConn in playerList.Keys)
                    if (netConn.Status == NetConnectionStatus.Connected)
                        netServer.SendMessage(msgBuffer, netConn, NetChannel.ReliableUnordered);

            }
            //ConsoleWrite("BLOCKSET: " + x + " " + y + " " + z + " " + blockType.ToString());
        }
コード例 #5
0
        public void createBase(PlayerTeam team)
        {
            int pos = randGen.Next(10, 50);
            int posy = 61 - randGen.Next(10, 20);

            if(team == PlayerTeam.Red)
            {
                for (int a = -10; a < 10; a++)
                    for (int b = -3; b < 3; b++)
                        for (int c = -10; c < 10; c++)//clear rock
                        {
                            if (blockList[pos + a, posy + b, 50 + c] == BlockType.Rock)
                            {
                                blockList[pos + a, posy + b, 50 + c] = BlockType.Dirt;
                            }
                        }

                for (int a = -3; a < 3; a++)
                    for (int b = -2; b < 3; b++)
                        for (int c = -3; c < 3; c++)//place outer shell
                        {
                            blockList[pos + a, posy + b, 50 + c] = BlockType.SolidRed2;
                            blockListHP[pos + a, posy + b, 50 + c] = 400;
                            blockCreatorTeam[pos + a, posy + b, 50 + c] = PlayerTeam.None;
                        }

                for (int a = -2; a < 2; a++)
                    for (int b = -1; b < 2; b++)
                        for (int c = -2; c < 2; c++)//prevent players from adding stuff to it
                        {
                            blockList[pos + a, posy + b, 50 + c] = BlockType.Vacuum;
                        }

                blockList[pos, posy - 1, 50 - 3] = BlockType.TransRed;
                blockList[pos, posy, 50 - 3] = BlockType.TransRed;
                blockList[pos-1, posy - 1, 50 - 3] = BlockType.TransRed;
                blockList[pos-1, posy, 50 - 3] = BlockType.TransRed;

                blockList[pos, posy - 1, 50 - 4] = BlockType.None;
                blockList[pos, posy, 50 - 4] = BlockType.None;
                blockList[pos - 1, posy - 1, 50 - 4] = BlockType.None;
                blockList[pos - 1, posy, 50 - 4] = BlockType.None;

                RedBase = new PlayerBase();
                basePosition.Add(PlayerTeam.Red,RedBase);
                basePosition[PlayerTeam.Red].team = PlayerTeam.Red;
                basePosition[PlayerTeam.Red].X = pos;
                basePosition[PlayerTeam.Red].Y = posy;
                basePosition[PlayerTeam.Red].Z = 50;
                blockList[pos - 2, posy - 1, 51] = BlockType.BaseRed;
                //SetBlock((ushort)(pos - 2), (ushort)(posy - 1), 50, BlockType.BeaconRed, PlayerTeam.Red);
                blockList[pos - 2, posy - 1, 49] = BlockType.BankRed;

                Beacon newBeacon = new Beacon();
                newBeacon.ID = "HOME";
                newBeacon.Team = PlayerTeam.Red;
                beaconList[new Vector3(pos - 2, posy - 1, 50)] = newBeacon;
                SendSetBeacon(new Vector3(pos - 2, posy, 50), newBeacon.ID, newBeacon.Team);
            }
            else
            {
                for (int a = -10; a < 10; a++)
                    for (int b = -3; b < 3; b++)
                        for (int c = -10; c < 10; c++)
                        {
                            if (blockList[pos + a, posy + b, 14 + c] == BlockType.Rock)
                            {
                                blockList[pos + a, posy + b, 14 + c] = BlockType.Dirt;
                            }
                        }

                for (int a = -3; a < 3; a++)
                    for (int b = -3; b < 3; b++)
                        for (int c = -3; c < 3; c++)
                        {
                            blockList[pos + a, posy + b, 14 + c] = BlockType.SolidBlue2;
                            blockListHP[pos + a, posy + b, 14 + c] = 400;
                            blockCreatorTeam[pos + a, posy + b, 14 + c] = PlayerTeam.None;
                        }

                for (int a = -2; a < 2; a++)
                    for (int b = -1; b < 2; b++)
                        for (int c = -2; c < 2; c++)
                        {
                            blockList[pos + a, posy + b, 14 + c] = BlockType.Vacuum;
                        }

                blockList[pos, posy - 1, 14 + 2] = BlockType.TransBlue;
                blockList[pos, posy, 14 + 2] = BlockType.TransBlue;
                blockList[pos - 1, posy - 1, 14 + 2] = BlockType.TransBlue;
                blockList[pos - 1, posy, 14 + 2] = BlockType.TransBlue;

                blockList[pos, posy - 1, 14 + 3] = BlockType.None;
                blockList[pos, posy, 14 + 3] = BlockType.None;
                blockList[pos - 1, posy - 1, 14 + 3] = BlockType.None;
                blockList[pos - 1, posy, 14 + 3] = BlockType.None;

                BlueBase = new PlayerBase();
                basePosition.Add(PlayerTeam.Blue,BlueBase);
                basePosition[PlayerTeam.Blue].team = PlayerTeam.Blue;
                basePosition[PlayerTeam.Blue].X = pos;
                basePosition[PlayerTeam.Blue].Y = posy;
                basePosition[PlayerTeam.Blue].Z = 14;
                blockList[pos-2, posy-1, 13] = BlockType.BaseBlue;
                blockList[pos-2, posy-1, 15] = BlockType.BankBlue;
                //SetBlock((ushort)(pos - 2), (ushort)(posy - 1), 14, BlockType.BeaconBlue, PlayerTeam.Blue);
                Beacon newBeacon = new Beacon();
                newBeacon.ID = "HOME";
                newBeacon.Team = PlayerTeam.Blue;
                beaconList[new Vector3(pos - 2, posy - 1, 14)] = newBeacon;
                SendSetBeacon(new Vector3(pos - 2, posy, 14), newBeacon.ID, newBeacon.Team);
            }
        }
コード例 #6
0
        public bool ProcessCommand(string input, short authority, Player sender)
        {
            //if (authority == 0)
             //   return false;
            if (sender != null && authority > 0)
                sender.admin = GetAdmin(sender.IP);
            string[] args = input.Split(' '.ToString().ToCharArray(),2);
            if (args[0].StartsWith("/") && args[0].Length > 2)
                args[0] = args[0].Substring(1);
            switch (args[0].ToLower())
            {
                case "help":
                    {
                        if (sender == null)
                        {
                            ConsoleWrite("SERVER CONSOLE COMMANDS:");
                            ConsoleWrite(" fps");
                            ConsoleWrite(" physics");
                            ConsoleWrite(" announce");
                            ConsoleWrite(" players");
                            ConsoleWrite(" kick <ip>");
                            ConsoleWrite(" kickn <name>");
                            ConsoleWrite(" ban <ip>");
                            ConsoleWrite(" bann <name>");
                            ConsoleWrite(" say <message>");
                            ConsoleWrite(" save <mapfile>");
                            ConsoleWrite(" load <mapfile>");
                            ConsoleWrite(" toggle <var>");
                            ConsoleWrite(" <var> <value>");
                            ConsoleWrite(" <var>");
                            ConsoleWrite(" listvars");
                            ConsoleWrite(" status");
                            ConsoleWrite(" restart");
                            ConsoleWrite(" quit");
                        }
                        else
                        {
                            SendServerMessageToPlayer(sender.Handle + ", the " + args[0].ToLower() + " command is only for use in the server console.", sender.NetConn);
                        }
                    }
                    break;
                case "players":
                    {
                        if (sender == null)
                        {
                            ConsoleWrite("( " + playerList.Count + " / " + varGetI("maxplayers") + " )");
                            foreach (Player p in playerList.Values)
                            {
                                string teamIdent = "";
                                if (p.Team == PlayerTeam.Red)
                                    teamIdent = " (R)";
                                else if (p.Team == PlayerTeam.Blue)
                                    teamIdent = " (B)";
                                if (p.IsAdmin)
                                    teamIdent += " (Admin)";
                                ConsoleWrite(p.Handle + teamIdent);
                                ConsoleWrite("  - " + p.IP);
                            }
                        }else{
                            SendServerMessageToPlayer(sender.Handle + ", the " + args[0].ToLower() + " command is only for use in the server console.", sender.NetConn);
                        }
                    }
                    break;
                case "rename":
                    {
                        if (sender != null)
                        if (args.Length == 2 && sender.Alive)
                        {
                            if (args[1].Length < 11)
                            {
                                int px = (int)sender.Position.X;
                                int py = (int)sender.Position.Y;
                                int pz = (int)sender.Position.Z;

                                for (int x = -1+px; x < 2+px; x++)
                                    for (int y = -1+py; y < 2+py; y++)
                                        for (int z = -1+pz; z < 2+pz; z++)
                                        {
                                            if (x < 1 || y < 1 || z < 1 || x > MAPSIZE - 2 || y > MAPSIZE - 2 || z > MAPSIZE - 2)
                                            {
                                                //out of map
                                            }
                                            else if (blockList[x, y, z] == BlockType.BeaconRed && sender.Team == PlayerTeam.Red)
                                            {
                                                SendServerMessageToPlayer("You renamed " + beaconList[new Vector3(x, y, z)].ID + " to " + args[1].ToUpper() + ".", sender.NetConn);
                                                if (beaconList.ContainsKey(new Vector3(x, y, z)))
                                                    beaconList.Remove(new Vector3(x, y, z));
                                                SendSetBeacon(new Vector3(x, y + 1, z), "", PlayerTeam.None);

                                                Beacon newBeacon = new Beacon();
                                                newBeacon.ID = args[1].ToUpper();
                                                newBeacon.Team = PlayerTeam.Red;
                                                beaconList[new Vector3(x, y, z)] = newBeacon;
                                                SendSetBeacon(new Vector3(x, y + 1, z), newBeacon.ID, newBeacon.Team);

                                                return true;
                                            }
                                            else if (blockList[x, y, z] == BlockType.BeaconBlue && sender.Team == PlayerTeam.Blue)
                                            {
                                                SendServerMessageToPlayer("You renamed " + beaconList[new Vector3(x, y, z)].ID + " to " + args[1].ToUpper() + ".", sender.NetConn);
                                                if (beaconList.ContainsKey(new Vector3(x, y, z)))
                                                    beaconList.Remove(new Vector3(x, y, z));
                                                SendSetBeacon(new Vector3(x, y + 1, z), "", PlayerTeam.None);

                                                Beacon newBeacon = new Beacon();
                                                newBeacon.ID = args[1].ToUpper();
                                                newBeacon.Team = PlayerTeam.Blue;
                                                beaconList[new Vector3(x, y, z)] = newBeacon;
                                                SendSetBeacon(new Vector3(x, y + 1, z), newBeacon.ID, newBeacon.Team);

                                                return true;
                                            }
                                        }

                                SendServerMessageToPlayer("You must be closer to the beacon.", sender.NetConn);

                            }
                            else
                            {
                                SendServerMessageToPlayer("Beacons are restricted to 10 characters.", sender.NetConn);
                            }
                        }
                    }
                    break;
                case "fps":
                    {
                        ConsoleWrite("Server FPS:"+frameCount );
                    }
                    break;
                case "physics":
                    {
                        if (authority > 0)
                        {
                            physicsEnabled = !physicsEnabled;
                            ConsoleWrite("Physics state is now: " + physicsEnabled);
                        }
                    }
                    break;
                case "liquid":
                    {
                        if (authority > 0)
                        {
                            lavaBlockCount = 0;
                            waterBlockCount = 0;
                            int tempBlockCount = 0;

                            for (ushort i = 0; i < MAPSIZE; i++)
                                for (ushort j = 0; j < MAPSIZE; j++)
                                    for (ushort k = 0; k < MAPSIZE; k++)
                                    {
                                        if (blockList[i, j, k] == BlockType.Lava)
                                        {
                                            lavaBlockCount += 1;
                                            if (blockListContent[i, j, k, 1] > 0)
                                            {
                                                tempBlockCount += 1;
                                            }
                                        }
                                        else if (blockList[i, j, k] == BlockType.Water)
                                        {
                                            waterBlockCount += 1;
                                        }
                                    }

                            ConsoleWrite(waterBlockCount + " water blocks, " + lavaBlockCount + " lava blocks.");
                            ConsoleWrite(tempBlockCount + " temporary blocks.");
                        }
                    }
                    break;
                case "flowsleep":
                    {
                        if (authority > 0)
                        {
                            uint sleepcount = 0;

                            for (ushort i = 0; i < MAPSIZE; i++)
                                for (ushort j = 0; j < MAPSIZE; j++)
                                    for (ushort k = 0; k < MAPSIZE; k++)
                                        if (flowSleep[i, j, k] == true)
                                            sleepcount += 1;

                            ConsoleWrite(sleepcount + " liquids are happily sleeping.");
                        }
                    }
                    break;
                case "admins":
                    {
                        if (authority > 0)
                        {
                            ConsoleWrite("Admin list:");
                            foreach (string ip in admins.Keys)
                                ConsoleWrite(ip);
                        }
                    }
                    break;
                case "admin":
                    {
                        if (authority > 0)
                        {
                            if (args.Length == 2)
                            {
                                if (sender == null || sender.admin >= 2)
                                    AdminPlayer(args[1]);
                                else
                                    SendServerMessageToPlayer("You do not have the authority to add admins.", sender.NetConn);
                            }
                        }
                    }
                    break;
                case "adminn":
                    {
                        if (authority > 0)
                        {
                            if (args.Length == 2)
                            {
                                if (sender == null || sender.admin >= 2)
                                    AdminPlayer(args[1], true);
                                else
                                    SendServerMessageToPlayer("You do not have the authority to add admins.", sender.NetConn);
                            }
                        }
                    }
                    break;
                case "listvars":
                    if (sender==null)
                        varList(true);
                    else{
                        SendServerMessageToPlayer(sender.Handle + ", the " + args[0].ToLower() + " command is only for use in the server console.", sender.NetConn);
                    }
                    break;
                case "status":
                    if (sender == null)
                        status();
                    else
                        SendServerMessageToPlayer(sender.Handle + ", the " + args[0].ToLower() + " command is only for use in the server console.", sender.NetConn);
                    break;
                case "announce":
                    {
                        if (authority > 0)
                        {
                            PublicServerListUpdate(true);
                        }
                    }
                    break;
                case "kick":
                    {
                        if (authority>=1&&args.Length == 2)
                        {
                            if (sender != null)
                                ConsoleWrite("SERVER: " + sender.Handle + " has kicked " + args[1]);
                            KickPlayer(args[1]);
                        }
                    }
                    break;
                case "kickn":
                    {
                        if (authority >= 1 && args.Length == 2)
                        {
                            if (sender != null)
                                ConsoleWrite("SERVER: " + sender.Handle + " has kicked " + args[1]);
                            KickPlayer(args[1], true);
                        }
                    }
                    break;

                case "ban":
                    {
                        if (authority >= 1 && args.Length == 2)
                        {
                            if (sender != null)
                                ConsoleWrite("SERVER: " + sender.Handle + " has banned " + args[1]);
                            BanPlayer(args[1]);
                            KickPlayer(args[1]);
                        }
                    }
                    break;

                case "bann":
                    {
                        if (authority >= 1 && args.Length == 2)
                        {
                            if (sender != null)
                                ConsoleWrite("SERVER: " + sender.Handle + " has banned " + args[1]);
                            BanPlayer(args[1], true);
                            KickPlayer(args[1], true);
                        }
                    }
                    break;

                case "toggle":
                    if (authority >= 1 && args.Length == 2)
                    {
                        int exists = varExists(args[1]);
                        if (exists == 1)
                        {
                            bool val = varGetB(args[1]);
                            varSet(args[1], !val);
                        }
                        else if (exists == 2)
                            ConsoleWrite("Cannot toggle a string value.");
                        else
                            varReportStatus(args[1]);
                    }
                    else
                        ConsoleWrite("Need variable name to toggle!");
                    break;
                case "quit":
                    {
                        if (authority >= 2){
                            if ( sender!=null)
                                ConsoleWrite(sender.Handle + " is shutting down the server.");
                             keepRunning = false;
                        }
                    }
                    break;

                case "restart":
                    {
                        if (authority >= 2){
                            if (sender != null)
                                ConsoleWrite(sender.Handle + " is restarting the server.");
                            else
                            {
                                ConsoleWrite("Restarting server in 5 seconds.");
                            }
                            //disconnectAll();

                            SendServerMessage("Server restarting in 5 seconds.");
                            restartTriggered = true;
                            restartTime = DateTime.Now+TimeSpan.FromSeconds(5);
                        }
                    }
                    break;

                case "say":
                    {
                        if (authority > 0)
                        {
                            if (args.Length == 2)
                            {
                                string message = "SERVER: " + args[1];
                                SendServerMessage(message);
                            }
                        }
                    }
                    break;

                case "save":
                    {
                        if (authority > 0)
                        {
                            if (args.Length >= 2)
                            {
                                if (sender != null)
                                    ConsoleWrite(sender.Handle + " is saving the map.");
                                SaveLevel(args[1]);
                            }
                        }
                    }
                    break;

                case "load":
                    {
                        if (authority > 0)
                        {
                            if (args.Length >= 2)
                            {
                                if (sender != null)
                                    ConsoleWrite(sender.Handle + " is loading a map.");
                                physicsEnabled = false;
                                Thread.Sleep(2);
                                LoadLevel(args[1]);
                                physicsEnabled = true;
                                /*if (LoadLevel(args[1]))
                                    Console.WriteLine("Loaded level " + args[1]);
                                else
                                    Console.WriteLine("Level file not found!");*/
                            }
                            else if (levelToLoad != "")
                            {
                                physicsEnabled = false;
                                Thread.Sleep(2);
                                LoadLevel(levelToLoad);
                                physicsEnabled = true;
                            }
                        }
                    }
                    break;
                default: //Check / set var
                    {
                        if (authority == 0)
                            return false;

                        string name = args[0];
                        int exists = varExists(name);
                        if (exists > 0)
                        {
                            if (args.Length == 2)
                            {
                                try
                                {
                                    if (exists == 1)
                                    {
                                        bool newVal = false;
                                        newVal = bool.Parse(args[1]);
                                        varSet(name, newVal);
                                    }
                                    else if (exists == 2)
                                    {
                                        varSet(name, args[1]);
                                    }
                                    else if (exists == 3)
                                    {
                                        varSet(name, Int32.Parse(args[1]));
                                    }

                                }
                                catch { }
                            }
                            else
                            {
                                if (sender==null)
                                    varReportStatus(name);
                                else
                                    SendServerMessageToPlayer(sender.Handle + ": The " + args[0].ToLower() + " command is only for use in the server console.",sender.NetConn);
                            }
                        }
                        else
                        {
                            char first = args[0].ToCharArray()[0];
                            if (first == 'y' || first == 'Y')
                            {
                                string message = "SERVER: " + args[0].Substring(1);
                                if (args.Length > 1)
                                    message += (message != "SERVER: " ? " " : "") + args[1];
                                SendServerMessage(message);
                            }
                            else
                            {
                                if (sender == null)
                                    ConsoleWrite("Unknown command/var.");
                                return false;
                            }
                        }
                    }
                    break;
            }
            return true;
        }
コード例 #7
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.
            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:
                    {
                        byte x = msgBuffer.ReadByte();
                        byte 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();
                        }
                    }
                    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);
        }
コード例 #8
0
ファイル: InfiniminerServer.cs プロジェクト: Xeio/Infiniminer
        public void SetBlock(ushort x, ushort y, ushort z, BlockType blockType, PlayerTeam team)
        {
            if (x < 0 || y < 0 || z < 0 || x >= GlobalVariables.MAPSIZE || y >= GlobalVariables.MAPSIZE || z >= GlobalVariables.MAPSIZE)
                return;
            var oldBlockType = blockList[x, y, z];

            if (blockType == BlockType.BeaconRed || blockType == BlockType.BeaconBlue)
            {
                Beacon newBeacon = new Beacon();
                newBeacon.ID = GenerateBeaconID();
                newBeacon.Team = blockType == BlockType.BeaconRed ? PlayerTeam.Red : PlayerTeam.Blue;
                beaconList[new Vector3(x, y, z)] = newBeacon;
                SendSetBeacon(new Vector3(x, y+1, z), newBeacon.ID, newBeacon.Team);
            }

            if (blockType == BlockType.None && (blockList[x, y, z] == BlockType.BeaconRed || blockList[x, y, z] == BlockType.BeaconBlue))
            {
                if (beaconList.ContainsKey(new Vector3(x,y,z)))
                    beaconList.Remove(new Vector3(x,y,z));
                SendSetBeacon(new Vector3(x, y+1, z), "", PlayerTeam.None);
            }

            blockList[x, y, z] = blockType;
            blockCreatorTeam[x, y, z] = team;

            // x, y, z, type, all bytes
            NetBuffer msgBuffer = netServer.CreateBuffer();
            msgBuffer.Write((byte)InfiniminerMessage.BlockSet);
            msgBuffer.Write((byte)x);
            msgBuffer.Write((byte)y);
            msgBuffer.Write((byte)z);
            msgBuffer.Write((byte)blockType);
            foreach (NetConnection netConn in playerList.Keys)
                if (netConn.Status == NetConnectionStatus.Connected)
                    netServer.SendMessage(msgBuffer, netConn, NetChannel.ReliableUnordered);

            if (oldBlockType == BlockType.Lava && blockType != BlockType.Lava)
            {
                LavaBlocks.Remove(new Point3D() { X = x, Y = y, Z = z });
            }
            else if (blockType == BlockType.Lava && oldBlockType != BlockType.Lava)
            {
                LavaBlocks.Add(new Point3D() { X = x, Y = y, Z = z }, 0);
            }

            //ConsoleWrite("BLOCKSET: " + x + " " + y + " " + z + " " + blockType.ToString());
        }
コード例 #9
0
        public void UpdateNetwork(GameTime gameTime)
        {
            // Update the server with our status.
            timeSinceLastUpdate += gameTime.ElapsedGameTime.TotalSeconds;
            if (timeSinceLastUpdate > NETWORK_UPDATE_TIME)
            {
                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");//needed to reset

                                Thread.Sleep(50);
                                JoinGame(lastConnection);//attempts to reconnect
                                ChangeState("Infiniminer.States.LoadingState");
                            }
                        }
                        break;
                    case NetMessageType.ConnectionApproval:
                        anyPacketsReceived = true;
                        break;
                    case NetMessageType.ConnectionRejected:
                        {
                            anyPacketsReceived = false;
                            try
                            {
                                string[] reason = msgBuffer.ReadString().Split(";".ToCharArray());
                                if (reason.Length < 2 || reason[0] == "VER")
                                    System.Windows.Forms.MessageBox.Show("Error: client/server version incompability!\r\nServer: " + msgBuffer.ReadString() + "\r\nClient: " + Defines.INFINIMINER_VERSION);
                                else
                                    System.Windows.Forms.MessageBox.Show("Error: you are banned from this server!");
                            }
                            catch { }
                            ChangeState("Infiniminer.States.ServerBrowserState");
                        }
                        break;

                    case NetMessageType.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 - 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();

                                                    lastConnection = new IPEndPoint(propertyBag.netClient.ServerConnection.RemoteEndpoint.Address, 5565);
                                                    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.SetItem:
                                        {
                                            ItemType iType = (ItemType)(msgBuffer.ReadByte());
                                            Item newItem = new Item((Game)this,iType);
                                            newItem.ID = msgBuffer.ReadUInt32();
                                            newItem.Position = msgBuffer.ReadVector3();
                                            newItem.Team = (PlayerTeam)msgBuffer.ReadByte();
                                            newItem.Heading = msgBuffer.ReadVector3();
                                            newItem.deltaPosition = newItem.Position;
                                            newItem.Content[1] = msgBuffer.ReadInt32();
                                            newItem.Content[2] = msgBuffer.ReadInt32();
                                            newItem.Content[3] = msgBuffer.ReadInt32();
                                            newItem.Content[10] = msgBuffer.ReadInt32();
                                            propertyBag.itemList.Add(newItem.ID, newItem);
                                        }
                                        break;
                                    case InfiniminerMessage.ActiveArtifactUpdate:
                                        {
                                            propertyBag.artifactActive[msgBuffer.ReadByte(), msgBuffer.ReadInt32()] = msgBuffer.ReadInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.ItemUpdate:
                                        {
                                            uint id = msgBuffer.ReadUInt32();

                                            //if (propertyBag.itemList.ContainsKey(id))
                                            propertyBag.itemList[id].Position = msgBuffer.ReadVector3();
                                        }
                                        break;
                                    case InfiniminerMessage.ItemScaleUpdate:
                                        {

                                            uint id = msgBuffer.ReadUInt32();

                                            //if (propertyBag.itemList.ContainsKey(id))
                                            propertyBag.itemList[id].Scale = msgBuffer.ReadFloat();
                                        }
                                        break;
                                    case InfiniminerMessage.ItemContentSpecificUpdate:
                                        {

                                            uint id = msgBuffer.ReadUInt32();
                                            uint cc = msgBuffer.ReadUInt32();
                                            //if (propertyBag.itemList.ContainsKey(id))
                                            propertyBag.itemList[id].Content[cc] = msgBuffer.ReadInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.SetItemRemove:
                                        {
                                            uint id = msgBuffer.ReadUInt32();

                                            if (propertyBag.itemList.ContainsKey(id))
                                                propertyBag.itemList.Remove(id);

                                        }
                                        break;

                                    case InfiniminerMessage.TriggerConstructionGunAnimation:
                                        {
                                            propertyBag.constructionGunAnimation = msgBuffer.ReadFloat();
                                            if (propertyBag.constructionGunAnimation <= -0.1)
                                                propertyBag.PlaySound(InfiniminerSound.RadarSwitch);
                                        }
                                        break;
                                    case InfiniminerMessage.ScoreUpdate:
                                        {
                                            propertyBag.teamArtifactsRed = msgBuffer.ReadUInt32();
                                            propertyBag.teamArtifactsBlue = msgBuffer.ReadUInt32();
                                            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();
                                            propertyBag.teamArtifactsRed = msgBuffer.ReadUInt32();
                                            propertyBag.teamArtifactsBlue = msgBuffer.ReadUInt32();
                                            propertyBag.winningCashAmount = msgBuffer.ReadUInt32();
                                            propertyBag.playerHealth = msgBuffer.ReadUInt32();
                                            propertyBag.playerHealthMax = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.TeamCashUpdate:
                                        {
                                            // ore, cash, weight, max ore, max weight, team ore, red cash, blue cash, all uint
                                            propertyBag.teamRedCash = msgBuffer.ReadUInt32();
                                            propertyBag.teamBlueCash = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.TeamOreUpdate:
                                        {
                                           propertyBag.teamOre = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.HealthUpdate:
                                        {
                                            propertyBag.playerHealth = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.PlayerSlap:
                                        {
                                            uint pID = msgBuffer.ReadUInt32();
                                            uint aID = msgBuffer.ReadUInt32();

                                            if (pID == propertyBag.playerMyId)
                                            {
                                                propertyBag.screenEffect = ScreenEffect.Fall;
                                                propertyBag.screenEffectCounter = 2 - 0.5;

                                                if (aID == 0)//bomb or other hurt
                                                {
                                                    propertyBag.particleEngine.CreateBloodSplatter(propertyBag.playerPosition, propertyBag.playerTeam == PlayerTeam.Red ? Color.Red : Color.Blue, 0.4f);
                                                }
                                                else//player attack
                                                {
                                                    propertyBag.particleEngine.CreateBloodSplatter(propertyBag.playerPosition, propertyBag.playerTeam == PlayerTeam.Red ? Color.Red : Color.Blue, 0.2f);
                                                    propertyBag.forceVector = propertyBag.playerList[aID].Heading;
                                                    propertyBag.forceVector.Y = 0;
                                                    //propertyBag.forceVector.Normalize();
                                                    if (propertyBag.artifactActive[(byte)propertyBag.playerTeam, 9] > 0)//stone artifact prevents kb
                                                    {
                                                        if (propertyBag.artifactActive[(byte)propertyBag.playerTeam, 9] < 4)
                                                            propertyBag.forceStrength = 4.0f - (float)propertyBag.artifactActive[(byte)propertyBag.playerTeam, 9];
                                                        else
                                                            propertyBag.forceStrength = 0;
                                                    }
                                                    else if (propertyBag.Content[10] == 9)//stone artifact
                                                    {
                                                        propertyBag.forceStrength = 0;
                                                    }
                                                    else
                                                    {
                                                        propertyBag.forceStrength = 4.0f;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (aID == 0)//bomb or other hurt
                                                {
                                                    propertyBag.particleEngine.CreateBloodSplatter(propertyBag.playerList[pID].Position, propertyBag.playerList[pID].Team == PlayerTeam.Red ? Color.Red : Color.Blue, 0.4f);
                                                }
                                                else
                                                {
                                                    propertyBag.particleEngine.CreateBloodSplatter(propertyBag.playerList[pID].Position, propertyBag.playerList[pID].Team == PlayerTeam.Red ? Color.Red : Color.Blue, 0.2f);
                                                }
                                            }

                                        }
                                        break;
                                    case InfiniminerMessage.WeightUpdate:
                                        {
                                            propertyBag.playerWeight = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.OreUpdate:
                                        {
                                            propertyBag.playerOre = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.CashUpdate:
                                        {
                                            propertyBag.playerCash = msgBuffer.ReadUInt32();
                                        }
                                        break;
                                    case InfiniminerMessage.ContentUpdate:
                                        {
                                            //update all player content values
                                            for (int a = 0; a < 50; a++)
                                            {
                                                propertyBag.Content[a] = msgBuffer.ReadInt32();
                                            }
                                        }
                                        break;
                                    case InfiniminerMessage.ContentSpecificUpdate:
                                        {
                                            // update specific value
                                            int val = msgBuffer.ReadInt32();
                                            propertyBag.Content[val] = msgBuffer.ReadInt32();

                                            if (val == 10)//artifact change
                                            {
                                                switch(propertyBag.Content[val])
                                                {
                                                    case 1:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", granting 10 ore periodically!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 2:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", granting life stealing attacks!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 3:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", granting powerful life regeneration, and regenerating any nearby blocks when thrown!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 4:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", granting water breathing and digging underwater!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 5:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", transmuting every 100 ore into 10 gold!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 6:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", shocking nearby enemies, and creating a torrential downpour when thrown!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 7:
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", reflecting half of damage taken!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 8://medical
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", healing friendlies with each swing; however you may no longer hit enemies!", ChatMessageType.SayAll, 10);
                                                        break;
                                                    case 9://stone
                                                        propertyBag.addChatMessage("You now wield the " + ArtifactInformation.GetName(propertyBag.Content[val]) + ", making you immune to knockback and resistant to fall damage!", ChatMessageType.SayAll, 10);
                                                        break;
                                                }
                                            }
                                        }
                                        break;
                                    case InfiniminerMessage.PlayerPosition:
                                        {
                                            // ore, cash, weight, max ore, max weight, team ore, red cash, blue cash, all uint
                                            propertyBag.playerPosition = msgBuffer.ReadVector3();
                                        }
                                        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.particleEngine.CreateExplosionDebris(new Vector3(x, y, z));
                                                }
                                                propertyBag.blockEngine.AddBlock(x, y, z, blockType);

                                                //if(!propertyBag.playerDead)
                                                //CheckForStandingInLava();
                                            }
                                        }
                                        break;
                                    case InfiniminerMessage.BlockSetDebris:
                                        {
                                            // x, y, z, type, all bytes
                                            byte x = msgBuffer.ReadByte();
                                            byte y = msgBuffer.ReadByte();
                                            byte z = msgBuffer.ReadByte();
                                            BlockType blockType = (BlockType)msgBuffer.ReadByte();
                                            Vector3 tv = new Vector3(x, y, z);

                                            if (blockType == BlockType.None)
                                            {
                                                if (propertyBag.blockEngine.BlockAtPoint(tv) != BlockType.None)
                                                {
                                                    float distFromDebris = (tv + 0.5f * Vector3.One - propertyBag.playerPosition).Length();

                                                    propertyBag.particleEngine.CreateBlockDebris(tv + 0.5f * Vector3.One, propertyBag.blockEngine.blockList[x, y, z], Math.Min(14.0f - distFromDebris, 10.0f));
                                                    propertyBag.blockEngine.RemoveBlock(x, y, z);

                                                }

                                            }

                                        }
                                        break;
                                    case InfiniminerMessage.TriggerEarthquake:
                                        {
                                            Vector3 blockPos = msgBuffer.ReadVector3();
                                            uint expStrength = msgBuffer.ReadUInt32();

                                            // Play the explosion sound.
                                            propertyBag.PlaySound(InfiniminerSound.Explosion, blockPos);//, (int)(expStrength/1.5));

                                            // Create some particles.
                                            propertyBag.particleEngine.CreateExplosionDebris(blockPos);

                                            // Figure out what the effect is.
                                            float distFromExplosive = (blockPos + 0.5f * Vector3.One - propertyBag.playerPosition).Length();

                                            propertyBag.screenEffectCounter = Math.Min(propertyBag.screenEffectCounter, 1 / 5);

                                        }
                                        break;
                                    case InfiniminerMessage.TriggerExplosion:
                                        {
                                            Vector3 blockPos = msgBuffer.ReadVector3();
                                            uint expStrength = msgBuffer.ReadUInt32();

                                            // 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.Effect:
                                        {
                                            Vector3 blockPos = msgBuffer.ReadVector3();
                                            uint effectType = msgBuffer.ReadUInt32();
                                            float distFromDebris = (blockPos + 0.5f * Vector3.One - propertyBag.playerPosition).Length();

                                            if(distFromDebris < 14 && effectType == 1)
                                                propertyBag.particleEngine.CreateHidden(blockPos, Color.GhostWhite);
                                        }
                                        break;
                                  case InfiniminerMessage.TriggerDebris:
                                        {
                                            Vector3 blockPos = msgBuffer.ReadVector3();
                                            BlockType blockType = (BlockType)msgBuffer.ReadByte();
                                            uint debrisType = msgBuffer.ReadUInt32();
                                            // Play the debris sound.
                                            //propertyBag.PlaySound(InfiniminerSound.Explosion, blockPos);

                                            // Create some particles.

                                            float distFromDebris = (blockPos + 0.5f * Vector3.One - propertyBag.playerPosition).Length();

                                            if (debrisType == 1)//block was destroyed
                                            {
                                                    propertyBag.particleEngine.CreateBlockDebris(blockPos, blockType, Math.Min(20.0f - distFromDebris, 10.0f));
                                            }
                                            else if(debrisType == 0)//other players digging
                                            {
                                                if (distFromDebris < 24)
                                                    propertyBag.particleEngine.CreateDiggingDebris(blockPos, blockType);
                                            }
                                            else if (debrisType == 2)//block dmg debris
                                            {
                                                if (distFromDebris < 16)
                                                    if (blockType == BlockType.SolidRed)
                                                        propertyBag.particleEngine.CreateBloodSplatter(blockPos, Color.Red, 1.0f);
                                                    else
                                                        propertyBag.particleEngine.CreateBloodSplatter(blockPos, Color.Blue, 1.0f);
                                            }
                                            else if (debrisType > 10)//anything over this determines damage intensity for hurt effect
                                            {
                                                if (distFromDebris < 15)
                                                {
                                                    if (blockType == BlockType.SolidRed)
                                                        propertyBag.particleEngine.CreateBloodSplatter(blockPos, Color.Red, (float)(debrisType)/100);
                                                    else
                                                        propertyBag.particleEngine.CreateBloodSplatter(blockPos, Color.Blue, (float)(debrisType)/100);
                                                }
                                            }

                                        }
                                        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.PlayerSetClass:
                                        {
                                            uint playerId = msgBuffer.ReadUInt32();
                                            if (propertyBag.playerList.ContainsKey(playerId))
                                            {
                                                Player player = propertyBag.playerList[playerId];
                                                player.Class = (PlayerClass)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.PlayerContentUpdate:
                                        {
                                            uint playerId = msgBuffer.ReadUInt32();
                                            uint cc = msgBuffer.ReadUInt32();
                                            int val = msgBuffer.ReadInt32();

                                            propertyBag.playerList[playerId].Content[cc] = val;
                                        }
                                        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, 2.0f);
                                                if (playerId != propertyBag.playerMyId)
                                                    propertyBag.PlaySound(InfiniminerSound.Death, player.Position);
                                                else if(propertyBag.playerDead == false)
                                                {
                                                    propertyBag.PlaySound(InfiniminerSound.Death, player.Position);
                                                    propertyBag.playerVelocity = Vector3.Zero;
                                                    propertyBag.playerDead = true;
                                                    propertyBag.allowRespawn = false;
                                                    propertyBag.screenEffect = ScreenEffect.Death;
                                                    propertyBag.screenEffectCounter = 0;
                                                    propertyBag.Content[5] = 0;
                                                }
                                            }
                                        }
                                        break;

                                    case InfiniminerMessage.PlayerAlive:
                                        {
                                            uint playerId = msgBuffer.ReadUInt32();
                                            if (propertyBag.playerList.ContainsKey(playerId))
                                            {
                                                Player player = propertyBag.playerList[playerId];
                                                player.Alive = true;
                                            }
                                        }
                                        break;

                                    case InfiniminerMessage.PlayerRespawn:
                                        {
                                            //propertyBag.playerList[propertyBag.playerMyId].UpdatePosition(msgBuffer.ReadVector3(), gameTime.TotalGameTime.TotalSeconds);
                                            propertyBag.playerPosition = msgBuffer.ReadVector3();
                                            propertyBag.allowRespawn = 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);
                                                player.Health = (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);
        }
コード例 #10
0
        public void SetBlock(ushort x, ushort y, ushort z, BlockType blockType, PlayerTeam team)
        {
            if (x <= 0 || y <= 0 || z <= 0 || (int)x > MAPSIZE - 1 || (int)y > MAPSIZE - 1 || (int)z > MAPSIZE - 1)
                return;

            if (blockType == BlockType.None)//block removed, we must unsleep liquids nearby
            {
                Disturb(x, y, z);
            }
            if (blockType == BlockType.BeaconRed || blockType == BlockType.BeaconBlue)
            {
                Beacon newBeacon = new Beacon();
                newBeacon.ID = GenerateBeaconID();
                newBeacon.Team = blockType == BlockType.BeaconRed ? PlayerTeam.Red : PlayerTeam.Blue;
                beaconList[new Vector3(x, y, z)] = newBeacon;
                SendSetBeacon(new Vector3(x, y+1, z), newBeacon.ID, newBeacon.Team);
            }
            else if(blockType == BlockType.Pipe)
            {
                blockListContent[x, y, z, 0] = 0;//state
                blockListContent[x, y, z, 1] = 0;
                blockListContent[x, y, z, 2] = 0;
                blockListContent[x, y, z, 3] = 0;
            }
            else if (blockType == BlockType.Compressor)
            {
                blockListContent[x, y, z, 0] = 0;//state
                blockListContent[x, y, z, 1] = 0;//containtype
                blockListContent[x, y, z, 2] = 0;//amount
                blockListContent[x, y, z, 3] = 0;
            }
            else if (blockType == BlockType.Pump)
            {
                blockListContent[x, y, z, 0] = 0;//state
                blockListContent[x, y, z, 1] = 0;//direction
                blockListContent[x, y, z, 2] = 0;//x input
                blockListContent[x, y, z, 3] = -1;//y input
                blockListContent[x, y, z, 4] = 0;//z input
                blockListContent[x, y, z, 5] = 0;//x output
                blockListContent[x, y, z, 6] = 1;//y output
                blockListContent[x, y, z, 7] = 0;//z output
            }

            if (blockType == BlockType.None && (blockList[x, y, z] == BlockType.BeaconRed || blockList[x, y, z] == BlockType.BeaconBlue))
            {
                if (beaconList.ContainsKey(new Vector3(x,y,z)))
                    beaconList.Remove(new Vector3(x,y,z));
                SendSetBeacon(new Vector3(x, y+1, z), "", PlayerTeam.None);
            }

            blockList[x, y, z] = blockType;
            blockCreatorTeam[x, y, z] = team;
            flowSleep[x, y, z] = false;

            // x, y, z, type, all bytes
            NetBuffer msgBuffer = netServer.CreateBuffer();
            msgBuffer.Write((byte)InfiniminerMessage.BlockSet);
            msgBuffer.Write((byte)x);
            msgBuffer.Write((byte)y);
            msgBuffer.Write((byte)z);
            if (blockType == BlockType.Vacuum)
            {
                msgBuffer.Write((byte)BlockType.None);
            }
            else
            {
                msgBuffer.Write((byte)blockType);
            }
            foreach (NetConnection netConn in playerList.Keys)
                if (netConn.Status == NetConnectionStatus.Connected)
                    netServer.SendMessage(msgBuffer, netConn, NetChannel.ReliableUnordered);

            //ConsoleWrite("BLOCKSET: " + x + " " + y + " " + z + " " + blockType.ToString());
        }