// // NostaleSkillsPacketApi.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using NosSmooth.Core.Client; using NosSmooth.Game.Data.Characters; using NosSmooth.Game.Data.Entities; using NosSmooth.Game.Errors; using NosSmooth.Packets.Client.Battle; using NosSmooth.Packets.Enums.Entities; using Remora.Results; namespace NosSmooth.Game.Apis; /// /// Packet api for using character skills. /// public class NostaleSkillsPacketApi { private readonly INostaleClient _client; private readonly Game _game; /// /// Initializes a new instance of the class. /// /// The nostale client. /// The game. public NostaleSkillsPacketApi(INostaleClient client, Game game) { _client = client; _game = game; } /// /// Use the given (targetable) skill on specified entity. /// /// /// For skills that can be used only on self, use of the character. /// For skills that cannot be targeted on an entity, proceed to UseSkillAt. /// /// The cast id of the skill. /// The id of the entity to use the skill on. /// The type of the supplied entity. /// The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillOn ( short castId, long entityId, EntityType entityType, short? mapX = default, short? mapY = default, CancellationToken ct = default ) { return _client.SendPacketAsync ( new UseSkillPacket ( castId, entityType, entityId, mapX, mapY ), ct ); } /// /// Use the given (targetable) skill on character itself. /// /// /// For skills that cannot be targeted on an entity, proceed to UseSkillAt. /// /// The cast id of the skill. /// The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public async Task UseSkillOnCharacter ( short castId, short? mapX = default, short? mapY = default, CancellationToken ct = default ) { var character = _game.Character; if (character is null) { return new NotInitializedError("Character"); } return await _client.SendPacketAsync ( new UseSkillPacket ( castId, EntityType.Player, character.Id, mapX, mapY ), ct ); } /// /// Use the given (targetable) skill on specified entity. /// /// /// For skills that can be used only on self, use of the character. /// For skills that cannot be targeted on an entity, proceed to UseSkillAt. /// /// The cast id of the skill. /// The entity to use the skill on. /// The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillOn ( short castId, ILivingEntity entity, short? mapX = default, short? mapY = default, CancellationToken ct = default ) { return _client.SendPacketAsync ( new UseSkillPacket ( castId, entity.Type, entity.Id, mapX, mapY ), ct ); } /// /// Use the given (targetable) skill on specified entity. /// /// /// The skill won't be used if it is on cooldown. /// For skills that can be used only on self, use of the character. /// For skills that cannot be targeted on an entity, proceed to UseSkillAt. /// /// The skill to use. /// The entity to use the skill on. /// The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillOn ( Skill skill, ILivingEntity entity, short? mapX = default, short? mapY = default, CancellationToken ct = default ) { if (skill.IsOnCooldown) { return Task.FromResult(new SkillOnCooldownError(skill)); } if (skill.Info is null) { return Task.FromResult(new NotInitializedError("skill info")); } return _client.SendPacketAsync ( new UseSkillPacket ( skill.Info.CastId, entity.Type, entity.Id, mapX, mapY ), ct ); } /// /// Use the given (targetable) skill on specified entity. /// /// /// For skills that can be used only on self, use of the character. /// For skills that cannot be targeted on an entity, proceed to UseSkillAt. /// /// The skill to use. /// The id of the entity to use the skill on. /// The type of the supplied entity. /// The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.) /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillOn ( Skill skill, long entityId, EntityType entityType, short? mapX = default, short? mapY = default, CancellationToken ct = default ) { if (skill.IsOnCooldown) { return Task.FromResult(new SkillOnCooldownError(skill)); } if (skill.Info is null) { return Task.FromResult(new NotInitializedError("skill info")); } return _client.SendPacketAsync ( new UseSkillPacket ( skill.Info.CastId, entityType, entityId, mapX, mapY ), ct ); } /// /// Use the given (aoe) skill on the specified place. /// /// /// For skills that can have targets, proceed to UseSkillOn. /// /// The id of the skill. /// The x coordinate to use the skill at. /// The y coordinate to use the skill at. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillAt ( long skillVNum, short mapX, short mapY, CancellationToken ct = default ) { return _client.SendPacketAsync ( new UseAOESkillPacket(skillVNum, mapX, mapY), ct ); } /// /// Use the given (aoe) skill on the specified place. /// /// /// For skills that can have targets, proceed to UseSkillOn. /// /// The skill to use. /// The x coordinate to use the skill at. /// The y coordinate to use the skill at. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task UseSkillAt ( Skill skill, short mapX, short mapY, CancellationToken ct = default ) { if (skill.IsOnCooldown) { return Task.FromResult(new SkillOnCooldownError(skill)); } return _client.SendPacketAsync ( new UseAOESkillPacket(skill.SkillVNum, mapX, mapY), ct ); } }