]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Send entire map to clients
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 11:52:38 +0000 (13:52 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 11:52:38 +0000 (13:52 +0200)
src/map.c
src/map.h
src/servercommands.c

index b0df6f3b2172bc31264590f8bdfaa59aa063721b..94d5ea8612cbe7c1d32e407462628aa251e27259 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -27,16 +27,6 @@ static MapBlock *allocate_block(v3s32 pos)
        return block;
 }
 
-static MapBlock *get_block(MapSector *sector, size_t idx)
-{
-       return ((MapBlock **) sector->blocks.ptr)[idx];
-}
-
-static MapSector *get_sector(Map *map, size_t idx)
-{
-       return ((MapSector **) map->sectors.ptr)[idx];
-}
-
 Map *map_create()
 {
        Map *map = malloc(sizeof(Map));
@@ -48,9 +38,9 @@ Map *map_create()
 void map_delete(Map *map)
 {
        for (size_t s = 0; s < map->sectors.siz; s++) {
-               MapSector *sector = get_sector(map, s);
+               MapSector *sector = map_get_sector_raw(map, s);
                for (size_t b = 0; b < sector->blocks.siz; b++)
-                       map_free_block(get_block(sector, b));
+                       map_free_block(map_get_block_raw(sector, b));
                if (sector->blocks.ptr)
                        free(sector->blocks.ptr);
                free(sector);
@@ -60,13 +50,23 @@ void map_delete(Map *map)
        free(map);
 }
 
+MapSector *map_get_sector_raw(Map *map, size_t idx)
+{
+       return ((MapSector **) map->sectors.ptr)[idx];
+}
+
+MapBlock *map_get_block_raw(MapSector *sector, size_t idx)
+{
+       return ((MapBlock **) sector->blocks.ptr)[idx];
+}
+
 MapSector *map_get_sector(Map *map, v2s32 pos, bool create)
 {
        u64 hash = ((u64) pos.x << 32) + (u64) pos.y;
        ArraySearchResult res = array_search(&map->sectors, &hash);
 
        if (res.success)
-               return get_sector(map, res.index);
+               return map_get_sector_raw(map, res.index);
        if (! create)
                return NULL;
 
@@ -92,7 +92,7 @@ MapBlock *map_get_block(Map *map, v3s32 pos, bool create)
        MapBlock *block = NULL;
 
        if (res.success) {
-               block = get_block(sector, res.index);
+               block = map_get_block_raw(sector, res.index);
        } else if (create) {
                block = allocate_block(pos);
                array_insert(&sector->blocks, &block, res.index);
@@ -155,7 +155,7 @@ bool map_deserialize_block(int fd, Map *map, bool dummy)
        if (dummy) {
                block = allocate_block(pos);
        } else if (res.success) {
-               block = get_block(sector, res.index);
+               block = map_get_block_raw(sector, res.index);
        } else {
                block = allocate_block(pos);
                array_insert(&sector->blocks, &block, res.index);
@@ -181,9 +181,9 @@ bool map_deserialize_block(int fd, Map *map, bool dummy)
 bool map_serialize(int fd, Map *map)
 {
        for (size_t s = 0; s < map->sectors.siz; s++) {
-               MapSector *sector = get_sector(map, s);
+               MapSector *sector = map_get_sector_raw(map, s);
                for (size_t b = 0; b < sector->blocks.siz; b++)
-                       if (! map_serialize_block(fd, get_block(sector, b)))
+                       if (! map_serialize_block(fd, map_get_block_raw(sector, b)))
                                return false;
        }
        return true;
index d610d5c131bf50490b3fe869b3505d21d5c5c14b..2341379b919125ac0404102cf97ba87e2936b2c1 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -43,6 +43,8 @@ typedef struct
 Map *map_create();
 void map_delete(Map *map);
 
+MapSector *map_get_sector_raw(Map *map, size_t idx);
+MapBlock *map_get_block_raw(MapSector *sector, size_t idx);
 MapSector *map_get_sector(Map *map, v2s32 pos, bool create);
 MapBlock *map_get_block(Map *map, v3s32 pos, bool create);
 
index 0e343b3b63ac2423769dbb39cb291d60bbe1f382..1d9e241f9f624db646550fa92835e6f11e03b442 100644 (file)
@@ -10,6 +10,17 @@ static bool disconnect_handler(Client *client, bool good)
        return true;
 }
 
+static bool send_map(Client *client)
+{
+       for (size_t s = 0; s < client->server->map->sectors.siz; s++) {
+               MapSector *sector = map_get_sector_raw(client->server->map, s);
+               for (size_t b = 0; b < sector->blocks.siz; b++)
+                       if (! (write_u32(client->fd, CC_BLOCK) && map_serialize_block(client->fd, map_get_block_raw(sector, b))))
+                               return false;
+       }
+       return true;
+}
+
 static bool auth_handler(Client *client, bool good)
 {
        char *name = read_string(client->fd, NAME_MAX);
@@ -34,7 +45,7 @@ static bool auth_handler(Client *client, bool good)
        }
 
        pthread_mutex_lock(&client->mtx);
-       bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
+       bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success) && send_map(client);
        pthread_mutex_unlock(&client->mtx);
 
        return ret;