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  * Additional copyright for this file:
8  * Copyright (C) 1994-1998 Revolution Software Ltd.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 
26 #include "common/memstream.h"
27 #include "common/rect.h"
28 
29 #include "sword2/sword2.h"
30 #include "sword2/header.h"
31 #include "sword2/defs.h"
32 #include "sword2/logic.h"
33 #include "sword2/mouse.h"
34 #include "sword2/resman.h"
35 
36 namespace Sword2 {
37 
addMenuObject(byte * ptr)38 void Mouse::addMenuObject(byte *ptr) {
39 	assert(_totalTemp < TOTAL_engine_pockets);
40 
41 	Common::MemoryReadStream readS(ptr, 2 * sizeof(int32));
42 
43 	_tempList[_totalTemp].icon_resource = readS.readSint32LE();
44 	_tempList[_totalTemp].luggage_resource = readS.readSint32LE();
45 	_totalTemp++;
46 }
47 
addSubject(int32 id,int32 ref)48 void Mouse::addSubject(int32 id, int32 ref) {
49 	uint32 in_subject = _vm->_logic->readVar(IN_SUBJECT);
50 
51 	if (in_subject == 0) {
52 		// This is the start of the new subject list. Set the default
53 		// repsonse id to zero in case we're never passed one.
54 		_defaultResponseId = 0;
55 	}
56 
57 	if (id == -1) {
58 		// Id -1 is used for setting the default response, i.e. the
59 		// response when someone uses an object on a person and he
60 		// doesn't know anything about it. See fnChoose().
61 
62 		_defaultResponseId = ref;
63 	} else {
64 		debug(5, "fnAddSubject res %d, uid %d", id, ref);
65 		_subjectList[in_subject].res = id;
66 		_subjectList[in_subject].ref = ref;
67 		_vm->_logic->writeVar(IN_SUBJECT, in_subject + 1);
68 	}
69 }
70 
71 /**
72  * Create and start the inventory (bottom) menu
73  */
74 
buildMenu()75 void Mouse::buildMenu() {
76 	uint32 i, j;
77 	byte menuIconWidth;
78 
79 	if (Sword2Engine::isPsx())
80 		menuIconWidth = RDMENU_PSXICONWIDE;
81 	else
82 		menuIconWidth = RDMENU_ICONWIDE;
83 
84 	// Clear the temporary inventory list, since we are going to build a
85 	// new one from scratch.
86 
87 	for (i = 0; i < TOTAL_engine_pockets; i++)
88 		_tempList[i].icon_resource = 0;
89 
90 	_totalTemp = 0;
91 
92 	// Run the 'build_menu' script in the 'menu_master' object. This will
93 	// register all carried menu objects.
94 
95 	_vm->_logic->runResScript(MENU_MASTER_OBJECT, 0);
96 
97 	// Create a new master list based on the old master inventory list and
98 	// the new temporary inventory list. The purpose of all this is, as
99 	// far as I can tell, that the new list is ordered in the same way as
100 	// the old list, with new objects added to the end of it.
101 
102 	// Compare new with old. Anything in master thats not in new gets
103 	// removed from master - if found in new too, remove from temp
104 
105 	for (i = 0; i < _totalMasters; i++) {
106 		bool found_in_temp = false;
107 
108 		for (j = 0; j < TOTAL_engine_pockets; j++) {
109 			if (_masterMenuList[i].icon_resource == _tempList[j].icon_resource) {
110 				// We alread know about this object, so kill it
111 				// in the temporary list.
112 				_tempList[j].icon_resource = 0;
113 				found_in_temp = true;
114 				break;
115 			}
116 		}
117 
118 		if (!found_in_temp) {
119 			// The object is in the master list, but not in the
120 			// temporary list. The player must have lost the object
121 			// since the last time we checked, so kill it in the
122 			// master list.
123 			_masterMenuList[i].icon_resource = 0;
124 		}
125 	}
126 
127 	// Eliminate blank entries from the master list.
128 
129 	_totalMasters = 0;
130 
131 	for (i = 0; i < TOTAL_engine_pockets; i++) {
132 		if (_masterMenuList[i].icon_resource) {
133 			if (i != _totalMasters) {
134 				memcpy(&_masterMenuList[_totalMasters], &_masterMenuList[i], sizeof(MenuObject));
135 				_masterMenuList[i].icon_resource = 0;
136 			}
137 			_totalMasters++;
138 		}
139 	}
140 
141 	// Add the new objects - i.e. the ones still in the temporary list but
142 	// not yet in the master list - to the end of the master.
143 
144 	for (i = 0; i < TOTAL_engine_pockets; i++) {
145 		if (_tempList[i].icon_resource) {
146 			memcpy(&_masterMenuList[_totalMasters++], &_tempList[i], sizeof(MenuObject));
147 		}
148 	}
149 
150 	// Initialize the menu from the master list.
151 
152 	for (i = 0; i < 15; i++) {
153 		uint32 res = _masterMenuList[i].icon_resource;
154 		byte *icon = NULL;
155 
156 		if (res) {
157 			bool icon_colored;
158 
159 			uint32 object_held = _vm->_logic->readVar(OBJECT_HELD);
160 			uint32 combine_base = _vm->_logic->readVar(COMBINE_BASE);
161 
162 			if (_examiningMenuIcon) {
163 				// When examining an object, that object is
164 				// colored. The rest are greyed out.
165 				icon_colored = (res == object_held);
166 			} else if (combine_base) {
167 				// When combining two menu object (i.e. using
168 				// one on another), both are colored. The rest
169 				// are greyed out.
170 				icon_colored = (res == object_held || combine_base);
171 			} else {
172 				// If an object is selected but we are not yet
173 				// doing anything with it, the selected object
174 				// is greyed out. The rest are colored.
175 				icon_colored = (res != object_held);
176 			}
177 
178 			icon = _vm->_resman->openResource(res) + ResHeader::size();
179 
180 			// The colored icon is stored directly after the
181 			// greyed out one.
182 
183 			if (icon_colored)
184 				icon += (menuIconWidth * RDMENU_ICONDEEP);
185 		}
186 
187 		setMenuIcon(RDMENU_BOTTOM, i, icon);
188 
189 		if (res)
190 			_vm->_resman->closeResource(res);
191 	}
192 
193 	showMenu(RDMENU_BOTTOM);
194 }
195 
196 /**
197  * Build a fresh system (top) menu.
198  */
199 
buildSystemMenu()200 void Mouse::buildSystemMenu() {
201 	uint32 icon_list[5] = {
202 		OPTIONS_ICON,
203 		QUIT_ICON,
204 		SAVE_ICON,
205 		RESTORE_ICON,
206 		RESTART_ICON
207 	};
208 
209 	byte menuIconWidth;
210 
211 	if (Sword2Engine::isPsx())
212 		menuIconWidth = RDMENU_PSXICONWIDE;
213 	else
214 		menuIconWidth = RDMENU_ICONWIDE;
215 
216 	// Build them all high in full color - when one is clicked on all the
217 	// rest will grey out.
218 
219 	for (int i = 0; i < ARRAYSIZE(icon_list); i++) {
220 		byte *icon = _vm->_resman->openResource(icon_list[i]) + ResHeader::size();
221 
222 		// The only case when an icon is grayed is when the player
223 		// is dead. Then SAVE is not available.
224 
225 		if (!_vm->_logic->readVar(DEAD) || icon_list[i] != SAVE_ICON)
226 			icon += (menuIconWidth * RDMENU_ICONDEEP);
227 
228 		setMenuIcon(RDMENU_TOP, i, icon);
229 		_vm->_resman->closeResource(icon_list[i]);
230 	}
231 
232 	showMenu(RDMENU_TOP);
233 }
234 
235 } // End of namespace Sword2
236