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

Commit

Permalink
console/cmd: follow libtrx folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 20, 2024
1 parent a905583 commit 76dfd94
Show file tree
Hide file tree
Showing 38 changed files with 716 additions and 509 deletions.
16 changes: 14 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,20 @@ dll_sources = [
'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/kill.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
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;
124 changes: 124 additions & 0 deletions src/game/console/cmd/kill.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "game/console/cmd/kill.h"

#include "game/creature.h"
#include "game/game_string.h"
#include "game/items.h"
#include "game/lara/lara_cheat.h"
#include "game/lara/lara_control.h"
#include "game/objects/names.h"
#include "game/objects/vars.h"
#include "global/vars.h"

#include <libtrx/game/objects/common.h>
#include <libtrx/game/objects/ids.h>
#include <libtrx/memory.h>
#include <libtrx/strings.h>

static bool M_CanTargetObjectCreature(GAME_OBJECT_ID object_id);
static COMMAND_RESULT M_Entrypoint(const char *args);

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

static COMMAND_RESULT M_Entrypoint(const char *const args)
{
// kill all the enemies in the level
if (String_Equivalent(args, "all")) {
int32_t num_killed = 0;
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)) {
continue;
}
if (Lara_Cheat_KillEnemy(item_num)) {
num_killed++;
}
}

if (num_killed == 0) {
Console_Log(GS(OSD_KILL_ALL_FAIL));
return CR_FAILURE;
}

Console_Log(GS(OSD_KILL_ALL), num_killed);
return CR_SUCCESS;
}

// kill all the enemies around Lara within one tile, or a single nearest
// enemy
if (String_Equivalent(args, "")) {
bool found = false;
while (true) {
const int16_t best_item_num = Lara_GetNearestEnemy();
if (best_item_num == NO_ITEM) {
break;
}

const ITEM_INFO *const item = &g_Items[best_item_num];
const int32_t distance = Item_GetDistance(item, &g_LaraItem->pos);
found |= Lara_Cheat_KillEnemy(best_item_num);
if (distance >= WALL_L) {
break;
}
}

if (!found) {
Console_Log(GS(OSD_KILL_FAIL));
return CR_FAILURE;
}

Console_Log(GS(OSD_KILL));
return CR_SUCCESS;
}

// kill a single enemy type
{
bool matches_found = false;
int32_t num_killed = 0;
int32_t match_count = 0;
GAME_OBJECT_ID *matching_objs =
Object_IdsFromName(args, &match_count, M_CanTargetObjectCreature);

for (int16_t item_num = 0; item_num < Item_GetTotalCount();
item_num++) {
const ITEM_INFO *const item = &g_Items[item_num];

bool is_matched = false;
for (int32_t i = 0; i < match_count; i++) {
if (matching_objs[i] == item->object_id) {
is_matched = true;
break;
}
}
if (!is_matched) {
continue;
}
matches_found = true;

if (Lara_Cheat_KillEnemy(item_num)) {
num_killed++;
}
}
Memory_FreePointer(&matching_objs);

if (!matches_found) {
Console_Log(GS(OSD_INVALID_OBJECT), args);
return CR_FAILURE;
}
if (num_killed == 0) {
Console_Log(GS(OSD_OBJECT_NOT_FOUND), args);
return CR_FAILURE;
}
Console_Log(GS(OSD_KILL_ALL), num_killed);
return CR_SUCCESS;
}
}

CONSOLE_COMMAND g_Console_Cmd_Kill = {
.prefix = "kill",
.proc = M_Entrypoint,
};
5 changes: 5 additions & 0 deletions src/game/console/cmd/kill.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_Kill;
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;
Loading

0 comments on commit 76dfd94

Please sign in to comment.