Beispiel #1
0
        public override double TransferTo(PartResourceInfo destination, double amount)
        {
            SimplePartResource dest = (SimplePartResource)destination;

            amount = Math.Min(Math.Min(amount, Amount), dest.MaxAmount - dest.Amount);
            dest.resource.amount += amount;
            resource.amount      -= amount;
            return(amount);
        }
        private void RebuildLists(Vessel vessel)
        {
            this.Log("Rebuilding resource lists.");
            // try to restore the old vessel info if we're switching vessels
            if (vesselInfo.vessel != vessel)
            {
                recentVessels.RemoveAll(v => !FlightGlobals.Vessels.Contains(v.vessel)); // remove information about dead vessels
                int index = recentVessels.FindIndex(v => v.vessel == vessel);
                if (vesselInfo.vessel != null)                                           // save the current data if it's not the initial, uninitialized state
                {
                    recentVessels.Add(vesselInfo);
                }
                if (index >= 0) // if we found the vessel in our memory, use it
                {
                    vesselInfo = recentVessels[index];
                    recentVessels.RemoveAt(index); // we'll add it back the next time we switch ships
                }
                else
                {
                    vesselInfo = new VesselInfo(vessel);
                }

                if (recentVessels.Count >= MaxRecentVessels)
                {
                    recentVessels.RemoveAt(0);
                }
            }



            // Remove parts that don't exist any more
            List <string> toDelete = new List <string>();

            foreach (KeyValuePair <string, ResourceInfo> resourceEntry in vesselInfo.resources)
            {
//				this.Log( "Removing Parts for: " + resourceEntry.Key );
                resourceEntry.Value.parts.RemoveAll(partInfo => !vessel.parts.Contains(partInfo.part));

                if (resourceEntry.Value.parts.Count == 0)
                {
//					this.Log( "Remove: " + resourceEntry.Key );
                    toDelete.AddUnique(resourceEntry.Key);
                }
            }
            // See if we have eliminated a whole resource
            foreach (string resource in toDelete)
            {
                vesselInfo.resources.Remove(resource);
//				this.Log( "Removed: " + resource );
            }



            // If tanks change their contents then we need to keep up.
            toDelete.Clear( );
            foreach (Part part in vessel.parts)
            {
                foreach (KeyValuePair <string, ResourceInfo> resourceEntry in vesselInfo.resources)
                {
                    if (resourceEntry.Key == "_RocketFuel")                      // "_RocketFuel" isn't real
                    {
                        List <ResourcePartMap> PartsToRemove = new List <ResourcePartMap>( );
                        foreach (ResourcePartMap pi in resourceEntry.Value.parts)
                        {
                            if (pi.part == part)
                            {
                                if (!part.Resources.Contains("Oxidizer") || !part.Resources.Contains("LiquidFuel"))
                                {
                                    PartsToRemove.Add(pi);
//									this.Log( part.name + " changed" );
                                }
                            }
                        }
                        foreach (ResourcePartMap pi in PartsToRemove)
                        {
                            resourceEntry.Value.parts.Remove(pi);
                        }
                        if (resourceEntry.Value.parts.Count == 0)
                        {
//							this.Log( "Remove: " + resourceEntry.Key );
                            toDelete.AddUnique(resourceEntry.Key);
                        }
                    }
                    else
                    {
                        List <ResourcePartMap> PartsToRemove = new List <ResourcePartMap>( );
                        foreach (ResourcePartMap pi in resourceEntry.Value.parts)
                        {
                            if (pi.part == part)
                            {
                                if (!part.Resources.Contains(resourceEntry.Key))
                                {
                                    PartsToRemove.Add(pi);
//									this.Log( part.name + " changed" );
                                }
                            }
                        }
                        foreach (ResourcePartMap pi in PartsToRemove)
                        {
                            resourceEntry.Value.parts.Remove(pi);
                        }
                        if (resourceEntry.Value.parts.Count == 0)
                        {
//							this.Log( "Remove: " + resourceEntry.Key );
                            toDelete.AddUnique(resourceEntry.Key);
                        }
                    }
                }
            }
            // See if we have eliminated a whole resource
            foreach (string resource in toDelete)
            {
                vesselInfo.resources.Remove(resource);
//				this.Log( "Removed: " + resource );
            }



            // Add all resources in all tanks
            Dictionary <object, int> shipIds = ComputeShipIds(vessel);

            foreach (Part part in vessel.parts)
            {
                if (part.Resources.Contains("Oxidizer") && part.Resources.Contains("LiquidFuel"))
                {
                    AddResource("_RocketFuel", part, shipIds, (p, n) => { var r = new RocketFuelResource(); r.Refresh(p); return(r); });
                }
                foreach (PartResource resource in part.Resources)
                {
//this.Log( part.name + " - " + resource.resourceName + ":" + resource.amount.ToString( ) );
                    // skip the electric charge resource of engines with alternators, because they can't be balanced.
                    // any charge placed in an alternator just disappears
                    if (resource.resourceName == "ElectricCharge" && part.Modules.GetModules <ModuleAlternator>().Count != 0)
                    {
                        continue;
                    }
                    AddResource(resource.resourceName, part, shipIds, (p, n) => { var r = new SimplePartResource(n); r.Refresh(p); return(r); });
                }
            }

            SortParts((a, b) => a.shipId - b.shipId); // make sure resources are grouped by ship ID

            vesselInfo.lastPartCount = vessel.parts.Count;
            vesselInfo.lastSituation = vessel.situation;
        }