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

Import /hp and /pos console commands to libtrx #29

Merged
merged 6 commits into from
Sep 18, 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
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ repos:
entry: tools/additional_lint
language: python
stages: [commit]

- id: imports
name: imports
entry: tools/sort_imports
language: system
files: \.[ch](pp)?$
3 changes: 1 addition & 2 deletions include/libtrx/config/config_map.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "config_option.h"

#include "../enum_str.h"
#include "../utils.h"
#include "config_option.h"

#include <stdbool.h>
#include <stddef.h>
Expand Down
82 changes: 82 additions & 0 deletions include/libtrx/game/collision.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

#include "math.h"

#if TR_VERSION == 1
typedef struct COLL_INFO {
int32_t mid_floor;
int32_t mid_ceiling;
int32_t mid_type;
int32_t front_floor;
int32_t front_ceiling;
int32_t front_type;
int32_t left_floor;
int32_t left_ceiling;
int32_t left_type;
int32_t right_floor;
int32_t right_ceiling;
int32_t right_type;
int32_t radius;
int32_t bad_pos;
int32_t bad_neg;
int32_t bad_ceiling;
XYZ_32 shift;
XYZ_32 old;
int16_t facing;
DIRECTION quadrant;
int16_t coll_type;
int8_t tilt_x;
int8_t tilt_z;
int8_t hit_by_baddie;
int8_t hit_static;
uint16_t slopes_are_walls : 1;
uint16_t slopes_are_pits : 1;
uint16_t lava_is_pit : 1;
uint16_t enable_baddie_push : 1;
uint16_t enable_spaz : 1;
} COLL_INFO;

#elif TR_VERSION == 2
typedef struct __unaligned
{
int32_t floor;
int32_t ceiling;
int32_t type;
}
COLL_SIDE;

typedef struct __unaligned
{
COLL_SIDE side_mid;
COLL_SIDE side_front;
COLL_SIDE side_left;
COLL_SIDE side_right;
int32_t radius;
int32_t bad_pos;
int32_t bad_neg;
int32_t bad_ceiling;
XYZ_32 shift;
XYZ_32 old;
int16_t old_anim_state;
int16_t old_anim_num;
int16_t old_frame_num;
int16_t facing;
int16_t quadrant;
int16_t coll_type;
int16_t *trigger;
int8_t x_tilt;
int8_t z_tilt;
int8_t hit_by_baddie;
int8_t hit_static;
// clang-format off
uint16_t slopes_are_walls: 1; // 0x01 1
uint16_t slopes_are_pits: 1; // 0x02 2
uint16_t lava_is_pit: 1; // 0x04 4
uint16_t enable_baddie_push: 1; // 0x08 8
uint16_t enable_spaz: 1; // 0x10 16
uint16_t hit_ceiling: 1; // 0x20 32
uint16_t pad: 10;
// clang-format on
}
COLL_INFO;
#endif
5 changes: 5 additions & 0 deletions include/libtrx/game/console/commands/pos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../common.h"

extern CONSOLE_COMMAND g_Console_Cmd_Pos;
5 changes: 5 additions & 0 deletions include/libtrx/game/console/commands/set_health.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../common.h"

extern CONSOLE_COMMAND g_Console_Cmd_SetHealth;
17 changes: 17 additions & 0 deletions include/libtrx/game/console/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdbool.h>

typedef enum {
CR_SUCCESS,
CR_FAILURE,
CR_UNAVAILABLE,
CR_BAD_INVOCATION,
} COMMAND_RESULT;

typedef struct {
const char *prefix;
COMMAND_RESULT (*proc)(const char *args);
} CONSOLE_COMMAND;

void Console_Log(const char *fmt, ...);
4 changes: 4 additions & 0 deletions include/libtrx/game/const.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

#define DONT_TARGET (-16384)

#define PHD_ONE 0x10000
#define STEP_L 256
#define WALL_L 1024
5 changes: 5 additions & 0 deletions include/libtrx/game/game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdbool.h>

bool Game_IsPlayable(void);
3 changes: 3 additions & 0 deletions include/libtrx/game/game_string.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GS_DEFINE(OSD_POS_GET, "Room: %d\nPosition: %.3f, %.3f, %.3f\nRotation: %.3f,%.3f,%.3f")
GS_DEFINE(OSD_CURRENT_HEALTH_GET, "Current Lara's health: %d")
GS_DEFINE(OSD_CURRENT_HEALTH_SET, "Lara's health set to %d")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this require language translations to now translate this second file on top of the strings in the gameflow? Unless I'm reading wrong this early lol. That would be my only feedback.

Copy link
Contributor Author

@rr- rr- Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. These provide reasonable defaults in case the player or level builder removes the strings from the gameflow. Our tools automatically backpropagate these as default overrides to the gameflow files to highlight what can be overridden, but they are not strictly necessary as they ship the same strings that we provide here in the executable itself.

For a developer this means updating it only in this single place for libtrx strings, and in relevant .def files for TR1X and TR2X specific strings. The rest should be taken care of by tools/update_gameflow.

2 changes: 1 addition & 1 deletion include/libtrx/game/items.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "math.h"
#include "objects.h"
#include "objects/ids.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
7 changes: 7 additions & 0 deletions include/libtrx/game/lara/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "../items.h"

#define LARA_MAX_HITPOINTS 1000

ITEM_INFO *Lara_GetItem(void);
9 changes: 9 additions & 0 deletions include/libtrx/game/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ typedef struct {
int16_t z;
} XYZ_16;

#if TR_VERSION == 1
typedef enum {
DIR_NORTH = 0,
DIR_EAST = 1,
DIR_SOUTH = 2,
DIR_WEST = 3,
} DIRECTION;
#endif

#pragma pack(pop)
103 changes: 103 additions & 0 deletions include/libtrx/game/objects/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include "../collision.h"
#include "../items.h"
#include "../math.h"
#include "ids.h"

#include <stdint.h>

#if TR_VERSION == 1
typedef struct {
XYZ_16 min;
XYZ_16 max;
} BOUNDS_16;

typedef struct {
struct {
XYZ_16 min;
XYZ_16 max;
} shift, rot;
} OBJECT_BOUNDS;

typedef struct {
BOUNDS_16 bounds;
XYZ_16 offset;
int16_t nmeshes;
int32_t *mesh_rots;
} FRAME_INFO;

typedef struct {
int16_t nmeshes;
int16_t mesh_index;
int32_t bone_index;
FRAME_INFO *frame_base;
void (*initialise)(int16_t item_num);
void (*control)(int16_t item_num);
int16_t (*floor_height_func)(
const ITEM_INFO *item, int32_t x, int32_t y, int32_t z, int16_t height);
int16_t (*ceiling_height_func)(
const ITEM_INFO *item, int32_t x, int32_t y, int32_t z, int16_t height);
void (*draw_routine)(ITEM_INFO *item);
void (*collision)(int16_t item_num, ITEM_INFO *lara_item, COLL_INFO *coll);
const OBJECT_BOUNDS *(*bounds)(void);
int16_t anim_index;
int16_t hit_points;
int16_t pivot_length;
int16_t radius;
int16_t smartness;
int16_t shadow_size;
uint16_t loaded : 1;
uint16_t intelligent : 1;
uint16_t save_position : 1;
uint16_t save_hitpoints : 1;
uint16_t save_flags : 1;
uint16_t save_anim : 1;
} OBJECT_INFO;

#elif TR_VERSION == 2
typedef struct __unaligned
{
int16_t mesh_count;
int16_t mesh_idx;
int32_t bone_idx;
int16_t *frame_base; // TODO: make me FRAME_INFO

void (*initialise)(int16_t item_num);
void (*control)(int16_t item_num);
void (*floor)(
const ITEM_INFO *item, int32_t x, int32_t y, int32_t z,
int32_t *height);
void (*ceiling)(
const ITEM_INFO *item, int32_t x, int32_t y, int32_t z,
int32_t *height);
void (*draw_routine)(const ITEM_INFO *item);
void (*collision)(int16_t item_num, ITEM_INFO *lara_item, COLL_INFO *coll);

int16_t anim_idx;
int16_t hit_points;
int16_t pivot_length;
int16_t radius;
int16_t shadow_size;

union {
uint16_t flags;
// clang-format off
struct {
uint16_t loaded: 1; // 0x01 1
uint16_t intelligent: 1; // 0x02 2
uint16_t save_position: 1; // 0x04 4
uint16_t save_hitpoints: 1; // 0x08 8
uint16_t save_flags: 1; // 0x10 16
uint16_t save_anim: 1; // 0x20 32
uint16_t semi_transparent: 1; // 0x40 64
uint16_t water_creature: 1; // 0x80 128
uint16_t pad: 8;
};
// clang-format on
};
}
OBJECT_INFO;
#endif

OBJECT_INFO *Object_GetObject(GAME_OBJECT_ID object_id);
2 changes: 1 addition & 1 deletion include/libtrx/gfx/2d/2d_renderer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "2d_surface.h"
#include "../gl/buffer.h"
#include "../gl/program.h"
#include "../gl/sampler.h"
#include "../gl/texture.h"
#include "../gl/vertex_array.h"
#include "2d_surface.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
3 changes: 1 addition & 2 deletions include/libtrx/gfx/2d/2d_surface.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "../gl/gl_core_3_3.h"

#include "../../engine/image.h"
#include "../gl/gl_core_3_3.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion include/libtrx/gfx/3d/3d_renderer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "vertex_stream.h"
#include "../common.h"
#include "../config.h"
#include "../gl/gl_core_3_3.h"
#include "../gl/program.h"
#include "../gl/sampler.h"
#include "../gl/texture.h"
#include "vertex_stream.h"

#define GFX_MAX_TEXTURES 128
#define GFX_NO_TEXTURE (-1)
Expand Down
3 changes: 1 addition & 2 deletions include/libtrx/gfx/gl/utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "../gl/gl_core_3_3.h"

#include "../../log.h"
#include "../gl/gl_core_3_3.h"

#define GFX_GL_CheckError() \
{ \
Expand Down
3 changes: 2 additions & 1 deletion include/libtrx/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ bool String_EndsWith(const char *str, const char *suffix);
bool String_Equivalent(const char *a, const char *b);

const char *String_CaseSubstring(const char *subject, const char *pattern);
int32_t String_Match(const char *subject, const char *pattern);
bool String_Match(const char *subject, const char *pattern);

bool String_IsEmpty(const char *value);
bool String_ParseBool(const char *value, bool *target);
bool String_ParseInteger(const char *value, int32_t *target);
bool String_ParseDecimal(const char *value, float *target);
2 changes: 1 addition & 1 deletion include/libtrx/virtual_file.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <stdint.h>

typedef struct {
char *content;
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ sources = [
'src/engine/image.c',
'src/enum_str.c',
'src/filesystem.c',
'src/game/console/commands/pos.c',
'src/game/console/commands/set_health.c',
'src/game/items.c',
'src/game/game_string.c',
'src/gfx/2d/2d_renderer.c',
Expand Down
38 changes: 38 additions & 0 deletions src/game/console/commands/pos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "game/console/commands/pos.h"

#include "game/console/common.h"
#include "game/const.h"
#include "game/game_string.h"
#include "game/lara/common.h"
#include "game/objects/common.h"

static COMMAND_RESULT Console_Cmd_Pos(const char *args);

static COMMAND_RESULT Console_Cmd_Pos(const char *const args)
{
const OBJECT_INFO *const object = Object_GetObject(O_LARA);
if (!object->loaded) {
return CR_UNAVAILABLE;
}

const ITEM_INFO *const lara_item = Lara_GetItem();

// clang-format off
Console_Log(
GS(OSD_POS_GET),
lara_item->room_num,
lara_item->pos.x / (float)WALL_L,
lara_item->pos.y / (float)WALL_L,
lara_item->pos.z / (float)WALL_L,
lara_item->rot.x * 360.0f / (float)PHD_ONE,
lara_item->rot.y * 360.0f / (float)PHD_ONE,
lara_item->rot.z * 360.0f / (float)PHD_ONE);
// clang-format on

return CR_SUCCESS;
}

CONSOLE_COMMAND g_Console_Cmd_Pos = {
.prefix = "pos",
.proc = Console_Cmd_Pos,
};
Loading