Esempio n. 1
0
        /**
         * Removes the calculated number of snacks from the vessel.
         * returns the number of snacks that were required, but missing.
         * */
        public double ConsumeAndGetDeficit(Vessel vessel)
        {
            double demand    = 0;
            double fed       = 0;
            double crewCount = SnacksScenario.Instance.GetNonExemptCrewCount(vessel);

            //Calculate for loaded vessel
            if (vessel.loaded)
            {
                demand = vessel.GetCrewCount() * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.GetVesselCrew());

                if (demand <= 0)
                {
                    return(0);
                }

                fed = vessel.rootPart.RequestResource(SnacksProperties.SnacksResourceName, demand, ResourceFlowMode.ALL_VESSEL);
            }

            //Calculate for proto vessel
            else
            {
                //Unloaded vessels need to run their recyclers and snack processors.
                runConverters(vessel.protoVessel);

                //Now calculate demand.
                demand = vessel.protoVessel.GetVesselCrew().Count *SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.protoVessel.GetVesselCrew());

                if (demand <= 0)
                {
                    return(0);
                }

                fed = GetSnackResource(vessel.protoVessel.protoPartSnapshots, demand);
            }

            //Fire consume snacks event
            //Gives listeners a chance to alter the values.
            SnackConsumption snackConsumption = new SnackConsumption();

            snackConsumption.demand = demand;
            snackConsumption.fed    = fed;
            snackConsumption.vessel = vessel;
            SnackController.onConsumeSnacks.Fire(snackConsumption);

            //Request resource (loaded vessel)
            if (vessel.loaded)
            {
                if (fed == 0)
                {
                    return(vessel.GetCrewCount() * SnacksProperties.SnacksPerMeal);
                }

                //If recycling is enabled then produce soil.
                if (SnacksProperties.RecyclersEnabled)
                {
                    vessel.rootPart.RequestResource(SnacksProperties.SoilResourceName, -fed, ResourceFlowMode.ALL_VESSEL);
                }
            }

            //Request resource (unloaded vessel)
            else
            {
                if (fed == 0)
                {
                    return(vessel.protoVessel.GetVesselCrew().Count *SnacksProperties.SnacksPerMeal);
                }

                //If recycling is enabled then produce soil.
                if (SnacksProperties.RecyclersEnabled)
                {
                    AddSoilResource(vessel.protoVessel.protoPartSnapshots, fed);
                }
            }

            return(demand - fed);
        }
Esempio n. 2
0
        /**
         * Removes the calculated number of snacks from the vessel.
         * returns the number of snacks that were required, but missing.
         * */
        public double ConsumeAndGetDeficit(Vessel vessel)
        {
            Log("ConsumeAndGetDeficit called for vessel:" + vessel.vesselName + " type: " + vessel.vesselType.ToString());
            double demand    = 0;
            double fed       = 0;
            double crewCount = SnacksScenario.Instance.GetNonExemptCrewCount(vessel);

            Log("Non-exempt crewCount: " + crewCount);

            //Calculate for loaded vessel
            if (vessel.loaded)
            {
                demand = crewCount * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.GetVesselCrew());
                Log("(Loaded vessel) demand: " + demand);

                if (demand <= 0)
                {
                    return(0);
                }

                fed = vessel.rootPart.RequestResource(SnacksProperties.SnacksResourceName, demand, ResourceFlowMode.ALL_VESSEL);
                Log("(Loaded vessel) fed: " + fed);
            }

            //Calculate for proto vessel
            else
            {
                //Unloaded vessels need to run their recyclers and snack processors.
                Log("Calling runConverters");
                try
                {
                    runConverters(vessel.protoVessel);
                }
                catch (Exception ex)
                {
                    Log("runConverters encountered an error: " + ex.ToString());
                }

                //Now calculate demand.
                try
                {
                    demand = crewCount * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.protoVessel.GetVesselCrew());
                    Log("(Unloaded vessel) demand: " + demand);

                    if (demand <= 0)
                    {
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    Log("calculateExtraSnacksRequired encountered an error: " + ex.ToString());
                    return(0);
                }

                try
                {
                    fed = GetSnackResource(vessel.protoVessel.protoPartSnapshots, demand);
                    Log("(Unloaded vessel) fed: " + fed);
                }
                catch (Exception ex)
                {
                    Log("GetSnackResource encountered an error: " + ex.ToString());
                    return(0);
                }
            }

            //Fire consume snacks event
            //Gives listeners a chance to alter the values.
            try
            {
                SnackConsumption snackConsumption = new SnackConsumption();
                snackConsumption.demand = demand;
                snackConsumption.fed    = fed;
                snackConsumption.vessel = vessel;
                Log("Fired event onConsumeSnacks");
                SnackController.onConsumeSnacks.Fire(snackConsumption);
            }
            catch (Exception ex)
            {
                Log("onConsumeSnacks event encountered an error: " + ex.ToString());
            }

            //Request resource (loaded vessel)
            if (vessel.loaded)
            {
                if (fed == 0)
                {
                    return(vessel.GetCrewCount() * SnacksProperties.SnacksPerMeal);
                }

                //If recycling is enabled then produce soil.
                if (SnacksProperties.RecyclersEnabled)
                {
                    vessel.rootPart.RequestResource(SnacksProperties.SoilResourceName, -fed, ResourceFlowMode.ALL_VESSEL);
                    Log("Produced soil: " + fed);
                }
            }

            //Request resource (unloaded vessel)
            else
            {
                if (fed == 0)
                {
                    return(vessel.protoVessel.GetVesselCrew().Count *SnacksProperties.SnacksPerMeal);
                }

                //If recycling is enabled then produce soil.
                if (SnacksProperties.RecyclersEnabled)
                {
                    AddSoilResource(vessel.protoVessel.protoPartSnapshots, fed);
                    Log("Produced soil: " + fed);
                }
            }

            return(demand - fed);
        }