summaryrefslogtreecommitdiff
path: root/arm9/src/unk_020139D8.c
blob: d88ca02d9889cee763d8273124a6401729404dc8 (plain)
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
#include "global.h"
#include "msgdata.h"
#include "heap.h"
#include "save_block_2.h"
#include "math_util.h"
#include "unk_020139D8.h"
#include "msgdata/msg.naix"

#pragma thumb on

const u8 sNarcLanguages[][2] = {
    { LANGUAGE_JAPANESE, 0 },
    { LANGUAGE_ENGLISH,  1 },
    { LANGUAGE_FRENCH,   2 },
    { LANGUAGE_ITALIAN,  3 },
    { LANGUAGE_GERMAN,   4 },
    { LANGUAGE_SPANISH,  5 },
};

const u16 sNarcMsgBanks[] = {
    NARC_msg_narc_0362_bin, // Species names
    NARC_msg_narc_0589_bin, // Move names
    NARC_msg_narc_0565_bin, // Type names
    NARC_msg_narc_0553_bin, // Ability names
    NARC_msg_narc_0388_bin, // Trainer
    NARC_msg_narc_0389_bin, // People
    NARC_msg_narc_0390_bin, // Greetings
    NARC_msg_narc_0391_bin, // Lifestyle
    NARC_msg_narc_0392_bin, // Feelings
    NARC_msg_narc_0393_bin, // Tough words
    NARC_msg_narc_0394_bin  // Union
};

const u16 sNarcMsgCounts[] = {
    496, // Species names
    468, // Move names
    18,  // Type names
    124, // Ability names
    38,  // Trainer
    38,  // People
    107, // Greetings
    104, // Lifestyle
    47,  // Feelings
    32,  // Tough words
    23   // Union
};

const u16 UNK_020ED580[] = { 0x04C0, 0x04F0 };
const u16 UNK_020ED5A8[] = { 0x04A7, 0x04E1 };
const u16 UNK_020ED5AC[] = { 0x0479, 0x0498, 0x049B, 0x049C };
const u16 UNK_020ED5A0[] = { 0x048F, 0x0497 };
const u16 UNK_020ED59C[] = { 0x04FC, 0x0596 };
const u16 UNK_020ED594[] = { 0x047A, 0x048D };
const u16 UNK_020ED58C[] = { 0x0441, 0x0599 };
const u16 UNK_020ED590[] = { 0x042C, 0x059D };
const u16 UNK_020ED5A4[] = { 0x04B0, 0x04FF };
const u16 UNK_020ED598[] = { 0x04B6, 0x0505 };
const u16 UNK_020ED584[] = { 0x024E, 0x03D2 };
const u16 UNK_020ED588[] = { 0x04BB, 0x0500 };

const struct UnkStruct_020ED5EC
{
    const u16 * data;
    s32 count;
} UNK_020ED5EC[] = {
    { UNK_020ED58C, NELEMS(UNK_020ED58C) },
    { UNK_020ED580, NELEMS(UNK_020ED580) },
    { UNK_020ED5A8, NELEMS(UNK_020ED5A8) },
    { UNK_020ED5AC, NELEMS(UNK_020ED5AC) },
    { UNK_020ED5A0, NELEMS(UNK_020ED5A0) },
    { UNK_020ED59C, NELEMS(UNK_020ED59C) },
    { UNK_020ED588, NELEMS(UNK_020ED588) },
    { UNK_020ED584, NELEMS(UNK_020ED584) },
    { UNK_020ED590, NELEMS(UNK_020ED590) },
    { UNK_020ED5A4, NELEMS(UNK_020ED5A4) },
    { UNK_020ED598, NELEMS(UNK_020ED598) },
    { UNK_020ED594, NELEMS(UNK_020ED594) },
};

struct UnkStruct_020139D8 * EasyChatManager_new(u32 heap_id)
{
    struct UnkStruct_020139D8 * ret = (struct UnkStruct_020139D8 *)AllocFromHeap(heap_id, sizeof(struct UnkStruct_020139D8));
    s32 i;
    for (i = 0; i < 11; i++)
    {
        ret->heap_id = heap_id; // inadvertently inside the loop
        ret->msgDatas[i] = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, sNarcMsgBanks[i], heap_id);
    }
    return ret;
}

void EasyChatManager_delete(struct UnkStruct_020139D8 * unk)
{
    s32 i;
    for (i = 0; i < 11; i++)
    {
        DestroyMsgData(unk->msgDatas[i]);
    }
    FreeToHeap(unk);
}

void EasyChatManager_ReadWordIntoString(struct UnkStruct_020139D8 * unk, u16 wordIdx, struct String * str)
{
    s32 msgBank;
    s32 msgNo;
    GetCategoryAndMsgNoByECWordIdx(wordIdx, &msgBank, &msgNo);
    ReadMsgDataIntoString(unk->msgDatas[msgBank], (u32)msgNo, str);
}

void GetECWordIntoStringByIndex(u16 wordIdx, struct String * a1)
{
    s32 msgBank;
    s32 msgNo;
    if (wordIdx != 0xFFFF)
    {
        GetCategoryAndMsgNoByECWordIdx(wordIdx, &msgBank, &msgNo);
        msgBank = sNarcMsgBanks[msgBank];
        ReadMsgData_NewNarc_ExistingString(NARC_MSGDATA_MSG, (u32)msgBank, (u32)msgNo, 0, a1);
    }
    else
        StringSetEmpty(a1);
}

u16 GetECWordIndexByPair(u16 msgBank, u16 msgNo)
{
    u32 i;
    u16 k;
    u16 j;
    for (i = 0; i < 11; i++)
    {
        if (msgBank == sNarcMsgBanks[i])
        {
            for (j = 0, k = 0; j < i; j++)
                k += sNarcMsgCounts[j];
            return (u16)(k + msgNo);
        }
    }
    return 0xFFFF;
}

void GetCategoryAndMsgNoByECWordIdx(u32 wordIdx, s32 * msgBank_p, s32 * msgNo_p)
{
    s32 i;
    s32 j;
    u32 r3;

    r3 = wordIdx & 0xFFF;
    j = 0;

    for (i = 0; i < NELEMS(sNarcMsgCounts); i++)
    {
        j += sNarcMsgCounts[i];
        if (r3 < j)
        {
            *msgBank_p = i;
            *msgNo_p = (s32)(r3 - (j - sNarcMsgCounts[i]));
            return;
        }
    }
}

u32 FUN_02013B28(void)
{
    return sizeof(struct UnkStruct_02013B28);
}

void FUN_02013B2C(struct UnkStruct_02013B28 * unk)
{
    u32 i;
    unk->unk_0 = 0;
    unk->unk_4 = 0;
    for (i = 0; i < 6; i++)
    {
        if (sNarcLanguages[i][0] == GAME_LANGUAGE)
        {
            FUN_02013C18(unk, sNarcLanguages[i][1]);
            break;
        }
    }
}

struct UnkStruct_02013B28 * FUN_02013B5C(struct SaveBlock2 * sav2)
{
    return (struct UnkStruct_02013B28 *)SavArray_get(sav2, 34);
}

BOOL FUN_02013B68(struct UnkStruct_02013B28 * unk, u32 a1)
{
    return (BOOL)((unk->unk_4 >> a1) & 1);
}

s32 FUN_02013B74(struct UnkStruct_02013B28 * unk)
{
    u32 i;
    u32 count;
    u32 which_bit;
    for (i = 0, count = 0; i < 32; i++)
    {
        if (!((unk->unk_4 >> i) & 1))
            count++;
    }
    if (count != 0)
    {
        which_bit = LCRandom() % count;
        for (i = 0; i < 32; i++)
        {
            if (!((unk->unk_4 >> i) & 1))
            {
                if (which_bit == 0)
                {
                    unk->unk_4 |= (1 << i);
                    return (s32)i;
                }
                which_bit--;
            }
        }
    }
    return 32;
}

BOOL FUN_02013BC8(struct UnkStruct_02013B28 * unk)
{
    u32 i;
    for (i = 0; i < 32; i++)
    {
        if (!((unk->unk_4 >> i) & 1))
            return FALSE;
    }
    return TRUE;
}

u16 FUN_02013BE4(u16 a0)
{
    s32 i;
    u16 skip = 0;
    for (i = 0; i < 9; i++)
        skip += sNarcMsgCounts[i];
    return (u16)(skip + a0);
}

BOOL FUN_02013C0C(struct UnkStruct_02013B28 * unk, u32 a1)
{
    return (BOOL)((unk->unk_0 >> a1) & 1);
}

void FUN_02013C18(struct UnkStruct_02013B28 * unk, u32 a1)
{
    unk->unk_0 |= (1 << a1);
}

s32 FUN_02013C28(u16 a0)
{
    s32 r3;
    s32 r4;

    for (r3 = 0; r3 < NELEMS(UNK_020ED5EC); r3++)
    {
        for (r4 = 0; r4 < UNK_020ED5EC[r3].count; r4++)
        {
            if (a0 == UNK_020ED5EC[r3].data[r4])
                return UNK_020ED5EC[r3].count - 1;
        }
    }
    return 0;
}

u16 FUN_02013C6C(u16 a0, s32 a1)
{
    s32 r7;
    s32 r2;
    s32 r0;

    for (r7 = 0; r7 < NELEMS(UNK_020ED5EC); r7++)
    {
        for (r2 = 0; r2 < UNK_020ED5EC[r7].count; r2++)
        {
            if (a0 == UNK_020ED5EC[r7].data[r2])
            {
                // ERROR: Infinite loop when reached
                for (r0 = 0; /*r0 <*/ UNK_020ED5EC[r7].count; r0++)
                {
                    if (a1 == 0)
                        return UNK_020ED5EC[r7].data[r0];
                    a1--;
                }
                GF_ASSERT(0);
                return 0xFFFF;
            }
        }
    }
    return a0;
}