2022-12-17 06:30:52 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) 2022 ByteBit/xtreme8000
|
|
|
|
|
|
|
|
This file is part of CavEX.
|
|
|
|
|
|
|
|
CavEX is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
CavEX is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with CavEX. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-22 15:39:32 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-12-07 16:53:08 -05:00
|
|
|
#include "../cglm/cglm.h"
|
|
|
|
|
2022-12-17 06:09:30 -05:00
|
|
|
#include "../item/inventory.h"
|
2023-03-27 12:58:52 -04:00
|
|
|
#include "../platform/thread.h"
|
2022-11-22 15:39:32 -05:00
|
|
|
#include "client_interface.h"
|
2022-11-30 06:07:12 -05:00
|
|
|
#include "server_interface.h"
|
2022-11-22 15:39:32 -05:00
|
|
|
#include "server_local.h"
|
|
|
|
|
2022-11-30 06:07:12 -05:00
|
|
|
#define CHUNK_DIST2(x1, x2, z1, z2) \
|
|
|
|
(((x1) - (x2)) * ((x1) - (x2)) + ((z1) - (z2)) * ((z1) - (z2)))
|
|
|
|
|
|
|
|
static void server_local_process(struct server_rpc* call, void* user) {
|
|
|
|
assert(call && user);
|
|
|
|
|
|
|
|
struct server_local* s = user;
|
|
|
|
|
|
|
|
switch(call->type) {
|
|
|
|
case SRPC_PLAYER_POS:
|
2022-12-17 06:09:30 -05:00
|
|
|
if(s->player.finished_loading) {
|
|
|
|
s->player.x = call->payload.player_pos.x;
|
|
|
|
s->player.y = call->payload.player_pos.y;
|
|
|
|
s->player.z = call->payload.player_pos.z;
|
2023-02-11 11:18:11 -05:00
|
|
|
s->player.rx = call->payload.player_pos.rx;
|
|
|
|
s->player.ry = call->payload.player_pos.ry;
|
2022-12-17 06:09:30 -05:00
|
|
|
s->player.has_pos = true;
|
|
|
|
}
|
2023-02-04 17:47:37 -05:00
|
|
|
break;
|
2023-05-02 18:01:32 -04:00
|
|
|
case SRPC_HOTBAR_SLOT:
|
|
|
|
if(s->player.has_pos
|
|
|
|
&& call->payload.hotbar_slot.slot < INVENTORY_SIZE_HOTBAR)
|
|
|
|
inventory_set_hotbar(&s->player.inventory,
|
|
|
|
call->payload.hotbar_slot.slot);
|
|
|
|
break;
|
|
|
|
case SRPC_BLOCK_DIG:
|
|
|
|
if(s->player.has_pos && call->payload.block_place.y >= 0
|
|
|
|
&& call->payload.block_place.y < WORLD_HEIGHT) {
|
|
|
|
server_world_set_block(&s->world, call->payload.block_dig.x,
|
|
|
|
call->payload.block_dig.y,
|
|
|
|
call->payload.block_dig.z,
|
|
|
|
(struct block_data) {
|
|
|
|
.type = BLOCK_AIR,
|
|
|
|
.metadata = 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SRPC_BLOCK_PLACE:
|
|
|
|
if(s->player.has_pos && call->payload.block_place.y >= 0
|
|
|
|
&& call->payload.block_place.y < WORLD_HEIGHT) {
|
|
|
|
int x, y, z;
|
|
|
|
blocks_side_offset(call->payload.block_place.side, &x, &y, &z);
|
|
|
|
|
|
|
|
size_t slot = inventory_get_hotbar(&s->player.inventory);
|
|
|
|
|
|
|
|
struct item_data it_data;
|
|
|
|
inventory_get_slot(&s->player.inventory, slot, &it_data);
|
|
|
|
struct item* it = item_get(&it_data);
|
|
|
|
|
|
|
|
if(it && it->onItemPlace) {
|
|
|
|
struct block_data blk_where, blk_on;
|
|
|
|
if(server_world_get_block(
|
|
|
|
&s->world, call->payload.block_place.x + x,
|
|
|
|
call->payload.block_place.y + y,
|
|
|
|
call->payload.block_place.z + z, &blk_where)
|
|
|
|
&& server_world_get_block(
|
|
|
|
&s->world, call->payload.block_place.x,
|
|
|
|
call->payload.block_place.y,
|
|
|
|
call->payload.block_place.z, &blk_on)) {
|
|
|
|
struct block_info where = (struct block_info) {
|
|
|
|
.block = &blk_where,
|
|
|
|
.neighbours = NULL,
|
|
|
|
.x = call->payload.block_place.x + x,
|
|
|
|
.y = call->payload.block_place.y + y,
|
|
|
|
.z = call->payload.block_place.z + z,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct block_info on = (struct block_info) {
|
|
|
|
.block = &blk_on,
|
|
|
|
.neighbours = NULL,
|
|
|
|
.x = call->payload.block_place.x,
|
|
|
|
.y = call->payload.block_place.y,
|
|
|
|
.z = call->payload.block_place.z,
|
|
|
|
};
|
|
|
|
|
|
|
|
if((!blocks[blk_where.type]
|
|
|
|
|| blocks[blk_where.type]->place_ignore)
|
|
|
|
&& it->onItemPlace(s, &it_data, &where, &on,
|
|
|
|
call->payload.block_place.side)) {
|
|
|
|
inventory_consume(&s->player.inventory, slot);
|
|
|
|
|
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_INVENTORY_SLOT,
|
|
|
|
.payload.inventory_slot.slot = slot,
|
|
|
|
.payload.inventory_slot.item
|
|
|
|
= s->player.inventory.items[slot],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-11 10:19:09 -05:00
|
|
|
}
|
|
|
|
break;
|
2023-02-04 17:47:37 -05:00
|
|
|
case SRPC_UNLOAD_WORLD:
|
|
|
|
// save chunks here, then destroy all
|
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_WORLD_RESET,
|
2023-02-05 10:17:28 -05:00
|
|
|
.payload.world_reset.dimension = WORLD_DIM_OVERWORLD,
|
2023-02-04 17:47:37 -05:00
|
|
|
});
|
|
|
|
|
2023-02-11 11:18:11 -05:00
|
|
|
level_archive_write_player(
|
|
|
|
&s->level, (vec3) {s->player.x, s->player.y, s->player.z},
|
|
|
|
(vec2) {s->player.rx, s->player.ry}, NULL, s->player.dimension);
|
|
|
|
|
2023-02-07 15:51:56 -05:00
|
|
|
server_world_destroy(&s->world);
|
2023-02-04 17:47:37 -05:00
|
|
|
level_archive_destroy(&s->level);
|
|
|
|
|
|
|
|
s->player.has_pos = false;
|
|
|
|
s->player.finished_loading = false;
|
|
|
|
string_reset(s->level_name);
|
|
|
|
break;
|
|
|
|
case SRPC_LOAD_WORLD:
|
2023-02-07 15:51:56 -05:00
|
|
|
assert(!s->player.has_pos);
|
2023-02-04 17:47:37 -05:00
|
|
|
|
|
|
|
string_set(s->level_name, call->payload.load_world.name);
|
|
|
|
string_clear(call->payload.load_world.name);
|
|
|
|
|
|
|
|
if(level_archive_create(&s->level, s->level_name)) {
|
|
|
|
vec3 pos;
|
2023-02-11 11:18:11 -05:00
|
|
|
vec2 rot;
|
2023-02-05 10:17:28 -05:00
|
|
|
enum world_dim dim;
|
2023-02-11 11:18:11 -05:00
|
|
|
if(level_archive_read_player(&s->level, pos, rot, NULL, &dim)) {
|
2023-02-07 15:51:56 -05:00
|
|
|
server_world_create(&s->world, s->level_name, dim);
|
2023-02-04 17:47:37 -05:00
|
|
|
s->player.x = pos[0];
|
|
|
|
s->player.y = pos[1];
|
|
|
|
s->player.z = pos[2];
|
2023-02-11 11:18:11 -05:00
|
|
|
s->player.rx = rot[0];
|
|
|
|
s->player.ry = rot[1];
|
2023-02-05 10:17:28 -05:00
|
|
|
s->player.dimension = dim;
|
2023-02-04 17:47:37 -05:00
|
|
|
s->player.has_pos = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(level_archive_read(&s->level, LEVEL_TIME, &s->world_time, 0))
|
|
|
|
s->world_time_start = time_get();
|
|
|
|
|
2023-02-05 10:17:28 -05:00
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_WORLD_RESET,
|
|
|
|
.payload.world_reset.dimension = dim,
|
|
|
|
});
|
|
|
|
}
|
2022-11-30 06:07:12 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_local_update(struct server_local* s) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
svin_process_messages(server_local_process, s, false);
|
|
|
|
|
2022-12-01 16:07:08 -05:00
|
|
|
if(!s->player.has_pos)
|
|
|
|
return;
|
|
|
|
|
2022-11-22 15:39:32 -05:00
|
|
|
w_coord_t px = WCOORD_CHUNK_OFFSET(floor(s->player.x));
|
|
|
|
w_coord_t pz = WCOORD_CHUNK_OFFSET(floor(s->player.z));
|
|
|
|
|
2023-02-07 15:51:56 -05:00
|
|
|
w_coord_t cx, cz;
|
|
|
|
if(server_world_furthest_chunk(&s->world, MAX_VIEW_DISTANCE, px, pz, &cx,
|
|
|
|
&cz)) {
|
2022-11-30 06:07:12 -05:00
|
|
|
// unload just one chunk
|
2023-02-07 15:51:56 -05:00
|
|
|
server_world_save_chunk(&s->world, true, cx, cz);
|
2022-11-30 06:07:12 -05:00
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_UNLOAD_CHUNK,
|
2023-02-07 15:51:56 -05:00
|
|
|
.payload.unload_chunk.x = cx,
|
|
|
|
.payload.unload_chunk.z = cz,
|
2022-11-30 06:07:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:39:32 -05:00
|
|
|
// iterate over all chunks that should be loaded
|
2022-11-30 06:07:12 -05:00
|
|
|
bool c_nearest = false;
|
|
|
|
w_coord_t c_nearest_x, c_nearest_z;
|
|
|
|
w_coord_t c_nearest_dist2;
|
2023-02-07 15:51:56 -05:00
|
|
|
for(w_coord_t z = pz - MAX_VIEW_DISTANCE; z <= pz + MAX_VIEW_DISTANCE;
|
|
|
|
z++) {
|
|
|
|
for(w_coord_t x = px - MAX_VIEW_DISTANCE; x <= px + MAX_VIEW_DISTANCE;
|
|
|
|
x++) {
|
|
|
|
w_coord_t d = CHUNK_DIST2(px, x, pz, z);
|
|
|
|
if(!server_world_is_chunk_loaded(&s->world, x, z)
|
|
|
|
&& (d < c_nearest_dist2 || !c_nearest)
|
|
|
|
&& server_world_disk_has_chunk(&s->world, x, z)) {
|
|
|
|
c_nearest_dist2 = d;
|
|
|
|
c_nearest_x = x;
|
|
|
|
c_nearest_z = z;
|
|
|
|
c_nearest = true;
|
2022-11-22 15:39:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 06:07:12 -05:00
|
|
|
|
|
|
|
// load just one chunk
|
2023-02-07 15:51:56 -05:00
|
|
|
struct server_chunk* sc;
|
|
|
|
if(c_nearest
|
|
|
|
&& server_world_load_chunk(&s->world, c_nearest_x, c_nearest_z, &sc)) {
|
2023-02-11 10:19:09 -05:00
|
|
|
size_t sz = CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT;
|
|
|
|
void* ids = malloc(sz);
|
|
|
|
void* metadata = malloc(sz / 2);
|
|
|
|
void* lighting_sky = malloc(sz / 2);
|
|
|
|
void* lighting_torch = malloc(sz / 2);
|
|
|
|
|
|
|
|
memcpy(ids, sc->ids, sz);
|
|
|
|
memcpy(metadata, sc->metadata, sz / 2);
|
|
|
|
memcpy(lighting_sky, sc->lighting_sky, sz / 2);
|
|
|
|
memcpy(lighting_torch, sc->lighting_torch, sz / 2);
|
2022-12-01 16:07:08 -05:00
|
|
|
|
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_CHUNK,
|
|
|
|
.payload.chunk.x = c_nearest_x * CHUNK_SIZE,
|
|
|
|
.payload.chunk.y = 0,
|
|
|
|
.payload.chunk.z = c_nearest_z * CHUNK_SIZE,
|
|
|
|
.payload.chunk.sx = CHUNK_SIZE,
|
|
|
|
.payload.chunk.sy = WORLD_HEIGHT,
|
|
|
|
.payload.chunk.sz = CHUNK_SIZE,
|
|
|
|
.payload.chunk.ids = ids,
|
|
|
|
.payload.chunk.metadata = metadata,
|
2023-02-07 15:51:56 -05:00
|
|
|
.payload.chunk.lighting_sky = lighting_sky,
|
|
|
|
.payload.chunk.lighting_torch = lighting_torch,
|
2022-12-01 16:07:08 -05:00
|
|
|
});
|
2022-12-17 06:09:30 -05:00
|
|
|
} else if(!s->player.finished_loading) {
|
|
|
|
struct client_rpc pos;
|
|
|
|
pos.type = CRPC_PLAYER_POS;
|
|
|
|
if(level_archive_read_player(&s->level, pos.payload.player_pos.position,
|
2023-02-05 10:17:28 -05:00
|
|
|
pos.payload.player_pos.rotation, NULL,
|
|
|
|
NULL))
|
2022-12-17 06:09:30 -05:00
|
|
|
clin_rpc_send(&pos);
|
|
|
|
|
2022-12-19 18:09:57 -05:00
|
|
|
s->world_time_start = time_get();
|
|
|
|
|
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_TIME_SET,
|
|
|
|
.payload.time_set = s->world_time,
|
|
|
|
});
|
|
|
|
|
2023-05-02 18:01:32 -04:00
|
|
|
if(level_archive_read_inventory(&s->level, &s->player.inventory)) {
|
2022-12-17 06:09:30 -05:00
|
|
|
for(size_t k = 0; k < INVENTORY_SIZE; k++) {
|
2023-05-02 18:01:32 -04:00
|
|
|
if(s->player.inventory.items[k].id > 0) {
|
2022-12-17 06:09:30 -05:00
|
|
|
clin_rpc_send(&(struct client_rpc) {
|
|
|
|
.type = CRPC_INVENTORY_SLOT,
|
|
|
|
.payload.inventory_slot.slot = k,
|
2023-05-02 18:01:32 -04:00
|
|
|
.payload.inventory_slot.item
|
|
|
|
= s->player.inventory.items[k],
|
2022-12-17 06:09:30 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->player.finished_loading = true;
|
2022-11-30 06:07:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* server_local_thread(void* user) {
|
|
|
|
while(1) {
|
|
|
|
server_local_update(user);
|
2023-03-27 12:58:52 -04:00
|
|
|
thread_msleep(50);
|
2022-11-30 06:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_local_create(struct server_local* s) {
|
|
|
|
assert(s);
|
2022-12-19 18:09:57 -05:00
|
|
|
s->world_time = 0;
|
|
|
|
s->world_time_start = time_get();
|
2022-12-01 16:07:08 -05:00
|
|
|
s->player.has_pos = false;
|
2022-12-17 06:09:30 -05:00
|
|
|
s->player.finished_loading = false;
|
2023-02-04 17:47:37 -05:00
|
|
|
string_init(s->level_name);
|
2023-05-02 18:01:32 -04:00
|
|
|
inventory_clear(&s->player.inventory);
|
2022-12-19 18:09:57 -05:00
|
|
|
|
2023-03-27 12:58:52 -04:00
|
|
|
struct thread t;
|
|
|
|
thread_create(&t, server_local_thread, s, 8);
|
2022-11-22 15:39:32 -05:00
|
|
|
}
|