Пример #1
0
 public PayloadAssembly(PayloadAssembly init = null)
 {
     if (init != null)
     {
         this.mass                 = init.mass;
         this.value                = init.value;
         this.partCount            = init.partCount;
         this.containsInvalidParts = init.containsInvalidParts;
     }
 }
Пример #2
0
 public PayloadAssembly(PayloadAssembly init = null)
 {
     this.parts = new List <Part>();
     if (init != null)
     {
         this.mass                 = init.mass;
         this.value                = init.value;
         this.partCount            = init.partCount;
         this.containsInvalidParts = init.containsInvalidParts;
         foreach (Part part in init.parts)
         {
             this.parts.Add(part);
         }
     }
 }
Пример #3
0
        // Recursively finds all detachable assemblies, which are attached as children to the given part and
        // stores them in "payloadAssemblies":
        // TODO: Maybe we should look at the direction dockung-ports and decouples are facing. If they stay on the ship, thes should not count as payload, just as seperators should not get counted as well.
        protected PayloadAssembly FindPayloadAssemblies(FlightRecording recording, Part part, Part parent, List <string> crewedPartIds)
        {
            PayloadAssembly assembly = new PayloadAssembly();

            // Iterate through all attached parts:
            List <Part> attachedParts = new List <Part>(part.children);

            if (part.parent != null)
            {
                attachedParts.Add(part.parent);
            }
            foreach (Part attachedPart in attachedParts)
            {
                if (parent != null && attachedPart.flightID == parent.flightID)
                {
                    continue;                                                             // Ignore the part we came from in the iteration.
                }
                PayloadAssembly subassembly = this.FindPayloadAssemblies(recording, attachedPart, part, crewedPartIds);
                if (subassembly == null)
                {
                    continue;
                }
                assembly.mass      += subassembly.mass;
                assembly.partCount += subassembly.partCount;
                assembly.parts.AddRange(subassembly.parts);
                assembly.value += subassembly.value;
                if (subassembly.containsInvalidParts)
                {
                    assembly.containsInvalidParts = true;
                }
            }

            if (assembly.partCount > 0 && (
                    part.FindModulesImplementing <ModuleDockingNode>().Count() > 0 ||
                    part.FindModulesImplementing <ModuleDecouple>().Count() > 0 ||
                    part.FindModulesImplementing <ModuleAnchoredDecoupler>().Count() > 0
                    ))
            {
                // This is a seperator/dockingport, add all children as valid, detachable subassembly (excluding the seperator, providing the subassembly is actually valid):
                if (!assembly.containsInvalidParts)
                {
                    // Create a copy of the assembly, which will alow us to use the original object for assemblies higher up the recursion-chain:
                    PayloadAssembly payloadAssembly = new PayloadAssembly(assembly);

                    AvailablePart availablePart = null;
                    if (KSTS.partDictionary.TryGetValue(part.name.ToString(), out availablePart))
                    {
                        payloadAssembly.name = availablePart.title.ToString();
                    }
                    else
                    {
                        payloadAssembly.name = part.name.ToString();
                    }
                    payloadAssembly.detachmentPart = part;
                    payloadAssembly.id             = part.flightID.ToString();
                    if (!this.payloadAssemblies.ContainsKey(payloadAssembly.id))
                    {
                        this.payloadAssemblies.Add(payloadAssembly.id, payloadAssembly);
                    }
                }
                else
                {
                    assembly = new PayloadAssembly();
                }
            }

            // Check if this part was active at some point during the flight, making this an invalid subassembly:
            if (recording.usedPartIds.Contains(part.flightID.ToString()))
            {
                assembly.containsInvalidParts = true;
            }

            // Check if the part has a crewmember and thous can not be used as payload:
            if (crewedPartIds.Contains(part.flightID.ToString()))
            {
                assembly.containsInvalidParts = true;
            }

            // Determine the cost of the current part:
            double partCost = 0;
            string partName = SanitizeParteName(part.name);

            if (KSTS.partDictionary.ContainsKey(partName))
            {
                partCost += KSTS.partDictionary[partName].cost; // Includes resource-costs
                foreach (PartResource resource in part.Resources)
                {
                    // Determine the real value of the part with the current amount of resources inside:
                    if (!KSTS.resourceDictionary.ContainsKey(resource.resourceName))
                    {
                        continue;
                    }
                    partCost -= KSTS.resourceDictionary[resource.resourceName].unitCost * resource.maxAmount;
                    partCost += KSTS.resourceDictionary[resource.resourceName].unitCost * resource.amount;
                }
            }

            // Add this part's mass for subassemblies higher up the recursion:
            assembly.mass      += part.mass + part.resourceMass;
            assembly.partCount += 1;
            assembly.parts.Add(part);
            assembly.value += partCost;
            return(assembly);
        }