Skip to content

Commit a341349

Browse files
committed
implement mob "panic" behavior
1 parent e7e9d30 commit a341349

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

include/globals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ typedef struct {
238238
uint8_t y;
239239
short z;
240240
// Lower 5 bits: health
241-
// Upper 3 bits: reserved (?)
241+
// Middle 1 bit: reserved for future use
242+
// Upper 2 bits: panic timer
242243
uint8_t data;
243244
} MobData;
244245

src/procedures.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ void hurtEntity (int entity_id, int attacker_id, uint8_t damage_type, uint8_t da
14001400
// Don't continue if the mob is already dead
14011401
if (mob_health == 0) return;
14021402

1403+
// Set the mob's panic timer
1404+
mob->data |= (3 << 6);
1405+
14031406
// Process health change on the server
14041407
if (mob_health <= damage) {
14051408

@@ -1544,6 +1547,9 @@ void handleServerTick (int64_t time_since_last_tick) {
15441547
mob_data[i].type == 95 || // Pig
15451548
mob_data[i].type == 106 // Sheep
15461549
);
1550+
// Mob "panic" timer, set to 3 after being hit
1551+
// Currently has no effect on hostile mobs
1552+
uint8_t panic = (mob_data[i].data >> 6) & 3;
15471553

15481554
// Burn hostile mobs if above ground during sunlight
15491555
if (!passive && (world_time < 13000 || world_time > 23460) && mob_data[i].y > 48) {
@@ -1553,8 +1559,21 @@ void handleServerTick (int64_t time_since_last_tick) {
15531559
uint32_t r = fast_rand();
15541560

15551561
if (passive) {
1556-
// Update passive mobs once per 4 seconds on average
1557-
if (r % (4 * (unsigned int)TICKS_PER_SECOND) != 0) continue;
1562+
if (panic) {
1563+
// If panicking, move randomly at up to 4 times per second
1564+
if (TICKS_PER_SECOND >= 4) {
1565+
uint32_t ticks_per_panic = (uint32_t)(TICKS_PER_SECOND / 4);
1566+
if (server_ticks % ticks_per_panic != 0) continue;
1567+
}
1568+
// Reset panic state after timer runs out
1569+
// Each panic timer tick takes one second
1570+
if (server_ticks % (uint32_t)TICKS_PER_SECOND == 0) {
1571+
mob_data[i].data -= (1 << 6);
1572+
}
1573+
} else {
1574+
// When not panicking, move idly once per 4 seconds on average
1575+
if (r % (4 * (unsigned int)TICKS_PER_SECOND) != 0) continue;
1576+
}
15581577
} else {
15591578
// Update hostile mobs once per second
15601579
if (server_ticks % (uint32_t)TICKS_PER_SECOND != 0) continue;

0 commit comments

Comments
 (0)