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

Commit

Permalink
console/cmd: move /give to libtrx
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 20, 2024
1 parent 2ec8cf5 commit a905583
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 78 deletions.
2 changes: 1 addition & 1 deletion data/ship/cfg/TR2X_gameflow.json5
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
"OSD_GIVE_ITEM_CHEAT": "Lara's backpack just got way heavier!",
"OSD_HEAL_ALREADY_FULL_HP": "Lara's already at full health",
"OSD_HEAL_SUCCESS": "Healed Lara back to full health",
"OSD_INVALID_ITEM": "Invalid item: %s",
"OSD_INVALID_ITEM": "Unknown item: %s",
"OSD_INVALID_LEVEL": "Invalid level",
"OSD_INVALID_OBJECT": "Invalid object",
"OSD_INVALID_ROOM": "Invalid room: %d. Valid rooms are 0-%d",
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ dll_sources = [
'src/decomp/effects.c',
'src/decomp/stats.c',
'src/game/background.c',
'src/game/backpack.c',
'src/game/box.c',
'src/game/camera.c',
'src/game/clock.c',
Expand Down
8 changes: 8 additions & 0 deletions src/game/backpack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "game/inventory/backpack.h"

#include <libtrx/game/backpack.h>

bool Backpack_AddItem(const GAME_OBJECT_ID object_id)
{
return Inv_AddItem(object_id);
}
15 changes: 9 additions & 6 deletions src/game/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <libtrx/game/console/common.h>
#include <libtrx/log.h>
#include <libtrx/memory.h>
#include <libtrx/strings.h>
#include <libtrx/utils.h>

#include <stdarg.h>
Expand Down Expand Up @@ -97,16 +98,18 @@ static COMMAND_RESULT M_Eval(const char *const cmdline)
if (cur_cmd == NULL) {
break;
}
if (strstr(cmdline, cur_cmd->prefix) != cmdline) {

char regex[strlen(cur_cmd->prefix) + 13];
sprintf(regex, "^(%s)(\\s+.*)?$", cur_cmd->prefix);
if (!String_Match(cmdline, regex)) {
continue;
}

if (cmdline[strlen(cur_cmd->prefix)] == ' ') {
args = cmdline + strlen(cur_cmd->prefix) + 1;
} else if (cmdline[strlen(cur_cmd->prefix)] == '\0') {
args = "";
args = strstr(cmdline, " ");
if (args != NULL) {
args++;
} else {
continue;
args = "";
}

matching_cmd = cur_cmd;
Expand Down
70 changes: 2 additions & 68 deletions src/game/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "lara/lara_misc.h"

#include <libtrx/game/console/commands/config.h>
#include <libtrx/game/console/commands/give_item.h>
#include <libtrx/game/console/commands/heal.h>
#include <libtrx/game/console/commands/pos.h>
#include <libtrx/game/console/commands/set_health.h>
Expand All @@ -31,12 +32,10 @@

static bool M_CanTargetObject(GAME_OBJECT_ID object_id);
static bool M_CanTargetObjectCreature(GAME_OBJECT_ID object_id);
static bool M_CanTargetObjectPickup(GAME_OBJECT_ID object_id);
static bool M_IsFloatRound(float num);
static COMMAND_RESULT Console_Cmd_Teleport(const char *args);
static COMMAND_RESULT Console_Cmd_Fly(const char *const args);
static COMMAND_RESULT Console_Cmd_FlipMap(const char *args);
static COMMAND_RESULT Console_Cmd_GiveItem(const char *args);
static COMMAND_RESULT Console_Cmd_Kill(const char *args);
static COMMAND_RESULT Console_Cmd_EndLevel(const char *args);
static COMMAND_RESULT Console_Cmd_StartLevel(const char *args);
Expand All @@ -60,11 +59,6 @@ static bool M_CanTargetObjectCreature(const GAME_OBJECT_ID object_id)
|| Object_IsObjectType(object_id, g_FriendObjects);
}

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

static inline bool M_IsFloatRound(const float num)
{
return (fabsf(num) - roundf(num)) < 0.0001f;
Expand Down Expand Up @@ -337,65 +331,6 @@ static COMMAND_RESULT Console_Cmd_Kill(const char *args)
}
}

static COMMAND_RESULT Console_Cmd_GiveItem(const char *args)
{
if (g_GameInfo.current_level.type == GFL_TITLE
|| g_GameInfo.current_level.type == GFL_DEMO
|| g_GameInfo.current_level.type == GFL_CUTSCENE) {
return CR_UNAVAILABLE;
}

if (g_LaraItem == NULL) {
return CR_UNAVAILABLE;
}

if (String_Equivalent(args, "keys")) {
return Lara_Cheat_GiveAllKeys() ? CR_SUCCESS : CR_FAILURE;
}

if (String_Equivalent(args, "guns")) {
return Lara_Cheat_GiveAllGuns() ? CR_SUCCESS : CR_FAILURE;
}

if (String_Equivalent(args, "all")) {
return Lara_Cheat_GiveAllItems() ? CR_SUCCESS : CR_FAILURE;
}

int32_t num = 1;
if (sscanf(args, "%d ", &num) == 1) {
args = strstr(args, " ");
if (!args) {
return CR_BAD_INVOCATION;
}
args++;
}

if (String_Equivalent(args, "")) {
return CR_BAD_INVOCATION;
}

bool found = false;
int32_t match_count = 0;
GAME_OBJECT_ID *matching_objs =
Object_IdsFromName(args, &match_count, M_CanTargetObjectPickup);
for (int32_t i = 0; i < match_count; i++) {
const GAME_OBJECT_ID object_id = matching_objs[i];
if (g_Objects[object_id].loaded) {
Inv_AddItemNTimes(object_id, num);
Console_Log(GS(OSD_GIVE_ITEM), Object_GetName(object_id));
found = true;
}
}
Memory_FreePointer(&matching_objs);

if (!found) {
Console_Log(GS(OSD_INVALID_ITEM), args);
return CR_FAILURE;
}

return CR_SUCCESS;
}

static COMMAND_RESULT Console_Cmd_EndLevel(const char *const args)
{
if (strcmp(args, "") == 0) {
Expand Down Expand Up @@ -540,8 +475,6 @@ static COMMAND_RESULT Console_Cmd_Abortion(const char *const args)
CONSOLE_COMMAND *g_ConsoleCommands[] = {
&(CONSOLE_COMMAND) { .prefix = "tp", .proc = Console_Cmd_Teleport },
&(CONSOLE_COMMAND) { .prefix = "fly", .proc = Console_Cmd_Fly },
&(CONSOLE_COMMAND) { .prefix = "give", .proc = Console_Cmd_GiveItem },
&(CONSOLE_COMMAND) { .prefix = "gimme", .proc = Console_Cmd_GiveItem },
&(CONSOLE_COMMAND) { .prefix = "flip", .proc = Console_Cmd_FlipMap },
&(CONSOLE_COMMAND) { .prefix = "flipmap", .proc = Console_Cmd_FlipMap },
&(CONSOLE_COMMAND) { .prefix = "kill", .proc = Console_Cmd_Kill },
Expand All @@ -560,5 +493,6 @@ CONSOLE_COMMAND *g_ConsoleCommands[] = {
&g_Console_Cmd_Pos,
&g_Console_Cmd_Heal,
&g_Console_Cmd_SetHealth,
&g_Console_Cmd_GiveItem,
NULL,
};
2 changes: 0 additions & 2 deletions src/game/game_string.def
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ GS_DEFINE(OSD_POS_SET_ROOM, "Teleported to room: %d")
GS_DEFINE(OSD_POS_SET_ROOM_FAIL, "Failed to teleport to room: %d")
GS_DEFINE(OSD_POS_SET_ITEM, "Teleported to object: %s")
GS_DEFINE(OSD_POS_SET_ITEM_FAIL, "Failed to teleport to object: %s")
GS_DEFINE(OSD_GIVE_ITEM, "Added %s to Lara's inventory")
GS_DEFINE(OSD_GIVE_ITEM_ALL_KEYS, "Surprise! Every key item Lara needs is now in her backpack.")
GS_DEFINE(OSD_GIVE_ITEM_ALL_GUNS, "Lock'n'load - Lara's armed to the teeth!")
GS_DEFINE(OSD_GIVE_ITEM_CHEAT, "Lara's backpack just got way heavier!")
Expand All @@ -20,7 +19,6 @@ GS_DEFINE(OSD_KILL, "Bye-bye!")
GS_DEFINE(OSD_KILL_FAIL, "No enemy nearby...")
GS_DEFINE(OSD_COMPLETE_LEVEL, "Level complete!")
GS_DEFINE(OSD_PLAY_LEVEL, "Loading %s")
GS_DEFINE(OSD_INVALID_ITEM, "Invalid item: %s")
GS_DEFINE(OSD_INVALID_ROOM, "Invalid room: %d. Valid rooms are 0-%d")
GS_DEFINE(OSD_INVALID_OBJECT, "Invalid object")
GS_DEFINE(OSD_OBJECT_NOT_FOUND, "Object not found")
Expand Down

0 comments on commit a905583

Please sign in to comment.