diff --git a/Barotrauma/BarotraumaClient/ClientSource/Characters/CharacterInfo.cs b/Barotrauma/BarotraumaClient/ClientSource/Characters/CharacterInfo.cs index 028b373b2..fafc4aaf9 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Characters/CharacterInfo.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Characters/CharacterInfo.cs @@ -419,7 +419,12 @@ namespace Barotrauma float scale = Math.Min(targetAreaSize.X / headSprite.size.X, targetAreaSize.Y / headSprite.size.Y); headSprite.SourceRect = new Rectangle(CalculateOffset(headSprite, Head.SheetIndex.ToPoint()), headSprite.SourceRect.Size); SetHeadEffect(spriteBatch); - headSprite.Draw(spriteBatch, screenPos, scale: scale, color: Head.SkinColor, spriteEffect: spriteEffects); + Vector2 origin = headSprite.Origin; + if (flip) + { + origin.X = headSprite.size.X - origin.X; + } + headSprite.Draw(spriteBatch, screenPos, origin: origin, scale: scale, color: Head.SkinColor, spriteEffect: spriteEffects); if (AttachmentSprites != null) { float depthStep = 0.000001f; @@ -467,6 +472,14 @@ namespace Barotrauma { origin = head.Origin; attachment.Sprite.Origin = origin; + if (spriteEffects.HasFlag(SpriteEffects.FlipHorizontally)) + { + origin.X = head.size.X - origin.X; + } + if (spriteEffects.HasFlag(SpriteEffects.FlipVertically)) + { + origin.Y = head.size.Y - origin.Y; + } } else { diff --git a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.ShowSoldItems.cs b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.ShowSoldItems.cs new file mode 100644 index 000000000..16550e581 --- /dev/null +++ b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.ShowSoldItems.cs @@ -0,0 +1,174 @@ +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +namespace Barotrauma +{ + internal static partial class DebugConsole + { + private static void InitShowSoldItems() + { + commands.Add(new Command("showsolditems", + "showsolditems [filter (no-defined/only-min/only-max/name:pattern)] [Include stores (true/false)] [Sold only (true/false)] [limit (number)] [Hide store overrides from output. (true/false)]: " + + "Lists items and their shop availability settings. Filter can be availability filter or name pattern (e.g. 'name:*rifle*'). Include stores controls whether to check store-specific overrides (default true). Sold only controls whether to show only sold items (default true). Limit parameter controls how many items to show (default 50). Hide store overrides from output, defaults to false.", + (string[] args) => + { + string filter = args.Length > 0 ? args[0].ToLowerInvariant() : null; + bool includeStores = args.Length <= 1 || !args[1].Equals("false", StringComparison.InvariantCultureIgnoreCase); + bool soldOnly = args.Length <= 2 || !args[2].Equals("false", StringComparison.InvariantCultureIgnoreCase); + int limit = 50; + if (args.Length > 3 && int.TryParse(args[3], out int parsedLimit)) + { + limit = Math.Max(1, parsedLimit); + } + bool hideStoreOverrides = args.Length > 4 && args[4].Equals("true", StringComparison.InvariantCultureIgnoreCase); + + var itemsWithPrice = ItemPrefab.Prefabs + .Where(item => item.ConfigElement.Element.Element("Price") != null); + + // apply filtering + var matchingItems = itemsWithPrice + .OrderBy(i => i.Name.Value) + .Where(item => MatchesFilter(item, filter, includeStores, soldOnly)) + .ToList(); + + // output results + NewMessage("=== Shop Item Availability ===", Color.Cyan); + NewMessage($"Filter: {filter ?? "all"}, IncludeStores: {includeStores}, SoldOnly: {soldOnly}, Limit: {limit}, HideStoreOverrides: {hideStoreOverrides}", Color.Yellow); + NewMessage($"Items: {matchingItems.Count} matching out of {itemsWithPrice.Count()} being sold (showing first {Math.Min(limit, matchingItems.Count)})", Color.LightGreen); + NewMessage("", Color.White); + + foreach (var item in matchingItems.Take(limit)) + { + PrintItemInfo(item, hideStoreOverrides); + } + }, + getValidArgs: () => + [ + ["all", "no-defined", "only-min", "only-max", "name:*"], // filter + ["true", "false"], // includeStores + ["true", "false"], // soldOnly + ["10", "25", "50", "100"], // limit suggestions + ["false", "true"] // hidestoreoverrides + ])); + } + + private static bool MatchesFilter(ItemPrefab item, string filter, bool includeStores, bool soldOnly) + { + var priceElement = item.ConfigElement.Element.Element("Price"); + if (priceElement == null) { return false; } // No price = not sold = don't include + if (!includeStores) { return MatchesPriceElement(priceElement, item, filter, soldOnly); } + + // Check if Base element matches first... + if (MatchesPriceElement(priceElement, item, filter, soldOnly)) { return true; } + + // ...then check store-specific price element matches + foreach (var storeElement in priceElement.Elements().Where(e => e.Name == "Price")) + { + if (MatchesPriceElement(storeElement, item, filter, soldOnly)) { return true; } + } + + return false; + } + + private static bool MatchesPriceElement(XElement priceEl, ItemPrefab itemPrefab, string filter, bool soldOnly) + { + bool isSold = PriceInfo.GetSold(priceEl, true); + if (soldOnly && !isSold) { return false; } + if (filter == null) { return true; } + + // Handle name pattern matching + if (filter.StartsWith("name:")) + { + string pattern = filter[5..]; + string name = itemPrefab.Name.Value.ToLowerInvariant(); + string identifier = itemPrefab.Identifier.Value.ToLowerInvariant(); + + // If pattern contains '*', treat as wildcard (convert to regex) + if (pattern.Contains('*')) + { + // Escape regex special chars except * + string regexPattern = System.Text.RegularExpressions.Regex.Escape(pattern).Replace("\\*", ".*"); + return System.Text.RegularExpressions.Regex.IsMatch(name, $"^{regexPattern}$") + || System.Text.RegularExpressions.Regex.IsMatch(identifier, $"^{regexPattern}$"); + } + else + { + // No wildcards: match exactly + return name == pattern || identifier == pattern; + } + } + + bool hasMin = PriceInfo.HasMinAmountDefined(priceEl); + bool hasMax = PriceInfo.HasMaxAmountDefined(priceEl); + + // Apply the filter logic + return filter switch + { + "no-defined" => !hasMin && !hasMax, // Neither min nor max defined + "only-min" => hasMin && !hasMax, // Only min defined + "only-max" => !hasMin && hasMax, // Only max defined + _ => true // No filter or show all + }; + } + + private static void PrintItemInfo(ItemPrefab item, bool hideStoreOverrides = false) + { + var priceElement = item.ConfigElement.Element.Element("Price"); + if (priceElement == null) { return; } + + bool hasMinDefined = PriceInfo.HasMinAmountDefined(priceElement); + bool hasMaxDefined = PriceInfo.HasMaxAmountDefined(priceElement); + + string minRaw = PriceInfo.GetMinAmountString(priceElement); + string maxRaw = PriceInfo.GetMaxAmountString(priceElement); + int minLevelDifficulty = PriceInfo.GetMinLevelDifficulty(priceElement, 0); + + // Get the resolved values (what PriceInfo would actually use) + var priceInfo = new PriceInfo(priceElement); + int resolvedMin = priceInfo.MinAvailableAmount; + int resolvedMax = priceInfo.MaxAvailableAmount; + + string minStatus = hasMinDefined ? $"XML:{minRaw}" : "DEFAULT:1"; + string maxStatus = hasMaxDefined ? $"XML:{maxRaw}" : "DEFAULT:5"; + + string minLevelInfo = minLevelDifficulty > 0 ? $" | MinLvl: {minLevelDifficulty}" : ""; + NewMessage($"{item.Name} ({item.Identifier}) | Min: {minStatus} → {resolvedMin} | Max: {maxStatus} → {resolvedMax}{minLevelInfo}", Color.White); + + if (hideStoreOverrides) { return; } + + var storeOverrides = priceElement.Elements().Where(e => e.Name == "Price") + .Select(p => { + string storeId = PriceInfo.GetStoreIdentifier(p, "unknown"); + string storeMin = PriceInfo.GetMinAmountString(p); + string storeMax = PriceInfo.GetMaxAmountString(p); + bool? storeSold = PriceInfo.HasSoldDefined(p) ? PriceInfo.GetSold(p, true) : null; + + // Check if this store overrides anything + if (storeMin != null || storeMax != null || storeSold != null) + { + var parts = new List(); + if (storeMin != null || storeMax != null) + { + parts.Add($"min:{storeMin ?? "base"}, max:{storeMax ?? "base"}"); + } + if (storeSold != null) + { + parts.Add($"sold:{storeSold.Value.ToString().ToLowerInvariant()}"); + } + return $"{storeId}({string.Join(", ", parts)})"; + } + return null; + }) + .Where(s => s != null) + .ToList(); + + if (storeOverrides.Count != 0) + { + NewMessage($" Store overrides: {string.Join(", ", storeOverrides)}", Color.Gray); + } + } + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs index 1d00ad15f..360038eed 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs @@ -397,6 +397,8 @@ namespace Barotrauma private static void InitProjectSpecific() { + InitShowSoldItems(); + commands.Add(new Command("eosStat", "Query and display all logged in EOS users. Normally this is at most two users, but in a developer environment it could be more.", args => { if (!EosInterface.Core.IsInitialized) @@ -3466,6 +3468,13 @@ namespace Barotrauma } } })); + + commands.Add(new Command("multiclienttestmode", "Makes the client enable some special logic (such as using a client-specific folder for downloads) to prevent conflicts between multiple clients on the same machine. Useful for testing the campaign with multiple clients running locally.", (string[] args) => + { + GameClient.MultiClientTestMode = !GameClient.MultiClientTestMode; + NewMessage($"{(GameClient.MultiClientTestMode ? "Enabled" : "Disabled")} MultiClientTestMode on the client."); + })); + AssignRelayToServer("multiclienttestmode", false); #endif commands.Add(new Command("reloadcorepackage", "", (string[] args) => @@ -4204,8 +4213,12 @@ namespace Barotrauma NewMessage("Minimum main path width: " + (Level.Loaded.LevelData?.MinMainPathWidth?.ToString() ?? "unknown")); } }); + + } + + private static void ReloadWearables(Character character, int variant = 0) { foreach (var limb in character.AnimController.Limbs) @@ -4442,7 +4455,9 @@ namespace Barotrauma #endif System.Threading.Thread.Sleep(1000); } - +#if DEBUG + GameClient.MultiClientTestMode = true; +#endif GameMain.Client = new GameClient("client1", new LidgrenEndpoint(System.Net.IPAddress.Loopback, NetConfig.DefaultPort), "localhost", Option.None()); @@ -4454,9 +4469,9 @@ namespace Barotrauma { System.Threading.Thread.Sleep(1000); #if WINDOWS - Process.Start("Barotrauma.exe", arguments: "-connect server localhost -username client" + i); + Process.Start("Barotrauma.exe", arguments: "-connect server localhost -username client" + i + " -multiclienttestmode"); #else - Process.Start("./Barotrauma", arguments: "-connect server localhost -username client" + i); + Process.Start("./Barotrauma", arguments: "-connect server localhost -username client" + i + " -multiclienttestmode"); #endif } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/GoToMission.cs b/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/GoToMission.cs index a7a45837c..3559851cc 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/GoToMission.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/GoToMission.cs @@ -2,7 +2,10 @@ { partial class GoToMission : Mission { - public override bool DisplayAsCompleted => State >= Prefab.MaxProgressState; + public override bool DisplayAsCompleted => + State >= Prefab.MaxProgressState && + //if there's some additional check for completion, don't display as completed until we've checked it and set the mission as completed + (Completed || completeCheckDataAction == null); public override bool DisplayAsFailed => false; } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/SalvageMission.cs b/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/SalvageMission.cs index 2009a3819..8b9d8c492 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/SalvageMission.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Events/Missions/SalvageMission.cs @@ -139,6 +139,8 @@ namespace Barotrauma } } - public override IEnumerable HudIconTargets => targets.Where(static t => !t.Retrieved && t.Item?.GetRootInventoryOwner() is not Character { IsLocalPlayer: true }).Select(static t => t.Item); + public override IEnumerable HudIconTargets => targets + .Where(static t => t.Item != null && !t.Retrieved && t.Item?.GetRootInventoryOwner() is not Character { IsLocalPlayer: true }) + .Select(static t => t.Item); } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/LoadingScreen.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/LoadingScreen.cs index 3231c2c8f..90ca5d713 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/LoadingScreen.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/LoadingScreen.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Input; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; namespace Barotrauma @@ -50,9 +51,15 @@ namespace Barotrauma } private RichString selectedTip; + + private string selectedTipString; + private ImmutableArray? selectedTipRichTextData; + private void SetSelectedTip(LocalizedString tip) { selectedTip = RichString.Rich(tip); + selectedTipString = string.Empty; + selectedTipRichTextData = null; } public float LoadState; @@ -165,13 +172,20 @@ namespace Barotrauma textPos.Y += GUIStyle.LargeFont.MeasureString(loadText.ToUpper()).Y * 1.2f; } - if (GUIStyle.Font.HasValue && selectedTip != null) + if (GUIStyle.Font.HasValue && selectedTip != null && !selectedTip.SanitizedValue.IsNullOrEmpty()) { - string wrappedTip = ToolBox.WrapText(selectedTip.SanitizedValue, GameMain.GraphicsWidth * 0.3f, GUIStyle.Font.Value); - string[] lines = wrappedTip.Split('\n'); - float lineHeight = GUIStyle.Font.MeasureString(selectedTip).Y; + //store the string value of the LocalizedString to prevent the text from changing if/when new text packs are loaded during the loading screen + if (selectedTipString.IsNullOrEmpty()) + { + selectedTipString = selectedTip.SanitizedValue; + selectedTipRichTextData = selectedTip.RichTextData; + } - if (selectedTip.RichTextData != null) + string wrappedTip = ToolBox.WrapText(selectedTipString, GameMain.GraphicsWidth * 0.3f, GUIStyle.Font.Value); + string[] lines = wrappedTip.Split('\n'); + float lineHeight = GUIStyle.Font.MeasureString(selectedTipString).Y; + + if (selectedTipRichTextData != null) { int rtdOffset = 0; for (int i = 0; i < lines.Length; i++) @@ -179,7 +193,7 @@ namespace Barotrauma GUIStyle.Font.DrawStringWithColors(spriteBatch, lines[i], new Vector2(textPos.X, (int)(textPos.Y + i * lineHeight)), Color.White, - 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f, selectedTip.RichTextData.Value, rtdOffset); + 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f, selectedTipRichTextData.Value, rtdOffset); rtdOffset += lines[i].Length; } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/Store.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/Store.cs index b635014ad..bb806e255 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/Store.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/Store.cs @@ -860,26 +860,22 @@ namespace Barotrauma FilterStoreItems(category, searchBox.Text); } - private static KeyValuePair? GetReputationRequirement(PriceInfo priceInfo) + private static float GetReputationRequirement(PriceInfo priceInfo, Identifier faction) { - return GameMain.GameSession?.Campaign is not null - ? priceInfo.MinReputation.FirstOrNull() - : null; + return priceInfo.MinReputation.GetValueOrDefault(faction); } - private static KeyValuePair? GetTooLowReputation(PriceInfo priceInfo) + private static bool ReputationRequirementsMet(PriceInfo priceInfo, Identifier faction) { + if (priceInfo.MinReputation.None()) { return true; } if (GameMain.GameSession?.Campaign is CampaignMode campaign) { - foreach (var minRep in priceInfo.MinReputation) + if (priceInfo.MinReputation.TryGetValue(faction, out float requirement)) { - if (MathF.Round(campaign.GetReputation(minRep.Key)) < minRep.Value) - { - return minRep; - } + return MathF.Round(campaign.GetReputation(faction)) >= requirement; } } - return null; + return false; } int prevDailySpecialCount, prevRequestedGoodsCount, prevSubRequestedGoodsCount; @@ -948,7 +944,7 @@ namespace Barotrauma SetPriceGetters(itemFrame, true); } - SetItemFrameStatus(itemFrame, hasPermissions && quantity > 0 && !GetTooLowReputation(priceInfo).HasValue); + SetItemFrameStatus(itemFrame, hasPermissions && quantity > 0 && ReputationRequirementsMet(priceInfo, ActiveStore.GetMerchantOrLocationFactionIdentifier())); existingItemFrames.Add(itemFrame); } } @@ -1464,8 +1460,9 @@ namespace Barotrauma PriceInfo priceInfo2 = item2.ItemPrefab.GetPriceInfo(ActiveStore); if (priceInfo1 != null && priceInfo2 != null) { - var requiredReputation1 = GetTooLowReputation(priceInfo1)?.Value ?? 0.0f; - var requiredReputation2 = GetTooLowReputation(priceInfo2)?.Value ?? 0.0f; + Identifier faction = ActiveStore.GetMerchantOrLocationFactionIdentifier(); + float requiredReputation1 = ReputationRequirementsMet(priceInfo1, faction) ? 0.0f : GetReputationRequirement(priceInfo1, faction); + float requiredReputation2 = ReputationRequirementsMet(priceInfo2, faction) ? 0.0f : GetReputationRequirement(priceInfo2, faction); return requiredReputation1.CompareTo(requiredReputation2); } return 0; @@ -1942,14 +1939,15 @@ namespace Barotrauma var campaign = GameMain.GameSession?.Campaign; if (priceInfo != null && campaign != null) { - var requiredReputation = GetReputationRequirement(priceInfo); - if (requiredReputation != null) + Identifier faction = ActiveStore.GetMerchantOrLocationFactionIdentifier(); + float requiredReputation = GetReputationRequirement(priceInfo, faction); + if (requiredReputation > 0) { var repStr = TextManager.GetWithVariables( "campaignstore.reputationrequired", - ("[amount]", ((int)requiredReputation.Value.Value).ToString()), - ("[faction]", TextManager.Get("faction." + requiredReputation.Value.Key).Value)); - Color color = MathF.Round(campaign.GetReputation(requiredReputation.Value.Key)) < requiredReputation.Value.Value ? + ("[amount]", ((int)requiredReputation).ToString()), + ("[faction]", TextManager.Get("faction." + faction).Value)); + Color color = MathF.Round(campaign.GetReputation(faction)) < requiredReputation ? GUIStyle.Orange : GUIStyle.Green; toolTip += $"\n‖color:{color.ToStringHex()}‖{repStr}‖color:end‖"; } diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/UpgradeStore.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/UpgradeStore.cs index e8e9dd49f..24d45bcdc 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/UpgradeStore.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/UpgradeStore.cs @@ -1743,12 +1743,12 @@ namespace Barotrauma } } - static void CreateMaterialCosts(GUIListBox list, UpgradePrefab prefab, int targetLevel) + static void CreateMaterialCosts(GUIListBox list, UpgradePrefab upgradePrefab, int targetLevel) { list.Content.ClearChildren(); var allItems = CargoManager.FindAllItemsOnPlayerAndSub(Character.Controlled); - var resources = prefab.GetApplicableResources(targetLevel); + var resources = upgradePrefab.GetApplicableResources(targetLevel); foreach (ApplicableResourceCollection collection in resources) { @@ -1769,7 +1769,7 @@ namespace Barotrauma bool hasItems = collection.Cost.Amount <= allItems.Count(collection.Cost.MatchesItem); - Sprite icon = defaultItemPrefab.InventoryIcon ?? prefab.Sprite; + Sprite icon = defaultItemPrefab.InventoryIcon ?? defaultItemPrefab.Sprite; Color iconColor = defaultItemPrefab.InventoryIcon is null ? defaultItemPrefab.SpriteColor : defaultItemPrefab.InventoryIconColor; GUIImage itemIcon = new GUIImage(new RectTransform(Vector2.One, itemFrame.RectTransform, scaleBasis: ScaleBasis.Smallest, anchor: Anchor.Center), sprite: icon, scaleToFit: true) @@ -1798,7 +1798,7 @@ namespace Barotrauma if (index > length) { index = 0; } ItemPrefab currentPrefab = collection.MatchingItems[(int)MathF.Floor(index)]; - Sprite icon = currentPrefab.InventoryIcon ?? prefab.Sprite; + Sprite icon = currentPrefab.InventoryIcon ?? currentPrefab.Sprite; Color iconColor = currentPrefab.InventoryIcon is null ? currentPrefab.SpriteColor : currentPrefab.InventoryIconColor; itemIcon.Sprite = icon; itemIcon.Color = hasItems ? iconColor : iconColor * 0.9f; diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs index 38c159d25..36ee6950d 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs @@ -270,6 +270,13 @@ namespace Barotrauma ConnectCommand = Option.None(); } +#if DEBUG + if (ConsoleArguments.Contains("-multiclienttestmode")) + { + DebugConsole.NewMessage("Enabled MultiClientTestMode on the client"); + GameClient.MultiClientTestMode = true; + } +#endif GUI.KeyboardDispatcher = new EventInput.KeyboardDispatcher(Window); PerformanceCounter = new PerformanceCounter(); diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameSession/CrewManager.cs b/Barotrauma/BarotraumaClient/ClientSource/GameSession/CrewManager.cs index 08bf2e2be..e3a8efb27 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameSession/CrewManager.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameSession/CrewManager.cs @@ -2866,10 +2866,11 @@ namespace Barotrauma } contextualOrders.RemoveAll(o => !IsOrderAvailable(o)); var offsets = MathUtils.GetPointsOnCircumference(Vector2.Zero, nodeDistance, contextualOrders.Count, MathHelper.ToRadians(90f + 180f / contextualOrders.Count)); - bool disableNode = !CanCharacterBeHeard(); + bool canCharacterBeHeard = !CanCharacterBeHeard(); for (int i = 0; i < contextualOrders.Count; i++) { var order = contextualOrders[i]; + bool disableNode = !canCharacterBeHeard && !order.TargetAllCharacters; int hotkey = (i + 1) % 10; var component = order.Option.IsEmpty ? CreateOrderNode(nodeSize, commandFrame.RectTransform, offsets[i].ToPoint(), order, hotkey, disableNode: disableNode, checkIfOrderCanBeHeard: false) : diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameSession/GameModes/SinglePlayerCampaign.cs b/Barotrauma/BarotraumaClient/ClientSource/GameSession/GameModes/SinglePlayerCampaign.cs index 3c49e47eb..1cec81758 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameSession/GameModes/SinglePlayerCampaign.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameSession/GameModes/SinglePlayerCampaign.cs @@ -258,9 +258,9 @@ namespace Barotrauma { SlideshowPlayer = new SlideshowPlayer(GUICanvas.Instance, slideshow); } - var outpost = GameMain.GameSession.Level.StartOutpost; - var borders = outpost.GetDockedBorders(); - borders.Location += outpost.WorldPosition.ToPoint(); + var subToFocusTo = GameMain.GameSession.Level.StartOutpost ?? Submarine.MainSub; + var borders = subToFocusTo.GetDockedBorders(); + borders.Location += subToFocusTo.WorldPosition.ToPoint(); GameMain.GameScreen.Cam.Position = new Vector2(borders.X + borders.Width / 2, borders.Y - borders.Height / 2); float startZoom = 0.8f / ((float)Math.Max(borders.Width, borders.Height) / (float)GameMain.GameScreen.Cam.Resolution.X); @@ -646,9 +646,12 @@ namespace Barotrauma modeElement.Add(GameMain.GameSession?.EventManager.Save()); } - foreach (Identifier unlockedRecipe in GameMain.GameSession.UnlockedRecipes) + foreach ((CharacterTeamType team, Identifier unlockedRecipe) in GameMain.GameSession.UnlockedRecipes) { - modeElement.Add(new XElement("unlockedrecipe", new XAttribute("identifier", unlockedRecipe))); + modeElement.Add( + new XElement("unlockedrecipe", + new XAttribute("identifier", unlockedRecipe), + new XAttribute("team", team))); } //save and remove all items that are in someone's inventory so they don't get included in the sub file as well diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemContainer.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemContainer.cs index da3a36e6a..c0a6ece27 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemContainer.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemContainer.cs @@ -197,8 +197,8 @@ namespace Barotrauma.Items.Components }; LocalizedString labelText = GetUILabel(); - GUITextBlock label = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform, Anchor.TopCenter), - labelText, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.CenterLeft, wrap: true) + GUITextBlock label = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform, Anchor.TopLeft), + labelText, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.TopLeft, wrap: true) { IgnoreLayoutGroups = true }; @@ -206,6 +206,8 @@ namespace Barotrauma.Items.Components int buttonSize = GUIStyle.ItemFrameTopBarHeight; Point margin = new Point(buttonSize / 4, buttonSize / 6); + int buttonCount = 0; + GUILayoutGroup buttonArea = new GUILayoutGroup(new RectTransform(new Point(content.Rect.Width, buttonSize - margin.Y * 2), content.RectTransform, Anchor.TopRight) { AbsoluteOffset = new Point(0, margin.Y) }, isHorizontal: true, childAnchor: Anchor.TopRight) { @@ -213,24 +215,37 @@ namespace Barotrauma.Items.Components }; if (Inventory.Capacity > 1) { - new GUIButton(new RectTransform(Vector2.One, buttonArea.RectTransform, scaleBasis: ScaleBasis.Smallest), style: "SortItemsButton") + if (ShowSortButton) { - ToolTip = TextManager.Get("SortItemsAlphabetically"), - OnClicked = (btn, userdata) => + buttonCount++; + new GUIButton(new RectTransform(Vector2.One, buttonArea.RectTransform, scaleBasis: ScaleBasis.Smallest), style: "SortItemsButton") { - SortItems(); - return true; - } - }; - new GUIButton(new RectTransform(Vector2.One, buttonArea.RectTransform, scaleBasis: ScaleBasis.Smallest), style: "MergeStacksButton") + ToolTip = TextManager.Get("SortItemsAlphabetically"), + OnClicked = (btn, userdata) => + { + SortItems(); + return true; + } + }; + } + if (ShowMergeButton) { - ToolTip = TextManager.Get("MergeItemStacks"), - OnClicked = (btn, userdata) => + buttonCount++; + new GUIButton(new RectTransform(Vector2.One, buttonArea.RectTransform, scaleBasis: ScaleBasis.Smallest), style: "MergeStacksButton") { - MergeStacks(); - return true; - } - }; + ToolTip = TextManager.Get("MergeItemStacks"), + OnClicked = (btn, userdata) => + { + MergeStacks(); + return true; + } + }; + } + } + + if (buttonCount > 0) + { + label.RectTransform.MaxSize = new Point(label.Parent.Rect.Width - buttonCount * buttonSize, int.MaxValue); } float minInventoryAreaSize = 0.5f; diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/MiniMap.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/MiniMap.cs index fafa2ee3f..3c08f7fe7 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/MiniMap.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/MiniMap.cs @@ -1078,7 +1078,7 @@ namespace Barotrauma.Items.Components if (hullData is null) { hullData = new HullData(); - GetLinkedHulls(hull, hullData.LinkedHulls); + hull.GetLinkedHulls(hullData.LinkedHulls); hullDatas.Add(hull, hullData); } @@ -1586,19 +1586,6 @@ namespace Barotrauma.Items.Components } } - public static void GetLinkedHulls(Hull hull, List linkedHulls) - { - foreach (var linkedEntity in hull.linkedTo) - { - if (linkedEntity is Hull linkedHull) - { - if (linkedHulls.Contains(linkedHull) || linkedHull.IsHidden) { continue; } - linkedHulls.Add(linkedHull); - GetLinkedHulls(linkedHull, linkedHulls); - } - } - } - public static GUIFrame CreateMiniMap(Submarine sub, GUIComponent parent, MiniMapSettings settings) { return CreateMiniMap(sub, parent, settings, null, out _); @@ -1791,7 +1778,7 @@ namespace Barotrauma.Items.Components if (combinedHulls.ContainsKey(hull) || combinedHulls.Values.Any(hh => hh.Contains(hull))) { continue; } List linkedHulls = new List(); - GetLinkedHulls(hull, linkedHulls); + hull.GetLinkedHulls(linkedHulls); linkedHulls.Remove(hull); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/Pump.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/Pump.cs index 2c0adbcb6..e2afb09df 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/Pump.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Machines/Pump.cs @@ -133,7 +133,7 @@ namespace Barotrauma.Items.Components partial void UpdateProjSpecific(float deltaTime) { - if (FlowPercentage < 0.0f) + if (currFlow < 0f) { foreach (var (position, emitter) in pumpOutEmitters) { @@ -154,10 +154,10 @@ namespace Barotrauma.Items.Components } emitter.Emit(deltaTime, item.WorldPosition + relativeParticlePos, item.CurrentHull, angle, - velocityMultiplier: MathHelper.Lerp(0.5f, 1.0f, -FlowPercentage / 100.0f)); + velocityMultiplier: MathHelper.Lerp(0.5f, 1.0f, -currFlow / maxFlow)); } } - else if (FlowPercentage > 0.0f) + else if (currFlow > 0f) { foreach (var (position, emitter) in pumpInEmitters) { @@ -174,7 +174,7 @@ namespace Barotrauma.Items.Components relativeParticlePos.Y = -relativeParticlePos.Y; } emitter.Emit(deltaTime, item.WorldPosition + relativeParticlePos, item.CurrentHull, angle, - velocityMultiplier: MathHelper.Lerp(0.5f, 1.0f, FlowPercentage / 100.0f)); + velocityMultiplier: MathHelper.Lerp(0.5f, 1.0f, currFlow / maxFlow)); } } } @@ -185,7 +185,7 @@ namespace Barotrauma.Items.Components { autoControlIndicator.Selected = IsAutoControlled; PowerButton.Enabled = isActiveLockTimer <= 0.0f; - if (HasPower) + if (HasPower && !Disabled) { flickerTimer = 0; powerLight.Selected = IsActive; @@ -229,6 +229,7 @@ namespace Barotrauma.Items.Components float flowPercentage = msg.ReadRangedInteger(-10, 10) * 10.0f; bool isActive = msg.ReadBoolean(); bool hijacked = msg.ReadBoolean(); + bool disabled = msg.ReadBoolean(); float? targetLevel; if (msg.ReadBoolean()) { @@ -250,6 +251,7 @@ namespace Barotrauma.Items.Components FlowPercentage = flowPercentage; IsActive = isActive; Hijacked = hijacked; + Disabled = disabled; TargetLevel = targetLevel; } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Rope.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Rope.cs index 32a3d1528..34764275d 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Rope.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/Rope.cs @@ -34,8 +34,8 @@ namespace Barotrauma.Items.Components set; } - [Serialize("0.5,0.5)", IsPropertySaveable.No)] - public Vector2 Origin { get; set; } = new Vector2(0.5f, 0.5f); + [Serialize("0.5,0.5", IsPropertySaveable.No)] + public Vector2 Origin { get; set; } [Serialize(true, IsPropertySaveable.No, description: "")] public bool BreakFromMiddle diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/StatusHUD.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/StatusHUD.cs index 078f49ebd..9b999dbae 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/StatusHUD.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/StatusHUD.cs @@ -314,9 +314,12 @@ namespace Barotrauma.Items.Components textColors.Add(GUIStyle.Orange); } - int oxygenTextIndex = MathHelper.Clamp((int)Math.Floor((1.0f - (target.Oxygen / 100.0f)) * OxygenTexts.Length), 0, OxygenTexts.Length - 1); - texts.Add(OxygenTexts[oxygenTextIndex]); - textColors.Add(Color.Lerp(GUIStyle.Red, GUIStyle.Green, target.Oxygen / 100.0f)); + if (target.NeedsOxygen) + { + int oxygenTextIndex = MathHelper.Clamp((int)Math.Floor((1.0f - (target.Oxygen / 100.0f)) * OxygenTexts.Length), 0, OxygenTexts.Length - 1); + texts.Add(OxygenTexts[oxygenTextIndex]); + textColors.Add(Color.Lerp(GUIStyle.Red, GUIStyle.Green, target.Oxygen / 100.0f)); + } if (target.Bleeding > 0.0f) { diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Inventory.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Inventory.cs index fd2dc5ef0..21f681e5e 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Inventory.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Inventory.cs @@ -359,7 +359,7 @@ namespace Barotrauma #endif if (!item.Prefab.UnlockedRecipeInToolTip.IsEmpty && GameMain.GameSession is { } GameSession) { - if (GameSession.UnlockedRecipes.Contains(item.Prefab.UnlockedRecipeInToolTip)) + if (GameSession.HasUnlockedRecipe(Character.Controlled, item.Prefab.UnlockedRecipeInToolTip)) { toolTip += TextManager.Get("unlockedrecipe.true"); } @@ -1435,8 +1435,20 @@ namespace Barotrauma { if (giver == null || receiver == null || draggedItems.None()) { return false; } if (receiver == giver) { return false; } + + CharacterInventory.AccessLevel accessLevel; + if (draggedItems.Any(it => it.HasTag(Tags.HandLockerItem))) + { + //handcuffs can't be given to players by dragging and dropping (because it can allow handcuffing them) + accessLevel = CharacterInventory.AccessLevel.AllowBotsAndPets; + } + else + { + accessLevel = IsDragAndDropGiveAllowed ? CharacterInventory.AccessLevel.AllowFriendly : CharacterInventory.AccessLevel.AllowBotsAndPets; + } + return - receiver.IsInventoryAccessibleTo(giver, IsDragAndDropGiveAllowed ? CharacterInventory.AccessLevel.Allowed : CharacterInventory.AccessLevel.Limited) && + receiver.IsInventoryAccessibleTo(giver, accessLevel) && receiver.Inventory.CanBePut(draggedItems.FirstOrDefault(), InvSlotType.Any); } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Map/Structure.cs b/Barotrauma/BarotraumaClient/ClientSource/Map/Structure.cs index a537a930c..e15d5bd48 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Map/Structure.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Map/Structure.cs @@ -501,10 +501,14 @@ namespace Barotrauma { float newCutoff = MathHelper.Lerp(0.0f, 0.65f, Sections[i].damage / MaxHealth); - if (Math.Abs(newCutoff - Submarine.DamageEffectCutoff) > 0.05f) + //change the parameters of the damage effect and start a new sprite batch if the damage is different by 5% or more + if (Math.Abs(newCutoff - Submarine.DamageEffectCutoff) > 0.01f || + //if we were previously rendering some small amount of damage but now 0 damage, make sure we update the parameters + //"no damage" vs "just a tiny fraction of damage" makes a difference, even though normally 5% differences in damage aren't noticeable + MathUtils.NearlyEqual(newCutoff, 0.0f) != MathUtils.NearlyEqual(Submarine.DamageEffectCutoff, 0.0f)) { spriteBatch.End(); - spriteBatch.Begin(SpriteSortMode.BackToFront, + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, null, null, damageEffect, diff --git a/Barotrauma/BarotraumaClient/ClientSource/Map/Submarine.cs b/Barotrauma/BarotraumaClient/ClientSource/Map/Submarine.cs index aa50745c8..bbb921dcf 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Map/Submarine.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Map/Submarine.cs @@ -160,37 +160,39 @@ namespace Barotrauma public static float DamageEffectCutoff; + private static readonly List depthSortedDamageable = new List(); + public static void DrawDamageable(SpriteBatch spriteBatch, Effect damageEffect, bool editing = false, Predicate predicate = null) { - if (!editing && visibleEntities != null) + var entitiesToRender = !editing && visibleEntities != null ? visibleEntities : MapEntity.MapEntityList; + + depthSortedDamageable.Clear(); + + //insertion sort according to draw depth + foreach (MapEntity e in entitiesToRender) { - foreach (MapEntity e in visibleEntities) + if (e is Structure structure && structure.DrawDamageEffect) { - if (e is Structure structure && structure.DrawDamageEffect) + if (predicate != null) { - if (predicate != null) - { - if (!predicate(structure)) { continue; } - } - structure.DrawDamage(spriteBatch, damageEffect, editing); + if (!predicate(e)) { continue; } } - } - } - else - { - foreach (Structure structure in Structure.WallList) - { - if (structure.DrawDamageEffect) + float drawDepth = structure.GetDrawDepth(); + int i = 0; + while (i < depthSortedDamageable.Count) { - if (predicate != null) - { - if (!predicate(structure)) { continue; } - } - structure.DrawDamage(spriteBatch, damageEffect, editing); + float otherDrawDepth = depthSortedDamageable[i].GetDrawDepth(); + if (otherDrawDepth < drawDepth) { break; } + i++; } + depthSortedDamageable.Insert(i, structure); } } + foreach (Structure s in depthSortedDamageable) + { + s.DrawDamage(spriteBatch, damageEffect, editing); + } } public static void DrawPaintedColors(SpriteBatch spriteBatch, bool editing = false, Predicate predicate = null) @@ -287,7 +289,7 @@ namespace Barotrauma if (combinedHulls.ContainsKey(hull) || combinedHulls.Values.Any(hh => hh.Contains(hull))) { continue; } List linkedHulls = new List(); - MiniMap.GetLinkedHulls(hull, linkedHulls); + hull.GetLinkedHulls(linkedHulls); linkedHulls.Remove(hull); @@ -297,7 +299,6 @@ namespace Barotrauma { combinedHulls.Add(hull, new HashSet()); } - combinedHulls[hull].Add(linkedHull); } } @@ -567,6 +568,8 @@ namespace Barotrauma { if (item.GetComponent() is not OxygenGenerator oxygenGenerator) { continue; } + oxygenGenerator.GetVents(); + Dictionary hullOxygenFlow = new Dictionary(); foreach (var linkedTo in item.linkedTo) diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/FileTransfer/FileReceiver.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/FileTransfer/FileReceiver.cs index 5bda4ceb1..c47244bf5 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/FileTransfer/FileReceiver.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/FileTransfer/FileReceiver.cs @@ -256,6 +256,15 @@ namespace Barotrauma.Networking } string downloadFolder = downloadFolders[(FileTransferType)fileType]; +#if CLIENT && DEBUG + if (GameClient.MultiClientTestMode) + { + //append the name of the client to the download folder to avoid multiple clients + //from trying to download a file into the same path at the same time + downloadFolder += "_" + GameMain.Client.Name; + } +#endif + if (!Directory.Exists(downloadFolder)) { try diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs index d8b8c7f54..3c6b7df5a 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs @@ -11,7 +11,6 @@ using System.Globalization; using System.Linq; using System.Threading.Tasks; using System.Xml.Linq; -using Barotrauma.PerkBehaviors; namespace Barotrauma.Networking { @@ -26,6 +25,8 @@ namespace Barotrauma.Networking #if DEBUG public float DebugServerVoipAmplitude; + + public static bool MultiClientTestMode; #endif public override Voting Voting { get; } @@ -873,8 +874,9 @@ namespace Barotrauma.Networking ReadAchievement(inc); break; case ServerPacketHeader.UNLOCKRECIPE: + CharacterTeamType team = (CharacterTeamType)inc.ReadByte(); Identifier identifier = inc.ReadIdentifier(); - GameMain.GameSession.UnlockRecipe(identifier, showNotifications: true); + GameMain.GameSession?.UnlockRecipe(team, identifier, showNotifications: true); break; case ServerPacketHeader.ACHIEVEMENT_STAT: ReadAchievementStat(inc); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/DualStackP2PSocket.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/DualStackP2PSocket.cs index 613767851..9ac3b368f 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/DualStackP2PSocket.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/DualStackP2PSocket.cs @@ -10,30 +10,31 @@ sealed class DualStackP2PSocket : P2PSocket private DualStackP2PSocket( Callbacks callbacks, Option eosSocket, - Option steamSocket) : - base(callbacks) + Option steamSocket, + OwnerOrClient type) : + base(callbacks, type) { this.eosSocket = eosSocket; this.steamSocket = steamSocket; } - public static Result Create(Callbacks callbacks) + public static Result Create(Callbacks callbacks, OwnerOrClient type) { - var eosP2PSocketResult = EosP2PSocket.Create(callbacks); - var steamP2PSocketResult = SteamListenSocket.Create(callbacks); + var eosP2PSocketResult = EosP2PSocket.Create(callbacks, type); + var steamP2PSocketResult = SteamListenSocket.Create(callbacks, type); if (eosP2PSocketResult.TryUnwrapFailure(out var eosError) && steamP2PSocketResult.TryUnwrapFailure(out var steamError)) { return Result.Failure(new Error(eosError, steamError)); } - return Result.Success((P2PSocket)new DualStackP2PSocket( - callbacks, - eosP2PSocketResult.TryUnwrapSuccess(out var eosP2PSocket) - ? Option.Some((EosP2PSocket)eosP2PSocket) - : Option.None, - steamP2PSocketResult.TryUnwrapSuccess(out var steamP2PSocket) - ? Option.Some((SteamListenSocket)steamP2PSocket) - : Option.None)); + return Result.Success(new DualStackP2PSocket( + callbacks, + eosP2PSocketResult.TryUnwrapSuccess(out var eosP2PSocket) + ? Option.Some((EosP2PSocket)eosP2PSocket) + : Option.None, + steamP2PSocketResult.TryUnwrapSuccess(out var steamP2PSocket) + ? Option.Some((SteamListenSocket)steamP2PSocket) + : Option.None, type)); } public override void ProcessIncomingMessages() diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/EosP2PSocket.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/EosP2PSocket.cs index 73531529b..985e84221 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/EosP2PSocket.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/EosP2PSocket.cs @@ -8,13 +8,14 @@ sealed class EosP2PSocket : P2PSocket private EosP2PSocket( Callbacks callbacks, - EosInterface.P2PSocket eosSocket) - : base(callbacks) + EosInterface.P2PSocket eosSocket, + OwnerOrClient type) + : base(callbacks, type) { this.eosSocket = eosSocket; } - public static Result Create(Callbacks callbacks) + public static Result Create(Callbacks callbacks, OwnerOrClient type) { if (!EosInterface.Core.IsInitialized) { return Result.Failure(new Error(ErrorCode.EosNotInitialized)); } @@ -26,19 +27,25 @@ sealed class EosP2PSocket : P2PSocket var socketCreateResult = EosInterface.P2PSocket.Create(puids[0], eosSocketId); if (!socketCreateResult.TryUnwrapSuccess(out var eosSocket)) { return Result.Failure(new Error(ErrorCode.FailedToCreateEosP2PSocket, socketCreateResult.ToString())); } - var retVal = new EosP2PSocket(callbacks, eosSocket); + var retVal = new EosP2PSocket(callbacks, eosSocket, type); eosSocket.HandleIncomingConnection.Register("Event".ToIdentifier(), retVal.OnIncomingConnection); eosSocket.HandleClosedConnection.Register("Event".ToIdentifier(), retVal.OnConnectionClosed); - return Result.Success((P2PSocket)retVal); + return Result.Success(retVal); } public override void ProcessIncomingMessages() { foreach (var msg in eosSocket.GetMessageBatch()) { - callbacks.OnData(new EosP2PEndpoint(msg.Sender), new ReadWriteMessage(msg.Buffer, 0, msg.ByteLength * 8, false)); + EosP2PEndpoint endpoint = new EosP2PEndpoint(msg.Sender); + callbacks.OnData(endpoint, new ReadWriteMessage(msg.Buffer, 0, msg.ByteLength * 8, false)); + + if (Type is OwnerOrClient.Owner) + { + dosProtection.OnPacket(endpoint); + } } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/P2PSocket.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/P2PSocket.cs index 4c54b406d..deccacb60 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/P2PSocket.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/P2PSocket.cs @@ -8,6 +8,15 @@ namespace Barotrauma.Networking; abstract class P2PSocket : IDisposable { + public readonly P2POwnerDoSProtection dosProtection; + public readonly OwnerOrClient Type; + + public enum OwnerOrClient + { + Client, + Owner + } + public enum ErrorCode { EosNotInitialized, @@ -38,12 +47,16 @@ abstract class P2PSocket : IDisposable public readonly record struct Callbacks( Predicate OnIncomingConnection, Action OnConnectionClosed, + P2POwnerDoSProtection.ExcessivePacketDelegate OnExcessivePackets, Action OnData); protected readonly Callbacks callbacks; - protected P2PSocket(Callbacks callbacks) + protected P2PSocket(Callbacks callbacks, OwnerOrClient type) { this.callbacks = callbacks; + Type = type; + + dosProtection = new P2POwnerDoSProtection(callbacks.OnExcessivePackets); } public abstract void ProcessIncomingMessages(); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamConnectSocket.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamConnectSocket.cs index aceb50a95..21fdc3650 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamConnectSocket.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamConnectSocket.cs @@ -64,13 +64,13 @@ sealed class SteamConnectSocket : P2PSocket private readonly SteamP2PEndpoint expectedEndpoint; private readonly ConnectionManager connectionManager; - private SteamConnectSocket(SteamP2PEndpoint expectedEndpoint, Callbacks callbacks, ConnectionManager connectionManager) : base(callbacks) + private SteamConnectSocket(SteamP2PEndpoint expectedEndpoint, Callbacks callbacks, ConnectionManager connectionManager, OwnerOrClient type) : base(callbacks, type) { this.expectedEndpoint = expectedEndpoint; this.connectionManager = connectionManager; } - public static Result Create(SteamP2PEndpoint endpoint, Callbacks callbacks) + public static Result Create(SteamP2PEndpoint endpoint, Callbacks callbacks, OwnerOrClient type) { if (!SteamManager.IsInitialized) { return Result.Failure(new Error(ErrorCode.SteamNotInitialized)); } @@ -87,7 +87,7 @@ sealed class SteamConnectSocket : P2PSocket if (connectionManager is null) { return Result.Failure(new Error(ErrorCode.FailedToCreateSteamP2PSocket)); } connectionManager.SetEndpointAndCallbacks(endpoint, callbacks); - return Result.Success((P2PSocket)new SteamConnectSocket(endpoint, callbacks, connectionManager)); + return Result.Success(new SteamConnectSocket(endpoint, callbacks, connectionManager, type)); } public override void ProcessIncomingMessages() diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamListenSocket.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamListenSocket.cs index 6ca70f22d..078790285 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamListenSocket.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/P2PSocket/SteamListenSocket.cs @@ -10,12 +10,14 @@ sealed class SteamListenSocket : P2PSocket private sealed class SocketManager : Steamworks.SocketManager, Steamworks.ISocketManager { private Callbacks callbacks; + private P2PSocket socket; private readonly Dictionary endpointToConnection = new(); public void SetCallbacks(Callbacks callbacks) - { - this.callbacks = callbacks; - } + => this.callbacks = callbacks; + + public void SetSocket(P2PSocket socket) + => this.socket = socket; public override void OnConnecting(Steamworks.Data.Connection connection, Steamworks.Data.ConnectionInfo info) { @@ -65,7 +67,7 @@ sealed class SteamListenSocket : P2PSocket callbacks.OnConnectionClosed(remoteEndpoint, peerDisconnectPacket); base.OnDisconnected(connection, info); } - + public override void OnMessage(Steamworks.Data.Connection connection, Steamworks.Data.NetIdentity identity, IntPtr data, int size, long messageNum, long recvTime, int channel) { if (!identity.IsSteamId || data == IntPtr.Zero) { return; } @@ -75,6 +77,11 @@ sealed class SteamListenSocket : P2PSocket Marshal.Copy(source: data, destination: dataArray, startIndex: 0, length: size); callbacks.OnData(endpoint, new ReadWriteMessage(dataArray, bitPos: 0, lBits: size * 8, copyBuf: false)); + + if (socket?.Type is OwnerOrClient.Owner) + { + socket.dosProtection.OnPacket(endpoint); + } } internal bool SendMessage(SteamP2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod) @@ -107,26 +114,49 @@ sealed class SteamListenSocket : P2PSocket private SteamListenSocket( Callbacks callbacks, - SocketManager socketManager) - : base(callbacks) + SocketManager socketManager, + OwnerOrClient type) + : base(callbacks, type) { this.socketManager = socketManager; } - public static Result Create(Callbacks callbacks) + public static Result Create(Callbacks callbacks, OwnerOrClient type) { if (!SteamManager.IsInitialized) { return Result.Failure(new Error(ErrorCode.SteamNotInitialized)); } var socketManager = Steamworks.SteamNetworkingSockets.CreateRelaySocket(); if (socketManager is null) { return Result.Failure(new Error(ErrorCode.FailedToCreateSteamP2PSocket)); } - socketManager.SetCallbacks(callbacks); - return Result.Success((P2PSocket)new SteamListenSocket(callbacks, socketManager)); + socketManager.SetCallbacks(callbacks); + P2PSocket socket = new SteamListenSocket(callbacks, socketManager, type); + socketManager.SetSocket(socket); + + return Result.Success(socket); } public override void ProcessIncomingMessages() { - socketManager.Receive(); + const int bufferSize = 32; + const int maxIterations = 100; + + // could technically cause a stack overflow since the call is recursive, + // use a while loop instead + int iteration; + for (iteration = 0; iteration < maxIterations; iteration++) + { + int received = socketManager.Receive(bufferSize: bufferSize, receiveToEnd: false); + + if (received < bufferSize) + { + break; + } + } + + if (iteration >= maxIterations) + { + DebugConsole.ThrowError("Steam P2P socket received too many messages in a single frame."); + } } public override bool SendMessage(P2PEndpoint endpoint, IWriteMessage outMsg, DeliveryMethod deliveryMethod) diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2PClientPeer.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2PClientPeer.cs index 9e0e1e3c3..7cccef8c1 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2PClientPeer.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2PClientPeer.cs @@ -59,11 +59,11 @@ namespace Barotrauma.Networking ServerConnection = ServerEndpoint.MakeConnectionFromEndpoint(); - var socketCallbacks = new P2PSocket.Callbacks(OnIncomingConnection, OnConnectionClosed, OnP2PData); + var socketCallbacks = new P2PSocket.Callbacks(OnIncomingConnection, OnConnectionClosed, OnExcessivePackets, OnP2PData); var socketCreateResult = ServerEndpoint switch { - EosP2PEndpoint => EosP2PSocket.Create(socketCallbacks), - SteamP2PEndpoint steamP2PEndpoint => SteamConnectSocket.Create(steamP2PEndpoint, socketCallbacks), + EosP2PEndpoint => EosP2PSocket.Create(socketCallbacks, P2PSocket.OwnerOrClient.Client), + SteamP2PEndpoint steamP2PEndpoint => SteamConnectSocket.Create(steamP2PEndpoint, socketCallbacks, P2PSocket.OwnerOrClient.Client), _ => throw new Exception($"Invalid server endpoint: {ServerEndpoint.GetType()} {ServerEndpoint}") }; socket = socketCreateResult.TryUnwrapSuccess(out var s) @@ -97,6 +97,11 @@ namespace Barotrauma.Networking isActive = true; } + private void OnExcessivePackets(P2PEndpoint endpoint, bool shouldBan) + { + // do nothing + } + private bool OnIncomingConnection(P2PEndpoint remoteEndpoint) { if (remoteEndpoint == ServerEndpoint) @@ -163,7 +168,7 @@ namespace Barotrauma.Networking int completeMessageLengthBits = completeMessage.Length * 8; incomingDataMessages.Add(new ReadWriteMessage(completeMessage.ToArray(), 0, completeMessageLengthBits, copyBuf: false)); } - else if (packetHeader.IsHeartbeatMessage()) + else if (packetHeader.IsHeartbeatMessage() || packetHeader.IsDoSProtectionMessage()) { return; //TODO: implement heartbeats } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2POwnerPeer.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2POwnerPeer.cs index f9b0d0081..3d4d7bdb7 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2POwnerPeer.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/Primitives/Peers/P2POwnerPeer.cs @@ -88,8 +88,8 @@ namespace Barotrauma.Networking remotePeers.Clear(); - var socketCallbacks = new P2PSocket.Callbacks(OnIncomingConnection, OnConnectionClosed, OnP2PData); - var socketCreateResult = DualStackP2PSocket.Create(socketCallbacks); + var socketCallbacks = new P2PSocket.Callbacks(OnIncomingConnection, OnConnectionClosed, OnExcessivePackets, OnP2PData); + var socketCreateResult = DualStackP2PSocket.Create(socketCallbacks, type: P2PSocket.OwnerOrClient.Owner); socket = socketCreateResult.TryUnwrapSuccess(out var s) ? s : throw new Exception($"Failed to create dual-stack socket: {socketCreateResult}"); @@ -187,6 +187,29 @@ namespace Barotrauma.Networking } } + private void OnExcessivePackets(P2PEndpoint endpoint, bool shouldBan) + { + IWriteMessage msg = new WriteOnlyMessage(); + msg.WriteNetSerializableStruct(new P2POwnerToServerHeader + { + EndpointStr = selfPrimaryEndpoint.StringRepresentation, + AccountInfo = selfAccountInfo + }); + msg.WriteNetSerializableStruct(new PeerPacketHeaders + { + DeliveryMethod = DeliveryMethod.Reliable, + PacketHeader = PacketHeader.IsDoSProtectionMessage + }); + msg.WriteNetSerializableStruct(new DoSProtectionPacket(endpoint.StringRepresentation, shouldBan)); + string dcMsg = TextManager.Get(shouldBan ? "DoSProtectionBanned" : "DoSProtectionKicked") + .Fallback(TextManager.Get("DoSProtectionKicked")).Value; + + msg.WriteNetSerializableStruct(shouldBan + ? PeerDisconnectPacket.Banned(dcMsg) + : PeerDisconnectPacket.Kicked(dcMsg)); + ForwardToServerProcess(msg); + } + private void StartAuthTask(IReadMessage inc, RemotePeer remotePeer) { remotePeer.AuthStatus = RemotePeer.AuthenticationStatus.AuthenticationPending; diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerList/ServerInfo.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerList/ServerInfo.cs index 55a748c19..05376aa1b 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerList/ServerInfo.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerList/ServerInfo.cs @@ -612,7 +612,7 @@ namespace Barotrauma.Networking } public bool Equals(ServerInfo other) - => other.Endpoints.Any(e => Endpoints.Contains(e)); + => other != null && other.Endpoints.Any(Endpoints.Contains); public override int GetHashCode() => Endpoints.First().GetHashCode(); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerLog.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerLog.cs index 76036545e..7b9ffffa0 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerLog.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/ServerLog.cs @@ -9,6 +9,8 @@ namespace Barotrauma.Networking { public partial class ServerLog { + const int MaxLines = 500; + public GUIButton LogFrame; private GUIListBox listBox; private GUIButton reverseButton; @@ -17,6 +19,8 @@ namespace Barotrauma.Networking private bool reverseOrder = false; + private readonly bool[] msgTypeHidden = new bool[Enum.GetValues(typeof(MessageType)).Length]; + private bool OnReverseClicked(GUIButton btn, object obj) { SetMessageReversal(!reverseOrder); @@ -105,7 +109,10 @@ namespace Barotrauma.Networking reverseButton.Children.ForEach(c => c.SpriteEffects = reverseOrder ? SpriteEffects.FlipVertically : SpriteEffects.None); reverseButton.OnClicked = OnReverseClicked; - listBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.95f), listBoxLayout.RectTransform)); + listBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.95f), listBoxLayout.RectTransform)) + { + AutoHideScrollBar = false + }; GUIButton closeButton = new GUIButton(new RectTransform(new Vector2(0.25f, 0.05f), rightColumn.RectTransform), TextManager.Get("Close")) { @@ -127,7 +134,8 @@ namespace Barotrauma.Networking listBox.UpdateScrollBarSize(); - if (listBox.BarScroll == 0.0f || listBox.BarScroll == 1.0f) { listBox.BarScroll = 1.0f; } + //scrolled all the way down by default + listBox.BarScroll = 1.0f; msgFilter = ""; } @@ -189,11 +197,19 @@ namespace Barotrauma.Networking { float prevSize = listBox.BarSize; + GUIComponent firstVisibleLine = listBox.Content.Children.FirstOrDefault(c => c.Rect.Y > listBox.Content.Rect.Y); + int firstVisibileYPos = firstVisibleLine?.Rect.Y ?? 0; + + while (listBox.Content.CountChildren > MaxLines) + { + listBox.Content.RemoveChild(reverseOrder ? listBox.Content.Children.Last() : listBox.Content.Children.First()); + } + GUIFrame textContainer = null; Anchor anchor = Anchor.TopLeft; Pivot pivot = Pivot.TopLeft; - RichString richString = line.Text as RichString; + RichString richString = line.Text; if (richString != null && richString.RichTextData.HasValue) { foreach (var data in richString.RichTextData.Value) @@ -217,7 +233,7 @@ namespace Barotrauma.Networking line.Text, wrap: true, font: GUIStyle.SmallFont) { TextColor = messageColor[line.Type], - Visible = !msgTypeHidden[(int)line.Type], + Visible = !ShouldFilterMessage(line), CanBeFocused = false, UserData = line }; @@ -247,31 +263,47 @@ namespace Barotrauma.Networking } } - if ((prevSize == 1.0f && listBox.BarScroll == 0.0f) || (prevSize < 1.0f && listBox.BarScroll == 1.0f)) listBox.BarScroll = 1.0f; + //if the list was scrolled to the bottom, or to the top while the list wasn't full yet, + //keep it scrolled to the bottom + if ((MathUtils.NearlyEqual(prevSize, 1.0f) && MathUtils.NearlyEqual(listBox.BarScroll, 0.0f)) || + (prevSize < 1.0f && MathUtils.NearlyEqual(listBox.BarScroll, 1.0f))) + { + listBox.BarScroll = 1.0f; + } + //otherwise modify the scroll so the topmost element stays where it was (list doesn't jump as new lines are added when scrolled up) + else if (firstVisibleLine != null) + { + listBox.UpdateScrollBarSize(); + listBox.RecalculateChildren(); + int diff = firstVisibleLine.Rect.Y - firstVisibileYPos; + if (diff != 0) + { + listBox.BarScroll += diff / listBox.TotalSize * (prevSize / listBox.BarSize); + } + } } private bool FilterMessages() { - string filter = msgFilter == null ? "" : msgFilter.ToLower(); - foreach (GUIComponent child in listBox.Content.Children) { - if (!(child is GUITextBlock textBlock)) { continue; } + if (child is not GUITextBlock) { continue; } child.Visible = true; - if (msgTypeHidden[(int)((LogMessage)child.UserData).Type]) - { - child.Visible = false; - continue; - } - - textBlock.Visible = string.IsNullOrEmpty(filter) || textBlock.Text.ToLower().Contains(filter); + child.Visible = !ShouldFilterMessage((LogMessage)child.UserData); } listBox.UpdateScrollBarSize(); - listBox.BarScroll = 0.0f; + listBox.BarScroll = 1.0f; return true; } + private bool ShouldFilterMessage(LogMessage message) + { + if (msgTypeHidden[(int)message.Type]) { return true; } + string text = message.Text.SanitizedValue; + return !string.IsNullOrEmpty(msgFilter) && !text.Contains(msgFilter, StringComparison.InvariantCultureIgnoreCase); + } + private void SetMessageReversal(bool reverse) { if (reverseOrder == reverse) { return; } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Physics/PhysicsBody.cs b/Barotrauma/BarotraumaClient/ClientSource/Physics/PhysicsBody.cs index 26746a5ea..918849847 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Physics/PhysicsBody.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Physics/PhysicsBody.cs @@ -84,7 +84,7 @@ namespace Barotrauma } if (IsValidShape(Radius, Height, Width)) { - DrawShape(drawPosition, DrawRotation, color); + DrawShape(DrawPosition, DrawRotation, color); } if (LastServerState != null) diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EditorNode.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EditorNode.cs index 3210e1122..a890d8d0d 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EditorNode.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EditorNode.cs @@ -233,7 +233,7 @@ namespace Barotrauma protected virtual Color BackgroundColor => new Color(150, 150, 150); - private void DrawBack(SpriteBatch spriteBatch) + protected virtual void DrawBack(SpriteBatch spriteBatch) { Color outlineColor = Color.White * 0.8f; Color fontColor = Color.White; @@ -253,9 +253,19 @@ namespace Barotrauma GUI.DrawRectangle(spriteBatch, HeaderRectangle, outlineColor, isFilled: false, depth: 1.0f, thickness: (int) Math.Max(1, 1.25f / camZoom)); GUI.DrawRectangle(spriteBatch, bodyRect, outlineColor, isFilled: false, depth: 1.0f, thickness: (int) Math.Max(1, 1.25f / camZoom)); + DrawConnections(spriteBatch); + + Vector2 headerSize = GUIStyle.SubHeadingFont.MeasureString(Name); + GUIStyle.SubHeadingFont.DrawString(spriteBatch, Name, HeaderRectangle.Location.ToVector2() + (HeaderRectangle.Size.ToVector2() / 2) - (headerSize / 2), fontColor); + } + + protected virtual void DrawConnections(SpriteBatch spriteBatch) + { int x = 0, y = 0; foreach (EventEditorNodeConnection connection in Connections) { + if (!ShouldDrawConnection(connection)) { continue; } + switch (connection.Type.NodeSide) { case NodeConnectionType.Side.Left: @@ -268,9 +278,11 @@ namespace Barotrauma break; } } + } - Vector2 headerSize = GUIStyle.SubHeadingFont.MeasureString(Name); - GUIStyle.SubHeadingFont.DrawString(spriteBatch, Name, HeaderRectangle.Location.ToVector2() + (HeaderRectangle.Size.ToVector2() / 2) - (headerSize / 2), fontColor); + protected virtual bool ShouldDrawConnection(EventEditorNodeConnection connection) + { + return true; // Base implementation draws all connections } public void AddConnection(NodeConnectionType connectionType) @@ -337,6 +349,11 @@ namespace Barotrauma { private readonly Type type; + protected override Color BackgroundColor => + EventEditorScreen.ConversationMode && !IsInstanceOf(type, typeof(ConversationAction)) + ? new Color(80, 80, 80) // Darker for non-conversation nodes in conversation mode + : new Color(150, 150, 150); // Normal color + public EventNode(Type type, string name) : base(name) { this.type = type; @@ -387,8 +404,15 @@ namespace Barotrauma Type? t = Type.GetType(element.GetAttributeString("type", string.Empty)); if (t == null) { return null; } + + string name = element.GetAttributeString("name", string.Empty); + int id = element.GetAttributeInt("i", -1); - EventNode newNode = new EventNode(t, element.GetAttributeString("name", string.Empty)) { ID = element.GetAttributeInt("i", -1) }; + // Create the appropriate node type based on whether it's a conversation action + EditorNode newNode = IsInstanceOf(t, typeof(ConversationAction)) + ? new EventConversationNode(t, name) { ID = id } + : new EventNode(t, name) { ID = id }; + float posX = element.GetAttributeFloat("xpos", 0f); float posY = element.GetAttributeFloat("ypos", 0f); newNode.Position = new Vector2(posX, posY); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventConversationNode.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventConversationNode.cs new file mode 100644 index 000000000..2387cb239 --- /dev/null +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventConversationNode.cs @@ -0,0 +1,398 @@ +#nullable enable +using System; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System.Collections.Generic; + +namespace Barotrauma +{ + /// + /// Base class for event nodes that display text content and can have inner Text nodes + /// + internal abstract class EventTextDisplayNode(Type type, string name) : EventNode(type, name) + { + protected virtual bool ShowOptions => false; + + private new Rectangle HeaderRectangle + { + get + { + if (!EventEditorScreen.ConversationMode) { return base.HeaderRectangle; } + + Rectangle drawRect = GetDrawRectangle(); + return new Rectangle(Position.ToPoint(), new Point(drawRect.Width, 32)); + } + } + + public override Rectangle GetDrawRectangle() + { + if (!EventEditorScreen.ConversationMode) { return base.GetDrawRectangle(); } + + var textConnection = Connections.Find(c => string.Equals(c.Attribute, "Text", StringComparison.OrdinalIgnoreCase)); + var optionConnections = ShowOptions ? Connections.Where(c => c.Type == NodeConnectionType.Option) : Enumerable.Empty(); + + const int width = 300; + int height = 50; + + // Calculate height for text section + if (textConnection != null) + { + string textContent = GetTextContent(textConnection); + if (!string.IsNullOrEmpty(textContent) && GUIStyle.Font.Value != null) + { + string wrappedText = ToolBox.WrapText(textContent, width - 16, GUIStyle.Font.Value); + Vector2 textSize = GUIStyle.Font.MeasureString(wrappedText); + height += (int)textSize.Y + 10; + } + else + { + height += 25; + } + } + + // Calculate height for each option (only for conversation nodes) + if (ShowOptions) + { + int optionIndex = 0; + foreach (var option in optionConnections) + { + string optionText = GetOptionText(option, optionIndex); + + if (GUIStyle.Font.Value != null) + { + string wrappedOption = ToolBox.WrapText(optionText, width - 40, GUIStyle.Font.Value); + Vector2 optionSize = GUIStyle.Font.MeasureString(wrappedOption); + height += (int)optionSize.Y + 20; + } + else + { + height += 40; + } + optionIndex++; + } + } + + Rectangle rect = Rectangle; + return new Rectangle(rect.X, rect.Y, width, height); + } + + protected override void DrawBack(SpriteBatch spriteBatch) + { + if (!EventEditorScreen.ConversationMode) + { + base.DrawBack(spriteBatch); + return; + } + + Rectangle bodyRect = GetDrawRectangle(); + + // Background colors + Color headerColor = IsSelected ? new Color(100, 150, 200) : new Color(120, 170, 220); + Color bodyColor = new Color(90, 120, 150); + Color borderColor = Color.LightBlue; + + // Draw background + GUI.DrawRectangle(spriteBatch, HeaderRectangle, headerColor, isFilled: true, depth: 1.0f); + GUI.DrawRectangle(spriteBatch, bodyRect, bodyColor, isFilled: true, depth: 1.0f); + GUI.DrawRectangle(spriteBatch, HeaderRectangle, borderColor, isFilled: false, depth: 1.0f); + GUI.DrawRectangle(spriteBatch, bodyRect, borderColor, isFilled: false, depth: 1.0f); + + // Draw header text + Vector2 headerSize = GUIStyle.SubHeadingFont.MeasureString(Name); + Vector2 headerPos = HeaderRectangle.Location.ToVector2() + (HeaderRectangle.Size.ToVector2() / 2) - (headerSize / 2); + GUIStyle.SubHeadingFont.DrawString(spriteBatch, Name, headerPos, Color.White); + + // Draw text content + DrawTextContent(spriteBatch, bodyRect); + + // Let base class handle standard connections (Activate, Next, etc.) + DrawConnections(spriteBatch); + } + + protected virtual void DrawTextContent(SpriteBatch spriteBatch, Rectangle bodyRect) + { + var textConnection = Connections.Find(c => string.Equals(c.Attribute, "Text", StringComparison.OrdinalIgnoreCase)); + var optionConnections = ShowOptions ? Connections.Where(c => c.Type == NodeConnectionType.Option) : Enumerable.Empty(); + + Vector2 mousePos = Screen.Selected.Cam.ScreenToWorld(PlayerInput.MousePosition); + mousePos.Y = -mousePos.Y; + + const int padding = 8; + int currentY = bodyRect.Y + padding + 30; + + // Draw text section + if (textConnection != null) + { + string textContent = GetTextContent(textConnection); + + // Wrap text and calculate height + string wrappedText = textContent; + int textHeight = 25; + if (GUIStyle.Font.Value != null) + { + wrappedText = ToolBox.WrapText(textContent, bodyRect.Width - 24, GUIStyle.Font.Value); + Vector2 textSize = GUIStyle.Font.MeasureString(wrappedText); + textHeight = (int)textSize.Y + 10; + } + + Rectangle textRect = new Rectangle(bodyRect.X + padding, currentY, bodyRect.Width - padding * 2, textHeight); + + // background + GUI.DrawRectangle(spriteBatch, textRect, new Color(70, 100, 130), isFilled: true); + GUI.DrawRectangle(spriteBatch, textRect, Color.CornflowerBlue, isFilled: false); + + // wrapped text + Vector2 textPos = new Vector2(textRect.X + 4, textRect.Y + 4); + GUI.DrawString(spriteBatch, textPos, wrappedText, Color.Yellow, font: GUIStyle.Font); + + // tooltip + if (textRect.Contains(mousePos)) + { + string rawTextKey = GetRawTextKey(textConnection); + if (!string.IsNullOrEmpty(rawTextKey)) + { + EventEditorScreen.DrawnTooltip = rawTextKey; + } + } + + currentY += textHeight + 5; + } + + // Draw options (only for conversation nodes) + if (ShowOptions) + { + DrawOptions(spriteBatch, bodyRect, optionConnections, currentY); + } + } + + protected static void DrawOptions(SpriteBatch spriteBatch, Rectangle bodyRect, IEnumerable optionConnections, int startY) + { + Vector2 mousePos = Screen.Selected.Cam.ScreenToWorld(PlayerInput.MousePosition); + mousePos.Y = -mousePos.Y; + const int padding = 8; + int currentY = startY; + + int optionIndex = 0; + foreach (var option in optionConnections) + { + string optionText = GetOptionText(option, optionIndex); + + // Wrap option text and calculate height + string wrappedOption = optionText; + int optionHeight = 30; + if (GUIStyle.Font.Value != null) + { + wrappedOption = ToolBox.WrapText(optionText, bodyRect.Width - 40, GUIStyle.Font.Value); + Vector2 optionSize = GUIStyle.Font.MeasureString(wrappedOption); + optionHeight = (int)optionSize.Y + 16; + } + + Rectangle optionRect = new Rectangle(bodyRect.X + padding, currentY, bodyRect.Width - padding * 2, optionHeight); + + // background - red for end conversation, blue for normal + Color optionBg = option.EndConversation ? new Color(120, 80, 80) : new Color(80, 80, 120); + GUI.DrawRectangle(spriteBatch, optionRect, optionBg, isFilled: true); + GUI.DrawRectangle(spriteBatch, optionRect, Color.White, isFilled: false); + + Vector2 optionPos = new Vector2(optionRect.X + 4, optionRect.Y + 4); + GUI.DrawString(spriteBatch, optionPos, wrappedOption, Color.White, font: GUIStyle.Font); + + // tooltip + if (optionRect.Contains(mousePos)) + { + string rawOptionKey = option.OptionText ?? ""; + if (!string.IsNullOrEmpty(rawOptionKey)) + { + EventEditorScreen.DrawnTooltip = rawOptionKey; + } + } + + // connection point + Rectangle connRect = new Rectangle(bodyRect.Right - 1, optionRect.Y + optionHeight / 2 - 8, 16, 16); + GUI.DrawRectangle(spriteBatch, connRect, Color.DarkGray, isFilled: true); + GUI.DrawRectangle(spriteBatch, connRect, Color.White, isFilled: false); + option.DrawRectangle = connRect; + + // connection lines + foreach (var connected in option.ConnectedTo) + { + Vector2 start = new Vector2(connRect.Right, connRect.Center.Y); + Vector2 end = new Vector2(connected.DrawRectangle.Left, connected.DrawRectangle.Center.Y); + + float knobLength = 24; + var (points, _) = ToolBox.GetSquareLineBetweenPoints(start, end, knobLength); + + Color lineColor = GUIStyle.Red; + float lineWidth = Math.Max(2.0f, 2.0f / (Screen.Selected is EventEditorScreen eventEditor ? eventEditor.Cam.Zoom : 1.0f)); + + GUI.DrawLine(spriteBatch, points[0], points[1], lineColor, width: (int)lineWidth); + GUI.DrawLine(spriteBatch, points[1], points[2], lineColor, width: (int)lineWidth); + GUI.DrawLine(spriteBatch, points[2], points[3], lineColor, width: (int)lineWidth); + GUI.DrawLine(spriteBatch, points[3], points[4], lineColor, width: (int)lineWidth); + GUI.DrawLine(spriteBatch, points[4], points[5], lineColor, width: (int)lineWidth); + } + + currentY += optionHeight + 5; + optionIndex++; + } + } + + private static string GetOptionText(EventEditorNodeConnection option, int optionIndex) + { + string optionTextKey = option.OptionText ?? $"Option {optionIndex + 1}"; + var allVariants = TextManager.GetAll(optionTextKey); + + int variantCount = allVariants.Count(); + return variantCount switch + { + > 1 => $"[{variantCount} variants] {string.Join(" / ", allVariants)}", + 1 => allVariants.First(), + _ => optionTextKey + }; + } + + private string GetTextContent(EventEditorNodeConnection textConnection) + { + string textContent = ""; + + // First check if there's a direct text attribute + if (textConnection.OverrideValue != null) + { + textContent = textConnection.OverrideValue.ToString() ?? ""; + } + else + { + object? connectedValue = textConnection.GetValue(); + if (connectedValue != null) + { + textContent = connectedValue.ToString() ?? ""; + } + } + + // If no direct text, check for inner Text nodes via Add connections + if (string.IsNullOrEmpty(textContent)) + { + var addConnection = Connections.FirstOrDefault(c => c.Type == NodeConnectionType.Add); + if (addConnection != null && addConnection.ConnectedTo.Any()) + { + var connectedNode = addConnection.ConnectedTo.First(); + if (connectedNode.Parent?.Name == "Text") + { + // Get the text content from the connected Text node + var textNodeConnection = connectedNode.Parent.Connections.FirstOrDefault(c => + string.Equals(c.Attribute, "tag", StringComparison.OrdinalIgnoreCase)); + + if (textNodeConnection?.OverrideValue != null) + { + textContent = textNodeConnection.OverrideValue.ToString() ?? ""; + } + } + } + } + + // Translate the text if we found any + if (!string.IsNullOrEmpty(textContent)) + { + var translated = TextManager.Get(textContent); + if (translated.Loaded) + { + textContent = translated.Value; + } + } + + return textContent; + } + + private string GetRawTextKey(EventEditorNodeConnection textConnection) + { + string textKey = ""; + + // First check if there's a direct text attribute + if (textConnection.OverrideValue != null) + { + textKey = textConnection.OverrideValue.ToString() ?? ""; + } + else + { + var connectedValue = textConnection.GetValue(); + if (connectedValue != null) + { + textKey = connectedValue.ToString() ?? ""; + } + } + + // If no direct text, check for inner Text nodes via Add connections + if (string.IsNullOrEmpty(textKey)) + { + var addConnection = Connections.FirstOrDefault(c => c.Type == NodeConnectionType.Add); + if (addConnection != null && addConnection.ConnectedTo.Any()) + { + var connectedNode = addConnection.ConnectedTo.First(); + if (connectedNode.Parent?.Name == "Text") + { + // Get the text key from the connected Text node + var textNodeConnection = connectedNode.Parent.Connections.FirstOrDefault(c => + string.Equals(c.Attribute, "tag", StringComparison.OrdinalIgnoreCase)); + + if (textNodeConnection?.OverrideValue != null) + { + textKey = textNodeConnection.OverrideValue.ToString() ?? ""; + } + } + } + } + + return textKey; + } + + protected override bool ShouldDrawConnection(EventEditorNodeConnection connection) + { + if (!EventEditorScreen.ConversationMode) { return base.ShouldDrawConnection(connection); } + + // In conversation mode, exclude Options and Text since we draw them manually + // Also exclude Add connections since we hide the child Text nodes and display their content inline + return connection.Type == NodeConnectionType.Activate || + connection.Type == NodeConnectionType.Next; + } + + protected override void DrawConnections(SpriteBatch spriteBatch) + { + if (!EventEditorScreen.ConversationMode) + { + base.DrawConnections(spriteBatch); + return; + } + + // In conversation mode, use the correct rectangle for connection positioning + Rectangle correctRect = GetDrawRectangle(); + int x = 0, y = 0; + foreach (EventEditorNodeConnection connection in Connections) + { + if (!ShouldDrawConnection(connection)) { continue; } + + switch (connection.Type.NodeSide) + { + case NodeConnectionType.Side.Left: + connection.Draw(spriteBatch, correctRect, y); + y++; + break; + case NodeConnectionType.Side.Right: + connection.Draw(spriteBatch, correctRect, x); + x++; + break; + } + } + } + } + + internal class EventConversationNode(Type type, string name) : EventTextDisplayNode(type, name) + { + protected override bool ShowOptions => true; + } + + internal class EventLogNode(Type type, string name) : EventTextDisplayNode(type, name) + { + protected override bool ShowOptions => false; + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventEditorScreen.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventEditorScreen.cs index ed6a1db23..9a1a37b2f 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventEditorScreen.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/EventEditor/EventEditorScreen.cs @@ -18,6 +18,8 @@ namespace Barotrauma public override Camera Cam { get; } public static string? DrawnTooltip { get; set; } + + public static bool ConversationMode { get; set; } public static readonly List nodeList = new List(); @@ -37,6 +39,11 @@ namespace Barotrauma private LocationType? lastTestType; private GUITickBox? isTraitorEventBox; + private GUITickBox? conversationModeBox; + + private readonly LanguageIdentifier originalLanguage; + + private GUIDropDown? languageDropdown; private static int CreateID() { @@ -50,25 +57,99 @@ namespace Barotrauma { Cam = new Camera(); nodeList.Clear(); + + originalLanguage = GameSettings.CurrentConfig.Language; + CreateGUI(); } + public override void Select() + { + GUI.PreventPauseMenuToggle = false; + projectName = TextManager.Get("EventEditor.Unnamed").Value; + + UpdateLanguageDropdownSelection(); + + base.Select(); + } + + private void UpdateLanguageDropdownSelection() + { + if (languageDropdown == null) { return; } + + languageDropdown.SelectItem(GameSettings.CurrentConfig.Language); + } + + protected override void DeselectEditorSpecific() + { + // Restore the original language when leaving the editor + var config = GameSettings.CurrentConfig; + config.Language = originalLanguage; + GameSettings.SetCurrentConfig(config); + TextManager.LanguageChanged(); + } + + private static readonly HashSet hiddenNodesInConversationMode = new HashSet(); + + private static bool ShouldHideNodeInConversationMode(EditorNode node) + { + return hiddenNodesInConversationMode.Contains(node); + } + + private static void UpdateHiddenNodesInConversationMode() + { + hiddenNodesInConversationMode.Clear(); + + // Find all text display nodes (ConversationAction and EventLogAction) and mark their inner Text nodes (and descendants) as hidden + foreach (var textDisplayNode in nodeList.Where(IsEventTextDisplayNode)) + { + var addConnection = textDisplayNode.Connections.FirstOrDefault(c => c.Type == NodeConnectionType.Add); + if (addConnection != null && addConnection.ConnectedTo.Any()) + { + foreach (var connectedNode in addConnection.ConnectedTo) + { + if (connectedNode.Parent?.Name == "Text") + { + MarkNodeAndDescendantsAsHidden(connectedNode.Parent); + } + } + } + } + } + + private static bool IsEventTextDisplayNode(EditorNode node) => node is EventTextDisplayNode; + + private static void MarkNodeAndDescendantsAsHidden(EditorNode node) + { + hiddenNodesInConversationMode.Add(node); + + // Recursively mark all connected child nodes as hidden + foreach (var connection in node.Connections) + { + foreach (var connectedNode in connection.ConnectedTo) + { + if (connectedNode.Parent != null && !hiddenNodesInConversationMode.Contains(connectedNode.Parent)) + { + MarkNodeAndDescendantsAsHidden(connectedNode.Parent); + } + } + } + } + private void CreateGUI() { - GuiFrame = new GUIFrame(new RectTransform(new Vector2(0.2f, 0.4f), GUI.Canvas) { MinSize = new Point(300, 420) }); + GuiFrame = new GUIFrame(new RectTransform(new Vector2(0.2f, 0.5f), GUI.Canvas) { MinSize = new Point(300, 520) }); GUILayoutGroup layoutGroup = new GUILayoutGroup(RectTransform(0.9f, 0.9f, GuiFrame, Anchor.Center)) { Stretch = true, AbsoluteSpacing = GUI.IntScale(5) }; // === BUTTONS === // - GUILayoutGroup buttonLayout = new GUILayoutGroup(RectTransform(1.0f, 0.50f, layoutGroup)) { RelativeSpacing = 0.04f }; - GUIButton newProjectButton = new GUIButton(RectTransform(1.0f, 0.33f, buttonLayout), TextManager.Get("EventEditor.NewProject")); - GUIButton saveProjectButton = new GUIButton(RectTransform(1.0f, 0.33f, buttonLayout), TextManager.Get("EventEditor.SaveProject")); - GUIButton loadProjectButton = new GUIButton(RectTransform(1.0f, 0.33f, buttonLayout), TextManager.Get("EventEditor.LoadProject")); - GUIButton exportProjectButton = new GUIButton(RectTransform(1.0f, 0.33f, buttonLayout), TextManager.Get("EventEditor.Export")); - + GUILayoutGroup buttonLayout = new GUILayoutGroup(RectTransform(1.0f, 0.40f, layoutGroup)) { RelativeSpacing = 0.04f }; + GUIButton newProjectButton = new GUIButton(RectTransform(1.0f, 0.25f, buttonLayout), TextManager.Get("EventEditor.NewProject")); + GUIButton saveProjectButton = new GUIButton(RectTransform(1.0f, 0.25f, buttonLayout), TextManager.Get("EventEditor.SaveProject")); + GUIButton loadProjectButton = new GUIButton(RectTransform(1.0f, 0.25f, buttonLayout), TextManager.Get("EventEditor.LoadProject")); + GUIButton exportProjectButton = new GUIButton(RectTransform(1.0f, 0.25f, buttonLayout), TextManager.Get("EventEditor.Export")); // === LOAD PREFAB === // - - GUILayoutGroup loadEventLayout = new GUILayoutGroup(RectTransform(1.0f, 0.125f, layoutGroup)); + GUILayoutGroup loadEventLayout = new GUILayoutGroup(RectTransform(1.0f, 0.10f, layoutGroup)); new GUITextBlock(RectTransform(1.0f, 0.5f, loadEventLayout), TextManager.Get("EventEditor.LoadEvent"), font: GUIStyle.SubHeadingFont); GUILayoutGroup loadDropdownLayout = new GUILayoutGroup(RectTransform(1.0f, 0.5f, loadEventLayout), isHorizontal: true, childAnchor: Anchor.CenterLeft); @@ -76,8 +157,7 @@ namespace Barotrauma GUIButton loadButton = new GUIButton(RectTransform(0.2f, 1.0f, loadDropdownLayout), TextManager.Get("Load")); // === ADD ACTION === // - - GUILayoutGroup addActionLayout = new GUILayoutGroup(RectTransform(1.0f, 0.125f, layoutGroup)); + GUILayoutGroup addActionLayout = new GUILayoutGroup(RectTransform(1.0f, 0.10f, layoutGroup)); new GUITextBlock(RectTransform(1.0f, 0.5f, addActionLayout), TextManager.Get("EventEditor.AddAction"), font: GUIStyle.SubHeadingFont); GUILayoutGroup addActionDropdownLayout = new GUILayoutGroup(RectTransform(1.0f, 0.5f, addActionLayout), isHorizontal: true, childAnchor: Anchor.CenterLeft); @@ -85,7 +165,7 @@ namespace Barotrauma GUIButton addActionButton = new GUIButton(RectTransform(0.2f, 1.0f, addActionDropdownLayout), TextManager.Get("EventEditor.Add")); // === ADD VALUE === // - GUILayoutGroup addValueLayout = new GUILayoutGroup(RectTransform(1.0f, 0.125f, layoutGroup)); + GUILayoutGroup addValueLayout = new GUILayoutGroup(RectTransform(1.0f, 0.10f, layoutGroup)); new GUITextBlock(RectTransform(1.0f, 0.5f, addValueLayout), TextManager.Get("EventEditor.AddValue"), font: GUIStyle.SubHeadingFont); GUILayoutGroup addValueDropdownLayout = new GUILayoutGroup(RectTransform(1.0f, 0.5f, addValueLayout), isHorizontal: true, childAnchor: Anchor.CenterLeft); @@ -93,7 +173,7 @@ namespace Barotrauma GUIButton addValueButton = new GUIButton(RectTransform(0.2f, 1.0f, addValueDropdownLayout), TextManager.Get("EventEditor.Add")); // === ADD SPECIAL === // - GUILayoutGroup addSpecialLayout = new GUILayoutGroup(RectTransform(1.0f, 0.125f, layoutGroup)); + GUILayoutGroup addSpecialLayout = new GUILayoutGroup(RectTransform(1.0f, 0.10f, layoutGroup)); new GUITextBlock(RectTransform(1.0f, 0.5f, addSpecialLayout), TextManager.Get("EventEditor.AddSpecial"), font: GUIStyle.SubHeadingFont); GUILayoutGroup addSpecialDropdownLayout = new GUILayoutGroup(RectTransform(1.0f, 0.5f, addSpecialLayout), isHorizontal: true, childAnchor: Anchor.CenterLeft); GUIDropDown addSpecialDropdown = new GUIDropDown(RectTransform(0.8f, 1.0f, addSpecialDropdownLayout), elementCount: 1); @@ -156,7 +236,45 @@ namespace Barotrauma return true; }; - isTraitorEventBox = new GUITickBox(RectTransform(1.0f, 0.125f, layoutGroup), "Traitor event"); + isTraitorEventBox = new GUITickBox(RectTransform(1.0f, 0.10f, layoutGroup), "Traitor event"); + + // === CONVERSATION MODE CHECKBOX === // + conversationModeBox = new GUITickBox(RectTransform(1.0f, 0.10f, layoutGroup), "Conversation Mode"); + conversationModeBox.Selected = ConversationMode; + conversationModeBox.OnSelected = box => + { + ConversationMode = !ConversationMode; + UpdateHiddenNodesInConversationMode(); + return true; + }; + + // === LANGUAGE SELECTION === // + GUILayoutGroup languageLayout = new GUILayoutGroup(RectTransform(1.0f, 0.10f, layoutGroup)); + new GUITextBlock(RectTransform(1.0f, 0.5f, languageLayout), TextManager.Get("Language"), font: GUIStyle.SubHeadingFont); + + var languages = TextManager.AvailableLanguages + .OrderBy(l => TextManager.GetTranslatedLanguageName(l).ToIdentifier()); + + languageDropdown = new GUIDropDown(RectTransform(1.0f, 0.5f, languageLayout), elementCount: 10); + foreach (var language in languages) + { + languageDropdown.AddItem(TextManager.GetTranslatedLanguageName(language), language); + } + + // Select current language + languageDropdown.SelectItem(GameSettings.CurrentConfig.Language); + + languageDropdown.OnSelected = (component, userData) => + { + if (userData is LanguageIdentifier selectedLanguage) + { + var config = GameSettings.CurrentConfig; + config.Language = selectedLanguage; + GameSettings.SetCurrentConfig(config); + TextManager.LanguageChanged(); + } + return true; + }; screenResolution = new Point(GameMain.GraphicsWidth, GameMain.GraphicsHeight); } @@ -323,6 +441,9 @@ namespace Barotrauma { GUI.NotifyPrompt(TextManager.Get("EventEditor.RandomGenerationHeader"), TextManager.Get("EventEditor.RandomGenerationBody")); } + + // Update hidden nodes after loading + UpdateHiddenNodesInConversationMode(); return true; }); return true; @@ -334,7 +455,22 @@ namespace Barotrauma Vector2 spawnPos = Cam.WorldViewCenter; spawnPos.Y = -spawnPos.Y; - EventNode newNode = new EventNode(type, type.Name) { ID = CreateID() }; + + // Create the appropriate node type based on the action type + EditorNode newNode; + if (EditorNode.IsInstanceOf(type, typeof(ConversationAction))) + { + newNode = new EventConversationNode(type, type.Name) { ID = CreateID() }; + } + else if (EditorNode.IsInstanceOf(type, typeof(EventLogAction))) + { + newNode = new EventLogNode(type, type.Name) { ID = CreateID() }; + } + else + { + newNode = new EventNode(type, type.Name) { ID = CreateID() }; + } + newNode.Position = spawnPos - newNode.Size / 2; nodeList.Add(newNode); return true; @@ -399,7 +535,19 @@ namespace Barotrauma Type? t = Type.GetType($"Barotrauma.{subElement.Name}"); if (t != null && EditorNode.IsInstanceOf(t, typeof(EventAction))) { - newNode = new EventNode(t, subElement.Name.ToString()) { Position = new Vector2(ident, 0), ID = CreateID() }; + // Create the appropriate node type based on the action type + if (EditorNode.IsInstanceOf(t, typeof(ConversationAction))) + { + newNode = new EventConversationNode(t, subElement.Name.ToString()) { Position = new Vector2(ident, 0), ID = CreateID() }; + } + else if (EditorNode.IsInstanceOf(t, typeof(EventLogAction))) + { + newNode = new EventLogNode(t, subElement.Name.ToString()) { Position = new Vector2(ident, 0), ID = CreateID() }; + } + else + { + newNode = new EventNode(t, subElement.Name.ToString()) { Position = new Vector2(ident, 0), ID = CreateID() }; + } } else { @@ -547,13 +695,6 @@ namespace Barotrauma return new RectTransform(new Vector2(x, y), parent.RectTransform, anchor); } - public override void Select() - { - GUI.PreventPauseMenuToggle = false; - projectName = TextManager.Get("EventEditor.Unnamed").Value; - base.Select(); - } - public override void AddToGUIUpdateList() { GuiFrame.AddToGUIUpdateList(); @@ -671,6 +812,7 @@ namespace Barotrauma private static void Load(XElement saveElement) { nodeList.Clear(); + hiddenNodesInConversationMode.Clear(); projectName = saveElement.GetAttributeString("name", TextManager.Get("EventEditor.Unnamed").Value); foreach (XElement element in saveElement.Elements()) { @@ -702,6 +844,9 @@ namespace Barotrauma } } } + + // Update hidden nodes after loading + UpdateHiddenNodesInConversationMode(); } private static void CreateContextMenu(EditorNode node, EventEditorNodeConnection? connection = null) @@ -971,21 +1116,25 @@ namespace Barotrauma foreach (EditorNode node in nodeList.Where(node => node is SpecialNode)) { + if (ConversationMode && ShouldHideNodeInConversationMode(node)) { continue; } node.Draw(spriteBatch); } // Render value nodes below event nodes foreach (EditorNode node in nodeList.Where(node => node is ValueNode)) { + if (ConversationMode && ShouldHideNodeInConversationMode(node)) { continue; } node.Draw(spriteBatch); } foreach (EditorNode node in nodeList.Where(node => node is EventNode)) { + if (ConversationMode && ShouldHideNodeInConversationMode(node)) { continue; } node.Draw(spriteBatch); } - + draggedNode?.Draw(spriteBatch); + foreach (var (node, _) in markedNodes) { node.Draw(spriteBatch); @@ -1013,6 +1162,11 @@ namespace Barotrauma CreateGUI(); } + if (PlayerInput.KeyHit(Keys.R) && PlayerInput.KeyDown(Keys.LeftShift)) + { + CreateGUI(); + } + Cam.MoveCamera((float) deltaTime, allowMove: true, allowZoom: GUI.MouseOn == null); Vector2 mousePos = Cam.ScreenToWorld(PlayerInput.MousePosition); mousePos.Y = -mousePos.Y; diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/GameScreen.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/GameScreen.cs index 7eeb3526e..1e9dee31e 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/GameScreen.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/GameScreen.cs @@ -319,14 +319,21 @@ namespace Barotrauma graphics.Clear(Color.Transparent); DamageEffect.CurrentTechnique = DamageEffect.Techniques["StencilShader"]; - DamageEffect.CurrentTechnique.Passes[0].Apply(); - spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.LinearWrap, effect: DamageEffect, transformMatrix: cam.Transform); + //reset so any parameters left over from previous usages of the shader don't persist + ResetDamageEffect(); + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, effect: DamageEffect, transformMatrix: cam.Transform); Submarine.DrawDamageable(spriteBatch, DamageEffect, false); - DamageEffect.Parameters["aCutoff"].SetValue(0.0f); - DamageEffect.Parameters["cCutoff"].SetValue(0.0f); - Submarine.DamageEffectCutoff = 0.0f; - DamageEffect.CurrentTechnique.Passes[0].Apply(); spriteBatch.End(); + //reset so parameters set in DrawDamageable don't persist + ResetDamageEffect(); + + void ResetDamageEffect() + { + DamageEffect.Parameters["aCutoff"].SetValue(0.0f); + DamageEffect.Parameters["cCutoff"].SetValue(0.0f); + Submarine.DamageEffectCutoff = 0.0f; + DamageEffect.CurrentTechnique.Passes[0].Apply(); + } sw.Stop(); GameMain.PerformanceCounter.AddElapsedTicks("Draw:Map:FrontDamageable", sw.ElapsedTicks); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/NetLobbyScreen.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/NetLobbyScreen.cs index a2fb9b852..629d8ea27 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/NetLobbyScreen.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/NetLobbyScreen.cs @@ -2090,7 +2090,10 @@ namespace Barotrauma }; serverLogReverseButton = new GUIButton(new RectTransform(new Vector2(1.0f, 0.05f), serverLogListboxLayout.RectTransform), style: "UIToggleButtonVertical"); - serverLogBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.95f), serverLogListboxLayout.RectTransform)); + serverLogBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.95f), serverLogListboxLayout.RectTransform)) + { + AutoHideScrollBar = false + }; //filter tickbox list ------------------------------------------------------------------ @@ -2198,7 +2201,7 @@ namespace Barotrauma OnClicked = (btn, obj) => { if (GameMain.Client == null) { return true; } - GUI.CreateVerificationPrompt(GameMain.GameSession.GameMode is CampaignMode ? "PauseMenuReturnToServerLobbyVerification" : "EndRoundSubNotAtLevelEnd", + GUI.CreateVerificationPrompt(GameMain.GameSession?.GameMode is CampaignMode ? "PauseMenuReturnToServerLobbyVerification" : "EndRoundSubNotAtLevelEnd", () => { GameMain.Client?.RequestEndRound(save: false); @@ -3304,11 +3307,17 @@ namespace Barotrauma VoteType voteType; if (component.Parent == GameMain.NetLobbyScreen.SubList.Content) { - if (SelectedMode == GameModePreset.PvP && MultiplayerPreferences.Instance.TeamPreference is not (CharacterTeamType.Team1 or CharacterTeamType.Team2)) + if (SelectedMode == GameModePreset.PvP && + MultiplayerPreferences.Instance.TeamPreference is not (CharacterTeamType.Team1 or CharacterTeamType.Team2)) { + if (TeamPreferenceListBox == null) + { + //refresh player frame to ensure we create the team preference list box + UpdatePlayerFrame(characterInfo: GameMain.Client?.CharacterInfo); + } + // we are in PvP but don't have a team selected, so we can't select a sub // and also highlight the team selection list - foreach (GUIComponent child in TeamPreferenceListBox.Content.Children) { if (child.UserData is CharacterTeamType.None) { continue; } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/SlideshowPlayer.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/SlideshowPlayer.cs index 458614977..c73d5eeb0 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/SlideshowPlayer.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/SlideshowPlayer.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework; +using Barotrauma.Extensions; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Linq; @@ -42,6 +43,8 @@ namespace Barotrauma protected override void Update(float deltaTime) { + if (slideshowPrefab.Slides.IsEmpty) { return; } + var slide = slideshowPrefab.Slides[Math.Min(state, slideshowPrefab.Slides.Length - 1)]; if (!Visible || (Finished && timer > slide.FadeOutDuration)) { return; } @@ -104,6 +107,7 @@ namespace Barotrauma private void RefreshText() { + if (slideshowPrefab.Slides.IsEmpty) { return; } var slide = slideshowPrefab.Slides[Math.Min(state, slideshowPrefab.Slides.Length - 1)]; currentText = slide.Text .Replace("[submarine]", Submarine.MainSub?.Info.Name ?? GameMain.GameSession?.SubmarineInfo?.Name ?? "Unknown") diff --git a/Barotrauma/BarotraumaClient/ClientSource/Screens/SubEditorScreen.cs b/Barotrauma/BarotraumaClient/ClientSource/Screens/SubEditorScreen.cs index cd3420642..256c6bbbf 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Screens/SubEditorScreen.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Screens/SubEditorScreen.cs @@ -2088,7 +2088,7 @@ namespace Barotrauma if (packageToSaveTo != null) { var modProject = new ModProject(packageToSaveTo); - var fileListPath = packageToSaveTo.Path; + string fileListPath = packageToSaveTo.Path; if (packageToSaveTo == ContentPackageManager.VanillaCorePackage) { #if !DEBUG @@ -2104,10 +2104,12 @@ namespace Barotrauma SubmarineType.Wreck => "Content/Map/Wrecks/{0}", SubmarineType.BeaconStation => "Content/Map/BeaconStations/{0}", SubmarineType.EnemySubmarine => "Content/Map/EnemySubmarines/{0}", - SubmarineType.OutpostModule => MainSub.Info.FilePath.Contains("RuinModules") ? "Content/Map/RuinModules/{0}" : "Content/Map/Outposts/{0}", + SubmarineType.OutpostModule => MainSub.Info.FilePath != null && MainSub.Info.FilePath.Contains("RuinModules") ? "Content/Map/RuinModules/{0}" : "Content/Map/Outposts/{0}", _ => throw new InvalidOperationException() }, savePath); modProject.ModVersion = ""; + addSubAndSave(modProject, savePath, fileListPath); + return true; } else { @@ -2116,27 +2118,41 @@ namespace Barotrauma if (existingFilePath != null) { savePath = existingFilePath; + addSubAndSave(modProject, savePath, fileListPath); + return true; } //otherwise make sure we're not trying to overwrite another sub in the same package else { - savePath = Path.Combine(packageToSaveTo.Dir, savePath); - if (File.Exists(savePath)) + var existingSubInContentPackage = + SubmarineInfo.SavedSubmarines.FirstOrDefault(s => s.Type == MainSub?.Info?.Type && packageToSaveTo.GetFiles().Any(f => f.Path == s.FilePath)); + if (existingSubInContentPackage != null) { - var verification = new GUIMessageBox(TextManager.Get("warning"), TextManager.Get("subeditor.duplicatesubinpackage"), - new LocalizedString[] { TextManager.Get("yes"), TextManager.Get("no") }); + string directoryName = Path.GetDirectoryName(existingSubInContentPackage.FilePath); + string directoryNameRelativeToPackage = Path.GetRelativePath(Path.GetDirectoryName(packageToSaveTo.Path), directoryName); + var verification = new GUIMessageBox(string.Empty, TextManager.GetWithVariable("subeditor.saveinexistingfolderprompt", "[folder]", directoryNameRelativeToPackage), + [TextManager.GetWithVariable("subeditor.saveinexistingfolderprompt.yes", "[folder]", directoryNameRelativeToPackage), TextManager.Get("subeditor.saveinexistingfolderprompt.no")]); verification.Buttons[0].OnClicked = (_, _) => { - addSubAndSave(modProject, savePath, fileListPath); + savePath = Path.Combine(directoryNameRelativeToPackage, savePath); + trySaveWithDuplicateCheck(modProject, fileListPath); + verification.Close(); + return true; + }; verification.Buttons[1].OnClicked = (_, _) => + { + trySaveWithDuplicateCheck(modProject, fileListPath); verification.Close(); return true; }; - verification.Buttons[1].OnClicked = verification.Close; - return false; + return true; + } + else + { + trySaveWithDuplicateCheck(modProject, fileListPath); + return true; } } } - addSubAndSave(modProject, savePath, fileListPath); } else { @@ -2150,9 +2166,28 @@ namespace Barotrauma { ModProject modProject = new ModProject { Name = name }; addSubAndSave(modProject, savePath, Path.Combine(Path.GetDirectoryName(savePath), ContentPackage.FileListFileName)); + return true; } } + void trySaveWithDuplicateCheck(ModProject modProject, string fileListPath) + { + savePath = Path.Combine(packageToSaveTo.Dir, savePath); + if (File.Exists(savePath)) + { + var verification = new GUIMessageBox(TextManager.Get("warning"), TextManager.Get("subeditor.duplicatesubinpackage"), + [TextManager.Get("yes"), TextManager.Get("no")]); + verification.Buttons[0].OnClicked = (_, _) => + { + addSubAndSave(modProject, savePath, fileListPath); + verification.Close(); + return true; + }; + verification.Buttons[1].OnClicked = verification.Close; + } + addSubAndSave(modProject, savePath, fileListPath); + } + void addSubAndSave(ModProject modProject, string filePath, string packagePath) { filePath = filePath.CleanUpPath(); @@ -2234,8 +2269,6 @@ namespace Barotrauma subNameLabel.Text = ToolBox.LimitString(MainSub.Info.Name, subNameLabel.Font, subNameLabel.Rect.Width); } } - - return false; } private void CreateSaveScreen(bool quickSave = false) @@ -2653,13 +2686,15 @@ namespace Barotrauma //--------------------------------------- - var extraSettingsContainer = new GUILayoutGroup(new RectTransform(new Vector2(1, 0.5f), subTypeDependentSettingFrame.RectTransform)) + var extraSettingsContainer = new GUILayoutGroup(new RectTransform(new Vector2(1, 0.75f), subTypeDependentSettingFrame.RectTransform)) { CanBeFocused = true, Visible = false, Stretch = true }; + var extraSubInfo = GetExtraSubmarineInfo(MainSub?.Info); + var minDifficultyGroup = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.25f), extraSettingsContainer.RectTransform), isHorizontal: true) { Stretch = true @@ -2668,12 +2703,12 @@ namespace Barotrauma TextManager.Get("minleveldifficulty"), textAlignment: Alignment.CenterLeft, wrap: true); var numInput = new GUINumberInput(new RectTransform(new Vector2(0.4f, 1.0f), minDifficultyGroup.RectTransform), NumberType.Int) { - IntValue = (int)(MainSub?.Info?.GetExtraSubmarineInfo?.MinLevelDifficulty ?? 0), + IntValue = (int)(extraSubInfo?.MinLevelDifficulty ?? 0), MinValueInt = 0, MaxValueInt = 100, OnValueChanged = (numberInput) => { - MainSub.Info.GetExtraSubmarineInfo.MinLevelDifficulty = numberInput.IntValue; + extraSubInfo.MinLevelDifficulty = numberInput.IntValue; } }; minDifficultyGroup.RectTransform.MaxSize = numInput.TextBox.RectTransform.MaxSize; @@ -2685,16 +2720,17 @@ namespace Barotrauma TextManager.Get("maxleveldifficulty"), textAlignment: Alignment.CenterLeft, wrap: true); numInput = new GUINumberInput(new RectTransform(new Vector2(0.4f, 1.0f), maxDifficultyGroup.RectTransform), NumberType.Int) { - IntValue = (int)(MainSub?.Info?.GetExtraSubmarineInfo?.MaxLevelDifficulty ?? 100), + IntValue = (int)(extraSubInfo?.MaxLevelDifficulty ?? 100), MinValueInt = 0, MaxValueInt = 100, OnValueChanged = (numberInput) => { - MainSub.Info.GetExtraSubmarineInfo.MaxLevelDifficulty = numberInput.IntValue; + extraSubInfo.MaxLevelDifficulty = numberInput.IntValue; } }; maxDifficultyGroup.RectTransform.MaxSize = numInput.TextBox.RectTransform.MaxSize; + GUITextBox missionTagsBox = CreateMissionTagsUI(extraSettingsContainer, extraSubInfo?.MissionTags ?? Enumerable.Empty(), ChangeMissionTags); //--------------------------------------- @@ -2759,15 +2795,13 @@ namespace Barotrauma triggerMissionTagsGroup.RectTransform.MaxSize = triggerMissionTagsBox.RectTransform.MaxSize; //--------------------------------------- - var enemySubmarineSettingsContainer = new GUILayoutGroup(new RectTransform(Vector2.One, subTypeDependentSettingFrame.RectTransform)) + var enemySubmarineSettingsContainer = new GUILayoutGroup(new RectTransform(Vector2.One, extraSettingsContainer.RectTransform)) { CanBeFocused = true, Visible = false, Stretch = true }; - // ------------------- - var enemySubmarineRewardGroup = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.25f), enemySubmarineSettingsContainer.RectTransform), isHorizontal: true) { Stretch = true @@ -2802,26 +2836,6 @@ namespace Barotrauma } }; enemySubmarineDifficultyGroup.RectTransform.MaxSize = numInput.TextBox.RectTransform.MaxSize; - var enemySubmarineTagsGroup = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.25f), enemySubmarineSettingsContainer.RectTransform), isHorizontal: true) - { - Stretch = true - }; - new GUITextBlock(new RectTransform(new Vector2(0.6f, 1.0f), enemySubmarineTagsGroup.RectTransform), - TextManager.Get("sp.item.tags.name"), textAlignment: Alignment.CenterLeft, wrap: true); - var tagsBox = new GUITextBox(new RectTransform(new Vector2(0.4f, 1.0f), enemySubmarineTagsGroup.RectTransform)) - { - OnEnterPressed = ChangeEnemySubTags, - OverflowClip = true, - Text = "default" - }; - tagsBox.OnDeselected += (textbox, _) => ChangeEnemySubTags(textbox, textbox.Text); - if (MainSub?.Info?.EnemySubmarineInfo?.MissionTags != null) - { - tagsBox.Text = string.Join(',', MainSub.Info.EnemySubmarineInfo.MissionTags); - } - - enemySubmarineTagsGroup.RectTransform.MaxSize = tagsBox.RectTransform.MaxSize; - enemySubmarineSettingsContainer.RectTransform.MinSize = new Point(0, enemySubmarineSettingsContainer.RectTransform.Children.Sum(c => c.Children.Any() ? c.Children.Max(c2 => c2.MinSize.Y) : 0)); //-------------------------------------------------------- @@ -2870,7 +2884,6 @@ namespace Barotrauma return true; } }; - beaconSettingsContainer.RectTransform.MinSize = new Point(0, beaconSettingsContainer.RectTransform.Children.Sum(c => c.Children.Any() ? c.Children.Max(c2 => c2.MinSize.Y) : 0)); //------------------------------------------------------------------ @@ -3110,13 +3123,26 @@ namespace Barotrauma { MainSub.Info.EnemySubmarineInfo ??= new EnemySubmarineInfo(MainSub.Info); } + + // Update mission tags UI when submarine type changes + var newExtraSubInfo = GetExtraSubmarineInfo(MainSub.Info); + if (newExtraSubInfo != null) + { + missionTagsBox.Text = string.Join(',', newExtraSubInfo.MissionTags); + } + previewImageButtonHolder.Children.ForEach(c => c.Enabled = MainSub.Info.AllowPreviewImage); outpostModuleSettingsContainer.Visible = type == SubmarineType.OutpostModule; - extraSettingsContainer.Visible = type == SubmarineType.BeaconStation || type == SubmarineType.Wreck; + extraSettingsContainer.Visible = newExtraSubInfo != null; beaconSettingsContainer.Visible = type == SubmarineType.BeaconStation; + beaconSettingsContainer.IgnoreLayoutGroups = !beaconSettingsContainer.Visible; enemySubmarineSettingsContainer.Visible = type == SubmarineType.EnemySubmarine; + enemySubmarineSettingsContainer.IgnoreLayoutGroups = !enemySubmarineSettingsContainer.Visible; subSettingsContainer.Visible = type == SubmarineType.Player; outpostSettingsContainer.Visible = type == SubmarineType.Outpost; + + extraSettingsContainer.Recalculate(); + return true; }; subSettingsContainer.RectTransform.MinSize = new Point(0, subSettingsContainer.RectTransform.Children.Sum(c => c.Children.Any() ? c.Children.Max(c2 => c2.MinSize.Y) : 0)); @@ -3412,6 +3438,18 @@ namespace Barotrauma if (quickSave) { SaveSub(packageToSaveInList.SelectedData as ContentPackage); } } + private static ExtraSubmarineInfo GetExtraSubmarineInfo(SubmarineInfo subInfo) + { + if (subInfo == null) { return null; } + return subInfo.Type switch + { + SubmarineType.BeaconStation => subInfo.BeaconStationInfo, + SubmarineType.Wreck => subInfo.WreckInfo, + SubmarineType.EnemySubmarine => subInfo.EnemySubmarineInfo, + _ => null, + }; + } + private void CreateSaveAssemblyScreen() { SetMode(Mode.Default); @@ -4948,29 +4986,46 @@ namespace Barotrauma return true; } - private bool ChangeEnemySubTags(GUITextBox textBox, string text) + private bool ChangeMissionTags(GUITextBox textBox, string text) { - if (string.IsNullOrWhiteSpace(text)) - { - textBox.Flash(GUIStyle.Red); - return false; - } + // Get the ExtraSubmarineInfo (all types inherit MissionTags from the parent class) + var extraSubInfo = GetExtraSubmarineInfo(MainSub?.Info); - if (MainSub.Info.EnemySubmarineInfo is { } enemySubInfo) + if (extraSubInfo?.MissionTags != null) { - enemySubInfo.MissionTags.Clear(); + extraSubInfo.MissionTags.Clear(); string[] tags = text.Split(','); foreach (string tag in tags) { - enemySubInfo.MissionTags.Add(tag.ToIdentifier()); + extraSubInfo.MissionTags.Add(tag.ToIdentifier()); } } + textBox.Text = text; textBox.Flash(GUIStyle.Green); - return true; } + private static GUITextBox CreateMissionTagsUI(GUIComponent parent, IEnumerable missionTags, GUITextBox.OnEnterHandler onEnterPressed) + { + var missionTagsGroup = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.25f), parent.RectTransform), isHorizontal: true) + { + Stretch = true + }; + new GUITextBlock(new RectTransform(new Vector2(0.6f, 1.0f), missionTagsGroup.RectTransform), + TextManager.Get("subeditor.missiontags"), textAlignment: Alignment.CenterLeft, wrap: true); + var tagsBox = new GUITextBox(new RectTransform(new Vector2(0.4f, 1.0f), missionTagsGroup.RectTransform)) + { + ToolTip = TextManager.Get("subeditor.missiontags.tooltip"), + OnEnterPressed = onEnterPressed, + OverflowClip = true, + Text = missionTags != null ? string.Join(',', missionTags) : "" + }; + tagsBox.OnDeselected += (textbox, _) => onEnterPressed(textbox, textbox.Text); + missionTagsGroup.RectTransform.MaxSize = tagsBox.RectTransform.MaxSize; + return tagsBox; + } + private void ChangeSubDescription(GUITextBox textBox, string text) { if (MainSub != null) @@ -5851,7 +5906,8 @@ namespace Barotrauma if (entity is Item item && item.Components.Any(ic => ic is not ConnectionPanel && ic is not Repairable && ic.GuiFrame != null)) { var container = item.GetComponents().ToList(); - if (!container.Any() || container.Any(ic => ic?.DrawInventory ?? false)) + if (container.None() || container.Any(ic => ic?.DrawInventory ?? false) || + item.GetComponent() != null) { OpenItem(item); break; diff --git a/Barotrauma/BarotraumaClient/ClientSource/SpamServerFilter.cs b/Barotrauma/BarotraumaClient/ClientSource/SpamServerFilter.cs index c2f07cf96..9a7fceda2 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/SpamServerFilter.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/SpamServerFilter.cs @@ -36,11 +36,18 @@ namespace Barotrauma { public bool IsFiltered(ServerInfo info) { - if (!Filters.Any()) { return false; } + if (Filters.IsEmpty) { return false; } foreach (var (type, value) in Filters) { - if (!IsFiltered(info, type, value)) { return false; } + try + { + if (!IsFiltered(info, type, value)) { return false; } + } + catch (Exception e) + { + DebugConsole.ThrowError($"Failed to check filter type {type} on the server info {(info.ServerName ?? "null")}.", e); + } } return true; @@ -63,7 +70,9 @@ namespace Barotrauma SpamServerFilterType.MessageEquals => CompareEquals(desc, value), SpamServerFilterType.MessageContains => CompareContains(desc, value), - SpamServerFilterType.Endpoint => info.Endpoints.First().StringRepresentation.Equals(value, StringComparison.OrdinalIgnoreCase), + SpamServerFilterType.Endpoint => + info.Endpoints != null && + info.Endpoints.First().StringRepresentation.Equals(value, StringComparison.OrdinalIgnoreCase), SpamServerFilterType.PlayerCountLarger => info.PlayerCount > parsedInt, SpamServerFilterType.PlayerCountExact => info.PlayerCount == parsedInt, @@ -79,10 +88,23 @@ namespace Barotrauma }; static bool CompareEquals(string a, string b) - => a.Equals(b, StringComparison.OrdinalIgnoreCase) || Homoglyphs.Compare(a, b); + { + if (a == null || b == null) + { + return a == b; + } + return a.Equals(b, StringComparison.OrdinalIgnoreCase) || Homoglyphs.Compare(a, b); + + } static bool CompareContains(string a, string b) - => a.Contains(b, StringComparison.OrdinalIgnoreCase); + { + if (a == null || b == null) + { + return a == b; + } + return a.Contains(b, StringComparison.OrdinalIgnoreCase); + } } public XElement Serialize() diff --git a/Barotrauma/BarotraumaClient/ClientSource/Utils/P2POwnerDoSProtection.cs b/Barotrauma/BarotraumaClient/ClientSource/Utils/P2POwnerDoSProtection.cs new file mode 100644 index 000000000..6bb34f26e --- /dev/null +++ b/Barotrauma/BarotraumaClient/ClientSource/Utils/P2POwnerDoSProtection.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Barotrauma.Networking; + +namespace Barotrauma +{ + internal sealed class P2POwnerDoSProtection + { + /// + /// Delegate to be called when a client has sent too many packets in a short time. + /// + /// The endpoint of the client. + /// A suggestion to ban the client due to too many kicks. + public delegate void ExcessivePacketDelegate(P2PEndpoint endpoint, bool shouldBan); + + private readonly Dictionary packetCounts = new(); + private readonly Dictionary kicksByEndpoint = new(); + + private readonly ExcessivePacketDelegate onExcessivePackets; + private double nextCheckTime; + + // check every 10 seconds + private const int PacketCheckTimer = 10; + + public P2POwnerDoSProtection(ExcessivePacketDelegate onExcessivePackets) + { + this.onExcessivePackets = onExcessivePackets; + nextCheckTime = Timing.TotalTime + PacketCheckTimer; + } + + private static int MaxPacketCount + { + get + { + // Normally the packet limit is per second, but we want to check faster than that. + // multiply by 1.2 to allow for some leeway to allow the DoS protection deeper in the stack + // to handle this first. + const float limitMultiplier = (PacketCheckTimer / 60f) * 1.2f; + + if (GameMain.Client?.ServerSettings is not { } serverSettings) + { + // Shouldn't happen, but just in case. + return (int)MathF.Ceiling(ServerSettings.PacketLimitDefault * limitMultiplier); + } + + return (int)MathF.Ceiling(serverSettings.MaxPacketAmount * MathF.Max(serverSettings.TickRate / (float)ServerSettings.DefaultTickRate, 1f) * limitMultiplier); + } + } + + private static bool ShouldCheck() + { + if (GameMain.Client?.ServerSettings is { } serverSettings) + { + return serverSettings.EnableDoSProtection && serverSettings.MaxPacketAmount > ServerSettings.PacketLimitMin; + } + + return false; + } + + public void OnPacket(P2PEndpoint endpoint) + { + if (!ShouldCheck()) { return; } + + // count = default(int), if the endpoint is not in the dictionary + packetCounts.TryGetValue(endpoint, out int count); + packetCounts[endpoint] = ++count; + + if (Timing.TotalTime > nextCheckTime) + { + foreach (P2PEndpoint e in packetCounts.Keys.ToArray()) + { + CheckForExcessivePackets(e, count); + } + packetCounts.Clear(); + nextCheckTime = Timing.TotalTime + PacketCheckTimer; + } + } + + private void CheckForExcessivePackets(P2PEndpoint endpoint, int count) + { + if (count > MaxPacketCount) + { + kicksByEndpoint.TryGetValue(endpoint, out int kickCount); + kicksByEndpoint[endpoint] = ++kickCount; + + onExcessivePackets(endpoint, kickCount > 3); + + packetCounts.Remove(endpoint); + } + } + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaClient/LinuxClient.csproj b/Barotrauma/BarotraumaClient/LinuxClient.csproj index 14dd9e549..dfc77450d 100644 --- a/Barotrauma/BarotraumaClient/LinuxClient.csproj +++ b/Barotrauma/BarotraumaClient/LinuxClient.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2024 AnyCPU;x64 Barotrauma diff --git a/Barotrauma/BarotraumaClient/MacClient.csproj b/Barotrauma/BarotraumaClient/MacClient.csproj index ebf575985..011b3992c 100644 --- a/Barotrauma/BarotraumaClient/MacClient.csproj +++ b/Barotrauma/BarotraumaClient/MacClient.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2024 AnyCPU;x64 Barotrauma diff --git a/Barotrauma/BarotraumaClient/WindowsClient.csproj b/Barotrauma/BarotraumaClient/WindowsClient.csproj index 7787e4d7c..0ef2232a4 100644 --- a/Barotrauma/BarotraumaClient/WindowsClient.csproj +++ b/Barotrauma/BarotraumaClient/WindowsClient.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2024 AnyCPU;x64 Barotrauma diff --git a/Barotrauma/BarotraumaServer/LinuxServer.csproj b/Barotrauma/BarotraumaServer/LinuxServer.csproj index 9d39aaae1..68d267482 100644 --- a/Barotrauma/BarotraumaServer/LinuxServer.csproj +++ b/Barotrauma/BarotraumaServer/LinuxServer.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma Dedicated Server - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2023 AnyCPU;x64 DedicatedServer diff --git a/Barotrauma/BarotraumaServer/MacServer.csproj b/Barotrauma/BarotraumaServer/MacServer.csproj index e36baf22a..fd8876d56 100644 --- a/Barotrauma/BarotraumaServer/MacServer.csproj +++ b/Barotrauma/BarotraumaServer/MacServer.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma Dedicated Server - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2023 AnyCPU;x64 DedicatedServer diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index 13f248b8e..74b208a47 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -1850,6 +1850,10 @@ namespace Barotrauma HandleCommandForCrewOrSingleCharacter(args, ToggleGodMode, client); void ToggleGodMode(Character targetCharacter) { + if (args.Length > 1 && bool.TryParse(args[1], out bool removeafflictions)) + { + if (removeafflictions) { targetCharacter.CharacterHealth.RemoveAllAfflictions(); } + } targetCharacter.GodMode = godmodeStateOnFirstCharacter ?? !targetCharacter.GodMode; godmodeStateOnFirstCharacter = targetCharacter.GodMode; GameMain.NetworkMember.CreateEntityEvent(targetCharacter, new Character.CharacterStatusEventData()); diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameSession/GameModes/MultiPlayerCampaign.cs b/Barotrauma/BarotraumaServer/ServerSource/GameSession/GameModes/MultiPlayerCampaign.cs index 0d8d1793f..7adb32f32 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameSession/GameModes/MultiPlayerCampaign.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameSession/GameModes/MultiPlayerCampaign.cs @@ -1530,9 +1530,12 @@ namespace Barotrauma modeElement.Add(GameMain.GameSession?.EventManager.Save()); } - foreach (Identifier unlockedRecipe in GameMain.GameSession.UnlockedRecipes) + foreach ((CharacterTeamType team, Identifier unlockedRecipe) in GameMain.GameSession.UnlockedRecipes) { - modeElement.Add(new XElement("unlockedrecipe", new XAttribute("identifier", unlockedRecipe))); + modeElement.Add( + new XElement("unlockedrecipe", + new XAttribute("identifier", unlockedRecipe), + new XAttribute("team", team))); } CampaignMetadata?.Save(modeElement); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Machines/Pump.cs b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Machines/Pump.cs index a026feea0..854291a36 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Machines/Pump.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Machines/Pump.cs @@ -7,7 +7,7 @@ namespace Barotrauma.Items.Components const float NetworkUpdateInterval = 5.0f; private float networkUpdateTimer; - partial void UpdateProjSpecific(float deltaTime) + partial void UpdateNetworking(float deltaTime) { networkUpdateTimer -= deltaTime; if (networkUpdateTimer <= 0.0f) @@ -51,6 +51,7 @@ namespace Barotrauma.Items.Components msg.WriteRangedInteger((int)(flowPercentage / 10.0f), -10, 10); msg.WriteBoolean(IsActive); msg.WriteBoolean(Hijacked); + msg.WriteBoolean(Disabled); if (TargetLevel != null) { msg.WriteBoolean(true); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Signal/WifiComponent.cs b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Signal/WifiComponent.cs index 12105db73..9cba92029 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Signal/WifiComponent.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/Signal/WifiComponent.cs @@ -4,6 +4,8 @@ namespace Barotrauma.Items.Components { partial class WifiComponent { + private readonly int[] networkReceivedChannelMemory = new int[ChannelMemorySize]; + public void ServerEventWrite(IWriteMessage msg, Client c, NetEntityEvent.IData extraData = null) { SharedEventWrite(msg); @@ -11,8 +13,21 @@ namespace Barotrauma.Items.Components public void ServerEventRead(IReadMessage msg, Client c) { - SharedEventRead(msg); - + int newChannel = msg.ReadRangedInteger(MinChannel, MaxChannel); + for (int i = 0; i < ChannelMemorySize; i++) + { + networkReceivedChannelMemory[i] = msg.ReadRangedInteger(MinChannel, MaxChannel); + } + + if (item.CanClientAccess(c)) + { + Channel = newChannel; + for (int i = 0; i < ChannelMemorySize; i++) + { + channelMemory[i] = networkReceivedChannelMemory[i]; + } + } + // Create an event to notify other clients about the changes item.CreateServerEvent(this); } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Items/Inventory.cs b/Barotrauma/BarotraumaServer/ServerSource/Items/Inventory.cs index 6748365aa..3d85e08ad 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Items/Inventory.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Items/Inventory.cs @@ -59,7 +59,7 @@ namespace Barotrauma ServerLogRemovedItems(); #region local functions - bool IsInventoryAccessible() => sender.Character.CanAccessInventory(this, IsDragAndDropGiveAllowed ? CharacterInventory.AccessLevel.Allowed : CharacterInventory.AccessLevel.Limited); + bool IsInventoryAccessible() => sender.Character.CanAccessInventory(this, IsDragAndDropGiveAllowed ? CharacterInventory.AccessLevel.AllowFriendly : CharacterInventory.AccessLevel.AllowBotsAndPets); void CreateCorrectiveNetworkEvent() { @@ -177,7 +177,7 @@ namespace Barotrauma if (item.GetComponent() is not Pickable pickable || (pickable.IsAttached && !pickable.PickingDone) || item.AllowedSlots.None() || !item.IsInteractable(sender.Character)) { - DebugConsole.AddWarning($"Client {sender.Name} tried to pick up a non-pickable item \"{item}\" (parent inventory: {item.ParentInventory?.Owner.ToString() ?? "null"})", + DebugConsole.AddWarning($"Client {sender.Name} failed to put \"{item}\" in the inventory of {Owner} (parent inventory: {item.ParentInventory?.Owner.ToString() ?? "null"})", item.Prefab.ContentPackage); continue; } @@ -187,13 +187,20 @@ namespace Barotrauma var holdable = item.GetComponent(); if (holdable != null && !holdable.CanBeDeattached()) { continue; } + bool itemAccessDenied = !prevItems.Contains(item) && !itemAccessibility[item] && (sender.Character == null || item.PreviousParentInventory == null || !sender.Character.CanAccessInventory(item.PreviousParentInventory)); + //more restricted "adding" of handcuffs: we can't allow putting handcuffs on a player just because dragging and dropping is allowed + if (item.HasTag(Tags.HandLockerItem) && !itemAccessDenied) + { + itemAccessDenied = + !sender.Character.CanAccessInventory(this, CharacterInventory.AccessLevel.AllowBotsAndPets); + } if (itemAccessDenied) { #if DEBUG || UNSTABLE - DebugConsole.NewMessage($"Client {sender.Name} failed to pick up item \"{item}\" (parent inventory: {item.ParentInventory?.Owner.ToString() ?? "null"}). No access.", Color.Yellow); + DebugConsole.NewMessage($"Client {sender.Name} failed to put \"{item}\" in the inventory of {Owner} (parent inventory: {item.ParentInventory?.Owner.ToString() ?? "null"}). No access.", Color.Yellow); #endif if (item.body != null && !sender.PendingPositionUpdates.Contains(item)) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index b87e85a62..4ce36cbcf 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -3716,7 +3716,7 @@ namespace Barotrauma.Networking UpdateVoteStatus(); - SendChatMessage(peerDisconnectPacket.ChatMessage(client).Value, ChatMessageType.Server, changeType: peerDisconnectPacket.ConnectionChangeType); + SendChatMessage(peerDisconnectPacket.ChatMessage(client.Name).Value, ChatMessageType.Server, changeType: peerDisconnectPacket.ConnectionChangeType); UpdateCrewFrame(); @@ -4234,13 +4234,14 @@ namespace Barotrauma.Networking serverPeer.Send(msg, client.Connection, DeliveryMethod.Reliable); } - public void UnlockRecipe(Identifier identifier) + public void UnlockRecipe(CharacterTeamType team, Identifier identifier) { + IWriteMessage msg = new WriteOnlyMessage(); + msg.WriteByte((byte)ServerPacketHeader.UNLOCKRECIPE); + msg.WriteByte((byte)team); + msg.WriteIdentifier(identifier); foreach (var client in connectedClients) { - IWriteMessage msg = new WriteOnlyMessage(); - msg.WriteByte((byte)ServerPacketHeader.UNLOCKRECIPE); - msg.WriteIdentifier(identifier); serverPeer.Send(msg, client.Connection, DeliveryMethod.Reliable); } } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/P2PServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/P2PServerPeer.cs index 3b317e7a7..c170aef88 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/P2PServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/P2PServerPeer.cs @@ -137,6 +137,10 @@ namespace Barotrauma.Networking { Disconnect(connectedClient.Connection, PeerDisconnectPacket.Banned(banReason)); } + else + { + SendDisconnectMessage(senderEndpoint, PeerDisconnectPacket.Banned(banReason)); + } } else if (packetHeader.IsDisconnectMessage()) { @@ -149,9 +153,10 @@ namespace Barotrauma.Networking Disconnect(connectedClient.Connection, PeerDisconnectPacket.WithReason(DisconnectReason.Disconnected)); } } - else if (packetHeader.IsHeartbeatMessage()) + else if (packetHeader.IsHeartbeatMessage() || packetHeader.IsDoSProtectionMessage()) { - //message exists solely as a heartbeat, ignore its contents + // ignore these messages, heartbeat messages just need to be acknowledged, + // and only the owner should be sending DoS protection messages return; } else if (packetHeader.IsConnectionInitializationStep()) @@ -206,6 +211,49 @@ namespace Barotrauma.Networking return; } + if (packetHeader.IsDoSProtectionMessage()) + { + var packet = INetSerializableStruct.Read(inc); + var disconnectPacket = INetSerializableStruct.Read(inc); + + if (packet.Endpoint.TryUnwrap(out var endpoint)) + { + PendingClient? pendingClient = pendingClients.Find(c => c.Connection.Endpoint == endpoint); + ClientConnectionData? connectedClientData = connectedClients.Find(c => c.Connection.Endpoint == endpoint); + string? clientName; + + if (pendingClient != null) + { + clientName = pendingClient.Name; + if (packet.ShouldBan) + { + BanPendingClient(pendingClient, disconnectPacket.AdditionalInformation, duration: null); + } + RemovePendingClient(pendingClient, disconnectPacket); + } + else if (connectedClientData != null) + { + clientName = connectedClientData.TryGetClientName(); + if (packet.ShouldBan) + { + connectedClientData.BanClient(serverSettings, disconnectPacket.AdditionalInformation, duration: null); + } + Disconnect(connectedClientData.Connection, disconnectPacket); + } + else + { + string errorMsg = $"Unable to remove client {endpoint} for triggering DoS protection, client not found in pending or connected clients"; + DebugConsole.ThrowError(errorMsg); + GameServer.Log(errorMsg, ServerLog.MessageType.Error); + return; + } + + GameServer.Log($"Client {clientName ?? endpoint.ToString()} {(packet.ShouldBan ? "banned" : "disconnected")} due to DoS protection (Sending too many packets).", ServerLog.MessageType.DoSProtection); + GameMain.Server?.SendChatMessage(disconnectPacket.ChatMessage(clientName).Value, ChatMessageType.Server, changeType: disconnectPacket.ConnectionChangeType); + } + return; + } + if (packetHeader.IsConnectionInitializationStep()) { if (OwnerConnection is null) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs index e8a0e2e2e..a48385067 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs @@ -13,7 +13,7 @@ namespace Barotrauma.Networking protected ServerPeer(Callbacks callbacks, ServerSettings serverSettings) : base(callbacks) { this.serverSettings = serverSettings; - this.connectedClients = new List(); + this.connectedClients = new List(); this.pendingClients = new List(); List contentPackageList = new List(); @@ -65,21 +65,57 @@ namespace Barotrauma.Networking } } - protected sealed class ConnectedClient + protected sealed class ClientConnectionData(TConnection connection) { - public readonly TConnection Connection; - public readonly MessageFragmenter Fragmenter; - public readonly MessageDefragmenter Defragmenter; + public readonly TConnection Connection = connection; + public readonly MessageFragmenter Fragmenter = new(); + public readonly MessageDefragmenter Defragmenter = new(); - public ConnectedClient(TConnection connection) + /// + /// Attempts to retrieve the name of the client associated with this connection + /// from a higher layer. + /// + /// Name of the client if found, null otherwise. + public string? TryGetClientName() { - Connection = connection; - Fragmenter = new(); - Defragmenter = new(); + if (GameMain.Server?.ConnectedClients is { } connClients) + { + foreach (Client? client in connClients) + { + if (client?.Connection is not { } clientConnection) { continue; } + + if (clientConnection.EndpointMatches(Connection.Endpoint) ) + { + return client.Name; + } + } + } + + return null; + } + + public void BanClient(ServerSettings settings, string banReason, TimeSpan? duration) + { + string clientName = TryGetClientName() ?? "Player"; + + Connection.AccountInfo.OtherMatchingIds.ForEach(BanAccountId); + + if (Connection.AccountInfo.AccountId.TryUnwrap(out var accountId)) + { + BanAccountId(accountId); + } + else + { + settings.BanList.BanPlayer(clientName, Connection.Endpoint, banReason, duration); + } + return; + + void BanAccountId(AccountId id) + => settings.BanList.BanPlayer(clientName, id, banReason, duration); } } - protected readonly List connectedClients; + protected readonly List connectedClients; protected readonly List pendingClients; protected readonly ServerSettings serverSettings; @@ -235,7 +271,7 @@ namespace Barotrauma.Networking if (pendingClient.InitializationStep == ConnectionInitialization.Success) { TConnection newConnection = pendingClient.Connection; - connectedClients.Add(new ConnectedClient(newConnection)); + connectedClients.Add(new ClientConnectionData(newConnection)); pendingClients.Remove(pendingClient); callbacks.OnInitializationComplete.Invoke(newConnection, pendingClient.Name); diff --git a/Barotrauma/BarotraumaServer/WindowsServer.csproj b/Barotrauma/BarotraumaServer/WindowsServer.csproj index 32e783019..808dc379b 100644 --- a/Barotrauma/BarotraumaServer/WindowsServer.csproj +++ b/Barotrauma/BarotraumaServer/WindowsServer.csproj @@ -6,7 +6,7 @@ Barotrauma FakeFish, Undertow Games Barotrauma Dedicated Server - 1.9.8.0 + 1.10.5.0 Copyright © FakeFish 2018-2023 AnyCPU;x64 DedicatedServer diff --git a/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/Dugong_PipeTest.sub b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/Dugong_PipeTest.sub new file mode 100644 index 000000000..e0d5f17b7 Binary files /dev/null and b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/Dugong_PipeTest.sub differ diff --git a/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/filelist.xml b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/filelist.xml new file mode 100644 index 000000000..0e67713b5 --- /dev/null +++ b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]PipeTestSub/filelist.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/filelist.xml b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/filelist.xml new file mode 100644 index 000000000..bb59ee8d3 --- /dev/null +++ b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/filelist.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/testGapSub.sub b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/testGapSub.sub new file mode 100644 index 000000000..9f730ea4c Binary files /dev/null and b/Barotrauma/BarotraumaShared/LocalMods/[DebugOnlyTest]testGapSub/testGapSub.sub differ diff --git a/Barotrauma/BarotraumaShared/ModLists/Release checklist mods.xml b/Barotrauma/BarotraumaShared/ModLists/Release checklist mods.xml index 8a2bef012..80da34a6a 100644 --- a/Barotrauma/BarotraumaShared/ModLists/Release checklist mods.xml +++ b/Barotrauma/BarotraumaShared/ModLists/Release checklist mods.xml @@ -6,7 +6,6 @@ - diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/AIController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/AIController.cs index 31db3cded..1405cf7a1 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/AIController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/AIController.cs @@ -312,9 +312,10 @@ namespace Barotrauma if (!slots.HasFlag(characterInventory.SlotTypes[i])) { continue; } } targetSlot = i; - //slot free, continue var otherItem = targetInventory.GetItemAt(i); + //slot free, continue if (otherItem == null) { continue; } + if (!otherItem.IsInteractable(Character)) { return false; } //try to move the existing item to LimbSlot.Any and continue if successful if (otherItem.AllowedSlots.Contains(InvSlotType.Any) && targetInventory.TryPutItem(otherItem, Character, CharacterInventory.AnySlot)) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/HumanAIController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/HumanAIController.cs index 8f0293ba7..f7122bff5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/HumanAIController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/HumanAIController.cs @@ -1092,6 +1092,8 @@ namespace Barotrauma } if (!isFleeing) { + CheckForDraggedCorpses(); + foreach (Character target in Character.CharacterList) { if (target.CurrentHull != hull) { continue; } @@ -1209,6 +1211,34 @@ namespace Barotrauma } } } + + private void CheckForDraggedCorpses() + { + if (Character.IsOnPlayerTeam) { return; } + if (Character.Submarine is not { Info.IsOutpost: true }) { return; } + + //find corpses in the same team + foreach (Character otherCharacter in Character.CharacterList) + { + if (otherCharacter.SelectedCharacter == null || + !otherCharacter.SelectedCharacter.IsDead || + otherCharacter.SelectedCharacter.TeamID != Character.TeamID || + otherCharacter.IsInstigator) + { + continue; + } + + if (!Character.CanSeeTarget(otherCharacter)) { continue; } + + // Player is dragging a corpse from our team + string dialogTag = Character.IsSecurity ? "dialogdraggingcorpsereactionsecurity" : "dialogdraggingcorpsereaction"; + Character.Speak(TextManager.Get(dialogTag).Value, messageType: null, + delay: Rand.Range(0.5f, 1.0f), identifier: "dialogdraggingcorpsereaction".ToIdentifier(), minDurationBetweenSimilar: 10.0f); + + AddCombatObjective(Character.IsSecurity ? AIObjectiveCombat.CombatMode.Arrest : AIObjectiveCombat.CombatMode.Retreat, otherCharacter); + break; // Only react to one at a time + } + } public override void OnHealed(Character healer, float healAmount) { @@ -1660,6 +1690,10 @@ namespace Barotrauma public AIObjective SetForcedOrder(Order order) { var objective = ObjectiveManager.CreateObjective(order); + if (order != null && !order.IsDismissal) + { + System.Diagnostics.Debug.Assert(objective != null); + } ObjectiveManager.SetForcedOrder(objective); return objective; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/NPCConversation.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/NPCConversation.cs index 46e1e6aff..d1ca09d38 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/NPCConversation.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/NPCConversation.cs @@ -71,7 +71,7 @@ namespace Barotrauma private static List GetCurrentFlags(Character speaker) { var currentFlags = new List(); - if (Submarine.MainSub != null && Submarine.MainSub.AtDamageDepth) { currentFlags.Add("SubmarineDeep".ToIdentifier()); } + if (Submarine.MainSub != null && Submarine.MainSub.AtCosmeticDamageDepth) { currentFlags.Add("SubmarineDeep".ToIdentifier()); } if (GameMain.GameSession != null && Level.Loaded != null) { @@ -84,7 +84,6 @@ namespace Barotrauma if (GameMain.GameSession.RoundDuration < 120.0f && speaker?.CurrentHull != null && GameMain.GameSession.Map?.CurrentLocation?.Reputation?.Value >= 0.0f && - (speaker.TeamID == CharacterTeamType.FriendlyNPC || speaker.TeamID == CharacterTeamType.None) && Character.CharacterList.Any(c => c.TeamID != speaker.TeamID && c.CurrentHull == speaker.CurrentHull)) { currentFlags.Add("EnterOutpost".ToIdentifier()); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCleanupItems.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCleanupItems.cs index a8eeb0daf..b5304e13c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCleanupItems.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCleanupItems.cs @@ -101,6 +101,7 @@ namespace Barotrauma public static bool IsValidTarget(Item item, Character character, bool checkInventory, bool allowUnloading = true, bool requireValidContainer = true, bool ignoreItemsMarkedForDeconstruction = true) { if (item == null) { return false; } + if (item.GetComponents().None(c => c is not Door && c.CanBePicked)) { return false; } if (item.DontCleanUp) { return false; } if (item.Illegitimate == character.IsOnPlayerTeam) { return false; } if (item.ParentInventory != null) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCombat.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCombat.cs index 59d930560..092c07626 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCombat.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveCombat.cs @@ -65,7 +65,7 @@ namespace Barotrauma private readonly AIObjectiveFindSafety findSafety; private readonly HashSet weapons = new HashSet(); - private readonly HashSet ignoredWeapons = new HashSet(); + private readonly HashSet ignoredWeapons = new HashSet(); private AIObjectiveContainItem seekAmmunitionObjective; private AIObjectiveGoTo retreatObjective; @@ -503,7 +503,8 @@ namespace Barotrauma HashSet allWeapons = FindWeaponsFromInventory(); while (allWeapons.Any()) { - Weapon = GetWeapon(allWeapons, out _weaponComponent); + Weapon = GetWeapon(allWeapons, out ItemComponent newWeaponComponent); + _weaponComponent = newWeaponComponent; if (Weapon == null) { // No weapons @@ -512,7 +513,7 @@ namespace Barotrauma if (!character.Inventory.Contains(Weapon) || WeaponComponent == null) { // Not in the inventory anymore or cannot find the weapon component - allWeapons.Remove(WeaponComponent); + allWeapons.RemoveWhere(weaponComponent => weaponComponent.Item == Weapon); Weapon = null; continue; } @@ -540,7 +541,7 @@ namespace Barotrauma else { // No ammo and should not try to seek ammo. - allWeapons.Remove(WeaponComponent); + allWeapons.RemoveWhere(weaponComponent => weaponComponent.Item == Weapon); Weapon = null; } } @@ -980,7 +981,6 @@ namespace Barotrauma weapons.Clear(); foreach (var item in character.Inventory.AllItems) { - if (ignoredWeapons.Contains(item)) { continue; } GetWeapons(item, weapons); if (item.OwnInventory != null) { @@ -990,11 +990,12 @@ namespace Barotrauma return weapons; } - private static void GetWeapons(Item item, ICollection weaponList) + private void GetWeapons(Item item, ICollection weaponList) { if (item == null) { return; } foreach (var component in item.Components) { + if (ignoredWeapons.Contains(component)) { continue; } if (component.CombatPriority > 0) { weaponList.Add(component); @@ -1332,19 +1333,21 @@ namespace Barotrauma { SteeringManager.Reset(); RemoveSubObjective(ref seekAmmunitionObjective); - ignoredWeapons.Add(Weapon); + ignoredWeapons.Add(WeaponComponent); Weapon = null; }); } - + /// /// Reloads the ammunition found in the inventory. /// If seekAmmo is true, tries to get find the ammo elsewhere. /// + /// True if the weapon was reloaded successfully. private bool Reload(bool seekAmmo) { if (WeaponComponent == null) { return false; } if (Weapon.OwnInventory == null) { return true; } + if (!Weapon.IsInteractable(character)) { return false; } HumanAIController.UnequipEmptyItems(Weapon, allowDestroying: !character.IsOnPlayerTeam); ImmutableHashSet ammunitionIdentifiers = null; if (WeaponComponent.RequiredItems.ContainsKey(RelatedItem.RelationType.Contained)) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs index 7cc00e50a..0dee7b8f0 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs @@ -443,6 +443,10 @@ namespace Barotrauma newCurrentObjective.Abandoned += () => DismissSelf(order); CurrentOrders.Add(order.WithObjective(newCurrentObjective)); } + else if (!order.IsDismissal) + { + DebugConsole.ThrowError($"Failed to create an objective for the order: {order.Name}"); + } if (!HasOrders()) { // Recreate objectives, because some of them may be removed, if impossible to complete (e.g. due to path finding) @@ -458,6 +462,9 @@ namespace Barotrauma } } + /// + /// Creates an AI objective based on the order. Note that the method can return null in the case of e.g. Dismissal orders or orders that erroneously target something non-interactable. + /// public AIObjective CreateObjective(Order order, float priorityModifier = 1) { if (order == null || order.IsDismissal) { return null; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Order.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Order.cs index 95a6cab00..658c83afb 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Order.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Order.cs @@ -341,6 +341,8 @@ namespace Barotrauma { if (component?.GetType() is Type componentType) { + // Items used via a controller (i.e. turrets) are not selectable but should still be valid targets. + if (!UseController && !component.CanBeSelected) { continue; } if (componentType == ItemComponentType) { return component; } if (CanTypeBeSubclass && componentType.IsSubclassOf(ItemComponentType)) { return component; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/PetBehavior.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/PetBehavior.cs index db7b774b7..69b92d180 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/PetBehavior.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/PetBehavior.cs @@ -5,6 +5,7 @@ using Microsoft.Xna.Framework; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Runtime.InteropServices; using System.Xml.Linq; using static Barotrauma.CharacterParams; @@ -434,6 +435,21 @@ namespace Barotrauma var petBehavior = (c.AIController as EnemyAIController)?.PetBehavior; if (petBehavior == null) { continue; } + //never save hostile pets or pets left outside + if (c.TeamID == CharacterTeamType.None || + c.TeamID == CharacterTeamType.Team2 || + c.Submarine == null) + { + continue; + } + + //pets must be in a player sub or owned by someone to be persistent + if (c.Submarine is not { Info.IsPlayer: true } && + petBehavior.Owner is not { IsOnPlayerTeam: true }) + { + continue; + } + XElement petElement = new XElement("pet", new XAttribute("speciesname", c.SpeciesName), new XAttribute("ownerhash", petBehavior.Owner?.Info?.GetIdentifier() ?? 0), diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AICharacter.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AICharacter.cs index ed01119ed..879c4197f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AICharacter.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AICharacter.cs @@ -42,7 +42,7 @@ namespace Barotrauma { enemyAi.PetBehavior?.Update(deltaTime); } - if (IsDead || Vitality <= 0.0f || Stun > 0.0f || IsIncapacitated) + if (IsDead || IsUnconscious || Stun > 0.0f || IsIncapacitated) { //don't enable simple physics on dead/incapacitated characters //the ragdoll controls the movement of incapacitated characters instead of the collider, diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/AnimController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/AnimController.cs index 4405a791e..6d49180c4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/AnimController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/AnimController.cs @@ -8,8 +8,15 @@ using Barotrauma.Extensions; namespace Barotrauma { - abstract class AnimController : Ragdoll + abstract class AnimController : Ragdoll, ISerializableEntity { + /// + /// Most of the properties in this class are read-only, but can be useful for conditionals + /// + public Dictionary SerializableProperties { get; private set; } + + public string Name => nameof(AnimController); + public Vector2 RightHandIKPos { get; protected set; } public Vector2 LeftHandIKPos { get; protected set; } @@ -200,7 +207,10 @@ namespace Barotrauma public float WalkPos { get; protected set; } - public AnimController(Character character, string seed, RagdollParams ragdollParams = null) : base(character, seed, ragdollParams) { } + public AnimController(Character character, string seed, RagdollParams ragdollParams = null) : base(character, seed, ragdollParams) + { + SerializableProperties = SerializableProperty.GetProperties(this); + } public void UpdateAnimations(float deltaTime) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs index d950dbf76..ca34f5bdb 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs @@ -2411,7 +2411,9 @@ namespace Barotrauma if (Inventory != null) { - if (IsKeyHit(InputType.DropItem) && Screen.Selected is { IsEditor: false }) + //this doesn't need to be run by the server, clients sync the contents of their inventory with the server instead of the inputs used to manipulate the inventory +#if CLIENT + if (IsKeyHit(InputType.DropItem) && Screen.Selected is { IsEditor: false } && CharacterHUD.ShouldDrawInventory(this)) { foreach (Item item in HeldItems) { @@ -2429,6 +2431,7 @@ namespace Barotrauma break; } } +#endif bool CanUseItemsWhenSelected(Item item) => item == null || !item.Prefab.DisableItemUsageWhenSelected; if (CanUseItemsWhenSelected(SelectedItem) && CanUseItemsWhenSelected(SelectedSecondaryItem)) @@ -2632,6 +2635,7 @@ namespace Barotrauma public bool Unequip(Item item) { if (!HasEquippedItem(item)) { return false; } + if (!item.IsInteractable(this)) { return false; } if (!TryPutItemInAnySlot(item)) { if (!TryPutItemInBag(item)) @@ -2642,7 +2646,7 @@ namespace Barotrauma return true; } - public bool CanAccessInventory(Inventory inventory, CharacterInventory.AccessLevel accessLevel = CharacterInventory.AccessLevel.Limited) + public bool CanAccessInventory(Inventory inventory, CharacterInventory.AccessLevel accessLevel = CharacterInventory.AccessLevel.AllowBotsAndPets) { if (!CanInteract || inventory.Locked) { return false; } @@ -2686,7 +2690,7 @@ namespace Barotrauma /// /// Is the inventory accessible to the character? Doesn't check if the character can actually interact with it (distance checks etc). /// - public bool IsInventoryAccessibleTo(Character character, CharacterInventory.AccessLevel accessLevel = CharacterInventory.AccessLevel.Limited) + public bool IsInventoryAccessibleTo(Character character, CharacterInventory.AccessLevel accessLevel = CharacterInventory.AccessLevel.AllowBotsAndPets) { if (Removed || Inventory == null) { return false; } if (!Inventory.AccessibleWhenAlive && !IsDead) @@ -2701,9 +2705,9 @@ namespace Barotrauma if (IsKnockedDownOrRagdolled || LockHands) { return true; } return accessLevel switch { - CharacterInventory.AccessLevel.Restricted => false, - CharacterInventory.AccessLevel.Limited => (IsBot && IsOnSameTeam()) || IsFriendlyPet(), - CharacterInventory.AccessLevel.Allowed => IsOnSameTeam() || IsFriendlyPet(), + CharacterInventory.AccessLevel.OnlyIfIncapacitated => false, + CharacterInventory.AccessLevel.AllowBotsAndPets => (IsBot && IsOnSameTeam()) || IsFriendlyPet(), + CharacterInventory.AccessLevel.AllowFriendly => IsOnSameTeam() || IsFriendlyPet(), _ => throw new NotImplementedException() }; @@ -5714,7 +5718,7 @@ namespace Barotrauma public bool HasRecipeForItem(Identifier recipeIdentifier) { - if (GameMain.GameSession != null && GameMain.GameSession.UnlockedRecipes.Contains(recipeIdentifier)) { return true; } + if (GameMain.GameSession != null && GameMain.GameSession.HasUnlockedRecipe(this, recipeIdentifier)) { return true; } return characterTalents.Any(t => t.UnlockedRecipes.Contains(recipeIdentifier)); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index af18586d3..7720554ab 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -135,6 +135,9 @@ namespace Barotrauma private Affliction stunAffliction; public Affliction BloodlossAffliction { get => bloodlossAffliction; } + /// + /// Is the character dead or below 0 vitality and not able to stay conscious? + /// public bool IsUnconscious { get { return Character.IsDead || (Vitality <= 0.0f && !Character.HasAbilityFlag(AbilityFlags.AlwaysStayConscious)); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentFile/CharacterFile.cs b/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentFile/CharacterFile.cs index 2222c41c4..e608d06ba 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentFile/CharacterFile.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentFile/CharacterFile.cs @@ -106,6 +106,8 @@ namespace Barotrauma void AddTexturePath(string path) { if (string.IsNullOrEmpty(path)) { return; } + //if the path contains a gender variable, we can't load it yet because we don't know which gender we need + if (path.Contains("[GENDER]")) { return; } texturePaths.Add(ContentPath.FromRaw(characterPrefab.ContentPackage, ragdollParams.Texture)); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/DebugConsole.cs b/Barotrauma/BarotraumaShared/SharedSource/DebugConsole.cs index 6d41c1c58..8dc9bb60d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/DebugConsole.cs @@ -656,13 +656,17 @@ namespace Barotrauma NewMessage("***************", Color.Cyan); })); - commands.Add(new Command("godmode", "godmode [character name]: Toggle character godmode. Makes the targeted character invulnerable to damage. If the name parameter is omitted, the controlled character will receive godmode.", + commands.Add(new Command("godmode", "godmode [character name] [remove afflictions (true/false)]: Toggle character godmode. Makes the targeted character invulnerable to damage. If the name parameter is omitted, the controlled character will receive godmode.", (string[] args) => { bool? godmodeStateOnFirstCharacter = null; HandleCommandForCrewOrSingleCharacter(args, ToggleGodMode); void ToggleGodMode(Character targetCharacter) { + if (args.Length > 1 && bool.TryParse(args[1], out bool removeafflictions)) + { + if (removeafflictions) { targetCharacter.CharacterHealth.RemoveAllAfflictions(); } + } targetCharacter.GodMode = godmodeStateOnFirstCharacter ?? !targetCharacter.GodMode; godmodeStateOnFirstCharacter = targetCharacter.GodMode; NewMessage((targetCharacter.GodMode ? "Enabled godmode on " : "Disabled godmode on ") + targetCharacter.Name, diff --git a/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/MissionStateAction.cs b/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/MissionStateAction.cs index 48ef33028..a6dd612be 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/MissionStateAction.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/MissionStateAction.cs @@ -21,6 +21,9 @@ [Serialize(0, IsPropertySaveable.Yes, description: "The state to set the mission to, or how much to add to the state of the mission.")] public int State { get; set; } + [Serialize(false, IsPropertySaveable.Yes, description: "If set to true, the mission is forced to fail without a chance of retrying it.")] + public bool ForceFailure { get; set; } + private bool isFinished; public MissionStateAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) @@ -31,7 +34,7 @@ DebugConsole.ThrowError($"Error in event \"{parentEvent.Prefab.Identifier}\": MissionIdentifier has not been configured.", contentPackage: element.ContentPackage); } - if (Operation == OperationType.Add && State == 0) + if (Operation == OperationType.Add && State == 0 && !ForceFailure) { DebugConsole.AddWarning($"Potential error in event \"{parentEvent.Prefab.Identifier}\": {nameof(MissionStateAction)} is set to add 0 to the mission state, which will do nothing.", contentPackage: element.ContentPackage); @@ -54,6 +57,11 @@ foreach (Mission mission in GameMain.GameSession.Missions) { if (mission.Prefab.Identifier != MissionIdentifier) { continue; } + if (ForceFailure) + { + mission.ForceFailure = true; + } + switch (Operation) { case OperationType.Set: diff --git a/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/SpawnAction.cs b/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/SpawnAction.cs index 4974504fc..10832764f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/SpawnAction.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/SpawnAction.cs @@ -410,16 +410,39 @@ namespace Barotrauma public static WayPoint GetSpawnPos(SpawnLocationType spawnLocation, SpawnType? spawnPointType, IEnumerable moduleFlags = null, IEnumerable spawnpointTags = null, bool asFarAsPossibleFromAirlock = false, bool requireTaggedSpawnPoint = false, bool allowInPlayerView = true) { bool requireHull = spawnLocation == SpawnLocationType.MainSub || spawnLocation == SpawnLocationType.Outpost; - List potentialSpawnPoints = WayPoint.WayPointList.FindAll(wp => IsValidSubmarineType(spawnLocation, wp.Submarine) && (wp.CurrentHull != null || !requireHull)); - potentialSpawnPoints = potentialSpawnPoints.FindAll(wp => wp.ConnectedDoor == null && wp.Ladders == null && wp.IsTraversable); + + IEnumerable potentialSpawnPoints = WayPoint.WayPointList.FindAll(wp => IsValidSubmarineType(spawnLocation, wp.Submarine) && (wp.CurrentHull != null || !requireHull)); + potentialSpawnPoints = potentialSpawnPoints.Where(wp => wp.ConnectedDoor == null && wp.Ladders == null && wp.IsTraversable); + + //find spawnpoints with the desired type, or any random spawnpoints if not specified + IEnumerable spawnPointsWithCorrectType; + if (spawnPointType.HasValue) + { + spawnPointsWithCorrectType = potentialSpawnPoints.Where(wp => + spawnPointType.Value.HasFlag(wp.SpawnType) && + //need to handle zero (SpawnType.Path) separately, because spawnPointType will always have the flag 0 + (wp.SpawnType != 0 || spawnPointType.Value == 0)); + } + else + { + spawnPointsWithCorrectType = potentialSpawnPoints.Where(wp => wp.SpawnType != SpawnType.Path); + } + if (spawnPointsWithCorrectType.Any()) + { + potentialSpawnPoints = spawnPointsWithCorrectType; + } + + //with correct module flags, if there are any if (moduleFlags != null && moduleFlags.Any()) { - var spawnPoints = potentialSpawnPoints.Where(wp => wp.CurrentHull is Hull h && h.OutpostModuleTags.Any(moduleFlags.Contains)); - if (spawnPoints.Any()) + var spawnPointsWithCorrectFlags = potentialSpawnPoints.Where(wp => wp.CurrentHull is Hull h && h.OutpostModuleTags.Any(moduleFlags.Contains)); + if (spawnPointsWithCorrectFlags.Any()) { - potentialSpawnPoints = spawnPoints.ToList(); + potentialSpawnPoints = spawnPointsWithCorrectFlags.ToList(); } } + + //with correct spawn point tags, if there are any if (spawnpointTags != null && spawnpointTags.Any()) { var spawnPointsWithTag = potentialSpawnPoints.Where(wp => spawnpointTags.Any(tag => wp.Tags.Contains(tag) && wp.ConnectedDoor == null && wp.IsTraversable)); @@ -461,16 +484,8 @@ namespace Barotrauma return null; } - IEnumerable validSpawnPoints; - if (spawnPointType.HasValue) - { - validSpawnPoints = potentialSpawnPoints.FindAll(wp => spawnPointType.Value.HasFlag(wp.SpawnType)); - } - else - { - validSpawnPoints = potentialSpawnPoints.FindAll(wp => wp.SpawnType != SpawnType.Path); - if (!validSpawnPoints.Any()) { validSpawnPoints = potentialSpawnPoints; } - } + //spawnpoints that match the desired criteria found, choose the best one next + IEnumerable validSpawnPoints = potentialSpawnPoints; //don't spawn in an airlock module if there are other options var airlockSpawnPoints = potentialSpawnPoints.Where(wp => wp.CurrentHull?.OutpostModuleTags.Contains("airlock".ToIdentifier()) ?? false); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/BeaconMission.cs b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/BeaconMission.cs index b79d4e13f..f9b102a87 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/BeaconMission.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/BeaconMission.cs @@ -2,6 +2,7 @@ using Microsoft.Xna.Framework; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Xml.Linq; @@ -23,10 +24,12 @@ namespace Barotrauma private bool swarmSpawned; private readonly List monsterSets = new List(); private readonly LocalizedString sonarLabel; + private readonly ImmutableArray beaconTags; public BeaconMission(MissionPrefab prefab, Location[] locations, Submarine sub) : base(prefab, locations, sub) { swarmSpawned = false; + beaconTags = prefab.ConfigElement.GetAttributeIdentifierArray("beacontags", []).ToImmutableArray(); foreach (var monsterElement in prefab.ConfigElement.GetChildElements("monster")) { @@ -185,6 +188,29 @@ namespace Barotrauma { levelData.HasBeaconStation = true; levelData.IsBeaconActive = false; + + if (beaconTags.Length > 0) + { + var selectedBeacon = GetRandomBeaconByTags(beaconTags, levelData); + if (selectedBeacon != null) + { + levelData.ForceBeaconStation = selectedBeacon; + } + else + { + DebugConsole.ThrowError($"Beacon mission \"{Prefab.Identifier}\" could not find a suitable beacon station with beacontags \"{string.Join(", ", beaconTags)}\" for level difficulty {levelData.Difficulty:F1}.", + contentPackage: Prefab.ContentPackage); + } + } + } + + private static SubmarineInfo GetRandomBeaconByTags(ImmutableArray tags, LevelData levelData) + { + return GetRandomSubmarineByTagsAndDifficulty( + tags, + levelData, + s => s.IsBeacon, + "beacon station"); } } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/Mission.cs b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/Mission.cs index 757561538..e6629022b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/Mission.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/Mission.cs @@ -48,7 +48,7 @@ namespace Barotrauma protected static bool IsClient => GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient; - private readonly CheckDataAction completeCheckDataAction; + protected readonly CheckDataAction completeCheckDataAction; public readonly ImmutableArray Headers; public readonly ImmutableArray Messages; @@ -110,9 +110,11 @@ namespace Barotrauma public bool Failed { - get { return failed; } + get { return failed || ForceFailure; } } + public bool ForceFailure; + public virtual bool AllowRespawning { get { return true; } @@ -541,9 +543,10 @@ namespace Barotrauma { if (GameMain.NetworkMember is not { IsClient: true }) { - completed = + completed = + !ForceFailure && DetermineCompleted() && - (completeCheckDataAction == null ||completeCheckDataAction.GetSuccess()); + (completeCheckDataAction == null || completeCheckDataAction.GetSuccess()); } if (completed) { @@ -569,6 +572,10 @@ namespace Barotrauma TimesAttempted++; EndMissionSpecific(completed); + if (ForceFailure) + { + failed = true; + } } protected abstract bool DetermineCompleted(); @@ -829,6 +836,51 @@ namespace Barotrauma cargoSpawnPos.Position.X + Rand.Range(-20.0f, 20.0f, Rand.RandSync.ServerAndClient), cargoRoom.Rect.Y - cargoRoom.Rect.Height + itemPrefab.Size.Y / 2); } + + /// + /// Gets a random submarine by tags, filtered by difficulty. Used by missions that force specific submarines (wrecks, beacons, etc.) + /// + /// Mission tags to match against + /// Random seed for selection + /// Function to filter submarines by type (e.g., s => s.IsWreck) + /// Name of submarine type for error messages (e.g., "wreck", "beacon station") + /// Selected submarine, or null if none found + protected static SubmarineInfo GetRandomSubmarineByTagsAndDifficulty( + IEnumerable tags, + LevelData levelData, + Func submarineSelector, + string submarineTypeName) + { + var rand = new MTRandom(ToolBox.StringToInt(levelData.Seed)); + float levelDifficulty = levelData.Difficulty; + + var submarinesWithTags = SubmarineInfo.SavedSubmarines + .Where(submarineSelector) + .Where(s => + { + return s.GetExtraSubmarineInfo is { } extraInfo && (tags.None() || tags.Any(t => extraInfo.MissionTags.Contains(t))); + }) + .ToList(); + + var matchingSubmarines = submarinesWithTags + .Where(s => + { + return s.GetExtraSubmarineInfo is { } extraInfo && + levelDifficulty >= extraInfo.MinLevelDifficulty && + levelDifficulty <= extraInfo.MaxLevelDifficulty; + }) + .ToList(); + + if (matchingSubmarines.Count == 0) + { + if (submarinesWithTags.Count > 0) + { + DebugConsole.ThrowError($"Found {submarinesWithTags.Count} {submarineTypeName}(s) with matching tags \"{string.Join(", ", tags)}\", but none are suitable for level difficulty {levelDifficulty:F1}."); + } + return null; + } + return matchingSubmarines[rand.Next(matchingSubmarines.Count)]; + } } class AbilityMissionMoneyGainMultiplier : AbilityObject, IAbilityValue, IAbilityMission diff --git a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/SalvageMission.cs b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/SalvageMission.cs index 7f2dbba0a..602e4dd0d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/SalvageMission.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Events/Missions/SalvageMission.cs @@ -4,6 +4,7 @@ using FarseerPhysics; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; namespace Barotrauma @@ -240,6 +241,8 @@ namespace Barotrauma /// private readonly float requiredDeliveryAmount; + private readonly ImmutableArray wreckTags; + private LocalizedString pickedUpMessage; /// @@ -311,12 +314,13 @@ namespace Barotrauma : base(prefab, locations, sub) { requiredDeliveryAmount = prefab.ConfigElement.GetAttributeFloat(nameof(requiredDeliveryAmount), 0.98f); - //LevelData may not be instantiated at this point, in that case use the name identifier of the location rng = new MTRandom(ToolBox.StringToInt( locations[0].LevelData?.Seed ?? locations[0].NameIdentifier.Value + locations[1].LevelData?.Seed ?? locations[1].NameIdentifier.Value)); + wreckTags = prefab.ConfigElement.GetAttributeIdentifierArray("wrecktags", []).ToImmutableArray(); + partiallyRetrievedMessage = GetMessage(nameof(partiallyRetrievedMessage)); allRetrievedMessage = GetMessage(nameof(allRetrievedMessage)); pickedUpMessage = GetMessage(nameof(pickedUpMessage)); @@ -756,5 +760,31 @@ namespace Barotrauma target.Reset(); } } + + public override void AdjustLevelData(LevelData levelData) + { + if (wreckTags.Length > 0) + { + var selectedWreck = GetRandomWreckByTags(wreckTags, levelData); + if (selectedWreck != null) + { + levelData.ForceWreck = selectedWreck; + } + else + { + DebugConsole.ThrowError($"Salvage mission \"{Prefab.Identifier}\" could not find a suitable wreck with wrecktags \"{string.Join(", ", wreckTags)}\" for level difficulty {levelData.Difficulty:F1}.", + contentPackage: Prefab.ContentPackage); + } + } + } + + private static SubmarineInfo GetRandomWreckByTags(ImmutableArray tags, LevelData levelData) + { + return GetRandomSubmarineByTagsAndDifficulty( + tags, + levelData, + s => s.IsWreck, + "wreck"); + } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/GameSession/CargoManager.cs b/Barotrauma/BarotraumaShared/SharedSource/GameSession/CargoManager.cs index e4d8f4c58..7bbcf7418 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/GameSession/CargoManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/GameSession/CargoManager.cs @@ -304,12 +304,17 @@ namespace Barotrauma // Check all the prices before starting the transaction to make sure the modifiers stay the same for the whole transaction var buyValues = GetBuyValuesAtCurrentLocation(storeIdentifier, itemsToPurchase.Select(i => i.ItemPrefab)); var itemsInStoreCrate = GetBuyCrateItems(storeIdentifier, create: true); - foreach (PurchasedItem item in itemsToPurchase) + //handle checking which items can be purchased and deducting money first + foreach (PurchasedItem item in itemsToPurchase.ToList()) { if (item.Quantity <= 0) { continue; } // Exchange money int itemValue = item.Quantity * buyValues[item.ItemPrefab]; - if (!campaign.TryPurchase(client, itemValue)) { continue; } + if (!campaign.TryPurchase(client, itemValue)) + { + itemsToPurchase.Remove(item); + continue; + } // Add to the purchased items var purchasedItem = itemsPurchasedFromStore.Find(pi => pi.ItemPrefab == item.ItemPrefab && pi.DeliverImmediately == item.DeliverImmediately); @@ -329,6 +334,7 @@ namespace Barotrauma } store.Balance += itemValue; } + //actually spawn the items at this point if (GameMain.NetworkMember is not { IsClient: true }) { Character targetCharacter; diff --git a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameModes/CampaignMode.cs b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameModes/CampaignMode.cs index a64925b36..a16e44f2d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameModes/CampaignMode.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameModes/CampaignMode.cs @@ -1724,7 +1724,10 @@ namespace Barotrauma GameMain.GameSession.EventManager.Load(subElement); break; case "unlockedrecipe": - GameMain.GameSession.UnlockRecipe(subElement.GetAttributeIdentifier("identifier", Identifier.Empty), showNotifications: false); + GameMain.GameSession.UnlockRecipe( + subElement.GetAttributeEnum("team", CharacterTeamType.Team1), + subElement.GetAttributeIdentifier("identifier", Identifier.Empty), + showNotifications: false); break; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs index 355687eb6..7608e395b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs @@ -170,8 +170,8 @@ namespace Barotrauma public Submarine? Submarine { get; set; } - private readonly HashSet unlockedRecipes = new HashSet(); - public IEnumerable UnlockedRecipes => unlockedRecipes; + private readonly HashSet<(CharacterTeamType team, Identifier identifier)> unlockedRecipes = new HashSet<(CharacterTeamType, Identifier)>(); + public IEnumerable<(CharacterTeamType, Identifier)> UnlockedRecipes => unlockedRecipes; public CampaignDataPath DataPath { get; set; } @@ -1499,25 +1499,32 @@ namespace Barotrauma #endif } - public void UnlockRecipe(Identifier identifier, bool showNotifications) + public void UnlockRecipe(CharacterTeamType team, Identifier identifier, bool showNotifications) { - if (unlockedRecipes.Add(identifier)) + if (unlockedRecipes.Add((team, identifier))) { #if CLIENT if (showNotifications) { foreach (var character in GetSessionCrewCharacters(CharacterType.Both)) { + if (character.TeamID != team) { continue; } LocalizedString recipeName = TextManager.Get($"entityname.{identifier}").Fallback(identifier.Value); character.AddMessage(TextManager.GetWithVariable("recipeunlockednotification", "[name]", recipeName).Value, GUIStyle.Yellow, playSound: true); } } #else - GameMain.Server.UnlockRecipe(identifier); + GameMain.Server.UnlockRecipe(team, identifier); #endif } } + public bool HasUnlockedRecipe(Character character, Identifier itemIdentifier) + { + if (character == null) { return false; } + return unlockedRecipes.Contains((character.TeamID, itemIdentifier)); + } + public static bool IsCompatibleWithEnabledContentPackages(IList contentPackageNames, out LocalizedString errorMsg) { errorMsg = ""; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/CharacterInventory.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/CharacterInventory.cs index 15eb5f958..291903c48 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/CharacterInventory.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/CharacterInventory.cs @@ -17,15 +17,21 @@ namespace Barotrauma { /// /// How much access other characters have to the inventory? - /// = Only accessible when character is knocked down or handcuffed. - /// = Can also access inventories of bots on the same team and friendly pets. - /// = Can also access other players in the same team (used for drag and drop give). /// public enum AccessLevel { - Restricted, - Limited, - Allowed + /// + /// Only accessible when character is knocked down or handcuffed. + /// + OnlyIfIncapacitated, + /// + /// Can also access inventories of bots on the same team and friendly pets. + /// + AllowBotsAndPets, + /// + /// Can also access other players in the same team (used for drag and drop give). + /// + AllowFriendly } private readonly Character character; @@ -342,8 +348,9 @@ namespace Barotrauma { foreach (Item existingItem in slots[slot].Items.ToList()) { + if (!existingItem.IsInteractable(character)) { continue; } existingItem.Drop(user); - if (existingItem.ParentInventory != null) { existingItem.ParentInventory.RemoveItem(existingItem); } + existingItem.ParentInventory?.RemoveItem(existingItem); } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/Throwable.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/Throwable.cs index 6ca8e627f..851e0a8b1 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/Throwable.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/Throwable.cs @@ -1,5 +1,6 @@ using Barotrauma.Networking; using Microsoft.Xna.Framework; +using System; using System.Linq; namespace Barotrauma.Items.Components @@ -197,7 +198,16 @@ namespace Barotrauma.Items.Components item.Drop(CurrentThrower, createNetworkEvent: GameMain.NetworkMember == null || GameMain.NetworkMember.IsServer); item.WaterDragCoefficient = WaterDragCoefficient; - item.body.ApplyLinearImpulse(throwVector * ThrowForce * item.body.Mass * 3.0f, maxVelocity: NetConfig.MaxPhysicsBodyVelocity); + + float throwForce = ThrowForce; + //Reduce force when aiming down + float downwardsDotProduct = Vector2.Dot(-Vector2.UnitY, throwVector); //1 when pointing directly down, 0 when sideways, -1 when up + if (downwardsDotProduct > 0) + { + throwForce *= (1.0f - downwardsDotProduct * 0.7f); + } + + item.body.ApplyLinearImpulse(throwVector * throwForce * item.body.Mass * 3.0f, maxVelocity: NetConfig.MaxPhysicsBodyVelocity); //disable platform collisions until the item comes back to rest again item.body.CollidesWith = Physics.CollisionWall | Physics.CollisionLevel; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemComponent.cs index 0f2ec8e49..8d7df0c21 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemComponent.cs @@ -483,6 +483,9 @@ namespace Barotrauma.Items.Components public virtual bool UpdateWhenInactive => false; + [Serialize(false, IsPropertySaveable.No, "If true, the component will retain its normal functionality when the item reaches 0 condition.")] + public bool UpdateWhenBroken { get; set; } + //called when isActive is true and condition > 0.0f public virtual void Update(float deltaTime, Camera cam) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemContainer.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemContainer.cs index ae47871e3..ae7016391 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemContainer.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemContainer.cs @@ -132,6 +132,12 @@ namespace Barotrauma.Items.Components set; } + [Serialize(true, IsPropertySaveable.No, description: "Should a button that allows sorting the items alphabetically be shown in the container's UI panel?")] + public bool ShowSortButton { get; set; } + + [Serialize(true, IsPropertySaveable.No, description: "Should a button that merges items into stacks be shown in the container's UI panel?")] + public bool ShowMergeButton { get; set; } + [Serialize(true, IsPropertySaveable.Yes, description: "When this item is equipped, and you 'quick use' (double click / equip button) another equippable item, should the game attempt to move that item inside this one?")] public bool QuickUseMovesItemsInside { get; set; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Controller.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Controller.cs index 5bd44334a..899258e4f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Controller.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Controller.cs @@ -406,6 +406,7 @@ namespace Barotrauma.Items.Components if (IsOutOfPower()) { return false; } + ApplyStatusEffects(ActionType.OnUse, 1.0f, activator); if (IsToggle && (activator == null || lastUsed < Timing.TotalTime - 0.1)) { if (GameMain.NetworkMember == null || GameMain.NetworkMember.IsServer) @@ -421,8 +422,7 @@ namespace Barotrauma.Items.Components item.SendSignal(new Signal(output, sender: user), "trigger_out"); } - lastUsed = Timing.TotalTime; - ApplyStatusEffects(ActionType.OnUse, 1.0f, activator); + lastUsed = Timing.TotalTime; return true; } @@ -541,6 +541,7 @@ namespace Barotrauma.Items.Components #if CLIENT PlaySound(ActionType.OnUse, picker); #endif + ApplyStatusEffects(ActionType.OnUse, 1f, picker); return true; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/OxygenGenerator.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/OxygenGenerator.cs index 811deb841..a0f210be2 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/OxygenGenerator.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/OxygenGenerator.cs @@ -84,7 +84,10 @@ namespace Barotrauma.Items.Components CurrFlow = 0.0f; } - private void GetVents() + /// + /// Finds all the linked vents and calculates how much oxygen should be distributed to each of them based on the hull volumes. + /// + public void GetVents() { totalHullVolume = 0.0f; ventList ??= new List<(Vent vent, float hullVolume)>(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Pump.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Pump.cs index 6cf8f28a1..dab4a647c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Pump.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Pump.cs @@ -2,6 +2,7 @@ using Barotrauma.Networking; using Microsoft.Xna.Framework; using System; +using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -35,7 +36,7 @@ namespace Barotrauma.Items.Components { get { - if (item.ConditionPercentage > 10.0f || !IsActive) { return 0.0f; } + if (item.ConditionPercentage > 10.0f || !IsActive || Disabled) { return 0.0f; } return (1.0f - item.ConditionPercentage / 10.0f) * 100.0f; } } @@ -61,6 +62,23 @@ namespace Barotrauma.Items.Components set => maxFlow = value; } + private bool disabled; + [Serialize(false, IsPropertySaveable.Yes, description: "If true, the pump is unable to pump water.", alwaysUseInstanceValues: true)] + public bool Disabled + { + get => disabled; + set + { + if (disabled == value) { return; } + disabled = value; +#if SERVER + //send a network update soon + //don't force to 0 though so this doesn't lead to spam if the property is toggled rapidly + networkUpdateTimer = Math.Min(networkUpdateTimer, 0.5f); +#endif + } + } + [Editable, Serialize(true, IsPropertySaveable.Yes, alwaysUseInstanceValues: true)] public bool IsOn { @@ -68,15 +86,13 @@ namespace Barotrauma.Items.Components set { IsActive = value; } } + [Serialize(false, IsPropertySaveable.No)] + public bool CanCauseLethalPressure { get; set; } + private float currFlow; - public float CurrFlow - { - get - { - if (!IsActive) { return 0.0f; } - return Math.Abs(currFlow); - } - } + public float CurrFlow => IsActive ? Math.Abs(currFlow) : 0.0f; + + public bool IsHullFull => item.CurrentHull != null && item.CurrentHull.WaterVolume >= item.CurrentHull.Volume * Hull.MaxCompress; public override bool HasPower => IsActive && Voltage >= MinVoltage; public bool IsAutoControlled => pumpSpeedLockTimer > 0.0f || isActiveLockTimer > 0.0f; @@ -85,7 +101,7 @@ namespace Barotrauma.Items.Components public override bool UpdateWhenInactive => true; - public float CurrentStress => Math.Abs(flowPercentage / 100.0f); + public float CurrentStress => IsActive ? Math.Abs(flowPercentage / 100.0f) : 0.0f; public Pump(Item item, ContentXElement element) : base(item, element) @@ -95,48 +111,42 @@ namespace Barotrauma.Items.Components partial void InitProjSpecific(ContentXElement element); + private readonly List linkedHulls = []; + public override void Update(float deltaTime, Camera cam) { pumpSpeedLockTimer -= deltaTime; isActiveLockTimer -= deltaTime; - if (!IsActive) + currFlow = 0f; + + if (item.CurrentHull == null) { + if (TargetLevel != null) { FlowPercentage = 0f; } return; } - currFlow = 0.0f; - if (TargetLevel != null) { - float hullPercentage = 0.0f; - if (item.CurrentHull != null) + float hullWaterVolume = item.CurrentHull.WaterVolume; + float totalHullVolume = item.CurrentHull.Volume; + + linkedHulls.Clear(); + //hidden hulls still affect buoyancy, include them here + item.CurrentHull.GetLinkedHulls(linkedHulls, includeHiddenHulls: true); + foreach (var linkedHull in linkedHulls) { - float hullWaterVolume = item.CurrentHull.WaterVolume; - float totalHullVolume = item.CurrentHull.Volume; - foreach (var linked in item.CurrentHull.linkedTo) - { - if ((linked is Hull linkedHull)) - { - hullWaterVolume += linkedHull.WaterVolume; - totalHullVolume += linkedHull.Volume; - } - } - hullPercentage = hullWaterVolume / totalHullVolume * 100.0f; + hullWaterVolume += linkedHull.WaterVolume; + totalHullVolume += linkedHull.Volume; } + float hullPercentage = hullWaterVolume / totalHullVolume * 100.0f; FlowPercentage = ((float)TargetLevel - hullPercentage) * 10.0f; } - if (!HasPower) - { - return; - } + UpdateNetworking(deltaTime); - UpdateProjSpecific(deltaTime); - - ApplyStatusEffects(ActionType.OnActive, deltaTime); - - if (item.CurrentHull == null) { return; } + if (!IsActive || Disabled) { return; } + if (flowPercentage <= 0f && item.CurrentHull.WaterVolume <= 0f) { return; } float powerFactor = Math.Min(currPowerConsumption <= 0.0f || MinVoltage <= 0.0f ? 1.0f : Voltage, MaxOverVoltageFactor); @@ -150,8 +160,22 @@ namespace Barotrauma.Items.Components //less effective when in a bad condition currFlow *= MathHelper.Lerp(0.5f, 1.0f, item.Condition / item.MaxCondition); - item.CurrentHull.WaterVolume += currFlow * deltaTime * Timing.FixedUpdateRate; - if (item.CurrentHull.WaterVolume > item.CurrentHull.Volume) { item.CurrentHull.Pressure += 30.0f * deltaTime; } + if (MathUtils.NearlyEqual(currFlow, 0f, epsilon: 0.01f)) + { + currFlow = 0f; // Set to 0 for conditionals. + return; + } + + item.CurrentHull.WaterVolume += currFlow * deltaTime * Timing.FixedUpdateRate; + + if (flowPercentage > 0f && item.CurrentHull.WaterVolume > item.CurrentHull.Volume) + { + item.CurrentHull.Pressure += 30f * deltaTime; + if (CanCauseLethalPressure) { item.CurrentHull.LethalPressure += Hull.PressureBuildUpSpeed * deltaTime; } + } + + ApplyStatusEffects(ActionType.OnActive, deltaTime); + UpdateProjSpecific(deltaTime); } public void InfectBallast(Identifier identifier, bool allowMultiplePerShip = false) @@ -188,7 +212,7 @@ namespace Barotrauma.Items.Components public override float GetCurrentPowerConsumption(Connection connection = null) { //There shouldn't be other power connections to this - if (connection != this.powerIn || !IsActive) + if (connection != this.powerIn || !IsActive || Disabled) { return 0; } @@ -202,6 +226,8 @@ namespace Barotrauma.Items.Components partial void UpdateProjSpecific(float deltaTime); + partial void UpdateNetworking(float deltaTime); + public override void ReceiveSignal(Signal signal, Connection connection) { if (Hijacked) { return; } @@ -276,5 +302,11 @@ namespace Barotrauma.Items.Components } return true; } + + protected override void RemoveComponentSpecific() + { + base.RemoveComponentSpecific(); + linkedHulls.Clear(); + } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Power/PowerDistributor.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Power/PowerDistributor.cs index fd4340201..6086b0b24 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Power/PowerDistributor.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Power/PowerDistributor.cs @@ -82,6 +82,11 @@ namespace Barotrauma.Items.Components #if CLIENT CreateGUI(); + if (Screen.Selected is not { IsEditor: true }) + { + //set text via the property to refresh the UI + Name = name; + } #endif } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/TriggerComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/TriggerComponent.cs index 28728b077..b2ff80ee6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/TriggerComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/TriggerComponent.cs @@ -13,10 +13,24 @@ namespace Barotrauma.Items.Components { partial class TriggerComponent : ItemComponent { - [Editable, Serialize(0.0f, IsPropertySaveable.Yes, description: "The maximum amount of force applied to the triggering entitites.", alwaysUseInstanceValues: true)] + [Editable, Serialize(0f, IsPropertySaveable.Yes, description: "The maximum amount of force applied to the triggering entitites.", alwaysUseInstanceValues: true)] public float Force { get; set; } + + [Editable, Serialize("0,0", IsPropertySaveable.Yes, description: "The maximum amount of directional force applied to the triggering entitites.", alwaysUseInstanceValues: true)] + public Vector2 DirectionalForce { get; set; } + + [Editable, Serialize(false, IsPropertySaveable.Yes, $"If true, {nameof(DirectionalForce)} is relative to the angle between the target and the item, Similar to {nameof(Force)}.\nIf false, it always pushes in the same direction, with respect to the item's rotation.", alwaysUseInstanceValues: true)] + public bool RelativeDirectionalForce { get; set; } + + [Editable, Serialize(true, IsPropertySaveable.Yes, "If false, no vertical force will be applied.", alwaysUseInstanceValues: true)] + public bool VerticalForce { get; set; } + + [Editable, Serialize(true, IsPropertySaveable.Yes, "If false, no horizontal force will be applied.", alwaysUseInstanceValues: true)] + public bool HorizontalForce { get; set; } + [Editable, Serialize(false, IsPropertySaveable.Yes, description: "Determines if the force gets higher the closer the triggerer is to the center of the trigger.", alwaysUseInstanceValues: true)] public bool DistanceBasedForce { get; set; } + [Editable, Serialize(false, IsPropertySaveable.Yes, description: "Determines if the force fluctuates over time or if it stays constant.", alwaysUseInstanceValues: true)] public bool ForceFluctuation { get; set; } @@ -141,12 +155,29 @@ namespace Barotrauma.Items.Components get => base.IsActive; set { + bool wasActive = base.IsActive; + base.IsActive = value; if (!IsActive) { TriggerActive = false; triggerers.Clear(); } + else if (!wasActive && PhysicsBody?.FarseerBody != null) + { + //when the trigger becomes active, we need to check which entities are inside it + ContactEdge ce = PhysicsBody.FarseerBody.ContactList; + while (ce != null && ce.Contact != null) + { + if (ce.Contact.Enabled) + { + var thisFixture = ce.Contact.FixtureA.Body == PhysicsBody.FarseerBody ? ce.Contact.FixtureA : ce.Contact.FixtureB; + var otherFixture = ce.Contact.FixtureA.Body == PhysicsBody.FarseerBody ? ce.Contact.FixtureB : ce.Contact.FixtureA; + OnCollision(thisFixture, otherFixture, ce.Contact); + } + ce = ce.Next; + } + } } } @@ -374,7 +405,9 @@ namespace Barotrauma.Items.Components float amount = MathUtils.InverseLerp(-1.0f, 1.0f, v); CurrentForceFluctuation = MathHelper.Lerp(1.0f - ForceFluctuationStrength, 1.0f, amount); ForceFluctuationTimer = 0.0f; - GameMain.NetworkMember?.CreateEntityEvent(this); +#if SERVER + item.CreateServerEvent(this); +#endif } } @@ -398,7 +431,7 @@ namespace Barotrauma.Items.Components } } - if (Math.Abs(Force) < 0.01f) + if (Force < 0.01f && DirectionalForce.LengthSquared() < 0.0001f) { // Just ignore very minimal forces continue; @@ -436,7 +469,25 @@ namespace Barotrauma.Items.Components if (diff.LengthSquared() < 0.0001f) { return; } float distanceFactor = DistanceBasedForce ? LevelTrigger.GetDistanceFactor(body, PhysicsBody, RadiusInDisplayUnits) : 1.0f; if (distanceFactor <= 0.0f) { return; } - Vector2 force = distanceFactor * (CurrentForceFluctuation * Force) * Vector2.Normalize(diff) * multiplier; + Vector2 radialForce = Force * Vector2.Normalize(diff); + Vector2 directionalForce; + if (RelativeDirectionalForce) + { + directionalForce = DirectionalForce * new Vector2(Math.Sign(diff.X), Math.Sign(diff.Y)); + } + else + { + Vector2 flippedForce = DirectionalForce; + if (item.FlippedX) { flippedForce.X = -flippedForce.X; } + if (item.FlippedY) { flippedForce.Y = -flippedForce.Y; } + directionalForce = MathUtils.RotatePoint(flippedForce, -item.RotationRad); + } + + Vector2 force = (radialForce + directionalForce) * CurrentForceFluctuation * distanceFactor * multiplier; + + if (!HorizontalForce) { force.Y = 0.0f; } + if (!VerticalForce) { force.Y = 0.0f; } + if (force.LengthSquared() < 0.01f) { return; } if (body.Mass < 1) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index 7fe35e286..945590ae3 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -461,20 +461,15 @@ namespace Barotrauma } } - public float ImpactTolerance - { - get { return Prefab.ImpactTolerance; } - } - - public float InteractDistance - { - get { return Prefab.InteractDistance; } - } + public float ImpactTolerance => Prefab.ImpactTolerance; - public float InteractPriority - { - get { return Prefab.InteractPriority; } - } + public float ImpactDamage => Prefab.ImpactDamage; + public float ImpactDamageProbability => Prefab.ImpactDamageProbability; + + public float InteractDistance => Prefab.InteractDistance; + + public float InteractPriority => Prefab.InteractPriority; + public override Vector2 Position { @@ -1767,7 +1762,7 @@ namespace Barotrauma ic.Move(amount, ignoreContacts); } - if (body != null && (Submarine == null || !Submarine.Loading)) { FindHull(); } + if (body != null && (Submarine == null || !Submarine.Loading) || Screen.Selected is { IsEditor: true }) { FindHull(); } } public Rectangle TransformTrigger(Rectangle trigger, bool world = false) @@ -2387,7 +2382,7 @@ namespace Barotrauma { while (impactQueue.TryDequeue(out float impact)) { - HandleCollision(impact); + ReceiveImpact(impact); } } if (isDroppedStackOwner && body != null) @@ -2461,7 +2456,7 @@ namespace Barotrauma if (ic.IsActive || ic.UpdateWhenInactive) { - if (condition <= 0.0f) + if (!ic.UpdateWhenBroken && condition <= 0.0f) { ic.UpdateBroken(deltaTime, cam); } @@ -2713,25 +2708,35 @@ namespace Barotrauma return true; } - private void HandleCollision(float impact) + public void ReceiveImpact(float impactStrength, bool recursive = true) { - OnCollisionProjSpecific(impact); + OnCollisionProjSpecific(impactStrength); if (GameMain.NetworkMember is { IsClient: true }) { return; } - if (ImpactTolerance > 0.0f && Math.Abs(impact) > ImpactTolerance && hasStatusEffectsOfType[(int)ActionType.OnImpact]) + if (ImpactTolerance > 0.0f && Math.Abs(impactStrength) > ImpactTolerance && Rand.Range(0.0f, 1.0f) < ImpactDamageProbability) { - foreach (StatusEffect effect in statusEffectLists[ActionType.OnImpact]) + if (ImpactDamage != 0.0f) { - ApplyStatusEffect(effect, ActionType.OnImpact, deltaTime: 1.0f); + Condition -= impactStrength * ImpactDamage; } + + if (hasStatusEffectsOfType[(int)ActionType.OnImpact]) + { + foreach (StatusEffect effect in statusEffectLists[ActionType.OnImpact]) + { + ApplyStatusEffect(effect, ActionType.OnImpact, deltaTime: 1.0f); + } #if SERVER - GameMain.Server?.CreateEntityEvent(this, new ApplyStatusEffectEventData(ActionType.OnImpact)); + GameMain.Server?.CreateEntityEvent(this, new ApplyStatusEffectEventData(ActionType.OnImpact)); #endif + } } + if (!recursive) { return; } + foreach (Item contained in ContainedItems) { - if (contained.body != null) { contained.HandleCollision(impact); } + if (contained.body != null) { contained.ReceiveImpact(impactStrength, recursive: true); } } } @@ -3698,18 +3703,15 @@ namespace Barotrauma SerializableProperty property = extraData.SerializableProperty; ISerializableEntity entity = extraData.Entity; - msg.WriteVariableUInt32((uint)allProperties.Count); - if (property != null) { if (allProperties.Count > 1) { - int propertyIndex = allProperties.FindIndex(p => p.property == property && p.obj == entity); - if (propertyIndex < 0) + if (allProperties.None(p => p.property == property && p.obj == entity)) { throw new Exception($"Could not find the property \"{property.Name}\" in \"{entity.Name ?? "null"}\""); } - msg.WriteVariableUInt32((uint)propertyIndex); + msg.WriteIdentifier(property.Name.ToIdentifier()); } object value = property.GetValue(entity); @@ -3814,21 +3816,11 @@ namespace Barotrauma var allProperties = inGameEditableOnly ? GetInGameEditableProperties(ignoreConditions: true) : GetProperties(); if (allProperties.Count == 0) { return; } - int propertyCount = (int)msg.ReadVariableUInt32(); - if (propertyCount != allProperties.Count) + Identifier propertyIdentifier = msg.ReadIdentifier(); + int propertyIndex = allProperties.IndexOf(p => p.property.Name == propertyIdentifier); + if (propertyIndex < 0) { - throw new Exception($"Error in {nameof(ReadPropertyChange)}. The number of properties on the item \"{Prefab.Identifier}\" does not match between the server and the client. Server: {propertyCount}, client: {allProperties.Count}."); - } - - int propertyIndex = 0; - if (allProperties.Count > 1) - { - propertyIndex = (int)msg.ReadVariableUInt32(); - } - - if (propertyIndex >= allProperties.Count || propertyIndex < 0) - { - throw new Exception($"Error in {nameof(ReadPropertyChange)}. Property index out of bounds (item: {Prefab.Identifier}, index: {propertyIndex}, property count: {allProperties.Count}, in-game editable only: {inGameEditableOnly})"); + throw new Exception($"Error in {nameof(ReadPropertyChange)}. Could not find the property \"{propertyIdentifier}\" in item \"{Prefab.Identifier}\" (property count: {allProperties.Count}, in-game editable only: {inGameEditableOnly})"); } bool allowEditing = true; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/ItemPrefab.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/ItemPrefab.cs index 3ae662440..c8357fc58 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/ItemPrefab.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/ItemPrefab.cs @@ -821,6 +821,15 @@ namespace Barotrauma set { impactTolerance = Math.Max(value, 0.0f); } } + [Serialize(0.0f, IsPropertySaveable.No, description: "The amount of damage the item takes from impacts. Acts as a multiplier on the strength of the impact. Note that ImpactTolerance must be set for impacts to register.")] + public float ImpactDamage { get; set; } + + [Serialize(1.0f, IsPropertySaveable.No, description: "Probability for impacts to register. Defaults to 1. Note that ImpactTolerance must also be set for impacts to register.")] + public float ImpactDamageProbability { get; set; } + + [Serialize(false, IsPropertySaveable.No, "If true, submarine impacts will trigger OnImpact effects. Only applies to items with a null or non-dynamic physics body - items with dynamic bodies always react to impacts.")] + public bool ReceiveSubmarineImpacts { get; set; } + [Serialize(0.0f, IsPropertySaveable.No)] public float OnDamagedThreshold { get; set; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/RelatedItem.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/RelatedItem.cs index e3250d7cf..2bb9cb587 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/RelatedItem.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/RelatedItem.cs @@ -102,12 +102,12 @@ namespace Barotrauma } /// - /// Index of the slot the target must be in when targeting a Contained item + /// Index of the slot the target must be in when targeting a Contained item or a character inventory. /// public int TargetSlot = -1; /// - /// The slot type the target must be in when targeting an item contained inside a character's inventory + /// The slot type the target must be in when targeting an item contained inside a character's inventory. /// public InvSlotType CharacterInventorySlotType; @@ -329,7 +329,6 @@ namespace Barotrauma IgnoreInEditor = element.GetAttributeBool("ignoreineditor", false); MatchOnEmpty = element.GetAttributeBool("matchonempty", false); TargetSlot = element.GetAttributeInt("targetslot", -1); - } public bool CheckRequirements(Character character, Item parentItem) @@ -344,22 +343,21 @@ namespace Barotrauma return CheckItem(parentItem.Container, this); case RelationType.Equipped: if (character == null) { return false; } - var heldItems = character.HeldItems; - if (RequireOrMatchOnEmpty && heldItems.None()) { return true; } - foreach (Item equippedItem in heldItems) + foreach (var item in character.Inventory.AllItemsMod) { - if (equippedItem == null) { continue; } - if (CheckItem(equippedItem, this)) + if (character.HasEquippedItem(item) && CheckItem(item, this)) { - if (RequireEmpty && equippedItem.Condition > 0) { return false; } + if (RequireEmpty && item.Condition > 0) { return false; } return true; } } - break; + //got this far -> no matching item was equipped + //return true if we require or want to match "empty" (no matching item), otherwise false + return RequireOrMatchOnEmpty; case RelationType.Picked: if (character == null) { return false; } if (character.Inventory == null) { return MatchOnEmpty || RequireEmpty; } - var allItems = character.Inventory.AllItems; + var allItems = TargetSlot == -1 ? character.Inventory.AllItems : character.Inventory.GetItemsAt(TargetSlot); if (RequireOrMatchOnEmpty && allItems.None()) { return true; } foreach (Item pickedItem in allItems) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Explosion.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Explosion.cs index 179355407..ea8a21405 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Explosion.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Explosion.cs @@ -396,7 +396,10 @@ namespace Barotrauma } } - if (MathUtils.NearlyEqual(force, 0.0f) && MathUtils.NearlyEqual(Attack.Stun, 0.0f) && Attack.Afflictions.None()) + if (Attack.Afflictions.None() && + MathUtils.NearlyEqual(force, 0.0f) && MathUtils.NearlyEqual(Attack.Stun, 0.0f) && + MathUtils.NearlyEqual(Attack.ItemDamage, 0.0f) && + MathUtils.NearlyEqual(Attack.StructureDamage, 0.0f)) { return; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs index b589458a1..07e187866 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs @@ -370,18 +370,31 @@ namespace Barotrauma public override void Update(float deltaTime, Camera cam) { + Hull hull1 = linkedTo.Count < 1 ? null : linkedTo[0] as Hull; + Hull hull2 = linkedTo.Count < 2 ? null : (Hull)linkedTo[1]; + int updateInterval = 4; - float flowMagnitude = flowForce.LengthSquared(); - if (flowMagnitude < 1.0f) + //if one hull is at lethal pressure (connected to outside), and the other not yet, + //we need frequent updates to quickly move water into the other hull + if (hull1 != null && hull2 != null && + hull1.LethalPressure > 0.0f != hull2.LethalPressure > 0.0f) { - //very sparse updates if there's practically no water moving - updateInterval = 8; - } - else if (linkedTo.Count == 2 && flowMagnitude > 10.0f) - { - //frequent updates if water is moving between hulls updateInterval = 1; } + else + { + float flowMagnitude = flowForce.LengthSquared(); + if (flowMagnitude < 1.0f) + { + //very sparse updates if there's practically no water moving + updateInterval = 8; + } + else if (linkedTo.Count == 2 && flowMagnitude > 10.0f) + { + //frequent updates if water is moving between hulls + updateInterval = 1; + } + } updateCount++; if (updateCount < updateInterval) { return; } @@ -409,8 +422,6 @@ namespace Barotrauma return; } - Hull hull1 = (Hull)linkedTo[0]; - Hull hull2 = linkedTo.Count < 2 ? null : (Hull)linkedTo[1]; if (hull1 == hull2) { return; } UpdateOxygen(hull1, hull2, deltaTime); @@ -469,6 +480,8 @@ namespace Barotrauma higherSurface = Math.Max(hull1.Surface, hull2.Surface + subOffset.Y); float delta = 0.0f; + Hull flowSourceHull = null; + //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) { @@ -479,10 +492,9 @@ namespace Barotrauma { 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; + flowSourceHull = hull2; //make sure not to move more than what the room contains delta = Math.Min(((hull2.Pressure + subOffset.Y) - hull1.Pressure) * 300.0f * sizeModifier * deltaTime, Math.Min(hull2.WaterVolume, hull2.Volume)); @@ -504,6 +516,7 @@ namespace Barotrauma lowerSurface = hull2.Surface - hull2.WaveY[hull2.WaveY.Length - 1]; flowTargetHull = hull2; + flowSourceHull = hull1; //make sure not to move more than what the room contains delta = Math.Min((hull1.Pressure - (hull2.Pressure + subOffset.Y)) * 300.0f * sizeModifier * deltaTime, Math.Min(hull1.WaterVolume, hull1.Volume)); @@ -547,7 +560,6 @@ namespace Barotrauma if (hull2.Pressure + subOffset.Y > hull1.Pressure && hull2.WaterVolume > 0.0f) { float delta = Math.Min(hull2.WaterVolume - hull2.Volume + (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) { @@ -623,19 +635,30 @@ namespace Barotrauma } } - void UpdateRoomToOut(float deltaTime, Hull hull1) + /// + /// How much water can flow through the gap to the hull if the gap is connected outside. + /// + private float GetWaterFlowFromOutside(Hull hull, float deltaTime, bool ignoreCurrentWater = false) { //a variable affecting the water flow through the gap //the larger the gap is, the faster the water flows float sizeModifier = Size * open * open * (1.0f - overlappingGapFlowRateReduction); - float delta = 500.0f * sizeModifier * deltaTime; + if (!ignoreCurrentWater) + { + delta = Math.Min(delta, hull.Volume * Hull.MaxCompress - hull.WaterVolume); + } + return delta; + } + + void UpdateRoomToOut(float deltaTime, Hull hull1) + { + float delta = GetWaterFlowFromOutside(hull1, deltaTime); //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; - if (hull1.WaterVolume > hull1.Volume) { hull1.Pressure += 30.0f * deltaTime; } + if (hull1.WaterVolume > hull1.Volume) { hull1.Pressure += 100.0f * deltaTime; } flowTargetHull = hull1; @@ -698,6 +721,65 @@ namespace Barotrauma hull1.LethalPressure += ((Submarine != null && Submarine.AtDamageDepth) ? 100.0f : Hull.PressureBuildUpSpeed) * PressureDistributionSpeed * deltaTime; } } + + if (hull1.LethalPressure > 0) + { + SimulateWaterFlowFromOutsideToConnectedHulls(hull1, maxFlow: GetWaterFlowFromOutside(hull1, deltaTime, ignoreCurrentWater: true), deltaTime: deltaTime); + } + } + + private Hull GetOtherLinkedHull(Hull hull1) + { + if (linkedTo.Count != 2 || hull1 == null) { return null; } + return (linkedTo[0] == hull1 ? linkedTo[1] : linkedTo[0]) as Hull; + } + + private static readonly HashSet checkedHulls = new HashSet(); + + /// + /// Simulates water flow from the source to all the hulls it's connected to across the sub, as if the water was coming directly from outside. + /// Used to prevent gaps from slowing down flooding when hulls are directly connected outside and highly pressurized. + /// + void SimulateWaterFlowFromOutsideToConnectedHulls(Hull hull, float maxFlow, float deltaTime) + { + checkedHulls.Clear(); + checkedHulls.Add(hull); + foreach (var connectedGap in hull.ConnectedGaps) + { + if (connectedGap == this || !connectedGap.IsRoomToRoom || connectedGap.open <= 0.0f) { continue; } + var otherHull = connectedGap.GetOtherLinkedHull(hull); + if (otherHull == null) { continue; } + SimulateWaterFlowFromOutsideToConnectedHullsRecursive(otherHull, connectedGap, checkedHulls, hull, maxFlow, deltaTime); + } + } + + static void SimulateWaterFlowFromOutsideToConnectedHullsRecursive(Hull targetHull, Gap gap, HashSet checkedHulls, Hull originHull, float maxFlow, float deltaTime) + { + const float decay = 0.95f; + + maxFlow = Math.Min(maxFlow, gap.GetWaterFlowFromOutside(targetHull, deltaTime, ignoreCurrentWater: true)) * decay; + if (maxFlow <= 0.001f) { return; } + + checkedHulls.Add(targetHull); + + //don't multiply by deltatime here, we already did that in GetWaterFlowFromOutside + targetHull.WaterVolume += maxFlow; + //lerp lethal pressure up very fast + if (targetHull.WaterVolume > targetHull.Volume) + { + targetHull.LethalPressure = Math.Max(targetHull.LethalPressure, MathHelper.Lerp(targetHull.LethalPressure, originHull.LethalPressure, 0.1f)); + } + + //stop pushing water to the following hulls once we get to a hull that's not at high pressure yet + if (targetHull.LethalPressure <= 0 || targetHull.WaterVolume < targetHull.Volume) { return; } + + foreach (var connectedGap in targetHull.ConnectedGaps) + { + if (connectedGap == gap || !connectedGap.IsRoomToRoom || connectedGap.open <= 0.0f) { continue; } + var otherHull = connectedGap.GetOtherLinkedHull(targetHull); + if (otherHull == null || checkedHulls.Contains(otherHull)) { continue; } + SimulateWaterFlowFromOutsideToConnectedHullsRecursive(otherHull, connectedGap, checkedHulls, originHull, maxFlow, deltaTime); + } } public bool RefreshOutsideCollider() @@ -884,6 +966,8 @@ namespace Barotrauma base.Remove(); GapList.Remove(this); + checkedHulls.Clear(); + foreach (Hull hull in Hull.HullList) { hull.ConnectedGaps.Remove(this); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Hull.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Hull.cs index dce43c267..2a79f9482 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Hull.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Hull.cs @@ -785,7 +785,7 @@ namespace Barotrauma #region Shared network write private void SharedStatusWrite(IWriteMessage msg) { - msg.WriteRangedSingle(MathHelper.Clamp(waterVolume / Volume, 0.0f, 1.5f), 0.0f, 1.5f, 8); + msg.WriteSingle(waterVolume); System.Diagnostics.Debug.Assert(FireSources.Count <= MaxFireSources, $"Too many fire sources ({FireSources.Count}) in hull {ID} (max {MaxFireSources})."); msg.WriteRangedInteger(Math.Min(FireSources.Count, MaxFireSources), 0, MaxFireSources); @@ -833,7 +833,7 @@ namespace Barotrauma private void SharedStatusRead(IReadMessage msg, out float newWaterVolume, out NetworkFireSource[] newFireSources) { - newWaterVolume = msg.ReadRangedSingle(0.0f, 1.5f, 8) * Volume; + newWaterVolume = msg.ReadSingle(); int fireSourceCount = msg.ReadRangedInteger(0, MaxFireSources); newFireSources = new NetworkFireSource[fireSourceCount]; @@ -1269,6 +1269,23 @@ namespace Barotrauma return null; } + /// + /// Recursively find all the hulls linked to the specified hull. + /// + public void GetLinkedHulls(List linkedHulls, bool includeHiddenHulls = false) + { + foreach (var linkedEntity in linkedTo) + { + if (linkedEntity is Hull linkedHull) + { + if (linkedHulls.Contains(linkedHull)) { continue; } + if (!includeHiddenHulls && linkedHull.IsHidden) { continue; } + linkedHulls.Add(linkedHull); + linkedHull.GetLinkedHulls(linkedHulls, includeHiddenHulls); + } + } + } + public static void DetectItemVisibility(Character c=null) { if (c==null) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/Level.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/Level.cs index 45f5c4244..c5f0d161f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/Level.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/Level.cs @@ -4291,6 +4291,11 @@ namespace Barotrauma { placeableWrecks.RemoveAt(i); } + // Exclude wrecks that have mission tags, those can't show up randomly + else if (wreckInfo.MissionTags.Count != 0) + { + placeableWrecks.RemoveAt(i); + } } if (placeableWrecks.None()) { @@ -4742,6 +4747,11 @@ namespace Barotrauma { beaconStationFiles.RemoveAt(i); } + // Exclude beacons that have mission tags, those can't show up randomly + else if (beaconInfo.MissionTags.Count != 0) + { + beaconStationFiles.RemoveAt(i); + } } } if (beaconStationFiles.None()) @@ -4926,17 +4936,17 @@ namespace Barotrauma { int corpseCount = Rand.Range(Loaded.GenerationParams.MinCorpseCount, Loaded.GenerationParams.MaxCorpseCount + 1); var allSpawnPoints = WayPoint.WayPointList.FindAll(wp => wp.Submarine == wreck && wp.CurrentHull != null); - var pathPoints = allSpawnPoints.FindAll(wp => wp.SpawnType == SpawnType.Path); + var humanSpawnPoints = allSpawnPoints.FindAll(wp => wp.SpawnType == SpawnType.Human); var corpsePoints = allSpawnPoints.FindAll(wp => wp.SpawnType == SpawnType.Corpse); - if (!corpsePoints.Any() && !pathPoints.Any()) { continue; } - pathPoints.Shuffle(Rand.RandSync.ServerAndClient); + if (corpsePoints.None() && humanSpawnPoints.None()) { continue; } + humanSpawnPoints.Shuffle(Rand.RandSync.ServerAndClient); // Sort by job so that we first spawn those with a predefined job (might have special id cards) corpsePoints = corpsePoints.OrderBy(p => p.AssignedJob == null).ThenBy(p => Rand.Value()).ToList(); var usedJobs = new HashSet(); int spawnCounter = 0; for (int j = 0; j < corpseCount; j++) { - WayPoint sp = corpsePoints.FirstOrDefault() ?? pathPoints.FirstOrDefault(); + WayPoint sp = corpsePoints.FirstOrDefault() ?? humanSpawnPoints.FirstOrDefault(); JobPrefab job = sp?.AssignedJob; CorpsePrefab selectedPrefab; if (job == null) @@ -4949,8 +4959,8 @@ namespace Barotrauma if (selectedPrefab == null) { corpsePoints.Remove(sp); - pathPoints.Remove(sp); - sp = corpsePoints.FirstOrDefault(sp => sp.AssignedJob == null) ?? pathPoints.FirstOrDefault(sp => sp.AssignedJob == null); + humanSpawnPoints.Remove(sp); + sp = corpsePoints.FirstOrDefault(sp => sp.AssignedJob == null) ?? humanSpawnPoints.FirstOrDefault(sp => sp.AssignedJob == null); // Deduce the job from the selected prefab selectedPrefab = GetCorpsePrefab(usedJobs); if (selectedPrefab != null) @@ -4972,7 +4982,7 @@ namespace Barotrauma { worldPos = sp.WorldPosition; corpsePoints.Remove(sp); - pathPoints.Remove(sp); + humanSpawnPoints.Remove(sp); } job ??= selectedPrefab.GetJobPrefab(predicate: p => !usedJobs.Contains(p)); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Location.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Location.cs index 95d602dce..c55e16485 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Location.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Location.cs @@ -473,7 +473,7 @@ namespace Barotrauma { get { - availableMissions.RemoveAll(m => m.Completed || (m.Failed && !m.Prefab.AllowRetry)); + availableMissions.RemoveAll(m => m.Completed || (m.Failed && !m.Prefab.AllowRetry) || m.ForceFailure); return availableMissions; } } @@ -1091,6 +1091,12 @@ namespace Barotrauma mission.TimesAttempted = loadedMission.TimesAttempted; availableMissions.Add(mission); if (loadedMission.SelectedMission) { selectedMissions.Add(mission); } + + var levelData = destination == this ? LevelData : Connections.FirstOrDefault(c => c.OtherLocation(this) == destination)?.LevelData; + if (levelData != null) + { + mission.AdjustLevelData(levelData); + } } loadedMissions = null; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/LocationConnection.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/LocationConnection.cs index 964aaceb3..6506f3d1d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/LocationConnection.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/LocationConnection.cs @@ -39,7 +39,7 @@ namespace Barotrauma { get { - availableMissions.RemoveAll(m => m.Completed || (m.Failed && !m.Prefab.AllowRetry)); + availableMissions.RemoveAll(m => m.Completed || (m.Failed && !m.Prefab.AllowRetry) || m.ForceFailure); return availableMissions; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Map.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Map.cs index dbfb5ece0..785f729c6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Map.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Map/Map.cs @@ -235,15 +235,17 @@ namespace Barotrauma //backwards compatibility (or support for loading maps created with mods that modify the end biome setup): //if there's too few end locations, create more - int missingOutpostCount = endLocations.First().Biome.EndBiomeLocationCount - endLocations.Count; - Location firstEndLocation = EndLocations[0]; + Biome endBiome = firstEndLocation.Biome; + int missingOutpostCount = endBiome.EndBiomeLocationCount - endLocations.Count; + for (int i = 0; i < missingOutpostCount; i++) { Vector2 mapPos = new Vector2( MathHelper.Lerp(firstEndLocation.MapPosition.X, Width, MathHelper.Lerp(0.2f, 0.8f, i / (float)missingOutpostCount)), Height * MathHelper.Lerp(0.2f, 1.0f, (float)rand.NextDouble())); - var newEndLocation = new Location(mapPos, generationParams.DifficultyZones, firstEndLocation.Biome.Identifier, rand, forceLocationType: firstEndLocation.Type, existingLocations: Locations); + var newEndLocation = new Location(mapPos, generationParams.DifficultyZones, endBiome.Identifier, rand, forceLocationType: firstEndLocation.Type, existingLocations: Locations); + newEndLocation.Biome = endBiome; newEndLocation.LevelData = new LevelData(newEndLocation, this, difficulty: 100.0f); Locations.Add(newEndLocation); endLocations.Add(newEndLocation); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/ExtraSubmarineInfo.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/ExtraSubmarineInfo.cs index 2f46623fe..533b283e0 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/ExtraSubmarineInfo.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/ExtraSubmarineInfo.cs @@ -11,6 +11,8 @@ namespace Barotrauma public Dictionary SerializableProperties { get; protected set; } + public HashSet MissionTags { get; } = []; + [Serialize(0.0f, IsPropertySaveable.Yes), Editable] public float MinLevelDifficulty { get; set; } @@ -21,6 +23,10 @@ namespace Barotrauma { Name = $"{nameof(ExtraSubmarineInfo)} ({submarineInfo.Name})"; SerializableProperties = SerializableProperty.DeserializeProperties(this, element); + foreach (var missionTag in element.GetAttributeIdentifierArray(nameof(MissionTags), [])) + { + MissionTags.Add(missionTag); + } } public ExtraSubmarineInfo(SubmarineInfo submarineInfo) @@ -41,11 +47,18 @@ namespace Barotrauma kvp.Value.TrySetValue(this, kvp.Value.GetValue(original)); } } + foreach (var missionTag in original.MissionTags) + { + MissionTags.Add(missionTag); + } } public virtual void Save(XElement element) { SerializableProperty.SerializeProperties(this, element); + // MissionTags is not automatically serialized because HashSet is not a supported type + // We need to manually serialize it as a comma-separated string + element.SetAttributeValue(nameof(MissionTags), string.Join(',', MissionTags)); } } @@ -135,18 +148,10 @@ namespace Barotrauma [Serialize(50.0f, IsPropertySaveable.Yes), Editable] public float PreferredDifficulty { get; set; } - private readonly HashSet missionTags = new HashSet(); - - public HashSet MissionTags => missionTags; - public EnemySubmarineInfo(SubmarineInfo submarineInfo, XElement element) : base(submarineInfo, element) { Name = $"{nameof(EnemySubmarineInfo)} ({submarineInfo.Name})"; SerializableProperties = SerializableProperty.DeserializeProperties(this, element); - foreach (var missionTag in element.GetAttributeIdentifierArray(nameof(MissionTags), Array.Empty())) - { - missionTags.Add(missionTag); - } } public EnemySubmarineInfo(SubmarineInfo submarineInfo) : base(submarineInfo) @@ -156,16 +161,8 @@ namespace Barotrauma public EnemySubmarineInfo(EnemySubmarineInfo original) : base(original) { - foreach (var missionTag in original.missionTags) - { - missionTags.Add(missionTag); - } } - public override void Save(XElement element) - { - base.Save(element); - element.Add(new XAttribute(nameof(MissionTags), string.Join(',', missionTags))); - } + } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/OutpostGenerator.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/OutpostGenerator.cs index dcb60b4b1..8b840c161 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/OutpostGenerator.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Outposts/OutpostGenerator.cs @@ -159,11 +159,11 @@ namespace Barotrauma if (GameMain.NetworkMember?.ServerSettings is { } serverSettings && serverSettings.SelectedOutpostName != "Random") { + var matchingOutpost = outpostInfos.FirstOrDefault(o => o.Name == serverSettings.SelectedOutpostName); //...but only if the outpost is suitable for the mission (or if the mission has no specific requirements for the outpost) - if (outpostInfosSuitableForMission.None() || - outpostInfosSuitableForMission.Any(outpostInfo => outpostInfo.OutpostTags.Contains(serverSettings.SelectedOutpostName))) + if (outpostInfosSuitableForMission.Contains(matchingOutpost) || + outpostInfosSuitableForMission.None()) { - var matchingOutpost = outpostInfos.FirstOrDefault(o => o.Name == serverSettings.SelectedOutpostName); if (matchingOutpost != null) { return matchingOutpost; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/PriceInfo.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/PriceInfo.cs index ae4124505..529263889 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/PriceInfo.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/PriceInfo.cs @@ -37,14 +37,14 @@ namespace Barotrauma public bool RequiresUnlock { get; } /// - /// Used when neither or are defined. + /// Default minimum amount when no MinAvailableAmount is defined. /// - private const int DefaultAmount = 5; + private const int DefaultMinAmount = 1; /// - /// How much more the maximum stock is relative to the minimum stock if not defined. Stores will gradually stock up towards the maximum. + /// Default maximum amount when no MaxAvailableAmount is defined. /// - private const float DefaultMaxAvailabilityRelativeToMin = 1.2f; + private const int DefaultMaxAmount = 5; /// /// If set, the item is only available in outposts with this faction. @@ -66,11 +66,12 @@ namespace Barotrauma public PriceInfo(XElement element) { Price = element.GetAttributeInt("buyprice", 0); - MinLevelDifficulty = element.GetAttributeInt("minleveldifficulty", 0); + MinLevelDifficulty = GetMinLevelDifficulty(element, 0); BuyingPriceMultiplier = element.GetAttributeFloat("buyingpricemultiplier", 1f); CanBeBought = true; - MinAvailableAmount = Math.Min(GetMinAmount(element, defaultValue: DefaultAmount), CargoManager.MaxQuantity); - MaxAvailableAmount = MathHelper.Clamp(GetMaxAmount(element, defaultValue: (int)(MinAvailableAmount * DefaultMaxAvailabilityRelativeToMin)), MinAvailableAmount, CargoManager.MaxQuantity); + MinAvailableAmount = Math.Min(GetMinAmount(element, defaultValue: DefaultMinAmount), CargoManager.MaxQuantity); + int maxAmount = GetMaxAmount(element, defaultValue: DefaultMaxAmount); + MaxAvailableAmount = MathHelper.Clamp(maxAmount, MinAvailableAmount, CargoManager.MaxQuantity); RequiresUnlock = element.GetAttributeBool("requiresunlock", false); RequiredFaction = element.GetAttributeIdentifier(nameof(RequiredFaction), Identifier.Empty); System.Diagnostics.Debug.Assert(MaxAvailableAmount >= MinAvailableAmount); @@ -112,27 +113,27 @@ namespace Barotrauma var priceInfos = new List(); defaultPrice = null; int basePrice = element.GetAttributeInt("baseprice", 0); - int minAmount = GetMinAmount(element, defaultValue: DefaultAmount); - int maxAmount = GetMaxAmount(element, defaultValue: (int)(DefaultAmount * DefaultMaxAvailabilityRelativeToMin)); - int minLevelDifficulty = element.GetAttributeInt("minleveldifficulty", 0); + int minAmount = GetMinAmount(element, defaultValue: DefaultMinAmount); + int maxAmount = GetMaxAmount(element, defaultValue: DefaultMaxAmount); + int minLevelDifficulty = GetMinLevelDifficulty(element, 0); bool canBeSpecial = element.GetAttributeBool("canbespecial", true); float buyingPriceMultiplier = element.GetAttributeFloat("buyingpricemultiplier", 1f); bool displayNonEmpty = element.GetAttributeBool("displaynonempty", false); - bool soldByDefault = element.GetAttributeBool("sold", element.GetAttributeBool("soldbydefault", true)); + bool soldByDefault = GetSold(element, element.GetAttributeBool("soldbydefault", true)); bool requiresUnlock = element.GetAttributeBool("requiresunlock", false); Identifier requiredFactionByDefault = element.GetAttributeIdentifier(nameof(RequiredFaction), Identifier.Empty); foreach (XElement childElement in element.GetChildElements("price")) { float priceMultiplier = childElement.GetAttributeFloat("multiplier", 1.0f); - bool sold = childElement.GetAttributeBool("sold", soldByDefault); - int storeMinLevelDifficulty = childElement.GetAttributeInt("minleveldifficulty", minLevelDifficulty); + bool sold = GetSold(childElement, soldByDefault); + int storeMinLevelDifficulty = GetMinLevelDifficulty(childElement, minLevelDifficulty); float storeBuyingMultiplier = childElement.GetAttributeFloat("buyingpricemultiplier", buyingPriceMultiplier); string backwardsCompatibleIdentifier = childElement.GetAttributeString("locationtype", ""); if (!string.IsNullOrEmpty(backwardsCompatibleIdentifier)) { backwardsCompatibleIdentifier = $"merchant{backwardsCompatibleIdentifier}"; } - string storeIdentifier = childElement.GetAttributeString("storeidentifier", backwardsCompatibleIdentifier); + string storeIdentifier = GetStoreIdentifier(childElement, backwardsCompatibleIdentifier); // TODO: Add some error messages if we have defined the min or max amount while the item is not sold var priceInfo = new PriceInfo(price: (int)(priceMultiplier * basePrice), canBeBought: sold, @@ -167,12 +168,40 @@ namespace Barotrauma return priceInfos; } - private static int GetMinAmount(XElement element, int defaultValue) => element != null ? - element.GetAttributeInt("minamount", element.GetAttributeInt("minavailable", defaultValue)) : - defaultValue; + private static int GetMinAmount(XElement element, int defaultValue) => + element?.GetAttributeInt("minamount", element.GetAttributeInt("minavailable", defaultValue)) ?? defaultValue; - private static int GetMaxAmount(XElement element, int defaultValue) => element != null ? - element.GetAttributeInt("maxamount", element.GetAttributeInt("maxavailable", defaultValue)) : - defaultValue; + private static int GetMaxAmount(XElement element, int defaultValue) => + element?.GetAttributeInt("maxamount", element.GetAttributeInt("maxavailable", defaultValue)) ?? defaultValue; + + public static bool HasMinAmountDefined(XElement element) => element != null && + (element.GetAttribute("minamount") != null || element.GetAttribute("minavailable") != null); + + public static bool HasMaxAmountDefined(XElement element) => element != null && + (element.GetAttribute("maxamount") != null || element.GetAttribute("maxavailable") != null); + + public static bool HasSoldDefined(XElement element) => element != null && + element.GetAttribute("sold") != null; + + public static string GetMinAmountString(XElement element) + { + if (element == null) { return null; } + return element.GetAttributeString("minamount", null) ?? element.GetAttributeString("minavailable", null); + } + + public static string GetMaxAmountString(XElement element) + { + if (element == null) { return null; } + return element.GetAttributeString("maxamount", null) ?? element.GetAttributeString("maxavailable", null); + } + + public static bool GetSold(XElement element, bool defaultValue = true) => + element?.GetAttributeBool("sold", defaultValue) ?? defaultValue; + + public static int GetMinLevelDifficulty(XElement element, int defaultValue = 0) => + element?.GetAttributeInt("minleveldifficulty", defaultValue) ?? defaultValue; + + public static string GetStoreIdentifier(XElement element, string defaultValue = "unknown") => + element?.GetAttributeString("storeidentifier", defaultValue) ?? defaultValue; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Submarine.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Submarine.cs index e3816f58c..45df0b97f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Submarine.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Submarine.cs @@ -343,6 +343,9 @@ namespace Barotrauma } + /// + /// Is the sub at the depth where it starts to take damage to appear due to the pressure? + /// public bool AtDamageDepth { get @@ -352,6 +355,18 @@ namespace Barotrauma } } + /// + /// Is the sub at the depth where cosmetic effects (e.g. camera shake) start to appear due to the pressure? + /// + public bool AtCosmeticDamageDepth + { + get + { + if (Level.Loaded == null || subBody == null) { return false; } + return RealWorldDepth > Level.Loaded.RealWorldCrushDepth + SubmarineBody.CosmeticDamageEffectThreshold && RealWorldDepth > RealWorldCrushDepth + SubmarineBody.CosmeticDamageEffectThreshold; + } + } + public bool IsRespawnShuttle => GameMain.NetworkMember?.RespawnManager is { } respawnManager && respawnManager.RespawnShuttles.Contains(this); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/SubmarineBody.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/SubmarineBody.cs index e1be7173a..28b8af8c5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/SubmarineBody.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/SubmarineBody.cs @@ -579,13 +579,16 @@ namespace Barotrauma Body.SetTransform(ConvertUnits.ToSimUnits(position), 0.0f); } + /// + /// Camera shake and sounds start playing 500 meters before crush depth + /// + public const float CosmeticDamageEffectThreshold = -500.0f; + private void UpdateDepthDamage(float deltaTime) { if (GameMain.GameSession?.GameMode is TestGameMode) { return; } if (Level.Loaded == null) { return; } - //camera shake and sounds start playing 500 meters before crush depth - const float CosmeticEffectThreshold = -500.0f; //breaches won't get any more severe 500 meters below crush depth const float MaxEffectThreshold = 500.0f; const float MinWallDamageProbability = 0.1f; @@ -598,7 +601,7 @@ namespace Barotrauma //(gives you a bit of time to react and return if you start the round in a level that's too deep) const float MinRoundDuration = 60.0f; - if (Submarine.RealWorldDepth < Level.Loaded.RealWorldCrushDepth + CosmeticEffectThreshold || Submarine.RealWorldDepth < Submarine.RealWorldCrushDepth + CosmeticEffectThreshold) + if (!Submarine.AtCosmeticDamageDepth) { return; } @@ -606,9 +609,9 @@ namespace Barotrauma damageSoundTimer -= deltaTime; if (damageSoundTimer <= 0.0f) { - const float PressureSoundRange = -CosmeticEffectThreshold; + const float PressureSoundRange = -CosmeticDamageEffectThreshold; //Ratio between 0 (where the 'approaching crush depth' indication starts) and 1 (at crush depth or past it) - float closenessToCrushDepthRatio = Math.Clamp((Submarine.RealWorldDepth - (Submarine.RealWorldCrushDepth + CosmeticEffectThreshold)) / PressureSoundRange, 0f, 1f); + float closenessToCrushDepthRatio = Math.Clamp((Submarine.RealWorldDepth - (Submarine.RealWorldCrushDepth + CosmeticDamageEffectThreshold)) / PressureSoundRange, 0f, 1f); #if CLIENT SoundPlayer.PlayDamageSound("pressure", MathHelper.Lerp(0f, 100f, closenessToCrushDepthRatio), submarine.WorldPosition + Rand.Vector(Rand.Range(0.0f, Math.Min(submarine.Borders.Width, submarine.Borders.Height))), 20000.0f, gain: 1f + closenessToCrushDepthRatio * 2); #endif @@ -1066,8 +1069,15 @@ namespace Barotrauma foreach (Item item in Item.ItemList) { - if (item.Submarine != submarine || item.CurrentHull == null || item.body == null || !item.body.Enabled) { continue; } - if (item.body.Mass > impulseMagnitude) { continue; } + if (item.Submarine != submarine) { continue; } + + if (item.body is not { BodyType: BodyType.Dynamic }) + { + if (!item.Prefab.ReceiveSubmarineImpacts) { continue; } + item.ReceiveImpact(impact, recursive: false); + } + + if (!item.body.Enabled || item.CurrentHull == null || item.body.Mass > impulseMagnitude) { continue; } item.body.ApplyLinearImpulse(impulse, 10.0f); item.PositionUpdateInterval = 0.0f; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Networking/INetSerializableStruct.cs b/Barotrauma/BarotraumaShared/SharedSource/Networking/INetSerializableStruct.cs index 9d2fbc195..69d121f72 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Networking/INetSerializableStruct.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Networking/INetSerializableStruct.cs @@ -820,16 +820,22 @@ namespace Barotrauma { int prevPos = inc.BitPosition; + const int MaxBytesToLog = 500; + StringBuilder hexData = new(); inc.BitPosition = 0; - while (inc.BitPosition < inc.LengthBits) + while (inc.BitPosition < inc.LengthBits && + inc.BytePosition < MaxBytesToLog) { byte b = inc.ReadByte(); hexData.Append($"{b:X2} "); } // trim the last space if there is one if (hexData.Length > 0) { hexData.Length--; } - + if (inc.BytePosition >= MaxBytesToLog) + { + hexData.Append($" (data truncated, {inc.LengthBytes} bytes in the full message)"); + } inc.BitPosition = prevPos; //only log the error once per sender, so this can't be abused by spamming the server with malformed data to fill up the console with errors diff --git a/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkExtensions.cs b/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkExtensions.cs index da74d80f2..4475b9b89 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkExtensions.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkExtensions.cs @@ -19,7 +19,7 @@ namespace Barotrauma.Networking return msg; } #endif - public static void WriteNetSerializableStruct(this IWriteMessage msg, INetSerializableStruct serializableStruct) + public static void WriteNetSerializableStruct(this IWriteMessage msg, T serializableStruct) where T : INetSerializableStruct { serializableStruct.Write(msg); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkPeerStructs.cs b/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkPeerStructs.cs index 99cbc056e..b484d0f78 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkPeerStructs.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Networking/Primitives/NetworkPeerStructs.cs @@ -94,6 +94,12 @@ namespace Barotrauma.Networking public Option RetriesLeft; } + [NetworkSerialize] + internal readonly record struct DoSProtectionPacket(string EndpointStr, bool ShouldBan) : INetSerializableStruct + { + public Option Endpoint => P2PEndpoint.Parse(EndpointStr); + } + [NetworkSerialize] internal readonly struct PeerDisconnectPacket : INetSerializableStruct { @@ -109,19 +115,24 @@ namespace Barotrauma.Networking AdditionalInformation = additionalInformation; } - public LocalizedString ChatMessage(Client c) + public LocalizedString ChatMessage(string? name) { + if (string.IsNullOrEmpty(name)) + { + name = TextManager.Get("ServerMessage.UnknownClient").Value; + } + LocalizedString message = DisconnectReason switch { DisconnectReason.Disconnected => TextManager.GetWithVariable("ServerMessage.ClientLeftServer", - "[client]", c.Name), - DisconnectReason.Banned => TextManager.GetWithVariable("servermessage.bannedfromserver", "[client]", c.Name), - DisconnectReason.Kicked => TextManager.GetWithVariable("servermessage.kickedfromserver", "[client]", c.Name), + "[client]", name), + DisconnectReason.Banned => TextManager.GetWithVariable("servermessage.bannedfromserver", "[client]", name), + DisconnectReason.Kicked => TextManager.GetWithVariable("servermessage.kickedfromserver", "[client]", name), _ => TextManager.GetWithVariables("ChatMsg.DisconnectedWithReason", - ("[client]", c.Name), + ("[client]", name), ("[reason]", TextManager.Get($"ChatMsg.DisconnectReason.{DisconnectReason}"))) }; - if (!string.IsNullOrEmpty(AdditionalInformation) && + if (!string.IsNullOrEmpty(AdditionalInformation) && DisconnectReason is DisconnectReason.Banned or DisconnectReason.Kicked) { message += " "+ TextManager.Get("banreason") + " " + TextManager.GetServerMessage(AdditionalInformation); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Networking/ServerLog.cs b/Barotrauma/BarotraumaShared/SharedSource/Networking/ServerLog.cs index b84244ee1..3f0795cc8 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Networking/ServerLog.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Networking/ServerLog.cs @@ -89,8 +89,6 @@ namespace Barotrauma.Networking private readonly Queue lines; private readonly Queue unsavedLines; - private readonly bool[] msgTypeHidden = new bool[Enum.GetValues(typeof(MessageType)).Length]; - public int LinesPerFile { get { return linesPerFile; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Prefabs/IImplementsVariants.cs b/Barotrauma/BarotraumaShared/SharedSource/Prefabs/IImplementsVariants.cs index 1c70a649d..33b259cc8 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Prefabs/IImplementsVariants.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Prefabs/IImplementsVariants.cs @@ -79,14 +79,13 @@ namespace Barotrauma bool cleared = false; foreach (var subElement in element.Elements()) { - if (elementNamesToRemove.Contains(subElement.NameAsIdentifier())) - { - if (!elementsToRemove.Contains(subElement)) { elementsToRemove.Add(subElement); } - matchingElementFound = true; - continue; - } if (replacementSubElement.Name.ToString().Equals("clearall", StringComparison.OrdinalIgnoreCase)) { + if (elementNamesToRemove.Contains(subElement.NameAsIdentifier())) + { + if (!elementsToRemove.Contains(subElement)) { elementsToRemove.Add(subElement); } + matchingElementFound = true; + } continue; } else if (replacementSubElement.Name.ToString().Equals("clear", StringComparison.OrdinalIgnoreCase)) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs b/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs index ab31f4582..440d9bdf7 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs @@ -876,7 +876,18 @@ namespace Barotrauma Dictionary dictionary = new Dictionary(); foreach (var property in properties) { - var serializableProperty = new SerializableProperty(property); + //if the getter is private, we must get it from the declaring type to access it and check if it exists + SerializableProperty serializableProperty = null; + try + { + serializableProperty = new SerializableProperty(property); + } + catch (AmbiguousMatchException) + { + //can happen e.g. with AnimController.CurrentGroundedParams, which is of an abstract type - + //let's just ignore these types of properties (you can't really do anything with SerializableProperties that are reference types anyway) + continue; + } dictionary.Add(serializableProperty.Name.ToIdentifier(), serializableProperty); } @@ -1066,6 +1077,15 @@ namespace Barotrauma } } } + else if (attributeName == "unlockrecipe" || attributeName == "unlockrecipes") + { + var recipes = subElement.GetAttributeIdentifierImmutableHashSet("unlockrecipes", + def: subElement.GetAttributeIdentifierImmutableHashSet("unlockrecipe", ImmutableHashSet.Empty)); + foreach (var recipe in recipes) + { + GameMain.GameSession?.UnlockRecipe(CharacterTeamType.Team1, recipe, showNotifications: false); + } + } if (entity.SerializableProperties.TryGetValue(attributeName, out SerializableProperty property)) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Settings/GameSettings.cs b/Barotrauma/BarotraumaShared/SharedSource/Settings/GameSettings.cs index 1f5e2d51b..eb44f9d6b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Settings/GameSettings.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Settings/GameSettings.cs @@ -696,7 +696,8 @@ namespace Barotrauma root.Add(CampaignSettings.CurrentSettings.Save()); #endif - configDoc.SaveSafe(PlayerConfigPath); + //allow retrying a few times because the file may be in use if the player is running multiple instances of the game on the same machine + configDoc.SaveSafe(PlayerConfigPath, maxRetries: 4); System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings { diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/PropertyConditional.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/PropertyConditional.cs index ba05591dd..87d64919a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/PropertyConditional.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/PropertyConditional.cs @@ -425,6 +425,11 @@ namespace Barotrauma { return PropertyMatchesRequirement(targetChar, characterProperty); } + else if (targetChar?.AnimController?.SerializableProperties is { } animControllerProperties + && animControllerProperties.TryGetValue(AttributeName, out var animControllerProperty)) + { + return PropertyMatchesRequirement(targetChar.AnimController, animControllerProperty); + } return ComparisonOperatorIsNotEquals; case ConditionType.SkillRequirement: if (targetChar != null) diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index 6777e0a04..3ca3b308d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -420,7 +420,7 @@ namespace Barotrauma [Serialize(1, IsPropertySaveable.No, description: "How many characters to spawn.")] public int Count { get; private set; } - [Serialize(false, IsPropertySaveable.No, description: + [Serialize(false, IsPropertySaveable.No, description: "Should the buffs of the character executing the effect be transferred to the spawned character?"+ " Useful for effects that \"transform\" a character to something else by deleting the character and spawning a new one on its place.")] public bool TransferBuffs { get; private set; } @@ -445,11 +445,11 @@ namespace Barotrauma [Serialize("", IsPropertySaveable.No, description: "An affliction to apply on the spawned character.")] public Identifier AfflictionOnSpawn { get; private set; } - [Serialize(1, IsPropertySaveable.No, description: + [Serialize(1, IsPropertySaveable.No, description: $"The strength of the affliction applied on the spawned character. Only relevant if {nameof(AfflictionOnSpawn)} is defined.")] public int AfflictionStrength { get; private set; } - [Serialize(false, IsPropertySaveable.No, description: + [Serialize(false, IsPropertySaveable.No, description: "Should the player controlling the character that executes the effect gain control of the spawned character?" + " Useful for effects that \"transform\" a character to something else by deleting the character and spawning a new one on its place.")] public bool TransferControl { get; private set; } @@ -470,7 +470,7 @@ namespace Barotrauma [Serialize(false, IsPropertySaveable.No)] public bool InheritEventTags { get; private set; } - + [Serialize(false, IsPropertySaveable.No, description: "Should the character team be inherited from the entity that owns the status effect?")] public bool InheritTeam { get; private set; } @@ -722,9 +722,9 @@ namespace Barotrauma private readonly List<(Identifier eventIdentifier, Identifier tag)> eventTargetTags; /// - /// Can be used to make the effect unlock a fabrication recipe globally for the entire crew. + /// Can be used to make the effect unlock a fabrication recipe (or multiple recipes separated by a comma) globally for the entire crew. /// - public readonly Identifier UnlockRecipe; + public readonly ImmutableHashSet UnlockRecipes; private Character user; @@ -740,6 +740,8 @@ namespace Barotrauma /// public readonly float SeverLimbsProbability; + private readonly Vector2 randomCondition; + public PhysicsBody sourceBody; /// @@ -802,11 +804,11 @@ namespace Barotrauma private readonly List talentTriggers; private readonly List giveExperiences; private readonly List giveSkills; - + private HashSet<(Character targetCharacter, AnimLoadInfo anim)> failedAnimations; public readonly record struct AnimLoadInfo(AnimationType Type, Either File, float Priority, ImmutableArray ExpectedSpeciesNames); private readonly List animationsToTrigger; - + /// /// How long the effect runs (in seconds). Note that if is true, /// there can be multiple instances of the effect running at a time. @@ -903,9 +905,10 @@ namespace Barotrauma } SeverLimbsProbability = MathHelper.Clamp(element.GetAttributeFloat(0.0f, "severlimbs", "severlimbsprobability"), 0.0f, 1.0f); + randomCondition = element.GetAttributeVector2("randomcondition", Vector2.Zero); - string[] targetTypesStr = - element.GetAttributeStringArray("target", null) ?? + string[] targetTypesStr = + element.GetAttributeStringArray("target", null) ?? element.GetAttributeStringArray("targettype", Array.Empty()); foreach (string s in targetTypesStr) { @@ -939,7 +942,10 @@ namespace Barotrauma playSoundOnRequiredItemFailure = element.GetAttributeBool("playsoundonrequireditemfailure", false); #endif - UnlockRecipe = element.GetAttributeIdentifier(nameof(UnlockRecipe), Identifier.Empty); + UnlockRecipes = + element.GetAttributeIdentifierImmutableHashSet(nameof(UnlockRecipes), + //backwards compatibility + def: element.GetAttributeIdentifierImmutableHashSet("UnlockRecipe", ImmutableHashSet.Empty)); List propertyAttributes = new List(); propertyConditionals = new List(); @@ -1014,7 +1020,7 @@ namespace Barotrauma DebugConsole.AddWarning( $"StatusEffect tags defined using the attribute 'tags' in StatusEffect ({parentDebugName}). "+ "Please use the attribute 'statuseffecttags' or 'settags' instead to make it more explicit whether the 'tags' attribute means the status effect's tags, or tags the effect is supposed to set. " + - "The game now assumes it means the status effect's tags.", + "The game now assumes it means the status effect's tags.", contentPackage: element.ContentPackage); #endif } @@ -1038,7 +1044,7 @@ namespace Barotrauma //if the status effect has a duration, assume tags mean this status effect's tags and leave item tags untouched. propertyAttributes.RemoveAll(a => a.Name.ToString().Equals("tags", StringComparison.OrdinalIgnoreCase)); } - + List<(Identifier propertyName, object value)> propertyEffects = new List<(Identifier propertyName, object value)>(); foreach (XAttribute attribute in propertyAttributes) { @@ -1074,7 +1080,7 @@ namespace Barotrauma dropItem = true; break; case "removecharacter": - removeCharacter = true; + removeCharacter = true; containerForItemsOnCharacterRemoval = subElement.GetAttributeIdentifier("moveitemstocontainer", Identifier.Empty); break; case "breaklimb": @@ -1132,7 +1138,7 @@ namespace Barotrauma continue; } } - + Affliction afflictionInstance = afflictionPrefab.Instantiate(subElement.GetAttributeFloat(1.0f, "amount", nameof(afflictionInstance.Strength))); // Deserializing the object normally might cause some unexpected side effects. At least it clamps the strength of the affliction, which we don't want here. // Could probably be solved by using the NonClampedStrength or by bypassing the clamping, but ran out of time and played it safe here. @@ -1166,10 +1172,10 @@ namespace Barotrauma break; case "spawnitem": var newSpawnItem = new ItemSpawnInfo(subElement, parentDebugName); - if (newSpawnItem.ItemPrefab != null) + if (newSpawnItem.ItemPrefab != null) { spawnItems ??= new List(); - spawnItems.Add(newSpawnItem); + spawnItems.Add(newSpawnItem); } break; case "triggerevent": @@ -1247,7 +1253,7 @@ namespace Barotrauma Identifier[] expectedSpeciesNames = subElement.GetAttributeIdentifierArray("expectedspecies", Array.Empty()); animationsToTrigger ??= new List(); animationsToTrigger.Add(new AnimLoadInfo(animType, file, priority, expectedSpeciesNames.ToImmutableArray())); - + break; case "forcesay": forceSayIdentifier = subElement.GetAttributeIdentifier("message", Identifier.Empty); @@ -1294,7 +1300,7 @@ namespace Barotrauma return conditionValue < 0.0f || (setValue && conditionValue <= 0.0f); } } - return false; + return randomCondition.X < 0f || randomCondition.Y < 0f; } public bool IncreasesItemCondition() @@ -1306,7 +1312,7 @@ namespace Barotrauma return conditionValue > 0.0f || (setValue && conditionValue > 0.0f); } } - return false; + return randomCondition.X > 0f || randomCondition.Y > 0f; } private bool ChangesItemCondition(Identifier propertyName, object value, out float conditionValue) @@ -1383,7 +1389,7 @@ namespace Barotrauma if (HasTargetType(TargetType.NearbyItems)) { //optimization for powered components that can be easily fetched from Powered.PoweredList - if (TargetIdentifiers != null && + if (TargetIdentifiers != null && TargetIdentifiers.Count == 1 && (TargetIdentifiers.Contains("powered") || TargetIdentifiers.Contains("junctionbox") || TargetIdentifiers.Contains("relaycomponent"))) { @@ -1491,8 +1497,8 @@ namespace Barotrauma { owner = ownerItem.ParentInventory?.Owner; } - if (owner is Item container) - { + if (owner is Item container) + { if (pc.Type == PropertyConditional.ConditionType.HasTag) { //if we're checking for tags, just check the Item object, not the ItemComponents @@ -1500,8 +1506,8 @@ namespace Barotrauma } else { - if (shouldShortCircuit(AnyTargetMatches(container.AllPropertyObjects, pc.TargetItemComponent, pc), out valueToReturn)) { return valueToReturn; } - } + if (shouldShortCircuit(AnyTargetMatches(container.AllPropertyObjects, pc.TargetItemComponent, pc), out valueToReturn)) { return valueToReturn; } + } } if (owner is Character character && shouldShortCircuit(pc.Matches(character), out valueToReturn)) { return valueToReturn; } } @@ -1673,7 +1679,7 @@ namespace Barotrauma PlaySound(entity, GetHull(entity), GetPosition(entity, targets, worldPosition)); } #endif - return; + return; } if (Duration > 0.0f && !Stackable) @@ -1743,7 +1749,7 @@ namespace Barotrauma } } } - + } position += Offset; position += Rand.Vector(Rand.Range(0.0f, RandomOffset)); @@ -1784,7 +1790,7 @@ namespace Barotrauma } for (int i = 0; i < targets.Count; i++) { - if (targets[i] is not Item item) { continue; } + if (targets[i] is not Item item) { continue; } for (int j = 0; j < useItemCount; j++) { if (item.Removed) { continue; } @@ -1807,8 +1813,8 @@ namespace Barotrauma { for (int i = 0; i < targets.Count; i++) { - if (targets[i] is Item item) - { + if (targets[i] is Item item) + { foreach (var itemContainer in item.GetComponents()) { foreach (var containedItem in itemContainer.Inventory.AllItemsMod) @@ -1885,6 +1891,11 @@ namespace Barotrauma if (target is Entity targetEntity) { if (targetEntity.Removed) { continue; } + if (targetEntity is Item targetItem && randomCondition != Vector2.Zero) + { + float newCondition = Rand.Range(randomCondition.X, randomCondition.Y); + targetItem.Condition = GetModifiedValue(targetItem.Condition, newCondition, deltaTime); + } } else if (target is Limb limb) { @@ -1893,10 +1904,7 @@ namespace Barotrauma } foreach (var (propertyName, value) in PropertyEffects) { - if (!target.SerializableProperties.TryGetValue(propertyName, out SerializableProperty property)) - { - continue; - } + if (!target.SerializableProperties.TryGetValue(propertyName, out SerializableProperty property)) { continue; } ApplyToProperty(target, property, value, deltaTime); } } @@ -2034,10 +2042,10 @@ namespace Barotrauma } } } - + TryTriggerAnimation(target, entity); - if (!forceSayIdentifier.IsEmpty) + if (!forceSayIdentifier.IsEmpty) { LocalizedString messageToSay = TextManager.Get(forceSayIdentifier).Fallback(forceSayIdentifier.Value); @@ -2121,7 +2129,7 @@ namespace Barotrauma foreach (GiveTalentInfo giveTalentInfo in giveTalentInfos) { if (giveTalentInfo.GiveRandom) - { + { // for the sake of technical simplicity, for now do not allow talents to be given if the character could unlock them in their talent tree as well IEnumerable viableTalents = giveTalentInfo.TalentIdentifiers.Where(id => !targetCharacter.Info.UnlockedTalents.Contains(id) && !characterTalentTree.AllTalentIdentifiers.Contains(id)); if (viableTalents.None()) { continue; } @@ -2142,8 +2150,8 @@ namespace Barotrauma { foreach ((Identifier eventId, Identifier tag) in eventTargetTags) { - if (GameMain.GameSession.EventManager.ActiveEvents.FirstOrDefault(e => e.Prefab.Identifier == eventId) is ScriptedEvent ev) - { + if (GameMain.GameSession.EventManager.ActiveEvents.FirstOrDefault(e => e.Prefab.Identifier == eventId) is ScriptedEvent ev) + { targets.Where(t => t is Entity).ForEach(t => ev.AddTarget(tag, (Entity)t)); } } @@ -2157,9 +2165,13 @@ namespace Barotrauma fire.Size = new Vector2(FireSize, fire.Size.Y); } - if (isNotClient && !UnlockRecipe.IsEmpty && GameMain.GameSession is { } gameSession) + if (isNotClient && UnlockRecipes.Any() && GameMain.GameSession is { } gameSession) { - gameSession.UnlockRecipe(UnlockRecipe, showNotifications: true); + foreach (var unlockRecipe in UnlockRecipes) + { + Character targetCharacter = user ?? targets.Select(GetCharacterFromTarget).NotNull().FirstOrDefault(); + gameSession.UnlockRecipe(targetCharacter?.TeamID ?? CharacterTeamType.Team1, unlockRecipe, showNotifications: true); + } } if (isNotClient && triggeredEvents != null && GameMain.GameSession?.EventManager is { } eventManager) @@ -2168,7 +2180,7 @@ namespace Barotrauma { Event ev = eventPrefab.CreateInstance(eventManager.RandomSeed); if (ev == null) { continue; } - eventManager.QueuedEvents.Enqueue(ev); + eventManager.QueuedEvents.Enqueue(ev); if (ev is ScriptedEvent scriptedEvent) { if (!triggeredEventTargetTag.IsEmpty) @@ -2198,7 +2210,7 @@ namespace Barotrauma foreach (CharacterSpawnInfo characterSpawnInfo in spawnCharacters) { var characters = new List(); - + CharacterTeamType? inheritedTeam = null; if (characterSpawnInfo.InheritTeam) { @@ -2211,16 +2223,16 @@ namespace Barotrauma _ => null // Default to Team1, when we can't deduce the team (for example when spawning outside the sub AND character inventory). } ?? (isPvP ? CharacterTeamType.None : CharacterTeamType.Team1); - + CharacterTeamType? GetTeamFromSubmarine(MapEntity e) { if (e.Submarine == null) { return null; } // Don't allow team FriendlyNPC in outposts, because if you buy a spawner item (such as husk container) from the store and choose to get it immediately, it will be spawned in the outpost. - return !isPvP && e.Submarine.Info.IsOutpost && e.Submarine.TeamID == CharacterTeamType.FriendlyNPC ? + return !isPvP && e.Submarine.Info.IsOutpost && e.Submarine.TeamID == CharacterTeamType.FriendlyNPC ? CharacterTeamType.Team1 : e.Submarine.TeamID; } } - + for (int i = 0; i < characterSpawnInfo.Count; i++) { Entity.Spawner.AddCharacterToSpawnQueue(characterSpawnInfo.SpeciesName, position + Rand.Vector(characterSpawnInfo.Spread, Rand.RandSync.Unsynced) + characterSpawnInfo.Offset, @@ -2311,11 +2323,11 @@ namespace Barotrauma Character.Controlled = newCharacter; } #elif SERVER - foreach (Client c in GameMain.Server.ConnectedClients) - { - if (c.Character != target) { continue; } - GameMain.Server.SetClientCharacter(c, newCharacter); - } + foreach (Client c in GameMain.Server.ConnectedClients) + { + if (c.Character != target) { continue; } + GameMain.Server.SetClientCharacter(c, newCharacter); + } #endif } if (characterSpawnInfo.RemovePreviousCharacter) { Entity.Spawner?.AddEntityToRemoveQueue(character); } @@ -2477,7 +2489,7 @@ namespace Barotrauma position = entity.WorldPosition; if (entity is Item it) { - sourceBody ??= + sourceBody ??= (entity as Item)?.body ?? (entity as Character)?.AnimController.Collider; } @@ -2532,7 +2544,7 @@ namespace Barotrauma else if (parentItem != null) { rotation = PhysicsBody.TransformRotation( - -parentItem.RotationRad + chosenItemSpawnInfo.RotationRad, + -parentItem.RotationRad + chosenItemSpawnInfo.RotationRad, dir: parentItem.FlippedX ? -1.0f : 1.0f); } break; @@ -2758,29 +2770,17 @@ namespace Barotrauma private void ApplyToProperty(ISerializableEntity target, SerializableProperty property, object value, float deltaTime) { - if (disableDeltaTime || setValue) { deltaTime = 1.0f; } - if (value is int || value is float) + if (value is int or float) { - float propertyValueF = property.GetFloatValue(target); + float newValue = GetModifiedValue(property.GetFloatValue(target), Convert.ToSingle(value), deltaTime); if (property.PropertyType == typeof(float)) { - float floatValue = value is float single ? single : (int)value; - floatValue *= deltaTime; - if (!setValue) - { - floatValue += propertyValueF; - } - property.TrySetValue(target, floatValue); + property.TrySetValue(target, newValue); return; } else if (property.PropertyType == typeof(int)) { - int intValue = (int)(value is float single ? single * deltaTime : (int)value * deltaTime); - if (!setValue) - { - intValue += (int)propertyValueF; - } - property.TrySetValue(target, intValue); + property.TrySetValue(target, (int)newValue); return; } } @@ -2792,6 +2792,13 @@ namespace Barotrauma property.TrySetValue(target, value); } + private float GetModifiedValue(float currValue, float newValue, float deltaTime) + { + if (setValue) { return newValue; } + if (disableDeltaTime) { deltaTime = 1f; } + return currValue + newValue * deltaTime; + } + public static void UpdateAll(float deltaTime) { UpdateAllProjSpecific(deltaTime); @@ -2848,7 +2855,7 @@ namespace Barotrauma element.Parent.RegisterTreatmentResults(element.Parent.user, element.Entity as Item, limb, affliction, result); } } - + foreach ((Identifier affliction, float amount) in element.Parent.ReduceAffliction) { Limb targetLimb = null; @@ -2894,10 +2901,10 @@ namespace Barotrauma element.Parent.TryTriggerAnimation(target, element.Entity); } - element.Parent.ApplyProjSpecific(deltaTime, - element.Entity, - element.Targets, - element.Parent.GetHull(element.Entity), + element.Parent.ApplyProjSpecific(deltaTime, + element.Entity, + element.Targets, + element.Parent.GetHull(element.Entity), element.Parent.GetPosition(element.Entity, element.Targets), playSound: element.Timer >= element.Duration); @@ -2970,7 +2977,7 @@ namespace Barotrauma if (limb == null) { return; } foreach (Affliction limbAffliction in limb.character.CharacterHealth.GetAllAfflictions()) { - if (result.Afflictions != null && + if (result.Afflictions != null && /* "affliction" is the affliction directly defined in the status effect (e.g. "5 internal damage (per second / per frame / however the effect is defined to run)"), * "result" is how much we actually applied of that affliction right now (taking into account the elapsed time, resistances and such) */ result.Afflictions.FirstOrDefault(a => a.Prefab == limbAffliction.Prefab) is Affliction resultAffliction && diff --git a/Barotrauma/BarotraumaShared/SharedSource/Text/RichString.cs b/Barotrauma/BarotraumaShared/SharedSource/Text/RichString.cs index 7e8300c5c..285d1a30c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Text/RichString.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Text/RichString.cs @@ -1,7 +1,6 @@ #nullable enable using System; using System.Collections.Immutable; -using System.Diagnostics; namespace Barotrauma { @@ -30,6 +29,8 @@ namespace Barotrauma public LocalizedString NestedStr { get; private set; } public readonly LocalizedString SanitizedString; + public bool Loaded => loaded; + #if CLIENT private readonly GUIFont? font; private readonly GUIComponentStyle? componentStyle; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Utils/SafeIO.cs b/Barotrauma/BarotraumaShared/SharedSource/Utils/SafeIO.cs index 1f22f9682..e3acf1d32 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Utils/SafeIO.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Utils/SafeIO.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Linq; +using System.Threading; + #if CLIENT using Barotrauma.Networking; using Barotrauma.Steam; @@ -99,7 +101,8 @@ namespace Barotrauma.IO this System.Xml.Linq.XDocument doc, string path, System.Xml.Linq.SaveOptions saveOptions = System.Xml.Linq.SaveOptions.None, - bool throwExceptions = false) + bool throwExceptions = false, + int maxRetries = 0) { if (!Validation.CanWrite(path, false)) { @@ -114,7 +117,21 @@ namespace Barotrauma.IO } return; } - doc.Save(path, saveOptions); + + for (int i = 0; i <= maxRetries; i++) + { + try + { + doc.Save(path, saveOptions); + break; + } + catch (IOException e) + { + if (i >= maxRetries) { throw; } + DebugConsole.NewMessage("Failed save XML document {" + e.Message + "}, retrying in 250 ms..."); + Thread.Sleep(250); + } + } } public static void SaveSafe(this System.Xml.Linq.XElement element, string path, bool throwExceptions = false) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Utils/SaveUtil.cs b/Barotrauma/BarotraumaShared/SharedSource/Utils/SaveUtil.cs index 66151feec..67d2740dd 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Utils/SaveUtil.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Utils/SaveUtil.cs @@ -116,10 +116,23 @@ namespace Barotrauma #if SERVER get { return Path.Combine(GetSaveFolder(SaveType.Singleplayer), "temp_server"); } #else - get { return Path.Combine(GetSaveFolder(SaveType.Singleplayer), "temp"); } + get + + { + string tempFolder = Path.Combine(GetSaveFolder(SaveType.Singleplayer), "temp"); +#if DEBUG + if (GameClient.MultiClientTestMode && GameMain.Client != null) + { + //append the name of the client to the download folder to avoid multiple clients + //from trying to download a file into the same path at the same time + tempFolder += "_" + GameMain.Client.Name; + } +#endif + return tempFolder; + } #endif } - + public static void EnsureSaveFolderExists() { try diff --git a/Barotrauma/BarotraumaShared/changelog.txt b/Barotrauma/BarotraumaShared/changelog.txt index f6878cdb4..de28279f8 100644 --- a/Barotrauma/BarotraumaShared/changelog.txt +++ b/Barotrauma/BarotraumaShared/changelog.txt @@ -1,3 +1,69 @@ +------------------------------------------------------------------------------------------------------------------------------------------------- +v1.10.5.0 +------------------------------------------------------------------------------------------------------------------------------------------------- + +Balance: +- Railgun rebalance: The railgun has been deemed on the weaker side by the community. It has been given a satisfaction pass, where now the high-power shell behaves more as expected, capable of penetrating multiple limbs or submarine walls, dealing more damage. +- Broad-spectrum Antibiotics now affect all limbs with infected wound with one application, but cure less infection per limb. Base prices for Plastiseal, Antibiotic Glue and Broad-Spectrum Antibiotics have been adjusted. +- Antibiotic Glue is now less effective on healing burns, bleeding and infections and inflicts organ damage even with successful application (and even more with failure). +- Added a pass to make grenades more easy to aim (and less likely to always hit the wall in the back due to excessive sliding/rolling) + - Reduced and equalized throwforce of most throwables (grenades, explosives) to 3.5 (some were 3.5 already, some 4.0). + - Add angular dampening on grenades to reduce excessive sliding/rolling. + - Frag grenade now uses a rectangular body instead of round/capsule, to avoid rolling too far (more in line with other grenades). + - Reduced throw force when aiming throwables downwards. +- Lower the rate at which skyholder artifacts and portable pumps drain water (both were way too effective for managing leaks). +- Made all variants of fractal guardians vulnerable to EMPs. +- Security NPCs on submarine encounters are now better armed, with weapons fitting the faction. + +Blueprints: +- Added new "blueprint" type of items, which unlock crafting recipes. +- Three new alien material blueprints: physicorium, dementonite (gravity) and incendium, which unlock relevant (ammo) recipes. +- Alien blueprints need to be researched at a Research station. +- Merchants of all factions now sell item blueprints fitting to the faction's identity, locked behind reputation. + +Miscellaneous changes: +- Made vent sounds a bit more quiet. +- Made crate shelf and makeshift shelf container UIs vertical to match the look of the sprites. +- Made some items be attachable only to the floor. + - Planters cannot overlap, and attach only to the floor, rather than attaching to the wall. + - Makeshift shelves are attached to the floor. +- Outpost NPCs react to players dragging other outpost NPC's corpses. Normal NPCs flee, security arrests you. + +Fixes: +- Fixed gaps significantly slowing down the flow of water, especially when water is flowing up through the gaps. +- Fixed PvP outpost selection not working reliably (when you selected some specific outpost, it was possible for some other outpost to get selected regardless). +- Fixed bot AI not running if a bot below 0 vitality is forced to stay unconscious with e.g. adrenaline or talents. +- Fixed incorrect offset on the light that renders on a boarding pod that's in a loader. +- Fixed defense bot only escaping if it attemps to fire it's weapon and fails due to being out of ammo, but not when it doesn't have any ammo available. +- Fixed outpost NPCs sometimes spawning on non-human spawnpoints, e.g. the monster spawnpoints in research modules. +- Fixed NPC conversations about the submarine being very deep starting to appear too late (at the point when the sub is already at crush depth, instead of the point where camera shake and audio effects start appearing). +- Fixed yet another issue with modded hairs: hairs got misaligned in the bottom-right character portrait if they were set to inherit the origin of the head sprite, and the head sprite's origin was not at the center. +- Fixed all pets in the level getting added to the player crew at the end of a round, e.g. even hostile pets or pets inside beacon stations. +- Fixed id cards only being sold in normal outposts and cities (not mining, research or military outposts). +- Fixed loading screen tips often cycling too fast to read during the initial loading screen. +- Fixed "Find Jacov Subra" mission completing if you enter the outpost he's hiding in, even if you don't find him. +- Fixed vents' oxygen output warning being incorrectly shown when vents have been moved between hulls in the sub editor. +- The "drop item" hotkey is disabled when the inventory is not visible (e.g. when operating a turret). +- Fixed inability to issue orders that don't target any character in particular (e.g. ignore or deconstruct orders) when no-one can hear the order. +- Server log fixes: + - Fixed new lines added to the log ignoring the filtering. + - Fixed log jumping up when new lines are added while you've scrolled up to read older messages. + +Modding: +- When saving a submarine or an outpost in the editor, the game suggests saving it in a subfolder that contains subs of the same type instead of always saving it in the root folder of the mod. +- Fixed "Equipped" item requirement only checking held items, not worn items as the documentation says. +- Option to define tags for wrecks and beacon stations in the sub editor, and to make a wreck or beacon mission choose a random wreck or beacon station with the specified tags using the attributes "BeaconTags" and "WreckTags". +- Fixed new elements defined in an item variant failing to be added to the variant if there's ClearAll elements present. +- Fixed submarine upgrade UI displaying an incorrect icon on items that don't have an inventory icon but use the item's normal sprite instead. +- The "sort stacks" and "merge stacks" buttons can be hidden from containers using the attributes "ShowSortButton" and "ShowMergeButton". +- Changed how store item availabilities work by default: if no min/max amount is defined, there can be 0-5 items available instead of there always being 5. +- Some new decorative outpost props: empty versions of the existing storage shelves which contained some decorative items. +- Fixed game freezing if a bot uses a weapon that has both a RangedWeapon and a RepairTool component. +- AnimController properties can now be accessed using conditionals. +- Fixed bots being able to unequip and reload non-interactable items if they have those in their inventory (e.g. if some custom event forces them to spawn with non-interactable items). +- Fixed corpses spawning in wrecks that have neither human or corpse spawnpoints (making it impossible to create wrecks with no corpses). +- Fixed unnecessary console errors about failing to load a texture when trying to preload some monster/enemy with [GENDER] tags in the filename. + ------------------------------------------------------------------------------------------------------------------------------------------------- v1.9.8.0 ------------------------------------------------------------------------------------------------------------------------------------------------- @@ -76,6 +142,16 @@ Fixes: - Fixed inability to focus on unconscious characters by clicking on the crew list. - Fixed parts of the Jove sculptures found in ruins being misaligned in mirrored levels (= when travelling through the level backwards). - Fixed the Multi-tool not functioning as a screwdriver for event checks. + +Changes and additions: +- Upgraded to .NET 8. This should not cause any noticeable changes, aside from perhaps very minor performance improvements. Allows code mods to use C# 12 features. +- Added option to filter by name and to change the sorting of the save files listed in the "load game" menu. +- Made circuit boxes' external connections (the once that hook up to other items) use the same labels that have been set inside the circuit box. +- Made the freecam console command a toggle: entering it again gives you back control of the character. +- Added "loslightingfreecam" console command (convenient for testing, executes those 3 commands). + +Fixes: +- Fixed bots being reluctant to go into rooms with low oxygen, even if they're trying to get diving gear from that room. - Fixed "fake fires" you see when under psychosis sometimes turning into real fires in single player. - Fixed PvP mode weapon crates showing up in the sub editor. - Fixed undoing the removal of an item from a container sometimes causing a crash in the sub editor. diff --git a/Libraries/BarotraumaLibs/BarotraumaCore/Networking/Primitives/NetworkEnums.cs b/Libraries/BarotraumaLibs/BarotraumaCore/Networking/Primitives/NetworkEnums.cs index b7c9866e0..5e7c5bd04 100644 --- a/Libraries/BarotraumaLibs/BarotraumaCore/Networking/Primitives/NetworkEnums.cs +++ b/Libraries/BarotraumaLibs/BarotraumaCore/Networking/Primitives/NetworkEnums.cs @@ -32,7 +32,8 @@ namespace Barotrauma.Networking IsDisconnectMessage = 0x4, IsServerMessage = 0x8, IsHeartbeatMessage = 0x10, - IsDataFragment = 0x20 + IsDataFragment = 0x20, + IsDoSProtectionMessage = 0x40, } public static class NetworkEnumExtensions @@ -51,9 +52,12 @@ namespace Barotrauma.Networking public static bool IsHeartbeatMessage(this PacketHeader h) => h.HasFlag(PacketHeader.IsHeartbeatMessage); - + public static bool IsDataFragment(this PacketHeader h) => h.HasFlag(PacketHeader.IsDataFragment); + + public static bool IsDoSProtectionMessage(this PacketHeader h) + => h.HasFlag(PacketHeader.IsDoSProtectionMessage); } public static class NetworkMagicStrings diff --git a/Libraries/MonoGame.Framework/Src/MonoGame.Framework/Graphics/SpriteBatch.cs b/Libraries/MonoGame.Framework/Src/MonoGame.Framework/Graphics/SpriteBatch.cs index 2d08ff6ec..b5775bbc6 100644 --- a/Libraries/MonoGame.Framework/Src/MonoGame.Framework/Graphics/SpriteBatch.cs +++ b/Libraries/MonoGame.Framework/Src/MonoGame.Framework/Graphics/SpriteBatch.cs @@ -91,7 +91,7 @@ namespace Microsoft.Xna.Framework.Graphics Effect _spriteEffect; readonly EffectParameter _matrixTransform; - readonly EffectPass _spritePass; + EffectPass _spritePass; Matrix? _matrix; private Viewport _lastViewport; @@ -238,6 +238,7 @@ namespace Microsoft.Xna.Framework.Graphics else _matrixTransform.SetValue(_projection); + _spritePass = _spriteEffect.CurrentTechnique.Passes[0]; _spritePass.Apply(); }