Cargo items can be set to spawn inside specific types of containers instead of being scattered on the cargo room floor.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Barotrauma.Items.Components;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -54,6 +55,11 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
public void CreateItems()
|
||||
{
|
||||
CreateItems(purchasedItems);
|
||||
}
|
||||
|
||||
public static void CreateItems(List<ItemPrefab> itemsToSpawn)
|
||||
{
|
||||
WayPoint wp = WayPoint.GetRandom(SpawnType.Cargo, null, Submarine.MainSub);
|
||||
|
||||
@@ -71,24 +77,82 @@ namespace Barotrauma
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ItemPrefab prefab in purchasedItems)
|
||||
Dictionary<ItemContainer, int> availableContainers = new Dictionary<ItemContainer, int>();
|
||||
foreach (ItemPrefab prefab in itemsToSpawn)
|
||||
{
|
||||
Vector2 position = new Vector2(
|
||||
Rand.Range(cargoRoom.Rect.X + 20, cargoRoom.Rect.Right - 20),
|
||||
cargoRoom.Rect.Y - cargoRoom.Rect.Height + prefab.Size.Y/2);
|
||||
cargoRoom.Rect.Y - cargoRoom.Rect.Height + prefab.Size.Y / 2);
|
||||
|
||||
if (GameMain.Server != null)
|
||||
ItemContainer itemContainer = null;
|
||||
if (!string.IsNullOrEmpty(prefab.CargoContainerName))
|
||||
{
|
||||
Entity.Spawner.AddToSpawnQueue(prefab, position, wp.Submarine);
|
||||
itemContainer = availableContainers.Keys.ToList().Find(ac =>
|
||||
ac.Item.Prefab.NameMatches(prefab.CargoContainerName) ||
|
||||
ac.Item.Prefab.Tags.Contains(prefab.CargoContainerName.ToLowerInvariant()));
|
||||
|
||||
if (itemContainer == null)
|
||||
{
|
||||
var containerPrefab = MapEntityPrefab.List.Find(ep =>
|
||||
ep.NameMatches(prefab.CargoContainerName) ||
|
||||
(ep.Tags != null && ep.Tags.Contains(prefab.CargoContainerName.ToLowerInvariant()))) as ItemPrefab;
|
||||
|
||||
if (containerPrefab == null)
|
||||
{
|
||||
DebugConsole.ThrowError("Cargo spawning failed - could not find the item prefab for container \"" + containerPrefab.Name + "\"!");
|
||||
continue;
|
||||
}
|
||||
|
||||
Item containerItem = new Item(containerPrefab, position, wp.Submarine);
|
||||
itemContainer = containerItem.GetComponent<ItemContainer>();
|
||||
if (itemContainer == null)
|
||||
{
|
||||
DebugConsole.ThrowError("Cargo spawning failed - container \"" + containerItem.Name + "\" does not have an ItemContainer component!");
|
||||
continue;
|
||||
}
|
||||
availableContainers.Add(itemContainer, itemContainer.Capacity);
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
Entity.Spawner.CreateNetworkEvent(itemContainer.Item, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemContainer == null)
|
||||
{
|
||||
//no container, place at the waypoint
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
Entity.Spawner.AddToSpawnQueue(prefab, position, wp.Submarine);
|
||||
}
|
||||
else
|
||||
{
|
||||
new Item(prefab, position, wp.Submarine);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
new Item(prefab, position, wp.Submarine);
|
||||
}
|
||||
//place in the container
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
Entity.Spawner.AddToSpawnQueue(prefab, itemContainer.Inventory);
|
||||
}
|
||||
else
|
||||
{
|
||||
var item = new Item(prefab, position, wp.Submarine);
|
||||
itemContainer.Inventory.TryPutItem(item, null);
|
||||
}
|
||||
|
||||
//reduce the number of available slots in the container
|
||||
availableContainers[itemContainer]--;
|
||||
if (availableContainers[itemContainer] <= 0)
|
||||
{
|
||||
availableContainers.Remove(itemContainer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
purchasedItems.Clear();
|
||||
itemsToSpawn.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +145,13 @@ namespace Barotrauma
|
||||
private set;
|
||||
}
|
||||
|
||||
[Serialize("", false)]
|
||||
public string CargoContainerName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public bool CanSpriteFlipX
|
||||
{
|
||||
get { return canSpriteFlipX; }
|
||||
|
||||
@@ -1303,29 +1303,16 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
if (sub == null) continue;
|
||||
|
||||
WayPoint cargoSpawnPos = WayPoint.GetRandom(SpawnType.Cargo, null, sub);
|
||||
|
||||
if (cargoSpawnPos?.CurrentHull == null)
|
||||
List<ItemPrefab> spawnList = new List<ItemPrefab>();
|
||||
foreach (KeyValuePair<ItemPrefab, int> kvp in extraCargo)
|
||||
{
|
||||
DebugConsole.ThrowError("Couldn't spawn additional cargo (no cargo spawnpoint inside any of the hulls)");
|
||||
continue;
|
||||
}
|
||||
|
||||
var cargoRoom = cargoSpawnPos.CurrentHull;
|
||||
Vector2 position = new Vector2(
|
||||
cargoSpawnPos.Position.X,
|
||||
cargoRoom.Rect.Y - cargoRoom.Rect.Height);
|
||||
|
||||
foreach (string s in extraCargo.Keys)
|
||||
{
|
||||
ItemPrefab itemPrefab = MapEntityPrefab.Find(s) as ItemPrefab;
|
||||
if (itemPrefab == null) continue;
|
||||
|
||||
for (int i = 0; i < extraCargo[s]; i++)
|
||||
for (int i = 0; i < kvp.Value; i++)
|
||||
{
|
||||
Entity.Spawner.AddToSpawnQueue(itemPrefab, position + new Vector2(Rand.Range(-20.0f, 20.0f), itemPrefab.Size.Y / 2), sub);
|
||||
spawnList.Add(kvp.Key);
|
||||
}
|
||||
}
|
||||
|
||||
CargoManager.CreateItems(spawnList);
|
||||
}
|
||||
|
||||
TraitorManager = null;
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Barotrauma.Networking
|
||||
private set;
|
||||
}
|
||||
|
||||
public Dictionary<string, int> extraCargo;
|
||||
public Dictionary<ItemPrefab, int> extraCargo;
|
||||
|
||||
public bool ShowNetStats;
|
||||
|
||||
@@ -345,7 +345,7 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
monsterEnabled.Add(s, true);
|
||||
}
|
||||
extraCargo = new Dictionary<string, int>();
|
||||
extraCargo = new Dictionary<ItemPrefab, int>();
|
||||
}
|
||||
|
||||
public void LoadClientPermissions()
|
||||
|
||||
Reference in New Issue
Block a user