summaryrefslogtreecommitdiff
path: root/src/d/snd/d_snd_distant_sound_actor_pool.cpp
blob: 931ea1754786ec678f05804573785aabefa0157c (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
#include "d/snd/d_snd_distant_sound_actor_pool.h"

#include "common.h"
#include "d/snd/d_snd_distant_sound_actor.h"
#include "d/snd/d_snd_source.h"
#include "nw4r/math/math_types.h"
#include "nw4r/ut/ut_list.h"

#include <cmath>

SND_DISPOSER_DEFINE(dSndDistantSoundActorPool_c);

dSndSourceParam dSndDistantSoundActorPool_c::sParam;

dSndDistantSoundActorPool_c::dSndDistantSoundActorPool_c() {
    field_0x4210 = 0;
    // TODO offsetof
    nw4r::ut::List_Init(&mActiveActors, 0xE4);
    sParam.reset(INFINITY);
}

void dSndDistantSoundActorPool_c::setup() {
    field_0x4210 = 0;
    for (int i = 0; i < POOL_SIZE; i++) {
        mSounds[i].loadDefaultParam();
        mSounds[i].setSource(nullptr);
        mSounds[i].initSource(nullptr);
    }
}

void dSndDistantSoundActorPool_c::calc() {
    dSndDistantSoundActor_c *it, *next;
    for (dSndDistantSoundActor_c *it = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetFirst(&mActiveActors));  it != nullptr; it = next) {
        next = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetNext(&mActiveActors, it));
        if (it->hasPlayingSounds()) {
            it->updatePosition();
        } else {
            if (it->hasAttachedSource()) {
                it->detachFromSource();
            }
            it->resetHandle();
            removeFromActiveList(it);
        }
    }
}

dSndDistantSoundActor_c *
dSndDistantSoundActorPool_c::acquireActor(u32 soundId, const nw4r::math::VEC3 *position, dSoundSource_c *source) {
    if (soundId == -1) {
        return nullptr;
    }

    dSndDistantSoundActor_c *ac = nullptr;
    int i = 0;
    while (ac == nullptr) {
        ac = &mSounds[field_0x4210];
        field_0x4210++;
        if (field_0x4210 >= POOL_SIZE) {
            field_0x4210 = 0;
        }
        if (ac->isActive()) {
            i++;
            ac = nullptr;
            if (i >= POOL_SIZE) {
                break;
            }
        }
    }

    if (ac != nullptr) {
        if (source != nullptr) {
            ac->initSource(source);
            source->attachDistantSound(ac);
        } else {
            ac->loadDefaultParam();
            ac->setSource(nullptr);
        }
        ac->SetPosition(*position);
        ac->updateListenerParameters();
        ac->updateCameraTargetParameters();

        addToActiveList(ac, soundId);

        return ac;
    }
    return nullptr;
}

void dSndDistantSoundActorPool_c::addToActiveList(dSndDistantSoundActor_c *actor, u32 id) {
    if (actor == nullptr) {
        return;
    }
    if (actor->isActive()) {
        return;
    }
    nw4r::ut::List_Append(&mActiveActors, actor);
    actor->setActive(true);
}

void dSndDistantSoundActorPool_c::removeFromActiveList(dSndDistantSoundActor_c *actor) {
    if (actor == nullptr) {
        return;
    }
    if (!actor->isActive()) {
        return;
    }
    nw4r::ut::List_Remove(&mActiveActors, actor);
    actor->setActive(false);
}

bool dSndDistantSoundActorPool_c::startSound(u32 soundId, const nw4r::math::VEC3 *position) {
    dSndDistantSoundActor_c *ac = acquireActor(soundId, position, nullptr);
    if (ac != nullptr) {
        return ac->startSound(soundId, *position, nullptr);
    }
    return false;
}

dSndDistantSoundActor_c *dSndDistantSoundActorPool_c::findActiveActor(u32 soundId, dSoundSource_c *source) {
    for (dSndDistantSoundActor_c *it = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetFirst(&mActiveActors));  it != nullptr; it = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetNext(&mActiveActors, it))) {
        if (it->isAttachedSource(source) && it->isPlayingSound(soundId)) {
            return it;
        }
    }

    return nullptr;
}

bool dSndDistantSoundActorPool_c::holdSound(u32 soundId, const nw4r::math::VEC3 *position) {
    dSndDistantSoundActor_c *ac = findActiveActor(soundId, nullptr);
    if (ac == nullptr) {
        ac = acquireActor(soundId, position, nullptr);
        if (ac != nullptr) {
            ac->updateSome3DField();
        }
    }
    if (ac != nullptr) {
        return ac->holdSound(soundId, *position, nullptr);
    }
    return false;

}

void dSndDistantSoundActorPool_c::setAllPause(bool flag, s32 fadeFrames) {
    for (dSndDistantSoundActor_c *it = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetFirst(&mActiveActors));  it != nullptr; it = static_cast<dSndDistantSoundActor_c*>(nw4r::ut::List_GetNext(&mActiveActors, it))) {
        it->setPause(flag, fadeFrames);
    }
}

void dSndDistantSoundActorPool_c::disableAll() {
    for (int i = 0; i < POOL_SIZE; i++) {
        mSounds[i].setDisabled(true);
    }
}

void dSndDistantSoundActorPool_c::enableAll() {
    for (int i = 0; i < POOL_SIZE; i++) {
        mSounds[i].setDisabled(false);
    }
}

void dSndDistantSoundActorPool_c::onChangeStage() {
    nw4r::math::VEC3 v(INFINITY, INFINITY, INFINITY);
    for (int i = 0; i < POOL_SIZE; i++) {
        mSounds[i].SetPosition(v);
    }
}