summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2TrafficInfo.cpp
blob: 04722a48469ea272354c9cbdfcfb265a62cae3ff (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
#include "T2Archive.h"
#include "T2FloorInfo.h"
#include "T2TowerDoc.h"
#include "T2TowerMainView.h"
#include "T2TrafficInfo.h"
#include "URect.h"

T2TrafficInfo::T2TrafficInfo(RECT inArea, unsigned int inA, unsigned int inB, unsigned int inC, unsigned int inUnitSize) {
    mUnitSize = inUnitSize;
    if (mUnitSize == 0)
        mUnitSize = 4;

    m8 = inA / 4;
    mC = inB / 4;
    m10 = inC / 4;

    mArea = inArea;
    mHeight = URect::Height(inArea);
    mWidth = URect::Width(inArea) / mUnitSize;
    mUnitData = new T2TrafficUnit[mWidth * mHeight];

    unsigned int nUnits = mWidth * mHeight;
    T2TrafficUnit *unt = mUnitData;
    for (unsigned int i = 0; i < nUnits; i++, unt++) {
        unt->x0 = 0;
        unt->x4 = 0;
    }
}

/*virtual*/ T2TrafficInfo::~T2TrafficInfo() {
    if (mUnitData)
        delete[] mUnitData;
}

void T2TrafficInfo::Pass(POINT inPt1, POINT inPt2, short inCost) {
    POINT point1;
    POINT point2;

    if (inPt1.x < inPt2.x) {
        point1 = inPt1;
        point2 = inPt2;
        point2.y = inPt1.y;
    } else {
        point1 = inPt2;
        point1.y = inPt1.y;
        point2 = inPt1;
    }

    T2TrafficUnit *un1 = GetUnit(point1);
    T2TrafficUnit *un2 = GetUnit(point2);

    if (un1 && un2) {
        for (T2TrafficUnit *unit = un1; unit <= un2; unit++)
            unit->x0 += inCost;
    }
}

void T2TrafficInfo::HourChanged(T2TowerDoc* inDoc) {
    unsigned int nUnits = mWidth * mHeight;
    T2TrafficUnit *unt = mUnitData;

    for (unsigned int i = 0; i < nUnits; i++, unt++) {
        unt->x4 *= 0.96875f;
        if ((unt->x4 + unt->x0) > 0xFFFF)
            unt->x4 = 0xFFFF;
        else
            unt->x4 += unt->x0;
        unt->x0 = 0;

        int range = CalcRange(unt->x4);
        if (range != unt->range) {
            unt->range = range;
            if (inDoc->towerDoc_vf140() == ViewMode_5) {
                RECT area;
                T2FloorInfo *theFloorInfo = inDoc->towerDoc_vf12C();

                CalcUnitArea(i, area);
                theFloorInfo->SetTenantDrawModeByRect(area, DrawMode3);
                inDoc->GetTowerMainView()->tmv_vf128(area);
            }
        }
    }
}

unsigned int T2TrafficInfo::CalcRange(unsigned int inValue) const {
    short range = 3;

    if (inValue < m8)
        range = 0;
    else if (inValue < mC)
        range = 1;
    else if (inValue < m10)
        range = 2;

    return range;
}

void T2TrafficInfo::CalcUnitArea(unsigned int inIndex, RECT& outRect) const {
    unsigned int h = inIndex / mWidth;
    unsigned int v = (inIndex % mWidth) * mUnitSize;
    SetRect(&outRect, h, v, h + mUnitSize, v + 1);
    OffsetRect(&outRect, mArea.left, mArea.top);
}

short T2TrafficInfo::GetRange(POINT inPt) const {
    short range = 0;

    T2TrafficUnit *theUnit = GetUnit(inPt);
    if (theUnit)
        range = theUnit->range;

    return range;
}

T2TrafficUnit* T2TrafficInfo::GetUnit(POINT inPt) const {
    T2TrafficUnit *theUnit = NULL;

    if (mUnitData) {
        int index = CalcUnitIndex(inPt);
        if (index > 0)
            theUnit = mUnitData + index;
    }

    return theUnit;
}

int T2TrafficInfo::CalcUnitIndex(POINT inPt) const {
    int index = -1;
    POINT point = inPt;

    if (PtInRect(&mArea, point)) {
        int h = (inPt.x - mArea.left) / mUnitSize;
        int v = inPt.y - mArea.top;
        index = mWidth * v + h;
    }

    return index;
}

void T2TrafficInfo::Read(T2Archive& inArchive) {
    unsigned int nUnits = mWidth * mHeight;
    T2TrafficUnit *unt = mUnitData;
    for (unsigned int i = 0; i < nUnits; i++, unt++) {
        unsigned short v;

        inArchive >> v;
        unt->x0 = v;
        inArchive >> v;
        unt->x4 = v;

        unt->range = CalcRange(unt->x4);
    }
}

void T2TrafficInfo::Write(T2Archive& inArchive) const {
    unsigned int nUnits = mWidth * mHeight;
    T2TrafficUnit *unt = mUnitData;
    for (unsigned int i = 0; i < nUnits; i++, unt++) {
        inArchive << (unsigned short) unt->x0;
        inArchive << (unsigned short) unt->x4;
    }
}