mirror of
https://github.com/p2r3/bareiron.git
synced 2025-10-02 07:35:08 +02:00
make event timing independent of tickrate
This commit is contained in:
@@ -31,8 +31,7 @@
|
|||||||
// Time between server ticks in microseconds (default = 1s)
|
// Time between server ticks in microseconds (default = 1s)
|
||||||
#define TIME_BETWEEN_TICKS 1000000
|
#define TIME_BETWEEN_TICKS 1000000
|
||||||
// Calculated from TIME_BETWEEN_TICKS
|
// Calculated from TIME_BETWEEN_TICKS
|
||||||
#define TICKS_PER_SECOND (1000000 / TIME_BETWEEN_TICKS)
|
#define TICKS_PER_SECOND ((float)1000000 / TIME_BETWEEN_TICKS)
|
||||||
#define TICKS_TO_EAT (unsigned int)(1.6f * TICKS_PER_SECOND)
|
|
||||||
// How many visited chunks to "remember"
|
// How many visited chunks to "remember"
|
||||||
// The server will not re-send chunks that the player has recently been in
|
// The server will not re-send chunks that the player has recently been in
|
||||||
#define VISITED_HISTORY 4
|
#define VISITED_HISTORY 4
|
||||||
@@ -114,6 +113,8 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
short x;
|
short x;
|
||||||
|
// When the mob is dead (health is 0), the Y coordinate acts
|
||||||
|
// as a timer for deleting and deallocating the mob
|
||||||
uint8_t y;
|
uint8_t y;
|
||||||
short z;
|
short z;
|
||||||
// Lower 5 bits: health
|
// Lower 5 bits: health
|
||||||
|
@@ -98,6 +98,7 @@ void handlePacket (int client_fd, int length, int packet_id) {
|
|||||||
// For more info on the arguments, see the spawnMob function
|
// For more info on the arguments, see the spawnMob function
|
||||||
for (int i = 0; i < MAX_MOBS; i ++) {
|
for (int i = 0; i < MAX_MOBS; i ++) {
|
||||||
if (mob_data[i].type == 0) continue;
|
if (mob_data[i].type == 0) continue;
|
||||||
|
if ((mob_data[i].data & 31) == 0) continue;
|
||||||
sc_spawnEntity(
|
sc_spawnEntity(
|
||||||
client_fd, 65536 + i, recv_buffer,
|
client_fd, 65536 + i, recv_buffer,
|
||||||
mob_data[i].type, mob_data[i].x, mob_data[i].y, mob_data[i].z,
|
mob_data[i].type, mob_data[i].x, mob_data[i].y, mob_data[i].z,
|
||||||
|
@@ -842,6 +842,7 @@ void hurtEntity (int entity_id, int attacker_id, uint8_t damage_type, uint8_t da
|
|||||||
else if (held_item == I_netherite_sword) damage *= 8;
|
else if (held_item == I_netherite_sword) damage *= 8;
|
||||||
// Enable attack cooldown
|
// Enable attack cooldown
|
||||||
player->flags |= 0x01;
|
player->flags |= 0x01;
|
||||||
|
player->flagval_8 = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whether this attack caused the target entity to die
|
// Whether this attack caused the target entity to die
|
||||||
@@ -859,6 +860,7 @@ void hurtEntity (int entity_id, int attacker_id, uint8_t damage_type, uint8_t da
|
|||||||
uint8_t mob_health = mob->data & 31;
|
uint8_t mob_health = mob->data & 31;
|
||||||
if (mob_health <= damage) {
|
if (mob_health <= damage) {
|
||||||
mob->data -= mob_health;
|
mob->data -= mob_health;
|
||||||
|
mob->y = 0;
|
||||||
entity_died = true;
|
entity_died = true;
|
||||||
// Handle mob drops
|
// Handle mob drops
|
||||||
if (attacker_id < 65536) switch (mob->type) {
|
if (attacker_id < 65536) switch (mob->type) {
|
||||||
@@ -896,10 +898,15 @@ void handleServerTick (int64_t time_since_last_tick) {
|
|||||||
sc_keepAlive(player_data[i].client_fd);
|
sc_keepAlive(player_data[i].client_fd);
|
||||||
sc_updateTime(player_data[i].client_fd, world_time);
|
sc_updateTime(player_data[i].client_fd, world_time);
|
||||||
// Reset player attack cooldown
|
// Reset player attack cooldown
|
||||||
player_data[i].flags &= ~0x01;
|
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 ++;
|
||||||
|
}
|
||||||
// Handle eating animation
|
// Handle eating animation
|
||||||
if (player_data[i].flags & 0x10) {
|
if (player_data[i].flags & 0x10) {
|
||||||
if (player_data[i].flagval_16 >= TICKS_TO_EAT) {
|
if (player_data[i].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_data[i].flags &= ~0x10;
|
||||||
player_data[i].flagval_16 = 0;
|
player_data[i].flagval_16 = 0;
|
||||||
@@ -930,8 +937,12 @@ void handleServerTick (int64_t time_since_last_tick) {
|
|||||||
for (int i = 0; i < MAX_MOBS; i ++) {
|
for (int i = 0; i < MAX_MOBS; i ++) {
|
||||||
if (mob_data[i].type == 0) continue;
|
if (mob_data[i].type == 0) continue;
|
||||||
|
|
||||||
// Mob has died, deallocate it
|
// Handle deallocation on mob death
|
||||||
if ((mob_data[i].data & 31) == 0) {
|
if ((mob_data[i].data & 31) == 0) {
|
||||||
|
if (mob_data[i].y < (unsigned int)TICKS_PER_SECOND) {
|
||||||
|
mob_data[i].y ++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
mob_data[i].type = 0;
|
mob_data[i].type = 0;
|
||||||
for (int j = 0; j < MAX_PLAYERS; j ++) {
|
for (int j = 0; j < MAX_PLAYERS; j ++) {
|
||||||
if (player_data[j].client_fd == -1) continue;
|
if (player_data[j].client_fd == -1) continue;
|
||||||
@@ -952,8 +963,13 @@ void handleServerTick (int64_t time_since_last_tick) {
|
|||||||
|
|
||||||
uint32_t r = fast_rand();
|
uint32_t r = fast_rand();
|
||||||
|
|
||||||
// Skip 25% of passive mob ticks randomly
|
if (passive) {
|
||||||
if (passive && (r & 3)) continue;
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
// Find the player closest to this mob
|
// Find the player closest to this mob
|
||||||
PlayerData* closest_player;
|
PlayerData* closest_player;
|
||||||
|
Reference in New Issue
Block a user