fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosUpdated a bunch of joystick translations to match games x/y values between -1 and 17277dd1

v2.1.1a
2.3 KiB95 linesraw
1local core = {}
2
3local GAME = {}
4GAME.__index = GAME
5
6function GAME:map(address, structure)
7	self.memorymap[address] = structure
8end
9
10function core.newGame(...)
11	local game = setmetatable({
12		memorymap = {},
13		translateJoyStick = core.translateJoyStick,
14		translateCStick = core.translateCStick,
15		translateTriggers = core.translateTriggers,
16	}, GAME)
17
18	local genericControllers = {...}
19
20	if #genericControllers > 0 then
21		for k, addr in ipairs(genericControllers) do
22			core.loadGenericControllerMap(addr, game)
23		end
24	end
25
26	return game
27end
28
29function core.loadGenericControllerMap(addr, game)
30	local controllers = {
31		[1] = addr + 0xC * 0,
32		[2] = addr + 0xC * 1,
33		[3] = addr + 0xC * 2,
34		[4] = addr + 0xC * 3,
35	}
36
37	local controller_struct = {
38		[0x0] = { type = "short",	name = "controller.%d.buttons.pressed" },
39		[0x2] = { type = "sbyte",	name = "controller.%d.joystick.x" },
40		[0x3] = { type = "sbyte",	name = "controller.%d.joystick.y" },
41		[0x4] = { type = "sbyte",	name = "controller.%d.cstick.x" },
42		[0x5] = { type = "sbyte",	name = "controller.%d.cstick.y" },
43		[0x6] = { type = "byte",	name = "controller.%d.analog.l" },
44		[0x7] = { type = "byte",	name = "controller.%d.analog.r" },
45		[0xA] = { type = "byte",	name = "controller.%d.plugged" },
46	}
47
48	for port, address in ipairs(controllers) do
49		for offset, info in pairs(controller_struct) do
50			game.memorymap[address + offset] = {
51				type = info.type,
52				debug = info.debug,
53				name = info.name:format(port),
54			}
55		end
56	end
57end
58
59local abs = math.abs
60
61-- These functions seem to apply to many games
62
63function core.translateJoyStick(x, y)
64	x = x/72
65	y = y/72
66
67	local near = 1 - (abs(abs(x) - abs(y)))
68
69	local angle = math.atan2(x, y)
70	local mag = math.sqrt(x*x + y*y)
71
72	-- Amplify the magnitute when x,y values are headed towards a diagonal
73	return x + math.sin(angle) * mag * near * 0.25, y + math.cos(angle) * mag * near * 0.25
74end
75
76function core.translateCStick(x, y)
77	x = x/59
78	y = y/59
79
80	local near = 1 - (abs(abs(x) - abs(y)))
81
82	local angle = math.atan2(x, y)
83	local mag = math.sqrt(x*x + y*y)
84
85	-- Amplify the magnitute when x,y values are headed towards a diagonal
86	return x + math.sin(angle) * mag * near * 0.25, y + math.cos(angle) * mag * near * 0.25
87end
88
89local min = math.min
90
91function core.translateTriggers(l, r)
92	return min(1, l/125), min(1, r/125)
93end
94
95return core