Create server side inventories, callbacks, seperate class & userdatas

* Create LocalInventoryRefs - unfinished
* Create WATCH_INV and UNWATCH_INV packet types
* Add player_join, player_connect, and player_disconnect callbacks
* Begin networked inventory support
master
Nicole Collings 2020-02-25 15:14:07 -08:00
parent e4de8f2556
commit 228e0e4d30
74 changed files with 1065 additions and 372 deletions

View File

@ -31,10 +31,10 @@ set(ZEPHA_SRC
game/scene/GameScene.h
game/graph/Renderer.cpp
game/graph/Renderer.h
lua/parser/LocalLuaParser.cpp
lua/parser/LocalLuaParser.h
game/hud/components/basic/GuiText.cpp
game/hud/components/basic/GuiText.h
lua/parser/LocalLuaParser.cpp
lua/parser/LocalLuaParser.h
game/hud/components/basic/GuiText.cpp
game/hud/components/basic/GuiText.h
game/hud/DebugGui.cpp
game/hud/DebugGui.h
game/scene/world/Player.cpp
@ -53,7 +53,7 @@ set(ZEPHA_SRC
server/Server.h
util/net/Packet.cpp
util/net/Packet.h
game/scene/net/ClientNetworkInterpreter.cpp
game/scene/net/ClientNetworkInterpreter.cpp
game/scene/net/ClientNetworkInterpreter.h
def/gen/MapGen.cpp
def/gen/MapGen.h
@ -61,15 +61,15 @@ set(ZEPHA_SRC
util/net/NetHandler.h
game/hud/GameGui.cpp
game/hud/GameGui.h
server/conn/ClientList.cpp
server/conn/ClientList.h
server/conn/ClientList.cpp
server/conn/ClientList.h
server/conn/ServerClient.h
util/net/PacketChannel.h
game/entity/engine/PlayerEntity.h
game/hud/components/basic/GuiRect.cpp
game/hud/components/basic/GuiRect.h
game/hud/components/basic/GuiGraph.cpp
game/hud/components/basic/GuiGraph.h
game/entity/engine/PlayerEntity.h
game/hud/components/basic/GuiRect.cpp
game/hud/components/basic/GuiRect.h
game/hud/components/basic/GuiGraph.cpp
game/hud/components/basic/GuiGraph.h
def/gen/MapGenJob.h
util/Interp.h
def/gen/NoiseSample.cpp
@ -93,32 +93,32 @@ set(ZEPHA_SRC
world/LocalDimension.cpp
world/LocalDimension.h
world/region/Region.h
game/entity/engine/WireframeEntity.cpp
game/entity/engine/WireframeEntity.h
game/entity/engine/WireframeEntity.cpp
game/entity/engine/WireframeEntity.h
def/item/SelectionBox.h
game/graph/drawable/Drawable.h
game/graph/drawable/DrawableGroup.cpp
game/graph/drawable/DrawableGroup.h
game/graph/window/Input.cpp
game/graph/window/Input.h
game/entity/engine/BlockCrackEntity.cpp
game/entity/engine/BlockCrackEntity.h
game/entity/engine/BlockCrackEntity.cpp
game/entity/engine/BlockCrackEntity.h
def/texture/TextureAtlas.cpp
def/texture/TextureAtlas.h
def/texture/AtlasRef.h
world/region/MapBlock.h
def/ClientGame.cpp
def/ClientGame.h
def/ClientGame.cpp
def/ClientGame.h
world/region/Region.cpp
world/region/MapBlock.cpp
util/Util.h
world/block/PointedThing.h
game/hud/components/compound/GuiLabelledGraph.cpp
game/hud/components/compound/GuiLabelledGraph.cpp
game/entity/engine/ParticleEntity.cpp
game/entity/engine/ParticleEntity.h
def/ServerGame.cpp
def/ServerGame.h
game/hud/components/compound/GuiLabelledGraph.cpp
game/hud/components/compound/GuiLabelledGraph.cpp
game/entity/engine/ParticleEntity.cpp
game/entity/engine/ParticleEntity.h
def/ServerGame.cpp
def/ServerGame.h
def/ServerDefinitionAtlas.cpp
def/ServerDefinitionAtlas.h
def/item/BlockDef.cpp
@ -127,8 +127,8 @@ set(ZEPHA_SRC
def/DefinitionAtlas.h
lua/LuaParser.cpp
lua/LuaParser.h
lua/parser/ServerLuaParser.cpp
lua/parser/ServerLuaParser.h
lua/parser/ServerLuaParser.cpp
lua/parser/ServerLuaParser.h
util/Log.h
lua/Callback.h
game/graph/shader/GuiUniforms.h
@ -145,10 +145,10 @@ set(ZEPHA_SRC
game/scene/net/ServerConnection.cpp
game/scene/net/ServerConnection.h
game/scene/world/MeshDetails.h
game/hud/components/GuiComponent.cpp
game/hud/components/GuiComponent.h
game/hud/components/basic/GuiContainer.cpp
game/hud/components/basic/GuiContainer.h
game/hud/components/GuiComponent.cpp
game/hud/components/GuiComponent.h
game/hud/components/basic/GuiContainer.cpp
game/hud/components/basic/GuiContainer.h
lua/LuaModFile.h
lua/LuaMod.h
lua/LuaModConfig.h
@ -176,7 +176,7 @@ set(ZEPHA_SRC
game/entity/AnimationState.cpp
game/entity/AnimationState.h
game/entity/AnimationSegment.h
lua/api/type/cLocalLuaEntity.h
lua/api/usertype/cLuaEntity.h
StartGame.h
game/graph/shader/SSAOShader.cpp
game/graph/shader/SSAOShader.h
@ -194,31 +194,31 @@ set(ZEPHA_SRC
def/item/CraftItemDef.cpp
def/item/CraftItemDef.h
lua/api/modules/add_entity.h
lua/api/type/LocalLuaEntity.cpp
lua/api/type/LocalLuaEntity.h
lua/api/class/LocalLuaEntity.cpp
lua/api/class/LocalLuaEntity.h
def/model/SerializedModel.h
server/asset/AssetType.h
def/model/ModelStore.h
lua/api/modules/remove_entity.h
lua/api/functions/update_entities.h
lua/api/functions/update_entities.h
lua/api/modules/register_keybind.h
lua/LuaInputManager.cpp
lua/LuaInputManager.h
lua/api/type/cLuaLocalPlayer.h
lua/api/type/LocalLuaPlayer.cpp
lua/api/type/LocalLuaPlayer.h
lua/api/usertype/cLocalPlayer.h
lua/api/class/LocalLuaPlayer.cpp
lua/api/class/LocalLuaPlayer.h
game/entity/Collidable.cpp
game/entity/Collidable.h
game/hud/components/compound/GuiInventoryList.cpp
game/hud/components/compound/GuiInventoryList.h
game/hud/components/basic/GuiInventoryItem.cpp
game/hud/components/basic/GuiInventoryItem.h
game/hud/components/compound/GuiInventoryList.cpp
game/hud/components/compound/GuiInventoryList.h
game/hud/components/basic/GuiInventoryItem.cpp
game/hud/components/basic/GuiInventoryItem.h
game/scene/world/InventoryList.cpp
game/scene/world/InventoryList.h
game/scene/world/ItemStack.h
server/LocalServerInstance.cpp
server/LocalServerInstance.h
game/hud/SerialGui.h
game/hud/SerialGui.h
util/Voronoi3D.cpp
util/Voronoi3D.h
def/gen/BiomeDef.cpp
@ -232,8 +232,8 @@ set(ZEPHA_SRC
world/Dimension.cpp
world/Dimension.h
def/texture/RawTexData.h
game/hud/components/compound/GuiImageButton.cpp
game/hud/components/compound/GuiImageButton.h
game/hud/components/compound/GuiImageButton.cpp
game/hud/components/compound/GuiImageButton.h
game/ClientState.cpp
game/scene/menu/Subgame.cpp
game/scene/menu/Subgame.h
@ -247,30 +247,30 @@ set(ZEPHA_SRC
game/hud/GameGuiBuilder.cpp
game/hud/GameGuiBuilder.h
lua/api/menu/mStartGame.h
lua/api/type/LuaInventoryList.cpp
lua/api/type/LuaInventoryList.h
lua/api/type/LuaInventory.cpp
lua/api/type/LuaInventory.h
lua/api/class/ServerLuaInventoryList.cpp
lua/api/class/ServerLuaInventoryList.h
lua/api/class/ServerLuaInventory.cpp
lua/api/class/ServerLuaInventory.h
game/scene/world/Inventory.cpp
game/scene/world/Inventory.h
lua/api/type/LuaItemStack.cpp
lua/api/type/LuaItemStack.h
lua/api/class/LuaItemStack.cpp
lua/api/class/LuaItemStack.h
game/scene/world/ItemStack.cpp
lua/api/type/cLuaInventory.h
lua/api/type/cLuaItemStack.h
game/hud/components/basic/GuiModel.cpp
game/hud/components/basic/GuiModel.h
lua/api/usertype/cInventoryRef.h
lua/api/usertype/cItemStack.h
game/hud/components/basic/GuiModel.cpp
game/hud/components/basic/GuiModel.h
server/world/ServerEntity.cpp
server/world/ServerEntity.h
lua/api/type/ServerLuaEntity.cpp
lua/api/type/ServerLuaEntity.h
lua/api/class/ServerLuaEntity.cpp
lua/api/class/ServerLuaEntity.h
game/scene/LuaErrorScene.cpp
game/scene/LuaErrorScene.h
lua/api/type/sServerLuaEntity.h
lua/api/usertype/sLuaEntity.h
util/net/Serializer.h
util/net/Deserializer.h
lua/api/type/ServerLocalLuaEntity.cpp
lua/api/type/ServerLocalLuaEntity.h
lua/api/class/ServerLocalLuaEntity.cpp
lua/api/class/ServerLocalLuaEntity.h
lua/api/modules/register_item.h
lua/api/modules/register_biome.h
lua/api/modules/delay.h
@ -286,15 +286,33 @@ set(ZEPHA_SRC
lua/register/RegisterBlocks.h
lua/register/RegisterItems.h
lua/register/RegisterKeybinds.h
lua/api/type/LocalLuaAnimationManager.cpp
lua/api/type/LocalLuaAnimationManager.h
lua/api/type/cLocalLuaAnimationManager.h
lua/api/class/LocalLuaAnimationManager.cpp
lua/api/class/LocalLuaAnimationManager.h
lua/api/usertype/cAnimationManager.h
game/scene/world/Schematic.cpp
game/scene/world/Schematic.h
lua/VenusParser.cpp
lua/VenusParser.h
lua/ErrorFormatter.cpp
lua/ErrorFormatter.h
util/RIE.h lua/api/type/ServerLuaPlayer.cpp lua/api/type/ServerLuaPlayer.h lua/api/type/sServerLuaPlayer.h lua/api/modules/register_on.h lua/api/functions/trigger_event.h lua/parser/ServerModHandler.cpp lua/parser/ServerModHandler.h lua/parser/LocalModHandler.cpp lua/parser/LocalModHandler.h)
util/RIE.h
lua/api/class/ServerLuaPlayer.cpp
lua/api/class/ServerLuaPlayer.h
lua/api/usertype/sServerPlayer.h
lua/api/modules/register_on.h
lua/api/functions/trigger_event.h
lua/parser/ServerModHandler.cpp
lua/parser/ServerModHandler.h
lua/parser/LocalModHandler.cpp
lua/parser/LocalModHandler.h
game/scene/net/NetPlayerField.h
game/scene/world/LocalInventoryRefs.cpp
game/scene/world/LocalInventoryRefs.h
game/scene/world/LocalInventory.cpp
game/scene/world/LocalInventory.h
lua/api/class/LocalLuaInventory.cpp
lua/api/class/LocalLuaInventory.h
lua/api/class/LocalLuaInventoryList.cpp
lua/api/class/LocalLuaInventoryList.h)
add_library (Zepha_Core ${ZEPHA_SRC})

View File

@ -2,9 +2,6 @@
// Created by aurailus on 13/08/19.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-static-cast-downcast"
#include "DefinitionAtlas.h"
ItemDef& DefinitionAtlas::fromId(unsigned int id) const {
@ -67,6 +64,4 @@ unsigned int DefinitionAtlas::size() {
DefinitionAtlas::~DefinitionAtlas() {
for (auto def : defs) delete def;
}
#pragma clang diagnostic pop
}

View File

@ -16,7 +16,7 @@ public:
ItemDef& fromId(unsigned int index) const;
ItemDef& fromStr(const std::string& identifier) const;
//Specializations that throw an error upon an invalid type.
//Specializations that throw an error upon an invalid usertype.
BlockDef& blockFromId(unsigned int index) const;
BlockDef& blockFromStr(const std::string& identifier) const;
CraftItemDef& craftItemFromId(unsigned int index) const;

View File

@ -5,14 +5,13 @@
#include "GameGui.h"
#include "components/compound/GuiInventoryList.h"
GameGui::GameGui(Inventory& inventory, InventoryList& hand, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer) :
GameGui::GameGui(LocalInventoryRefs& refs, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer) :
menuRoot(std::make_shared<GuiContainer>("__luaroot")),
handList(std::make_shared<GuiInventoryList>("hand")),
builder(inventory, hand, defs, menuRoot),
inventory(inventory),
builder(refs, defs, menuRoot),
renderer(renderer),
win(bufferSize),
hand(hand),
refs(refs),
defs(defs) {
auto crosshair = std::make_shared<GuiRect>("crosshair");
@ -26,7 +25,7 @@ GameGui::GameGui(Inventory& inventory, InventoryList& hand, glm::vec2 bufferSize
add(menuRoot);
handList->create({3, 3}, {}, {}, hand, hand, defs);
handList->create({3, 3}, {}, {}, refs.getHand(), refs.getHand(), defs);
add(handList);
}

View File

@ -18,7 +18,7 @@
class GameGui : public GuiContainer {
public:
explicit GameGui(Inventory& inventory, InventoryList& hand, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer);
explicit GameGui(LocalInventoryRefs& refs, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer);
void winResized(glm::ivec2 win);
void update(double delta) override;
@ -41,7 +41,6 @@ private:
GuiContainer builtIn = {};
GameGuiBuilder builder;
InventoryList& hand;
Inventory& inventory;
LocalInventoryRefs& refs;
};

View File

@ -23,7 +23,7 @@ std::shared_ptr<GuiComponent> GameGuiBuilder::createComponent(SerialGui::Elem &d
switch (Util::hash(data.type.c_str())) {
default: break;
case Util::hash("inventory"): {
c = GuiInventoryList::fromSerialized(data, game, bounds, inventory, hand);
c = GuiInventoryList::fromSerialized(data, game, bounds, refs);
break;
}
}

View File

@ -5,17 +5,16 @@
#pragma once
#include "GuiBuilder.h"
#include "../scene/world/InventoryList.h"
#include "../scene/world/Inventory.h"
#include "../scene/world/LocalInventoryRefs.h"
class GameGuiBuilder : public GuiBuilder {
public:
GameGuiBuilder(Inventory& inventory, InventoryList& hand, ClientGame& defs, std::shared_ptr<GuiContainer> root) :
inventory(inventory), hand(hand), GuiBuilder(defs, root) {};
GameGuiBuilder(LocalInventoryRefs& refs, ClientGame& defs, std::shared_ptr<GuiContainer> root) :
refs(refs), GuiBuilder(defs, root) {};
std::shared_ptr<GuiComponent> createComponent(SerialGui::Elem& data, glm::ivec2 bounds) override;
private:
Inventory& inventory;
InventoryList& hand;
LocalInventoryRefs& refs;
};

View File

@ -117,7 +117,7 @@ void GuiBuilder::recursivelyCreate(std::vector<SerialGui::Elem> components, std:
std::shared_ptr<GuiComponent> component = createComponent(data, bounds);
if (component == nullptr) continue;
parent->add(component);
recursivelyCreate(data.children, component, bounds);
recursivelyCreate(data.children, component, component->getScale());
}
}

View File

@ -5,12 +5,12 @@
#include "GuiInventoryList.h"
#include "../basic/GuiInventoryItem.h"
#include "../../../scene/world/LocalInventoryList.cpp"
#include "../../../../def/texture/Font.h"
GuiInventoryList::GuiInventoryList(const std::string &key) : GuiContainer(key) {}
std::shared_ptr<GuiInventoryList> GuiInventoryList::fromSerialized(SerialGui::Elem s, ClientGame &game,
glm::ivec2 bounds, Inventory& inventory, InventoryList& hand) {
glm::ivec2 bounds, LocalInventoryRefs& refs) {
glm::vec2 pos = SerialGui::deserializeToken<glm::vec2>(s.tokens, "position", bounds);
glm::vec2 offset = SerialGui::deserializeToken<glm::vec2>(s.tokens, "position_anchor");
@ -21,41 +21,32 @@ std::shared_ptr<GuiInventoryList> GuiInventoryList::fromSerialized(SerialGui::El
std::string source = s.tokens["source"];
std::string list = s.tokens["list"];
if (source != "current_player") {
std::cerr << "Invalid source specified, " << source << std::endl;
return nullptr;
}
if (!inventory[list]) {
std::cerr << "Invalid list specified, " << list << std::endl;
return nullptr;
}
auto invList = inventory[list];
auto invList = refs.getList(source, list);
auto inv = std::make_shared<GuiInventoryList>(s.key);
inv->create(glm::vec2(SerialGui::SCALE_MODIFIER), padding * SerialGui::SCALE_MODIFIER,
slotspc * SerialGui::SCALE_MODIFIER, *invList, hand, game);
slotspc * SerialGui::SCALE_MODIFIER, invList, refs.getHand(), game);
inv->setPos(pos);
return inv;
}
void GuiInventoryList::create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 innerPadding, InventoryList &list, InventoryList& hand, ClientGame &defs) {
this->list = &list;
this->hand = &hand;
void GuiInventoryList::create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 innerPadding,
std::shared_ptr<LocalInventoryList> list, std::shared_ptr<LocalInventoryList> hand, ClientGame& defs) {
this->list = list;
this->hand = hand;
this->defs = &defs;
this->scale = scale;
this->padding = padding;
this->innerPadding = innerPadding;
this->hitbox = glm::ivec2 {
padding.x + list.getWidth() * (innerPadding.x*scale.x),
padding.y + (list.getLength() / list.getWidth()) * (innerPadding.y*scale.y)
padding.x + list->getWidth() * (innerPadding.x*scale.x),
padding.y + (list->getLength() / list->getWidth()) * (innerPadding.y*scale.y)
};
drawContents();
list.setGuiCallback(std::bind(&GuiInventoryList::drawContents, this));
list->setGuiCallback(std::bind(&GuiInventoryList::drawContents, this));
hoverRect->create({}, {}, {1, 1, 1, 0.1});

View File

@ -10,7 +10,8 @@
#include "../../SerialGui.h"
#include "../../../../def/ClientGame.h"
#include "../../../scene/world/Inventory.h"
#include "../../../scene/world/InventoryList.h"
#include "../../../scene/world/LocalInventoryList.h"
#include "../../../scene/world/LocalInventoryRefs.h"
class GuiInventoryList : public GuiContainer {
public:
@ -19,9 +20,10 @@ public:
~GuiInventoryList() override;
static std::shared_ptr<GuiInventoryList> fromSerialized(SerialGui::Elem s, ClientGame &game,
glm::ivec2 bounds, Inventory& inventory, InventoryList& hand);
glm::ivec2 bounds, LocalInventoryRefs& refs);
void create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 innerPadding, InventoryList& list, InventoryList& hand, ClientGame& defs);
void create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 innerPadding,
std::shared_ptr<LocalInventoryList> list, std::shared_ptr<LocalInventoryList> hand, ClientGame& defs);
void setHoverCallback(const callback& hoverCallback) override;
void setLeftClickCallback(const callback& leftClickCallback) override;
@ -34,8 +36,7 @@ public:
private:
std::shared_ptr<GuiRect> hoverRect = std::make_shared<GuiRect>("hover_rect");
InventoryList* list;
InventoryList* hand;
ClientGame* defs;
std::shared_ptr<LocalInventoryList> list, hand;
glm::ivec2 innerPadding;
ClientGame* defs;
};

View File

@ -5,19 +5,19 @@
#include "GameScene.h"
GameScene::GameScene(ClientState& state) : Scene(state),
defs(state.defs),
server(state.connection, defs, player),
world(defs, &playerPos, &server),
player(world, defs, state.renderer),
debugGui(state.renderer.window.getSize(), defs) {
game(state.defs),
refs(game.defs, net),
world(game, &playerPos, &net),
net(state.connection, game, player),
player(world, game, state.renderer, refs),
debugGui(state.renderer.window.getSize(), game) {
state.renderer.setClearColor(148, 194, 240);
state.renderer.window.lockMouse(true);
defs.init(world, player);
game.init(world, player);
world.init();
server.init(&world);
net.init(&world);
Packet r(PacketType::CONNECT_DATA_RECVD);
r.sendTo(state.connection.getPeer(), PacketChannel::CONNECT);
@ -28,9 +28,9 @@ GameScene::GameScene(ClientState& state) : Scene(state),
}
void GameScene::update() {
defs.update(state.deltaTime, state.renderer.window.keys);
defs.textures.update();
server.update();
game.update(state.deltaTime, state.renderer.window.keys);
game.textures.update();
net.update();
Window& window = state.renderer.window;
@ -40,11 +40,11 @@ void GameScene::update() {
for (auto entity : entities) entity->update(state.deltaTime);
for (auto &chunkPacket : server.chunkPackets) world.loadChunkPacket(std::move(chunkPacket));
server.chunkPackets.clear();
for (auto &chunkPacket : net.chunkPackets) world.loadChunkPacket(std::move(chunkPacket));
net.chunkPackets.clear();
debugGui.update(player, world, defs, state.fps, world.getMeshChunkCount(), drawCalls, server.serverSideChunkGens, server.recvPackets);
server.recvPackets = 0;
debugGui.update(player, world, game, state.fps, world.getMeshChunkCount(), drawCalls, net.serverSideChunkGens, net.recvPackets);
net.recvPackets = 0;
world.update(state.deltaTime);
if (window.input.isKeyPressed(GLFW_KEY_F1)) {
@ -64,7 +64,7 @@ void GameScene::draw() {
Camera& camera = renderer.camera;
renderer.beginChunkDeferredCalls();
renderer.enableTexture(&defs.textures.atlasTexture);
renderer.enableTexture(&game.textures.atlasTexture);
drawCalls = world.renderChunks(renderer);
@ -76,7 +76,7 @@ void GameScene::draw() {
renderer.endDeferredCalls();
renderer.beginGUIDrawCalls();
renderer.enableTexture(&defs.textures.atlasTexture);
renderer.enableTexture(&game.textures.atlasTexture);
player.drawViginette(renderer);
debugGui.draw(renderer);

View File

@ -18,16 +18,16 @@ public:
void cleanup() override;
public:
ClientGame& defs;
ClientGame& game;
ClientNetworkInterpreter net;
LocalInventoryRefs refs;
LocalWorld world;
glm::vec3 playerPos;
Player player;
ClientNetworkInterpreter server;
LocalWorld world;
std::vector<Drawable*> entities;
DebugGui debugGui;
int drawCalls = 0;

View File

@ -2,21 +2,11 @@
// Created by aurailus on 11/01/19.
//
#include "../world/Player.h"
#include "../../../util/net/NetHandler.h"
#include "../../../util/net/Serializer.h"
#include "ClientNetworkInterpreter.h"
//#include <string>
//#include <iostream>
//#include <glm/vec3.hpp>
//
//#include "../../../util/Timer.h"
//#include "../../../util/net/Packet.h"
//#include "../../entity/engine/PlayerEntity.h"
//#include "../../graph/drawable/DrawableGroup.h"
//#include "../../../util/net/Address.h"
#include "NetPlayerField.h"
#include "../world/Player.h"
#include "../../../util/net/NetHandler.h"
ClientNetworkInterpreter::ClientNetworkInterpreter(ServerConnection &connection, ClientGame &defs, Player& player) :
world(nullptr),
@ -61,7 +51,7 @@ void ClientNetworkInterpreter::update() {
}
//Send Player Position
Packet p(PacketType::THIS_PLAYER_INFO);
Packet p(PacketType::PLAYER_INFO);
p.data = Serializer()
.append(player.getPos())
.append(player.getPitch())
@ -71,15 +61,6 @@ void ClientNetworkInterpreter::update() {
p.sendTo(connection.getPeer(), PacketChannel::PLAYER);
}
void ClientNetworkInterpreter::setBlock(glm::ivec3 pos, unsigned int block) {
Packet p(PacketType::BLOCK_SET);
Serializer()
.append(pos)
.append(block)
.packet(PacketType::BLOCK_SET)
.sendTo(connection.getPeer(), PacketChannel::BLOCK);
}
void ClientNetworkInterpreter::receivedPacket(std::unique_ptr<Packet> p) {
Deserializer d(p->data);
@ -91,11 +72,28 @@ void ClientNetworkInterpreter::receivedPacket(std::unique_ptr<Packet> p) {
}
case PacketType::THIS_PLAYER_INFO: {
id = d.read<unsigned int>();
player.setPos(d.read<glm::vec3>());
player.setPitch(d.read<float>());
player.setYaw(d.read<float>());
while (!d.atEnd()) {
switch (d.read<unsigned int>()) {
case static_cast<unsigned int>(NetPlayerField::ID): {
id = d.read<unsigned int>();
break;
}
case static_cast<unsigned int>(NetPlayerField::POSITION): {
player.setPos(d.read<glm::vec3>());
break;
}
case static_cast<unsigned int>(NetPlayerField::PITCH): {
player.setPitch(d.read<float>());
break;
}
case static_cast<unsigned int>(NetPlayerField::YAW): {
player.setYaw(d.read<float>());
break;
}
}
}
break;
}
@ -146,6 +144,21 @@ void ClientNetworkInterpreter::receivedPacket(std::unique_ptr<Packet> p) {
}
}
void ClientNetworkInterpreter::setBlock(glm::ivec3 pos, unsigned int block) {
Serializer().append(pos).append(block)
.packet(PacketType::BLOCK_SET).sendTo(connection.getPeer(), PacketChannel::BLOCK);
}
void ClientNetworkInterpreter::watchInv(const std::string& inv, const std::string& list) {
Serializer().append(inv).append(list)
.packet(PacketType::WATCH_INV).sendTo(connection.getPeer(), PacketChannel::INVENTORY);
}
void ClientNetworkInterpreter::unwatchInv(const std::string& inv, const std::string& list) {
Serializer().append(inv).append(list)
.packet(PacketType::UNWATCH_INV).sendTo(connection.getPeer(), PacketChannel::INVENTORY);
}
ClientNetworkInterpreter::~ClientNetworkInterpreter() {
connection.disconnect();
}
}

View File

@ -18,6 +18,8 @@ public:
void receivedPacket(std::unique_ptr<Packet> ePacket);
void setBlock(glm::ivec3 pos, unsigned int block);
void watchInv(const std::string& inv, const std::string& list);
void unwatchInv(const std::string& inv, const std::string& list);
~ClientNetworkInterpreter();

View File

@ -0,0 +1,12 @@
//
// Created by aurailus on 2020-02-24.
//
#pragma once
enum class NetPlayerField {
ID,
POSITION,
PITCH,
YAW
};

View File

@ -5,8 +5,8 @@
#pragma once
#include <map>
#include <memory>
#include <string>
#include <memory>
#include "InventoryList.h"
class Inventory {

View File

@ -5,7 +5,7 @@
#include <iostream>
#include <algorithm>
#include "InventoryList.h"
#include "../../../lua/api/type/LuaItemStack.h"
#include "../../../lua/api/class/LuaItemStack.h"
InventoryList::InventoryList(DefinitionAtlas& defs, std::string name, unsigned short size, unsigned short width) :
defs(defs),

View File

@ -27,7 +27,7 @@ public:
ItemStack addStack(ItemStack stack, bool playerInitiated = false);
// Returns how many of `stack`'s items fit, from 0 to stack.count.
unsigned short stackFits(const ItemStack& stack);
// Attempts to remove the type and amount of items specified by `stack`. Returns actual ItemStack of what was removed.
// Attempts to remove the usertype and amount of items specified by `stack`. Returns actual ItemStack of what was removed.
ItemStack takeStack(ItemStack request, bool playerInitiated = false);
void setStack(unsigned short i, const ItemStack& stack);

View File

@ -3,7 +3,7 @@
//
#include "ItemStack.h"
#include "../../../lua/api/type/LuaItemStack.h"
#include "../../../lua/api/class/LuaItemStack.h"
ItemStack::ItemStack(LuaItemStack &stack, const DefinitionAtlas &atlas) :
id((stack.get_count() == 0) ? 0 : atlas.fromStr(stack.get_name()).index),

View File

@ -0,0 +1,34 @@
//
// Created by aurailus on 2020-02-24.
//
#include "LocalInventory.h"
void LocalInventory::createList(std::string name, unsigned short length, unsigned short width, bool maintain) {
lists.insert({name, {(maintain ? -1 : 0), std::make_shared<LocalInventoryList>(defs, name, length, width)}});
}
std::shared_ptr<LocalInventoryList> LocalInventory::operator[](std::string name) {
if (lists.count(name)) return lists[name].second;
else return nullptr;
}
bool LocalInventory::pruneLists(ClientNetworkInterpreter &net, double time) {
for (auto lIt = lists.begin(); lIt != lists.end();) {
if (lIt->second.first != -1) {
// Start the timeout for Inventories that aren't being used.
if (lIt->second.first == 0 && lIt->second.second.use_count() == 1) lIt->second.first = time + 60;
// Remove the timeout for Inventories that are being used.
else if (lIt->second.first != 0 && lIt->second.second.use_count() > 1) lIt->second.first = 0;
// Delete InventoryLists that have passed their timeout.
else if (lIt->second.first <= time) {
net.unwatchInv(name, lIt->first);
lIt = lists.erase(lIt);
}
else lIt++;
}
else lIt++;
}
return lists.size() == 0;
}

View File

@ -0,0 +1,25 @@
//
// Created by aurailus on 2020-02-24.
//
#pragma once
#include "LocalInventoryList.h"
#include "../../../def/DefinitionAtlas.h"
#include "../net/ClientNetworkInterpreter.h"
class LocalInventory {
public:
LocalInventory(DefinitionAtlas& defs, const std::string& name) : defs(defs), name(name) {};
void createList(std::string name, unsigned short length, unsigned short width, bool maintain = false);
std::shared_ptr<LocalInventoryList> operator[](std::string name);
bool pruneLists(ClientNetworkInterpreter& net, double time);
DefinitionAtlas& defs;
private:
std::string name;
std::unordered_map<std::string, std::pair<double, std::shared_ptr<LocalInventoryList>>> lists;
};

View File

@ -0,0 +1,239 @@
//
// Created by aurailus on 2019-10-29.
//
#include <iostream>
#include <algorithm>
#include "LocalInventoryList.h"
#include "../../../lua/api/class/LuaItemStack.h"
LocalInventoryList::LocalInventoryList(DefinitionAtlas& defs, std::string name, unsigned short size, unsigned short width) :
defs(defs),
name(name),
itemstacks(size),
width(width) {}
void LocalInventoryList::setGuiCallback(std::function<void()> cb) {
this->guiCallback = cb;
}
void LocalInventoryList::setLuaCallback(LocalInventoryList::Callback type, sol::function cb) {
luaCallbacks[static_cast<size_t>(type)] = cb;
}
sol::function LocalInventoryList::getLuaCallback(LocalInventoryList::Callback type) {
return luaCallbacks[static_cast<size_t>(type)];
}
ItemStack LocalInventoryList::getStack(unsigned short i) {
return itemstacks[i];
}
void LocalInventoryList::setStack(unsigned short i, const ItemStack &stack) {
if (stack != getStack(i)) {
itemstacks[i] = stack;
triggerCallback();
}
}
ItemStack LocalInventoryList::placeStack(unsigned short i, const ItemStack &stack, bool playerInitiated) {
auto otherStack = getStack(i);
unsigned short allowedTake = otherStack.count;
if (playerInitiated) {
auto allowTake = luaCallbacks[static_cast<int>(Callback::ALLOW_TAKE)];
if (allowTake) allowedTake = std::min(static_cast<unsigned short>(allowTake(i+1, LuaItemStack(otherStack, defs))), allowedTake);
}
unsigned short allowedPut = stack.count;
if (playerInitiated) {
auto allowPut = luaCallbacks[static_cast<int>(Callback::ALLOW_PUT)];
if (allowPut) allowedPut = std::min(static_cast<unsigned short>(allowPut(i+1, LuaItemStack(stack, defs))), allowedPut);
}
sol::function on_put = luaCallbacks[static_cast<int>(Callback::ON_PUT)];
sol::function on_take = luaCallbacks[static_cast<int>(Callback::ON_TAKE)];
if (stack.count == 0) {
if (allowedTake == otherStack.count) setStack(i, {});
else setStack(i, {otherStack.id, static_cast<unsigned short>(otherStack.count - allowedTake)});
if (allowedTake > 0 && on_take) on_take(i+1, LuaItemStack(otherStack, defs));
return {otherStack.id, allowedTake};
}
else {
if (otherStack.count) {
if (otherStack.id == stack.id) {
unsigned short maxStack = defs.fromId(stack.id).maxStackSize;
if (allowedPut >= stack.count && allowedPut + otherStack.count < maxStack) {
setStack(i, {stack.id, static_cast<unsigned short>(otherStack.count + allowedPut)});
if (on_put) on_put(i+1, LuaItemStack(otherStack, defs));
return {};
}
else if (allowedPut > 0) {
allowedPut = std::min(allowedPut, static_cast<unsigned short>(maxStack - otherStack.count));
setStack(i, {stack.id, static_cast<unsigned short>(otherStack.count + allowedPut)});
if (allowedPut > 0 && on_put) on_put(i+1, LuaItemStack(otherStack, defs));
return {stack.id, static_cast<unsigned short>(stack.count - allowedPut)};
}
else {
allowedTake = std::min(allowedTake, static_cast<unsigned short>(maxStack - stack.count));
setStack(i, {stack.id, static_cast<unsigned short>(otherStack.count - allowedTake)});
if (allowedTake > 0 && on_take) on_take(i+1, LuaItemStack(otherStack, defs));
return {stack.id, static_cast<unsigned short>(stack.count + allowedTake)};
}
}
else {
if (stack.count <= allowedPut && otherStack.count <= allowedTake) {
setStack(i, stack);
if (allowedPut > 0 && on_put) on_put(i+1, LuaItemStack(otherStack, defs));
if (on_take) on_take(i+1, LuaItemStack(otherStack, defs));
return otherStack;
}
else {
return stack;
}
}
}
else {
setStack(i, {stack.id, static_cast<unsigned short>(otherStack.count + allowedPut)});
if (allowedPut > 0 && on_put) on_put(i+1, LuaItemStack(otherStack, defs));
return {stack.id, static_cast<unsigned short>(stack.count - allowedPut)};
}
}
}
ItemStack LocalInventoryList::splitStack(unsigned short i, bool playerInitiated) {
auto stack = getStack(i);
unsigned short allowedTake = stack.count;
if (playerInitiated) {
auto allowTake = luaCallbacks[static_cast<int>(Callback::ALLOW_TAKE)];
if (allowTake) allowedTake = std::min(static_cast<unsigned short>(allowTake(i + 1, LuaItemStack(stack, defs))), allowedTake);
}
unsigned short initialCount = stack.count;
unsigned short takeCount = std::min(static_cast<unsigned short>(ceil(initialCount / 2.f)), allowedTake);
setStack(i, {stack.id, static_cast<unsigned short>(initialCount - takeCount)});
sol::function on_take = luaCallbacks[static_cast<int>(Callback::ON_TAKE)];
if (on_take) on_take(i+1, stack);
return {stack.id, takeCount};
}
ItemStack LocalInventoryList::addStack(ItemStack stack, bool playerInitiated) {
unsigned short maxStack = defs.fromId(stack.id).maxStackSize;
unsigned short i = 0;
while (i < itemstacks.size() && stack.count > 0) {
if (itemstacks[i].count == 0) {
if (stack.count <= maxStack) {
itemstacks[i] = stack;
triggerCallback();
return ItemStack{};
}
else {
itemstacks[i] = stack;
itemstacks[i].count = maxStack;
stack.count -= maxStack;
}
}
else if (itemstacks[i].id == stack.id) {
unsigned int fits = maxStack - itemstacks[i].count;
if (fits >= stack.count) {
itemstacks[i].count += stack.count;
triggerCallback();
return ItemStack {};
}
else {
stack.count -= fits;
itemstacks[i].count += fits;
}
}
i++;
}
triggerCallback();
return stack;
}
unsigned short LocalInventoryList::stackFits(const ItemStack &stack) {
unsigned short maxStack = defs.fromId(stack.id).maxStackSize;
unsigned short i = 0;
unsigned short fits = 0;
while (i < itemstacks.size() && fits < stack.count) {
if (itemstacks[i].count == 0) {
fits += std::min(static_cast<unsigned short>(maxStack), stack.count);
}
else if (itemstacks[i].id == stack.id) {
unsigned int canfit = maxStack - itemstacks[i].count;
if (canfit >= stack.count - fits) {
fits += stack.count - fits;
}
else {
fits = stack.count;
}
}
i++;
}
return fits;
}
ItemStack LocalInventoryList::takeStack(ItemStack request, bool playerInitiated) {
unsigned short i = 0;
unsigned short to_remove = request.count;
while (i < itemstacks.size() && request.count > 0) {
if (itemstacks[i].id == request.id) {
unsigned int can_take = itemstacks[i].count;
if (can_take >= to_remove) {
itemstacks[i].count -= to_remove;
triggerCallback();
return request;
}
else {
to_remove -= can_take;
itemstacks[i] = ItemStack {};
}
}
i++;
}
request.count = (request.count - to_remove);
triggerCallback();
return request;
}
ItemStack LocalInventoryList::removeStack(unsigned short ind, unsigned short count) {
auto stack = getStack(ind);
if (count >= stack.count) {
setStack(ind, {0, 0});
return stack;
}
else {
stack.count -= count;
setStack(ind, stack);
stack.count = count;
return stack;
}
}
void LocalInventoryList::triggerCallback() {
if (guiCallback != nullptr) guiCallback();
}
unsigned short LocalInventoryList::getLength() {
return itemstacks.size();
}
unsigned short LocalInventoryList::getWidth() {
return width;
}
std::string LocalInventoryList::getName() {
return name;
}

View File

@ -0,0 +1,57 @@
//
// Created by aurailus on 2019-10-29.
//
#pragma once
#include <list>
#include <vector>
#include <functional>
#include "ItemStack.h"
class LocalInventoryList {
public:
enum class Callback { ALLOW_TAKE, ALLOW_PUT, ON_TAKE, ON_PUT };
LocalInventoryList(DefinitionAtlas& defs, std::string name, unsigned short size, unsigned short width);
unsigned short getLength();
unsigned short getWidth();
std::string getName();
// Place the stack at i into the existing stack, returning overflow or other stack.
ItemStack placeStack(unsigned short i, const ItemStack& stack, bool playerInitiated = false);
// Split the stack at i and return half of it, rounded up
ItemStack splitStack(unsigned short i, bool playerInitiated = false);
// Adds as much of `stack` to the inventory as possible, returns leftover.
ItemStack addStack(ItemStack stack, bool playerInitiated = false);
// Returns how many of `stack`'s items fit, from 0 to stack.count.
unsigned short stackFits(const ItemStack& stack);
// Attempts to remove the type and amount of items specified by `stack`. Returns actual ItemStack of what was removed.
ItemStack takeStack(ItemStack request, bool playerInitiated = false);
void setStack(unsigned short i, const ItemStack& stack);
ItemStack getStack(unsigned short i);
// Removes up to count items from ind, returns the items removed
ItemStack removeStack(unsigned short ind, unsigned short count);
void setGuiCallback(std::function<void()> cb);
void setLuaCallback(Callback type, sol::function cb);
sol::function getLuaCallback(Callback type);
DefinitionAtlas& defs;
private:
bool initialized = false;
void triggerCallback();
std::vector<ItemStack> itemstacks {};
unsigned short width = 0;
std::string name;
std::function<void()> guiCallback = nullptr;
std::array<sol::function, 4> luaCallbacks = {};
};

View File

@ -0,0 +1,41 @@
//
// Created by aurailus on 2020-02-24.
//
#include "LocalInventoryRefs.h"
LocalInventoryRefs::LocalInventoryRefs(LocalDefinitionAtlas& defs, ClientNetworkInterpreter& net) :
defs(defs),
net(net) {
std::cout << "Constructred default inv" << std::endl;
inventories.insert({"current_player", std::make_shared<LocalInventory>(defs, "current_player")});
inventories["current_player"]->createList("hand", 1, 1, true);
}
void LocalInventoryRefs::update(double delta) {
time += delta;
for (auto mIt = inventories.begin(); mIt != inventories.end();) {
bool del = mIt->second->pruneLists(net, time);
if (del) mIt = inventories.erase(mIt);
else mIt++;
}
}
std::shared_ptr<LocalInventory> LocalInventoryRefs::getInv(const std::string& inv) {
return inventories[inv];
}
std::shared_ptr<LocalInventoryList> LocalInventoryRefs::getList(const std::string& inv, const std::string& list) {
if (!inventories.count(inv)) inventories.insert({inv, {}});
if (inventories[inv]->operator[](list) == nullptr) {
inventories[inv]->createList(list, 1, 1);
net.watchInv(inv, list);
}
return inventories[inv]->operator[](list);
}
std::shared_ptr<LocalInventoryList> LocalInventoryRefs::getHand() {
return inventories["current_player"]->operator[]("hand");
}

View File

@ -0,0 +1,31 @@
//
// Created by aurailus on 2020-02-24.
//
#pragma once
#include <string>
#include <unordered_map>
#include "LocalInventory.h"
#include "LocalInventoryList.h"
#include "../net/ClientNetworkInterpreter.h"
#include "../../../def/LocalDefinitionAtlas.h"
class LocalInventoryRefs {
public:
LocalInventoryRefs(LocalDefinitionAtlas& defs, ClientNetworkInterpreter& net);
void update(double delta);
std::shared_ptr<LocalInventory> getInv(const std::string& inv);
std::shared_ptr<LocalInventoryList> getList(const std::string& inv, const std::string& list);
std::shared_ptr<LocalInventoryList> getHand();
private:
std::unordered_map<std::string, std::shared_ptr<LocalInventory>> inventories {};
ClientNetworkInterpreter& net;
LocalDefinitionAtlas& defs;
double time = 0;
};

View File

@ -6,16 +6,14 @@
#include "../../../util/Ray.h"
Player::Player(LocalWorld& world, ClientGame& defs, Renderer& renderer) :
Player::Player(LocalWorld& world, ClientGame& defs, Renderer& renderer, LocalInventoryRefs& refs) :
Collidable(world, defs, {{-0.3, 0, -0.3}, {0.3, 1.8, 0.3}}),
hand(defs.defs, "hand", 1, 1),
inventory(defs.defs),
gameGui(inventory, hand, renderer.window.getSize(), defs, renderer),
wireframe({}, 0.01, {1, 1, 1}),
refs(refs),
defs(defs),
renderer(renderer),
defs(defs) {
}
wireframe({}, 0.01, {1, 1, 1}),
gameGui(refs, renderer.window.getSize(), defs, renderer) {}
void Player::update(Input &input, double delta, glm::vec2 mouseDelta) {
if (activeBlock == -1) {
@ -142,11 +140,9 @@ void Player::findPointedThing(Input &input) {
blockChunk = world.getChunk(chunkPos);
}
unsigned int blockID = 0;
if (blockChunk != nullptr) {
blockID = blockChunk->getBlock(Space::Block::relative::toChunk(roundedPos));
}
if (blockChunk == nullptr) continue;
unsigned int blockID = blockChunk->getBlock(Space::Block::relative::toChunk(roundedPos));
auto& boxes = defs.defs.blockFromId(blockID).sBoxes;
for (auto& sBox : boxes) {
@ -251,8 +247,8 @@ PointedThing& Player::getPointedThing() {
return pointedThing;
}
Inventory &Player::getInventory() {
return inventory;
LocalInventory& Player::getInventory() {
return *refs.getInv("current_player");
}
/*

View File

@ -23,7 +23,7 @@ public:
static constexpr float BLOCK_DAMAGE = 0.36f;
static constexpr float BLOCK_INTERVAL = 0.02f;
Player(LocalWorld& world, ClientGame& defs, Renderer& renderer);
Player(LocalWorld& world, ClientGame& defs, Renderer& renderer, LocalInventoryRefs& refs);
void update(Input &input, double delta, glm::vec2 mouseDelta);
~Player();
@ -53,7 +53,7 @@ public:
void drawGUI(Renderer& renderer);
void drawViginette(Renderer& renderer);
Inventory& getInventory();
LocalInventory& getInventory();
PointedThing& getPointedThing();
private:
void moveAndLook(Input &input, double delta, glm::vec2 mouseDelta);
@ -62,22 +62,20 @@ private:
void updateWireframe();
void breakBlock(Input& input, double delta);
Renderer& renderer;
ClientGame& defs;
Renderer& renderer;
LocalInventoryRefs& refs;
InventoryList hand;
Inventory inventory;
GameGui gameGui;
Entity handModel;
WireframeEntity wireframe;
float yaw = 0;
float pitch = 0;
bool flying = false;
unsigned int activeBlock = -1;
PointedThing pointedThing;
WireframeEntity wireframe;
Entity handModel;
double breakInterval = 0;
};

View File

@ -2,9 +2,6 @@
// Created by aurailus on 03/10/19.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-static-cast-downcast"
#include "LocalLuaEntity.h"
void LocalLuaEntity::snap_pos(const sol::table &pos) {
@ -95,6 +92,4 @@ void LocalLuaEntity::set_display_type(const std::string &type, const std::string
model->fromSerialized(defs.models.models[arg], {defs.textures[*arg2]});
entity->setModel(model);
}
}
#pragma clang diagnostic pop
}

View File

@ -0,0 +1,9 @@
//
// Created by aurailus on 2020-02-24.
//
#include "LocalLuaInventory.h"
LocalLuaInventoryList LocalLuaInventory::get_list(std::string name) {
return LocalLuaInventoryList(*inventory[name]);
}

View File

@ -0,0 +1,20 @@
//
// Created by aurailus on 2020-02-24.
//
#pragma once
#include "LocalLuaInventoryList.h"
#include "../../../game/scene/world/LocalInventory.h"
class LocalLuaInventory {
public:
LocalLuaInventory(LocalInventory& inventory) : inventory(inventory), defs(inventory.defs) {}
LocalInventory& inventory;
DefinitionAtlas& defs;
// LocalLuaInventoryList add_list(std::string name, int size, int width);
LocalLuaInventoryList get_list(std::string name);
// void remove_list(std::string name);
};

View File

@ -0,0 +1,79 @@
//
// Created by aurailus on 2020-02-24.
//
#include "LocalLuaInventoryList.h"
#include "LuaItemStack.h"
int LocalLuaInventoryList::get_length() {
return list.getLength();
}
int LocalLuaInventoryList::get_width() {
return list.getWidth();
}
std::string LocalLuaInventoryList::get_name() {
return list.getName();
}
LuaItemStack LocalLuaInventoryList::get_stack(unsigned short i) {
return LuaItemStack(list.getStack(i - 1), list.defs);
}
void LocalLuaInventoryList::set_stack(unsigned short i, LuaItemStack stack) {
list.setStack(i - 1, ItemStack(stack, list.defs));
}
void LocalLuaInventoryList::set_stack(unsigned short i, sol::table stack) {
list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
}
LuaItemStack LocalLuaInventoryList::place_stack(unsigned short i, LuaItemStack stack) {
return LuaItemStack(list.placeStack(i - 1, ItemStack(stack, list.defs)), list.defs);
}
LuaItemStack LocalLuaInventoryList::place_stack(unsigned short i, sol::table stack) {
return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
}
LuaItemStack LocalLuaInventoryList::split_stack(unsigned short i) {
return LuaItemStack(list.splitStack(i - 1), list.defs);
}
LuaItemStack LocalLuaInventoryList::add_stack(LuaItemStack stack) {
return LuaItemStack(list.addStack(ItemStack(stack, list.defs)), list.defs);
}
LuaItemStack LocalLuaInventoryList::add_stack(sol::table stack) {
return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
}
int LocalLuaInventoryList::stack_fits(LuaItemStack stack) {
return list.stackFits(ItemStack(stack, list.defs));
}
int LocalLuaInventoryList::stack_fits(sol::table stack) {
return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
}
LuaItemStack LocalLuaInventoryList::take_stack(LuaItemStack request) {
return LuaItemStack(list.takeStack(ItemStack(request, list.defs)), list.defs);
}
LuaItemStack LocalLuaInventoryList::take_stack(sol::table request) {
return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request.get<unsigned short>(2))), list.defs);
}
LuaItemStack LocalLuaInventoryList::remove_stack(unsigned short ind, unsigned short count) {
return LuaItemStack(list.removeStack(ind - 1, count), list.defs);
}
void LocalLuaInventoryList::set_callback(LocalInventoryList::Callback t, sol::function fun) {
list.setLuaCallback(t, fun);
}
sol::function LocalLuaInventoryList::get_callback(LocalInventoryList::Callback t) {
return list.getLuaCallback(t);
}

View File

@ -0,0 +1,43 @@
//
// Created by aurailus on 2020-02-24.
//
#pragma once
#include "../../../game/scene/world/LocalInventoryList.h"
class LocalLuaInventoryList {
public:
LocalLuaInventoryList(LocalInventoryList& list) :
list(list) {}
LocalInventoryList& list;
int get_length();
int get_width();
std::string get_name();
LuaItemStack get_stack(unsigned short i);
void set_stack(unsigned short i, LuaItemStack stack);
void set_stack(unsigned short i, sol::table stack);
LuaItemStack place_stack(unsigned short i, LuaItemStack stack);
LuaItemStack place_stack(unsigned short i, sol::table stack);
LuaItemStack split_stack(unsigned short i);
LuaItemStack add_stack(LuaItemStack stack);
LuaItemStack add_stack(sol::table stack);
int stack_fits(LuaItemStack stack);
int stack_fits(sol::table stack);
LuaItemStack take_stack(LuaItemStack request);
LuaItemStack take_stack(sol::table request);
LuaItemStack remove_stack(unsigned short ind, unsigned short count);
void set_callback(LocalInventoryList::Callback t, sol::function fun);
sol::function get_callback(LocalInventoryList::Callback t);
};

View File

@ -82,8 +82,8 @@ void LocalLuaPlayer::close_menu() {
player.closeMenu();
}
LuaInventory LocalLuaPlayer::get_inventory() {
return LuaInventory(player.getInventory());
LocalLuaInventory LocalLuaPlayer::get_inventory() {
return LocalLuaInventory(player.getInventory());
}
void LocalLuaPlayer::set_selected_block(std::string block) {

View File

@ -6,7 +6,7 @@
#include <sol2/sol.hpp>
#include "LuaInventory.h"
#include "LocalLuaInventory.h"
#include "../../../def/ClientGame.h"
#include "../../../game/scene/world/Player.h"
@ -33,7 +33,7 @@ public:
void close_menu();
void open_menu(sol::this_state s, std::string menu, sol::optional<sol::table> callbacks);
LuaInventory get_inventory();
LocalLuaInventory get_inventory();
void set_selected_block(std::string block);

View File

@ -2,9 +2,6 @@
// Created by aurailus on 2019-12-28.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-static-cast-downcast"
#include "ServerLuaEntity.h"
void ServerLuaEntity::snap_pos(const sol::table &pos) {
@ -94,6 +91,4 @@ void ServerLuaEntity::set_display_type(const std::string &type, const std::strin
else if (strncmp(type.data(), "model", 5) == 0 && arg2 && !arg2->empty()) {
entity->setAppearance("model", arg, *arg2);
}
}
#pragma clang diagnostic pop
}

View File

@ -0,0 +1,18 @@
//
// Created by aurailus on 2019-12-17.
//
#include "ServerLuaInventory.h"
ServerLuaInventoryList ServerLuaInventory::add_list(std::string name, int size, int width) {
inventory.createList(name, size, width);
return ServerLuaInventoryList(*inventory[name]);
}
ServerLuaInventoryList ServerLuaInventory::get_list(std::string name) {
return ServerLuaInventoryList(*inventory[name]);
}
void ServerLuaInventory::remove_list(std::string name) {
inventory.removeList(name);
}

View File

@ -0,0 +1,20 @@
//
// Created by aurailus on 2019-12-17.
//
#pragma once
#include "../../../game/scene/world/Inventory.h"
#include "ServerLuaInventoryList.h"
class ServerLuaInventory {
public:
ServerLuaInventory(Inventory& inventory) : inventory(inventory), defs(inventory.defs) {}
Inventory& inventory;
DefinitionAtlas& defs;
ServerLuaInventoryList add_list(std::string name, int size, int width);
ServerLuaInventoryList get_list(std::string name);
void remove_list(std::string name);
};

View File

@ -2,76 +2,76 @@
// Created by aurailus on 2019-12-17.
//
#include "LuaInventoryList.h"
#include "ServerLuaInventoryList.h"
int LuaInventoryList::get_length() {
int ServerLuaInventoryList::get_length() {
return list.getLength();
}
int LuaInventoryList::get_width() {
int ServerLuaInventoryList::get_width() {
return list.getWidth();
}
std::string LuaInventoryList::get_name() {
std::string ServerLuaInventoryList::get_name() {
return list.getName();
}
LuaItemStack LuaInventoryList::get_stack(unsigned short i) {
LuaItemStack ServerLuaInventoryList::get_stack(unsigned short i) {
return LuaItemStack(list.getStack(i - 1), list.defs);
}
void LuaInventoryList::set_stack(unsigned short i, LuaItemStack stack) {
void ServerLuaInventoryList::set_stack(unsigned short i, LuaItemStack stack) {
list.setStack(i - 1, ItemStack(stack, list.defs));
}
void LuaInventoryList::set_stack(unsigned short i, sol::table stack) {
void ServerLuaInventoryList::set_stack(unsigned short i, sol::table stack) {
list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
}
LuaItemStack LuaInventoryList::place_stack(unsigned short i, LuaItemStack stack) {
LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, LuaItemStack stack) {
return LuaItemStack(list.placeStack(i - 1, ItemStack(stack, list.defs)), list.defs);
}
LuaItemStack LuaInventoryList::place_stack(unsigned short i, sol::table stack) {
LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, sol::table stack) {
return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
}
LuaItemStack LuaInventoryList::split_stack(unsigned short i) {
LuaItemStack ServerLuaInventoryList::split_stack(unsigned short i) {
return LuaItemStack(list.splitStack(i - 1), list.defs);
}
LuaItemStack LuaInventoryList::add_stack(LuaItemStack stack) {
LuaItemStack ServerLuaInventoryList::add_stack(LuaItemStack stack) {
return LuaItemStack(list.addStack(ItemStack(stack, list.defs)), list.defs);
}
LuaItemStack LuaInventoryList::add_stack(sol::table stack) {
LuaItemStack ServerLuaInventoryList::add_stack(sol::table stack) {
return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2))), list.defs);
}
int LuaInventoryList::stack_fits(LuaItemStack stack) {
int ServerLuaInventoryList::stack_fits(LuaItemStack stack) {
return list.stackFits(ItemStack(stack, list.defs));
}
int LuaInventoryList::stack_fits(sol::table stack) {
int ServerLuaInventoryList::stack_fits(sol::table stack) {
return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack.get<unsigned short>(2)));
}
LuaItemStack LuaInventoryList::take_stack(LuaItemStack request) {
LuaItemStack ServerLuaInventoryList::take_stack(LuaItemStack request) {
return LuaItemStack(list.takeStack(ItemStack(request, list.defs)), list.defs);
}
LuaItemStack LuaInventoryList::take_stack(sol::table request) {
LuaItemStack ServerLuaInventoryList::take_stack(sol::table request) {
return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request.get<unsigned short>(2))), list.defs);
}
LuaItemStack LuaInventoryList::remove_stack(unsigned short ind, unsigned short count) {
LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short ind, unsigned short count) {
return LuaItemStack(list.removeStack(ind - 1, count), list.defs);
}
void LuaInventoryList::set_callback(InventoryList::Callback t, sol::function fun) {
void ServerLuaInventoryList::set_callback(InventoryList::Callback t, sol::function fun) {
list.setLuaCallback(t, fun);
}
sol::function LuaInventoryList::get_callback(InventoryList::Callback t) {
sol::function ServerLuaInventoryList::get_callback(InventoryList::Callback t) {
return list.getLuaCallback(t);
}

View File

@ -7,9 +7,9 @@
#include "../../../game/scene/world/InventoryList.h"
#include "LuaItemStack.h"
class LuaInventoryList {
class ServerLuaInventoryList {
public:
LuaInventoryList(InventoryList& list) :
ServerLuaInventoryList(InventoryList& list) :
list(list) {}
InventoryList& list;

View File

@ -6,7 +6,7 @@
#include "../../../util/net/NetHandler.h"
void ServerLuaPlayer::set_pos(const sol::table &pos) {
player.setPos({pos["x"], pos["y"], pos["z"]});
player.assertPos({pos["x"], pos["y"], pos["z"]});
}
sol::table ServerLuaPlayer::get_pos(sol::this_state s) {
@ -29,7 +29,7 @@ sol::table ServerLuaPlayer::get_block_pos(sol::this_state s) {
//}
void ServerLuaPlayer::set_look_yaw(float rot) {
player.setYaw(rot);
player.assertYaw(rot);
}
float ServerLuaPlayer::get_look_yaw() {
@ -37,7 +37,7 @@ float ServerLuaPlayer::get_look_yaw() {
}
void ServerLuaPlayer::set_look_pitch(float rot) {
player.setPitch(rot);
player.assertPitch(rot);
}
float ServerLuaPlayer::get_look_pitch() {
@ -64,6 +64,6 @@ std::string ServerLuaPlayer::get_address() {
return NetHandler::intToIPString(player.address.host) + ":" + std::to_string(player.address.port);
}
LuaInventory ServerLuaPlayer::get_inventory() {
return LuaInventory(player.getInventory());
ServerLuaInventory ServerLuaPlayer::get_inventory() {
return ServerLuaInventory(player.getInventory());
}

View File

@ -5,7 +5,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "LuaInventory.h"
#include "ServerLuaInventory.h"
#include "../../../server/conn/ServerClient.h"
class ServerLuaPlayer {
@ -33,7 +33,7 @@ public:
// void close_menu();
// void open_menu(sol::this_state s, std::string menu, sol::optional<sol::table> callbacks);
LuaInventory get_inventory();
ServerLuaInventory get_inventory();
// void set_selected_block(std::string block);

View File

@ -5,10 +5,13 @@
#pragma once
#include <sol2/sol.hpp>
#include "../type/LocalLuaEntity.h"
#include "../class/LocalLuaEntity.h"
#include "../class/ServerLuaEntity.h"
#include "../../../def/ServerGame.h"
#include "../../../server/world/ServerWorld.h"
#include "../../../game/scene/world/LocalWorld.h"
#include "../../../server/world/ServerEntity.h"
namespace Api {
static int entities_ind = 0;

View File

@ -5,7 +5,8 @@
#pragma once
#include <sol2/sol.hpp>
#include "../type/LocalLuaEntity.h"
#include "../class/LocalLuaEntity.h"
#include "../../../game/scene/world/LocalWorld.h"
namespace Api {

View File

@ -1,18 +0,0 @@
//
// Created by aurailus on 2019-12-17.
//
#include "LuaInventory.h"
LuaInventoryList LuaInventory::add_list(std::string name, int size, int width) {
inventory.createList(name, size, width);
return LuaInventoryList(*inventory[name]);
}
LuaInventoryList LuaInventory::get_list(std::string name) {
return LuaInventoryList(*inventory[name]);
}
void LuaInventory::remove_list(std::string name) {
inventory.removeList(name);
}

View File

@ -1,20 +0,0 @@
//
// Created by aurailus on 2019-12-17.
//
#pragma once
#include "../../../game/scene/world/Inventory.h"
#include "LuaInventoryList.h"
class LuaInventory {
public:
LuaInventory(Inventory& inventory) : inventory(inventory), defs(inventory.defs) {}
Inventory& inventory;
DefinitionAtlas& defs;
LuaInventoryList add_list(std::string name, int size, int width);
LuaInventoryList get_list(std::string name);
void remove_list(std::string name);
};

View File

@ -1,56 +0,0 @@
//
// Created by aurailus on 2019-12-17.
//
#pragma once
#include <sol2/sol.hpp>
#include "LuaInventory.h"
namespace ClientApi {
static void inventory(sol::state& lua) {
lua.new_usertype<LuaInventory>("InventoryRef",
"add_list", &LuaInventory::add_list,
"get_list", &LuaInventory::get_list,
"remove_list", &LuaInventory::remove_list
);
lua.new_usertype<LuaInventoryList>("InventoryListRef",
"length", sol::property(&LuaInventoryList::get_length),
"width", sol::property(&LuaInventoryList::get_width),
"name", sol::property(&LuaInventoryList::get_name),
"get_stack", &LuaInventoryList::get_stack,
"set_stack", sol::overload(
sol::resolve<void(unsigned short, LuaItemStack)>(&LuaInventoryList::set_stack),
sol::resolve<void(unsigned short, sol::table)>(&LuaInventoryList::set_stack)),
"place_stack", sol::overload(
sol::resolve<LuaItemStack(unsigned short, LuaItemStack)>(&LuaInventoryList::place_stack),
sol::resolve<LuaItemStack(unsigned short, sol::table)>(&LuaInventoryList::place_stack)),
"split_stack", &LuaInventoryList::split_stack,
"add_stack", sol::overload(
sol::resolve<LuaItemStack(LuaItemStack)>(&LuaInventoryList::add_stack),
sol::resolve<LuaItemStack(sol::table)>(&LuaInventoryList::add_stack)),
"stack_fits", sol::overload(
sol::resolve<int(LuaItemStack)>(&LuaInventoryList::stack_fits),
sol::resolve<int(sol::table)>(&LuaInventoryList::stack_fits)),
"take_stack", sol::overload(
sol::resolve<LuaItemStack(LuaItemStack)>(&LuaInventoryList::take_stack),
sol::resolve<LuaItemStack(sol::table)>(&LuaInventoryList::take_stack)),
"remove_stack", &LuaInventoryList::remove_stack,
"on_put", sol::property(
[](LuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_PUT); },
[](LuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_PUT, f); }),
"on_take", sol::property(
[](LuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_TAKE); },
[](LuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_TAKE, f); }),
"allow_put", sol::property(
[](LuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_PUT); },
[](LuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_PUT, f); }),
"allow_take", sol::property(
[](LuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_TAKE); },
[](LuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_TAKE, f); })
);
}
}

View File

@ -4,12 +4,12 @@
#pragma once
#include "LocalLuaAnimationManager.h"
#include "../class/LocalLuaAnimationManager.h"
#include <sol2/sol.hpp>
namespace ClientApi {
void animation_manager(sol::state& lua) {
lua.new_usertype<LocalLuaAnimationManager>("animation_manager",
lua.new_usertype<LocalLuaAnimationManager>("AnimationManager",
"define", &LocalLuaAnimationManager::define,
"set_anim", &LocalLuaAnimationManager::set_anim,
"play", &LocalLuaAnimationManager::play,

View File

@ -0,0 +1,54 @@
//
// Created by aurailus on 2019-12-17.
//
#pragma once
#include <sol2/sol.hpp>
#include "../class/LocalLuaInventory.h"
namespace ClientApi {
static void inventory(sol::state& lua) {
lua.new_usertype<LocalLuaInventory>("InventoryRef",
"get_list", &LocalLuaInventory::get_list
);
lua.new_usertype<LocalLuaInventoryList>("InventoryListRef",
"length", sol::property(&LocalLuaInventoryList::get_length),
"width", sol::property(&LocalLuaInventoryList::get_width),
"name", sol::property(&LocalLuaInventoryList::get_name),
"get_stack", &LocalLuaInventoryList::get_stack,
// "set_stack", sol::overload(
// sol::resolve<void(unsigned short, LuaItemStack)>(&LocalLuaInventoryList::set_stack),
// sol::resolve<void(unsigned short, sol::table)>(&LocalLuaInventoryList::set_stack)),
// "place_stack", sol::overload(
// sol::resolve<LuaItemStack(unsigned short, LuaItemStack)>(&LocalLuaInventoryList::place_stack),
// sol::resolve<LuaItemStack(unsigned short, sol::table)>(&LocalLuaInventoryList::place_stack)),
// "split_stack", &LocalLuaInventoryList::split_stack,
// "add_stack", sol::overload(
// sol::resolve<LuaItemStack(LuaItemStack)>(&LocalLuaInventoryList::add_stack),
// sol::resolve<LuaItemStack(sol::table)>(&LocalLuaInventoryList::add_stack)),
// "stack_fits", sol::overload(
// sol::resolve<int(LuaItemStack)>(&LocalLuaInventoryList::stack_fits),
// sol::resolve<int(sol::table)>(&LocalLuaInventoryList::stack_fits)),
// "take_stack", sol::overload(
// sol::resolve<LuaItemStack(LuaItemStack)>(&LocalLuaInventoryList::take_stack),
// sol::resolve<LuaItemStack(sol::table)>(&LocalLuaInventoryList::take_stack)),
// "remove_stack", &LocalLuaInventoryList::remove_stack,
"on_put", sol::property(
[](LocalLuaInventoryList l){ return l.get_callback(LocalInventoryList::Callback::ON_PUT); },
[](LocalLuaInventoryList l, sol::function f){ l.set_callback(LocalInventoryList::Callback::ON_PUT, f); }),
"on_take", sol::property(
[](LocalLuaInventoryList l){ return l.get_callback(LocalInventoryList::Callback::ON_TAKE); },
[](LocalLuaInventoryList l, sol::function f){ l.set_callback(LocalInventoryList::Callback::ON_TAKE, f); }),
"allow_put", sol::property(
[](LocalLuaInventoryList l){ return l.get_callback(LocalInventoryList::Callback::ALLOW_PUT); },
[](LocalLuaInventoryList l, sol::function f){ l.set_callback(LocalInventoryList::Callback::ALLOW_PUT, f); }),
"allow_take", sol::property(
[](LocalLuaInventoryList l){ return l.get_callback(LocalInventoryList::Callback::ALLOW_TAKE); },
[](LocalLuaInventoryList l, sol::function f){ l.set_callback(LocalInventoryList::Callback::ALLOW_TAKE, f); })
);
}
}

View File

@ -5,7 +5,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "LuaItemStack.h"
#include "../class/LuaItemStack.h"
namespace ClientApi {
static void item_stack(sol::state& lua) {

View File

@ -5,7 +5,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "LocalLuaPlayer.h"
#include "../class/LocalLuaPlayer.h"
namespace ClientApi {
void local_player(sol::state& lua) {
@ -30,7 +30,7 @@ namespace ClientApi {
"block_pos", sol::property(&LocalLuaPlayer::get_block_pos, &LocalLuaPlayer::set_pos),
"vel", sol::property(&LocalLuaPlayer::get_vel, &LocalLuaPlayer::set_vel),
"look_yaw", sol::property(&LocalLuaPlayer::get_look_yaw, &LocalLuaPlayer::set_look_yaw),
"look_yaw", sol::property(&LocalLuaPlayer::get_look_pitch, &LocalLuaPlayer::set_look_pitch),
"look_pitch", sol::property(&LocalLuaPlayer::get_look_pitch, &LocalLuaPlayer::set_look_pitch),
"flying", sol::property(&LocalLuaPlayer::set_flying, &LocalLuaPlayer::get_flying),

View File

@ -4,7 +4,7 @@
#pragma once
#include "LocalLuaEntity.h"
#include "../class/LocalLuaEntity.h"
#include "../../../game/scene/world/LocalWorld.h"
#include <sol2/sol.hpp>

View File

@ -0,0 +1,56 @@
//
// Created by aurailus on 2019-12-17.
//
#pragma once
#include <sol2/sol.hpp>
#include "../class/ServerLuaInventory.h"
namespace ServerApi {
static void inventory(sol::state& lua) {
lua.new_usertype<ServerLuaInventory>("InventoryRef",
"get_list", &ServerLuaInventory::get_list,
"add_list", &ServerLuaInventory::add_list,
"remove_list", &ServerLuaInventory::remove_list
);
lua.new_usertype<ServerLuaInventoryList>("InventoryListRef",
"length", sol::property(&ServerLuaInventoryList::get_length),
"width", sol::property(&ServerLuaInventoryList::get_width),
"name", sol::property(&ServerLuaInventoryList::get_name),
"get_stack", &ServerLuaInventoryList::get_stack,
"set_stack", sol::overload(
sol::resolve<void(unsigned short, LuaItemStack)>(&ServerLuaInventoryList::set_stack),
sol::resolve<void(unsigned short, sol::table)>(&ServerLuaInventoryList::set_stack)),
"place_stack", sol::overload(
sol::resolve<LuaItemStack(unsigned short, LuaItemStack)>(&ServerLuaInventoryList::place_stack),
sol::resolve<LuaItemStack(unsigned short, sol::table)>(&ServerLuaInventoryList::place_stack)),
"split_stack", &ServerLuaInventoryList::split_stack,
"add_stack", sol::overload(
sol::resolve<LuaItemStack(LuaItemStack)>(&ServerLuaInventoryList::add_stack),
sol::resolve<LuaItemStack(sol::table)>(&ServerLuaInventoryList::add_stack)),
"stack_fits", sol::overload(
sol::resolve<int(LuaItemStack)>(&ServerLuaInventoryList::stack_fits),
sol::resolve<int(sol::table)>(&ServerLuaInventoryList::stack_fits)),
"take_stack", sol::overload(
sol::resolve<LuaItemStack(LuaItemStack)>(&ServerLuaInventoryList::take_stack),
sol::resolve<LuaItemStack(sol::table)>(&ServerLuaInventoryList::take_stack)),
"remove_stack", &ServerLuaInventoryList::remove_stack,
"on_put", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_PUT, f); }),
"on_take", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_TAKE, f); }),
"allow_put", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_PUT, f); }),
"allow_take", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_TAKE, f); })
);
}
}

View File

@ -4,7 +4,7 @@
#pragma once
#include "ServerLuaEntity.h"
#include "../class/ServerLuaEntity.h"
#include <sol2/sol.hpp>
namespace ServerApi {

View File

@ -5,7 +5,7 @@
#pragma once
#include <sol2/sol.hpp>
#include "ServerLuaPlayer.h"
#include "../class/ServerLuaPlayer.h"
namespace ServerApi {
void server_player(sol::state& lua) {
@ -31,7 +31,7 @@ namespace ServerApi {
"block_pos", sol::property(&ServerLuaPlayer::get_block_pos, &ServerLuaPlayer::set_pos),
// "vel", sol::property(&ServerLuaPlayer::get_vel, &ServerLuaPlayer::set_vel),
"look_yaw", sol::property(&ServerLuaPlayer::get_look_yaw, &ServerLuaPlayer::set_look_yaw),
"look_yaw", sol::property(&ServerLuaPlayer::get_look_pitch, &ServerLuaPlayer::set_look_pitch),
"look_pitch", sol::property(&ServerLuaPlayer::get_look_pitch, &ServerLuaPlayer::set_look_pitch),
"flying", sol::property(&ServerLuaPlayer::set_flying, &ServerLuaPlayer::get_flying),

View File

@ -10,13 +10,12 @@
#include "LocalLuaParser.h"
// Types
#include "../api/type/LocalLuaPlayer.h"
#include "../api/type/cLocalLuaEntity.h"
#include "../api/type/cLuaLocalPlayer.h"
#include "../api/type/cLuaInventory.h"
#include "../api/type/cLuaItemStack.h"
#include "../api/type/cLocalLuaAnimationManager.h"
// Usertypes
#include "../api/usertype/cItemStack.h"
#include "../api/usertype/cLocalPlayer.h"
#include "../api/usertype/cLuaEntity.h"
#include "../api/usertype/cInventoryRef.h"
#include "../api/usertype/cAnimationManager.h"
// Modules
#include "../api/modules/delay.h"
@ -69,11 +68,11 @@ void LocalLuaParser::loadApi(ClientGame &defs, LocalWorld &world, Player& player
core["__builtin"] = lua.create_table();
// Types
ClientApi::entity(lua);
ClientApi::entity (lua);
ClientApi::animation_manager(lua);
ClientApi::local_player(lua);
ClientApi::inventory(lua);
ClientApi::item_stack(lua);
ClientApi::local_player (lua);
ClientApi::inventory (lua);
ClientApi::item_stack (lua);
core["client"] = true;
core["player"] = LocalLuaPlayer(player);
@ -132,13 +131,16 @@ sol::protected_function_result LocalLuaParser::errorCallback(lua_State*, sol::pr
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
break;
exit(1);
}
}
break;
}
}
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << std::endl << errString << Log::endl;
exit(1);
return errPfr;
}

View File

@ -12,11 +12,11 @@
#include "ServerLuaParser.h"
// Types
#include "../api/type/sServerLuaPlayer.h"
#include "../api/type/sServerLuaEntity.h"
#include "../api/type/cLuaInventory.h"
#include "../api/type/cLuaItemStack.h"
// Usertypes
#include "../api/usertype/sServerPlayer.h"
#include "../api/usertype/sLuaEntity.h"
#include "../api/usertype/sInventoryRef.h"
#include "../api/usertype/cItemStack.h"
// Modules
#include "../api/modules/delay.h"
@ -98,10 +98,10 @@ void ServerLuaParser::loadApi(ServerGame &defs, ServerWorld &world) {
core["__builtin"] = lua.create_table();
// Types
ServerApi::entity(lua);
ServerApi::entity (lua);
ServerApi::server_player(lua);
ClientApi::inventory(lua);
ClientApi::item_stack(lua);
ServerApi::inventory (lua);
ClientApi::item_stack (lua);
core["server"] = true;
core["players"] = lua.create_table();
@ -143,7 +143,7 @@ sol::protected_function_result ServerLuaParser::errorCallback(lua_State*, sol::p
sol::error err = errPfr;
std::string errString = err.what();
std::string::size_type slash = errString.find('/');
std::string::size_type slash = errString.find_first_of("/");
assert(slash != std::string::npos);
std::string modString = errString.substr(0, slash);
@ -161,13 +161,16 @@ sol::protected_function_result ServerLuaParser::errorCallback(lua_State*, sol::p
for (auto& file : mod.files) {
if (file.path == fileName) {
std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl;
break;
exit(1);
}
}
break;
}
}
std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:"
<< std::endl << std::endl << errString << Log::endl;
exit(1);
return errPfr;
}
@ -187,7 +190,7 @@ sol::protected_function_result ServerLuaParser::runFileSandboxed(const std::stri
env["_MODNAME"] = mod.config.name;
return lua.safe_script(f.file, env, std::bind(&ServerLuaParser::errorCallback, this,
std::placeholders::_1, std::placeholders::_2), "@" + f.path, sol::load_mode::text);
std::placeholders::_1, std::placeholders::_2), "@" + f.path);
}
}

View File

@ -160,7 +160,7 @@ std::vector<LuaMod> ServerModHandler::initializeLuaMods(const std::list<std::str
fileStr = VenusParser::parse(file, fileStr);
}
catch (std::runtime_error e) {
std::cout << std::endl << e.what() << std::endl;
std::cout << Log::err << "Encountered a venus compilation err" << std::endl << e.what() << Log::endl;
exit(1);
}
}

View File

@ -106,7 +106,7 @@ void Server::handlePlayerPacket(ServerClient &client, Packet& p) {
std::cout << Log::err << "Invalid packet type (" << static_cast<int>(p.type) << ") recieved." << Log::endl;
break;
}
case PacketType::THIS_PLAYER_INFO: {
case PacketType::PLAYER_INFO: {
Deserializer d(p.data);
client.setPos(d.read<glm::vec3>());
client.setPitch(d.read<float>());

View File

@ -6,6 +6,7 @@
#include "../../util/net/Serializer.h"
#include "../../util/net/NetHandler.h"
#include "../../def/ServerGame.h"
#include "../../game/scene/net/NetPlayerField.h"
ClientList::ClientList(ServerGame& defs) :
@ -45,10 +46,10 @@ void ClientList::createPlayer(std::shared_ptr<ServerClient> c) {
Packet p(PacketType::THIS_PLAYER_INFO);
p.data = Serializer()
.append<unsigned int>(static_cast<unsigned int>(NetPlayerField::ID))
.append(c->cid)
.append<unsigned int>(static_cast<unsigned int>(NetPlayerField::POSITION))
.append(c->getPos())
.append(c->getPitch())
.append(c->getYaw())
.data;
p.sendTo(c->peer, PacketChannel::ENTITY);

View File

@ -4,6 +4,9 @@
#include <iostream>
#include "ServerClient.h"
#include "../../util/net/Packet.h"
#include "../../util/net/Serializer.h"
#include "../../game/scene/net/NetPlayerField.h"
ServerClient::ServerClient(ENetPeer *peer, ENetAddress address, DefinitionAtlas& defs) :
peer(peer),
@ -16,6 +19,16 @@ void ServerClient::setUsername(const std::string &name) {
this->username = name;
}
void ServerClient::assertPos(glm::vec3 pos) {
setPos(pos);
Serializer()
.append(static_cast<unsigned int>(NetPlayerField::POSITION))
.append(pos)
.packet(PacketType::THIS_PLAYER_INFO)
.sendTo(peer, PacketChannel::PLAYER);
}
void ServerClient::setPos(glm::vec3 pos) {
glm::vec3 lastMapBlock = Space::MapBlock::world::fromBlock(this->pos);
glm::vec3 newMapBlock = Space::MapBlock::world::fromBlock(pos);
@ -32,6 +45,15 @@ glm::vec3 ServerClient::getPos() {
return pos;
}
void ServerClient::assertPitch(float pitch) {
setPitch(pitch);
Serializer()
.append(static_cast<unsigned int>(NetPlayerField::PITCH))
.append(pitch)
.packet(PacketType::THIS_PLAYER_INFO)
.sendTo(peer, PacketChannel::PLAYER);
}
void ServerClient::setPitch(float pitch) {
this->pitch = pitch;
}
@ -40,6 +62,15 @@ float ServerClient::getPitch() {
return pitch;
}
void ServerClient::assertYaw(float yaw) {
setYaw(yaw);
Serializer()
.append(static_cast<unsigned int>(NetPlayerField::YAW))
.append(yaw)
.packet(PacketType::THIS_PLAYER_INFO)
.sendTo(peer, PacketChannel::PLAYER);
}
void ServerClient::setYaw(float yaw) {
this->yaw = yaw;
}
@ -67,4 +98,4 @@ unsigned long long ServerClient::getMapBlockIntegrity(glm::ivec3 pos) {
Inventory &ServerClient::getInventory() {
return inventory;
}
}

View File

@ -20,12 +20,15 @@ public:
void setUsername(const std::string& name);
void assertPos(glm::vec3 pos);
void setPos(glm::vec3 pos);
glm::vec3 getPos();
void assertPitch(float pitch);
void setPitch(float pitch);
float getPitch();
void assertYaw(float yaw);
void setYaw(float yaw);
float getYaw();

View File

@ -14,7 +14,10 @@ NetHandler::NetHandler(const Address& hostAddress, int attempts, int timeout) {
NetHandler::NetHandler(unsigned short port, short max_clients) {
initServer(port, max_clients);
if (state == NetState::ENET_ERROR) exit(1);
if (state == NetState::ENET_ERROR) {
std::cout << Log::err << "Failed to initialize ENet." << Log::endl;
exit(1);
}
}
void NetHandler::initServer(unsigned short port, short max_clients) {

View File

@ -13,5 +13,6 @@ enum class PacketChannel {
ENTITY,
CHUNK,
BLOCK,
INVENTORY
};

View File

@ -6,8 +6,6 @@
enum class PacketType {
UNDEFINED = 0,
CONNECT,
AUTH,
// Information Request Types
BLOCK_IDENTIFIER_LIST,
@ -22,9 +20,10 @@ enum class PacketType {
SERVER_INFO,
THIS_PLAYER_INFO,
PLAYER_INFO,
MAPBLOCK,
CHUNK,
BLOCK_SET,
ENTITY_INFO
ENTITY_INFO,
WATCH_INV,
UNWATCH_INV
};

View File

@ -5,8 +5,8 @@
#pragma once
#include "Dimension.h"
#include "../lua/api/type/LocalLuaEntity.h"
#include "../lua/api/type/ServerLocalLuaEntity.h"
#include "../lua/api/class/LocalLuaEntity.h"
#include "../lua/api/class/ServerLocalLuaEntity.h"
#include "../game/entity/engine/PlayerEntity.h"
#include "../game/scene/world/MeshGenStream.h"
#include "../game/scene/world/graph/ChunkRenderElem.h"

View File

@ -5,7 +5,8 @@
#pragma once
#include "Dimension.h"
#include "../lua/api/type/ServerLuaEntity.h"
#include "../lua/api/class/ServerLuaEntity.h"
class ServerClient;