summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2ReturnStack.cpp
blob: f2e1984a54a649d81402a2bd4b0811a7430b5f79 (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
#include "StdAfx.h"
#include "T2Archive.h"
#include "T2ReturnStack.h"

T2ReturnStack::T2ReturnStack() {
	Init();
}

T2ReturnStack::~T2ReturnStack() {
}

void T2ReturnStack::Init() {
	for (int i = 0; i < 3; i++) {
		mEntries[i].tenant = 0;
		mEntries[i].time = 0;
	}
	mCount = 0;
}

BOOL T2ReturnStack::Current(unsigned int &tenant, unsigned int &time) {
	if (mCount > 0) {
		tenant = mEntries[0].tenant;
		time = mEntries[0].time;
		return true;
	}
	return false;
}

BOOL T2ReturnStack::Push(unsigned int tenant, unsigned int time) {
	if (mCount < 3) {
		for (int i = mCount - 1; i >= 0; i--) {
			mEntries[i + 1] = mEntries[i];
		}
		mCount++;
		mEntries[0].tenant = tenant;
		mEntries[0].time = time;
		return true;
	} else {
		return false;
	}
}

BOOL T2ReturnStack::Pop(unsigned int &tenant, unsigned int &time) {
	if (mCount > 0) {
		tenant = mEntries[0].tenant;
		time = mEntries[0].time;
		for (int i = 1; i < mCount; i++) {
			mEntries[i - 1] = mEntries[i];
		}
		mCount--;
		return true;
	} else {
		return false;
	}
}

BOOL T2ReturnStack::IsSetTime() const {
	BOOL result = false;
	if (mCount > 0 && mEntries[0].time != 0)
		result = true;
	return result;
}

unsigned int T2ReturnStack::GetTenant() const {
	unsigned int result = 0;
	if (mCount > 0)
		result = mEntries[0].tenant;
	return result;
}

unsigned int T2ReturnStack::GetTime() const {
	unsigned int result = 0;
	if (mCount > 0)
		result = mEntries[0].time;
	return result;
}

void T2ReturnStack::SetTime(unsigned int time) {
	if (mCount > 0)
		mEntries[0].time = time;
}

void T2ReturnStack::DayChanged() {
	for (int i = 0; i < mCount; i++) {
		if (mEntries[i].time >= 1440) {
			mEntries[i].time -= 1440;
		} else if (mEntries[i].time > 1438) {
			mEntries[i].time = 0;
		}
	}
}

BOOL T2ReturnStack::Remove(unsigned int tenant) {
	BOOL result = false;

	for (int i = 0; i < mCount; i++) {
		if (!result) {
			if (mEntries[i].tenant == tenant)
				result = true;
		} else {
			mEntries[i - 1] = mEntries[i];
		}
	}

	if (result)
		mCount--;

	return result;
}

void T2ReturnStack::Peek(int index, unsigned int &tenant, unsigned int &time) {
	tenant = mEntries[index].tenant;
	time = mEntries[index].time;
}

void T2ReturnStack::Poke(int index, unsigned int tenant, unsigned int time) {
	mEntries[index].tenant = tenant;
	mEntries[index].time = time;
}

void T2ReturnStack::Read(T2Archive &stream, T2TowerDoc *towerDoc) {
	stream >> mCount;
	for (int i = 0; i < mCount; i++) {
		unsigned short v;
		stream >> v;
		mEntries[i].tenant = v;
		stream >> v;
		mEntries[i].time = v;
	}
}

void T2ReturnStack::Write(T2Archive &stream) {
	stream << mCount;
	for (int i = 0; i < mCount; i++) {
		unsigned short v;
		v = mEntries[i].tenant;
		stream << v;
		v = mEntries[i].time;
		stream << v;
	}
}