scummvm/sword2/logic.cpp

351 lines
9.1 KiB
C++
Raw Normal View History

/* Copyright (C) 1994-2004 Revolution Software Ltd
2003-07-28 01:44:38 +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$
*/
#include "common/stdafx.h"
2003-10-28 19:51:30 +00:00
#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/console.h"
2003-10-28 19:51:30 +00:00
#include "sword2/interpreter.h"
#include "sword2/logic.h"
#include "sword2/resman.h"
2003-07-28 01:44:38 +00:00
#define LEVEL (_curObjectHub->logic_level)
2003-07-28 01:44:38 +00:00
#define Debug_Printf _vm->_debugger->DebugPrintf
namespace Sword2 {
/**
* Do one cycle of the current session.
*/
2003-07-28 01:44:38 +00:00
int Logic::processSession(void) {
2003-09-17 17:34:04 +00:00
// might change during the session, so take a copy here
2004-04-07 14:18:27 +00:00
uint32 run_list = _currentRunList;
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
_pc = 0; // first object in list
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// by minusing the pc we can cause an immediate cessation of logic
// processing on the current list
2003-07-28 01:44:38 +00:00
while (_pc != 0xffffffff) {
2004-04-07 14:18:27 +00:00
uint32 ret, script;
char *raw_script_ad;
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
StandardHeader *head = (StandardHeader *) _vm->_resman->openResource(run_list);
assert(head->fileType == RUN_LIST);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
uint32 *game_object_list = (uint32 *) (head + 1);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// read the next id
2004-04-07 14:18:27 +00:00
uint id = game_object_list[_pc++];
_scriptVars[ID] = id;
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// Release the list again so it can float in memory - at this
2003-09-17 17:34:04 +00:00
// point not one thing should be locked
2003-07-28 01:44:38 +00:00
_vm->_resman->closeResource(run_list);
2003-09-17 17:34:04 +00:00
if (!_scriptVars[ID]) {
2004-04-07 14:18:27 +00:00
// End of list - end the session naturally
2003-09-17 17:34:04 +00:00
return 0;
}
2003-07-28 01:44:38 +00:00
head = (StandardHeader *) _vm->_resman->openResource(_scriptVars[ID]);
2004-04-07 14:18:27 +00:00
assert(head->fileType == GAME_OBJECT);
2003-07-28 01:44:38 +00:00
_curObjectHub = (ObjectHub *) (head + 1);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
debug(5, "Level %d id(%d) pc(%d)",
LEVEL,
_curObjectHub->script_id[LEVEL],
_curObjectHub->script_pc[LEVEL]);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// Do the logic for this object. We keep going until a function
// says to stop - remember, system operations are run via
// function calls to drivers now.
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
do {
2004-04-07 14:18:27 +00:00
// There is a distinction between running one of our
// own scripts and that of another object.
2003-07-28 01:44:38 +00:00
script = _curObjectHub->script_id[LEVEL];
2003-07-28 01:44:38 +00:00
if (script / SIZE == _scriptVars[ID]) {
2004-04-07 14:18:27 +00:00
// It's our own script
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
debug(5, "Run script %d pc=%d",
script / SIZE,
_curObjectHub->script_pc[LEVEL]);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// This is the script data. Script and data
// object are the same.
2003-07-28 01:44:38 +00:00
raw_script_ad = (char *) head;
2003-07-28 01:44:38 +00:00
ret = runScript(raw_script_ad, raw_script_ad, &_curObjectHub->script_pc[LEVEL]);
2003-09-17 17:34:04 +00:00
} else {
2004-04-07 14:18:27 +00:00
// We're running the script of another game
// object - get our data object address.
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
StandardHeader *far_head = (StandardHeader *) _vm->_resman->openResource(script / SIZE);
assert(far_head->fileType == GAME_OBJECT || far_head->fileType == SCREEN_MANAGER);
2003-07-28 01:44:38 +00:00
raw_script_ad = (char *) far_head;
2004-04-07 14:18:27 +00:00
char *raw_data_ad = (char *) head;
2003-07-28 01:44:38 +00:00
ret = runScript(raw_script_ad, raw_data_ad, &_curObjectHub->script_pc[LEVEL]);
2003-07-28 01:44:38 +00:00
_vm->_resman->closeResource(script / SIZE);
2003-09-17 17:34:04 +00:00
// reset to us for service script
raw_script_ad = raw_data_ad;
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
if (ret == 1) {
2004-04-07 14:18:27 +00:00
// The script finished - drop down a level
if (LEVEL)
LEVEL--;
2003-09-17 17:34:04 +00:00
else {
// Hmmm, level 0 terminated :-| Let's
// be different this time and simply
// let it restart next go :-)
2004-04-07 14:18:27 +00:00
// Note that this really does happen a
// lot, so don't make it a warning.
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
debug(5, "object %d script 0 terminated!", id);
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
// reset to rerun, drop out for a cycle
_curObjectHub->script_pc[LEVEL] = _curObjectHub->script_id[LEVEL] & 0xffff;
2003-09-17 17:34:04 +00:00
ret = 0;
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
} else if (ret > 2) {
error("processSession: illegal script return type %d", ret);
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
// if ret == 2 then we simply go around again - a new
// script or subroutine will kick in and run
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// keep processing scripts until 0 for quit is returned
2003-09-20 12:24:53 +00:00
} while (ret);
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
// Any post logic system requests to go here
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
// Clear any syncs that were waiting for this character - it
2003-09-17 17:34:04 +00:00
// has used them or now looses them
2003-07-28 01:44:38 +00:00
clearSyncs(_scriptVars[ID]);
2003-07-28 01:44:38 +00:00
if (_pc != 0xffffffff) {
2004-04-07 14:18:27 +00:00
// The session is still valid so run the graphics/mouse
// service script
uint32 null_pc = 0;
runScript(raw_script_ad, raw_script_ad, &null_pc);
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
// and that's it so close the object resource
2003-07-28 01:44:38 +00:00
_vm->_resman->closeResource(_scriptVars[ID]);
2003-09-17 17:34:04 +00:00
}
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// Leaving a room so remove all ids that must reboot correctly. Then
// restart the loop.
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
for (uint32 i = 0; i < _kills; i++)
_vm->_resman->remove(_objectKillList[i]);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
resetKillList();
2003-09-17 17:34:04 +00:00
return 1;
2003-07-28 01:44:38 +00:00
}
/**
* Bring an immediate halt to the session and cause a new one to start without
* a screen update.
*/
2003-09-17 17:34:04 +00:00
void Logic::expressChangeSession(uint32 sesh_id) {
2004-04-07 14:18:27 +00:00
// Set new session and force the old one to quit.
_currentRunList = sesh_id;
_pc = 0xffffffff;
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
// Reset now in case we double-clicked an exit prior to changing screen
_scriptVars[EXIT_FADING] = 0;
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
// We're trashing the list - presumably to change room. In theory,
// sync waiting in the list could be left behind and never removed -
// so we trash the lot
memset(_syncList, 0, sizeof(_syncList));
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// Various clean-ups
_router->clearWalkGridList();
_vm->clearFxQueue();
_router->freeAllRouteMem();
2003-07-28 01:44:38 +00:00
}
/**
* @return The private _currentRunList variable.
*/
uint32 Logic::getRunList(void) {
return _currentRunList;
2003-07-28 01:44:38 +00:00
}
2004-04-07 14:18:27 +00:00
/**
* This function is by start scripts.
*/
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
int32 Logic::fnSetSession(int32 *params) {
// params: 0 id of new run list
expressChangeSession(params[0]);
2003-09-17 17:34:04 +00:00
return IR_CONT;
2003-07-28 01:44:38 +00:00
}
/**
* Causes no more objects in this logic loop to be processed. The logic engine
* will restart at the beginning of the new list. The current screen will not
* be drawn!
*/
2003-09-17 17:34:04 +00:00
int32 Logic::fnEndSession(int32 *params) {
// params: 0 id of new run-list
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// terminate current and change to next run-list
expressChangeSession(params[0]);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// stop the script - logic engine will now go around and the new
// screen will begin
return IR_STOP;
2003-07-28 01:44:38 +00:00
}
/**
* Move the current object up a level. Called by fnGosub command. Remember:
* only the logic object has access to _curObjectHub.
*/
2003-07-28 01:44:38 +00:00
void Logic::logicUp(uint32 new_script) {
debug(5, "new pc = %d", new_script & 0xffff);
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// going up a level - and we'll keep going this cycle
LEVEL++;
assert(LEVEL < 3); // Can be 0, 1, 2
logicReplace(new_script);
2003-07-28 01:44:38 +00:00
}
/**
* Force the level to one.
*/
2003-07-28 01:44:38 +00:00
void Logic::logicOne(uint32 new_script) {
LEVEL = 1;
2004-04-07 14:18:27 +00:00
logicReplace(new_script);
2003-07-28 01:44:38 +00:00
}
/**
* Change current logic. Script must quit with a TERMINATE directive, which
* does not write to &pc
*/
2003-07-28 01:44:38 +00:00
void Logic::logicReplace(uint32 new_script) {
_curObjectHub->script_id[LEVEL] = new_script;
_curObjectHub->script_pc[LEVEL] = new_script & 0xffff;
2003-07-28 01:44:38 +00:00
}
void Logic::examineRunList(void) {
2003-09-17 17:34:04 +00:00
uint32 *game_object_list;
StandardHeader *file_header;
2003-07-28 01:44:38 +00:00
if (_currentRunList) {
2003-09-17 17:34:04 +00:00
// open and lock in place
game_object_list = (uint32 *) (_vm->_resman->openResource(_currentRunList) + sizeof(StandardHeader));
2003-07-28 01:44:38 +00:00
Debug_Printf("Runlist number %d\n", _currentRunList);
2003-07-28 01:44:38 +00:00
for (int i = 0; game_object_list[i]; i++) {
file_header = (StandardHeader *) _vm->_resman->openResource(game_object_list[i]);
Debug_Printf("%d %s\n", game_object_list[i], file_header->name);
_vm->_resman->closeResource(game_object_list[i]);
2003-07-28 01:44:38 +00:00
}
_vm->_resman->closeResource(_currentRunList);
2003-09-17 17:34:04 +00:00
} else
Debug_Printf("No run list set\n");
2003-07-28 01:44:38 +00:00
}
/**
* Reset the object and restart script 1 on level 0
*/
2003-07-28 01:44:38 +00:00
int32 Logic::fnTotalRestart(int32 *params) {
2003-09-17 17:34:04 +00:00
// mega runs this to restart its base logic again - like being cached
// in again
2003-07-28 01:44:38 +00:00
// params: none
2004-04-07 14:18:27 +00:00
LEVEL = 0;
_curObjectHub->script_pc[0] = 1;
2003-09-17 17:34:04 +00:00
return IR_TERMINATE;
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
/**
* Mark this object for killing - to be killed when player leaves this screen.
* Object reloads and script restarts upon re-entry to screen, which causes
* this object's startup logic to be re-run every time we enter the screen.
* "Which is nice."
*
* @note Call ONCE from object's logic script, i.e. in startup code, so not
* re-called every time script frops off and restarts!
*/
2003-07-28 01:44:38 +00:00
int32 Logic::fnAddToKillList(int32 *params) {
// params: none
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// DON'T EVER KILL GEORGE!
2004-04-07 14:18:27 +00:00
if (_scriptVars[ID] == 8)
return IR_CONT;
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// Scan the list to see if it's already included
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
for (uint32 i = 0; i < _kills; i++) {
if (_objectKillList[i] == _scriptVars[ID])
return IR_CONT;
}
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
assert(_kills < OBJECT_KILL_LIST_SIZE); // no room at the inn
2003-09-17 17:34:04 +00:00
2004-04-07 14:18:27 +00:00
_objectKillList[_kills++] = _scriptVars[ID];
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// "another one bites the dust"
2003-07-28 01:44:38 +00:00
2004-04-07 14:18:27 +00:00
// When we leave the screen, all these object resources are to be
// cleaned out of memory and the kill list emptied by doing
// '_kills = 0', ensuring that all resources are in fact still in
// memory and, more importantly, closed before killing!
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
return IR_CONT;
2003-07-28 01:44:38 +00:00
}
void Logic::resetKillList(void) {
_kills = 0;
2003-09-17 17:34:04 +00:00
}
2003-10-04 00:52:27 +00:00
} // End of namespace Sword2