/// <summary>
        /// Used externally to return the target Celestial Body
        /// </summary>
        /// <param name="cP">Instance of the requested Contract</param>
        /// <returns>Celestial Body object</returns>
        public static CelestialBody TargetBody(Contract c)
        {
            if (c == null || c.GetType() != typeof(DMSurveyContract))
            {
                return(null);
            }

            DMSurveyContract Instance = (DMSurveyContract)c;

            return(Instance.body);
        }
        /// <summary>
        /// Used externally to return the target Celestial Body
        /// </summary>
        /// <param name="cP">Instance of the requested Contract</param>
        /// <returns>Celestial Body object</returns>
        public static CelestialBody TargetBody(Contract c)
        {
            if (c == null || c.GetType() != typeof(DMSurveyContract))
            {
                return(null);
            }

            try
            {
                DMSurveyContract Instance = (DMSurveyContract)c;
                return(Instance.body);
            }
            catch (Exception e)
            {
                Debug.LogError("Error while accessing DMagic Survey Contract Target Body\n" + e);
                return(null);
            }
        }
        protected override bool Generate()
        {
            DMSurveyContract[] surveyContracts = ContractSystem.Instance.GetCurrentContracts <DMSurveyContract>();
            int offers    = 0;
            int active    = 0;
            int maxOffers = DMContractDefs.DMSurvey.maxOffers;
            int maxActive = DMContractDefs.DMSurvey.maxActive;

            for (int i = 0; i < surveyContracts.Length; i++)
            {
                DMSurveyContract s = surveyContracts[i];
                if (s.ContractState == State.Offered)
                {
                    offers++;
                }
                else if (s.ContractState == State.Active)
                {
                    active++;
                }
            }

            if (offers >= maxOffers)
            {
                return(false);
            }
            if (active >= maxActive)
            {
                return(false);
            }

            if (!DMUtils.partAvailable(new List <string>(1)
            {
                "dmmagBoom"
            }))
            {
                return(false);
            }

            sciList.AddRange(DMUtils.availableScience[DMScienceType.Space.ToString()].Values);

            if (sciList.Count > 0)
            {
                DMScience = sciList[rand.Next(0, sciList.Count)];
                sciList.Remove(DMScience);
            }
            else
            {
                return(false);
            }

            //Generates the science experiment, returns null if experiment fails any check
            if ((newParams[0] = DMSurveyGenerator.fetchSurveyScience(this.Prestige, GetBodies_Reached(false, false), GetBodies_NextUnreached(4, null), DMScience)) == null)
            {
                return(false);
            }

            body = newParams[0].Body;

            //Add an orbital parameter to difficult contracts
            if (this.Prestige == ContractPrestige.Exceptional)
            {
                this.AddParameter(new EnterOrbit(body));
            }

            for (int j = 1; j < 8; j++)
            {
                if (sciList.Count > 0)
                {
                    DMScience    = sciList[rand.Next(0, sciList.Count)];
                    newParams[j] = DMSurveyGenerator.fetchSurveyScience(body, DMScience);
                    sciList.Remove(DMScience);
                }
                else
                {
                    newParams[j] = null;
                }
            }

            //Add the science collection parent parameter
            DMCompleteParameter DMcp = new DMCompleteParameter(0, 1);

            this.AddParameter(DMcp);

            int limit       = 1;
            int maxRequests = 1;

            switch (prestige)
            {
            case ContractPrestige.Trivial:
                maxRequests = DMContractDefs.DMSurvey.trivialScienceRequests;
                break;

            case ContractPrestige.Significant:
                maxRequests = DMContractDefs.DMSurvey.significantScienceRequests;
                break;

            case ContractPrestige.Exceptional:
                maxRequests = DMContractDefs.DMSurvey.exceptionalScienceRequests;
                break;
            }

            //Add in all acceptable paramaters to the contract
            foreach (DMCollectScience DMC in newParams)
            {
                if (limit > maxRequests)
                {
                    break;
                }
                if (DMC != null)
                {
                    if (DMC.Container == null)
                    {
                        continue;
                    }

                    DMcp.addToSubParams(DMC);
                    float locationMod = GameVariables.Instance.ScoreSituation(DMUtils.convertSit(DMC.Situation), DMC.Body) * ((float)rand.Next(85, 116) / 100f);
                    DMC.SetScience(DMC.Container.Exp.baseValue * DMContractDefs.DMSurvey.Science.ParamReward * DMUtils.fixSubjectVal(DMC.Situation, 1f, body), null);
                    DMC.SetFunds(DMContractDefs.DMSurvey.Funds.ParamReward * locationMod, DMContractDefs.DMSurvey.Funds.ParamFailure * locationMod, body);
                    DMC.SetReputation(DMContractDefs.DMSurvey.Reputation.ParamReward * locationMod, DMContractDefs.DMSurvey.Reputation.ParamFailure * locationMod, null);
                    limit++;
                }
            }

            if (DMcp.ParameterCount < 3)
            {
                return(false);
            }

            int a = rand.Next(0, 4);

            if (a == 0)
            {
                this.agent = AgentList.Instance.GetAgent("DMagic");
            }
            else if (a == 1)
            {
                this.agent = AgentList.Instance.GetAgent(newParams[0].Container.Agent);
            }
            else
            {
                this.agent = AgentList.Instance.GetAgentRandom();
            }

            if (this.agent == null)
            {
                this.agent = AgentList.Instance.GetAgentRandom();
            }

            float primaryLocationMod = GameVariables.Instance.ScoreSituation(DMUtils.convertSit(newParams[0].Situation), newParams[0].Body) * ((float)rand.Next(85, 116) / 100f);

            float Mod = primaryLocationMod * DMcp.ParameterCount;

            base.SetExpiry(DMContractDefs.DMSurvey.Expire.MinimumExpireDays, DMContractDefs.DMSurvey.Expire.MaximumExpireDays);
            base.SetDeadlineYears(DMContractDefs.DMSurvey.Expire.DeadlineYears * ((float)rand.Next(80, 121)) / 100f, body);
            base.SetReputation(DMContractDefs.DMSurvey.Reputation.BaseReward * primaryLocationMod, DMContractDefs.DMSurvey.Reputation.BaseFailure * primaryLocationMod, null);
            base.SetFunds(DMContractDefs.DMSurvey.Funds.BaseAdvance * Mod, DMContractDefs.DMSurvey.Funds.BaseReward * Mod, DMContractDefs.DMSurvey.Funds.BaseFailure * Mod, body);
            base.SetScience(DMContractDefs.DMSurvey.Science.BaseReward * primaryLocationMod, null);
            return(true);
        }
        protected override bool Generate()
        {
            DMSurveyContract[] surveyContracts = ContractSystem.Instance.GetCurrentContracts <DMSurveyContract>();
            int offers    = 0;
            int active    = 0;
            int maxOffers = DMUtils.maxSurveyOffered;
            int maxActive = DMUtils.maxSurveyActive;

            for (int i = 0; i < surveyContracts.Length; i++)
            {
                DMSurveyContract s = surveyContracts[i];
                if (s.ContractState == State.Offered)
                {
                    offers++;
                }
                else if (s.ContractState == State.Active)
                {
                    active++;
                }
            }

            if (offers >= maxOffers)
            {
                return(false);
            }
            if (active >= maxActive)
            {
                return(false);
            }

            AvailablePart aPart = PartLoader.getPartInfoByName("dmmagBoom");

            if (aPart == null)
            {
                return(false);
            }
            if (!ResearchAndDevelopment.PartModelPurchased(aPart))
            {
                return(false);
            }

            sciList.AddRange(DMUtils.availableScience[DMScienceType.Space.ToString()].Values);

            if (sciList.Count > 0)
            {
                DMScience = sciList[rand.Next(0, sciList.Count)];
                sciList.Remove(DMScience);
            }
            else
            {
                return(false);
            }

            //Generates the science experiment, returns null if experiment fails any check
            if ((newParams[0] = DMSurveyGenerator.fetchSurveyScience(this.Prestige, GetBodies_Reached(false, false), GetBodies_NextUnreached(4, null), DMScience)) == null)
            {
                return(false);
            }

            body = newParams[0].Body;

            //Add an orbital parameter to difficult contracts
            if (this.Prestige == ContractPrestige.Exceptional)
            {
                this.AddParameter(new EnterOrbit(body));
            }

            for (int j = 1; j < 8; j++)
            {
                if (sciList.Count > 0)
                {
                    DMScience    = sciList[rand.Next(0, sciList.Count)];
                    newParams[j] = DMSurveyGenerator.fetchSurveyScience(body, DMScience);
                    sciList.Remove(DMScience);
                }
                else
                {
                    newParams[j] = null;
                }
            }

            //Add the science collection parent parameter
            DMCompleteParameter DMcp = new DMCompleteParameter(0, 1);

            this.AddParameter(DMcp);

            int limit = 1;

            //Add in all acceptable paramaters to the contract
            foreach (DMCollectScience DMC in newParams)
            {
                if (limit > (3 + (int)this.Prestige))
                {
                    break;
                }
                if (DMC != null)
                {
                    DMcp.addToSubParams(DMC, "CollectScience");
                    float locationMod = GameVariables.Instance.ScoreSituation(DMUtils.convertSit(DMC.Situation), DMC.Body) * ((float)rand.Next(85, 116) / 100f);
                    DMC.SetScience(DMC.Container.Exp.baseValue * 0.2f * DMUtils.science * DMUtils.fixSubjectVal(DMC.Situation, 1f, body), null);
                    DMC.SetFunds(3500f * DMUtils.reward * locationMod, body);
                    limit++;
                }
            }

            if (DMcp.ParameterCount < 3)
            {
                return(false);
            }

            int a = rand.Next(0, 4);

            if (a == 0)
            {
                this.agent = AgentList.Instance.GetAgent("DMagic");
            }
            else if (a == 1)
            {
                this.agent = AgentList.Instance.GetAgent(newParams[0].Container.Agent);
            }
            else
            {
                this.agent = AgentList.Instance.GetAgentRandom();
            }

            float primaryLocationMod = GameVariables.Instance.ScoreSituation(DMUtils.convertSit(newParams[0].Situation), newParams[0].Body) * ((float)rand.Next(85, 116) / 100f);

            base.SetExpiry(10f * DMUtils.deadline, 20f * DMUtils.deadline);
            base.SetDeadlineYears(1.7f * ((float)rand.Next(80, 121)) / 100f * DMUtils.deadline, body);
            base.SetReputation(1.9f * DMcp.ParameterCount * DMUtils.reward * primaryLocationMod, 1.5f * DMcp.ParameterCount * DMUtils.penalty * primaryLocationMod, null);
            base.SetFunds(8500 * DMcp.ParameterCount * DMUtils.forward * primaryLocationMod, 10500 * DMcp.ParameterCount * DMUtils.reward * primaryLocationMod, 7500 * DMcp.ParameterCount * DMUtils.penalty * primaryLocationMod, body);
            return(true);
        }