Skip to content

Commit

Permalink
[WorldSelectionState] Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 20, 2020
1 parent afc4918 commit 18c2752
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 10 deletions.
11 changes: 8 additions & 3 deletions source/client/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "ServerLoadingState.hpp"
#include "SettingsMenuState.hpp"
#include "TitleScreenState.hpp"
#include "WorldSelectionState.hpp"

#include "ServerApplication.hpp" // For ServerOnlineEvent

Expand All @@ -42,7 +43,7 @@ TitleScreenState::TitleScreenState(u16 port) : m_port(port) {
m_menuWidget.setScale(Config::guiScale, Config::guiScale, 1);

m_menuWidget.addButton("Singleplayer", [this] (TextButton &) {
startSingleplayer(true);
m_stateStack->push<WorldSelectionState>(this);
});

m_menuWidget.addButton("Multiplayer", [this] (TextButton &) {
Expand Down Expand Up @@ -91,7 +92,7 @@ void TitleScreenState::onEvent(const sf::Event &event) {
void TitleScreenState::update() {
}

void TitleScreenState::startSingleplayer(bool showLoadingState) {
void TitleScreenState::startSingleplayer(bool showLoadingState, const std::string &save) {
auto &game = m_stateStack->push<GameState>();
game.setSingleplayer(true);

Expand All @@ -101,8 +102,12 @@ void TitleScreenState::startSingleplayer(bool showLoadingState) {
if (m_thread.joinable())
m_thread.join();

m_thread = std::thread([this] () {
m_thread = std::thread([this, save] () {
ServerApplication app{*m_eventHandler};

if (!save.empty())
app.setSaveFile(save);

app.setSingleplayer(true);
app.setPort(sf::Socket::AnyPort);
app.run();
Expand Down
2 changes: 1 addition & 1 deletion source/client/states/TitleScreenState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TitleScreenState : public InterfaceState {

void update() override;

void startSingleplayer(bool showLoadingState);
void startSingleplayer(bool showLoadingState, const std::string &save = "");
void startMultiplayer(const std::string &host);

void setTexturePack(const std::string &texturePack) { m_texturePack = texturePack; }
Expand Down
110 changes: 110 additions & 0 deletions source/client/states/WorldSelectionState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Debug.hpp>

#include <filesystem.hpp>

#include "Config.hpp"
#include "TitleScreenState.hpp"
#include "WorldSelectionState.hpp"

namespace fs = ghc::filesystem;

WorldSelectionState::WorldSelectionState(TitleScreenState *titleScreen)
: InterfaceState(titleScreen), m_titleScreen(titleScreen)
{
m_menuWidget.setScale(Config::guiScale, Config::guiScale);

m_menuWidget.addButton("New world", [this](TextButton &) {
m_stateStack->pop();
m_titleScreen->startSingleplayer(true);
});

m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setText("Cancel");
m_cancelButton.setCallback([this](TextButton &) {
m_stateStack->pop();
});

updateButtonPosition();
loadSaveList();
}

void WorldSelectionState::onEvent(const sf::Event &event) {
InterfaceState::onEvent(event);

if (event.type == sf::Event::Resized) {
updateButtonPosition();
if (!m_stateStack->empty() && &m_stateStack->top() != this)
m_menuWidget.onEvent(event);
}

if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_menuWidget.onEvent(event);
m_cancelButton.onEvent(event);
}
}

void WorldSelectionState::update() {
}

void WorldSelectionState::updateButtonPosition() {
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2.0f, Config::screenHeight * 0.85);
}

void WorldSelectionState::loadSaveList() {
if (fs::is_directory("saves")) {
fs::path basePath = fs::current_path();
fs::directory_iterator dir("saves/");
for (const auto &entry : dir) {
if (entry.is_regular_file()) {
std::string filename = entry.path().filename();
if (filename.substr(filename.find_last_of('.')) == ".dat") {
std::string saveFile = filename.substr(0, filename.find_last_of('.'));
m_menuWidget.addButton("- " + saveFile + " -", [&, saveFile](TextButton &) {
m_stateStack->pop();
m_titleScreen->startSingleplayer(true, saveFile);
});
}
}
}
}
}

void WorldSelectionState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);

if (&m_stateStack->top() == this) {
prepareDraw(target, states);

target.draw(m_menuWidget, states);
target.draw(m_cancelButton, states);
}
}

56 changes: 56 additions & 0 deletions source/client/states/WorldSelectionState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef WORLDSELECTIONSTATE_HPP_
#define WORLDSELECTIONSTATE_HPP_

#include "InterfaceState.hpp"
#include "MenuWidget.hpp"

class TitleScreenState;

class WorldSelectionState : public InterfaceState {
public:
WorldSelectionState(TitleScreenState *titleScreen);

void onEvent(const sf::Event &event) override;

void update() override;

private:
void updateButtonPosition();
void loadSaveList();

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

TitleScreenState *m_titleScreen = nullptr;

MenuWidget m_menuWidget;

TextButton m_cancelButton;
};

#endif // WORLDSELECTIONSTATE_HPP_
11 changes: 11 additions & 0 deletions source/server/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void ServerApplication::init() {
BlockGeometry::initOrientation();

m_argumentParser.addArgument("port", {"-p", "--port", "Select the port to use.", "port"});
m_argumentParser.addArgument("save", {"-s", "--save", "Select a save file to use.", "save"});
m_argumentParser.addArgument("working-dir", {"-w", "--working-dir", "Change the working direction to <dir>.", "dir"});

m_argumentParser.parse();
Expand All @@ -79,6 +80,9 @@ void ServerApplication::init() {
if (m_argumentParser.getArgument("port").isFound)
m_port = std::stoi(m_argumentParser.getArgument("port").parameter);

if (m_argumentParser.getArgument("save").isFound)
m_saveFile = m_argumentParser.getArgument("save").parameter;

ServerConfig::loadConfigFromFile("config/server.lua");

m_server.init(m_port);
Expand All @@ -103,6 +107,9 @@ void ServerApplication::init() {

if (m_eventHandler)
m_eventHandler->emplaceEvent<ServerOnlineEvent>(true, m_server.port());

if (!m_saveFile.empty())
m_worldController.load(m_saveFile);
}

int ServerApplication::run(bool isProtected) {
Expand Down Expand Up @@ -135,6 +142,10 @@ int ServerApplication::run(bool isProtected) {

gkInfo() << "Stopping server...";

if (!m_saveFile.empty()) {
m_worldController.save(m_saveFile);
}

ServerConfig::saveConfigToFile("config/server.lua");
ServerConfig::options.clear();

Expand Down
3 changes: 3 additions & 0 deletions source/server/core/ServerApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ServerApplication {

void setSingleplayer(bool isSingleplayer) { m_server.setSingleplayer(isSingleplayer); }
void setPort(u16 port) { m_port = port; }
void setSaveFile(const std::string &saveFile) { m_saveFile = saveFile; }

private:
void update();
Expand All @@ -70,6 +71,8 @@ class ServerApplication {

u16 m_port = 4242;

std::string m_saveFile;

WorldController m_worldController{m_registry, m_clock};
PlayerList m_players;

Expand Down
1 change: 1 addition & 0 deletions source/server/world/ServerChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ServerChunk : public Chunk {
void setSent(bool isSent) { m_isSent = isSent; }

bool hasBeenModified() const { return m_hasBeenModified; }
void setModified(bool hasBeenModified) { m_hasBeenModified = hasBeenModified; }

private:
std::atomic_bool m_isSent{false};
Expand Down
16 changes: 10 additions & 6 deletions source/server/world/WorldController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
*
* =====================================================================================
*/
#include <filesystem>
#include <fstream>

#include <filesystem.hpp>

#include "Registry.hpp"
#include "WorldController.hpp"

namespace fs = ghc::filesystem;

void WorldController::init(PlayerList &players) {
for (const Dimension &dimension : m_registry.dimensions()) {
m_worldList.emplace_back(players, dimension, m_clock);
Expand Down Expand Up @@ -69,7 +72,7 @@ void WorldController::load(const std::string &name) {
unsigned int chunkCount;
save >> chunkCount;

gkInfo() << "Loading dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;
// gkInfo() << "Loading dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;

for (unsigned int i = 0 ; i < chunkCount ; ++i) {
int cx, cy, cz;
Expand All @@ -92,17 +95,18 @@ void WorldController::load(const std::string &name) {

chunk.setInitialized(true);
chunk.setSent(false);
chunk.setModified(true);
}
}
}

gkInfo() << "Loading done.";
// gkInfo() << "Loading done.";
}

void WorldController::save(const std::string &name) {
gkInfo() << ("Saving '" + name + "'...").c_str();

std::filesystem::create_directory("saves");
fs::create_directory("saves");

std::ofstream file("saves/" + name + ".dat", std::ofstream::binary | std::ofstream::trunc);

Expand All @@ -129,13 +133,13 @@ void WorldController::save(const std::string &name) {
++chunkCount;
}

gkInfo() << "Saving dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;
// gkInfo() << "Saving dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;

save << chunkCount;
save.append(chunks.getData(), chunks.getDataSize());
}

file.write((const char *)save.getData(), save.getDataSize());

gkInfo() << "Saving done.";
// gkInfo() << "Saving done.";
}

0 comments on commit 18c2752

Please sign in to comment.