1SDLK_UP           = 82 + 1073741824
2SDLK_DOWN         = 81 + 1073741824
3SDLK_RIGHT        = 79 + 1073741824
4SDLK_LEFT         = 80 + 1073741824
5
6g_img_tbl = {}
7
8local function poll_for_esc()
9	local input = input_poll()
10	if input ~= nil and input == 27 then
11		return true
12	end
13
14	if engine_should_quit() == 1 then
15		return true
16	end
17
18	return false
19end
20
21local function wait_for_input()
22	local input = nil
23	while input == nil do
24		canvas_update()
25		input = input_poll()
26			if input ~= nil then
27				break
28			end
29
30			if engine_should_quit() == 1 then
31				break
32			end
33	end
34
35	return input
36end
37
38local function should_exit(input)
39	if engine_should_quit() == 1 then
40		return true
41	end
42
43	if input ~=nil and input == 27 then
44		return true
45	end
46
47	return false
48end
49
50local function fade_out()
51	local i
52	for i=0xff,0,-3 do
53		canvas_set_opacity(i)
54		canvas_update()
55
56		if engine_should_quit() == 1 then
57			break
58		end
59	end
60
61	return false
62end
63
64local function fade_out_sprite(sprite, speed)
65	local i
66	if speed ~= nil then
67		speed = -speed
68	else
69		speed = -3
70	end
71
72	for i=0xff,0,speed do
73		sprite.opacity = i
74		canvas_update()
75
76		if engine_should_quit() == 1 then
77			break
78		end
79	end
80
81	return false
82end
83
84local function fade_in()
85	local i
86	for i=0x0,0xff,3 do
87		canvas_set_opacity(i)
88		canvas_update()
89
90		if engine_should_quit() == 1 then
91			break
92		end
93	end
94
95	return false
96end
97
98local function fade_in_sprite(sprite, speed)
99	local i
100	if speed ~= nil then
101		speed = speed
102	else
103		speed = 3
104	end
105
106	for i=0,0xff,speed do
107		sprite.opacity = i
108		canvas_update()
109
110		if engine_should_quit() == 1 then
111			break
112		end
113	end
114
115	return false
116end
117
118local function load_images(filename)
119	g_img_tbl = image_load_all(filename)
120end
121
122g_clock_tbl = {}
123
124local function load_clock()
125	g_clock_tbl["h1"] = sprite_new(nil, 0xdd, 0x14, true)
126	g_clock_tbl["h2"] = sprite_new(nil, 0xdd+4, 0x14, true)
127	g_clock_tbl["m1"] = sprite_new(nil, 0xdd+0xa, 0x14, true)
128	g_clock_tbl["m2"] = sprite_new(nil, 0xdd+0xe, 0x14, true)
129end
130
131local function display_clock()
132	local t = os.date("*t")
133	local hour = t.hour
134	local minute = t.min
135
136	if hour > 12 then
137		hour = hour - 12
138	end
139
140	if hour < 10 then
141		g_clock_tbl["h1"].image = g_img_tbl[12]
142	else
143		g_clock_tbl["h1"].image = g_img_tbl[3]
144	end
145
146	if hour >= 10 then
147		hour = hour - 10
148	end
149
150	g_clock_tbl["h2"].image = g_img_tbl[hour+2]
151
152	g_clock_tbl["m1"].image = g_img_tbl[math.floor(minute / 10) + 2]
153	g_clock_tbl["m2"].image = g_img_tbl[(minute % 10) + 2]
154
155end
156
157g_lounge_tbl = {}
158
159g_tv_base_x = 0xe5
160g_tv_base_y = 0x32
161
162local function load_lounge()
163
164	g_lounge_tbl["bg0"] = sprite_new(g_img_tbl[15], 210, 0, true)
165	g_lounge_tbl["bg"] = sprite_new(g_img_tbl[0], 0, 0, true)
166	g_lounge_tbl["bg1"] = sprite_new(g_img_tbl[1], 320, 0, true)
167	g_lounge_tbl["avatar"] = sprite_new(g_img_tbl[0xd], 0, 63, true)
168
169	g_lounge_tbl["tv"] = {sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true)}
170
171	for i=1,5 do
172		local sprite = g_lounge_tbl["tv"][i]
173		sprite.clip_x = g_tv_base_x
174		sprite.clip_y = g_tv_base_y
175		sprite.clip_w = 57
176		sprite.clip_h = 37
177	end
178
179	g_lounge_tbl["finger"] = sprite_new(g_img_tbl[0xe], 143, 91, true)
180
181	g_lounge_tbl["tv_static"] = image_new(57,37)
182
183	load_clock()
184end
185
186g_tv_loop_pos = 0
187g_tv_loop_cnt = 0
188g_tv_cur_pos = 1
189g_tv_cur_program = 3
190
191g_tv_news_image = 0
192g_tv_news_image_tbl = {0x5,0x6,0x7,0x0D,0x0E,0x18,0x19,0x1F,0x20}
193
194g_tv_programs = {
195{0x82,0x82,0x80,0x3,0x2,0x8A,0x2,0x8A,0x1,0x8A,0x1,0x8A,0x0,0x8A,0x0,0x8A,0x1,0x8A,0x1,0x81}, --breathing
196{0x82,0x82,0x80,0x28,0x3,0x8B,0x81}, --intestines
197{0x82,0x82,0x80,0x4,0x4,0x81,0x80,0x4,0x8,0x81,0x80,0x4,0x9,0x81,0x80,0x4,0x0A,0x81,0x80,0x4,0x0B,0x81,0x80,0x4,0x0C,0x81}, --vacuum
198{0x82,0x82,0x87,0x80,0x46,0x0F,0x86,0x84,0x9,0x10,0x10,0x10,0x11,0x11,0x11,0x12,0x12,0x12,0x81}, --tv anchor man
199{0x82,0x82,0x80,0x32,0x83,0x81}, --endless road
200{0x82,0x82,0x80,0x5,0x27,0x8A,0x28,0x8A,0x29,0x81,0x80,0x6,0x2A,0x8A,0x2A,0x8A,0x2B,0x8A,0x2B,0x8A,0x2C,0x8A,0x2C,0x8A,0x2D,0x8A,0x2D,0x81,0x80,0x0A,0x2E,0x8A,0x2F,0x8A,0x30,0x8A,0x84,0x9,0x2E,0x2E,0x2E,0x2F,0x2F,0x2F,0x30,0x30,0x30,0x8A,0x2E,0x8A,0x2F,0x8A,0x30,0x81}, --rock band
201{0x82,0x82,0x80,0x55,0x16,0x17,0x84,0x0C,0x13,0x13,0x13,0x13,0x14,0x14,0x14,0x14,0x15,0x15,0x15,0x15,0x88,0x81,0x80,0x0F,0x16,0x84,0x2,0x1A,0x1B,0x89,0x88,0x81,0x80,0x3,0x16,0x1A,0x89,0x88,0x81,0x80,0x3,0x16,0x1C,0x88,0x81,0x80,0x3,0x16,0x1D,0x88,0x81,0x80,0x3,0x16,0x1E,0x88,0x81,0x80,0x3,0x16,0x23,0x88,0x81,0x80,0x3,0x16,0x24,0x88,0x81,0x80,0x32,0x16,0x88,0x81} --pledge now!
202}
203
204
205g_tv_x_off = {
2060x0,0x0,0x0,0x0,0x0,0x1F,0x1F,0x1F,0x0,0x0,0x0,0x0,0x0,0x1F,0x1F,
2070x0,0x9,0x9,0x9,0x0C,0x0C,0x0C,0x0,0x4,0x1F,0x1F,0x4,0x0,0x4,
2080x4,0x4,0x1F,0x1F,0x0,0x0,0x6,0x6,0x8,0x4,0x0,0x0,0x0,0x0,0x0,0x0,
2090x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
210}
211
212g_tv_y_off = {
2130x0,0x0,0x0,0x0,0x0,0x2,0x2,0x2,0x0,0x0,0x0,0x0,0x0,0x2,0x2,0x0,0x7,0x7,
2140x7,0x3,0x3,0x3,0x0,0x2,0x2,0x2,0x0,0x0,0x0,0x0,0x1,0x2,0x2,0x0,0x0,0x3,
2150x8,0x1D,0x1C,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
2160x0,0x0
217}
218
219g_tv_pledge_counter = 0
220g_tv_pledge_image = 37
221
222g_tv_road_offset = 0xe
223
224local function display_tv_sprite(s_idx, image_num)
225	local sprite = g_lounge_tbl.tv[s_idx]
226	if sprite ~= nil then
227		sprite.image = g_img_tbl[0x10+image_num]
228		sprite.x = g_tv_base_x + g_tv_x_off[image_num+1]
229		sprite.y = g_tv_base_y + g_tv_y_off[image_num+1]
230		if g_tv_base_x < 0 then
231			sprite.clip_x = 0
232		else
233			sprite.clip_x = g_tv_base_x
234		end
235		sprite.clip_y = g_tv_base_y
236		sprite.visible = true
237	end
238end
239
240local function display_tv()
241	local should_exit = false
242	local s_idx = 1
243	for i=1,5 do
244		g_lounge_tbl["tv"][i].visible = false
245	end
246
247	g_lounge_tbl.finger.visible = false
248
249	while should_exit == false do
250		local item = g_tv_programs[g_tv_cur_program][g_tv_cur_pos]
251		if item < 0x80 then
252			display_tv_sprite(s_idx, item)
253			s_idx = s_idx + 1
254		elseif item == 0x82 then
255			--static
256			local sprite = g_lounge_tbl.tv[s_idx]
257			if sprite ~= nil then
258				sprite.image = g_lounge_tbl["tv_static"]
259				image_static(sprite.image)
260				sprite.x = g_tv_base_x
261				sprite.y = g_tv_base_y
262				sprite.clip_x = g_tv_base_x
263				sprite.clip_y = g_tv_base_y
264				sprite.visible = true
265			end
266			g_lounge_tbl.finger.visible = true
267			should_exit = true
268		elseif item == 0x80 then --loop start
269			g_tv_cur_pos = g_tv_cur_pos + 1
270			g_tv_loop_cnt = g_tv_programs[g_tv_cur_program][g_tv_cur_pos]
271			g_tv_loop_pos = g_tv_cur_pos
272		elseif item == 0x81 then --loop end
273			if g_tv_loop_cnt > 0 then
274				g_tv_cur_pos = g_tv_loop_pos
275				g_tv_loop_cnt = g_tv_loop_cnt - 1
276			end
277			should_exit = true
278		elseif item == 0x83 then --endless road
279			g_tv_road_offset = g_tv_road_offset - 1
280			if g_tv_road_offset == 0 then
281				g_tv_road_offset = 0xe
282			end
283			g_tv_y_off[0x23] = 0x15 - g_tv_road_offset
284			display_tv_sprite(s_idx, 0x22)
285			s_idx = s_idx + 1
286			g_tv_y_off[0x23] = 0x24 - g_tv_road_offset
287			display_tv_sprite(s_idx, 0x22)
288			s_idx = s_idx + 1
289			display_tv_sprite(s_idx, 0x21)
290			s_idx = s_idx + 1
291		elseif item == 0x84 then --select random image from list.
292			local rand_len = g_tv_programs[g_tv_cur_program][g_tv_cur_pos+1]
293			item = g_tv_programs[g_tv_cur_program][g_tv_cur_pos+math.random(1,rand_len)+1]
294			display_tv_sprite(s_idx, item)
295			s_idx = s_idx + 1
296			g_tv_cur_pos = g_tv_cur_pos + 1 + rand_len
297		elseif item == 0x86 then --display news image
298			display_tv_sprite(s_idx, g_tv_news_image)
299			s_idx = s_idx + 1
300		elseif item == 0x87 then --select news image
301			g_tv_news_image = g_tv_news_image_tbl[math.random(1, 9)]
302		elseif item == 0x88 then --pledge now!
303			g_tv_pledge_counter = g_tv_pledge_counter + 1
304			if g_tv_pledge_counter % 4 == 0 then
305				display_tv_sprite(s_idx, g_tv_pledge_image)
306				s_idx = s_idx + 1
307			end
308
309			if g_tv_pledge_counter == 16 then
310				if g_tv_pledge_image == 37 then
311					g_tv_pledge_image = 38
312				else
313					g_tv_pledge_image = 37
314				end
315				g_tv_pledge_counter = 0
316			end
317		elseif item == 0x89 then
318			display_tv_sprite(s_idx, math.random(50, 52))
319			s_idx = s_idx + 1
320		elseif item == 0x8a then
321			should_exit = true
322		elseif item == 0x8b then
323			canvas_rotate_palette(0xe0, 5)
324		end
325
326		g_tv_cur_pos = g_tv_cur_pos + 1
327		if g_tv_programs[g_tv_cur_program][g_tv_cur_pos] == nil then
328			g_tv_cur_program = g_tv_cur_program + 1
329			g_tv_cur_pos = 1
330
331			if g_tv_programs[g_tv_cur_program] == nil then
332				g_tv_cur_program = 1
333			end
334		end
335	end
336end
337
338g_tv_update = true
339
340local function display_lounge()
341	display_clock()
342	if g_tv_update == true then
343		display_tv()
344		g_tv_update = false
345	else
346		g_tv_update = true
347	end
348end
349
350local function scroll_clock()
351g_clock_tbl["h1"].x = g_clock_tbl["h1"].x - 1
352g_clock_tbl["h2"].x = g_clock_tbl["h2"].x - 1
353g_clock_tbl["m1"].x = g_clock_tbl["m1"].x - 1
354g_clock_tbl["m2"].x = g_clock_tbl["m2"].x - 1
355end
356
357local function scroll_lounge()
358g_lounge_tbl.bg.x = g_lounge_tbl.bg.x - 1
359g_lounge_tbl.bg1.x = g_lounge_tbl.bg1.x - 1
360g_tv_base_x = g_tv_base_x - 1
361g_lounge_tbl.avatar.x = g_lounge_tbl.avatar.x - 2
362g_lounge_tbl.finger.x = g_lounge_tbl.finger.x - 2
363
364if g_lounge_tbl.bg1.x < 166 and g_lounge_tbl.bg1.x % 2 == 0 then
365	g_lounge_tbl.bg0.x = g_lounge_tbl.bg0.x - 1
366end
367
368scroll_clock()
369end
370
371local function hide_lounge()
372
373	g_lounge_tbl["bg0"].visible = false
374	g_lounge_tbl["bg"].visible = false
375	g_lounge_tbl["bg1"].visible = false
376	g_lounge_tbl["avatar"].visible = false
377	g_lounge_tbl["tv"].visible = false
378end
379
380local function lounge_sequence()
381
382	load_lounge()
383
384	canvas_set_palette("palettes.int", 1)
385
386	--for i=0,0xff do
387		--display_lounge()
388		--canvas_update()
389	--end
390	local scroll_img = image_load("blocks.shp", 1)
391	local scroll = sprite_new(scroll_img, 1, 0x9f, true)
392
393	local x, y = image_print(scroll_img, "Upon your world, five seasons have passed since your ", 8, 312, 8, 10, 0x3e)
394	image_print(scroll_img, "triumphant homecoming from Britannia.", 8, 312, x, y, 0x3e)
395
396	local input = input_poll()
397
398	while input == nil do
399		display_lounge()
400		canvas_update()
401		input = input_poll()
402	end
403
404	if should_exit(input) then
405		return false
406	end
407
408	scroll_img = image_load("blocks.shp", 2)
409	x, y = image_print(scroll_img, "You have traded the Avatar's life of peril and adventure ", 7, 310, 7, 8, 0x3e)
410	x, y = image_print(scroll_img, "for the lonely serenity of a world at peace. But ", 7, 310, x, y, 0x3e)
411	x, y = image_print(scroll_img, "television supermen cannot take the place of friends ", 7, 310, x, y, 0x3e)
412	image_print(scroll_img, "who died at your side!", 7, 310, x, y, 0x3e)
413
414	scroll.image = scroll_img
415	scroll.x = 1
416	scroll.y = 0x98
417
418	input = input_poll()
419
420	while input == nil do
421		display_lounge()
422		canvas_update()
423		input = input_poll()
424	end
425
426	if should_exit(input) then
427		return false
428	end
429
430	scroll_img = image_load("blocks.shp", 0)
431	image_print(scroll_img, "Outside, a chill wind rises...", 39, 200, 39, 8, 0x3e)
432
433	scroll.image = scroll_img
434	scroll.x = 0x21
435	scroll.y = 0x9d
436
437	for i=0,319 do
438		scroll_lounge()
439		display_lounge()
440		canvas_update()
441		input = input_poll()
442		if should_exit(input) then
443			return false
444		end
445	end
446
447	input = input_poll()
448	while input == nil do
449		canvas_update()
450		input = input_poll()
451	end
452
453	if should_exit(input) then
454		return false
455	end
456
457	hide_lounge()
458	scroll.visible = false
459
460	return true
461end
462
463g_window_tbl = {}
464local function load_window()
465	local rand = math.random
466	g_window_tbl["cloud_x"] = -400
467
468	g_window_tbl["sky"] = sprite_new(g_img_tbl[1], 0, 0, true)
469	g_window_tbl["cloud1"] = sprite_new(g_img_tbl[0], g_window_tbl["cloud_x"], -5, true)
470	g_window_tbl["cloud2"] = sprite_new(g_img_tbl[0], g_window_tbl["cloud_x"], -5, true)
471	g_window_tbl["clouds"] = {}
472
473	local i
474	for i=1,5 do
475		table.insert(g_window_tbl["clouds"], sprite_new(g_img_tbl[rand(2,3)], rand(0,320) - 260, rand(0, 30), true))
476	end
477
478	g_window_tbl["lightning"] = sprite_new(nil, 0, 0, false)
479	g_window_tbl["ground"] = sprite_new(g_img_tbl[10], 0, 0x4c, true)
480	g_window_tbl["trees"] = sprite_new(g_img_tbl[8], 0, 0, true)
481
482	g_window_tbl["strike"] = sprite_new(g_img_tbl[rand(19,23)], 158, 114, false)
483
484
485
486	--FIXME rain here.
487	local rain = {}
488	local i
489	for i = 0,100 do
490		rain[i] = sprite_new(g_img_tbl[math.random(4,7)], math.random(0,320), math.random(0,200), false)
491	end
492	g_window_tbl["rain"] = rain
493
494	g_window_tbl["frame"] = sprite_new(g_img_tbl[28], 0, 0, true)
495	g_window_tbl["window"] = sprite_new(g_img_tbl[26], 0x39, 0, true)
496	g_window_tbl["door_left"] = sprite_new(g_img_tbl[24], 320, 0, true)
497	g_window_tbl["door_right"] = sprite_new(g_img_tbl[25], 573, 0, true)
498
499	g_window_tbl["flash"] = 0
500	g_window_tbl["drops"] = 0
501	g_window_tbl["rain_delay"] = 20
502	g_window_tbl["lightning_counter"] = 0
503
504end
505
506local function hide_window()
507	g_window_tbl["sky"].visible = false
508	g_window_tbl["cloud1"].visible = false
509	g_window_tbl["cloud2"].visible = false
510
511	local i
512	for i=1,5 do
513		g_window_tbl["clouds"][i].visible = false
514	end
515
516	g_window_tbl["lightning"].visible = false
517	g_window_tbl["ground"].visible = false
518	g_window_tbl["trees"].visible = false
519
520	g_window_tbl["strike"].visible = false
521
522	local i
523	for i = 0,100 do
524		g_window_tbl["rain"][i].visible = false
525	end
526
527	g_window_tbl["frame"].visible = false
528	g_window_tbl["window"].visible = false
529	g_window_tbl["door_left"].visible = false
530	g_window_tbl["door_right"].visible = false
531
532end
533
534local function display_window()
535
536	local i
537	local rain = g_window_tbl["rain"]
538	local rand = math.random
539	local c_num
540
541	--FIXME display clouds here.
542	local cloud
543	for i,cloud in ipairs(g_window_tbl["clouds"]) do
544		if cloud.x > 320 then
545			cloud.x = rand(0, 320) - 320
546			cloud.y = rand(0, 30)
547		end
548
549		cloud.x = cloud.x + 2
550	end
551
552	g_window_tbl["cloud_x"] = g_window_tbl["cloud_x"] + 1
553	if g_window_tbl["cloud_x"] == 320 then
554		g_window_tbl["cloud_x"] = 0
555	end
556
557	g_window_tbl["cloud1"].x = g_window_tbl["cloud_x"]
558	g_window_tbl["cloud2"].x = g_window_tbl["cloud_x"] - 320
559
560	if rand(0, 6) == 0 and g_window_tbl["lightning_counter"] == 0 then --fixme var_1a, var_14
561		g_window_tbl["lightning_counter"] = rand(1, 4)
562		g_window_tbl["lightning"].image = g_img_tbl[rand(11,18)]
563		g_window_tbl["lightning"].visible = true
564		g_window_tbl["lightning_x"] = rand(-5,320)
565		g_window_tbl["lightning_y"] = rand(-5,200)
566	end
567
568	if g_window_tbl["lightning_counter"] > 0 then
569		g_window_tbl["lightning"].x = g_window_tbl["lightning_x"] + rand(0, 3)
570		g_window_tbl["lightning"].y = g_window_tbl["lightning_y"] + rand(0, 3)
571	end
572
573	if (g_window_tbl["lightning_counter"] > 0 and rand(0,3) == 0) or g_window_tbl["strike"].visible == true then
574		canvas_set_palette_entry(0x58, 0x40,0x94,0xfc)
575		canvas_set_palette_entry(0x5a, 0x40,0x94,0xfc)
576		canvas_set_palette_entry(0x5c, 0x40,0x94,0xfc)
577	else
578		canvas_set_palette_entry(0x58, 0x20,0xa4,0x80)
579		canvas_set_palette_entry(0x5a, 0x14,0x8c,0x74)
580		canvas_set_palette_entry(0x5c, 0x0c,0x74,0x68)
581	end
582
583	if rand(0,1) == 0 then
584		g_window_tbl["strike"].image = g_img_tbl[rand(19,23)]
585	end
586
587	if g_window_tbl["flash"] > 0 then
588		g_window_tbl["flash"] = g_window_tbl["flash"] - 1
589	else
590		if rand(0,5) == 0 or g_window_tbl["strike"].visible == true then
591			g_window_tbl["window"].image = g_img_tbl[27]
592			g_window_tbl["flash"] = rand(1, 5)
593		else
594			g_window_tbl["window"].image = g_img_tbl[26]
595		end
596	end
597
598	if g_window_tbl["drops"] < 100 then
599		if rand(0,g_window_tbl["rain_delay"]) == 0 then
600			g_window_tbl["rain_delay"] = g_window_tbl["rain_delay"] - 2
601			if g_window_tbl["rain_delay"] < 1 then
602				g_window_tbl["rain_delay"] = 1
603			end
604			i = g_window_tbl["drops"]
605			rain[i].visible = true
606			rain[i].image = g_img_tbl[rand(4,7)]
607			rain[i].y = -4
608			rain[i].x = rand(0,320)
609			g_window_tbl["drops"] = i + 1
610		end
611	end
612
613	for i = 0,g_window_tbl["drops"] do
614
615		if rain[i].visible == true then
616			rain[i].x = rain[i].x + 2
617			rain[i].y = rain[i].y + 8
618			if rain[i].x > 320 or rain[i].y > 200 then
619				rain[i].visible = false
620			end
621		else
622			rain[i].visible = true
623			rain[i].image = g_img_tbl[rand(4,7)]
624			rain[i].y = -4
625			rain[i].x = rand(0,320)
626		end
627	end
628
629	if g_window_tbl["lightning_counter"] > 0 then
630		g_window_tbl["lightning_counter"] = g_window_tbl["lightning_counter"] - 1
631	end
632end
633
634local function window_update()
635	local input = input_poll()
636
637	while input == nil do
638		display_window()
639		canvas_update()
640		input = input_poll()
641		if input ~= nil then
642			break
643		end
644		canvas_update()
645	end
646
647	return should_exit(input)
648end
649
650local function window_sequence()
651	load_images("intro_2.shp")
652
653	load_window()
654
655	canvas_set_palette("palettes.int", 2)
656
657	local i = 0
658	local input
659	while i < 20 do
660		display_window()
661		canvas_update()
662		input = input_poll()
663		if should_exit(input) then
664			return false
665		end
666
667		i = i + 1
668		canvas_update()
669	end
670
671	local scroll_img = image_load("blocks.shp", 1)
672	local scroll = sprite_new(scroll_img, 1, 0x98, true)
673
674	local x, y = image_print(scroll_img, "...and in moments, the storm is upon you.", 8, 312, 36, 14, 0x3e)
675
676	if window_update() == true then
677		return false
678	end
679
680	scroll_img = image_load("blocks.shp", 1)
681	x, y = image_print(scroll_img, "Tongues of lightning lash the sky, conducting an unceasing ", 8, 310, 8, 10, 0x3e)
682	image_print(scroll_img, "crescendo of thunder....", 8, 310, x, y, 0x3e)
683	scroll.image = scroll_img
684
685	if window_update() == true then
686		return false
687	end
688
689	scroll_img = image_load("blocks.shp", 1)
690	x, y = image_print(scroll_img, "In a cataclysm of sound and light, a bolt of searing ", 8, 310, 8, 10, 0x3e)
691	image_print(scroll_img, "blue fire strikes the earth!", 8, 310, x, y, 0x3e)
692	scroll.image = scroll_img
693
694	g_window_tbl["strike"].visible = true
695
696	if window_update() == true then
697		return false
698	end
699
700	scroll_img = image_load("blocks.shp", 1)
701	x, y = image_print(scroll_img, "Lightning among the stones!", 8, 310, 73, 10, 0x3e)
702	image_print(scroll_img, "Is this a sign from distant Britannia?", 8, 310, 41, 18, 0x3e)
703	scroll.image = scroll_img
704
705	--scroll window.
706	i = 0
707	while i < 320 do
708
709		display_window()
710		canvas_update()
711		input = input_poll()
712		if should_exit(input) then
713			return false
714		end
715
716		canvas_update()
717
718		g_window_tbl["window"].x = g_window_tbl["window"].x - 2
719		g_window_tbl["frame"].x = g_window_tbl["frame"].x - 2
720		g_window_tbl["door_left"].x = g_window_tbl["door_left"].x - 2
721		g_window_tbl["door_right"].x = g_window_tbl["door_right"].x - 2
722		i = i + 2
723		if i > 160 and g_window_tbl["strike"].visible == true then
724			g_window_tbl["strike"].visible = false
725		end
726	end
727
728	if window_update() == true then
729		return false
730	end
731
732	scroll.visible = false
733	i = 0
734	while i < 68 do
735
736		display_window()
737		canvas_update()
738		input = input_poll()
739		if should_exit(input) then
740			return false
741		end
742
743		canvas_update()
744		input = input_poll()
745
746		g_window_tbl["door_left"].x = g_window_tbl["door_left"].x - 1
747		g_window_tbl["door_right"].x = g_window_tbl["door_right"].x + 1
748		i = i + 1
749	end
750
751	scroll_img = image_load("blocks.shp", 2)
752	x, y = image_print(scroll_img, "You bolt from your house, stumbling, running blind in the", 7, 310, 8, 12, 0x3e)
753	x, y = image_print(scroll_img, " storm. Into the forest, down the path, through the ", 7, 310, x, y, 0x3e)
754	image_print(scroll_img, "rain... to the stones.", 7, 310, x, y, 0x3e)
755	scroll.image = scroll_img
756	scroll.visible = true
757
758	if window_update() == true then
759		return false
760	end
761
762	scroll.visible = false
763
764end
765
766local function stones_rotate_palette()
767	if g_pal_counter == 4 then
768		canvas_rotate_palette(144, 16)
769		g_pal_counter = 0
770	else
771		g_pal_counter = g_pal_counter + 1
772	end
773end
774
775local function stones_update()
776	local input = input_poll()
777
778	while input == nil do
779		stones_rotate_palette()
780		canvas_update()
781		input = input_poll()
782		if input ~= nil then
783			break
784		end
785	end
786
787	return should_exit(input)
788end
789
790local function stones_shake_moongate()
791	local input = input_poll()
792
793	while input == nil do
794		stones_rotate_palette()
795		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
796		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
797		canvas_update()
798		stones_rotate_palette()
799		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
800		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
801		canvas_update()
802		input = input_poll()
803		if input ~= nil then
804			break
805		end
806	end
807
808	return should_exit(input)
809end
810
811g_pal_counter = 0
812
813g_stones_tbl = {}
814
815local function stones_sequence()
816
817	load_images("intro_3.shp")
818
819	canvas_set_palette("palettes.int", 3)
820
821	g_stones_tbl["bg"] = sprite_new(g_img_tbl[0], 0, 0, true)
822	g_stones_tbl["stone_cover"] = sprite_new(g_img_tbl[3], 0x96, 0x64, false)
823	g_stones_tbl["gate_cover"] = sprite_new(g_img_tbl[4], 0x5e, 0x66, false)
824	g_stones_tbl["hand"] = sprite_new(g_img_tbl[1], 0xbd, 0xc7, false)
825	g_stones_tbl["moon_gate"] = sprite_new(g_img_tbl[2], 0x7c, 0x64, false)
826	g_stones_tbl["moon_gate"].clip_x = 0
827	g_stones_tbl["moon_gate"].clip_y = 0
828	g_stones_tbl["moon_gate"].clip_w = 320
829	g_stones_tbl["moon_gate"].clip_h = 0x66
830	g_stones_tbl["avatar"] = sprite_new(g_img_tbl[7], -2, 0x12, false)
831
832	local scroll_img = image_load("blocks.shp", 2)
833	local scroll = sprite_new(scroll_img, 1, 0xc, true)
834
835	local x, y = image_print(scroll_img, "Near the stones, the smell of damp, blasted earth hangs ", 7, 303, 7, 8, 0x3e)
836	x, y = image_print(scroll_img, "in the air. In a frozen moment of lightning-struck ", 7, 303, x, y, 0x3e)
837	x, y = image_print(scroll_img, "daylight, you glimpse a tiny obsidian stone in the ", 7, 303, x, y, 0x3e)
838	x, y = image_print(scroll_img, "midst of the circle!", 7, 303, x, y, 0x3e)
839
840	fade_in()
841
842	if stones_update() == true then
843		return false
844	end
845
846	g_stones_tbl["stone_cover"].visible = true
847	g_stones_tbl["hand"].visible = true
848
849	scroll_img = image_load("blocks.shp", 0)
850	image_print(scroll_img, "Wondering, you pick it up....", 8, 234, 0x2a, 8, 0x3e)
851	scroll.image = scroll_img
852	scroll.x = 0x21
853	scroll.y = 0x1e
854
855	local i
856	for i=0xc7,0x54,-2  do
857		--		display_stones()
858		g_stones_tbl["hand"].y = i
859		canvas_update()
860		local input = input_poll()
861		if input ~= nil and should_exit(input) then
862			return false
863		end
864	end
865
866	if stones_update() == true then
867		return false
868	end
869
870	g_stones_tbl["bg"].image = g_img_tbl[5]
871	g_stones_tbl["stone_cover"].visible = false
872	g_stones_tbl["gate_cover"].visible = true
873
874	scroll_img = image_load("blocks.shp", 1)
875	x, y = image_print(scroll_img, "...and from the heart of the stones, a softly glowing door ", 7, 303, 7, 10, 0x3e)
876	image_print(scroll_img, "ascends in silence!", 7, 303, x, y, 0x3e)
877	scroll.image = scroll_img
878	scroll.x = 0x1
879	scroll.y = 0xa0
880
881	g_stones_tbl["moon_gate"].visible = true
882
883	for i=0x64,0x5,-1  do
884		--		display_stones()
885		stones_rotate_palette()
886		g_stones_tbl["moon_gate"].y = i
887		canvas_update()
888		local input = input_poll()
889		if input ~= nil and should_exit(input) then
890			return false
891		end
892	end
893
894	for i=0x54,0xc7,2  do
895		--		display_stones()
896		stones_rotate_palette()
897		g_stones_tbl["hand"].y = i
898		canvas_update()
899		local input = input_poll()
900		if input ~= nil and should_exit(input) then
901			return false
902		end
903	end
904
905	g_stones_tbl["hand"].visible = false
906
907	if stones_update() == true then
908		return false
909	end
910
911	g_stones_tbl["hand"].image = g_img_tbl[6]
912	g_stones_tbl["hand"].x = 0x9b
913	g_stones_tbl["hand"].visible = true
914	scroll.visible = false
915
916	for i=0xc7,0x44,-2  do
917		stones_rotate_palette()
918		g_stones_tbl["hand"].y = i
919		g_stones_tbl["bg"].y = g_stones_tbl["bg"].y - 1
920		g_stones_tbl["gate_cover"].y = g_stones_tbl["gate_cover"].y - 1
921		g_stones_tbl["moon_gate"].y = g_stones_tbl["moon_gate"].y - 1
922		canvas_update()
923		local input = input_poll()
924		if input ~= nil and should_exit(input) then
925			return false
926		end
927	end
928
929	scroll_img = image_load("blocks.shp", 2)
930	x, y = image_print(scroll_img, "Exultant memories wash over you as you clutch the stone. ", 7, 303, 7, 8, 0x3e)
931	x, y = image_print(scroll_img, "When last you saw an orb such as this, it was cast down ", 7, 303, x, y, 0x3e)
932	image_print(scroll_img, "by Lord British to banish the tyrant Blackthorn!", 7, 303, x, y, 0x3e)
933	scroll.image = scroll_img
934	scroll.x = 0x1
935	scroll.y = 0x98
936	scroll.visible = true
937
938	if stones_update() == true then
939		return false
940	end
941
942	scroll.visible = false
943
944	for i=0x44,0xc7,2  do
945		stones_rotate_palette()
946		g_stones_tbl["hand"].y = i
947		g_stones_tbl["bg"].y = g_stones_tbl["bg"].y + 1
948		g_stones_tbl["gate_cover"].y = g_stones_tbl["gate_cover"].y + 1
949		g_stones_tbl["moon_gate"].y = g_stones_tbl["moon_gate"].y + 1
950		canvas_update()
951		local input = input_poll()
952		if input ~= nil and should_exit(input) then
953			return false
954		end
955	end
956
957	g_stones_tbl["hand"].visible = false
958
959	scroll_img = image_load("blocks.shp", 2)
960	image_print(scroll_img, "But your joy soon gives way to apprehension.", 7, 303, 16, 8, 0x3e)
961	image_print(scroll_img, "The gate to Britannia has always been blue...", 7, 303, 18, 24, 0x3e)
962	image_print(scroll_img, "as blue as the morning sky.", 7, 303, 76, 32, 0x3e)
963	scroll.image = scroll_img
964	scroll.visible = true
965
966	if stones_update() == true then
967		return false
968	end
969
970	scroll_img = image_load("blocks.shp", 1)
971	x,y = image_print(scroll_img, "Abruptly, the portal quivers and begins to sink ", 7, 303, 7, 10, 0x3e)
972	image_print(scroll_img, "into the ground.  Its crimson light wanes!", 7, 303, x, y, 0x3e)
973	scroll.image = scroll_img
974	scroll.x = 0x1
975	scroll.y = 0xa0
976
977	if stones_shake_moongate() == true then
978		return false
979	end
980
981	scroll_img = image_load("blocks.shp", 1)
982	x,y = image_print(scroll_img, "Desperation makes the decision an easy one.", 7, 303, 22, 14, 0x3e)
983	scroll.image = scroll_img
984	scroll.x = 0x1
985	scroll.y = 0xa0
986
987	if stones_shake_moongate() == true then
988		return false
989	end
990
991	scroll.visible = false
992
993	g_stones_tbl["avatar"].visible = true
994
995	canvas_set_palette_entry(0x19, 0, 0, 0)
996
997	for i=0,19,1 do
998		g_stones_tbl["avatar"].image = g_img_tbl[7+i]
999
1000		local j
1001		for j=0,4 do
1002			canvas_update()
1003			stones_rotate_palette()
1004			g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
1005			g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
1006		end
1007
1008		g_stones_tbl["avatar"].y = g_stones_tbl["avatar"].y - 3
1009
1010		local input = input_poll()
1011		if input ~= nil and should_exit(input) then
1012			return false
1013		end
1014	end
1015
1016
1017	for i=0xff,0,-3 do
1018		canvas_update()
1019		stones_rotate_palette()
1020		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
1021		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
1022
1023		g_stones_tbl["avatar"].opacity = i
1024
1025		local input = input_poll()
1026		if input ~= nil and should_exit(input) then
1027			return false
1028		end
1029	end
1030
1031	canvas_set_palette_entry(0x19, 0x74, 0x74, 0x74)
1032
1033	g_stones_tbl["moon_gate"].x = 0x7c
1034
1035	for i=0x5,0x64,1  do
1036		stones_rotate_palette()
1037		g_stones_tbl["moon_gate"].y = i
1038		canvas_update()
1039		local input = input_poll()
1040		if input ~= nil and should_exit(input) then
1041			return false
1042		end
1043	end
1044
1045	g_stones_tbl["bg"].image = g_img_tbl[0]
1046	g_stones_tbl["moon_gate"].visible = false
1047	g_stones_tbl["gate_cover"].visible = false
1048	g_stones_tbl["stone_cover"].visible = true
1049
1050	if stones_update() == true then
1051		return false
1052	end
1053
1054	return true
1055end
1056
1057local function play()
1058mouse_cursor_set_pointer(0)
1059mouse_cursor_visible(false)
1060load_images("intro_1.shp")
1061music_play("bootup.m")
1062--[ [
1063canvas_set_palette("palettes.int", 0)
1064canvas_set_update_interval(25)
1065
1066local img = g_img_tbl[0x45]
1067
1068local background = sprite_new(img)
1069
1070background.x = 0
1071background.y = 0
1072background.opacity = 0
1073background.visible = true
1074
1075music_play("bootup.m")
1076for i=0,0xff,3 do
1077	background.opacity = i
1078	if poll_for_esc() == true then return end
1079	canvas_update()
1080end
1081
1082for i=0xff,0,-3 do
1083background.opacity = i
1084	if poll_for_esc() == true then return end
1085
1086--if input ~= nil and input == ESCAPE_KEY then
1087--	return
1088--end
1089canvas_update()
1090end
1091
1092img = g_img_tbl[0x46]
1093
1094background.image = img
1095
1096for i=0,0xff,3 do
1097	background.opacity = i
1098		if poll_for_esc() == true then return end
1099
1100	canvas_update()
1101end
1102
1103for i=0xff,0,-3 do
1104background.opacity = i
1105	if poll_for_esc() == true then return end
1106
1107canvas_update()
1108end
1109
1110background.visible = false
1111
1112
1113
1114if lounge_sequence() == false then
1115	return
1116end
1117
1118if window_sequence() == false then
1119	return
1120end
1121
1122fade_out()
1123hide_window()
1124--] ]
1125stones_sequence()
1126end
1127
1128local function gypsy_update_bubbles(bubble_image)
1129	if g_gypsy_tbl["bubble_counter"] == 0 then
1130		image_bubble_effect(bubble_image)
1131		g_gypsy_tbl["bubble_counter"] = 3
1132	else
1133		g_gypsy_tbl["bubble_counter"] = g_gypsy_tbl["bubble_counter"] - 1
1134	end
1135end
1136
1137
1138local function gypsy_ab_select(question)
1139	local a_lookup_tbl = {
1140	0, 0, 0, 0, 0, 0, 0, 1,
1141	1, 1, 1, 1, 1, 2, 2, 2,
1142	2, 2, 3, 3, 3, 3, 4, 4,
1143	4, 5, 5, 6,
1144	}
1145
1146	local b_lookup_tbl = {
1147	1, 2, 3, 4, 5, 6, 7, 2,
1148	3, 4, 5, 6, 7, 3, 4, 5,
1149	6, 7, 4, 5, 6, 7, 5, 6,
1150	7, 6, 7, 7,
1151	}
1152	if g_highlight_index == nil then
1153		g_highlight_index = 0
1154	end
1155
1156	local input = nil
1157	while input == nil do
1158		canvas_update()
1159		input = input_poll()
1160		if input ~= nil then
1161			if input == 65 or input == 97 or (input == 13 and g_highlight_index == 0) then
1162				return a_lookup_tbl[question - 104] + 1
1163			elseif input == 66 or input == 98 or input == 13 then
1164				return b_lookup_tbl[question - 104] + 1
1165			elseif input == SDLK_RIGHT and g_highlight_index == 0 then -- right
1166				g_highlight_index = 1
1167				g_ab_highlight.x = g_ab_highlight.x + 17
1168			elseif input == SDLK_LEFT and g_highlight_index == 1 then -- left
1169				g_highlight_index = 0
1170				g_ab_highlight.x = g_ab_highlight.x - 17
1171			elseif input == 0 then
1172				local y = get_mouse_y()
1173				if(y > 173 and y < 190) then
1174					local x = get_mouse_x()
1175					if x > 278 and x < 296 then
1176						return a_lookup_tbl[question - 104] + 1
1177					elseif x > 296 and x < 313 then
1178						return b_lookup_tbl[question - 104] + 1
1179					end
1180				end
1181			end
1182			input = nil
1183		end
1184		if g_gypsy_tbl["jar_liquid"].visible == true then
1185			gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
1186		end
1187	end
1188end
1189
1190
1191local gypsy_question_text = {
1192"\"Entrusted to deliver an uncounted purse of gold, thou dost meet a poor beggar. Dost thou A) deliver the gold Honestly, knowing the trust in thee was well-placed; or B) show Compassion and give the beggar a coin, knowing it won't be missed?\127",
1193"\"Thou has been prohibited by thy absent Lord from joining thy friends in a close-pitched battle. Dost thou A) refrain, so thou may Honestly claim obedience; or B) show Valor and aid thy comrades, knowing thou may deny it later?\127",
1194"\"A merchant owes thy friend money, now long past due. Thou dost see the same merchant drop a purse of gold. Dost thou A) Honestly return the purse intact; or B) Justly give thy friend a portion of the gold first?\127",
1195"\"Thee and thy friend are valiant but penniless warriors. Thou both set forth to slay a mighty dragon. Thy friend dost think he slew it, but the killing blow was thine. Dost thou A) Honestly claim the reward; or B) Sacrifice the gold for the sake of thy friendship?\127",
1196"\"Thou art sworn to protect thy Lord at any cost, yet thou knowest he hath committed a crime. Authorities ask thee of the affair. Dost thou A) Break thine oath by Honestly speaking; or B) uphold Honor by silently keeping thine oath?\127",
1197"\"Thy friend doth seek admittance to thy spiritual order. Thou art asked to vouch for his purity of spirit, of which thou art uncertain. Dost thou A) Honestly express thy doubt; or B) Vouch for him, hoping for his Spiritual improvement?\127",
1198"\"Thy Lord doth mistakenly believe he slew a dragon. Thou hast proof that thy lance felled the beast. When asked, dost thou A) Honestly claim the kill; or B) Humbly permit thy Lord his belief?\127",
1199"\"Thou dost manage to disarm thy mortal enemy in a duel. He is at thy mercy. Dost thou A) Show Compassion by permitting him to yield; or B) Slay him, as expected of a Valiant duelist?\127",
1200"\"After twenty years thou hast found the slayer of thy best friends. The villain proves to be a man who provides the sole support for a young girl. Dost thou A) Spare him in Compassion for the child; or B) Slay him in the name of Justice?\127",
1201"\"Thee and thy friends hath been routed and ordered to retreat. In defiance of thy orders, dost thou A) Stop in Compassion to aid a wounded comrade; or B) Sacrifice thyself to slow the pursuing enemy so that others might escape?\127",
1202"\"Thou art sworn to uphold a Lord who doth participate in the forbidden torture of prisoners. Each night their cries of pain reach thee. Dost thou A) Show Compassion by reporting the deeds; or B) Honor thy oath and ignore the deeds?\127",
1203"\"Thou hast been taught to preserve all life as sacred. A man lies fatally stung by a venomous serpent. He prays for a merciful death. Dost thou A) Show Compassion and end his pain; or B) heed thy Spiritual beliefs and turn away?\127",
1204"\"The Captain of the King's Guard asks that one among thee visit a hospital to cheer the children with tales of valiant personal deeds. Dost thou A) Show thy Compassion and play the braggart; or B) Humbly let another go?\127",
1205"\"Thou hast been sent to secure a needed treaty with a distant Lord. Thy host is agreeable to thy proposal but insults thy country at dinner. Dost thou A) Valiantly bear the slurs; or B) Justly rise and demand an apology?\127",
1206"\"A burly knight accosts thee and demands thy food. Dost thou A) Valiantly refuse and engage the knight, or B) Sacrifice thy rations unto the hungry knight?\127",
1207"\"During battle thou art ordered to guard thy commander's empty tent. The battle goes poorly and thou dost yearn to aid thy fellows. Dost thou A) Valiantly enter the battle; or B) Honor thy post as guard?\127",
1208"\"A local bully pushes for a fight. Dost thou A) Valiantly trounce the rogue; or B) Decline, knowing in thy Spirit that no lasting good will come of it?\127",
1209"\"Although a simple fisherman, thou art also a skillful swordsman. Thy Lord dost seek to assemble a peacetime guard. Dost thou A) Answer the call, so that all may witness thy Valor; or B) Humbly decline the offer to join thy Lord's largely ceremonial knighthood?\127",
1210"\"During a pitched battle, thou dost see a fellow desert his post, endangering many. As he flees, he is set upon by several enemies. Dost thou A) Justly let him fight alone; or B) Risk the Sacrifice of thine own life to aid him?\127",
1211"\"Thou hast sworn to do thy Lord's bidding in all. He covets a piece of land and orders its owner removed. Dost thou A) Serve Justice, refusing to act, thus being disgraced; or B) Honor thine oath and evict the landowner?\127",
1212"\"Thou dost believe that virtue resides in all people. Thou dost see a rogue steal from thy Lord. Dost thou A) Call him to Justice; or B) Personally try to sway him back to the Spiritual path of good?\127",
1213"\"Unwitnessed, thou hast slain a mighty dragon in defense of thy life. A poor warrior claims the offered reward. Dost thou A) Justly step forward to claim the bounty; or B) Humbly go about thy life, secure in thy self-esteem?\127",
1214"\"Thou art a bounty hunter sworn to return an alleged murderer. After his capture, thou dost come to believe him innocent. Dost thou A) Sacrifice thy sizable bounty for thy belief; or B) Honor thine oath to return him as promised?\127",
1215"\"Thou hast spent thy life in charitable and righteous work. Thine uncle the innkeeper lies ill and asks thee to take over his tavern. Dost thou A) Sacrifice thy life of purity to aid thy kin; or B) Decline and follow the call of Spirituality?\127",
1216"\"Thou art an elderly, wealthy merchant. Thy end is near. Dost thou A) Sacrifice all thy wealth to feed hundreds of starving children, receiving public adulation; or B) Humbly live out thy life, willing thy fortune to thy heirs?\127",
1217"\"In thy youth thou didst pledge to marry thy sweetheart. Now thou art on a sacred quest in distant lands. Thy sweetheart doth ask thee to keep thy vow. Dost thou A) Honor thy pledge to wed; or B) Follow thy Spiritual crusade?\127",
1218"\"Though thou art but a peasant shepherd, thou art discovered to be the sole descendant of a noble family long thought extinct. Dost thou A) Honorably take up the arms of thy ancestors; or B) Humbly resume thy life of simplicity and peace?\127",
1219"\"Thy parents wish thee to become an apprentice. Two positions are available. Dost thou A) Become an acolyte in a worthy Spiritual order; or B) Become an assistant to a humble village cobbler?\127",
1220}
1221
1222	local gypsy_questions = {
1223	-1, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
1224	0x69, -1, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
1225	0x6A, 0x70, -1, 0x76, 0x77, 0x78, 0x79, 0x7A,
1226	0x6B, 0x71, 0x76, -1, 0x7B, 0x7C, 0x7D, 0x7E,
1227	0x6C, 0x72, 0x77, 0x7B, -1, 0x7F, 0x80, 0x81,
1228	0x6D, 0x73, 0x78, 0x7C, 0x7F, -1, 0x82, 0x83,
1229	0x6E, 0x74, 0x79, 0x7D, 0x80, 0x82, -1, 0x84,
1230	0x6F, 0x75, 0x7A, 0x7E, 0x81, 0x83, 0x84, -1,
1231	}
1232
1233	g_str = 0xf
1234	g_dex = 0xf
1235	g_int = 0xf
1236
1237	g_question_tbl = {0, 1, 2, 3, 4, 5, 6, 7}
1238
1239local function shuffle_question_tbl(shuffle_len)
1240	local random = math.random
1241	local c = random(0, (shuffle_len * shuffle_len)-1) + shuffle_len
1242
1243	for i=0,c,1 do
1244		local j = random(1, shuffle_len)
1245		local k = random(1, shuffle_len)
1246
1247		local tmp = g_question_tbl[j]
1248
1249		g_question_tbl[j] = g_question_tbl[k]
1250		g_question_tbl[k] = tmp
1251	end
1252
1253end
1254
1255local function gypsy_start_pouring(vial_num, vial_level)
1256	local pour_img_tbl =
1257	{
1258	 0x2B, 0x43, 0x56, 0x6B, 0x24, 0x74, 0x19, 0x2C,
1259	 0x2C, 0x44, 0x57, 0x6C, 0x23, 0x73, 0x18, 0x2B,
1260	 0x2E, 0x45, 0x58, 0x6D, 0x22, 0x72, 0x17, 0x2A,
1261	}
1262
1263	if vial_level <= 3 and vial_level > 0 then
1264		g_gypsy_tbl["pour"].visible = true
1265		local img1 = pour_img_tbl[(3 - vial_level) * 8 + vial_num]
1266		--io.stderr:write("pour: "..vial_level.." img1="..img1.."vial_num="..vial_num.."\n")
1267		if vial_num > 6 then
1268			img1 = img1 + 9
1269		else
1270			img1 = img1 + 0x42
1271		end
1272
1273		g_gypsy_tbl["pour"].image = g_gypsy_img_tbl[img1]
1274
1275		local pour_y_tbl = {0x32, 0x37, 0x40}
1276		g_gypsy_tbl["pour"].y = pour_y_tbl[3 - vial_level + 1] - 20
1277
1278		local pour_x_tbl =
1279		{
1280		 0x92, 0x92, 0x92, 0x92, 0x0A9, 0x0A9, 0x0A9, 0x0A9,
1281		 0x91, 0x91, 0x91, 0x91, 0x0A9, 0x0A9, 0x0A9, 0x0A9,
1282		 0x94, 0x94, 0x94, 0x94, 0x0AA, 0x0AA, 0x0AA, 0x0AA,
1283		}
1284
1285		g_gypsy_tbl["pour"].x = pour_x_tbl[(3 - vial_level) * 8 + vial_num]
1286	end
1287
1288	g_gypsy_tbl["jar_level"] = g_gypsy_tbl["jar_level"] + 1
1289
1290	g_gypsy_tbl["jar_liquid"].visible = true
1291	g_gypsy_tbl["jar_liquid"].image = g_gypsy_img_tbl[8 + g_gypsy_tbl["jar_level"]]
1292	g_gypsy_tbl["jar_liquid"].y = g_gypsy_tbl["jar_liquid"].y - 1
1293
1294	local vial_colors = {239, 14, 231, 103, 228, 5, 15, 219}
1295
1296	image_bubble_effect_add_color(vial_colors[vial_num])
1297	g_gypsy_tbl["bubble_counter"] = 0
1298
1299end
1300
1301local function gypsy_stop_pouring()
1302	g_gypsy_tbl["pour"].visible = false
1303end
1304
1305local function gypsy_vial_anim_liquid(img_num, vial_num, vial_level, hand_x, hand_y)
1306
1307	--io.stderr:write(img_num..", "..vial_num..", "..vial_level..", "..hand_x..", "..hand_y.."\n")
1308
1309	if vial_level == 3 then
1310		vial_level = 2
1311	end
1312
1313	if vial_level <= 0 then
1314		g_gypsy_tbl["vial_liquid"][vial_num].visible = false
1315		return
1316	end
1317
1318	local si = 0
1319	if img_num == 0xf then return end
1320
1321
1322	if img_num > 0xf then
1323		if img_num == 0x12 then
1324			si = 1
1325		else
1326			if img_num > 0x12 then
1327				if img_num == 0x25 then return end
1328				if img_num == 0x2e then
1329					si = 2
1330				end
1331			else
1332				if img_num == 0x10 then
1333					si = 2
1334				else
1335					if img_num == 0x11 then
1336						si = 0
1337					end
1338				end
1339			end
1340		end
1341
1342	else
1343
1344		img_num = img_num - 9
1345		if img_num == 0 then
1346			si = 0
1347		elseif img_num == 1 then
1348			si = 1
1349		elseif img_num >= 2 and img_num <= 5 then
1350			si = 3
1351		end
1352
1353	end
1354
1355	local vial_liquid_tbl = {
1356	0x3A, 0x4F, 0x64, 0x7C, 0x28, 0x7E, 0x20, 0x34,
1357	0x36, 0x4B, 0x60, 0x76, 0x26, 0x78, 0x1C, 0x30,
1358	0x36, 0x4B, 0x60, 0x76, 0x26, 0x78, 0x1C, 0x30,
1359	0x3C, 0x51, 0x66, 0x7F, 0x14, 0x5F, 0x22, 0x36,
1360	0x38, 0x4D, 0x62, 0x79, 0x27, 0x7B, 0x1E, 0x32,
1361	0x38, 0x4D, 0x62, 0x79, 0x27, 0x7B, 0x1E, 0x32,
1362	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
1363	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
1364	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
1365	0x0, 0x3, 0x6, 0x9, 0x16, 0x19, 0x1C, 0x1F,
1366	0x1, 0x4, 0x7, 0x0A, 0x17, 0x1A, 0x1D, 0x20,
1367	0x2, 0x5, 0x8, 0x0B, 0x18, 0x1B, 0x1E, 0x21
1368	}
1369
1370	local img_offset
1371
1372	if vial_num > 6 and si ~= 3 then
1373		img_offset = 9
1374	else
1375		img_offset = 0x42
1376	end
1377
1378	--io.stderr:write("si ="..si.."\n")
1379
1380	if vial_level > 0 and vial_level < 3 then
1381		--vial_liquid_tbl[vial_level * 2 + si * 24 + vial_num] + img_offset
1382		local img_idx = vial_liquid_tbl[(vial_level-1) * 8 + si * 24 + vial_num]
1383		g_gypsy_tbl["vial_liquid"][vial_num].image = g_gypsy_img_tbl[img_idx + img_offset]
1384
1385		local hand_y_tbl = {0x1B, 0x13, 0x13, 0x16, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0B, 0x0B, 0x0B}
1386		g_gypsy_tbl["vial_liquid"][vial_num].y = hand_y + hand_y_tbl[si * 3 + vial_level] - 20
1387
1388		local hand_x_tbl =
1389		{
1390		0x4, 0x4, 0x4, 0x4, 0x14, 0x14, 0x14, 0x14,
1391		0x4, 0x4, 0x4, 0x4, 0x13, 0x13, 0x13, 0x13,
1392		0x4, 0x4, 0x4, 0x4, 0x13, 0x13, 0x13, 0x13,
1393		0x4, 0x4, 0x4, 0x4, 0x17, 0x17, 0x17, 0x17,
1394		0x3, 0x3, 0x3, 0x3, 0x17, 0x17, 0x17, 0x17,
1395		0x3, 0x3, 0x3, 0x3, 0x17, 0x17, 0x17, 0x17,
1396		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
1397		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
1398		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
1399		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
1400		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
1401		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
1402		}
1403
1404		local hand_x_tbl1 = {10, 10, 10, 10, -13, -13, -13, -13}
1405
1406		g_gypsy_tbl["vial_liquid"][vial_num].x = hand_x + hand_x_tbl[si * 24 + (vial_level-1) * 8 + vial_num] + hand_x_tbl1[vial_num]
1407	end
1408
1409end
1410
1411local function gypsy_vial_anim(vial)
1412
1413	local vial_level = g_gypsy_tbl["vial_level"]
1414	local vial_img_off = {2, 5, 8, 0xB, 0x18, 0x1B, 0x1E, 0x21}
1415
1416	local vial_x_offset = {41, 62, 83, 104, 200, 221, 242, 263}
1417
1418	local idx
1419
1420	--g_gypsy_tbl["vial"][vial].visible = false
1421	--g_gypsy_tbl["vial_liquid"][vial].visible = false
1422
1423
1424	local arm_img_tbl = {0, 0, 0, 0, 5, 5, 5, 5}
1425	local arm_img_tbl1 = {1, 1, 1, 1, 4, 4, 4, 4}
1426	local arm_img_tbl2 = {2, 2, 2, 2, 3, 3, 3, 3}
1427
1428	local hand_img_tbl = {12, 12, 12, 12, 13, 13, 13, 13}
1429	local hand_img_tbl1 = {9, 9, 9, 9, 17, 17, 17, 17}
1430	local hand_img_tbl2 = {10, 10, 10, 10, 18, 18, 18, 18}
1431	local hand_img_tbl3 = {16, 16, 16, 16, 46, 46, 46, 46}
1432	local hand_img_tbl4 = {15, 15, 15, 15, 37, 37, 37, 37}
1433
1434	local arm_x_offset = {93, 93, 93, 93, 172, 172, 172, 172}
1435
1436	local hand_x_offset = {29, 50, 71, 92, 202, 223, 244, 264}
1437	local hand_x_offset1 = {107, 107, 107, 107, 170, 170, 170, 170}
1438	local hand_x_offset2 = {109, 109, 109, 109, 168, 168, 168, 168}
1439	local hand_x_offset3 = {112, 112, 112, 112, 165, 165, 165, 165}
1440	local hand_x_offset4 = {10, 10, 10, 10, -13, -13, -13, -13}
1441
1442	local hand_img_num
1443	local should_update
1444	for idx=0,16 do
1445		should_update = true
1446		if idx == 0 then
1447			--
1448			hand_img_num = 0xf
1449			should_update = false
1450		elseif idx == 1 or idx == 15 then
1451			--
1452			g_gypsy_tbl["arm"].visible = true
1453			g_gypsy_tbl["hand"].visible = true
1454			g_gypsy_tbl["vial"][vial].visible = false
1455			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl[vial]]
1456			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
1457			g_gypsy_tbl["arm"].y = 21
1458
1459			hand_img_num = hand_img_tbl[vial]
1460			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1461			g_gypsy_tbl["hand"].x = vial_x_offset[vial]
1462			g_gypsy_tbl["hand"].y = 66
1463			g_gypsy_tbl["hand"].visible = true
1464		elseif idx == 2 or idx == 14 then
1465			--
1466			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl1[vial]]
1467			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
1468			g_gypsy_tbl["arm"].y = 21
1469
1470			hand_img_num = hand_img_tbl[vial]
1471			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1472			g_gypsy_tbl["hand"].x = vial_x_offset[vial]
1473			g_gypsy_tbl["hand"].y = 29
1474
1475		elseif idx == 3 or idx == 13 then
1476			--
1477			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl1[vial]]
1478			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
1479			g_gypsy_tbl["arm"].y = 21
1480
1481			hand_img_num = hand_img_tbl1[vial]
1482			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1483			g_gypsy_tbl["hand"].x = hand_x_offset[vial]
1484			g_gypsy_tbl["hand"].y = 25
1485
1486		elseif idx == 4 or idx == 12 then
1487			--
1488			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
1489			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
1490			g_gypsy_tbl["arm"].y = 21
1491
1492			hand_img_num = hand_img_tbl1[vial]
1493			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1494			g_gypsy_tbl["hand"].x = hand_x_offset1[vial]
1495			g_gypsy_tbl["hand"].y = 20
1496
1497		elseif idx == 5 or idx == 11 then
1498			--
1499			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
1500			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
1501			g_gypsy_tbl["arm"].y = 21
1502
1503			hand_img_num = hand_img_tbl2[vial]
1504			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1505			g_gypsy_tbl["hand"].x = hand_x_offset1[vial]
1506			g_gypsy_tbl["hand"].y = 20
1507
1508			if idx == 11 then
1509				vial_level[vial] = vial_level[vial] - 1
1510				if vial_level[vial] == 2 then
1511					gypsy_stop_pouring()
1512				end
1513			end
1514
1515			if vial_level[vial] == 3 then
1516				gypsy_start_pouring(vial, vial_level[vial])
1517			end
1518		elseif idx == 6 or idx == 10 then
1519			--
1520			if vial_level[vial] > 2 then
1521				should_update = false
1522			end
1523			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
1524			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
1525			g_gypsy_tbl["arm"].y = 21
1526
1527			hand_img_num = hand_img_tbl3[vial]
1528			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1529			g_gypsy_tbl["hand"].x = hand_x_offset2[vial]
1530			g_gypsy_tbl["hand"].y = 21
1531
1532			if vial_level[vial] == 2 then
1533				if idx == 6 then
1534					gypsy_start_pouring(vial, vial_level[vial])
1535				elseif idx == 10 then
1536					gypsy_stop_pouring()
1537				end
1538			end
1539		elseif idx == 7 or idx == 8 or idx == 9 then
1540			--
1541			if vial_level[vial] > 1 then
1542				should_update = false
1543			end
1544			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
1545			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
1546			g_gypsy_tbl["arm"].y = 21
1547
1548			hand_img_num = hand_img_tbl4[vial]
1549			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
1550			g_gypsy_tbl["hand"].x = hand_x_offset3[vial]
1551			g_gypsy_tbl["hand"].y = 13
1552
1553			if vial_level[vial] == 1 then
1554				if idx == 7 then
1555					gypsy_start_pouring(vial, vial_level[vial])
1556					g_gypsy_tbl["vial_liquid"][vial].visible = false
1557				elseif idx == 9 then
1558					gypsy_stop_pouring()
1559				end
1560			end
1561		end
1562
1563		--if idx > 3 and idx < 9 or idx > 8 and idx < 13 then
1564		--	g_gypsy_tbl["hand"].x = g_gypsy_tbl["hand"].x - hand_x_offset4[vial]
1565		--end
1566
1567		--io.stderr:write("idx ="..idx.."\n")
1568		if should_update == true then
1569			gypsy_vial_anim_liquid(hand_img_num, vial, vial_level[vial], g_gypsy_tbl["hand"].x - hand_x_offset4[vial], g_gypsy_tbl["hand"].y + 20)
1570
1571			--g_gypsy_tbl["hand"].x = g_gypsy_tbl["hand"].x + hand_x_offset4[vial]
1572
1573			local j
1574			for j = 1,8 do
1575				if g_gypsy_tbl["jar_level"] > 0 then
1576					gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
1577				end
1578				canvas_update()
1579				input_poll()
1580			end
1581		end
1582	--[[
1583		local j
1584		for j = 1,8 do
1585			local img = g_gypsy_tbl["jar_liquid"].image
1586			if img ~= nil then
1587				gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
1588			end
1589			canvas_update()
1590			input_poll()
1591		end
1592	--]]
1593	end
1594
1595	g_gypsy_tbl["hand"].visible = false
1596
1597	--io.stderr:write("vial #"..vial.." level="..vial_level[vial].."\n")
1598
1599
1600
1601	g_gypsy_tbl["hand"].visible = false
1602	g_gypsy_tbl["arm"].visible = false
1603
1604	g_gypsy_tbl["vial"][vial].visible = true
1605	--g_gypsy_tbl["vial_liquid"][vial].visible = true
1606end
1607
1608local function gypsy_ask_questions(num_questions, scroll)
1609
1610	local strength_adjustment_tbl = { 0, 0, 2, 0, 1, 1, 1, 0 }
1611	local dex_adjustment_tbl = { 0, 2, 0, 1, 1, 0, 1, 0 }
1612	local int_adjustment_tbl = { 2, 0, 0, 1, 0, 1, 1, 0 }
1613
1614	for i=0,num_questions-1,1 do
1615		local q = gypsy_questions[g_question_tbl[i*2+1]*8 + g_question_tbl[i*2+2] + 1]
1616
1617		local scroll_img = image_load("blocks.shp", 3)
1618		scroll.image = scroll_img
1619		image_print(scroll_img, gypsy_question_text[q - 104], 7, 303, 8, 9, 0x3e)
1620
1621		local vial = gypsy_ab_select(q)
1622
1623		gypsy_vial_anim(vial)
1624
1625		g_str = g_str + strength_adjustment_tbl[vial]
1626		g_dex = g_dex + dex_adjustment_tbl[vial]
1627		g_int = g_int + int_adjustment_tbl[vial]
1628
1629		g_question_tbl[i+1] = vial-1
1630
1631		--io.stderr:write(q.." "..vial.."("..g_str..","..g_dex..","..g_int..")\n")
1632	end
1633end
1634
1635
1636
1637g_keycode_tbl =
1638{
1639[32]=" ",
1640[39]="'",
1641[44]=",",
1642[45]="-",
1643[46]=".",
1644[48]="0",
1645[49]="1",
1646[50]="2",
1647[51]="3",
1648[52]="4",
1649[53]="5",
1650[54]="6",
1651[55]="7",
1652[56]="8",
1653[57]="9",
1654[65]="A",
1655[66]="B",
1656[67]="C",
1657[68]="D",
1658[69]="E",
1659[70]="F",
1660[71]="G",
1661[72]="H",
1662[73]="I",
1663[74]="J",
1664[75]="K",
1665[76]="L",
1666[77]="M",
1667[78]="N",
1668[79]="O",
1669[80]="P",
1670[81]="Q",
1671[82]="R",
1672[83]="S",
1673[84]="T",
1674[85]="U",
1675[86]="V",
1676[87]="W",
1677[88]="X",
1678[89]="Y",
1679[90]="Z",
1680
1681[97]="a",
1682[98]="b",
1683[99]="c",
1684[100]="d",
1685[101]="e",
1686[102]="f",
1687[103]="g",
1688[104]="h",
1689[105]="i",
1690[106]="j",
1691[107]="k",
1692[108]="l",
1693[109]="m",
1694[110]="n",
1695[111]="o",
1696[112]="p",
1697[113]="q",
1698[114]="r",
1699[115]="s",
1700[116]="t",
1701[117]="u",
1702[118]="v",
1703[119]="w",
1704[120]="x",
1705[121]="y",
1706[122]="z",
1707}
1708local function create_character()
1709	music_play("create.m")
1710
1711	local bubbles = sprite_new(image_new(100,100, 0), 110, 30, false)
1712	local bg = sprite_new(image_load("vellum1.shp", 0), 0x10, 0x50, true)
1713	image_print(bg.image, "By what name shalt thou be called?", 7, 303, 36, 24, 0x48)
1714
1715	local name = sprite_new(nil, 0x34, 0x78, true)
1716	name.text = ""
1717	local char_index = 0
1718	local input = nil
1719
1720	while input == nil do
1721		canvas_update()
1722		input = input_poll()
1723		if input ~= nil then
1724			if should_exit(input) == true then
1725				bg.visible = false
1726				name.visible = false
1727				return false
1728			end
1729			local name_text = name.text
1730			local len = string.len(name_text)
1731			if (input == 8 or input == SDLK_LEFT) and len > 0 then
1732				name.text = string.sub(name_text, 1, len - 1)
1733				if len == 1 then -- old len
1734					char_index = 0
1735				else
1736					char_index = string.byte(name_text, len -1)
1737				end
1738			elseif input == 13 and len > 0 then --return
1739				break;
1740			elseif g_keycode_tbl[input] ~= nil and len < 13 then
1741				char_index = input
1742				name.text = name_text..g_keycode_tbl[input]
1743			elseif input == SDLK_UP then --up
1744				if char_index == 0 then
1745					if len > 0 then
1746						char_index = 97 --a
1747					else
1748						char_index = 65 --A
1749					end
1750				elseif char_index <= 32 then --gap in characters
1751					char_index = 48
1752				elseif char_index >= 57 and  char_index < 65 then --gap in characters
1753					char_index = 65
1754				elseif char_index >= 90 and char_index < 97 then --gap in characters
1755					char_index = 97
1756				elseif char_index >= 122 then --last char
1757					char_index = 32
1758				else
1759					char_index = char_index + 1
1760				end
1761
1762				if len > 0 then -- erase char
1763					name_text = string.sub(name_text, 1, len - 1)
1764				end
1765				name.text = name_text..g_keycode_tbl[char_index]
1766			elseif input == SDLK_DOWN then --down
1767				if char_index == 0 then
1768					if len > 0 then
1769						char_index = 122 --z
1770					else
1771						char_index = 90 --Z
1772					end
1773				elseif char_index == 65 then --gap in characters
1774					char_index = 57
1775				elseif char_index == 97 then --gap in characters
1776					char_index = 90
1777				elseif char_index <= 32 then --first char
1778					char_index = 122
1779				elseif char_index <= 48 then --gap in characters
1780					char_index = 32
1781				else
1782					char_index = char_index - 1
1783				end
1784
1785				if len > 0 then -- erase char
1786					name_text = string.sub(name_text, 1, len - 1)
1787				end
1788				name.text = name_text..g_keycode_tbl[char_index]
1789			elseif input == SDLK_RIGHT and len < 13 then --right
1790				char_index = 97 --a
1791				name.text = name_text.."a"
1792			end
1793			input = nil
1794		end
1795	end
1796
1797	name.x = 0x10 + (284 - canvas_string_length(name.text)) / 2
1798
1799	image_print(bg.image, "And art thou Male, or Female?", 7, 303, 52, 56, 0x48)
1800	local gender_sprite = sprite_new(nil, 154, 152, true)
1801	gender_sprite.text = ""
1802
1803	input = nil
1804	local gender = 0
1805	while input == nil do
1806		canvas_update()
1807		input = input_poll()
1808		if input ~= nil then
1809			if should_exit(input) == true then
1810				bg.visible = false
1811				name.visible = false
1812				gender_sprite.visible = false
1813				return false
1814			end
1815			if input == 77 or input == 109 then
1816				gender = 1 --male
1817				break
1818			elseif input == 70 or input == 102 then
1819				gender = 0 --female
1820				break
1821			elseif input == SDLK_UP or input == SDLK_DOWN then --up and down
1822				if gender == 0 then
1823					gender = 1 --male
1824					gender_sprite.text = "M"
1825				else
1826					gender = 0 --female
1827					gender_sprite.text = "F"
1828				end
1829			elseif input == 13 and gender_sprite.text ~= "" then --return
1830				break;
1831			end
1832
1833			input = nil
1834		end
1835	end
1836	gender_sprite.visible = false
1837	load_images("vellum1.shp")
1838	bg.image = g_img_tbl[0]
1839
1840	name.y = 0x59
1841
1842	image_draw_line(bg.image, 14, 19, 277, 19, 0x48)
1843
1844	local new_sex = sprite_new(g_img_tbl[3], 0x5e, 0x70, true)
1845	local new_portrait = sprite_new(g_img_tbl[0x12], 0x3e, 0x81, true)
1846	local continue = sprite_new(g_img_tbl[0x10], 0x56, 0x92, true)
1847	local esc_abort = sprite_new(g_img_tbl[4], 0x50, 0xa3, true)
1848
1849	local montage_img_tbl = image_load_all("montage.shp")
1850	local portrait_num = 0
1851	local avatar = sprite_new(montage_img_tbl[gender*6+portrait_num], 0xc3, 0x76, true)
1852	local button_index = 0
1853	local old_button_index = 0
1854
1855	local sex_highlight = sprite_new(image_new(58, 2), new_sex.x, new_sex.y +14, true)
1856	image_draw_line(sex_highlight.image, 0, 0, 59, 0, 248)
1857	image_draw_line(sex_highlight.image, 0, 1, 59, 1, 248)
1858
1859	local portrait_highlight = sprite_new(image_new(90, 2), new_portrait.x, new_portrait.y +14, false)
1860	image_draw_line(portrait_highlight.image, 0, 0, 91, 0, 248)
1861	image_draw_line(portrait_highlight.image, 0, 1, 91, 1, 248)
1862
1863	local continue_highlight = sprite_new(image_new(66, 2), continue.x, continue.y +14, false)
1864	image_draw_line(continue_highlight.image, 0, 0, 67, 0, 248)
1865	image_draw_line(continue_highlight.image, 0, 1, 67, 1, 248)
1866
1867	local esc_abort_highlight = sprite_new(image_new(72, 2), esc_abort.x, esc_abort.y +14, false)
1868	image_draw_line(esc_abort_highlight.image, 0, 0, 73, 0, 248)
1869	image_draw_line(esc_abort_highlight.image, 0, 1, 73, 1, 248)
1870
1871	input = nil
1872	local exiting = false
1873
1874	while input == nil do
1875		canvas_update()
1876		input = input_poll()
1877		if input ~= nil then
1878			if should_exit(input) == true  or (input == 13 and button_index == 3) then
1879				exiting = true
1880			end
1881			if input == 112 or input == 80 or (input == 13 and button_index == 1) then
1882				if portrait_num == 5 then
1883					portrait_num = 0
1884				else
1885					portrait_num = portrait_num + 1
1886				end
1887
1888				avatar.image = montage_img_tbl[gender*6+portrait_num]
1889			elseif input == 115 or input == 83 or (input == 13 and button_index == 0) then
1890				if gender == 0 then
1891					gender = 1
1892				else
1893					gender = 0
1894				end
1895
1896				avatar.image = montage_img_tbl[gender*6+portrait_num]
1897			elseif input == 99 or input == 67 or (input == 13 and button_index == 2) then
1898				break
1899			elseif input == SDLK_UP and button_index > 0 then --up
1900				button_index = button_index -1;
1901			elseif input == SDLK_DOWN and button_index < 3 then --down
1902				button_index = button_index +1;
1903			elseif input == 0 then -- FIXME redundant stuff
1904				local x = get_mouse_x()
1905				if(x < 152) then
1906					local y = get_mouse_y()
1907					if x > 93 and y > 111 and y < 128 then
1908						if gender == 0 then
1909							gender = 1
1910						else
1911							gender = 0
1912						end
1913						avatar.image = montage_img_tbl[gender*6+portrait_num]
1914					elseif x > 61 and y > 128 and y < 145 then
1915						if portrait_num == 5 then
1916							portrait_num = 0
1917						else
1918							portrait_num = portrait_num + 1
1919						end
1920						avatar.image = montage_img_tbl[gender*6+portrait_num]
1921					elseif x > 85 and y > 145 and y < 162 then
1922						break
1923					elseif x > 79 and y > 162 and y < 179 then
1924						exiting = true
1925					end
1926				end
1927			end
1928			if button_index ~= old_button_index or exiting == true then
1929
1930				if old_button_index == 0 then
1931					sex_highlight.visible = false
1932				elseif old_button_index == 1 then
1933					portrait_highlight.visible = false
1934				elseif old_button_index == 2 then
1935					continue_highlight.visible = false
1936				else
1937					esc_abort_highlight.visible = false
1938				end
1939
1940				if exiting == true then
1941					bg.visible = false
1942					name.visible = false
1943					new_sex.visible = false
1944					new_portrait.visible = false
1945					continue.visible = false
1946					esc_abort.visible = false
1947					avatar.visible = false
1948					return false
1949				end
1950
1951				if button_index == 0 then
1952					sex_highlight.visible = true
1953				elseif button_index == 1 then
1954					portrait_highlight.visible = true
1955				elseif button_index == 2 then
1956					continue_highlight.visible = true
1957				else
1958					esc_abort_highlight.visible = true
1959				end
1960
1961				old_button_index = button_index
1962			end
1963			input = nil
1964		end
1965	end
1966	mouse_cursor_visible(false)
1967	fade_out()
1968	canvas_hide_all_sprites()
1969
1970	canvas_set_palette("palettes.int", 4)
1971	local woods_img_tbl = image_load_all("woods.shp")
1972	local woods = sprite_new(woods_img_tbl[0], 0, 0, true)
1973	local woods1 = sprite_new(woods_img_tbl[1], 0x140, 0, true)
1974
1975	fade_in()
1976
1977	local scroll_img = image_load("blocks.shp", 1)
1978	local scroll = sprite_new(scroll_img, 1, 0xa0, true)
1979
1980	local x, y = image_print(scroll_img, "\"Welcome, O Seeker!\127", 7, 303, 96, 14, 0x3e)
1981
1982	wait_for_input()
1983
1984	scroll.visible = false
1985
1986	input = nil
1987	for i=0,0xbd,2 do
1988		woods.x = woods.x - 2
1989		woods1.x = woods1.x - 2
1990		canvas_update()
1991		input = input_poll()
1992		if input ~= nil then
1993			break
1994		end
1995	end
1996
1997	woods.x = -190
1998	woods1.x = 130
1999
2000	scroll_img = image_load("blocks.shp", 2)
2001	scroll.x = 1
2002	scroll.y = 0x98
2003	image_print(scroll_img, "A lonely stroll along an unfamiliar forest path brings you upon a curious gypsy wagon, its exotic colors dappled in the summer shade.", 7, 303, 8, 12, 0x3e)
2004	scroll.image = scroll_img
2005	scroll.visible = true
2006
2007	wait_for_input()
2008
2009	scroll_img = image_load("blocks.shp", 2)
2010	scroll.x = 1
2011	scroll.y = 0x98
2012	image_print(scroll_img, "A woman's voice rings out with friendship, beckoning you into across the wagon's threshold and, as it happens, into another life....", 7, 303, 8, 12, 0x3e)
2013	scroll.image = scroll_img
2014	scroll.visible = true
2015
2016	wait_for_input()
2017
2018	scroll.visible = false
2019
2020	fade_out()
2021	canvas_hide_all_sprites()
2022	canvas_set_palette("palettes.int", 5)
2023
2024	g_gypsy_img_tbl = image_load_all("gypsy.shp")
2025
2026	bg.image = g_gypsy_img_tbl[0]
2027	bg.x = 0
2028	bg.y = -20
2029	bg.visible = true
2030
2031	g_gypsy_tbl = {}
2032	g_gypsy_tbl["arm"] = sprite_new(nil, 0, 0, false)
2033	g_gypsy_tbl["pour"] = sprite_new(nil, 0, 0, false)
2034
2035
2036	g_gypsy_tbl["jar_level"] = 0
2037	g_gypsy_tbl["jar_liquid"] = sprite_new(nil, 0x92, 0x53, false)
2038	g_gypsy_tbl["jar_liquid"].clip_x = 0
2039	g_gypsy_tbl["jar_liquid"].clip_y = 0
2040	g_gypsy_tbl["jar_liquid"].clip_w = 320
2041	g_gypsy_tbl["jar_liquid"].clip_h = 0x6d
2042
2043	g_gypsy_tbl["jar"] = sprite_new(g_gypsy_img_tbl[16], 0x8e, 0x38, true)
2044
2045	g_gypsy_tbl["bubble_counter"] = 0
2046
2047	g_gypsy_tbl["vial_level"] = {3,3,3,3,3,3,3,3}
2048	local vial_level = g_gypsy_tbl["vial_level"]
2049
2050	local vial_img_off = {2, 5, 8, 0xB, 0x18, 0x1B, 0x1E, 0x21}
2051
2052	g_gypsy_tbl["vial_liquid"] = {
2053		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[1] + vial_level[1] - 3], 44, 0x4d, true),
2054		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[2] + vial_level[2] - 3], 65, 0x4d, true),
2055		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[3] + vial_level[3] - 3], 86, 0x4d, true),
2056		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[4] + vial_level[4] - 3], 107, 0x4d, true),
2057		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[5] + vial_level[5] - 3], 203, 0x4d, true),
2058		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[6] + vial_level[6] - 3], 224, 0x4d, true),
2059		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[7] + vial_level[7] - 3], 245, 0x4d, true),
2060		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[8] + vial_level[8] - 3], 266, 0x4d, true),
2061	}
2062
2063	g_gypsy_tbl["hand"] = sprite_new(nil, 0, 0, false)
2064
2065	g_gypsy_tbl["vial"] = {
2066		sprite_new(g_gypsy_img_tbl[20], 41, 0x42, true),
2067		sprite_new(g_gypsy_img_tbl[20], 62, 0x42, true),
2068		sprite_new(g_gypsy_img_tbl[20], 83, 0x42, true),
2069		sprite_new(g_gypsy_img_tbl[20], 104, 0x42, true),
2070		sprite_new(g_gypsy_img_tbl[23], 200, 0x42, true),
2071		sprite_new(g_gypsy_img_tbl[23], 221, 0x42, true),
2072		sprite_new(g_gypsy_img_tbl[23], 242, 0x42, true),
2073		sprite_new(g_gypsy_img_tbl[23], 263, 0x42, true),
2074	}
2075
2076	fade_in()
2077
2078	scroll.x = 1
2079	scroll.y = 0x7c
2080	scroll.visible = true
2081
2082	local scroll_img = image_load("blocks.shp", 3)
2083	scroll.image = scroll_img
2084	x, y = image_print(scroll_img, "\"At last thou hast come to fulfill thy destiny,\127 the gypsy says. She smiles, as if in great relief.", 7, 303, 8, 19, 0x3e)
2085	image_print(scroll_img, "\"Sit before me now, and I shall pour the light of Virtue into the shadows of thy future.\127", 7, 303, 8, y+16, 0x3e)
2086
2087	wait_for_input()
2088
2089	scroll_img = image_load("blocks.shp", 3)
2090	scroll.image = scroll_img
2091	x, y = image_print(scroll_img, "On a wooden table eight bottles stand, a rainbow of bubbling liquids.", 7, 303, 8, 19, 0x3e)
2092	image_print(scroll_img, "\"Behold the Virtues of the Avatar,\127 the woman says. \"Let us begin the casting!\127", 7, 303, 8, y+16, 0x3e)
2093
2094	wait_for_input()
2095	canvas_set_palette_entry(19, 200, 200, 200) -- fix mouse cursor
2096	canvas_set_palette_entry(27, 68, 68, 68) -- fix mouse cursor
2097
2098	mouse_cursor_visible(true)
2099	local a_button = sprite_new(g_gypsy_img_tbl[7], 0x117, 0xae, true)
2100	local b_button = sprite_new(g_gypsy_img_tbl[8], 0x128, 0xae, true)
2101	g_ab_highlight = sprite_new(image_new(16, 2), a_button.x, a_button.y + 13, true)
2102	image_draw_line(g_ab_highlight.image, 0, 0, 17, 0, 248)
2103	image_draw_line(g_ab_highlight.image, 0, 1, 17, 1, 248)
2104
2105	g_str = 0xf
2106	g_dex = 0xf
2107	g_int = 0xf
2108
2109
2110
2111	shuffle_question_tbl(8)
2112	gypsy_ask_questions(4, scroll)
2113
2114	shuffle_question_tbl(4)
2115
2116	gypsy_ask_questions(2, scroll)
2117	gypsy_ask_questions(1, scroll)
2118
2119	a_button.visible = false
2120	b_button.visible = false
2121	g_ab_highlight.visible = false
2122	mouse_cursor_visible(false)
2123
2124	scroll_img = image_load("blocks.shp", 3)
2125	scroll.image = scroll_img
2126	image_print(scroll_img, "\"The path of the Avatar lies beneath thy feet, worthy "..name.text..",\127 the gypsy intones. With a mysterious smile, she passes you the flask of shimmering liquids. \"Drink of these waters and go forth among our people, who shall receive thee in joy!\127", 7, 303, 8, 16, 0x3e)
2127
2128	-- wait_for_input()
2129
2130	input = nil
2131	while input == nil do
2132		gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
2133		canvas_update()
2134		input = input_poll()
2135		if input ~= nil then
2136			break
2137		end
2138	end
2139
2140	fade_out()
2141
2142	canvas_hide_all_sprites()
2143	canvas_set_palette("palettes.int", 6)
2144
2145
2146	--local big_flask = sprite_new(g_gypsy_img_tbl[198], 0, 0, true)
2147	bubbles.visible = true
2148	bg.x = 0
2149	bg.y = 0
2150	bg.visible = true
2151	bg.image = g_gypsy_img_tbl[198]
2152	g_gypsy_tbl["bubble_counter"] = 0
2153	gypsy_update_bubbles(bubbles.image)
2154
2155	fade_in()
2156
2157	scroll_img = image_load("blocks.shp", 2)
2158	scroll.image = scroll_img
2159	scroll.x = 1
2160	scroll.y = 0x98
2161	scroll.visible = true
2162	image_print(scroll_img, "As you drink from the flask, vertigo overwhelms you. A soothing mist obscures the gypsy's face, and you sink without fear into an untroubled sleep.", 7, 303, 8, 8, 0x3e)
2163
2164	--wait_for_input()
2165
2166	input = nil
2167	while input == nil do
2168		gypsy_update_bubbles(bubbles.image)
2169		canvas_update()
2170		input = input_poll()
2171		if input ~= nil then
2172			break
2173		end
2174	end
2175
2176	scroll.visible = false
2177
2178	canvas_set_bg_color(0x75)
2179	fade_out()
2180
2181	bubbles.visible = false
2182
2183	bg.image = g_gypsy_img_tbl[199]
2184	bg.x = 0
2185	bg.y = 0
2186	bg.visible = true
2187
2188	scroll.visible = false
2189
2190	fade_in()
2191
2192	scroll_img = image_load("blocks.shp", 2)
2193	scroll.image = scroll_img
2194	scroll.visible = true
2195	image_print(scroll_img, "You wake in a different time, upon another world's shore. Though the Avatar's quests bring you both triumph and tragedy, never do you stray from the path of the Eight Virtues.", 7, 303, 8, 8, 0x3e)
2196
2197	wait_for_input()
2198
2199	scroll_img = image_load("blocks.shp", 2)
2200	scroll.image = scroll_img
2201	scroll.visible = true
2202	image_print(scroll_img, "The sagas of Ultima IV and Ultima V chronicle your perilous travels, and your name and your deeds are written forever among Britannia's legends....", 7, 303, 8, 8, 0x3e)
2203
2204	wait_for_input()
2205
2206	scroll_img = image_load("blocks.shp", 2)
2207	scroll.image = scroll_img
2208	scroll.visible = true
2209	image_print(scroll_img, "Finally, tempered by your struggles against the enemies of Virtue, you are proven ready to answer the epic challenge of Ultima VI!", 7, 303, 8, 12, 0x3e)
2210
2211	wait_for_input()
2212	scroll.visible = false
2213
2214	canvas_set_bg_color(0)
2215	fade_out()
2216
2217	config_set("config/newgame", true)
2218
2219	config_set("config/newgamedata/name", name.text)
2220	config_set("config/newgamedata/gender", gender)
2221	config_set("config/newgamedata/portrait", portrait_num)
2222	config_set("config/newgamedata/str", g_str)
2223	config_set("config/newgamedata/dex", g_dex)
2224	config_set("config/newgamedata/int", g_int)
2225
2226	g_gypsy_img_tbl = nil
2227	g_gypsy_tbl = nil
2228
2229	return true
2230end
2231
2232local function ack_header(scroll_img)
2233	image_print(scroll_img, "Ultima VI", 7, 303, 115, 9, 0x3d)
2234	image_print(scroll_img, "A Lord British Production", 7, 303, 63, 17, 0x3d)
2235	image_draw_line(scroll_img, 9, 26, 274, 26, 0x3d)
2236end
2237
2238local function acknowledgements()
2239
2240	local bg = sprite_new(image_load("vellum1.shp", 0), 0x10, 0x50, true)
2241
2242	ack_header(bg.image)
2243
2244	image_print(bg.image, "Produced by", 7, 303, 106, 32, 0xC)
2245	image_print(bg.image, "Richard Garriott and Warren Spector", 7, 303, 28, 40, 0x48)
2246
2247	image_print(bg.image, "Executive Producer", 7, 303, 82, 56, 0xC)
2248	image_print(bg.image, "Dallas Snell", 7, 303, 106, 64, 0x48)
2249
2250	image_print(bg.image, "Programming", 7, 303, 104, 80, 0xC)
2251	image_print(bg.image, "Cheryl Chen    John Miles", 7, 303, 67, 88, 0x48)
2252	image_print(bg.image, "Herman Miller    Gary Scott Smith", 7, 303, 40, 96, 0x48)
2253
2254	fade_in_sprite(bg)
2255	local input
2256
2257	wait_for_input()
2258
2259	bg.image = image_load("vellum1.shp", 0)
2260
2261	ack_header(bg.image)
2262
2263	image_print(bg.image, "Writing", 7, 303, 120, 47, 0xC)
2264	image_print(bg.image, "Stephen Beeman    Dr. Cat    \39Manda Dee", 7, 303, 25, 55, 0x48)
2265	image_print(bg.image, "Richard Garriott    Greg Malone", 7, 303, 46, 63, 0x48)
2266	image_print(bg.image, "John Miles    Herman Miller", 7, 303, 61, 71, 0x48)
2267	image_print(bg.image, "Todd Porter    Warren Spector", 7, 303, 50, 79, 0x48)
2268
2269	wait_for_input()
2270
2271	bg.image = image_load("vellum1.shp", 0)
2272
2273	ack_header(bg.image)
2274
2275	image_print(bg.image, "Art", 7, 303, 132, 31, 0xC)
2276	image_print(bg.image, "Keith Berdak    Daniel Bourbonnais", 7, 303, 37, 39, 0x48)
2277	image_print(bg.image, "Jeff Dee    \39Manda Dee", 7, 303, 75, 47, 0x48)
2278	image_print(bg.image, "Glen Johnson    Denis Loubet", 7, 303, 56, 55, 0x48)
2279
2280	image_print(bg.image, "Music", 7, 303, 126, 71, 0xC)
2281	image_print(bg.image, "Ken Arnold    Iolo Fitzowen", 7, 303, 61, 79, 0x48)
2282	image_print(bg.image, "Herman Miller    Todd Porter", 7, 303, 56, 87, 0x48)
2283
2284	wait_for_input()
2285
2286	bg.image = image_load("vellum1.shp", 0)
2287
2288	ack_header(bg.image)
2289
2290	image_print(bg.image, "Quality Assurance", 7, 303, 87, 31, 0xC)
2291	image_print(bg.image, "Paul Malone    Mike Romero", 7, 303, 62, 39, 0x48)
2292	image_print(bg.image, "", 7, 303, 49, 47, 0x48)
2293
2294	image_print(bg.image, "Additional Support", 7, 303, 84, 63, 0xC)
2295	image_print(bg.image, "Michelle Caddel    Melanie Fleming", 7, 303, 39, 71, 0x48)
2296	image_print(bg.image, "Alan Gardner    Jeff Hillhouse", 7, 303, 51, 79, 0x48)
2297	image_print(bg.image, "Sherry Hunter    Steve Muchow", 7, 303, 49, 87, 0x48)
2298	image_print(bg.image, "Cheryl Neeld", 7, 303, 104, 95, 0x48)
2299
2300	wait_for_input()
2301
2302	fade_out_sprite(bg, 4)
2303
2304	bg.visible = false
2305
2306	return true
2307end
2308
2309
2310local function intro_sway_gargs(sprite, idx, angry_flag)
2311	if math.random(0, 3) == 0 then
2312		return idx
2313	end
2314
2315	local movement_tbl = {1,1,1, -1,-1,-1,  -1,-1,-1,  1, 1, 1}
2316
2317	if idx == 12 then
2318		idx = 1
2319	else
2320		idx = idx + 1
2321	end
2322
2323	sprite.x = sprite.x + movement_tbl[idx]
2324
2325	return idx
2326end
2327
2328local function moongate_rotate_palette(idx, num)
2329	if g_pal_counter == 3 then
2330		canvas_rotate_palette(232, 8)
2331		g_pal_counter = 0
2332	else
2333		g_pal_counter = g_pal_counter + 1
2334	end
2335end
2336
2337local function intro_exit()
2338	fade_out()
2339	canvas_hide_all_sprites()
2340	canvas_set_palette("palettes.int", 0)
2341end
2342
2343local function intro_wait()
2344	if should_exit(wait_for_input()) == true then
2345		intro_exit()
2346		return false
2347	end
2348
2349	return true
2350end
2351
2352local function intro()
2353	local input
2354	local intro_img_tbl = image_load_all("intro.shp")
2355
2356	local bg = sprite_new(intro_img_tbl[6], 0, 0, true)
2357	local moongate = sprite_new(intro_img_tbl[7], 0x78, 0x3a, false)
2358	local gargs_left = sprite_new(intro_img_tbl[3], -84, 0x6d, true)
2359	local gargs_right = sprite_new(intro_img_tbl[4], 326, 0xc7, true)
2360	local garg_body = sprite_new(intro_img_tbl[8], 0xd5, 0x8d, false)
2361	local garg_head = sprite_new(intro_img_tbl[11], 0x123, 0x9b, false)
2362	local iolo = sprite_new(intro_img_tbl[1], 0xa8, 0xca, false)
2363	local shamino = sprite_new(intro_img_tbl[2], 0x44, 0x7a, false)
2364	local dupre = sprite_new(intro_img_tbl[0], -0x20, 0x7a, false)
2365
2366	local avatar = sprite_new(intro_img_tbl[9], 0x31, 0x44, false)
2367	local alter = sprite_new(intro_img_tbl[5], 0, 0x70, true)
2368	local ropes = sprite_new(intro_img_tbl[12], 0xd2, 0x84, false)
2369
2370	canvas_set_palette("palettes.int", 7)
2371	music_play("intro.m")
2372
2373	fade_in()
2374
2375	local scroll_img = image_load("blocks.shp", 2)
2376	local scroll = sprite_new(scroll_img, 1, 0x98, true)
2377
2378	image_print(scroll_img, "Dazed, you emerge from the portal to find yourself standing on a desolate plain. Nearby rests a massive rune-struck altar, shrouded in moonlit fog.", 7, 308, 8, 8, 0x3e)
2379
2380	if should_exit(wait_for_input()) == true then intro_exit() return end
2381
2382	scroll_img = image_load("blocks.shp", 2)
2383	scroll.image = scroll_img
2384	image_print(scroll_img, "At first the plain is still. Then a hundred voices raise a slow, deathlike song, drawing closer and closer with each passing moment. You are seized by an urge to run...", 7, 308, 8, 8, 0x3e)
2385
2386	if intro_wait() == false then return end
2387
2388	local l_move_tbl_x = {1, 1, 0, 1, 1, 1}
2389	local l_move_tbl_y = {0, 0, -1, 0, 0, -1}
2390
2391	local r_move_tbl_x = {-1, -1, -1, -1, -1, -1}
2392	local r_move_tbl_y = {-1, -1, 0, -1, -1, -1}
2393
2394	scroll.visible = false
2395
2396	local i
2397	for i=0,95,1 do
2398		gargs_left.x = gargs_left.x + l_move_tbl_x[(i%6)+1]
2399		gargs_left.y = gargs_left.y + l_move_tbl_y[(i%6)+1]
2400
2401		if i > 23 then
2402			gargs_right.x = gargs_right.x + r_move_tbl_x[(i%6)+1] * 2
2403			gargs_right.y = gargs_right.y + r_move_tbl_y[(i%6)+1] * 2
2404		end
2405
2406		input = input_poll()
2407		if input ~= nil then
2408			if should_exit(input) ==  true then
2409				intro_exit()
2410				return
2411			end
2412
2413			gargs_left.x = -4
2414			gargs_left.y = 77
2415			gargs_right.x = 182
2416			gargs_right.y = 79
2417
2418			break
2419		end
2420		canvas_update()
2421		canvas_update()
2422	end
2423
2424	scroll_img = image_load("blocks.shp", 0)
2425	scroll.image = scroll_img
2426	scroll.x = 0x21
2427	scroll.y = 0x9d
2428	scroll.visible = true
2429	image_print(scroll_img, "...but you have no place to go.", 7, 308, 35, 8, 0x3e)
2430
2431	if intro_wait() == false then return end
2432
2433	scroll_img = image_load("blocks.shp", 2)
2434	scroll.image = scroll_img
2435	scroll.x = 0x1
2436	scroll.y = 0x98
2437	image_print(scroll_img, "Before you can offer a protest to the creatures who surround you, scaly claws grasp your body.", 7, 308, 8, 12, 0x3e)
2438
2439	if intro_wait() == false then return end
2440
2441	scroll_img = image_load("blocks.shp", 1)
2442	scroll.image = scroll_img
2443	scroll.x = 0x1
2444	scroll.y = 0xa0
2445	image_print(scroll_img, "With unearthly strength, the monsters bind you to the altar stone!", 7, 308, 11, 10, 0x3e)
2446
2447	avatar.visible = true
2448	ropes.visible = true
2449
2450	if intro_wait() == false then return end
2451
2452	gargs_left.y = gargs_left.y + 4
2453	gargs_right.y = gargs_right.y + 4
2454	scroll.visible = false
2455
2456	garg_body.visible = true
2457	garg_head.visible = true
2458
2459	for i=0,22,1 do
2460		garg_body.x = garg_body.x - 3
2461		garg_body.y = garg_body.y - 3
2462		garg_head.x = garg_head.x - 3
2463		garg_head.y = garg_head.y - 3
2464
2465		input = input_poll()
2466		if input ~= nil then
2467			if should_exit(input) ==  true then
2468				intro_exit()
2469				return
2470			end
2471
2472			break
2473		end
2474		canvas_update()
2475		canvas_update()
2476	end
2477
2478	if input == nil then
2479		for i=0,13,1 do
2480			garg_body.y = garg_body.y - 3
2481			garg_head.y = garg_head.y - 3
2482
2483			input = input_poll()
2484			if input ~= nil then
2485				if should_exit(input) ==  true then
2486					intro_exit()
2487					return
2488				end
2489
2490				break
2491			end
2492
2493			canvas_update()
2494			canvas_update()
2495		end
2496	end
2497
2498	garg_body.x = 144
2499	garg_body.y = 30
2500	garg_head.x = 222
2501	garg_head.y = 44
2502
2503	scroll_img = image_load("blocks.shp", 1)
2504	scroll.image = scroll_img
2505	scroll.x = 0x1
2506	scroll.y = 0xa0
2507	scroll.visible = true
2508	image_print(scroll_img, "Kneeling, the hordes sway and chant as a stately winged nightmare steps forward.", 32, 262, 33, 10, 0x3e)
2509
2510	local input = nil
2511	local left_idx = 1
2512	local right_idx = 6
2513	while input == nil do
2514		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2515		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2516		canvas_update()
2517		input = input_poll()
2518			if input ~= nil then
2519				if should_exit(input) ==  true then
2520					intro_exit()
2521					return
2522				end
2523				break
2524			end
2525		canvas_update()
2526	end
2527
2528	scroll_img = image_load("blocks.shp", 2)
2529	scroll.image = scroll_img
2530	scroll.x = 0x1
2531	scroll.y = 0x98
2532	scroll.visible = true
2533	image_print(scroll_img, "The leader unwraps a velvet covered, brass-bound book and recites from it in a formal, stilted tongue.", 7, 308, 8, 12, 0x3e)
2534
2535	input = nil
2536	while input == nil do
2537		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2538		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2539		canvas_update()
2540		input = input_poll()
2541		if input ~= nil then
2542			if should_exit(input) ==  true then
2543				intro_exit()
2544				return
2545			end
2546			break
2547		end
2548		canvas_update()
2549	end
2550
2551	scroll_img = image_load("blocks.shp", 2)
2552	scroll.image = scroll_img
2553	image_print(scroll_img, "Shouts and jeers explode from the masses as the priest slams shut the book. In his hand a malignant dagger drips with moonlight.", 7, 308, 8, 12, 0x3e)
2554
2555	input = nil
2556	while input == nil do
2557		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2558		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2559		canvas_update()
2560		input = input_poll()
2561		if input ~= nil then
2562			if should_exit(input) ==  true then
2563				intro_exit()
2564				return
2565			end
2566			break
2567		end
2568		canvas_update()
2569	end
2570
2571	scroll_img = image_load("blocks.shp", 1)
2572	scroll.image = scroll_img
2573	scroll.x = 0x1
2574	scroll.y = 0xa0
2575	image_print(scroll_img, "You close your eyes. A dying scream, certainly your own, curdles the air.", 80, 228, 16, 10, 0x3e)
2576
2577	moongate.visible = true
2578	g_pal_counter = 0
2579
2580	for i=0x78,0x3a,-1 do
2581		moongate.y = i
2582		moongate_rotate_palette()
2583		canvas_update()
2584		input = input_poll()
2585		if input ~= nil then
2586			if should_exit(input) then
2587				intro_exit()
2588				return
2589			end
2590			moongate.y = 0x3a
2591			break
2592		end
2593	end
2594
2595	input = nil
2596	while input == nil do
2597		moongate_rotate_palette()
2598		canvas_update()
2599		input = input_poll()
2600		if input ~= nil then
2601			if should_exit(input) then
2602				intro_exit()
2603				return
2604			end
2605			break
2606		end
2607	end
2608
2609	for i=0xff,0,-3 do
2610		moongate_rotate_palette()
2611		canvas_set_opacity(i)
2612		canvas_update()
2613		input_poll()
2614	end
2615
2616	canvas_hide_all_sprites()
2617
2618	scroll_img = image_load("blocks.shp", 1)
2619	scroll.image = scroll_img
2620	scroll.x = 0x1
2621	scroll.y = 0x50
2622	scroll.visible = true
2623	image_print(scroll_img, "Catcalls, the dagger, a scream, Death....", 7, 308, 39, 14, 0x3e)
2624
2625	fade_in()
2626
2627	if intro_wait() == false then return end
2628
2629	fade_out()
2630
2631	scroll_img = image_load("blocks.shp", 1)
2632	scroll.image = scroll_img
2633	image_print(scroll_img, "Pandemonium. Shrieks of rage, of terror.", 7, 308, 34, 14, 0x3e)
2634
2635	fade_in()
2636
2637	if intro_wait() == false then return end
2638
2639	fade_out()
2640
2641	scroll_img = image_load("blocks.shp", 1)
2642	scroll.image = scroll_img
2643	image_print(scroll_img, "From the inevitable, an impossibility emerges.", 7, 308, 16, 14, 0x3e)
2644
2645	fade_in()
2646
2647	if intro_wait() == false then return end
2648
2649	fade_out()
2650
2651	scroll_img = image_load("blocks.shp", 1)
2652	scroll.image = scroll_img
2653	image_print(scroll_img, "You are still alive.", 7, 308, 101, 14, 0x3e)
2654
2655	fade_in()
2656
2657	if intro_wait() == false then return end
2658
2659	fade_out()
2660
2661	bg.visible = true
2662	moongate.visible = true
2663	gargs_left.visible = true
2664	gargs_right.visible = true
2665	garg_body.visible = true
2666	garg_head.visible = true
2667	alter.visible = true
2668	avatar.visible = true
2669	ropes.visible = true
2670
2671	scroll.visible = false
2672
2673	for i=0,0xff,3 do
2674		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2675		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2676		moongate_rotate_palette()
2677		canvas_set_opacity(i)
2678		canvas_update()
2679		input_poll()
2680	end
2681
2682	garg_head.image = intro_img_tbl[10]
2683
2684	scroll_img = image_load("blocks.shp", 2)
2685	scroll.image = scroll_img
2686	scroll.x = 0x1
2687	scroll.y = 0x98
2688	scroll.visible = true
2689	image_print(scroll_img, "Silent red light fills the darkness. There is the wooden clack of a crossbow, and a violet- fletched rose blooms in the priest\39s barren forehead.", 7, 308, 8, 8, 0x3e)
2690
2691	input = nil
2692	while input == nil do
2693		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2694		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2695		moongate_rotate_palette()
2696		canvas_update()
2697		input = input_poll()
2698		if input ~= nil then
2699			if should_exit(input) then
2700				intro_exit()
2701				return
2702			end
2703			break
2704		end
2705	end
2706
2707	scroll.visible = false
2708
2709	for i=0,42,1 do
2710		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2711		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2712		moongate_rotate_palette()
2713
2714		local x =  math.random(-1, 2)
2715		garg_body.x = garg_body.x + x
2716		garg_body.y = garg_body.y + 3
2717		garg_head.x = garg_head.x + x
2718		garg_head.y = garg_head.y + 3
2719
2720		input = input_poll()
2721		if input ~= nil then
2722			if should_exit(input) ==  true then
2723				intro_exit()
2724				return
2725			end
2726			garg_body.visible = false
2727			garg_head.visible = false
2728			break
2729		end
2730		canvas_update()
2731	end
2732
2733	garg_body.visible = false
2734	garg_head.visible = false
2735
2736	iolo.visible = true
2737	dupre.visible = true
2738	shamino.visible = true
2739
2740	for i=0,82,1 do
2741		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2742		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2743		moongate_rotate_palette()
2744
2745
2746		if i > 33 then
2747			dupre.x = dupre.x + 2
2748		else
2749			dupre.x = dupre.x + 1
2750		end
2751
2752		dupre.y = dupre.y - 1
2753
2754		if i > 13 then
2755			if i > 36 then
2756				shamino.x = shamino.x + 2
2757			else
2758				shamino.x = shamino.x + 1
2759			end
2760			shamino.y = shamino.y - 1
2761		end
2762
2763		if i > 10 then
2764			iolo.x = iolo.x + 1
2765			if i > 14 then
2766				iolo.y = iolo.y - 2
2767			else
2768				iolo.y = iolo.y - 1
2769			end
2770		end
2771
2772		input = input_poll()
2773		if input ~= nil then
2774			if should_exit(input) ==  true then
2775				intro_exit()
2776				return
2777			end
2778			dupre.x = 99
2779			dupre.y = 40
2780			shamino.x = 183
2781			shamino.y = 53
2782			iolo.x = 240
2783			iolo.y = 62
2784			break
2785		end
2786		canvas_update()
2787	end
2788
2789	scroll_img = image_load("blocks.shp", 2)
2790	scroll.image = scroll_img
2791	scroll.x = 0x1
2792	scroll.y = 0x98
2793	scroll.visible = true
2794	image_print(scroll_img, "Friendly faces vault from a newborn moongate, while a rain of quarrels holds the furious mob at bay. The knight Dupre\39s sword flashes twice in the darkness, slicing away your bonds!", 7, 308, 8, 8, 0x3e)
2795
2796	input = nil
2797	while input == nil do
2798		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2799		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2800		moongate_rotate_palette()
2801		canvas_update()
2802		input = input_poll()
2803		if input ~= nil then
2804			if should_exit(input) then
2805				intro_exit()
2806				return
2807			end
2808			break
2809		end
2810	end
2811
2812	scroll.visible = false
2813	ropes.visible = false
2814
2815	for i=0,82,1 do
2816		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2817		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2818		moongate_rotate_palette()
2819
2820		dupre.x = dupre.x - 1
2821		dupre.y = dupre.y + 2
2822
2823
2824		shamino.x = shamino.x - 1
2825		shamino.y = shamino.y + 2
2826
2827		iolo.x = iolo.x - 1
2828		iolo.y = iolo.y + 2
2829
2830		avatar.y = avatar.y + 1
2831
2832		input = input_poll()
2833		if input ~= nil then
2834			if should_exit(input) ==  true then
2835				intro_exit()
2836				return
2837			end
2838			dupre.visible = false
2839			shamino.visible = false
2840			iolo.visible = false
2841			avatar.visible = false
2842			break
2843		end
2844		canvas_update()
2845	end
2846
2847	scroll_img = image_load("blocks.shp", 2)
2848	scroll.image = scroll_img
2849	scroll.visible = true
2850	image_print(scroll_img, "\"Quickly, old friend! To the gate!\127 Accompanied by the swordsman Shamino and a grinning, crossbow-wielding Iolo the Bard, Dupre thrusts a spare sword into your hand.", 7, 308, 8, 8, 0x3e)
2851
2852	input = nil
2853	while input == nil do
2854		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2855		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2856		moongate_rotate_palette()
2857		canvas_update()
2858		input = input_poll()
2859		if input ~= nil then
2860			if should_exit(input) then
2861				intro_exit()
2862				return
2863			end
2864			break
2865		end
2866	end
2867
2868	scroll_img = image_load("blocks.shp", 2)
2869	scroll.image = scroll_img
2870	image_print(scroll_img, "Snatching the fallen priest\39s book, Iolo dives into the redness with Shamino at his heels. The howling throng surges forward, all of one terrible mind.", 7, 308, 8, 8, 0x3e)
2871
2872	input = nil
2873	while input == nil do
2874		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2875		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2876		moongate_rotate_palette()
2877		canvas_update()
2878		input = input_poll()
2879		if input ~= nil then
2880			if should_exit(input) then
2881				intro_exit()
2882				return
2883			end
2884			break
2885		end
2886	end
2887
2888	scroll_img = image_load("blocks.shp", 1)
2889	scroll.image = scroll_img
2890	scroll.x = 0x1
2891	scroll.y = 0xa0
2892	image_print(scroll_img, "The gate wanes rapidly as you and Dupre charge through...", 130, 178, 12, 10, 0x3e)
2893
2894	for i=0,150,1 do
2895		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2896		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2897		if i % 2 == 0 then
2898			moongate.y = moongate.y + 1
2899		end
2900		moongate_rotate_palette()
2901		canvas_update()
2902		input = input_poll()
2903		if input ~= nil then
2904			if should_exit(input) then
2905				intro_exit()
2906				return
2907			end
2908			moongate.visible = false
2909			break
2910		end
2911	end
2912
2913	scroll_img = image_load("blocks.shp", 0)
2914	scroll.image = scroll_img
2915	scroll.x = 0x21
2916	scroll.y = 0x9d
2917	image_print(scroll_img, "...but not rapidly enough.", 7, 308, 50, 8, 0x3e)
2918
2919	input = nil
2920	while input == nil do
2921		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2922		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2923		canvas_update()
2924		input = input_poll()
2925		if input ~= nil then
2926			if should_exit(input) then
2927				intro_exit()
2928				return
2929			end
2930			break
2931		end
2932	end
2933
2934	scroll_img = image_load("blocks.shp", 2)
2935	scroll.image = scroll_img
2936	scroll.x = 0x1
2937	scroll.y = 0x98
2938	image_print(scroll_img, "From the mob\39s vanguard, three of the abominations scramble toward the gate. Driven by fury, the creatures hurl their bodies into the portal\39s last handspan of light!", 7, 308, 8, 8, 0x3e)
2939
2940	input = nil
2941	while input == nil do
2942		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
2943		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
2944		canvas_update()
2945		input = input_poll()
2946		if input ~= nil then
2947			if should_exit(input) then
2948				intro_exit()
2949				return
2950			end
2951			break
2952		end
2953	end
2954
2955	scroll.visible = false
2956
2957	intro_exit()
2958end
2959
2960g_menu_pal =
2961{
2962	{232,96,0},
2963	{236,128,0},
2964	{244,164,0},
2965	{248,200,0},
2966	{252,252,84},
2967	{248,200,0},
2968	{244,164,0},
2969	{236,128,0},
2970	{232,96,0}
2971}
2972
2973g_menu_pal_idx = { 14, 33, 34, 35, 36 }
2974local function main_menu_set_pal(idx)
2975	local i
2976
2977	for i = 1,5,1 do
2978		local colour = g_menu_pal[5+(i-1)-idx]
2979		canvas_set_palette_entry(g_menu_pal_idx[i], colour[1], colour[2], colour[3])
2980	end
2981end
2982
2983local function main_menu_load()
2984	g_menu = {}
2985
2986	if engine_should_quit() == 1 then
2987		return
2988	end
2989
2990	music_play("ultima.m")
2991
2992	canvas_set_palette("palettes.int", 0)
2993
2994	local title_img_tbl = image_load_all("titles.shp")
2995	g_menu["title"] = sprite_new(title_img_tbl[0], 0x13, 0, true)
2996	g_menu["subtitle"] = sprite_new(title_img_tbl[1], 0x3b, 0x2f, false)
2997
2998	g_menu["menu"] = sprite_new(image_load("mainmenu.shp", 0), 0x31, 0x53, false)
2999
3000	fade_in()
3001
3002	g_menu["subtitle"].visible = true
3003	g_menu["menu"].visible = true
3004
3005	fade_in_sprite(g_menu["menu"])
3006	mouse_cursor_visible(true)
3007end
3008
3009	g_menu_idx = 0
3010
3011local function selected_intro()
3012	mouse_cursor_visible(false)
3013	main_menu_set_pal(0)
3014	fade_out()
3015	canvas_hide_all_sprites()
3016	intro()
3017	music_play("ultima.m")
3018	g_menu["title"].visible = true
3019	fade_in()
3020	g_menu["subtitle"].visible = true
3021	g_menu["menu"].visible = true
3022
3023	fade_in_sprite(g_menu["menu"])
3024	mouse_cursor_visible(true)
3025end
3026
3027local function selected_create_character()
3028	main_menu_set_pal(1)
3029	fade_out_sprite(g_menu["menu"],6)
3030	if create_character() == true then
3031		return true
3032	end
3033	canvas_set_palette("palettes.int", 0)
3034	g_menu_idx=0
3035	main_menu_set_pal(g_menu_idx)
3036	music_play("ultima.m")
3037	fade_in_sprite(g_menu["menu"])
3038	return false
3039end
3040
3041local function selected_acknowledgments()
3042	main_menu_set_pal(3)
3043	fade_out_sprite(g_menu["menu"],6)
3044	acknowledgements()
3045	canvas_set_palette("palettes.int", 0)
3046	g_menu_idx=0
3047	main_menu_set_pal(g_menu_idx)
3048	fade_in_sprite(g_menu["menu"])
3049end
3050
3051local function main_menu()
3052	g_menu["title"].visible = true
3053	g_menu["subtitle"].visible = true
3054	g_menu["menu"].visible = true
3055	canvas_set_update_interval(10)
3056
3057	local input
3058
3059	while true do
3060		canvas_update()
3061		input = input_poll(true)
3062
3063		if engine_should_quit() == 1 then
3064			return "Q"
3065		end
3066
3067		if input ~= nil then
3068			canvas_set_update_interval(25)
3069
3070			if input == 113 then     --q quit
3071				return "Q"
3072			elseif input == 105 or input == 13 and g_menu_idx == 0 then --i
3073				selected_intro()
3074			elseif input == 99 or input == 13 and g_menu_idx == 1 then  --c
3075				if selected_create_character()== true then
3076					return "J"
3077				end
3078			elseif input == 116 or input == 13 and g_menu_idx == 2 then --t
3079				--transfer a character
3080			elseif input == 97 or input == 13 and g_menu_idx == 3 then  --a
3081				selected_acknowledgments()
3082			elseif input == 106 or input == 13 and g_menu_idx == 4 then --j
3083				main_menu_set_pal(4)
3084				fade_out()
3085				return "J"
3086			elseif input == SDLK_DOWN then --down key
3087				if g_menu_idx < 4 then
3088					g_menu_idx = g_menu_idx + 1
3089					main_menu_set_pal(g_menu_idx)
3090				end
3091			elseif input == SDLK_UP then --up key
3092				if g_menu_idx > 0 then
3093					g_menu_idx = g_menu_idx - 1
3094					main_menu_set_pal(g_menu_idx)
3095				end
3096			elseif input >= 48 and input <= 57 or input == 45 or input == 61 then --play music play pressing number keys or '-' or '='
3097				if input == 45 then
3098					input = 11
3099				elseif input == 48 then
3100					input = 10
3101				elseif input == 61 then
3102					input = 12
3103				else
3104					input = input - 48
3105				end
3106
3107				local song_names = {
3108					"ultima.m",
3109					"bootup.m",
3110					"intro.m",
3111					"create.m",
3112					"forest.m",
3113					"hornpipe.m",
3114					"engage.m",
3115					"stones.m",
3116					"dungeon.m",
3117					"brit.m",
3118					"gargoyle.m",
3119					"end.m"
3120				}
3121				music_play(song_names[input])
3122			elseif input == 0 then --mouse click
3123				local x = get_mouse_x()
3124				if x > 56 and x < 264 then
3125					local y = get_mouse_y()
3126					if y > 86 and y < 108 then
3127						selected_intro()
3128					elseif y > 107 and y < 128 then
3129						if selected_create_character() == true then
3130							return "J"
3131						end
3132					elseif y > 127 and y < 149 then
3133						--transfer a character
3134					elseif y > 148 and y < 170 then
3135						selected_acknowledgments()
3136					elseif y > 169 and y < 196 then
3137						main_menu_set_pal(4)
3138						fade_out()
3139						return "J" -- journey onward
3140					end
3141				end
3142			elseif input == 1 then --mouse movement
3143				local x = get_mouse_x()
3144				if x > 56 and x < 264 then
3145					local old_menu_idx = g_menu_idx
3146					local y = get_mouse_y()
3147					if y > 86 and y < 108 then
3148						g_menu_idx = 0
3149					elseif y > 107 and y < 128 then
3150						g_menu_idx = 1
3151					elseif y > 127 and y < 149 then
3152						g_menu_idx = 2
3153					elseif y > 148 and y < 170 then
3154						g_menu_idx = 3
3155					elseif y > 169 and y < 196 then
3156						g_menu_idx = 4
3157					end
3158					if g_menu_idx ~= old_menu_idx then
3159						main_menu_set_pal(g_menu_idx)
3160					end
3161				end
3162			end
3163
3164			input = nil
3165			canvas_set_update_interval(10)
3166		end
3167	end
3168end
3169
3170
3171play()
3172fade_out()
3173
3174g_img_tbl = nil
3175g_clock_tbl = nil
3176g_lounge_tbl = nil
3177g_window_tbl = nil
3178g_stones_tbl = nil
3179
3180canvas_hide_all_sprites()
3181
3182main_menu_load()
3183
3184if main_menu() == "Q" then -- returns "Q" for quit or "J" for Journey Onward
3185	config_set("config/quit", true)
3186end
3187
3188music_stop()
3189canvas_hide_all_sprites()
3190canvas_hide()
3191