Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
console: improve item targeting
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 4, 2024
1 parent 5ca75e4 commit 2f7736a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
49 changes: 31 additions & 18 deletions src/game/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <math.h>
#include <stdio.h>

static bool Console_Cmd_CanTargetObject(GAME_OBJECT_ID object_id);
static bool Console_Cmd_CanTargetObjectCreature(GAME_OBJECT_ID object_id);
static bool Console_Cmd_CanTargetObjectPickup(GAME_OBJECT_ID object_id);
static bool Console_Cmd_IsFloatRound(float num);
static COMMAND_RESULT Console_Cmd_Pos(const char *args);
static COMMAND_RESULT Console_Cmd_Teleport(const char *args);
Expand All @@ -43,6 +46,24 @@ static COMMAND_RESULT Console_Cmd_ExitToTitle(const char *args);
static COMMAND_RESULT Console_Cmd_ExitGame(const char *args);
static COMMAND_RESULT Console_Cmd_Abortion(const char *args);

static bool Console_Cmd_CanTargetObject(const GAME_OBJECT_ID object_id)
{
return !Object_IsObjectType(object_id, g_NullObjects)
&& !Object_IsObjectType(object_id, g_AnimObjects)
&& !Object_IsObjectType(object_id, g_InvObjects);
}

static bool Console_Cmd_CanTargetObjectCreature(const GAME_OBJECT_ID object_id)
{
return Object_IsObjectType(object_id, g_EnemyObjects)
|| Object_IsObjectType(object_id, g_FriendObjects);
}

static bool Console_Cmd_CanTargetObjectPickup(const GAME_OBJECT_ID object_id)
{
return Object_IsObjectType(object_id, g_PickupObjects);
}

static inline bool Console_Cmd_IsFloatRound(const float num)
{
return (fabsf(num) - roundf(num)) < 0.0001f;
Expand Down Expand Up @@ -135,7 +156,8 @@ static COMMAND_RESULT Console_Cmd_Teleport(const char *const args)
// Nearest item of this name
if (!String_Equivalent(args, "")) {
int32_t match_count = 0;
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(args, &match_count);
GAME_OBJECT_ID *matching_objs =
Object_IdsFromName(args, &match_count, Console_Cmd_CanTargetObject);

const ITEM_INFO *best_item = NULL;
int32_t best_distance = INT32_MAX;
Expand All @@ -149,12 +171,6 @@ static COMMAND_RESULT Console_Cmd_Teleport(const char *const args)
continue;
}

if (Object_IsObjectType(item->object_num, g_NullObjects)
|| Object_IsObjectType(item->object_num, g_AnimObjects)
|| Object_IsObjectType(item->object_num, g_InvObjects)) {
continue;
}

if (item->flags & IF_KILLED) {
continue;
}
Expand Down Expand Up @@ -347,14 +363,12 @@ static COMMAND_RESULT Console_Cmd_Kill(const char *args)
bool matches_found = false;
int32_t num_killed = 0;
int32_t match_count = 0;
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(args, &match_count);
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(
args, &match_count, Console_Cmd_CanTargetObjectCreature);

for (int16_t item_num = 0; item_num < Item_GetTotalCount();
item_num++) {
const ITEM_INFO *const item = &g_Items[item_num];
if (!Creature_IsEnemy(item) && !Creature_IsAlly(item)) {
continue;
}

bool is_matched = false;
for (int32_t i = 0; i < match_count; i++) {
Expand Down Expand Up @@ -426,15 +440,14 @@ static COMMAND_RESULT Console_Cmd_GiveItem(const char *args)

bool found = false;
int32_t match_count = 0;
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(args, &match_count);
GAME_OBJECT_ID *matching_objs = Object_IdsFromName(
args, &match_count, Console_Cmd_CanTargetObjectPickup);
for (int32_t i = 0; i < match_count; i++) {
const GAME_OBJECT_ID obj_id = matching_objs[i];
if (Object_IsObjectType(obj_id, g_PickupObjects)) {
if (g_Objects[obj_id].loaded) {
Inv_AddItemNTimes(obj_id, num);
Console_Log(GS(OSD_GIVE_ITEM), Object_GetName(obj_id));
found = true;
}
if (g_Objects[obj_id].loaded) {
Inv_AddItemNTimes(obj_id, num);
Console_Log(GS(OSD_GIVE_ITEM), Object_GetName(obj_id));
found = true;
}
}
Memory_FreePointer(&matching_objs);
Expand Down
14 changes: 12 additions & 2 deletions src/game/objects/names.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ static void Object_TryMatch(
VECTOR *const matches, const char *const user_input, const char *const name,
const GAME_OBJECT_ID obj_id)
{
const int32_t score = Object_NameMatch(user_input, name);
int32_t score = Object_NameMatch(user_input, name);
if (!g_Objects[obj_id].loaded) {
score -= GOOD_MATCH_THRESHOLD;
}
if (score > 0) {
MATCH match = {
.score = score,
Expand Down Expand Up @@ -134,7 +137,8 @@ void Object_SetName(const GAME_OBJECT_ID obj_id, const char *const name)
}

GAME_OBJECT_ID *Object_IdsFromName(
const char *user_input, int32_t *out_match_count)
const char *user_input, int32_t *out_match_count,
bool (*filter)(GAME_OBJECT_ID))
{
// first, calculate the number of matches to allocate
VECTOR *matches = Vector_Create(sizeof(MATCH));
Expand All @@ -145,11 +149,17 @@ GAME_OBJECT_ID *Object_IdsFromName(
const INVENTORY_ITEM *const item = *item_ptr;
const GAME_OBJECT_ID obj_id =
Object_GetCognateInverse(item->obj_num, g_ItemToInvObjectMap);
if (filter != NULL && !filter(obj_id)) {
continue;
}
Object_TryMatch(matches, user_input, item->string, obj_id);
}

// Store matches from hardcoded strings
for (GAME_OBJECT_ID obj_id = 0; obj_id < O_NUMBER_OF; obj_id++) {
if (filter != NULL && !filter(obj_id)) {
continue;
}
Object_TryMatch(matches, user_input, Object_GetName(obj_id), obj_id);
}

Expand Down
5 changes: 2 additions & 3 deletions src/game/objects/names.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <libtrx/enum_str.h>

#include <stdint.h>

extern ENUM_STRING_MAP ENUM_STRING_MAP(GAME_OBJECT_ID)[];

void Object_SetupNames(void);
Expand All @@ -16,4 +14,5 @@ void Object_SetName(GAME_OBJECT_ID obj_id, const char *name);
// Return a list of object ids that match given string.
// out_match_count may be NULL.
// The result must be freed by the caller.
GAME_OBJECT_ID *Object_IdsFromName(const char *name, int32_t *out_match_count);
GAME_OBJECT_ID *Object_IdsFromName(
const char *name, int32_t *out_match_count, bool (*filter)(GAME_OBJECT_ID));

0 comments on commit 2f7736a

Please # to comment.