void UpdateRoomToRoom(float deltaTime) { if (linkedTo.Count < 2) { return; } Hull hull1 = (Hull)linkedTo[0]; Hull hull2 = (Hull)linkedTo[1]; Vector2 subOffset = Vector2.Zero; if (hull1.Submarine != Submarine) { subOffset = Submarine.Position - hull1.Submarine.Position; } else if (hull2.Submarine != Submarine) { subOffset = hull2.Submarine.Position - Submarine.Position; } if (hull1.WaterVolume <= 0.0 && hull2.WaterVolume <= 0.0) { return; } float size = IsHorizontal ? rect.Height : rect.Width; //a variable affecting the water flow through the gap //the larger the gap is, the faster the water flows float sizeModifier = size / 100.0f * open; //horizontal gap (such as a regular door) if (IsHorizontal) { higherSurface = Math.Max(hull1.Surface, hull2.Surface + subOffset.Y); float delta = 0.0f; //water level is above the lower boundary of the gap if (Math.Max(hull1.Surface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.Surface + subOffset.Y + hull2.WaveY[0]) > rect.Y - size) { int dir = (hull1.Pressure > hull2.Pressure + subOffset.Y) ? 1 : -1; //water flowing from the righthand room to the lefthand room if (dir == -1) { if (!(hull2.WaterVolume > 0.0f)) { return; } lowerSurface = hull1.Surface - hull1.WaveY[hull1.WaveY.Length - 1]; //delta = Math.Min((room2.water.pressure - room1.water.pressure) * sizeModifier, Math.Min(room2.water.Volume, room2.Volume)); //delta = Math.Min(delta, room1.Volume - room1.water.Volume + Water.MaxCompress); flowTargetHull = hull1; //make sure not to move more than what the room contains delta = Math.Min(((hull2.Pressure + subOffset.Y) - hull1.Pressure) * 5.0f * sizeModifier, Math.Min(hull2.WaterVolume, hull2.Volume)); //make sure not to place more water to the target room than it can hold delta = Math.Min(delta, hull1.Volume + Hull.MaxCompress - (hull1.WaterVolume)); hull1.WaterVolume += delta; hull2.WaterVolume -= delta; if (hull1.WaterVolume > hull1.Volume) { hull1.Pressure = Math.Max(hull1.Pressure, (hull1.Pressure + hull2.Pressure + subOffset.Y) / 2); } flowForce = new Vector2(-delta, 0.0f); } else if (dir == 1) { if (!(hull1.WaterVolume > 0.0f)) { return; } lowerSurface = hull2.Surface - hull2.WaveY[hull2.WaveY.Length - 1]; flowTargetHull = hull2; //make sure not to move more than what the room contains delta = Math.Min((hull1.Pressure - (hull2.Pressure + subOffset.Y)) * 5.0f * sizeModifier, Math.Min(hull1.WaterVolume, hull1.Volume)); //make sure not to place more water to the target room than it can hold delta = Math.Min(delta, hull2.Volume + Hull.MaxCompress - (hull2.WaterVolume)); hull1.WaterVolume -= delta; hull2.WaterVolume += delta; if (hull2.WaterVolume > hull2.Volume) { hull2.Pressure = Math.Max(hull2.Pressure, ((hull1.Pressure - subOffset.Y) + hull2.Pressure) / 2); } flowForce = new Vector2(delta, 0.0f); } if (delta > 100.0f && subOffset == Vector2.Zero) { float avg = (hull1.Surface + hull2.Surface) / 2.0f; if (hull1.WaterVolume < hull1.Volume - Hull.MaxCompress && hull1.Surface + hull1.WaveY[hull1.WaveY.Length - 1] < rect.Y) { hull1.WaveVel[hull1.WaveY.Length - 1] = (avg - (hull1.Surface + hull1.WaveY[hull1.WaveY.Length - 1])) * 0.1f; hull1.WaveVel[hull1.WaveY.Length - 2] = hull1.WaveVel[hull1.WaveY.Length - 1]; } if (hull2.WaterVolume < hull2.Volume - Hull.MaxCompress && hull2.Surface + hull2.WaveY[0] < rect.Y) { hull2.WaveVel[0] = (avg - (hull2.Surface + hull2.WaveY[0])) * 0.1f; hull2.WaveVel[1] = hull2.WaveVel[0]; } } } } else { //lower room is full of water if (hull2.Pressure + subOffset.Y > hull1.Pressure) { float delta = Math.Min(hull2.WaterVolume - hull2.Volume + Hull.MaxCompress, deltaTime * 8000.0f * sizeModifier); //make sure not to place more water to the target room than it can hold if (hull1.WaterVolume + delta > hull1.Volume + Hull.MaxCompress) { delta -= (hull1.WaterVolume + delta) - (hull1.Volume + Hull.MaxCompress); } delta = Math.Max(delta, 0.0f); hull1.WaterVolume += delta; hull2.WaterVolume -= delta; flowForce = new Vector2( 0.0f, Math.Min(Math.Min((hull2.Pressure + subOffset.Y) - hull1.Pressure, 200.0f), delta)); flowTargetHull = hull1; if (hull1.WaterVolume > hull1.Volume) { hull1.Pressure = Math.Max(hull1.Pressure, (hull1.Pressure + (hull2.Pressure + subOffset.Y)) / 2); } } //there's water in the upper room, drop to lower else if (hull1.WaterVolume > 0) { flowTargetHull = hull2; //make sure the amount of water moved isn't more than what the room contains float delta = Math.Min(hull1.WaterVolume, deltaTime * 25000f * sizeModifier); //make sure not to place more water to the target room than it can hold if (hull2.WaterVolume + delta > hull2.Volume + Hull.MaxCompress) { delta -= (hull2.WaterVolume + delta) - (hull2.Volume + Hull.MaxCompress); } hull1.WaterVolume -= delta; hull2.WaterVolume += delta; flowForce = new Vector2( hull1.WaveY[hull1.GetWaveIndex(rect.X)] - hull1.WaveY[hull1.GetWaveIndex(rect.Right)], Math.Max(Math.Max((hull2.Pressure + subOffset.Y - hull1.Pressure) * 10.0f, -200.0f), -delta)); if (hull2.WaterVolume > hull2.Volume) { hull2.Pressure = Math.Max(hull2.Pressure, ((hull1.Pressure - subOffset.Y) + hull2.Pressure) / 2); } } } if (open > 0.0f) { if (hull1.WaterVolume > hull1.Volume - Hull.MaxCompress && hull2.WaterVolume > hull2.Volume - Hull.MaxCompress) { float avgLethality = (hull1.LethalPressure + hull2.LethalPressure) / 2.0f; hull1.LethalPressure = avgLethality; hull2.LethalPressure = avgLethality; } else { hull1.LethalPressure = 0.0f; hull2.LethalPressure = 0.0f; } } }