diff --git a/code/game/src/packets/pkt_00_init.c b/code/game/src/packets/pkt_00_init.c index ef5b121..8f18b52 100644 --- a/code/game/src/packets/pkt_00_init.c +++ b/code/game/src/packets/pkt_00_init.c @@ -34,6 +34,6 @@ int32_t pkt_00_init_handler(pkt_header *header) { uint64_t peer_id = (uint64_t)header->udata; uint64_t ent_id = player_spawn(NULL); ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = ent_id, .view_id = header->view_id }); - pkt_01_welcome_send(peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount()); + pkt_01_welcome_send(world_seed(), peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount()); return 0; } diff --git a/code/game/src/packets/pkt_01_welcome.c b/code/game/src/packets/pkt_01_welcome.c index 3b36316..0444cdc 100644 --- a/code/game/src/packets/pkt_01_welcome.c +++ b/code/game/src/packets/pkt_01_welcome.c @@ -6,6 +6,7 @@ #include "camera.h" pkt_desc pkt_01_welcome_desc[] = { + { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, seed) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, ent_id) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, world_size) }, @@ -18,12 +19,13 @@ size_t pkt_01_welcome_encode(pkt_01_welcome *table) { pkt_pack_struct(&pc, pkt_01_welcome_desc, PKT_STRUCT_PTR(table)); return pkt_pack_msg_size(&pc); } -size_t pkt_01_welcome_send(uint64_t peer_id, +size_t pkt_01_welcome_send(uint32_t seed, + uint64_t peer_id, uint16_t view_id, uint64_t ent_id, uint16_t chunk_size, uint16_t world_size) { - pkt_01_welcome table = {.ent_id = ent_id, .chunk_size = chunk_size, .world_size = world_size}; + pkt_01_welcome table = {.seed = seed, .ent_id = ent_id, .chunk_size = chunk_size, .world_size = world_size}; return pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, view_id, (void*)peer_id); } @@ -32,9 +34,9 @@ int32_t pkt_01_welcome_handler(pkt_header *header) { PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, pkt_pack_desc_args(pkt_01_welcome_desc), PKT_STRUCT_PTR(&table))); world_view *view = game_world_view_get(header->view_id); - + zpl_printf("[INFO] initializing read-only world view ...\n"); - world_view_init(view, table.ent_id, table.chunk_size, table.world_size); + world_view_init(view, table.seed, table.ent_id, table.chunk_size, table.world_size); game_world_view_set_active(view); return 0; } diff --git a/code/game/src/packets/pkt_01_welcome.h b/code/game/src/packets/pkt_01_welcome.h index f689868..d88af6c 100644 --- a/code/game/src/packets/pkt_01_welcome.h +++ b/code/game/src/packets/pkt_01_welcome.h @@ -3,12 +3,14 @@ #include "packet_utils.h" typedef struct { + uint32_t seed; uint64_t ent_id; uint16_t chunk_size; uint16_t world_size; } pkt_01_welcome; -size_t pkt_01_welcome_send(uint64_t peer_id, +size_t pkt_01_welcome_send(uint32_t seed, + uint64_t peer_id, uint16_t view_id, uint64_t ent_id, uint16_t chunk_size, diff --git a/code/game/src/world/blocks.c b/code/game/src/world/blocks.c index 2a62acb..2689414 100644 --- a/code/game/src/world/blocks.c +++ b/code/game/src/world/blocks.c @@ -5,6 +5,7 @@ #include "raylib.h" #include "gen/texgen.h" #include "world_view.h" +#include "perlin.h" #define BLOCKS_COUNT (sizeof(blocks)/sizeof(block)) @@ -111,7 +112,7 @@ void blocks_build_chunk_tex(uint64_t id, uint8_t *chunk_blocks, uint8_t *outer_c DrawTexturePro(blk, src, dst, (Vector2){0.0f,0.0f}, 0.0f, WHITE); #else static float rots[] = { 0.0f, 90.0f, 180.f, 270.0f }; - float rot = rots[rand() % 4]; + float rot = rots[(int32_t)(perlin_fbm(view->seed, x, y, 1.2f, 3) * 4.0f) % 4]; float half_block = WORLD_BLOCK_SIZE / 2.0f; Texture2D blk = blocks[chunk_blocks[(y*view->chunk_size)+x]].img; Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE}; diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 61bbcf5..98da76b 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -318,6 +318,10 @@ uint32_t world_buf(uint8_t const **ptr, uint32_t *width) { return world.size; } +uint32_t world_seed(void) { + return world.seed; +} + ecs_world_t * world_ecs() { if (world.ecs_stage != NULL) { return world.ecs_stage; diff --git a/code/game/src/world/world.h b/code/game/src/world/world.h index b9ca930..f184d0a 100644 --- a/code/game/src/world/world.h +++ b/code/game/src/world/world.h @@ -55,6 +55,7 @@ int32_t world_read(void* data, uint32_t datalen, void *udata); int32_t world_write(pkt_header *pkt, void *udata); uint32_t world_buf(uint8_t const **ptr, uint32_t *width); +uint32_t world_seed(void); ecs_world_t *world_ecs(void); void world_set_stage(ecs_world_t *ecs); librg_world *world_tracker(void); diff --git a/code/game/src/world_view.c b/code/game/src/world_view.c index e09fc49..ece0bb1 100644 --- a/code/game/src/world_view.c +++ b/code/game/src/world_view.c @@ -75,7 +75,8 @@ world_view world_view_create(uint16_t view_id) { return view; } -void world_view_init(world_view *view, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount) { +void world_view_init(world_view *view, uint32_t seed, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount) { + view->seed = seed; view->owner_id = ent_id; view->chunk_size = chunk_size; view->chunk_amount = chunk_amount; diff --git a/code/game/src/world_view.h b/code/game/src/world_view.h index 7bd4e9d..3cc2485 100644 --- a/code/game/src/world_view.h +++ b/code/game/src/world_view.h @@ -9,6 +9,7 @@ typedef struct { entity_view_tbl entities; librg_world *tracker; + uint32_t seed; uint32_t size; uint32_t dim; uint16_t chunk_size; @@ -21,5 +22,5 @@ typedef struct { } world_view; world_view world_view_create(uint16_t view_id); -void world_view_init(world_view *view, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount); +void world_view_init(world_view *view, uint32_t seed, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount); void world_view_destroy(world_view *view);