Exemplo n.º 1
0
        public static void LoadEdsmSystemsJson(string filename)
        {
            Console.WriteLine($"Loading EDSM systems from {filename}");
            using (Stream s = File.OpenRead(filename))
            {
                using (TextReader r = new StreamReader(s))
                {
                    using (JsonReader rdr = new JsonTextReader(r))
                    {
                        if (rdr.Read() && rdr.TokenType == JsonToken.StartArray)
                        {
                            int i = 0;

                            while (rdr.Read() && rdr.TokenType == JsonToken.StartObject)
                            {
                                JObject jo     = JObject.Load(rdr);
                                uint    edsmid = jo.Value <uint>("id");
                                string  name   = jo.Value <string>("name");
                                JObject co     = (JObject)jo["coords"];
                                if (co != null && edsmid != 0)
                                {
                                    Vector3 starpos = new Vector3 {
                                        X = co.Value <double>("x"), Y = co.Value <double>("y"), Z = co.Value <double>("z")
                                    };
                                    PGStarMatch sm = GetStarMatch(name, starpos, edsmid: edsmid);

                                    if (sm.RegionCoords == ByteXYZ.Invalid || sm.RegionRelCoords == UShortXYZ.Invalid)
                                    {
                                        Console.WriteLine($"Bad EDSM System: id={edsmid} name=\"{name}\" coords={starpos}");
                                    }
                                    else
                                    {
                                        if (EdsmIdToSystemId.Length <= edsmid)
                                        {
                                            Array.Resize(ref EdsmIdToSystemId, (int)edsmid + 100000);
                                        }

                                        EdsmIdToSystemId[edsmid] = sm.Id;
                                    }
                                }

                                i++;
                                if (i % 10000 == 0)
                                {
                                    Console.Write(".");
                                    if (i % 500000 == 0)
                                    {
                                        Console.WriteLine("");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Done");
        }
Exemplo n.º 2
0
        public static void LoadEddbSystemsCsv(string filename)
        {
            Console.WriteLine($"Loading EDDB systems from {filename}");

            using (Stream s = File.OpenRead(filename))
            {
                using (TextReader r = new StreamReader(s))
                {
                    using (CsvParser p = new CsvParser(r))
                    {
                        List <string> headers   = p.Read().ToList();
                        int           eddbidcol = headers.IndexOf("id");
                        int           edsmidcol = headers.IndexOf("edsm_id");
                        int           namecol   = headers.IndexOf("name");
                        int           xcol      = headers.IndexOf("x");
                        int           ycol      = headers.IndexOf("y");
                        int           zcol      = headers.IndexOf("z");
                        int           i         = 0;
                        string[]      fields;

                        while ((fields = p.Read()) != null)
                        {
                            uint edsmid;
                            uint eddbid;

                            if (UInt32.TryParse(fields[eddbidcol], out eddbid) && UInt32.TryParse(fields[edsmidcol], out edsmid))
                            {
                                if (edsmid < EdsmIdToSystemId.Length && edsmid != 0)
                                {
                                    long id = EdsmIdToSystemId[edsmid];
                                    if (id != 0 && SystemsById.ContainsKey(id))
                                    {
                                        PGStarMatch sm = SystemsById[id];
                                        sm._EddbId      = eddbid;
                                        SystemsById[id] = sm;
                                    }
                                }
                            }

                            i++;
                            if (i % 10000 == 0)
                            {
                                Console.Write(".");
                                if (i % 500000 == 0)
                                {
                                    Console.WriteLine("");
                                }
                            }
                        }
                    }
                }
            }

            Console.WriteLine("Done");
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            string basedir    = args.Length > 0 ? args[0] : ".";
            uint   structsize = SizeOf <PGStarMatch>();

            PGStarMatch.LoadNamedSystemsJson(Path.Combine(basedir, "edsystems-all-withcoords.json"));
            PGStarMatch.LoadEdsmSystemsJson(Path.Combine(basedir, "systemsWithCoordinates.json"));
            PGStarMatch.LoadEddbSystemsCsv(Path.Combine(basedir, "systems.csv"));
            Listener listener = new Listener();

            listener.Run();
        }
Exemplo n.º 4
0
        private void ProcessFSDJump(JObject header, JObject body)
        {
            string  sysname = body.Value <string>("StarSystem");
            JArray  ca      = (JArray)body["StarPos"];
            Vector3 syspos  = new Vector3 {
                X = ca[0].Value <double>(), Y = ca[1].Value <double>(), Z = ca[2].Value <double>()
            };
            PGStarMatch sm = PGStarMatch.GetStarMatch(sysname, syspos);

            if (sm.Name != sysname)
            {
                Console.WriteLine($"Unknown system {sysname} received at {syspos}");
            }
        }
        public ByteXYZ GetBlockCoords(PGStarMatch m)
        {
            int starclass = m.StarClass;

            int[] v0 = GetBaseBlockCoords(starclass);
            int[] v  = m.BlockCoords;
            int[] bc = new int[] { v[0] - v0[0], v[1] - v0[1], v[2] - v0[2] };
            if (bc[0] < 0 || bc[0] >= 128 || bc[1] < 0 || bc[1] >= 128 || bc[2] < 0 || bc[2] >= 128)
            {
                return(ByteXYZ.Invalid);
            }
            else
            {
                ByteXYZ blockcoords = new ByteXYZ {
                    X = (sbyte)bc[0], Y = (sbyte)bc[1], Z = (sbyte)bc[2]
                };
                string suffix = PGStarMatch.GetPgSuffix(blockcoords, m.StarClass, m.StarSeq);

                return(blockcoords);
            }
        }
Exemplo n.º 6
0
        public static void LoadNamedSystemsJson(string filename)
        {
            Console.WriteLine($"Loading named systems from {filename}");
            using (Stream s = File.OpenRead(filename))
            {
                using (TextReader r = new StreamReader(s))
                {
                    using (JsonReader rdr = new JsonTextReader(r))
                    {
                        if (rdr.Read() && rdr.TokenType == JsonToken.StartArray)
                        {
                            int i = 0;

                            while (rdr.Read() && rdr.TokenType == JsonToken.StartObject)
                            {
                                JObject jo     = JObject.Load(rdr);
                                long    id     = jo.Value <long>("id");
                                string  pgname = jo.Value <string>("pgname");
                                string  name   = jo.Value <string>("name");
                                JArray  ca     = (JArray)jo["coords"];

                                if (name != null && ca != null)
                                {
                                    Vector3 coords = new Vector3 {
                                        X = ca[0].Value <double>(), Y = ca[1].Value <double>(), Z = ca[2].Value <double>()
                                    };
                                    IdToName[id] = name;
                                    PGStarMatch sm = GetStarMatch(pgname, coords);
                                    if (!SystemsById.ContainsKey(sm.Id))
                                    {
                                        SystemsById[sm.Id] = sm;
                                    }

                                    if (!SystemsByName.ContainsKey(name))
                                    {
                                        SystemsByName[name] = new List <long>();
                                    }

                                    SystemsByName[name].Add(sm.Id);
                                }

                                i++;
                                if (i % 10000 == 0)
                                {
                                    Console.Write(".");
                                    if (i % 500000 == 0)
                                    {
                                        Console.WriteLine("");
                                    }
                                }
                            }
                        }
                    }
                }
            }

            NamedSystemsBySector = SystemsById.Values.GroupBy(s => s.RegionCoords).OrderByDescending(g => g.Count()).ToDictionary(g => g.Key, g => g.Select(v => v.Id).ToArray());

            foreach (KeyValuePair <ByteXYZ, long[]> regionsys_kvp in NamedSystemsBySector.ToList())
            {
                SystemNamesBySector[regionsys_kvp.Key] = new string[regionsys_kvp.Value.Length];
                for (int i = 0; i < regionsys_kvp.Value.Length; i++)
                {
                    long        id  = regionsys_kvp.Value[i];
                    PGStarMatch sys = SystemsById[id];
                    SystemNamesBySector[regionsys_kvp.Key][i] = IdToName[sys.Id];
                    sys._NameIndexInSector = (ushort)(i + 1);
                    SystemsById[id]        = sys;
                }
            }

            Console.WriteLine("Done");
        }
Exemplo n.º 7
0
        public static PGStarMatch GetStarMatch(string sysname, Vector3 starpos, uint edsmid = 0, uint eddbid = 0)
        {
            int     index     = 0;
            int     starclass = 0;
            ByteXYZ blkcoords = new ByteXYZ();

            int[] coords = null;
            HandAuthoredSector sector = null;

            if (SystemsByName.ContainsKey(sysname))
            {
                List <PGStarMatch> matches = SystemsByName[sysname].Select(i => SystemsById[i]).ToList();

                if (matches.Count == 1)
                {
                    return(matches[0]);
                }

                foreach (PGStarMatch sm in matches)
                {
                    Vector3 smcoords = sm.Coords;
                    Vector3 diff     = new Vector3 {
                        X = smcoords.X - starpos.X, Y = smcoords.Y - starpos.Y, Z = smcoords.Z - starpos.Z
                    };
                    double sqdist = diff.X * diff.X + diff.Y * diff.Y + diff.Z * diff.Z;
                    if (sqdist < 0.015625)
                    {
                        sector = sm.HASector;
                        return(sm);
                    }
                }
            }

            string regionname = GetProcGenRegionNameFromSystemName(sysname, out index, out starclass, ref blkcoords);

            int blocksize = 40960 >> starclass;

            double vx = starpos.X + 49985;
            double vy = starpos.Y + 40985;
            double vz = starpos.Z + 24105;

            if (vx < 0 || vx >= 163840 || vy < 0 || vy > 163840 || vz < 0 || vz > 163840)
            {
                return(PGStarMatch.Invalid);
            }

            int x = (int)(vx * 32 + 0.5);
            int y = (int)(vy * 32 + 0.5);
            int z = (int)(vz * 32 + 0.5);

            int cx = x / blocksize;
            int cy = y / blocksize;
            int cz = z / blocksize;

            if (regionname != null && starpos != null && !Double.IsNaN(starpos.X) && !Double.IsNaN(starpos.Y) && !Double.IsNaN(starpos.Z))
            {
                sector = HandAuthoredSectors.Sectors.FindSector(regionname)?.First();
                if (sector != null)
                {
                    int[] basecoords = sector.GetBaseBlockCoords(starclass);
                    coords = new int[]
                    {
                        basecoords[0] + blkcoords.X,
                        basecoords[1] + blkcoords.Y,
                        basecoords[2] + blkcoords.Z
                    };
                }
                else
                {
                    int     bx           = (x % 40960) / blocksize;
                    int     by           = (y % 40960) / blocksize;
                    int     bz           = (z % 40960) / blocksize;
                    ByteXYZ regioncoords = new ByteXYZ {
                        X = (sbyte)(x / 40960), Y = (sbyte)(y / 40960), Z = (sbyte)(z / 40960)
                    };
                    string  pgregion   = PGSectors.GetSectorName(regioncoords);
                    ByteXYZ nregcoords = pgregion == regionname ? regioncoords : PGSectors.GetSectorPos(regionname);

                    if (bx == blkcoords.X && by == blkcoords.Y && bz == blkcoords.Z && nregcoords == regioncoords)
                    {
                        coords = new int[] { cx, cy, cz };
                    }
                    else
                    {
                        ByteXYZ relcoords = new ByteXYZ {
                            X = (sbyte)bx, Y = (sbyte)by, Z = (sbyte)bz
                        };

                        if (nregcoords == ByteXYZ.Invalid)
                        {
                            Console.WriteLine($"System {sysname}: Unknown sector {regionname} @ {regioncoords} - coordname={pgregion} {GetPgSuffix(relcoords, starclass, index)}");
                        }
                        else
                        {
                            Vector3 namecoords = new Vector3
                            {
                                X = (nregcoords.X * 40960 + blkcoords.X * blocksize) / 32.0 - 49985,
                                Y = (nregcoords.Y * 40960 + blkcoords.Y * blocksize) / 32.0 - 40985,
                                Z = (nregcoords.Z * 40960 + blkcoords.Z * blocksize) / 32.0 - 24105
                            };
                            Console.WriteLine($"System {sysname}: Bad coordinates for sector {regionname} @ {regioncoords} - coordname={pgregion} {GetPgSuffix(relcoords, starclass, index)} | namecoords={namecoords}");
                        }
                        return(PGStarMatch.Invalid);
                    }
                }

                int ix = coords[0];
                int iy = coords[1];
                int iz = coords[2];

                long id = GetProcGenId(coords, starclass, index);

                if (SystemsById.ContainsKey(id))
                {
                    PGStarMatch sm = SystemsById[id];

                    if (cx != ix || cy != iy || cz != iz)
                    {
                        Vector3 error = new Vector3 {
                            X = coords[0] * blocksize / 32.0 - 49985, Y = coords[1] * blocksize / 32.0 - 40985, Z = coords[2] * blocksize / 32.0 - 24105
                        };
                        Console.WriteLine($"Warning: System {sysname} Coord mismatch - starpos={starpos} | match={sm.Coords} | namepos={error}");
                    }

                    if ((eddbid == 0 && edsmid != 0 && sm._EdsmId == 0) || (sm._EdsmId == edsmid && eddbid != 0 && sm._EddbId == 0))
                    {
                        if (eddbid == 0 && edsmid != 0 && sm._EdsmId == 0)
                        {
                            sm._EdsmId = edsmid;
                        }

                        if (sm._EdsmId == edsmid && eddbid != 0 && sm._EddbId == 0)
                        {
                            sm._EddbId = eddbid;
                        }

                        SystemsById[id] = sm;
                    }

                    return(sm);
                }
                else
                {
                    PGStarMatch sm = new PGStarMatch
                    {
                        _RegionCoordsX           = (sbyte)(x / 40960),
                        _RegionCoordsY           = (sbyte)(y / 40960),
                        _RegionCoordsZ           = (sbyte)(z / 40960),
                        _RelCoordsX              = (ushort)(x % 40960),
                        _RelCoordsY              = (ushort)(y % 40960),
                        _RelCoordsZ              = (ushort)(z % 40960),
                        _StarSeq                 = (ushort)index,
                        _StarClass               = (byte)starclass,
                        _HandAuthoredSectorIndex = (ushort)(HandAuthoredSectors.Sectors.IndexOf(sector) + 1),
                        _EdsmId = edsmid,
                        _EddbId = eddbid
                    };

                    if (cx != ix || cy != iy || cz != iz)
                    {
                        Vector3 error = new Vector3 {
                            X = coords[0] * blocksize / 32.0 - 49985, Y = coords[1] * blocksize / 32.0 - 40985, Z = coords[2] * blocksize / 32.0 - 24105
                        };
                        int[] bc = sm.BlockCoords;
                        Console.WriteLine($"Warning: System {sysname} Coord mismatch - pgname={sm.HPGName} | starpos={starpos} | namepos={error} | nc=({coords[0]}, {coords[1]}, {coords[2]}) | pc=({bc[0]}, {bc[1]}, {bc[2]})");
                    }

                    SystemsById[id] = sm;
                    return(sm);
                }
            }
            else
            {
                return(PGStarMatch.Invalid);
            }
        }