summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2RouteNavigator.cpp
blob: eb7995fa3eca862dd69dce0983a2c27c1665cdb0 (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
#include "StdAfx.h"
#include "T2FloorInfo.h"
#include "T2FloorPtrList.h"
#include "T2Mover.h"
#include "T2MoverArray.h"
#include "T2MoverArrayList.h"
#include "T2RouteNavigator.h"
#include "T2RoutingTable.h"
#include "T2Tenant.h"
#include "URect.h"

T2RouteNavigator::T2RouteNavigator(T2FloorInfo* inFloorInfo) {
    mFloorPtrList = NULL;

    for (unsigned int index = 0; index < kMaxRouteType; index++)
        mRoutingTables[index] = NULL;

    mFloorPtrList = new T2FloorPtrList(inFloorInfo->GetTenantArrayList());
    mFloorInfo = inFloorInfo;

    for (int n = 0; n < kMaxRouteType; n++)
        mRoutingTables[n] = new T2RoutingTable(inFloorInfo, mFloorPtrList, n);

    LArrayIterator iterator(*inFloorInfo->GetMoverArrayList());
    T2MoverArray *theMoverArray;

    while (iterator.Next(&theMoverArray)) {
        for (int i = 0; i < T2MoverArray::kGroupSize; i++) {
            T2Mover *theMover = theMoverArray->GetIndexMover(i);
            if (theMover->IsUsed())
                MoverAdded(theMover, false);
        }
    }

    Update();
}

/*virtual*/ T2RouteNavigator::~T2RouteNavigator() {
    if (mFloorPtrList)
        delete mFloorPtrList;

    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            delete mRoutingTables[n];
    }
}

void T2RouteNavigator::FloorAdded(T2Tenant* inTenant, BOOL inFlag) {
    mFloorPtrList->AddItem(inTenant);

    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->FloorAdded();
    }

    if (inFlag)
        Update();
}

void T2RouteNavigator::FloorRemoved(T2Tenant* inTenant, BOOL inFlag) {
    int index = mFloorPtrList->GetIndex(inTenant);
    mFloorPtrList->RemoveItem(index);

    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->FloorRemoved(index);
    }

    if (inFlag)
        Update();
}

void T2RouteNavigator::MoverAdded(T2Mover* inMover, BOOL inFlag) {
    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->MoverAdded(inMover, inFlag);
    }
}

void T2RouteNavigator::MoverRemoved(T2Mover* inMover, BOOL inFlag) {
    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->MoverRemoved(inMover, inFlag);
    }
}

void T2RouteNavigator::MoverModified(T2Mover* inMover, BOOL inFlag) {
    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->MoverModified(inMover, inFlag);
    }
}

void T2RouteNavigator::Update() {
    for (int n = 0; n < kMaxRouteType; n++) {
        if (mRoutingTables[n])
            mRoutingTables[n]->FullUpdate();
    }
}

BOOL T2RouteNavigator::CheckRoute(POINT inFromPt, POINT inToPt, unsigned int inSearchScore, int inRouteType) const {
    BOOL result = false;

    if ((inRouteType >= 0) && (inRouteType <= kMaxRouteType) && mRoutingTables[inRouteType])
        result = mRoutingTables[inRouteType]->CheckRoute(inFromPt, inToPt, inSearchScore);

    return result;
}

BOOL T2RouteNavigator::IsConnectRouteFromLobby(POINT inPt) const {
    BOOL result = false;

    if (mRoutingTables[kRouteType0])
        result = mRoutingTables[kRouteType0]->IsConnectRouteFromLobby(inPt);

    return result;
}

BOOL T2RouteNavigator::GetNextRoute(POINT inFromPt, POINT& ioToPt, int inRouteType) const {
    BOOL result = false;

    if (inRouteType >= 0 && inRouteType <= kMaxRouteType) {
        if (mRoutingTables[inRouteType])
            result = mRoutingTables[inRouteType]->GetNextRoute(inFromPt, ioToPt);
    } else if (inRouteType == kRouteTypeNeg1) {
        result = GetNextRouteUsingEStair(inFromPt, ioToPt);
    }

    return result;
}

T2Tenant* T2RouteNavigator::SelectNearTenant(POINT inPt, unsigned int inSearchScore) const {
    T2Tenant *result = NULL;

    if (mRoutingTables[kRouteType0])
        result = mRoutingTables[kRouteType0]->SelectNearTenant(inPt, inSearchScore);

    return result;
}

BOOL T2RouteNavigator::GetNextRouteUsingEStair(POINT inFromPt, POINT& ioToPt) const {
    BOOL foundRoute = false;

    T2Tenant *fromFloor = mFloorInfo->GetFloor(inFromPt.y, inFromPt.x);
    T2Tenant *toFloor = mFloorInfo->GetFloor(ioToPt.y, ioToPt.x);

    if (fromFloor && toFloor) {
        RECT fromEquipArea, toEquipArea;
        fromFloor->GetEquipArea(fromEquipArea);
        toFloor->GetEquipArea(toEquipArea);

        RECT searchRect = toEquipArea;
        OffsetRect(&searchRect, 0, fromEquipArea.top - toEquipArea.top);

        if (IntersectRect(&searchRect, &searchRect, &fromEquipArea)) {
            ioToPt.x = searchRect.left;
            if (fromEquipArea.top > toEquipArea.top)
                ioToPt.y = inFromPt.y - URect::Height(fromEquipArea);
            else
                ioToPt.y = inFromPt.y + 1;

            T2Tenant *floor = mFloorInfo->GetFloor(ioToPt.y, ioToPt.x);
            if (floor) {
                RECT floorArea;
                floor->GetEquipArea(floorArea);
                ioToPt.y = floorArea.bottom - 1;

                int hDistance;
                int bestScore = 10000;

                if (fromEquipArea.left >= floorArea.left) {
                    hDistance = abs(fromEquipArea.left - inFromPt.x);
                    if (hDistance < bestScore) {
                        bestScore = hDistance;
                        ioToPt.x = searchRect.left;
                    }
                }

                if (fromEquipArea.right <= floorArea.right) {
                    hDistance = abs(fromEquipArea.right - 2 - inFromPt.x);
                    if (hDistance < bestScore) {
                        bestScore = hDistance;
                        ioToPt.x = fromEquipArea.right - 2;
                    }
                }

                hDistance = abs(floorArea.left - inFromPt.x);
                if (hDistance < bestScore) {
                    bestScore = hDistance;
                    ioToPt.x = floorArea.left;
                }

                hDistance = abs(floorArea.right - 2 - inFromPt.x);
                if (hDistance < bestScore) {
                    ioToPt.x = floorArea.right - 2;
                }

                foundRoute = true;
            }
        }
    }

    return foundRoute;
}