summaryrefslogtreecommitdiff
path: root/src/dewford_trend.c
blob: 3050ebecf483176913d8c7348325ae78fb852cde (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
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
#include "global.h"
#include "dewford_trend.h"
#include "easy_chat.h"
#include "event_data.h"
#include "link.h"
#include "malloc.h"
#include "random.h"
#include "text.h"
#include "tv.h"
#include "string_util.h"
#include "constants/easy_chat.h"

// static functions
static void sub_8122804(struct EasyChatPair *s, u16 b, u8 c);
static bool8 sub_8122A58(struct EasyChatPair *a, struct EasyChatPair *b, u8 c);
static void sub_8122B28(struct EasyChatPair *s);
static bool8 SB1ContainsWords(u16 *a);
static bool8 IsEasyChatPairEqual(u16 *words1, u16 *words2);
static s16 GetEqualEasyChatPairIndex(struct EasyChatPair *s, struct EasyChatPair *a, u16 b);

// text
void InitDewfordTrend(void)
{
    u16 i;

    for (i = 0; i < 5; i++)
    {
        gSaveBlock1Ptr->easyChatPairs[i].words[0] = sub_811EE38(EC_GROUP_CONDITIONS);

        if (Random() & 1)
            gSaveBlock1Ptr->easyChatPairs[i].words[1] = sub_811EE38(EC_GROUP_LIFESTYLE);
        else
            gSaveBlock1Ptr->easyChatPairs[i].words[1] = sub_811EE38(EC_GROUP_HOBBIES);

        gSaveBlock1Ptr->easyChatPairs[i].unk1_6 = Random() & 1;
        sub_8122B28(&(gSaveBlock1Ptr->easyChatPairs[i]));
    }
    sub_8122804(gSaveBlock1Ptr->easyChatPairs, 5, 0);
}

void UpdateDewfordTrendPerDay(u16 a)
{
    u16 i;

    if (a != 0)
    {
        u32 sp0 = a * 5;

        for (i = 0; i < 5; i++)
        {
            u32 r4;
            u32 r2 = sp0;
            struct EasyChatPair *r5 = &(gSaveBlock1Ptr->easyChatPairs[i]);

            if (r5->unk1_6 == 0)
            {
                if (r5->unk0_0 >= (u16)r2)
                {
                    r5->unk0_0 -= r2;
                    if (r5->unk0_0 == 0)
                        r5->unk1_6 = 1;
                    continue;
                }
                r2 -= r5->unk0_0;
                r5->unk0_0 = 0;
                r5->unk1_6 = 1;
            }
            r4 = r5->unk0_0 + r2;
            if ((u16)r4 > r5->unk0_7)
            {
                u32 sp4 = r4 % r5->unk0_7;
                r4 = r4 / r5->unk0_7;

                r5->unk1_6 = r4 ^ 1;
                if (r5->unk1_6)
                    r5->unk0_0 = sp4;
                else
                    r5->unk0_0 = r5->unk0_7 - sp4;
            }
            else
            {
                r5->unk0_0 = r4;

                if (r5->unk0_0 == r5->unk0_7)
                    r5->unk1_6 = 0;
            }
        }
        sub_8122804(gSaveBlock1Ptr->easyChatPairs, 5, 0);
    }
}


bool8 sub_81226D8(u16 *a)
{
    struct EasyChatPair s = {0};
    u16 i;

    if (!SB1ContainsWords(a))
    {
        if (!FlagGet(FLAG_SYS_POPWORD_INPUT))
        {
            FlagSet(FLAG_SYS_POPWORD_INPUT);
            if (!FlagGet(FLAG_SYS_MIX_RECORD))
            {
                gSaveBlock1Ptr->easyChatPairs[0].words[0] = a[0];
                gSaveBlock1Ptr->easyChatPairs[0].words[1] = a[1];
                return TRUE;
            }
        }

        s.words[0] = a[0];
        s.words[1] = a[1];
        s.unk1_6 = 1;
        sub_8122B28(&s);

        for (i = 0; i < 5; i++)
        {
            if (sub_8122A58(&s, &(gSaveBlock1Ptr->easyChatPairs[i]), 0))
            {
                u16 r3 = 4;

                while (r3 > i)
                {
                    gSaveBlock1Ptr->easyChatPairs[r3] = gSaveBlock1Ptr->easyChatPairs[r3 - 1];
                    r3--;
                }
                gSaveBlock1Ptr->easyChatPairs[i] = s;
                if(i == 4)
                    sub_80EDC60(a);
                return (i == 0);
            }
        }
        gSaveBlock1Ptr->easyChatPairs[4] = s;
        sub_80EDC60(a);
    }
    return FALSE;
}


static void sub_8122804(struct EasyChatPair *s, u16 b, u8 c)
{
    u16 h;

    for (h = 0; h < b; h++)
    {
        u16 i;

        for (i = h + 1; i < b; i++)
        {
            if (sub_8122A58(&s[i], &s[h], c))
            {
                struct EasyChatPair temp;

                temp = s[i];
                s[i] = s[h];
                s[h] = temp;
            }
        }
    }
}

void ReceiveEasyChatPairsData(void *a, u32 b, u8 unused)
{
    u16 i, j, r3, players;
    struct EasyChatPair *buffer1, *buffer2, *src, *dst, *foo_of_buffer2;

    buffer1 = Alloc(0x100);
    if(buffer1 != NULL)
    {
        buffer2 = Alloc(0x100);
        if(buffer2 == NULL)
        {
            Free(buffer1);
        }
        else
        {
            players = GetLinkPlayerCount();
            for (i = 0; i < players; i++)
                memcpy(&(buffer1[i * 5]), (u8 *)a + i * b, 40);
            src = buffer1;
            dst = buffer2;
            r3 = 0;
            for (i = 0; i < players; i++)
            {
                for (j = 0; j < 5; j++)
                {
                    s16 foo = GetEqualEasyChatPairIndex(buffer2, src, r3);
                    if (foo < 0)
                    {
                        *(dst++) = *src;
                        r3++;
                    }
                    else
                    {
                        foo_of_buffer2 = (struct EasyChatPair *)((u32)buffer2 + (foo * 8)); //required to do this to reverse the order of register operands in add ASM statement
                        if (foo_of_buffer2->unk0_0 < src->unk0_0)
                        {
                            *foo_of_buffer2 = *src;
                        }
                    }
                    src++;
                }
            }
            sub_8122804(buffer2, r3, 2);
            src = buffer2;
            dst = gSaveBlock1Ptr->easyChatPairs;
            for (i = 0; i < 5; i++)
                *(dst++) = *(src++);
            Free(buffer1);
            Free(buffer2);
        }
    }
}

void BufferTrendyPhraseString(void)
{
    struct EasyChatPair *s = &gSaveBlock1Ptr->easyChatPairs[gSpecialVar_0x8004];

    ConvertEasyChatWordsToString(gStringVar1, s->words, 2, 1);
}

void TrendyPhraseIsOld(void)
{
    u16 result = 0;

    if (gSaveBlock1Ptr->easyChatPairs[0].unk0_0 - gSaveBlock1Ptr->easyChatPairs[1].unk0_0 < 2)
    {
        asm("":::"r2"); //Force the compiler to store address of gSaveBlock1 in r3 instead of r2
        if (!gSaveBlock1Ptr->easyChatPairs[0].unk1_6 && gSaveBlock1Ptr->easyChatPairs[1].unk1_6)
            result = 1;
    }
    gSpecialVar_Result = result;
}

void GetDewfordHallPaintingNameIndex(void)
{
    gSpecialVar_Result = (gSaveBlock1Ptr->easyChatPairs[0].words[0] + gSaveBlock1Ptr->easyChatPairs[0].words[1]) & 7;
}

static bool8 sub_8122A58(struct EasyChatPair *a, struct EasyChatPair *b, u8 c)
{
    switch (c)
    {
    case 0:
        if (a->unk0_0 > b->unk0_0)
            return 1;
        if (a->unk0_0 < b->unk0_0)
            return 0;
        if (a->unk0_7 > b->unk0_7)
            return 1;
        if (a->unk0_7 < b->unk0_7)
            return 0;
        break;
    case 1:
        if (a->unk0_7 > b->unk0_7)
            return 1;
        if (a->unk0_7 < b->unk0_7)
            return 0;
        if (a->unk0_0 > b->unk0_0)
            return 1;
        if (a->unk0_0 < b->unk0_0)
            return 0;
        break;
    case 2:
        if (a->unk0_0 > b->unk0_0)
            return 1;
        if (a->unk0_0 < b->unk0_0)
            return 0;
        if (a->unk0_7 > b->unk0_7)
            return 1;
        if (a->unk0_7 < b->unk0_7)
            return 0;
        if (a->unk2 > b->unk2)
            return 1;
        if (a->unk2 < b->unk2)
            return 0;
        if (a->words[0] > b->words[0])
            return 1;
        if (a->words[0] < b->words[0])
            return 0;
        if (a->words[1] > b->words[1])
            return 1;
        if (a->words[1] < b->words[1])
            return 0;
        return 1;
    }
    return Random() & 1;
}

static void sub_8122B28(struct EasyChatPair *s)
{
    u16 r4;

    r4 = Random() % 98;
    if (r4 > 50)
    {
        r4 = Random() % 98;
        if (r4 > 80)
            r4 = Random() % 98;
    }
    s->unk0_7 = r4 + 30;
    s->unk0_0 = (Random() % (r4 + 1)) + 30;
    s->unk2 = Random();
}

static bool8 SB1ContainsWords(u16 *a)
{
    u16 i;

    for (i = 0; i < 5; i++)
    {
        if (IsEasyChatPairEqual(a, gSaveBlock1Ptr->easyChatPairs[i].words) != 0)
            return TRUE;
    }
    return FALSE;
}

static bool8 IsEasyChatPairEqual(u16 *words1, u16 *words2)
{
    u16 i;

    for (i = 0; i < 2; i++)
    {
        if (*(words1++) != *(words2++))
            return FALSE;
    }
    return TRUE;
}

static s16 GetEqualEasyChatPairIndex(struct EasyChatPair*s, struct EasyChatPair *a, u16 b)
{
    s16 i;

    for (i = 0; i < b; i++)
    {
        if (IsEasyChatPairEqual(a->words, s->words))
            return i;
        s++;
    }
    return -1;
}