aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/scripting/controls/slot_control.cpp
blob: d5551adcb37eedc663480f23e1a72804538df11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/scummsys.h"

#include "zvision/scripting/controls/slot_control.h"

#include "zvision/zvision.h"
#include "zvision/scripting/script_manager.h"
#include "zvision/graphics/cursors/cursor_manager.h"
#include "zvision/graphics/render_manager.h"

#include "common/stream.h"

namespace ZVision {

SlotControl::SlotControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream)
	: Control(engine, key, CONTROL_SLOT),
	  _cursor(CursorIndex_Active),
	  _distanceId('0') {

	_renderedItem = 0;
	_bkg = NULL;

	// Loop until we find the closing brace
	Common::String line = stream.readLine();
	_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
	Common::String param;
	Common::String values;
	getParams(line, param, values);

	while (!stream.eos() && !line.contains('}')) {
		if (param.matchString("hotspot", true)) {
			int x;
			int y;
			int width;
			int height;

			sscanf(values.c_str(), "%d %d %d %d", &x, &y, &width, &height);

			_hotspot = Common::Rect(x, y, width, height);
		} else if (param.matchString("rectangle", true)) {
			int x;
			int y;
			int width;
			int height;

			sscanf(values.c_str(), "%d %d %d %d", &x, &y, &width, &height);

			_rectangle = Common::Rect(x, y, width, height);
		} else if (param.matchString("cursor", true)) {
			_cursor = _engine->getCursorManager()->getCursorId(values);
		} else if (param.matchString("distance_id", true)) {
			sscanf(values.c_str(), "%c", &_distanceId);
		} else if (param.matchString("venus_id", true)) {
			_venusId = atoi(values.c_str());
		} else if (param.matchString("eligible_objects", true)) {
			char buf[256];
			memset(buf, 0, 256);
			strncpy(buf, values.c_str(), 255);

			char *curpos = buf;
			char *strend = buf + strlen(buf);
			while (true) {
				char *st = curpos;

				if (st >= strend)
					break;

				while (*curpos != ' ' && curpos < strend)
					curpos++;

				*curpos = 0;
				curpos++;

				int obj = atoi(st);

				_eligibleObjects.push_back(obj);
			}
		}

		line = stream.readLine();
		_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
		getParams(line, param, values);
	}

	if (_hotspot.isEmpty() || _rectangle.isEmpty()) {
		warning("Slot %u was parsed incorrectly", key);
	}
}

SlotControl::~SlotControl() {
	// Clear the state value back to 0
	//_engine->getScriptManager()->setStateValue(_key, 0);

	if (_bkg)
		delete _bkg;
}

bool SlotControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
		return false;

	if (_hotspot.contains(backgroundImageSpacePos)) {
		setVenus();

		int item = _engine->getScriptManager()->getStateValue(_key);
		int mouseItem = _engine->getScriptManager()->getStateValue(StateKey_InventoryItem);
		if (item != 0) {
			if (mouseItem != 0) {
				if (eligeblity(mouseItem)) {
					_engine->getScriptManager()->inventoryDrop(mouseItem);
					_engine->getScriptManager()->inventoryAdd(item);
					_engine->getScriptManager()->setStateValue(_key, mouseItem);
				}
			} else {
				_engine->getScriptManager()->inventoryAdd(item);
				_engine->getScriptManager()->setStateValue(_key, 0);
			}
		} else if (mouseItem == 0) {
			if (eligeblity(0)) {
				_engine->getScriptManager()->inventoryDrop(0);
				_engine->getScriptManager()->setStateValue(_key, 0);
			}
		} else if (eligeblity(mouseItem)) {
			_engine->getScriptManager()->setStateValue(_key, mouseItem);
			_engine->getScriptManager()->inventoryDrop(mouseItem);
		}
	}
	return false;
}

bool SlotControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
		return false;

	if (_hotspot.contains(backgroundImageSpacePos)) {
		_engine->getCursorManager()->changeCursor(_cursor);
		return true;
	}

	return false;
}

bool SlotControl::process(uint32 deltaTimeInMillis) {
	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
		return false;

	if (_engine->canRender()) {
		int curItem = _engine->getScriptManager()->getStateValue(_key);
		if (curItem != _renderedItem) {
			if (_renderedItem != 0 && curItem == 0) {
				_engine->getRenderManager()->blitSurfaceToBkg(*_bkg, _rectangle.left, _rectangle.top);
				_renderedItem = curItem;
			} else {
				if (_renderedItem == 0) {
					if (_bkg)
						delete _bkg;

					_bkg = _engine->getRenderManager()->getBkgRect(_rectangle);
				} else {
					_engine->getRenderManager()->blitSurfaceToBkg(*_bkg, _rectangle.left, _rectangle.top);
				}

				char buf[16];
				if (_engine->getGameId() == GID_NEMESIS)
					sprintf(buf, "%d%cobj.tga", curItem, _distanceId);
				else
					sprintf(buf, "g0z%cu%2.2x1.tga", _distanceId, curItem);

				Graphics::Surface *srf = _engine->getRenderManager()->loadImage(buf);

				int16 drawx = _rectangle.left;
				int16 drawy = _rectangle.top;

				if (_rectangle.width() > srf->w)
					drawx = _rectangle.left + (_rectangle.width() - srf->w) / 2;

				if (_rectangle.height() > srf->h)
					drawy = _rectangle.top + (_rectangle.height() - srf->h) / 2;

				_engine->getRenderManager()->blitSurfaceToBkg(*srf, drawx, drawy, 0);

				delete srf;

				_renderedItem = curItem;
			}
		}
	}
	return false;
}

bool SlotControl::eligeblity(int itemId) {
	for (Common::List<int>::iterator it = _eligibleObjects.begin(); it != _eligibleObjects.end(); it++)
		if (*it == itemId)
			return true;
	return false;
}

} // End of namespace ZVision