summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2RequestArrayList.cpp
blob: 0b5acbb68200946dca8da346a7809224859bb2ad (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
#include "T2Archive.h"
#include "T2RequestArray.h"
#include "T2RequestArrayList.h"

T2RequestArrayList::T2RequestArrayList()
    : LArray(sizeof(T2RequestArray *))
{
    T2RequestArray *requestArray = new T2RequestArray(1000);
    Add(requestArray);
}

/*virtual*/ T2RequestArrayList::~T2RequestArrayList() {
    LArrayIterator iterator(*this);
    T2RequestArray *requestArray;

    while (iterator.Next(&requestArray))
        delete requestArray;
}

void T2RequestArrayList::Add(T2RequestArray* requestArray) {
    InsertItemsAt(1, mItemCount + 1, &requestArray);
}

unsigned int T2RequestArrayList::GetItemCount() {
    return GetCount();
}

T2RequestArray* T2RequestArrayList::GetItemAt(int index) {
    T2RequestArray *requestArray;
    if (FetchItemAt(index, &requestArray))
        return requestArray;
    else
        return NULL;
}

T2Request* T2RequestArrayList::GetRequestByID(unsigned int requestID) {
    if (requestID == 0)
        return NULL;
    if (requestID < 1000)
        return NULL;

    int group = (requestID - 1000) / 64;
    int indexInGroup = (requestID - 1000) % 64;
    T2RequestArray *requestArray = GetItemAt(group + 1);
    if (requestArray)
        return requestArray->GetIndexRequest(indexInGroup);
    else
        return NULL;
}

T2Request* T2RequestArrayList::FindUnusedRequest() {
    T2Request *result = NULL;
    LArrayIterator iterator(*this);
    T2RequestArray *requestArray = NULL;

    while (!result && iterator.Next(&requestArray)) {
        result = requestArray->FindUnusedRequest();
    }

    if (!result) {
        unsigned int newStartID = 1000;
        if (requestArray)
            newStartID = requestArray->GetStartID() + 64;

        requestArray = new T2RequestArray(newStartID);
        if (requestArray) {
            Add(requestArray);
            result = requestArray->FindUnusedRequest();
        }
    }

    return result;
}

void T2RequestArrayList::BreakoutEmergency(T2TowerDoc* towerDoc) {
    LArrayIterator iterator(*this);
    T2RequestArray *requestArray;

    while (iterator.Next(&requestArray))
        requestArray->BreakoutEmergency(towerDoc);
}

void T2RequestArrayList::Read(T2Archive& archive, T2TowerDoc* towerDoc) {
    int count;
    archive >> count;

    RemoveItemsAt(GetCount(), 1);
    unsigned int startID = 1000;

    for (int i = 0; i < count; i++) {
        T2RequestArray *requestArray = new T2RequestArray(startID);
        requestArray->Read(archive, towerDoc);
        Add(requestArray);
        startID += 64;
    }
}

void T2RequestArrayList::Write(T2Archive& archive) {
    int count = GetItemCount();
    archive << count;

    LArrayIterator iterator(*this);
    T2RequestArray *requestArray;

    while (iterator.Next(&requestArray))
        requestArray->Write(archive);
}

void T2RequestArrayList::DispatchIdle(T2TowerDoc* towerDoc) {
    LArrayIterator iterator(*this);
    T2RequestArray *requestArray;

    while (iterator.Next(&requestArray))
        requestArray->DispatchIdle(towerDoc, 0);
}