//
// NostaleMapApi.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.Game.Apis.Unsafe;
using NosSmooth.Game.Data.Entities;
using NosSmooth.Game.Errors;
using Remora.Results;
namespace NosSmooth.Game.Apis.Safe;
///
/// Packet api for managing maps in inventory.
///
public class NostaleMapApi
{
private readonly Game _game;
private readonly UnsafeMapApi _unsafeMapApi;
///
/// The range the player may pick up items in.
///
public static short PickUpRange => 5;
///
/// Initializes a new instance of the class.
///
/// The game.
/// The unsafe map api.
public NostaleMapApi(Game game, UnsafeMapApi unsafeMapApi)
{
_game = game;
_unsafeMapApi = unsafeMapApi;
}
///
/// Pick up the given item.
///
///
/// Checks that the item is in distance,
/// if the character's position or
/// item's position is not initialized, returns
/// an error.
///
/// The item.
/// The cancellation token used for cancelling the operation.
/// A result that may or may not have succeeded.
public async Task CharacterPickUpAsync(GroundItem item, CancellationToken ct = default)
{
var character = _game.Character;
if (character is null)
{
return new NotInitializedError("Game.Character");
}
var characterPosition = character.Position;
if (characterPosition is null)
{
return new NotInitializedError("Game.Character.Position");
}
var itemPosition = item.Position;
if (itemPosition is null)
{
return new NotInitializedError("item.Position");
}
if (!itemPosition.Value.IsInRange(characterPosition.Value, PickUpRange))
{
return new NotInRangeError("Character", characterPosition.Value, itemPosition.Value, PickUpRange);
}
return await _unsafeMapApi.CharacterPickUpAsync(item.Id, ct);
}
///
/// Pick up the given item.
///
///
/// Checks that the character has a pet company,
/// that the item is in distance.
/// When the pet's position or
/// item's position is not initialized, returns
/// an error.
///
/// The item.
/// The cancellation token used for cancelling the operation.
/// A result that may or may not have succeeded.
public async Task PetPickUpAsync(GroundItem item, CancellationToken ct = default)
{
var mates = _game.Mates;
if (mates is null)
{
return new NotInitializedError("Game.Mates");
}
var pet = mates.CurrentPet;
if (pet is null)
{
return new NotInitializedError("Game.Mates.CurrentPet");
}
var entity = _game.CurrentMap?.Entities.GetEntity(pet.Pet.MateId);
if (entity is null)
{
return new NotInitializedError("Game.CurrentMap.Entities.PetEntity");
}
var petPosition = entity.Position;
if (petPosition is null)
{
return new NotInitializedError("Game.CurrentMap.Entities.PetEntity.Position");
}
var itemPosition = item.Position;
if (itemPosition is null)
{
return new NotInitializedError("item.Position");
}
if (!itemPosition.Value.IsInRange(petPosition.Value, PickUpRange))
{
return new NotInRangeError("Pet", petPosition.Value, itemPosition.Value, PickUpRange);
}
return await _unsafeMapApi.PetPickUpAsync(item.Id, ct);
}
}