Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
odorajbotoj committed Oct 15, 2024
2 parents ef6ad1e + ce22101 commit 9bd9605
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 55 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2024-10-16

### Added

+ Added lua api `vec3.newFromRotation`
+ Added lua api `vec2.newFromDirection`

### Fixed

+ Fixed simplayer lookat
+ Fixed rotation set when spawn simplayer

### Changed

+ SimPlayerInfo api now will throw error when ptr simPlayer is null

## [2.1.2] - 2024-10-14

We skipped `v2.1.0` and `v2.1.1`. They have some serious bugs.
Expand Down Expand Up @@ -39,6 +55,7 @@ We skipped `v2.1.0` and `v2.1.1`. They have some serious bugs.
+ Move CoralFans SimulatedPlayer System from [CoralFans](https://github.com/CoralFans-Dev/CoralFans) to here
+ Added Script Arg

[2.2.0]: https://github.com/CoralFans-Dev/CFSP/compare/v2.1.2...v2.2.0
[2.1.2]: https://github.com/CoralFans-Dev/CFSP/compare/v2.0.0...v2.1.2
[2.0.0]: https://github.com/CoralFans-Dev/CFSP/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/CoralFans-Dev/CFSP/releases/tag/v1.0.0
99 changes: 81 additions & 18 deletions src/cfsp/simplayer/CFSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <boost/smart_ptr/shared_ptr.hpp>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -92,16 +93,36 @@ class SimPlayerManager {
CFSP_API inline std::string getName() { return name; }
CFSP_API inline std::string getXuid() { return xuid; }
CFSP_API inline int getStatus() { return status; }
CFSP_API inline Vec3 getPos() { return simPlayer->getPosition(); }
CFSP_API inline Vec3 getFeetPos() { return simPlayer->getFeetPos(); }
CFSP_API inline BlockPos getStandingOn() { return simPlayer->getBlockPosCurrentlyStandingOn(simPlayer); }
CFSP_API inline Vec2 getRot() { return simPlayer->getRotation(); }
CFSP_API inline int getHealth() { return simPlayer->getHealth(); }
CFSP_API inline float getHunger() { return simPlayer->getAttribute(SimulatedPlayer::HUNGER).getCurrentValue(); }
CFSP_API inline bool sneaking(bool enable) {
CFSP_API Vec3 getPos() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getPosition();
}
CFSP_API Vec3 getFeetPos() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getFeetPos();
}
CFSP_API BlockPos getStandingOn() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getBlockPosCurrentlyStandingOn(simPlayer);
}
CFSP_API Vec2 getRot() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getRotation();
}
CFSP_API int getHealth() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getHealth();
}
CFSP_API float getHunger() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->getAttribute(SimulatedPlayer::HUNGER).getCurrentValue();
}
CFSP_API bool sneaking(bool enable) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return enable ? simPlayer->simulateSneaking() : simPlayer->simulateStopSneaking();
}
CFSP_API void swimming(bool enable) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
if (enable) {
simPlayer->startSwimming();
taskid = scheduler->add(1, [sp = this->simPlayer](unsigned long long) {
Expand All @@ -117,12 +138,17 @@ class SimPlayerManager {
}
}
CFSP_API bool attack() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
const auto& hit = simPlayer->traceRay(5.25f, true, false);
if (hit) return simPlayer->simulateAttack(hit.getEntity());
else return false;
}
CFSP_API inline void chat(std::string const& msg) { simPlayer->simulateChat(msg); }
CFSP_API bool destroy() {
CFSP_API void chat(std::string const& msg) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateChat(msg);
}
CFSP_API bool destroy() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (hit)
return simPlayer->simulateDestroyBlock(
Expand All @@ -131,8 +157,12 @@ class SimPlayerManager {
);
else return false;
}
CFSP_API inline bool dropSelectedItem() { return simPlayer->simulateDropSelectedItem(); }
CFSP_API bool dropInv() {
CFSP_API bool dropSelectedItem() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->simulateDropSelectedItem();
}
CFSP_API bool dropInv() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
bool rst = true;
if (simPlayer->getSelectedItem() != ItemStack::EMPTY_ITEM) rst &= simPlayer->simulateDropSelectedItem();
int sel = simPlayer->getSelectedItemSlot();
Expand All @@ -146,6 +176,8 @@ class SimPlayerManager {
return rst;
}
CFSP_API void swap(Player* player) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
if (!player) throw std::invalid_argument("Player is null");
// get data
auto& spInv = simPlayer->getInventory();
auto& spArmor = ActorEquipment::getArmorContainer(simPlayer->getEntityContext());
Expand Down Expand Up @@ -185,6 +217,7 @@ class SimPlayerManager {
player->refreshInventory();
}
CFSP_API bool runCmd(std::string const& cmd) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
CommandContext ctx(cmd, std::make_unique<PlayerCommandOrigin>(PlayerCommandOrigin(*simPlayer)));
auto mc = ll::service::getMinecraft();
if (mc) {
Expand All @@ -194,30 +227,35 @@ class SimPlayerManager {
return false;
}
CFSP_API std::pair<BlockPos, bool> getBlockPosFromView() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
const auto& hit = simPlayer->traceRay(5.25f, false, true);
return {hit.mBlockPos, hit.mType == HitResultType::Tile};
}
CFSP_API int searchInInvWithId(int id, int start = 0) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = start; i < size; ++i)
if (inv.getItem(i).getId() == id) return i;
return -1;
}
CFSP_API int searchInInvWithName(std::string const& itemName, int start = 0) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = start; i < size; ++i)
if (utils::removeMinecraftPrefix(inv.getItem(i).getTypeName()) == itemName) return i;
return -1;
}
CFSP_API bool selectSlot(int slot) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
if (slot < 0 || slot >= simPlayer->getInventory().getContainerSize()) return false;
int sel = simPlayer->getSelectedItemSlot();
utils::swapItemInContainer(simPlayer, sel, slot);
return true;
}
CFSP_API bool select(int id) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
int sel = simPlayer->getSelectedItemSlot();
int target = searchInInvWithId(id);
if (target == sel) target = searchInInvWithId(id, sel + 1);
Expand All @@ -226,22 +264,42 @@ class SimPlayerManager {
return true;
}
CFSP_API const ItemStack& getItemFromInv(int slot) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
auto& inv = simPlayer->getInventory();
return inv.getItem(slot);
}
CFSP_API inline bool interact() { return simPlayer->simulateInteract(); }
CFSP_API inline bool jump() { return simPlayer->simulateJump(); }
CFSP_API void useItem(int delay) {
CFSP_API bool interact() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->simulateInteract();
}
CFSP_API bool jump() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
return simPlayer->simulateJump();
}
CFSP_API void useItem(int delay) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateUseItemInSlot(simPlayer->getSelectedItemSlot());
taskid = scheduler->add(delay, [sp = this->simPlayer](unsigned long long) {
if (sp) sp->simulateStopUsingItem();
return false;
});
}
CFSP_API inline void startBuild() { simPlayer->simulateStartBuildInSlot(simPlayer->getSelectedItemSlot()); }
CFSP_API inline void lookAt(Vec3 const& pos) { simPlayer->simulateLookAt(pos, ::sim::LookDuration{}); }
CFSP_API inline void moveTo(Vec3 const& pos) { simPlayer->simulateMoveToLocation(pos, 4.3f, true); }
CFSP_API inline void navigateTo(Vec3 const& pos) { simPlayer->simulateNavigateToLocation(pos, 4.3f); }
CFSP_API void startBuild() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateStartBuildInSlot(simPlayer->getSelectedItemSlot());
}
CFSP_API void lookAt(Vec3 const& pos) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateLookAt(pos, ::sim::LookDuration{2});
}
CFSP_API void moveTo(Vec3 const& pos) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateMoveToLocation(pos, 4.3f, true);
}
CFSP_API void navigateTo(Vec3 const& pos) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateNavigateToLocation(pos, 4.3f);
}
CFSP_API inline void cancelTask() { scheduler->cancel(taskid); }
CFSP_API inline void cancelScript() { scheduler->cancel(scriptid); }
CFSP_API bool isTaskFree() {
Expand All @@ -260,6 +318,7 @@ class SimPlayerManager {
}
CFSP_API inline bool isFree() { return isTaskFree() && isScriptFree(); }
CFSP_API void stopAction() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
simPlayer->simulateStopBuild();
simPlayer->simulateStopDestroyingBlock();
simPlayer->simulateStopFlying();
Expand All @@ -275,6 +334,7 @@ class SimPlayerManager {
scriptid = 0;
}
CFSP_API int getFirstEmptySlot() {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = 0; i < size; ++i)
Expand All @@ -292,6 +352,7 @@ class SimPlayerManager {
NoItem = 7
};
CFSP_API ContainerOperationErrCode trySwapSlotWithContainer(int invSlot, int targetSlot) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
Expand All @@ -316,6 +377,7 @@ class SimPlayerManager {
return errc::Success;
}
CFSP_API ContainerOperationErrCode tryPutIntoContainer(int invSlot) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
Expand Down Expand Up @@ -344,6 +406,7 @@ class SimPlayerManager {
return errc::NoEnoughSpace;
}
CFSP_API ContainerOperationErrCode tryGetFromContainerWithName(std::string const& typeName) {
if (!simPlayer) throw std::invalid_argument("SimPlayer is null");
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
Expand Down
13 changes: 8 additions & 5 deletions src/cfsp/simplayer/SimPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,11 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c
auto serverNetworkHandler = mc->getServerNetworkHandler();
if (!serverNetworkHandler) return {"translate.simplayer.error.cannotcreate"_tr(), false};
if (rejoin) {
Vec3 offlinePos{spIt->second->offlinePosX, spIt->second->offlinePosY, spIt->second->offlinePosZ};
Vec2 offlineRot{spIt->second->offlineRotX, spIt->second->offlineRotY};
auto* simPlayer = SimulatedPlayer::create(
spname,
{spIt->second->offlinePosX, spIt->second->offlinePosY, spIt->second->offlinePosZ},
offlinePos,
spIt->second->offlineDim,
serverNetworkHandler,
spIt->second->xuid
Expand All @@ -385,10 +387,10 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c
simPlayer->setPlayerGameType(
magic_enum::enum_cast<GameType>(spIt->second->offlineGameType).value_or(GameType::WorldDefault)
);
simPlayer->teleport(
{spIt->second->offlinePosX, spIt->second->offlinePosY, spIt->second->offlinePosZ},
spIt->second->offlineDim,
{spIt->second->offlineRotX, spIt->second->offlineRotY}
simPlayer->teleport(offlinePos, spIt->second->offlineDim, offlineRot);
simPlayer->simulateLookAt(
simPlayer->getPosition() + Vec3::directionFromRotation(offlineRot),
::sim::LookDuration{2}
);
spIt->second->status = SimPlayerStatus::Alive;
spIt->second->simPlayer = simPlayer;
Expand All @@ -409,6 +411,7 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c
if (!simPlayer) return {"translate.simplayer.error.cannotcreate"_tr(), false};
simPlayer->setPlayerGameType(player->getPlayerGameType());
simPlayer->teleport(pos, player->getDimensionId(), rot);
simPlayer->simulateLookAt(simPlayer->getPosition() + Vec3::directionFromRotation(rot), ::sim::LookDuration{2});
// add to map
this->mNameSimPlayerMap[spname] = boost::shared_ptr<SimPlayerInfo>(new SimPlayerInfo{
spname,
Expand Down
38 changes: 25 additions & 13 deletions src/cfsp/simplayer/luaapi/Vec2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "mc/math/Vec2.h"
#include "cfsp/base/Macros.h"
#include "cfsp/simplayer/luaapi/Utils.h"
#include "mc/math/Vec3.h"
#include <string>

extern "C" {
Expand Down Expand Up @@ -98,18 +98,30 @@ LUAAPI(vec2_newZero) {
return 1;
}

static const luaL_Reg lua_reg_vec2_c[11] = {
{"new", lua_api_vec2_new },
{"newLowest", lua_api_vec2_newLowest },
{"newMax", lua_api_vec2_newMax },
{"newMin", lua_api_vec2_newMin },
{"newNegUnitX", lua_api_vec2_newNegUnitX},
{"newNegUnitY", lua_api_vec2_newNegUnitY},
{"newOne", lua_api_vec2_newOne },
{"newUnitX", lua_api_vec2_newUnitX },
{"newUnitY", lua_api_vec2_newUnitY },
{"newZero", lua_api_vec2_newZero },
{NULL, NULL }
LUAAPI(vec2_newFromDirection) {
LUA_ARG_COUNT_CHECK_C(1)
Vec3* pos = (Vec3*)luaL_checkudata(L, 1, "vec3_mt");
luaL_argcheck(L, pos != nullptr, 1, "invalid userdata");
lua_settop(L, 0);
Vec2* rot = (Vec2*)lua_newuserdata(L, sizeof(Vec2));
*rot = Vec3::rotationFromDirection(*pos);
luaL_setmetatable(L, "vec2_mt");
return 1;
}

static const luaL_Reg lua_reg_vec2_c[12] = {
{"new", lua_api_vec2_new },
{"newLowest", lua_api_vec2_newLowest },
{"newMax", lua_api_vec2_newMax },
{"newMin", lua_api_vec2_newMin },
{"newNegUnitX", lua_api_vec2_newNegUnitX },
{"newNegUnitY", lua_api_vec2_newNegUnitY },
{"newOne", lua_api_vec2_newOne },
{"newUnitX", lua_api_vec2_newUnitX },
{"newUnitY", lua_api_vec2_newUnitY },
{"newZero", lua_api_vec2_newZero },
{"newFromDirection", lua_api_vec2_newFromDirection},
{NULL, NULL }
};

LUAAPI(vec2_get) {
Expand Down
4 changes: 3 additions & 1 deletion src/cfsp/simplayer/luaapi/Vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ int lua_api_vec2_newUnitY(lua_State*);

int lua_api_vec2_newZero(lua_State*);

extern const luaL_Reg lua_reg_vec2_c[11];
int lua_api_vec2_newFromDirection(lua_State*);

extern const luaL_Reg lua_reg_vec2_c[12];

int lua_api_vec2_get(lua_State*);

Expand Down
Loading

0 comments on commit 9bd9605

Please sign in to comment.