Merge branch 'master' into new-netcode

Conflicts:
	Subsurface/Properties/AssemblyInfo.cs
	Subsurface/Source/Items/Components/Signal/Wire.cs
	Subsurface/Source/Items/Item.cs
	Subsurface/Source/Items/ItemInventory.cs
	Subsurface/Source/Networking/GameServer.cs
This commit is contained in:
Regalis
2016-11-09 17:58:56 +02:00
25 changed files with 486 additions and 262 deletions
@@ -380,7 +380,7 @@ namespace Barotrauma.Items.Components
return;
}
List<ItemSound> matchingSounds = null;
List<ItemSound> matchingSounds;
if (!sounds.TryGetValue(type, out matchingSounds)) return;
ItemSound itemSound = null;
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using FarseerPhysics;
using Microsoft.Xna.Framework;
@@ -14,7 +15,7 @@ namespace Barotrauma.Items.Components
List<RelatedItem> containableItems;
public ItemInventory Inventory;
private bool hasStatusEffects;
private List<Pair<Item, StatusEffect>> itemsWithStatusEffects;
//how many items can be contained
[HasDefaultValue(5, false)]
@@ -110,24 +111,39 @@ namespace Barotrauma.Items.Components
case "containable":
RelatedItem containable = RelatedItem.Load(subElement);
if (containable == null) continue;
foreach (StatusEffect effect in containable.statusEffects)
{
if (effect.type == ActionType.OnContaining) hasStatusEffects = true;
}
containableItems.Add(containable);
break;
}
}
IsActive = true;
itemsWithStatusEffects = new List<Pair<Item, StatusEffect>>();
}
public void RemoveContained(Item item)
public void OnItemContained(Item item)
{
Inventory.RemoveItem(item);
item.SetContainedItemPositions();
RelatedItem ri = containableItems.Find(x => x.MatchesItem(item));
if (ri != null)
{
foreach (StatusEffect effect in ri.statusEffects)
{
itemsWithStatusEffects.Add(Pair<Item, StatusEffect>.Create(item, effect));
}
}
//no need to Update() if this item has no statuseffects and no physics body
IsActive = itemsWithStatusEffects.Count > 0 || item.body != null;
}
public void OnItemRemoved(Item item)
{
itemsWithStatusEffects.RemoveAll(i => i.First == item);
//deactivate if the inventory is empty
IsActive = itemsWithStatusEffects.Count > 0 || item.body != null;
}
public bool CanBeContained(Item item)
@@ -138,31 +154,24 @@ namespace Barotrauma.Items.Components
public override void Update(float deltaTime, Camera cam)
{
if (item.body != null && item.body.FarseerBody.Awake)
if (item.body != null &&
item.body.Enabled &&
item.body.FarseerBody.Awake)
{
foreach (Item contained in Inventory.Items)
{
if (contained == null) continue;
contained.SetTransform(item.SimPosition, 0.0f);
}
item.SetContainedItemPositions();
}
if (!hasStatusEffects) return;
foreach (Item contained in Inventory.Items)
foreach (Pair<Item, StatusEffect> itemAndEffect in itemsWithStatusEffects)
{
if (contained == null || contained.Condition <= 0.0f) continue;
Item contained = itemAndEffect.First;
if (contained.Condition < 0.0f) continue;
RelatedItem ri = containableItems.Find(x => x.MatchesItem(contained));
if (ri == null) continue;
StatusEffect effect = itemAndEffect.Second;
foreach (StatusEffect effect in ri.statusEffects)
{
if (effect.Targets.HasFlag(StatusEffect.TargetType.This)) effect.Apply(ActionType.OnContaining, deltaTime, item, item.AllPropertyObjects);
if (effect.Targets.HasFlag(StatusEffect.TargetType.Contained)) effect.Apply(ActionType.OnContaining, deltaTime, item, contained.AllPropertyObjects);
}
//contained.ApplyStatusEffects(ActionType.OnContained, deltaTime);
if (effect.Targets.HasFlag(StatusEffect.TargetType.This))
effect.Apply(ActionType.OnContaining, deltaTime, item, item.AllPropertyObjects);
if (effect.Targets.HasFlag(StatusEffect.TargetType.Contained))
effect.Apply(ActionType.OnContaining, deltaTime, item, contained.AllPropertyObjects);
}
}
@@ -232,15 +241,13 @@ namespace Barotrauma.Items.Components
public override bool Combine(Item item)
{
if (containableItems.Find(x => x.MatchesItem(item)) == null) return false;
if (!containableItems.Any(x => x.MatchesItem(item))) return false;
if (Inventory.TryPutItem(item))
{
IsActive = true;
if (hideItems || (item.body!=null && !item.body.Enabled)) item.body.Enabled = false;
//item.Container = this.item;
if (hideItems && item.body != null) item.body.Enabled = false;
return true;
}
@@ -22,7 +22,9 @@ namespace Barotrauma.Items.Components
//affects how fast changes in power/load are carried over the grid
static float inertia = 5.0f;
static List<Powered> connectedList = new List<Powered>();
static HashSet<Powered> connectedList = new HashSet<Powered>();
private List<Connection> powerConnections;
private float powerLoad;
@@ -35,24 +37,27 @@ namespace Barotrauma.Items.Components
: base(item, element)
{
IsActive = true;
powerConnections = new List<Connection>();
}
public override void Update(float deltaTime, Camera cam)
{
//reset and recalculate the power generated/consumed
//by the constructions connected to the grid
fullPower = 0.0f;
fullLoad = 0.0f;
connectedList.Clear();
if (updateTimer > 0)
{
//this junction box has already been updated this frame
updateTimer--;
return;
}
//reset and recalculate the power generated/consumed
//by the constructions connected to the grid
fullPower = 0.0f;
fullLoad = 0.0f;
updateTimer = 0;
connectedList.Clear();
CheckJunctions(deltaTime);
updateTimer = 0;
foreach (Powered p in connectedList)
{
@@ -68,8 +73,7 @@ namespace Barotrauma.Items.Components
//(except if running as a client)
if (GameMain.Client != null) continue;
if (-pt.currPowerConsumption < Math.Max(pt.powerLoad * Rand.Range(1.9f,2.1f), 200.0f)) continue;
float prevCondition = pt.item.Condition;
pt.item.Condition -= deltaTime * 10.0f;
@@ -109,37 +113,31 @@ namespace Barotrauma.Items.Components
ApplyStatusEffects(ActionType.OnActive, deltaTime, null);
List<Connection> connections = item.Connections;
if (connections == null) return;
foreach (Connection c in connections)
foreach (Connection c in powerConnections)
{
if (!c.IsPower) continue;
var recipients = c.Recipients;
foreach (Connection recipient in recipients)
{
if (recipient == null || !c.IsPower) continue;
if (recipient == null) continue;
Item it = recipient.Item;
if (it == null) continue;
//if (it.Updated) continue;
Powered powered = it.GetComponent<Powered>();
if (powered == null || !powered.IsActive) continue;
if (connectedList.Contains(powered)) continue;
PowerTransfer powerTransfer = powered as PowerTransfer;
PowerContainer powerContainer = powered as PowerContainer;
if (powerTransfer != null)
{
//if (powerTransfer.updateTimer>0) continue;
powerTransfer.CheckJunctions(deltaTime);
continue;
}
else if (powerContainer != null)
PowerContainer powerContainer = powered as PowerContainer;
if (powerContainer != null)
{
if (recipient.Name == "power_in")
{
@@ -187,6 +185,19 @@ namespace Barotrauma.Items.Components
GuiFrame.Update(1.0f / 60.0f);
}
public override void OnMapLoaded()
{
var connections = item.Connections;
if (connections == null)
{
IsActive = false;
return;
}
powerConnections = connections.FindAll(c => c.IsPower);
if (powerConnections.Count == 0) IsActive = false;
}
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power)
{
base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
@@ -10,6 +10,47 @@ namespace Barotrauma.Items.Components
{
class Wire : ItemComponent, IDrawableComponent
{
class WireSection
{
private Vector2 start;
private float angle;
private float length;
public WireSection(Vector2 start, Vector2 end)
{
this.start = start;
angle = MathUtils.VectorToAngle(end - start);
length = Vector2.Distance(start, end);
}
public void Draw(SpriteBatch spriteBatch, Color color, Vector2 offset, float depth, float width = 0.3f)
{
spriteBatch.Draw(wireSprite.Texture,
new Vector2(start.X+offset.X, -(start.Y+offset.Y)), null, color,
-angle,
new Vector2(0.0f, wireSprite.size.Y / 2.0f),
new Vector2(length / wireSprite.Texture.Width, width),
SpriteEffects.None,
depth);
}
public static void Draw(SpriteBatch spriteBatch, Vector2 start, Vector2 end, Color color, float depth, float width = 0.3f)
{
start.Y = -start.Y;
end.Y = -end.Y;
spriteBatch.Draw(wireSprite.Texture,
start, null, color,
MathUtils.VectorToAngle(end - start),
new Vector2(0.0f, wireSprite.size.Y / 2.0f),
new Vector2((Vector2.Distance(start, end)) / wireSprite.Texture.Width, width),
SpriteEffects.None,
depth);
}
}
const float nodeDistance = 32.0f;
const float heightFromFloor = 128.0f;
@@ -17,6 +58,8 @@ namespace Barotrauma.Items.Components
public List<Vector2> Nodes;
private List<WireSection> sections;
Connection[] connections;
private Vector2 newNodePos;
@@ -39,22 +82,15 @@ namespace Barotrauma.Items.Components
wireSprite = new Sprite("Content/Items/wireHorizontal.png", new Vector2(0.5f, 0.5f));
wireSprite.Depth = 0.85f;
}
Nodes = new List<Vector2>();
sections = new List<WireSection>();
connections = new Connection[2];
IsActive = false;
}
public override void Move(Vector2 amount)
{
//for (int i = 0; i < Nodes.Count; i++)
//{
// Nodes[i] += amount;
//}
}
public Connection OtherConnection(Connection connection)
{
if (connection == null) return null;
@@ -125,25 +161,23 @@ namespace Barotrauma.Items.Components
if (Nodes.Count > 0 && Nodes[0] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break;
if (Nodes.Count > 1 && Nodes[Nodes.Count-1] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break;
if (i == 0)
{
Nodes.Insert(0, newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition);
Nodes.Insert(0, newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition);
}
else
{
Nodes.Add(newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition);
}
break;
}
if (connections[0] != null && connections[1] != null)
{
//List<Vector2> prevNodes = new List<Vector2>(Nodes);
foreach (ItemComponent ic in item.components)
{
if (ic == this) continue;
@@ -155,12 +189,12 @@ namespace Barotrauma.Items.Components
IsActive = false;
//Nodes = prevNodes;
CleanNodes();
}
Drawable = Nodes.Any();
UpdateSections();
return true;
}
@@ -190,28 +224,11 @@ namespace Barotrauma.Items.Components
{
if (Nodes.Count == 0) return;
//item.FindHull();
//Vector2 position = item.Position;
//position.X = MathUtils.Round(item.Position.X, nodeDistance);
//if (item.CurrentHull == null)
//{
// position.Y = MathUtils.Round(item.Position.Y, nodeDistance);
//}
//else
//{
// position.Y -= item.CurrentHull.Rect.Y - item.CurrentHull.Rect.Height;
// position.Y = Math.Max(MathUtils.Round(position.Y, nodeDistance), heightFromFloor);
// position.Y += item.CurrentHull.Rect.Y - item.CurrentHull.Rect.Height;
//}
Submarine sub = null;
if (connections[0] != null && connections[0].Item.Submarine != null) sub = connections[0].Item.Submarine;
if (connections[1] != null && connections[1].Item.Submarine != null) sub = connections[1].Item.Submarine;
if (item.Submarine != sub)
if (item.Submarine != sub && Screen.Selected != GameMain.EditMapScreen)
{
ClearConnections();
Nodes.Clear();
@@ -219,20 +236,6 @@ namespace Barotrauma.Items.Components
}
newNodePos = RoundNode(item.Position, item.CurrentHull) - sub.HiddenSubPosition;
//if (Vector2.Distance(position, nodes[nodes.Count - 1]) > nodeDistance*10)
//{
// nodes.Add(position);
// item.NewComponentEvent(this, true);
//}
//else if (Math.Abs(position.Y - nodes[nodes.Count - 1].Y) > nodeDistance)
//{
// nodes.Add(new Vector2(nodes[nodes.Count - 1].X,
// position.Y));
// item.NewComponentEvent(this, true);
//}
}
public override bool Use(float deltaTime, Character character = null)
@@ -242,6 +245,8 @@ namespace Barotrauma.Items.Components
if (newNodePos!= Vector2.Zero && Nodes.Count>0 && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance)
{
Nodes.Add(newNodePos);
UpdateSections();
Drawable = true;
newNodePos = Vector2.Zero;
@@ -254,6 +259,7 @@ namespace Barotrauma.Items.Components
if (Nodes.Count > 1)
{
Nodes.RemoveAt(Nodes.Count - 1);
UpdateSections();
}
Drawable = Nodes.Any();
@@ -266,9 +272,20 @@ namespace Barotrauma.Items.Components
return true;
}
public void UpdateSections()
{
sections.Clear();
for (int i = 0; i < Nodes.Count-1; i++)
{
sections.Add(new WireSection(Nodes[i], Nodes[i + 1]));
}
}
private void ClearConnections()
{
Nodes.Clear();
sections.Clear();
for (int i = 0; i < 2; i++)
{
@@ -288,8 +305,6 @@ namespace Barotrauma.Items.Components
{
if (Screen.Selected == GameMain.EditMapScreen)
{
//position = GameMain.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition) - Submarine.Loaded.Position;// Nodes[(int)selectedNodeIndex];
position.X = MathUtils.Round(position.X, Submarine.GridSize.X / 2.0f);
position.Y = MathUtils.Round(position.Y, Submarine.GridSize.Y / 2.0f);
}
@@ -345,35 +360,42 @@ namespace Barotrauma.Items.Components
public void Draw(SpriteBatch spriteBatch, bool editing)
{
//for (int i = 0; i < nodes.Count; i++)
//{
// GUI.DrawRectangle(spriteBatch, new Rectangle((int)nodes[i].X, (int)-nodes[i].Y, 5, 5), Color.DarkGray, true, wireSprite.Depth - 0.01f);
//}
if (!Nodes.Any())
{
Drawable = false;
return;
}
Vector2 drawOffset = Vector2.Zero;
if (item.Submarine != null)
{
drawOffset = item.Submarine.DrawPosition + item.Submarine.HiddenSubPosition;
}
float depth = wireSprite.Depth + ((item.ID % 100) * 0.00001f);
if (item.IsHighlighted)
{
for (int i = 1; i < Nodes.Count; i++)
foreach (WireSection section in sections)
{
DrawSection(spriteBatch, Nodes[i], Nodes[i - 1], Color.Gold, 0.5f);
section.Draw(spriteBatch, Color.Gold, drawOffset, depth, 0.5f);
}
}
for (int i = 1; i < Nodes.Count; i++)
foreach (WireSection section in sections)
{
DrawSection(spriteBatch, Nodes[i], Nodes[i - 1], item.Color);
section.Draw(spriteBatch, item.Color, drawOffset, depth, 0.3f);
}
if (IsActive && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance)
{
DrawSection(spriteBatch, Nodes[Nodes.Count - 1], newNodePos, item.Color * 0.5f);
//nodes.Add(newNodePos);
WireSection.Draw(
spriteBatch,
new Vector2(Nodes[Nodes.Count - 1].X, Nodes[Nodes.Count - 1].Y) + drawOffset,
new Vector2(newNodePos.X, newNodePos.Y) + drawOffset,
item.Color * 0.5f,
depth,
0.3f);
}
if (!editing || !PlayerInput.MouseInsideWindow || !GameMain.EditMapScreen.WiringMode) return;
@@ -395,7 +417,6 @@ namespace Barotrauma.Items.Components
continue;
}
GUI.DrawRectangle(spriteBatch, worldPos + new Vector2(-10, -10), new Vector2(20, 20), Color.Red, false, 0.0f);
if (selectedNodeIndex == null && draggingWire == null)// && !MapEntity.SelectedAny)
@@ -447,26 +468,6 @@ namespace Barotrauma.Items.Components
}
}
private void DrawSection(SpriteBatch spriteBatch, Vector2 start, Vector2 end, Color color, float width = 0.3f)
{
if (item.Submarine != null)
{
start += item.Submarine.DrawPosition + item.Submarine.HiddenSubPosition;
end += item.Submarine.DrawPosition + item.Submarine.HiddenSubPosition;
}
start.Y = -start.Y;
end.Y = -end.Y;
spriteBatch.Draw(wireSprite.Texture,
start, null, color,
MathUtils.VectorToAngle(end - start),
new Vector2(0.0f, wireSprite.size.Y / 2.0f),
new Vector2((Vector2.Distance(start, end)) / wireSprite.Texture.Width, width),
SpriteEffects.None,
wireSprite.Depth + ((item.ID % 100) * 0.00001f));
}
public override void FlipX()
{
for (int i = 0; i < Nodes.Count; i++)
@@ -168,9 +168,8 @@ namespace Barotrauma.Items.Components
public override void Update(float deltaTime, Camera cam)
{
base.Update(deltaTime, cam);
item.SetTransform(picker.SimPosition, 0.0f);
item.SetContainedItemPositions();
ApplyStatusEffects(ActionType.OnWearing, deltaTime, picker);