Skip to content

Commit

Permalink
console-cmd: fix /tp not picking the closest item
Browse files Browse the repository at this point in the history
Resolves #1486.
  • Loading branch information
rr- committed Sep 3, 2024
1 parent 0e3c01a commit 31b5bad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- fixed `/give` console command confusing logging around mismatched items (#1463, regression from 3.0)
- fixed `/flip` console command misreporting an already enabled flipmap as off (regression from 4.0)
- fixed `/kill` console command not fully killing enemies (#1482, regression from 3.0)
- fixed `/tp` console command not always picking the closest item (#1486, regression from 4.1)
- fixed console commands causing improper ring shutdown with selected inventory item (#1460, regression from 3.0)
- fixed console input immediately ending demo (#1480, regression from 4.1)
- improved level load times
Expand Down
53 changes: 27 additions & 26 deletions src/game/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,37 +170,38 @@ static COMMAND_RESULT Console_Cmd_Teleport(const char *const args)
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(args, &match_count);

const ITEM_INFO *best_item = NULL;
for (int i = 0; i < match_count; i++) {
const GAME_OBJECT_ID obj_id = matching_objs[i];
const bool is_pickup = Object_IsObjectType(obj_id, g_PickupObjects);

bool matched = false;
int32_t best_distance = INT32_MAX;
for (int16_t item_num = 0; item_num < Item_GetTotalCount();
item_num++) {
const ITEM_INFO *const item = &g_Items[item_num];
if (item->object_number != obj_id
|| (item->flags & IF_KILLED_ITEM)
|| (is_pickup
&& (item->status == IS_INVISIBLE
|| item->status == IS_DEACTIVATED))) {
continue;
}
int32_t best_distance = INT32_MAX;

for (int16_t item_num = 0; item_num < Item_GetTotalCount();
item_num++) {
const ITEM_INFO *const item = &g_Items[item_num];
const bool is_pickup =
Object_IsObjectType(item->object_number, g_PickupObjects);
if (is_pickup
&& (item->status == IS_INVISIBLE
|| item->status == IS_DEACTIVATED)) {
continue;
}

if (item->flags & IF_KILLED_ITEM) {
continue;
}

const int32_t distance =
Item_GetDistance(item, &g_LaraItem->pos);
if (distance >= best_distance) {
continue;
bool is_matched = false;
for (int32_t i = 0; i < match_count; i++) {
if (matching_objs[i] == item->object_number) {
is_matched = true;
break;
}
}
if (!is_matched) {
continue;
}

const int32_t distance = Item_GetDistance(item, &g_LaraItem->pos);
if (distance < best_distance) {
best_distance = distance;
best_item = item;
matched = true;
}

if (matched) {
// abort for the first matching item
break;
}
}

Expand Down

0 comments on commit 31b5bad

Please sign in to comment.