Skip to content

Commit

Permalink
Fix pr #338: Fix issue #337: Displacement
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Jan 28, 2025
1 parent 863f7ed commit 43d20a8
Showing 1 changed file with 38 additions and 61 deletions.
99 changes: 38 additions & 61 deletions app/src/libs/combat/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,80 +1151,57 @@ export const displacement = (
const { pass } = preventCheck(usersEffects, "moveprevent", target);
if (!pass) return preventResponse(effect, target, "resisted being displaced");

// Find an empty ground position
// The effect needs to be updated with target location in a second step
// This first step just marks that the target can be displaced
if (!effect.latitude || !effect.longitude) {
return {
txt: `${target.username} is ready to be displaced`,
color: "blue",
};
}

// Check if the target location is occupied
const occupiedPositions = new Set(
usersState.map((u) => `${u.latitude},${u.longitude}`)
);
if (occupiedPositions.has(`${effect.latitude},${effect.longitude}`)) {
return {
txt: `${user.username} failed to displace ${target.username} - ground is occupied`,
color: "red",
};
}

// Try positions in a spiral pattern around the target
const dx = [0, 1, 0, -1] as const; // right, down, left, up
const dy = [1, 0, -1, 0] as const;
const getDirectionOffset = (dir: number): { x: number; y: number } => {
const index = ((dir % 4) + 4) % 4; // Ensure positive index between 0-3
// We know index is between 0-3, so these accesses are safe
const x = dx[index] as number;
const y = dy[index] as number;
return { x, y };
// Update movement information
info = {
txt: `${user.username} displaces ${target.username} to [${effect.latitude}, ${effect.longitude}]`,
color: "blue",
};
let x = target.longitude;
let y = target.latitude;
let layer = 1;
let found = false;

while (layer <= 5 && !found) { // Try up to 5 layers out
for (let direction = 0; direction < 4; direction++) {
for (let step = 0; step < layer; step++) {
const offset = getDirectionOffset(direction as 0 | 1 | 2 | 3);
x += offset.x;
y += offset.y;

if (!occupiedPositions.has(`${y},${x}`)) {
// Found an empty spot
// Update movement information
info = {
txt: `${user.username} displaces ${target.username} to [${y}, ${x}]`,
color: "blue",
};

// Handle ground effects
groundEffects.forEach((g) => {
if (g.timeTracker && target.userId in g.timeTracker) {
delete g.timeTracker[target.userId];
}
});

groundEffects.forEach((g) => {
if (
g.timeTracker &&
g.longitude === x &&
g.latitude === y
) {
g.timeTracker[target.userId] = effect.createdRound;
}
});
// Handle ground effects
groundEffects.forEach((g) => {
if (g.timeTracker && target.userId in g.timeTracker) {
delete g.timeTracker[target.userId];
}
});

// Update target location
target.longitude = x;
target.latitude = y;
found = true;
break;
}
}
if (found) break;
groundEffects.forEach((g) => {
if (
g.timeTracker &&
g.longitude === effect.longitude &&
g.latitude === effect.latitude
) {
g.timeTracker[target.userId] = effect.createdRound;
}
layer++;
}
});

if (!found) {
info = {
txt: `${user.username} failed to displace ${target.username} - no empty ground found`,
color: "red",
};
}
// Update target location
target.longitude = effect.longitude;
target.latitude = effect.latitude;
}
return info;
};


export const move = (
effect: GroundEffect,
usersEffects: UserEffect[],
Expand Down

0 comments on commit 43d20a8

Please sign in to comment.