Fix player not being initialized at mod load time. Targets have dim.
parent
b312193969
commit
dfb90e8c03
|
@ -1,16 +1,15 @@
|
|||
zepha.after(function()
|
||||
zepha.player:set_hud(zepha.build_gui(function()
|
||||
return Gui.Body {
|
||||
background = "base:viginette",
|
||||
zepha.player:set_hud(zepha.build_gui(function()
|
||||
return Gui.Body {
|
||||
background = "base:viginette",
|
||||
|
||||
Gui.Rect {
|
||||
key = "crosshair",
|
||||
position = { pc(50), pc(50) },
|
||||
position_anchor = { pc(50), pc(50) },
|
||||
size = { 22 / 3, 22 / 3 },
|
||||
Gui.Rect {
|
||||
key = "crosshair",
|
||||
|
||||
background = "base:crosshair"
|
||||
}
|
||||
position = { pc(50), pc(50) },
|
||||
position_anchor = { pc(50), pc(50) },
|
||||
size = { 22 / 3, 22 / 3 },
|
||||
|
||||
background = "base:crosshair"
|
||||
}
|
||||
end))
|
||||
end, 0)
|
||||
}
|
||||
end))
|
|
@ -27,9 +27,10 @@ function zepha.block_interact(player, target)
|
|||
local block = player.dim:get_block(target.pos)
|
||||
local def = zepha.registered_blocks[block]
|
||||
|
||||
local args = { target.dim, target.pos, player }
|
||||
local cb = zepha.server and "on_interact" or "on_interact_client"
|
||||
if type(def[cb]) == "function" then def[cb](target.pos, player) end
|
||||
zepha.trigger(cb, target.pos, player)
|
||||
if type(def[cb]) == "function" then def[cb](unpack(args)) end
|
||||
zepha.trigger(cb, unpack(args))
|
||||
|
||||
return def.on_interact or def.on_interact_client
|
||||
end
|
||||
|
|
|
@ -15,12 +15,8 @@ LocalSubgame::LocalSubgame(const std::string& texPath) :
|
|||
textures.loadDirectory(texPath);
|
||||
}
|
||||
|
||||
void LocalSubgame::initApi(WorldPtr world, ClientState& state) {
|
||||
lua->init(world, state);
|
||||
}
|
||||
|
||||
void LocalSubgame::loadPlayer(PlayerPtr player) {
|
||||
lua->loadPlayer(player);
|
||||
void LocalSubgame::init(WorldPtr world, PlayerPtr player, ClientState& state) {
|
||||
lua->init(world, player, state);
|
||||
}
|
||||
|
||||
void LocalSubgame::update(double delta) {
|
||||
|
|
|
@ -25,8 +25,7 @@ public:
|
|||
explicit LocalSubgame(const std::string& texPath);
|
||||
~LocalSubgame();
|
||||
|
||||
void initApi(WorldPtr world, ClientState& state);
|
||||
void loadPlayer(PlayerPtr player);
|
||||
void init(WorldPtr world, PlayerPtr player, ClientState& state);
|
||||
void update(double delta);
|
||||
|
||||
std::string texPath;
|
||||
|
|
|
@ -16,9 +16,9 @@ DebugGui::DebugGui(glm::vec2 bufferSize, SubgamePtr game, WorldPtr world) :
|
|||
game(game),
|
||||
world(world) {
|
||||
|
||||
auto fontRef = game.l()->textures["font"];
|
||||
auto fpsHistogramRef = game.l()->textures["histogram"];
|
||||
auto genericHistogramRef = game.l()->textures["histogram_white"];
|
||||
auto fontRef = game.l()->textures["font"];
|
||||
|
||||
Font f(game.l()->textures, fontRef);
|
||||
|
||||
|
@ -79,8 +79,12 @@ void DebugGui::positionElements(glm::vec2 bufferSize) {
|
|||
}
|
||||
|
||||
void DebugGui::update(std::shared_ptr<LocalPlayer> player, double fps, int /*chunks*/, int drawCalls, int ssGen, int ssPack) {
|
||||
Target target = player->getTarget();
|
||||
|
||||
{ //Top Right Graphs
|
||||
auto& onBiomeDef = game->getBiomes().biomeFromId(world.l()->getActiveDimension()->getBiome(glm::floor(player->getPos())));
|
||||
auto& targetedBlockDef = game->getDefs().blockFromId(world.l()->getActiveDimension()->getBlock(target.pos));
|
||||
|
||||
{ // Top Right Graphs
|
||||
get<GuiLabelledGraph>("fpsGraph")->pushValue(static_cast<float>(fps));
|
||||
get<GuiLabelledGraph>("drawsGraph")->pushValue(drawCalls);
|
||||
|
||||
|
@ -90,27 +94,23 @@ void DebugGui::update(std::shared_ptr<LocalPlayer> player, double fps, int /*chu
|
|||
glGetIntegerv(0x9049, &videoMemAvail);
|
||||
|
||||
get<GuiLabelledGraph>("gpuGraph")->pushValue(static_cast<int>(std::round(
|
||||
(videoMemTotal - videoMemAvail) / static_cast<float>(videoMemTotal) * 100.0))
|
||||
/ 100.0f);
|
||||
(videoMemTotal - videoMemAvail) / static_cast<float>(videoMemTotal) * 100.0)) / 100.0f);
|
||||
}
|
||||
|
||||
{ //Bottom Right Graphs
|
||||
{ // Bottom Right Graphs
|
||||
get<GuiLabelledGraph>("meshGraph")->pushValue(world.l()->lastMeshUpdates);
|
||||
get<GuiLabelledGraph>("interpGraph")->pushValue(world.l()->mapBlocksInterpolated);
|
||||
get<GuiLabelledGraph>("genGraph")->pushValue(static_cast<float>(ssGen));
|
||||
get<GuiLabelledGraph>("packetGraph")->pushValue(static_cast<float>(ssPack));
|
||||
}
|
||||
|
||||
{ //Top-left Data
|
||||
unsigned int biomeID = world.l()->getActiveDimension()->getBiome(glm::floor(player->getPos()));
|
||||
std::string biome = game->getBiomes().biomeFromId(biomeID).identifier;
|
||||
|
||||
{ // Top-left Data
|
||||
glm::vec3 playerPos = glm::floor(player->getPos());
|
||||
glm::vec3 chunkPos = Space::Chunk::world::fromBlock(playerPos);
|
||||
glm::vec3 mapBlockPos = Space::MapBlock::world::fromChunk(chunkPos);
|
||||
glm::vec3 regionPos = Space::Region::world::fromChunk(chunkPos);
|
||||
|
||||
//Block Coordinates offset from respective container
|
||||
// Block Coordinates offset from respective container
|
||||
glm::vec3 posOffsetFromChunk = Space::Block::relative::toChunk(playerPos);
|
||||
glm::vec3 posOffsetFromBlock = Space::Block::relative::toMapBlock(playerPos);
|
||||
glm::vec3 posOffsetFromRegion = Space::Block::relative::toRegion(playerPos);
|
||||
|
@ -130,24 +130,23 @@ void DebugGui::update(std::shared_ptr<LocalPlayer> player, double fps, int /*chu
|
|||
str << "Yaw: " << floatToString(player->getYaw()) << ", ";
|
||||
str << "Pitch: " << floatToString(player->getPitch()) << std::endl << std::endl;
|
||||
|
||||
str << "Biome: " << biome << std::endl << std::endl;
|
||||
str << "Biome: " << onBiomeDef.identifier << " [" << onBiomeDef.index << "]" << std::endl << std::endl;
|
||||
|
||||
str << "Texture Slots: " << game.l()->textures.textureSlotsUsed << " / " << game.l()->textures.maxTextureSlots
|
||||
<< " (" << round(game.l()->textures.textureSlotsUsed / static_cast<float>(game.l()->textures.maxTextureSlots) * 100) << "%)" << std::endl << std::endl;
|
||||
|
||||
Target thing = player->getPointedThing();
|
||||
if (thing.type == Target::Type::BLOCK) {
|
||||
if (target.type == Target::Type::BLOCK) {
|
||||
std::string face =
|
||||
thing.face == EVec::TOP ? "TOP" :
|
||||
thing.face == EVec::BOTTOM ? "BOTTOM" :
|
||||
thing.face == EVec::LEFT ? "LEFT" :
|
||||
thing.face == EVec::RIGHT ? "RIGHT" :
|
||||
thing.face == EVec::FRONT ? "FRONT" :
|
||||
thing.face == EVec::BACK ? "BACK" :
|
||||
"NONE" ;
|
||||
target.face == EVec::TOP ? "TOP" :
|
||||
target.face == EVec::BOTTOM ? "BOTTOM" :
|
||||
target.face == EVec::LEFT ? "LEFT" :
|
||||
target.face == EVec::RIGHT ? "RIGHT" :
|
||||
target.face == EVec::FRONT ? "FRONT" :
|
||||
target.face == EVec::BACK ? "BACK" :
|
||||
"NONE" ;
|
||||
|
||||
str << "Pointing At: " << game->getDefs().blockFromId(world.l()->getActiveDimension()->getBlock(thing.pos)).identifier << std::endl;
|
||||
str << "Pointed Position: " << vecToString(thing.pos) << std::endl;
|
||||
str << "Pointing At: " << targetedBlockDef.identifier << " [" << targetedBlockDef.index << "]" << std::endl;
|
||||
str << "Pointed Position: " << vecToString(target.pos) << std::endl;
|
||||
str << "Pointed Face: " << face << std::endl;
|
||||
}
|
||||
else {
|
||||
|
@ -157,15 +156,10 @@ void DebugGui::update(std::shared_ptr<LocalPlayer> player, double fps, int /*chu
|
|||
get<GuiText>("dataText")->setText(str.str());
|
||||
}
|
||||
|
||||
{ //Crosshair Text
|
||||
Target target = player->getPointedThing();
|
||||
|
||||
std::ostringstream crossText;
|
||||
if (target.type == Target::Type::BLOCK) {
|
||||
crossText << game->getDefs().blockFromId(world.l()->getActiveDimension()->getBlock(target.pos)).name
|
||||
<< " (" << game->getDefs().blockFromId(world.l()->getActiveDimension()->getBlock(target.pos)).identifier << ")" << std::endl;
|
||||
}
|
||||
get<GuiText>("crosshairText")->setText(crossText.str());
|
||||
{ // Crosshair Text
|
||||
if (target.type == Target::Type::BLOCK) get<GuiText>("crosshairText")->setText(targetedBlockDef.name + " (" +
|
||||
targetedBlockDef.identifier + ") [" + std::to_string(targetedBlockDef.index) + "]");
|
||||
else get<GuiText>("crosshairText")->setText("");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,7 +167,7 @@ void DebugGui::bufferResized(glm::vec2 bufferSize) {
|
|||
positionElements(bufferSize);
|
||||
}
|
||||
|
||||
//0 = All, 1 = None, 2 = FPS
|
||||
// 0 = All, 1 = None, 2 = FPS
|
||||
void DebugGui::changeVisibilityState(int state) {
|
||||
displayMode = state;
|
||||
|
||||
|
|
|
@ -11,11 +11,12 @@
|
|||
#include "../../def/LocalDefinitionAtlas.h"
|
||||
#include "../../net/client/ClientNetworkInterpreter.h"
|
||||
|
||||
LocalInventoryRefs::LocalInventoryRefs(SubgamePtr game, ClientNetworkInterpreter& net) : InventoryRefs(game), net(net) {}
|
||||
|
||||
void LocalInventoryRefs::init() {
|
||||
LocalInventoryRefs::LocalInventoryRefs(SubgamePtr game, ClientNetworkInterpreter& net) : InventoryRefs(game), net(net) {
|
||||
createInventory("current_player");
|
||||
getInventory("current_player")->createList("cursor", 1, 1);
|
||||
}
|
||||
|
||||
void LocalInventoryRefs::init() {
|
||||
watch("current_player", "cursor");
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ GameScene::GameScene(ClientState& state) : Scene(state),
|
|||
r.sendTo(state.connection.getPeer(), PacketChannel::CONNECT);
|
||||
|
||||
world.l()->connect();
|
||||
game.l()->initApi(world, state);
|
||||
if (world.l()->initPlayer()) game.l()->loadPlayer(world.l()->getPlayer());
|
||||
game .l()->init(world, world.l()->getPlayer(), state);
|
||||
world.l()->updatePlayerDimension();
|
||||
|
||||
state.renderer.window.addResizeCallback("gamescene", Util::bind_this(&debugGui, &DebugGui::bufferResized));
|
||||
state.renderer.setClearColor(148, 194, 240);
|
||||
|
|
|
@ -13,11 +13,11 @@
|
|||
#include "../../../world/chunk/Chunk.h"
|
||||
#include "../../../net/client/ClientNetworkInterpreter.h"
|
||||
|
||||
LocalPlayer::LocalPlayer(SubgamePtr game, DimensionPtr dim, Renderer &renderer) :
|
||||
Player(game, dim),
|
||||
LocalPlayer::LocalPlayer(SubgamePtr game, LocalWorld& world, DimensionPtr dim, Renderer &renderer) :
|
||||
Player(game, world, dim),
|
||||
renderer(renderer),
|
||||
wireframe({1, 1, 1}),
|
||||
gameGui(dim->getWorld().getRefs().l(), renderer.window.getSize(), game.l(), renderer) {
|
||||
gameGui(world.getRefs().l(), renderer.window.getSize(), game.l(), renderer) {
|
||||
|
||||
handItemModel.parent = &handModel;
|
||||
|
||||
|
@ -43,17 +43,17 @@ void LocalPlayer::update(Input &input, double delta, glm::vec2 mouseDelta) {
|
|||
|
||||
void LocalPlayer::assertField(Packet packet) {
|
||||
packet.type = PacketType::THIS_PLAYER_INFO;
|
||||
static_cast<LocalWorld&>(dim->getWorld()).getNet().sendPacket(packet, PacketChannel::INTERACT);
|
||||
static_cast<LocalWorld&>(world).getNet().sendPacket(packet, PacketChannel::INTERACT);
|
||||
}
|
||||
|
||||
void LocalPlayer::handleAssertion(Deserializer &d) {
|
||||
while (!d.atEnd()) {
|
||||
switch (static_cast<NetField>(d.read<unsigned int>())) {
|
||||
switch (d.readE<NetField>()) {
|
||||
case NetField::ID: id = d.read<unsigned int>(); break;
|
||||
case NetField::POS: setPos(d.read<glm::vec3>()); break;
|
||||
case NetField::VEL: setVel(d.read<glm::vec3>()); break;
|
||||
case NetField::PITCH: setPitch(d.read<float>()); break;
|
||||
case NetField::YAW: setYaw(d.read<float>()); break;
|
||||
case NetField::PITCH: setPitch(d.read<float>()); break;
|
||||
case NetField::FLYING: setFlying(d.read<bool>()); break;
|
||||
|
||||
case NetField::HAND_INV: setHandList(d.read<std::string>()); break;
|
||||
|
@ -80,13 +80,13 @@ void LocalPlayer::setLookOffset(glm::vec3 eyeOffset, bool assert) {
|
|||
|
||||
void LocalPlayer::setHandList(const std::string &list, bool assert) {
|
||||
Player::setHandList(list, assert);
|
||||
dim->getWorld().getRefs().l()->setHandList(list);
|
||||
world.getRefs().l()->setHandList(list);
|
||||
updateWieldAndHandItems();
|
||||
}
|
||||
|
||||
void LocalPlayer::setWieldList(const std::string& list, bool assert) {
|
||||
Player::setWieldList(list, false);
|
||||
dim->getWorld().getRefs().l()->setWieldList(list);
|
||||
world.getRefs().l()->setWieldList(list);
|
||||
setWieldIndex(wieldIndex);
|
||||
updateWieldAndHandItems();
|
||||
if (assert) assertField(Serializer().append(
|
||||
|
@ -94,13 +94,18 @@ void LocalPlayer::setWieldList(const std::string& list, bool assert) {
|
|||
}
|
||||
|
||||
void LocalPlayer::setWieldIndex(unsigned short index, bool assert) {
|
||||
auto wieldList = dim->getWorld().getRefs().l()->getWieldList();
|
||||
auto wieldList = world.getRefs().l()->getWieldList();
|
||||
wieldIndex = index % std::max((wieldList ? wieldList->getLength() : 1), 1);
|
||||
updateWieldAndHandItems();
|
||||
if (assert) assertField(Serializer().append(
|
||||
static_cast<unsigned int>(NetField::WIELD_INDEX)).append(index).packet());
|
||||
}
|
||||
|
||||
void LocalPlayer::setDimension(DimensionPtr dim) {
|
||||
Player::setDimension(dim);
|
||||
static_cast<LocalWorld&>(world).setActiveDimension(dim);
|
||||
}
|
||||
|
||||
//
|
||||
// UI Related
|
||||
//
|
||||
|
@ -139,7 +144,7 @@ InventoryPtr LocalPlayer::getInventory() {
|
|||
return dim->getWorld().getRefs()->getInventory("current_player");
|
||||
}
|
||||
|
||||
Target& LocalPlayer::getPointedThing() {
|
||||
Target& LocalPlayer::getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
|
@ -312,7 +317,7 @@ void LocalPlayer::findPointedThing(Input &input) {
|
|||
auto face = sBox.intersects(rayEnd, roundedPos);
|
||||
|
||||
if (face != EVec::NONE) {
|
||||
target = Target(roundedPos, face);
|
||||
target = Target(dim, roundedPos, face);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -353,8 +358,8 @@ void LocalPlayer::interact(Input& input, double delta) {
|
|||
}
|
||||
|
||||
void LocalPlayer::updateWieldAndHandItems() {
|
||||
auto handList = dim->getWorld().getRefs().l()->getHandList();
|
||||
auto wieldList = dim->getWorld().getRefs().l()->getWieldList();
|
||||
auto handList = world.getRefs().l()->getHandList();
|
||||
auto wieldList = world.getRefs().l()->getWieldList();
|
||||
|
||||
handItem = handList && handList->getLength() > 0 ? handList->getStack(0).id : 0;
|
||||
wieldItem = wieldList && wieldList->getLength() > wieldIndex ? wieldList->getStack(wieldIndex).id : 0;
|
||||
|
|
|
@ -23,7 +23,7 @@ class LocalPlayer : public virtual DrawableEntity, public Player {
|
|||
public:
|
||||
enum class PlayerControl { FORWARD, LEFT, BACKWARD, RIGHT, JUMP, MOD1, MOD2 };
|
||||
|
||||
LocalPlayer(SubgamePtr game, DimensionPtr dim, Renderer &renderer);
|
||||
LocalPlayer(SubgamePtr game, LocalWorld& world, DimensionPtr dim, Renderer &renderer);
|
||||
|
||||
void update(Input &input, double delta, glm::vec2 mouseDelta);
|
||||
virtual void assertField(Packet packet) override;
|
||||
|
@ -36,6 +36,7 @@ public:
|
|||
virtual void setHandList(const std::string& list, bool assert = false) override;
|
||||
virtual void setWieldList(const std::string& list, bool assert = false) override;
|
||||
virtual void setWieldIndex(unsigned short index, bool assert = false) override;
|
||||
virtual void setDimension(DimensionPtr dim) override;
|
||||
|
||||
bool isInMenu();
|
||||
void showMenu(std::shared_ptr<LuaGuiElement> root);
|
||||
|
@ -45,7 +46,7 @@ public:
|
|||
std::shared_ptr<LuaGuiElement> getHud();
|
||||
void setHudVisible(bool hudVisible);
|
||||
|
||||
Target& getPointedThing();
|
||||
Target& getTarget();
|
||||
|
||||
void draw(Renderer& renderer) override;
|
||||
void drawHud(Renderer& renderer);
|
||||
|
|
|
@ -14,16 +14,17 @@ LocalWorld::LocalWorld(SubgamePtr game, ServerConnection& conn, Renderer& render
|
|||
renderer(renderer),
|
||||
net(conn, *this),
|
||||
refs(std::make_shared<LocalInventoryRefs>(game, net)),
|
||||
worldGenStream(std::make_shared<WorldInterpolationStream>(55, game)) {}
|
||||
worldGenStream(std::make_shared<WorldInterpolationStream>(55, game)),
|
||||
player(std::make_shared<LocalPlayer>(game, *this, DimensionPtr(nullptr), renderer)) {}
|
||||
|
||||
void LocalWorld::connect() {
|
||||
net.init(Util::bind_this(&(*refs), &LocalInventoryRefs::packetReceived));
|
||||
refs->init();
|
||||
}
|
||||
|
||||
bool LocalWorld::initPlayer() {
|
||||
bool LocalWorld::updatePlayerDimension() {
|
||||
if (defaultDimension.empty()) return false;
|
||||
player = PlayerPtr(std::make_shared<LocalPlayer>(game, getDefaultDimension(), renderer));
|
||||
player->setDimension(getDefaultDimension());
|
||||
activeDimension = getDefaultDimension().l();
|
||||
return true;
|
||||
}
|
||||
|
@ -83,6 +84,10 @@ DimensionPtr LocalWorld::getActiveDimension() {
|
|||
return activeDimension;
|
||||
}
|
||||
|
||||
void LocalWorld::setActiveDimension(DimensionPtr dim) {
|
||||
this->activeDimension = dim.l();
|
||||
}
|
||||
|
||||
ClientNetworkInterpreter& LocalWorld::getNet() {
|
||||
return net;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
LocalWorld(SubgamePtr game, ServerConnection& conn, Renderer& window);
|
||||
|
||||
void connect();
|
||||
bool initPlayer();
|
||||
bool updatePlayerDimension();
|
||||
void update(double delta) override;
|
||||
|
||||
void handleWorldPacket(std::unique_ptr<PacketView> p);
|
||||
|
@ -31,6 +31,7 @@ public:
|
|||
virtual DimensionPtr createDimension(const std::string& identifier) override;
|
||||
|
||||
DimensionPtr getActiveDimension();
|
||||
void setActiveDimension(DimensionPtr);
|
||||
|
||||
PlayerPtr getPlayer();
|
||||
virtual InventoryRefsPtr getRefs() override;
|
||||
|
|
|
@ -88,6 +88,11 @@ void Player::setWieldIndex(unsigned short index, bool assert) {
|
|||
if (assert) assertField(Serializer().append(NetField::WIELD_INDEX).append(index).packet());
|
||||
}
|
||||
|
||||
void Player::setDimension(DimensionPtr dim) {
|
||||
this->dim = dim;
|
||||
}
|
||||
|
||||
|
||||
DimensionPtr Player::getDimension() {
|
||||
return dim;
|
||||
}
|
||||
}
|
|
@ -25,8 +25,8 @@ public:
|
|||
ID, POS, VEL, PITCH, YAW, LOOK_OFF, FLYING,
|
||||
HAND_INV, WIELD_INV, WIELD_INDEX };
|
||||
|
||||
Player(SubgamePtr game, DimensionPtr dim, unsigned int id = 0) :
|
||||
game(game), dim(dim), id(id), lookOffset(0, 1.65, 0) {
|
||||
Player(SubgamePtr game, World& world, DimensionPtr dim, unsigned int id = 0) :
|
||||
game(game), world(world), dim(dim), id(id), lookOffset(0, 1.65, 0) {
|
||||
collision = {{-0.3, 0, -0.3}, {0.3, 1.8, 0.3}};
|
||||
}
|
||||
|
||||
|
@ -57,8 +57,10 @@ public:
|
|||
virtual unsigned short getWieldIndex();
|
||||
virtual void setWieldIndex(unsigned short index, bool assert = false);
|
||||
|
||||
virtual InventoryPtr getInventory() = 0;
|
||||
DimensionPtr getDimension();
|
||||
virtual void setDimension(DimensionPtr dim);
|
||||
|
||||
virtual InventoryPtr getInventory() = 0;
|
||||
|
||||
virtual void handleAssertion(Deserializer& d) = 0;
|
||||
protected:
|
||||
|
@ -66,6 +68,7 @@ protected:
|
|||
|
||||
unsigned int id = 0;
|
||||
|
||||
World& world;
|
||||
SubgamePtr game;
|
||||
DimensionPtr dim;
|
||||
|
||||
|
|
|
@ -33,20 +33,16 @@
|
|||
|
||||
LocalLuaParser::LocalLuaParser(LocalSubgame& game): LuaParser(game), keybinds(this) {}
|
||||
|
||||
void LocalLuaParser::init(WorldPtr world, ClientState& state) {
|
||||
void LocalLuaParser::init(WorldPtr world, PlayerPtr player, ClientState& state) {
|
||||
lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::math, sol::lib::table);
|
||||
|
||||
loadApi(world);
|
||||
handler.executeMods(std::bind(&LocalLuaParser::runFileSandboxed, this, std::placeholders::_1));
|
||||
state.renderer.window.input.setCallback(std::bind(&LuaKeybindHandler::keybindHandler, &keybinds, std::placeholders::_1, std::placeholders::_2));
|
||||
loadApi(world, player);
|
||||
handler.executeMods(Util::bind_this(this, &LocalLuaParser::runFileSandboxed));
|
||||
state.renderer.window.input.setCallback(Util::bind_this(&keybinds, &LuaKeybindHandler::keybindHandler));
|
||||
|
||||
registerDefs();
|
||||
}
|
||||
|
||||
void LocalLuaParser::loadPlayer(PlayerPtr player) {
|
||||
core["player"] = Api::Usertype::LocalPlayer(player);
|
||||
}
|
||||
|
||||
void LocalLuaParser::update(double delta) {
|
||||
this->delta += delta;
|
||||
while (this->delta > static_cast<double>(UPDATE_STEP)) {
|
||||
|
@ -60,7 +56,7 @@ LocalModHandler& LocalLuaParser::getHandler() {
|
|||
return handler;
|
||||
}
|
||||
|
||||
void LocalLuaParser::loadApi(WorldPtr world) {
|
||||
void LocalLuaParser::loadApi(WorldPtr world, PlayerPtr player) {
|
||||
//Create Zepha Table
|
||||
core = lua.create_table();
|
||||
lua["zepha"] = core;
|
||||
|
@ -79,6 +75,7 @@ void LocalLuaParser::loadApi(WorldPtr world) {
|
|||
Api::Usertype::InventoryList::bind(Api::State::CLIENT, lua, core);
|
||||
|
||||
core["client"] = true;
|
||||
core["player"] = Api::Usertype::LocalPlayer(player);
|
||||
|
||||
// Modules
|
||||
modules.emplace_back(std::make_unique<Api::Module::Time>(Api::State::CLIENT, lua, core));
|
||||
|
|
|
@ -18,14 +18,13 @@ class LocalSubgame;
|
|||
class LocalLuaParser : public LuaParser {
|
||||
public:
|
||||
explicit LocalLuaParser(LocalSubgame& game);
|
||||
void init(WorldPtr world, ClientState& state);
|
||||
void loadPlayer(PlayerPtr player);
|
||||
void init(WorldPtr world, PlayerPtr player, ClientState& state);
|
||||
|
||||
void update(double delta) override;
|
||||
|
||||
LocalModHandler& getHandler();
|
||||
private:
|
||||
void loadApi(WorldPtr world);
|
||||
void loadApi(WorldPtr world, PlayerPtr player);
|
||||
void registerDefs();
|
||||
|
||||
virtual sol::protected_function_result errorCallback(sol::protected_function_result r) const override;
|
||||
|
|
|
@ -80,7 +80,7 @@ void ServerLuaParser::playerDisconnected(std::shared_ptr<ServerPlayer> player) {
|
|||
for (auto& pair : core.get<sol::table>("players")) {
|
||||
auto& p = pair.second.as<Api::Usertype::ServerPlayer>();
|
||||
if (p.get_id() == player->getId()) {
|
||||
safe_function(core["trigger"], "player_disconnect", p);
|
||||
safe_function(core["trigger"], "player_leave", p);
|
||||
|
||||
core.get<sol::table>("players")[pair.first] = sol::nil;
|
||||
break;
|
||||
|
|
|
@ -41,7 +41,9 @@ void Api::Module::Register::bind() {
|
|||
|
||||
void Api::Module::Register::createRegisterFn(const std::string& name, const std::string& table) {
|
||||
std::string tableName = "registered_" + (table.empty() ? name + "s" : table);
|
||||
|
||||
core[tableName] = lua.create_table();
|
||||
|
||||
core.set_function("register_" + name, [=](sol::this_environment env, std::string identifier, sol::table data)
|
||||
{ registerFn(tableName, static_cast<sol::environment>(env), identifier, data); });
|
||||
}
|
||||
|
|
|
@ -60,6 +60,10 @@ Api::Usertype::Dimension Api::Usertype::ServerPlayer::get_dimension() {
|
|||
return Dimension(player->getDimension());
|
||||
}
|
||||
|
||||
void Api::Usertype::ServerPlayer::set_dimension(const std::string& identifier) {
|
||||
player->setDimension(player->getDimension()->getWorld().getDimension(identifier));
|
||||
}
|
||||
|
||||
sol::object Api::Usertype::ServerPlayer::get_hand_list(sol::this_state s) {
|
||||
auto listStr = player->getHandList();
|
||||
if (listStr.empty()) return sol::nil;
|
||||
|
@ -115,7 +119,7 @@ bool Api::Usertype::ServerPlayer::get_flying() {
|
|||
return player->isFlying();
|
||||
}
|
||||
|
||||
void Api::Usertype::ServerPlayer::bind(State state, sol::state &lua, sol::table &core) {
|
||||
void Api::Usertype::ServerPlayer::bind(State, sol::state &lua, sol::table &core) {
|
||||
lua.new_usertype<ServerPlayer>("Player",
|
||||
"get_id", &ServerPlayer::get_id,
|
||||
"get_pos", &ServerPlayer::get_pos,
|
||||
|
@ -139,6 +143,7 @@ void Api::Usertype::ServerPlayer::bind(State state, sol::state &lua, sol::table
|
|||
"get_wield_stack", &ServerPlayer::get_wield_stack,
|
||||
|
||||
"get_dimension", &ServerPlayer::get_dimension,
|
||||
"set_dimension", &ServerPlayer::set_dimension,
|
||||
|
||||
"pos", sol::property(&ServerPlayer::get_pos, &ServerPlayer::set_pos),
|
||||
"block_pos", sol::property(&ServerPlayer::get_block_pos, &ServerPlayer::set_pos),
|
||||
|
@ -171,7 +176,7 @@ void Api::Usertype::LocalPlayer::set_hud(std::shared_ptr<LuaGuiElement> hud) {
|
|||
player.l()->setHud(hud);
|
||||
}
|
||||
|
||||
void Api::Usertype::LocalPlayer::bind(State state, sol::state &lua, sol::table &core) {
|
||||
void Api::Usertype::LocalPlayer::bind(State, sol::state &lua, sol::table &core) {
|
||||
lua.new_usertype<LocalPlayer>("Player",
|
||||
"get_id", &LocalPlayer::get_id,
|
||||
"get_pos", &LocalPlayer::get_pos,
|
||||
|
|
|
@ -45,7 +45,9 @@ namespace Api::Usertype {
|
|||
void set_wield_list(sol::optional<sol::object> list);
|
||||
|
||||
Inventory get_inventory();
|
||||
|
||||
Dimension get_dimension();
|
||||
void set_dimension(const std::string& identifier);
|
||||
|
||||
unsigned int get_wield_index();
|
||||
void set_wield_index(unsigned int index);
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
Api::Usertype::Target::Target(const ::Target &target) :
|
||||
pos(target.pos),
|
||||
type(target.type),
|
||||
dim(Dimension(target.dim)),
|
||||
pos_above(target.getAbovePos()) {}
|
||||
|
||||
std::string Api::Usertype::Target::getType() {
|
||||
|
@ -22,6 +23,7 @@ void Api::Usertype::Target::bind(State, sol::state &lua, sol::table &core) {
|
|||
"type", sol::property(&Target::getType),
|
||||
|
||||
"pos", sol::readonly(&Target::pos),
|
||||
"dim", sol::readonly(&Target::dim),
|
||||
"pos_above", sol::readonly(&Target::pos_above)
|
||||
);
|
||||
}
|
|
@ -7,6 +7,8 @@
|
|||
#include <glm/vec3.hpp>
|
||||
|
||||
#include "SubgameUsertype.h"
|
||||
|
||||
#include "Dimension.h"
|
||||
#include "../../world/Target.h"
|
||||
|
||||
namespace Api::Usertype {
|
||||
|
@ -18,6 +20,7 @@ namespace Api::Usertype {
|
|||
|
||||
::Target::Type type;
|
||||
|
||||
Dimension dim;
|
||||
glm::ivec3 pos;
|
||||
glm::ivec3 pos_above;
|
||||
|
||||
|
|
|
@ -21,6 +21,14 @@ public:
|
|||
return *this;
|
||||
};
|
||||
|
||||
template<typename E, typename = typename std::enable_if<std::is_enum<E>::value>::type> inline E readE() {
|
||||
return static_cast<E>(read<unsigned short>());
|
||||
};
|
||||
template<typename E, typename = typename std::enable_if<std::is_enum<E>::value>::type> inline Deserializer& readE(E& ref) {
|
||||
ref = static_cast<E>(read<unsigned short>());
|
||||
return *this;
|
||||
};
|
||||
|
||||
bool atEnd() {
|
||||
return ind >= len;
|
||||
};
|
||||
|
|
|
@ -68,7 +68,6 @@ void ClientNetworkInterpreter::receivedPacket(std::unique_ptr<PacketView> p) {
|
|||
serverSideChunkGens = p->d.read<unsigned int>(); break;
|
||||
|
||||
case PacketType::THIS_PLAYER_INFO:
|
||||
if (!world.getPlayer()) throw std::runtime_error("Received player info *before* the player was created.");
|
||||
world.getPlayer()->handleAssertion(p->d); break;
|
||||
|
||||
case PacketType::PLAYER_ENT_INFO:
|
||||
|
|
|
@ -51,19 +51,20 @@ void ServerClients::handleDisconnect(ENetEvent e) {
|
|||
}
|
||||
|
||||
void ServerClients::createPlayer(std::shared_ptr<ServerClient> client, DimensionPtr dimension) {
|
||||
auto player = std::make_shared<ServerPlayer>(*client, game, dimension);
|
||||
auto player = std::make_shared<ServerPlayer>(*client, dimension->getWorld(), game, dimension);
|
||||
player->getInventory()->createList("cursor", 1, 1);
|
||||
client->player = player;
|
||||
|
||||
players.push_back(player);
|
||||
game.s()->getParser().playerConnected(player);
|
||||
|
||||
player->setPos({0, -37, 0}, true);
|
||||
|
||||
Serializer()
|
||||
.appendE(Player::NetField::ID).append(player->getId())
|
||||
.appendE(Player::NetField::POS).append(player->getPos())
|
||||
.appendE(Player::NetField::PITCH).append(player->getPitch())
|
||||
.appendE(Player::NetField::YAW).append(player->getYaw())
|
||||
.packet(PacketType::THIS_PLAYER_INFO).sendTo(player->getPeer(), PacketChannel::ENTITY);
|
||||
.packet(PacketType::THIS_PLAYER_INFO).sendTo(player->getPeer(), PacketChannel::INTERACT);
|
||||
}
|
||||
|
||||
const std::shared_ptr<ServerClient> ServerClients::getClient(unsigned int id) const {
|
||||
|
|
|
@ -10,9 +10,9 @@
|
|||
#include "../world/ServerWorld.h"
|
||||
#include "../../../game/inventory/ServerInventoryRefs.h"
|
||||
|
||||
ServerPlayer::ServerPlayer(ServerClient& client, SubgamePtr game, DimensionPtr dim) :
|
||||
Player(game, dim, client.id), client(client),
|
||||
inventory(dim->getWorld().getRefs()->createInventory("player:" + std::to_string(id))) {}
|
||||
ServerPlayer::ServerPlayer(ServerClient& client, World& world, SubgamePtr game, DimensionPtr dim) :
|
||||
Player(game, world, dim, client.id), client(client),
|
||||
inventory(world.getRefs()->createInventory("player:" + std::to_string(id))) {}
|
||||
|
||||
void ServerPlayer::assertField(Packet packet) {
|
||||
packet.type = PacketType::THIS_PLAYER_INFO;
|
||||
|
@ -21,7 +21,7 @@ void ServerPlayer::assertField(Packet packet) {
|
|||
|
||||
void ServerPlayer::handleAssertion(Deserializer &d) {
|
||||
while (!d.atEnd()) {
|
||||
switch (static_cast<NetField>(d.read<unsigned int>())) {
|
||||
switch (d.readE<NetField>()) {
|
||||
case NetField::POS: setPos(d.read<glm::vec3>()); break;
|
||||
case NetField::VEL: setVel(d.read<glm::vec3>()); break;
|
||||
case NetField::PITCH: setPitch(d.read<float>()); break;
|
||||
|
|
|
@ -21,12 +21,13 @@ enum class NetPlayerField;
|
|||
|
||||
class ServerPlayer : public Player {
|
||||
public:
|
||||
ServerPlayer(ServerClient& client, SubgamePtr game, DimensionPtr dim);
|
||||
ServerPlayer(ServerClient& client, World& world, SubgamePtr game, DimensionPtr dim);
|
||||
|
||||
virtual void assertField(Packet packet) override;
|
||||
virtual void handleAssertion(Deserializer& d) override;
|
||||
|
||||
virtual void setPos(glm::vec3 pos, bool assert = false) override;
|
||||
|
||||
virtual InventoryPtr getInventory() override;
|
||||
|
||||
ENetPeer* getPeer();
|
||||
|
|
|
@ -39,6 +39,7 @@ class CovariantPtr {
|
|||
std::shared_ptr<B> b = nullptr;
|
||||
public:
|
||||
CovariantPtr() = default;
|
||||
CovariantPtr(std::nullptr_t) {};
|
||||
CovariantPtr(std::shared_ptr<B> ptr) : b(ptr) {}
|
||||
CovariantPtr(std::shared_ptr<L> ptr) : b(std::dynamic_pointer_cast<B>(ptr)) {}
|
||||
CovariantPtr(std::shared_ptr<S> ptr) : b(std::dynamic_pointer_cast<B>(ptr)) {}
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
|
||||
#include "../def/item/SelectionBox.h"
|
||||
|
||||
Target::Target(glm::ivec3 pos, EVec face) : type(Type::BLOCK), pos(pos), face(face) {}
|
||||
Target::Target(DimensionPtr dim, glm::ivec3 pos, EVec face) :
|
||||
type(Type::BLOCK), dim(dim), pos(pos), face(face) {}
|
||||
|
||||
glm::ivec3 Target::getAbovePos() const {
|
||||
return glm::ivec3(pos) + SelectionBox::faceToOffset(face);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "../util/Vec.h"
|
||||
#include "../util/CovariantPtr.h"
|
||||
|
||||
class Target {
|
||||
public:
|
||||
|
@ -12,7 +13,7 @@ public:
|
|||
|
||||
Target() = default;
|
||||
Target(const Target &o) = default;
|
||||
Target(glm::ivec3 pos, EVec face);
|
||||
Target(DimensionPtr dim, glm::ivec3 pos, EVec face);
|
||||
|
||||
glm::ivec3 getAbovePos() const;
|
||||
|
||||
|
@ -20,5 +21,6 @@ public:
|
|||
Type type = Target::Type::NOTHING;
|
||||
|
||||
glm::vec3 pos {};
|
||||
DimensionPtr dim;
|
||||
EVec face = EVec::NONE;
|
||||
};
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
runfile(_PATH .. "chest")
|
||||
|
||||
local function stacker_action(pos)
|
||||
local function stacker_action(dim, pos)
|
||||
local v = V(0, 1, 0)
|
||||
while zepha.get_block(pos + v) == "@aurailus:crazyblocks:stacker" do v = v + V(0, 1, 0) end
|
||||
zepha.set_block(pos + v, "@aurailus:crazyblocks:stacker")
|
||||
while dim:get_block(pos + v) == "@aurailus:crazyblocks:stacker" do v = v + V(0, 1, 0) end
|
||||
dim:set_block(pos + v, "@aurailus:crazyblocks:stacker")
|
||||
end
|
||||
|
||||
local function inventory_action()
|
||||
|
@ -27,9 +27,10 @@ zepha.register_block("@aurailus:crazyblocks:inventory", {
|
|||
|
||||
if zepha.server then
|
||||
zepha.bind("new_player", function(player)
|
||||
player:get_inventory():get_list("hot_wheel_1"):add_stack({"zeus:default:dirt", 32})
|
||||
player:get_inventory():get_list("hot_wheel_1"):add_stack({"@aurailus:crazyblocks:stacker", 1})
|
||||
player:get_inventory():get_list("hot_wheel_1"):add_stack({"@aurailus:crazyblocks:inventory", 1})
|
||||
player:get_inventory():get_list("hot_wheel_1"):add_stack({"@aurailus:crazyblocks:box", 1})
|
||||
local hw = player:get_inventory():get_list("hot_wheel_1")
|
||||
hw:add_stack({"zeus:default:dirt", 32})
|
||||
hw:add_stack({"@aurailus:crazyblocks:stacker", 1})
|
||||
hw:add_stack({"@aurailus:crazyblocks:inventory", 1})
|
||||
hw:add_stack({"@aurailus:crazyblocks:box", 1})
|
||||
end)
|
||||
end
|
|
@ -5,9 +5,6 @@ if zepha.server then
|
|||
end
|
||||
|
||||
if zepha.client then
|
||||
-- Delay so that we can access player.
|
||||
zepha.after(function()
|
||||
runfile(_PATH .. "ui")
|
||||
runfile(_PATH .. "keys")
|
||||
end, 0)
|
||||
runfile(_PATH .. "ui")
|
||||
runfile(_PATH .. "keys")
|
||||
end
|
||||
|
|
|
@ -14,6 +14,6 @@ zepha.register_block(":dirt", {
|
|||
|
||||
yields = "zeus:default:dirt",
|
||||
|
||||
on_interact = function(pos) zepha.set_block(pos, "zeus:default:grass") end,
|
||||
on_interact_client = function(pos) zepha.set_block(pos, "zeus:default:grass") end
|
||||
on_interact = function(dim, pos) dim:set_block(pos, "zeus:default:grass") end,
|
||||
on_interact_client = function(dim, pos) dim:set_block(pos, "zeus:default:grass") end
|
||||
})
|
|
@ -1,4 +1,20 @@
|
|||
zepha.create_dimension("zeus:default")
|
||||
zepha.set_default_dimension("zeus:default")
|
||||
|
||||
zepha.create_dimension("zeus:second")
|
||||
zepha.create_dimension("zeus:second")
|
||||
|
||||
local v = false
|
||||
zepha.register_keybind("zeus:world:go_to_hell", {
|
||||
default = zepha.keys.enter,
|
||||
on_press = function()
|
||||
print("Here we go again")
|
||||
|
||||
if not v then
|
||||
zepha.player:set_dimension("zeus:second")
|
||||
v = true
|
||||
else
|
||||
zepha.player:set_dimension("zeus:default")
|
||||
v = false
|
||||
end
|
||||
end
|
||||
})
|
Loading…
Reference in New Issue