From 23c66654a21c60a0bf22ae87530fde26165bb8d1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 05:56:14 +0300 Subject: [PATCH] add ENTITY_NONE, ENTITY_AUTO reserved entity id & update player.set_entity(...) --- src/constants.hpp | 1 + src/logic/scripting/lua/libs/libplayer.cpp | 4 +++- src/objects/Player.cpp | 4 ++-- src/objects/Player.hpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/constants.hpp b/src/constants.hpp index 6f4a6ce6..8755331d 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -27,6 +27,7 @@ inline constexpr blockid_t BLOCK_OBSTACLE = 1; inline constexpr blockid_t BLOCK_STRUCT_AIR = 2; inline constexpr itemid_t ITEM_EMPTY = 0; inline constexpr entityid_t ENTITY_NONE = 0; +inline constexpr entityid_t ENTITY_AUTO = std::numeric_limits::max(); inline constexpr int CHUNK_W = 16; inline constexpr int CHUNK_H = 256; diff --git a/src/logic/scripting/lua/libs/libplayer.cpp b/src/logic/scripting/lua/libs/libplayer.cpp index 12b78a97..d4e55b77 100644 --- a/src/logic/scripting/lua/libs/libplayer.cpp +++ b/src/logic/scripting/lua/libs/libplayer.cpp @@ -229,7 +229,9 @@ static int l_set_entity(lua::State* L) { if (player == nullptr) { return 0; } - if (auto entity = get_entity(L, 2)) { + if (lua::isnumber(L, 2)) { + player->setEntity(lua::tointeger(L, 2)); + } else if (auto entity = get_entity(L, 2)) { player->setEntity(entity->getUID()); } return 0; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 777fd8ed..888fce96 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -62,7 +62,7 @@ Player::Player( Player::~Player() = default; void Player::updateEntity() { - if (eid == 0) { + if (eid == ENTITY_AUTO) { auto& def = level.content.entities.require("base:player"); eid = level.entities->spawn(def, getPosition()); if (auto entity = level.entities->get(eid)) { @@ -73,7 +73,7 @@ void Player::updateEntity() { if (auto entity = level.entities->get(eid)) { entity->setPlayer(id); } - } else if (chunks->getChunkByVoxel(position)) { + } else if (chunks->getChunkByVoxel(position) && eid != ENTITY_NONE) { logger.error() << "player entity despawned or deleted; " "will be respawned"; eid = 0; diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index b695c75e..4b9f2449 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -54,7 +54,7 @@ class Player : public Serializable { bool infiniteItems = true; bool instantDestruction = true; bool loadingChunks = true; - entityid_t eid; + entityid_t eid = ENTITY_AUTO; entityid_t selectedEid = 0; glm::vec3 rotation {};