1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifdef ENABLE_LOL
24 
25 #include "kyra/engine/lol.h"
26 #include "kyra/engine/timer.h"
27 
28 #include "common/system.h"
29 
30 namespace Kyra {
31 
32 #define TimerV2(x) new Common::Functor1Mem<int, void, LoLEngine>(this, &LoLEngine::x)
33 
setupTimers()34 void LoLEngine::setupTimers() {
35 	_timer->addTimer(0, TimerV2(timerProcessDoors), 15, true);
36 	_timer->addTimer(0x10, TimerV2(timerProcessMonsters), 6, true);
37 	_timer->addTimer(0x11, TimerV2(timerProcessMonsters), 6, true);
38 	_timer->setNextRun(0x11, _system->getMillis() + 3 * _tickLength);
39 	_timer->addTimer(3, TimerV2(timerSpecialCharacterUpdate), 15, true);
40 	_timer->addTimer(4, TimerV2(timerProcessFlyingObjects), 1, true);
41 	_timer->addTimer(0x50, TimerV2(timerRunSceneAnimScript), 0, false);
42 	_timer->addTimer(0x51, TimerV2(timerRunSceneAnimScript), 0, false);
43 	_timer->addTimer(0x52, TimerV2(timerRunSceneAnimScript), 0, false);
44 	_timer->addTimer(8, TimerV2(timerRegeneratePoints), 1200, true);
45 	_timer->addTimer(9, TimerV2(timerUpdatePortraitAnimations), 10, true);
46 	_timer->addTimer(10, TimerV2(timerUpdateLampState), 360, true);
47 	_timer->addTimer(11, TimerV2(timerFadeMessageText), 360, false);
48 	_timer->resetNextRun();
49 }
50 
timerProcessMonsters(int timerNum)51 void LoLEngine::timerProcessMonsters(int timerNum) {
52 	for (int i = timerNum & 0x0F; i < 30; i += 2)
53 		updateMonster(&_monsters[i]);
54 }
55 
timerSpecialCharacterUpdate(int timerNum)56 void LoLEngine::timerSpecialCharacterUpdate(int timerNum) {
57 	int eventsLeft = 0;
58 	for (int i = 0; i < 4; i++) {
59 		if (!(_characters[i].flags & 1))
60 			continue;
61 
62 		for (int ii = 0; ii < 5; ii++) {
63 			if (!(_characters[i].characterUpdateEvents[ii]))
64 				continue;
65 
66 			if (--_characters[i].characterUpdateDelay[ii] > 0) {
67 				if (_characters[i].characterUpdateDelay[ii] > eventsLeft)
68 					eventsLeft = _characters[i].characterUpdateDelay[ii];
69 				continue;
70 			}
71 
72 			switch (_characters[i].characterUpdateEvents[ii] - 1) {
73 			case 0:
74 				if (_characters[i].weaponHit) {
75 					_characters[i].weaponHit = 0;
76 					_characters[i].characterUpdateDelay[ii] = calcMonsterSkillLevel(i, 6);
77 					if (_characters[i].characterUpdateDelay[ii] > eventsLeft)
78 						eventsLeft = _characters[i].characterUpdateDelay[ii];
79 				} else {
80 					_characters[i].flags &= 0xFFFB;
81 				}
82 
83 				gui_drawCharPortraitWithStats(i);
84 				break;
85 
86 			case 1:
87 				_characters[i].damageSuffered = 0;
88 				gui_drawCharPortraitWithStats(i);
89 				break;
90 
91 			case 2:
92 				_characters[i].flags &= 0xFFBF;
93 				gui_drawCharPortraitWithStats(i);
94 				break;
95 
96 			case 3:
97 				eventsLeft = rollDice(1, 2);
98 				if (inflictDamage(i, eventsLeft, 0x8000, 0, 0x80)) {
99 					_txt->printMessage(2, getLangString(0x4022), _characters[i].name);
100 					_characters[i].characterUpdateDelay[ii] = 10;
101 					if (_characters[i].characterUpdateDelay[ii] > eventsLeft)
102 						eventsLeft = _characters[i].characterUpdateDelay[ii];
103 				}
104 				break;
105 
106 			case 4:
107 				_characters[i].flags &= 0xFEFF;
108 				_txt->printMessage(0, getLangString(0x4027), _characters[i].name);
109 				gui_drawCharPortraitWithStats(i);
110 				break;
111 
112 			case 5:
113 				setTemporaryFaceFrame(i, 0, 0, 1);
114 				break;
115 
116 			case 6:
117 				_characters[i].flags &= 0xEFFF;
118 				gui_drawCharPortraitWithStats(i);
119 				break;
120 
121 			case 7:
122 				restoreSwampPalette();
123 				break;
124 
125 			default:
126 				break;
127 			}
128 
129 			if (_characters[i].characterUpdateDelay[ii] <= 0)
130 				_characters[i].characterUpdateEvents[ii] = 0;
131 		}
132 	}
133 
134 	if (eventsLeft)
135 		_timer->enable(3);
136 	else
137 		_timer->disable(3);
138 }
139 
timerProcessFlyingObjects(int timerNum)140 void LoLEngine::timerProcessFlyingObjects(int timerNum) {
141 	for (int i = 0; i < 8; i++) {
142 		if (!_flyingObjects[i].enable)
143 			continue;
144 		updateFlyingObject(&_flyingObjects[i]);
145 	}
146 }
147 
timerRunSceneAnimScript(int timerNum)148 void LoLEngine::timerRunSceneAnimScript(int timerNum) {
149 	runLevelScript(0x401 + (timerNum & 0x0F), -1);
150 }
151 
timerRegeneratePoints(int timerNum)152 void LoLEngine::timerRegeneratePoints(int timerNum) {
153 	for (int i = 0; i < 4; i++) {
154 		if (!(_characters[i].flags & 1))
155 			continue;
156 
157 		// check for Duble ring
158 		int hInc = (_characters[i].flags & 8) ? 0 : (itemEquipped(i, 228) ? 4 : 1);
159 		// check for Talba ring
160 		int mInc = _drainMagic ? ((_characters[i].magicPointsMax >> 5) * -1) :
161 		           ((_characters[i].flags & 8) ? 0 : (itemEquipped(i, 227) ? (_characters[i].magicPointsMax / 10) : 1));
162 
163 		_characters[i].magicPointsCur = CLIP<int16>(_characters[i].magicPointsCur + mInc, 0, _characters[i].magicPointsMax);
164 
165 		if (!(_characters[i].flags & 0x80))
166 			increaseCharacterHitpoints(i, hInc, false);
167 
168 		gui_drawCharPortraitWithStats(i);
169 	}
170 }
171 
timerUpdatePortraitAnimations(int skipUpdate)172 void LoLEngine::timerUpdatePortraitAnimations(int skipUpdate) {
173 	if (skipUpdate != 1)
174 		skipUpdate = 0;
175 
176 	for (int i = 0; i < 4; i++) {
177 		if (!(_characters[i].flags & 1) || (_characters[i].flags & 8) || (_characters[i].curFaceFrame > 1))
178 			continue;
179 
180 		if (_characters[i].curFaceFrame != 1) {
181 			if (--_characters[i].nextAnimUpdateCountdown <= 0 && !skipUpdate) {
182 				_characters[i].curFaceFrame = 1;
183 				gui_drawCharPortraitWithStats(i);
184 				_timer->setCountdown(9, 10);
185 			}
186 		} else {
187 			_characters[i].curFaceFrame = 0;
188 			gui_drawCharPortraitWithStats(i);
189 			_characters[i].nextAnimUpdateCountdown = rollDice(1, 12) + 6;
190 		}
191 	}
192 }
193 
timerUpdateLampState(int timerNum)194 void LoLEngine::timerUpdateLampState(int timerNum) {
195 	if ((_flagsTable[31] & 0x08) && (_flagsTable[31] & 0x04) && _brightness && _lampOilStatus)
196 		_lampOilStatus--;
197 }
198 
timerFadeMessageText(int timerNum)199 void LoLEngine::timerFadeMessageText(int timerNum) {
200 	_timer->disable(timerNum);
201 	initTextFading(0, 0);
202 }
203 
204 } // End of namespace Kyra
205 
206 #endif // ENABLE_LOL
207