Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/Map/Hull.cs
T
Joonas Rikkonen d4c8fd5649 cd50479...1ec6577
commit 1ec6577ad95e1d7ebc8cbfa261c9893c55ea987c
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 17:10:31 2019 +0200

    Don't attempt to move character's hands in UpdateUseItem if the character is disabled. May be causing "attempted to move pulljoint extremely far" errors (see #895)

commit 1aabc5110ce8e0ec208adce6380cbafd255b576f
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 16:47:50 2019 +0200

    removed syringes from the fabricator

commit 0a80a2d8029275f6cffdae580d1ba86cd44dd1a2
Merge: 039c04818 686d4c482
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 16:43:55 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 039c04818e3598f2e4cdef4065434000a48bb229
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 16:43:51 2019 +0200

    Updated: Alien artifacts to spawn inside containers tagged artifactholders in salvage missions & artifact events

commit 686d4c482f692bcc31eb37aeaea0d5074a537fe9
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:42:11 2019 +0200

    Fixed a bunch more buttons/sliders being invisible while pressed

commit 823728511547f14a3dcb4fcf5025db592ea3dac9
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:13:36 2019 +0200

    Reset the unsaved changes when exitting the character editor (Not applied when the switching between the game view and the character editor).

commit 9ecc56db4e73dd2ccd577130014020a1a502ae15
Merge: efbeda895 b824fc3f2
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:04:57 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit efbeda895fceecb8b4701f23ae5a51057aa44efb
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:03:54 2019 +0200

    Fix crash on trying to undo limb/joint deletion. Undo does not currently work on these cases, but the ragdoll can be reset to the original state from the reset button.

commit b824fc3f281aed76e8e0ed78ffb16d2213d2fda1
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 16:03:03 2019 +0200

    removed syringe from medical doctor default kit

commit c40c716962c7ae1a16d791b0fbd744627a2283c5
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:01:33 2019 +0200

    Nerfed door welding (takes 4x longer to weld a door shut now)

commit 7a27fcab1cec322fe75bcfad553aa42a37ec7d53
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:01:09 2019 +0200

    Fixed StatusEffects not being able to target item components. Caused doors to be impossible to weld and most likely other issues with item StatusEffects as well. Closes #1053

commit 50c09ad85ee44018418f537827c009892f4feb23
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:52:38 2019 +0200

    Fixed depth charges disappearing from loaders when interacting them with both hand slots full. Closes #1046

commit 648a5ba889623f776f184fb30b99133a8551234a
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:47:16 2019 +0200

    Allow to delete the main limb in the character editor. Should not crash anymore.

commit 751bda4d29b81c58c1b1df1653158543a3b7710d
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:46:53 2019 +0200

    Add a bunch of null checks to prevent crashes when the main limb is null.

commit 05c068f7aa35f7070e2cf19c526eff0c8d196b53
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:35:20 2019 +0200

    Syringes removed from medical.xml due to being legacy items we should no longer support.

commit 74389aa95e29fc7415de48230c8f12fd4a0a6847
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:30:13 2019 +0200

    Attempt to fix LightSource removal causing nullref exceptions.

commit 0907516bcac599b5ae20dc8e35f3a66adb8704b5
Merge: 659e1c58d f042994f4
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 15:25:07 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 659e1c58d3fb03d21acf2bb6065d07d1ac2ab914
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 15:24:59 2019 +0200

    Added: Yes to All / No to All to the prompts when deleting multiple containers with contents. Fixed: Aforementioned prompts staying active when quitting the submarine editor. Moved: Logic for the prompts from ItemContainer.cs to SubEditorScreen.cs

commit f042994f4b7eeff8cf5a6a68715c7c6aa6b8ddfb
Merge: 70a968a16 5a2ca22bd
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:08:15 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 70a968a165c383c2ee765164b4507aa6072e0504
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:08:01 2019 +0200

    Use inventory icons in the extra cargo menu, layout fixes

commit 5a2ca22bde4be43f4577d20ae4e0a1ec20a23497
Merge: b2f89ea59 9ca7440be
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:06:11 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit b2f89ea59d481c109046d1edbd2807a516502b0c
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:05:39 2019 +0200

    redesigned walls in junction compartment of Typhon, closing #1042

commit 9ca7440be41a4fc2082b73618e90312af57c0817
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:05:35 2019 +0200

    Add some null checks to prevent crashing if the xml document or the source element is not found.

commit 6d1331e9b86a5583761bc374c5dfb42868b03f45
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:03:53 2019 +0200

    Fixed the layout of the extra cargo menu in server settings. Closes #1038

commit 02ac14076ace0418a5d35df7a574f99811d56017
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 14:10:34 2019 +0200

    Fixed fire sounds persisting in menus. Closes #1047

commit d7d4516ddc9e21a2dba8f7bfd14a61ba7a805d4a
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 14:06:03 2019 +0200

    Added a null check to CampaignUI to help diagnose #1029

commit 0fee58808c261cecd4a602db22e39dfb855ee018
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:55:50 2019 +0200

    Fixed "attempting to remove an already removed item" console errors when ending a round.

commit 5e4448024ce00231e9e479aa238ed4b134b07321
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:53:59 2019 +0200

    Fixed crashing when ending a round while a character is outside the sub. Closes #1040

commit e3ff050561a8f823fc8abfddfec9d22ad4dc14a5
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:35:28 2019 +0200

    Fixed battery positioning in charging docks. Closes #1041

commit d383aec6cb7f4525906fa858375729d4ad4fc0d4
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:26:00 2019 +0200

    Fixed debugconsole adding the file number after the extension ("file123.txt (2)" instead of "file123 (2).txt"). Closes #1043

commit fee18b7ebcaea949c3f7d0c8027ab6253769b49c
Merge: d03807237 c06e8990f
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 12:10:12 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit d038072372ab1993a44542298fe4031db6448ae4
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 12:10:04 2019 +0200

    Fixed: Setting an item's condition to zero in the SubmarineEditor causing its OnBroken status effects to be fired

commit c06e8990f97af7ae9b4788b281bf2801cdd8e2ff
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 11:43:35 2019 +0200

    Fixed chatbox being deselected in the net lobby when receiving a lobby update from the server. Closes #1019

commit 2962647c37cec01fdb01092b94c25063c74c9ee0
Merge: b77bc3654 c89df8750
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:41:24 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit b77bc365470c8cfb86dc83e4b21e5b36e0e28918
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:41:08 2019 +0200

    Warning about automatic hull generation not working correctly with sloped walls when using the autohull command

commit 85ef20a48532e06ef0c2774aa81e1fb517814b14
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:40:20 2019 +0200

    Log the name of the item in the "attempting to remove an already removed item" error

commit b6e32d4009a898db9c97316bcb6236170a7a92fe
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:39:46 2019 +0200

    Fixed sliders not moving in the PowerContainer interface when an AI character is operating it

commit 4b4848b5caa7bf3893e4ae2f5afef3c980f60e46
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:39:09 2019 +0200

    Nullref fix in AIObjectiveGoto

commit c89df87500c9026b3a6e75139391e64a4b1073e7
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 21:02:59 2019 -0300

    whoops

commit a85cc7705ea7611fff02fff7c18bc9826f03029a
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 21:01:51 2019 -0300

    ReduceAffliction loops back around if there's a surplus on index 0

commit e0047df3e089fd8be1646aecbd53b86b69949b13
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 19:45:52 2019 -0300

    Fixed ReduceAffliction freezing

    Thanks to nilanthanimosus for catching this
2019-03-18 21:11:46 +02:00

975 lines
34 KiB
C#

using Barotrauma.Networking;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
partial class Hull : MapEntity, ISerializableEntity, IServerSerializable
{
const float NetworkUpdateInterval = 0.5f;
public static List<Hull> hullList = new List<Hull>();
public static List<EntityGrid> EntityGrids { get; } = new List<EntityGrid>();
public static bool ShowHulls = true;
public static bool EditWater, EditFire;
public const float OxygenDistributionSpeed = 500.0f;
public const float OxygenDetoriationSpeed = 0.3f;
public const float OxygenConsumptionSpeed = 1000.0f;
public const int WaveWidth = 32;
public static float WaveStiffness = 0.02f;
public static float WaveSpread = 0.05f;
public static float WaveDampening = 0.05f;
//how much excess water the room can contain (= more than the volume of the room)
public const float MaxCompress = 10000f;
public readonly Dictionary<string, SerializableProperty> properties;
public Dictionary<string, SerializableProperty> SerializableProperties
{
get { return properties; }
}
private float lethalPressure;
private float surface, drawSurface;
private float waterVolume;
private float pressure;
private float oxygen;
private bool update;
public bool Visible = true;
float[] waveY; //displacement from the surface of the water
float[] waveVel; //velocity of the point
float[] leftDelta;
float[] rightDelta;
private float lastSentVolume, lastSentOxygen;
private float sendUpdateTimer;
public List<Gap> ConnectedGaps;
public override string Name
{
get
{
return "Hull";
}
}
[Editable, Serialize("", true)]
public string RoomName
{
get;
set;
}
public override Rectangle Rect
{
get
{
return base.Rect;
}
set
{
base.Rect = value;
if (Submarine == null || !Submarine.Loading)
{
Item.UpdateHulls();
Gap.UpdateHulls();
}
surface = drawSurface = rect.Y - rect.Height + WaterVolume / rect.Width;
Pressure = surface;
}
}
public override bool Linkable
{
get { return true; }
}
public float LethalPressure
{
get { return lethalPressure; }
set { lethalPressure = MathHelper.Clamp(value, 0.0f, 100.0f); }
}
public Vector2 Size
{
get { return new Vector2(rect.Width, rect.Height); }
}
public float CeilingHeight
{
get;
private set;
}
public float Surface
{
get { return surface; }
}
public float DrawSurface
{
get { return drawSurface; }
set
{
if (Math.Abs(drawSurface - value) < 0.00001f) return;
drawSurface = MathHelper.Clamp(value, rect.Y - rect.Height, rect.Y);
update = true;
}
}
public float WorldSurface
{
get { return Submarine == null ? surface : surface + Submarine.Position.Y; }
}
public float WaterVolume
{
get { return waterVolume; }
set
{
if (!MathUtils.IsValid(value)) return;
waterVolume = MathHelper.Clamp(value, 0.0f, Volume + MaxCompress);
if (waterVolume < Volume) Pressure = rect.Y - rect.Height + waterVolume / rect.Width;
if (waterVolume > 0.0f) update = true;
}
}
[Serialize(100000.0f, true)]
public float Oxygen
{
get { return oxygen; }
set
{
if (!MathUtils.IsValid(value)) return;
oxygen = MathHelper.Clamp(value, 0.0f, Volume);
}
}
public float OxygenPercentage
{
get { return oxygen / Volume * 100.0f; }
set { Oxygen = (value / 100.0f) * Volume; }
}
public float Volume
{
get { return rect.Width * rect.Height; }
}
public float Pressure
{
get { return pressure; }
set { pressure = value; }
}
public float[] WaveY
{
get { return waveY; }
}
public float[] WaveVel
{
get { return waveVel; }
}
public List<FireSource> FireSources { get; private set; }
public Hull(MapEntityPrefab prefab, Rectangle rectangle)
: this (prefab, rectangle, Submarine.MainSub)
{
}
public Hull(MapEntityPrefab prefab, Rectangle rectangle, Submarine submarine)
: base (prefab, submarine)
{
rect = rectangle;
OxygenPercentage = 100.0f;
FireSources = new List<FireSource>();
properties = SerializableProperty.GetProperties(this);
int arraySize = (int)Math.Ceiling((float)rectangle.Width / WaveWidth + 1);
waveY = new float[arraySize];
waveVel = new float[arraySize];
leftDelta = new float[arraySize];
rightDelta = new float[arraySize];
surface = rect.Y - rect.Height;
aiTarget = new AITarget(this);
hullList.Add(this);
ConnectedGaps = new List<Gap>();
if (submarine == null || !submarine.Loading)
{
Item.UpdateHulls();
Gap.UpdateHulls();
}
WaterVolume = 0.0f;
InsertToList();
}
public static Rectangle GetBorders()
{
if (!hullList.Any()) return Rectangle.Empty;
Rectangle rect = hullList[0].rect;
foreach (Hull hull in hullList)
{
if (hull.Rect.X < rect.X)
{
rect.Width += rect.X - hull.rect.X;
rect.X = hull.rect.X;
}
if (hull.rect.Right > rect.Right) rect.Width = hull.rect.Right - rect.X;
if (hull.rect.Y > rect.Y)
{
rect.Height += hull.rect.Y - rect.Y;
rect.Y = hull.rect.Y;
}
if (hull.rect.Y - hull.rect.Height < rect.Y - rect.Height) rect.Height = rect.Y - (hull.rect.Y - hull.rect.Height);
}
return rect;
}
public override MapEntity Clone()
{
return new Hull(MapEntityPrefab.Find(null, "hull"), rect, Submarine);
}
public static EntityGrid GenerateEntityGrid(Rectangle worldRect)
{
var newGrid = new EntityGrid(worldRect, 200.0f);
EntityGrids.Add(newGrid);
return newGrid;
}
public static EntityGrid GenerateEntityGrid(Submarine submarine)
{
var newGrid = new EntityGrid(submarine, 200.0f);
EntityGrids.Add(newGrid);
foreach (Hull hull in hullList)
{
if (hull.Submarine == submarine) newGrid.InsertEntity(hull);
}
return newGrid;
}
public override void OnMapLoaded()
{
CeilingHeight = Rect.Height;
Body lowerPickedBody = Submarine.PickBody(SimPosition, SimPosition - new Vector2(0.0f, ConvertUnits.ToSimUnits(rect.Height / 2.0f + 0.1f)), null, Physics.CollisionWall);
if (lowerPickedBody != null)
{
Vector2 lowerPickedPos = Submarine.LastPickedPosition;
if (Submarine.PickBody(SimPosition, SimPosition + new Vector2(0.0f, ConvertUnits.ToSimUnits(rect.Height / 2.0f + 0.1f)), null, Physics.CollisionWall) != null)
{
Vector2 upperPickedPos = Submarine.LastPickedPosition;
CeilingHeight = ConvertUnits.ToDisplayUnits(upperPickedPos.Y - lowerPickedPos.Y);
}
}
}
public void AddToGrid(Submarine submarine)
{
foreach (EntityGrid grid in EntityGrids)
{
if (grid.Submarine != submarine) continue;
rect.Location -= MathUtils.ToPoint(submarine.HiddenSubPosition);
grid.InsertEntity(this);
rect.Location += MathUtils.ToPoint(submarine.HiddenSubPosition);
return;
}
}
public int GetWaveIndex(Vector2 position)
{
return GetWaveIndex(position.X);
}
public int GetWaveIndex(float xPos)
{
int index = (int)(xPos - rect.X) / WaveWidth;
index = (int)MathHelper.Clamp(index, 0, waveY.Length - 1);
return index;
}
public override void Move(Vector2 amount)
{
rect.X += (int)amount.X;
rect.Y += (int)amount.Y;
if (Submarine == null || !Submarine.Loading)
{
Item.UpdateHulls();
Gap.UpdateHulls();
}
surface = drawSurface = rect.Y - rect.Height + WaterVolume / rect.Width;
Pressure = surface;
}
public override void ShallowRemove()
{
base.Remove();
hullList.Remove(this);
if (Submarine == null || (!Submarine.Loading && !Submarine.Unloading))
{
Item.UpdateHulls();
Gap.UpdateHulls();
}
List<FireSource> fireSourcesToRemove = new List<FireSource>(FireSources);
foreach (FireSource fireSource in fireSourcesToRemove)
{
fireSource.Remove();
}
FireSources.Clear();
if (EntityGrids != null)
{
foreach (EntityGrid entityGrid in EntityGrids)
{
entityGrid.RemoveEntity(this);
}
}
}
public override void Remove()
{
base.Remove();
hullList.Remove(this);
if (Submarine != null && !Submarine.Loading && !Submarine.Unloading)
{
Item.UpdateHulls();
Gap.UpdateHulls();
}
List<FireSource> fireSourcesToRemove = new List<FireSource>(FireSources);
foreach (FireSource fireSource in fireSourcesToRemove)
{
fireSource.Remove();
}
FireSources.Clear();
if (EntityGrids != null)
{
foreach (EntityGrid entityGrid in EntityGrids)
{
entityGrid.RemoveEntity(this);
}
}
}
public void AddFireSource(FireSource fireSource)
{
FireSources.Add(fireSource);
if (GameMain.Server != null && !IdFreed) GameMain.Server.CreateEntityEvent(this);
}
public override void Update(float deltaTime, Camera cam)
{
UpdateProjSpecific(deltaTime, cam);
Oxygen -= OxygenDetoriationSpeed * deltaTime;
FireSource.UpdateAll(FireSources, deltaTime);
aiTarget.SightRange = Submarine == null ? 0.0f : Math.Max(Submarine.Velocity.Length() * 500.0f, 500.0f);
aiTarget.SoundRange -= deltaTime * 1000.0f;
//update client hulls if the amount of water has changed by >10%
//or if oxygen percentage has changed by 5%
if (Math.Abs(lastSentVolume - waterVolume) > Volume * 0.1f ||
Math.Abs(lastSentOxygen - OxygenPercentage) > 5f)
{
if (GameMain.Server != null && !IdFreed)
{
sendUpdateTimer -= deltaTime;
if (sendUpdateTimer < 0.0f)
{
GameMain.Server.CreateEntityEvent(this);
lastSentVolume = waterVolume;
lastSentOxygen = OxygenPercentage;
sendUpdateTimer = NetworkUpdateInterval;
}
}
}
if (!update)
{
lethalPressure = 0.0f;
return;
}
surface = Math.Max(MathHelper.Lerp(
surface,
rect.Y - rect.Height + WaterVolume / rect.Width,
deltaTime * 10.0f), rect.Y - rect.Height);
//interpolate the position of the rendered surface towards the "target surface"
drawSurface = Math.Max(MathHelper.Lerp(
drawSurface,
rect.Y - rect.Height + WaterVolume / rect.Width,
deltaTime * 10.0f), rect.Y - rect.Height);
for (int i = 0; i < waveY.Length; i++)
{
//apply velocity
waveY[i] = waveY[i] + waveVel[i];
//if the wave attempts to go "through" the top of the hull, make it bounce back
if (surface + waveY[i] > rect.Y)
{
float excess = (surface + waveY[i]) - rect.Y;
waveY[i] -= excess;
waveVel[i] = waveVel[i] * -0.5f;
}
//if the wave attempts to go "through" the bottom of the hull, make it bounce back
else if (surface + waveY[i] < rect.Y - rect.Height)
{
float excess = (surface + waveY[i]) - (rect.Y - rect.Height);
waveY[i] -= excess;
waveVel[i] = waveVel[i] * -0.5f;
}
//acceleration
float a = -WaveStiffness * waveY[i] - waveVel[i] * WaveDampening;
waveVel[i] = waveVel[i] + a;
}
//apply spread (two iterations)
for (int j = 0; j < 2; j++)
{
for (int i = 1; i < waveY.Length - 1; i++)
{
leftDelta[i] = WaveSpread * (waveY[i] - waveY[i - 1]);
waveVel[i - 1] += leftDelta[i];
rightDelta[i] = WaveSpread * (waveY[i] - waveY[i + 1]);
waveVel[i + 1] += rightDelta[i];
}
for (int i = 1; i < waveY.Length - 1; i++)
{
waveY[i - 1] += leftDelta[i];
waveY[i + 1] += rightDelta[i];
}
}
//make waves propagate through horizontal gaps
foreach (Gap gap in ConnectedGaps)
{
if (!gap.IsRoomToRoom || !gap.IsHorizontal || gap.Open <= 0.0f) continue;
if (surface > gap.Rect.Y || surface < gap.Rect.Y - gap.Rect.Height) continue;
Hull hull2 = this == gap.linkedTo[0] as Hull ? (Hull)gap.linkedTo[1] : (Hull)gap.linkedTo[0];
float otherSurfaceY = hull2.surface;
if (otherSurfaceY > gap.Rect.Y || otherSurfaceY < gap.Rect.Y - gap.Rect.Height) continue;
float surfaceDiff = (surface - otherSurfaceY) * gap.Open;
if (this != gap.linkedTo[0] as Hull)
{
//the first hull linked to the gap handles the wave propagation,
//the second just updates the surfaces to the same level
if (surfaceDiff < 32.0f)
{
hull2.waveY[hull2.waveY.Length - 1] = surfaceDiff * 0.5f;
waveY[0] = -surfaceDiff * 0.5f;
}
continue;
}
for (int j = 0; j < 2; j++)
{
int i = waveY.Length - 1;
leftDelta[i] = WaveSpread * (waveY[i] - waveY[i - 1]);
waveVel[i - 1] += leftDelta[i];
rightDelta[i] = WaveSpread * (waveY[i] - hull2.waveY[0] + surfaceDiff);
hull2.waveVel[0] += rightDelta[i];
i = 0;
hull2.leftDelta[i] = WaveSpread * (hull2.waveY[i] - waveY[waveY.Length - 1] - surfaceDiff);
waveVel[waveVel.Length - 1] += hull2.leftDelta[i];
hull2.rightDelta[i] = WaveSpread * (hull2.waveY[i] - hull2.waveY[i + 1]);
hull2.waveVel[i + 1] += hull2.rightDelta[i];
}
if (surfaceDiff < 32.0f)
{
//update surfaces to the same level
hull2.waveY[0] = surfaceDiff * 0.5f;
waveY[waveY.Length - 1] = -surfaceDiff * 0.5f;
}
else
{
hull2.waveY[0] += rightDelta[waveY.Length - 1];
waveY[waveY.Length - 1] += hull2.leftDelta[0];
}
}
if (waterVolume < Volume)
{
LethalPressure -= 10.0f * deltaTime;
if (WaterVolume <= 0.0f)
{
//wait for the surface to be lerped back to bottom and the waves to settle until disabling update
if (drawSurface > rect.Y - rect.Height + 1) return;
for (int i = 1; i < waveY.Length - 1; i++)
{
if (waveY[i] > 0.1f) return;
}
update = false;
}
}
}
partial void UpdateProjSpecific(float deltaTime, Camera cam);
public void ApplyFlowForces(float deltaTime, Item item)
{
foreach (var gap in ConnectedGaps.Where(gap => gap.Open > 0))
{
//var pos = gap.Position - body.Position;
var distance = MathHelper.Max(Vector2.DistanceSquared(item.Position, gap.Position)/1000, 1f);
//pos.Normalize();
item.body.ApplyForce((gap.LerpedFlowForce/distance) * deltaTime);
}
}
public void Extinguish(float deltaTime, float amount, Vector2 position)
{
for (int i = FireSources.Count - 1; i >= 0; i-- )
{
FireSources[i].Extinguish(deltaTime, amount, position);
}
}
public void RemoveFire(FireSource fire)
{
FireSources.Remove(fire);
if (GameMain.Server != null && !Removed && !IdFreed)
{
GameMain.Server.CreateEntityEvent(this);
}
}
public IEnumerable<Hull> GetConnectedHulls(int? searchDepth)
{
return GetAdjacentHulls(new HashSet<Hull>(), 0, searchDepth);
}
private HashSet<Hull> GetAdjacentHulls(HashSet<Hull> connectedHulls, int steps, int? searchDepth)
{
connectedHulls.Add(this);
if (searchDepth != null && steps >= searchDepth.Value) return connectedHulls;
foreach (Gap g in ConnectedGaps)
{
for (int i = 0; i < 2 && i < g.linkedTo.Count; i++)
{
if (g.linkedTo[i] is Hull hull && !connectedHulls.Contains(hull))
{
hull.GetAdjacentHulls(connectedHulls, steps++, searchDepth);
}
}
}
return connectedHulls;
}
/// <summary>
/// Approximate distance from this hull to the target hull, moving through open gaps without passing through walls.
/// Uses a greedy algo and may not use the most optimal path. Returns float.MaxValue if no path is found.
/// </summary>
public float GetApproximateDistance(Hull target, float maxDistance)
{
return GetApproximateHullDistance(new HashSet<Hull>(), target, 0.0f, maxDistance);
}
private float GetApproximateHullDistance(HashSet<Hull> connectedHulls, Hull target, float distance, float maxDistance)
{
if (distance >= maxDistance) return float.MaxValue;
if (this == target) return distance;
connectedHulls.Add(this);
foreach (Gap g in ConnectedGaps)
{
if (g.ConnectedDoor != null)
{
//gap blocked if the door is not open or the predicted state is not open
if (!g.ConnectedDoor.IsOpen || (g.ConnectedDoor.PredictedState.HasValue && !g.ConnectedDoor.PredictedState.Value))
{
if (g.ConnectedDoor.OpenState < 0.1f) continue;
}
}
else if (g.Open <= 0.0f)
{
continue;
}
for (int i = 0; i < 2 && i < g.linkedTo.Count; i++)
{
if (g.linkedTo[i] is Hull hull && !connectedHulls.Contains(hull))
{
float dist = hull.GetApproximateHullDistance(connectedHulls, target, distance + Vector2.Distance(g.Position, this.Position), maxDistance);
if (dist < float.MaxValue) return dist;
}
}
}
return float.MaxValue;
}
//returns the water block which contains the point (or null if it isn't inside any)
public static Hull FindHull(Vector2 position, Hull guess = null, bool useWorldCoordinates = true, bool inclusive = true)
{
if (EntityGrids == null) return null;
if (guess != null)
{
if (Submarine.RectContains(useWorldCoordinates ? guess.WorldRect : guess.rect, position, inclusive)) return guess;
}
foreach (EntityGrid entityGrid in EntityGrids)
{
if (entityGrid.Submarine != null && !entityGrid.Submarine.Loading)
{
System.Diagnostics.Debug.Assert(!entityGrid.Submarine.Removed);
Rectangle borders = entityGrid.Submarine.Borders;
if (useWorldCoordinates)
{
Vector2 worldPos = entityGrid.Submarine.WorldPosition;
borders.Location += new Point((int)worldPos.X, (int)worldPos.Y);
}
else
{
borders.Location += new Point((int)entityGrid.Submarine.HiddenSubPosition.X, (int)entityGrid.Submarine.HiddenSubPosition.Y);
}
const float padding = 128.0f;
if (position.X < borders.X - padding || position.X > borders.Right + padding ||
position.Y > borders.Y + padding || position.Y < borders.Y - borders.Height - padding)
{
continue;
}
}
Vector2 transformedPosition = position;
if (useWorldCoordinates && entityGrid.Submarine != null) transformedPosition -= entityGrid.Submarine.Position;
var entities = entityGrid.GetEntities(transformedPosition);
if (entities == null) continue;
foreach (Hull hull in entities)
{
if (Submarine.RectContains(hull.rect, transformedPosition, inclusive)) return hull;
}
}
return null;
}
//returns the water block which contains the point (or null if it isn't inside any)
public static Hull FindHullOld(Vector2 position, Hull guess = null, bool useWorldCoordinates = true, bool inclusive = true)
{
return FindHullOld(position, hullList, guess, useWorldCoordinates, inclusive);
}
public static Hull FindHullOld(Vector2 position, List<Hull> hulls, Hull guess = null, bool useWorldCoordinates = true, bool inclusive = true)
{
if (guess != null && hulls.Contains(guess))
{
if (Submarine.RectContains(useWorldCoordinates ? guess.WorldRect : guess.rect, position, inclusive)) return guess;
}
foreach (Hull hull in hulls)
{
if (Submarine.RectContains(useWorldCoordinates ? hull.WorldRect : hull.rect, position, inclusive)) return hull;
}
return null;
}
public static void DetectItemVisibility(Character c=null)
{
if (c==null)
{
foreach (Item it in Item.ItemList)
{
it.Visible = true;
}
}
else
{
Hull h = c.CurrentHull;
hullList.ForEach(j => j.Visible = false);
List<Hull> visibleHulls;
if (h == null || c.Submarine == null)
{
visibleHulls = hullList.FindAll(j => j.CanSeeOther(null, false));
}
else
{
visibleHulls = hullList.FindAll(j => h.CanSeeOther(j, true));
}
visibleHulls.ForEach(j => j.Visible = true);
foreach (Item it in Item.ItemList)
{
if (it.CurrentHull == null || visibleHulls.Contains(it.CurrentHull)) it.Visible = true;
else it.Visible = false;
}
}
}
private bool CanSeeOther(Hull other, bool allowIndirect = true)
{
if (other == this) return true;
if (other != null && other.Submarine == Submarine)
{
bool retVal = false;
foreach (Gap g in ConnectedGaps)
{
if (g.ConnectedWall != null && g.ConnectedWall.CastShadow) continue;
List<Hull> otherHulls = hullList.FindAll(h => h.ConnectedGaps.Contains(g) && h != this);
retVal = otherHulls.Any(h => h == other);
if (!retVal && allowIndirect) retVal = otherHulls.Any(h => h.CanSeeOther(other, false));
if (retVal) return true;
}
}
else
{
foreach (Gap g in ConnectedGaps)
{
if (g.ConnectedDoor != null && !hullList.Any(h => h.ConnectedGaps.Contains(g) && h != this)) return true;
}
List<MapEntity> structures = mapEntityList.FindAll(me => me is Structure && me.Rect.Intersects(Rect));
return structures.Any(st => !(st as Structure).CastShadow);
}
return false;
}
public string CreateRoomName()
{
List<string> roomItems = new List<string>();
foreach (Item item in Item.ItemList)
{
if (item.CurrentHull != this) continue;
if (item.GetComponent<Items.Components.Reactor>() != null) roomItems.Add("reactor");
if (item.GetComponent<Items.Components.Engine>() != null) roomItems.Add("engine");
if (item.GetComponent<Items.Components.Steering>() != null) roomItems.Add("steering");
if (item.GetComponent<Items.Components.Sonar>() != null) roomItems.Add("sonar");
if (item.HasTag("ballast")) roomItems.Add("ballast");
}
if (roomItems.Contains("reactor"))
return TextManager.Get("ReactorRoom");
else if (roomItems.Contains("engine"))
return TextManager.Get("EngineRoom");
else if (roomItems.Contains("steering") && roomItems.Contains("sonar"))
return TextManager.Get("CommandRoom");
else if (roomItems.Contains("ballast"))
return TextManager.Get("Ballast");
if (ConnectedGaps.Any(g => !g.IsRoomToRoom && g.ConnectedDoor != null))
{
return TextManager.Get("Airlock");
}
Rectangle subRect = Submarine.CalculateDimensions();
Alignment roomPos;
if (rect.Y - rect.Height / 2 > subRect.Y + subRect.Height * 0.66f)
roomPos = Alignment.Top;
else if (rect.Y - rect.Height / 2 > subRect.Y + subRect.Height * 0.33f)
roomPos = Alignment.CenterY;
else
roomPos = Alignment.Bottom;
if (rect.Center.X < subRect.X + subRect.Width * 0.33f)
roomPos |= Alignment.Left;
else if (rect.Center.X < subRect.X + subRect.Width * 0.66f)
roomPos |= Alignment.CenterX;
else
roomPos |= Alignment.Right;
return TextManager.Get("Sub" + roomPos.ToString());
}
public void ServerWrite(NetBuffer message, Client c, object[] extraData = null)
{
message.WriteRangedSingle(MathHelper.Clamp(waterVolume / Volume, 0.0f, 1.5f), 0.0f, 1.5f, 8);
message.WriteRangedSingle(MathHelper.Clamp(OxygenPercentage, 0.0f, 100.0f), 0.0f, 100.0f, 8);
message.Write(FireSources.Count > 0);
if (FireSources.Count > 0)
{
message.WriteRangedInteger(0, 16, Math.Min(FireSources.Count, 16));
for (int i = 0; i < Math.Min(FireSources.Count, 16); i++)
{
var fireSource = FireSources[i];
Vector2 normalizedPos = new Vector2(
(fireSource.Position.X - rect.X) / rect.Width,
(fireSource.Position.Y - (rect.Y - rect.Height)) / rect.Height);
message.WriteRangedSingle(MathHelper.Clamp(normalizedPos.X, 0.0f, 1.0f), 0.0f, 1.0f, 8);
message.WriteRangedSingle(MathHelper.Clamp(normalizedPos.Y, 0.0f, 1.0f), 0.0f, 1.0f, 8);
message.WriteRangedSingle(MathHelper.Clamp(fireSource.Size.X / rect.Width, 0.0f, 1.0f), 0, 1.0f, 8);
}
}
}
public void ClientRead(ServerNetObject type, NetBuffer message, float sendingTime)
{
WaterVolume = message.ReadRangedSingle(0.0f, 1.5f, 8) * Volume;
OxygenPercentage = message.ReadRangedSingle(0.0f, 100.0f, 8);
bool hasFireSources = message.ReadBoolean();
int fireSourceCount = 0;
if (hasFireSources)
{
fireSourceCount = message.ReadRangedInteger(0, 16);
for (int i = 0; i < fireSourceCount; i++)
{
Vector2 pos = Vector2.Zero;
float size = 0.0f;
pos.X = MathHelper.Clamp(message.ReadRangedSingle(0.0f, 1.0f, 8), 0.05f, 0.95f);
pos.Y = MathHelper.Clamp(message.ReadRangedSingle(0.0f, 1.0f, 8), 0.05f, 0.95f);
size = message.ReadRangedSingle(0.0f, 1.0f, 8);
pos = new Vector2(
rect.X + rect.Width * pos.X,
rect.Y - rect.Height + (rect.Height * pos.Y));
size = size * rect.Width;
var newFire = i < FireSources.Count ?
FireSources[i] :
new FireSource(Submarine == null ? pos : pos + Submarine.Position, null, true);
newFire.Position = pos;
newFire.Size = new Vector2(size, newFire.Size.Y);
//ignore if the fire wasn't added to this room (invalid position)?
if (!FireSources.Contains(newFire))
{
newFire.Remove();
continue;
}
}
}
for (int i = FireSources.Count - 1; i >= fireSourceCount; i--)
{
FireSources[i].Remove();
if (i < FireSources.Count)
{
FireSources.RemoveAt(i);
}
}
}
public static Hull Load(XElement element, Submarine submarine)
{
Rectangle rect = Rectangle.Empty;
if (element.Attribute("rect") != null)
{
rect = element.GetAttributeRect("rect", Rectangle.Empty);
}
else
{
//backwards compatibility
rect = new Rectangle(
int.Parse(element.Attribute("x").Value),
int.Parse(element.Attribute("y").Value),
int.Parse(element.Attribute("width").Value),
int.Parse(element.Attribute("height").Value));
}
var hull = new Hull(MapEntityPrefab.Find(null, "hull"), rect, submarine)
{
waterVolume = element.GetAttributeFloat("pressure", 0.0f),
ID = (ushort)int.Parse(element.Attribute("ID").Value)
};
SerializableProperty.DeserializeProperties(hull, element);
if (element.Attribute("oxygen") == null) { hull.Oxygen = hull.Volume; }
return hull;
}
public override XElement Save(XElement parentElement)
{
if (Submarine == null)
{
string errorMsg = "Error - tried to save a hull that's not a part of any submarine.\n" + Environment.StackTrace;
DebugConsole.ThrowError(errorMsg);
GameAnalyticsManager.AddErrorEventOnce("Hull.Save:WorldHull", GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg);
return null;
}
XElement element = new XElement("Hull");
element.Add
(
new XAttribute("ID", ID),
new XAttribute("rect",
(int)(rect.X - Submarine.HiddenSubPosition.X) + "," +
(int)(rect.Y - Submarine.HiddenSubPosition.Y) + "," +
rect.Width + "," + rect.Height),
new XAttribute("water", waterVolume)
);
SerializableProperty.SerializeProperties(this, element);
parentElement.Add(element);
return element;
}
}
}