Skip to content

Commit

Permalink
Merge pull request #87 from bedrock-dev/lua-script
Browse files Browse the repository at this point in the history
Lua script support for sim player
  • Loading branch information
hhhxiao authored Mar 16, 2023
2 parents 172c807 + a9cc284 commit 9820411
Show file tree
Hide file tree
Showing 29 changed files with 32,757 additions and 240 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.16)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# variables
set(TRAPDOOR_VERSION 0.30.0)
set(TRAPDOOR_VERSION 0.35.0)
set(GAME_VERSION 1.19.61.01)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)
Expand Down Expand Up @@ -45,6 +45,10 @@ file(GLOB_RECURSE SRC_FILES_DIR
include_directories(SDK-cpp/include)
include_directories(SDK-cpp/include/llapi)
include_directories(${PROJECT_SOURCE_DIR}/src/include)
include_directories(${PROJECT_SOURCE_DIR}/src/include/3rd)
include_directories(${PROJECT_SOURCE_DIR}/src/include/3rd/sol)
include_directories(${PROJECT_SOURCE_DIR}/src/include/3rd/lua)

link_directories(.)

add_definitions(-D"NDEBUG" -D"TEMPLATE_EXPORTS" -D"WIN32_LEAN_AND_MEAN" -D"_CRT_SECURE_NO_WARNINGS" -D"_WINDOWS"
Expand All @@ -64,7 +68,8 @@ add_link_options(

add_library(${TARGET_NAME} SHARED ${SRC_FILES_DIR})

target_link_libraries(${TARGET_NAME} Pdh)
#后面LUA可以调整成dll
target_link_libraries(${TARGET_NAME} Pdh ${PROJECT_SOURCE_DIR}/src/include/3rd/lua54.lib)

add_custom_command(TARGET ${TARGET_NAME} PRE_BUILD
COMMAND cmd /c ${PROJECT_SOURCE_DIR}/prepareLib.cmd ${PROJECT_SOURCE_DIR}
Expand Down
16 changes: 16 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
### trapdoor-ll-0.35.0-1.19.63.01

#### 更新日志

- 修复假人部分刷物品的bug
- 调整假人的相关命令(`useonpos`,`useon`,`destroypos`,`destroy`)
- 为假人增加脚本执行功能,具体见[假人脚本API & 教程](https://bedrock-dev.github.io/tr-wiki/scripts.html)

---

- Fixed the bug of simulated player can duplicate items
- Adjust some `player` subcommands (`useonpos`,`useon`,`destroypos`,`destroy`)
- Add script execution features for simulated player,
see [Dummy Script API & Tutorial](https://bedrock-dev.github.io/tr-wiki/scripts.html) for details
(Due to limited time, the English document is suspended)

### trapdoor-ll-0.30.0-1.19.61.01

**由于`installer.exe`会自动判别LL版本,因此此后不再标出LL版本信息**
Expand Down
11 changes: 11 additions & 0 deletions docs/simplayerscrpit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```c

void onstart(){

}

void loop(){

}

```
2 changes: 1 addition & 1 deletion metadata
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
0.30.0
0.35.0
1.19.63.01
2.11.0
2 changes: 1 addition & 1 deletion src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void prepare() {
tryCreateFolder("./plugins/trapdoor/sim");
tryCreateFolder("./plugins/trapdoor/player");
tryCreateFolder("./plugins/trapdoor/logs");

tryCreateFolder("./plugins/trapdoor/scripts");
}

void PluginInit() {
Expand Down
13 changes: 12 additions & 1 deletion src/base/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,16 @@ namespace trapdoor {
return builder.get();
}

std::vector<std::string> Configuration::readBotScripts() {
std::vector<std::string> scripts;
namespace fs = std::filesystem;
for (auto &f: fs::directory_iterator("./plugins/trapdoor/scripts")) {
if (f.is_regular_file() && f.path().extension() == ".lua") {
trapdoor::logger().debug("Find script file: {} Found", f.path().filename().string());
scripts.push_back(f.path().filename().string());

} // namespace trapdoor
}
}
return scripts;
} // namespace trapdoor
}
5 changes: 5 additions & 0 deletions src/base/TrapdoorMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "SysInfoHelper.h"
#include "config.h"
#include "TrapdoorAPI.h"
#include <mc/SimulatedPlayer.hpp>


#define REG_COMMAND(c) \
auto cfg_##c = cmdCfg.getCommandConfig(#c); \
Expand Down Expand Up @@ -61,6 +63,7 @@ namespace trapdoor {
villageHelper.heavyTick();
hsaManager.HeavyTick();
HUDHelper.tick();
simPlayerManager.tick();
slimeChunkHelper.HeavyTick();
}

Expand Down Expand Up @@ -88,6 +91,8 @@ namespace trapdoor {
}

bool TrapdoorMod::initConfig(bool reload) {


auto modRootPath = std::string("./plugins/trapdoor/");
#ifdef DEV
modRootPath = "C:/Users/xhy/dev/trapdoor-ll/src/base/";
Expand Down
10 changes: 0 additions & 10 deletions src/base/hhhxiao.json

This file was deleted.

3 changes: 3 additions & 0 deletions src/base/sample_scripts/hello_world.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Tick()
Bot.say("Hello world")
end
1 change: 1 addition & 0 deletions src/commands/CommandHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace trapdoor {
ActionResult::ActionResult(std::string m, bool su) : msg(std::move(m)), success(su) {}

void ActionResult::sendTo(CommandOutput &output) const {
success ? output.success(msg) : output.error(msg);
}
Expand Down
34 changes: 29 additions & 5 deletions src/commands/PlayerCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace trapdoor {


void setup_playerCommand(int level) {
using ParamType = DynamicCommand::ParameterType;
// create a dynamic command
Expand All @@ -29,12 +30,14 @@ namespace trapdoor {

auto backpackOpt = command->setEnum("backpackOpt", {"backpack"});
auto stopOpt = command->setEnum("stopOpt", {"stop", "cancel"});
auto setOpt = command->setEnum("setOpt", {"set"});
auto selectOpt = command->setEnum("selectOpt", {"select"});
auto dropOpt = command->setEnum("dropOpt", {"drop", "droptype"});
auto cmdOpt = command->setEnum("cmdOpt", {"runcmd"});
auto followOpt = command->setEnum("followOpt", {"follow"});
auto tpOpt = command->setEnum("tpOpt", {"tp"});

auto scriptOpt = command->setEnum("scriptOpt", {"script"});

command->mandatory("player", ParamType::Enum, spawnOpt,
CommandParameterOption::EnumAutocompleteExpansion);
command->mandatory("player", ParamType::Enum, behOpt,
Expand Down Expand Up @@ -64,7 +67,7 @@ namespace trapdoor {
command->mandatory("player", ParamType::Enum, stopOpt,
CommandParameterOption::EnumAutocompleteExpansion);

command->mandatory("player", ParamType::Enum, setOpt,
command->mandatory("player", ParamType::Enum, selectOpt,
CommandParameterOption::EnumAutocompleteExpansion);

// 丢一组 丢一种
Expand All @@ -79,8 +82,15 @@ namespace trapdoor {
command->mandatory("player", ParamType::Enum, tpOpt,
CommandParameterOption::EnumAutocompleteExpansion);

command->mandatory("player", ParamType::Enum, scriptOpt,
CommandParameterOption::EnumAutocompleteExpansion);


command->mandatory("name", ParamType::SoftEnum, command->setSoftEnum("name", {}));

command->mandatory("file", ParamType::SoftEnum,
command->setSoftEnum("file", trapdoor::Configuration::readBotScripts()));

command->mandatory("command", ParamType::String);
command->mandatory("itemId", ParamType::Item);
command->mandatory("vec3", ParamType::Vec3);
Expand All @@ -92,14 +102,16 @@ namespace trapdoor {

command->mandatory("slot", ParamType::Int);

command->optional("errorstop", ParamType::Bool);

// clang-format off
// cancel task and stop action
command->addOverload({"name", stopOpt});
// check inv
command->addOverload({"name", backpackOpt});
//set
command->addOverload({"name", setOpt, "itemId"});
command->addOverload({"name", setOpt, "slot"});
command->addOverload({"name", selectOpt, "itemId"});
command->addOverload({"name", selectOpt, "slot"});
//drop
command->addOverload({"name", dropOpt, "itemId"});

Expand Down Expand Up @@ -141,6 +153,9 @@ namespace trapdoor {

//传送
command->addOverload({"name", tpOpt, "vec3"});

command->addOverload({"name", scriptOpt, "file", "interval", "errorstop"});

// clang-format on

auto cb = [](DynamicCommand const &command, CommandOrigin const &origin,
Expand All @@ -165,6 +180,11 @@ namespace trapdoor {

auto vec3 = results["vec3"].isSet ? results["vec3"].get<Vec3>() : Vec3::MAX;

auto scriptFile = results["file"].isSet ? results["file"].get<std::string>() : "";

auto errorStop = !results["errorstop"].isSet || results["errorstop"].get<bool>();


switch (do_hash(results["player"].getRaw<std::string>().c_str())) {
case do_hash("spawn"):
trapdoor::mod()
Expand Down Expand Up @@ -193,7 +213,7 @@ namespace trapdoor {
.sendTo(output);
break;

case do_hash("set"):
case do_hash("select"):
trapdoor::mod()
.getSimPlayerManager()
.setItem(name, itemId, slot)
Expand Down Expand Up @@ -315,6 +335,10 @@ namespace trapdoor {
.swapBackpack(name, origin.getPlayer())
.sendTo(output);
break;
case do_hash("script"):
trapdoor::mod().getSimPlayerManager().runScript(name, scriptFile, interval, errorStop).sendTo(
output);

}
};

Expand Down
3 changes: 3 additions & 0 deletions src/commands/TrapdoorCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ namespace trapdoor {
}

ActionResult reloadConfig() {
//trapdoor::mod().getConfig().readBotScripts(); //刷新脚本
trapdoor::mod().getSimPlayerManager().refreshCommandScriptSoftEnum(
trapdoor::Configuration::readBotScripts());
return trapdoor::mod().initConfig(true)
? trapdoor::SuccessMsg("trapdoor.reload.success")
: trapdoor::ErrorMsg("trapdoor.reload.error");
Expand Down
Loading

0 comments on commit 9820411

Please sign in to comment.