Botcraft icon indicating copy to clipboard operation
Botcraft copied to clipboard

Lua Scripting API

Open Zwolf8 opened this issue 5 years ago • 8 comments

Lua is a popular embedded scripting language.

It would be very awesome if lua scripting was provided in botcraft. It would make the bot creation process a lot easier, no need to compile or anything of that sort.

I recreated the hello world bot example in lua:

local client = InterfaceClient.new(true, false)
client:SetAutoRespawn(true)
client:Connect(ip, port, login, password)
wait(5)
client:Say("Hi there!")
wait(5)
client:Disconnect()

I put together a quick example of how this could work using the sol2 library:

sol::state lua;
lua.open_libraries(
    sol::lib::base, 
    sol::lib::bit32, 
    sol::lib::coroutine, 
    sol::lib::io, 
    sol::lib::math, 
    sol::lib::os, 
    sol::lib::package, 
    sol::lib::string, 
    sol::lib::table, 
    sol::lib::utf8);


sol::usertype<Botcraft::InterfaceClient> InterfaceClient_bind = lua.new_usertype<Botcraft::InterfaceClient>("InterfaceClient",
    sol::constructors<Botcraft::InterfaceClient(const bool, const bool)>());

lua["wait"] = [](long seconds) -> bool 
{
    std::this_thread::sleep_for(std::chrono::seconds(seconds));
    return true;
};

InterfaceClient_bind["Connect"] = &Botcraft::InterfaceClient::Connect;
InterfaceClient_bind["Disconnect"] = &Botcraft::InterfaceClient::Disconnect;
InterfaceClient_bind["Say"] = &Botcraft::InterfaceClient::Say;
InterfaceClient_bind["Respawn"] = &Botcraft::InterfaceClient::Respawn;
InterfaceClient_bind["SetAutoRespawn"] = &Botcraft::InterfaceClient::SetAutoRespawn;

I enjoy the thought of people being able to share their minecraft bots with a short lua script.

Zwolf8 avatar Sep 19 '20 20:09 Zwolf8

I'm affraid that's beyond the scope of what I want to do with this library. I don't know anything about Lua and I'd rather spend the few hours I have to develop new features for botcraft rather than another interface doing the exact same thing.

However I will gladly accept a PR if you want to do it yourself!

adepierre avatar Sep 19 '20 22:09 adepierre

Sure.

Zwolf8 avatar Sep 20 '20 00:09 Zwolf8

I don't think Lua is the best library to use, I'd think something more along the lines of JS would be better tbh (imo)

Technisha avatar Apr 22 '21 08:04 Technisha

@Technisha if you are looking for JS bots, you might want to check mineflayer.

adepierre avatar Apr 22 '21 13:04 adepierre

1+ For LUA scripting!

codingwatching avatar Oct 10 '21 10:10 codingwatching

@Technisha if you are looking for JS bots, you might want to check mineflayer.

The problem with mineflayer is it's poor code quality and performance, makes it hard to develop complex bots. A JS Interface for a multithreaded "base client" wouldn't be that useless tbh.

constantins2001 avatar Dec 13 '21 13:12 constantins2001

The last time I used mineflayer it had better performance than botcraft as far as memory footprint and chunk parse time go. It had slower world pathfinding and looping over blocks was slower also.

Darker avatar Feb 08 '22 18:02 Darker

The last time I used mineflayer it had better performance than botcraft as far as memory footprint and chunk parse time go. It had slower world pathfinding and looping over blocks was slower also.

That doesn't really surprise me. Mineflayer is really good and developed by a lot of talented people. I only started to optimize stuff in botcraft last month, and don't really have a performance goal.

adepierre avatar Feb 09 '22 11:02 adepierre

This feature would probably be more suitable for a separate project, not something in Botcraft itself. C bindings would be a more practical solution, as it could allow any language bindings to be more easily made.

Zwolf8 avatar Sep 03 '22 22:09 Zwolf8