scummvm/scumm/verbs.cpp

587 lines
15 KiB
C++
Raw Normal View History

2001-10-09 14:30:12 +00:00
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001-2004 The ScummVM project
2001-10-09 14:30:12 +00:00
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
2001-10-09 14:30:12 +00:00
*
*/
#include "stdafx.h"
#include "scumm/charset.h"
#include "scumm/object.h"
#include "scumm/resource.h"
#include "scumm/scumm.h"
#include "scumm/verbs.h"
namespace Scumm {
2001-10-09 14:30:12 +00:00
enum {
kInventoryUpArrow = 4,
kInventoryDownArrow = 5,
kSentenceLine = 6
};
void ScummEngine::initV2MouseOver() {
int i;
int arrow_color, color, hi_color;
if (_version == 1) {
color = 16;
hi_color = 7;
arrow_color = 6;
} else {
color = 13;
hi_color = 14;
arrow_color = 1;
}
v2_mouseover_box = -1;
// Inventory items
for (i = 0; i < 2; i++) {
v2_mouseover_boxes[2 * i].rect.left = 0;
v2_mouseover_boxes[2 * i].rect.right = 144;
v2_mouseover_boxes[2 * i].rect.top = 32 + 8 * i;
v2_mouseover_boxes[2 * i].rect.bottom = v2_mouseover_boxes[2 * i].rect.top + 8;
v2_mouseover_boxes[2 * i].color = color;
v2_mouseover_boxes[2 * i].hicolor = hi_color;
v2_mouseover_boxes[2 * i + 1].rect.left = 176;
v2_mouseover_boxes[2 * i + 1].rect.right = 320;
v2_mouseover_boxes[2 * i + 1].rect.top = v2_mouseover_boxes[2 * i].rect.top;
v2_mouseover_boxes[2 * i + 1].rect.bottom = v2_mouseover_boxes[2 * i].rect.bottom;
v2_mouseover_boxes[2 * i + 1].color = color;
v2_mouseover_boxes[2 * i + 1].hicolor = hi_color;
}
// Inventory arrows
v2_mouseover_boxes[kInventoryUpArrow].rect.left = 144;
v2_mouseover_boxes[kInventoryUpArrow].rect.right = 176;
v2_mouseover_boxes[kInventoryUpArrow].rect.top = 32;
v2_mouseover_boxes[kInventoryUpArrow].rect.bottom = 40;
v2_mouseover_boxes[kInventoryUpArrow].color = arrow_color;
v2_mouseover_boxes[kInventoryUpArrow].hicolor = hi_color;
v2_mouseover_boxes[kInventoryDownArrow].rect.left = 144;
v2_mouseover_boxes[kInventoryDownArrow].rect.right = 176;
v2_mouseover_boxes[kInventoryDownArrow].rect.top = 40;
v2_mouseover_boxes[kInventoryDownArrow].rect.bottom = 48;
v2_mouseover_boxes[kInventoryDownArrow].color = arrow_color;
v2_mouseover_boxes[kInventoryDownArrow].hicolor = hi_color;
// Sentence line
v2_mouseover_boxes[kSentenceLine].rect.left = 0;
v2_mouseover_boxes[kSentenceLine].rect.right = 320;
v2_mouseover_boxes[kSentenceLine].rect.top = 0;
v2_mouseover_boxes[kSentenceLine].rect.bottom = 8;
v2_mouseover_boxes[kSentenceLine].color = color;
v2_mouseover_boxes[kSentenceLine].hicolor = hi_color;
}
void ScummEngine::checkV2MouseOver(Common::Point pos) {
VirtScreen *vs = &virtscr[kVerbVirtScreen];
Common::Rect rect;
byte *ptr, *dst;
int i, x, y, new_box = -1;
// Don't do anything unless the inventory is active
if (!(_userState & 64)) {
v2_mouseover_box = -1;
return;
}
if (_cursor.state > 0) {
for (i = 0; i < ARRAYSIZE(v2_mouseover_boxes); i++) {
if (v2_mouseover_boxes[i].rect.contains(pos.x, pos.y - vs->topline)) {
new_box = i;
break;
}
}
}
if (new_box != v2_mouseover_box) {
if (v2_mouseover_box != -1) {
rect = v2_mouseover_boxes[v2_mouseover_box].rect;
dst = ptr = vs->screenPtr + vs->xstart + rect.top * vs->width + rect.left;
// Remove highlight.
for (y = rect.height() - 1; y >= 0; y--) {
for (x = rect.width() - 1; x >= 0; x--) {
if (dst[x] == v2_mouseover_boxes[v2_mouseover_box].hicolor)
dst[x] = v2_mouseover_boxes[v2_mouseover_box].color;
}
dst += vs->width;
}
2004-01-08 03:24:41 +00:00
markRectAsDirty(kVerbVirtScreen, rect);
}
if (new_box != -1) {
rect = v2_mouseover_boxes[new_box].rect;
dst = ptr = vs->screenPtr + vs->xstart + rect.top * vs->width + rect.left;
// Apply highlight
for (y = rect.height() - 1; y >= 0; y--) {
for (x = rect.width() - 1; x >= 0; x--) {
if (dst[x] == v2_mouseover_boxes[new_box].color)
dst[x] = v2_mouseover_boxes[new_box].hicolor;
}
dst += vs->width;
}
2004-01-08 03:24:41 +00:00
markRectAsDirty(kVerbVirtScreen, rect);
}
v2_mouseover_box = new_box;
}
}
void ScummEngine::checkV2Inventory(int x, int y) {
int object = 0;
y -= virtscr[kVerbVirtScreen].topline;
if ((y < 34) || !(_mouseButStat & MBS_LEFT_CLICK))
return;
if (v2_mouseover_boxes[kInventoryUpArrow].rect.contains(x, y)) {
if (_inventoryOffset >= 2) {
_inventoryOffset -= 2;
redrawV2Inventory();
}
} else if (v2_mouseover_boxes[kInventoryDownArrow].rect.contains(x, y)) {
if (_inventoryOffset + 4 < getInventoryCount(_scummVars[VAR_EGO])) {
_inventoryOffset += 2;
redrawV2Inventory();
}
}
for (object = 0; object < 4; object++) {
if (v2_mouseover_boxes[object].rect.contains(x, y)) {
break;
}
}
if (object >= 4)
return;
object = findInventory(_scummVars[VAR_EGO], object + 1 + _inventoryOffset);
if (object > 0) {
2003-05-21 23:54:39 +00:00
runInputScript(3, object, 0);
}
}
void ScummEngine::redrawV2Inventory() {
VirtScreen *vs = &virtscr[kVerbVirtScreen];
int i;
int max_inv;
Common::Rect inventoryBox;
2003-05-21 09:29:24 +00:00
v2_mouseover_box = -1;
if (!(_userState & 64)) // Don't draw inventory unless active
return;
2003-05-21 09:29:24 +00:00
// Clear on all invocations
inventoryBox.top = vs->topline + 32;
inventoryBox.bottom = vs->topline + virtscr[2].height;
inventoryBox.left = 0;
inventoryBox.right = vs->width;
restoreBG(inventoryBox);
_string[1].charset = 1;
2003-05-21 09:29:24 +00:00
max_inv = getInventoryCount(_scummVars[VAR_EGO]) - _inventoryOffset;
if (max_inv > 4)
max_inv = 4;
for (i = 0; i < max_inv; i++) {
int obj = findInventory(_scummVars[VAR_EGO], i + 1 + _inventoryOffset);
if (obj == 0)
break;
_string[1].ypos = v2_mouseover_boxes[i].rect.top + vs->topline;
_string[1].xpos = v2_mouseover_boxes[i].rect.left;
2003-05-21 09:29:24 +00:00
_string[1].color = v2_mouseover_boxes[i].color;
const byte *tmp = getObjOrActorName(obj);
assert(tmp);
// Prevent inventory entries from overflowing by truncating the text
// after 144/8 = 18 chars
byte msg[18 + 1];
msg[18] = 0;
strncpy((char *)msg, (const char *)tmp, 18);
// Draw it
drawString(1, msg);
2003-05-21 09:29:24 +00:00
}
// If necessary, draw "up" arrow
if (_inventoryOffset > 0) {
_string[1].xpos = v2_mouseover_boxes[kInventoryUpArrow].rect.left;
_string[1].ypos = v2_mouseover_boxes[kInventoryUpArrow].rect.top + vs->topline;
_string[1].color = v2_mouseover_boxes[kInventoryUpArrow].color;
drawString(1, (const byte *)" \1\2");
2003-05-21 09:29:24 +00:00
}
// If necessary, draw "down" arrow
if (_inventoryOffset + 4 < getInventoryCount(_scummVars[VAR_EGO])) {
_string[1].xpos = v2_mouseover_boxes[kInventoryDownArrow].rect.left;
_string[1].ypos = v2_mouseover_boxes[kInventoryDownArrow].rect.top + vs->topline;
_string[1].color = v2_mouseover_boxes[kInventoryDownArrow].color;
drawString(1, (const byte *)" \3\4");
2003-05-21 09:29:24 +00:00
}
}
void ScummEngine::redrawVerbs() {
if (_version <= 2 && !(_userState & 128)) // Don't draw verbs unless active
return;
int i, verb = 0;
if ((_gameId == GID_FT) || _cursor.state > 0)
verb = checkMouseOver(_mouse.x, _mouse.y);
for (i = _numVerbs-1; i >= 0; i--) {
if (i == verb && _verbs[verb].hicolor)
drawVerb(i, 1);
else
drawVerb(i, 0);
}
_verbMouseOver = verb;
2001-10-09 14:30:12 +00:00
}
void ScummEngine::checkExecVerbs() {
int i, over;
2001-10-09 14:30:12 +00:00
VerbSlot *vs;
if (_userPut <= 0 || _mouseButStat == 0)
2001-10-09 14:30:12 +00:00
return;
if (_mouseButStat < MBS_MAX_KEY) {
2001-10-09 14:30:12 +00:00
/* Check keypresses */
2001-10-16 10:01:48 +00:00
vs = &_verbs[1];
for (i = 1; i < _numVerbs; i++, vs++) {
if (vs->verbid && vs->saveid == 0 && vs->curmode == 1) {
2001-10-09 14:30:12 +00:00
if (_mouseButStat == vs->key) {
// Trigger verb as if the user clicked it
2001-10-09 14:30:12 +00:00
runInputScript(1, vs->verbid, 1);
return;
}
}
}
// Generic keyboard input
2001-10-09 14:30:12 +00:00
runInputScript(4, _mouseButStat, 1);
} else if (_mouseButStat & MBS_MOUSE_MASK) {
2003-05-21 23:54:39 +00:00
VirtScreen *zone = findVirtScreen(_mouse.y);
byte code = _mouseButStat & MBS_LEFT_CLICK ? 1 : 2;
if (_version <= 2 && zone->number == 2 && _mouse.y <= zone->topline + 8) {
// Click into V2 sentence line
runInputScript(5, 0, 0);
} else if (_version <= 2 && zone->number == 2 && _mouse.y > zone->topline + 32) {
// Click into V2 inventory
2003-05-21 23:54:39 +00:00
checkV2Inventory(_mouse.x, _mouse.y);
2001-10-09 14:30:12 +00:00
} else {
over = checkMouseOver(_mouse.x, _mouse.y);
if (over != 0) {
// Verb was clicked
runInputScript(1, _verbs[over].verbid, code);
} else {
// Scene was clicked
runInputScript((zone->number == 0) ? 2 : 1, 0, code);
}
2001-10-09 14:30:12 +00:00
}
}
}
void ScummEngine::verbMouseOver(int verb) {
// Don't do anything unless verbs are active
if (_version <= 2 && !(_userState & 128))
return;
if (_gameId == GID_FT)
return;
if (_verbMouseOver == verb)
2001-10-09 14:30:12 +00:00
return;
2002-11-29 18:27:35 +00:00
if (_verbs[_verbMouseOver].type != kImageVerbType) {
2001-10-09 14:30:12 +00:00
drawVerb(_verbMouseOver, 0);
_verbMouseOver = verb;
}
2002-11-29 18:27:35 +00:00
if (_verbs[verb].type != kImageVerbType && _verbs[verb].hicolor) {
drawVerb(verb, 1);
2001-10-09 14:30:12 +00:00
_verbMouseOver = verb;
}
}
int ScummEngine::checkMouseOver(int x, int y) const {
2001-10-09 14:30:12 +00:00
VerbSlot *vs;
int i = _numVerbs - 1;
2001-10-09 14:30:12 +00:00
2001-10-16 10:01:48 +00:00
vs = &_verbs[i];
2001-10-09 14:30:12 +00:00
do {
if (vs->curmode != 1 || !vs->verbid || vs->saveid || y < vs->curRect.top || y >= vs->curRect.bottom)
continue;
2001-10-09 14:30:12 +00:00
if (vs->center) {
if (x < -(vs->curRect.right - 2 * vs->curRect.left) || x >= vs->curRect.right)
continue;
} else {
if (x < vs->curRect.left || x >= vs->curRect.right)
2001-10-09 14:30:12 +00:00
continue;
}
2001-10-09 14:30:12 +00:00
return i;
} while (--vs, --i);
2001-10-09 14:30:12 +00:00
return 0;
}
void ScummEngine::drawVerb(int verb, int mode) {
2001-10-09 14:30:12 +00:00
VerbSlot *vs;
bool tmp;
2001-10-09 14:30:12 +00:00
2002-12-13 00:52:14 +00:00
if (!verb)
2001-10-09 14:30:12 +00:00
return;
2002-12-13 00:52:14 +00:00
vs = &_verbs[verb];
2001-10-09 14:30:12 +00:00
if (!vs->saveid && vs->curmode && vs->verbid) {
2002-11-29 18:27:35 +00:00
if (vs->type == kImageVerbType) {
drawVerbBitmap(verb, vs->curRect.left, vs->curRect.top);
2001-10-09 14:30:12 +00:00
return;
}
2004-07-17 10:25:22 +00:00
restoreVerbBG(verb);
2001-10-09 14:30:12 +00:00
_string[4].charset = vs->charset_nr;
_string[4].xpos = vs->curRect.left;
_string[4].ypos = vs->curRect.top;
_string[4].right = _screenWidth - 1;
_string[4].center = vs->center;
if (vs->curmode == 2)
_string[4].color = vs->dimcolor;
else if (mode && vs->hicolor)
_string[4].color = vs->hicolor;
else
_string[4].color = vs->color;
// FIXME For the future: Indy3 and under inv scrolling
/*
2002-12-13 00:52:14 +00:00
if (verb >= 31 && verb <= 36)
verb += _inventoryOffset;
2002-07-07 20:25:23 +00:00
*/
const byte *msg = getResourceAddress(rtVerb, verb);
if (!msg)
return;
tmp = _charset->_center;
_charset->_center = 0;
drawString(4, msg);
_charset->_center = tmp;
vs->curRect.right = _charset->_str.right;
vs->curRect.bottom = _charset->_str.bottom;
vs->oldRect = _charset->_str;
_charset->_str.left = _charset->_str.right;
2004-07-17 10:25:22 +00:00
} else {
2004-07-10 11:46:15 +00:00
restoreVerbBG(verb);
2001-10-09 14:30:12 +00:00
}
}
void ScummEngine::restoreVerbBG(int verb) {
if (_gameId == GID_FT || _gameId == GID_CMI)
2004-07-17 10:25:22 +00:00
return;
2001-10-09 14:30:12 +00:00
VerbSlot *vs;
2001-10-16 10:01:48 +00:00
vs = &_verbs[verb];
2001-10-09 14:30:12 +00:00
if (vs->oldRect.left != -1) {
restoreBG(vs->oldRect, vs->bkcolor);
vs->oldRect.left = -1;
2001-10-09 14:30:12 +00:00
}
}
void ScummEngine::drawVerbBitmap(int verb, int x, int y) {
2001-10-09 14:30:12 +00:00
VirtScreen *vs;
VerbSlot *vst;
2004-01-06 19:17:01 +00:00
bool twobufs;
2003-05-28 20:01:47 +00:00
const byte *imptr = 0;
2001-10-09 14:30:12 +00:00
int ydiff, xstrip;
int imgw, imgh;
int i, tmp;
2001-10-26 17:34:50 +00:00
byte *obim;
2003-05-28 20:01:47 +00:00
const ImageHeader *imhd;
2002-02-15 17:17:35 +00:00
uint32 size;
2001-10-09 14:30:12 +00:00
if ((vs = findVirtScreen(y)) == NULL)
2001-10-09 14:30:12 +00:00
return;
2001-10-26 17:34:50 +00:00
gdi.disableZBuffer();
2001-10-09 14:30:12 +00:00
twobufs = vs->hasTwoBuffers;
vs->hasTwoBuffers = 0;
2001-10-09 14:30:12 +00:00
xstrip = x / 8;
2001-10-09 14:30:12 +00:00
ydiff = y - vs->topline;
obim = getResourceAddress(rtVerb, verb);
assert(obim);
if (_features & GF_OLD_BUNDLE) {
imgw = obim[0];
imgh = obim[1] / 8;
imptr = obim + 2;
} else if (_features & GF_SMALL_HEADER) {
size = READ_LE_UINT32(obim);
imgw = (*(obim + size + 11));
imgh = (*(obim + size + 17)) / 8;
imptr = getObjectImage(obim, 1);
} else {
2003-05-28 20:01:47 +00:00
imhd = (const ImageHeader *)findResourceData(MKID('IMHD'), obim);
if (_version >= 7) {
imgw = READ_LE_UINT16(&imhd->v7.width) / 8;
imgh = READ_LE_UINT16(&imhd->v7.height) / 8;
} else {
imgw = READ_LE_UINT16(&imhd->old.width) / 8;
imgh = READ_LE_UINT16(&imhd->old.height) / 8;
}
imptr = getObjectImage(obim, 1);
}
assert(imptr);
if (_version == 1) {
gdi._C64ObjectMode = true;
gdi.decodeC64Gfx(imptr, gdi._C64ObjectMap, imgw * imgh * 3);
}
for (i = 0; i < imgw; i++) {
2001-10-26 17:34:50 +00:00
tmp = xstrip + i;
if (tmp < gdi._numStrips)
gdi.drawBitmap(imptr, vs, tmp, ydiff, imgw * 8, imgh * 8, i, 1, Gdi::dbAllowMaskOr);
2001-10-09 14:30:12 +00:00
}
2002-12-13 00:52:14 +00:00
vst = &_verbs[verb];
vst->curRect.right = vst->curRect.left + imgw * 8;
2004-01-16 10:20:43 +00:00
vst->curRect.bottom = vst->curRect.top + imgh * 8;
vst->oldRect = vst->curRect;
2001-10-26 17:34:50 +00:00
gdi.enableZBuffer();
2001-10-09 14:30:12 +00:00
2004-01-06 19:17:01 +00:00
vs->hasTwoBuffers = twobufs;
2001-10-09 14:30:12 +00:00
}
int ScummEngine::getVerbSlot(int id, int mode) const {
2001-10-09 14:30:12 +00:00
int i;
for (i = 1; i < _numVerbs; i++) {
2001-10-16 10:01:48 +00:00
if (_verbs[i].verbid == id && _verbs[i].saveid == mode) {
2001-10-09 14:30:12 +00:00
return i;
}
}
return 0;
}
void ScummEngine::killVerb(int slot) {
2001-10-09 14:30:12 +00:00
VerbSlot *vs;
if (slot == 0)
2001-10-09 14:30:12 +00:00
return;
2001-10-16 10:01:48 +00:00
vs = &_verbs[slot];
2001-10-09 14:30:12 +00:00
vs->verbid = 0;
vs->curmode = 0;
nukeResource(rtVerb, slot);
2001-10-09 14:30:12 +00:00
2004-07-10 11:46:15 +00:00
if (vs->saveid == 0) {
drawVerb(slot, 0);
verbMouseOver(0);
}
2001-10-09 14:30:12 +00:00
vs->saveid = 0;
}
void ScummEngine::setVerbObject(uint room, uint object, uint verb) {
2003-05-28 20:01:47 +00:00
const byte *obimptr;
const byte *obcdptr;
2002-02-15 17:17:35 +00:00
uint32 size, size2;
FindObjectInRoom foir;
int i;
2001-10-09 14:30:12 +00:00
if (_heversion >= 70) { // Windows titles. Here we always ignore room
room = getObjectRoom(object);
}
if (whereIsObject(object) == WIO_FLOBJECT)
2001-10-09 14:30:12 +00:00
error("Can't grab verb image from flobject");
if (_features & GF_OLD_BUNDLE) {
for (i = (_numLocalObjects-1); i > 0; i--) {
if (_objs[i].obj_nr == object) {
findObjectInRoom(&foir, foImageHeader, object, room);
size = READ_LE_UINT16(foir.obim);
byte *ptr = createResource(rtVerb, verb, size + 2);
obcdptr = getResourceAddress(rtRoom, room) + getOBCDOffs(object);
ptr[0] = *(obcdptr + 9); // Width
ptr[1] = *(obcdptr + 15); // Height
memcpy(ptr + 2, foir.obim, size);
return;
}
}
} else if (_features & GF_SMALL_HEADER) {
2003-01-12 08:37:59 +00:00
for (i = (_numLocalObjects-1); i > 0; i--) {
if (_objs[i].obj_nr == object) {
// FIXME - the only thing we need from the OBCD is the image size!
// So we could use almost the same code (save for offsets)
// as in the GF_OLD_BUNDLE code. But of course that would break save games
// unless we insert special conversion code... <sigh>
findObjectInRoom(&foir, foImageHeader, object, room);
size = READ_LE_UINT32(foir.obim);
2002-02-15 17:17:35 +00:00
obcdptr = getResourceAddress(rtRoom, room) + getOBCDOffs(object);
size2 = READ_LE_UINT32(obcdptr);
createResource(rtVerb, verb, size + size2);
obimptr = getResourceAddress(rtRoom, room) - foir.roomptr + foir.obim;
2002-02-15 17:17:35 +00:00
obcdptr = getResourceAddress(rtRoom, room) + getOBCDOffs(object);
memcpy(getResourceAddress(rtVerb, verb), obimptr, size);
memcpy(getResourceAddress(rtVerb, verb) + size, obcdptr, size2);
return;
}
}
} else {
findObjectInRoom(&foir, foImageHeader, object, room);
size = READ_BE_UINT32(foir.obim + 4);
createResource(rtVerb, verb, size);
obimptr = getResourceAddress(rtRoom, room) - foir.roomptr + foir.obim;
memcpy(getResourceAddress(rtVerb, verb), obimptr, size);
}
2001-10-09 14:30:12 +00:00
}
} // End of namespace Scumm