mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 07:38:19 -10:00
The D-pad pushes the left stick too, but always with its button bit. Games that read the D-pad apart (VP2's battle: left/right split the party) could not be given a plain stick move from a script. The new names push one stick to its end with no button bit or pressure, winning on their axis. Co-Authored-By: Claude Opus 5.5 <[email protected]>
475 lines
18 KiB
C++
475 lines
18 KiB
C++
// libpad (libraries.md section 6): the DualShock 2 controllers in ports 1 and 2 and a multitap's slots, answered as the
|
|
// reference does in its deterministic runs, their buttons from a PadInput (a frame-keyed script in the checks, the PC's
|
|
// devices in a window).
|
|
#include <tuple>
|
|
#include "bozo/libs/libraries.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
|
|
namespace bozo::libs
|
|
{
|
|
// ---- ScriptInput ---------------------------------------------------------------------------------------------
|
|
|
|
namespace
|
|
{
|
|
uint16_t buttonByName(const std::string &n)
|
|
{
|
|
static const std::pair<const char *, uint16_t> names[] = {
|
|
{"SELECT", kPadSelect}, {"L3", kPadL3}, {"R3", kPadR3}, {"START", kPadStart}, {"UP", kPadUp},
|
|
{"RIGHT", kPadRight}, {"DOWN", kPadDown}, {"LEFT", kPadLeft}, {"L2", kPadL2}, {"R2", kPadR2},
|
|
{"L1", kPadL1}, {"R1", kPadR1}, {"TRIANGLE", kPadTriangle}, {"CIRCLE", kPadCircle},
|
|
{"CROSS", kPadCross}, {"SQUARE", kPadSquare}};
|
|
for (const auto &[name, bit] : names)
|
|
if (n == name)
|
|
return bit;
|
|
return 0;
|
|
}
|
|
|
|
// A stick-only entry: the axis (1 lx, 2 ly, 3 rx, 4 ry) and the end it is pushed to; {0, 0x80} when not one.
|
|
std::pair<uint8_t, uint8_t> stickByName(const std::string &n)
|
|
{
|
|
static const std::tuple<const char *, uint8_t, uint8_t> names[] = {
|
|
{"LLEFT", 1, 0x00}, {"LRIGHT", 1, 0xFF}, {"LUP", 2, 0x00}, {"LDOWN", 2, 0xFF},
|
|
{"RLEFT", 3, 0x00}, {"RRIGHT", 3, 0xFF}, {"RUP", 4, 0x00}, {"RDOWN", 4, 0xFF}};
|
|
for (const auto &[name, axis, value] : names)
|
|
if (n == name)
|
|
return {axis, value};
|
|
return {0, 0x80};
|
|
}
|
|
}
|
|
|
|
int padPlayer(int port, int slot, bool multitap)
|
|
{
|
|
if (port == 0)
|
|
return multitap ? (slot >= 0 && slot < 4 ? slot : -1) : (slot == 0 ? 0 : -1);
|
|
return port == 1 && slot == 0 && !multitap ? 1 : -1;
|
|
}
|
|
|
|
bool ScriptInput::load(int player, const std::string &text, std::string &error)
|
|
{
|
|
std::vector<Press> &list = presses_[player];
|
|
size_t at = 0;
|
|
while (at < text.size())
|
|
{
|
|
size_t end = text.find_first_of(",\r\n", at);
|
|
if (end == std::string::npos)
|
|
end = text.size();
|
|
const std::string e = text.substr(at, end - at);
|
|
at = end + 1;
|
|
if (e.empty())
|
|
continue;
|
|
const size_t colon = e.find(':'), slash = e.find('/');
|
|
if (colon == std::string::npos || slash == std::string::npos || slash < colon)
|
|
{
|
|
error = "malformed pad script entry '" + e + "'";
|
|
return false;
|
|
}
|
|
Press p;
|
|
p.from = static_cast<uint32_t>(std::strtoul(e.substr(0, colon).c_str(), nullptr, 10));
|
|
const std::string name = e.substr(colon + 1, slash - colon - 1);
|
|
p.button = buttonByName(name);
|
|
std::tie(p.axis, p.value) = stickByName(name);
|
|
const uint64_t n = std::strtoull(e.substr(slash + 1).c_str(), nullptr, 10);
|
|
p.until = static_cast<uint32_t>(std::min<uint64_t>(p.from + n, 0xFFFFFFFFull));
|
|
if (!p.button && !p.axis)
|
|
{
|
|
error = "unknown button in pad script entry '" + e + "'";
|
|
return false;
|
|
}
|
|
list.push_back(p);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ScriptInput::read(uint32_t frame, int player, PadInputState &out)
|
|
{
|
|
out = PadInputState{};
|
|
auto it = presses_.find(player);
|
|
out.connected = player == 0 || it != presses_.end();
|
|
if (it == presses_.end())
|
|
return;
|
|
for (const Press &p : it->second)
|
|
if (frame >= p.from && frame < p.until)
|
|
out.buttons |= p.button;
|
|
// The D-pad moves the left stick to its end; every pressed button reports full pressure.
|
|
if (out.buttons & kPadLeft)
|
|
out.lx = 0x00;
|
|
if (out.buttons & kPadRight)
|
|
out.lx = 0xFF;
|
|
if (out.buttons & kPadUp)
|
|
out.ly = 0x00;
|
|
if (out.buttons & kPadDown)
|
|
out.ly = 0xFF;
|
|
// Stick entries after the D-pad: they win on their axis.
|
|
for (const Press &p : it->second)
|
|
if (p.axis && frame >= p.from && frame < p.until)
|
|
(p.axis == 1 ? out.lx : p.axis == 2 ? out.ly : p.axis == 3 ? out.rx : out.ry) = p.value;
|
|
// The reference's order puts R1 after L2 (match1p: L1 at byte 16, R1 at byte 18); L2 and R2 are never pressed
|
|
// in the reference runs, so their places (17, 19) are the remaining ones.
|
|
for (int i = 0; i < 12; ++i)
|
|
out.pressure[static_cast<size_t>(i)] = (out.buttons & kPadPressureOrder[i]) ? 0xFF : 0x00;
|
|
}
|
|
|
|
void LiveScriptInput::read(uint32_t frame, int player, PadInputState &out)
|
|
{
|
|
if (frame != lastFrame_)
|
|
{
|
|
lastFrame_ = frame;
|
|
std::ifstream file(path_, std::ios::binary);
|
|
if (file)
|
|
{
|
|
const std::string text{std::istreambuf_iterator<char>(file), {}};
|
|
if (text != lastText_)
|
|
{
|
|
ScriptInput candidate;
|
|
std::string error;
|
|
if (candidate.load(0, text, error))
|
|
{
|
|
script_ = std::move(candidate);
|
|
lastText_ = text;
|
|
origin_ = frame;
|
|
if (std::getenv("BOZO_TRACE_PAD"))
|
|
std::fprintf(stderr, "[pad] live script frame %u: %s\n", frame, text.c_str());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
script_.read(frame - origin_, player, out);
|
|
}
|
|
|
|
// ---- libpad --------------------------------------------------------------------------------------------------
|
|
|
|
namespace
|
|
{
|
|
PadState::Port *port(Libraries &l, Guest &g)
|
|
{
|
|
const uint32_t p = g.arg(0), s = g.arg(1);
|
|
return p < 2 && s < 4 ? &l.pad.ports[p][s] : nullptr;
|
|
}
|
|
|
|
// A socket's controller: the pads in ports 1 and 2 are always there (idle when no player is on them: port 2's
|
|
// behind a multitap); a multitap slot has one when its player has an input.
|
|
PadInputState current(Libraries &l, int p, int s)
|
|
{
|
|
PadInputState st;
|
|
const int player = padPlayer(p, s, l.pad.multitap);
|
|
if (player >= 0 && l.pad.input)
|
|
l.pad.input->read(l.framesEnded, player, st);
|
|
st.connected = (s == 0 && (p == 0 || p == 1)) || (player >= 0 && st.connected);
|
|
return st;
|
|
}
|
|
|
|
// A mode command (scePadSetMainMode, scePadEnterPressMode) keeps the pad busy (state 5, EXECCMD) in the frame
|
|
// it is given and the next; it is stable (6) from the one after.
|
|
bool busy(Libraries &l, PadState::Port &pt)
|
|
{
|
|
if (pt.busy && l.framesEnded > pt.commandFrame + 1)
|
|
pt.busy = false;
|
|
return pt.busy;
|
|
}
|
|
|
|
void command(Libraries &l, PadState::Port &pt)
|
|
{
|
|
pt.busy = true;
|
|
pt.commandFrame = l.framesEnded;
|
|
}
|
|
|
|
void init(Libraries &l, Guest &g)
|
|
{
|
|
l.pad.initialized = true;
|
|
g.ret(1);
|
|
}
|
|
|
|
void copyName(Guest &g, const char *const *names, uint32_t count)
|
|
{
|
|
const uint32_t i = g.arg(0);
|
|
const std::string name = i < count ? names[i] : "";
|
|
g.write(g.arg(1), name.c_str(), static_cast<uint32_t>(name.size() + 1));
|
|
}
|
|
|
|
void stateIntToStr(Libraries &, Guest &g)
|
|
{
|
|
static const char *const names[] = {"DISCONNECT", "", "FINDCTP1", "", "", "EXECCMD", "STABLE", "ERROR"};
|
|
copyName(g, names, 8);
|
|
}
|
|
|
|
void reqIntToStr(Libraries &, Guest &g)
|
|
{
|
|
static const char *const names[] = {"COMPLETE", "FAILED", "BUSY", "ERROR"};
|
|
copyName(g, names, 4);
|
|
}
|
|
|
|
void getDmaStr(Libraries &l, Guest &g)
|
|
{
|
|
const PadState::Port *pt = port(l, g);
|
|
g.retU(pt && pt->open ? pt->dmaBuffer : 0);
|
|
}
|
|
|
|
void getFrameCount(Libraries &l, Guest &g)
|
|
{
|
|
const PadState::Port *pt = port(l, g);
|
|
g.retU(pt && pt->open ? l.framesEnded : 0);
|
|
}
|
|
|
|
void setReqState(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
pt->reqState = g.sarg(2);
|
|
g.ret(1);
|
|
}
|
|
|
|
void infoComb(Libraries &, Guest &g) { g.ret(0); }
|
|
|
|
void setButtonInfo(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
command(l, *pt);
|
|
g.ret(1);
|
|
}
|
|
|
|
void exitPressMode(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
{
|
|
pt->pressureMode = false;
|
|
command(l, *pt);
|
|
}
|
|
g.ret(1);
|
|
}
|
|
|
|
void setVrefParam(Libraries &, Guest &g) { g.ret(1); }
|
|
void getPortMax(Libraries &, Guest &g) { g.ret(2); }
|
|
void getModVersion(Libraries &, Guest &g) { g.ret(0x400); }
|
|
void setWarningLevel(Libraries &, Guest &g) { g.ret(0); }
|
|
|
|
// scePadPortOpen(port, slot, buffer): every slot opens (1), the multitap's too.
|
|
void portOpen(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
{
|
|
pt->open = true;
|
|
pt->dmaBuffer = g.arg(2);
|
|
}
|
|
g.ret(1);
|
|
}
|
|
|
|
void portClose(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
pt->open = false;
|
|
g.ret(1);
|
|
}
|
|
|
|
void end(Libraries &l, Guest &g)
|
|
{
|
|
l.pad.initialized = false;
|
|
g.ret(1);
|
|
}
|
|
|
|
void getSlotMax(Libraries &l, Guest &g) { g.ret(g.arg(0) == 0u && l.pad.multitap ? 4 : g.arg(0) <= 1u ? 1 : 0); }
|
|
|
|
// scePadGetState(port, slot): 6 (stable), 5 (executing a command), 0 (nothing there: a slot without a multitap,
|
|
// or a multitap slot whose player has no controller).
|
|
void getState(Libraries &l, Guest &g)
|
|
{
|
|
PadState::Port *pt = port(l, g);
|
|
if (!pt || !current(l, static_cast<int>(g.arg(0)), static_cast<int>(g.arg(1))).connected)
|
|
{
|
|
g.ret(0);
|
|
return;
|
|
}
|
|
g.ret(busy(l, *pt) ? 5 : 6);
|
|
}
|
|
|
|
// scePadGetReqState: the last request completed (0), or what scePadSetReqState set.
|
|
void getReqState(Libraries &l, Guest &g)
|
|
{
|
|
const PadState::Port *pt = port(l, g);
|
|
g.ret(pt ? pt->reqState : 0);
|
|
}
|
|
|
|
// scePadInfoMode(port, slot, term, offset): the current or extended ID (1, 2): 7 (DualShock 2, analog) once
|
|
// scePadSetMainMode has set analog mode, 4 (digital) before; the mode table (4): its size at offset -1 (2),
|
|
// then 4 and 7.
|
|
void infoMode(Libraries &l, Guest &g)
|
|
{
|
|
PadState::Port *pt = port(l, g);
|
|
const int32_t term = static_cast<int32_t>(g.arg(2)), offset = static_cast<int32_t>(g.arg(3));
|
|
if (!pt)
|
|
{
|
|
g.ret(0);
|
|
return;
|
|
}
|
|
const bool analog = pt->analog && !busy(l, *pt);
|
|
switch (term)
|
|
{
|
|
case 1:
|
|
case 2: g.ret(analog ? 7 : 4); break;
|
|
case 3: g.ret(analog ? 1 : 0); break; // the current mode table entry
|
|
case 4: g.ret(offset == -1 ? 2 : offset == 0 ? 4 : offset == 1 ? 7 : 0); break;
|
|
default: g.ret(0); break;
|
|
}
|
|
}
|
|
|
|
void setMainMode(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
{
|
|
pt->analog = g.arg(2) == 1;
|
|
pt->locked = g.arg(3) == 3;
|
|
command(l, *pt);
|
|
}
|
|
g.ret(1);
|
|
}
|
|
|
|
// scePadInfoAct(port, slot, actuator, term): two actuators (at actuator -1); their details are not asked.
|
|
void infoAct(Libraries &, Guest &g) { g.ret(static_cast<int32_t>(g.arg(2)) == -1 ? 2 : 0); }
|
|
|
|
void setActAlign(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
for (int i = 0; i < 6; ++i)
|
|
pt->align[i] = g.read8(g.arg(2) + static_cast<uint32_t>(i));
|
|
g.ret(1);
|
|
}
|
|
|
|
void setActDirect(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
{
|
|
for (int i = 0; i < 6; ++i)
|
|
pt->direct[i] = g.read8(g.arg(2) + static_cast<uint32_t>(i));
|
|
// The motors the alignment gives each byte: 0 the small one (on / off), 1 the large one (0-255).
|
|
const int player = padPlayer(static_cast<int>(g.arg(0)), static_cast<int>(g.arg(1)), l.pad.multitap);
|
|
if (l.pad.input && player >= 0)
|
|
{
|
|
bool small = false;
|
|
uint8_t large = 0;
|
|
for (int i = 0; i < 6; ++i)
|
|
{
|
|
if (pt->align[i] == 0)
|
|
small = small || pt->direct[i] != 0;
|
|
else if (pt->align[i] == 1)
|
|
large = std::max(large, pt->direct[i]);
|
|
}
|
|
l.pad.input->rumble(player, small, large);
|
|
}
|
|
}
|
|
g.ret(1);
|
|
}
|
|
|
|
void infoPressMode(Libraries &, Guest &g) { g.ret(1); }
|
|
|
|
void enterPressMode(Libraries &l, Guest &g)
|
|
{
|
|
if (PadState::Port *pt = port(l, g))
|
|
{
|
|
pt->pressureMode = true;
|
|
command(l, *pt);
|
|
}
|
|
g.ret(1);
|
|
}
|
|
|
|
// scePadRead(port, slot, data): the pad data (status 0, the mode ID, the buttons active low, then in analog
|
|
// mode the sticks, in pressure mode the twelve pressures). Returns 1.
|
|
void read(Libraries &l, Guest &g)
|
|
{
|
|
PadState::Port *pt = port(l, g);
|
|
const PadInputState st = current(l, static_cast<int>(g.arg(0)), static_cast<int>(g.arg(1)));
|
|
if (!pt || !st.connected)
|
|
{
|
|
g.ret(0);
|
|
return;
|
|
}
|
|
++l.pad.reads;
|
|
++l.pad.socketReads[g.arg(0)][g.arg(1)];
|
|
uint8_t d[20] = {};
|
|
const uint16_t released = static_cast<uint16_t>(~st.buttons);
|
|
d[0] = 0;
|
|
d[2] = static_cast<uint8_t>(released);
|
|
d[3] = static_cast<uint8_t>(released >> 8);
|
|
uint32_t bytes = 4;
|
|
d[1] = 0x41;
|
|
if (pt->analog)
|
|
{
|
|
d[1] = pt->pressureMode ? 0x79 : 0x73;
|
|
d[4] = st.rx;
|
|
d[5] = st.ry;
|
|
d[6] = st.lx;
|
|
d[7] = st.ly;
|
|
bytes = 8;
|
|
if (pt->pressureMode)
|
|
{
|
|
for (int i = 0; i < 12; ++i)
|
|
d[8 + i] = st.pressure[static_cast<size_t>(i)];
|
|
bytes = 20;
|
|
}
|
|
}
|
|
g.write(g.arg(2), d, bytes);
|
|
g.ret(1);
|
|
}
|
|
|
|
// libmtap: a multitap in port 1 only. sceMtapGetModVersion: MTAPMAN 3.0, the major version sceMtapInit
|
|
// expects.
|
|
void mtapInit(Libraries &, Guest &g) { g.ret(1); }
|
|
void mtapPortOpen(Libraries &l, Guest &g)
|
|
{
|
|
const bool open = l.pad.multitapAttached && g.arg(0) == 0u;
|
|
if (open)
|
|
l.pad.multitap = true;
|
|
g.ret(open ? 1 : 0);
|
|
}
|
|
void mtapPortClose(Libraries &l, Guest &g)
|
|
{
|
|
if (g.arg(0) == 0u)
|
|
l.pad.multitap = false;
|
|
g.ret(1);
|
|
}
|
|
void mtapGetConnection(Libraries &l, Guest &g) { g.ret(g.arg(0) == 0u && l.pad.multitap ? 1 : 0); }
|
|
void mtapChangeThreadPriority(Libraries &, Guest &g) { g.ret(1); }
|
|
void mtapGetModVersion(Libraries &, Guest &g) { g.ret(0x0300); }
|
|
}
|
|
|
|
void addPad(std::vector<Function> &out)
|
|
{
|
|
out.push_back({"sceMtapInit", kPad, mtapInit});
|
|
out.push_back({"sceMtapPortOpen", kPad, mtapPortOpen});
|
|
out.push_back({"sceMtapPortClose", kPad, mtapPortClose});
|
|
out.push_back({"sceMtapGetConnection", kPad, mtapGetConnection});
|
|
out.push_back({"sceMtapChangeThreadPriority", kPad, mtapChangeThreadPriority});
|
|
out.push_back({"sceMtapGetModVersion", kPad, mtapGetModVersion});
|
|
out.push_back({"scePadInit", kPad, init});
|
|
out.push_back({"scePadInit2", kPad, init});
|
|
out.push_back({"scePadStateIntToStr", kPad, stateIntToStr});
|
|
out.push_back({"scePadReqIntToStr", kPad, reqIntToStr});
|
|
out.push_back({"scePadGetDmaStr", kPad, getDmaStr});
|
|
out.push_back({"scePadGetFrameCount", kPad, getFrameCount});
|
|
out.push_back({"scePadSetReqState", kPad, setReqState});
|
|
out.push_back({"scePadInfoComb", kPad, infoComb});
|
|
out.push_back({"scePadSetButtonInfo", kPad, setButtonInfo});
|
|
out.push_back({"scePadExitPressMode", kPad, exitPressMode});
|
|
out.push_back({"scePadSetVrefParam", kPad, setVrefParam});
|
|
out.push_back({"scePadGetPortMax", kPad, getPortMax});
|
|
out.push_back({"scePadGetModVersion", kPad, getModVersion});
|
|
out.push_back({"scePadSetWarningLevel", kPad, setWarningLevel});
|
|
out.push_back({"scePadPortOpen", kPad, portOpen});
|
|
out.push_back({"scePadPortClose", kPad, portClose});
|
|
out.push_back({"scePadEnd", kPad, end});
|
|
out.push_back({"scePadGetSlotMax", kPad, getSlotMax});
|
|
out.push_back({"scePadGetState", kPad, getState});
|
|
out.push_back({"scePadGetReqState", kPad, getReqState});
|
|
out.push_back({"scePadInfoMode", kPad, infoMode});
|
|
out.push_back({"scePadSetMainMode", kPad, setMainMode});
|
|
out.push_back({"scePadInfoAct", kPad, infoAct});
|
|
out.push_back({"scePadSetActAlign", kPad, setActAlign});
|
|
out.push_back({"scePadSetActDirect", kPad, setActDirect});
|
|
out.push_back({"scePadInfoPressMode", kPad, infoPressMode});
|
|
out.push_back({"scePadEnterPressMode", kPad, enterPressMode});
|
|
out.push_back({"scePadRead", kPad, read});
|
|
}
|
|
}
|