Example #1
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);
            }
        }