1local lua_file = nil
2
3--load common functions
4lua_file = nuvie_load("common/intro_common.lua"); lua_file();
5
6local FREUD_STATE_STARING  = 0
7local FREUD_STATE_WRITING  = 1
8
9local function origin_fx_sequence()
10   local g_img_tbl = image_load_all("title.lzc")
11
12   canvas_set_palette("strax.pal", 0)
13
14
15   local stars = sprite_new(g_img_tbl[0][0], 0, 24, true)
16   local logo_image = image_new(282,82)
17   image_blit(logo_image, g_img_tbl[0][1],0,16)
18   image_blit(logo_image, g_img_tbl[0][2],g_img_tbl[0][1].w,14)
19   image_blit(logo_image, g_img_tbl[0][3],g_img_tbl[0][1].w+g_img_tbl[0][2].w,0)
20
21   local  logo = sprite_new(logo_image, 20, 70, false)
22
23   local planet = sprite_new(g_img_tbl[12], 160, 48, true)
24
25   planet.clip_x = 0
26   planet.clip_y = 0
27   planet.clip_w = 320
28   planet.clip_h = 152
29   local players = {}
30   players[1] = create_player_sprite(g_img_tbl[1][0], 58, 118)
31   players[2] = create_player_sprite(g_img_tbl[2][0], 186, 118)
32   players[3] = create_player_sprite(g_img_tbl[3][0], 278, 118)
33
34   players[4] = create_player_sprite(g_img_tbl[4][0], 58, 126)
35   players[5] = create_player_sprite(g_img_tbl[5][0], 186, 126)
36   players[6] = create_player_sprite(g_img_tbl[6][0], 278, 126)
37
38   players[7] = create_player_sprite(g_img_tbl[7][0], 58, 134)
39   players[8] = create_player_sprite(g_img_tbl[8][0], 186, 134)
40   players[9] = create_player_sprite(g_img_tbl[9][0], 278, 134)
41
42   local conductor = sprite_new(g_img_tbl[10][0], 158, 98, true)
43   conductor.clip_x = 0
44   conductor.clip_y = 24
45   conductor.clip_w = 320
46   conductor.clip_h = 128
47
48   music_play("strx_mus.lzc", 0)
49
50   fade_in()
51
52   local i = 0
53   for i=0,6,1 do
54      conductor.image = g_img_tbl[10][i]
55
56      update_players(players, g_img_tbl)
57      if poll_for_key_or_button(4) == true then return end
58   end
59   for i=7,13,1 do
60      conductor.image = g_img_tbl[10][i]
61
62      update_players(players, g_img_tbl)
63      if poll_for_key_or_button(4) == true then return end
64   end
65   for i=7,12,1 do
66      conductor.image = g_img_tbl[10][i]
67
68      update_players(players, g_img_tbl)
69      if poll_for_key_or_button(4) == true then return end
70   end
71   local j
72   for i=1,4,1 do
73      for j=13,15,1 do
74         conductor.image = g_img_tbl[10][j]
75         if poll_for_key_or_button(1) == true then return end
76      end
77
78      conductor.image = g_img_tbl[10][14]
79      if poll_for_key_or_button(2) == true then return end
80      conductor.image = g_img_tbl[10][13]
81      if poll_for_key_or_button(2) == true then return end
82      conductor.image = g_img_tbl[10][16]
83
84      if poll_for_key_or_button(1) == true then return end
85         play_sfx(38, false)
86   end
87
88   for i=16,20,1 do
89      conductor.image = g_img_tbl[10][i]
90      if poll_for_key_or_button(4) == true then return end
91   end
92   if poll_for_key_or_button(135) == true then return end
93
94   --play_sfx(12, false)
95
96   conductor.image = g_img_tbl[10][6]
97
98   for i=1,21,1 do
99      conductor.y = 98 + i * 12
100      conductor.image.scale = 100 + i * 15
101
102      for j=1,9,1 do
103         players[j].y = players[j].y + 5
104         players[j].image.scale = 100 + i * 5
105         if j == 1 or j == 4 or j == 7 then
106            players[j].x = players[j].x - 2
107         end
108         if j == 3 or j == 6 or j == 9 then
109            players[j].x = players[j].x + 2
110         end
111      end
112
113      if poll_for_esc(4) == true then return end
114   end
115
116
117   logo.visible = true
118   logo.image.scale = 10
119
120
121   for i=1,18,1 do
122      planet.y = planet.y + 6
123
124      logo.image.scale = logo.image.scale + 5
125      logo.x = math.floor((320 - logo.image.w) / 2)
126      if i < 10 then
127         logo.y = logo.y - 4
128      else
129         logo.y = logo.y + 1
130      end
131
132      if poll_for_key_or_button(4) == true then return end
133   end
134
135   if poll_for_esc(45) == true then return end
136
137   fireworks(g_img_tbl, logo)
138   fade_out()
139end
140
141function show_logos()
142   local g_img_tbl = image_load_all("logos.lzc")
143   local sprite = sprite_new(g_img_tbl[0], 0, 0, true)
144   music_play("mdd_mus.lzc", 0)
145   fade_in()
146
147   poll_for_key_or_button(195)
148   if should_exit() then return end
149   --fade_out()
150   sprite.image = g_img_tbl[1]
151   fade_in()
152   poll_for_key_or_button(195)
153   if should_exit() then return end
154
155   sprite.image = g_img_tbl[2]
156   sprite.x = 15
157   music_play("mdd_mus.lzc", 1)
158   fade_in()
159   poll_for_key_or_button(175)
160   sprite.visible = false
161end
162
163function flash_effect(image, text_delay)
164   if text_delay == nil then
165      text_delay = 300
166   end
167
168   canvas_hide_all_sprites()
169
170   local s = sprite_new(image, 0, 0, true)
171   local bg = sprite_new(image_new(320,200,15), 0, 0, true)
172
173   if poll_for_key_or_button(6) == true then return end
174
175   local speed = 10
176   local i
177   for i=0x0,0xff,speed do
178      bg.opacity = 0xff - i
179      canvas_update()
180   end
181
182   return poll_for_key_or_button(text_delay)
183end
184
185function show_fair_ground()
186   local g_img_tbl = image_load_all("fair.lzc")
187
188   canvas_hide_all_sprites()
189
190   local fair_bg = sprite_new(g_img_tbl[0], 0, 24, true)
191   local buildings = sprite_new(g_img_tbl[1][0], 0, 24, true)
192   local buildings1 = sprite_new(g_img_tbl[1][1], 320, 24, true)
193   local wheel = sprite_new(g_img_tbl[2][0], 0, 24, true)
194   local column = sprite_new(g_img_tbl[3], 190, 24, true)
195   local podium = sprite_new(g_img_tbl[4], 240, 24, true)
196   local people = sprite_new(g_img_tbl[5][0], 0, 24, true)
197   local people1 = sprite_new(g_img_tbl[5][1], 320, 24, true)
198
199   local i
200   for i=0,9 do
201      wheel.image = g_img_tbl[2][i%5]
202      if poll_for_key_or_button(7) == true then return end
203   end
204
205   for i=0,79 do
206      buildings.x = 0 - i
207      buildings1.x = 320 - i
208      wheel.x = 0 - i * 2
209      wheel.image = g_img_tbl[2][i%5]
210      column.x = 190 - i * 2
211      podium.x = 240 - i * 2
212      people.x = 0 - i * 3
213      people1.x = 320 - i * 3
214      if poll_for_key_or_button(7) == true then return end
215   end
216
217   if poll_for_key_or_button(15) == true then return end
218
219end
220
221function show_lowell()
222
223   flash_effect(image_load("credits.lzc", 0))
224
225   canvas_hide_all_sprites()
226
227   local g_img_tbl = image_load_all("lowell.lzc")
228   local bg = sprite_new(g_img_tbl[0][0], 0, 24, true)
229   local lowell = sprite_new(g_img_tbl[0][1], 0, 24, true)
230
231   fade_in()
232
233   for i=1,20 do
234      lowell.image = g_img_tbl[0][i]
235      if poll_for_key_or_button(5) == true then return end
236   end
237
238   flash_effect(image_load("credits.lzc", 1))
239
240   if poll_for_key_or_button(70) == true then return end
241end
242
243function show_fuse()
244
245   canvas_hide_all_sprites()
246
247   local g_img_tbl = image_load_all("fuse.lzc")
248   local bg = sprite_new(g_img_tbl[0][0], 0, 24, true)
249   local fuse = sprite_new(g_img_tbl[0][1], 0, 24, true)
250
251   music_play("mdd_mus.lzc", 2)
252
253   fade_in(4)
254
255   for i=1,28 do
256      fuse.image = g_img_tbl[0][i]
257      if poll_for_key_or_button(2) == true then return end
258   end
259
260      flash_effect(image_load("credits.lzc", 2))
261end
262
263function show_flash()
264
265   canvas_hide_all_sprites()
266
267      local g_img_tbl = image_load_all("fair.lzc")
268
269      canvas_hide_all_sprites()
270
271      local fair_bg = sprite_new(g_img_tbl[0], 0, 24, true)
272      local buildings = sprite_new(g_img_tbl[1][0], -80, 24, true)
273      local buildings1 = sprite_new(g_img_tbl[1][1], 240, 24, true)
274      local column = sprite_new(g_img_tbl[3], 30, 24, true)
275      local podium = sprite_new(g_img_tbl[4], 80, 24, true)
276
277   music_play("mdd_mus.lzc", 3)
278
279   g_img_tbl = image_load_all("flash.lzc")
280
281   fade_in(8)
282   play_sfx(12, false) --explosion FIXME this might not be the correct sfx
283   local s = sprite_new(g_img_tbl[0][0], 80, 24, true)
284   local i
285   for i=0,9 do
286      s.image = g_img_tbl[0][i]
287      if poll_for_key_or_button(7) == true then return end
288   end
289   for i=0,8 do
290      s.image = g_img_tbl[1][i]
291      if poll_for_key_or_button(6) == true then return end
292   end
293
294   flash_effect(image_load("credits.lzc", 3), 215)
295end
296
297function show_flight()
298   canvas_hide_all_sprites()
299
300   local g_img_tbl = image_load_all("flight.lzc")
301   local bg = sprite_new(g_img_tbl[0], 0, 24, true)
302   music_play("mdd_mus.lzc", 4)
303   fade_in(12)
304   local capsule = create_sprite(g_img_tbl[1], 270, 140)
305
306   local i
307   for i=0,12 do
308      capsule.image.scale = 1 + i * i + (i * 2)
309      capsule.x = 270 - i * 4 * i
310      capsule.y = 140 - i * 2 * i
311      if poll_for_key_or_button(6) == true then return end
312   end
313
314   if poll_for_key_or_button(98) == true then return end
315   music_stop()
316   if poll_for_key_or_button(8) == true then return end
317
318end
319
320function show_cabin()
321   canvas_hide_all_sprites()
322
323   local g_img_tbl = image_load_all("cabin.lzc")
324
325   local bg = sprite_new(g_img_tbl[0], 0, 24, true)
326   local cloud = sprite_new(g_img_tbl[1][0], 116, 28, true)
327   local cloud1 = sprite_new(g_img_tbl[1][0], 222, 53, true)
328   local man = create_sprite(g_img_tbl[2], 0, 50)
329   local silhouette = create_sprite(g_img_tbl[3], -900, -150)
330   local hat1 = create_sprite(g_img_tbl[4], 62, -150)
331   local hat2 = create_sprite(g_img_tbl[5], 62, 0)
332   local hat3 = create_sprite(g_img_tbl[6], 62, 0)
333
334   music_play("mdd_mus.lzc", 5)
335
336   --FIXME rotate sprites.
337
338   local i
339   for i=0,30 do
340      cloud.image = g_img_tbl[1][(i + 2) % 15]
341      cloud1.image = g_img_tbl[1][i % 15]
342      silhouette.x = i * 64 - 900
343      silhouette.y = i * 16 - 150
344      man.x = 0 - (i * 4)
345      man.y = i * i * 2 + 50
346      man.image.scale = 100 + i * 32
347      hat1.x = i * 32 + 62
348      if i > 6 then
349         hat1.y = i * 4 + 15
350      else
351         hat1.y = i * 8 + 15
352      end
353
354      hat2.x = i * 32 - 150
355      hat2.y = i * 8
356
357      hat3.x = i * 32 - 500
358      hat3.y = i * 4
359
360      if poll_for_key_or_button(8) == true then return end
361   end
362
363   flash_effect(image_load("credits.lzc", 4),250)
364end
365
366function show_mars_flash()
367   canvas_hide_all_sprites()
368
369   local g_img_tbl = image_load_all("mars.lzc")
370
371   music_play("mdd_mus.lzc", 6)
372
373   local bg = sprite_new(g_img_tbl[0][0], 0, 24, true)
374   local flash = sprite_new(g_img_tbl[0][1], 0, 24, true)
375
376   fade_in(12)
377
378   if poll_for_key_or_button(60) == false then
379
380   local i
381   for i=-1,1 do
382      local j
383      for j=0,2 do
384      local k
385         for k=-1,2 do
386            flash.image = g_img_tbl[0][3-math.abs(k)]
387
388            local delay = 3
389            if i == 0 and k == 0 then
390               delay = 10
391            end
392            if poll_for_key_or_button(delay) == true then
393               flash_effect(image_load("credits.lzc", 5))
394               return
395            end
396         end
397
398         if poll_for_key_or_button(5) == true then
399            flash_effect(image_load("credits.lzc", 5))
400            return
401         end
402      end
403
404      if poll_for_key_or_button(30) == true then
405         flash_effect(image_load("credits.lzc", 5))
406         return
407      end
408   end
409
410   end
411
412   poll_for_key_or_button(40)
413
414   flash_effect(image_load("credits.lzc", 5))
415end
416
417function play_intro()
418
419   show_logos()
420   if should_exit() then return end
421
422   show_fair_ground()
423   if should_exit() then return end
424
425   show_lowell()
426   if should_exit() then return end
427
428   show_fuse()
429   if should_exit() then return end
430
431   show_flash()
432   if should_exit() then return end
433
434   show_flight()
435   if should_exit() then return end
436
437   show_cabin()
438   if should_exit() then return end
439
440   show_mars_flash()
441   if should_exit() then return end
442
443end
444
445function show_home()
446   canvas_hide_all_sprites()
447
448   local text_tbl = text_load("scenetxt.lzc", 0)
449
450   local g_img_tbl = image_load_all("scene1.lzc")
451
452   local bg = sprite_new(g_img_tbl[0][0], 0, 24, true)
453   local door = sprite_new(g_img_tbl[0][1], 0, 24, true)
454   local avatar = sprite_new(g_img_tbl[1][0], 240, 151, true)
455   local text = sprite_new(nil, 0, 160, true)
456   text.text = text_tbl[1]
457   text.text_color = 14
458   text.text_align = 2
459   fade_in()
460   music_play("mdd_mus.lzc", 8)
461
462   poll_for_key_or_button(75)
463   if should_exit() then return end
464
465   local woman = sprite_new(g_img_tbl[8][0], 0, 151, true)
466   local spector = create_sprite(g_img_tbl[2][0], 60, 151)
467
468   door.visible = false
469   avatar.image = g_img_tbl[1][1]
470   text.text = text_tbl[2]
471
472   poll_for_key_or_button(100)
473   if should_exit() then return end
474
475   avatar.image = g_img_tbl[1][3]
476   spector.image = g_img_tbl[2][1]
477   spector.x = 80
478   text.text = text_tbl[3]
479   text.text_color = 2
480
481   poll_for_key_or_button(100)
482   if should_exit() then return end
483
484   woman.image = g_img_tbl[8][1]
485   text.text = text_tbl[4]
486   text.text_color = 14
487
488   poll_for_key_or_button(100)
489   if should_exit() then return end
490
491   woman.image = g_img_tbl[8][2]
492   spector.image = g_img_tbl[2][2]
493   spector.x = 100
494   text.text = text_tbl[5]
495   text.text_color = 2
496
497   poll_for_key_or_button(75)
498   if should_exit() then return end
499
500   woman.image = g_img_tbl[3][0]
501   woman.x = -10
502   woman.y = 155
503   spector.image = g_img_tbl[2][3]
504   spector.x = 145
505   text.text = text_tbl[6]
506   text.text_color = 9
507
508   poll_for_key_or_button(75)
509   if should_exit() then return end
510
511   --woman hands spector note
512   text.text = text_tbl[7]
513   local i
514   for i=0,10 do
515      if i <= 9 then
516         woman.image = g_img_tbl[3][i]
517      end
518      spector.image = g_img_tbl[2][i + 5]
519      poll_for_key_or_button(5)
520   end
521
522   poll_for_key_or_button(50)
523   if should_exit() then return end
524
525   text.text = text_tbl[8]
526   text.text_color = 2
527
528   poll_for_key_or_button(100)
529   if should_exit() then return end
530
531
532   --close up on woman
533   canvas_hide_all_sprites()
534
535   bg.image = g_img_tbl[7]
536   bg.visible = true
537   text.text = text_tbl[9]
538   text.text_color = 9
539   text.visible = true
540
541   poll_for_key_or_button(100)
542   if should_exit() then return end
543
544   --spector, avatar review papers
545
546   bg.image = g_img_tbl[0][0]
547   avatar.image = g_img_tbl[1][1]
548   avatar.x = 270
549   avatar.visible = true
550
551   spector.image = g_img_tbl[2][16]
552   spector.x = 193
553   spector.y = 161
554   spector.visible = true
555
556   text.text = text_tbl[10]
557   text.text_color = 2
558
559   poll_for_key_or_button(75)
560   if should_exit() then return end
561
562   --close up on papers
563   canvas_hide_all_sprites()
564   bg.image = g_img_tbl[5][0]
565   bg.visible = true
566
567   local letter = sprite_new(g_img_tbl[5][1], 50, 24, true)
568   local hand = sprite_new(g_img_tbl[4], 235, 78, true)
569
570   text.text = text_tbl[11]
571   text.text_color = 14
572   text.visible = true
573
574   poll_for_key_or_button(100)
575   if should_exit() then return end
576
577   hand.visible = false
578   text.text = text_tbl[12]
579   text.text_color = 2
580
581   poll_for_key_or_button(100)
582   if should_exit() then return end
583
584   --pickup letter
585   letter.image = g_img_tbl[6]
586   letter.x = 0
587
588   text.text = text_tbl[13]
589   --FIXME need to rotate the letter sprite.
590   for i=0,19 do
591      letter.y = -20 - i
592      poll_for_key_or_button(1)
593   end
594
595   letter.visible = false
596   text.text = text_tbl[14]
597   text.text_color = 14
598
599   poll_for_key_or_button(100)
600   if should_exit() then return end
601
602   fade_out(6)
603end
604
605function show_map()
606
607   local xhair_tbl = {
608      {["x"]=190, ["y"]=80},
609      {["x"]=180, ["y"]=82},
610      {["x"]=170, ["y"]=84},
611      {["x"]=162, ["y"]=81},
612      {["x"]=154, ["y"]=75},
613      {["x"]=145, ["y"]=73},
614      {["x"]=135, ["y"]=73},
615      {["x"]=125, ["y"]=74},
616      {["x"]=115, ["y"]=76},
617      {["x"]=105, ["y"]=78},
618      {["x"]=95, ["y"]=81},
619      {["x"]=86, ["y"]=86}
620   }
621
622   canvas_hide_all_sprites()
623
624   local bg = sprite_new(image_load("scene2.lzc", 0), 0, 24, true)
625   fade_in(6)
626
627   poll_for_key_or_button(50)
628   if should_exit() then return end
629
630   local i
631   for i=1,12 do
632      image_draw_line(bg.image, xhair_tbl[i].x-1, xhair_tbl[i].y-24-5, xhair_tbl[i].x+3, xhair_tbl[i].y-24-5, 9)
633      image_draw_line(bg.image, xhair_tbl[i].x+1, xhair_tbl[i].y-1-24-5, xhair_tbl[i].x+1, xhair_tbl[i].y+1-24-5, 9)
634      poll_for_key_or_button(20)
635      if should_exit() then return end
636   end
637
638   poll_for_key_or_button(50)
639
640   fade_out(6)
641
642end
643
644function show_hike()
645   canvas_hide_all_sprites()
646   local g_img_tbl = image_load_all("scene2.lzc")
647
648   local bg = sprite_new(g_img_tbl[1], 0, 24, true)
649   local avatar = sprite_new(g_img_tbl[3][0], -15, 148, true)
650   local spector = sprite_new(g_img_tbl[4][0], -45, 149, true)
651   local fg = sprite_new(g_img_tbl[2], 0, 24, true)
652
653   fade_in(6)
654
655   local i
656   for i=0,94 do
657      avatar.image = g_img_tbl[3][i % 12]
658      avatar.x = i * 4 + -15
659      spector.image = g_img_tbl[4][(i+4) % 12]
660      spector.x = i * 4 + -45
661      poll_for_key_or_button(3)
662      if should_exit() then return end
663   end
664
665   fade_out(6)
666end
667
668function show_lab_present_day()
669   canvas_hide_all_sprites()
670   local g_img_tbl = image_load_all("scene3.lzc")
671   local text_tbl = text_load("scenetxt.lzc", 1)
672
673   local text = sprite_new(nil, 0, 160, true)
674   text.text = text_tbl[1]
675   text.text_color = 2
676   text.text_align = 2
677
678   local moongate_tbl = image_load_all("moongate.lzc")
679
680   local bg = sprite_new(g_img_tbl[0], 0, 24, true)
681
682   local moongate = create_sprite(moongate_tbl[0][0], 125, 130)
683   moongate.visible = false
684
685   local spector = sprite_new(g_img_tbl[3][0], 165, 137,true)
686   local table = create_sprite(g_img_tbl[1][0], 151, 82)
687   local avatar = create_sprite(g_img_tbl[2][0], 65, 150)
688
689   --local s = sprite_new(g_img_tbl[4][0], 0, 24,true)
690   music_play("mdd_mus.lzc", 9)
691
692   fade_in(6)
693
694   local lab_tbl = {
695      {1, 2,  0, 0, 50},
696      {1, 2,  0, 1, 100},
697      {2, 14, 0, 1, 100},
698      {3, 2,  0, 2, 100},
699      {4, 2, -1, 0, 100},
700      {5, 14,-2, 0, 100},
701      {6, 2, -1, 0, 100},
702      {7, 14, 1, 3, 100},
703      {8, 2,  1, 2, 100},
704      {9, 14, 0, 2, 100}
705
706   }
707
708   local i
709   for i=1,7 do
710      if lab_tbl[i][3] >= 0 then
711         spector.image = g_img_tbl[3][lab_tbl[i][4]]
712      else
713         spector.visible = false
714         table.visible = false
715         avatar.visible = false
716         bg.image = g_img_tbl[4][math.abs(lab_tbl[i][3])-1]
717      end
718
719      text.text = text_tbl[lab_tbl[i][1]]
720      text.text_color = lab_tbl[i][2]
721      poll_for_key_or_button(lab_tbl[i][5])
722      if should_exit() then return end
723   end
724
725   bg.image = g_img_tbl[0]
726   spector.visible = true
727   avatar.visible = true
728   table.visible = true
729   table.image = g_img_tbl[1][1]
730
731   spector.image = g_img_tbl[3][3]
732   avatar.image = g_img_tbl[2][1]
733
734   moongate.visible = true
735
736   --moongate rises up from floor
737   for i=0,8 do
738      moongate.image = moongate_tbl[0][i]
739      poll_for_key_or_button(4)
740      if should_exit() then return end
741   end
742
743   --avatar, spector discuss moongate
744   moongate.image = moongate_tbl[1][0]
745   for i=8,10 do
746      local j
747      for j=0,39 do
748         moongate.image = moongate_tbl[1][j % 8]
749         avatar.image = g_img_tbl[2][lab_tbl[i][3]]
750         spector.image = g_img_tbl[3][lab_tbl[i][4]]
751         text.text = text_tbl[lab_tbl[i][1]]
752         text.text_color = lab_tbl[i][2]
753
754         poll_for_key_or_button(4)
755         if should_exit() then return end
756      end
757   end
758
759
760   spector.image = moongate_tbl[4][0]
761   for i=0,64 do
762      moongate.image = moongate_tbl[1][i % 8]
763      avatar.image = moongate_tbl[2][math.floor(i/2)]
764
765      poll_for_key_or_button(4)
766      if should_exit() then return end
767   end
768
769   avatar.visible = false
770
771   for i=0,39 do
772      moongate.image = moongate_tbl[1][i % 8]
773      if i ~= 39 then
774         spector.image = moongate_tbl[4][math.floor(i/2)]
775      end
776
777      poll_for_key_or_button(4)
778      if should_exit() then return end
779   end
780
781   fade_out(6)
782end
783
784function show_lab_1895()
785   canvas_hide_all_sprites()
786   local scene4a_tbl = image_load_all("scene4a.lzc")
787   local scene4b_tbl = image_load_all("scene4b.lzc")
788   local moongate_tbl = image_load_all("moongate.lzc")
789
790   local text_tbl = text_load("scenetxt.lzc", 2)
791
792   local text = sprite_new(nil, 0, 160, true)
793   text.text = text_tbl[1]
794   text.text_color = 6
795   text.text_align = 2
796
797   local bg = sprite_new(scene4a_tbl[0], 0, 24, true)
798
799   local moongate = create_sprite(moongate_tbl[0][0], 140, 125)
800   moongate.visible = false
801
802   local tesla = sprite_new(scene4a_tbl[6][0], 187, 125, true)
803   local spark = sprite_new(scene4b_tbl[2][0], 32, 24, false)
804
805   local freud_body = create_sprite(scene4a_tbl[4][0], 0, 74)
806   local freud_head = create_sprite(scene4a_tbl[4][2], 23, 100)
807
808   local nellie = sprite_new(scene4b_tbl[1][0], 242, 24, false)
809
810   local bookcase = sprite_new(scene4a_tbl[1], 242, 24, false)
811
812   local blood = sprite_new(scene4a_tbl[5][0], 240, 40, true)
813   local garrett = sprite_new(scene4b_tbl[0][0], 270, 45, false)
814
815   fade_in(6)
816
817   music_play("mdd_mus.lzc", 10)
818
819   local i
820   for i=1,2 do
821      text.text = text_tbl[i]
822      local j
823      for j=0,20 do
824         if 10 - math.abs(j-10) > 5 then
825            tesla.image = scene4a_tbl[6][5]
826         else
827            tesla.image = scene4a_tbl[6][10 - math.abs(j-10)]
828         end
829
830         if j > 4 and j < 16 then
831            --FIXME need spark sfx
832            spark.visible = true
833            spark.image = scene4b_tbl[2][j % 10]
834         else
835            spark.visible = false
836         end
837
838         poll_for_key_or_button(4)
839         if should_exit() then return end
840      end
841      text.text_color = 7
842   end
843
844   moongate.visible = true
845
846   --moongate rises up from floor
847   tesla.x = 188
848   tesla.y = 128
849   tesla.image = scene4a_tbl[6][6]
850
851   for i=0,8 do
852      moongate.image = moongate_tbl[0][i]
853      if i == 5 then
854         tesla.image = scene4a_tbl[6][7]
855      end
856
857      if i == 2 then
858         blood.image = scene4a_tbl[5][1]
859      end
860
861      poll_for_key_or_button(4)
862      if should_exit() then return end
863   end
864
865   --avatar walks out of the newly risen moongate
866   local avatar = create_sprite(moongate_tbl[3][0], 80, 150)
867
868   tesla.x = 180
869   tesla.y = 133
870   tesla.image = scene4a_tbl[6][8]
871
872   for i=0,68 do
873      moongate.image = moongate_tbl[1][i % 8]
874      avatar.image = moongate_tbl[3][math.floor(i/2)]
875      if i == 3 then
876         blood.image = scene4a_tbl[5][2]
877      end
878
879      if i == 4 then
880         freud_head.image = scene4a_tbl[4][3]
881      end
882
883      if i == 11 then
884         text.text = text_tbl[3]
885         text.text_color = 7
886      end
887
888      poll_for_key_or_button(3)
889      if should_exit() then return end
890   end
891
892   --spector walks out of moongate
893   local spector = create_sprite(moongate_tbl[5][0], 140, 130)
894
895   text.text = text_tbl[4]
896   text.text_color = 14
897
898   for i=0,24 do
899      moongate.image = moongate_tbl[1][i % 8]
900      spector.image = moongate_tbl[5][math.floor(i/2)]
901
902      poll_for_key_or_button(3)
903      if should_exit() then return end
904   end
905
906   --spector face closeup shot
907   canvas_hide_all_sprites()
908
909   bg.image = scene4a_tbl[9][0]
910   bg.visible = true
911
912   local face = sprite_new(scene4a_tbl[9][1], 0, 24, true)
913
914   text.text = text_tbl[5]
915   text.text_color = 2
916   text.visible = true
917
918   poll_for_key_or_button(200)
919   if should_exit() then return end
920
921   --spector shows note to tesla
922   text.text = text_tbl[6]
923   face.visible = false
924   bg.image = scene4a_tbl[0]
925
926   tesla.visible = true
927   tesla.image = scene4a_tbl[6][9]
928
929   blood.visible = true
930
931   freud_head.image = scene4a_tbl[4][2]
932   freud_head.visible = true
933
934   freud_body.visible = true
935
936   avatar.visible = true
937   avatar.x = 80
938   avatar.y = 150
939   avatar.image = scene4a_tbl[2][1]
940
941   spector.visible = true
942   spector.x = 145
943   spector.y = 140
944   spector.image = scene4a_tbl[3][1]
945
946   poll_for_key_or_button(200)
947   if should_exit() then return end
948
949   --tesla face closeup shot
950   canvas_hide_all_sprites()
951
952   bg.image = scene4a_tbl[7][0]
953   bg.visible = true
954
955   face.image = scene4a_tbl[7][1]
956   face.visible = true
957
958   text.text = text_tbl[7]
959   text.text_color = 7
960   text.visible = true
961
962   poll_for_key_or_button(200)
963   if should_exit() then return end
964
965   --blood closeup.
966   canvas_hide_all_sprites()
967
968   bg.image = scene4a_tbl[8][0]
969   bg.visible = true
970
971   face.visible = true
972   text.visible = true
973   text.text_color = 4
974
975   for i=8,9 do
976      face.image = scene4a_tbl[8][i - 7]
977      text.text = text_tbl[i]
978
979      poll_for_key_or_button(200)
980      if should_exit() then return end
981   end
982
983   --Tesla calls Mr Garrett.
984   text.text = text_tbl[10]
985   text.text_color = 7
986   face.visible = false
987   bg.image = scene4a_tbl[0]
988
989   tesla.visible = true
990   tesla.image = scene4a_tbl[6][10]
991
992   blood.visible = true
993
994   freud_head.visible = true
995   freud_body.visible = true
996
997   avatar.visible = true
998   avatar.image = scene4a_tbl[2][0]
999
1000   spector.visible = true
1001   spector.image = scene4a_tbl[3][0]
1002
1003   poll_for_key_or_button(200)
1004   if should_exit() then return end
1005
1006   --Mr Garrett entrance
1007   garrett.visible = true
1008   spector.x = 150
1009   spector.y = 140
1010   text.text = text_tbl[11]
1011   text.text_color = 9
1012   for i=0,31 do
1013      if i == 5 then
1014         spector.image = scene4a_tbl[3][2]
1015      end
1016      if i <= 10 then
1017         avatar.image = scene4a_tbl[2][i]
1018      end
1019
1020      garrett.image = scene4b_tbl[0][i]
1021
1022      poll_for_key_or_button(3)
1023      if should_exit() then return end
1024   end
1025
1026   --Tesla checks note, talks about mission.
1027
1028   spector.image = scene4a_tbl[3][1]
1029   spector.x = 155
1030   avatar.image = scene4a_tbl[2][10]
1031   freud_head.image = scene4a_tbl[4][1]
1032
1033   local text_color_tbl = {4, 6, 7, 7, 14, 2, 2, 7, 4, 4, 7, 9, 7, 4, 7, 4, 7, 2, 7, 7, 7, 7, 7, 11, 2, 7, 7}
1034
1035   for i=12,20 do
1036      if i==13 or i==16 or i==20 then
1037         face.visible = true
1038         tesla.visible = false
1039         blood.visible = false
1040         freud_head.visible = false
1041         freud_body.visible = false
1042         avatar.visible = false
1043         spector.visible = false
1044         garrett.visible = false
1045         local idx
1046         if i==13 then
1047            idx = 8
1048         else
1049            idx = 7
1050         end
1051         bg.image = scene4a_tbl[idx][0]
1052
1053         if i==16 then
1054            face.image = scene4a_tbl[idx][2]
1055         else
1056            face.image = scene4a_tbl[idx][1]
1057         end
1058      else
1059         local idx
1060         if i == 14 then
1061            spector.image = scene4a_tbl[3][0]
1062         end
1063         if i == 12 or i == 17 then
1064            idx = 9
1065         elseif i == 14 or i == 15 then
1066            idx = 10
1067         else
1068            idx = 11
1069         end
1070         tesla.image = scene4a_tbl[6][idx]
1071      end
1072
1073      text.text = text_tbl[i]
1074      text.text_color = text_color_tbl[i+1]
1075
1076      poll_for_key_or_button(200)
1077      if should_exit() then return end
1078
1079      face.visible = false
1080      tesla.visible = true
1081      blood.visible = true
1082      freud_head.visible = true
1083      freud_body.visible = true
1084      avatar.visible = true
1085      spector.visible = true
1086      garrett.visible = true
1087      bg.image = scene4a_tbl[0]
1088   end
1089
1090
1091   --Nellie walks in
1092
1093   tesla.image = scene4a_tbl[6][10]
1094
1095   text.text = text_tbl[21]
1096   text.text_color = text_color_tbl[22]
1097
1098   nellie.visible = true
1099   nellie.y = 125
1100   bookcase.visible = true
1101
1102   for i=0,31 do
1103      if i == 7 then
1104         freud_head.image = scene4a_tbl[4][2]
1105      end
1106      nellie.image = scene4b_tbl[1][i%4]
1107      nellie.x = 320 - i * 3
1108      if i > 25 then
1109         nellie.y = 125 + (i - 26) * 2
1110      end
1111
1112      local avatar_idx = 20 - i
1113      if avatar_idx > 10 then
1114      avatar_idx = 10
1115      end
1116
1117      if avatar_idx < 4 then
1118      avatar_idx = 4
1119      end
1120      avatar.image = scene4a_tbl[2][avatar_idx]
1121
1122      local garrett_idx = 45 - i
1123      if garrett_idx > 31 then
1124         garrett_idx = 31
1125      end
1126
1127      if garrett_idx < 18 then
1128         garrett_idx = 18
1129      end
1130      garrett.image = scene4b_tbl[0][garrett_idx]
1131
1132      poll_for_key_or_button(3)
1133      if should_exit() then return end
1134   end
1135
1136   --Nellie shakes hands with Spector, Avatar
1137
1138   spector.x = 165
1139   nellie.x = 227
1140   nellie.y = 135
1141
1142   for i=22,26 do
1143      if i == 24 then
1144         nellie.image = scene4b_tbl[1][7]
1145         spector.image = scene4a_tbl[3][3]
1146      else
1147         if i == 23 then
1148            nellie.image = scene4b_tbl[1][6]
1149         else
1150            nellie.image = scene4b_tbl[1][5]
1151         end
1152         spector.image = scene4a_tbl[3][0]
1153      end
1154
1155      if i == 25 then
1156         tesla.image = scene4a_tbl[6][9]
1157      elseif i == 26 then
1158         tesla.image = scene4a_tbl[6][11]
1159      else
1160         tesla.image = scene4a_tbl[6][12]
1161      end
1162
1163      text.text = text_tbl[i]
1164      text.text_color = text_color_tbl[i+1]
1165
1166      poll_for_key_or_button(200)
1167      if should_exit() then return end
1168   end
1169
1170end
1171
1172function run_introduction()
1173
1174   show_home()
1175   if should_exit() then return end
1176
1177   show_map()
1178   if should_exit() then return end
1179
1180   show_hike()
1181   if should_exit() then return end
1182
1183   show_lab_present_day()
1184   if should_exit() then return end
1185
1186   show_lab_1895()
1187   if should_exit() then return end
1188
1189   fade_out(6)
1190end
1191
1192local char_creation_tbl = {
1193   {["text"]=-1, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1194   {["text"]=-2, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1195   {["text"]=3,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1196   {["text"]=4,  ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1197   {["text"]=5,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -4},
1198   {["text"]=6,  ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1199   {["text"]=7,  ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -2},
1200   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1201   {["text"]=8,  ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1202   {["text"]=9,  ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1203   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1204   {["text"]=10, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1205   {["text"]=11, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1206   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1207   {["text"]=12, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1208   {["text"]=13, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1209   {["text"]=14, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -3},
1210   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1211   {["text"]=15, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1212   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1213   {["text"]=16, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1214   {["text"]=17, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -2},
1215   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1216   {["text"]=18, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1217   {["text"]=19, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -2},
1218   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1219   {["text"]=20, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1220   {["text"]=21, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -2},
1221   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1222   {["text"]=22, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -2},
1223   {["text"]=0,  ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1224   {["text"]=23, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1225   {["text"]=24, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1226   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1227   {["text"]=25, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1228   {["text"]=26, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1229   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1230   {["text"]=27, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1231   {["text"]=28, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -2},
1232   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1233   {["text"]=29, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1234   {["text"]=30, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1235   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1236   {["text"]=31, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1237   {["text"]=32, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1238   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1239   {["text"]=33, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1240   {["text"]=34, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -2},
1241   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1242   {["text"]=35, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1243   {["text"]=36, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1244   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1245   {["text"]=37, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1246   {["text"]=38, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1247   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1248   {["text"]=39, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1249   {["text"]=40, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1250   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1251   {["text"]=41, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1252   {["text"]=42, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1253   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1254   {["text"]=43, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1255   {["text"]=44, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -2},
1256   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1257   {["text"]=45, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1258   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1259   {["text"]=46, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1260   {["text"]=47, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1261   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1262   {["text"]=48, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1263   {["text"]=49, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1264   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1265   {["text"]=50, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1266   {["text"]=51, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -2},
1267   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1268   {["text"]=52, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1269   {["text"]=53, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1270   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1271   {["text"]=54, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1272   {["text"]=55, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1273   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1274   {["text"]=56, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1275   {["text"]=57, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1276   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1277   {["text"]=58, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1278   {["text"]=59, ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -2},
1279   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1280   {["text"]=60, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1281   {["text"]=61, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1282   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1},
1283   {["text"]=62, ["eye_sprite"]=3, ["can_move_pen"]=true, ["action_code"]= -1},
1284   {["text"]=63, ["eye_sprite"]=1, ["can_move_pen"]=false, ["action_code"]= -1},
1285   {["text"]=0,  ["eye_sprite"]=2, ["can_move_pen"]=false, ["action_code"]= -1}
1286}
1287
1288local g_player_name = ""
1289local gender_answer = ""
1290local avatar_str
1291local avatar_dex
1292local avatar_int
1293
1294function insert_player_name(text)
1295   local output = ""
1296   local i = string.find(text,"$P")
1297   while i ~= nil do
1298      output = output .. string.sub(text,1,i-1) .. g_player_name
1299      text = string.sub(text,i+2)
1300      i = string.find(text, "$P")
1301   end
1302   output = output .. text
1303
1304   return output
1305end
1306
1307local g_keycode_tbl =
1308{
1309[32]=" ",
1310[39]="'",
1311[44]=",",
1312[45]="-",
1313[46]=".",
1314[48]="0",
1315[49]="1",
1316[50]="2",
1317[51]="3",
1318[52]="4",
1319[53]="5",
1320[54]="6",
1321[55]="7",
1322[56]="8",
1323[57]="9",
1324[65]="A",
1325[66]="B",
1326[67]="C",
1327[68]="D",
1328[69]="E",
1329[70]="F",
1330[71]="G",
1331[72]="H",
1332[73]="I",
1333[74]="J",
1334[75]="K",
1335[76]="L",
1336[77]="M",
1337[78]="N",
1338[79]="O",
1339[80]="P",
1340[81]="Q",
1341[82]="R",
1342[83]="S",
1343[84]="T",
1344[85]="U",
1345[86]="V",
1346[87]="W",
1347[88]="X",
1348[89]="Y",
1349[90]="Z",
1350
1351[97]="a",
1352[98]="b",
1353[99]="c",
1354[100]="d",
1355[101]="e",
1356[102]="f",
1357[103]="g",
1358[104]="h",
1359[105]="i",
1360[106]="j",
1361[107]="k",
1362[108]="l",
1363[109]="m",
1364[110]="n",
1365[111]="o",
1366[112]="p",
1367[113]="q",
1368[114]="r",
1369[115]="s",
1370[116]="t",
1371[117]="u",
1372[118]="v",
1373[119]="w",
1374[120]="x",
1375[121]="y",
1376[122]="z",
1377}
1378
1379local g_name_sprite = sprite_new(nil, 120, 184, false)
1380g_name_sprite.text_color = 14
1381local g_cursor_sprite = sprite_new(nil, 120, 184, false)
1382g_cursor_sprite.text = "_"
1383g_cursor_sprite.text_color = 14
1384local g_cursor_timer = 0
1385g_char_index = 0
1386
1387
1388function collect_player_name()
1389   local name_text = g_name_sprite.text
1390   local len = string.len(name_text)
1391
1392   g_name_sprite.visible = true
1393
1394   local input = poll_for_input()
1395   if input ~= nil then
1396      if should_exit() then
1397         return false
1398      end
1399
1400      if (input == SDLK_BACKSPACE or input == SDLK_LEFT) and len > 0 then
1401         g_name_sprite.text = string.sub(name_text, 1, len - 1)
1402         len = len - 1
1403         if len == 1 then -- old len
1404           g_char_index = 0
1405         else
1406           g_char_index = string.byte(name_text, len -1)
1407         end
1408      elseif (input == SDLK_RETURN or input == SDLK_KP_ENTER) and string.len(trim_string(name_text)) > 0 then --return
1409         g_name_sprite.visible = false
1410         g_cursor_sprite.visible = false
1411         g_player_name = trim_string(name_text)
1412         return true
1413      elseif input == SDLK_UP then --up
1414         if g_char_index == 0 then
1415           if len > 0 then
1416             g_char_index = SDLK_a
1417           else
1418             g_char_index = 65 --A
1419         end
1420         elseif g_char_index <= 32 then --gap in characters
1421           g_char_index = 48
1422         elseif g_char_index >= 57 and  g_char_index < 65 then --gap in characters
1423           g_char_index = 65
1424         elseif g_char_index >= 90 and g_char_index < 97 then --gap in characters
1425           g_char_index = 97
1426         elseif g_char_index >= 122 then --last char
1427           g_char_index = 32
1428         else
1429           g_char_index = g_char_index + 1
1430         end
1431
1432         if len > 0 then -- erase char
1433           name_text = string.sub(name_text, 1, len - 1)
1434         end
1435         g_name_sprite.text = name_text..g_keycode_tbl[g_char_index]
1436      elseif input == SDLK_DOWN then --down
1437         if g_char_index == 0 then
1438           if len > 0 then
1439             g_char_index = 122 --z
1440           else
1441             g_char_index = 90 --Z
1442           end
1443         elseif g_char_index == 65 then --gap in characters
1444           g_char_index = 57
1445         elseif g_char_index == 97 then --gap in characters
1446           g_char_index = 90
1447         elseif g_char_index <= 32 then --first char
1448           g_char_index = 122
1449         elseif g_char_index <= 48 then --gap in characters
1450           g_char_index = 32
1451         else
1452           g_char_index = g_char_index - 1
1453         end
1454
1455         if len > 0 then -- erase char
1456           name_text = string.sub(name_text, 1, len - 1)
1457         end
1458         g_name_sprite.text = name_text..g_keycode_tbl[g_char_index]
1459      elseif input == SDLK_RIGHT and len < 13 then --right
1460         g_char_index = SDLK_a --a
1461         g_name_sprite.text = name_text.."a"
1462      elseif g_keycode_tbl[input] ~= nil and len < 13 then
1463         g_name_sprite.text = name_text..g_keycode_tbl[input]
1464         len = len + 1
1465      end
1466
1467   end
1468
1469   if len == 13 then
1470      g_cursor_sprite.visible = false
1471   else
1472      g_cursor_sprite.x = 120 + g_name_sprite.text_width
1473      if g_cursor_timer == 0 then
1474         g_cursor_timer = 8
1475         if g_cursor_sprite.visible == true then
1476            g_cursor_sprite.visible = false
1477         else
1478            g_cursor_sprite.visible = true
1479         end
1480      else
1481         g_cursor_timer = g_cursor_timer - 1
1482      end
1483   end
1484
1485   return false
1486end
1487
1488function update_freud(freud)
1489   if freud.eyes.blink_timer < 200 then
1490      if freud.timer == 0 then
1491         freud.timer = math.random(100, 350)
1492         if freud.state == FREUD_STATE_STARING then
1493            freud.state = FREUD_STATE_WRITING
1494         else
1495            freud.state = FREUD_STATE_STARING
1496         end
1497      else
1498         freud.timer = freud.timer - 1
1499      end
1500
1501      local state = freud.state
1502      if state == FREUD_STATE_STARING then
1503         if freud.can_move_pen == true then
1504            freud.eyes.sprite.image = freud.images[0][1]
1505         else
1506            freud.eyes.sprite.image = freud.images[0][freud.eyes.sprite_idx]
1507         end
1508      elseif state == FREUD_STATE_WRITING and freud.can_move_pen == true then
1509         freud.eyes.sprite.image = freud.images[0][freud.eyes.sprite_idx]
1510         if freud.timer % 4 == 0 then
1511            freud.pen.sprite.image = freud.images[2][math.random(0,6)]
1512            freud.pen.x_off = freud.pen.x_off + math.random(0, 3)
1513            if freud.pen.x_off >= 30 then
1514               freud.pen.x_off = freud.pen.x_off % 30
1515               freud.pen.y_off = (freud.pen.y_off + 2) % 20
1516            end
1517
1518            freud.pen.sprite.x = 128 - freud.pen.x_off
1519            freud.pen.sprite.y = 83 + freud.pen.y_off
1520         end
1521      end
1522   end
1523
1524   if freud.eyes.blink_timer == 200 then
1525      if freud.eyes.sprite_idx == 2 then
1526         freud.eyes.sprite.image = freud.images[0][5]
1527      else
1528         freud.eyes.sprite.image = freud.images[0][4]
1529      end
1530   elseif freud.eyes.blink_timer == 215 then
1531      if state == FREUD_STATE_WRITING or freud.can_move_pen == false then
1532         freud.eyes.sprite.image = freud.images[0][freud.eyes.sprite_idx]
1533      else
1534         freud.eyes.sprite.image = freud.images[0][1]
1535      end
1536      freud.eyes.blink_timer = -1
1537   end
1538   freud.eyes.blink_timer = freud.eyes.blink_timer + 1
1539end
1540
1541function ask_question(question_idx, text, freud)
1542   question_idx = question_idx + 1
1543
1544   local key_input = nil
1545
1546   local text_offset = char_creation_tbl[question_idx].text
1547
1548   while text_offset ~= 0 do
1549
1550      if text_offset < 0 then
1551         text.text_color = 4
1552      else
1553         text.text_color = 6
1554      end
1555
1556      text.text = insert_player_name(freud.text_tbl[math.abs(text_offset)])
1557
1558      freud.eyes.sprite_idx = char_creation_tbl[question_idx].eye_sprite
1559      freud.eyes.sprite.image = freud.images[0][freud.eyes.sprite_idx]
1560      freud.eyes.blink_timer = 0
1561      freud.state = FREUD_STATE_STARING
1562      freud.can_move_pen = char_creation_tbl[question_idx].can_move_pen
1563      local action = char_creation_tbl[question_idx].action_code
1564
1565      local continue_loop = true
1566      while continue_loop do
1567         if action == -1 then
1568            local input = poll_for_input()
1569            if input ~= nil then
1570               continue_loop = false
1571            end
1572         elseif action == -2 then
1573            -- A, B input
1574            local input = poll_for_input()
1575            if input ~= nil and (input == SDLK_a or input == SDLK_b) then
1576               key_input = input
1577               continue_loop = false
1578            end
1579         elseif action == -3 then
1580            local input = poll_for_input()
1581            if input ~= nil and (input == SDLK_a or input == SDLK_b or input == SDLK_c) then
1582               key_input = input
1583               continue_loop = false
1584            end
1585         elseif action == -4 then
1586            if collect_player_name() == true then
1587               continue_loop = false
1588            end
1589         end
1590
1591         if should_exit() then
1592            return nil
1593         end
1594
1595         update_freud(freud)
1596         canvas_update()
1597      end
1598
1599      question_idx = question_idx + 1
1600      text_offset = char_creation_tbl[question_idx].text
1601   end
1602
1603   return key_input
1604end
1605
1606function question_1_answer_a(text, freud, rand_high, rand_low)
1607      ask_question(18, text, freud)
1608      local var_14 = ask_question(20, text, freud)
1609      if var_14 == SDLK_a then
1610         local var_16 = ask_question(23, text, freud)
1611         if var_16 == SDLK_a then
1612            local answer = ask_question(29, text, freud)
1613            if answer == SDLK_a then
1614               avatar_str = rand_high
1615               avatar_int = rand_low
1616               if gender_answer == SDLK_a then
1617                  ask_question(31, text, freud)
1618               else
1619                  ask_question(34, text, freud)
1620               end
1621            elseif answer == SDLK_b then
1622               avatar_int = rand_high
1623               avatar_str = rand_low
1624            end
1625         elseif var_16 == SDLK_b then
1626            local answer = ask_question(37, text, freud)
1627            if answer == SDLK_a then
1628               avatar_int = rand_high
1629               avatar_dex = rand_low
1630               ask_question(40, text, freud)
1631            elseif answer == SDLK_b then
1632               avatar_dex = rand_high
1633               avatar_int = rand_low
1634               ask_question(43, text, freud)
1635            end
1636         end
1637      elseif var_14 == SDLK_b then
1638         local var_16 = ask_question(26, text, freud)
1639         if var_16 == SDLK_a then
1640            local answer = ask_question(46, text, freud)
1641            if answer == SDLK_a then
1642               avatar_str = rand_high
1643               avatar_dex = rand_low
1644               if gender_answer == SDLK_a then
1645                  ask_question(49, text, freud)
1646               else
1647                  ask_question(52, text, freud)
1648               end
1649            elseif answer == SDLK_b then
1650               avatar_dex = rand_high
1651               avatar_str = rand_low
1652               if gender_answer == SDLK_a then
1653                  ask_question(58, text, freud)
1654               else
1655                  ask_question(55, text, freud)
1656               end
1657            end
1658         elseif var_16 == SDLK_b then
1659            local answer = ask_question(61, text, freud)
1660            if answer == SDLK_a then
1661               avatar_int = rand_high
1662               avatar_dex = rand_low
1663               ask_question(64, text, freud)
1664            elseif answer == SDLK_b then
1665               avatar_dex = rand_high
1666               avatar_int = rand_low
1667               ask_question(66, text, freud)
1668            end
1669         end
1670      end
1671end
1672
1673function question_1_answer_b(text, freud, rand_high, rand_low)
1674   local var_14 = ask_question(26, text, freud)
1675   if var_14 == SDLK_a then
1676      local var_16 = ask_question(23, text, freud)
1677      if var_16 == SDLK_a then
1678         local answer = ask_question(29, text, freud)
1679         if answer == SDLK_a then
1680            avatar_str = rand_high
1681            avatar_int = rand_low
1682            if gender_answer == SDLK_a then
1683               ask_question(31, text, freud)
1684            else
1685               ask_question(34, text, freud)
1686            end
1687         elseif answer == SDLK_b then
1688            avatar_int = rand_high
1689            avatar_str = rand_low
1690         end
1691      elseif var_16 == SDLK_b then
1692         local answer = ask_question(37, text, freud)
1693         if answer == SDLK_a then
1694            avatar_int = rand_high
1695            avatar_dex = rand_low
1696            ask_question(40, text, freud)
1697         elseif answer == SDLK_b then
1698            avatar_dex = rand_high
1699            avatar_int = rand_low
1700            ask_question(43, text, freud)
1701         end
1702      end
1703   elseif var_14 == SDLK_b then
1704      local var_16 = ask_question(20, text, freud)
1705      if var_16 == SDLK_a then
1706         local answer = ask_question(72, text, freud)
1707         if answer == SDLK_a then
1708            avatar_int = rand_high
1709            avatar_str = rand_low
1710            ask_question(75, text, freud)
1711         elseif answer == SDLK_b then
1712            avatar_str = rand_high
1713            avatar_int = rand_low
1714            ask_question(78, text, freud)
1715         end
1716      elseif var_16 == SDLK_b then
1717         local answer = ask_question(46, text, freud)
1718         if answer == SDLK_a then
1719            avatar_str = rand_high
1720            avatar_dex = rand_low
1721            if gender_answer == SDLK_a then
1722               ask_question(49, text, freud)
1723            else
1724               ask_question(52, text, freud)
1725            end
1726         elseif answer == SDLK_b then
1727            avatar_dex = rand_high
1728            avatar_str = rand_low
1729            if gender_answer == SDLK_a then
1730               ask_question(58, text, freud)
1731            else
1732               ask_question(55, text, freud)
1733            end
1734         end
1735      end
1736   end
1737end
1738
1739function question_1_answer_c(text, freud, rand_high, rand_low)
1740   ask_question(81, text, freud)
1741   local var_14 = ask_question(23, text, freud)
1742   if var_14 == SDLK_a then
1743      local var_16 = ask_question(26, text, freud)
1744      if var_16 == SDLK_a then
1745         local answer = ask_question(46, text, freud)
1746         if answer == SDLK_a then
1747            avatar_str = rand_high
1748            avatar_dex = rand_low
1749            if gender_answer == SDLK_a then
1750               ask_question(49, text, freud)
1751            else
1752               ask_question(52, text, freud)
1753            end
1754         elseif answer == SDLK_b then
1755            avatar_dex = rand_high
1756            avatar_str = rand_low
1757            if gender_answer == SDLK_a then
1758               ask_question(58, text, freud)
1759            else
1760               ask_question(55, text, freud)
1761            end
1762         end
1763      elseif var_16 == SDLK_b then
1764         local answer = ask_question(37, text, freud)
1765         if answer == SDLK_a then
1766            avatar_int = rand_high
1767            avatar_dex = rand_low
1768            ask_question(40, text, freud)
1769         elseif answer == SDLK_b then
1770            avatar_dex = rand_high
1771            avatar_int = rand_low
1772            ask_question(43, text, freud)
1773         end
1774      end
1775   elseif var_14 == SDLK_b then
1776      local var_16 = ask_question(20, text, freud)
1777      if var_16 == SDLK_a then
1778         local answer = ask_question(29, text, freud)
1779         if answer == SDLK_a then
1780            avatar_str = rand_high
1781            avatar_int = rand_low
1782            if gender_answer == SDLK_a then
1783               ask_question(31, text, freud)
1784            else
1785               ask_question(34, text, freud)
1786            end
1787         elseif answer == SDLK_b then
1788            avatar_int = rand_high
1789            avatar_str = rand_low
1790         end
1791      elseif var_16 == SDLK_b then
1792         local answer = ask_question(84, text, freud)
1793         if answer == SDLK_a then
1794            avatar_dex = rand_high
1795            avatar_str = rand_low
1796            ask_question(87, text, freud)
1797         elseif answer == SDLK_b then
1798            avatar_str = rand_high
1799            avatar_dex = rand_low
1800            ask_question(90, text, freud)
1801         end
1802      end
1803   end
1804end
1805
1806function create_character()
1807   canvas_hide_all_sprites()
1808   local create_tbl = image_load_all("create.lzc")
1809
1810   local text_tbl = text_load("scenetxt.lzc", 5)
1811
1812   local text = sprite_new(nil, 0, 160, true)
1813   text.text = text_tbl[0]
1814   text.text_color = 2
1815   text.text_align = 2
1816
1817   local bg = sprite_new(create_tbl[0][0], 0, 24, true)
1818   local eyes_sprite = sprite_new(create_tbl[0][4], 0, 24, true)
1819   local pen_sprite = sprite_new(create_tbl[2][0], 128, 83, true)
1820   local clipboard = sprite_new(create_tbl[1], 89, 109, true)
1821
1822   local eyes ={["sprite"]=eyes_sprite, ["sprite_idx"]=4, ["blink_timer"]=0}
1823   local pen = {["sprite"]=pen_sprite,["x_off"]=0,["y_off"]=0,["sprite_idx"]=0,["timer"]=0,}
1824   local freud = {["eyes"]=eyes, ["pen"]=pen, ["state"]=FREUD_STATE_STARING, ["timer"] = 0, ["can_move_pen"] = false, ["images"]=create_tbl, ["text_tbl"]=text_tbl}
1825
1826   music_play("mdd_mus.lzc", 7)
1827
1828   local rand_high = math.random(24,26)
1829   local rand_low = math.random(22,24)
1830   local stat_base = math.random(18,22)
1831
1832   avatar_str = stat_base
1833   avatar_dex = stat_base
1834   avatar_int = stat_base
1835
1836   local gender
1837
1838   gender_answer = ask_question(0, text, freud)
1839   if gender_answer == SDLK_a then
1840      gender = 0 --male
1841      ask_question(8, text, freud)
1842   elseif gender_answer == SDLK_b then
1843      gender = 1 --female
1844      ask_question(11, text, freud)
1845   end
1846
1847   local answer = ask_question(14, text, freud)
1848
1849   if answer == SDLK_a then
1850      question_1_answer_a(text, freud, rand_high, rand_low)
1851   elseif answer == SDLK_b then
1852      question_1_answer_b(text, freud, rand_high, rand_low)
1853   elseif answer == SDLK_c then
1854      question_1_answer_c(text, freud, rand_high, rand_low)
1855   end
1856
1857   if should_exit() then
1858      return false
1859   end
1860
1861   ask_question(69, text, freud)
1862
1863   config_set("config/newgame", true)
1864   config_set("config/newgamedata/name", g_player_name)
1865   config_set("config/newgamedata/gender", gender)
1866   config_set("config/newgamedata/str", avatar_str)
1867   config_set("config/newgamedata/dex", avatar_dex)
1868   config_set("config/newgamedata/int", avatar_int)
1869
1870   --wait_for_input()
1871   fade_out(6)
1872
1873   return true
1874end
1875
1876function journey_onward()
1877end
1878
1879local g_menu_idx = 0
1880local g_menu_cursor_sprite = nil
1881local g_menu_bg_sprite = nil
1882
1883function execute_menu_item(cursor_pos)
1884   if cursor_pos ~= nil then
1885      set_menu_cursor_pos(cursor_pos)
1886   end
1887
1888   hide_mouse_cursor()
1889
1890   if g_menu_idx == 0 then -- story so far
1891      run_introduction()
1892   elseif g_menu_idx == 1 then -- create char
1893      if create_character() == true then
1894         return "J" -- starting new game
1895      end
1896   elseif g_menu_idx == 2 then -- journey onward
1897      journey_onward()
1898      return "J"
1899   elseif g_menu_idx == 3 then -- about md
1900      about_martian_dreams()
1901   end
1902
1903   clear_should_exit_flag()
1904   canvas_hide_all_sprites()
1905   canvas_set_opacity(0xff)
1906   music_stop()
1907   g_menu_bg_sprite.visible = true
1908   g_menu_cursor_sprite.visible = true
1909
1910   return "";
1911end
1912
1913function update_menu_cursor()
1914   local box_y_tbl = {[0]=130,[1]=147,[2]=164,[3]=181}
1915   g_menu_cursor_sprite.y = box_y_tbl[g_menu_idx]
1916end
1917
1918function set_menu_cursor_pos(new_pos)
1919   g_menu_idx = new_pos
1920   update_menu_cursor()
1921end
1922
1923function menu_cursor_down()
1924   g_menu_idx = (g_menu_idx + 1) % 4
1925   update_menu_cursor()
1926end
1927
1928function menu_cursor_up()
1929   g_menu_idx = g_menu_idx - 1
1930   if g_menu_idx < 0 then
1931      g_menu_idx = 3
1932   end
1933   update_menu_cursor()
1934end
1935
1936local g_mouse_cursor_visible = false
1937
1938function show_mouse_cursor()
1939   if g_mouse_cursor_visible == false then
1940      mouse_cursor_set_pointer(9)
1941      mouse_cursor_visible(true)
1942      g_mouse_cursor_visible = true
1943   end
1944end
1945
1946function hide_mouse_cursor()
1947   if g_mouse_cursor_visible == true then
1948      mouse_cursor_visible(false)
1949      g_mouse_cursor_visible = false
1950   end
1951end
1952
1953function main_menu()
1954   canvas_hide_all_sprites()
1955   music_stop()
1956   clear_should_exit_flag()
1957   local g_img_tbl = image_load_all("mdmenu.lzc")
1958
1959   g_menu_bg_sprite = sprite_new(g_img_tbl[0][0], 0, 0, true)
1960
1961   fade_in()
1962
1963   g_menu_cursor_sprite = sprite_new(g_img_tbl[0][2], 26, 0, true)
1964   update_menu_cursor()
1965   canvas_set_update_interval(10)
1966
1967   while true do
1968      canvas_update()
1969      local input = input_poll(true)
1970
1971      if engine_should_quit() == 1 then
1972         return "Q"
1973      end
1974
1975      if input ~= nil then
1976         canvas_set_update_interval(25)
1977
1978         if input == SDLK_q then -- q
1979            return "Q"
1980         elseif input == SDLK_RETURN or input == SDLK_SPACE or input == KP_ENTER then -- space or return
1981            if execute_menu_item() == "J" then
1982               return "J"
1983            end
1984         elseif input == SDLK_r or input == SDLK_i then -- Run Introduction
1985            execute_menu_item(0)
1986         elseif input == SDLK_c then -- c (create char)
1987            if execute_menu_item(1) == "J" then
1988               return "J" -- starting new game
1989            end
1990         elseif input == SDLK_j or input == SDLK_g then -- j, g (journey onward, continue Game)
1991            execute_menu_item(2)
1992            return "J"
1993         elseif input == SDLK_a then -- a (about MD)
1994            execute_menu_item(3)
1995         elseif input == SDLK_DOWN or input == SDL_KP2 then -- down key
1996            menu_cursor_down()
1997         elseif input == SDLK_UP or input == SDL_KP8 then -- up key
1998            menu_cursor_up()
1999         elseif input == MOUSE_MOTION or input == MOUSE_CLICK then --mouse movement
2000            show_mouse_cursor()
2001            local x = get_mouse_x()
2002            local y = get_mouse_y()
2003            if x > 57 and x < 260 and y > 130 then
2004               if y > 130 and y < 148 then -- run introduction
2005                  set_menu_cursor_pos(0)
2006               elseif y > 147 and y < 164 then -- create new char
2007                  set_menu_cursor_pos(1)
2008               elseif y > 163 and y < 181 then -- continue game
2009                  set_menu_cursor_pos(2)
2010               elseif y > 180 then -- about MD
2011                  set_menu_cursor_pos(3)
2012               end
2013               if input == MOUSE_CLICK then
2014                  if execute_menu_item() == "J" then
2015                     return "J"
2016                  end
2017               end
2018            end
2019         end
2020
2021         canvas_set_update_interval(10)
2022      end
2023   end
2024
2025end
2026
2027
2028mouse_cursor_visible(false)
2029canvas_set_update_interval(25)
2030canvas_set_bg_color(0)
2031canvas_set_opacity(0)
2032
2033origin_fx_sequence()
2034
2035canvas_set_palette("md_title.pal", 0)
2036canvas_hide_all_sprites()
2037play_intro()
2038
2039if main_menu() == "Q" then -- returns "Q" for quit or "J" for Journey Onward
2040   hide_mouse_cursor()
2041   fade_out(6)
2042   config_set("config/quit", true)
2043end
2044
2045music_stop()
2046canvas_hide()
2047