improve hunger mechanics

This commit is contained in:
p2r3
2025-08-27 17:03:55 +03:00
parent 68eb77c424
commit f5119d0b70
4 changed files with 20 additions and 18 deletions

View File

@@ -49,7 +49,7 @@ int sc_teleportEntity (int client_fd, int id, double x, double y, double z, floa
int sc_setHeadRotation (int client_fd, int id, uint8_t yaw);
int sc_updateEntityRotation (int client_fd, int id, uint8_t yaw, uint8_t pitch);
int sc_damageEvent (int client_fd, int id, int type);
int sc_setHealth (int client_fd, uint8_t health, uint8_t food);
int sc_setHealth (int client_fd, uint8_t health, uint8_t food, uint16_t saturation);
int sc_respawn (int client_fd);
int sc_systemChat (int client_fd, char* message, uint16_t len);
int sc_entityEvent (int client_fd, int entity_id, uint8_t status);

View File

@@ -243,7 +243,7 @@ void handlePacket (int client_fd, int length, int packet_id, int state) {
if (player->saturation == 0) {
if (player->hunger > 0) player->hunger--;
player->saturation = 200;
sc_setHealth(client_fd, player->health, player->hunger);
sc_setHealth(client_fd, player->health, player->hunger, player->saturation);
} else if (player->flags & 0x08) {
player->saturation -= 1;
}

View File

@@ -902,14 +902,14 @@ int sc_damageEvent (int client_fd, int entity_id, int type) {
}
// S->C Set Health
int sc_setHealth (int client_fd, uint8_t health, uint8_t food) {
int sc_setHealth (int client_fd, uint8_t health, uint8_t food, uint16_t saturation) {
writeVarInt(client_fd, 9 + sizeVarInt(food));
writeByte(client_fd, 0x61);
writeFloat(client_fd, (float)health);
writeVarInt(client_fd, food);
writeFloat(client_fd, 5.0f); // saturation
writeFloat(client_fd, (float)(saturation - 200) / 500.0f);
return 0;
}

View File

@@ -248,7 +248,7 @@ void spawnPlayer (PlayerData *player) {
}
sc_setHeldItem(player->client_fd, player->hotbar);
// Sync client health and hunger
sc_setHealth(player->client_fd, player->health, player->hunger);
sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation);
// Sync client clock time
sc_updateTime(player->client_fd, world_time);
@@ -479,7 +479,8 @@ uint8_t isColumnBlock (uint8_t block) {
block == B_short_grass ||
block == B_dead_bush ||
block == B_sand ||
block == B_torch
block == B_torch ||
block == B_oak_sapling
);
}
@@ -635,7 +636,7 @@ uint8_t handlePlayerEating (PlayerData *player, uint8_t just_check) {
if (*held_count == 0) *held_item = 0;
// Update the client of these changes
sc_setHealth(player->client_fd, player->health, player->hunger);
sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation);
sc_entityEvent(player->client_fd, player->client_fd, 9);
sc_setContainerSlot(
player->client_fd, 0,
@@ -962,7 +963,7 @@ void hurtEntity (int entity_id, int attacker_id, uint8_t damage_type, uint8_t da
entity_died = true;
} else player->health -= damage;
// Update health on the client
sc_setHealth(entity_id, player->health, player->hunger);
sc_setHealth(entity_id, player->health, player->hunger, player->saturation);
} else { // The attacked entity is a mob
MobData *mob = &mob_data[entity_id - 65536];
uint8_t mob_health = mob->data & 31;
@@ -1020,16 +1021,17 @@ void handleServerTick (int64_t time_since_last_tick) {
player_data[i].flagval_16 = 0;
} else player_data[i].flagval_16 ++;
}
// Heal from saturation
if (player_data[i].health >= 20) continue;
// Heal from saturation if player is able and has enough food
if (player_data[i].health >= 20 || player_data[i].health == 0) continue;
if (player_data[i].hunger < 18) continue;
if (player_data[i].saturation >= 600) {
player_data[i].saturation -= 600;
player_data[i].health ++;
} else if (player_data[i].hunger > 17) {
} else {
player_data[i].hunger --;
player_data[i].health ++;
}
sc_setHealth(player_data[i].client_fd, player_data[i].health, player_data[i].hunger);
sc_setHealth(player_data[i].client_fd, player_data[i].health, player_data[i].hunger, player_data[i].saturation);
}
/**
@@ -1072,12 +1074,12 @@ void handleServerTick (int64_t time_since_last_tick) {
uint32_t r = fast_rand();
if ((unsigned int)TICKS_PER_SECOND >= 1) {
if (passive) {
// Update passive mobs once per 4 seconds on average
if (r % (4 * (unsigned int)TICKS_PER_SECOND)) continue;
} else {
// Update hostile mobs once per second on average
if (r % (unsigned int)TICKS_PER_SECOND) continue;
if (passive) {
// Update passive mobs once per 4 seconds on average
if (r % (4 * (unsigned int)TICKS_PER_SECOND)) continue;
} else {
// Update hostile mobs once per second on average
if (r % (unsigned int)TICKS_PER_SECOND) continue;
}
}