mirror of
https://github.com/TheBozos/Steambot_Chronicles_Recomp.git
synced 2026-12-01 12:54:32 -10:00
On M_19_03 (the seafront) the camera at (120.5, -4.5, 10) sits inside one of the level's untextured boxes (prim 0x6792b0 at (120, 0, -30), 100 x 10 x 90). The IMD programs cull back faces in the object's space (L_2250 / L_1250), so the PS2 draws nothing of it from inside. The port compared screen areas and kept every triangle with a corner behind the camera, so the box's inside faces covered everything below the horizon, the player too. The cull (and the outline pass's back-face pick) now takes the determinant of the corners' (x, y, w): the screen area times the three w, whose sign holds behind the camera. Triangles wholly in front of the camera are judged as before. Compared with PCSX2 from the same start: tools/pcsx2/warp_opening.pnach makes New Game load 19:3:0, and SBC_WARP_OPENING does the same in the port. Still different there: the platform top's tint and a paved surface past its edge (docs/RECOMP_NOTES.md). Diagnostics: SBC_3D_PICK=<kick>:<x>:<y> (meshes over a pixel, with w), SBC_TRACE_OBJECTS now prints the camera and the matrices, SBC_TRACE_VERTS, SBC_TRACE_TEXTURE_FAILS, FRAME in the untextured trace. README screenshot. Co-authored-by: Thomas Martin <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
357 lines
16 KiB
C++
357 lines
16 KiB
C++
// Steambot Chronicles' cheats (sbc_cheats.h). The ELF is stripped: the fields are named from the code that uses them.
|
|
//
|
|
// - The Trotmobiles: four machine slots of 0xa360 bytes from 0x58f6a0, their pointers at 0x58f690 [4], the player's at
|
|
// 0x58f660 (0x1e95c0 / 0x1e9570 index them, 0x1eec50 the damage: *(0x58f690 + 4 n) + 0x54 -= damage, floored at 0).
|
|
// Each holds (maximum, current) floats: HP +0x50 / +0x54, fuel +0x5c / +0x60, boost +0x64 / +0x68 (338 / 640 / 50 for
|
|
// Vanilla's first Trotmobile; the PS2 raw codes 2058F6F0 / F700 / F708 agree).
|
|
// - The money: the int at 0x587408 (Money 99,999,999's raw code 20587408); the stomach at 0x587404 (3 sated).
|
|
// - The characters: 0x1d7920 (id) returns *(0x58f640 + 4 id) for an id under 7, the player's (Vanilla's) being id 0;
|
|
// a character's position is its +0x10 (x, y, z, 1; y grows downward), set at its creation from the map's place
|
|
// (SetChrPlayer 0x1576a0 -> 0x1d7540 -> 0x1e43b0).
|
|
// - One-hit kills: every machine but the player's keeps HP 1 (when it has an HP maximum): the next hit takes it to 0 and
|
|
// the game's own damage code and defeat follow.
|
|
#include "sbc_cheats.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <mutex>
|
|
|
|
namespace sbc::cheats
|
|
{
|
|
namespace
|
|
{
|
|
constexpr uint32_t kStomach = 0x587404, kMoneyAt = 0x587408;
|
|
// The stage (StageSet 0x12db80: area, sub-map, entrance) and the request the main loop takes (0x12dbd0's flag,
|
|
// read by 0x10d420, which switches the game to mode 7 and the stage load 0x1312d0).
|
|
constexpr uint32_t kStageArea = 0x57c170, kStageSub = 0x57c178, kStageEntry = 0x57c180, kStageRequest = 0x57c2e8;
|
|
std::atomic<int> g_warpArea{-1}, g_warpSub{0}, g_warpEntry{0};
|
|
constexpr uint32_t kPlayerMachine = 0x58f660, kMachines = 0x58f690, kMachineCount = 4;
|
|
constexpr uint32_t kHpMax = 0x50, kHp = 0x54, kFuelMax = 0x5c, kFuel = 0x60, kBoostMax = 0x64, kBoost = 0x68;
|
|
constexpr uint32_t kCharacters = 0x58f640; // ids 0..6; 0 the player
|
|
constexpr uint32_t kPosition = 0x10;
|
|
// The character's copies of its position (all equal in play; +0x330 / +0x350 the step before).
|
|
constexpr uint32_t kPositionCopies[] = {0x10, 0x270, 0x2b0, 0x2d0, 0x330, 0x350, 0x440, 0x800, 0x840, 0x860};
|
|
constexpr int32_t kSated = 3;
|
|
|
|
bool fromEnvironment(const char *name)
|
|
{
|
|
const char *e = std::getenv(name);
|
|
return e && *e && *e != '0';
|
|
}
|
|
|
|
std::atomic<bool> g_on[static_cast<int>(Cheat::Count)] = {
|
|
fromEnvironment("SBC_CHEAT_HEALTH"), fromEnvironment("SBC_CHEAT_FUEL"), fromEnvironment("SBC_CHEAT_BOOST"),
|
|
fromEnvironment("SBC_CHEAT_MONEY"), fromEnvironment("SBC_CHEAT_FULL"), fromEnvironment("SBC_CHEAT_KILLS")};
|
|
|
|
std::mutex g_lock; // the status and the bookmarks, between the game's thread and the window's
|
|
Status g_status;
|
|
std::array<std::array<float, 3>, kPositionSlots> g_saved{};
|
|
std::array<bool, kPositionSlots> g_hasSaved{};
|
|
std::atomic<int> g_saveRequest{-1}, g_loadRequest{-1};
|
|
std::string g_positionStatus;
|
|
|
|
uint32_t pointerAt(const uint8_t *ram, uint32_t a)
|
|
{
|
|
uint32_t v;
|
|
std::memcpy(&v, ram + a, 4);
|
|
v &= 0x1FFFFFFu;
|
|
return v >= 0x100000u && v < 0x2000000u - 0x1000u ? v : 0u;
|
|
}
|
|
float readFloat(const uint8_t *ram, uint32_t a)
|
|
{
|
|
float v;
|
|
std::memcpy(&v, ram + a, 4);
|
|
return std::isfinite(v) ? v : 0.0f;
|
|
}
|
|
void writeFloat(uint8_t *ram, uint32_t a, float v) { std::memcpy(ram + a, &v, 4); }
|
|
int32_t readInt(const uint8_t *ram, uint32_t a)
|
|
{
|
|
int32_t v;
|
|
std::memcpy(&v, ram + a, 4);
|
|
return v;
|
|
}
|
|
|
|
// A (maximum, current) pair kept full.
|
|
void fill(uint8_t *ram, uint32_t machine, uint32_t maxAt, uint32_t at)
|
|
{
|
|
const float most = readFloat(ram, machine + maxAt);
|
|
if (most > 0 && readFloat(ram, machine + at) < most)
|
|
writeFloat(ram, machine + at, most);
|
|
}
|
|
|
|
void setPositionStatus(const std::string &s)
|
|
{
|
|
g_positionStatus = s;
|
|
std::printf("[sbc:cheats] %s\n", s.c_str());
|
|
}
|
|
|
|
// SBC_CHEAT_SAVE_AT=<frame> / SBC_CHEAT_LOAD_AT=<frame>: bookmark 1 saved / restored at that frame (tests).
|
|
uint64_t frameFromEnvironment(const char *name)
|
|
{
|
|
const char *e = std::getenv(name);
|
|
return e ? std::strtoull(e, nullptr, 0) : 0;
|
|
}
|
|
} // namespace
|
|
|
|
// Every map on the disc (M_<area>_<map>; area 10 has no map file). The names: what each one shows at its entrance 0
|
|
// (warps from the beach, every one loading into play); Seagull Beach and Neuhafen are the game's own. Left out: M_19_02
|
|
// (warped to from the story's opening it does not fit in memory: the game's pools come out empty and its map script
|
|
// never starts, docs/RECOMP_NOTES.md).
|
|
const Destination kDestinations[] = {
|
|
{1, 1, "Seagull Beach"},
|
|
{2, 1, "Country road with a windmill"},
|
|
{3, 1, "Town street (bus stop)"},
|
|
{3, 2, "Indoor arena"},
|
|
{3, 3, "Canal street"},
|
|
{3, 4, "Room with a window"},
|
|
{4, 1, "Canyon road"},
|
|
{5, 1, "Open plain"},
|
|
{6, 1, "Walled yard"},
|
|
{7, 1, "Night road by a train"},
|
|
{8, 1, "Industrial yard"},
|
|
{9, 1, "City street at night"},
|
|
{9, 2, "Rooftop"},
|
|
{9, 3, "Room"},
|
|
{11, 1, "Tunnel"},
|
|
{12, 1, "Pit stop"},
|
|
{13, 1, "Brick square"},
|
|
{14, 1, "Neuhafen"},
|
|
{14, 2, "Neuhafen: arena"},
|
|
{14, 3, "Neuhafen: kitchen"},
|
|
{15, 1, "Tree-lined road"},
|
|
{16, 1, "Paved plaza"},
|
|
{16, 2, "Street"},
|
|
{17, 1, "Forest"},
|
|
{18, 1, "Mine entrance"},
|
|
{19, 1, "Farmland"},
|
|
{19, 3, "Seafront with a lighthouse"},
|
|
{20, 1, "Hills by a lake"},
|
|
{21, 1, "Area 21"},
|
|
{22, 1, "River bank"},
|
|
{23, 1, "Cave lake"},
|
|
{24, 1, "Hangar"},
|
|
{25, 1, "Workshop"},
|
|
};
|
|
const size_t kDestinationCount = sizeof kDestinations / sizeof kDestinations[0];
|
|
|
|
bool enabled(Cheat cheat) { return g_on[static_cast<int>(cheat)].load(std::memory_order_relaxed); }
|
|
void setEnabled(Cheat cheat, bool on) { g_on[static_cast<int>(cheat)].store(on, std::memory_order_relaxed); }
|
|
|
|
Status status()
|
|
{
|
|
std::lock_guard<std::mutex> lock(g_lock);
|
|
return g_status;
|
|
}
|
|
|
|
void requestSavePosition(int slot)
|
|
{
|
|
if (slot >= 0 && slot < kPositionSlots)
|
|
g_saveRequest.store(slot, std::memory_order_relaxed);
|
|
}
|
|
void requestLoadPosition(int slot)
|
|
{
|
|
if (slot >= 0 && slot < kPositionSlots)
|
|
g_loadRequest.store(slot, std::memory_order_relaxed);
|
|
}
|
|
void requestWarp(int area, int subMap, int entrance)
|
|
{
|
|
g_warpSub.store(subMap, std::memory_order_relaxed);
|
|
g_warpEntry.store(entrance, std::memory_order_relaxed);
|
|
g_warpArea.store(area, std::memory_order_relaxed);
|
|
}
|
|
bool hasSavedPosition(int slot)
|
|
{
|
|
std::lock_guard<std::mutex> lock(g_lock);
|
|
return slot >= 0 && slot < kPositionSlots && g_hasSaved[slot];
|
|
}
|
|
std::string positionStatus()
|
|
{
|
|
std::lock_guard<std::mutex> lock(g_lock);
|
|
return g_positionStatus;
|
|
}
|
|
|
|
void apply(uint8_t *ram, uint64_t frame)
|
|
{
|
|
static const uint64_t saveAt = frameFromEnvironment("SBC_CHEAT_SAVE_AT"), loadAt = frameFromEnvironment("SBC_CHEAT_LOAD_AT");
|
|
if (saveAt && frame == saveAt)
|
|
requestSavePosition(0);
|
|
if (loadAt && frame == loadAt)
|
|
requestLoadPosition(0);
|
|
// SBC_TRACE_ENTRANCES=1: a map's entrances (the 0x40-byte records at *(0x57c1a8) the stage matches its
|
|
// entrance against, 0x12fac0; +0xa negative ends them) each time the stage changes.
|
|
if (static const bool entrances = std::getenv("SBC_TRACE_ENTRANCES") != nullptr; entrances)
|
|
{
|
|
static uint32_t last = ~0u;
|
|
const uint32_t list = pointerAt(ram, 0x57c1a8);
|
|
int32_t area, sub, entry;
|
|
std::memcpy(&area, ram + kStageArea, 4);
|
|
std::memcpy(&sub, ram + kStageSub, 4);
|
|
std::memcpy(&entry, ram + kStageEntry, 4);
|
|
const uint32_t key = static_cast<uint32_t>(area * 100 + sub) ^ list << 8;
|
|
if (list && key != last)
|
|
{
|
|
last = key;
|
|
// The places (0x12f380 matches the entrance against +0xe; a negative +0xe ends them).
|
|
uint32_t count = 0;
|
|
for (uint32_t at = list; count < 4096 && at + 0x40 < 0x2000000u; at += 0x40, ++count)
|
|
{
|
|
int16_t id;
|
|
std::memcpy(&id, ram + at + 0xe, 2);
|
|
if (id < 0)
|
|
break;
|
|
}
|
|
std::printf("[sbc:cheats] area %d map %d entrance %d (frame %llu): %u places%c", area, sub, entry,
|
|
static_cast<unsigned long long>(frame), count, 10);
|
|
// Their ids and positions (the entrances a warp can name).
|
|
for (uint32_t i = 0; i < count && i < 64; ++i)
|
|
{
|
|
const uint32_t at = list + i * 0x40u;
|
|
int16_t id;
|
|
float p[3];
|
|
std::memcpy(&id, ram + at + 0xe, 2);
|
|
std::memcpy(p, ram + at + 0x10, sizeof p);
|
|
std::printf("[sbc:cheats] place %u: id %d at (%.0f, %.0f, %.0f)%c", i, id, p[0], p[1], p[2], 10);
|
|
}
|
|
}
|
|
}
|
|
// SBC_WARP_OPENING=<area>:<sub-map>:<entrance>: New Game's first stage (the beach's opening, area 1 entrance 10)
|
|
// made that one instead, as PCSX2's reference cheat does it (tools/pcsx2/warp_opening.pnach): the same start on
|
|
// both for comparisons.
|
|
if (static const char *e = std::getenv("SBC_WARP_OPENING"); e)
|
|
{
|
|
int32_t area, entry;
|
|
std::memcpy(&area, ram + kStageArea, 4);
|
|
std::memcpy(&entry, ram + kStageEntry, 4);
|
|
int to[3];
|
|
if (area == 1 && entry == 10 && std::sscanf(e, "%d:%d:%d", &to[0], &to[1], &to[2]) == 3)
|
|
{
|
|
std::memcpy(ram + kStageArea, &to[0], 4);
|
|
std::memcpy(ram + kStageSub, &to[1], 4);
|
|
std::memcpy(ram + kStageEntry, &to[2], 4);
|
|
std::printf("[sbc:cheats] the opening's stage made area %d, map %d, entrance %d (frame %llu)\n", to[0], to[1],
|
|
to[2], static_cast<unsigned long long>(frame));
|
|
}
|
|
}
|
|
// SBC_WARP=<frame>:<area>:<sub-map>:<entrance>: a warp requested at that frame (tests).
|
|
// Several, comma-separated.
|
|
if (static const char *e = std::getenv("SBC_WARP"); e)
|
|
for (const char *p = e; p && *p;)
|
|
{
|
|
unsigned long long at = 0;
|
|
int area = 0, sub = 0, entry = 0;
|
|
if (std::sscanf(p, "%llu:%d:%d:%d", &at, &area, &sub, &entry) == 4 && frame == at)
|
|
requestWarp(area, sub, entry);
|
|
p = std::strchr(p, ',');
|
|
if (p)
|
|
++p;
|
|
}
|
|
if (const int area = g_warpArea.load(std::memory_order_relaxed); area >= 0)
|
|
{
|
|
const uint32_t sub = static_cast<uint32_t>(g_warpSub.load()), entry = static_cast<uint32_t>(g_warpEntry.load());
|
|
int32_t request;
|
|
std::memcpy(&request, ram + kStageRequest, 4);
|
|
if (request == 0 && (pointerAt(ram, kCharacters) || pointerAt(ram, kPlayerMachine))) // on foot or riding
|
|
{
|
|
const uint32_t a = static_cast<uint32_t>(area);
|
|
std::memcpy(ram + kStageArea, &a, 4);
|
|
std::memcpy(ram + kStageSub, &sub, 4);
|
|
std::memcpy(ram + kStageEntry, &entry, 4);
|
|
const int32_t one = 1;
|
|
std::memcpy(ram + kStageRequest, &one, 4);
|
|
g_warpArea.store(-1, std::memory_order_relaxed);
|
|
std::lock_guard<std::mutex> lock(g_lock);
|
|
char text[128];
|
|
std::snprintf(text, sizeof text, "warping to area %d, map %u, entrance %u (frame %llu)", area, sub, entry,
|
|
static_cast<unsigned long long>(frame));
|
|
setPositionStatus(text);
|
|
}
|
|
}
|
|
|
|
const uint32_t player = pointerAt(ram, kPlayerMachine);
|
|
const bool machineReady = player && readFloat(ram, player + kHpMax) > 0;
|
|
if (machineReady)
|
|
{
|
|
if (enabled(Cheat::InfiniteHealth))
|
|
fill(ram, player, kHpMax, kHp);
|
|
if (enabled(Cheat::InfiniteFuel))
|
|
fill(ram, player, kFuelMax, kFuel);
|
|
if (enabled(Cheat::InfiniteBoost))
|
|
fill(ram, player, kBoostMax, kBoost);
|
|
if (enabled(Cheat::OneHitKills))
|
|
for (uint32_t k = 0; k < kMachineCount; ++k)
|
|
{
|
|
const uint32_t m = pointerAt(ram, kMachines + 4 * k);
|
|
if (m && m != player && readFloat(ram, m + kHpMax) > 0 && readFloat(ram, m + kHp) > 1.0f)
|
|
writeFloat(ram, m + kHp, 1.0f);
|
|
}
|
|
if (enabled(Cheat::InfiniteMoney) && readInt(ram, kMoneyAt) != kMoney)
|
|
std::memcpy(ram + kMoneyAt, &kMoney, 4);
|
|
if (enabled(Cheat::NeverHungry) && readInt(ram, kStomach) != kSated)
|
|
std::memcpy(ram + kStomach, &kSated, 4);
|
|
}
|
|
|
|
// The player character (Vanilla on foot): its position, the bookmarks.
|
|
const uint32_t vanilla = pointerAt(ram, kCharacters);
|
|
const bool hasPosition = vanilla && readFloat(ram, vanilla + kPosition + 12) == 1.0f;
|
|
std::lock_guard<std::mutex> lock(g_lock);
|
|
if (const int slot = g_saveRequest.exchange(-1, std::memory_order_relaxed); slot >= 0)
|
|
{
|
|
if (hasPosition)
|
|
{
|
|
for (int c = 0; c < 3; ++c)
|
|
g_saved[slot][c] = readFloat(ram, vanilla + kPosition + 4 * c);
|
|
g_hasSaved[slot] = true;
|
|
char text[128];
|
|
std::snprintf(text, sizeof text, "bookmark %d saved: %.2f, %.2f, %.2f (frame %llu)", slot + 1, g_saved[slot][0],
|
|
g_saved[slot][1], g_saved[slot][2], static_cast<unsigned long long>(frame));
|
|
setPositionStatus(text);
|
|
}
|
|
else
|
|
setPositionStatus("no player to bookmark (not in play)");
|
|
}
|
|
if (const int slot = g_loadRequest.exchange(-1, std::memory_order_relaxed); slot >= 0)
|
|
{
|
|
if (hasPosition && g_hasSaved[slot])
|
|
{
|
|
// Every copy the character keeps (its position, the coordinate block's, the place's, the last step's).
|
|
for (uint32_t at : kPositionCopies)
|
|
for (int c = 0; c < 3; ++c)
|
|
writeFloat(ram, vanilla + at + 4 * c, g_saved[slot][c]);
|
|
char text[128];
|
|
std::snprintf(text, sizeof text, "moved to bookmark %d: %.2f, %.2f, %.2f (frame %llu)", slot + 1, g_saved[slot][0],
|
|
g_saved[slot][1], g_saved[slot][2], static_cast<unsigned long long>(frame));
|
|
setPositionStatus(text);
|
|
}
|
|
else
|
|
setPositionStatus(hasPosition ? "that bookmark is empty" : "no player to move (not in play)");
|
|
}
|
|
|
|
g_status.ready = machineReady;
|
|
if (machineReady)
|
|
{
|
|
g_status.hp = readFloat(ram, player + kHp);
|
|
g_status.hpMax = readFloat(ram, player + kHpMax);
|
|
g_status.fuel = readFloat(ram, player + kFuel);
|
|
g_status.fuelMax = readFloat(ram, player + kFuelMax);
|
|
g_status.boost = readFloat(ram, player + kBoost);
|
|
g_status.boostMax = readFloat(ram, player + kBoostMax);
|
|
g_status.money = readInt(ram, kMoneyAt);
|
|
g_status.stomach = readInt(ram, kStomach);
|
|
}
|
|
g_status.hasPosition = hasPosition;
|
|
std::memcpy(&g_status.area, ram + kStageArea, 4);
|
|
std::memcpy(&g_status.subMap, ram + kStageSub, 4);
|
|
std::memcpy(&g_status.entrance, ram + kStageEntry, 4);
|
|
if (hasPosition)
|
|
{
|
|
g_status.x = readFloat(ram, vanilla + kPosition);
|
|
g_status.y = readFloat(ram, vanilla + kPosition + 4);
|
|
g_status.z = readFloat(ram, vanilla + kPosition + 8);
|
|
}
|
|
}
|
|
} // namespace sbc::cheats
|