Changed ItemSpawner to EntitySpawner, used for syncing both item and character spawning

This commit is contained in:
Regalis
2016-10-12 19:25:01 +03:00
parent ac3539da63
commit 60b36e020c
12 changed files with 227 additions and 181 deletions
+11 -3
View File
@@ -49,8 +49,6 @@ namespace Barotrauma
} }
} }
public bool SpawnedMidRound;
public List<Item> SpawnItems = new List<Item>(); public List<Item> SpawnItems = new List<Item>();
private bool enabled; private bool enabled;
@@ -1846,7 +1844,7 @@ namespace Barotrauma
msg.Write(Info.Job == null ? "" : Info.Job.Name); msg.Write(Info.Job == null ? "" : Info.Job.Name);
} }
public static Character ReadSpawnData(NetIncomingMessage inc) public static Character ReadSpawnData(NetIncomingMessage inc, bool spawn=true)
{ {
if (GameMain.Server != null) return null; if (GameMain.Server != null) return null;
@@ -1862,6 +1860,8 @@ namespace Barotrauma
if (noInfo) if (noInfo)
{ {
if (!spawn) return null;
character = Character.Create(configPath, position, null, true); character = Character.Create(configPath, position, null, true);
character.ID = id; character.ID = id;
} }
@@ -1877,6 +1877,8 @@ namespace Barotrauma
int headSpriteID = inc.ReadByte(); int headSpriteID = inc.ReadByte();
string jobName = inc.ReadString(); string jobName = inc.ReadString();
if (!spawn) return null;
JobPrefab jobPrefab = JobPrefab.List.Find(jp => jp.Name == jobName); JobPrefab jobPrefab = JobPrefab.List.Find(jp => jp.Name == jobName);
CharacterInfo ch = new CharacterInfo(configPath, newName, isFemale ? Gender.Female : Gender.Male, jobPrefab); CharacterInfo ch = new CharacterInfo(configPath, newName, isFemale ? Gender.Female : Gender.Male, jobPrefab);
@@ -1885,6 +1887,12 @@ namespace Barotrauma
character = Character.Create(configPath, position, ch, true, hasAi); character = Character.Create(configPath, position, ch, true, hasAi);
character.ID = id; character.ID = id;
if (GameMain.Client.MyCharacterID == id)
{
GameMain.Client.Character = character;
Controlled = character;
}
if (configPath == Character.HumanConfigFile) if (configPath == Character.HumanConfigFile)
{ {
GameMain.GameSession.CrewManager.characters.Add(character); GameMain.GameSession.CrewManager.characters.Add(character);
@@ -171,7 +171,7 @@ namespace Barotrauma
character.Enabled = false; character.Enabled = false;
husk.SpawnedMidRound = true; Entity.Spawner.AddToSpawnedList(husk);
} }
} }
} }
+1 -1
View File
@@ -294,7 +294,7 @@ namespace Barotrauma
if (spawnedCharacter != null && GameMain.Server != null) if (spawnedCharacter != null && GameMain.Server != null)
{ {
spawnedCharacter.SpawnedMidRound = true; Entity.Spawner.AddToSpawnedList(spawnedCharacter);
} }
break; break;
+115 -2
View File
@@ -31,7 +31,7 @@ namespace Barotrauma
public static List<Item> ItemList = new List<Item>(); public static List<Item> ItemList = new List<Item>();
private ItemPrefab prefab; private ItemPrefab prefab;
public static ItemSpawner Spawner = new ItemSpawner();
public static ItemRemover Remover = new ItemRemover(); public static ItemRemover Remover = new ItemRemover();
public static bool ShowLinks = true; public static bool ShowLinks = true;
@@ -1556,7 +1556,120 @@ namespace Barotrauma
} }
public void ServerWrite(NetOutgoingMessage msg, Client c) { } public void ServerWrite(NetOutgoingMessage msg, Client c) { }
public void ClientRead(NetIncomingMessage msg) { } public void ClientRead(NetIncomingMessage msg) { }
public void WriteSpawnData(NetOutgoingMessage msg)
{
if (GameMain.Server == null) return;
msg.Write(Prefab.Name);
msg.Write(ID);
if (ParentInventory == null || ParentInventory.Owner == null)
{
msg.Write((ushort)0);
msg.Write(Position.X);
msg.Write(Position.Y);
msg.Write(Submarine != null ? Submarine.ID : (ushort)0);
}
else
{
msg.Write(ParentInventory.Owner.ID);
int index = ParentInventory.FindIndex(this);
msg.Write(index < 0 ? (byte)255 : (byte)index);
}
if (Name == "ID Card") msg.Write(Tags);
}
public static Item ReadSpawnData(NetIncomingMessage msg, bool spawn = true)
{
if (GameMain.Server != null) return null;
string itemName = msg.ReadString();
ushort itemId = msg.ReadUInt16();
ushort inventoryId = msg.ReadUInt16();
Vector2 pos = Vector2.Zero;
Submarine sub = null;
int inventorySlotIndex = -1;
if (inventoryId > 0)
{
inventorySlotIndex = msg.ReadByte();
}
else
{
pos = new Vector2(msg.ReadSingle(), msg.ReadSingle());
ushort subID = msg.ReadUInt16();
if (subID > 0)
{
sub = Submarine.Loaded.Find(s => s.ID == subID);
}
}
string tags = "";
if (itemName == "ID Card")
{
tags = msg.ReadString();
}
if (!spawn) return null;
//----------------------------------------
var prefab = MapEntityPrefab.list.Find(me => me.Name == itemName);
if (prefab == null) return null;
var itemPrefab = prefab as ItemPrefab;
if (itemPrefab == null) return null;
Inventory inventory = null;
var inventoryOwner = Entity.FindEntityByID(inventoryId);
if (inventoryOwner != null)
{
if (inventoryOwner is Character)
{
inventory = (inventoryOwner as Character).Inventory;
}
else if (inventoryOwner is Item)
{
var containers = (inventoryOwner as Item).GetComponents<Items.Components.ItemContainer>();
if (containers != null && containers.Any())
{
inventory = containers.Last().Inventory;
}
}
}
var item = new Item(itemPrefab, pos, sub);
item.ID = itemId;
if (sub != null)
{
item.CurrentHull = Hull.FindHull(pos + sub.Position, null, true);
item.Submarine = item.CurrentHull == null ? null : item.CurrentHull.Submarine;
}
if (!string.IsNullOrEmpty(tags)) item.Tags = tags;
if (inventory != null)
{
if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 &&
inventory.TryPutItem(item, inventorySlotIndex, false))
{
return null;
}
inventory.TryPutItem(item, item.AllowedSlots);
}
return item;
}
public static void Load(XElement element, Submarine submarine) public static void Load(XElement element, Submarine submarine)
{ {
+1
View File
@@ -11,6 +11,7 @@ namespace Barotrauma
{ {
private static Dictionary<ushort, Entity> dictionary = new Dictionary<ushort, Entity>(); private static Dictionary<ushort, Entity> dictionary = new Dictionary<ushort, Entity>();
public static EntitySpawner Spawner = new EntitySpawner();
private ushort id; private ushort id;
+1 -1
View File
@@ -269,7 +269,7 @@ namespace Barotrauma
item.Update(cam, deltaTime); item.Update(cam, deltaTime);
} }
Item.Spawner.Update(); Entity.Spawner.Update();
Item.Remover.Update(); Item.Remover.Update();
} }
+2 -2
View File
@@ -36,7 +36,7 @@ namespace Barotrauma.Networking
public UInt32 lastSentChatMsgID = 0; //last msg this client said public UInt32 lastSentChatMsgID = 0; //last msg this client said
public UInt32 lastRecvChatMsgID = 0; //last msg this client knows about public UInt32 lastRecvChatMsgID = 0; //last msg this client knows about
public UInt32 lastRecvItemSpawnID = 0; public UInt32 lastRecvEntitySpawnID = 0;
public UInt32 lastRecvItemRemoveID = 0; public UInt32 lastRecvItemRemoveID = 0;
@@ -63,7 +63,7 @@ namespace Barotrauma.Networking
lastSentChatMsgID = 0; lastSentChatMsgID = 0;
lastRecvChatMsgID = ChatMessage.LastID; lastRecvChatMsgID = ChatMessage.LastID;
lastRecvItemSpawnID = 0; lastRecvEntitySpawnID = 0;
lastRecvItemRemoveID = 0; lastRecvItemRemoveID = 0;
} }
@@ -6,15 +6,22 @@ using System;
namespace Barotrauma namespace Barotrauma
{ {
class ItemSpawner : IServerSerializable class EntitySpawner : IServerSerializable
{ {
private enum SpawnableType { Item, Character };
public UInt32 NetStateID public UInt32 NetStateID
{ {
get; get;
private set; private set;
} }
class ItemSpawnInfo interface IEntitySpawnInfo
{
Entity Spawn();
}
class ItemSpawnInfo : IEntitySpawnInfo
{ {
public readonly ItemPrefab Prefab; public readonly ItemPrefab Prefab;
@@ -40,17 +47,32 @@ namespace Barotrauma
Prefab = prefab; Prefab = prefab;
Inventory = inventory; Inventory = inventory;
} }
public Entity Spawn()
{
Item spawnedItem = null;
if (Inventory != null)
{
spawnedItem = new Item(Prefab, Vector2.Zero, null);
Inventory.TryPutItem(spawnedItem, spawnedItem.AllowedSlots);
}
else
{
spawnedItem = new Item(Prefab, Position, Submarine);
}
return spawnedItem;
}
} }
private readonly Queue<ItemSpawnInfo> spawnQueue; private readonly Queue<IEntitySpawnInfo> spawnQueue;
private List<Entity> spawnedEntities = new List<Entity>();
public List<Item> spawnItems = new List<Item>();
public EntitySpawner()
public ItemSpawner()
{ {
spawnQueue = new Queue<ItemSpawnInfo>(); spawnQueue = new Queue<IEntitySpawnInfo>();
} }
public void QueueItem(ItemPrefab itemPrefab, Vector2 worldPosition, bool isNetworkMessage = false) public void QueueItem(ItemPrefab itemPrefab, Vector2 worldPosition, bool isNetworkMessage = false)
@@ -80,45 +102,27 @@ namespace Barotrauma
public void Update() public void Update()
{ {
if (!spawnQueue.Any()) return; if (!spawnQueue.Any()) return;
List<Item> items = new List<Item>();
//List<Inventory> inventories = new List<Inventory>(); //List<Inventory> inventories = new List<Inventory>();
while (spawnQueue.Count>0) while (spawnQueue.Count>0)
{ {
var itemInfo = spawnQueue.Dequeue(); var entitySpawnInfo = spawnQueue.Dequeue();
Item spawnedItem = null; var spawnedEntity = entitySpawnInfo.Spawn();
if (itemInfo.Inventory != null) if (spawnedEntity!= null) AddToSpawnedList(spawnedEntity);
{
spawnedItem = new Item(itemInfo.Prefab, Vector2.Zero, null);
itemInfo.Inventory.TryPutItem(spawnedItem, spawnedItem.AllowedSlots);
}
else
{
spawnedItem = new Item(itemInfo.Prefab, itemInfo.Position, itemInfo.Submarine);
}
AddToSpawnedList(spawnedItem);
items.Add(spawnedItem);
} }
//if (GameMain.Server != null) GameMain.Server.SendItemSpawnMessage(items); //if (GameMain.Server != null) GameMain.Server.SendItemSpawnMessage(items);
} }
public void AddToSpawnedList(List<Item> items) public void AddToSpawnedList(Entity entity)
{ {
foreach (Item item in items) if (GameMain.Server == null) return;
{ if (entity == null) return;
AddToSpawnedList(item);
}
}
public void AddToSpawnedList(Item item) spawnedEntities.Add(entity);
{ NetStateID = (UInt32)spawnedEntities.Count;
spawnItems.Add(item);
NetStateID = (UInt32)spawnItems.Count;
} }
public void ServerWrite(Lidgren.Network.NetOutgoingMessage message, Client client) public void ServerWrite(Lidgren.Network.NetOutgoingMessage message, Client client)
@@ -126,35 +130,22 @@ namespace Barotrauma
if (GameMain.Server == null) return; if (GameMain.Server == null) return;
//skip items that the client already knows about //skip items that the client already knows about
List<Item> items = spawnItems.Skip((int)client.lastRecvItemSpawnID).ToList(); List<Entity> entities = spawnedEntities.Skip((int)client.lastRecvEntitySpawnID).ToList();
message.Write((UInt32)spawnItems.Count); message.Write((UInt32)spawnedEntities.Count);
message.Write((byte)items.Count); message.Write((byte)entities.Count);
for (int i = 0; i < items.Count; i++) for (int i = 0; i < entities.Count; i++)
{ {
message.Write(items[i].Prefab.Name); if (entities[i] is Item)
message.Write(items[i].ID);
if (items[i].ParentInventory == null || items[i].ParentInventory.Owner == null)
{ {
message.Write((ushort)0); message.Write((byte)SpawnableType.Item);
((Item)entities[i]).WriteSpawnData(message);
message.Write(items[i].Position.X);
message.Write(items[i].Position.Y);
message.Write(items[i].Submarine != null ? items[i].Submarine.ID : (ushort)0);
} }
else else if (entities[i] is Character)
{ {
message.Write(items[i].ParentInventory.Owner.ID); message.Write((byte)SpawnableType.Character);
((Character)entities[i]).WriteSpawnData(message);
int index = items[i].ParentInventory.FindIndex(items[i]);
message.Write(index < 0 ? (byte)255 : (byte)index);
}
if (items[i].Name == "ID Card")
{
message.Write(items[i].Tags);
} }
} }
} }
@@ -165,102 +156,38 @@ namespace Barotrauma
UInt32 ID = message.ReadUInt32(); UInt32 ID = message.ReadUInt32();
var itemCount = message.ReadByte(); var entityCount = message.ReadByte();
for (int i = 0; i < itemCount; i++) for (int i = 0; i < entityCount; i++)
{ {
string itemName = message.ReadString(); switch (message.ReadByte())
ushort itemId = message.ReadUInt16();
ushort inventoryId = message.ReadUInt16();
Vector2 pos = Vector2.Zero;
Submarine sub = null;
int inventorySlotIndex = -1;
if (inventoryId > 0)
{ {
inventorySlotIndex = message.ReadByte(); case (byte)SpawnableType.Item:
} Item.ReadSpawnData(message, ID - entityCount + i >= NetStateID);
else break;
{ case (byte)SpawnableType.Character:
pos = new Vector2(message.ReadSingle(), message.ReadSingle()); Character.ReadSpawnData(message, ID - entityCount + i >= NetStateID);
break;
ushort subID = message.ReadUInt16(); default:
if (subID > 0) DebugConsole.ThrowError("Received invalid entity spawn message (unknown spawnable type)");
{ break;
sub = Submarine.Loaded.Find(s => s.ID == subID);
}
} }
string tags = "";
if (itemName == "ID Card")
{
tags = message.ReadString();
}
if (ID - itemCount + i < NetStateID) continue;
//----------------------------------------
var prefab = MapEntityPrefab.list.Find(me => me.Name == itemName);
if (prefab == null) continue;
var itemPrefab = prefab as ItemPrefab;
if (itemPrefab == null) continue;
Inventory inventory = null;
var inventoryOwner = Entity.FindEntityByID(inventoryId);
if (inventoryOwner != null)
{
if (inventoryOwner is Character)
{
inventory = (inventoryOwner as Character).Inventory;
}
else if (inventoryOwner is Item)
{
var containers = (inventoryOwner as Item).GetComponents<Items.Components.ItemContainer>();
if (containers!=null && containers.Any())
{
inventory = containers.Last().Inventory;
}
}
}
var item = new Item(itemPrefab, pos, sub);
item.ID = itemId;
if (sub != null)
{
item.CurrentHull = Hull.FindHull(pos + sub.Position, null, true);
item.Submarine = item.CurrentHull == null ? null : item.CurrentHull.Submarine;
}
if (!string.IsNullOrEmpty(tags)) item.Tags = tags;
if (inventory != null)
{
if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 &&
inventory.TryPutItem(item, inventorySlotIndex, false))
{
continue;
}
inventory.TryPutItem(item, item.AllowedSlots);
}
} }
NetStateID = Math.Max(ID, NetStateID); NetStateID = Math.Max(ID, NetStateID);
} }
public void Clear() public void Clear()
{ {
NetStateID = 0; NetStateID = 0;
spawnQueue.Clear(); spawnQueue.Clear();
spawnItems.Clear(); spawnedEntities.Clear();
} }
} }
//todo: turn into a generic EntityRemover class + sync
class ItemRemover : IServerSerializable class ItemRemover : IServerSerializable
{ {
public UInt32 NetStateID public UInt32 NetStateID
+20 -11
View File
@@ -44,6 +44,13 @@ namespace Barotrauma.Networking
get { return myID; } get { return myID; }
} }
public ushort MyCharacterID
{
get;
private set;
}
public override List<Client> ConnectedClients public override List<Client> ConnectedClients
{ {
get get
@@ -570,6 +577,8 @@ namespace Barotrauma.Networking
bool respawnAllowed = inc.ReadBoolean(); bool respawnAllowed = inc.ReadBoolean();
bool loadSecondSub = inc.ReadBoolean(); bool loadSecondSub = inc.ReadBoolean();
MyCharacterID = inc.ReadUInt16();
GameModePreset gameMode = GameModePreset.list.Find(gm => gm.Name == modeName); GameModePreset gameMode = GameModePreset.list.Find(gm => gm.Name == modeName);
if (gameMode == null) if (gameMode == null)
@@ -595,16 +604,16 @@ namespace Barotrauma.Networking
if (respawnAllowed) respawnManager = new RespawnManager(this, GameMain.NetLobbyScreen.SelectedShuttle); if (respawnAllowed) respawnManager = new RespawnManager(this, GameMain.NetLobbyScreen.SelectedShuttle);
int characterCount = inc.ReadByte(); //int characterCount = inc.ReadByte();
for (int i = 0; i < characterCount; i++) //for (int i = 0; i < characterCount; i++)
{ //{
var character = Character.ReadSpawnData(inc); // var character = Character.ReadSpawnData(inc);
if (inc.ReadBoolean()) // if (inc.ReadBoolean())
{ // {
myCharacter = character; // myCharacter = character;
Character.Controlled = character; // Character.Controlled = character;
} // }
} //}
gameStarted = true; gameStarted = true;
@@ -704,7 +713,7 @@ namespace Barotrauma.Networking
case ServerNetObject.CHAT_MESSAGE: case ServerNetObject.CHAT_MESSAGE:
ChatMessage.ClientRead(inc); ChatMessage.ClientRead(inc);
break; break;
case ServerNetObject.ITEM_SPAWN: case ServerNetObject.ENTITY_SPAWN:
Item.Spawner.ClientRead(inc); Item.Spawner.ClientRead(inc);
break; break;
} }
+7 -21
View File
@@ -601,7 +601,7 @@ namespace Barotrauma.Networking
c.lastRecvGeneralUpdate = Math.Max(c.lastRecvGeneralUpdate, inc.ReadUInt32()); c.lastRecvGeneralUpdate = Math.Max(c.lastRecvGeneralUpdate, inc.ReadUInt32());
c.lastRecvChatMsgID = Math.Max(c.lastRecvChatMsgID, inc.ReadUInt32()); c.lastRecvChatMsgID = Math.Max(c.lastRecvChatMsgID, inc.ReadUInt32());
c.lastRecvItemSpawnID = Math.Max(c.lastRecvItemSpawnID, inc.ReadUInt32()); c.lastRecvEntitySpawnID = Math.Max(c.lastRecvEntitySpawnID, inc.ReadUInt32());
break; break;
case ClientNetObject.CHAT_MESSAGE: case ClientNetObject.CHAT_MESSAGE:
@@ -656,9 +656,9 @@ namespace Barotrauma.Networking
outmsg.WritePadBits(); outmsg.WritePadBits();
} }
if (Item.Spawner.NetStateID > c.lastRecvItemSpawnID) if (Item.Spawner.NetStateID > c.lastRecvEntitySpawnID)
{ {
outmsg.Write((byte)ServerNetObject.ITEM_SPAWN); outmsg.Write((byte)ServerNetObject.ENTITY_SPAWN);
Item.Spawner.ServerWrite(outmsg, c); Item.Spawner.ServerWrite(outmsg, c);
outmsg.WritePadBits(); outmsg.WritePadBits();
} }
@@ -893,7 +893,9 @@ namespace Barotrauma.Networking
foreach (Character c in GameMain.GameSession.CrewManager.characters) foreach (Character c in GameMain.GameSession.CrewManager.characters)
{ {
Item.Spawner.AddToSpawnedList(c.SpawnItems); Entity.Spawner.AddToSpawnedList(c);
c.SpawnItems.ForEach(item => Entity.Spawner.AddToSpawnedList(item));
} }
SendStartMessage(roundStartSeed, Submarine.MainSub, GameMain.GameSession.gameMode.Preset, connectedClients); SendStartMessage(roundStartSeed, Submarine.MainSub, GameMain.GameSession.gameMode.Preset, connectedClients);
@@ -947,23 +949,7 @@ namespace Barotrauma.Networking
msg.Write(AllowRespawn); msg.Write(AllowRespawn);
msg.Write(Submarine.MainSubs[1] != null); //loadSecondSub msg.Write(Submarine.MainSubs[1] != null); //loadSecondSub
var clientsWithCharacter = clients.FindAll(c => c.Character != null); msg.Write(client.Character.ID);
int characterCount = clientsWithCharacter.Count;
if (myCharacter != null) characterCount++;
msg.Write((byte)characterCount);
foreach (Client c in clientsWithCharacter)
{
c.Character.WriteSpawnData(msg);
msg.Write(c == client);
}
if (myCharacter != null)
{
myCharacter.WriteSpawnData(msg);
msg.Write(false);
}
server.SendMessage(msg, client.Connection, NetDeliveryMethod.ReliableUnordered); server.SendMessage(msg, client.Connection, NetDeliveryMethod.ReliableUnordered);
} }
@@ -47,7 +47,7 @@ namespace Barotrauma.Networking
CHARACTER_POSITION, CHARACTER_POSITION,
ITEM_STATE, ITEM_STATE,
ITEM_SPAWN ENTITY_SPAWN
} }
enum VoteType enum VoteType
+2
View File
@@ -67,10 +67,12 @@ namespace Barotrauma
if (Character.Controlled!=null) if (Character.Controlled!=null)
{ {
cam.Position = Character.Controlled.WorldPosition; cam.Position = Character.Controlled.WorldPosition;
cam.UpdateTransform();
} }
else if (Submarine.MainSub != null) else if (Submarine.MainSub != null)
{ {
cam.Position = Submarine.MainSub.WorldPosition; cam.Position = Submarine.MainSub.WorldPosition;
cam.UpdateTransform();
} }
foreach (MapEntity entity in MapEntity.mapEntityList) foreach (MapEntity entity in MapEntity.mapEntityList)