예제 #1
0
 private static void PushMany <T>(SortedStack <T> stack, params T[] values)
     where T : IComparable <T>
 {
     foreach (var value in values)
     {
         stack.Push(value);
     }
 }
예제 #2
0
        public void Start()
        {
            Stats.Start();

            Channels.PropertyChanged += OnChannelsPropertyChanged;
            Channels.Server           = this;
            Channels.Start(Config.Port);

            for (ushort i = 0; i < Config.MaxClients; i++)
            {
                idpool.Push(i);
            }

            Config.PropertyChanged += Config_PropertyChanged;

            if (Config.UseTcpSockets || Config.UseWebSockets)
            {
                LocalAddresses = Utils.GetLocalAddresses();

                listener           = new AresTcpSocket(new ClientFormatter());
                listener.Accepted += ClientAccepted;
                listener.Bind(new IPEndPoint(Config.LocalIp, Config.Port));
                listener.Listen(25);

                if (Config.UseTlsSockets)
                {
                    tlslistener           = new AresTcpSocket(new ClientFormatter());
                    tlslistener.Accepted += TLSClientAccepted;
                    tlslistener.Bind(new IPEndPoint(Config.LocalIp, Config.Port + 1));
                    tlslistener.Listen(25);
                }
            }

            Running = true;

            Stats.StartTime = DateTime.Now;
            timer           = new Timer(new TimerCallback(OnTimer), null, ticklength, ticklength);

            Logging.Info("AresServer", "Chatroom server started.");
        }
예제 #3
0
        public void Start()
        {
            stats.Start();
            channels.Start();

            for (ushort i = 0; i < config.MaxClients; i++)
            {
                idpool.Push(i);
            }

            config.PropertyChanged += Config_PropertyChanged;

            listener           = new AresTcpSocket(new ClientFormatter());
            listener.Accepted += ClientAccepted;

            Logging.WriteLines(new[] {
                "Server started",
                "----------",
                String.Format("Listening on port: {0}", config.Port),
                "----------"
            });

            IPHostEntry entry = Dns.GetHostEntry(Environment.MachineName);

            foreach (var ip in entry.AddressList)
            {
                if (ip.AddressFamily == listener.Socket.AddressFamily)
                {
                    LocalIp = ip;
                    break;
                }
            }

            listener.Bind(new IPEndPoint(IPAddress.Any, config.Port));
            listener.Listen(25);

            RaisePropertyChanged(() => Running);

            timer = new Timer(new TimerCallback(OnTimer), null, ticklength, ticklength);
        }
예제 #4
0
        //var s = Surfaces.Plane(50, 50).Mult(1.0 / 50).Move(-0.5, -0.5, 0).ToShape2().CutOutside(Polygons.Sinus(1, 3, 5, 500)).ToShape3();
        //var points = s.Points2;

        //var q2 = Math.Sqrt(2);

        //double Distance(int i, int j)
        //{
        //    var a = points[i];
        //    var b = points[j];

        //    var dx = Math.Abs(a.x - b.x);
        //    var dy = Math.Abs(a.y - b.y);

        //    var min = Math.Min(dx, dy);
        //    var max = Math.Max(dx, dy);

        //    return (max - min) + min * 2;

        //    //if (Math.Abs(a.x -b.x) < 0.00001 || Math.Abs(a.y - b.y) < 0.00001)
        //    return (b - a).Len;

        //    //return 0.99 * (b - a).Len;
        //}

        //var g = s.ToGraph();
        //var from = g.nodes[^1219];
        //var to = g.nodes[^835];

        //var(path, open, close, infos) = g.FindPathAStar((a, b) => Distance(a.i, b.i), from, to);

        //    var pathShape = new Shape()
        //    {
        //        Points = s.Points,
        //        Convexes = path.SelectPair((a, b) => new[] { a.i, b.i }).ToArray()
        //    };

        //var openShape = new Shape()
        //{
        //    Points = open.Select(n => s.Points[n.i] + new Vector4(0, 0, infos[n].PathDistance * 0.3, 0)).ToArray(),
        //};

        //var closeList = close.ToList();
        //var closeShape = new Shape()
        //{
        //    Points = close.Select(n => s.Points[n.i] + new Vector4(0, 0, infos[n].PathDistance * 0.3, 0)).ToArray(),
        //    Convexes = path.Select(n => closeList.IndexOf(n)).SelectPair((i, j) => new[] { i, j }).ToArray()
        //};

        //pathShape = pathShape.ToMetaShape3(0.2, 1, Color.Black, Color.Green);//.ApplyColor(Color.Red);//.ToLines3(1, Color.Blue);

        //    var shape = pathShape +
        //                openShape.ToSpots3(0.22, Color.Blue) +
        //                closeShape.ToMetaShape3(0.22, 1, Color.Red, Color.Green)


        // how to: https://www.youtube.com/watch?v=-L-WgKMFuhE
        // todo: можно оптимизировать заменив double на long, и для равноудаленных узлов брать ближайший к цели (как на видео)
        public (Node[] path, Node[] open, Node[] close, Dictionary <Node, Info> infos) FindPathAStar(Func <Node, Node, double> distanceFn, Node from = null, Node to = null)
        {
            from ??= nodes[0];
            to ??= nodes[^ 1];

            var infos    = new Dictionary <Node, Info>();
            var openSet  = new SortedStack <Node>(nodes.Count);
            var closeSet = new HashSet <Node>(nodes.Count);

            void UpdateOpenSetItem(Node prev, Node n)
            {
                var prevPathDistanceFrom = infos.TryGetValue(prev, out Info prevInfo) ? prevInfo.PathDistanceFrom : 0;
                var pathDistanceFrom     = prevPathDistanceFrom + distanceFn(prev, n);

                if (!infos.TryGetValue(n, out Info info))
                {
                    info = new Info()
                    {
                        DistanceTo       = distanceFn(n, to),
                        PathDistanceFrom = pathDistanceFrom,
                        Node             = n,
                        Prev             = prev
                    };
                    infos.Add(n, info);
                    openSet.Push(n, info.PathDistance);
                }
                else
                {
                    if (pathDistanceFrom < info.PathDistanceFrom)
                    {
                        info.PathDistanceFrom = pathDistanceFrom;
                        info.Prev             = prev;
                        openSet.Update(n, info.PathDistance);
                    }
                }
            }

            UpdateOpenSetItem(from, from);

            do
            {
                var n = openSet.Pop();
                closeSet.Add(n);

                if (n == to)
                {
                    break;
                }

                foreach (var nn in n.edges.Select(e => e.Another(n)).Where(node => !closeSet.Contains(node)))
                {
                    UpdateOpenSetItem(n, nn);
                }
            } while (!openSet.IsEmpty);

            Debug.WriteLine($"{openSet.Count} + {closeSet.Count} = {openSet.Count + closeSet.Count}");

            List <Node> path = new();

            var node = to;

            while (node != from)
            {
                var info = infos[node];
                Debug.WriteLine($"{node.i}: {info.DistanceTo:F3} + {info.PathDistanceFrom:F3} = {info.PathDistance:F3} ");

                path.Add(node);
                //yield return node;

                node = info.Prev;
            }

            var infoFrom = infos[from];

            Debug.WriteLine($"{node.i}: {infoFrom.DistanceTo:F3} + {infoFrom.PathDistanceFrom:F3} = {infoFrom.PathDistance:F3} ");

            path.Add(from);
            //yield return from;
            path.Reverse();

            return(path.ToArray(), openSet.ToArray(), closeSet.ToArray(), infos);
        }