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

Move /give and /kill to libtrx; restructure console commands #250

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 14 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,24 @@ 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',
'src/game/collide.c',
'src/game/console.c',
'src/game/console_cmd.c',
'src/game/console/cmd/die.c',
'src/game/console/cmd/end_level.c',
'src/game/console/cmd/exit_game.c',
'src/game/console/cmd/exit_to_title.c',
'src/game/console/cmd/flipmap.c',
'src/game/console/cmd/fly.c',
'src/game/console/cmd/load_game.c',
'src/game/console/cmd/play_demo.c',
'src/game/console/cmd/play_level.c',
'src/game/console/cmd/save_game.c',
'src/game/console/cmd/teleport.c',
'src/game/console/common.c',
'src/game/console/setup.c',
'src/game/creature.c',
'src/game/demo.c',
'src/game/effects.c',
Expand Down
2 changes: 1 addition & 1 deletion src/decomp/decomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "game/background.h"
#include "game/camera.h"
#include "game/console.h"
#include "game/console/common.h"
#include "game/effects.h"
#include "game/game.h"
#include "game/gameflow.h"
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);
}
30 changes: 30 additions & 0 deletions src/game/console/cmd/die.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "game/console/cmd/die.h"

#include "decomp/effects.h"
#include "game/sound.h"
#include "global/vars.h"

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
if (!g_Objects[O_LARA].loaded) {
return CR_UNAVAILABLE;
}

if (g_LaraItem->hit_points <= 0) {
return CR_UNAVAILABLE;
}

Sound_Effect(SFX_LARA_FALL, &g_LaraItem->pos, SPM_NORMAL);
Effect_ExplodingDeath(g_Lara.item_num, -1, 1);

g_LaraItem->hit_points = 0;
g_LaraItem->flags |= IF_INVISIBLE;
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_Die = {
.prefix = "abortion|natlastinks",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/die.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_Die;
21 changes: 21 additions & 0 deletions src/game/console/cmd/end_level.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "game/console/cmd/end_level.h"

#include "game/lara/lara_cheat.h"

#include <string.h>

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
if (strcmp(args, "") == 0) {
Lara_Cheat_EndLevel();
return CR_SUCCESS;
}
return CR_FAILURE;
}

CONSOLE_COMMAND g_Console_Cmd_EndLevel = {
.prefix = "endlevel",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/end_level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_EndLevel;
16 changes: 16 additions & 0 deletions src/game/console/cmd/exit_game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "game/console/cmd/exit_game.h"

#include "global/vars.h"

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
g_GF_OverrideDir = GFD_EXIT_GAME;
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_ExitGame = {
.prefix = "exit",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/exit_game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_ExitGame;
16 changes: 16 additions & 0 deletions src/game/console/cmd/exit_to_title.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "game/console/cmd/exit_to_title.h"

#include "global/vars.h"

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
g_GF_OverrideDir = GFD_EXIT_TO_TITLE;
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_ExitToTitle = {
.prefix = "title",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/exit_to_title.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_ExitToTitle;
42 changes: 42 additions & 0 deletions src/game/console/cmd/flipmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "game/console/cmd/flipmap.h"

#include "game/game_string.h"
#include "game/gameflow/gameflow_new.h"
#include "game/room.h"
#include "global/vars.h"

#include <libtrx/strings.h>

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const 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;
}

bool new_state;
if (String_Equivalent(args, "")) {
new_state = !g_FlipStatus;
} else if (!String_ParseBool(args, &new_state)) {
return CR_BAD_INVOCATION;
}

if (g_FlipStatus == new_state) {
Console_Log(
new_state ? GS(OSD_FLIPMAP_FAIL_ALREADY_ON)
: GS(OSD_FLIPMAP_FAIL_ALREADY_OFF));
return CR_SUCCESS;
}

Room_FlipMap();
Console_Log(new_state ? GS(OSD_FLIPMAP_ON) : GS(OSD_FLIPMAP_OFF));
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_FlipMap = {
.prefix = "flip|flipmap",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/flipmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_FlipMap;
21 changes: 21 additions & 0 deletions src/game/console/cmd/fly.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "game/console/cmd/fly.h"

#include "game/game.h"
#include "game/lara/lara_cheat.h"
#include "global/vars.h"

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
if (!Game_IsPlayable()) {
return CR_UNAVAILABLE;
}
Lara_Cheat_EnterFlyMode();
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_Fly = {
.prefix = "fly",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/fly.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_Fly;
39 changes: 39 additions & 0 deletions src/game/console/cmd/load_game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "game/console/cmd/load_game.h"

#include "game/game_string.h"
#include "global/vars.h"

#include <libtrx/strings.h>

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
int32_t slot_num;
if (!String_ParseInteger(args, &slot_num)) {
return CR_BAD_INVOCATION;
}

// convert 1-indexing to 0-indexing
const int32_t slot_idx = slot_num - 1;

if (slot_idx < 0 || slot_idx >= MAX_SAVE_SLOTS) {
Console_Log(GS(OSD_INVALID_SAVE_SLOT), slot_num);
return CR_FAILURE;
}

// TODO: replace this with a proper status check
if (g_SavedLevels[slot_idx] <= 0) {
Console_Log(GS(OSD_LOAD_GAME_FAIL_UNAVAILABLE_SLOT), slot_num);
return CR_FAILURE;
}

g_GF_OverrideDir = GFD_START_SAVED_GAME | slot_idx;
Console_Log(GS(OSD_LOAD_GAME), slot_num);
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_LoadGame = {
.prefix = "load",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/load_game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_LoadGame;
16 changes: 16 additions & 0 deletions src/game/console/cmd/play_demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "game/console/cmd/play_demo.h"

#include "global/vars.h"

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
g_GF_OverrideDir = GFD_START_DEMO;
return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_PlayDemo = {
.prefix = "demo",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/play_demo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_PlayDemo;
53 changes: 53 additions & 0 deletions src/game/console/cmd/play_level.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "game/console/cmd/play_level.h"

#include "game/game_string.h"
#include "global/vars.h"

#include <libtrx/strings.h>

#include <stdio.h>

static COMMAND_RESULT M_Entrypoint(const char *args);

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
int32_t level_to_load = -1;

if (level_to_load == -1) {
int32_t num = 0;
if (sscanf(args, "%d", &num) == 1) {
level_to_load = num;
}
}

if (level_to_load == -1 && strlen(args) >= 2) {
for (int i = 0; i < g_GameFlow.num_levels; i++) {
if (String_CaseSubstring(g_GF_LevelNames[i], args) != NULL) {
level_to_load = i;
break;
}
}
}

if (level_to_load == -1 && String_Equivalent(args, "gym")) {
level_to_load = LV_GYM;
}

if (level_to_load >= g_GameFlow.num_levels) {
Console_Log(GS(OSD_INVALID_LEVEL));
return CR_FAILURE;
}

if (level_to_load != -1) {
g_GF_OverrideDir = GFD_START_GAME | level_to_load;
Console_Log(GS(OSD_PLAY_LEVEL), g_GF_LevelNames[level_to_load]);
return CR_SUCCESS;
}

return CR_BAD_INVOCATION;
}

CONSOLE_COMMAND g_Console_Cmd_PlayLevel = {
.prefix = "play|level",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/play_level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <libtrx/game/console/common.h>

extern CONSOLE_COMMAND g_Console_Cmd_PlayLevel;
Loading
Loading