mirror of
https://github.com/p2r3/bareiron.git
synced 2025-10-02 07:35:08 +02:00
prevent players from leaving world boundaries
This commit is contained in:
@@ -76,7 +76,7 @@ typedef struct {
|
|||||||
char name[16];
|
char name[16];
|
||||||
int client_fd;
|
int client_fd;
|
||||||
short x;
|
short x;
|
||||||
short y;
|
uint8_t y;
|
||||||
short z;
|
short z;
|
||||||
short visited_x[VISITED_HISTORY];
|
short visited_x[VISITED_HISTORY];
|
||||||
short visited_z[VISITED_HISTORY];
|
short visited_z[VISITED_HISTORY];
|
||||||
@@ -94,6 +94,7 @@ typedef struct {
|
|||||||
uint8_t inventory_count[41];
|
uint8_t inventory_count[41];
|
||||||
uint8_t craft_count[9];
|
uint8_t craft_count[9];
|
||||||
// 0x01 - attack cooldown
|
// 0x01 - attack cooldown
|
||||||
|
// 0x02 - has not spawned yet
|
||||||
uint8_t flags;
|
uint8_t flags;
|
||||||
} PlayerData;
|
} PlayerData;
|
||||||
|
|
||||||
|
11
src/main.c
11
src/main.c
@@ -167,7 +167,7 @@ void handlePacket (int client_fd, int length, int packet_id) {
|
|||||||
|
|
||||||
// Handle fall damage
|
// Handle fall damage
|
||||||
if (on_ground) {
|
if (on_ground) {
|
||||||
int8_t damage = player->grounded_y - player->y - 3;
|
int16_t damage = player->grounded_y - player->y - 3;
|
||||||
if (damage > 0 && getBlockAt(player->x, player->y, player->z) != B_water) {
|
if (damage > 0 && getBlockAt(player->x, player->y, player->z) != B_water) {
|
||||||
hurtEntity(client_fd, -1, D_fall, damage);
|
hurtEntity(client_fd, -1, D_fall, damage);
|
||||||
}
|
}
|
||||||
@@ -224,6 +224,15 @@ void handlePacket (int client_fd, int length, int packet_id) {
|
|||||||
short dx = _x - (player->x < 0 ? player->x - 16 : player->x) / 16;
|
short dx = _x - (player->x < 0 ? player->x - 16 : player->x) / 16;
|
||||||
short dz = _z - (player->z < 0 ? player->z - 16 : player->z) / 16;
|
short dz = _z - (player->z < 0 ? player->z - 16 : player->z) / 16;
|
||||||
|
|
||||||
|
// Prevent players from leaving the world
|
||||||
|
if (cy < 0) {
|
||||||
|
cy = 0;
|
||||||
|
sc_synchronizePlayerPosition(client_fd, cx, 0, cz, player->yaw * 180 / 127, player->pitch * 90 / 127);
|
||||||
|
} else if (cy > 255) {
|
||||||
|
cy = 255;
|
||||||
|
sc_synchronizePlayerPosition(client_fd, cx, 255, cz, player->yaw * 180 / 127, player->pitch * 90 / 127);
|
||||||
|
}
|
||||||
|
|
||||||
// Update position in player data
|
// Update position in player data
|
||||||
player->x = cx;
|
player->x = cx;
|
||||||
player->y = cy;
|
player->y = cy;
|
||||||
|
@@ -47,7 +47,8 @@ void resetPlayerData (PlayerData *player) {
|
|||||||
player->hunger = 20;
|
player->hunger = 20;
|
||||||
player->x = 8;
|
player->x = 8;
|
||||||
player->z = 8;
|
player->z = 8;
|
||||||
player->y = -32767;
|
player->y = 0;
|
||||||
|
player->flags &= 0x02;
|
||||||
player->grounded_y = 0;
|
player->grounded_y = 0;
|
||||||
for (int i = 0; i < 41; i ++) {
|
for (int i = 0; i < 41; i ++) {
|
||||||
player->inventory_items[i] = 0;
|
player->inventory_items[i] = 0;
|
||||||
@@ -203,7 +204,7 @@ void spawnPlayer (PlayerData *player) {
|
|||||||
float spawn_x = 8.5f, spawn_y = 80.0f, spawn_z = 8.5f;
|
float spawn_x = 8.5f, spawn_y = 80.0f, spawn_z = 8.5f;
|
||||||
float spawn_yaw = 0.0f, spawn_pitch = 0.0f;
|
float spawn_yaw = 0.0f, spawn_pitch = 0.0f;
|
||||||
|
|
||||||
if (player->y == -32767) { // Is this a new player?
|
if ((player->flags & 0x02) == 0) { // Is this a new player?
|
||||||
// Determine spawning Y coordinate based on terrain height
|
// Determine spawning Y coordinate based on terrain height
|
||||||
spawn_y = getHeightAt(8, 8) + 1;
|
spawn_y = getHeightAt(8, 8) + 1;
|
||||||
} else { // Not a new player
|
} else { // Not a new player
|
||||||
|
Reference in New Issue
Block a user