public CivilianPopulationGrowthSimulator()
        {
            this.rng = new System.Random();
            this.idx = 0;

            CivPopKerbalBuilder builder = new CivPopKerbalBuilder(GetName);

            growth = new CivPopServiceGrowth(builder);
            death  = new CivPopServiceDeath();

            repo = new CivPopRepository();

            CivPopVessel vessel = new CivPopVessel("vessel");

            vessel.SetAllowBreeding(true);
            vessel.SetCapacity(1000000);
            repo.Add(vessel);

            CivPopKerbal male = new CivPopKerbal(GetName(CivPopKerbalGender.MALE), CivPopKerbalGender.MALE, 0, true);

            male.SetVesselId("vessel");
            repo.Add(male);
            CivPopKerbal female = new CivPopKerbal(GetName(CivPopKerbalGender.FEMALE), CivPopKerbalGender.FEMALE, 0, true);

            female.SetVesselId("vessel");
            repo.Add(female);
        }
Exemple #2
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());
                        childs.Add(child);
                    }
                }
            }

            foreach (CivPopKerbal child in childs)
            {
                repo.Add(child);
            }
        }
        public void add_rent_every_day_for_1_civilian()
        {
            CivPopRepository repo = new CivPopRepository();

            repo.Add(new CivPopVessel("vessel"));

            CivPopKerbal valentina = new CivPopKerbal("Valentina", CivPopKerbalGender.FEMALE, 0, true);

            repo.Add(valentina);
            valentina.SetVesselId("vessel");

            CivPopKerbal bill = new CivPopKerbal("Bill", CivPopKerbalGender.MALE, 0, false);

            repo.Add(bill);
            bill.SetVesselId("vessel");

            CivPopServiceRent service = new CivPopServiceRent();

            double date = 0;

            service.Update(date, repo);
            Assert.AreEqual(0, repo.GetFunds());

            date += 60 * 60;
            service.Update(date, repo);
            Assert.AreEqual(0, repo.GetFunds());

            date += TimeUnit.DAY;
            service.Update(date, repo);
            Assert.AreEqual(200, repo.GetFunds());

            date += TimeUnit.DAY;
            service.Update(date, repo);
            Assert.AreEqual(400, repo.GetFunds());
        }
Exemple #4
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);
            }
        }
        public void create_couples_when_many_males_and_females()
        {
            for (int i = 0; i < 100; i++)
            {
                CivPopKerbal kerbal = builder.build(0);
                kerbal.SetVesselId(vessel.GetId());
                repo.Add(kerbal);
            }
            IEnumerable <CivPopCouple> couples = service.makeCouples(0, vessel, repo);

            Assert.AreEqual(19, couples.Count());
        }
 public void Update(double date, CivPopRepository repo)
 {
     foreach (CivPopVessel vessel in repo.GetVessels())
     {
         if (vessel.IsOrbiting() &&
             vessel.GetBody().getType() != CelestialBodyType.OTHERS &&
             vessel.IsAllowDocking() &&
             vessel.GetCapacity() > repo.GetLivingRosterForVessel(vessel.GetId()).Count()
             )
         {
             if (vessel.GetMissionArrival() < 0)
             {
                 if (vessel.GetBody().getType() != CelestialBodyType.HOMEWORLD)
                 {
                     vessel.SetMissionArrival(date + MISSION_DURATION * 2);
                     vessel.SetMissionType(MOON);
                 }
                 else
                 {
                     vessel.SetMissionArrival(date + MISSION_DURATION);
                     vessel.SetMissionType(HOMEWORLD);
                 }
             }
             else
             {
                 if (HOMEWORLD.Equals(vessel.GetMissionType()) &&
                     !vessel.GetBody().getType().Equals(CelestialBodyType.HOMEWORLD))
                 {
                     CancelMission(vessel);
                 }
                 else if (MOON.Equals(vessel.GetMissionType()) &&
                          !vessel.GetBody().getType().Equals(CelestialBodyType.HOMEWORLD_MOON))
                 {
                     CancelMission(vessel);
                 }
                 else
                 {
                     if (date > vessel.GetMissionArrival())
                     {
                         CivPopKerbal kerbal = builder.build(date);
                         kerbal.SetVesselId(vessel.GetId());
                         repo.Add(kerbal);
                         CancelMission(vessel);
                     }
                 }
             }
         }
         else
         {
             CancelMission(vessel);
         }
     }
 }
        public void create_no_couples_when_no_females()
        {
            for (int i = 0; i < 100; i++)
            {
                CivPopKerbal kerbal = builder.build(0);
                kerbal.SetVesselId(vessel.GetId());
                if (CivPopKerbalGender.MALE.Equals(kerbal.GetGender()))
                {
                    repo.Add(kerbal);
                }
            }
            IEnumerable <CivPopCouple> couples = service.makeCouples(0, vessel, repo);

            Assert.AreEqual(0, couples.Count());
        }
        public void make_female_give_birth_after_pregnancy_duration_no_room_in_vessel()
        {
            vessel.SetCapacity(1);
            CivPopKerbal kerbal = builder.build(0);

            kerbal.SetVesselId(vessel.GetId());
            kerbal.SetExpectingBirthAt(2);
            repo.Add(kerbal);

            service.Update(0, repo);
            service.Update(TimeUnit.DAY, repo);

            Assert.AreEqual(-1, kerbal.GetExpectingBirthAt());
            Assert.AreEqual(1, repo.GetLivingRosterForVessel(vessel.GetId()).Count());
        }
        public void not_turn_pregnant_some_female_when_breeding_not_allowed()
        {
            for (int i = 0; i < 1000; i++)
            {
                CivPopKerbal kerbal = builder.build(0);
                kerbal.SetVesselId(vessel.GetId());
                repo.Add(kerbal);
            }
            IEnumerable <CivPopCouple> couples = service.makeCouples(0, vessel, repo);

            service.turnPregnantSomeFemales(0, couples, false);

            int count = repo.GetLivingRosterForVessel(vessel.GetId())
                        .Where(k => k.GetExpectingBirthAt() > 0)
                        .Count();

            Assert.AreEqual(0, count);
        }
        public void cancel_civilian_delivery_mission_if_docks_are_full()
        {
            vessel.SetOrbiting(true);
            vessel.SetBody(KERBIN);
            vessel.SetMissionType("HOMEWORLD");
            vessel.SetMissionArrival(TimeUnit.DAY * 12);
            vessel.SetAllowDocking(true);

            for (int i = 0; i < 4; i++)
            {
                CivPopKerbal kerbal = new CivPopKerbal("kerbal" + i, CivPopKerbalGender.FEMALE, 0, true);
                repo.Add(kerbal);
                kerbal.SetVesselId(vessel.GetId());
            }

            service.Update(0, repo);

            Assert.AreEqual(-1, repo.GetVessel("vessel").GetMissionArrival());
            Assert.AreEqual(null, repo.GetVessel("vessel").GetMissionType());
        }
        public void launch_a_mission_to_replace_a_dead_crew()
        {
            vessel.SetOrbiting(true);
            vessel.SetBody(KERBIN);
            vessel.SetMissionType(null);
            vessel.SetMissionArrival(-1);
            vessel.SetAllowDocking(true);

            for (int i = 0; i < 4; i++)
            {
                CivPopKerbal kerbal = new CivPopKerbal("kerbal" + i, CivPopKerbalGender.FEMALE, 0, true);
                repo.Add(kerbal);
                kerbal.SetVesselId(vessel.GetId());
                repo.Kill(kerbal);
            }

            service.Update(0, repo);

            Assert.AreEqual(TimeUnit.DAY * 85, repo.GetVessel("vessel").GetMissionArrival());
            Assert.AreEqual("HOMEWORLD", repo.GetVessel("vessel").GetMissionType());
        }
        private void UpdateRepository(CivPopRepository repo)
        {
            if (repo.GetFunds() > 0)
            {
                Funding.Instance.AddFunds(repo.GetFunds(), TransactionReasons.Progression);
                repo.AddFunds(repo.GetFunds() * -1);
            }

            ProtoCrewMember.KerbalType type = ProtoCrewMember.KerbalType.Crew;
            //ProtoCrewMember.KerbalType.Applicant;
            //ProtoCrewMember.KerbalType.Crew;
            //ProtoCrewMember.KerbalType.Tourist;
            //ProtoCrewMember.KerbalType.Unowned;

            ProtoCrewMember.RosterStatus[] statuses =
            {
                ProtoCrewMember.RosterStatus.Assigned,
                ProtoCrewMember.RosterStatus.Available,
                ProtoCrewMember.RosterStatus.Dead,
                ProtoCrewMember.RosterStatus.Missing
            };
            IEnumerable <ProtoCrewMember> kerbals = HighLogic.CurrentGame.CrewRoster.Kerbals(type, statuses);

            foreach (ProtoCrewMember kerbal in kerbals)
            {
                CivPopKerbal civKerbal = repo.GetKerbal(kerbal.name);
                if (civKerbal == null)
                {
                    string             kerbalName = kerbal.name;
                    CivPopKerbalGender gender     = CivPopKerbalGender.FEMALE;
                    if (ProtoCrewMember.Gender.Male.Equals(kerbal.gender))
                    {
                        gender = CivPopKerbalGender.MALE;
                    }
                    double birthdate = Planetarium.GetUniversalTime() - 15 * TimeUnit.YEAR - rng.Next(15 * TimeUnit.YEAR);
                    civKerbal = new CivPopKerbal(kerbalName, gender, birthdate, false);
                }
                bool civilian = "Civilian".Equals(kerbal.trait);
                civKerbal.SetCivilian(civilian);
                if (ProtoCrewMember.RosterStatus.Assigned.Equals(kerbal.rosterStatus))
                {
                    repo.Add(civKerbal);
                }
                else
                {
                    repo.Remove(civKerbal);
                }
            }

            foreach (Vessel vessel in FlightGlobals.Vessels)
            {
                CivPopVessel civVessel;
                if (vessel != null && !repo.VesselExists(vessel.id.ToString()))
                {
                    civVessel = new CivPopVessel(vessel);
                }
                else
                {
                    civVessel = repo.GetVessel(vessel.id.ToString());
                }
                civVessel.SetOrbiting(!vessel.LandedOrSplashed);
                civVessel.SetBody(new Domain.CelestialBody(vessel.mainBody.name, GetBodyType(vessel.mainBody)));

                foreach (VesselModule module in vessel.vesselModules)
                {
                    if (module.GetType() == typeof(CivilianPopulationVesselModule))
                    {
                        CivilianPopulationVesselModule civModule = (CivilianPopulationVesselModule)module;
                        civVessel.SetCapacity(civModule.capacity);
                        civVessel.SetAllowDocking(civModule.allowDocking);
                        civVessel.SetAllowBreeding(civModule.allowBreeding);
                    }
                }

                foreach (ProtoCrewMember kerbal in vessel.GetVesselCrew())
                {
                    CivPopKerbal civKerbal = repo.GetKerbal(kerbal.name);
                    if (civKerbal != null)
                    {
                        civKerbal.SetVesselId(vessel.id.ToString());
                    }
                }
                repo.Add(civVessel);
            }

            foreach (CivPopVessel civVessel in repo.GetVessels())
            {
                bool found = false;
                foreach (Vessel vessel in FlightGlobals.Vessels)
                {
                    if (vessel != null && vessel.id.ToString().Equals(civVessel.GetId()))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    repo.Remove(civVessel);
                }
            }
        }