Skip to content
This repository has been archived by the owner on Feb 18, 2020. It is now read-only.

Commit

Permalink
Implemented world savegame
Browse files Browse the repository at this point in the history
  • Loading branch information
kperdlich committed Jan 19, 2018
1 parent 293dea5 commit fef6668
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 22 deletions.
Binary file modified WoxelCraft.dol
Binary file not shown.
Binary file modified WoxelCraft.elf
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#define DEFAULT_FONT_ID 0
#define DEFAULT_MINECRAFT_FONT_ID 1

#define FILE_PATH "/apps/WoxelCraft"
#define WORLD_PATH FILE_PATH "/world"

#define LOG_FILE FILE_PATH "/Log.txt"
#define SEED_FILE WORLD_PATH "/Seed.dat"

#define DEBUG

class Engine {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

void Debug::Init()
{
m_file.open("Log.txt");
m_file.open(LOG_FILE);
}

void Debug::Log(const char* format, ...)
Expand Down
8 changes: 6 additions & 2 deletions src/utils/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fat.h>
#include <dirent.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <iostream>
#include <assert.h>

Expand All @@ -31,6 +32,9 @@
void FileSystem::Init()
{
assert(fatInitDefault());

if (!DirectoryExist(WORLD_PATH))
CreateDirectory(WORLD_PATH);
}

bool FileSystem::CreateDirectory(const std::string& directoryPath)
Expand All @@ -45,8 +49,8 @@ bool FileSystem::CreateDirectory(const std::string& directoryPath)

bool FileSystem::DirectoryExist(const std::string& directoryPath)
{
// todo implement
return false;
struct stat sb;
return stat(directoryPath.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
}

bool FileSystem::FileExist(const std::string &filePath)
Expand Down
31 changes: 29 additions & 2 deletions src/world/GameWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ GameWorld::GameWorld()
m_blockManager = new BlockManager();
m_blockManager->LoadBlocks();

srand (time(nullptr));
m_noise.Set(.10, .1, .5, 6.0, rand());
SetSeed();
}

GameWorld::~GameWorld()
Expand Down Expand Up @@ -157,6 +156,34 @@ void GameWorld::DrawFocusOnSelectedCube()
}
}

void GameWorld::SetSeed()
{
srand (time(nullptr));
int seed = rand();

if (FileSystem::FileExist(SEED_FILE))
{
std::ifstream file;
file.open(SEED_FILE);
if (file.is_open())
{
std::string line;
std::getline(file, line);
seed = std::atoi(line.c_str());
file.close();
}
}
else
{
std::ofstream stream(SEED_FILE);
stream << seed << '\n';
stream.flush();
stream.close();
}

m_noise.Set(.10, .1, .5, 6.0, seed);
}

PerlinNoise GameWorld::GetNoise() const
{
return m_noise;
Expand Down
1 change: 1 addition & 0 deletions src/world/GameWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class GameWorld {

private:
void DrawFocusOnSelectedCube();
void SetSeed();

private:

Expand Down
2 changes: 1 addition & 1 deletion src/world/chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Vector3 Chunk::GetPhysicalPosition(const Vector3& position) const
std::string Chunk::GetFilePath() const
{
std::ostringstream filename;
filename << "World/";
filename << WORLD_PATH "/";
filename << m_centerPosition.GetX();
filename << '_';
filename << m_centerPosition.GetY();
Expand Down
40 changes: 24 additions & 16 deletions src/world/chunk/jobs/SerializationJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ void* QueueJob(void* data)

while(true)
{
if (queue->IsEmpty())
{
if(thread->Stop())
{
break;
}
else
{

if (queue->IsEmpty())
{
if(thread->Stop())
{
break;
}
else
{
LWP_SuspendThread(LWP_GetSelf());
}
}
}
else
{
Expand All @@ -55,10 +56,11 @@ void* QueueJob(void* data)

std::ifstream file;
file.open(filename);
bool serialized = false;
bool bReplaced = false;

if (file.is_open())
{
std::vector<std::string> fileContent;
std::ostringstream search;
search << "X";
search << blockData.BlockPosition.X;
Expand All @@ -76,32 +78,38 @@ void* QueueJob(void* data)
if(pos!=std::string::npos)
{
search << static_cast<unsigned short>(blockData.Type);
search << '\n';
line.replace(pos, search.str().length(), search.str());
//LOG("Replace File %s -> Line: %s : SearchLine %s", filename.c_str(), line.c_str(), search.str().c_str());
serialized = true;
break;
bReplaced = true;
}
fileContent.push_back(line);
}

file.close();

if(!serialized)
if(bReplaced)
{
std::ofstream stream(filename, std::ofstream::out | std::ofstream::trunc);
for (auto& s : fileContent)
stream << s << '\n';
stream.flush();
stream.close();
}
else
{
std::ofstream stream(filename, std::ios_base::app | std::ios_base::out);
stream << "X" << blockData.BlockPosition.X << "Y" << blockData.BlockPosition.Y << "Z" << blockData.BlockPosition.Z << ":" << static_cast<unsigned short>(blockData.Type) << '\n';
stream.flush();
stream.close();
//LOG("Add Line into file %s", filename.c_str());
}
fileContent.clear();
}
else
{
std::ofstream stream(filename);

stream << blockData.ChunkPosition.GetX() << ';' << blockData.ChunkPosition.GetY() << ';' << blockData.ChunkPosition.GetZ() << '\n';
stream << "X" << blockData.BlockPosition.X << "Y" << blockData.BlockPosition.Y << "Z" << blockData.BlockPosition.Z << ":" << static_cast<unsigned short>(blockData.Type) << '\n';

stream.flush();
stream.close();
//LOG("Create File %s", filename.c_str());
Expand Down

0 comments on commit fef6668

Please sign in to comment.