ScummVM API documentation
pipes.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 ASYLUM_PUZZLES_PIPES_H
23#define ASYLUM_PUZZLES_PIPES_H
24
25#include "common/list.h"
26#include "common/hashmap.h"
27#include "common/array.h"
28#include "common/random.h"
29#include "common/str.h"
30
31#include "asylum/puzzles/puzzle.h"
32
33namespace Asylum {
34
35class AsylumEngine;
36class Connector;
37
38static const uint32 connectorsCount = 21, peepholesCount = 37;
39
40enum BinNum {
41 kBinNum0000,
42 kBinNum0001,
43 kBinNum0010,
44 kBinNum0011,
45 kBinNum0100,
46 kBinNum0101,
47 kBinNum0110,
48 kBinNum0111,
49 kBinNum1000,
50 kBinNum1001,
51 kBinNum1010,
52 kBinNum1011,
53 kBinNum1100,
54 kBinNum1101,
55 kBinNum1110,
56 kBinNum1111
57};
58
59enum ConnectorType {
60 kConnectorTypeI = kBinNum0101,
61 kConnectorTypeL = kBinNum0110,
62 kConnectorTypeT = kBinNum0111
63};
64
65enum Direction {
66 kDirectionNowhere = kBinNum0000,
67 kDirectionNh = kBinNum0001,
68 kDirectionEt = kBinNum0010,
69 kDirectionSh = kBinNum0100,
70 kDirectionWt = kBinNum1000
71};
72
73class Peephole {
74public:
75 Peephole() : _id(0) {}
76 ~Peephole() {}
77
78 static bool marks[peepholesCount];
79 uint32 _flowValues[4];
80
81 uint32 getId() { return _id; }
82 void setId(uint32 id) { _id = id; }
83 uint32 getLevel() { return (_flowValues[0] > 0) + (_flowValues[1] > 0) + (_flowValues[2] > 0) + (_flowValues[3] > 0); }
84 uint32 getLevel1() { return _flowValues[0] + _flowValues[1] + _flowValues[2] + _flowValues[3]; }
85 bool isConnected() { return isConnected(0) || isConnected(1) || isConnected(2) || isConnected(3); }
86
87 void connect(Connector *connector) { _connectors.push_back(connector); }
88 void disconnect(Connector *connector) { _connectors.remove(connector); }
89 void startUpWater(bool flag = false);
90
91private:
92 uint32 _id;
93 Common::List<Connector *> _connectors;
94
95 bool isConnected(uint32 val) { return _flowValues[val]; }
96};
97
98class Connector {
99public:
100 Connector();
101 ~Connector() {}
102
103 uint32 getId() { return _id; }
104 void setId(uint32 id) { _id = id; }
105 void setPos(uint32 *pos) { _position = pos; }
106 BinNum getState() { return _state; }
107 ConnectorType getType() { return _type; }
108
109 void init(Peephole *n, Peephole *e, Peephole *s, Peephole *w, uint32 pos, ConnectorType type, Connector *nextConnector = NULL, Direction nextConnectorPosition = kDirectionNowhere);
110 void initGroup();
111 void turn(bool updpos = true);
112
113private:
114 uint32 _id;
115 BinNum _state;
116 ConnectorType _type;
117 uint32 *_position;
118 Peephole *_nodes[4];
119 Common::List<Peephole *> _connectedNodes;
120
121 Connector *_nextConnector;
122 Direction _nextConnectorPosition;
123 bool _isConnected;
124
125 void connect(Connector *connector);
126 void disconnect(Connector *connector);
127
128 bool isReadyForConnection() { return _state & _nextConnectorPosition; }
129
130 friend void Peephole::startUpWater(bool);
131};
132
133class Spider {
134public:
135 Spider(AsylumEngine *engine, const Common::Rect &rect);
136 ~Spider() {}
137
138 bool isAlive() const { return _isAlive; }
139 bool isActive() const { return _delta != Common::Point(0, 0); }
140 bool isVisible(Common::Rect rect) const { return rect.contains(_location); }
141
142 Direction getDirection() const { return _direction; }
143 Common::Rect getPolygon(Common::Rect frame) const { return Common::Rect(_location.x - frame.left, _location.y - frame.top, _location.x + frame.right, _location.y + frame.bottom); }
144
145 Common::Point move();
146 void smash() { _isAlive = false; }
147private:
148 static const uint32 minStepsNumber = 20, maxStepsNumber = 200;
149 AsylumEngine *_vm;
150 bool _isAlive;
151 Common::Point _location;
152 Common::Point _delta;
153 Common::Rect _boundingBox;
154 Direction _direction;
155 uint32 _stepsNumber;
156 uint32 _steps;
157
158 void randomize(Direction excluded = kDirectionNowhere);
159};
160
161class PuzzlePipes : public Puzzle {
162public:
163 PuzzlePipes(AsylumEngine *engine);
164 ~PuzzlePipes();
165
166 // Serializable
167 virtual void saveLoadWithSerializer(Common::Serializer &s);
168
169private:
170 int32 _previousMusicVolume;
171 int32 _rectIndex;
172 uint32 _frameIndex, _frameIndexLever;
173 bool _levelFlags[5];
174 float _levelValues[4], _previousLevels[4];
175 bool _isLeverReady;
176 Common::HashMap<uint32, uint32> _connectorResources;
177 Connector _connectors[connectorsCount];
178 uint32 _positions[connectorsCount];
179 Peephole _peepholes[peepholesCount];
180 Peephole *_sinks[4], *_sources[4];
182 uint32 *_frameIndexSpider;
183
185 // Event Handling
187 bool init(const AsylumEvent &evt);
188 void updateScreen();
189 bool mouseLeftDown(const AsylumEvent &evt);
190 bool exitPuzzle();
191
193 // Helpers
195 void initResources();
196 void setup();
197 void updateCursor();
198 int32 findRect();
199 uint32 checkFlags();
200 void startUpWater();
201 void checkConnections();
202};
203
204} // End of namespace Asylum
205
206#endif // ASYLUM_PUZZLES_PIPES_H
Definition: asylum.h:73
Definition: pipes.h:98
Definition: pipes.h:73
Definition: puzzle.h:41
Definition: pipes.h:161
Definition: pipes.h:133
Definition: array.h:52
Definition: list.h:44
Definition: serializer.h:80
Definition: eventhandler.h:43
T y
Definition: rect.h:49
T x
Definition: rect.h:48
Definition: rect.h:144
T right
Definition: rect.h:171
T left
Definition: rect.h:170
Definition: rect.h:536