public override void CompTick()
        {
            base.CompTick();

            if (Find.TickManager.TicksAbs % 50 != 0)
            {
                return;
            }

            var def     = parent.Map.GetSpaceAtmosphereMapComponent().DefinitionAt(parent.Position);
            var calc    = new ShipDefinition.GasCalculator(def);
            var rg      = parent.GetRoomGroup();
            var roomGas = def.GetGas(rg);

            if (parent.Map.IsSpace())
            {
                //dump air into the current environment
                var pressureDif = GasMixture.EarthNorm.totalPressure - roomGas.mixture.totalPressure;
                if (pressureDif > 0.0f)
                {
                    //release enough gas (or whatever's left) to get the gas up to 101.325 kPa.
                    var pressureNeeded = pressureDif * rg.CellCount;

                    if (pressureNeeded > gas.totalPressure)
                    {
                        calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, gas.totalPressure / (float)rg.CellCount), rg.CellCount));
                    }
                    else
                    {
                        calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, pressureDif), rg.CellCount));
                    }
                    gas = gas - pressureNeeded;
                }
            }
            else
            {
                if (gas.totalPressure >= MaxPressure)
                {
                    return;
                }
                //don't steal gas from the ship while in atmo. Deal with this later.
                //calc.GasExchange(rg, removed: new GasVolume(roomGas.mixture, 2f));
                //calc.Execute();

                gas = gas + GasMixture.EarthNorm * 2f;

                if (gas.totalPressure > MaxPressure)
                {
                    gas = GasMixture.atPressure(gas, MaxPressure);
                }
            }
        }
        private void TryRepressurize()
        {
            Map currentMap = parent.Map;

            var def     = currentMap.GetSpaceAtmosphereMapComponent().DefinitionAt(parent.Position);
            var calc    = new ShipDefinition.GasCalculator(def);
            var rg      = parent.GetRoomGroup();
            var roomGas = def.GetGas(rg);

            gas += new GasMixture(0, 1000, 1000);

            var pressureDif = GasMixture.EarthNorm.totalPressure - roomGas.mixture.totalPressure;

            if (pressureDif > 0.0f)
            {
                //release enough gas (or whatever's left) to get the gas up to 101.325 kPa.
                var pressureNeeded = pressureDif * rg.CellCount;

                if (pressureNeeded > gas.totalPressure)
                {
                    calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, gas.totalPressure / (float)rg.CellCount), rg.CellCount));
                }
                else
                {
                    calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, pressureDif), rg.CellCount));
                }
                gas -= pressureNeeded;

                calc.Execute();

                if (gas.totalPressure < 0)
                {
                    gas += GasMixture.atPressure(SpaceConstants.EarthNorm, 0);
                }
            }
        }
        public override void CompTick()
        {
            base.CompTick();

            if (Find.TickManager.TicksAbs % 50 != 0)
            {
                return;
            }

            Map currentMap = parent.Map;

            if (currentMap.IsSpace())
            {
                var def     = currentMap.GetSpaceAtmosphereMapComponent().DefinitionAt(parent.Position);
                var calc    = new ShipDefinition.GasCalculator(def);
                var rg      = parent.GetRoomGroup();
                var roomGas = def.GetGas(rg);

                bool roomHasLifeSupport = !AtmosphereAssistant.IsMixtureToxic(roomGas.mixture);

                var pressureDif = GasMixture.EarthNorm.totalPressure - roomGas.mixture.totalPressure;

                bool roomAtPressure = pressureDif <= 0.0f;

                bool TankEmpty     = gas.totalPressure <= 0;
                bool TankFull      = gas.totalPressure >= MaxPressure;
                bool TankHasOxygen = gas.O2Partial > 0;
                bool TankHasCo2    = gas.Co2Partial > 0;

                if (roomAtPressure)
                {
                    bool recalc = false;


                    if (AtmosphereAssistant.IsMixtureCo2Toxic(roomGas.mixture))
                    {
                        if (!TankFull)
                        {
                            gas += new GasMixture(0, 0, 1);
                            calc.GasExchange(rg, removed: new GasVolume(new GasMixture(0, 0, 1), rg.CellCount));
                            recalc  = true;
                            BarType = 2;
                        }
                    }
                    else
                    {
                        if (BarType == 2)
                        {
                            BarType = 0;
                        }

                        if (TankHasCo2 && roomGas.mixture.Co2Partial < 1) //dump some co2 in for plants etc
                        {
                            gas -= new GasMixture(0, 0, 0.5f);
                            calc.GasExchange(rg, added: new GasVolume(new GasMixture(0, 0, 0.5f), rg.CellCount));
                            recalc  = true;
                            BarType = 2;
                        }
                    }

                    if (AtmosphereAssistant.MixtureHasInertGas(roomGas.mixture))
                    {
                        if (!TankFull)
                        {
                            gas += new GasMixture(1, 0, 0);
                            calc.GasExchange(rg, removed: new GasVolume(new GasMixture(1, 0, 0), rg.CellCount));
                            recalc = true;
                        }
                    }

                    if (recalc)
                    {
                        calc.Execute();
                        pressureDif    = GasMixture.EarthNorm.totalPressure - roomGas.mixture.totalPressure;
                        roomAtPressure = pressureDif <= 0.0f;
                    }

                    if (TankHasOxygen)
                    {
                        if (!roomAtPressure && !AtmosphereAssistant.IsOxygenRich(roomGas.mixture))
                        {
                            gas -= new GasMixture(0, 1, 0);
                            calc.GasExchange(rg, added: new GasVolume(new GasMixture(0, 1, 0), rg.CellCount));
                            recalc = true;

                            if (BarType == 0)
                            {
                                BarType = 1;
                            }
                        }
                        else
                        {
                            if (BarType == 1)
                            {
                                BarType = 0;
                            }
                        }
                    }

                    if (recalc)
                    {
                        calc.Execute();
                    }
                }
                else
                {
                    bool canReleaseGas = gas.totalPressure >= (MaxPressure * 0.005f);

                    if (AtmosphereAssistant.IsMixtureOxygenated(roomGas.mixture, 10f))
                    {
                        if (!TankFull)
                        {
                            float drainRate = 1f;

                            if (roomGas.mixture.O2Partial > 30)
                            {
                                drainRate = 2f;
                            }

                            gas += SpaceConstants.ShipNorm * (2f * drainRate);
                            // gas += new GasMixture(90, 0, SpaceConstants.EarthNorm.Co2Partial);
                            calc.GasExchange(rg, removed: new GasVolume(new GasMixture(0, drainRate, 0), rg.CellCount));
                        }
                    }


                    if (canReleaseGas)
                    {
                        var pressureNeeded = pressureDif * rg.CellCount;

                        if (pressureNeeded > gas.totalPressure)
                        {
                            calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, gas.totalPressure / (float)rg.CellCount), rg.CellCount));
                        }
                        else
                        {
                            calc.GasExchange(rg, added: new GasVolume(GasMixture.atPressure(gas, pressureDif), rg.CellCount));
                        }
                        gas -= pressureNeeded;

                        BarType = 1;

                        calc.Execute();

                        if (gas.totalPressure < 0)
                        {
                            gas += GasMixture.atPressure(SpaceConstants.EarthNorm, 0);
                        }
                    }
                    else
                    {
                        if (BarType != 0)
                        {
                            BarType = 0;
                        }

                        if (gas.totalPressure <= (MaxPressure * 0.005f) && roomGas.mixture.totalPressure < HalfEarthPressure)
                        {
                            gas    += new GasMixture(100, 0, SpaceConstants.EarthNorm.Co2Partial);
                            BarType = 2;
                        }
                        else
                        {
                            if (BarType == 2)
                            {
                                BarType = 0;
                            }
                        }
                    }

                    if (AtmosphereAssistant.IsMixtureCo2Toxic(roomGas.mixture))
                    {
                        if (!TankFull)
                        {
                            gas += new GasMixture(0, 0, 1);
                            calc.GasExchange(rg, removed: new GasVolume(new GasMixture(0, 0, 1), rg.CellCount));
                            calc.Execute();
                            BarType = 3;
                        }
                    }
                    else
                    {
                        if (BarType == 3)
                        {
                            BarType = 0;
                        }
                    }
                }
            }
            else
            {
                if (gas.totalPressure >= MaxPressure)
                {
                    return;
                }
                //don't steal gas from the ship while in atmo. Deal with this later.
                //calc.GasExchange(rg, removed: new GasVolume(roomGas.mixture, 2f));
                //calc.Execute();

                gas += GasMixture.EarthNorm * 2f;

                if (gas.totalPressure > MaxPressure)
                {
                    gas = GasMixture.atPressure(gas, MaxPressure);
                }
            }
        }
Exemple #4
0
        public override void MapComponentTick()
        {
            base.MapComponentTick();

            if (Find.TickManager.TicksAbs % 75 != 0)
            {
                return;
            }

            //move atmosphere around!
            foreach (var def in shipDefinitions)
            {
                var shipRooms           = map.regionGrid.allRooms.Where(x => x.Cells.Any(p => def.Positions.Contains(p)));
                var shipRoomGroups      = shipRooms.Select(x => x.Group).Distinct();
                var vents               = def.Things.OfType <BuildingLifeSupportVent>().ToList();
                var roomGroupsWithVents = vents
                                          .Select(x => x.GetRoom(RegionType.Set_All).Group);

                var ventingGroups = shipRoomGroups.Where(x => x.OpenRoofCount > 0 || x.AnyRoomTouchesMapEdge).ToList();

                var gasCalc = new ShipDefinition.GasCalculator(def);

                foreach (var ventGroup in ventingGroups)
                {
                    gasCalc.Equalize(ventGroup, map.IsSpace() ? GasMixture.Vacuum : GasMixture.EarthNorm);
                }

                foreach (var group in shipRoomGroups)
                {
                    gasCalc.Equalize(group);
                }

                gasCalc.Equalize(roomGroupsWithVents);

                var breathingPawns = map.mapPawns.
                                     PawnsInFaction(Faction.OfPlayer).Where(p => p.def.race.FleshType == FleshTypeDefOf.Normal)
                                     .Select(x => new Pair <Pawn, RoomGroup>(x, x.GetRoomGroup()));

                foreach (var pawn in breathingPawns)
                {
                    //do some calc to decide the amount?
                    gasCalc.GasExchange(
                        rg: pawn.Second,
                        removed: new GasVolume(new GasMixture(0f, 20f, 0f), 0.3f),
                        added: new GasVolume(new GasMixture(0f, 16f, 4f), 0.3f)
                        );
                }

                var plants = def.Things.OfType <Building_PlantGrower>();

                foreach (var plantGrower in plants)
                {
                    //do some calc to decide the amount?
                    gasCalc.GasExchange(
                        rg: plantGrower.GetRoomGroup(),
                        removed: new GasVolume(new GasMixture(0f, 0f, 2f * plantGrower.PlantsOnMe.Count()), 0.3f),
                        added: new GasVolume(new GasMixture(0f, 2f * plantGrower.PlantsOnMe.Count(), 0f), 0.3f)
                        );
                }

                var doorRegion = def.Things.OfType <Building_Door>().ToList()
                                 .Where(x => x.Open || !map.IsSpace())
                                 .Select(x => x.GetRegion(RegionType.Portal))
                                 .Where(x => x != null)
                                 .SelectMany(x => x.links)
                                 .Where(x => x.RegionA.Room.Group != x.RegionB.Room.Group);

                foreach (var reg in doorRegion)
                {
                    gasCalc.Equalize(new RoomGroup[] { reg.RegionA.Room.Group, reg.RegionB.Room.Group });
                }

                gasCalc.Execute();

                foreach (var room in shipRooms)
                {
                    //update stats
                    room.Notify_BedTypeChanged();
                }
            }
        }