Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/Map/Hull.cs
T
Joonas Rikkonen 58a14fd054 178a853...bd9a92d
commit bd9a92df11a8d83d0d0087ce64422f2fba3f6f99
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 20:07:36 2019 +0200

    v0.8.9.2

commit 4e1d5fa3744fb8cab80cb006a433d2efe7685562
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 19:20:03 2019 +0200

    Moved humpback's cargo spawn position a bit to prevent the cargo from falling down the hatch under it.

commit f0ec5530b0c7ecbdaaf8bd266d9a57fac2c92fe9
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 19:18:41 2019 +0200

    Fixed structure BodyOffset not being taken into account when generating the collider for the sub. Caused the button outside Orca's button to be impossible to interact with.

commit 1360f5511522280d03d608c8eb6f56fb6608aab5
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 19:10:37 2019 +0200

    Halved the status effect for damage to submerged items. This might be a bit too generous, let's see.

commit 06afb668bc816a345dbd12899b819843549e908a
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 18:22:00 2019 +0200

    Changed mantis loading tip to mud raptor, fixed a glaring error in one of the npc conversations

commit d2dfa960dd5fa43426eb4dfd02de6c497ae9d838
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 18:17:17 2019 +0200

    Fixed water flow forces occasionally being way too high due to bugged force calculation logic when water is flowing from a hull to the one below it. + Added a hard cap to flow forces to prevent this from happening again. Closes #572

commit 21754b39ded247e8c9837d58c6de9658f56a9772
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 17:24:48 2019 +0200

    Entities cannot be placed in the sub editor when the cursor is on a UI element. Fixes an entity being placed when selecting an item from them menu while something else is already selected. Closes #1023

commit 97d0cf92f738a058fa0140ec841549a5d6c746a3
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 16:59:56 2019 +0200

    Fixed a typo in one of the affliction descriptions

commit ddccbdc9a438199d3cb796789dd9915cc305ec5b
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 16:59:18 2019 +0200

    Fixed sliders buttons becoming invisible when pressed

commit bc28473a1c61698c9447825b1c5a13edd6e7bf6c
Merge: 2d212802e f3dfe5990
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 16:45:33 2019 +0200

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

commit 2d212802ea945c6cbd1c889d304595b68692e05c
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 16:44:30 2019 +0200

    corrected the condition at which railgun and coilgun loaders can be repaired

commit f3dfe5990a03cb3c577635dffb25883085d919f1
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 16:41:34 2019 +0200

    Fixed second submarine overlapping with the outpost at the end of the level in combat missions.

commit 1f224e9b7cae3f94e9b6937d66c171edbd8ef523
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 15:35:22 2019 +0200

    Fixed sub/mode selection setting tickboxes

commit 0eb99486454c1bb47b51b20e4d23f42f4147dd8d
Merge: ff90fdb35 80785d627
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 15:08:18 2019 +0200

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

commit ff90fdb352af6800d0dcf224816cf9e831184f1e
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 15:08:12 2019 +0200

    Don't do chat message similarity tests on order messages (caused spam kicks way too easily when giving orders or reporting things)

commit af8a350c7998ae2c729b4354c3c02cbcf75cabde
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 15:06:42 2019 +0200

    Fixed Huskappendage.xml not being copied to the output directory

commit df3af76d909099e9c9e59f74b1f5b3b381303a99
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 14:06:19 2019 +0200

    Revert "Ignore anything but the content path when checking the filepath cases." Doesn't work correctly if there's a folder named "Content" in some subfolder (e.g. "Mods/SomeMod/Content") or if checking file paths somewhere else than the content folder (Submarines, Data, NewWorkshopItem, Mods...)

    This reverts commit 123e84ea1ac6f4392c293d0d5c64b857bfc4eb30.

commit 80785d627c82948ce1784654070a0da8f732ca33
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 14:43:36 2019 +0200

    added better visual feedback when reactor is in poor condition

commit e450872721d8e0b47b3ae5792292ea2071a8eefe
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 14:40:42 2019 +0200

    added a small stun effect to railgun shells

commit 05792285eab781990bdc3b658a9217ff38704d1e
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Sat Feb 2 14:40:03 2019 +0200

    added a little extra fuel, moved the position of guns to hull to prevent players getting caught under them

commit 8c85c33023b6cec1c18b8743d1f05fd4fc8c2ea7
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sat Feb 2 13:58:55 2019 +0200

    Fixed crashing when attempting to use active sonar in the sub editor. Closes #1024

commit d71225d8c58fb285cbf207549b77cabdc7e1df11
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 18:27:06 2019 +0200

    Fixed crashing when selecting an item in a fabricator that's linked to containers, but not set to display the contents of the containers

commit a6fa5d82c7b1505932b8283ccfef75798b839d92
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 18:07:40 2019 +0200

    Fixed "can't create and entity event for Hull" error messages when attempting to remove a hull with fire sources in it in MP

commit 72737c6d3eb7ca7daf5e5d84fdcbc1f060b79b10
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 18:05:04 2019 +0200

    Filename case fix part 913, fixed rewards mentioned in mission descriptions not matching the actual reward of the mission.

commit a148c7843f2805b1757d231bcf028bafdbc22187
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 17:56:18 2019 +0200

    Attempt to forward steam query port when UPnP is enabled

commit 3e73d705c221a417f60baf76c8c7d100391f7802
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 17:44:34 2019 +0200

    Removed the "training" button from the main menu (not needed, tutorial is now implemented directly in the SP campaign)

commit 1f07c3f1b4de0bb09fe11e9834faeea30e4afade
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 17:33:01 2019 +0200

    Fixed crewmanager UI elements hidden outside the extents of the crew list overlapping with other UI elements

commit aa0c5f4e90b57a8ee3f152b9aeba1e73c7c539c0
Merge: 123e84ea1 73f51a405
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Fri Feb 1 16:21:27 2019 +0200

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

commit 123e84ea1ac6f4392c293d0d5c64b857bfc4eb30
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Fri Feb 1 16:21:19 2019 +0200

    Ignore anything but the content path when checking the filepath cases.

commit 73f51a4055ca7fd1cd65570a51d88db1f1272d23
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Fri Feb 1 15:20:15 2019 +0200

    Don't show the "incorrect case" errors in non-linux release builds. It seems that Path.GetFullPath may return paths with an incorrect case on Windows (try changing the case of any of the game's parent folders to repro).

commit 5a598129a9d63c5185529107d469404c35be59c5
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Fri Feb 1 11:29:41 2019 +0200

    Renamed hull areas... again.
2019-03-18 21:09:32 +02:00

971 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;
}
}
}
while (FireSources.Count > fireSourceCount)
{
FireSources[FireSources.Count - 1].Remove();
}
}
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;
}
}
}