aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/duckman/propertytimers.cpp
blob: 60e58866b2954b4ba6c0e4573ac0e8da1ac37f50 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "illusions/duckman/illusions_duckman.h"
#include "illusions/duckman/propertytimers.h"
#include "illusions/resources/scriptresource.h"
#include "illusions/time.h"
#include "illusions/updatefunctions.h"
#include "engines/util.h"

namespace Illusions {

// PropertyTimers

PropertyTimers::PropertyTimers(IllusionsEngine_Duckman *vm) {
	_propertyTimersActive = false;
	_propertyTimersPaused = false;
}

PropertyTimers::~PropertyTimers() {
}

void PropertyTimers::addPropertyTimer(uint32 propertyId) {
	PropertyTimer *propertyTimer;
	if (findPropertyTimer(propertyId, propertyTimer) || findPropertyTimer(0, propertyTimer)) {
		propertyTimer->_propertyId = propertyId;
		propertyTimer->_startTime = 0;
		propertyTimer->_duration = 0;
		propertyTimer->_endTime = 0;
	}
}

void PropertyTimers::setPropertyTimer(uint32 propertyId, uint32 duration) {
	PropertyTimer *propertyTimer;
	if (findPropertyTimer(propertyId, propertyTimer)) {
		propertyTimer->_startTime = getCurrentTime();
		propertyTimer->_duration = duration;
		propertyTimer->_endTime = duration + propertyTimer->_startTime;
	}
	_vm->_scriptResource->_properties.set(propertyId, false);
	if (!_propertyTimersActive) {
		_vm->_updateFunctions->add(29, _vm->getCurrentScene(), new Common::Functor1Mem<uint, int, PropertyTimers>
			(this, &PropertyTimers::updatePropertyTimers));
		_propertyTimersActive = true;
	}
}

void PropertyTimers::removePropertyTimer(uint32 propertyId) {
	PropertyTimer *propertyTimer;
	if (findPropertyTimer(propertyId, propertyTimer))
		propertyTimer->_propertyId = 0;
	_vm->_scriptResource->_properties.set(propertyId, true);
}

bool PropertyTimers::findPropertyTimer(uint32 propertyId, PropertyTimer *&propertyTimer) {
	for (uint i = 0; i < kPropertyTimersCount; ++i)
		if (_propertyTimers[i]._propertyId == propertyId) {
			propertyTimer = &_propertyTimers[i];
			return true;
		}
	return false;
}

int PropertyTimers::updatePropertyTimers(uint flags) {
	int result = 1;
	uint32 currTime = getCurrentTime();
	if (_vm->_pauseCtr <= 0) {
		if (_propertyTimersPaused) {
			for (uint i = 0; i < kPropertyTimersCount; ++i) {
				PropertyTimer &propertyTimer = _propertyTimers[i];
				propertyTimer._startTime = currTime;
				propertyTimer._endTime = currTime + propertyTimer._duration;
			}
			_propertyTimersPaused = false;
		}
		if (flags & 1) {
			_propertyTimersActive = false;
			_propertyTimersPaused = false;
			result = 2;
		} else {
			bool timersActive = false;
			for (uint i = 0; i < kPropertyTimersCount; ++i) {
				PropertyTimer &propertyTimer = _propertyTimers[i];
				if (propertyTimer._propertyId) {
					timersActive = true;
					if (!_vm->_scriptResource->_properties.get(propertyTimer._propertyId) &&
						isTimerExpired(propertyTimer._startTime, propertyTimer._endTime))
						_vm->_scriptResource->_properties.set(propertyTimer._propertyId, true);
				}
			}
			if (!timersActive) {
				_propertyTimersActive = false;
				_propertyTimersPaused = false;
				result = 2;
			}
		}
	} else {
		if (!_propertyTimersPaused) {
			for (uint i = 0; i < kPropertyTimersCount; ++i) {
				PropertyTimer &propertyTimer = _propertyTimers[i];
				propertyTimer._duration -= getDurationElapsed(propertyTimer._startTime, propertyTimer._endTime);
			}
			_propertyTimersPaused = true;
		}
		result = 1;
	}
	return result;
}

} // End of namespace Illusions