<commit_before>﻿using MoonSharp.Interpreter;
using PROBot.Utils;
using PROProtocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace PROBot.Scripting
{
    public class LuaScript : BaseScript
    {
        public BotClient Bot { get; private set; }

#if DEBUG
        public int TimeoutDelay = 60000;
#else
        public int TimeoutDelay = 3000;
#endif

        private Script _lua;
        private string _path;
        private string _content;

        private bool _actionExecuted;

        public LuaScript(BotClient bot, string path, string content)
        {
            Bot = bot;
            _path = Path.GetDirectoryName(path);
            _content = content;
        }

        public override void Initialize()
        {
            CreateLuaInstance();

            Name = _lua.Globals.Get("name").CastToString();
            Author = _lua.Globals.Get("author").CastToString();
            Description = _lua.Globals.Get("description").CastToString();
        }

        public override void Start()
        {
            CallFunctionSafe("onStart");
        }

        public override void Stop()
        {
            CallFunctionSafe("onStop");
        }

        public override void Pause()
        {
            CallFunctionSafe("onPause");
        }

        public override void Resume()
        {
            CallFunctionSafe("onResume");
        }

        public override void OnDialogMessage(string message)
        {
            CallFunctionSafe("onDialogMessage", message);
        }

        public override void OnBattleMessage(string message)
        {
            CallFunctionSafe("onBattleMessage", message);
        }

        public override void OnSystemMessage(string message)
        {
            CallFunctionSafe("onSystemMessage", message);
        }

        public override void OnLearningMove(string moveName, int pokemonIndex)
        {
            CallFunctionSafe("onLearningMove", moveName, pokemonIndex);
        }

        public override bool ExecuteNextAction()
        {
            if (Bot.Game.IsInBattle && Bot.AI.UseMandatoryAction())
            {
                return true;
            }

            string functionName = Bot.Game.IsInBattle ? "onBattleAction" : "onPathAction";

            _actionExecuted = false;
            try
            {
                CallFunction(functionName);
            }
            catch (ScriptRuntimeException ex)
            {
                throw new Exception(ex.DecoratedMessage, ex);
            }
            return _actionExecuted;
        }

        private void CreateLuaInstance()
        {
            _lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.LoadMethods);
            _lua.Options.ScriptLoader = new CustomScriptLoader(_path) { ModulePaths = new [] { "?.lua" } };
            _lua.Options.CheckThreadAccess = false;
            _lua.Globals["log"] = new Action<string>(Log);
            _lua.Globals["fatal"] = new Action<string>(Fatal);
            _lua.Globals["stringContains"] = new Func<string, string, bool>(StringContains);

            // General conditions
            _lua.Globals["getPlayerX"] = new Func<int>(GetPlayerX);
            _lua.Globals["getPlayerY"] = new Func<int>(GetPlayerY);
            _lua.Globals["getMapName"] = new Func<string>(GetMapName);
            _lua.Globals["getTeamSize"] = new Func<int>(GetTeamSize);

            _lua.Globals["getPokemonId"] = new Func<int, int>(GetPokemonId);
            _lua.Globals["getPokemonName"] = new Func<int, string>(GetPokemonName);
            _lua.Globals["getPokemonHealth"] = new Func<int, int>(GetPokemonHealth);
            _lua.Globals["getPokemonHealthPercent"] = new Func<int, int>(GetPokemonHealthPercent);
            _lua.Globals["getPokemonMaxHealth"] = new Func<int, int>(GetPokemonMaxHealth);
            _lua.Globals["getPokemonLevel"] = new Func<int, int>(GetPokemonLevel);
            _lua.Globals["getPokemonTotalExperience"] = new Func<int, int>(GetPokemonTotalExperience);
            _lua.Globals["getPokemonRemainingExperience"] = new Func<int, int>(GetPokemonRemainingExperience);
            _lua.Globals["getPokemonStatus"] = new Func<int, string>(GetPokemonStatus);
            _lua.Globals["getPokemonHeldItem"] = new Func<int, string>(GetPokemonHeldItem);
            _lua.Globals["getPokemonUniqueId"] = new Func<int, int>(GetPokemonUniqueId);
            _lua.Globals["getRemainingPowerPoints"] = new Func<int, string, int>(GetRemainingPowerPoints);
            _lua.Globals["getPokemonMaxPowerPoints"] = new Func<int, int, int>(GetPokemonMaxPowerPoints);
            _lua.Globals["isPokemonShiny"] = new Func<int, bool>(IsPokemonShiny);
            _lua.Globals["getPokemonMove"] = new Func<int, int, string>(GetPokemonMove);
            _lua.Globals["getPokemonNature"] = new Func<int, string>(GetPokemonNature);
            _lua.Globals["getPokemonAbility"] = new Func<int, string>(GetPokemonAbility);
            _lua.Globals["getPokemonEffortValue"] = new Func<int, string, int>(GetPokemonEffortValue);
            _lua.Globals["getPokemonIndividualValue"] = new Func<int, string, int>(GetPokemonIndividualValue);
            _lua.Globals["getPokemonHappiness"] = new Func<int, int>(GetPokemonHappiness);
            _lua.Globals["getPokemonRegion"] = new Func<int, string>(GetPokemonRegion);
            _lua.Globals["getPokemonOriginalTrainer"] = new Func<int, string>(GetPokemonOriginalTrainer);
            _lua.Globals["getPokemonGender"] = new Func<int, string>(GetPokemonGender);
            _lua.Globals["isPokemonUsable"] = new Func<int, bool>(IsPokemonUsable);
            _lua.Globals["getUsablePokemonCount"] = new Func<int>(GetUsablePokemonCount);
            _lua.Globals["hasMove"] = new Func<int, string, bool>(HasMove);

            _lua.Globals["hasItem"] = new Func<string, bool>(HasItem);
            _lua.Globals["getItemQuantity"] = new Func<string, int>(GetItemQuantity);
            _lua.Globals["hasPokemonInTeam"] = new Func<string, bool>(HasPokemonInTeam);
            _lua.Globals["isTeamSortedByLevelAscending"] = new Func<bool>(IsTeamSortedByLevelAscending);
            _lua.Globals["isTeamSortedByLevelDescending"] = new Func<bool>(IsTeamSortedByLevelDescending);
            _lua.Globals["isTeamRangeSortedByLevelAscending"] = new Func<int, int, bool>(IsTeamRangeSortedByLevelAscending);
            _lua.Globals["isTeamRangeSortedByLevelDescending"] = new Func<int, int, bool>(IsTeamRangeSortedByLevelDescending);
            _lua.Globals["isNpcVisible"] = new Func<string, bool>(IsNpcVisible);
            _lua.Globals["isNpcOnCell"] = new Func<int, int, bool>(IsNpcOnCell);
            _lua.Globals["isShopOpen"] = new Func<bool>(IsShopOpen);
            _lua.Globals["getMoney"] = new Func<int>(GetMoney);
            _lua.Globals["isMounted"] = new Func<bool>(IsMounted);
            _lua.Globals["isPrivateMessageEnabled"] = new Func<bool>(IsPrivateMessageEnabled);
            _lua.Globals["getTime"] = new GetTimeDelegate(GetTime);
            _lua.Globals["isMorning"] = new Func<bool>(IsMorning);
            _lua.Globals["isNoon"] = new Func<bool>(IsNoon);
            _lua.Globals["isNight"] = new Func<bool>(IsNight);

            _lua.Globals["isCurrentPCBoxRefreshed"] = new Func<bool>(IsCurrentPCBoxRefreshed);
            _lua.Globals["getCurrentPCBoxId"] = new Func<int>(GetCurrentPCBoxId);<commit_msg>API: Add isOutside<commit_after>﻿using MoonSharp.Interpreter;
using PROBot.Utils;
using PROProtocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace PROBot.Scripting
{
    public class LuaScript : BaseScript
    {
        public BotClient Bot { get; private set; }

#if DEBUG
        public int TimeoutDelay = 60000;
#else
        public int TimeoutDelay = 3000;
#endif

        private Script _lua;
        private string _path;
        private string _content;

        private bool _actionExecuted;

        public LuaScript(BotClient bot, string path, string content)
        {
            Bot = bot;
            _path = Path.GetDirectoryName(path);
            _content = content;
        }

        public override void Initialize()
        {
            CreateLuaInstance();

            Name = _lua.Globals.Get("name").CastToString();
            Author = _lua.Globals.Get("author").CastToString();
            Description = _lua.Globals.Get("description").CastToString();
        }

        public override void Start()
        {
            CallFunctionSafe("onStart");
        }

        public override void Stop()
        {
            CallFunctionSafe("onStop");
        }

        public override void Pause()
        {
            CallFunctionSafe("onPause");
        }

        public override void Resume()
        {
            CallFunctionSafe("onResume");
        }

        public override void OnDialogMessage(string message)
        {
            CallFunctionSafe("onDialogMessage", message);
        }

        public override void OnBattleMessage(string message)
        {
            CallFunctionSafe("onBattleMessage", message);
        }

        public override void OnSystemMessage(string message)
        {
            CallFunctionSafe("onSystemMessage", message);
        }

        public override void OnLearningMove(string moveName, int pokemonIndex)
        {
            CallFunctionSafe("onLearningMove", moveName, pokemonIndex);
        }

        public override bool ExecuteNextAction()
        {
            if (Bot.Game.IsInBattle && Bot.AI.UseMandatoryAction())
            {
                return true;
            }

            string functionName = Bot.Game.IsInBattle ? "onBattleAction" : "onPathAction";

            _actionExecuted = false;
            try
            {
                CallFunction(functionName);
            }
            catch (ScriptRuntimeException ex)
            {
                throw new Exception(ex.DecoratedMessage, ex);
            }
            return _actionExecuted;
        }

        private void CreateLuaInstance()
        {
            _lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.LoadMethods);
            _lua.Options.ScriptLoader = new CustomScriptLoader(_path) { ModulePaths = new [] { "?.lua" } };
            _lua.Options.CheckThreadAccess = false;
            _lua.Globals["log"] = new Action<string>(Log);
            _lua.Globals["fatal"] = new Action<string>(Fatal);
            _lua.Globals["stringContains"] = new Func<string, string, bool>(StringContains);

            // General conditions
            _lua.Globals["getPlayerX"] = new Func<int>(GetPlayerX);
            _lua.Globals["getPlayerY"] = new Func<int>(GetPlayerY);
            _lua.Globals["getMapName"] = new Func<string>(GetMapName);
            _lua.Globals["getTeamSize"] = new Func<int>(GetTeamSize);

            _lua.Globals["getPokemonId"] = new Func<int, int>(GetPokemonId);
            _lua.Globals["getPokemonName"] = new Func<int, string>(GetPokemonName);
            _lua.Globals["getPokemonHealth"] = new Func<int, int>(GetPokemonHealth);
            _lua.Globals["getPokemonHealthPercent"] = new Func<int, int>(GetPokemonHealthPercent);
            _lua.Globals["getPokemonMaxHealth"] = new Func<int, int>(GetPokemonMaxHealth);
            _lua.Globals["getPokemonLevel"] = new Func<int, int>(GetPokemonLevel);
            _lua.Globals["getPokemonTotalExperience"] = new Func<int, int>(GetPokemonTotalExperience);
            _lua.Globals["getPokemonRemainingExperience"] = new Func<int, int>(GetPokemonRemainingExperience);
            _lua.Globals["getPokemonStatus"] = new Func<int, string>(GetPokemonStatus);
            _lua.Globals["getPokemonHeldItem"] = new Func<int, string>(GetPokemonHeldItem);
            _lua.Globals["getPokemonUniqueId"] = new Func<int, int>(GetPokemonUniqueId);
            _lua.Globals["getRemainingPowerPoints"] = new Func<int, string, int>(GetRemainingPowerPoints);
            _lua.Globals["getPokemonMaxPowerPoints"] = new Func<int, int, int>(GetPokemonMaxPowerPoints);
            _lua.Globals["isPokemonShiny"] = new Func<int, bool>(IsPokemonShiny);
            _lua.Globals["getPokemonMove"] = new Func<int, int, string>(GetPokemonMove);
            _lua.Globals["getPokemonNature"] = new Func<int, string>(GetPokemonNature);
            _lua.Globals["getPokemonAbility"] = new Func<int, string>(GetPokemonAbility);
            _lua.Globals["getPokemonEffortValue"] = new Func<int, string, int>(GetPokemonEffortValue);
            _lua.Globals["getPokemonIndividualValue"] = new Func<int, string, int>(GetPokemonIndividualValue);
            _lua.Globals["getPokemonHappiness"] = new Func<int, int>(GetPokemonHappiness);
            _lua.Globals["getPokemonRegion"] = new Func<int, string>(GetPokemonRegion);
            _lua.Globals["getPokemonOriginalTrainer"] = new Func<int, string>(GetPokemonOriginalTrainer);
            _lua.Globals["getPokemonGender"] = new Func<int, string>(GetPokemonGender);
            _lua.Globals["isPokemonUsable"] = new Func<int, bool>(IsPokemonUsable);
            _lua.Globals["getUsablePokemonCount"] = new Func<int>(GetUsablePokemonCount);
            _lua.Globals["hasMove"] = new Func<int, string, bool>(HasMove);

            _lua.Globals["hasItem"] = new Func<string, bool>(HasItem);
            _lua.Globals["getItemQuantity"] = new Func<string, int>(GetItemQuantity);
            _lua.Globals["hasPokemonInTeam"] = new Func<string, bool>(HasPokemonInTeam);
            _lua.Globals["isTeamSortedByLevelAscending"] = new Func<bool>(IsTeamSortedByLevelAscending);
            _lua.Globals["isTeamSortedByLevelDescending"] = new Func<bool>(IsTeamSortedByLevelDescending);
            _lua.Globals["isTeamRangeSortedByLevelAscending"] = new Func<int, int, bool>(IsTeamRangeSortedByLevelAscending);
            _lua.Globals["isTeamRangeSortedByLevelDescending"] = new Func<int, int, bool>(IsTeamRangeSortedByLevelDescending);
            _lua.Globals["isNpcVisible"] = new Func<string, bool>(IsNpcVisible);
            _lua.Globals["isNpcOnCell"] = new Func<int, int, bool>(IsNpcOnCell);
            _lua.Globals["isShopOpen"] = new Func<bool>(IsShopOpen);
            _lua.Globals["getMoney"] = new Func<int>(GetMoney);
            _lua.Globals["isMounted"] = new Func<bool>(IsMounted);
            _lua.Globals["isPrivateMessageEnabled"] = new Func<bool>(IsPrivateMessageEnabled);
            _lua.Globals["getTime"] = new GetTimeDelegate(GetTime);
            _lua.Globals["isMorning"] = new Func<bool>(IsMorning);
            _lua.Globals["isNoon"] = new Func<bool>(IsNoon);
            _lua.Globals["isNight"] = new Func<bool>(IsNight);
            _lua.Globals["isOutside"] = new Func<bool>(IsOutside);

            _lua.Globals["isCurrentPCBoxRefreshed"] = new Func<bool>(IsCurrentPCBoxRefreshed);
            _lua.Globals["getCurrentPCBoxId"] = new Func<int>(GetCurrentPCBoxId);
{'fraction_non_alphanumeric': 0.1157035848047084, 'fraction_numerical': 0.001203852327447833, 'mean_word_length': 2.77029752899647, 'pattern_counts': {'":': 0, '<': 106, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '334488', 'n_tokens_mistral': 4644, 'n_tokens_neox': 4546, 'n_words': 740}