public SearchNode(SectorNode sn, SearchNode parent, int g, Point3D goal)
 {
     SectorNode = sn;
     Parent     = parent;
     G          = g;
     Score      = g + Heuristic(goal);
 }
Example #2
0
        static async Task Main(string[] args)
        {
            var diceSeed   = GetDiceSeed(args);
            var outputPath = GetOutputPath(args);

            var startUp          = new Startup(diceSeed);
            var serviceContainer = new ServiceCollection();

            startUp.RegisterServices(serviceContainer);

            var serviceProvider = serviceContainer.BuildServiceProvider();

            var schemeService = serviceProvider.GetRequiredService <ISchemeService>();

            var sectorSchemeResult = GetSectorScheme(args, schemeService);

            var biome      = new Biome(sectorSchemeResult.Location);
            var sectorNode = new SectorNode(biome, sectorSchemeResult.Sector);

            var sectorFactory = serviceProvider.GetRequiredService <ISectorGenerator>();
            var sector        = await sectorFactory.GenerateAsync(sectorNode).ConfigureAwait(false);

            sector.Scheme = sectorSchemeResult.Location;

            // Проверка

            var sectorValidators = GetValidatorsInAssembly();
            var checkTask        = CheckSectorAsync(sectorValidators, serviceProvider, sector);

            var saveTask = SaveMapAsImageAsync(outputPath, sector);

            await Task.WhenAll(checkTask, saveTask).ConfigureAwait(false);
        }
        private static void EventSink_WorldLoad()
        {
            try
            {
                Console.Write("Loading SectorNodes...");
                DateTime dt = DateTime.Now;
                using (FileStream fs = new FileStream("Data/SectorPathData.dat", FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        BinaryFileReader reader = new BinaryFileReader(br);

                        if (reader.ReadInt() != (Map.Felucca.Width >> Map.SectorShift))
                        {
                            throw new Exception("SectorNode data has different width than current map.");
                        }
                        if (reader.ReadInt() != (Map.Felucca.Height >> Map.SectorShift))
                        {
                            throw new Exception("SectorNode data has different height than current map.");
                        }

                        m_Nodes = new SectorNode[Map.Felucca.Width >> Map.SectorShift, Map.Felucca.Height >> Map.SectorShift];

                        for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
                        {
                            for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
                            {
                                m_Nodes[x, y] = new SectorNode();
                            }
                        }

                        for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
                        {
                            for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
                            {
                                m_Nodes[x, y].Deserialize(reader);
                            }
                        }

                        reader.Close();
                    }
                }
                Console.WriteLine("done in {0}ms.", (DateTime.Now - dt).TotalMilliseconds);
            }
            catch (Exception e)
            {
                LogHelper.LogException(e);
                Console.WriteLine("error:");
                Console.WriteLine(e.Message);
                Console.WriteLine("SectorNode data must be recomputed.");
                m_Nodes = null;
            }
        }
Example #4
0
        public static async Task Main(string[] args)
        {
            var diceSeed   = GetDiceSeed(args);
            var outputPath = GetOutputPath(args);

            var startUp          = new Startup(diceSeed);
            var serviceContainer = new ServiceCollection();

            startUp.RegisterServices(serviceContainer);

            var serviceProvider = serviceContainer.BuildServiceProvider();

            var schemeService = serviceProvider.GetRequiredService <ISchemeService>();

            var sectorSchemeResult = ValidationHelper.GetSectorScheme(_random, args, schemeService);

            var biome      = new Biome(sectorSchemeResult.LocationScheme);
            var sectorNode = new SectorNode(biome, sectorSchemeResult.SectorScheme);

            biome.AddNode(sectorNode);

            var nextCount = 3;

            for (var i = 0; i < nextCount; i++)
            {
                var nextSectorNode = new SectorNode(biome, sectorSchemeResult.SectorScheme);
                biome.AddNode(nextSectorNode);
                biome.AddEdge(sectorNode, nextSectorNode);
            }

            var sectorFactory = serviceProvider.GetRequiredService <ISectorGenerator>();
            var sector        = await sectorFactory.GenerateAsync(sectorNode).ConfigureAwait(false);

            sector.Scheme = sectorSchemeResult.LocationScheme;

            // Проверка

            var sectorValidators = ValidatorCollector.GetValidatorsInAssembly();
            var checkTask        = ValidationHelper.CheckSectorAsync(sectorValidators, serviceProvider, sector);

            var saveTask = ImageHelper.SaveMapAsImageAsync(outputPath, sector);

            await Task.WhenAll(checkTask, saveTask).ConfigureAwait(false);
        }
			public SearchNode(SectorNode sn, SearchNode parent, int g, Point3D goal)
			{
				SectorNode = sn;
				Parent = parent;
				G = g;
				Score = g + Heuristic(goal);
			}
		private static void EventSink_WorldLoad()
		{
			try
			{
				Console.Write("Loading SectorNodes...");
				DateTime dt = DateTime.Now;
				using (FileStream fs = new FileStream("Data/SectorPathData.dat", FileMode.Open))
				{
					using (BinaryReader br = new BinaryReader(fs))
					{
						BinaryFileReader reader = new BinaryFileReader(br);

						if (reader.ReadInt() != (Map.Felucca.Width >> Map.SectorShift))
							throw new Exception("SectorNode data has different width than current map.");
						if (reader.ReadInt() != (Map.Felucca.Height >> Map.SectorShift))
							throw new Exception("SectorNode data has different height than current map.");

						m_Nodes = new SectorNode[Map.Felucca.Width >> Map.SectorShift, Map.Felucca.Height >> Map.SectorShift];

						for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
						{
							for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
							{
								m_Nodes[x, y] = new SectorNode();
							}
						}

						for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
						{
							for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
							{
								m_Nodes[x, y].Deserialize(reader);
							}
						}

						reader.Close();
					}
				}
				Console.WriteLine("done in {0}ms.", (DateTime.Now - dt).TotalMilliseconds);
			}
			catch (Exception e)
			{
				LogHelper.LogException(e);
				Console.WriteLine("error:");
				Console.WriteLine(e.Message);
				Console.WriteLine("SectorNode data must be recomputed.");
				m_Nodes = null;
			}
		}
		public static void BuildSectorNodeNetwork(CommandEventArgs e)
		{
			if (m_Nodes == null)
			{
				try
				{
					Console.Write("Initializing SectorNodes...");
					DateTime dt = DateTime.Now;

					m_Nodes = new SectorNode[Map.Felucca.Width >> Map.SectorShift, Map.Felucca.Height >> Map.SectorShift];

					for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
					{
						for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
						{
							SectorNode sn = new SectorNode();
							sn.Point = Point3D.Zero;
							sn.Island = -1;
							sn.Links = new SectorNode[8];
							sn.Distances = new int[8];
							sn.NumLinks = 0;

							for (int sy = 0; sy < Map.SectorSize && sn.Point == Point3D.Zero; sy++)
							{
								for (int sx = 0; sx < Map.SectorSize && sn.Point == Point3D.Zero; sx++)
								{
									if (Map.Felucca.CanSpawnMobile((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy, 
										Map.Felucca.GetAverageZ((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy)))
									{
										sn.Point = new Point3D((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy, 
											Map.Felucca.GetAverageZ((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy));
									}
								}
							}

							m_Nodes[x, y] = sn;
						}
					}

					Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
					Console.Write("Computing SectorNode network...");
					dt = DateTime.Now;

					Mobile m = new Server.Mobiles.WanderingHealer();
					MovementPath mp = null;
			
					for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
					{
						for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
						{
							if (m_Nodes[x, y].Point != Point3D.Zero)
							{
								m.MoveToWorld(m_Nodes[x, y].Point, Map.Felucca);

								if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && y > 0 && m_Nodes[x + 1, y - 1].Point != Point3D.Zero &&
									(mp = new MovementPath(m, m_Nodes[x + 1, y - 1].Point)).Success)
								{
									m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks] = m_Nodes[x + 1, y - 1];
									m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
									m_Nodes[x, y].NumLinks++;

									m_Nodes[x + 1, y - 1].Links[m_Nodes[x + 1, y - 1].NumLinks] = m_Nodes[x, y];
									m_Nodes[x + 1, y - 1].Distances[m_Nodes[x + 1, y - 1].NumLinks] = mp.Directions.Length;
									m_Nodes[x + 1, y - 1].NumLinks++;
								}
						
								if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && m_Nodes[x + 1, y].Point != Point3D.Zero &&
									(mp = new MovementPath(m, m_Nodes[x + 1, y].Point)).Success)
								{
									m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks] = m_Nodes[x + 1, y];
									m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
									m_Nodes[x, y].NumLinks++;

									m_Nodes[x + 1, y].Links[m_Nodes[x + 1, y].NumLinks] = m_Nodes[x, y];
									m_Nodes[x + 1, y].Distances[m_Nodes[x + 1, y].NumLinks] = mp.Directions.Length;
									m_Nodes[x + 1, y].NumLinks++;
								}

								if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && y < (Map.Felucca.Height >> Map.SectorShift) - 1 &&
									m_Nodes[x + 1, y + 1].Point != Point3D.Zero && (mp = new MovementPath(m, m_Nodes[x + 1, y + 1].Point)).Success)
								{
									m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks] = m_Nodes[x + 1, y + 1];
									m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
									m_Nodes[x, y].NumLinks++;

									m_Nodes[x + 1, y + 1].Links[m_Nodes[x + 1, y + 1].NumLinks] = m_Nodes[x, y];
									m_Nodes[x + 1, y + 1].Distances[m_Nodes[x + 1, y + 1].NumLinks] = mp.Directions.Length;
									m_Nodes[x + 1, y + 1].NumLinks++;
								}

								if (y < (Map.Felucca.Height >> Map.SectorShift) - 1 && m_Nodes[x, y + 1].Point != Point3D.Zero &&
									(mp = new MovementPath(m, m_Nodes[x, y + 1].Point)).Success)
								{
									m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks] = m_Nodes[x, y + 1];
									m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
									m_Nodes[x, y].NumLinks++;

									m_Nodes[x, y + 1].Links[m_Nodes[x, y + 1].NumLinks] = m_Nodes[x, y];
									m_Nodes[x, y + 1].Distances[m_Nodes[x, y + 1].NumLinks] = mp.Directions.Length;
									m_Nodes[x, y + 1].NumLinks++;
								}
							}
						}
					}

					m.Delete();

					Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
					Console.Write("Finding islands...");
					dt = DateTime.Now;

					int nextIsland = 0;
					Queue open = new Queue();
					ArrayList closed = new ArrayList();

					for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
					{
						for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
						{
							if (m_Nodes[x, y].Point == Point3D.Zero)
								continue;

							if (m_Nodes[x, y].Island == -1)
							{
								int island = nextIsland++;

								// now use dijkstra-style flood fill to find all connected nodes
								open.Clear();
								closed.Clear();

								open.Enqueue(m_Nodes[x, y]);

								while (open.Count > 0)
								{
									SectorNode sn = (SectorNode)open.Dequeue();
									closed.Add(sn);

									sn.Island = island;

									for (int i = 0; i < sn.NumLinks; i++)
										if (!closed.Contains(sn.Links[i]) && !open.Contains(sn.Links[i]))
											open.Enqueue(sn.Links[i]);
								}
							}
						}
					}

					Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
				}
				catch (Exception ex)
				{
					LogHelper.LogException(ex);
					Console.WriteLine("error!");
					Console.WriteLine(ex);
				}
			}
		}
        public static Point3D[] FindWaypoints(Mobile m, Map map, Point3D start, Point3D goal)
        {
            DateTime s = DateTime.Now;

            if (m_Nodes == null)
            {
                return(null);                // sanity check
            }
            if (!SameIsland(start, goal))
            {
                return(null);
            }

            if (!Utility.InRange(start, goal, 512))
            {
                return(null);
            }

            SearchNodeHeap open   = new SearchNodeHeap();
            ArrayList      closed = new ArrayList();

            SectorNode goalnode = m_Nodes[goal.X >> Map.SectorShift, goal.Y >> Map.SectorShift];

            open.Add(new SearchNode(m_Nodes[start.X >> Map.SectorShift, start.Y >> Map.SectorShift], null, 0, goal));

            while (open.Count > 0)
            {
                SearchNode curnode = open.Pop();
                closed.Add(curnode);

                if (curnode.SectorNode == goalnode)
                {
                    break;
                }

                for (int i = 0; i < curnode.SectorNode.NumLinks; i++)
                {
                    SearchNode newnode = new SearchNode(curnode.SectorNode.Links[i], curnode, curnode.G + curnode.SectorNode.Distances[i], goal);

                    if (closed.Contains(newnode))
                    {
                        continue;
                    }

                    SearchNode existing = open.FindMatch(newnode);
                    if (existing == null)
                    {
                        open.Add(newnode);
                    }
                    else if (newnode.G < existing.G)
                    {
                        open.Adjust(newnode);
                    }
                    // else skip
                }
            }

            SearchNode sn = (SearchNode)closed[closed.Count - 1];

            if (sn.SectorNode == goalnode)
            {
                Stack nodes = new Stack();
                while (sn != null)
                {
                    closed.Remove(sn);
                    nodes.Push(sn);
                    sn = sn.Parent;
                }

                Point3D[] points = new Point3D[nodes.Count + 1];
                for (int i = 0; i < points.Length - 1; i++)
                {
                    points[i] = ((SearchNode)nodes.Pop()).SectorNode.Point;
                }
                points[points.Length - 1] = goal;

                return(points);
            }

            return(null);
        }
        public static void BuildSectorNodeNetwork(CommandEventArgs e)
        {
            if (m_Nodes == null)
            {
                try
                {
                    Console.Write("Initializing SectorNodes...");
                    DateTime dt = DateTime.Now;

                    m_Nodes = new SectorNode[Map.Felucca.Width >> Map.SectorShift, Map.Felucca.Height >> Map.SectorShift];

                    for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
                    {
                        for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
                        {
                            SectorNode sn = new SectorNode();
                            sn.Point     = Point3D.Zero;
                            sn.Island    = -1;
                            sn.Links     = new SectorNode[8];
                            sn.Distances = new int[8];
                            sn.NumLinks  = 0;

                            for (int sy = 0; sy < Map.SectorSize && sn.Point == Point3D.Zero; sy++)
                            {
                                for (int sx = 0; sx < Map.SectorSize && sn.Point == Point3D.Zero; sx++)
                                {
                                    if (Map.Felucca.CanSpawnMobile((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy,
                                                                   Map.Felucca.GetAverageZ((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy)))
                                    {
                                        sn.Point = new Point3D((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy,
                                                               Map.Felucca.GetAverageZ((x << Map.SectorShift) + sx, (y << Map.SectorShift) + sy));
                                    }
                                }
                            }

                            m_Nodes[x, y] = sn;
                        }
                    }

                    Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
                    Console.Write("Computing SectorNode network...");
                    dt = DateTime.Now;

                    Mobile       m  = new Server.Mobiles.WanderingHealer();
                    MovementPath mp = null;

                    for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
                    {
                        for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
                        {
                            if (m_Nodes[x, y].Point != Point3D.Zero)
                            {
                                m.MoveToWorld(m_Nodes[x, y].Point, Map.Felucca);

                                if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && y > 0 && m_Nodes[x + 1, y - 1].Point != Point3D.Zero &&
                                    (mp = new MovementPath(m, m_Nodes[x + 1, y - 1].Point)).Success)
                                {
                                    m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks]     = m_Nodes[x + 1, y - 1];
                                    m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x, y].NumLinks++;

                                    m_Nodes[x + 1, y - 1].Links[m_Nodes[x + 1, y - 1].NumLinks]     = m_Nodes[x, y];
                                    m_Nodes[x + 1, y - 1].Distances[m_Nodes[x + 1, y - 1].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x + 1, y - 1].NumLinks++;
                                }

                                if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && m_Nodes[x + 1, y].Point != Point3D.Zero &&
                                    (mp = new MovementPath(m, m_Nodes[x + 1, y].Point)).Success)
                                {
                                    m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks]     = m_Nodes[x + 1, y];
                                    m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x, y].NumLinks++;

                                    m_Nodes[x + 1, y].Links[m_Nodes[x + 1, y].NumLinks]     = m_Nodes[x, y];
                                    m_Nodes[x + 1, y].Distances[m_Nodes[x + 1, y].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x + 1, y].NumLinks++;
                                }

                                if (x < (Map.Felucca.Width >> Map.SectorShift) - 1 && y < (Map.Felucca.Height >> Map.SectorShift) - 1 &&
                                    m_Nodes[x + 1, y + 1].Point != Point3D.Zero && (mp = new MovementPath(m, m_Nodes[x + 1, y + 1].Point)).Success)
                                {
                                    m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks]     = m_Nodes[x + 1, y + 1];
                                    m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x, y].NumLinks++;

                                    m_Nodes[x + 1, y + 1].Links[m_Nodes[x + 1, y + 1].NumLinks]     = m_Nodes[x, y];
                                    m_Nodes[x + 1, y + 1].Distances[m_Nodes[x + 1, y + 1].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x + 1, y + 1].NumLinks++;
                                }

                                if (y < (Map.Felucca.Height >> Map.SectorShift) - 1 && m_Nodes[x, y + 1].Point != Point3D.Zero &&
                                    (mp = new MovementPath(m, m_Nodes[x, y + 1].Point)).Success)
                                {
                                    m_Nodes[x, y].Links[m_Nodes[x, y].NumLinks]     = m_Nodes[x, y + 1];
                                    m_Nodes[x, y].Distances[m_Nodes[x, y].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x, y].NumLinks++;

                                    m_Nodes[x, y + 1].Links[m_Nodes[x, y + 1].NumLinks]     = m_Nodes[x, y];
                                    m_Nodes[x, y + 1].Distances[m_Nodes[x, y + 1].NumLinks] = mp.Directions.Length;
                                    m_Nodes[x, y + 1].NumLinks++;
                                }
                            }
                        }
                    }

                    m.Delete();

                    Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
                    Console.Write("Finding islands...");
                    dt = DateTime.Now;

                    int       nextIsland = 0;
                    Queue     open       = new Queue();
                    ArrayList closed     = new ArrayList();

                    for (int y = 0; y < (Map.Felucca.Height >> Map.SectorShift); y++)
                    {
                        for (int x = 0; x < (Map.Felucca.Width >> Map.SectorShift); x++)
                        {
                            if (m_Nodes[x, y].Point == Point3D.Zero)
                            {
                                continue;
                            }

                            if (m_Nodes[x, y].Island == -1)
                            {
                                int island = nextIsland++;

                                // now use dijkstra-style flood fill to find all connected nodes
                                open.Clear();
                                closed.Clear();

                                open.Enqueue(m_Nodes[x, y]);

                                while (open.Count > 0)
                                {
                                    SectorNode sn = (SectorNode)open.Dequeue();
                                    closed.Add(sn);

                                    sn.Island = island;

                                    for (int i = 0; i < sn.NumLinks; i++)
                                    {
                                        if (!closed.Contains(sn.Links[i]) && !open.Contains(sn.Links[i]))
                                        {
                                            open.Enqueue(sn.Links[i]);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    Console.WriteLine("done in {0} seconds.", (DateTime.Now - dt).TotalSeconds);
                }
                catch (Exception ex)
                {
                    LogHelper.LogException(ex);
                    Console.WriteLine("error!");
                    Console.WriteLine(ex);
                }
            }
        }