ScummVM API documentation
bestiary.h
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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (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, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef MM1_DATA_BESTIARY_H
23#define MM1_DATA_BESTIARY_H
24
25#include "common/stream.h"
26#include "mm/mm1/data/monsters.h"
27
28namespace MM {
29namespace MM1 {
30
36private:
37 byte _seen[(MONSTERS_COUNT + 7) / 8] = {};
38
39public:
40 void clear() {
41 memset(_seen, 0, sizeof(_seen));
42 }
43
44 bool hasSeen(uint id) const {
45 return id >= 1 && id <= MONSTERS_COUNT &&
46 (_seen[(id - 1) / 8] & (1 << ((id - 1) % 8)));
47 }
48
49 void discover(uint id) {
50 if (id >= 1 && id <= MONSTERS_COUNT)
51 _seen[(id - 1) / 8] |= 1 << ((id - 1) % 8);
52 }
53
54 uint count() const {
55 uint result = 0;
56 for (uint id = 1; id <= MONSTERS_COUNT; ++id) {
57 if (hasSeen(id))
58 ++result;
59 }
60
61 return result;
62 }
63
64 void save(Common::WriteStream &stream) const {
65 stream.writeUint32BE(0x42535431); // 'BST1' extension marker
66 stream.writeUint16BE(MONSTERS_COUNT);
67 stream.write(_seen, sizeof(_seen));
68 }
69
70 void load(Common::SeekableReadStream &stream) {
71 clear();
72
73 const int64 start = stream.pos();
74 if (stream.size() - start < 6 + (int64)sizeof(_seen))
75 return;
76
77 if (stream.readUint32BE() != 0x42535431 ||
78 stream.readUint16BE() != MONSTERS_COUNT) {
79 stream.seek(start);
80 return;
81 }
82
83 if (stream.read(_seen, sizeof(_seen)) != sizeof(_seen))
84 clear();
85 }
86};
87
88} // namespace MM1
89} // namespace MM
90
91#endif
uint32 readUint32BE()
Definition: stream.h:515
uint16 readUint16BE()
Definition: stream.h:501
virtual uint32 read(void *dataPtr, uint32 dataSize)=0
Definition: stream.h:745
virtual int64 pos() const =0
virtual int64 size() const =0
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
Definition: stream.h:77
void writeUint16BE(uint16 value)
Definition: stream.h:173
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
void writeUint32BE(uint32 value)
Definition: stream.h:180
Definition: bestiary.h:35
Definition: detection.h:27