private void tweakSeed(ref sysSeed s)
        {
            uint temp;

            temp = (s.w0 + s.w1 + s.w2);             /* 2 byte aritmetic */
            s.w0 = s.w1;
            s.w1 = s.w2;
            s.w2 = temp;
        }
        public StarSystemUtils(StarSystem starSystem)
        {
            this.starSystem = starSystem;

            //need to actually force the creation of the planets so we can calculate some params

            int habitability = 0;

            foreach (SystemBody body in starSystem.SystemBodies)
            {
                switch (body.BodyType)
                {
                case SystemBodyType.Asteroid:
                    if (body.Temperature < 70 && body.Temperature > -150)
                    {
                        habitability += 1;
                    }
                    break;

                case SystemBodyType.GasGiant:
                    break;

                case SystemBodyType.IceWorld:
                    habitability += 50;
                    break;

                case SystemBodyType.Inferno:
                    break;

                case SystemBodyType.RingedGasGiant:
                    break;

                case SystemBodyType.RockyPlanetoid:
                    if (body.Temperature < 70 && body.Temperature > -150)
                    {
                        habitability += 5;
                    }
                    break;

                case SystemBodyType.RockyWorld:
                    if (body.Temperature < 70 && body.Temperature > -150)
                    {
                        habitability += 10;
                    }
                    break;

                case SystemBodyType.SubGasGiant:
                    break;

                case SystemBodyType.Terrestrial:
                    habitability += 100; break;

                case SystemBodyType.Venuzian:
                    break;

                case SystemBodyType.WaterWorld:
                    habitability += 80; break;
                }
            }

            sysSeed systemSeed = new sysSeed();

            systemSeed.w0 = (uint)(starSystem.SystemId);
            systemSeed.w1 = (uint)(starSystem.Coords.Sector.X << 10 | (int)starSystem.SystemId);
            systemSeed.w2 = (uint)(starSystem.Coords.Sector.Y << 10 | (int)starSystem.SystemId);

            tweakSeed(ref systemSeed);
            tweakSeed(ref systemSeed);
            tweakSeed(ref systemSeed);
            tweakSeed(ref systemSeed);

            planSys  = this.MakeSystemBaseInfo(systemSeed, habitability);
            rnd_seed = planSys.goatsoupseed;
            seed     = (int)(systemSeed.w0 << 16 & systemSeed.w1);
        }
        private PlanSys MakeSystemBaseInfo(sysSeed s, int habitability)
        {
            PlanSys thisSys      = new PlanSys();
            uint    longnameflag = s.w0 & 64;

            thisSys.descOverride = null;
            Random rand = new Random((int)(s.w1 >> 8));



            thisSys.x = (s.w1 >> 8);
            thisSys.y = (s.w0 >> 8);

            int[] possibleEconomies = null;

            if (habitability > 0 && habitability <= 10)
            {
                thisSys.descOverride = "Mainly arid system with some small scale mining operations";
                thisSys.govType      = (uint)rand.Next(0, 2);
                possibleEconomies    = new int[] { 2, 3 };
            }
            else if (habitability > 10 && habitability <= 20)
            {
                thisSys.govType   = (uint)rand.Next(0, 4);
                possibleEconomies = new int[] { 1, 2, 3 };
            }
            else if (habitability > 20 && habitability <= 40)
            {
                thisSys.govType   = (uint)rand.Next(1, 5);
                possibleEconomies = new int[] { 0, 1, 2, 3 };
            }
            else if (habitability > 40 && habitability <= 80)
            {
                thisSys.govType   = (uint)rand.Next(2, 5);
                possibleEconomies = new int[] { 0, 1, 2, 3 };
            }
            else
            {
                possibleEconomies = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
                thisSys.govType   = (uint)rand.Next(2, 7);
            }

            if (habitability == 0)
            {
                thisSys.govType      = 0;
                thisSys.economy      = 0;
                thisSys.Population   = 0;
                thisSys.Productivity = 0;
                thisSys.techLevel    = 0;
            }
            else
            {
                thisSys.economy = (uint)rand.Next(0, possibleEconomies.Length - 1);

                if (thisSys.govType <= 1)
                {
                    thisSys.economy = ((thisSys.economy) | 2);
                }


                thisSys.techLevel  = ((s.w1 >> 8) & 3) + (thisSys.economy ^ 7);
                thisSys.techLevel += (thisSys.govType >> 1);
                if (((thisSys.govType) & 1) == 1)
                {
                    thisSys.techLevel += 1;
                }

                /* simulation of 6502's LSR then ADC */
                thisSys.Population  = 4 * (uint)((thisSys.techLevel + thisSys.economy) * (habitability / 100.0));
                thisSys.Population += (thisSys.govType) + 1;

                thisSys.Productivity  = (((thisSys.economy) ^ 7) + 3) * ((thisSys.govType) + 4);
                thisSys.Productivity *= (thisSys.Population) * 8;
            }

            thisSys.Radius = 256 * (((s.w2 >> 8) & 15) + 11) + thisSys.x;

            thisSys.goatsoupseed.a = (int)(s.w1 & 0xFF);
            thisSys.goatsoupseed.b = (int)(s.w1 >> 8);
            thisSys.goatsoupseed.c = (int)(s.w2 & 0xFF);
            thisSys.goatsoupseed.d = (int)(s.w2 >> 8);

            return(thisSys);
        }