ScummVM API documentation
pegspuzzle.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 NANCY_ACTION_PEGSPUZZLE_H
23 #define NANCY_ACTION_PEGSPUZZLE_H
24 
25 #include "engines/nancy/commontypes.h"
26 #include "engines/nancy/action/actionrecord.h"
27 
28 namespace Nancy {
29 namespace Action {
30 
31 // Peg-solitaire puzzle, new in Nancy13 (AR 173, "leaping" jump game). The board is
32 // a grid of holes (in Nancy13, a 7x7 grid with the four 2x2 corners blocked off,
33 // i.e. the classic 33-hole English peg-solitaire cross). A peg jumps over an
34 // adjacent peg into an empty hole two cells away, removing the jumped peg. The
35 // puzzle is solved when no move remains and the number of pegs left is at most the
36 // target count.
37 //
38 // Pegs are dragged and dropped: clicking a movable peg picks it up (the hand/drag
39 // cursor carries the piece) and dropping it on a hole it can reach performs the jump.
40 // Cell state matches the original: 0 = peg, 1 = empty hole, 2 = blocked (no hole).
42 public:
44  virtual ~PegsPuzzle() {}
45 
46  void init() override;
47 
48  void readData(Common::SeekableReadStream &stream) override;
49  void execute() override;
50  void handleInput(NancyInput &input) override;
51 
52  bool isViewportRelative() const override { return true; }
53 
54 protected:
55  Common::String getRecordTypeName() const override { return "PegsPuzzle"; }
56 
57  enum CellState : byte { kPeg = 0, kEmpty = 1, kBlocked = 2 };
58 
59  int cellIndex(int col, int row) const { return col + row * 8; }
60  bool validCell(int col, int row) const;
61  // Whether a peg at (col,row) can jump in the direction (dCol,dRow), and if so the
62  // landing cell.
63  bool canJump(int col, int row, int dCol, int dRow, int &destCol, int &destRow) const;
64  // Whether the carried peg can jump onto (col,row).
65  bool isCarriedTarget(int col, int row) const;
66  // The board cell whose destination rect contains the (screen-space) cursor, or false.
67  bool cellAtCursor(const Common::Point &mousePos, int &outCol, int &outRow) const;
68  bool cellHasAnyMove(int col, int row) const;
69  bool anyMovesLeft() const;
70  int pegCount() const;
71  void doJump(int fromCol, int fromRow, int destCol, int destRow);
72 
73  Common::Point cursorToViewport(const Common::Point &mousePos) const;
74  // The puzzle's cursors are raw Nancy13 cursor type ids stored in the AR data.
75  void setDataCursor(uint16 cursorType) const;
76 
77  void redraw();
78  SoundDescription playSoundBlock(const RandomSoundBlock &block);
79 
80  // -- File data (96-byte header) --
81  Common::Path _imageName; // 0x00
82  uint16 _hoverCursorType = 0; // 0x21 - cursor while hovering a movable peg (open hand)
83  uint16 _dragCursorType = 0; // 0x23 - cursor while carrying a peg (pointing finger)
84  byte _startEmptyFlag = 0; // 0x25 - 0 => mark _startEmptyPos empty at init
85  byte _startEmptyPos = 0; // 0x26 - the initially-empty hole (usually the centre)
86  byte _targetPegCount = 0; // 0x27 - win when pegs remaining <= this
87  SceneChangeDescription _winScene; // 0x28 - shown when solved (9999 => none)
88  SceneChangeDescription _loseScene; // 0x2d - shown when no move remains and unsolved
89  Common::Rect _pegSrc; // 0x32 - normal peg sprite (viewport image)
90  Common::Rect _selectedSrc; // 0x42 - highlight sprite (selected peg / target holes)
91  uint16 _originY = 0; // 0x52 - board top-left (viewport space)
92  uint16 _originX = 0; // 0x54
93  uint16 _numRows = 0; // 0x56
94  uint16 _numCols = 0; // 0x58
95  uint16 _pitchYBias = 0; // 0x5a
96  uint16 _pitchXBias = 0; // 0x5c
97  uint16 _numBlocked = 0; // 0x5e - count of blocked-corner positions
98 
99  Common::Array<byte> _blockedPositions;
100 
101  // The clickable "give up / exit" hotspot (the base-class hotspot record).
102  Common::Rect _exitHotspot;
103  uint16 _exitCursorType = 0; // the record's leading field is its cursor type
104  SceneChangeDescription _exitScene;
105  FlagDescription _exitFlag; // set on give-up
106 
107  Common::Array<RandomSoundBlock> _sounds; // 5 blocks: select / jump / pulse / win / lose
108 
109  // -- Runtime state --
110  Common::Array<byte> _board; // 64 cells (8-wide stride)
111  Common::Array<Common::Rect> _destRects;
112  int _carriedCol = -1; // the peg currently picked up (dragged), or -1
113  int _carriedRow = -1;
114  Common::Point _dragPos; // cursor position (viewport space) while dragging
115  bool _ended = false;
116  bool _solved = false;
117  bool _exitRequested = false;
118  uint32 _endTime = 0;
119  SoundDescription _endSound; // the win/lose cue we wait on before changing scene
120 
122 };
123 
124 } // End of namespace Action
125 } // End of namespace Nancy
126 
127 #endif // NANCY_ACTION_PEGSPUZZLE_H
Definition: managed_surface.h:51
Definition: str.h:59
Definition: commontypes.h:165
Definition: pegspuzzle.h:41
Definition: rect.h:536
Definition: path.h:52
Definition: stream.h:745
Definition: input.h:41
Definition: actionrecord.h:161
Definition: commontypes.h:300
Definition: rect.h:144
Definition: commontypes.h:282
Definition: commontypes.h:180
Definition: actionmanager.h:32