Skip to content

Commit d2802d2

Browse files
Merge pull request #177 from konstructs/feature-handle-new-chunk-updates
Handle server updates more intelligently
2 parents 8d6fc67 + 8e9074f commit d2802d2

4 files changed

Lines changed: 49 additions & 8 deletions

File tree

src/chunk_shader.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ namespace konstructs {
167167
Vector3i pos(p, q, k);
168168
int distance = (pos - player_chunk).norm();
169169
int score;
170+
auto it = models.find(pos);
171+
bool is_updated = world.chunk_updated_since_requested(pos);
170172
if(chunk_visible(planes, pos)) {
171-
auto it = models.find(pos);
172173
if(it != models.end()) {
173174
/* Ok, found the model, let's render it!
174175
*/
@@ -177,10 +178,18 @@ namespace konstructs {
177178
c.set(translation, m->translation);
178179
c.draw(m);
179180
faces += m->faces;
180-
/* We already have a model of the chunk,
181-
* so we really don't need it.
182-
*/
183-
score = NO_CHUNK_FOUND;
181+
if(is_updated) {
182+
/* We already have a model of the chunk,
183+
* but it's outdated, so it needs to be refreshed.
184+
*/
185+
score = distance / 2;
186+
} else {
187+
/* We already have a model of the chunk,
188+
* it didn't change since last requested,
189+
* so we really don't need it.
190+
*/
191+
score = NO_CHUNK_FOUND;
192+
}
184193
} else {
185194
/* We wanted to render the model,
186195
* but we didn't have it, so we really want this chunk!
@@ -192,7 +201,13 @@ namespace konstructs {
192201
* but if she turns around she might need this chunk
193202
* so let's fetch it, but not super urgent
194203
*/
195-
score = distance;
204+
if(it != models.end() && !is_updated) {
205+
/* We already have it and it's not updated */
206+
score = NO_CHUNK_FOUND;
207+
} else {
208+
/* We don't have it or it's update*/
209+
score = distance;
210+
}
196211
}
197212
if(score < best_chunk_score && world.chunk_not_requested(pos)) {
198213
best_chunk_score = score;

src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ class Konstructs: public nanogui::Screen {
366366
case 'T':
367367
handle_time(packet->to_string());
368368
break;
369+
case 'c':
370+
handle_chunk_updated(packet->to_string());
371+
break;
369372
default:
370373
cout << "UNKNOWN: " << packet->type << endl;
371374
break;
@@ -506,6 +509,15 @@ class Konstructs: public nanogui::Screen {
506509
glfwSetTime((double)time_value);
507510
}
508511

512+
void handle_chunk_updated(const string &str) {
513+
int p,q,k;
514+
if(sscanf(str.c_str(), ",%d,%d,%d", &p, &q, &k) != 3) {
515+
throw std::runtime_error(str);
516+
}
517+
Vector3i pos(p, q, k);
518+
world.set_chunk_updated(pos);
519+
}
520+
509521
float time_of_day() {
510522
if (day_length <= 0) {
511523
return 0.5;

src/world.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ namespace konstructs {
44
void World::request_chunk(const Vector3i &pos, Client &client) {
55
/* Keep track of the chunk so we don't request it again */
66
requested.insert(pos);
7+
/* Remove requested chunks from updated (we will get the latest update) */
8+
updated.erase(pos);
79
client.chunk(pos);
810
}
911

12+
void World::set_chunk_updated(const Vector3i &pos) {
13+
updated.insert(pos);
14+
}
15+
1016
bool World::chunk_not_requested(const Vector3i &pos) const {
11-
return requested.find(pos) == requested.end() && chunks.find(pos) == chunks.end();
17+
return (updated.find(pos) != updated.end() && chunks.find(pos) != chunks.end()) // Updated and exist in the world
18+
|| (requested.find(pos) == requested.end() && chunks.find(pos) == chunks.end()); // Not requested and not in world
19+
}
20+
21+
bool World::chunk_updated_since_requested(const Vector3i &pos) const {
22+
return updated.find(pos) != updated.end();
1223
}
1324

1425
void World::delete_unused_chunks(const Vector3f position, const int radi) {

src/world.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ namespace konstructs {
1212
class World {
1313
public:
1414
void request_chunk(const Vector3i &pos, Client &client);
15+
void set_chunk_updated(const Vector3i &pos);
1516
bool chunk_not_requested(const Vector3i &pos) const;
16-
void delete_unused_chunks(const Vector3f position, const int radi);
17+
bool chunk_updated_since_requested(const Vector3i &pos) const;
18+
void delete_unused_chunks(const Vector3f position, const int radi);
1719
void insert(std::shared_ptr<ChunkData> data);
1820
const std::shared_ptr<ChunkData> chunk_at(const Vector3i &block_pos) const;
1921
const std::shared_ptr<ChunkData> chunk(const Vector3i &chunk_pos) const;
@@ -23,6 +25,7 @@ void delete_unused_chunks(const Vector3f position, const int radi);
2325
private:
2426
std::unordered_map<Vector3i, std::shared_ptr<ChunkData>, matrix_hash<Vector3i>> chunks;
2527
std::unordered_set<Vector3i, matrix_hash<Vector3i>> requested;
28+
std::unordered_set<Vector3i, matrix_hash<Vector3i>> updated;
2629
};
2730
};
2831

0 commit comments

Comments
 (0)