1local lua_file = nil
2
3--load common functions
4lua_file = nuvie_load("common/intro_common.lua"); lua_file();
5
6-- TODO - ALL TRANSITIONS - FADE IN/OUT, STAR FADE
7-- TODO - CURSORS
8-- TODO - CONFIRM/CONFIGURE KEYS TO ADVANCE/BYPASS
9-- TODO - MEMORY MANAGEMENT (prevent leaks)
10-- TODO - CREATE NEW CHARACTER OVER EXISTING?
11-- TODO - CHANGE MENU FOR NO CHARACTER YET CREATED? (new install)
12
13local function should_exit(input)
14	if input ~=nil and input == SDLK_ESCAPE then
15		return true
16	end
17
18	return false
19end
20
21local function fade_out_sprite(sprite, speed)
22	local i
23	if speed ~= nil then
24		speed = -speed
25	else
26		speed = -3
27	end
28
29	for i=0xff,0,speed do
30		sprite.opacity = i
31		canvas_update()
32	end
33
34	return false
35end
36
37local function destroy_sprite(sprite)
38-- TODO Correct Memory Management
39	sprite.visible = false
40--	sprite_gc(sprite)
41end
42
43local function destroy_sprites(sprites)
44   local k,v
45   for k,v in pairs(sprites) do
46      destroy_sprite(v)
47   end
48	return false
49end
50
51local function opaque_sprites(sprites, number)
52	for j=0,number-1,1 do
53		-- TODO Opaque = 0x100 or 0xff???
54		sprites[j+1].opacity = 0x100
55		canvas_update()
56	end
57	return false
58end
59
60local function fade_out_sprites(sprites, speed, number)
61	local i
62	if speed ~= nil then
63		speed = -speed
64	else
65		speed = -3
66	end
67
68	for i=0xff,0,speed do
69		for j=0,number-1,1 do
70			sprites[j+1].opacity = i
71			canvas_update()
72		end
73	end
74
75	-- Fully Transparent
76	for j=0,number-1,1 do
77		sprites[j+1].opacity = 0
78		canvas_update()
79	end
80
81	return false
82end
83
84local function fade_in_sprites(sprites, speed, number)
85	local i
86	if speed ~= nil then
87		speed = speed
88	else
89		speed = 3
90	end
91
92	for i=0,0xff,speed do
93		for j=0,number-1,1 do
94			sprites[j+1].opacity = i
95			canvas_update()
96		end
97	end
98
99	-- Fully Opaque
100	for j=0,number-1,1 do
101		-- TODO Opaque = 0x100 or 0xff???
102		sprites[j+1].opacity = 0x100
103		canvas_update()
104	end
105
106	return false
107end
108
109local function origin_fx_sequence()
110	local g_img_tbl = image_load_all("title.lzc")
111
112	canvas_set_palette("savage.pal", 2)
113
114
115	local stars = sprite_new(g_img_tbl[0][0], 0, 24, true)
116	local logo_image = image_new(282,82)
117	image_blit(logo_image, g_img_tbl[0][1],0,16)
118	image_blit(logo_image, g_img_tbl[0][2],g_img_tbl[0][1].w,14)
119	image_blit(logo_image, g_img_tbl[0][3],g_img_tbl[0][1].w+g_img_tbl[0][2].w,0)
120
121	local  logo = sprite_new(logo_image, 20, 70, false)
122
123	local planet = sprite_new(g_img_tbl[12], 160, 48, true)
124	planet.clip_x = 0
125	planet.clip_y = 0
126	planet.clip_w = 320
127	planet.clip_h = 152
128	local players = {}
129	players[1] = create_player_sprite(g_img_tbl[1][0], 58, 118)
130	players[2] = create_player_sprite(g_img_tbl[2][0], 186, 118)
131	players[3] = create_player_sprite(g_img_tbl[3][0], 278, 118)
132
133	players[4] = create_player_sprite(g_img_tbl[4][0], 58, 126)
134	players[5] = create_player_sprite(g_img_tbl[5][0], 186, 126)
135	players[6] = create_player_sprite(g_img_tbl[6][0], 278, 126)
136
137	players[7] = create_player_sprite(g_img_tbl[7][0], 58, 134)
138	players[8] = create_player_sprite(g_img_tbl[8][0], 186, 134)
139	players[9] = create_player_sprite(g_img_tbl[9][0], 278, 134)
140
141	local conductor = sprite_new(g_img_tbl[10][0], 158, 98, true)
142	conductor.clip_x = 0
143	conductor.clip_y = 24
144	conductor.clip_w = 320
145	conductor.clip_h = 128
146
147	fade_in()
148
149	music_play("music.lzc", 19)
150
151	local i = 0
152	for i=0,6,1 do
153		conductor.image = g_img_tbl[10][i]
154
155		update_players(players, g_img_tbl)
156		if poll_for_key_or_button(4) == true then return end
157	end
158	for i=7,13,1 do
159		conductor.image = g_img_tbl[10][i]
160
161		update_players(players, g_img_tbl)
162		if poll_for_key_or_button(4) == true then return end
163	end
164	for i=7,12,1 do
165		conductor.image = g_img_tbl[10][i]
166
167		update_players(players, g_img_tbl)
168		if poll_for_key_or_button(4) == true then return end
169	end
170	local j
171	for i=1,4,1 do
172		for j=13,15,1 do
173			conductor.image = g_img_tbl[10][j]
174			if poll_for_key_or_button(1) == true then return end
175		end
176
177		conductor.image = g_img_tbl[10][14]
178		if poll_for_key_or_button(2) == true then return end
179		conductor.image = g_img_tbl[10][13]
180		if poll_for_key_or_button(2) == true then return end
181		conductor.image = g_img_tbl[10][16]
182
183		if poll_for_key_or_button(1) == true then return end
184			play_sfx(38, false)
185	end
186
187	for i=16,20,1 do
188		conductor.image = g_img_tbl[10][i]
189		if poll_for_key_or_button(4) == true then return end
190	end
191	if poll_for_key_or_button(200) == true then return end
192
193	play_sfx(12, false)
194
195	conductor.image = g_img_tbl[10][6]
196
197	for i=1,21,1 do
198		conductor.y = 98 + i * 12
199		conductor.image.scale = 100 + i * 15
200
201		for j=1,9,1 do
202			players[j].y = players[j].y + 5
203			players[j].image.scale = 100 + i * 5
204			if j == 1 or j == 4 or j == 7 then
205				players[j].x = players[j].x - 2
206			end
207			if j == 3 or j == 6 or j == 9 then
208				players[j].x = players[j].x + 2
209			end
210		end
211
212		if poll_for_esc(4) == true then return end
213	end
214
215
216	logo.visible = true
217	logo.image.scale = 10
218
219
220	for i=1,18,1 do
221		planet.y = planet.y + 6
222
223		logo.image.scale = logo.image.scale + 5
224		logo.x = math.floor((320 - logo.image.w) / 2)
225		if i < 10 then
226			logo.y = logo.y - 4
227		else
228			logo.y = logo.y + 1
229		end
230
231		if poll_for_key_or_button(4) == true then return end
232	end
233
234	fireworks(g_img_tbl, logo)
235end
236
237local function intro_sequence(g_img_tbl)
238
239	canvas_set_palette("savage.pal", 0)
240	music_play("music.lzc", 18)
241
242	local  logo = sprite_new(g_img_tbl[0][0], 0, 0, true)
243	fade_in()
244	if poll_for_key_or_button(175) == true then return end
245	fade_out()
246	logo.image = g_img_tbl[0][1]
247	fade_in()
248	if poll_for_key_or_button(175) == true then return end
249	fade_out()
250
251	canvas_set_palette("savage.pal", 0)
252	local  bg = sprite_new(g_img_tbl[1][0], 0, 0, true) -- Background 0
253	local  bg1 = sprite_new(g_img_tbl[1][1], 320, 0, true) -- Final Section
254	local  bg2 = sprite_new(g_img_tbl[1][2], 0, 0, true) -- Background 1
255	local  bg3 = sprite_new(g_img_tbl[1][3], 320, 0, true) -- Bushes w/ Triceratops
256	local  bg4 = sprite_new(g_img_tbl[1][4], 640, 0, true) -- Left section of Tower
257	local  t1 = sprite_new(g_img_tbl[2][0], 0, 240, true)
258	local  bg5 = sprite_new(g_img_tbl[1][5], 0, 0, true) -- Dinosaurs & Bushes
259	local  bg6 = sprite_new(g_img_tbl[1][6], 320, 0, true) -- Tree w/ Sleeping Dinosaur
260	local  bg7 = sprite_new(g_img_tbl[1][7], 640, 0, true) -- Bushes on sides w/ flower on left
261	canvas_set_opacity(0xff)
262	i = 0
263	while i < 240 do
264	      canvas_update()
265	      t1.y = t1.y - 3
266	      if (t1.y < 0) then t1.y = 0 end
267	      i = i + 3
268	      if poll_for_key_or_button(1) == true then return end
269	end
270	if poll_for_key_or_button(30) == true then return end
271	fade_out_sprite(t1, 4)
272	i = 0
273	current_credit = 1
274	local  credit_sprite = sprite_new(g_img_tbl[2][current_credit], 0, 0, true)
275	credit_time = 15
276	credit_show = 1
277	while i < 636 do
278	      canvas_update()
279	      bg.x = bg.x - 1
280	      bg1.x = bg1.x - 1
281	      bg2.x = bg2.x - 2
282	      bg3.x = bg3.x - 2
283	      bg4.x = bg4.x - 2
284	      bg5.x = bg5.x - 3
285	      bg6.x = bg6.x - 3
286	      bg7.x = bg7.x - 3
287	      i = i + 3
288	      credit_time = credit_time - 1
289	      if (credit_time == 0) then
290	      	 if (credit_show == 1) then
291		    credit_show = 0
292		    credit_sprite.visible = false
293		    credit_time = 20
294		 else
295		    credit_show = 1
296		    current_credit = current_credit + 1
297		    if (current_credit < 7) then
298		       credit_sprite = sprite_new(g_img_tbl[2][current_credit], 0, 0, true)
299		       credit_time = 15
300		    end
301		 end
302	      end
303	      if poll_for_key_or_button(8) == true then return end
304	end
305end
306
307local story_so_far_text = {
308"You had disturbing dreams of a faraway jungle,",
309"an engangered princess, and the black",
310"moonstone you carried away from Britannia in",
311"your last adventure there.",
312"The spirit of Lord British commanded you to",
313"find out all you could about the stone.",
314"",
315"",
316"So you took it to your old friend Dr. Rafkin,",
317"curator of the local museum of natural",
318"history, hoping that he could unravel the",
319"mystery.",
320"At Dr. Rafkin's, you met ace reporter and",
321"\"Ultimate Adventures\" correspondent Jimmy",
322"Malone.  Malone's a little too nosy to suit you...",
323"",
324"...but he kept his wits when everything went",
325"haywire, when Dr. Rafkin's experiments on your",
326"moonstone sent the whole museum lab through",
327"a bizarre black moongate!",
328"You were shocked to lift your head and see",
329"jungle around you: Jungle, and this enormous",
330"creature descending... descending upon--",
331"",
332"Her! The woman you dreamt of! A fiery tribal",
333"she-warrior of some time-lost land... You and",
334"your friends rescued her from the giant",
335"pteranodon and befriended her.",
336"She said her name was Aiela, princess of the",
337"Kurak tribe, and seemed to be quite taken with",
338"you, her most unusual rescuer.",
339"",
340"But Aiela's hated suitor, Darden the Huge,",
341"prince of the Urali tribe, arrived with his men,",
342"scattering your friends and smashing you into",
343"the earth.",
344"Now you lie broken and defeated, struggling to",
345"regain consciousness...",
346"",
347""
348}
349
350local function story_so_far(img_tbl2)
351	local prev_f1 = nil
352	local prev_f2 = nil
353	canvas_set_palette("savage.pal", 1)
354	for i = 0, 9, 1 do
355		-- Need to figure out if I need to get a new version
356		-- of the Top level sprite or not
357		local new_img = image_copy(img_tbl2[5][0])
358		local  f1 = sprite_new(new_img, 0, 0, true)
359		local  f2 = sprite_new(img_tbl2[5][i+1], 120, 45, true)
360		if prev_f1 ~= nil then
361			destroy_sprite(prev_f1)
362			prev_f1 = nil
363		end
364		if prev_f2 ~= nil then
365			destroy_sprite(prev_f2)
366			prev_f2 = nil
367		end
368		prev_f1 = f1;
369		prev_f2 = f2;
370		image_print(new_img, story_so_far_text[i*4+1], 0, 300, 25, 136, 0)
371		image_print(new_img, story_so_far_text[i*4+2], 0, 300, 25, 144, 0)
372		image_print(new_img, story_so_far_text[i*4+3], 0, 300, 25, 152, 0)
373		image_print(new_img, story_so_far_text[i*4+4], 0, 300, 25, 160, 0)
374		wait_for_input()
375	end
376	if prev_f1 ~= nil then
377		destroy_sprite(prev_f1)
378	end
379	if prev_f2 ~= nil then
380		destroy_sprite(prev_f2)
381	end
382	canvas_set_palette("savage.pal", 0)
383end
384
385local function shaman_drop_anim(sprites, color)
386	-- TODO - TIMING
387	-- Arm Anim 4-9
388	-- Drop Anim 10-18
389	local prev_shaman = nil
390	for i = 4,9,1 do
391		sprites[i].visible = true
392		if prev_shaman ~= nil then
393			prev_shaman.visible = false
394		end
395		prev_shaman = sprites[i]
396		canvas_update()
397	end
398	local prev_drop = nil
399	for i = 10,26,1 do
400		sprites[27+color].visible = true
401		sprites[i].visible = true
402		if prev_drop ~= nil then
403			prev_drop.visible = false
404		end
405		prev_drop = sprites[i]
406		canvas_update()
407	end
408	sprites[27+color].visible = false
409	prev_drop.visible = false
410	for i = 8,4,-1 do
411		sprites[i].visible = true
412		if prev_shaman ~= nil then
413			prev_shaman.visible = false
414		end
415		prev_shaman = sprites[i]
416		canvas_update()
417	end
418	prev_shaman.visible = false
419	canvas_update()
420end
421
422-- Organization of questions (Red, Yellow, Blue)
423-- Int (Red) vs Str (Yellow), Int (Red) vs Dex (Blue), Str (Yellow) vs. Dex (Blue)
424-- 4 Lines for Each Question (nil on final question means no first screen)
425-- Two sets of questions for each type, reversing answer order (Red vs. Yellow, then Yellow vs. Red)
426local create_questions_text = {
427"Your chief has told you to guard the village while a battle rages nearby. Your chief fights in the battle",
428"Will you (a) obey the word of your chief, or (b) join your fellow warriors in the fight?",
429nil,
430nil,
431"You fight a warrior you hate, and knock his spear from his hands.",
432"Another blow and he will be dead.",
433"Will you (a) let him surrender, and spare him if he does, or (b) slay him where he stands?",
434nil,
435"One warrior borrows another's spear and fails to return it. Days later, he mislays his own spear and you find it.",
436"Do you (a) return it to him, or (b) give it to the warrior who is owed the spear?",
437nil,
438nil,
439"You have sworn to protect your chief at any cost. Yet you see him foully murder a warrior of his own tribe.",
440"The murdered man's brother asks you what you saw.",
441"Will you (a) keep your oath and protect your king, or (b) tell the truth of the matter?",
442nil,
443"A huge, powerful warrior stands against you and demands you give him your food.",
444"Will you (a) throw his demand in his teeth and attack him, or (b) give him your food, since it is clear he is hungry?",
445nil,
446nil,
447"Your chief gives you a pouch of gems to take to another; he has not counted them.",
448"On the path to the other village, you find a lamed warrior who cannot hunt.",
449"He could trade you a few of your gems for food, and they will not be missed.",
450"Will you (a) deliver the gems as you were charged, or (b) give the warrior a gem?",
451}
452
453local create_questions_A_tbl = {
4542,
4551,
4561,
4570,
4580,
4592
460}
461
462local create_questions_B_tbl = {
4630,
4640,
4652,
4662,
4671,
4681
469}
470
471local function process_answer(q_num, input, sprites)
472	local stat_type
473	if input == SDLK_a then
474		stat_type = create_questions_A_tbl[q_num]
475	else
476		stat_type = create_questions_B_tbl[q_num]
477	end
478
479	g_stats[stat_type] = g_stats[stat_type] + 3 + math.random(0, 2)
480
481	local color = 0 -- stat_type == 2
482	if stat_type == 0 then color = 1 end
483	if stat_type == 1 then color = 2 end
484
485	shaman_drop_anim(sprites, color)
486	return stat_type
487end
488
489local function ask_question(q_num, q_ord, sprites, text_image, a_b_border_img, images, text_width)
490	-- Do I need to display two pages?
491	local q_index = q_num -- * 2 + q_ord
492	-- Do I need to display two pages?
493	local two_questions = false
494	if create_questions_text[q_index*4+3+1] ~= nil then two_questions = true end
495	local txt_ind = 1
496	local x,y
497	if (two_questions) then
498		image_blit(text_image, images[4][2], 0, 0)
499		x, y = image_print(text_image, create_questions_text[q_index*4+txt_ind], 8, text_width, 8, 10, 0x00)
500		x, y = image_print(text_image, create_questions_text[q_index*4+txt_ind+1], 8, text_width, 8, y+20, 0x00)
501		canvas_update()
502		wait_for_input()
503		txt_ind = 3
504	end
505	image_blit(text_image, images[4][2], 0, 0)
506	x, y = image_print(text_image, create_questions_text[q_index*4+txt_ind], 8, text_width, 8, 10, 0x00)
507	x, y = image_print(text_image, create_questions_text[q_index*4+txt_ind+1], 8, text_width, 8, y+20, 0x00)
508	if txt_ind ~= 3 then
509		if create_questions_text[q_index*4+txt_ind+2] ~= nil then
510			x, y = image_print(text_image, create_questions_text[q_index*4+txt_ind+2], 8, text_width, 8, y+20, 0x00)
511		end
512	end
513
514	local red = 0x04
515	local black = 0x00
516	local answer = SDLK_a
517	local old_answer = 0
518	local stat_type = -1
519	local need_to_process_answer = false
520	local a_sprite = sprite_new(nil, 56 + 170, 129 + 45, true)
521	local b_sprite = sprite_new(nil, 72 + 170, 129 + 45, true)
522	a_sprite.text = "a"
523	b_sprite.text = "b"
524	a_sprite.text_color = red
525	b_sprite.text_color = black
526
527	while stat_type == -1 do
528		canvas_update()
529		input = input_poll(true)
530		if input ~= nil then
531			if input == SDLK_a or input == SDLK_b then
532				answer = input
533				need_to_process_answer = true
534			elseif input == SDLK_LEFT or input == SDLK_KP4 then
535				answer = SDLK_a
536			elseif input == SDLK_RIGHT or input == SDLK_KP6 then
537				answer = SDLK_b
538			elseif input == SDLK_KP_ENTER or input == SDLK_RETURN or input == SDLK_SPACE then
539				need_to_process_answer = true
540			elseif input == MOUSE_CLICK or input == MOUSE_MOTION then
541				local mouse_y = get_mouse_y()
542				if mouse_y >= b_sprite.y and mouse_y <= b_sprite.y + 7 then
543					local mouse_x = get_mouse_x()
544					if mouse_x > a_sprite.x and mouse_x < a_sprite.x + 7 then
545						answer = SDLK_a
546						if input == MOUSE_CLICK then
547							need_to_process_answer = true
548						end
549					elseif mouse_x > b_sprite.x and mouse_x < b_sprite.x + 6 then
550						answer = SDLK_b
551						if input == MOUSE_CLICK then
552							need_to_process_answer = true
553						end
554					end
555				end
556			end
557			if answer ~= old_answer then
558				old_answer = answer
559				if answer == SDLK_a then
560					a_sprite.text_color = red
561					b_sprite.text_color = black
562				else
563					a_sprite.text_color = black
564					b_sprite.text_color = red
565				end
566			end
567			if need_to_process_answer then
568				stat_type = process_answer(q_index+1, answer, sprites)
569			end
570		end
571	end
572
573	destroy_sprite(a_sprite)
574	destroy_sprite(b_sprite)
575
576	local next_q
577	if stat_type == 0 then
578		if q_ord == 0 then
579			next_q = 2
580		elseif q_num == 0 then
581			next_q = 4
582		else
583			next_q = 3
584		end
585	elseif stat_type == 1 then
586		if q_ord == 0 then
587			next_q = 0
588		elseif q_num == 2 then
589			next_q = 4
590		else
591			next_q = 5
592		end
593	elseif stat_type == 2 then
594		if q_ord == 0 then
595			next_q = 1
596		elseif q_num == 0 then
597			next_q = 5
598		else
599			next_q = 3
600		end
601	end
602
603	return next_q
604end
605
606
607
608g_keycode_tbl =
609{
610[32]=" ",
611[39]="'",
612[44]=",",
613[45]="-",
614[46]=".",
615[48]="0",
616[49]="1",
617[50]="2",
618[51]="3",
619[52]="4",
620[53]="5",
621[54]="6",
622[55]="7",
623[56]="8",
624[57]="9",
625[65]="A",
626[66]="B",
627[67]="C",
628[68]="D",
629[69]="E",
630[70]="F",
631[71]="G",
632[72]="H",
633[73]="I",
634[74]="J",
635[75]="K",
636[76]="L",
637[77]="M",
638[78]="N",
639[79]="O",
640[80]="P",
641[81]="Q",
642[82]="R",
643[83]="S",
644[84]="T",
645[85]="U",
646[86]="V",
647[87]="W",
648[88]="X",
649[89]="Y",
650[90]="Z",
651
652[97]="a",
653[98]="b",
654[99]="c",
655[100]="d",
656[101]="e",
657[102]="f",
658[103]="g",
659[104]="h",
660[105]="i",
661[106]="j",
662[107]="k",
663[108]="l",
664[109]="m",
665[110]="n",
666[111]="o",
667[112]="p",
668[113]="q",
669[114]="r",
670[115]="s",
671[116]="t",
672[117]="u",
673[118]="v",
674[119]="w",
675[120]="x",
676[121]="y",
677[122]="z",
678}
679
680	g_stats = {}	-- Int/Str/Dex
681
682local function create_new_character(img_tbl2)
683	local input
684	canvas_set_palette("savage.pal", 1)
685	local create_char_sprites = {}
686	for i=0,2,1 do
687		g_stats[i] = 16 + math.random(0,2)
688	end
689	create_char_sprites[1] = sprite_new(img_tbl2[1][2], 0, 0, true) -- Full Screen Border Around Shaman
690	create_char_sprites[2] = sprite_new(img_tbl2[1][0], 8, 28, true) -- Create Character Shaman
691	create_char_sprites[4] = sprite_new(img_tbl2[2][0], 8, 28, false) -- Shaman Arm Anim1
692	create_char_sprites[5] = sprite_new(img_tbl2[2][1], 8, 28, false) -- Shaman Arm Anim2
693	create_char_sprites[6] = sprite_new(img_tbl2[2][2], 8, 28, false) -- Shaman Arm Anim3
694	create_char_sprites[7] = sprite_new(img_tbl2[2][3], 8, 28, false) -- Shaman Arm Anim4
695	create_char_sprites[8] = sprite_new(img_tbl2[2][4], 8, 28, false) -- Shaman Arm Anim5
696	create_char_sprites[9] = sprite_new(img_tbl2[2][5], 8, 28, false) -- Shaman Arm Anim6
697	create_char_sprites[27] = sprite_new(img_tbl2[3][17], 8, 28, false) -- Red
698	create_char_sprites[28] = sprite_new(img_tbl2[3][18], 8, 28, false) -- Yellow
699	create_char_sprites[29] = sprite_new(img_tbl2[3][19], 8, 28, false) -- Blue
700	create_char_sprites[10] = sprite_new(img_tbl2[3][0], 8, 28, false) -- Drop Anim
701	create_char_sprites[11] = sprite_new(img_tbl2[3][1], 8, 28, false) --
702	create_char_sprites[12] = sprite_new(img_tbl2[3][2], 8, 28, false) --
703	create_char_sprites[13] = sprite_new(img_tbl2[3][3], 8, 28, false) --
704	create_char_sprites[14] = sprite_new(img_tbl2[3][4], 8, 28, false) --
705	create_char_sprites[15] = sprite_new(img_tbl2[3][5], 8, 28, false) --
706	create_char_sprites[16] = sprite_new(img_tbl2[3][6], 8, 28, false) --
707	create_char_sprites[17] = sprite_new(img_tbl2[3][7], 8, 28, false) --
708	create_char_sprites[18] = sprite_new(img_tbl2[3][8], 8, 28, false) --
709	create_char_sprites[19] = sprite_new(img_tbl2[3][9], 8, 28, false) --
710	create_char_sprites[20] = sprite_new(img_tbl2[3][10], 8, 28, false) --
711	create_char_sprites[21] = sprite_new(img_tbl2[3][11], 8, 28, false) --
712	create_char_sprites[22] = sprite_new(img_tbl2[3][12], 8, 28, false) --
713	create_char_sprites[23] = sprite_new(img_tbl2[3][13], 8, 28, false) --
714	create_char_sprites[24] = sprite_new(img_tbl2[3][14], 8, 28, false) --
715	create_char_sprites[25] = sprite_new(img_tbl2[3][15], 8, 28, false) --
716	create_char_sprites[26] = sprite_new(img_tbl2[3][16], 8, 28, false) --
717	create_char_sprites[3] = sprite_new(img_tbl2[1][1], 8, 28, true) -- Weird Border Around Shaman
718	-- TODO BORDER SHADOW (Solid Black)
719	-- TODO Memory Management on Image?
720	-- Intro Screen1
721	local scroll_img = image_copy(img_tbl2[4][2])
722	local a_b_border = image_copy(img_tbl2[4][2])
723	local text_width = 130
724	create_char_sprites[32] = sprite_new(a_b_border, 170, 45, false) -- Border for a and b answers (overlapped by next border)
725	create_char_sprites[30] = sprite_new(scroll_img, 170, 25, true) -- Border
726	local x, y = image_print(scroll_img, "You are in a dirt-floored hut... and a tribesman with wise eyes stares down at you.", 8, text_width, 8, 10, 0x00)
727	x, y = image_print(scroll_img, "You feel as though you are floating. You cannot move your limbs. It is like a dream... yet you sense it is not a dream.", 8, text_width, 8, y+20, 0x00)
728	canvas_update()
729	wait_for_input()
730	-- Intro Screen2
731	image_blit(scroll_img, img_tbl2[4][2], 0, 0)
732	local x, y = image_print(scroll_img, "The man speaks: \"Yes, warrior, you can hear. You see Intanya, shaman and healer. Intanya will heal you... but to concoct his healing potion, he must ask you questions.\"", 8, text_width, 8, 10, 0x00)
733	canvas_update()
734	wait_for_input()
735	-- Intro Screen3
736	image_blit(scroll_img, img_tbl2[4][2], 0, 0)
737	x, y = image_print(scroll_img, "\"In order to heal your spirit, Intanya must know your spirit, so you must answer with truthful words.\"", 8, text_width, 8, 10, 0x00)
738	x, y = image_print(scroll_img, "\"Intanya will speak of deeds and choices; you must answer with the choices you would make.\"", 8, text_width, 8, y+20, 0x00)
739	canvas_update()
740	wait_for_input()
741	-- Name Input
742	image_blit(scroll_img, img_tbl2[4][2], 0, 0)
743	x, y = image_print(scroll_img, "\"First, though, Intanya must know the warrior's name, for there is much power in names.", 8, text_width, 8, 10, 0x00)
744	x, y = image_print(scroll_img, "\"What does the warrior call himself?\"", 8, text_width, 8, y+20, 0x00)
745	canvas_update()
746	local name = sprite_new(nil, 178, 25+y+10, true)
747	create_char_sprites[31] = name
748	local num_sprites = 32
749	name.text = ""
750	name.text_color = 0x00
751	local char_index = 0
752	while input == nil do
753		canvas_update()
754		input = input_poll()
755		if input ~= nil then
756			if should_exit(input) == true then
757				destroy_sprites(create_char_sprites)
758				canvas_set_palette("savage.pal", 0)
759				return false -- back to main menu
760			end
761			local name_text = name.text
762			local len = string.len(name_text)
763			if (input == SDLK_BACKSPACE or input == SDLK_LEFT) and len > 0 then
764				name.text = string.sub(name_text, 1, len - 1)
765				if len == 1 then -- old len
766					char_index = 0
767				else
768					char_index = string.byte(name_text, len -1)
769				end
770			elseif (input == SDLK_RETURN or input == SDLK_KP_ENTER) and len > 0 then --return
771				break;
772			elseif g_keycode_tbl[input] ~= nil and len < 13 then
773				char_index = input
774				name.text = name_text..g_keycode_tbl[input]
775			elseif input == SDLK_UP then --up
776				if char_index == 0 then
777					if len > 0 then
778						char_index = SDLK_a
779					else
780						char_index = 65 --A
781					end
782				elseif char_index == 32 then --gap in characters
783					char_index = 39
784				elseif char_index == 39 then --gap in characters
785					char_index = 44
786				elseif char_index == 46 then --gap in characters
787					char_index = 48
788				elseif char_index == 57 then --gap in characters
789					char_index = 65
790				elseif char_index == 90 then --gap in characters
791					char_index = 97
792				elseif char_index == 122 then --last char
793					char_index = 32
794				else
795					char_index = char_index + 1
796				end
797
798				if len > 0 then -- erase char
799					name_text = string.sub(name_text, 1, len - 1)
800				end
801				name.text = name_text..g_keycode_tbl[char_index]
802			elseif input == SDLK_DOWN then --down
803				if char_index == 0 then
804					if len > 0 then
805						char_index = 122 --z
806					else
807						char_index = 90 --Z
808					end
809				elseif char_index == 39 then --gap in characters
810					char_index = 32
811				elseif char_index == 44 then --gap in characters
812					char_index = 39
813				elseif char_index == 48 then --gap in characters
814					char_index = 46
815				elseif char_index == 65 then --gap in characters
816					char_index = 57
817				elseif char_index == 97 then --gap in characters
818					char_index = 90
819				elseif char_index == 32 then --first char
820					char_index = 122
821				else
822					char_index = char_index - 1
823				end
824
825				if len > 0 then -- erase char
826					name_text = string.sub(name_text, 1, len - 1)
827				end
828				name.text = name_text..g_keycode_tbl[char_index]
829			elseif input == SDLK_RIGHT and len < 13 then --right
830				char_index = SDLK_a --a
831				name.text = name_text.."a"
832			end
833			input = nil
834		end
835	end
836
837--	name.x = 0x10 + (284 - canvas_string_length(name.text)) / 2
838	name.visible = false
839
840	-- Questions
841	local next_q = math.random(0,2)
842
843	mouse_cursor_visible(true)
844	create_char_sprites[32].visible = true -- a b border
845	-- Answers are 0-Red, 1-Yellow, 2-Blue (order of sprites in file)
846	next_q = ask_question(next_q, 0, create_char_sprites, scroll_img, a_b_border, img_tbl2, text_width)
847	next_q = ask_question(next_q, 1, create_char_sprites, scroll_img, a_b_border, img_tbl2, text_width)
848	ask_question(next_q, 2, create_char_sprites, scroll_img, a_b_border, img_tbl2, text_width)
849
850	config_set("config/newgame", true)
851	config_set("config/newgamedata/name", name.text)
852	config_set("config/newgamedata/str", g_stats[0])
853	config_set("config/newgamedata/dex", g_stats[1])
854	config_set("config/newgamedata/int", g_stats[2])
855
856	--io.stderr:write("str "..g_stats[0].." dex "..g_stats[1].." int "..g_stats[2].."\n")
857
858	destroy_sprites(create_char_sprites)
859	canvas_set_palette("savage.pal", 0)
860	return true -- Journey onward
861end
862
863-- Could/should get heights from sprite, but was getting error when trying
864local credit_heights = {
865      182, 157, 100, 188, 136, 193, 183, 176, 97, 190, 190, 50
866}
867
868function create_about_sprite(image, x, y)
869   local sprite = sprite_new(image, x, y, true)
870   sprite.clip_x = 0
871   sprite.clip_y = 100
872   sprite.clip_w = 320
873   sprite.clip_h = 100
874   return sprite
875end
876
877local function about_the_savage_empire(img_tbl2)
878	local ypos = 200
879	local about_sprites = {}
880
881	local speed = 1
882	local i
883	for i=0,11 do
884	  table.insert(about_sprites, create_about_sprite(img_tbl2[6][i],   100, ypos))
885	  ypos = ypos + credit_heights[i+1]
886	end
887
888	local done = 0
889
890   local k,v
891	while done < ypos do
892
893		for k,v in pairs(about_sprites) do
894		 v.y = v.y - speed
895		end
896
897		done = done + speed
898		if poll_for_key_or_button(1) == true then
899			break
900		end
901	end
902
903	destroy_sprites(about_sprites)
904end
905
906	g_menu_idx = 0
907	old_g_menu_idx = 0
908	g_menu1 = nil -- The Story So far...
909	g_menu2 = nil -- Create New Character
910	g_menu3 = nil -- About the Savage Empire
911	g_menu4 = nil -- Journey Onward
912	g_m1_focus = nil -- The Story So far...
913	g_m2_focus = nil -- Create New Character
914	g_m3_focus = nil -- About the Savage Empire
915	g_m4_focus = nil -- Journey Onward
916	g_m1_highlight = nil -- The Story So far...
917	g_m2_highlight = nil -- Create New Character
918	g_m3_highlight = nil -- About the Savage Empire
919	g_m4_highlight = nil -- Journey Onward
920	g_menu_sprites = {}
921
922local function selected_story_so_far(img_tbl2)
923	mouse_cursor_visible(false)
924	g_menu1.visible = false
925	g_m1_focus.visible = false
926	g_m1_highlight.visible = true
927	canvas_update()
928	story_so_far(img_tbl2)
929	g_menu1.visible = true
930	g_m1_focus.visible = true
931	g_m1_highlight.visible = false
932	mouse_cursor_visible(true)
933end
934
935local function selected_create_character(img_tbl2)
936	mouse_cursor_visible(false)
937	local start_new_game = create_new_character(img_tbl2)
938	mouse_cursor_visible(true)
939	return start_new_game
940end
941
942local function selected_about_the_savage_empire(img_tbl2)
943	mouse_cursor_visible(false)
944	g_menu3.visible = false
945	g_m3_focus.visible = false
946	g_m3_highlight.visible = true
947	canvas_update()
948	fade_out_sprites(g_menu_sprites, 50, 12)
949	about_the_savage_empire(img_tbl2)
950	g_menu3.visible = true
951	g_m3_focus.visible = true
952	g_m3_highlight.visible = false
953	fade_in_sprites(g_menu_sprites, 50, 12)
954	mouse_cursor_visible(true)
955end
956
957local function journey_onward()
958	mouse_cursor_visible(false)
959	g_menu4.visible = false
960	g_m4_focus.visible = false
961	g_m4_highlight.visible = true
962	canvas_update()
963end
964
965local function initialize_main_g_menu_sprites(img_tbl2)
966	g_menu1 = sprite_new(img_tbl2[0][5], 85, 117, true) -- The Story So far...
967	g_menu2 = sprite_new(img_tbl2[0][1], 85, 135, true) -- Create New Character
968	g_menu3 = sprite_new(img_tbl2[0][9], 85, 153, true) -- About the Savage Empire
969	g_menu4 = sprite_new(img_tbl2[0][3], 85, 171, true) -- Journey Onward
970	g_m1_focus = sprite_new(img_tbl2[0][6], 85, 117, true) -- The Story So far...
971	g_m2_focus = sprite_new(img_tbl2[0][2], 85, 135, false) -- Create New Character
972	g_m3_focus = sprite_new(img_tbl2[0][10], 85, 153, false) -- About the Savage Empire
973	g_m4_focus = sprite_new(img_tbl2[0][4], 85, 171, false) -- Journey Onward
974	g_m1_highlight = sprite_new(img_tbl2[0][13], 85, 117, false) -- The Story So far...
975	g_m2_highlight = sprite_new(img_tbl2[0][11], 85, 135, false) -- Create New Character
976	g_m3_highlight = sprite_new(img_tbl2[0][15], 85, 153, false) -- About the Savage Empire
977	g_m4_highlight = sprite_new(img_tbl2[0][12], 85, 171, false) -- Journey Onward
978	g_menu_sprites[1] = g_menu1
979	g_menu_sprites[2] = g_menu2
980	g_menu_sprites[3] = g_menu3
981	g_menu_sprites[4] = g_menu4
982	g_menu_sprites[5] = g_m1_highlight
983	g_menu_sprites[6] = g_m2_highlight
984	g_menu_sprites[7] = g_m3_highlight
985	g_menu_sprites[8] = g_m4_highlight
986	g_menu_sprites[9] = g_m1_focus
987	g_menu_sprites[10] = g_m2_focus
988	g_menu_sprites[11] = g_m3_focus
989	g_menu_sprites[12] = g_m4_focus
990
991	g_m2_focus.visible = false
992	g_m3_focus.visible = false
993	g_m4_focus.visible = false
994end
995
996local function set_main_menu_highlight()
997	if g_menu_idx == old_g_menu_idx then -- no change
998		return
999	end
1000	g_menu_sprites[g_menu_idx + 9].visible = true
1001	g_menu_sprites[old_g_menu_idx + 9].visible = false
1002	old_g_menu_idx = g_menu_idx
1003end
1004
1005local function main_menu(img_tbl2)
1006	local input
1007	local seTitle = sprite_new(img_tbl2[0][0], 0, 0, true)
1008	mouse_cursor_visible(true)
1009	initialize_main_g_menu_sprites(img_tbl2)
1010   canvas_set_update_interval(10)
1011
1012	while true do
1013		canvas_update()
1014		input = input_poll(true)
1015
1016		if engine_should_quit() == 1 then
1017			return "Q"
1018		end
1019
1020		if input ~= nil then
1021			canvas_set_update_interval(25)
1022
1023			if input == SDLK_q then -- q
1024				return "Q"
1025			elseif input == SDLK_RETURN or input == SDLK_SPACE or input == KP_ENTER then -- space or return
1026				if g_menu_idx == 0 then -- story so far
1027					selected_story_so_far(img_tbl2)
1028				elseif g_menu_idx == 1 then -- create char
1029					if selected_create_character(img_tbl2) == true then
1030						return "J" -- starting new game
1031					end
1032				elseif g_menu_idx == 2 then -- about SE
1033					selected_about_the_savage_empire(img_tbl2)
1034				elseif g_menu_idx == 3 then -- journey onward
1035					journey_onward()
1036					return "J"
1037				end
1038			elseif input == SDLK_s or input == SDLK_t then -- s or t (story so far)
1039				g_menu_idx = 0
1040				set_main_menu_highlight()
1041				selected_story_so_far(img_tbl2)
1042			elseif input == SDLK_c then -- c (create char)
1043				g_menu_idx = 1
1044				set_main_menu_highlight()
1045				if selected_create_character(img_tbl2) == true then
1046					return "J" -- starting new game
1047				end
1048			elseif input == SDLK_a then -- a (about SE)
1049				g_menu_idx = 2
1050				set_main_menu_highlight()
1051				selected_about_the_savage_empire(img_tbl2)
1052			elseif input == SDLK_j then -- j (journey onward)
1053				g_menu_idx = 3
1054				set_main_menu_highlight()
1055				journey_onward()
1056				return "J"
1057			elseif input == SDLK_DOWN or input == SDL_KP2 then -- down key
1058				g_menu_idx = g_menu_idx + 1
1059				if (g_menu_idx == 4) then g_menu_idx = 0 end
1060				set_main_menu_highlight()
1061			elseif input == SDLK_UP or input == SDL_KP8 then -- up key
1062				g_menu_idx = g_menu_idx - 1
1063				if (g_menu_idx == -1) then g_menu_idx = 3 end
1064				set_main_menu_highlight()
1065			elseif input == MOUSE_CLICK then --mouse click
1066				local x = get_mouse_x()
1067				if x > 84 and x < 242 then
1068					local y = get_mouse_y()
1069					if y > 118 and y < 134 then -- story so far
1070						selected_story_so_far(img_tbl2)
1071					elseif y > 133 and y < 152 then -- create new char
1072						if selected_create_character(img_tbl2) == true then
1073							return "J" -- starting new game
1074						end
1075					elseif y > 151 and y < 170 then -- about SE
1076						selected_about_the_savage_empire(img_tbl2)
1077					elseif y > 169 and y < 185 then -- journey onward
1078						journey_onward()
1079						return "J"
1080					end
1081				end
1082			elseif input == MOUSE_MOTION then --mouse movement
1083				local x = get_mouse_x()
1084				if x > 84 and x < 242 then
1085					local y = get_mouse_y()
1086					if y > 118 and y < 134 then -- story so far
1087						g_menu_idx = 0
1088					elseif y > 133 and y < 152 then -- create new char
1089						g_menu_idx = 1
1090					elseif y > 151 and y < 170 then -- about SE
1091						g_menu_idx = 2
1092					elseif y > 169 and y < 185 then -- journey onward
1093						g_menu_idx = 3
1094					end
1095					set_main_menu_highlight()
1096				end
1097			end
1098
1099			canvas_set_update_interval(10)
1100		end
1101	end
1102--[[		This code should never execute because there is no break in the loop
1103	wait_for_input()
1104	g_menu1.visible = false
1105	g_menu2.visible = false
1106	g_menu3.visible = false
1107	g_menu4.visible = false]]--
1108--	canvas_set_palette("savage.pal", 1)
1109--	local  e4 = sprite_new(img_tbl2[4][3], 0, 0, true) -- Small Head/Eyes
1110--	local  e5 = sprite_new(img_tbl2[4][4], 50, 0, true) -- Truth
1111--	local  e6 = sprite_new(img_tbl2[4][5], 100, 0, true) -- Small Head/Eyes
1112--	local  e7 = sprite_new(img_tbl2[4][6], 150, 0, true) -- Love
1113--	local  e8 = sprite_new(img_tbl2[4][7], 100, 0, true) -- Small Head/Eyes
1114--	local  e9 = sprite_new(img_tbl2[4][8], 100, 0, true) -- Full Top
1115--	local  e10 = sprite_new(img_tbl2[4][9], 100, 0, true) -- Full Top2
1116--	local  h1 = sprite_new(img_tbl2[7][0], 0, 0, true) -- Small Top1 (Correct Palette?)
1117--	local  h2 = sprite_new(img_tbl2[7][1], 50, 50, true) -- Small Top2
1118end
1119
1120mouse_cursor_visible(false)
1121canvas_set_update_interval(25)
1122canvas_set_bg_color(0)
1123canvas_set_opacity(0)
1124
1125origin_fx_sequence()
1126
1127--canvas_hide_all_sprites()
1128
1129-- Load Graphics for Intro & Main Menu
1130local g_img_tbl = image_load_all("intro.lzc")
1131local img_tbl2 = image_load_all("create.lzc")
1132intro_sequence(g_img_tbl)
1133
1134if main_menu(img_tbl2) == "Q" then -- returns "Q" for quit or "J" for Journey Onward
1135   fade_out(6)
1136	config_set("config/quit", true)
1137end
1138
1139music_stop()
1140canvas_hide_all_sprites()
1141canvas_hide()
1142