use global tick counter to handle regular events at different rates

This commit is contained in:
p2r3
2025-08-31 21:45:47 +03:00
parent 8011a1adfd
commit d608014470
3 changed files with 38 additions and 34 deletions

View File

@@ -99,6 +99,7 @@ extern uint32_t world_seed;
extern uint32_t rng_seed; extern uint32_t rng_seed;
extern uint16_t world_time; extern uint16_t world_time;
extern uint32_t server_ticks;
extern char motd[]; extern char motd[];
extern uint8_t motd_len; extern uint8_t motd_len;

View File

@@ -30,6 +30,7 @@ uint32_t world_seed = 0xA103DE6C;
uint32_t rng_seed = 0xE2B9419; uint32_t rng_seed = 0xE2B9419;
uint16_t world_time = 0; uint16_t world_time = 0;
uint32_t server_ticks = 0;
char motd[] = { "A bareiron server" }; char motd[] = { "A bareiron server" };
uint8_t motd_len = sizeof(motd) - 1; uint8_t motd_len = sizeof(motd) - 1;

View File

@@ -1376,47 +1376,51 @@ void handleServerTick (int64_t time_since_last_tick) {
// Update world time // Update world time
world_time = (world_time + time_since_last_tick / 50000) % 24000; world_time = (world_time + time_since_last_tick / 50000) % 24000;
// Increment server tick counter
server_ticks ++;
// Update player events // Update player events
for (int i = 0; i < MAX_PLAYERS; i ++) { for (int i = 0; i < MAX_PLAYERS; i ++) {
if (player_data[i].client_fd == -1) continue; // Skip offline players PlayerData *player = &player_data[i];
if (player_data[i].flags & 0x20) { // Check "client loading" flag if (player->client_fd == -1) continue; // Skip offline players
if (player->flags & 0x20) { // Check "client loading" flag
// If 3 seconds (60 vanilla ticks) have passed, assume player has loaded // If 3 seconds (60 vanilla ticks) have passed, assume player has loaded
player_data[i].flagval_16 ++; player->flagval_16 ++;
if (player_data[i].flagval_16 > (unsigned int)(3 * TICKS_PER_SECOND)) { if (player->flagval_16 > (unsigned int)(3 * TICKS_PER_SECOND)) {
player_data[i].flags &= ~0x20; player->flags &= ~0x20;
player_data[i].flagval_16 = 0; player->flagval_16 = 0;
} else continue; } else continue;
} }
// Send Keep Alive and Update Time packets // Send Keep Alive and Update Time packets
sc_keepAlive(player_data[i].client_fd); sc_keepAlive(player->client_fd);
sc_updateTime(player_data[i].client_fd, world_time); sc_updateTime(player->client_fd, world_time);
// Reset player attack cooldown // Reset player attack cooldown
if (player_data[i].flags & 0x01) { if (player->flags & 0x01) {
if (player_data[i].flagval_8 >= (unsigned int)(0.6f * TICKS_PER_SECOND)) { if (player->flagval_8 >= (unsigned int)(0.6f * TICKS_PER_SECOND)) {
player_data[i].flags &= ~0x01; player->flags &= ~0x01;
player_data[i].flagval_8 = 0; player->flagval_8 = 0;
} else player_data[i].flagval_8 ++; } else player->flagval_8 ++;
} }
// Handle eating animation // Handle eating animation
if (player_data[i].flags & 0x10) { if (player->flags & 0x10) {
if (player_data[i].flagval_16 >= (unsigned int)(1.6f * TICKS_PER_SECOND)) { if (player->flagval_16 >= (unsigned int)(1.6f * TICKS_PER_SECOND)) {
handlePlayerEating(&player_data[i], false); handlePlayerEating(&player_data[i], false);
player_data[i].flags &= ~0x10; player->flags &= ~0x10;
player_data[i].flagval_16 = 0; player->flagval_16 = 0;
} else player_data[i].flagval_16 ++; } else player->flagval_16 ++;
} }
// Heal from saturation if player is able and has enough food // Heal from saturation if player is able and has enough food
if (player_data[i].health >= 20 || player_data[i].health == 0) continue; if (server_ticks % (uint32_t)TICKS_PER_SECOND != 0) continue;
if (player_data[i].hunger < 18) continue; if (player->health >= 20 || player->health == 0) continue;
if (player_data[i].saturation >= 600) { if (player->hunger < 18) continue;
player_data[i].saturation -= 600; if (player->saturation >= 600) {
player_data[i].health ++; player->saturation -= 600;
player->health ++;
} else { } else {
player_data[i].hunger --; player->hunger --;
player_data[i].health ++; player->health ++;
} }
sc_setHealth(player_data[i].client_fd, player_data[i].health, player_data[i].hunger, player_data[i].saturation); sc_setHealth(player->client_fd, player->health, player->hunger, player->saturation);
} }
// Write player data to file (if applicable) // Write player data to file (if applicable)
@@ -1466,14 +1470,12 @@ void handleServerTick (int64_t time_since_last_tick) {
uint32_t r = fast_rand(); uint32_t r = fast_rand();
if ((unsigned int)TICKS_PER_SECOND >= 1) { if (passive) {
if (passive) { // Update passive mobs once per 4 seconds on average
// Update passive mobs once per 4 seconds on average if (r % (4 * (unsigned int)TICKS_PER_SECOND) != 0) continue;
if (r % (4 * (unsigned int)TICKS_PER_SECOND)) continue; } else {
} else { // Update hostile mobs once per second
// Update hostile mobs once per second on average if (server_ticks % (uint32_t)TICKS_PER_SECOND != 0) continue;
if (r % (unsigned int)TICKS_PER_SECOND) continue;
}
} }
// Find the player closest to this mob // Find the player closest to this mob