From 81ca1a409b907df155482cfecf4bed82bb117db4 Mon Sep 17 00:00:00 2001 From: Regalis Date: Mon, 25 Apr 2016 21:52:27 +0300 Subject: [PATCH] Fixed NullReferenceException when attempting to send an ApplyStatusEffect update for an item that was removed from the inventory by the statuseffect (eg syringe with nitroglyserine which was dropped because of the explosion), oxygen level detoriates more slowly when unconscious, showing a tooltip for items in a subinventory, nitroglyserine explodes on first injection (instead of after a few injections when condition drops to zero) --- .../Content/Items/Weapons/explosives.xml | 4 +- Subsurface/Source/Characters/Character.cs | 38 ++++++++++--------- Subsurface/Source/Items/CharacterInventory.cs | 5 ++- Subsurface/Source/Items/Inventory.cs | 25 +++++++++++- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/Subsurface/Content/Items/Weapons/explosives.xml b/Subsurface/Content/Items/Weapons/explosives.xml index 4ad9d22c6..6c87ff27a 100644 --- a/Subsurface/Content/Items/Weapons/explosives.xml +++ b/Subsurface/Content/Items/Weapons/explosives.xml @@ -118,13 +118,13 @@ - + - + diff --git a/Subsurface/Source/Characters/Character.cs b/Subsurface/Source/Characters/Character.cs index b6d15bf26..14e2d0e92 100644 --- a/Subsurface/Source/Characters/Character.cs +++ b/Subsurface/Source/Characters/Character.cs @@ -1043,23 +1043,7 @@ namespace Barotrauma lowPassMultiplier = MathHelper.Lerp(lowPassMultiplier, 1.0f, 0.1f); - if (needsAir) - { - Oxygen += deltaTime * (oxygenAvailable < 30.0f ? -5.0f : 10.0f); - - PressureProtection -= deltaTime*100.0f; - - float hullAvailableOxygen = 0.0f; - - if (!AnimController.HeadInWater && AnimController.CurrentHull != null) - { - hullAvailableOxygen = AnimController.CurrentHull.OxygenPercentage; - - AnimController.CurrentHull.Oxygen -= Hull.OxygenConsumptionSpeed * deltaTime; - } - - OxygenAvailable += Math.Sign(hullAvailableOxygen - oxygenAvailable) * deltaTime * 50.0f; - } + if (needsAir) UpdateOxygen(deltaTime); Health -= bleeding * deltaTime; Bleeding -= BleedingDecreaseSpeed * deltaTime; @@ -1069,6 +1053,24 @@ namespace Barotrauma if (!IsDead) LockHands = false; } + private void UpdateOxygen(float deltaTime) + { + Oxygen += deltaTime * (oxygenAvailable < 30.0f ? -5.0f : 10.0f); + + PressureProtection -= deltaTime * 100.0f; + + float hullAvailableOxygen = 0.0f; + + if (!AnimController.HeadInWater && AnimController.CurrentHull != null) + { + hullAvailableOxygen = AnimController.CurrentHull.OxygenPercentage; + + AnimController.CurrentHull.Oxygen -= Hull.OxygenConsumptionSpeed * deltaTime; + } + + OxygenAvailable += Math.Sign(hullAvailableOxygen - oxygenAvailable) * deltaTime * 50.0f; + } + private void UpdateUnconscious(float deltaTime) { Stun = Math.Max(5.0f, Stun); @@ -1076,7 +1078,7 @@ namespace Barotrauma AnimController.ResetPullJoints(); selectedConstruction = null; - if (oxygen <= 0.0f) Oxygen -= deltaTime; + if (oxygen <= 0.0f) Oxygen -= deltaTime * 0.5f; if (health <= 0.0f) { diff --git a/Subsurface/Source/Items/CharacterInventory.cs b/Subsurface/Source/Items/CharacterInventory.cs index b29c00b3c..4c93b5c3f 100644 --- a/Subsurface/Source/Items/CharacterInventory.cs +++ b/Subsurface/Source/Items/CharacterInventory.cs @@ -94,8 +94,11 @@ namespace Barotrauma if (Items[slotIndex] == null) return false; + //save the ID in a variable in case the statuseffect causes the item to be dropped/destroyed + int itemID = Items[slotIndex].ID; + Items[slotIndex].ApplyStatusEffects(ActionType.OnUse, 1.0f, character); - new NetworkEvent(NetworkEventType.ApplyStatusEffect, character.ID, true, Items[slotIndex].ID); + new NetworkEvent(NetworkEventType.ApplyStatusEffect, character.ID, true, itemID); return true; } diff --git a/Subsurface/Source/Items/Inventory.cs b/Subsurface/Source/Items/Inventory.cs index 5413c3ff9..7be456bac 100644 --- a/Subsurface/Source/Items/Inventory.cs +++ b/Subsurface/Source/Items/Inventory.cs @@ -319,15 +319,36 @@ namespace Barotrauma Item[] containedItems = null; if (Items[slotIndex] != null) containedItems = Items[slotIndex].ContainedItems; + string toolTip = ""; + Rectangle highlightedRect = Rectangle.Empty; + if (containedItems != null) { for (int i = 0; i < itemCapacity; i++) { subRect.Y = subRect.Y - subRect.Height - 10; - UpdateSlot(spriteBatch, subRect, selectedSlot, i < containedItems.Count() ? containedItems[i] : null, true); + highlightedRect = subRect; + UpdateSlot(spriteBatch, subRect, selectedSlot, i < containedItems.Length ? containedItems[i] : null, true); + + if (i >= containedItems.Length || containedItems[i] == null) continue; + + if (highlightedRect.Contains(PlayerInput.MousePosition)) + { + if (GameMain.DebugDraw) + { + toolTip = containedItems[i].ToString(); + } + else + { + toolTip = string.IsNullOrEmpty(containedItems[i].Description) ? + containedItems[i].Name : + containedItems[i].Name + '\n' + containedItems[i].Description; + } + } } } - + + if (!string.IsNullOrWhiteSpace(toolTip)) DrawToolTip(spriteBatch, toolTip, highlightedRect); }