scummvm/sword2/logic.cpp

459 lines
12 KiB
C++
Raw Normal View History

2003-07-28 01:44:38 +00:00
/* Copyright (C) 1994-2003 Revolution Software Ltd
*
* 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$
*/
2003-07-28 03:12:49 +00:00
#include "stdafx.h"
#include "bs2/sword2.h"
#include "bs2/build_display.h"
#include "bs2/console.h"
#include "bs2/debug.h"
#include "bs2/interpreter.h"
#include "bs2/logic.h"
#include "bs2/router.h" // for clearWalkGridList()
#include "bs2/sound.h"
#include "bs2/sync.h"
2003-07-28 01:44:38 +00:00
2003-10-04 00:52:27 +00:00
namespace Sword2 {
Logic g_logic;
2003-07-28 01:44:38 +00:00
#define LEVEL (_curObjectHub->logic_level)
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// this must allow for the largest number of objects in a screen
#define OBJECT_KILL_LIST_SIZE 50
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
uint32 object_kill_list[OBJECT_KILL_LIST_SIZE];
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// keeps note of no. of objects in the kill list
uint32 kills = 0;
2003-07-28 01:44:38 +00:00
int Logic::processSession(void) {
2003-09-17 17:34:04 +00:00
// do one cycle of the current session
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
uint32 run_list;
uint32 ret, script;
uint32 *game_object_list;
char *raw_script_ad;
char *raw_data_ad;
uint32 null_pc;
_standardHeader *head;
_standardHeader *far_head;
uint32 id;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// might change during the session, so take a copy here
run_list = _currentRunList;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// point to first object in list
_pc = 0;
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) {
head = (_standardHeader*) res_man.open(run_list);
2003-09-17 17:34:04 +00:00
if (head->fileType != RUN_LIST)
2003-07-28 01:44:38 +00:00
Con_fatal_error("Logic_engine %d not a run_list", run_list);
2003-09-17 17:34:04 +00:00
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
ID = game_object_list[_pc++];
2003-09-17 17:34:04 +00:00
id = ID;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// release the list again so it can float in memory - at this
// point not one thing should be locked
2003-07-28 01:44:38 +00:00
res_man.close(run_list);
2003-09-17 17:34:04 +00:00
debug(5, "%d", ID);
2003-09-17 17:34:04 +00:00
// null terminated
if (!ID) {
// end the session naturally
return 0;
}
2003-07-28 01:44:38 +00:00
head = (_standardHeader*) res_man.open(ID);
2003-09-17 17:34:04 +00:00
if (head->fileType != GAME_OBJECT)
2003-07-28 01:44:38 +00:00
Con_fatal_error("Logic_engine %d not an object", ID);
_curObjectHub = (_object_hub *) (head + 1);
2003-07-28 01:44:38 +00:00
debug(5, " %d id(%d) pc(%d)",
LEVEL,
_curObjectHub->script_id[LEVEL],
_curObjectHub->script_pc[LEVEL]);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +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 {
// get the script id as we may be running a script
// from another object...
2003-07-28 01:44:38 +00:00
script = _curObjectHub->script_id[LEVEL];
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// there is a distinction between running one of our
// own scripts and that of another object
if (script / SIZE == ID) {
// its our script
2003-07-28 01:44:38 +00:00
debug(5, "run script %d pc%d",
script / SIZE,
_curObjectHub->script_pc[LEVEL]);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// this is the script data
// raw_script_ad = (char *) (_curObjectHub + 1);
2003-07-28 01:44:38 +00:00
raw_script_ad = (char*) head;
2003-09-17 17:34:04 +00:00
// script and data object are us/same
ret = runScript(raw_script_ad, raw_script_ad, &_curObjectHub->script_pc[LEVEL]);
2003-09-17 17:34:04 +00:00
} else {
// we're running the script of another game
// object - get our data object address
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// get the foreign objects script data address
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
raw_data_ad = (char*) head;
2003-07-28 01:44:38 +00:00
far_head = (_standardHeader*) res_man.open(script / SIZE);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
if (far_head->fileType != GAME_OBJECT && far_head->fileType != SCREEN_MANAGER)
Con_fatal_error("Logic_engine %d not a far object (its a %d)", script / SIZE, far_head->fileType);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// raw_script_ad = (char*) (head + 1) + sizeof(_standardHeader);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// get our objects data address
// raw_data_ad = (char*) (_curObjectHub + 1);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
raw_script_ad = (char*) far_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
2003-09-17 17:34:04 +00:00
// close foreign object again
res_man.close(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
// this script has finished - drop down a level
if (ret == 1) {
// check that it's not already on level 0 !
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 :-)
debug(5, "**WARNING object %d script 0 terminated!", id);
2003-09-17 17:34:04 +00:00
// reset to rerun
_curObjectHub->script_pc[LEVEL] = _curObjectHub->script_id[LEVEL] & 0xffff;
2003-09-17 17:34:04 +00:00
// cause us to drop out for a cycle
ret = 0;
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
} else if (ret > 2) {
Con_fatal_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
// any post logic system requests to go here
// clear any syncs that were waiting for this character - it
// has used them or now looses them
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
Clear_syncs(ID);
2003-07-28 01:44:38 +00:00
if (_pc != 0xffffffff) {
2003-09-17 17:34:04 +00:00
// the session is still valid so run the service script
null_pc = 0;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// call the base script - this is the graphic/mouse
// service call
2003-07-28 01:44:38 +00:00
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
// made for all live objects
// and that's it so close the object resource
2003-07-28 01:44:38 +00:00
res_man.close(ID);
2003-09-17 17:34:04 +00:00
}
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// leaving a room so remove all ids that must reboot correctly
processKillList();
2003-07-28 01:44:38 +00:00
debug(5, "RESTART the loop");
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// means restart the loop
return 1;
2003-07-28 01:44:38 +00:00
}
void Logic::expressChangeSession(uint32 sesh_id) {
2003-09-17 17:34:04 +00:00
// a game-object can bring an immediate halt to the session and cause
// a new one to start without a screen update
//set to new
_currentRunList = sesh_id;
2003-09-17 17:34:04 +00:00
//causes session to quit
_pc = 0xffffffff;
2003-09-17 17:34:04 +00:00
// reset now in case we double-clicked an exit prior to changing screen
EXIT_FADING = 0;
// 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
Init_sync_system();
2003-07-28 01:44:38 +00:00
// reset walkgrid list (see fnRegisterWalkGrid)
router.clearWalkGridList();
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// stops all fx & clears the queue
Clear_fx_queue();
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// free all the route memory blocks from previous game
router.freeAllRouteMem();
2003-07-28 01:44:38 +00:00
}
void Logic::naturalChangeSession(uint32 sesh_id) {
// FIXME: This function doesn't seem to be used anywhere
2003-09-17 17:34:04 +00:00
// A new session will begin next game cycle. The current cycle will
// conclude and build the screen and flip into view as normal
2003-07-28 01:44:38 +00:00
_currentRunList = sesh_id;
}
2003-09-17 17:34:04 +00:00
uint32 Logic::getRunList(void) {
// pass back the private _currentRunList variable
return _currentRunList;
2003-07-28 01:44:38 +00:00
}
int32 Logic::fnSetSession(int32 *params) {
2003-09-17 17:34:04 +00:00
// used by player invoked start scripts
2003-07-28 01:44:38 +00:00
// 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
}
int32 Logic::fnEndSession(int32 *params) {
2003-09-17 17:34:04 +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!!
// 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
}
void Logic::logicUp(uint32 new_script) {
2003-09-17 17:34:04 +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
2003-09-17 17:34:04 +00:00
// going up a level - and we'll keeping going this cycle
LEVEL++;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// can be 0, 1, 2
if (LEVEL == 3)
Con_fatal_error("logicUp id %d has run off script tree! :-O", ID);
2003-07-28 01:44:38 +00:00
// setup new script on next level (not the current level)
2003-07-28 01:44:38 +00:00
debug(5, "new pc = %d", new_script & 0xffff);
2003-07-28 01:44:38 +00:00
_curObjectHub->script_id[LEVEL] = new_script;
_curObjectHub->script_pc[LEVEL] = new_script & 0xffff;
2003-07-28 01:44:38 +00:00
}
void Logic::logicOne(uint32 new_script) {
2003-09-17 17:34:04 +00:00
// force to level one
2003-07-28 01:44:38 +00:00
LEVEL = 1;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// setup new script on level 1
_curObjectHub->script_id[1] = new_script;
_curObjectHub->script_pc[1] = new_script & 0xffff;
2003-07-28 01:44:38 +00:00
}
void Logic::logicReplace(uint32 new_script) {
2003-09-17 17:34:04 +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
2003-09-17 17:34:04 +00:00
// setup new script on this level
_curObjectHub->script_id[LEVEL] = new_script;
_curObjectHub->script_pc[LEVEL] = new_script & 0xffff;
2003-07-28 01:44:38 +00:00
}
uint32 Logic::examineRunList(void) {
2003-09-17 17:34:04 +00:00
uint32 *game_object_list;
_standardHeader *file_header;
int scrolls = 0;
_keyboardEvent ke;
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 *) (res_man.open(_currentRunList) + sizeof(_standardHeader));
2003-07-28 01:44:38 +00:00
Print_to_console("runlist number %d", _currentRunList);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
while(*(game_object_list)) {
file_header = (_standardHeader*) res_man.open(*(game_object_list));
2003-09-17 17:34:04 +00:00
Print_to_console(" %d %s", *(game_object_list), file_header->name);
res_man.close(*(game_object_list++));
2003-07-28 01:44:38 +00:00
scrolls++;
Build_display();
2003-09-17 17:34:04 +00:00
if (scrolls == 18) {
2003-07-28 01:44:38 +00:00
Temp_print_to_console("- Press ESC to stop or any other key to continue");
Build_display();
2003-09-17 17:34:04 +00:00
do {
g_display->updateDisplay();
} while (!KeyWaiting());
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// kill the key we just pressed
ReadKey(&ke);
if (ke.keycode == 27)
2003-07-28 01:44:38 +00:00
break;
2003-09-17 17:34:04 +00:00
// clear the Press Esc message ready for the
// new line
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
Clear_console_line();
scrolls = 0;
}
2003-07-28 01:44:38 +00:00
}
res_man.close(_currentRunList);
2003-09-17 17:34:04 +00:00
} else
Print_to_console("no run list set");
2003-07-28 01:44:38 +00:00
Scroll_console();
2003-09-17 17:34:04 +00:00
return 1;
2003-07-28 01:44:38 +00:00
}
void Logic::totalRestart(void) {
2003-09-17 17:34:04 +00:00
// reset the object restart script 1 on level 0
2003-07-28 01:44:38 +00:00
LEVEL = 0;
// _curObjectHub->script_id[0] = 1;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// reset to rerun
_curObjectHub->script_pc[0] = 1;
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
totalRestart();
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// drop out without saving pc and go around again
return IR_TERMINATE;
2003-07-28 01:44:38 +00:00
}
2003-09-17 17:34:04 +00:00
int32 Logic::fnAddToKillList(int32 *params) {
2003-07-28 01:44:38 +00:00
// call *once* from object's logic script - ie. in startup code
// - so not re-called every time script drops off & restarts!
2003-09-17 17:34:04 +00:00
// Mark this object for killing - to be killed when player leaves
// this screen. Object reloads & 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"
2003-07-28 01:44:38 +00:00
// params: none
2003-07-28 01:44:38 +00:00
uint32 entry;
2003-09-17 17:34:04 +00:00
// DON'T EVER KILL GEORGE!
if (ID != 8) {
// first, scan list to see if this object is already included
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
entry = 0;
while (entry < kills && object_kill_list[entry] != ID)
2003-07-28 01:44:38 +00:00
entry++;
2003-09-17 17:34:04 +00:00
// if this ID isn't already in the list, then add it,
// (otherwise finish)
2003-09-17 17:34:04 +00:00
if (entry == kills) {
#ifdef _SWORD2_DEBUG
// no room at the inn
if (kills == OBJECT_KILL_LIST_SIZE)
Con_fatal_error("List full in fnAddToKillList(%u)", ID);
2003-09-17 17:34:04 +00:00
#endif
// add this 'ID' to the kill list
object_kill_list[kills] = ID;
kills++;
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
// "another one bites the dust"
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +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 &
// more importantly closed before killing!
2003-07-28 01:44:38 +00:00
}
}
2003-09-17 17:34:04 +00:00
// continue script
return IR_CONT;
2003-07-28 01:44:38 +00:00
}
void Logic::processKillList(void) {
2003-09-17 17:34:04 +00:00
for (uint32 j = 0; j < kills; j++)
res_man.remove(object_kill_list[j]);
2003-07-28 01:44:38 +00:00
2003-09-17 17:34:04 +00:00
kills = 0;
2003-07-28 01:44:38 +00:00
}
void Logic::resetKillList(void) {
2003-09-17 17:34:04 +00:00
kills = 0;
}
2003-10-04 00:52:27 +00:00
} // End of namespace Sword2