Exemple #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());
                        childs.Add(child);
                    }
                }
            }

            foreach (CivPopKerbal child in childs)
            {
                repo.Add(child);
            }
        }
        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);
        }
        public void not_add_rent_for_1_civilians_on_KSC()
        {
            CivPopRepository repo = new CivPopRepository();

            repo.Add(new CivPopKerbal("Valentina", CivPopKerbalGender.FEMALE, 0, true));

            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(0, repo.GetFunds());

            date += TimeUnit.DAY;
            service.Update(date, repo);
            Assert.AreEqual(0, repo.GetFunds());
        }
        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 #5
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 Update(double date, CivPopRepository repo, bool now = false)
 {
     if (lastUpdate > -1 && GetDay(lastUpdate) < GetDay(date) || now)
     {
         DoUpdate(date, repo);
     }
     lastUpdate = date;
 }
Exemple #7
0
 public void Update(double date, CivPopRepository repo)
 {
     if (lastUpdate > -1 && GetDay(lastUpdate) < GetDay(date))
     {
         DoUpdate(date, repo);
     }
     lastUpdate = date;
 }
Exemple #8
0
 protected override void DoUpdate(double date, CivPopRepository repo)
 {
     foreach (CivPopVessel vessel in repo.GetVessels())
     {
         IEnumerable <CivPopCouple> couples = makeCouples(date, vessel, repo);
         turnPregnantSomeFemales(date, couples, vessel.IsAllowBreeding());
     }
     birthOfNewCivilans(date, repo);
 }
 public void update(double currentDate, CivPopRepository repo)
 {
     if (windowShown)
     {
         this.currentDate = currentDate;
         this.repo        = repo;
         windowPosition   = ClickThruBlocker.GUILayoutWindow(baseWindowID, windowPosition, drawWindow, "Civilian Population GUI");
     }
 }
Exemple #10
0
 public void update(double currentDate, CivPopRepository repo)
 {
     if (windowShown)
     {
         this.currentDate = currentDate;
         this.repo        = repo;
         windowPosition   = GUILayout.Window(0, windowPosition, drawWindow, "Civilian Population");
     }
 }
Exemple #11
0
        protected override void DoUpdate(double date, CivPopRepository repo)
        {
            int count = repo.GetRoster()
                        .Where(kerbal => kerbal.IsCivilian())
                        .Where(kerbal => kerbal.GetVesselId() != null)
                        .Where(kerbal => !kerbal.IsDead())
                        .Count()
            ;

            repo.AddFunds(this.rent * 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 SetUp()
        {
            CivPopKerbalBuilder builder = new CivPopKerbalBuilder(g => Guid.NewGuid().ToString());

            repo = new CivPopRepository();

            vessel = new CivPopVessel("vessel");
            repo.Add(vessel);
            vessel.SetCapacity(4);

            service = new CivPopServiceContractors(builder);
        }
        public void SetUp()
        {
            System.Random rng = new System.Random(42);

            builder = new CivPopKerbalBuilder(g => Guid.NewGuid().ToString(), rng);

            repo   = new CivPopRepository();
            vessel = new CivPopVessel("vessel");
            vessel.SetAllowBreeding(true);
            repo.Add(vessel);

            service = new CivPopServiceGrowth(builder, rng);
        }
        public void FixedUpdate()
        {
#endif
#if DEBUG
                monitor.add("Trash");
#endif
                double now = Planetarium.GetUniversalTime();
#if DEBUG
                monitor.add("GetUniversalTime");
#endif

                CivPopRepository repo = GetRepository();
                UpdateRepository(repo);
#if DEBUG
                monitor.add("UpdateRepository");
#endif
                contractors.Update(now, repo);
#if DEBUG
                monitor.add("contractors");
#endif
                death.Update(now, repo);
#if DEBUG
                monitor.add("death");
#endif
                growth.Update(now, repo);
#if DEBUG
                monitor.add("growth");
#endif

                if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
                {
                    rent.Update(now, repo);
                }
#if DEBUG
                monitor.add("rent");
#endif
                Vessel vessel = FlightGlobals.ActiveVessel;
                if (vessel != null)
                {
                    service.KillKerbals(repo, vessel);
                    service.CreateKerbals(repo, vessel);
                }
#if DEBUG
                monitor.add("Vessels");
#endif

                repoJSON = repo.ToJson();
#if DEBUG
                monitor.add("ToJson");
#endif
            }
        public void CreateKerbals(CivPopRepository repo, Vessel vessel)
        {
            foreach (CivPopKerbal current in repo.GetLivingRosterForVessel(vessel.id.ToString()))
            {
                var crew = vessel.GetVesselCrew().Find(c => c.name.Equals(current.GetName()));
                if (crew == null)
                {
                    var houses = vessel.FindPartModulesImplementing <CivilianPopulationHousingModule>();
                    if (houses.Count > 0)
                    {
                        foreach (var house in houses)
                        {
                            if (house.part.CrewCapacity > house.part.protoModuleCrew.Count)
                            {
                                var kspRoster = HighLogic.CurrentGame.CrewRoster;
                                var newKerbal = kspRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Crew);

                                var gender = ProtoCrewMember.Gender.Male;
                                if (current.GetGender().Equals(CivPopKerbalGender.FEMALE))
                                {
                                    gender = ProtoCrewMember.Gender.Female;
                                }

                                while (newKerbal.gender != gender || newKerbal.trait != "Civilian")
                                {
                                    kspRoster.Remove(newKerbal);
                                    newKerbal = kspRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Crew);
                                }
                                if (!newKerbal.ChangeName(current.GetName()))
                                {
                                    kspRoster.Remove(newKerbal);
                                    continue;
                                }
                                if (house.part.AddCrewmember(newKerbal))
                                {
                                    vessel.SpawnCrew();
                                    Log.Info("CreateKerbals : " + newKerbal.name + " has been placed successfully");
                                    break;
                                }
                            }
                        }
                    }
                }
                crew = vessel.GetVesselCrew().Find(c => c.name.Equals(current.GetName()));
                if (crew == null)
                {
                    current.SetDead(true);
                    Log.Info("CreateKerbals : " + current.GetName() + " died because of a lack of room");
                }
            }
        }
 private CivPopRepository GetRepository()
 {
     monitor.add("Trash");
     if (repository == null)
     {
         repository = new CivPopRepository();
         if (repoJSON != null)
         {
             repository = new CivPopRepository(repoJSON.Replace('[', '{').Replace(']', '}'));
         }
     }
     monitor.add("GetRepository");
     return(repository);
 }
Exemple #18
0
 protected override void DoUpdate(double date, CivPopRepository repo)
 {
     foreach (CivPopKerbal kerbal in repo.GetRoster())
     {
         int age           = (int)((date - kerbal.GetBirthdate()) / TimeUnit.YEAR);
         int chanceOfDeath = GetChanceOfDeath(age);
         if (chanceOfDeath > 0)
         {
             if (rng.Next() % chanceOfDeath == 0)
             {
                 repo.Kill(kerbal);
             }
         }
     }
 }
        internal void DoGrowthUpdateNow()
        {
            var currentDate       = Planetarium.GetUniversalTime();
            CivPopRepository repo = GetRepository();

            foreach (var crew in repo.GetRoster())
            {
                if (crew.GetExpectingBirthAt() <= currentDate)
                {
                    growth.Update(currentDate, repo, true);
#if DEBUG
                    monitor.add("growth");
#endif
                    return;
                }
            }
        }
Exemple #20
0
 protected override void DoUpdate(double date, CivPopRepository repo)
 {
     SimpleLogger.fetch.Info("DoUpdate, repo.Count: " + repo.GetVessels().Count());
     foreach (CivPopVessel vessel in repo.GetVessels())
     {
         if (vessel.KSPVessel == null)
         {
             SimpleLogger.fetch.Info("KSPVessel is null");
         }
         else
         {
             IEnumerable <CivPopCouple> couples = makeCouples(date, vessel, repo);
             turnPregnantSomeFemales(vessel.KSPVessel, date, couples, vessel.IsAllowBreeding());
         }
     }
     birthOfNewCivilans(date, repo);
 }
 public void KillKerbals(CivPopRepository repo, Vessel vessel)
 {
     foreach (var current in repo.GetDeadRosterForVessel(vessel.id.ToString()))
     {
         foreach (var part in vessel.parts)
         {
             foreach (var crew in part.protoModuleCrew)
             {
                 if (crew.name.Equals(current.GetName()))
                 {
                     part.RemoveCrewmember(crew);
                     vessel.RemoveCrew(crew);
                     crew.Die();
                 }
             }
         }
         repo.Remove(current);
     }
 }
        public void KillKerbals(CivPopRepository repo, Vessel vessel)
        {
            var deadList = repo.GetDeadRosterForVessel(vessel.id.ToString());

            for (int i = deadList.Count - 1; i > 0; i--)
            {
                var current = deadList[i];
                for (int p = vessel.parts.Count - 1; p > 0; p--)
                {
                    var part = vessel.parts[p];
                    for (int c = part.protoModuleCrew.Count - 1; c > 0; c--)
                    {
                        var crew = part.protoModuleCrew[c];
                        if (crew.name.Equals(current.GetName()))
                        {
                            part.RemoveCrewmember(crew);
                            vessel.RemoveCrew(crew);
                            crew.Die();
                        }
                    }
                }
                repo.Remove(current);
            }

#if false
            foreach (var current in repo.GetDeadRosterForVessel(vessel.id.ToString()))
            {
                foreach (var part in vessel.parts)
                {
                    foreach (var crew in part.protoModuleCrew)
                    {
                        if (crew.name.Equals(current.GetName()))
                        {
                            part.RemoveCrewmember(crew);
                            vessel.RemoveCrew(crew);
                            crew.Die();
                        }
                    }
                }
                repo.Remove(current);
            }
#endif
        }
 public void setRepository(CivPopRepository repo)
 {
     this.repo = repo;
 }
        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);
                }
            }
        }
Exemple #25
0
        public IEnumerable <CivPopCouple> makeCouples(double date, CivPopVessel vessel, CivPopRepository repo)
        {
            IEnumerable <CivPopKerbal> adults
                = repo.GetLivingRosterForVessel(vessel.GetId())
                  .Where(kerbal => kerbal.getAge(date) != CivilianKerbalAge.YOUNG);

            IEnumerable <CivPopKerbal> males
                = adults.Where(kerbal => kerbal.GetGender() == CivPopKerbalGender.MALE);
            IEnumerable <CivPopKerbal> females
                = adults.Where(kerbal => kerbal.GetGender() == CivPopKerbalGender.FEMALE);


            List <CivPopKerbal> availableMales = new List <CivPopKerbal>();

            foreach (CivPopKerbal kerbal in males)
            {
                if (rng.Next() % MALE_AVAILABILITY == 0)
                {
                    availableMales.Add(kerbal);
                }
            }

            List <CivPopKerbal> availableFemales = new List <CivPopKerbal>();

            foreach (CivPopKerbal kerbal in females)
            {
                if (rng.Next() % FEMALE_AVAILABILITY == 0)
                {
                    availableFemales.Add(kerbal);
                }
            }

            List <CivPopCouple> couples = new List <CivPopCouple>();

            foreach (CivPopKerbal male in availableMales)
            {
                if (availableFemales.Count > 0)
                {
                    int          index  = rng.Next() % availableFemales.Count();
                    CivPopKerbal female = availableFemales[index];
                    availableFemales.RemoveAt(index);
                    couples.Add(new CivPopCouple(male, female));
                }
            }
            return(couples);
        }
Exemple #26
0
 protected abstract void DoUpdate(double date, CivPopRepository repo);