public BombList(Player player, int capacity)
 {
     array = new Bomb[capacity];
     for (int i = 0; i < capacity; ++i)
     {
         array[i] = new Bomb(player);
     }
 }
        public int GetAlivePlayers(Player[] array, Player ignoredPlayer = null)
        {
            int count = 0;
            for (int i = 0; i < m_list.Count; ++i)
            {
                Player player = m_list[i];
                if (player != ignoredPlayer && player.IsAlive)
                {
                    array[count] = player;
                    ++count;
                }
            }

            return count;
        }
        public DiseaseList(Player player)
        {
            this.m_player = player;

            remains = new float[diseaseArray.Length];

            flags = new BitArray(diseaseArray.Length);
            oldFlags = new BitArray(diseaseArray.Length);

            randomIndices = new int[diseaseArray.Length];

            for (int i = 0; i < randomIndices.Length; ++i)
            {
                randomIndices[i] = i;
            }
        }
        protected override void OnStart()
        {
            base.OnStart();

            Application.SetWindowTitle("Server");

            game = new Game(MultiplayerMode.Server);

            networkPlayersLookup = new Dictionary<NetConnection, Player>();
            networkPlayers = new List<Player>();

            // local players are ready
            int playerIndex = 0;
            GameSettings.InputEntry[] entries = settings.inputEntries;
            for (; playerIndex < entries.Length; ++playerIndex)
            {
                Player player = new Player(entries[playerIndex].playerIndex);
                player.SetPlayerInput(entries[playerIndex].input);
                player.IsReady = true;
                game.AddPlayer(player);
            }

            // network players are not ready
            List<NetConnection> connections = GetServer().GetConnections();
            for (int i = 0; i < connections.Count; ++i)
            {
                Player player = new Player(playerIndex + i);
                player.SetPlayerInput(new PlayerNetworkInput());
                player.ResetNetworkState();
                game.AddPlayer(player);

                AddPlayerConnection(connections[i], player);
            }

            LoadField(settings.scheme);

            GetConsole().TryExecuteCommand("exec game.cfg");

            SetPacketRate(CVars.sv_packetRate.intValue);

            RegisterNotification(Notifications.ConsoleVariableChanged, ServerRateVarChangedCallback);
            RegisterNotification(NetworkNotifications.ClientConnected, ClientConnectedNotification);
            RegisterNotification(NetworkNotifications.ClientDisconnected, ClientDisconnectedNotification);
        }
        private void ReplayPlayerActions(Player player)
        {
            //float delta = Application.frameTime;
            //player.input.Reset();

            //for (int id = player.lastAckPacketId; id <= m_lastSentPacketId; ++id)
            //{
            //    ClientPacket packet = GetPacket(id);
            //    int actions = packet.actions;
            //    int actionsCount = (int)PlayerAction.Count;
            //    for (int i = 0; i < actionsCount; ++i)
            //    {
            //        player.input.SetActionPressed(i, (actions & (1 << i)) != 0);
            //    }

            //    player.UpdateMoving(delta);
            //}

            throw new NotImplementedException();
        }
        private void ReadFieldState(Peer peer, NetIncomingMessage message)
        {
            game = new Game(MultiplayerMode.Client);

            SetupField(settings.scheme);
            ReadFieldState(message);

            m_localPlayer = null;

            List<Player> players = game.GetPlayers().list;
            for (int i = 0; i < players.Count; ++i)
            {
                if (players[i].input == null)
                {
                    m_localPlayer = players[i];
                    m_localPlayer.SetPlayerInput(InputMapping.CreatePlayerInput(InputType.Keyboard1));
                    m_localPlayer.input.IsActive = true; // TODO: handle console
                    break;
                }
            }

            Debug.Assert(m_localPlayer != null);
            m_localPlayer.connection = peer.RemoteConnection;
            m_localPlayer.IsReady = true;
            m_localPlayer.needsFieldState = false;
        }
Ejemplo n.º 7
0
 public Bomb(Player player)
     : base(FieldCellType.Bomb, player.GetCx(), player.GetCy())
 {
     m_player = player;
     m_state = State.Undefined;
 }
Ejemplo n.º 8
0
 public void AddPlayer(Player player)
 {
     m_field.AddPlayer(player);
 }
 private void Remove(Player player, bool removeFromList)
 {
     Debug.Assert(m_list.Contains(player));
     if (removeFromList)
     {
         m_list.Remove(player);
     }
 }
 public void Kill(Player player)
 {
     player.Kill();
     Remove(player, false);
 }
 private void WriteRoundStartMessage(NetBuffer buffer, Player player)
 {
     if (player == null)
     {
         buffer.Write(false); // player is not ready
         buffer.Write(true);  // player needs field state
     }
     else
     {
         buffer.Write(player.IsReady);
         buffer.Write(player.needsFieldState);
     }
 }
        private void AddPlayerConnection(NetConnection connection, Player player)
        {
            Debug.Assert(!networkPlayersLookup.ContainsKey(connection));
            networkPlayersLookup.Add(connection, player);

            Debug.Assert(player.connection == null);
            player.connection = connection;

            Debug.Assert(!networkPlayers.Contains(player));
            networkPlayers.Add(player);
        }
        public void SetFrom(Player p)
        {
            direction = p.direction;
            px = p.px;
            py = p.py;
            speed = p.GetSpeed();
            // powerups = p.powerups; // TODO
            // diseases = p.diseases; // TODO

            bombs = EnsureArray(bombs, p.bombs.array.Length);
            for (int i = 0; i < bombs.Length; ++i)
            {
                bombs[i].SetFrom(p.bombs.array[i]);
            }
        }
        private void WritePlayerState(NetBuffer buffer, Player p)
        {
            bool alive = p.IsAlive;
            buffer.Write(alive);
            if (alive)
            {
                buffer.Write(p.px);
                buffer.Write(p.py);
                buffer.Write(p.IsMoving());
                buffer.Write((byte)p.direction);
                buffer.Write(p.GetSpeed());

                // powerups
                int powerupsCount = (int)Powerups.Count;
                for (int i = 0; i < powerupsCount; ++i)
                {
                    bool hasPower = p.powerups.HasPowerup(i);
                    buffer.Write(hasPower);
                    if (hasPower)
                    {
                        buffer.Write((byte)p.powerups.GetCount(i));
                    }
                }

                // diseases
                int diseasesCount = DiseaseList.diseaseArray.Length;
                for (int i = 0; i < diseasesCount; ++i)
                {
                    bool infected = p.diseases.IsInfected(i);
                    buffer.Write(infected);
                    if (infected)
                    {
                        buffer.WriteTime(NetTime.Now + p.diseases.GetInfectedRemains(i), false);
                    }
                }
            }

            Bomb[] bombs = p.bombs.array;
            for (int i = 0; i < bombs.Length; ++i)
            {
                WriteBombState(buffer, bombs[i]);
            }
        }
        private void ReadPlayerState(NetIncomingMessage msg, Player p)
        {
            bool alive = msg.ReadBoolean();
            if (alive)
            {
                float px = msg.ReadFloat();
                float py = msg.ReadFloat();
                bool moving = msg.ReadBoolean();
                Direction direction = (Direction)msg.ReadByte();
                float speed = msg.ReadFloat();

                // player state
                p.UpdateFromNetwork(px, py, moving, direction, speed);

                // powerups
                int powerupsCount = (int)Powerups.Count;
                for (int i = 0; i < powerupsCount; ++i)
                {
                    bool hasPower = msg.ReadBoolean();
                    if (hasPower)
                    {
                        int count = msg.ReadByte();
                        p.powerups.SetCount(i, count);
                    }
                    else
                    {
                        p.powerups.SetCount(i, 0);
                    }
                }

                // diseases
                int diseasesCount = DiseaseList.diseaseArray.Length;
                for (int i = 0; i < diseasesCount; ++i)
                {
                    bool infected = msg.ReadBoolean();
                    if (infected)
                    {
                        float remains = (float)(msg.ReadTime(false) - NetTime.Now);
                        p.diseases.TryInfect(i);
                        p.diseases.SetInfectedRemains(i, remains);
                    }
                    else
                    {
                        p.diseases.TryCure(i);
                    }
                }
            }
            else if (p.IsAlive)
            {
                Field.KillPlayer(p);
            }

            Bomb[] bombs = p.bombs.array;
            for (int i = 0; i < bombs.Length; ++i)
            {
                ReadBombState(msg, p, bombs[i]);
            }
        }
        private void ReadBombState(NetIncomingMessage msg, Player p, Bomb b)
        {
            bool active = msg.ReadBoolean();
            if (active)
            {
                float remains = (float)(msg.ReadTime(false) - NetTime.Now);
                float px = msg.ReadFloat();
                float py = msg.ReadFloat();
                Direction dir = (Direction)msg.ReadByte();
                float speed = msg.ReadFloat();
                bool jelly = msg.ReadBoolean();
                bool trigger = msg.ReadBoolean();

                if (!b.isActive)
                {
                    b.player = p;
                    b.Activate();
                    game.Field.SetBomb(b);
                }

                b.timeRemains = remains;
                b.SetPos(px, py);
                b.SetSpeed(speed);
                b.SetJelly(jelly);
                b.SetTrigger(trigger);
                // TODO: jelly & trigger
            }
            else if (b.isActive)
            {
                b.Deactivate();
                b.RemoveFromField();
            }
        }
        protected void WriteFieldState(NetBuffer buffer, Player player)
        {
            // bricks & powerups
            FieldCellSlot[] slots = game.Field.GetCells().slots;
            for (int i = 0; i < slots.Length; ++i)
            {
                BrickCell brick = slots[i].GetBrick();
                if (brick != null)
                {
                    bool hasPowerup = brick.HasPowerup();
                    buffer.Write(hasPowerup);
                    if (hasPowerup)
                    {
                        buffer.Write((byte)brick.powerup);
                    }
                }
            }

            // players
            List<Player> players = game.GetPlayers().list;

            int senderIndex = players.IndexOf(player);
            Debug.Assert(senderIndex != -1);

            buffer.Write((byte)senderIndex);
            buffer.Write((byte)players.Count);
            for (int i = 0; i < players.Count; ++i)
            {
                Player p = players[i];
                buffer.Write((byte)p.cx);
                buffer.Write((byte)p.cy);
            }
        }
        protected void ReadFieldState(NetBuffer buffer)
        {
            // bricks & powerups
            FieldCellSlot[] slots = game.Field.GetCells().slots;
            for (int i = 0; i < slots.Length; ++i)
            {
                BrickCell brick = slots[i].GetBrick();
                if (brick != null)
                {
                    bool hasPowerup = buffer.ReadBoolean();
                    if (hasPowerup)
                    {
                        brick.powerup = buffer.ReadByte();
                    }
                }
            }

            // players
            int senderIndex = buffer.ReadByte();
            int playersCount = buffer.ReadByte();
            for (int i = 0; i < playersCount; ++i)
            {
                Player player = new Player(i);
                int cx = buffer.ReadByte();
                int cy = buffer.ReadByte();
                player.SetCell(cx, cy);
                if (i != senderIndex)
                {
                    player.SetPlayerInput(new PlayerNetworkInput());
                }

                game.AddPlayer(player);
            }
        }
        private void WritePlayingMessage(NetOutgoingMessage msg, Player player)
        {
            Debug.Assert(player != null);

            int actions = 0;
            int actionsCount = (int)PlayerAction.Count;
            for (int i = 0; i < actionsCount; ++i)
            {
                if (player.input.IsActionPressed(i))
                {
                    actions |= 1 << i;
                }
            }

            ClientPacket packet;
            packet.id = player.lastSentPacketId;
            packet.lastAckServerPacketId = player.lastAckPacketId;
            packet.actions = actions;

            WriteClientPacket(msg, ref packet);

            PushPacket(ref packet);
        }
 private void WriteRoundEndMessage(NetBuffer buffer, Player player)
 {
     buffer.Write(player.IsReady);
     buffer.Write(player.needsRoundResults);
 }
Ejemplo n.º 21
0
 public bool IsInfectedSame(Player other)
 {
     return m_diseases.IsInfectedSame(other.m_diseases);
 }
            public LocalPlayerView(Player player)
            {
                m_player = player;

                m_packetDiffView = AddTextView();
                m_cordErrView = AddTextView("px: 0\npy: 0"); // HACK: need to adjust height
                LayoutVer(0);
                ResizeToFitViews();
            }
Ejemplo n.º 23
0
        private void Swap(Player other)
        {
            float tpx = other.px;
            float tpy = other.py;

            other.SetPos(px, py);
            SetPos(tpx, tpy);
        }
 public void Remove(Player player)
 {
     Remove(player, true);
 }
        public PlayerResultView(Player player)
        {
            m_player = player;

            Font font = Helper.fontButton;

            m_readyTextView = new TextView(font, "Not Ready");
            m_winsView = new TextView(font, "99");
            m_suicidesView = new TextView(font, "99");

            UpdateState();
            AddView(m_readyTextView);
            AddView(m_winsView);
            AddView(m_suicidesView);

            LayoutHor(10);
            ResizeToFitViews();
        }
 public void Add(Player player)
 {
     Debug.Assert(!m_list.Contains(player));
     m_list.Add(player);
 }
Ejemplo n.º 27
0
        private static bool TransmitDiseases(Player p1, Player p2)
        {
            if (p1.IsInfectedSame(p2))
            {
                return false;
            }

            bool transmitted = false;

            int totalCount = (int)Diseases.Count;
            for (int diseaseIndex = 0; diseaseIndex < totalCount; ++diseaseIndex)
            {
                if (p1.IsInfected(diseaseIndex))
                {
                    transmitted |= p2.TryInfect(diseaseIndex);
                }
                else if (p2.IsInfected(diseaseIndex))
                {
                    transmitted |= p1.TryInfect(diseaseIndex);
                }
            }

            return transmitted;
        }
 public FlameCell(Player player, int cx, int cy)
     : base(FieldCellType.Flame, cx, cy)
 {
     this.player = player;
     remains = CVars.cg_timeFlame.intValue * 0.001f;
 }
Ejemplo n.º 29
0
        private bool HandleCollision(Player other)
        {
            if (other.IsAlive)
            {
                return TransmitDiseases(this, other);
            }

            return false;
        }