Example #1
0
    private void LookForTradePartner(Civilization civ, string resource, string planetID)
    {
        PlanetData needyPlanet          = HelperFunctions.DataRetrivalFunctions.GetPlanet(planetID);
        SortedList sortedPlanetDistList = new SortedList();

        // create the sorted list
        foreach (PlanetData pData in needyPlanet.System.Province.PlanetList)
        {
            if (pData.ID != planetID && pData.IsInhabited)
            {
                float dist = HelperFunctions.Formulas.MeasureDistanceBetweenSystems(needyPlanet.System, pData.System);
                while (sortedPlanetDistList.ContainsKey(dist))
                {
                    dist += .001f;
                }
                sortedPlanetDistList.Add(dist, pData);
            }
        }

        // now check for general ability to create agreement (distance, starbase, support level of viceroy)
        foreach (DictionaryEntry de in sortedPlanetDistList)
        {
            PlanetData exportPlanet = (PlanetData)de.Value;                // cast?

            if (exportPlanet.IsTradeAgreementValid(needyPlanet, resource)) // check for food agreement (is there enough food to send?)
            {
                TradeAgreement newTrade = new TradeAgreement();
                newTrade.ExportPlanetID = exportPlanet.ID;
                newTrade.Status         = TradeAgreement.eAgreementStatus.Active;
                newTrade.ImportPlanetID = needyPlanet.ID;
                switch (resource)
                {
                case "food":
                {
                    if (exportPlanet.FoodExportAvailable >= Math.Abs(needyPlanet.FoodDifference))
                    {
                        newTrade.FoodSent = Math.Abs(needyPlanet.FoodDifference);
                    }
                    else if (exportPlanet.FoodExportAvailable > 0)
                    {
                        newTrade.FoodSent = exportPlanet.FoodExportAvailable;
                    }
                    else
                    {
                        newTrade.FoodSent = 0;         // no food avail, error check
                    }
                    if (newTrade.FoodSent >= exportPlanet.StarbaseCapacityRemaining)
                    {
                        newTrade.FoodSent = exportPlanet.StarbaseCapacityRemaining;
                    }
                    break;
                }

                case "energy":
                {
                    if (exportPlanet.EnergyExportAvailable >= Math.Abs(needyPlanet.EnergyDifference))
                    {
                        newTrade.EnergySent = Math.Abs(needyPlanet.EnergyDifference);
                    }
                    else if (exportPlanet.EnergyExportAvailable > 0)
                    {
                        newTrade.EnergySent = exportPlanet.EnergyExportAvailable;
                    }
                    else
                    {
                        newTrade.EnergySent = 0;
                    }

                    if (newTrade.EnergySent >= exportPlanet.StarbaseCapacityRemaining)
                    {
                        newTrade.EnergySent = exportPlanet.StarbaseCapacityRemaining;
                    }
                    break;
                }

                case "alpha":
                {
                    if (exportPlanet.AlphaExportAvailable >= Math.Abs(needyPlanet.AlphaPreProductionDifference))
                    {
                        newTrade.AlphaSent = Math.Abs(needyPlanet.AlphaPreProductionDifference);
                    }
                    else if (exportPlanet.AlphaExportAvailable > 0)
                    {
                        newTrade.AlphaSent = exportPlanet.AlphaExportAvailable;
                    }
                    else
                    {
                        newTrade.AlphaSent = 0;
                    }

                    if (newTrade.AlphaSent >= exportPlanet.StarbaseCapacityRemaining)
                    {
                        newTrade.AlphaSent = exportPlanet.StarbaseCapacityRemaining;
                    }
                    break;
                }

                case "heavy":
                {
                    if (exportPlanet.HeavyExportAvailable >= Math.Abs(needyPlanet.HeavyPreProductionDifference))
                    {
                        newTrade.HeavySent = Math.Abs(needyPlanet.HeavyPreProductionDifference);
                    }
                    else if (exportPlanet.HeavyExportAvailable > 0)
                    {
                        newTrade.HeavySent = exportPlanet.HeavyExportAvailable;
                    }
                    else
                    {
                        newTrade.HeavySent = 0;
                    }

                    if (newTrade.HeavySent >= exportPlanet.StarbaseCapacityRemaining)
                    {
                        newTrade.HeavySent = exportPlanet.StarbaseCapacityRemaining;
                    }
                    break;
                }

                case "rare":
                {
                    if (exportPlanet.RareExportAvailable >= Math.Abs(needyPlanet.RarePreProductionDifference))
                    {
                        newTrade.RareSent = Math.Abs(needyPlanet.RarePreProductionDifference);
                    }
                    else if (exportPlanet.RareExportAvailable > 0)
                    {
                        newTrade.RareSent = exportPlanet.RareExportAvailable;
                    }
                    else
                    {
                        newTrade.RareSent = 0;
                    }

                    //starbase capacity check
                    if (newTrade.RareSent >= exportPlanet.StarbaseCapacityRemaining)
                    {
                        newTrade.RareSent = exportPlanet.StarbaseCapacityRemaining;
                    }
                    break;
                }

                default:
                    break;
                }

                if (newTrade.TotalSent >= 0.01) // minimum threshold amount for trades, also check for capacity of the trade
                {
                    Debug.Log("Trade of " + newTrade.TotalSent.ToString("N2") + " " + resource + " to " + newTrade.ImportPlanet.Name + " from " + newTrade.ExportPlanet.Name + " over " + newTrade.Distance.ToString("N1") + " LY for $" + newTrade.Cost.ToString("N2") + " with a " + newTrade.CostModifier.ToString("N1") + " modifier.");
                    gDataRef.ActiveTradeAgreements.Add(newTrade); // add the new agreement
                }

                // update the trade info for each planet
                UpdatePlanetTradeInfo(needyPlanet);
                UpdatePlanetTradeInfo(exportPlanet);
            }
        }
    }