Browse Source

Multiplayer Fixes

bolt_update
laurids 3 years ago
parent
commit
9fb0d04948
12 changed files with 152 additions and 49 deletions
  1. +2
    -2
      Assets/GWConquest/Scripts/Battle.cs
  2. +0
    -3
      Assets/GWConquest/Scripts/BoltList.cs
  3. +15
    -4
      Assets/GWConquest/Scripts/District.cs
  4. +68
    -7
      Assets/GWConquest/Scripts/EntityList.cs
  5. +22
    -14
      Assets/GWConquest/Scripts/Formation.cs
  6. +34
    -13
      Assets/GWConquest/Scripts/Inventory.cs
  7. +5
    -0
      Assets/GWConquest/Scripts/Player.cs
  8. +1
    -1
      Assets/GWConquest/Scripts/SpawnAIUnits.cs
  9. +2
    -2
      Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs
  10. +1
    -1
      Assets/GWConquest/Scripts/UI/DebugUI.cs
  11. +1
    -1
      Assets/GWConquest/Scripts/Unit.cs
  12. +1
    -1
      Assets/GWConquest/Scripts/Util.cs

+ 2
- 2
Assets/GWConquest/Scripts/Battle.cs View File

@ -9,11 +9,11 @@ namespace GWConquest
public EntityList FormationList; public EntityList FormationList;
public IEnumerable<Player> AllPlayers { public IEnumerable<Player> AllPlayers {
get => FormationList.Select((f,i) => f.GetComponent<Formation>().Player).Distinct();
get => FormationList.Select((f,i) => f.Entity.GetComponent<Formation>().Player).Distinct();
} }
public IEnumerable<Unit> AllUnits { public IEnumerable<Unit> AllUnits {
get => FormationList.SelectMany(e => e.GetComponent<Formation>().Units);
get => FormationList.SelectMany(e => e.Entity.GetComponent<Formation>().Units);
} }
private List<UnitAction> UnitActions = new List<UnitAction>(); private List<UnitAction> UnitActions = new List<UnitAction>();


+ 0
- 3
Assets/GWConquest/Scripts/BoltList.cs View File

@ -12,9 +12,6 @@ namespace GWConquest
private IState state; private IState state;
private string propertyName; private string propertyName;
private Func<UdpPacket, T> readMethod;
private Action<UdpPacket, T> writeMethod;
public BoltList(IState _state, string _propertyName) public BoltList(IState _state, string _propertyName)
{ {
state = _state; state = _state;


+ 15
- 4
Assets/GWConquest/Scripts/District.cs View File

@ -22,13 +22,19 @@ namespace GWConquest
public Player ControllingPlayer { public Player ControllingPlayer {
get { get {
if(State.ControllingPlayerId == -1)
var id = State.ControllingPlayerId;
if(id < 0)
{ {
return null; return null;
} }
else if(id >= Player.PlayerList.Count)
{
BoltLog.Warn("Tried accessing player {0} in player list of length {1}", id, Player.PlayerList.Count);
return null;
}
else else
{ {
return Player.PlayerList[State.ControllingPlayerId];
return Player.GetPlayerById(State.ControllingPlayerId);
} }
} }
@ -52,10 +58,15 @@ namespace GWConquest
public override void Attached() public override void Attached()
{ {
StorageCapacity = GameManager.Instance.DefaultStorageCapacity;
if(entity.IsOwner)
{
StorageCapacity = GameManager.Instance.DefaultStorageCapacity;
State.ControllingPlayerId = -1;
}
Inventory = new Inventory(State, "Inventory"); Inventory = new Inventory(State, "Inventory");
Inventory.StorageCapacity = StorageCapacity; Inventory.StorageCapacity = StorageCapacity;
State.ControllingPlayerId = -1;
BoltLog.Info("Setting storage capacity to {0}", StorageCapacity);
AllDistricts.Add(this); AllDistricts.Add(this);
} }


+ 68
- 7
Assets/GWConquest/Scripts/EntityList.cs View File

@ -35,15 +35,66 @@ namespace GWConquest
} }
} }
public class EntityListToken : BoltListToken<BoltEntity>
public class CachedBoltEntity
{ {
public override BoltEntity ReadEntry(UdpPacket packet)
public NetworkId NetworkId { get; private set; }
private BoltEntity cachedEntity;
public CachedBoltEntity(NetworkId id)
{
NetworkId = id;
}
public CachedBoltEntity(BoltEntity e)
{
NetworkId = e.NetworkId;
cachedEntity = e;
}
public BoltEntity Entity
{
get
{
if (cachedEntity == null)
{
cachedEntity = BoltNetwork.FindEntity(NetworkId);
}
return cachedEntity;
}
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var other = obj as CachedBoltEntity;
return NetworkId == other.NetworkId;
}
public override int GetHashCode()
{
return NetworkId.PackedValue.GetHashCode();
}
public override string ToString()
{
return Entity?.ToString();
}
}
public class EntityListToken : BoltListToken<CachedBoltEntity>
{
public override CachedBoltEntity ReadEntry(UdpPacket packet)
{ {
var id = new NetworkId(packet.ReadULong());
return BoltNetwork.FindEntity(id);
var id = new NetworkId(packet.ReadULong());
return new CachedBoltEntity(id);
} }
public override void WriteEntry(UdpPacket packet, BoltEntity entry)
public override void WriteEntry(UdpPacket packet, CachedBoltEntity entry)
{ {
packet.WriteULong(entry.NetworkId.PackedValue); packet.WriteULong(entry.NetworkId.PackedValue);
} }
@ -51,8 +102,18 @@ public override void WriteEntry(UdpPacket packet, BoltEntity entry)
} }
public class EntityList : BoltList<BoltEntity, EntityListToken>
public class EntityList : BoltList<CachedBoltEntity, EntityListToken>
{ {
public EntityList(IState _state, string _propertyName) : base(_state, _propertyName) {}
public EntityList(IState _state, string _propertyName) : base(_state, _propertyName) { }
public void Add(BoltEntity e)
{
Add(new CachedBoltEntity(e));
}
public bool Remove(BoltEntity e)
{
return Remove(new CachedBoltEntity(e));
}
} }
} }

+ 22
- 14
Assets/GWConquest/Scripts/Formation.cs View File

@ -77,7 +77,7 @@ namespace GWConquest
public IEnumerable<Unit> Units public IEnumerable<Unit> Units
{ {
get => UnitEntities.Select((e,i) => e.GetComponent<Unit>());
get => UnitEntities.Select((e,i) => e.Entity.GetComponent<Unit>());
} }
public Unit HeroUnit public Unit HeroUnit
@ -93,7 +93,7 @@ namespace GWConquest
public IEnumerable<Formation> SubFormations public IEnumerable<Formation> SubFormations
{ {
get => SubFormationEntities.Select((e,i) => e.GetComponent<Formation>());
get => SubFormationEntities.Select((e,i) => e.Entity.GetComponent<Formation>());
} }
public ZoneList PathQueue; public ZoneList PathQueue;
@ -240,15 +240,11 @@ namespace GWConquest
movingArmyIcon.transform.SetParent(FindObjectOfType<IngameUI>().PlanetView.DistrictIcons); movingArmyIcon.transform.SetParent(FindObjectOfType<IngameUI>().PlanetView.DistrictIcons);
movingArmyIcon.transform.localRotation = Quaternion.identity; movingArmyIcon.transform.localRotation = Quaternion.identity;
var iconComp = movingArmyIcon.GetComponent<FormationIcon>(); var iconComp = movingArmyIcon.GetComponent<FormationIcon>();
iconComp.Arrow.gameObject.SetActive(true);
iconComp.UpdateDisplay(this); iconComp.UpdateDisplay(this);
iconComp.CanBeDragged = false; iconComp.CanBeDragged = false;
var stick = movingArmyIcon.AddComponent<StickUIToPlanet>(); var stick = movingArmyIcon.AddComponent<StickUIToPlanet>();
stick.target = transform; stick.target = transform;
//movingArmyIcon.GetComponent<StickUIToPlanet>().target = transform;
//movingArmyIcon.GetComponentInChildren<UnityEngine.UI.Image>().color = Player.Color;
//movingArmyIcon.GetComponentInChildren<UnityEngine.UI.Text>().color = Player.Color;
iconComp.Arrow.gameObject.SetActive(true);
//Transform circleTransform = movingArmyIcon.GetComponentInChildren<UnityEngine.UI.Image>().transform;
Camera cam = Camera.main; Camera cam = Camera.main;
Vector3 originPoint = RectTransformUtility.WorldToScreenPoint(cam, originZone.transform.position); Vector3 originPoint = RectTransformUtility.WorldToScreenPoint(cam, originZone.transform.position);
Vector3 targetPoint = RectTransformUtility.WorldToScreenPoint(cam, targetZone.transform.position); Vector3 targetPoint = RectTransformUtility.WorldToScreenPoint(cam, targetZone.transform.position);
@ -425,7 +421,13 @@ namespace GWConquest
public float GetFormationStrength() public float GetFormationStrength()
{ {
return UnitEntities.Sum(unit => unit.GetComponent<Unit>().Class.UnitStrength);
if(entity.IsAttached)
{
return UnitEntities.Sum(unit => unit.Entity.GetComponent<Unit>().Class.UnitStrength);
}
else {
return 0f;
}
} }
@ -436,20 +438,26 @@ namespace GWConquest
{ {
currentZone.OnFormationChanged(this); currentZone.OnFormationChanged(this);
} }
if(!keepFormationName)
if(entity.IsOwner)
{ {
ConstructName();
}
if(!keepFormationName)
{
ConstructName();
}
}
OnUnitsChanged?.Invoke(); OnUnitsChanged?.Invoke();
} }
public void OnUnitRemoved(Unit unit) public void OnUnitRemoved(Unit unit)
{ {
OnUnitsChanged?.Invoke(); OnUnitsChanged?.Invoke();
if(!keepFormationName)
if(entity.IsOwner)
{ {
ConstructName();
}
if(!keepFormationName)
{
ConstructName();
}
}
} }
public void AddSubFormation(Formation f) public void AddSubFormation(Formation f)


+ 34
- 13
Assets/GWConquest/Scripts/Inventory.cs View File

@ -7,7 +7,7 @@ using UnityEngine;
namespace GWConquest namespace GWConquest
{ {
[System.Serializable] [System.Serializable]
public class ItemStack : IIconObject
public class ItemStack : IIconObject, IProtocolToken
{ {
public static int MaxStackSize { public static int MaxStackSize {
get => GameManager.Instance.MaxStackSize; get => GameManager.Instance.MaxStackSize;
@ -98,27 +98,48 @@ namespace GWConquest
(stack1.ItemName == stack2.ItemName)); (stack1.ItemName == stack2.ItemName));
} }
public void Write(UdpPacket packet)
{
packet.WriteBool(IsUnit);
if(IsUnit)
{
packet.WriteULong(Unit.entity.NetworkId.PackedValue);
}
else {
packet.WriteString(ItemName);
}
packet.WriteBool(Stackable);
packet.WriteInt(Amount);
}
public void Read(UdpPacket packet)
{
IsUnit = packet.ReadBool();
if(IsUnit)
{
Unit = BoltNetwork.FindEntity(new NetworkId(packet.ReadULong()))?.GetComponent<Unit>();
}
else {
ItemName = packet.ReadString();
}
Stackable = packet.ReadBool();
Amount = packet.ReadInt();
}
} }
public class InventoryToken : BoltListToken<ItemStack> public class InventoryToken : BoltListToken<ItemStack>
{ {
public override void WriteEntry(UdpPacket packet, ItemStack entry)
public override void WriteEntry(UdpPacket packet, ItemStack item)
{ {
packet.WriteBool(entry.IsUnit);
packet.WriteString(entry.ItemName);
packet.WriteULong(entry.Unit.entity.NetworkId.PackedValue);
packet.WriteBool(entry.Stackable);
packet.WriteInt(entry.Amount);
item.Write(packet);
} }
public override ItemStack ReadEntry(UdpPacket packet) public override ItemStack ReadEntry(UdpPacket packet)
{ {
ItemStack item = new ItemStack();
item.IsUnit = packet.ReadBool();
item.ItemName = packet.ReadString();
item.Unit = BoltNetwork.FindEntity(new NetworkId(packet.ReadULong()))?.GetComponent<Unit>();
item.Stackable = packet.ReadBool();
item.Amount = packet.ReadInt();
var item = new ItemStack();
item.Read(packet);
return item; return item;
} }
} }


+ 5
- 0
Assets/GWConquest/Scripts/Player.cs View File

@ -10,6 +10,11 @@ namespace GWConquest
public static Player CurrentPlayer; public static Player CurrentPlayer;
public static Player GetPlayerById(int id)
{
return PlayerList.Where(p => p.PlayerId == id).FirstOrDefault();
}
public Faction Faction public Faction Faction
{ {
get => GameManager.Instance.Factions[State.FactionIndex]; get => GameManager.Instance.Factions[State.FactionIndex];


+ 1
- 1
Assets/GWConquest/Scripts/SpawnAIUnits.cs View File

@ -16,7 +16,7 @@ namespace GWConquest {
{ {
BoltLog.Info("Spawning AI units"); BoltLog.Info("Spawning AI units");
Player player = Player.PlayerList[playerId];
Player player = Player.GetPlayerById(playerId);
if(player != null) if(player != null)
{ {
for(int i = 0; i < unitCount; i++) for(int i = 0; i < unitCount; i++)


+ 2
- 2
Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs View File

@ -34,12 +34,12 @@ namespace GWConquest {
if(IsOwnPlayer) if(IsOwnPlayer)
{ {
return Battle.FormationList.Select((e,i) => e.GetComponent<Formation>()).Where(f => f.Player == Player.CurrentPlayer );
return Battle.FormationList.Select((e,i) => e.Entity.GetComponent<Formation>()).Where(f => f.Player == Player.CurrentPlayer );
} }
else { else {
return Battle.FormationList.Select((e,i) => e.GetComponent<Formation>()).Where(f => f.Player != Player.CurrentPlayer );
return Battle.FormationList.Select((e,i) => e.Entity.GetComponent<Formation>()).Where(f => f.Player != Player.CurrentPlayer );
} }
} }


+ 1
- 1
Assets/GWConquest/Scripts/UI/DebugUI.cs View File

@ -24,7 +24,7 @@ namespace GWConquest
if(playerID < 0 || playerID >= Player.PlayerList.Count) if(playerID < 0 || playerID >= Player.PlayerList.Count)
throw new ArgumentException("Player not found"); throw new ArgumentException("Player not found");
Player player = Player.PlayerList[playerID];
Player player = Player.GetPlayerById(playerID);
UnitClass uc = UnitClass.FromName(UnitNameText.text); UnitClass uc = UnitClass.FromName(UnitNameText.text);


+ 1
- 1
Assets/GWConquest/Scripts/Unit.cs View File

@ -33,7 +33,7 @@ namespace GWConquest
public Formation Formation public Formation Formation
{ {
get => state.Formation.GetComponent<Formation>();
get => state.Formation?.GetComponent<Formation>();
set => state.Formation = value.entity; set => state.Formation = value.entity;
} }


+ 1
- 1
Assets/GWConquest/Scripts/Util.cs View File

@ -42,7 +42,7 @@ namespace GWConquest
str += "["; str += "[";
foreach (T o in e) foreach (T o in e)
{ {
str += o.ToString();
str += o == null ? "null" : o.ToString();
str += "; "; str += "; ";
} }
str += "]"; str += "]";


Loading…
Cancel
Save