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/regions.h"
24
25 #include "bladerunner/savefile.h"
26
27 namespace BladeRunner {
28
Regions()29 Regions::Regions() {
30 _enabled = true;
31 _regions.resize(10);
32 clear();
33 }
34
clear()35 void BladeRunner::Regions::clear() {
36 for (int i = 0; i < 10; ++i)
37 remove(i);
38 }
39
add(int index,Common::Rect rect,int type)40 bool Regions::add(int index, Common::Rect rect, int type) {
41 if (index < 0 || index >= 10)
42 return false;
43
44 if (_regions[index].present)
45 return false;
46
47 _regions[index].rectangle = rect;
48 _regions[index].type = type;
49 _regions[index].present = 1;
50
51 return true;
52 }
53
remove(int index)54 bool Regions::remove(int index) {
55 if (index < 0 || index >= 10)
56 return false;
57
58 _regions[index].rectangle = Common::Rect(-1, -1, -1, -1);
59 _regions[index].type = -1;
60 _regions[index].present = 0;
61
62 return true;
63 }
64
getTypeAtXY(int x,int y) const65 int Regions::getTypeAtXY(int x, int y) const {
66 int index = getRegionAtXY(x, y);
67
68 if (index == -1)
69 return -1;
70
71 return _regions[index].type;
72 }
73
getRegionAtXY(int x,int y) const74 int Regions::getRegionAtXY(int x, int y) const {
75 if (!_enabled)
76 return -1;
77
78 for (int i = 0; i != 10; ++i) {
79 if (!_regions[i].present)
80 continue;
81
82 // Common::Rect::contains is exclusive of right and bottom but
83 // Blade Runner wants inclusive, so we adjust the edges.
84 Common::Rect r = _regions[i].rectangle;
85 ++(r.right);
86 ++(r.bottom);
87
88 if (r.contains(x, y))
89 return i;
90 }
91
92 return -1;
93 }
94
setEnabled(bool enabled)95 void Regions::setEnabled(bool enabled) {
96 _enabled = enabled;
97 }
98
enable()99 void Regions::enable() {
100 _enabled = true;
101 }
102
save(SaveFileWriteStream & f)103 void Regions::save(SaveFileWriteStream &f) {
104 f.writeBool(_enabled);
105 for (int i = 0; i != 10; ++i) {
106 f.writeRect(_regions[i].rectangle);
107 f.writeInt(_regions[i].type);
108 f.writeInt(_regions[i].present);
109 }
110 }
111
load(SaveFileReadStream & f)112 void Regions::load(SaveFileReadStream &f) {
113 _enabled = f.readBool();
114 for (int i = 0; i != 10; ++i) {
115 _regions[i].rectangle = f.readRect();
116 _regions[i].type = f.readInt();
117 _regions[i].present = f.readInt();
118 }
119 }
120
121 } // End of namespace BladeRunner
122