// execute the recipe
        public bool Execute(Vessel v, Vessel_Resources resources)
        {
            // determine worst input ratio
            // - pure input recipes can just underflow
            double worst_input = left;

            if (outputs.Count > 0)
            {
                for (int i = 0; i < inputs.Count; ++i)
                {
                    Entry         e   = inputs[i];
                    Resource_Info res = resources.Info(v, e.name);
                    worst_input = Lib.Clamp((res.amount + res.deferred) * e.inv_quantity, 0.0, worst_input);
                }
            }

            // determine worst output ratio
            // - pure output recipes can just overflow
            double worst_output = left;

            if (inputs.Count > 0)
            {
                for (int i = 0; i < outputs.Count; ++i)
                {
                    Entry e = outputs[i];
                    if (!e.dump) // ignore outputs that can dump overboard
                    {
                        Resource_Info res = resources.Info(v, e.name);
                        worst_output = Lib.Clamp((res.capacity - (res.amount + res.deferred)) * e.inv_quantity, 0.0, worst_output);
                    }
                }
            }

            // determine worst-io
            double worst_io = Math.Min(worst_input, worst_output);

            // consume inputs
            for (int i = 0; i < inputs.Count; ++i)
            {
                Entry e = inputs[i];
                resources.Consume(v, e.name, e.quantity * worst_io);
            }

            // produce outputs
            for (int i = 0; i < outputs.Count; ++i)
            {
                Entry e = outputs[i];
                resources.Produce(v, e.name, e.quantity * worst_io);
            }

            // update amount left to execute
            left -= worst_io;

            // the recipe was executed, at least partially
            return(worst_io > double.Epsilon);
        }
Exemple #2
0
 static void ProcessCommand(Vessel v, ProtoPartSnapshot p, ProtoPartModuleSnapshot m, ModuleCommand command, Vessel_Resources resources, double elapsed_s)
 {
     // do not consume if this is a MCM with no crew
     // rationale: for consistency, the game doesn't consume resources for MCM without crew in loaded vessels
     //            this make some sense: you left a vessel with some battery and nobody on board, you expect it to not consume EC
     if (command.minimumCrew == 0 || p.protoModuleCrew.Count > 0)
     {
         // for each input resource
         foreach (ModuleResource ir in command.resHandler.inputResources)
         {
             // consume the resource
             resources.Consume(v, ir.name, ir.rate * elapsed_s);
         }
     }
 }
        // consume EC for transmission, and transmit science data
        public static void Update(Vessel v, Vessel_Info vi, VesselData vd, Vessel_Resources resources, double elapsed_s)
        {
            // hard-coded transmission buffer size in Mb
            const double buffer_capacity = 8.0;

            // do nothing if science system is disabled
            if (!Features.Science)
            {
                return;
            }

            // avoid corner-case when RnD isn't live during scene changes
            // - this avoid losing science if the buffer reach threshold during a scene change
            if (HighLogic.CurrentGame.Mode != Game.Modes.SANDBOX && ResearchAndDevelopment.Instance == null)
            {
                return;
            }

            // get connection info
            ConnectionInfo conn = vi.connection;

            // consume ec if data is transmitted or relayed
            if (vi.transmitting.Length > 0 || vi.relaying.Length > 0)
            {
                if (Features.KCommNet)
                {
                    // This means that it is only relay, don't send data from yourself
                    // CommNet: When vessel is relay, only antennas that are relay will consume ec
                    if (vi.relaying.Length > 0 && vi.transmitting.Length == 0)
                    {
                        resources.Consume(v, "ElectricCharge", conn.relaycost * elapsed_s);
                    }
                    else
                    {
                        resources.Consume(v, "ElectricCharge", conn.cost * elapsed_s);
                    }
                }
                resources.Consume(v, "ElectricCharge", conn.cost * elapsed_s);
            }

            // get filename of data being downloaded
            string filename = vi.transmitting;

            // if some data is being downloaded
            // - avoid cornercase at scene changes
            if (filename.Length > 0 && vd.drive.files.ContainsKey(filename))
            {
                // get file
                File file = vd.drive.files[filename];

                // determine how much data is transmitted
                double transmitted = Math.Min(file.size, conn.rate * elapsed_s);

                // consume data in the file
                file.size -= transmitted;

                // accumulate in the buffer
                file.buff += transmitted;

                // if buffer is full, or file was transmitted completely
                if (file.size <= double.Epsilon || file.buff > buffer_capacity)
                {
                    // collect the science data
                    Science.Credit(filename, file.buff, true, v.protoVessel);

                    // reset the buffer
                    file.buff = 0.0;
                }

                // if file was transmitted completely
                if (file.size <= double.Epsilon)
                {
                    // remove the file
                    vd.drive.files.Remove(filename);

                    // inform the user
                    Message.Post
                    (
                        Lib.BuildString("<color=cyan><b>DATA RECEIVED</b></color>\nTransmission of <b>", Science.Experiment(filename).name, "</b> completed"),
                        Lib.TextVariant("Our researchers will jump on it right now", "The checksum is correct, data must be valid")
                    );
                }
            }
        }