aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/message.cpp
diff options
context:
space:
mode:
authorWalter van Niftrik2009-05-13 19:03:12 +0000
committerWalter van Niftrik2009-05-13 19:03:12 +0000
commite35b77e0a764cd7e7f510a0849a36f90683e0d68 (patch)
tree7924d224c3eadb9e337a221a0927c02b375e6e87 /engines/sci/engine/message.cpp
parentf0182121f79685063edb50c9647e4f6f40bd0ba6 (diff)
downloadscummvm-rg350-e35b77e0a764cd7e7f510a0849a36f90683e0d68.tar.gz
scummvm-rg350-e35b77e0a764cd7e7f510a0849a36f90683e0d68.tar.bz2
scummvm-rg350-e35b77e0a764cd7e7f510a0849a36f90683e0d68.zip
SCI: Message: Added a few more subfunctions; cleanup.
svn-id: r40529
Diffstat (limited to 'engines/sci/engine/message.cpp')
-rw-r--r--engines/sci/engine/message.cpp112
1 files changed, 69 insertions, 43 deletions
diff --git a/engines/sci/engine/message.cpp b/engines/sci/engine/message.cpp
index 0a6754fa03..8b137a1b29 100644
--- a/engines/sci/engine/message.cpp
+++ b/engines/sci/engine/message.cpp
@@ -28,29 +28,37 @@
namespace Sci {
-void MessageState::parse(IndexRecordCursor *cursor, MessageTuple *t) {
- t->noun = *(cursor->index_record + 0);
- t->verb = *(cursor->index_record + 1);
+MessageTuple MessageState::getTuple() {
+ MessageTuple t;
+
+ t.noun = *(_engineCursor.index_record + 0);
+ t.verb = *(_engineCursor.index_record + 1);
if (_version == 2101) {
- t->cond = 0;
- t->seq = 1;
+ t.cond = 0;
+ t.seq = 1;
} else {
- t->cond = *(cursor->index_record + 2);
- t->seq = *(cursor->index_record + 3);
+ t.cond = *(_engineCursor.index_record + 2);
+ t.seq = *(_engineCursor.index_record + 3);
}
+
+ return t;
}
-void MessageState::parseRef(IndexRecordCursor *cursor, MessageTuple *t) {
+MessageTuple MessageState::getRefTuple() {
+ MessageTuple t;
+
if (_version == 2101) {
- t->noun = 0;
- t->verb = 0;
- t->cond = 0;
+ t.noun = 0;
+ t.verb = 0;
+ t.cond = 0;
} else {
- t->noun = *(cursor->index_record + 7);
- t->verb = *(cursor->index_record + 8);
- t->cond = *(cursor->index_record + 9);
+ t.noun = *(_engineCursor.index_record + 7);
+ t.verb = *(_engineCursor.index_record + 8);
+ t.cond = *(_engineCursor.index_record + 9);
}
- t->seq = 1;
+ t.seq = 1;
+
+ return t;
}
void MessageState::initCursor() {
@@ -67,20 +75,22 @@ void MessageState::advanceCursor(bool increaseSeq) {
_engineCursor.nextSeq++;
}
-int MessageState::getMessage(MessageTuple *t) {
+int MessageState::findTuple(MessageTuple &t) {
+ if (_module == -1)
+ return 0;
+
// Reset the cursor
initCursor();
- _engineCursor.nextSeq = t->seq;
+ _engineCursor.nextSeq = t.seq;
// Do a linear search for the message
while (1) {
- MessageTuple looking_at;
- parse(&_engineCursor, &looking_at);
+ MessageTuple looking_at = getTuple();
- if (t->noun == looking_at.noun &&
- t->verb == looking_at.verb &&
- t->cond == looking_at.cond &&
- t->seq == looking_at.seq)
+ if (t.noun == looking_at.noun &&
+ t.verb == looking_at.verb &&
+ t.cond == looking_at.cond &&
+ t.seq == looking_at.seq)
break;
advanceCursor(false);
@@ -90,15 +100,16 @@ int MessageState::getMessage(MessageTuple *t) {
return 0;
}
- return getNext();
+ return 1;
}
-int MessageState::getNext() {
+int MessageState::getMessage() {
+ if (_module == -1)
+ return 0;
+
if (_engineCursor.index != _recordCount) {
- MessageTuple mesg;
- parse(&_engineCursor, &mesg);
- MessageTuple ref;
- parseRef(&_engineCursor, &ref);
+ MessageTuple mesg = getTuple();
+ MessageTuple ref = getRefTuple();
if (_engineCursor.nextSeq == mesg.seq) {
// We found the right sequence number, check for recursion
@@ -107,8 +118,8 @@ int MessageState::getNext() {
// Recursion, advance the current cursor and load the reference
advanceCursor(true);
- if (getMessage(&ref))
- return getNext();
+ if (findTuple(ref))
+ return getMessage();
else {
// Reference not found
return 0;
@@ -123,7 +134,7 @@ int MessageState::getNext() {
// We either ran out of records, or found an incorrect sequence number. Go to previous stack frame.
if (!_cursorStack.empty()) {
_engineCursor = _cursorStack.pop();
- return getNext();
+ return getMessage();
}
// Stack is empty, no message available
@@ -134,10 +145,22 @@ int MessageState::getTalker() {
return (_version == 2101) ? -1 : *(_engineCursor.index_record + 4);
}
-void MessageState::getText(char *buffer) {
+MessageTuple &MessageState::getLastTuple() {
+ return _lastReturned;
+}
+
+int MessageState::getLastModule() {
+ return _lastReturnedModule;
+}
+
+char *MessageState::getText() {
int offset = READ_LE_UINT16(_engineCursor.index_record + ((_version == 2101) ? 2 : 5));
- char *stringptr = (char *)_currentResource->data + offset;
- strcpy(buffer, stringptr);
+ return (char *)_currentResource->data + offset;
+}
+
+void MessageState::gotoNext() {
+ _lastReturned = getTuple();
+ _lastReturnedModule = _module;
advanceCursor(true);
}
@@ -148,24 +171,27 @@ int MessageState::getLength() {
}
int MessageState::loadRes(ResourceManager *resmgr, int module, bool lock) {
- if (_module == module)
- return 1;
+ if (_locked) {
+ // We already have a locked resource
+ if (_module == module) {
+ // If it's the same resource, we are done
+ return 1;
+ }
- // Unlock old resource
- if (_module != -1) {
+ // Otherwise, free the old resource
resmgr->unlockResource(_currentResource, _module, kResourceTypeMessage);
- _module = -1;
+ _locked = false;
}
_currentResource = resmgr->findResource(kResourceTypeMessage, module, lock);
if (_currentResource == NULL || _currentResource->data == NULL) {
- warning("Message subsystem: failed to load %d.msg", module);
+ warning("Message: failed to load %d.msg", module);
return 0;
}
- if (lock)
- _module = module;
+ _module = module;
+ _locked = lock;
_version = READ_LE_UINT16(_currentResource->data);