public static String FindOwnerName(PlanetData pData)
        {
            string oName; // base unowned name
            GlobalGameData gameDataRef = GameObject.Find("GameManager").GetComponent<GlobalGameData>();

            foreach (Civilization civ in gameDataRef.CivList)
            {
                for (int x = 0; x < civ.PlanetIDList.Count; x++)
                {
                    if (civ.PlanetIDList[x] == pData.ID) // will need to change
                    {
                        oName = civ.Name;
                        return oName.ToUpper();
                    }
                }
            }

            return "UNKNOWN DESIGNATION";
        }
        public static Color FindPlanetOwnerColor(PlanetData pData)
        {
            GlobalGameData gameDataRef = GameObject.Find("GameManager").GetComponent<GlobalGameData>();
            Color pColor = Color.white; // base unowned color

            foreach (Civilization civ in gameDataRef.CivList)
            {
                for (int x = 0; x < civ.PlanetIDList.Count; x++)
                {
                    if (civ.PlanetIDList[x] == pData.ID) // will need to change
                    {
                        pColor = civ.Color;
                        return pColor;
                    }
                }
            }

            return pColor;
        }
        void DrawPlanets(StarData sData)
        {
            GameObject selectedStar = GetSelectedStar();
            Vector3 starPosition = GetSelectedStar().GetComponent<Star>().transform.position;

            for (int x = 0; x < 6; x++)
            {
                if (sData.PlanetSpots[x] != null)
                {
                    // add planet to draw, if a sprite exists
                    if (sData.PlanetSpots[x].PlanetSpriteNumber != -1)
                        switch (sData.PlanetSpots[x].Type)
                        {
                            case PlanetData.ePlanetType.AsteroidBelt:
                                {
                                    planetsToDraw[x] = asteroidBelts[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Barren:
                                {
                                    planetsToDraw[x] = barrenPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Greenhouse:
                                {
                                    planetsToDraw[x] = greenhousePlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Desert:
                                {
                                    planetsToDraw[x] = desertPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Terran:
                                {
                                    planetsToDraw[x] = terranPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Ice:
                                {
                                    planetsToDraw[x] = icePlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.IceGiant:
                                {
                                    planetsToDraw[x] = dwarfJovianPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.GasGiant:
                                {
                                    planetsToDraw[x] = jovianPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Lava:
                                {
                                    planetsToDraw[x] = lavaPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Irradiated:
                                {
                                    planetsToDraw[x] = irradiatedPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.SuperEarth:
                                {
                                    planetsToDraw[x] = superEarthPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.Ocean:
                                {
                                    planetsToDraw[x] = waterPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.BrownDwarf:
                                {
                                    planetsToDraw[x] = brownDwarfPlanets[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.IceBelt:
                                {
                                    planetsToDraw[x] = iceBelts[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            case PlanetData.ePlanetType.DustRing:
                                {
                                    planetsToDraw[x] = dustRings[sData.PlanetSpots[x].PlanetSpriteNumber];
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                }
            }

            for (int x = 0; x < 6; x++)
            {
                if (planetsToDraw[x] != null)
                {
                    // planet draw routine
                    PlanetData pData = new PlanetData();
                    float startingPlanetMargin = 110;
                    float screenScaleFactor = (float)Screen.width / 1920f;
                    float screenWidthFactor = 0f;

                    if (Screen.width >= 1600)
                    {
                        screenWidthFactor = 15f;
                    }
                    else if (Screen.width >= 1400)
                    {
                        screenWidthFactor = 14f;
                    }
                    else if (Screen.width >= 1280)
                        screenWidthFactor = 14f;

                    else
                        screenWidthFactor = 12.3f;

                    pData = sData.PlanetSpots[x];

                    Vector3 position = new Vector3(starPosition.x + ((selectedStar.transform.localScale.x / 2) + (startingPlanetMargin * screenScaleFactor)) + ((x + 1) * ((float)Screen.width / screenWidthFactor)), starPosition.y, 10); // derive the planet position from the star
                    GameObject planet = Instantiate(planetsToDraw[x], position, Quaternion.Euler(0, 180, 0)) as GameObject; // draw the planet
                    planet.AddComponent<Planet>(); // add the planet container
                    planet.GetComponent<Planet>().planetData = pData; // and attach the data
                    planet.name = pData.Name;
                    planet.GetComponent<CircleCollider2D>().radius = 2.0f;
                    int size = 0;

                    // normalize the size of the object
                    if (pData.Type == PlanetData.ePlanetType.IceBelt || pData.Type == PlanetData.ePlanetType.AsteroidBelt || pData.Type == PlanetData.ePlanetType.IceBelt)
                        size = 50;
                    else
                        size = pData.Size;

                    // size of the planet (need to increase)
                    pData.PlanetSystemScaleSize = size *.25f; // set the planet's size as it should be in the system view (will probably set with the gameobject)

                    planet.transform.localScale = new Vector2(pData.PlanetSystemScaleSize, pData.PlanetSystemScaleSize); // scale the sprite depending on size
                    planet.transform.Rotate(180, 180, 0);
                    listSystemPlanetsCreated.Add(planet); // add the planet to the drawn list (for destruction later as a group)

                    //draw rings if they exist
                    if (pData.Rings)
                    {
                        GameObject ring = Instantiate(rings[pData.PlanetRingSpriteNumber], position, Quaternion.Euler(0, 0, 0)) as GameObject; // create the ring if it exists
                        ring.transform.SetParent(planet.transform, false); // set as child of planet
                        ring.transform.Rotate(new Vector3(0, 0, 0 + pData.PlanetRingTilt));
                        ring.transform.localScale = new Vector3(1, 1, 1);
                        ring.transform.localPosition = new Vector3(0, 0, 0);
                        ring.GetComponent<SpriteRenderer>().sortingOrder = 1;
                    }
                }
            }
            planetsDrawn = true; // set flag
        }
        public static StarData GetCivHomeSystem(Civilization civ)
        {
            GlobalGameData gameDataRef = GameObject.Find("GameManager").GetComponent<GlobalGameData>();
            GalaxyData galaxyDataRef = GameObject.Find("GameManager").GetComponent<GalaxyData>();
            PlanetData pData = new PlanetData();
            StarData sData = new StarData();

            pData = galaxyDataRef.GalaxyPlanetDataList.Find(p => p.ID == civ.CapitalPlanetID);
            sData = galaxyDataRef.GalaxyStarDataList.Find(p => p.ID == pData.SystemID);

            return sData;
        }
 public static string ConvertPlanetRankEnum(PlanetData.ePlanetRank rank)
 {
     switch (rank)
     {
         case PlanetData.ePlanetRank.EstablishedColony:
             return "Established Colony";
         case PlanetData.ePlanetRank.ImperialCapital:
             return "Civilization Capital";
         case PlanetData.ePlanetRank.NewColony:
             return "New Colony";
         case PlanetData.ePlanetRank.ProvinceCapital:
             return "Provincial Capital";
         case PlanetData.ePlanetRank.SystemCapital:
             return "System Capital";
         case PlanetData.ePlanetRank.Uninhabited:
             return "Uninhabited";
         default:
             return "Unknown Rank";
     }
 }
 public static string ConvertPlanetEnum(PlanetData.ePlanetType type)
 {
     switch (type)
     {
         case PlanetData.ePlanetType.AsteroidBelt:
             return "Asteroid Belt";
         case PlanetData.ePlanetType.Barren:
             return "Barren Planet";
         case PlanetData.ePlanetType.BrownDwarf:
             return "Brown Dwarf";
         case PlanetData.ePlanetType.City:
             return "City Planet";
         case PlanetData.ePlanetType.Desert:
             return "Desert Planet";
         case PlanetData.ePlanetType.DustRing:
             return "Dust Ring";
         case PlanetData.ePlanetType.GasGiant:
             return "Gas Giant";
         case PlanetData.ePlanetType.Greenhouse:
             return "Greenhouse Planet";
         case PlanetData.ePlanetType.Ice:
             return "Ice Planet";
         case PlanetData.ePlanetType.IceBelt:
             return "Ice Belt";
         case PlanetData.ePlanetType.IceGiant:
             return "Ice Giant";
         case PlanetData.ePlanetType.Irradiated:
             return "Irradiated Planet";
         case PlanetData.ePlanetType.Lava:
             return "Lava Planet";
         case PlanetData.ePlanetType.Ocean:
             return "Ocean Planet";
         case PlanetData.ePlanetType.Organic:
             return "Organic Planet";
         case PlanetData.ePlanetType.SuperEarth:
             return "Super Earth Planet";
         case PlanetData.ePlanetType.Terran:
             return "Terran Planet";
         default:
             return "Unknown Planet";
     }
 }
        public static PlanetData GetPlanet(string ID)
        {
            GalaxyData galaxyDataRef = GameObject.Find("GameManager").GetComponent<GalaxyData>();
            PlanetData pData = new PlanetData();

            pData = galaxyDataRef.GalaxyPlanetDataList.Find(p => p.ID == ID);
            return pData;
        }
        public static List<StarData> GetCivSystems(Civilization civ)
        {
            GalaxyData galaxyDataRef = GameObject.Find("GameManager").GetComponent<GalaxyData>();
            PlanetData pData = new PlanetData();
            List<PlanetData> pDataList = new List<PlanetData>();
            List<StarData> sDataList = new List<StarData>();

            foreach (string ID in civ.PlanetIDList)
            {
                pData = galaxyDataRef.GalaxyPlanetDataList.Find(p => p.ID == ID);
                if (!sDataList.Exists(p => p.ID == pData.SystemID))
                    sDataList.Add(galaxyDataRef.GalaxyStarDataList.Find(p => p.ID == pData.SystemID));
            }

            return sDataList;
        }
        // trade functions
        public bool IsTradeAgreementValid(PlanetData proposedPlanet, string resource)
        {
            bool supportsTrade = false;
            bool hasFood = false;
            bool hasEnergy = false;
            bool hasAlpha = false;
            bool hasHeavy = false;
            bool hasRare = false;
            bool hasExportCapability = false;
            bool hasStarBaseCapacity = false;
            bool validTradeAgreementPossible = false;

            // first check that base is acceptable
            if ((ProvGovSupport != eSupportLevel.None) || (SysGovSupport != eSupportLevel.None)) // province gov must be on board
            {
                supportsTrade = true;                   // step 1     
                if (IsTradeHub)
                {
                    hasExportCapability = true;         // step 2
                    if (StarbaseLevel > 0)
                    {
                        hasStarBaseCapacity = true;     // and step 3
                        validTradeAgreementPossible = true; //  you've gotten this far with 3 checks, a trade is valid as long as there's capacity and materials available
                    }
                }
               
            }

            if (!validTradeAgreementPossible)
                return false; // exits if not possible at this point

            // now check on specific resources           
            if (FoodExportAvailable > 0) // if greater than 0 available for trade
            {
                hasFood = true;
            }

            if (EnergyExportAvailable > 0) // if greater than 0 available for trade
            {
                hasEnergy = true;
            }

            if (AlphaExportAvailable > 0) // if greater than 0 available for trade
            {
                hasAlpha = true;
            }

            if (HeavyExportAvailable > 0) // if greater than 0 available for trade
            {
                hasHeavy = true;
            }

            if (RareExportAvailable > 0) // if greater than 0 available for trade
            {
                hasRare = true;
            }
          
            // finally, if they have the resource asked for, return true
            if (resource == "food" && hasFood)
            {                           
                return true;         
            }

            if (resource == "energy" && hasEnergy)
            {
                return true;
            }

            if (resource == "alpha" && hasAlpha)
            {
                return true;
            }

            if (resource == "heavy" && hasHeavy)
            {
                return true;
            }

            if (resource == "rare" && hasRare)
            {
                return true;
            }

            return false; // if no availability, return false
        }
        void DetermineSystemCapitals()
        {
            foreach (Civilization civ in gameDataRef.CivList)
            {
                List<StarData> sDataList = new List<StarData>();
                sDataList = HelperFunctions.DataRetrivalFunctions.GetCivSystemList(civ);

                // cycle through each system and determine if a system capital is available
                foreach (StarData sData in sDataList)
                {
                    int highPlanetValue = 0;
                    PlanetData proposedCapital = new PlanetData();

                    foreach (PlanetData pData in sData.PlanetList)
                    {
                        if (HelperFunctions.DataRetrivalFunctions.GetCivPlanetList(civ).Exists(p => p.Name == pData.Name))
                        {
                            proposedCapital = pData;
                            if (highPlanetValue < pData.BasePlanetValue)
                            {
                                proposedCapital = pData;
                                highPlanetValue = pData.BasePlanetValue;
                            }
                        }
                    }
                    if (proposedCapital.Rank != PlanetData.ePlanetRank.ImperialCapital && proposedCapital.Rank != PlanetData.ePlanetRank.ProvinceCapital)
                    {
                        proposedCapital.Rank = PlanetData.ePlanetRank.SystemCapital;
                    }
                    sData.SystemCapitalID = proposedCapital.ID; // set as system capital by default
                }
            }
        }
        void GenerateProvinces()
        {
            foreach (Civilization civ in gameDataRef.CivList)
            {
                if (civ.CivMaxProvinceSize > 1)
                {
                    List<StarData> sDataList = new List<StarData>();
                    sDataList = HelperFunctions.DataRetrivalFunctions.GetCivSystemList(civ);

                    // get a size for each province and then create it with provinces in range
                    int provinceSize = UnityEngine.Random.Range(2, civ.CivMaxProvinceSize); // vary each province size
                    int maxDist = civ.AdminRating * 90; // max dist between systems
                    int count = 0;
                    bool provinceValid = false;

                    foreach (StarData sData in sDataList)
                    {
                        Province newProvince = new Province();

                        // generate province name
                        if (DataManager.systemNameList.Count > 0)
                        {
                            var nameIndex = UnityEngine.Random.Range(0, DataManager.systemNameList.Count);
                            newProvince.Name = DataManager.systemNameList[nameIndex];
                            DataManager.systemNameList.RemoveAt(nameIndex);
                            DataManager.systemNameList.TrimExcess();
                        }
                        else
                            newProvince.Name = "Generic Province";

                        newProvince.ID = "PRO" + UnityEngine.Random.Range(0, 10000);
                        newProvince.OwningCivID = civ.ID;
                        int x = 0;
                        bool allSystemsChecked = false;
                        provinceValid = false;

                        do
                        {
                            for (int y = count; y < sDataList.Count; y++)
                            {
                                float dist = HelperFunctions.Formulas.MeasureDistanceBetweenSystems(sData,sDataList[y]);
                                if (dist > 0 && dist < maxDist)
                                {
                                    if (sData.AssignedProvinceID == "")
                                    {
                                        sData.AssignedProvinceID = newProvince.ID;
                                        x += 1;
                                        provinceValid = true;
                                        sData.IsProvinceHub = true; // is the central system, will have the province capital installed
                                    }
                                    if (sDataList[y].AssignedProvinceID == "")
                                    {
                                        sDataList[y].AssignedProvinceID = newProvince.ID;
                                        x += 1;
                                        if (x == 1)
                                            sDataList[y].IsProvinceHub = true;
                                        provinceValid = true;
                                    }
                                }
                            }

                            if (sData.AssignedProvinceID == "") // if the system is not assigned to a province, create a one-shot province
                            {
                                Province nProv = new Province();

                                // generate province name
                                if (DataManager.systemNameList.Count > 0)
                                {
                                    var nameIndex = UnityEngine.Random.Range(0, DataManager.systemNameList.Count);
                                    nProv.Name = DataManager.systemNameList[nameIndex];
                                    DataManager.systemNameList.RemoveAt(nameIndex);
                                    DataManager.systemNameList.TrimExcess();
                                }
                                else
                                    nProv.Name = "Generic Province";

                                nProv.ID = "PRO" + UnityEngine.Random.Range(0, 10000);
                                nProv.OwningCivID = civ.ID;
                                sData.AssignedProvinceID = nProv.ID;
                                sData.IsProvinceHub = true;
                                gData.AddProvinceToList(nProv);
                            }

                            if (provinceValid)
                                gData.AddProvinceToList(newProvince);

                            allSystemsChecked = true; // all systems have been checked; go to next star
                        }
                        while (x < provinceSize && !allSystemsChecked);
                        count += 1;
                    }
                    // now that provinces are created, designate the province capital
                    foreach (StarData sData in sDataList)
                    {
                        if (sData.IsProvinceHub)
                        {
                            int highPlanetValue = 0;
                            PlanetData proposedCapital = new PlanetData();
                            foreach (PlanetData pData in sData.PlanetList)
                            {
                                if (HelperFunctions.DataRetrivalFunctions.GetCivPlanetList(civ).Exists(p => p.Name == pData.Name))
                                {
                                    if (highPlanetValue < pData.BasePlanetValue)
                                    {
                                        if (pData.Rank != PlanetData.ePlanetRank.ImperialCapital) // don't overwrite the imperial capital! If no other capital, the imperial capital serves as province capital also
                                        {
                                            proposedCapital = pData;
                                            highPlanetValue = pData.BasePlanetValue;
                                        }
                                    }
                                }
                            }
                            proposedCapital.Rank = PlanetData.ePlanetRank.ProvinceCapital;
                            HelperFunctions.DataRetrivalFunctions.GetProvince(sData.AssignedProvinceID).CapitalPlanetID = proposedCapital.ID; // set the capital world ID
                        }
                    }
                }
            }
        }