public static void Run()
        {
            Log("Running at " + tickRate + "hz");

            Stopwatch watch = new Stopwatch();

            int delta = 0;

            while (true)
            {
                watch.Restart();

                server.PollEvents();
                Game.Update(delta);
                SendSnapshotToAll(Packets.CreateSnapshot());

                watch.Stop();

                delta = tickTime - (int)watch.ElapsedMilliseconds;

                if (delta <= 0)
                {
                    continue;
                }
                Thread.Sleep(delta);
            }
        }
Exemple #2
0
        private static void AssignTargets()
        {
            targetNeverAssigned = false;
            targetAssignedSince = 0;
            Log("Reassigning targets at time: " + time);
            Random r = new Random();

            List <Player> players = Server.players.Values.ToList();
            List <Player> left    = new List <Player>(players);

            foreach (Player player in players)
            {
                Player newTarget = left.ElementAt(r.Next(left.Count));
                while (newTarget == player)
                {
                    newTarget = left.ElementAt(r.Next(left.Count));
                }

                left.Remove(newTarget);
                player.SetTarget(newTarget);

                player.GetPeer().Send(Packets.CreateTargetChange(newTarget.GetName()), DeliveryMethod.ReliableOrdered);
            }
        }
        private static void SetupListeners()
        {
            listener.NetworkErrorEvent += (endpoint, error) =>
            {
                Console.WriteLine("An error occured on endpoint {0} with error {1}", endpoint.ToString(), error.ToString());
            };

            listener.NetworkLatencyUpdateEvent += (peer, latency) =>
            {
                //Console.WriteLine("Client {0} latency was updated to {1}", peer.Id, latency);
            };

            listener.ConnectionRequestEvent += rq =>
            {
                NetPeer peer;
                if (server.PeersCount <= 14 /* max connections */)
                {
                    peer = rq.Accept();
                }
                else
                {
                    rq.Reject();
                    return;
                }

                IdentificationPacket p = Packets.GetIdentification(rq.Data.GetRemainingBytes());
                players.Add(peer.Id, new Player(p, peer));
                Log("Player " + p.name + " is logged in with id " + p.id);
            };

            listener.PeerConnectedEvent += (peer) =>
            {
                peer.Send(Packets.CreateInit(), DeliveryMethod.ReliableOrdered);
            };

            listener.PeerDisconnectedEvent += (peer, reason) =>
            {
                Log("Peer " + peer.EndPoint + " disconnected");
                if (players.ContainsKey(peer.Id))
                {
                    players.Remove(peer.Id);
                }
            };

            listener.NetworkReceiveEvent += (peer, data, method) =>
            {
                Player p;
                players.TryGetValue(peer.Id, out p);
                if (p == null)
                {
                    server.DisconnectPeer(peer, Encoding.Unicode.GetBytes("Protocol error. Player must sent identification packet."));
                    return;
                }

                switch (data.GetByte())
                {
                case 1:                             //Chat message packet => (int) messageLength; (string) message; (int -> cast to enum) recipients
                    string msg = Packets.GetChatMsg(data.GetRemainingBytes());
                    SendToAll(Packets.CreateChat(p.GetName(), msg), DeliveryMethod.ReliableOrdered);
                    break;

                case 2:                             //Position update packet => (transform/compressed) transform; (int) movedAtTick

                    p.Move(data.GetShort(), data.GetShort(), data.GetShort());
                    p.Rotate(data.GetShort(), data.GetShort(), data.GetShort());

                    break;

                case 3:                                 //Shoot command => (Vector3/compressed) direction; (int) shootAtTick
                    break;

                case 4:                                 //TODO: define use
                    break;

                case 5:                                 //TODO: define use
                    break;
                }
            };
        }