summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2PeopleTypeArray.cpp
blob: 402da5e05be6ae4ca3ed703dfe59ff4ae402651b (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
#include "StdAfx.h"
#include "T2Archive.h"
#include "T2PeopleType.h"
#include "T2PeopleTypeArray.h"
#include "T2PoolDef.h"
#include "T2TenantMemberDef.h"

T2PeopleTypeArray::T2PeopleTypeArray(unsigned int val)
	: LArray(sizeof(T2PeopleType))
{
	m24 = val;
	m20 = false;
	InitSearchLimit(1.0f);
}

T2PeopleTypeArray::T2PeopleTypeArray(T2Archive& archive)
	: LArray(sizeof(T2PeopleType))
{
	m20 = false;
	Read(archive);
}

/*virtual*/ T2PeopleTypeArray::~T2PeopleTypeArray() {
}

void T2PeopleTypeArray::Add(T2PeopleType* type, unsigned int count, int where) {
	if (where == 0) {
		for (unsigned int i = 0; i < count; i++)
			LArray::Add(type);
	} else {
		InsertItemsAt(count, where, type);
	}
}

void T2PeopleTypeArray::Add(T2PeopleType* type, T2PoolTimeZoneDef* timeZoneDef, unsigned int count) {
	for (unsigned int i = 0; i < count; i++) {
		unsigned int numOfElems = timeZoneDef->GetNumOfElems();
		for (unsigned int j = 0; j < numOfElems; j++) {
			PeopleElem *thePeopleElem = timeZoneDef->GetPeopleElem(j);
#line 49
			_ASSERT(thePeopleElem != NULL);

			type->SetSilhouetteType(thePeopleElem->mSilhouetteType);
			type->SetEconoType(thePeopleElem->mEconoType);
			type->SetTransportType(thePeopleElem->mTransportType);
			type->SetLifeCount(127);
			Add(type, thePeopleElem->m4, 1);
		}
	}
}

void T2PeopleTypeArray::AdjustLife() {
	for (int i = m2C; i >= m28; i--) {
		T2PeopleType peopleType;
		if (FetchItemAt(i, &peopleType)) {
			peopleType.DecreaseLife();
			if (peopleType.HasLife())
				AssignItemsAt(1, i, &peopleType);
			else
				RemoveItemsAt(1, i);
		}
	}
}

void T2PeopleTypeArray::InitSearchLimit(float limit) {
	m28 = 1;
	m2C = GetCount();
	if (m24 == 0)
		m2C = m2C * limit;
}

BOOL T2PeopleTypeArray::Find(T2TenantMemberDef* tenantMemberDef, int inEconoType, unsigned int inTransportType, BOOL inCheckOnlyFirstEconoType) const {
	BOOL result = false;
	T2PeopleType thePeopleType;

    int n;
	int numPeople = tenantMemberDef->GetNumOfPeople();
	int numCheck = 0;
	int theEconoType = inEconoType;

	if (numPeople > 1 && m24 != 0)
		numPeople = 1;

	for (n = m28; !result && n <= m2C; n++) {
		if (FetchItemAt(n, &thePeopleType)) {
			if (thePeopleType.Check(tenantMemberDef, theEconoType, inTransportType)) {
				numCheck++;
				if (numCheck >= numPeople) {
					result = true;
				} else if (numCheck == 1 && inCheckOnlyFirstEconoType) {
					theEconoType = -1;
				}
			}
		}
	}

	return result;
}

BOOL T2PeopleTypeArray::Call(T2TenantMemberDef* tenantMemberDef, int inEconoType, unsigned int inTransportType, T2PeopleType& outPeopleType) {
	BOOL result = false;
	T2PeopleType thePeopleType;

	for (int n = m28; !result && n <= m2C; n++) {
		if (FetchItemAt(n, &thePeopleType)) {
			if (thePeopleType.Check(tenantMemberDef, inEconoType, inTransportType)) {
				result = true;
				outPeopleType = thePeopleType;
				if (!m24) {
					RemoveItemsAt(1, n);
					m2C--;
				}
			}
		}
	}

	return result;
}

void T2PeopleTypeArray::Restore(T2PeopleType& peopleType) {
	peopleType.RecoverLife();
	InsertItemsAt(1, 1, &peopleType);
	m28++;
	m2C++;
}

void T2PeopleTypeArray::Read(T2Archive& archive) {
	RemoveItemsAt(GetCount(), 1);

	int numPeople;
	archive >> numPeople;

	for (int i = 0; i < numPeople; i++) {
		T2PeopleType peopleType;
		peopleType.Read(archive);
		Add(&peopleType, 1);
	}

	archive >> m24;
	archive >> m28;
	archive >> m2C;
}

void T2PeopleTypeArray::Write(T2Archive& archive) {
	archive << mItemCount;

	LArrayIterator iter(*this);
	T2PeopleType peopleType;

	while (iter.Next(&peopleType))
		peopleType.Write(archive);

	archive << m24;
	archive << m28;
	archive << m2C;
}