2020-07-10 17:49:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-20 16:13:15 +00:00
|
|
|
#include "core/world/EdittableChunk.hpp"
|
2020-08-02 20:15:53 +00:00
|
|
|
#include <memory>
|
2021-01-20 16:13:15 +00:00
|
|
|
#include "core/world/generator/Abstract.hpp"
|
2020-07-10 17:49:16 +00:00
|
|
|
|
2020-09-20 16:41:54 +00:00
|
|
|
namespace world::server {
|
|
|
|
|
2021-01-20 16:13:15 +00:00
|
|
|
using fill_edits = robin_hood::unordered_map<chunk_pos, std::vector<world::ChunkEdits::Edit>>;
|
|
|
|
|
|
|
|
/// Server side chunk
|
|
|
|
class Chunk: public world::EdittableChunk {
|
2020-07-25 16:45:03 +00:00
|
|
|
public:
|
2021-01-20 16:13:15 +00:00
|
|
|
Chunk(owner<world::ChunkEdits*>);
|
|
|
|
Chunk(owner<world::ChunkEdits*>, const chunk_pos &pos, const std::unique_ptr<generator::Abstract> &rnd);
|
2021-01-26 15:18:30 +00:00
|
|
|
Chunk(owner<world::ChunkEdits*>, std::istream& str);
|
2020-09-20 16:41:54 +00:00
|
|
|
virtual ~Chunk();
|
2020-07-10 17:49:16 +00:00
|
|
|
|
2020-09-13 13:12:04 +00:00
|
|
|
/// Set voxel from index
|
2020-08-02 20:15:53 +00:00
|
|
|
void set(chunk_voxel_idx idx, const Voxel& val);
|
2020-09-13 13:12:04 +00:00
|
|
|
/// Set voxel from position
|
2020-09-20 16:41:54 +00:00
|
|
|
void setAt(const chunk_voxel_pos& pos, const Voxel& val);
|
|
|
|
|
2020-09-13 13:12:04 +00:00
|
|
|
/// Break voxel
|
2020-09-20 16:41:54 +00:00
|
|
|
virtual std::optional<Item> replace(chunk_voxel_idx idx, const Voxel &val, float delay = 0);
|
|
|
|
|
2020-09-13 13:12:04 +00:00
|
|
|
/// Is player modified
|
2020-07-26 20:53:14 +00:00
|
|
|
inline bool isModified() const { return modified; }
|
2020-11-06 16:32:00 +00:00
|
|
|
/// Write to file and return average (majorant material)
|
|
|
|
/// Voxel swap bit indicate perfect majority
|
2021-01-26 15:18:30 +00:00
|
|
|
Voxel write(std::ostream& str) const;
|
2020-07-10 17:49:16 +00:00
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
private:
|
|
|
|
/// Modified by player
|
|
|
|
bool modified = false;
|
|
|
|
};
|
2021-01-20 16:13:15 +00:00
|
|
|
|
|
|
|
struct ChunkFactory {
|
2021-01-26 15:18:30 +00:00
|
|
|
/// Dynamic chunk using rnd
|
2021-01-20 16:13:15 +00:00
|
|
|
virtual std::shared_ptr<Chunk> create(const chunk_pos &pos, const std::unique_ptr<generator::Abstract> &rnd) const = 0;
|
2021-01-26 15:18:30 +00:00
|
|
|
/// Dynamic chunk from stream
|
2021-01-20 16:13:15 +00:00
|
|
|
virtual std::shared_ptr<Chunk> create(std::istream &str) const = 0;
|
2021-01-26 15:18:30 +00:00
|
|
|
/// Light void chunk
|
2021-01-20 16:13:15 +00:00
|
|
|
virtual std::shared_ptr<Chunk> create() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Build for server only chunk
|
|
|
|
/// Not really edittable (get/set)Edits will fail
|
|
|
|
struct StandaloneChunkFactory final: public ChunkFactory {
|
|
|
|
std::shared_ptr<Chunk> create(const chunk_pos &pos, const std::unique_ptr<generator::Abstract> &rnd) const override {
|
|
|
|
return std::make_shared<Chunk>(nullptr, pos, rnd);
|
|
|
|
}
|
|
|
|
std::shared_ptr<Chunk> create(std::istream &str) const override { return std::make_shared<Chunk>(nullptr, str); }
|
|
|
|
std::shared_ptr<Chunk> create() const override { return std::make_shared<Chunk>(nullptr); }
|
|
|
|
};
|
2020-07-25 16:45:03 +00:00
|
|
|
}
|