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 /*
24  * This code is based on original Hugo Trilogy source code
25  *
26  * Copyright (c) 1989-1995 David P. Gray
27  *
28  */
29 
30 #ifndef HUGO_OBJECT_H
31 #define HUGO_OBJECT_H
32 
33 #include "common/file.h"
34 
35 namespace Hugo {
36 
37 struct Target {                                      // Secondary target for action
38 	uint16 _nounIndex;                               // Secondary object
39 	uint16 _verbIndex;                               // Action on secondary object
40 };
41 
42 struct Uses {                                        // Define uses of certain objects
43 	int16     _objId;                                // Primary object
44 	uint16    _dataIndex;                            // String if no secondary object matches
45 	Target   *_targets;                              // List of secondary targets
46 };
47 
48 class ObjectHandler {
49 public:
50 	ObjectHandler(HugoEngine *vm);
51 	virtual ~ObjectHandler();
52 
53 	Overlay   _objBound;
54 	Overlay   _boundary;                             // Boundary overlay file
55 	Overlay   _overlay;                              // First overlay file
56 	Overlay   _ovlBase;                              // First overlay base file
57 
58 	Object   *_objects;
59 	uint16    _numObj;
60 
61 	byte getBoundaryOverlay(uint16 index) const;
62 	byte getObjectBoundary(uint16 index) const;
63 	byte getBaseBoundary(uint16 index) const;
64 	byte getFirstOverlay(uint16 index) const;
65 
66 	int  deltaX(const int x1, const int x2, const int vx, int y) const;
67 	int  deltaY(const int x1, const int x2, const int vy, const int y) const;
68 	void boundaryCollision(Object *obj);
69 	void clearBoundary(const int x1, const int x2, const int y);
70 	void clearScreenBoundary(const int x1, const int x2, const int y);
71 	void storeBoundary(const int x1, const int x2, const int y);
72 
73 	virtual void homeIn(const int objIndex1, const int objIndex2, const int8 objDx, const int8 objDy) = 0;
74 	virtual void moveObjects() = 0;
75 	virtual void updateImages() = 0;
76 	virtual void swapImages(int objIndex1, int objIndex2) = 0;
77 
78 	bool isCarrying(uint16 wordIndex);
79 	bool findObjectSpace(Object *obj, int16 *destx, int16 *desty);
80 
81 	int   calcMaxScore();
82 	int16 findObject(uint16 x, uint16 y);
83 	void freeObjects();
84 	void loadObjectArr(Common::ReadStream &in);
85 	void loadObjectUses(Common::ReadStream &in);
86 	void loadNumObj(Common::ReadStream &in);
87 	void lookObject(Object *obj);
88 	void readObjectImages();
89 	void readObject(Common::ReadStream &in, Object &curObject);
90 	void readUse(Common::ReadStream &in, Uses &curUse);
91 	void restoreAllSeq();
92 	void restoreObjects(Common::SeekableReadStream *in);
93 	void saveObjects(Common::WriteStream *out);
94 	void saveSeq(Object *obj);
95 	void setCarriedScreen(int screenNum);
96 	void showTakeables();
97 	void useObject(int16 objId);
98 
99 	static int y2comp(const void *a, const void *b);
100 
101 	bool isCarried(int objIndex) const;
102 	void setCarry(int objIndex, bool val);
103 	void setVelocity(int objIndex, int8 vx, int8 vy);
104 	void setPath(int objIndex, Path pathType, int16 vxPath, int16 vyPath);
105 
106 protected:
107 	HugoEngine *_vm;
108 
109 	static const int kEdge = 10;                    // Closest object can get to edge of screen
110 	static const int kEdge2 = kEdge * 2;            // Push object further back on edge collision
111 	static const int kMaxObjNumb = 128;             // Used in Update_images()
112 
113 	uint16    _objCount;
114 	Uses     *_uses;
115 	uint16    _usesSize;
116 
117 	void restoreSeq(Object *obj);
118 
119 	inline bool checkBoundary(int16 x, int16 y);
120 	template<typename T>
sign(T a)121 	inline int sign(T a) { if ( a < 0) return -1; else return 1; }
122 };
123 
124 class ObjectHandler_v1d : public ObjectHandler {
125 public:
126 	ObjectHandler_v1d(HugoEngine *vm);
127 	virtual ~ObjectHandler_v1d();
128 
129 	virtual void homeIn(const int objIndex1, const int objIndex2, const int8 objDx, const int8 objDy);
130 	virtual void moveObjects();
131 	virtual void updateImages();
132 	virtual void swapImages(int objIndex1, int objIndex2);
133 };
134 
135 class ObjectHandler_v2d : public ObjectHandler_v1d {
136 public:
137 	ObjectHandler_v2d(HugoEngine *vm);
138 	virtual ~ObjectHandler_v2d();
139 
140 	virtual void moveObjects();
141 	virtual void updateImages();
142 
143 	void homeIn(const int objIndex1, const int objIndex2, const int8 objDx, const int8 objDy);
144 };
145 
146 class ObjectHandler_v3d : public ObjectHandler_v2d {
147 public:
148 	ObjectHandler_v3d(HugoEngine *vm);
149 	~ObjectHandler_v3d();
150 
151 	virtual void moveObjects();
152 	virtual void swapImages(int objIndex1, int objIndex2);
153 };
154 
155 class ObjectHandler_v1w : public ObjectHandler_v3d {
156 public:
157 	ObjectHandler_v1w(HugoEngine *vm);
158 	~ObjectHandler_v1w();
159 
160 	void moveObjects();
161 	void updateImages();
162 	void swapImages(int objIndex1, int objIndex2);
163 };
164 
165 } // End of namespace Hugo
166 #endif //HUGO_OBJECT_H
167