Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Commit

Permalink
Progress #1407 -- HandleSpawn and Champion refactor (#1440)
Browse files Browse the repository at this point in the history
I refactored the `HandleSpawn` class and unified the spawning of champions with the spawning of other objects, moving most of the code into the appropriate class.

Other changes:
- The `NotifyS2C_CreateHero` function now accepts an optional parameter that allows the packet to be wrapped in a vision packet when sent, similar to the `NotifySpawn` function.
- The same setting for `NotifySpawn` is now set to false by default.
- All comparisons of `userId` with `0` are reduced to the form `<=`.
  • Loading branch information
Killfrra authored May 1, 2022
1 parent 6630850 commit 10e791a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 120 deletions.
5 changes: 3 additions & 2 deletions GameServerCore/Packets/Interfaces/IPacketNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ public interface IPacketNotifier
/// </summary>
/// <param name="clientInfo">Information about the client which had their hero created.</param>
/// <param name="userId">User to send the packet to. Set to -1 to broadcast.</param>
void NotifyS2C_CreateHero(ClientInfo clientInfo, int userId = -1);
/// <param name="doVision">Whether or not to package the packets into a vision packet.</param>
void NotifyS2C_CreateHero(ClientInfo clientInfo, int userId = -1, bool doVision = false);
void NotifyS2C_CreateMinionCamp(IMonsterCamp monsterCamp);
void NotifyS2C_CreateNeutral(IMonster monster, float time);
/// <summary>
Expand Down Expand Up @@ -808,7 +809,7 @@ public interface IPacketNotifier
/// <param name="userId">UserId to send the packet to.</param>
/// <param name="gameTime">Time elapsed since the start of the game</param>
/// <param name="doVision">Whether or not to package the packets into a vision packet.</param>
void NotifySpawn(IGameObject obj, TeamId team, int userId, float gameTime, bool doVision = true);
void NotifySpawn(IGameObject obj, TeamId team, int userId, float gameTime, bool doVision = false);
/// <summary>
/// Sends a packet to the specified player detailing that the spawning (of champions & buildings) that occurs at the start of the game has ended.
/// </summary>
Expand Down
12 changes: 1 addition & 11 deletions GameServerLib/Chatbox/Commands/SpawnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,8 @@ public void SpawnChampForTeam(TeamId team, int userId, string model)
c.UpdateMoveOrder(OrderType.Stop);
c.LevelUp();

Game.PacketNotifier.NotifyS2C_CreateHero(clientInfoTemp);
Game.PacketNotifier.NotifyAvatarInfo(clientInfoTemp);
Game.ObjectManager.AddObject(c);
Game.PacketNotifier.NotifyEnterLocalVisibilityClient(c, ignoreVision: true);
Game.PacketNotifier.NotifyOnReplication(c, partial: false);

c.Stats.SetSpellEnabled(13, true);
c.Stats.SetSummonerSpellEnabled(0, true);
c.Stats.SetSummonerSpellEnabled(1, true);

Game.PacketNotifier.NotifyEnterVisibilityClient(c, 0, true, true);


ChatCommandManager.SendDebugMsgFormatted(DebugMsgType.INFO, "Spawned Bot" + clientInfoTemp.Name + " as " + c.Model + " with NetID: " + c.NetId + ".");
}

Expand Down
50 changes: 50 additions & 0 deletions GameServerLib/GameObjects/AttackableUnits/AI/Champion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Champion : ObjAiBase, IChampion

public byte SkillPoints { get; set; }

public override bool SpawnShouldBeHidden => false;

public Champion(Game game,
string model,
uint playerId,
Expand Down Expand Up @@ -103,6 +105,54 @@ public override void OnAdded()
_game.ObjectManager.AddChampion(this);
base.OnAdded();
TalentInventory.Initialize(this);

var bluePill = _itemManager.GetItemType(_game.Map.MapScript.MapScriptMetadata.RecallSpellItemId);
Inventory.SetExtraItem(7, bluePill);

// Runes
byte runeItemSlot = 14;
foreach (var rune in RuneList.Runes)
{
var runeItem = _itemManager.GetItemType(rune.Value);
var newRune = Inventory.SetExtraItem(runeItemSlot, runeItem);
Stats.AddModifier(runeItem);
runeItemSlot++;
}
Stats.SetSummonerSpellEnabled(0, true);
Stats.SetSummonerSpellEnabled(1, true);
}

protected override void OnSpawn(int userId, TeamId team, bool doVision)
{
var peerInfo = _game.PlayerManager.GetClientInfoByChampion(this);
_game.PacketNotifier.NotifyS2C_CreateHero(peerInfo, userId, doVision);
_game.PacketNotifier.NotifyAvatarInfo(peerInfo, userId);

bool ownChamp = peerInfo.PlayerId == userId;
if (ownChamp)
{
// Buy blue pill
var itemInstance = Inventory.GetItem(7);
_game.PacketNotifier.NotifyBuyItem(userId, this, itemInstance);

// Set spell levels
foreach (var spell in Spells)
{
var castInfo = spell.Value.CastInfo;
if (castInfo.SpellLevel > 0)
{
// NotifyNPC_UpgradeSpellAns has no effect here
_game.PacketNotifier.NotifyS2C_SetSpellLevel(userId, NetId, castInfo.SpellSlot, castInfo.SpellLevel);

float currentCD = spell.Value.CurrentCooldown;
float totalCD = spell.Value.GetCooldown();
if (currentCD > 0)
{
_game.PacketNotifier.NotifyCHAR_SetCooldown(this, castInfo.SpellSlot, currentCD, totalCD, userId);
}
}
}
}
}

public override void OnRemoved()
Expand Down
2 changes: 1 addition & 1 deletion GameServerLib/GameObjects/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public virtual void Sync(int userId, TeamId team, bool visible, bool forceSpawn
OnSync(userId, team);
}
}
else if (visible || !SpawnShouldBeHidden)
else if (forceSpawn || visible || !SpawnShouldBeHidden)
{
OnSpawn(userId, team, visible);
SetVisibleForPlayer(userId, visible);
Expand Down
76 changes: 0 additions & 76 deletions GameServerLib/Packets/PacketHandlers/HandleSpawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,86 +28,10 @@ public HandleSpawn(Game game)
private bool _firstSpawn = true;
public override bool HandlePacket(int userId, SpawnRequest req)
{
var players = _playerManager.GetPlayers(true);

if (_firstSpawn)
{
_firstSpawn = false;

var bluePill = _itemManager.GetItemType(_game.Map.MapScript.MapScriptMetadata.RecallSpellItemId);

foreach (var kv in players)
{
var peerInfo = kv.Item2;

var itemInstance = peerInfo.Champion.Inventory.SetExtraItem(7, bluePill);

// Runes
byte runeItemSlot = 14;
foreach (var rune in peerInfo.Champion.RuneList.Runes)
{
var runeItem = _itemManager.GetItemType(rune.Value);
var newRune = peerInfo.Champion.Inventory.SetExtraItem(runeItemSlot, runeItem);
peerInfo.Champion.Stats.AddModifier(runeItem);
runeItemSlot++;
}

peerInfo.Champion.Stats.SetSummonerSpellEnabled(0, true);
peerInfo.Champion.Stats.SetSummonerSpellEnabled(1, true);

_game.ObjectManager.AddObject(peerInfo.Champion);
}
}

_logger.Debug("Spawning map");
_game.PacketNotifier.NotifyS2C_StartSpawn(userId);

var userInfo = _playerManager.GetPeerInfo(userId);

foreach (var kv in players)
{
var peerInfo = kv.Item2;
var champ = peerInfo.Champion;

_game.PacketNotifier.NotifyS2C_CreateHero(peerInfo, userId);
_game.PacketNotifier.NotifyAvatarInfo(peerInfo, userId);

// Buy blue pill
var itemInstance = peerInfo.Champion.Inventory.GetItem(7);
_game.PacketNotifier.NotifyBuyItem(userId, peerInfo.Champion, itemInstance);

champ.SetSpawnedForPlayer(userId);

if (_game.IsRunning)
{
bool ownChamp = peerInfo.PlayerId == userId;
if (ownChamp || champ.IsVisibleForPlayer(userId))
{
_game.PacketNotifier.NotifyVisibilityChange(champ, userInfo.Team, true, userId);
}
if (ownChamp)
{
// Set spell levels
foreach (var spell in champ.Spells)
{
var castInfo = spell.Value.CastInfo;
if (castInfo.SpellLevel > 0)
{
// NotifyNPC_UpgradeSpellAns has no effect here
_game.PacketNotifier.NotifyS2C_SetSpellLevel(userId, champ.NetId, castInfo.SpellSlot, castInfo.SpellLevel);

float currentCD = spell.Value.CurrentCooldown;
float totalCD = spell.Value.GetCooldown();
if (currentCD > 0)
{
_game.PacketNotifier.NotifyCHAR_SetCooldown(champ, castInfo.SpellSlot, currentCD, totalCD, userId);
}
}
}
}
}
}

var om = _game.ObjectManager as ObjectManager;
if (_game.IsRunning)
{
Expand Down
2 changes: 2 additions & 0 deletions GameServerLib/Players/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void AddPlayer(KeyValuePair<string, IPlayerConfig> p)
c.StopMovement();
c.UpdateMoveOrder(OrderType.Stop);

_game.ObjectManager.AddObject(c);

player.Champion = c;
var pair = new Tuple<uint, ClientInfo> ((uint)player.PlayerId, player);
_players.Add(pair);
Expand Down
Loading

0 comments on commit 10e791a

Please sign in to comment.