~ubuntu-branches/ubuntu/lucid/fceux/lucid

« back to all changes in this revision

Viewing changes to fceu/output/luaScripts/Machrider.lua

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-12-14 08:05:17 UTC
  • Revision ID: james.westby@ubuntu.com-20091214080517-abi5tj8avthfan7c
Tags: upstream-2.1.2+repack
ImportĀ upstreamĀ versionĀ 2.1.2+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--Machrider - Speedometer
 
2
--Written by XKeeper
 
3
 
 
4
require("x_functions");
 
5
 
 
6
if not x_requires then
 
7
        -- Sanity check. If they require a newer version, let them know.
 
8
        timer   = 1;
 
9
        while (true) do
 
10
                timer = timer + 1;
 
11
                for i = 0, 32 do
 
12
                        gui.drawbox( 6, 28 + i, 250, 92 - i, "#000000");
 
13
                end;
 
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");
 
19
 
 
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);
 
22
 
 
23
                FCEU.frameadvance();
 
24
        end;
 
25
 
 
26
else
 
27
        x_requires(4);
 
28
end;
 
29
 
 
30
 
 
31
function savereplay(filename, replaydata) 
 
32
 
 
33
        stringout       = "";
 
34
 
 
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']);
 
38
                f:write(stringout);
 
39
        end;
 
40
 
 
41
        f:close();
 
42
 
 
43
        return true;
 
44
 
 
45
end;
 
46
 
 
47
 
 
48
 
 
49
olddist                 = 0;
 
50
lastcount               = 0;
 
51
counter                 = 0;
 
52
altunittype             = true;
 
53
waittimer               = 0;
 
54
autoshift               = true;         -- auto-handle shifting? for the lazy people
 
55
speedgraph              = {};
 
56
timer                   = 0;
 
57
graphlen                = 240;
 
58
graphheight             = 100;
 
59
maxspeed                = 500;
 
60
dorecording             = false;
 
61
 
 
62
while true do
 
63
        
 
64
        joydata         = joypad.read(1);
 
65
        if joydata['select'] and not joydatas then
 
66
                altunittype     = not altunittype;
 
67
                joydatas        = true;
 
68
 
 
69
        elseif joydata['up'] and joydata['B'] and not joydatas and dorecording then
 
70
                savereplay("machriderspeed.xrp", speedgraph);
 
71
                joydatas        = true;
 
72
                saved           = true;
 
73
 
 
74
        elseif not joydata['select'] then
 
75
                joydatas        = false;
 
76
        end;
 
77
 
 
78
        if saved and dorecording then
 
79
                text(100, 140, "Saved data!");
 
80
        end;
 
81
 
 
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);
 
89
 
 
90
        
 
91
        if autoshift and waittimer <= 0 then
 
92
                if gear < 2 then
 
93
                        memory.writebyte(0x0032, 2);
 
94
--                      waittimer       = 6;
 
95
                elseif speed <= 250 and gear > 2 then
 
96
                        memory.writebyte(0x0032, math.max(gear - 1, 2));
 
97
--                      waittimer       = 6;
 
98
                elseif speed >= 255 and gear < 3 then
 
99
                        memory.writebyte(0x0032, math.min(gear + 1, 3));
 
100
--                      waittimer       = 6;
 
101
                end;
 
102
        end;
 
103
        waittimer       = waittimer - 1;
 
104
 
 
105
        if dorecording then
 
106
                timer                           = timer + 1;
 
107
                speedgraph[timer]       = {speed = speed, gear = gear};
 
108
                if timer > graphlen then
 
109
                        temp                            = timer - graphlen - 1;
 
110
        --              speedgraph[temp]        = nil;
 
111
                end;
 
112
 
 
113
 
 
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;
 
118
 
 
119
                                if (speedgraph[i]['gear'] == 0) then
 
120
                                        c       = "blue";
 
121
                                elseif (speedgraph[i]['gear'] == 1) then
 
122
                                        c       = "green";
 
123
                                elseif (speedgraph[i]['gear'] == 2) then
 
124
                                        c       = "#cccc00";
 
125
                                elseif (speedgraph[i]['gear'] == 3) then
 
126
                                        c       = "red";
 
127
                                else
 
128
                                        c       = "gray";
 
129
                                end;
 
130
 
 
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");
 
135
 
 
136
        --                      pixel(((i + 3) - timer) + 60, 50 - speedgraph[i], "#ffffff");
 
137
                        end;
 
138
                end;
 
139
        end;
 
140
 
 
141
--[[
 
142
        dist            = math.fmod(memory.readbyte(0x0062), 0x20);
 
143
        text( 8, 15, string.format("%02X   %4dfr/mv", dist, lastcount));
 
144
        if dist > olddist then
 
145
                lastcount       = counter;
 
146
                counter         = 0;
 
147
        end;
 
148
        olddist                 = dist;
 
149
        counter         = counter + 1;
 
150
 
 
151
        lifebar( 8,  8, 0x1F * 5, 3, dist, 0x1F, "#ffffff", "#0000FF", "#000000", "#ffffff");
 
152
--]]
 
153
 
 
154
        barwidth                        = 100;
 
155
        segmentwidth            = 2;
 
156
        pct                                     = speed / maxspeed * 100;
 
157
 
 
158
        if altunittype then
 
159
                speedadjust             = (65535 / 60 / 60 / 60) * speed;       -- rough approximation of km/h
 
160
                text(barwidth * segmentwidth - 2, 221, string.format(" %3.1dkm/h", speedadjust));
 
161
        else
 
162
                text(barwidth * segmentwidth - 2, 221, string.format(" %3d", speed));
 
163
        end;
 
164
 
 
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");
 
170
        lastseg         = false;
 
171
 
 
172
        if pct > 0 then 
 
173
                for bl = 1, math.min(maxspeed - 1, speed) do
 
174
                
 
175
                        pct             = bl / maxspeed;
 
176
                        segment = math.floor(pct * barwidth);
 
177
                        if segment ~= lastseg then
 
178
 
 
179
                                if pct < 0.50 then
 
180
                                        val             = math.floor(pct * 2 * 0xFF);
 
181
                                        segcolor        = string.format("#%02XFF00", val);
 
182
 
 
183
                                elseif pct < 0.90 then
 
184
                                        val             = math.floor(0xFF - (pct - 0.5) * 100/40 * 0xFF);
 
185
                                        segcolor        = string.format("#FF%02X00", val);
 
186
 
 
187
                                elseif bl < maxspeed then
 
188
                                        val     = math.floor((pct - 0.90) * 10 * 0xFF);
 
189
                                        segcolor        = string.format("#FF%02X%02X", val, val);
 
190
 
 
191
                                else
 
192
                                        segcolor        = "#ffffff";
 
193
                                end;
 
194
 
 
195
 
 
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);
 
201
                        end;
 
202
                        lastseg = segment;
 
203
                end;
 
204
        end;
 
205
 
 
206
 
 
207
 
 
208
        maxrpm                          = 0x7F;
 
209
        barwidth                        = 104;
 
210
        segmentwidth            = 1;
 
211
        pct                                     = rpmhigh / maxrpm * 100;
 
212
 
 
213
        line(2, 220, (barwidth + 2) * segmentwidth + 2, 220, "#000000");
 
214
 
 
215
        if autoshift then
 
216
                text( 2, 203, string.format("AUTO %1d", gear + 1));
 
217
        else
 
218
                text( 2, 203, string.format("Manual %1d", gear + 1));
 
219
        end;
 
220
        if altunittype then
 
221
                text( 2, 211, string.format("%5dRPM", math.min(9999, rpm * .25)));
 
222
        else
 
223
                text( 2, 211, string.format("%5dRPM", rpm));
 
224
        end;
 
225
 
 
226
        rpmhigh                         = math.min(maxrpm + 10, rpmhigh);
 
227
        lastseg         = false;
 
228
 
 
229
        if pct > 0 then 
 
230
                for bl = 1, rpmhigh do
 
231
                
 
232
                        pct             = bl / maxrpm;
 
233
                        segment = math.floor(pct * barwidth);
 
234
                        if segment ~= lastseg then
 
235
                                if pct < 0.70 then
 
236
                                        val             = math.floor(pct * (100/70) * 0xFF);
 
237
                                        segcolor        = string.format("#%02XFF00", val);
 
238
 
 
239
                                elseif pct < 0.90 then
 
240
                                        val             = math.floor(0xFF - (pct - 0.7) * 100/20 * 0xFF);
 
241
                                        segcolor        = string.format("#FF%02X00", val);
 
242
 
 
243
                                elseif bl < maxrpm then
 
244
                                        val     = math.floor((pct - 0.90) * 10 * 0xFF);
 
245
                                        segcolor        = string.format("#FF%02X%02X", val, val);
 
246
 
 
247
                                else
 
248
                                        segcolor        = "#ffffff";
 
249
                                end;
 
250
 
 
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);
 
257
                        end;
 
258
                        lastseg = seg;
 
259
                end;
 
260
        end;
 
261
 
 
262
 
 
263
 
 
264
 
 
265
        FCEU.frameadvance();
 
266
end;
 
 
b'\\ No newline at end of file'