Ejemplo n.º 1
0
        protected void tallyResources(WBIResourceDistributor distributor, Dictionary <string, TalliedResource> sharedResourceTally, Dictionary <string, TalliedResource> requiredResourceTally)
        {
            //If the distributor isn't actively participating then we're done.
            if (!distributor.isParticipating)
            {
                return;
            }
            List <PartResource> sharedResources   = new List <PartResource>();
            List <PartResource> requiredResources = new List <PartResource>();
            TalliedResource     talliedResource;
            PartResource        resource;
            int totalCount;

            //Get the list of shared and required resources
            distributor.GetResourcesToDistribute(out sharedResources, out requiredResources);
            Log("[WBIDistributionManager] - sharedResources count: " + sharedResources.Count());
            Log("[WBIDistributionManager] - requiredResources count: " + requiredResources.Count());

            //For each required resource, add the distributor to the required resources map.
            totalCount = requiredResources.Count;
            for (int index = 0; index < totalCount; index++)
            {
                resource = requiredResources[index];

                //Add the resource to the dictionary if needed.
                if (requiredResourceTally.ContainsKey(resource.resourceName) == false)
                {
                    talliedResource = new TalliedResource();
                    requiredResourceTally.Add(resource.resourceName, talliedResource);
                    Log("[WBIDistributionManager] - requiredResourceTally added: " + resource.resourceName);
                }

                //Add the distributor to the tallied resource.
                talliedResource = requiredResourceTally[resource.resourceName];
                talliedResource.distributors.Add(distributor);
            }

            //Now add all non-required resources
            totalCount = sharedResources.Count;
            for (int index = 0; index < totalCount; index++)
            {
                resource = sharedResources[index];

                //Add the resource to the dictionary if needed.
                if (sharedResourceTally.ContainsKey(resource.resourceName) == false)
                {
                    talliedResource = new TalliedResource();
                    sharedResourceTally.Add(resource.resourceName, talliedResource);
                    Log("[WBIDistributionManager] - sharedResourceTally added: " + resource.resourceName);
                }

                //Add the distributor to the tallied resource
                talliedResource = sharedResourceTally[resource.resourceName];
                talliedResource.distributors.Add(distributor);

                //Tally up the current and max amounts
                talliedResource.grandTotal    += resource.amount;
                talliedResource.grandCapacity += resource.maxAmount;
            }
        }
Ejemplo n.º 2
0
        protected void tallyResources(WBIResourceDistributor distributor)
        {
            List <PartResource> sharedResources   = new List <PartResource>();
            List <PartResource> requiredResources = new List <PartResource>();
            TalliedResource     talliedResource;
            PartResource        resource;
            int totalCount;

            //Get the list of shared and required resources
            distributor.GetResourcesToDistribute(sharedResources, requiredResources);

            //For each required resource, add the distributor to the required resources map.
            totalCount = requiredResources.Count;
            for (int index = 0; index < totalCount; index++)
            {
                resource = requiredResources[index];

                //Add the resource to the dictionary if needed.
                if (requiredResourceTally.ContainsKey(resource.resourceName) == false)
                {
                    talliedResource = new TalliedResource();
                    requiredResourceTally.Add(resource.resourceName, talliedResource);
                }

                //Add the distributor to the tallied resource.
                talliedResource = requiredResourceTally[resource.resourceName];
                talliedResource.distributors.Add(distributor);
            }

            //Now add all non-required resources
            totalCount = sharedResources.Count;
            for (int index = 0; index < totalCount; index++)
            {
                resource = sharedResources[index];

                //Add the resource to the dictionary if needed.
                if (sharedResourceTally.ContainsKey(resource.resourceName) == false)
                {
                    talliedResource = new TalliedResource();
                    sharedResourceTally.Add(resource.resourceName, talliedResource);
                }

                //Add the distributor to the tallied resource
                talliedResource = sharedResourceTally[resource.resourceName];
                talliedResource.distributors.Add(distributor);

                //Tally up the current and max amounts
                talliedResource.grandTotal    += resource.amount;
                talliedResource.grandCapacity += resource.maxAmount;
            }
        }
Ejemplo n.º 3
0
        protected void recyclePart(Part doomed)
        {
            //Get the total units of the recycle resource
            double totalRecycleUnits = (doomed.mass / def.density) * recyclePercent;

            //Shut off any resource distribution
            WBIResourceDistributor distributor = doomed.FindModuleImplementing <WBIResourceDistributor>();

            if (distributor != null)
            {
                distributor.isParticipating = false;
                distributor.distribution    = EDistributionModes.DistributionModeOff;
            }

            //Find any resources that the part has.
            List <DistributedResource> scrappedResources = new List <DistributedResource>();

            PartResource[] doomedResources = doomed.Resources.ToArray();
            string         recycleMessage;

            for (int index = 0; index < doomedResources.Length; index++)
            {
                if (doomedResources[index].amount > 0.0001f)
                {
                    recycleMessage = string.Format(WBIPartScrapper.kResourceRecycled, doomedResources[index].amount, doomedResources[index].resourceName);
                    scrappedResources.Add(new DistributedResource(doomedResources[index].resourceName, doomedResources[index].amount, recycleMessage));
                }
            }

            //Now distribute the recycled resources
            recycleMessage = string.Format(WBIPartScrapper.kResourceRecycled, totalRecycleUnits, recycleResource);
            scrappedResources.Add(new DistributedResource(recycleResource, totalRecycleUnits, recycleMessage));
            int    totalResources      = scrappedResources.Count;
            double amountRecycled      = 0;
            double totalAmountRecycled = 0f;
            DistributedResource scrappedResource;
            int    totalVessels = FlightGlobals.VesselsLoaded.Count;
            Vessel currentVessel;

            for (int index = 0; index < totalResources; index++)
            {
                //Setup
                totalAmountRecycled = 0;
                scrappedResource    = scrappedResources[index];

                //Skip ElectricCharge
                if (scrappedResource.resourceName == "ElectricCharge")
                {
                    continue;
                }

                //Priority goes to the vessel that owns the recycler arm.
                currentVessel            = this.part.vessel;
                amountRecycled           = currentVessel.rootPart.RequestResource(scrappedResource.resourceName, -scrappedResource.amount);
                scrappedResource.amount += amountRecycled;
                Log("Arm owner recycled " + Math.Abs(amountRecycled) + " units of " + scrappedResource.resourceName);
                Log("Units remaining: " + scrappedResource.amount);

                //Zero out the resource if needed.
                if (scrappedResource.amount < 0.001f)
                {
                    scrappedResource.amount = 0f;
                }

                //Record what we recycled.
                totalAmountRecycled += amountRecycled;

                //Now go through every loaded vessel and hand out the resource.
                for (int vesselIndex = 0; vesselIndex < totalVessels; vesselIndex++)
                {
                    //Skip the distribution if we're out of the resource
                    if (scrappedResource.amount == 0f)
                    {
                        break;
                    }

                    //Get the vessel that we'll distribute the resource to.
                    currentVessel = FlightGlobals.VesselsLoaded[vesselIndex];

                    //Skip the arm's vessel
                    if (currentVessel == this.part.vessel)
                    {
                        continue;
                    }

                    //Skip the vessel if it's not in range
                    if ((Vector3.Distance(vessel.GetWorldPos3D(), FlightGlobals.ActiveVessel.GetWorldPos3D()) / WBIDistributionManager.kDefaultDistributionRange) > 1.0f)
                    {
                        Log("Vessel " + currentVessel.vesselName + " not in range");
                        continue;
                    }

                    //Hand out the resource.
                    amountRecycled           = currentVessel.rootPart.RequestResource(scrappedResource.resourceName, -scrappedResource.amount);
                    scrappedResource.amount += amountRecycled;
                    Log("Nearby vessel recycled " + Math.Abs(amountRecycled) + " units of " + scrappedResource.resourceName);
                    Log("Units remaining: " + scrappedResource.amount);

                    //Zero out the resource if needed.
                    if (scrappedResource.amount < 0.001f)
                    {
                        scrappedResource.amount = 0f;
                    }

                    //Record what we recycled.
                    totalAmountRecycled += amountRecycled;
                }

                //Report what we recycled.
                if (!string.IsNullOrEmpty(scrappedResource.message))
                {
                    ScreenMessages.PostScreenMessage(scrappedResource.message, 3.0f, ScreenMessageStyle.UPPER_LEFT);
                }
            }

            //Finally, poof the part.
            ScreenMessages.PostScreenMessage(string.Format(WBIPartScrapper.kPartRecycled, doomed.partInfo.title), WBIPartScrapper.kMessageDuration, ScreenMessageStyle.UPPER_CENTER);
            doomed.explosionPotential = WBIPartScrapper.kExplosionPotential;
            doomed.explode();
        }