Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/chunk_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ namespace konstructs {
Vector3i pos(p, q, k);
int distance = (pos - player_chunk).norm();
int score;
auto it = models.find(pos);
bool is_updated = world.chunk_updated_since_requested(pos);
if(chunk_visible(planes, pos)) {
auto it = models.find(pos);
if(it != models.end()) {
/* Ok, found the model, let's render it!
*/
Expand All @@ -177,10 +178,18 @@ namespace konstructs {
c.set(translation, m->translation);
c.draw(m);
faces += m->faces;
/* We already have a model of the chunk,
* so we really don't need it.
*/
score = NO_CHUNK_FOUND;
if(is_updated) {
/* We already have a model of the chunk,
* but it's outdated, so it needs to be refreshed.
*/
score = distance / 2;
} else {
/* We already have a model of the chunk,
* it didn't change since last requested,
* so we really don't need it.
*/
score = NO_CHUNK_FOUND;
}
} else {
/* We wanted to render the model,
* but we didn't have it, so we really want this chunk!
Expand All @@ -192,7 +201,13 @@ namespace konstructs {
* but if she turns around she might need this chunk
* so let's fetch it, but not super urgent
*/
score = distance;
if(it != models.end() && !is_updated) {
/* We already have it and it's not updated */
score = NO_CHUNK_FOUND;
} else {
/* We don't have it or it's update*/
score = distance;
}
}
if(score < best_chunk_score && world.chunk_not_requested(pos)) {
best_chunk_score = score;
Expand Down
12 changes: 12 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ class Konstructs: public nanogui::Screen {
case 'T':
handle_time(packet->to_string());
break;
case 'c':
handle_chunk_updated(packet->to_string());
break;
default:
cout << "UNKNOWN: " << packet->type << endl;
break;
Expand Down Expand Up @@ -506,6 +509,15 @@ class Konstructs: public nanogui::Screen {
glfwSetTime((double)time_value);
}

void handle_chunk_updated(const string &str) {
int p,q,k;
if(sscanf(str.c_str(), ",%d,%d,%d", &p, &q, &k) != 3) {
throw std::runtime_error(str);
}
Vector3i pos(p, q, k);
world.set_chunk_updated(pos);
}

float time_of_day() {
if (day_length <= 0) {
return 0.5;
Expand Down
13 changes: 12 additions & 1 deletion src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ namespace konstructs {
void World::request_chunk(const Vector3i &pos, Client &client) {
/* Keep track of the chunk so we don't request it again */
requested.insert(pos);
/* Remove requested chunks from updated (we will get the latest update) */
updated.erase(pos);
client.chunk(pos);
}

void World::set_chunk_updated(const Vector3i &pos) {
updated.insert(pos);
}

bool World::chunk_not_requested(const Vector3i &pos) const {
return requested.find(pos) == requested.end() && chunks.find(pos) == chunks.end();
return (updated.find(pos) != updated.end() && chunks.find(pos) != chunks.end()) // Updated and exist in the world
|| (requested.find(pos) == requested.end() && chunks.find(pos) == chunks.end()); // Not requested and not in world
}

bool World::chunk_updated_since_requested(const Vector3i &pos) const {
return updated.find(pos) != updated.end();
}

void World::delete_unused_chunks(const Vector3f position, const int radi) {
Expand Down
5 changes: 4 additions & 1 deletion src/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ namespace konstructs {
class World {
public:
void request_chunk(const Vector3i &pos, Client &client);
void set_chunk_updated(const Vector3i &pos);
bool chunk_not_requested(const Vector3i &pos) const;
void delete_unused_chunks(const Vector3f position, const int radi);
bool chunk_updated_since_requested(const Vector3i &pos) const;
void delete_unused_chunks(const Vector3f position, const int radi);
void insert(std::shared_ptr<ChunkData> data);
const std::shared_ptr<ChunkData> chunk_at(const Vector3i &block_pos) const;
const std::shared_ptr<ChunkData> chunk(const Vector3i &chunk_pos) const;
Expand All @@ -23,6 +25,7 @@ void delete_unused_chunks(const Vector3f position, const int radi);
private:
std::unordered_map<Vector3i, std::shared_ptr<ChunkData>, matrix_hash<Vector3i>> chunks;
std::unordered_set<Vector3i, matrix_hash<Vector3i>> requested;
std::unordered_set<Vector3i, matrix_hash<Vector3i>> updated;
};
};

Expand Down