Ejemplo n.º 1
0
        public void birthOfNewCivilans(double date, CivPopRepository repo)
        {
            List <CivPopKerbal> childs = new List <CivPopKerbal>();

            IEnumerable <CivPopKerbal> females = repo.GetRoster()
                                                 .Where(kerbal => kerbal.GetExpectingBirthAt() > 0)
                                                 .Where(kerbal => !kerbal.IsDead())
                                                 .Where(kerbal => kerbal.GetExpectingBirthAt() < date)
            ;

            foreach (CivPopKerbal female in females)
            {
                female.SetExpectingBirthAt(-1);
                if (female.GetVesselId() != null)
                {
                    CivPopVessel vessel = repo.GetVessel(female.GetVesselId());
                    if (vessel.GetCapacity() > repo.GetLivingRosterForVessel(vessel.GetId()).Count())
                    {
                        CivPopKerbal child = builder.build(date);
                        child.SetBirthdate(date);
                        child.SetVesselId(female.GetVesselId());

                        ProtoCrewMember pcm = new ProtoCrewMember(ProtoCrewMember.KerbalType.Crew, child.GetName());
                        KerbalRoster.SetExperienceTrait(pcm, "Civilian");//Set the Kerbal as the specified role (kerbalTraitName)
                        var plist = vessel.KSPVessel.parts.FindAll(p => p.CrewCapacity > p.protoModuleCrew.Count);

                        // There may be a better way, but this will look for space in the same part as the female giving birth
                        Part part = null;
                        foreach (var p in plist)
                        {
                            var crew = p.protoModuleCrew.Find(c => c.name == female.GetName());
                            if (crew != null)
                            {
                                part = p;
                                SimpleLogger.fetch.Info("Crew member: " + female.GetName() + ", found on part: " + p.partInfo.title);
                                break;
                            }
                        }
                        // If part is null, no room in same part, so just find a part with room
                        if (part == null)
                        {
                            part = vessel.KSPVessel.parts.Find(p => p.CrewCapacity > p.protoModuleCrew.Count);
                        }
                        if (part != null)
                        {
                            part.AddCrewmember(pcm);
                            vessel.KSPVessel.SpawnCrew();
                            SimpleLogger.fetch.Info("Child added to childs, name: " + child.GetName());
                        }
                        childs.Add(child);
                    }
                }
            }

            foreach (CivPopKerbal child in childs)
            {
                repo.Add(child);
            }
        }