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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/* 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.
*
*/
/*
* Based on the Reverse Engineering work of Christophe Fontanel,
* maintainer of the Dungeon Master Encyclopaedia (http://dmweb.free.fr/)
*/
#include "inventory.h"
#include "dungeonman.h"
#include "eventman.h"
#include "menus.h"
#include "gfx.h"
#include "text.h"
#include "objectman.h"
namespace DM {
Box gBoxFloppyZzzCross = Box(174, 218, 2, 12); // @ G0041_s_Graphic562_Box_ViewportFloppyZzzCross
Box gBoxPanel = Box(80, 223, 52, 124); // @ G0032_s_Graphic562_Box_Panel
Box gBoxFood = Box(112, 159, 60, 68); // @ G0035_s_Graphic562_Box_Food
Box gBoxWater = Box(112, 159, 83, 91); // @ G0036_s_Graphic562_Box_Water
Box gBoxPoisoned = Box(112, 207, 105, 119); // @ G0037_s_Graphic562_Box_Poisoned
InventoryMan::InventoryMan(DMEngine *vm) : _vm(vm) {
_panelContent = kPanelContentFoodWaterPoisoned;
for (uint16 i = 0; i < 8; ++i)
_chestSlots[i] = Thing::_thingNone;
_openChest = Thing::_thingNone;
_openChest = Thing::_thingNone;
}
void InventoryMan::toggleInventory(ChampionIndex championIndex) {
ChampionMan &cm = *_vm->_championMan;
EventManager &em = *_vm->_eventMan;
DisplayMan &dm = *_vm->_displayMan;
if ((championIndex != kChampionCloseInventory) && !cm._champions[championIndex]._currHealth)
return;
if (_vm->_pressingEye || _vm->_pressingMouth)
return;
_vm->_stopWaitingForPlayerInput = true;
int16 invChampOrdinal = _inventoryChampionOrdinal; // copy, as the original will be edited
if (_vm->indexToOrdinal(championIndex) == invChampOrdinal) {
championIndex = kChampionCloseInventory;
}
Champion *champion;
if (invChampOrdinal) {
_inventoryChampionOrdinal = _vm->indexToOrdinal(kChampionNone);
closeChest();
champion = &cm._champions[_vm->ordinalToIndex(invChampOrdinal)];
if (champion->_currHealth && !cm._candidateChampionOrdinal) {
champion->setAttributeFlag(kChampionAttributeStatusBox, true);
cm.drawChampionState((ChampionIndex)_vm->ordinalToIndex(invChampOrdinal));
}
if (cm._partyIsSleeping) {
return;
}
if (championIndex == kChampionCloseInventory) {
em._refreshMousePointerInMainLoop = true;
_vm->_menuMan->drawMovementArrows();
em._secondaryMouseInput = gSecondaryMouseInput_Movement;
warning("MISSING CODE: set G0444_ps_SecondaryKeyboardInput");
warning("MISSING CODE: F0357_COMMAND_DiscardAllInput");
return;
}
}
dm._useByteBoxCoordinates = false;
_inventoryChampionOrdinal = _vm->indexToOrdinal(championIndex);
if (!invChampOrdinal) {
warning("MISSING CODE: F0136_VIDEO_ShadeScreenBox");
}
champion = &cm._champions[championIndex];
int16 w = dm.getWidth(kInventoryGraphicIndice);
int16 h = dm.getHeight(kInventoryGraphicIndice);
dm.blitToScreen(dm.getBitmap(kInventoryGraphicIndice), w, 0, 0, 0, w, 0, h, kColorNoTransparency, gDungeonViewport);
if (cm._candidateChampionOrdinal) {
dm.clearScreenBox(kColorDarkestGray, gBoxFloppyZzzCross, gDungeonViewport);
}
_vm->_textMan->printToViewport(5, 116, kColorLightestGray, "HEALTH");
_vm->_textMan->printToViewport(5, 124, kColorLightestGray, "STAMINA");
_vm->_textMan->printToViewport(5, 132, kColorLightestGray, "MANA");
for (uint16 slotIndex = kChampionSlotReadyHand; slotIndex < kChampionSlotChest_1; slotIndex++) {
_vm->_championMan->drawSlot(championIndex, (ChampionSlot)slotIndex);
}
champion->setAttributeFlag(kChampionAttributeViewport, true);
champion->setAttributeFlag(kChampionAttributeStatusBox, true);
champion->setAttributeFlag(kChampionAttributePanel, true);
champion->setAttributeFlag(kChampionAttributeLoad, true);
champion->setAttributeFlag(kChampionAttributeStatistics, true);
champion->setAttributeFlag(kChampionAttributeNameTitle, true);
cm.drawChampionState(championIndex);
em._mousePointerBitmapUpdated = true;
em._secondaryMouseInput = gSecondaryMouseInput_ChampionInventory;
warning("MISSING CODE: set G0444_ps_SecondaryKeyboardInput");
warning("MISSING CODE: F0357_COMMAND_DiscardAllInput");
}
void InventoryMan::drawStatusBoxPortrait(ChampionIndex championIndex) {
DisplayMan &dispMan = *_vm->_displayMan;
dispMan._useByteBoxCoordinates = false;
Box box;
box._y1 = 0;
box._y2 = 28 + 1;
box._x1 = championIndex * kChampionStatusBoxSpacing + 7;
box._x2 = box._x1 + 31 + 1;
dispMan.blitToScreen(_vm->_championMan->_champions[championIndex]._portrait, 32, 0, 0, box, kColorNoTransparency);
}
void InventoryMan::drawPanelHorizontalBar(int16 x, int16 y, int16 pixelWidth, Color color) {
Box box;
box._x1 = x;
box._x2 = box._x1 + pixelWidth + 1;
box._y1 = y;
box._y2 = box._y1 + 6 + 1;
_vm->_displayMan->_useByteBoxCoordinates = false;
_vm->_displayMan->clearScreenBox(color, box);
}
void InventoryMan::drawPanelFoodOrWaterBar(int16 amount, int16 y, Color color) {
if (amount < -512) {
color = kColorRed;
} else if (amount < 0) {
color = kColorYellow;
}
int16 pixelWidth = amount + 1024;
if (pixelWidth == 3072) {
pixelWidth = 3071;
}
pixelWidth /= 32;
drawPanelHorizontalBar(115, y + 2, pixelWidth, kColorBlack);
drawPanelHorizontalBar(113, y, pixelWidth, color);
}
void InventoryMan::drawPanelFoodWaterPoisoned() {
Champion &champ = _vm->_championMan->_champions[_inventoryChampionOrdinal];
closeChest();
DisplayMan &dispMan = *_vm->_displayMan;
dispMan.blitToScreen(dispMan.getBitmap(kPanelEmptyIndice), 144, 0, 0, gBoxPanel, kColorRed);
dispMan.blitToScreen(dispMan.getBitmap(kFoodLabelIndice), 48, 0, 0, gBoxFood, kColorDarkestGray);
dispMan.blitToScreen(dispMan.getBitmap(kWaterLabelIndice), 48, 0, 0, gBoxWater, kColorDarkestGray);
if (champ._poisonEventCount) {
dispMan.blitToScreen(dispMan.getBitmap(kPoisionedLabelIndice), 96, 0, 0, gBoxPoisoned, kColorDarkestGray);
}
drawPanelFoodOrWaterBar(champ._food, 69, kColorLightBrown);
drawPanelFoodOrWaterBar(champ._water, 92, kColorBlue);
}
void InventoryMan::drawPanelResurrectReincarnate() {
_panelContent = kPanelContentResurrectReincarnate;
_vm->_displayMan->blitToScreen(_vm->_displayMan->getBitmap(kPanelResurectReincaranteIndice), 144, 0, 0, gBoxPanel, kColorDarkGreen, gDungeonViewport);
}
void InventoryMan::drawPanel() {
warning("possible reintroduction of BUG0_48");
closeChest(); // possibility of BUG0_48
ChampionMan &cm = *_vm->_championMan;
if (cm._candidateChampionOrdinal) {
drawPanelResurrectReincarnate();
return;
}
Thing thing = cm._champions[_vm->ordinalToIndex(_inventoryChampionOrdinal)].getSlot(kChampionSlotActionHand);
_panelContent = kPanelContentFoodWaterPoisoned;
switch (thing.getType()) {
case kContainerThingType:
_panelContent = kPanelContentChest;
break;
case kScrollThingType:
_panelContent = kPanelContentScroll;
break;
default:
thing = Thing::_thingNone;
break;
}
if (thing == Thing::_thingNone) {
drawPanelFoodWaterPoisoned();
} else {
warning("MISSING CODE: F0342_INVENTORY_DrawPanel_Object(L1075_T_Thing, C0_FALSE);");
}
}
void InventoryMan::closeChest() {
DungeonMan &dunMan = *_vm->_dungeonMan;
bool processFirstChestSlot = true;
if (_openChest == Thing::_thingNone)
return;
Container *container = (Container*)dunMan.getThingData(_openChest);
_openChest = Thing::_thingNone;
container->getSlot() = Thing::_thingEndOfList;
Thing prevThing;
for (int16 chestSlotIndex = 0; chestSlotIndex < 8; ++chestSlotIndex) {
Thing thing = _chestSlots[chestSlotIndex];
if (thing != Thing::_thingNone) {
_chestSlots[chestSlotIndex] = Thing::_thingNone; // CHANGE8_09_FIX
if (processFirstChestSlot) {
processFirstChestSlot = false;
*dunMan.getThingData(thing) = Thing::_thingEndOfList.toUint16();
container->getSlot() = prevThing = thing;
} else {
dunMan.linkThingToList(thing, prevThing, kMapXNotOnASquare, 0);
prevThing = thing;
}
}
}
}
void InventoryMan::drawPanelScrollTextLine(int16 yPos, char* text) {
warning("CHANGE5_03_IMPROVEMENT");
for (char* iter = text; *iter != '\0'; ++iter) {
if ((*iter >= 'A') && (*iter <= 'Z')) {
*iter -= 64;
} else if (*iter >= '{') { // this branch is CHANGE5_03_IMPROVEMENT
*iter -= 96;
}
}
_vm->_textMan->printToViewport(162 - (6 * strlen(text) / 2), yPos, kColorBlack, text, kColorWhite);
}
void InventoryMan::drawPanelScroll(Scroll* scroll) {
DisplayMan &dispMan = *_vm->_displayMan;
char stringFirstLine[300];
_vm->_dungeonMan->decodeText(stringFirstLine, Thing(scroll->getTextStringThingIndex()), (TextType)(kTextTypeScroll | kDecodeEvenIfInvisible));
char *charRed = stringFirstLine;
while (*charRed && (*charRed != '\n')) {
charRed++;
}
*charRed = '\0';
dispMan.blitToScreen(dispMan.getBitmap(kPanelOpenScrollIndice), 144, 0, 0, gBoxPanel, kColorRed, gDungeonViewport);
int16 lineCount = 1;
charRed++;
char *charGreen = charRed; // first char of the second line
while (*charGreen) {
warning("BUG0_47");
/* BUG0_47 Graphical glitch when you open a scroll. If there is a single line of text in a scroll
(with no carriage return) then charGreen points to undefined data. This may result in a graphical
glitch and also corrupt other memory. This is not an issue in the original dungeons where all
scrolls contain at least one carriage return character */
if (*charGreen == '\n') {
lineCount++;
}
charGreen++;
}
if (*(charGreen - 1) != '\n') {
lineCount++;
} else if (*(charGreen - 2) == '\n') {
lineCount--;
}
int16 yPos = 92 - (7 * lineCount) / 2; // center the text vertically
drawPanelScrollTextLine(yPos, stringFirstLine);
charGreen = charRed;
while (*charGreen) {
yPos += 7;
while (*charRed && (*charRed != '\n')) {
charRed++;
}
if (!(*charRed)) {
charRed[1] = '\0';
}
*charRed++ = '\0';
drawPanelScrollTextLine(yPos, charGreen);
charGreen = charRed;
}
}
void InventoryMan::openAndDrawChest(Thing thingToOpen, Container* chest, bool isPressingEye) {
DisplayMan &dispMan = *_vm->_displayMan;
ObjectMan &objMan = *_vm->_objectMan;
if (_openChest == thingToOpen)
return;
warning("CHANGE8_09_FIX");
if (_openChest != Thing::_thingNone)
closeChest(); // CHANGE8_09_FIX
_openChest = thingToOpen;
if (!isPressingEye) {
objMan.drawIconInSlotBox(kSlotBoxInventoryActionHand, kIconIndiceContainerChestOpen);
}
dispMan.blitToScreen(dispMan.getBitmap(kPanelOpenChestIndice), 144, 0, 0, gBoxPanel, kColorRed);
int16 chestSlotIndex = 0;
Thing thing = chest->getSlot();
int16 thingCount = 0;
while (thing != Thing::_thingEndOfList) {
warning("CHANGE8_08_FIX");
if (++thingCount > 8)
break; // CHANGE8_08_FIX, make sure that no more than the first 8 objects in a chest are drawn
objMan.drawIconInSlotBox(chestSlotIndex + kSlotBoxChestFirstSlot, objMan.getIconIndex(thing));
_chestSlots[chestSlotIndex++] = thing;
thing = _vm->_dungeonMan->getNextThing(thing);
}
while (chestSlotIndex < 8) {
objMan.drawIconInSlotBox(chestSlotIndex + kSlotBoxChestFirstSlot, kIconIndiceNone);
_chestSlots[chestSlotIndex++] = Thing::_thingNone;
}
}
void InventoryMan::drawIconToViewport(IconIndice iconIndex, int16 xPos, int16 yPos) {
static byte iconBitmap[16 * 16];
Box box;
box._x2 = (box._x1 = xPos) + 15 + 1;
box._y2 = (box._y1 = yPos) + 15 + 1;
_vm->_objectMan->extractIconFromBitmap(iconIndex, iconBitmap);
_vm->_displayMan->blitToScreen(iconBitmap, 16, 0, 0, box, kColorNoTransparency, gDungeonViewport);
}
void InventoryMan::buildObjectAttributeString(int16 potentialAttribMask, int16 actualAttribMask, char** attribStrings, char* destString, char* prefixString, char* suffixString) {
uint16 identicalBitCount = 0;
int16 attribMask = 1;
for (uint16 stringIndex = 0; stringIndex < 16; stringIndex++, attribMask <<= 1) {
if (attribMask & potentialAttribMask & actualAttribMask) {
identicalBitCount++;
}
}
if (identicalBitCount == 0) {
*destString = '\0';
return;
}
strcpy(destString, prefixString);
attribMask = 1;
for (uint16 stringIndex = 0; stringIndex < 16; stringIndex++, attribMask <<= 1) {
if (attribMask & potentialAttribMask & actualAttribMask) {
strcat(destString, attribStrings[stringIndex]);
if (identicalBitCount-- > 2) {
strcat(destString, ", ");
} else if (identicalBitCount == 1) {
strcat(destString, " AND "); // TODO: localization
}
}
}
strcat(destString, suffixString);
}
void InventoryMan::drawPanelObjectDescriptionString(char* descString) {
if (descString[0] == '\f') { // form feed
descString++;
_objDescTextXpos = 108;
_objDescTextYpos = 59;
}
if (descString[0]) {
char stringTmpBuff[128];
strcpy(stringTmpBuff, descString);
char *stringLine = stringTmpBuff;
bool severalLines = false;
char *string = nullptr;
while (*stringLine) {
if (strlen(stringLine) > 18) { // if string is too long to fit on one line
string = &stringLine[17];
while (*string != ' ') // go back to the last space character
string--;
*string = '\0'; // and split the string there
severalLines = true;
}
_vm->_textMan->printToViewport(_objDescTextXpos, _objDescTextYpos, kColorLightestGray, stringLine);
_objDescTextYpos += 7;
if (severalLines) {
severalLines = false;
stringLine = ++string;
} else {
*stringLine = '\0';
}
}
}
}
Box gBoxArrowOrEye = Box(83, 98, 57, 65); // @ G0033_s_Graphic562_Box_ArrowOrEye
void InventoryMan::drawPanelArrowOrEye(bool pressingEye) {
DisplayMan &dispMan = *_vm->_displayMan;
dispMan.blitToScreen(dispMan.getBitmap(pressingEye ? kEyeForObjectDescriptionIndice : kArrowForChestContentIndice),
16, 0, 0, gBoxArrowOrEye, kColorRed, gDungeonViewport);
}
}
|