Fixed crashing if the server attempts to spawn extra cargo that doesn't spawn in a container.

This commit is contained in:
Joonas Rikkonen
2018-07-24 19:10:18 +03:00
parent df720b67ba
commit 01e97fe4bd
@@ -111,24 +111,24 @@ namespace Barotrauma
Dictionary<ItemContainer, int> availableContainers = new Dictionary<ItemContainer, int>();
ItemPrefab containerPrefab = null;
foreach (PurchasedItem Pi in itemsToSpawn)
foreach (PurchasedItem pi in itemsToSpawn)
{
Vector2 position = new Vector2(
Rand.Range(cargoRoom.Rect.X + 20, cargoRoom.Rect.Right - 20),
cargoRoom.Rect.Y - cargoRoom.Rect.Height + Pi.itemPrefab.Size.Y / 2);
cargoRoom.Rect.Y - cargoRoom.Rect.Height + pi.itemPrefab.Size.Y / 2);
ItemContainer itemContainer = null;
if (!string.IsNullOrEmpty(Pi.itemPrefab.CargoContainerName))
if (!string.IsNullOrEmpty(pi.itemPrefab.CargoContainerName))
{
itemContainer = availableContainers.Keys.ToList().Find(ac =>
ac.Item.Prefab.NameMatches(Pi.itemPrefab.CargoContainerName) ||
ac.Item.Prefab.Tags.Contains(Pi.itemPrefab.CargoContainerName.ToLowerInvariant()));
ac.Item.Prefab.NameMatches(pi.itemPrefab.CargoContainerName) ||
ac.Item.Prefab.Tags.Contains(pi.itemPrefab.CargoContainerName.ToLowerInvariant()));
if (itemContainer == null)
{
containerPrefab = MapEntityPrefab.List.Find(ep =>
ep.NameMatches(Pi.itemPrefab.CargoContainerName) ||
(ep.Tags != null && ep.Tags.Contains(Pi.itemPrefab.CargoContainerName.ToLowerInvariant()))) as ItemPrefab;
ep.NameMatches(pi.itemPrefab.CargoContainerName) ||
(ep.Tags != null && ep.Tags.Contains(pi.itemPrefab.CargoContainerName.ToLowerInvariant()))) as ItemPrefab;
if (containerPrefab == null)
{
@@ -150,8 +150,21 @@ namespace Barotrauma
}
}
}
for (int i = 0; i < Pi.quantity; i++)
for (int i = 0; i < pi.quantity; i++)
{
if (itemContainer == null)
{
//no container, place at the waypoint
if (GameMain.Server != null)
{
Entity.Spawner.AddToSpawnQueue(pi.itemPrefab, position, wp.Submarine);
}
else
{
new Item(pi.itemPrefab, position, wp.Submarine);
}
continue;
}
//if the intial container has been removed due to it running out of space, add a new container
//of the same type and begin filling it
if (!availableContainers.ContainsKey(itemContainer))
@@ -165,28 +178,14 @@ namespace Barotrauma
}
}
if (itemContainer == null)
{
//no container, place at the waypoint
if (GameMain.Server != null)
{
Entity.Spawner.AddToSpawnQueue(Pi.itemPrefab, position, wp.Submarine);
}
else
{
new Item(Pi.itemPrefab, position, wp.Submarine);
}
}
else
{
//place in the container
if (GameMain.Server != null)
{
Entity.Spawner.AddToSpawnQueue(Pi.itemPrefab, itemContainer.Inventory);
Entity.Spawner.AddToSpawnQueue(pi.itemPrefab, itemContainer.Inventory);
}
else
{
var item = new Item(Pi.itemPrefab, position, wp.Submarine);
var item = new Item(pi.itemPrefab, position, wp.Submarine);
itemContainer.Inventory.TryPutItem(item, null);
}
@@ -202,7 +201,6 @@ namespace Barotrauma
}
}
}
}
itemsToSpawn.Clear();
}
}