using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

using InfServer.Logic;
using InfServer.Game;
using InfServer.Scripting;
using InfServer.Bots;
using InfServer.Protocol;

using Assets;

namespace InfServer.Script.GameType_MA
{   // Script Class
    /// Provides the interface between the script and arena
    ///////////////////////////////////////////////////////
    public partial class GameType_MA : Scripts.IScript
    {   ///////////////////////////////////////////////////
        // Member Variables
        ///////////////////////////////////////////////////


        public bool tryFireMissile(Player player, int vehID, int explosionID, short posX, short posY)
        {

            Player target = _arena.getPlayersInRange(posX, posY, 45).FirstOrDefault();

            if (target == null)
                return false;

            if (player.getInventoryAmount(2000) == 0)
            {
                player.sendMessage(-1, "Out of missiles");
                return false;
            }

            int active = _arena.Vehicles.Count(veh => veh._bBotVehicle && veh._type.Id == vehID && veh._creator == player);

            if (active >= 1)
            {
                player.sendMessage(-1, "You already have a missile active!");
                return false;
            }
            player.inventoryModify(2000, -1);

            CAMLGM missile = _arena.newBot(typeof(CAMLGM), (ushort)vehID, player._team, player, player._state, null) as CAMLGM;

            missile.init(target, explosionID);
            Helpers.Player_RouteExplosion(_arena.Players, 1032, player._state.positionX, player._state.positionY, 0, 0, player._id);

            return true;
        }

    }
}