Merge branch 'MihailRis:main' into fix

This commit is contained in:
Sergwest 2024-03-21 15:02:40 +03:00 committed by GitHub
commit 1a10258df2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 137 additions and 24 deletions

View File

@ -193,3 +193,13 @@ audio.set_velocity(speakerid: integer, x: number, y: number, z: number)
-- also returns 0, if duration is unknown (example: radio)
audio.get_duration(speakerid: integer) -> number
```
### Other functions
```lua
-- get current number of alive speakers
audio.count_speakers() -> integer
-- get current number of playing streams
audio.count_streams() -> integer
```

View File

@ -213,6 +213,19 @@ block.get_Z(x: int, y: int, z: int) -> int, int, int
Returns Z: integer direction vector of the block at specified coordinates.
Example: no rotation: 0, 0, 1
```python
block.get_rotation(x: int, y: int, z: int) -> int
```
Returns block rotation index based on used profile.
```python
block.set_rotation(x: int, y: int, z: int, rotation: int)
```
Set block rotation by index.
### User bits
Part of a voxel data used for scripting. Size: 8 bit.

View File

@ -194,3 +194,12 @@ audio.set_velocity(speakerid: integer, x: number, y: number, z: number)
audio.get_duration(speakerid: integer) -> number
```
### Другие функции
```lua
-- получить текущее число живых спикеров
audio.count_speakers() -> integer
-- получить текущее число проигрываемых аудио-потоков
audio.count_streams() -> integer
```

View File

@ -208,6 +208,18 @@ block.get_Z(x: int, y: int, z: int) -> int, int, int
Возвращает целочисленный единичный вектор Z блока на указанных координатах с учётом его вращения (три целых числа).
Если поворот отсутствует, возвращает 0, 0, 1
```python
block.get_rotation(x: int, y: int, z: int) -> int
```
Возвращает индекс поворота блока в его профиле вращения.
```python
block.set_rotation(x: int, y: int, z: int, rotation: int)
```
Устанавливает вращение блока по индексу в его профиле вращения.
### Пользовательские биты
Выделенная под использования в скриптах часть поля `voxel.states` хранящего доп-информацию о вокселе, такую как вращение блока. На данный момент выделенная часть составляет 8 бит.

View File

@ -135,6 +135,18 @@ function events.on(event, func)
table.insert(events.handlers[event], func)
end
function events.remove_by_prefix(prefix)
for name, handlers in pairs(events.handlers) do
if name:sub(1, #prefix) == prefix then
events.handlers[name] = nil
end
end
end
function pack.unload(prefix)
events.remove_by_prefix(prefix)
end
function events.emit(event, ...)
result = nil
if events.handlers[event] then

View File

@ -13,6 +13,7 @@
#include "../files/files.h"
#include "../coders/json.h"
#include "../typedefs.h"
#include "../core_defs.h"
#include "../data/dynamic.h"
#include "ContentPack.h"
@ -207,6 +208,10 @@ void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) {
root->str("script-name", def.scriptName);
root->str("ui-layout", def.uiLayout);
root->num("inventory-size", def.inventorySize);
if (def.hidden && def.pickingItem == def.name+BLOCK_ITEM_SUFFIX) {
def.pickingItem = CORE_EMPTY;
}
}
void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) {

View File

@ -1,27 +1,30 @@
#ifndef SRC_CORE_DEFS_H_
#define SRC_CORE_DEFS_H_
#ifndef CORE_DEFS_H_
#define CORE_DEFS_H_
#include <string>
const std::string TEXTURE_NOTFOUND = "notfound";
inline const std::string CORE_EMPTY = "core:empty";
inline const std::string CORE_AIR = "core:air";
inline const std::string TEXTURE_NOTFOUND = "notfound";
/* bindings used in engine code */
const std::string BIND_MOVE_FORWARD = "movement.forward";
const std::string BIND_MOVE_BACK = "movement.back";
const std::string BIND_MOVE_LEFT = "movement.left";
const std::string BIND_MOVE_RIGHT = "movement.right";
const std::string BIND_MOVE_JUMP = "movement.jump";
const std::string BIND_MOVE_SPRINT = "movement.sprint";
const std::string BIND_MOVE_CROUCH = "movement.crouch";
const std::string BIND_MOVE_CHEAT = "movement.cheat";
const std::string BIND_CAM_ZOOM = "camera.zoom";
const std::string BIND_CAM_MODE = "camera.mode";
const std::string BIND_PLAYER_NOCLIP = "player.noclip";
const std::string BIND_PLAYER_FLIGHT = "player.flight";
const std::string BIND_PLAYER_ATTACK = "player.attack";
const std::string BIND_PLAYER_BUILD = "player.build";
const std::string BIND_PLAYER_PICK = "player.pick";
const std::string BIND_HUD_INVENTORY = "hud.inventory";
inline const std::string BIND_MOVE_FORWARD = "movement.forward";
inline const std::string BIND_MOVE_BACK = "movement.back";
inline const std::string BIND_MOVE_LEFT = "movement.left";
inline const std::string BIND_MOVE_RIGHT = "movement.right";
inline const std::string BIND_MOVE_JUMP = "movement.jump";
inline const std::string BIND_MOVE_SPRINT = "movement.sprint";
inline const std::string BIND_MOVE_CROUCH = "movement.crouch";
inline const std::string BIND_MOVE_CHEAT = "movement.cheat";
inline const std::string BIND_CAM_ZOOM = "camera.zoom";
inline const std::string BIND_CAM_MODE = "camera.mode";
inline const std::string BIND_PLAYER_NOCLIP = "player.noclip";
inline const std::string BIND_PLAYER_FLIGHT = "player.flight";
inline const std::string BIND_PLAYER_ATTACK = "player.attack";
inline const std::string BIND_PLAYER_BUILD = "player.build";
inline const std::string BIND_PLAYER_PICK = "player.pick";
inline const std::string BIND_HUD_INVENTORY = "hud.inventory";
class ContentBuilder;
@ -30,4 +33,4 @@ namespace corecontent {
extern void setup(ContentBuilder* builder);
}
#endif // SRC_CORE_DEFS_H_
#endif // CORE_DEFS_H_

View File

@ -170,6 +170,14 @@ static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement el
align_from_string(element->attr("valign").getText(), label->getVerticalAlign())
);
}
if (element->has("supplier")) {
auto supplier = scripting::create_wstring_supplier(
reader.getEnvironment().getId(),
element->attr("supplier").getText(),
reader.getFilename()
);
label->textSupplier(supplier);
}
return label;
}

View File

@ -247,6 +247,14 @@ const char* lua::LuaState::tostring(int idx) {
return lua_tostring(L, idx);
}
bool lua::LuaState::isstring(int idx) {
return lua_isstring(L, idx);
}
bool lua::LuaState::isfunction(int idx) {
return lua_isfunction(L, idx);
}
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs, int nup) {
lua_newtable(L);
luaL_setfuncs(L, libfuncs, nup);

View File

@ -45,6 +45,8 @@ namespace lua {
luaint tointeger(int idx);
luanumber tonumber(int idx);
const char* tostring(int idx);
bool isstring(int idx);
bool isfunction(int idx);
int call(int argc);
int callNoThrow(int argc);
int execute(int env, const std::string& src, const std::string& file="<string>");

View File

@ -391,6 +391,18 @@ static int l_audio_get_velocity(lua_State* L) {
return 0;
}
// @brief audio.count_speakers() -> integer
static int l_audio_count_speakers(lua_State* L) {
lua_pushinteger(L, audio::count_speakers());
return 1;
}
// @brief audio.count_streams() -> integer
static int l_audio_count_streams(lua_State* L) {
lua_pushinteger(L, audio::count_streams());
return 1;
}
const luaL_Reg audiolib [] = {
{"play_sound", lua_wrap_errors<l_audio_play_sound>},
{"play_sound_2d", lua_wrap_errors<l_audio_play_sound_2d>},
@ -414,5 +426,7 @@ const luaL_Reg audiolib [] = {
{"get_duration", lua_wrap_errors<l_audio_get_duration>},
{"get_position", lua_wrap_errors<l_audio_get_position>},
{"get_velocity", lua_wrap_errors<l_audio_get_velocity>},
{"count_speakers", lua_wrap_errors<l_audio_count_speakers>},
{"count_streams", lua_wrap_errors<l_audio_count_streams>},
{NULL, NULL}
};

View File

@ -125,6 +125,15 @@ void scripting::on_world_quit() {
for (auto& pack : scripting::engine->getContentPacks()) {
emit_event(pack.id + ".worldquit");
}
state->getglobal("pack");
for (auto& pack : scripting::engine->getContentPacks()) {
state->getfield("unload");
state->pushstring(pack.id);
state->callNoThrow(1);
}
state->pop();
if (state->getglobal("__scripts_cleanup")) {
state->callNoThrow(0);
}

View File

@ -58,7 +58,9 @@ wstringsupplier scripting::create_wstring_supplier(
) {
return [=](){
if (processCallback(env, src, file)) {
state->callNoThrow(0);
if (state->isfunction(-1)) {
state->callNoThrow(0);
}
auto str = state->tostring(-1); state->pop();
return util::str2wstr_utf8(str);
}
@ -101,7 +103,9 @@ boolsupplier scripting::create_bool_supplier(
) {
return [=](){
if (processCallback(env, src, file)) {
state->callNoThrow(0);
if (state->isfunction(-1)) {
state->callNoThrow(0);
}
bool x = state->toboolean(-1); state->pop();
return x;
}
@ -129,7 +133,9 @@ doublesupplier scripting::create_number_supplier(
) {
return [=](){
if (processCallback(env, src, file)) {
state->callNoThrow(0);
if (state->isfunction(-1)) {
state->callNoThrow(0);
}
lua::luanumber x = state->tonumber(-1); state->pop();
return x;
}
@ -159,7 +165,9 @@ vec2supplier scripting::create_vec2_supplier(
) {
return [=](){
if (processCallback(env, src, file)) {
state->callNoThrow(0);
if (state->isfunction(-1)) {
state->callNoThrow(0);
}
lua::luanumber y = state->tonumber(-1); state->pop();
lua::luanumber x = state->tonumber(-1); state->pop();
return glm::vec2(x, y);