-
Notifications
You must be signed in to change notification settings - Fork 1
/
yuyuki.lua
425 lines (402 loc) · 13.6 KB
/
yuyuki.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
base_directory = "FILL THIS IN"
--dofile(base_directory .. "mesen.lua")
dofile(base_directory .. "fceux.lua")
messages_filename = base_directory .. "yuyuki_messages.bin"
options_filename = base_directory .. "yuyuki_options.bin"
function has_values(t)
for n in pairs(t) do
return true
end
return false
end
function bitand(a, b)
local result = 0
local bitval = 1
while a > 0 and b > 0 do
if a % 2 == 1 and b % 2 == 1 then -- test the rightmost bits
result = result + bitval -- set the current bit
end
bitval = bitval * 2 -- shift left
a = math.floor(a/2) -- shift right
b = math.floor(b/2)
end
return result
end
function overlap_strs(s1, s2)
for i=1,#s1 do
local new_s1 = string.sub(s1, i, #s1 - 1)
local new_s2 = string.sub(s2, 1, #s1 - i)
if new_s1 == new_s2 then
local new_s3 = string.sub(s2, #s1 - i)
local _, newlines = s1:sub(1, i):gsub("\n", "")
return newlines, new_s2, new_s3
end
end
return s2, nil
end
function clear_all()
Messages.current_message = nil
Messages.current_writing = nil
Messages.write_lag = 0
Messages.newlines = 0
Options.values = {}
bad_translation = true
global_x_offset = 0
last_option_pos = -1
end
Messages = {}
Messages.need_updating = false
function Messages.load_messages()
local file = assert(io.open(messages_filename), "rb")
local data = file:read("*all")
Messages.translations = {}
local start_index = 1
while true do
local end_index = string.find(data, "\0", start_index)
if end_index == nil then break end
local japanese = string.sub(data, start_index, end_index - 1)
start_index = end_index + 1
end_index = string.find(data, "\0", start_index)
local english = string.sub(data, start_index, end_index - 1)
start_index = end_index + 1
Messages.translations[japanese] = english
end
end
Messages.load_messages()
Messages.current_message = nil
Messages.current_writing = nil
Messages.write_lag = 0
Messages.newlines = 0
global_x_offset = 0
function Messages.display()
local scroll_color = e.get_pixel2(16, 25)
local y_offset = 0
if Messages.current_message == nil then return end
if Messages.current_writing ~= nil then
if Messages.write_lag > 0 then
Messages.write_lag = Messages.write_lag - 1
y_offset = 9*Messages.newlines * Messages.write_lag / 8
else
i = 1
while string.byte(Messages.current_writing, i) == 92 do i = i + 1 end
i = i + 1
Messages.current_message =
string.sub(Messages.current_message, 1, -2)
.. string.sub(Messages.current_writing, 1, i)
.. "\n"
Messages.current_writing = string.sub(Messages.current_writing, i + 1)
if Messages.current_writing == "" then
Messages.current_writing = nil
end
end
end
local i = 0
for line in Messages.current_message:gmatch("(.-)\n") do
local x_offset = 1
local heart = false
while string.byte(line, x_offset) == 92 do x_offset = x_offset + 1 end
if string.byte(line, x_offset) == 60 then
heart = true
end
if heart then
e.draw_text(
32 + x_offset + 1 + global_x_offset,
158 + i*9 + y_offset,
"<",
e.black,
e.clear
)
e.draw_text(
32 + x_offset + 3 + global_x_offset,
158 + i*9 + y_offset,
string.sub(line, x_offset + 1),
e.black,
e.clear
)
i = i + 1
else
e.draw_text(
32 + x_offset + global_x_offset,
158 + i*9 + y_offset,
string.sub(line, x_offset),
e.black,
e.clear
)
i = i + 1
end
end
local cursor_color = e.get_pixel2(35, 119)
local scroll_pos = e.read(0x6B)
if e.get_pixel2(34, 120) ~= cursor_color and scroll_pos == 23 then
x = 126
y = 208
e.draw_rect(x, y, x + 4, y, cursor_color)
e.draw_rect(x+1, y+1, x + 3, y+1, cursor_color)
e.draw_rect(x+2, y+2, x + 2, y+2, cursor_color)
end
end
function Messages.add_message()
bad_translation = false
if Messages.current_writing ~= nil then
Messages.current_message
= Messages.current_message .. Messages.current_writing
Messages.current_writing = nil
end
local value = {}
for i=0,0x35 do
local this_char = e.read(0x0560 + i*2)
local this_dak = e.read(0x0561 + i*2)
if this_char ~= 176 then
table.insert(value, this_char)
if this_dak ~= 176 then table.insert(value, this_dak) end
end
end
if #value ~= 0 then
mess = string.char(e.unpack(value))
local trans = Messages.translations[mess]
if not trans then
opt = Options.values[4]
if opt then
i = 1
while string.byte(opt, i) == 92 do i = i + 1 end
j = opt:find("\n")
trans = Messages.translations[mess .. opt:sub(i, j-1)]
end
end
if trans then
if Messages.current_message ~= nil then
Messages.newlines,
Messages.current_message,
Messages.current_writing
= overlap_strs(Messages.current_message, trans)
Messages.write_lag = 8
else
Messages.current_message = ""
Messages.current_writing = trans
end
local scroll_pos = e.read(0x6B)
if scroll_pos < 23 then
global_x_offset = (25 - scroll_pos) * 8
else
global_x_offset = 0
end
else
e.log("Could not find translation for message")
Messages.current_message = nil
bad_translation = true
--for i = 1,#value do
-- print(string.format("%x", value[i]) .. " " .. tostring(value[i]))
--end
end
end
end
Options = {}
Options.values = {}
Options.need_updating = false
function Options.load_options()
local file = assert(io.open(options_filename), "rb")
local data = file:read("*all")
Options.translations = {}
local start_index = 1
while true do
local end_index = string.find(data, "\0", start_index)
if end_index == nil then break end
local japanese = string.sub(data, start_index, end_index - 1)
start_index = end_index + 1
end_index = string.find(data, "\0", start_index)
local english = string.sub(data, start_index, end_index - 1)
start_index = end_index + 1
Options.translations[japanese] = english
end
end
Options.load_options()
function Options.add_value()
bad_translation = false
local total = e.read(0x00A0)
local this = e.read(0x00A1)
-- The name screen, hopefully it never uses all 12 anywhere else
if total == 12 then
Options.values = {}
return
end
local value = {}
for i=0,7 do
local this_char = e.read(0x0560 + i*2)
local this_dak = e.read(0x0561 + i*2)
if this_char ~= 176 then
table.insert(value, this_char)
if this_dak ~= 176 then table.insert(value, this_dak) end
end
end
if #value ~= 0 then
local trans = Options.translations[string.char(e.unpack(value))]
if trans then
Options.values[total - this+1] = trans
else
if value[1] ~= 0 then
e.log("Could not find translation for option")
Options.values[total - this+1] = "UNKNOWN\n"
--for i = 1,#value do
-- print(string.format("%x", value[i]) .. " " .. tostring(value[i]))
--end
end
end
end
end
function Options.display()
local scroll_color = e.get_pixel2(16, 25)
if not has_values(Options.values) then return end
for i, option in pairs(Options.values) do
local num = -1
for line in option:gmatch("(.-)\n") do
num = num + 1
end
local l = 0
for line in option:gmatch("(.-)\n") do
e.draw_text(
33,
28 + i*16 + l*8 - 4*num,
line,
e.black,
e.clear
)
l = l + 1
end
end
end
function scroll_rect(x1, y1, x2, y2, color, flip)
if flip then
e.draw_rect(y1, x1, y2, x2, color)
else
e.draw_rect(x1, y1, x2, y2, color)
end
end
function draw_scroll(x, y, length, flip)
if flip then
x, y = y, x
end
local black = e.get_pixel2(24, 24)
local background = e.get_pixel2(0, 0)
local scroll_color = e.get_pixel2(16, 25)
local dark_scroll = e.get_pixel2(17, 25)
scroll_rect(x, y, x, y + length - 1, black, flip)
scroll_rect(x + 1, y - 7, x + 1, y + length + 6, dark_scroll, flip)
scroll_rect(x + 2, y - 8, x + 2, y + length + 7, scroll_color, flip)
scroll_rect(x + 3, y - 7, x + 4, y + length + 6, dark_scroll, flip)
scroll_rect(x + 5, y - 7, x + 6, y + length + 6, black, flip)
scroll_rect(x + 7, y - 2, x + 7, y + length + 1, dark_scroll, flip)
scroll_rect(x, y - 1, x + 7, y - 1, black, flip)
scroll_rect(x, y - 2, x + 1, y - 2, scroll_color, flip)
scroll_rect(x + 1, y - 8, x + 3, y - 8, scroll_color, flip)
scroll_rect(x + 5, y - 2, x + 6, y - 2, background, flip)
scroll_rect(x + 6, y - 7, x + 6, y - 3, dark_scroll, flip)
scroll_rect(x + 4, y - 8, x + 6, y - 8, black, flip)
scroll_rect(x, y + length + 0, x + 7, y + length + 0, black, flip)
scroll_rect(x, y + length + 1, x + 1, y + length + 1, scroll_color, flip)
scroll_rect(x + 1, y + length + 7, x + 3, y + length + 7, scroll_color, flip)
scroll_rect(x + 5, y + length + 1, x + 6, y + length + 1, background, flip)
scroll_rect(x + 6, y + length + 6, x + 6, y + length + 2, dark_scroll, flip)
scroll_rect(x + 4, y + length + 7, x + 6, y + length + 7, black, flip)
end
function draw_scrolls()
if bad_translation then return end
local background = e.get_pixel2(0, 0)
local scroll_color = e.get_pixel2(16, 25)
local dark_scroll = e.get_pixel2(17, 25)
e.draw_rect(0, 32, 103, 231, background)
e.draw_rect(0, 144, 231, 231, background)
local message = e.read(0x006B)
local option = e.read(0x0071)
if message < 4 then
Messages.current_message = nil
end
while #Options.values > 0 and #Options.values * 2 + 3 > option do
table.remove(Options.values, #Options.values)
end
local message_offset
local option_offset
if message < 14 then
message_offset = 224 - (message - 1) / 13 * 15 * 8
else
message_offset = 104 - (message - 14) / 9 * 11 * 8
end
if option < 16 then
option_offset = 32 + (option - 1) / 15 * 13 * 8
else
--option_offset = 136 + (option - 16) / 11 * 9 * 8
option_offset = math.min(136 + (option - 16) * 8, 224)
end
function draw_messages()
if message == 1 then return end
e.draw_rect(message_offset, 152, 231, 215, dark_scroll)
e.draw_rect(message_offset, 156, 223, 211, scroll_color)
if message == 23 then
e.draw_rect(24, 156, 30, 211, dark_scroll)
end
Messages.display()
e.draw_rect(0, 156, message_offset - 1, 211, background)
end
function draw_options()
if option == 1 then return end
e.draw_rect(24, 32, 95, option_offset - 1, dark_scroll)
e.draw_rect(28, 39, 91, option_offset - 1, scroll_color)
Options.display()
e.draw_rect(28, option_offset, 91, 231, background)
index = e.read(0x00B0)
if index * 2 + 5 < option then
cursor_color = e.get_pixel2(204 - index * 16, 156)
if cursor_color ~= scroll_color then
y = 47 + index*16
e.draw_rect(28, y-2, 28, y+2, cursor_color)
e.draw_rect(29, y-1, 29, y+1, cursor_color)
e.draw_rect(30, y, 30, y, cursor_color)
e.draw_rect(91, y-2, 91, y+2, cursor_color)
e.draw_rect(90, y-1, 90, y+1, cursor_color)
e.draw_rect(89, y, 89, y, cursor_color)
end
end
end
if message <= 14 then
draw_messages()
draw_options()
else
draw_options()
draw_messages()
end
draw_scroll(message_offset, 152, 64, false)
draw_scroll(24, option_offset, 72, true)
end
function on_input(input)
if bad_translation then return input end
input["up"], input["right"] = input["right"], input["up"]
input["down"], input["left"] = input["left"], input["down"]
return input
end
-- 0: Not in game
-- 1: In game
function get_location()
if e.get_pixel2(16, 24) ~= e.get_pixel2(16, 25) then
return 1
else
return 0
end
end
function loop()
-- This is hopefully only when the BIOS is loading the game
if e.read(0x0022) == 16 then return end
local location = get_location()
if location == 1 then
draw_scrolls()
else
clear_all()
bad_translation = false
end
end
e.register_exec(0x7951, Messages.add_message) -- Drawing at the beginning
e.register_exec(0x79C9, Messages.add_message) -- Drawing partway through
e.register_exec(0x7997, Messages.add_message) -- After shifting
e.register_exec(0x8FEC, Options.add_value)
e.register_input(on_input)
e.register_frame(loop)
e.register_save(clear_all)
clear_all()