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 #include "bladerunner/slice_animations.h"
24 
25 #include "bladerunner/bladerunner.h"
26 #include "bladerunner/time.h"
27 
28 #include "common/debug.h"
29 #include "common/file.h"
30 #include "common/system.h"
31 
32 namespace BladeRunner {
33 
open(const Common::String & name)34 bool SliceAnimations::open(const Common::String &name) {
35 	Common::File file;
36 	if (!file.open(_vm->getResourceStream(name), name))
37 		return false;
38 
39 	_timestamp    = file.readUint32LE();
40 	_pageSize     = file.readUint32LE();
41 	_pageCount    = file.readUint32LE();
42 	_paletteCount = file.readUint32LE();
43 
44 	if (_timestamp != 0x3457b6f6) // Timestamp: Wed, 29 Oct 1997 22:21:42 GMT
45 		return false;
46 
47 	_palettes.resize(_paletteCount);
48 
49 	Graphics::PixelFormat screenFormat = screenPixelFormat();
50 
51 	for (uint32 i = 0; i != _paletteCount; ++i) {
52 		for (uint32 j = 0; j != 256; ++j) {
53 			uint8 color_r = file.readByte();
54 			uint8 color_g = file.readByte();
55 			uint8 color_b = file.readByte();
56 
57 			_palettes[i].color[j].r = color_r;
58 			_palettes[i].color[j].g = color_g;
59 			_palettes[i].color[j].b = color_b;
60 
61 			// We need to convert from 5 bits per channel (r,g,b) to 8 bits
62 			_palettes[i].value[j] = screenFormat.RGBToColor(Color::get8BitColorFrom5Bit(color_r), Color::get8BitColorFrom5Bit(color_g), Color::get8BitColorFrom5Bit(color_b));
63 		}
64 	}
65 
66 	uint32 animationCount = file.readUint32LE();
67 	_animations.resize(animationCount);
68 
69 	for (uint32 i = 0; i != animationCount; ++i) {
70 		_animations[i].frameCount       = file.readUint32LE();
71 		_animations[i].frameSize        = file.readUint32LE();
72 		_animations[i].fps              = file.readFloatLE();
73 		_animations[i].positionChange.x = file.readFloatLE();
74 		_animations[i].positionChange.y = file.readFloatLE();
75 		_animations[i].positionChange.z = file.readFloatLE();
76 		_animations[i].facingChange     = file.readFloatLE();
77 		_animations[i].offset           = file.readUint32LE();
78 	}
79 
80 	_pages.resize(_pageCount);
81 	for (uint32 i = 0; i != _pageCount; ++i)
82 		_pages[i]._data = nullptr;
83 
84 	return true;
85 }
86 
~SliceAnimations()87 SliceAnimations::~SliceAnimations() {
88 	for (uint32 i = 0; i != _pages.size(); ++i)
89 		free(_pages[i]._data);
90 
91 	// close open files
92 	_coreAnimPageFile.close(0);
93 	if (!_vm->_cutContent) {
94 		_framesPageFile.close(_framesPageFile._fileNumber);
95 	} else {
96 		for (uint i = 0; i < 5; ++i) {
97 			_framesPageFile.close(i);
98 		}
99 	}
100 }
101 
openCoreAnim()102 bool SliceAnimations::openCoreAnim() {
103 	return _coreAnimPageFile.open("COREANIM.DAT", 0);
104 }
105 
openFrames(int fileNumber)106 bool SliceAnimations::openFrames(int fileNumber) {
107 
108 	if (_framesPageFile._fileNumber == -1) { // Running for the first time, need to probe
109 		// First, try HDFRAMES.DAT
110 		if (_framesPageFile.open("HDFRAMES.DAT", 0)) {
111 			_framesPageFile._fileNumber = 0;
112 
113 			return true;
114 		} else {
115 			debug("SliceAnimations::openFrames: HDFRAMES.DAT resource not found. Falling back to using CDFRAMESx.DAT files instead...");
116 		}
117 	}
118 
119 	if (_framesPageFile._fileNumber == 0) // HDFRAMES.DAT
120 		return true;
121 
122 	if (!_vm->_cutContent && _framesPageFile._fileNumber == fileNumber)
123 		return true;
124 
125 	if (_vm->_cutContent && _framesPageFile._fileNumber == 5) // all frame files loaded
126 		return true;
127 
128 	if (!_vm->_cutContent) {
129 		// _fileNumber can normally be in [1,4]
130 		// but it will be "5" if we switched from restored content to original
131 		if (_framesPageFile._fileNumber == 5) {
132 			for (uint i = 1; i < 5; ++i) {
133 				_framesPageFile.close(i);
134 			}
135 		} else if (_framesPageFile._fileNumber > 0) {
136 			_framesPageFile.close(_framesPageFile._fileNumber);
137 		}
138 		_framesPageFile._fileNumber = fileNumber;
139 		// For Chapter1 we try both CDFRAMES.DAT and CDFRAMES1.DAT
140 		if (fileNumber == 1 && _framesPageFile.open("CDFRAMES.DAT", fileNumber)) {
141 			return true;
142 		}
143 
144 		if (_framesPageFile.open(Common::String::format("CDFRAMES%d.DAT", fileNumber), fileNumber)) {
145 			return true;
146 		}
147 	} else {
148 		// Restored cut content case
149 		// open all four CDFRAMESx.DAT files in slots [1,5]
150 		// So that all animation resources are available at all times as if we just had the single HDFRAMES.DAT file
151 		for (uint i = 1; i < 5; ++i) {
152 			_framesPageFile.close(i);
153 			if (i == 1
154 			    && (!_framesPageFile.open("CDFRAMES.DAT", i))
155 			    && (!_framesPageFile.open(Common::String::format("CDFRAMES%d.DAT", i), i))
156 			) {
157 				// For Chapter1 we try both CDFRAMES.DAT and CDFRAMES1.DAT
158 				return false;
159 			} else if (i != 1 &&
160 			          !_framesPageFile.open(Common::String::format("CDFRAMES%d.DAT", i), i)
161 			) {
162 				return false;
163 			}
164 		}
165 		_framesPageFile._fileNumber = 5;
166 		return true;
167 	}
168 	return false;
169 }
170 
open(const Common::String & name,int8 fileIdx)171 bool SliceAnimations::PageFile::open(const Common::String &name, int8 fileIdx) {
172 	if (!_files[fileIdx].open(name))
173 		return false;
174 
175 	uint32 timestamp = _files[fileIdx].readUint32LE();
176 	if (timestamp != _sliceAnimations->_timestamp)
177 		return false;
178 
179 	if (!_sliceAnimations->_vm->_cutContent
180 		|| (_pageOffsets.size() < _sliceAnimations->_pageCount) ) {
181 		_pageOffsets.resize(_sliceAnimations->_pageCount);
182 		_pageOffsetsFileIdx.resize(_sliceAnimations->_pageCount);
183 		for (uint32 i = 0; i != _sliceAnimations->_pageCount; ++i) {
184 			_pageOffsets[i] = -1;
185 			_pageOffsetsFileIdx[i] = -1;
186 		}
187 	}
188 
189 	uint32 pageCount  = _files[fileIdx].readUint32LE();
190 	uint32 dataOffset = 8 + 4 * pageCount;
191 
192 	for (uint32 i = 0; i != pageCount; ++i) {
193 		uint32 pageNumber = _files[fileIdx].readUint32LE();
194 		if (pageNumber == 0xffffffff)
195 			continue;
196 		_pageOffsets[pageNumber] = dataOffset + i * _sliceAnimations->_pageSize;
197 		_pageOffsetsFileIdx[pageNumber] = fileIdx;
198 	}
199 
200 	// debug(5, "PageFile::Open: page file \"%s\" opened with %d pages", name.c_str(), pageCount);
201 
202 	return true;
203 }
204 
close(int8 fileIdx)205 void SliceAnimations::PageFile::close(int8 fileIdx) {
206 	if (fileIdx >= 0 && fileIdx < 5) {
207 		if (_files[fileIdx].isOpen()) {
208 			_files[fileIdx].close();
209 		}
210 	}
211 }
212 
loadPage(uint32 pageNumber)213 void *SliceAnimations::PageFile::loadPage(uint32 pageNumber) {
214 	if (_pageOffsets.size() < _sliceAnimations->_pageCount
215 	    || _pageOffsetsFileIdx.size() < _sliceAnimations->_pageCount
216 	    || _pageOffsets[pageNumber] == -1
217 	    || _pageOffsetsFileIdx[pageNumber] == -1) {
218 		return nullptr;
219 	}
220 
221 	uint32 pageSize = _sliceAnimations->_pageSize;
222 
223 	// TODO: Retire oldest pages if we exceed some memory limit
224 
225 	void *data = malloc(pageSize);
226 	_files[_pageOffsetsFileIdx[pageNumber]].seek(_pageOffsets[pageNumber], SEEK_SET);
227 	uint32 r = _files[_pageOffsetsFileIdx[pageNumber]].read(data, pageSize);
228 	assert(r == pageSize);
229 
230 	return data;
231 }
232 
getFramePtr(uint32 animation,uint32 frame)233 void *SliceAnimations::getFramePtr(uint32 animation, uint32 frame) {
234 #if BLADERUNNER_ORIGINAL_BUGS
235 #else
236 	// FIXME: Maybe there's a better way?
237 	// Sanitize bad frame value
238 	// For some actors (currently only happened with hawkers_barkeep) it is possible
239 	// to SAVE a frame value (while saving a game)
240 	// that in conjunction with other actor script vars not being re-initialized
241 	// upon LOADING that game (for hawkers_barkeep this variable is "_var2")
242 	// will lead to an invalid frame here and an assertion fault (now commented out).
243 	// Example of faulty case:
244 	// hawkers_barkeep was SAVED as:
245 	// (animationState, animationFrame, animationStateNext, nextAnimation) = (0, 19, 0, 0)
246 	// while his animationID was 705
247 	// if _var1, _var2, _var3  == (0, 6, 1) when LOADING that save file,
248 	// then animationFrame will remain 19, which is invalid for his 705 animation
249 	// and the assert will produce a fault when trying to call drawInWorld for him.
250 	if (frame >= _animations[animation].frameCount) {
251 		debug("Bad frame: %u max: %u animation: %u", frame, _animations[animation].frameCount, animation);
252 		frame = 0;
253 	}
254 //	assert(frame < _animations[animation].frameCount);
255 #endif // BLADERUNNER_ORIGINAL_BUGS
256 
257 	uint32 frameOffset = _animations[animation].offset + frame * _animations[animation].frameSize;
258 	uint32 page        = frameOffset / _pageSize;
259 	uint32 pageOffset  = frameOffset % _pageSize;
260 
261 	if (_pages[page]._data == nullptr) {                          // if not cached already
262 		_pages[page]._data = _coreAnimPageFile.loadPage(page);    // look in COREANIM first
263 
264 		if (_pages[page]._data == nullptr) {                      // if not in COREAMIM
265 			_pages[page]._data = _framesPageFile.loadPage(page);  // Look in CDFRAMES or HDFRAMES loaded data
266 
267 			if (_pages[page]._data == nullptr) {
268 				error("Unable to locate page %d for animation %d frame %d", page, animation, frame);
269 			}
270 		}
271 	}
272 
273 	_pages[page]._lastAccess = _vm->_time->currentSystem();
274 
275 	return (byte *)_pages[page]._data + pageOffset;
276 }
277 
getPositionChange(int animation) const278 Vector3 SliceAnimations::getPositionChange(int animation) const {
279 	return _animations[animation].positionChange;
280 }
281 
getFacingChange(int animation) const282 float SliceAnimations::getFacingChange(int animation) const {
283 	return _animations[animation].facingChange;
284 }
285 
286 } // End of namespace BladeRunner
287