Commit Diff


commit - 8011a1adfdcb753f310eaa0f5f116df31f21c0af
commit + d608014470b6cd1d42349fd916e913d3ae28c242
blob - 004def67f16f6d94f55008e7460ff0d0775f43a4
blob + 2a2392f5144acbea765eeb7bbc2547bb146497a8
--- include/globals.h
+++ include/globals.h
@@ -99,6 +99,7 @@ extern uint32_t world_seed;
 extern uint32_t rng_seed;
 
 extern uint16_t world_time;
+extern uint32_t server_ticks;
 
 extern char motd[];
 extern uint8_t motd_len;
blob - d2dabcf76e0300d0a23948ece75e01a583a7e79d
blob + bbc7fdb767948228b193221022efa5479eaa4441
--- src/globals.c
+++ src/globals.c
@@ -30,6 +30,7 @@ uint32_t world_seed = 0xA103DE6C;
 uint32_t rng_seed = 0xE2B9419;
 
 uint16_t world_time = 0;
+uint32_t server_ticks = 0;
 
 char motd[] = { "A bareiron server" };
 uint8_t motd_len = sizeof(motd) - 1;
blob - 822f36e05d74a4b7691e866911a6eb1b1d3b7e04
blob + 8afdc87dafad893ff8f2ab6b3c6fd663e5767343
--- src/procedures.c
+++ src/procedures.c
@@ -1376,47 +1376,51 @@ void handleServerTick (int64_t time_since_last_tick) {
 
   // Update world time
   world_time = (world_time + time_since_last_tick / 50000) % 24000;
+  // Increment server tick counter
+  server_ticks ++;
 
   // Update player events
   for (int i = 0; i < MAX_PLAYERS; i ++) {
-    if (player_data[i].client_fd == -1) continue; // Skip offline players
-    if (player_data[i].flags & 0x20) { // Check "client loading" flag
+    PlayerData *player = &player_data[i];
+    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
-      player_data[i].flagval_16 ++;
-      if (player_data[i].flagval_16 > (unsigned int)(3 * TICKS_PER_SECOND)) {
-        player_data[i].flags &= ~0x20;
-        player_data[i].flagval_16 = 0;
+      player->flagval_16 ++;
+      if (player->flagval_16 > (unsigned int)(3 * TICKS_PER_SECOND)) {
+        player->flags &= ~0x20;
+        player->flagval_16 = 0;
       } else continue;
     }
     // Send Keep Alive and Update Time packets
-    sc_keepAlive(player_data[i].client_fd);
-    sc_updateTime(player_data[i].client_fd, world_time);
+    sc_keepAlive(player->client_fd);
+    sc_updateTime(player->client_fd, world_time);
     // Reset player attack cooldown
-    if (player_data[i].flags & 0x01) {
-      if (player_data[i].flagval_8 >= (unsigned int)(0.6f * TICKS_PER_SECOND)) {
-        player_data[i].flags &= ~0x01;
-        player_data[i].flagval_8 = 0;
-      } else player_data[i].flagval_8 ++;
+    if (player->flags & 0x01) {
+      if (player->flagval_8 >= (unsigned int)(0.6f * TICKS_PER_SECOND)) {
+        player->flags &= ~0x01;
+        player->flagval_8 = 0;
+      } else player->flagval_8 ++;
     }
     // Handle eating animation
-    if (player_data[i].flags & 0x10) {
-      if (player_data[i].flagval_16 >= (unsigned int)(1.6f * TICKS_PER_SECOND)) {
+    if (player->flags & 0x10) {
+      if (player->flagval_16 >= (unsigned int)(1.6f * TICKS_PER_SECOND)) {
         handlePlayerEating(&player_data[i], false);
-        player_data[i].flags &= ~0x10;
-        player_data[i].flagval_16 = 0;
-      } else player_data[i].flagval_16 ++;
+        player->flags &= ~0x10;
+        player->flagval_16 = 0;
+      } else player->flagval_16 ++;
     }
     // 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 ++;
+    if (server_ticks % (uint32_t)TICKS_PER_SECOND != 0) continue;
+    if (player->health >= 20 || player->health == 0) continue;
+    if (player->hunger < 18) continue;
+    if (player->saturation >= 600) {
+      player->saturation -= 600;
+      player->health ++;
     } else {
-      player_data[i].hunger --;
-      player_data[i].health ++;
+      player->hunger --;
+      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)
@@ -1466,14 +1470,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) != 0) continue;
+    } else {
+      // Update hostile mobs once per second
+      if (server_ticks % (uint32_t)TICKS_PER_SECOND != 0) continue;
     }
 
     // Find the player closest to this mob