Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix queryEntities #1123

Merged
merged 2 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion LiteLoader/include/llapi/mc/BlockSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ struct ClipParameters
* // If ignoreTargetType = true, query results will contain actorTypes other than targets.
* bs->queryEntities(ActorType::Creeper, aabb, true);
*
* // extendDistance means to the distance to expand outward when querying chunks, depends on the
* size of the collision box of entity you want to query
*
* @endcode
*/
LIAPI vector<Actor*> queryEntities(ActorType actorType, const AABB& range, bool ignoreTargetType = false);
LIAPI vector<Actor*> queryEntities(ActorType actorType, const AABB& range, bool ignoreTargetType = false,
float extendDistance = 2.0f);
LIAPI vector<Actor*> getEntities(const AABB& range, float extendDistance = 2.0f);

#undef AFTER_EXTRA
#ifndef DISABLE_CONSTRUCTOR_PREVENTION_BLOCKSOURCE
Expand Down
53 changes: 51 additions & 2 deletions LiteLoader/src/llapi/mc/BlockSourceAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,60 @@

#include "llapi/Global.h"
#include "llapi/mc/BlockInstance.hpp"
#include "llapi/mc/AABB.hpp"
#include "llapi/mc/ChunkPos.hpp"
#include "llapi/mc/LevelChunk.hpp"
#include "llapi/mc/WeakEntityRef.hpp"
#include "llapi/mc/ActorClassTree.hpp"

BlockInstance BlockSource::getBlockInstance(BlockPos a1) {
return BlockInstance{const_cast<Block*>(&getBlock(a1)), a1, this->getDimensionId()};
}

std::vector<Actor*> BlockSource::queryEntities(ActorType actorType, const AABB& range, bool ignoreType) {
return fetchEntities2(actorType, range, ignoreType);
std::vector<Actor*> BlockSource::queryEntities(ActorType actorType, const AABB& range, bool ignoreType,
float extendDistance) {
std::vector<Actor*> entities;
entities.clear();
ChunkPos minChunk(((int)std::floor(range.min.x - extendDistance)) >> 4,
((int)std::floor(range.min.z - extendDistance)) >> 4);
ChunkPos maxChunk(((int)std::floor(range.max.x + extendDistance)) >> 4,
((int)std::floor(range.max.z + extendDistance)) >> 4);

for (int x = minChunk.x; x <= maxChunk.x; x++)
for (int z = minChunk.z; z <= maxChunk.z; z++) {
LevelChunk* chunk = getChunk({x, z});
if (chunk != nullptr) {
for (auto& weakEntityRef : chunk->getChunkEntities()) {
Actor* actor = weakEntityRef.tryUnwrap();
if (actor != nullptr && ActorClassTree::isInstanceOf(*actor, actorType) != ignoreType &&
range.intersects(actor->getAABB())) {
entities.emplace_back(actor);
}
}
}
}
return std::move(entities);
}

std::vector<Actor*> BlockSource::getEntities(const AABB& range, float extendDistance) {
std::vector<Actor*> entities;
entities.clear();
ChunkPos minChunk(((int)std::floor(range.min.x - extendDistance)) >> 4,
((int)std::floor(range.min.z - extendDistance)) >> 4);
ChunkPos maxChunk(((int)std::floor(range.max.x + extendDistance)) >> 4,
((int)std::floor(range.max.z + extendDistance)) >> 4);

for (int x = minChunk.x; x <= maxChunk.x; x++)
for (int z = minChunk.z; z <= maxChunk.z; z++) {
LevelChunk* chunk = getChunk({x, z});
if (chunk != nullptr) {
for (auto& weakEntityRef : chunk->getChunkEntities()) {
Actor* actor = weakEntityRef.tryUnwrap();
if (actor != nullptr && range.intersects(actor->getAABB())) {
entities.emplace_back(actor);
}
}
}
}
return std::move(entities);
}
95 changes: 89 additions & 6 deletions ScriptEngine/src/api/EntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <llapi/mc/Attribute.hpp>
#include <llapi/mc/AttributeInstance.hpp>
#include <llapi/mc/Biome.hpp>
#include <llapi/mc/AABB.hpp>
#include <llapi/mc/BlockSource.hpp>
#include <magic_enum/magic_enum.hpp>

using magic_enum::enum_integer;
Expand Down Expand Up @@ -188,9 +190,7 @@ std::optional<Actor*> EntityClass::tryExtractActor(Local<Value> v) {
void EntityClass::set(Actor* actor) {
__try {
id = actor->getUniqueID();
} __except (EXCEPTION_EXECUTE_HANDLER) {
isValid = false;
}
} __except (EXCEPTION_EXECUTE_HANDLER) { isValid = false; }
}

Actor* EntityClass::get() {
Expand Down Expand Up @@ -982,8 +982,7 @@ Local<Value> EntityClass::getBlockStandingOn(const Arguments& args) {
if (!entity)
return Local<Value>();

return BlockClass::newBlock(entity->getBlockPosCurrentlyStandingOn(nullptr),
(int)entity->getDimensionId());
return BlockClass::newBlock(entity->getBlockPosCurrentlyStandingOn(nullptr), (int)entity->getDimensionId());
}
CATCH("Fail in getBlockStandingOn!");
}
Expand Down Expand Up @@ -1516,6 +1515,89 @@ Local<Value> McClass::getAllEntities(const Arguments& args) {
CATCH("Fail in GetAllEntities");
}

Local<Value> McClass::getEntities(const Arguments& args) {
try {
int dim;
float dis = 2.0f;
AABB aabb;
if (args.size() > 0) {

if (IsInstanceOf<IntPos>(args[0])) {
// IntPos
IntPos* posObj = IntPos::extractPos(args[0]);

aabb.min = posObj->getBlockPos().toVec3();
dim = posObj->dim;

} else if (IsInstanceOf<FloatPos>(args[0])) {
// FloatPos
FloatPos* posObj = FloatPos::extractPos(args[0]);
aabb.min = posObj->getVec3();
dim = posObj->dim;
} else {
LOG_WRONG_ARG_TYPE();
return Local<Value>();
}
if (args.size() > 1) {
if (IsInstanceOf<IntPos>(args[1])) {
// IntPos
IntPos* posObj = IntPos::extractPos(args[1]);
if (dim != posObj->dim) {
LOG_ERROR_WITH_SCRIPT_INFO("Wrong Dimension!");
return Local<Value>();
}
aabb.max = posObj->getBlockPos().toVec3()+1;
dim = posObj->dim;

} else if (IsInstanceOf<FloatPos>(args[1])) {
// FloatPos
FloatPos* posObj = FloatPos::extractPos(args[1]);
if(dim != posObj->dim){
LOG_ERROR_WITH_SCRIPT_INFO("Wrong Dimension!");
return Local<Value>();
}
aabb.max = posObj->getVec3();
dim = posObj->dim;
} else if (args[1].getKind() == ValueKind::kNumber) {
aabb.max = aabb.min + 1;
dis = args[1].asNumber().toFloat();
} else {
LOG_WRONG_ARG_TYPE();
return Local<Value>();
}
if (args.size() > 2) {
if (args[2].getKind() == ValueKind::kNumber) {
dis = args[1].asNumber().toFloat();
} else {
LOG_WRONG_ARG_TYPE();
return Local<Value>();
}
} else {
aabb.max = aabb.min + 1;
}
} else {
aabb.max = aabb.min + 1;
}
} else {
LOG_TOO_FEW_ARGS();
return Local<Value>();
}

auto arr = Array::newArray();
auto* bs = Level::getBlockSource(dim);
if(bs == nullptr) {
LOG_ERROR_WITH_SCRIPT_INFO("Wrong Dimension!");
return Local<Value>();
}
auto entityList = bs->getEntities(aabb, dis);
for (auto i : entityList) {
arr.add(EntityClass::newEntity(i));
}
return arr;
}
CATCH("Fail in GetAllEntities");
}

Local<Value> McClass::cloneMob(const Arguments& args) {
CHECK_ARGS_COUNT(args, 2);

Expand Down Expand Up @@ -1668,7 +1750,8 @@ Local<Value> McClass::explode(const Arguments& args) {
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
CHECK_ARG_TYPE(args[2], ValueKind::kNumber);
CHECK_ARG_TYPE(args[3], ValueKind::kNumber);
pos = {args[0].asNumber().toFloat(), args[1].asNumber().toFloat(), args[2].asNumber().toFloat(), args[3].toInt()};
pos = {args[0].asNumber().toFloat(), args[1].asNumber().toFloat(), args[2].asNumber().toFloat(),
args[3].toInt()};
} else {
LOG_WRONG_ARGS_COUNT();
return Local<Value>();
Expand Down
1 change: 1 addition & 0 deletions ScriptEngine/src/api/McAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ClassDefine<void> McClassBuilder =
.function("getPlayer", &McClass::getPlayer)
.function("getOnlinePlayers", &McClass::getOnlinePlayers)
.function("getAllEntities", McClass::getAllEntities)
.function("getEntities", McClass::getEntities)
.function("newItem", &McClass::newItem)
.function("spawnMob", &McClass::spawnMob)
.function("cloneMob", &McClass::cloneMob)
Expand Down
1 change: 1 addition & 0 deletions ScriptEngine/src/api/McAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class McClass {
static Local<Value> getPlayer(const Arguments& args);
static Local<Value> getOnlinePlayers(const Arguments& args);
static Local<Value> getAllEntities(const Arguments& args);
static Local<Value> getEntities(const Arguments& args);

static Local<Value> newItem(const Arguments& args);
static Local<Value> spawnMob(const Arguments& args);
Expand Down