Merge branch 'main' into animation

This commit is contained in:
MihailRis 2026-08-18 23:37:27 +03:00
commit 1186011dcf
23 changed files with 346 additions and 119 deletions

View file

@ -55,12 +55,12 @@ model::Model VcmModel::squashed() const {
}
static const std::unordered_map<std::string, int> side_indices {
{"east", 0},
{"west", 1},
{"bottom", 2},
{"top", 3},
{"south", 4},
{"north", 5},
{"west", 0}, // -X
{"east", 1}, // +X
{"bottom", 2}, // -Y
{"top", 3}, // +Y
{"north", 4}, // -Z
{"south", 5}, // +Z
};
static bool to_boolean(const xml::Attribute& attr) {
@ -267,12 +267,12 @@ static void perform_box(const xmlelement& root, ModelBuilder& builder) {
}
UVRegion regions[6] {};
regions[0].scale(to.x - from.x, to.y - from.y);
regions[1].scale(from.x - to.x, to.y - from.y);
regions[0].scale(to.z - from.z, to.y - from.y);
regions[1].scale(from.z - to.z, to.y - from.y);
regions[2].scale(to.x - from.x, to.z - from.z);
regions[3].scale(from.x - to.x, to.z - from.z);
regions[4].scale(to.z - from.z, to.y - from.y);
regions[5].scale(from.z - to.z, to.y - from.y);
regions[4].scale(to.x - from.x, to.y - from.y);
regions[5].scale(from.x - to.x, to.y - from.y);
auto center = (from + to) * 0.5f;
auto halfsize = (to - from) * 0.5f;
@ -306,7 +306,11 @@ static void perform_box(const xmlelement& root, ModelBuilder& builder) {
texfaces[idx] = elem->attr("texture").getText();
}
if (elem->has("region")) {
regions[idx].set(elem->attr("region").asVec4());
auto region = elem->attr("region").asVec4();
if (idx % 2 == 1) {
std::swap(region[0], region[2]);
}
regions[idx].set(region);
}
if (elem->has("region-scale")) {
regions[idx].scale(elem->attr("region-scale").asVec2());

View file

@ -39,16 +39,18 @@ std::unique_ptr<Content> ContentBuilder::build() {
} else {
def.defaults.rt.solid = isSolid(def.defaults) || def.explictlySolid;
}
if (def.material.empty()) {
defaults.at("block-material").get(def.material);
}
def.rt.solid = def.defaults.rt.solid;
const float EPSILON = 0.01f;
def.rt.solid =
def.rt.solid = def.rt.solid &&
def.obstacle &&
(glm::i8vec3(def.hitboxes[0].size() + EPSILON) == def.size);
def.rt.extended = def.size.x > 1 || def.size.y > 1 || def.size.z > 1;
if (def.material.empty()) {
defaults.at("block-material").get(def.material);
}
if (def.rotatable) {
for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) {
def.rt.hitboxes[i].reserve(def.hitboxes.size());

View file

@ -106,6 +106,7 @@ std::shared_ptr<UINode> HudElement::getNode() const {
std::shared_ptr<InventoryView> Hud::createContentAccess() {
auto& content = frontend.getLevel().content;
auto& indices = *content.getIndices();
auto& input = this->input;
auto inventory = player.getInventory();
size_t itemsCount = indices.items.count();
@ -115,12 +116,19 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
}
SlotLayout slotLayout(-1, glm::vec2(), false, true, nullptr,
[inventory, &indices](uint, ItemStack& item) {
[inventory, &indices, &input](uint, ItemStack& item) {
auto copy = ItemStack(item);
if (input.pressed(Keycode::LEFT_CONTROL)) {
copy.maximizeCount(indices);
}
inventory->move(copy, indices);
},
[this, inventory](uint, ItemStack& item) {
inventory->getSlot(player.getChosenSlot()).set(item);
[this, inventory, &indices, &input](uint, ItemStack& item) {
auto copy = ItemStack(item);
if (input.pressed(Keycode::LEFT_CONTROL)) {
copy.maximizeCount(indices);
}
inventory->getSlot(player.getChosenSlot()).set(copy);
});
InventoryBuilder builder(gui);

View file

@ -48,10 +48,14 @@ void gui::Frame::draw(const DrawContext& pctx, const Assets& assets) {
void gui::Frame::updateOutput(Assets& assets) {
if (fbo && (fbo->getWidth() != size.x || fbo->getHeight() != size.y)) {
fbo->resize(size.x, size.y);
assets.store(fbo->getSharedTexture(), outputTexture);
if (!outputTexture.empty()) {
assets.store(fbo->getSharedTexture(), outputTexture);
}
} else if (fbo == nullptr) {
fbo = std::make_unique<Framebuffer>(size.x, size.y, true);
assets.store(fbo->getSharedTexture(), outputTexture);
if (!outputTexture.empty()) {
assets.store(fbo->getSharedTexture(), outputTexture);
}
}
}

View file

@ -366,28 +366,12 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
);
return;
}
auto indices = *content->getIndices();
if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) {
action = InteractionAction::PUT;
stack.move(grabbed, *content->getIndices());
stack.move(grabbed, indices);
} else {
if (layout.itemSource) {
if (grabbed.isEmpty()) {
action = InteractionAction::TAKE;
grabbed.set(stack);
} else {
action = InteractionAction::PUT;
grabbed.clear();
}
} else if (grabbed.isEmpty()) {
if (layout.taking) {
action = InteractionAction::TAKE;
std::swap(grabbed, stack);
}
} else if (layout.taking && layout.placing) {
action = InteractionAction::PUT;
std::swap(grabbed, stack);
}
actIfCannotPut(stack, grabbed, action);
}
if (action != InteractionAction::UNDEFINED) {
@ -400,6 +384,39 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
}
}
void SlotView::actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action) {
const auto& input = gui.getInput();
auto indices = *content->getIndices();
if (layout.itemSource) {
if (grabbed.isEmpty()) {
action = InteractionAction::TAKE;
grabbed.set(stack);
if (input.pressed(Keycode::LEFT_CONTROL)) {
grabbed.maximizeCount(*content->getIndices());
}
} else {
if (grabbed.accepts(stack)) {
auto& def = indices.items.require(stack.getItemId());
if (grabbed.getCount() < def.stackSize) {
action = InteractionAction::TAKE;
grabbed.setCount(grabbed.getCount() + 1);
}
} else {
action = InteractionAction::PUT;
grabbed.clear();
}
}
} else if (grabbed.isEmpty()) {
if (layout.taking) {
action = InteractionAction::TAKE;
std::swap(grabbed, stack);
}
} else if (layout.taking && layout.placing) {
action = InteractionAction::PUT;
std::swap(grabbed, stack);
}
}
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
if (layout.rightClick) {
layout.rightClick(inventoryId, stack);

View file

@ -123,6 +123,8 @@ namespace gui {
SHARE = 2,
UNDEFINED,
};
private:
void actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action);
};
class InventoryView final : public gui::Container {

View file

@ -83,12 +83,12 @@ static util::TextureRegion determine_texture_region(
const Assets& assets, const std::string& texture
) {
static std::array<std::string, 6> faces {
"blocks:dbg_east",
"blocks:dbg_west",
"blocks:dbg_east",
"blocks:dbg_bottom",
"blocks:dbg_top",
"blocks:dbg_south",
"blocks:dbg_north",
"blocks:dbg_south",
};
if (texture.length() < 2 || texture.at(0) != '$') {

View file

@ -48,6 +48,14 @@ void ItemStack::setCount(itemcount_t count) {
}
}
void ItemStack::maximizeCount(const ContentIndices& indices) {
auto& def = indices.items.require(getItemId());
this->count = def.stackSize;
if (count == 0) {
clear();
}
}
void ItemStack::setField(std::string_view name, dv::value value) {
if (fields == nullptr) {
if (value == nullptr) {

View file

@ -18,6 +18,7 @@ public:
void set(const ItemStack& item);
void set(ItemStack&& item);
void setCount(itemcount_t count);
void maximizeCount(const ContentIndices& indices);
/// @brief Set a field in the item stack data.
void setField(std::string_view name, dv::value value);

View file

@ -2,6 +2,7 @@
#include "items/Inventories.hpp"
#include "items/ItemStack.hpp"
#include "logic/BlocksController.hpp"
#include "logic/scripting/lua/lua_util.hpp"
#include "world/Level.hpp"
#include "api_lua.hpp"
@ -180,7 +181,7 @@ static int l_move(lua::State* L) {
validate_slotid(slotAid, invA);
auto invBid = lua::tointeger(L, 3);
auto slotBid = lua::isnil(L, 4) ? -1 : lua::tointeger(L, 4);
auto slotBid = lua::isnoneornil(L, 4) ? -1 : lua::tointeger(L, 4);
auto& invB = get_inventory(invBid, 3);
auto& slot = invA.getSlot(slotAid);
if (slotBid == -1) {

View file

@ -1,6 +1,8 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <iostream>
#include <glm/gtx/string_cast.hpp>
#include "api_lua.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/quaternion.hpp>
#include <sstream>
@ -31,14 +33,14 @@ static int l_tostring(lua::State* L) {
lua::check_argc(L, 1);
auto quat = lua::toquat(L, 1);
float components[4] = {quat.w, quat.x, quat.y, quat.z};
std::stringstream ss;
ss << "quat"
<< "{";
ss << "quat {";
for (int i = 0; i < 4; i++) {
if (i > 0) {
ss << ", ";
}
ss << quat[i];
ss << components[i];
}
ss << "}";
return lua::pushstring(L, ss.str());
@ -52,10 +54,42 @@ static int l_from_euler(lua::State *L) {
return lua::pushquat(L, glm::quat(euler));
}
static int l_mul(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3);
glm::quat a = lua::toquat(L, 1);
glm::quat b = lua::toquat(L, 2);
glm::quat result = a * b;
if (argc == 3) {
lua::setquat(L, 3, result);
return 0;
}
return lua::pushquat(L, result);
}
static int l_mul_vec3(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3);
glm::quat q = lua::toquat(L, 1);
glm::vec3 v = lua::tovec3(L, 2);
glm::vec3 result = q * v;
if (argc == 3) {
lua::setvec(L, 3, result);
return 0;
}
return lua::pushvec3(L, result);
}
const luaL_Reg quatlib[] = {
{"from_mat4", lua::wrap<l_from_mat4>},
{"from_euler", lua::wrap<l_from_euler>},
{"slerp", lua::wrap<l_slerp>},
{"tostring", lua::wrap<l_tostring>},
{"mul", lua::wrap<l_mul>},
{"mul_vec3", lua::wrap<l_mul_vec3>},
{nullptr, nullptr}
};