summaryrefslogtreecommitdiff
path: root/src/manager/manager28.c
blob: 1e2eb8299cb990c4118781c227923533854fcbdc (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
#include "global.h"
#include "manager.h"
#include "entity.h"
#include "room.h"
#include "functions.h"

typedef struct {
    Manager manager;
    struct {
        Entity* enemies[8];
    } d;
} Manager28;

void Manager28_Init(Manager28*);
void Manager28_Main(Manager28*);
u32 Manager28_FindMatchingEntities(Manager28*);
Entity* Manager28_FindMatchingEntity(EntityData*);
void Manager28_RemoveDeletedEntities(Manager28*);

void (*const Manager28_ActionFuncs[])(Manager28*) = { Manager28_Init, Manager28_Main };

enum Manager28_Action { Init = 0, Main = 1 };

enum Manager28_InitState { DoInit = 0, DoNotInit = 1 };

void Manager28_Entry(Manager28* this) {
    Manager28_ActionFuncs[this->manager.action](this);
}

void Manager28_Init(Manager28* this) {
    if (this->manager.unk_0a == DoInit) {
        s32 tmp2;
        int tmp4;
        for (tmp4 = 0; tmp4 < 8; tmp4++) {
            this->d.enemies[tmp4] = 0;
        }
        tmp2 = Manager28_FindMatchingEntities(this);
        if (tmp2 > 1) {
            Manager28* tmp3 = (Manager28*)GetEmptyManager();
            if (tmp3) {
                tmp3->manager.type = 9;
                tmp3->manager.subtype = 0x28;
                tmp3->manager.unk_0a = DoNotInit;
                tmp3->manager.unk_0e = tmp2;
                /* for (tmp4 = 0; tmp4 < 8; tmp4++) { */
                /*     tmp3->enemies[tmp4] = this->enemies[tmp4]; */
                /* } */
                tmp3->d = this->d;
                AppendEntityToList((Entity*)tmp3, 6);
            }
        }
        DeleteManager(&this->manager);
    } else {
        this->manager.action = Main;
        this->manager.unk_0f = 0;
    }
}

void Manager28_Main(Manager28* this) {
    s32 n;
    s32 i;
    s32 j;
    Entity* tmp;
    Manager28_RemoveDeletedEntities(this);
    n = this->manager.unk_0e - 1;
    if ((this->manager.unk_0f++) & 1) {
        for (i = 0; i < n; i++) {
            tmp = this->d.enemies[i];
            for (j = i + 1; j < this->manager.unk_0e; j++) {
                sub_08004484(tmp, this->d.enemies[j]);
            }
        }
    } else {
        for (i = 0; i < n; i++) {
            tmp = this->d.enemies[i];
            for (j = i + 1; j < this->manager.unk_0e; j++) {
                sub_08004484(this->d.enemies[j], tmp);
            }
        }
    }
}

u32 Manager28_FindMatchingEntities(Manager28* this) {
    u32 re = 0;
    EntityData* tmp = GetCurrentRoomProperty(this->manager.unk_0b);
    if (!tmp)
        return 0;
    for (; *((u8*)tmp) != 0xFF && !(tmp->kind == 9 && tmp->id == 0x28); tmp++) {
        Entity* tmp2;
        u32 i;
        if (tmp->kind != 3)
            continue;
        tmp2 = Manager28_FindMatchingEntity(tmp);
        if (!tmp2)
            continue;
        for (i = 0; i < re; i++) {
            if (this->d.enemies[i] == tmp2)
                break;
        }
        if (i != re)
            continue;
        this->d.enemies[re++] = tmp2;
        if (re == 8)
            return 8;
    }
    return re;
}

Entity* Manager28_FindMatchingEntity(EntityData* unk1) {
    u32 x, y;
    Entity* i;
    LinkedList* tmp;
    x = unk1->xPos + gRoomControls.roomOriginX;
    y = unk1->yPos + gRoomControls.roomOriginY;
    tmp = &gUnk_03003D90;
    for (i = tmp->first; (u32)i != (u32)tmp; i = i->next) {
        if (x == i->x.HALF.HI && y == i->y.HALF.HI && unk1->id == i->id && 3 == i->kind && unk1->type == i->type) {
            return i;
        }
    }
    return 0;
}

void Manager28_RemoveDeletedEntities(Manager28* this) {
    s32 i;
    for (i = 0; i < this->manager.unk_0e; i++) {
        if (this->d.enemies[i]->next == 0) {
            if (this->manager.unk_0e - 1 == i) {
                this->d.enemies[i] = 0;
            } else {
                this->d.enemies[i] = this->d.enemies[this->manager.unk_0e - 1];
            }
            this->manager.unk_0e--;
        }
    }
    if (this->manager.unk_0e <= 1)
        DeleteThisEntity();
}