forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmauville_old_man.c
1505 lines (1359 loc) · 44.3 KB
/
mauville_old_man.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "main.h"
#include "constants/songs.h"
#include "constants/event_objects.h"
#include "mauville_old_man.h"
#include "event_data.h"
#include "string_util.h"
#include "text.h"
#include "easy_chat.h"
#include "script.h"
#include "random.h"
#include "event_scripts.h"
#include "task.h"
#include "menu.h"
#include "m4a.h"
#include "bard_music.h"
#include "sound.h"
#include "strings.h"
#include "overworld.h"
#include "field_message_box.h"
#include "script_menu.h"
#include "trader.h"
#include "m4a.h"
#include "constants/mauville_old_man.h"
static void InitGiddyTaleList(void);
static void StartBardSong(bool8 useNewSongLyrics);
static void Task_BardSong(u8 taskId);
static void StorytellerSetup(void);
static void Storyteller_ResetFlag(void);
static u8 sSelectedStory;
COMMON_DATA struct BardSong gBardSong = {0};
static EWRAM_DATA u16 sUnusedPitchTableIndex = 0;
static EWRAM_DATA struct MauvilleManStoryteller * sStorytellerPtr = NULL;
static EWRAM_DATA u8 sStorytellerWindowId = 0;
static const u16 sDefaultBardSongLyrics[NUM_BARD_SONG_WORDS] = {
#if FRENCH
EC_WORD_FEELING,
EC_WORD_DIET,
EC_WORD_IT_S,
EC_EMPTY_WORD,
EC_WORD_COOL,
EC_EMPTY_WORD
#elif ITALIAN
EC_WORD_DANCE,
EC_WORD_WHICH,
EC_WORD_PLAYED,
EC_WORD_MAGAZINE,
EC_WORD_LISTEN,
EC_WORD_PLUS,
#elif SPANISH
EC_WORD_WINNER,
EC_WORD_HIYAH,
EC_WORD_WINNER,
EC_WORD_HIYAH,
EC_WORD_WINNER,
EC_WORD_HIYAH,
#else //ENGLISH
EC_WORD_SHAKE,
EC_WORD_IT,
EC_WORD_DO,
EC_WORD_THE,
EC_WORD_DIET,
EC_WORD_DANCE
#endif
};
static const u8 * const sGiddyAdjectives[] = {
GiddyText_SoPretty,
GiddyText_SoDarling,
GiddyText_SoRelaxed,
GiddyText_SoSunny,
GiddyText_SoDesirable,
GiddyText_SoExciting,
GiddyText_SoAmusing,
GiddyText_SoMagical
};
// Non-random lines Giddy can say. Not all are strictly
// questions, but most are, and the player will receive
// a Yes/No prompt afterwards regardless.
static const u8 * const sGiddyQuestions[GIDDY_MAX_QUESTIONS] = {
GiddyText_ISoWantToGoOnAVacation,
GiddyText_IBoughtCrayonsWith120Colors,
GiddyText_WouldntItBeNiceIfWeCouldFloat,
GiddyText_WhenYouWriteOnASandyBeach,
GiddyText_WhatsTheBottomOfTheSeaLike,
GiddyText_WhenYouSeeTheSettingSunDoesIt,
GiddyText_LyingBackInTheGreenGrass,
GiddyText_SecretBasesAreSoWonderful
};
static void SetupBard(void)
{
u16 i;
struct MauvilleManBard *bard = &gSaveBlock1Ptr->oldMan.bard;
bard->id = MAUVILLE_MAN_BARD;
bard->hasChangedSong = FALSE;
bard->language = gGameLanguage;
for (i = 0; i < NUM_BARD_SONG_WORDS; i++)
bard->songLyrics[i] = sDefaultBardSongLyrics[i];
}
static void SetupHipster(void)
{
struct MauvilleManHipster *hipster = &gSaveBlock1Ptr->oldMan.hipster;
hipster->id = MAUVILLE_MAN_HIPSTER;
hipster->taughtWord = FALSE;
hipster->language = gGameLanguage;
}
static void SetupStoryteller(void)
{
StorytellerSetup();
}
static void SetupGiddy(void)
{
struct MauvilleManGiddy *giddy = &gSaveBlock1Ptr->oldMan.giddy;
giddy->id = MAUVILLE_MAN_GIDDY;
giddy->taleCounter = 0;
giddy->language = gGameLanguage;
}
static void SetupTrader(void)
{
TraderSetup();
}
void SetMauvilleOldMan(void)
{
u16 trainerId = (gSaveBlock2Ptr->playerTrainerId[1] << 8) | gSaveBlock2Ptr->playerTrainerId[0];
// Determine man based on the last digit of the player's trainer ID.
switch ((trainerId % 10) / 2)
{
case MAUVILLE_MAN_BARD:
SetupBard();
break;
case MAUVILLE_MAN_HIPSTER:
SetupHipster();
break;
case MAUVILLE_MAN_TRADER:
SetupTrader();
break;
case MAUVILLE_MAN_STORYTELLER:
SetupStoryteller();
break;
case MAUVILLE_MAN_GIDDY:
SetupGiddy();
break;
}
SetMauvilleOldManObjEventGfx();
}
u8 GetCurrentMauvilleOldMan(void)
{
return gSaveBlock1Ptr->oldMan.common.id;
}
void Script_GetCurrentMauvilleMan(void)
{
gSpecialVar_Result = GetCurrentMauvilleOldMan();
}
void HasBardSongBeenChanged(void)
{
gSpecialVar_Result = (&gSaveBlock1Ptr->oldMan.bard)->hasChangedSong;
}
void SaveBardSongLyrics(void)
{
u16 i;
struct MauvilleManBard *bard = &gSaveBlock1Ptr->oldMan.bard;
StringCopy(bard->playerName, gSaveBlock2Ptr->playerName);
for (i = 0; i < TRAINER_ID_LENGTH; i++)
bard->playerTrainerId[i] = gSaveBlock2Ptr->playerTrainerId[i];
for (i = 0; i < NUM_BARD_SONG_WORDS; i++)
bard->songLyrics[i] = bard->newSongLyrics[i];
bard->hasChangedSong = TRUE;
}
// Copies lyrics into gStringVar4.
// gSpecialVar_0x8004 is used in these functions to indicate which song should be played.
// If it's set to 0 the Bard's current song should be played, otherwise the new user-provided song should be played.
// Its set in the scripts right before 'PlayBardSong' is called.
static void PrepareSongText(void)
{
struct MauvilleManBard *bard = &gSaveBlock1Ptr->oldMan.bard;
u16 * lyrics = !gSpecialVar_0x8004 ? bard->songLyrics : bard->newSongLyrics;
u8 * wordEnd = gStringVar4;
u8 * str = wordEnd;
u16 paragraphNum;
// Easy chat "words" aren't strictly single words, e.g. EC_WORD_MATCH_UP is the string "MATCH UP".
// The bard song needs to know when it's at the end of an easy chat word and not just at a space in
// the middle of one, so the loop below will replace spaces in each easy chat word with CHAR_BARD_WORD_DELIMIT.
// When it comes time to print the song's text all the CHAR_BARD_WORD_DELIMIT will get replaced with CHAR_SPACE.
//
// The song text will be displayed in two paragraphs, each containing 3 easy chat words (2 on the first line and 1 on the second).
for (paragraphNum = 0; paragraphNum < 2; paragraphNum++)
{
// Line 1, 1st word
wordEnd = CopyEasyChatWord(wordEnd, *(lyrics++));
while (wordEnd != str)
{
if (*str == CHAR_SPACE)
*str = CHAR_BARD_WORD_DELIMIT;
str++;
}
str++;
*(wordEnd++) = CHAR_SPACE;
// Line 1, 2nd word
wordEnd = CopyEasyChatWord(wordEnd, *(lyrics++));
while (wordEnd != str)
{
if (*str == CHAR_SPACE)
*str = CHAR_BARD_WORD_DELIMIT;
str++;
}
str++;
*(wordEnd++) = CHAR_NEWLINE;
// Line 2, 1st word
wordEnd = CopyEasyChatWord(wordEnd, *(lyrics++));
while (wordEnd != str)
{
if (*str == CHAR_SPACE)
*str = CHAR_BARD_WORD_DELIMIT;
str++;
}
if (paragraphNum == 0)
{
// Erase the 1st paragraph for displaying the 2nd.
// The == 0 check assumes there are only 2 paragraphs.
*(wordEnd++) = EXT_CTRL_CODE_BEGIN;
*(wordEnd++) = EXT_CTRL_CODE_FILL_WINDOW;
}
}
}
void PlayBardSong(void)
{
StartBardSong(gSpecialVar_0x8004);
ScriptContext_Stop();
}
void HasHipsterTaughtWord(void)
{
gSpecialVar_Result = (&gSaveBlock1Ptr->oldMan.hipster)->taughtWord;
}
void SetHipsterTaughtWord(void)
{
(&gSaveBlock1Ptr->oldMan.hipster)->taughtWord = TRUE;
}
void HipsterTryTeachWord(void)
{
u16 word = UnlockRandomTrendySaying();
if (word == EC_EMPTY_WORD)
{
// All words already unlocked
gSpecialVar_Result = FALSE;
}
else
{
CopyEasyChatWord(gStringVar1, word);
gSpecialVar_Result = TRUE;
}
}
void GiddyShouldTellAnotherTale(void)
{
struct MauvilleManGiddy *giddy = &gSaveBlock1Ptr->oldMan.giddy;
if (giddy->taleCounter == GIDDY_MAX_TALES)
{
gSpecialVar_Result = FALSE;
giddy->taleCounter = 0;
}
else
{
gSpecialVar_Result = TRUE;
}
}
void GenerateGiddyLine(void)
{
struct MauvilleManGiddy *giddy = &gSaveBlock1Ptr->oldMan.giddy;
if (giddy->taleCounter == 0)
InitGiddyTaleList();
// A line from Giddy is either a line following this format:
// "{random word} is so {adjective}! Don't you agree?",
// or one of the texts in sGiddyQuestions.
if (giddy->randomWords[giddy->taleCounter] != EC_EMPTY_WORD)
{
u8 *stringPtr;
u32 adjective = Random();
adjective %= ARRAY_COUNT(sGiddyAdjectives);
stringPtr = CopyEasyChatWord(gStringVar4, giddy->randomWords[giddy->taleCounter]);
stringPtr = StringCopy(stringPtr, GiddyText_Is);
stringPtr = StringCopy(stringPtr, sGiddyAdjectives[adjective]);
StringCopy(stringPtr, GiddyText_DontYouAgree);
}
else
{
StringCopy(gStringVar4, sGiddyQuestions[giddy->questionList[giddy->questionNum++]]);
}
// 10% chance for Giddy to stop telling tales.
if (!(Random() % 10))
giddy->taleCounter = GIDDY_MAX_TALES;
else
giddy->taleCounter++;
gSpecialVar_Result = TRUE;
}
static void InitGiddyTaleList(void)
{
struct MauvilleManGiddy *giddy = &gSaveBlock1Ptr->oldMan.giddy;
u16 wordGroupsAndCount[][2] = {
{EC_GROUP_POKEMON, 0},
{EC_GROUP_LIFESTYLE, 0},
{EC_GROUP_HOBBIES, 0},
{EC_GROUP_MOVE_1, 0},
{EC_GROUP_MOVE_2, 0},
{EC_GROUP_POKEMON_NATIONAL, 0}
};
u16 i;
u16 totalWords;
u16 temp;
u16 var; // re-used
// Shuffle question list
for (i = 0; i < GIDDY_MAX_QUESTIONS; i++)
giddy->questionList[i] = i;
for (i = 0; i < GIDDY_MAX_QUESTIONS; i++)
{
var = Random() % (i + 1);
SWAP(giddy->questionList[i], giddy->questionList[var], temp);
}
// Count total number of words in above word groups
totalWords = 0;
for (i = 0; i < ARRAY_COUNT(wordGroupsAndCount); i++)
{
wordGroupsAndCount[i][1] = EasyChat_GetNumWordsInGroup(wordGroupsAndCount[i][0]);
totalWords += wordGroupsAndCount[i][1];
}
giddy->questionNum = 0;
temp = 0;
for (i = 0; i < GIDDY_MAX_TALES; i++)
{
var = Random() % 10;
if (var < 3 && temp < GIDDY_MAX_QUESTIONS)
{
// 30% chance for word to be empty (in which case Giddy
// will say one of his non-random questions), unless
// the limit for questions has been reached already.
giddy->randomWords[i] = EC_EMPTY_WORD;
temp++;
}
else
{
// Pick a random word id, then advance through the word
// groups until the group where that id landed.
s16 randWord = Random() % totalWords;
for (var = 0; i < ARRAY_COUNT(wordGroupsAndCount); var++)
if ((randWord -= wordGroupsAndCount[var][1]) <= 0)
break;
if (var == ARRAY_COUNT(wordGroupsAndCount))
var = 0;
// Save the randomly selected word
giddy->randomWords[i] = GetRandomEasyChatWordFromUnlockedGroup(wordGroupsAndCount[var][0]);
}
}
}
static void ResetBardFlag(void)
{
(&gSaveBlock1Ptr->oldMan.bard)->hasChangedSong = FALSE;
}
static void ResetHipsterFlag(void)
{
(&gSaveBlock1Ptr->oldMan.hipster)->taughtWord = FALSE;
}
static void ResetTraderFlag(void)
{
Trader_ResetFlag();
}
static void ResetStorytellerFlag(void)
{
Storyteller_ResetFlag();
}
void ResetMauvilleOldManFlag(void)
{
switch (GetCurrentMauvilleOldMan())
{
case MAUVILLE_MAN_BARD:
ResetBardFlag();
break;
case MAUVILLE_MAN_HIPSTER:
ResetHipsterFlag();
break;
case MAUVILLE_MAN_STORYTELLER:
ResetStorytellerFlag();
break;
case MAUVILLE_MAN_TRADER:
ResetTraderFlag();
break;
case MAUVILLE_MAN_GIDDY:
break;
}
SetMauvilleOldManObjEventGfx();
}
// States and task data for Task_BardSong.
// The function BardSing receives this task as an
// argument and reads its state as well.
enum {
BARD_STATE_INIT,
BARD_STATE_WAIT_BGM,
BARD_STATE_GET_WORD,
BARD_STATE_HANDLE_WORD,
BARD_STATE_WAIT_WORD,
BARD_STATE_PAUSE,
};
#define tState data[0]
#define tWordState data[1]
#define tDelay data[2]
#define tCharIndex data[3]
#define tLyricsIndex data[4]
#define tUseNewSongLyrics data[5]
// Takes a 16-bit easy chat word value and returns a value 0-4 (i.e. a value less than NUM_BARD_PITCH_TABLES_PER_SIZE).
// The relationship between the easy chat word and the chosen pitch table is essentially arbitrary.
// This value will be used twice; once for an unused variable, and again to select a pitch table in CalcWordSounds.
#define WORD_TO_PITCH_TABLE_INDEX(a) ( MOD(a, (NUM_BARD_PITCH_TABLES_PER_SIZE-1)) + (((a) >> 3) & 1) )
static void StartBardSong(bool8 useNewSongLyrics)
{
u8 taskId = CreateTask(Task_BardSong, 80);
gTasks[taskId].tUseNewSongLyrics = useNewSongLyrics;
}
static void EnableTextPrinters(void)
{
gDisableTextPrinters = FALSE;
}
static void DisableTextPrinters(struct TextPrinterTemplate * printer, u16 renderCmd)
{
gDisableTextPrinters = TRUE;
}
static void DrawSongTextWindow(const u8 * str)
{
DrawDialogueFrame(0, FALSE);
AddTextPrinterParameterized(0, FONT_NORMAL, str, 0, 1, 1, DisableTextPrinters);
gDisableTextPrinters = TRUE;
CopyWindowToVram(0, COPYWIN_FULL);
}
#define BARD_SONG_BASE_VOLUME 0x100
#define BARD_SONG_BASE_PITCH 0x200
enum {
SOUND_STATE_START,
SOUND_STATE_PLAY,
SOUND_STATE_SET_BASE,
SOUND_STATE_END,
SOUND_STATE_WAIT,
};
// Sing one frame of the bard's song. 'task' is a pointer to Task_BardSong, which handles changing the states in here.
static void BardSing(struct Task *task, struct BardSong *song)
{
switch (task->tState)
{
case BARD_STATE_INIT:
{
struct MauvilleManBard *bard = &gSaveBlock1Ptr->oldMan.bard;
u16 *lyrics;
s32 i;
// Copy lyrics
if (!gSpecialVar_0x8004)
lyrics = bard->songLyrics;
else
lyrics = bard->newSongLyrics;
for (i = 0; i < NUM_BARD_SONG_WORDS; i++)
song->lyrics[i] = lyrics[i];
song->lyricsIndex = 0;
break;
}
case BARD_STATE_GET_WORD:
{
u16 easyChatWord = song->lyrics[song->lyricsIndex];
song->soundTemplates = GetWordSoundTemplates(easyChatWord);
CalcWordSounds(song, WORD_TO_PITCH_TABLE_INDEX(easyChatWord));
song->lyricsIndex++;
if (song->soundTemplates[0].songId != PHONEME_ID_NONE)
{
// Word has valid sounds, begin playing.
song->state = SOUND_STATE_START;
}
else
{
// Word has no valid sounds, skip to the end.
song->state = SOUND_STATE_END;
song->timer = 2;
}
break;
}
case BARD_STATE_HANDLE_WORD:
case BARD_STATE_WAIT_WORD:
{
const struct BardSoundTemplate *template = &song->soundTemplates[song->soundIndex];
switch (song->state)
{
case SOUND_STATE_START:
song->timer = song->sounds[song->soundIndex].length;
if (template->songId < NUM_PHONEME_SONGS)
{
// Phoneme "songs" come in triplets of PH_*_BLEND, PH_*_HELD, and PH_*_SOLO.
// The division then multiplication by 3 below is rounding any value from one of these triplets to a PH_*_HELD.
// This means the actual song files for any phoneme other than PH_*_HELD won't be played here, and the only difference
// when specifying a PH_*_BLEND or PH_*_SOLO in the songId will be the length of the sound, determined by 'sPhonemeLengths'.
u8 phonemeTripletId = template->songId / 3;
m4aSongNumStart((FIRST_PHONEME_SONG + 1) + phonemeTripletId * 3);
}
song->state = SOUND_STATE_SET_BASE;
song->timer--;
break;
case SOUND_STATE_SET_BASE:
song->state = SOUND_STATE_PLAY;
if (template->songId < NUM_PHONEME_SONGS)
{
// Adjust the song volume for the current phoneme.
// In practice no phonemes use this, so volume here will always be BARD_SONG_BASE_VOLUME.
song->volume = BARD_SONG_BASE_VOLUME + template->volume * 16;
m4aMPlayVolumeControl(&gMPlayInfo_SE2, TRACKS_ALL, song->volume);
// Adjust the song pitch for the current phoneme.
song->pitch = BARD_SONG_BASE_PITCH + song->sounds[song->soundIndex].pitch;
m4aMPlayPitchControl(&gMPlayInfo_SE2, TRACKS_ALL, song->pitch);
}
break;
case SOUND_STATE_PLAY:
// Modulate the volume and pitch to make it sound a little more like singing.
if (song->voiceInflection > 10)
song->volume -= 2;
if (song->voiceInflection & 1)
song->pitch += 64;
else
song->pitch -= 64;
m4aMPlayVolumeControl(&gMPlayInfo_SE2, TRACKS_ALL, song->volume);
m4aMPlayPitchControl(&gMPlayInfo_SE2, TRACKS_ALL, song->pitch);
song->voiceInflection++;
song->timer--;
if (song->timer == 0)
{
if (++song->soundIndex != MAX_BARD_SOUNDS_PER_WORD && song->soundTemplates[song->soundIndex].songId != PHONEME_ID_NONE)
{
// There are more sounds to play for this word, return to the start.
song->state = SOUND_STATE_START;
}
else
{
// We've reached the final sound for this word, stop playing.
song->state = SOUND_STATE_END;
song->timer = 2;
}
}
break;
case SOUND_STATE_END:
// Delay, then stop playing the phoneme.
if (--song->timer == 0)
{
m4aMPlayStop(&gMPlayInfo_SE2);
song->state = SOUND_STATE_WAIT; // We'll remain stuck at this sound state until Task_BardSong changes states from HANDLE_WORD/WAIT_WORD
}
break;
}
break;
}
case BARD_STATE_PAUSE:
case BARD_STATE_WAIT_BGM:
// Non-singing states.
break;
}
}
static void Task_BardSong(u8 taskId)
{
struct Task *task = &gTasks[taskId];
BardSing(task, &gBardSong);
switch (task->tState)
{
case BARD_STATE_INIT:
PrepareSongText();
DrawSongTextWindow(gStringVar4);
task->tWordState = 0;
task->tDelay = 0;
task->tCharIndex = 0;
task->tLyricsIndex = 0;
FadeOutBGMTemporarily(4);
task->tState = BARD_STATE_WAIT_BGM;
break;
case BARD_STATE_WAIT_BGM:
if (IsBGMPausedOrStopped())
task->tState = BARD_STATE_GET_WORD;
break;
case BARD_STATE_GET_WORD:
{
struct MauvilleManBard *bard = &gSaveBlock1Ptr->oldMan.bard;
u8 *str = &gStringVar4[task->tCharIndex];
u16 wordLen = 0;
// Read letters until delimiter
while (*str != CHAR_SPACE
&& *str != CHAR_NEWLINE
&& *str != EXT_CTRL_CODE_BEGIN
&& *str != EOS)
{
str++;
wordLen++;
}
// sUnusedPitchTableIndex is never read. For debugging perhaps, or one of the other languages.
if (!task->tUseNewSongLyrics)
sUnusedPitchTableIndex = WORD_TO_PITCH_TABLE_INDEX(bard->songLyrics[task->tLyricsIndex]);
else
sUnusedPitchTableIndex = WORD_TO_PITCH_TABLE_INDEX(bard->newSongLyrics[task->tLyricsIndex]);
gBardSong.length /= wordLen;
if (gBardSong.length <= 0)
gBardSong.length = 1;
task->tLyricsIndex++;
if (task->tDelay == 0)
{
task->tState = BARD_STATE_HANDLE_WORD;
task->tWordState = 0;
}
else
{
task->tState = BARD_STATE_PAUSE;
task->tWordState = 0;
}
}
break;
case BARD_STATE_PAUSE:
// Wait before singing next word
if (task->tDelay == 0)
task->tState = BARD_STATE_HANDLE_WORD;
else
task->tDelay--;
break;
case BARD_STATE_HANDLE_WORD:
if (gStringVar4[task->tCharIndex] == EOS)
{
// End song
FadeInBGM(6);
m4aMPlayFadeOutTemporarily(&gMPlayInfo_SE2, 2);
ScriptContext_Enable();
DestroyTask(taskId);
}
else if (gStringVar4[task->tCharIndex] == CHAR_SPACE)
{
// End of easy chat word, move on to the next one.
EnableTextPrinters();
task->tCharIndex++;
task->tState = BARD_STATE_GET_WORD;
task->tDelay = 0;
}
else if (gStringVar4[task->tCharIndex] == CHAR_NEWLINE)
{
// Handle newline
task->tCharIndex++;
task->tState = BARD_STATE_GET_WORD;
task->tDelay = 0;
}
else if (gStringVar4[task->tCharIndex] == EXT_CTRL_CODE_BEGIN)
{
// Handle ctrl code
// The only expected ctrl codes are those for clearing the end of the paragraph,
// so this assumes there's a new word coming and does a short delay before the next paragraph.
task->tCharIndex += 2; // skip over control codes
task->tState = BARD_STATE_GET_WORD;
task->tDelay = 8;
}
else if (gStringVar4[task->tCharIndex] == CHAR_BARD_WORD_DELIMIT)
{
// Space within the current easy chat word (see PrepareSongText), just replace it with a real space.
gStringVar4[task->tCharIndex] = CHAR_SPACE;
EnableTextPrinters();
task->tCharIndex++;
task->tDelay = 0;
}
else
{
// Handle regular word
switch (task->tWordState)
{
case 0:
EnableTextPrinters();
task->tWordState++;
break;
case 1:
task->tWordState++;
break;
case 2:
task->tCharIndex++;
task->tWordState = 0;
task->tDelay = gBardSong.length;
task->tState = BARD_STATE_WAIT_WORD;
break;
}
}
break;
case BARD_STATE_WAIT_WORD:
// Wait for word to finish being sung.
// BardSing will continue to play it.
task->tDelay--;
if (task->tDelay == 0)
task->tState = BARD_STATE_HANDLE_WORD;
break;
}
RunTextPrintersAndIsPrinter0Active();
}
void SetMauvilleOldManObjEventGfx(void)
{
VarSet(VAR_OBJ_GFX_ID_0, OBJ_EVENT_GFX_BARD);
}
// Language fixers?
void SanitizeMauvilleOldManForRuby(union OldMan * oldMan)
{
s32 i;
u8 playerName[PLAYER_NAME_LENGTH + 1];
switch (oldMan->common.id)
{
case MAUVILLE_MAN_TRADER:
{
struct MauvilleOldManTrader * trader = &oldMan->trader;
for (i = 0; i < NUM_TRADER_ITEMS; i++)
{
if (trader->language[i] == LANGUAGE_JAPANESE)
ConvertInternationalString(trader->playerNames[i], LANGUAGE_JAPANESE);
}
break;
}
case MAUVILLE_MAN_STORYTELLER:
{
struct MauvilleManStoryteller * storyteller = &oldMan->storyteller;
for (i = 0; i < NUM_STORYTELLER_TALES; i++)
{
if (storyteller->gameStatIDs[i] != 0)
{
memcpy(playerName, storyteller->trainerNames[i], PLAYER_NAME_LENGTH);
playerName[PLAYER_NAME_LENGTH] = EOS;
if (IsStringJapanese(playerName))
{
memset(playerName, CHAR_SPACE, PLAYER_NAME_LENGTH + 1);
StringCopy(playerName, gText_Friend);
memcpy(storyteller->trainerNames[i], playerName, PLAYER_NAME_LENGTH);
storyteller->language[i] = GAME_LANGUAGE;
}
}
}
break;
}
}
}
static void UNUSED SetMauvilleOldManLanguage(union OldMan * oldMan, u32 language1, u32 language2, u32 language3)
{
s32 i;
switch (oldMan->common.id)
{
case MAUVILLE_MAN_TRADER:
{
struct MauvilleOldManTrader * trader = &oldMan->trader;
for (i = 0; i < NUM_TRADER_ITEMS; i++)
{
if (IsStringJapanese(trader->playerNames[i]))
trader->language[i] = language1;
else
trader->language[i] = language2;
}
}
break;
case MAUVILLE_MAN_STORYTELLER:
{
struct MauvilleManStoryteller * storyteller = &oldMan->storyteller;
for (i = 0; i < NUM_STORYTELLER_TALES; i++)
{
if (IsStringJapanese(storyteller->trainerNames[i]))
storyteller->language[i] = language1;
else
storyteller->language[i] = language2;
}
}
break;
case MAUVILLE_MAN_BARD:
{
struct MauvilleManBard * bard = &oldMan->bard;
if (language3 == LANGUAGE_JAPANESE)
bard->language = language1;
else
bard->language = language2;
}
break;
case MAUVILLE_MAN_HIPSTER:
{
struct MauvilleManHipster * hipster = &oldMan->hipster;
if (language3 == LANGUAGE_JAPANESE)
hipster->language = language1;
else
hipster->language = language2;
}
break;
case MAUVILLE_MAN_GIDDY:
{
struct MauvilleManGiddy * giddy = &oldMan->giddy;
if (language3 == LANGUAGE_JAPANESE)
giddy->language = language1;
else
giddy->language = language2;
}
break;
}
}
void SanitizeReceivedEmeraldOldMan(union OldMan * oldMan, u32 version, u32 language)
{
u8 playerName[PLAYER_NAME_LENGTH + 1];
s32 i;
if (oldMan->common.id == MAUVILLE_MAN_STORYTELLER && language == LANGUAGE_JAPANESE)
{
struct MauvilleManStoryteller * storyteller = &oldMan->storyteller;
for (i = 0; i < NUM_STORYTELLER_TALES; i++)
{
if (storyteller->gameStatIDs[i] != 0)
{
memcpy(playerName, storyteller->trainerNames[i], PLAYER_NAME_LENGTH);
playerName[PLAYER_NAME_LENGTH] = EOS;
if (IsStringJapanese(playerName))
storyteller->language[i] = LANGUAGE_JAPANESE;
else
storyteller->language[i] = GAME_LANGUAGE;
}
}
}
}
void SanitizeReceivedRubyOldMan(union OldMan * oldMan, u32 version, u32 language)
{
bool32 isRuby = (version == VERSION_SAPPHIRE || version == VERSION_RUBY);
switch (oldMan->common.id)
{
case MAUVILLE_MAN_TRADER:
{
struct MauvilleOldManTrader * trader = &oldMan->trader;
s32 i;
if (isRuby)
{
for (i = 0; i < NUM_TRADER_ITEMS; i++)
{
u8 * str = trader->playerNames[i];
if (str[0] == EXT_CTRL_CODE_BEGIN && str[1] == EXT_CTRL_CODE_JPN)
{
StripExtCtrlCodes(str);
trader->language[i] = LANGUAGE_JAPANESE;
}
else
{
trader->language[i] = language;
}
}
}
else
{
for (i = 0; i < NUM_TRADER_ITEMS; i++)
{
if (trader->language[i] == LANGUAGE_JAPANESE)
{
StripExtCtrlCodes(trader->playerNames[i]);
}
}
}
}
break;
case MAUVILLE_MAN_STORYTELLER:
{
struct MauvilleManStoryteller * storyteller = &oldMan->storyteller;
s32 i;
if (isRuby)
{
for (i = 0; i < NUM_STORYTELLER_TALES; i++)
{
if (storyteller->gameStatIDs[i] != 0)
storyteller->language[i] = language;
}
}
}
break;
case MAUVILLE_MAN_BARD:
{
struct MauvilleManBard * bard = &oldMan->bard;
if (isRuby)
{
bard->language = language;
}
}
break;
case MAUVILLE_MAN_HIPSTER:
{
struct MauvilleManHipster * hipster = &oldMan->hipster;
if (isRuby)
{
hipster->language = language;
}
}
break;
case MAUVILLE_MAN_GIDDY:
{
struct MauvilleManGiddy * giddy = &oldMan->giddy;
if (isRuby)
{
giddy->language = language;
}
}
break;
}
}
struct Story
{
u8 stat;
u8 minVal;
const u8 *title;
const u8 *action;
const u8 *fullText;
};