Skip to content

Commit

Permalink
fix: combat tabbing glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
Veradictus committed Sep 17, 2023
1 parent 75d81a3 commit e9aad9b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/network/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default class Connection {
entity = this.entities.get<Character>(instance);

// No entity found, just skip.
if (!entity || entity.moving || entity.hasPath()) return;
if (!entity) return;

/**
* When we detect a mismatch in client-sided and server-sided
Expand Down
1 change: 0 additions & 1 deletion packages/server/src/game/entity/character/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export default abstract class Character extends Entity {
public lastStep = -1;
public lastMovement = -1;
public lastRegionChange = -1;
public lastAttacker?: Character | undefined;

private healingInterval?: NodeJS.Timeout | undefined;
private effectInterval?: NodeJS.Timeout | undefined;
Expand Down
14 changes: 14 additions & 0 deletions packages/server/src/game/entity/character/combat/combat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export default class Combat {

this.character.follow();
this.lastFollow = Date.now();

if (this.shouldTeleportNearby()) this.character.findAdjacentTile();
}
}

Expand Down Expand Up @@ -263,6 +265,18 @@ export default class Combat {
);
}

/**
* Mob positions may not be updated if there is not a spectator and the player switches
* tabs. In this case we just teleport the mob next to the player. Because the lastMovement
* exceeds 3 seconds and the mob is still not nearby, we can conclude it's not able to
* move and we should teleport it.
* @returns Whether the character is a mob and it hasn't moved in the last 3 seconds.
*/

private shouldTeleportNearby(): boolean {
return this.character.isMob() && Date.now() - this.character.lastMovement > 3000;
}

/**
* Callback for when the combat starts.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/game/entity/character/player/incoming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ export default class Incoming {
// Prevent crazy position updates, add some leeway to the roaming distance.
if (entity.outsideRoaming(requestX!, requestY!, entity.roamDistance * 2)) return;

entity.lastMovement = Date.now();

// For mobs update the position without a packet.
return entity.setPosition(requestX!, requestY!, false);
}
Expand Down

0 comments on commit e9aad9b

Please sign in to comment.