Example #1
0
        public override bool IsValid(ProtoCrewMember astronaut)
        {
            if (!base.IsValid(astronaut))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                return(false);
            }

            //Get roster resource
            double amount     = 0;
            double maxAmount  = 0;
            double percentage = 0;

            if (isRosterResource)
            {
                AstronautData astronautData = SnacksScenario.Instance.GetAstronautData(astronaut);
                if (astronautData == null)
                {
                    return(false);
                }
                if (!astronautData.rosterResources.ContainsKey(resourceName))
                {
                    return(false);
                }

                SnacksRosterResource rosterResource = astronautData.rosterResources[resourceName];
                amount     = rosterResource.amount;
                maxAmount  = rosterResource.maxAmount;
                percentage = amount / maxAmount;
            }

            //Try to get vessel resource
            else
            {
                if (astronaut.KerbalRef == null || astronaut.KerbalRef.InVessel == null)
                {
                    return(false);
                }

                List <ProtoPartResourceSnapshot> protoPartResources = new List <ProtoPartResourceSnapshot>();
                ProcessedResource.GetResourceTotals(astronaut.KerbalRef.InVessel, resourceName, out amount, out maxAmount, protoPartResources);
                percentage = amount / maxAmount;
            }

            //Now perform the check
            if (checkMaxAmount)
            {
                amount = maxAmount;
            }
            else if (checkAsPercentage)
            {
                amount = percentage;
            }
            switch (checkType)
            {
            case CheckValueConditionals.checkEquals:
                return(amount.Equals(valueToCheck));

            case CheckValueConditionals.checkGreaterOrEqual:
                return(amount.Equals(valueToCheck) || amount > valueToCheck);

            case CheckValueConditionals.checkGreaterThan:
                return(amount > valueToCheck);

            case CheckValueConditionals.checkLesserOrEqual:
                return(amount.Equals(valueToCheck) || amount < valueToCheck);

            case CheckValueConditionals.checkLessThan:
                return(amount < valueToCheck);
            }

            return(false);
        }
Example #2
0
        public override void ApplyOutcome(Vessel vessel, SnacksProcessorResult result)
        {
            if (!isRosterResource && vessel != null)
            {
                List <ProtoPartResourceSnapshot> protoPartResources = new List <ProtoPartResourceSnapshot>();
                double vesselCurrentAmount = 0;
                double vesselMaxAmount     = 0;
                double demand = amount;

                //Select random amount if enabled
                if (randomMin != randomMax)
                {
                    demand = UnityEngine.Random.Range(randomMin, randomMax);
                }

                //Get current totals
                ProcessedResource.GetResourceTotals(vessel, resourceName, out vesselCurrentAmount, out vesselMaxAmount, protoPartResources);

                //If the vessel has no resource at all then it hasn't been visited in-game yet.
                if (vesselMaxAmount <= 0)
                {
                    result.resultType = SnacksResultType.notApplicable;

                    //Call the base class
                    base.ApplyOutcome(vessel, result);

                    return;
                }

                //Multiply demand by affected crew count
                if (result.appliedPerCrew && !selectRandomCrew)
                {
                    demand *= result.affectedKerbalCount;
                }

                //If we have enough to support the whole crew, then we're good.
                if ((vesselCurrentAmount / demand) >= 0.999)
                {
                    //Request resource
                    ProcessedResource.RequestResource(vessel, resourceName, demand, protoPartResources);
                }

                //We don't have enough to support the whole crew. Figure out how many we can support.
                else
                {
                    int totalServed = (int)Math.Floor(vesselCurrentAmount / amount);

                    demand = totalServed * amount;
                    ProcessedResource.RequestResource(vessel, resourceName, vesselCurrentAmount, protoPartResources);
                }

                //Inform player
                if (!string.IsNullOrEmpty(playerMessage))
                {
                    ScreenMessages.PostScreenMessage(playerMessage, 5, ScreenMessageStyle.UPPER_LEFT);
                }
            }

            else if (result.afftectedAstronauts.Count > 0)
            {
                ProtoCrewMember[] astronauts    = null;
                AstronautData     astronautData = null;
                string            message       = string.Empty;

                //Get the crew manifest
                astronauts = result.afftectedAstronauts.ToArray();

                //Select random crew if needed
                if (selectRandomCrew)
                {
                    int randomIndex = UnityEngine.Random.Range(0, astronauts.Length - 1);
                    astronauts = new ProtoCrewMember[] { astronauts[randomIndex] };
                }

                for (int index = 0; index < astronauts.Length; index++)
                {
                    astronautData = SnacksScenario.Instance.GetAstronautData(astronauts[index]);

                    if (astronautData.rosterResources.ContainsKey(resourceName))
                    {
                        SnacksRosterResource resource = astronautData.rosterResources[resourceName];
                        resource.amount -= amount;
                        if (resource.amount <= 0)
                        {
                            resource.amount = 0;
                        }
                        astronautData.rosterResources[resourceName] = resource;

                        SnacksScenario.onRosterResourceUpdated.Fire(vessel, resource, astronautData, astronauts[index]);
                    }

                    //Inform player
                    if (!string.IsNullOrEmpty(playerMessage))
                    {
                        message = vessel.vesselName + ": " + astronauts[index].name + " " + playerMessage;
                        ScreenMessages.PostScreenMessage(message, 5.0f, ScreenMessageStyle.UPPER_LEFT);
                    }
                }

                //Call the base class
                base.ApplyOutcome(vessel, result);
            }
        }