1
--Machrider - Speedometer
4
require("x_functions");
7
-- Sanity check. If they require a newer version, let them know.
12
gui.drawbox( 6, 28 + i, 250, 92 - i, "#000000");
14
gui.text( 10, 32, string.format("This Lua script requires the x_functions library."));
15
gui.text( 53, 42, string.format("It appears you do not have it."));
16
gui.text( 39, 58, "Please get the x_functions library at");
17
gui.text( 14, 69, "http://xkeeper.shacknet.nu/");
18
gui.text(114, 78, "emu/nes/lua/x_functions.lua");
20
warningboxcolor = string.format("%02X", math.floor(math.abs(30 - math.fmod(timer, 60)) / 30 * 0xFF));
21
gui.drawbox(7, 29, 249, 91, "#ff" .. warningboxcolor .. warningboxcolor);
31
function savereplay(filename, replaydata)
35
f = io.open(filename, "w");
36
for k, v in pairs(replaydata) do
37
stringout = string.format("%d:%d:%d\n", k, v['speed'], v['gear']);
54
autoshift = true; -- auto-handle shifting? for the lazy people
64
joydata = joypad.read(1);
65
if joydata['select'] and not joydatas then
66
altunittype = not altunittype;
69
elseif joydata['up'] and joydata['B'] and not joydatas and dorecording then
70
savereplay("machriderspeed.xrp", speedgraph);
74
elseif not joydata['select'] then
78
if saved and dorecording then
79
text(100, 140, "Saved data!");
82
speedlow = memory.readbyte(0x0040);
83
speedhigh = memory.readbyte(0x0041);
84
speed = speedhigh * 0x100 + speedlow;
85
rpmlow = memory.readbyte(0x0042);
86
rpmhigh = memory.readbyte(0x0043);
87
rpm = rpmhigh * 0x100 + rpmlow;
88
gear = memory.readbyte(0x0032);
91
if autoshift and waittimer <= 0 then
93
memory.writebyte(0x0032, 2);
95
elseif speed <= 250 and gear > 2 then
96
memory.writebyte(0x0032, math.max(gear - 1, 2));
98
elseif speed >= 255 and gear < 3 then
99
memory.writebyte(0x0032, math.min(gear + 1, 3));
103
waittimer = waittimer - 1;
107
speedgraph[timer] = {speed = speed, gear = gear};
108
if timer > graphlen then
109
temp = timer - graphlen - 1;
110
-- speedgraph[temp] = nil;
114
for i = timer - graphlen, timer do
115
if speedgraph[i] then
116
xp = ((i + 3) - timer) + graphlen;
117
yp = graphheight - (speedgraph[i]['speed'] / maxspeed) * graphheight;
119
if (speedgraph[i]['gear'] == 0) then
121
elseif (speedgraph[i]['gear'] == 1) then
123
elseif (speedgraph[i]['gear'] == 2) then
125
elseif (speedgraph[i]['gear'] == 3) then
131
-- pixel(((i + 3) - timer) + 60, 50 - speedgraph[i], "#ffffff");
132
line(xp, 10, xp, graphheight + 10, "#000000");
133
line(xp, yp + 10, xp, graphheight + 10, c);
134
pixel(xp, yp + 10, "#ffffff");
136
-- pixel(((i + 3) - timer) + 60, 50 - speedgraph[i], "#ffffff");
142
dist = math.fmod(memory.readbyte(0x0062), 0x20);
143
text( 8, 15, string.format("%02X %4dfr/mv", dist, lastcount));
144
if dist > olddist then
149
counter = counter + 1;
151
lifebar( 8, 8, 0x1F * 5, 3, dist, 0x1F, "#ffffff", "#0000FF", "#000000", "#ffffff");
156
pct = speed / maxspeed * 100;
159
speedadjust = (65535 / 60 / 60 / 60) * speed; -- rough approximation of km/h
160
text(barwidth * segmentwidth - 2, 221, string.format(" %3.1dkm/h", speedadjust));
162
text(barwidth * segmentwidth - 2, 221, string.format(" %3d", speed));
165
box(2, 221, barwidth * segmentwidth + 2, 230, "#000000");
166
box(2, 222, barwidth * segmentwidth + 2, 229, "#000000");
167
box(2, 223, barwidth * segmentwidth + 2, 228, "#000000");
168
box(2, 224, barwidth * segmentwidth + 2, 227, "#000000");
169
box(2, 225, barwidth * segmentwidth + 2, 226, "#000000");
173
for bl = 1, math.min(maxspeed - 1, speed) do
176
segment = math.floor(pct * barwidth);
177
if segment ~= lastseg then
180
val = math.floor(pct * 2 * 0xFF);
181
segcolor = string.format("#%02XFF00", val);
183
elseif pct < 0.90 then
184
val = math.floor(0xFF - (pct - 0.5) * 100/40 * 0xFF);
185
segcolor = string.format("#FF%02X00", val);
187
elseif bl < maxspeed then
188
val = math.floor((pct - 0.90) * 10 * 0xFF);
189
segcolor = string.format("#FF%02X%02X", val, val);
192
segcolor = "#ffffff";
196
yb = math.max(math.min(3, (pct * 100 - 50)), 0);
197
-- box(segment * segmentwidth + 3, 225 - yb, segment * segmentwidth + 3 + (segmentwidth - 2), 229, segcolor);
198
box(segment * segmentwidth + 3, 225 - yb, segment * segmentwidth + 3, 229, segcolor);
199
-- box(bl * 3 + 3, 218, bl * 3 + 4, 225, segcolor);
200
-- line(bl * 3 + 4, 218, bl * 3 + 4, 225, segcolor);
211
pct = rpmhigh / maxrpm * 100;
213
line(2, 220, (barwidth + 2) * segmentwidth + 2, 220, "#000000");
216
text( 2, 203, string.format("AUTO %1d", gear + 1));
218
text( 2, 203, string.format("Manual %1d", gear + 1));
221
text( 2, 211, string.format("%5dRPM", math.min(9999, rpm * .25)));
223
text( 2, 211, string.format("%5dRPM", rpm));
226
rpmhigh = math.min(maxrpm + 10, rpmhigh);
230
for bl = 1, rpmhigh do
233
segment = math.floor(pct * barwidth);
234
if segment ~= lastseg then
236
val = math.floor(pct * (100/70) * 0xFF);
237
segcolor = string.format("#%02XFF00", val);
239
elseif pct < 0.90 then
240
val = math.floor(0xFF - (pct - 0.7) * 100/20 * 0xFF);
241
segcolor = string.format("#FF%02X00", val);
243
elseif bl < maxrpm then
244
val = math.floor((pct - 0.90) * 10 * 0xFF);
245
segcolor = string.format("#FF%02X%02X", val, val);
248
segcolor = "#ffffff";
251
yb = math.floor(math.max(math.min(4, segment - 99), 0) / 2);
252
-- box(segment * segmentwidth + 3, 225 - yb, segment * segmentwidth + 3 + (segmentwidth - 2), 229, segcolor);
253
segment = math.min(segment, barwidth);
254
box(segment * segmentwidth + 3, 221, segment * segmentwidth + 3, 223 - yb, segcolor);
255
-- box(bl * 3 + 3, 218, bl * 3 + 4, 225, segcolor);
256
-- line(bl * 3 + 4, 218, bl * 3 + 4, 225, segcolor);
b'\\ No newline at end of file'