aboutsummaryrefslogtreecommitdiff
path: root/saga
diff options
context:
space:
mode:
authorJoost Peters2004-08-01 23:47:19 +0000
committerJoost Peters2004-08-01 23:47:19 +0000
commit2a7f8854777318edab70ec00164424a79572f721 (patch)
tree86d2748b3d82ada8a88483a6a3e3fea39a830941 /saga
parent873c886910f43cce67cfaf25e671c4adc70ebb7b (diff)
downloadscummvm-rg350-2a7f8854777318edab70ec00164424a79572f721.tar.gz
scummvm-rg350-2a7f8854777318edab70ec00164424a79572f721.tar.bz2
scummvm-rg350-2a7f8854777318edab70ec00164424a79572f721.zip
Clean up Script class a bit; add get/set functions
svn-id: r14428
Diffstat (limited to 'saga')
-rw-r--r--saga/script.cpp114
-rw-r--r--saga/script.h31
-rw-r--r--saga/sdata.cpp26
-rw-r--r--saga/sdebug.cpp5
-rw-r--r--saga/sthread.cpp28
5 files changed, 104 insertions, 100 deletions
diff --git a/saga/script.cpp b/saga/script.cpp
index b3ebae9fa5..541f306b7b 100644
--- a/saga/script.cpp
+++ b/saga/script.cpp
@@ -62,19 +62,16 @@ Script::Script() {
int i, j;
//initialize member variables
- _initialized = 0;
- _script_ctxt = 0;
- _voice_lut_present = 0;
- _script_lut = 0;
- _script_lut_max = 0;
- _script_lut_entrylen = 0;
- _current_script = 0;
- _thread_list = 0;
- memset(_data_buf, 0, sizeof(_data_buf));
+ _scriptContext = 0;
+ _voiceLUTPresent = false;
+ _scriptLUTEntryLen = 0;
+ _currentScript = 0;
+ _threadList = 0;
+ memset(_dataBuf, 0, sizeof(_dataBuf));
debug(0, "Initializing scripting subsystem");
// Load script resource file context
- result = GAME_GetFileContext(&_script_ctxt, R_GAME_SCRIPTFILE, 0);
+ result = GAME_GetFileContext(&_scriptContext, R_GAME_SCRIPTFILE, 0);
if (result != R_SUCCESS) {
error("Couldn't get script file context");
}
@@ -92,48 +89,49 @@ Script::Script() {
// Create logical script LUT from resource
if (rsc_len % R_S_LUT_ENTRYLEN_ITECD == 0) {
- _script_lut_entrylen = R_S_LUT_ENTRYLEN_ITECD;
+ _scriptLUTEntryLen = R_S_LUT_ENTRYLEN_ITECD;
} else if (rsc_len % R_S_LUT_ENTRYLEN_ITEDISK == 0) {
- _script_lut_entrylen = R_S_LUT_ENTRYLEN_ITEDISK;
+ _scriptLUTEntryLen = R_S_LUT_ENTRYLEN_ITEDISK;
} else {
error("Error: Invalid script lookup table length");
}
// Calculate number of entries
- _script_lut_max = rsc_len / _script_lut_entrylen;
+ _scriptLUTMax = rsc_len / _scriptLUTEntryLen;
// Allocate space for logical LUT
- _script_lut = (R_SCRIPT_LUT_ENTRY *)malloc(_script_lut_max * sizeof(R_SCRIPT_LUT_ENTRY));
- if (_script_lut == NULL) {
+ _scriptLUT = (R_SCRIPT_LUT_ENTRY *)malloc(_scriptLUTMax * sizeof(R_SCRIPT_LUT_ENTRY));
+ if (_scriptLUT == NULL) {
error("Error: Couldn't allocate memory for script resource look-up table");
}
// Convert LUT resource to logical LUT
MemoryReadStream readS(rsc_ptr, rsc_len);
- for (i = 0; i < _script_lut_max; i++) {
+ for (i = 0; i < _scriptLUTMax; i++) {
prevTell = readS.pos();
- _script_lut[i].script_rn = readS.readUint16LE();
- _script_lut[i].diag_list_rn = readS.readUint16LE();
- _script_lut[i].voice_lut_rn = readS.readUint16LE();
+ _scriptLUT[i].script_rn = readS.readUint16LE();
+ _scriptLUT[i].diag_list_rn = readS.readUint16LE();
+ _scriptLUT[i].voice_lut_rn = readS.readUint16LE();
+
// Skip the unused portion of the structure
- for (j = readS.pos(); j < prevTell + _script_lut_entrylen; j++)
+ for (j = readS.pos(); j < prevTell + _scriptLUTEntryLen; j++)
readS.readByte();
}
RSC_FreeResource(rsc_ptr);
// Any voice lookup table resources present?
- for (i = 0; i < _script_lut_max; i++) {
- if (_script_lut[i].voice_lut_rn) {
- _voice_lut_present = 1;
+ for (i = 0; i < _scriptLUTMax; i++) {
+ if (_scriptLUT[i].voice_lut_rn) {
+ _voiceLUTPresent = true;
break;
}
}
// Initialize script submodules
- _thread_list = ys_dll_create();
+ _threadList = ys_dll_create();
- _initialized = 1;
+ _initialized = true;
}
// Shut down script module gracefully; free all allocated module resources
@@ -148,17 +146,17 @@ Script::~Script() {
debug(0, "Shutting down scripting subsystem.");
// Free script lookup table
- free(_script_lut);
+ free(_scriptLUT);
// Stop all threads and destroy them
- for (thread_node = ys_dll_head(_thread_list); thread_node != NULL;
+ for (thread_node = ys_dll_head(_threadList); thread_node != NULL;
thread_node = ys_dll_next(thread_node)) {
thread = (R_SCRIPT_THREAD *)ys_dll_get_data(thread_node);
STHREAD_Destroy(thread);
}
- _initialized = 0;
+ _initialized = false;
}
// Loads a script; including script bytecode and dialogue list
@@ -180,7 +178,7 @@ int Script::loadScript(int script_num) {
}
// Validate script number
- if ((script_num < 0) || (script_num > _script_lut_max)) {
+ if ((script_num < 0) || (script_num > _scriptLUTMax)) {
warning("Script::loadScript(): Invalid script number");
return R_FAILURE;
}
@@ -205,9 +203,9 @@ int Script::loadScript(int script_num) {
script_data->voice = NULL;
// Load script bytecode
- scriptl_rn = _script_lut[script_num].script_rn;
+ scriptl_rn = _scriptLUT[script_num].script_rn;
- result = RSC_LoadResource(_script_ctxt, scriptl_rn, &bytecode_p, &bytecode_len);
+ result = RSC_LoadResource(_scriptContext, scriptl_rn, &bytecode_p, &bytecode_len);
if (result != R_SUCCESS) {
warning("Error loading script bytecode resource");
free(script_data);
@@ -224,10 +222,10 @@ int Script::loadScript(int script_num) {
}
// Load script dialogue list
- diagl_rn = _script_lut[script_num].diag_list_rn;
+ diagl_rn = _scriptLUT[script_num].diag_list_rn;
// Load dialogue list resource
- result = RSC_LoadResource(_script_ctxt, diagl_rn, &diagl_p, &diagl_len);
+ result = RSC_LoadResource(_scriptContext, diagl_rn, &diagl_p, &diagl_len);
if (result != R_SUCCESS) {
warning("Error loading dialogue list resource");
free(script_data);
@@ -246,11 +244,11 @@ int Script::loadScript(int script_num) {
}
// Load voice resource lookup table
- if (_voice_lut_present) {
- voicelut_rn = _script_lut[script_num].voice_lut_rn;
+ if (_voiceLUTPresent) {
+ voicelut_rn = _scriptLUT[script_num].voice_lut_rn;
// Load voice LUT resource
- result = RSC_LoadResource(_script_ctxt, voicelut_rn, &voicelut_p, &voicelut_len);
+ result = RSC_LoadResource(_scriptContext, voicelut_rn, &voicelut_p, &voicelut_len);
if (result != R_SUCCESS) {
warning("Error loading voice LUT resource");
free(script_data);
@@ -273,45 +271,45 @@ int Script::loadScript(int script_num) {
// Finish initialization
script_data->loaded = 1;
- _current_script = script_data;
+ _currentScript = script_data;
return R_SUCCESS;
}
// Frees all resources associated with current script.
int Script::freeScript() {
- if (_current_script == NULL) {
+ if (_currentScript == NULL) {
return R_FAILURE;
}
- if (!_current_script->loaded) {
+ if (!_currentScript->loaded) {
return R_FAILURE;
}
debug(0, "Releasing script data.");
// Finish initialization
- if (_current_script->diag != NULL) {
- free(_current_script->diag->str);
- free(_current_script->diag->str_off);
+ if (_currentScript->diag != NULL) {
+ free(_currentScript->diag->str);
+ free(_currentScript->diag->str_off);
}
- free(_current_script->diag);
+ free(_currentScript->diag);
- if (_current_script->bytecode != NULL) {
- free(_current_script->bytecode->entrypoints);
- RSC_FreeResource(_current_script->bytecode->bytecode_p);
+ if (_currentScript->bytecode != NULL) {
+ free(_currentScript->bytecode->entrypoints);
+ RSC_FreeResource(_currentScript->bytecode->bytecode_p);
}
- free(_current_script->bytecode);
+ free(_currentScript->bytecode);
- if (_voice_lut_present) {
- free(_current_script->voice->voices);
- free(_current_script->voice);
+ if (_voiceLUTPresent) {
+ free(_currentScript->voice->voices);
+ free(_currentScript->voice);
}
- free(_current_script);
+ free(_currentScript);
- _current_script = NULL;
+ _currentScript = NULL;
return R_SUCCESS;
}
@@ -494,21 +492,21 @@ void CF_script_info(int argc, char *argv[], void *refCon) {
uint32 i;
char *name_ptr;
- if (((Script *)refCon)->_current_script == NULL) {
+ if (((Script *)refCon)->currentScript() == NULL) {
return;
}
- if (!((Script *)refCon)->_current_script->loaded) {
+ if (!((Script *)refCon)->currentScript()->loaded) {
return;
}
- n_entrypoints = ((Script *)refCon)->_current_script->bytecode->n_entrypoints;
+ n_entrypoints = ((Script *)refCon)->currentScript()->bytecode->n_entrypoints;
CON_Print("Current script contains %d entrypoints:", n_entrypoints);
for (i = 0; i < n_entrypoints; i++) {
- name_ptr = (char *)((Script *)refCon)->_current_script->bytecode->bytecode_p +
- ((Script *)refCon)->_current_script->bytecode->entrypoints[i].name_offset;
+ name_ptr = (char *)((Script *)refCon)->currentScript()->bytecode->bytecode_p +
+ ((Script *)refCon)->currentScript()->bytecode->entrypoints[i].name_offset;
CON_Print("%lu: %s", i, name_ptr);
}
}
@@ -531,7 +529,7 @@ void CF_script_exec(int argc, char *argv[], void *refCon) {
}
}
- if (ep_num >= ((Script *)refCon)->_current_script->bytecode->n_entrypoints) {
+ if (ep_num >= ((Script *)refCon)->currentScript()->bytecode->n_entrypoints) {
CON_Print("Invalid entrypoint.");
return;
}
diff --git a/saga/script.h b/saga/script.h
index d318c2e08f..5df5f79587 100644
--- a/saga/script.h
+++ b/saga/script.h
@@ -104,18 +104,25 @@ public:
R_VOICE_LUT *loadVoiceLUT(const byte *voicelut_p, size_t voicelut_len, R_SCRIPTDATA *script);
int disassemble(R_SCRIPT_BYTECODE *script_list, R_DIALOGUE_LIST *diag_list);
-
-//protected:
-//these are temporarily public, eventually get/set methods should be created for them
- int _initialized;
- R_RSCFILE_CONTEXT *_script_ctxt;
- int _voice_lut_present;
- R_SCRIPT_LUT_ENTRY *_script_lut;
- int _script_lut_max;
- uint16 _script_lut_entrylen;
- R_SCRIPTDATA *_current_script;
- YS_DL_LIST *_thread_list;
- R_SCRIPT_DATABUF *_data_buf[R_SCRIPT_DATABUF_NUM];
+ bool isInitialized() const { return _initialized; }
+ bool isVoiceLUTPresent() const { return _voiceLUTPresent; }
+ R_SCRIPTDATA *currentScript() { return _currentScript; }
+ void setBuffer(int idx, R_SCRIPT_DATABUF *ptr) { _dataBuf[idx] = ptr; }
+ R_SCRIPT_DATABUF *dataBuffer(int idx) { return _dataBuf[idx]; }
+ YS_DL_LIST *threadList() { return _threadList; }
+
+protected:
+ bool _initialized;
+ bool _voiceLUTPresent;
+ R_RSCFILE_CONTEXT *_scriptContext;
+ R_SCRIPT_LUT_ENTRY *_scriptLUT;
+ int _scriptLUTMax;
+ uint16 _scriptLUTEntryLen;
+ R_SCRIPTDATA *_currentScript;
+ R_SCRIPT_DATABUF *_dataBuf[R_SCRIPT_DATABUF_NUM];
+ YS_DL_LIST *_threadList;
+
+public:
int _dbg_singlestep;
int _dbg_dostep;
R_SCRIPT_THREAD *_dbg_thread;
diff --git a/saga/sdata.cpp b/saga/sdata.cpp
index f88510cad1..0e791d7063 100644
--- a/saga/sdata.cpp
+++ b/saga/sdata.cpp
@@ -37,20 +37,20 @@ SData::SData() {
debug(0, "Initializing script data buffers");
for (i = 0; i < R_SCRIPT_DATABUF_NUM; i++) {
- alloc_ptr = malloc(sizeof *_vm->_script->_data_buf[0]);
+ alloc_ptr = malloc(sizeof *_vm->_script->dataBuffer(0));
if (alloc_ptr == NULL) {
error("Couldn't allocate memory for script data buffer %d", i);
}
- _vm->_script->_data_buf[i] = (R_SCRIPT_DATABUF *)alloc_ptr;
+ _vm->_script->setBuffer(i, (R_SCRIPT_DATABUF *)alloc_ptr);
alloc_ptr = calloc(R_SCRIPT_DATABUF_LEN, sizeof(SDataWord_T));
if (alloc_ptr == NULL) {
error("Couldn't allocate memory for script data buffer %d", i);
}
- _vm->_script->_data_buf[i]->len = R_SCRIPT_DATABUF_LEN;
- _vm->_script->_data_buf[i]->data = (SDataWord_T *)alloc_ptr;
+ _vm->_script->dataBuffer(i)->len = R_SCRIPT_DATABUF_LEN;
+ _vm->_script->dataBuffer(i)->data = (SDataWord_T *)alloc_ptr;
}
}
@@ -62,7 +62,7 @@ int SData::getWord(int n_buf, int n_word, SDataWord_T *data) {
return R_FAILURE;
}
- if ((n_word < 0) || (n_word >= _vm->_script->_data_buf[n_buf]->len)) {
+ if ((n_word < 0) || (n_word >= _vm->_script->dataBuffer(n_buf)->len)) {
return R_FAILURE;
}
@@ -70,7 +70,7 @@ int SData::getWord(int n_buf, int n_word, SDataWord_T *data) {
return R_FAILURE;
}
- *data = _vm->_script->_data_buf[n_buf]->data[n_word];
+ *data = _vm->_script->dataBuffer(n_buf)->data[n_word];
return R_SUCCESS;
}
@@ -80,11 +80,11 @@ int SData::putWord(int n_buf, int n_word, SDataWord_T data) {
return R_FAILURE;
}
- if ((n_word < 0) || (n_word >= _vm->_script->_data_buf[n_buf]->len)) {
+ if ((n_word < 0) || (n_word >= _vm->_script->dataBuffer(n_buf)->len)) {
return R_FAILURE;
}
- _vm->_script->_data_buf[n_buf]->data[n_word] = data;
+ _vm->_script->dataBuffer(n_buf)->data[n_word] = data;
return R_SUCCESS;
}
@@ -99,7 +99,7 @@ int SData::setBit(int n_buf, SDataWord_T n_bit, int bitstate) {
return R_FAILURE;
}
- if (n_bit >= (unsigned long)_vm->_script->_data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
+ if (n_bit >= (unsigned long)_vm->_script->dataBuffer(n_buf)->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
return R_FAILURE;
}
@@ -109,9 +109,9 @@ int SData::setBit(int n_buf, SDataWord_T n_bit, int bitstate) {
bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1));
if (bitstate) {
- _vm->_script->_data_buf[n_buf]->data[n_word] |= bit_pattern;
+ _vm->_script->dataBuffer(n_buf)->data[n_word] |= bit_pattern;
} else {
- _vm->_script->_data_buf[n_buf]->data[n_word] &= ~bit_pattern;
+ _vm->_script->dataBuffer(n_buf)->data[n_word] &= ~bit_pattern;
}
return R_SUCCESS;
@@ -127,7 +127,7 @@ int SData::getBit(int n_buf, SDataWord_T n_bit, int *bitstate) {
return R_FAILURE;
}
- if (n_bit >= (SDataWord_T) _vm->_script->_data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
+ if (n_bit >= (SDataWord_T) _vm->_script->dataBuffer(n_buf)->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
return R_FAILURE;
}
@@ -136,7 +136,7 @@ int SData::getBit(int n_buf, SDataWord_T n_bit, int *bitstate) {
bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1));
- *bitstate = (_vm->_script->_data_buf[n_buf]->data[n_word] & bit_pattern) ? 1 : 0;
+ *bitstate = (_vm->_script->dataBuffer(n_buf)->data[n_word] & bit_pattern) ? 1 : 0;
return R_SUCCESS;
}
diff --git a/saga/sdebug.cpp b/saga/sdebug.cpp
index 034375777c..a4fb10c2a6 100644
--- a/saga/sdebug.cpp
+++ b/saga/sdebug.cpp
@@ -67,10 +67,9 @@ int SDEBUG_PrintInstr(R_SCRIPT_THREAD *thread) {
tl_e.string = disp_buf;
tl_e.display = 1;
- // XXX
- MemoryReadStream readS(_vm->_script->_current_script->bytecode->bytecode_p
+ MemoryReadStream readS(_vm->_script->currentScript()->bytecode->bytecode_p
+ thread->i_offset,
- _vm->_script->_current_script->bytecode->bytecode_len
+ _vm->_script->currentScript()->bytecode->bytecode_len
- thread->i_offset);
in_char = readS.readByte();
sprintf(tmp_buf, "%04lX | %02X | ", thread->i_offset, in_char);
diff --git a/saga/sthread.cpp b/saga/sthread.cpp
index be43c57d38..47d696e1a1 100644
--- a/saga/sthread.cpp
+++ b/saga/sthread.cpp
@@ -47,7 +47,7 @@ R_SCRIPT_THREAD *STHREAD_Create() {
int result;
- if (!_vm->_script->_initialized) {
+ if (!_vm->_script->isInitialized()) {
return NULL;
}
@@ -62,7 +62,7 @@ R_SCRIPT_THREAD *STHREAD_Create() {
return NULL;
}
- new_node = ys_dll_add_head(_vm->_script->_thread_list, new_thread, sizeof *new_thread);
+ new_node = ys_dll_add_head(_vm->_script->threadList(), new_thread, sizeof *new_thread);
free(new_thread);
@@ -83,11 +83,11 @@ int STHREAD_ExecThreads(int msec) {
YS_DL_NODE *walk_p;
R_SCRIPT_THREAD *thread;
- if (!_vm->_script->_initialized) {
+ if (!_vm->_script->isInitialized()) {
return R_FAILURE;
}
- for (walk_p = ys_dll_head(_vm->_script->_thread_list); walk_p != NULL; walk_p = ys_dll_next(walk_p)) {
+ for (walk_p = ys_dll_head(_vm->_script->threadList()); walk_p != NULL; walk_p = ys_dll_next(walk_p)) {
thread = (R_SCRIPT_THREAD *)ys_dll_get_data(walk_p);
if (thread->executing) {
STHREAD_Run(thread, STHREAD_DEF_INSTR_COUNT, msec);
@@ -101,9 +101,9 @@ int STHREAD_SetEntrypoint(R_SCRIPT_THREAD *thread, int ep_num) {
R_SCRIPT_BYTECODE *bytecode;
int max_entrypoint;
- assert(_vm->_script->_initialized);
+ assert(_vm->_script->isInitialized());
- bytecode = _vm->_script->_current_script->bytecode;
+ bytecode = _vm->_script->currentScript()->bytecode;
max_entrypoint = bytecode->n_entrypoints;
if ((ep_num < 0) || (ep_num >= max_entrypoint)) {
@@ -117,9 +117,9 @@ int STHREAD_SetEntrypoint(R_SCRIPT_THREAD *thread, int ep_num) {
}
int STHREAD_Execute(R_SCRIPT_THREAD *thread, int ep_num) {
- assert(_vm->_script->_initialized);
+ assert(_vm->_script->isInitialized());
- if ((_vm->_script->_current_script == NULL) || (!_vm->_script->_current_script->loaded)) {
+ if ((_vm->_script->currentScript() == NULL) || (!_vm->_script->currentScript()->loaded)) {
return R_FAILURE;
}
@@ -132,15 +132,15 @@ int STHREAD_Execute(R_SCRIPT_THREAD *thread, int ep_num) {
}
unsigned char *GetReadPtr(R_SCRIPT_THREAD *thread) {
- return _vm->_script->_current_script->bytecode->bytecode_p + thread->i_offset;
+ return _vm->_script->currentScript()->bytecode->bytecode_p + thread->i_offset;
}
unsigned long GetReadOffset(const byte *read_p) {
- return (unsigned long)(read_p - (unsigned char *)_vm->_script->_current_script->bytecode->bytecode_p);
+ return (unsigned long)(read_p - (unsigned char *)_vm->_script->currentScript()->bytecode->bytecode_p);
}
size_t GetReadLen(R_SCRIPT_THREAD *thread) {
- return _vm->_script->_current_script->bytecode->bytecode_len - thread->i_offset;
+ return _vm->_script->currentScript()->bytecode->bytecode_len - thread->i_offset;
}
@@ -730,12 +730,12 @@ int STHREAD_Run(R_SCRIPT_THREAD *thread, int instr_limit, int msec) {
SSTACK_Pop(thread->stack, &data);
if (a_index < 0)
continue;
- if (!_vm->_script->_voice_lut_present) {
+ if (!_vm->_script->isVoiceLUTPresent()) {
voice_rn = -1;
} else {
- voice_rn = _vm->_script->_current_script->voice->voices[data];
+ voice_rn = _vm->_script->currentScript()->voice->voices[data];
}
- ACTOR_Speak(a_index, _vm->_script->_current_script->diag-> str[data], voice_rn, &thread->sem);
+ ACTOR_Speak(a_index, _vm->_script->currentScript()->diag-> str[data], voice_rn, &thread->sem);
}
}
break;