diff options
Diffstat (limited to 'engines/zvision/scripting')
18 files changed, 3436 insertions, 0 deletions
diff --git a/engines/zvision/scripting/actions.cpp b/engines/zvision/scripting/actions.cpp new file mode 100644 index 0000000000..878fa752d5 --- /dev/null +++ b/engines/zvision/scripting/actions.cpp @@ -0,0 +1,401 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/actions.h" + +#include "zvision/zvision.h" +#include "zvision/scripting/script_manager.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/sound/zork_raw.h" +#include "zvision/video/zork_avi_decoder.h" +#include "zvision/scripting/controls/timer_node.h" +#include "zvision/scripting/controls/animation_control.h" + +#include "common/file.h" + +#include "audio/decoders/wave.h" + + +namespace ZVision { + +////////////////////////////////////////////////////////////////////////////// +// ActionAdd +////////////////////////////////////////////////////////////////////////////// + +ActionAdd::ActionAdd(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%u,%u)", &_key, &_value); +} + +bool ActionAdd::execute(ZVision *engine) { + engine->getScriptManager()->addToStateValue(_key, _value); + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionAssign +////////////////////////////////////////////////////////////////////////////// + +ActionAssign::ActionAssign(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%u, %u)", &_key, &_value); +} + +bool ActionAssign::execute(ZVision *engine) { + engine->getScriptManager()->setStateValue(_key, _value); + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionAttenuate +////////////////////////////////////////////////////////////////////////////// + +ActionAttenuate::ActionAttenuate(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%u, %d)", &_key, &_attenuation); +} + +bool ActionAttenuate::execute(ZVision *engine) { + // TODO: Implement + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionChangeLocation +////////////////////////////////////////////////////////////////////////////// + +ActionChangeLocation::ActionChangeLocation(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%c,%c,%c%c,%u)", &_world, &_room, &_node, &_view, &_offset); +} + +bool ActionChangeLocation::execute(ZVision *engine) { + // We can't directly call ScriptManager::ChangeLocationIntern() because doing so clears all the Puzzles, and thus would corrupt the current puzzle checking + engine->getScriptManager()->changeLocation(_world, _room, _node, _view, _offset); + // Tell the puzzle system to stop checking any more puzzles + return false; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionCrossfade +////////////////////////////////////////////////////////////////////////////// + +ActionCrossfade::ActionCrossfade(const Common::String &line) { + sscanf(line.c_str(), + "%*[^(](%u %u %u %u %u %u %u)", + &_keyOne, &_keyTwo, &_oneStartVolume, &_twoStartVolume, &_oneEndVolume, &_twoEndVolume, &_timeInMillis); +} + +bool ActionCrossfade::execute(ZVision *engine) { + // TODO: Implement + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionDisableControl +////////////////////////////////////////////////////////////////////////////// + +ActionDisableControl::ActionDisableControl(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%u)", &_key); +} + +bool ActionDisableControl::execute(ZVision *engine) { + debug("Disabling control %u", _key); + + ScriptManager *scriptManager = engine->getScriptManager(); + scriptManager->setStateFlags(_key, scriptManager->getStateFlags(_key) | ScriptManager::DISABLED); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionEnableControl +////////////////////////////////////////////////////////////////////////////// + +ActionEnableControl::ActionEnableControl(const Common::String &line) { + sscanf(line.c_str(), "%*[^(](%u)", &_key); +} + +bool ActionEnableControl::execute(ZVision *engine) { + debug("Enabling control %u", _key); + + ScriptManager *scriptManager = engine->getScriptManager(); + scriptManager->setStateFlags(_key, scriptManager->getStateFlags(_key) & ~ScriptManager::DISABLED); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionMusic +////////////////////////////////////////////////////////////////////////////// + +ActionMusic::ActionMusic(const Common::String &line) : _volume(255) { + uint type; + char fileNameBuffer[25]; + uint loop; + uint volume = 255; + + sscanf(line.c_str(), "%*[^:]:%*[^:]:%u(%u %25s %u %u)", &_key, &type, fileNameBuffer, &loop, &volume); + + // type 4 are midi sound effect files + if (type == 4) { + _soundType = Audio::Mixer::kSFXSoundType; + _fileName = Common::String::format("midi/%s/%u.wav", fileNameBuffer, loop); + _loop = false; + } else { + // TODO: See what the other types are so we can specify the correct Mixer::SoundType. In the meantime use kPlainSoundType + _soundType = Audio::Mixer::kPlainSoundType; + _fileName = Common::String(fileNameBuffer); + _loop = loop == 1 ? true : false; + } + + // Volume is optional. If it doesn't appear, assume full volume + if (volume != 255) { + // Volume in the script files is mapped to [0, 100], but the ScummVM mixer uses [0, 255] + _volume = volume * 255 / 100; + } +} + +bool ActionMusic::execute(ZVision *engine) { + Audio::RewindableAudioStream *audioStream; + + if (_fileName.contains(".wav")) { + Common::File *file = new Common::File(); + if (file->open(_fileName)) { + audioStream = Audio::makeWAVStream(file, DisposeAfterUse::YES); + } else { + warning("Unable to open %s", _fileName.c_str()); + return false; + } + } else { + audioStream = makeRawZorkStream(_fileName, engine); + } + + if (_loop) { + Audio::LoopingAudioStream *loopingAudioStream = new Audio::LoopingAudioStream(audioStream, 0, DisposeAfterUse::YES); + engine->_mixer->playStream(_soundType, 0, loopingAudioStream, -1, _volume); + } else { + engine->_mixer->playStream(_soundType, 0, audioStream, -1, _volume); + } + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionPreloadAnimation +////////////////////////////////////////////////////////////////////////////// + +ActionPreloadAnimation::ActionPreloadAnimation(const Common::String &line) { + char fileName[25]; + + // The two %*u are always 0 and dont seem to have a use + sscanf(line.c_str(), "%*[^:]:%*[^:]:%u(%25s %*u %*u %u %u)", &_key, fileName, &_mask, &_framerate); + + _fileName = Common::String(fileName); +} + +bool ActionPreloadAnimation::execute(ZVision *engine) { + // TODO: We ignore the mask and framerate atm. Mask refers to a key color used for binary alpha. We assume the framerate is the default framerate embedded in the videos + + // TODO: Check if the Control already exists + + // Create the control, but disable it until PlayPreload is called + ScriptManager *scriptManager = engine->getScriptManager(); + scriptManager->addControl(new AnimationControl(engine, _key, _fileName)); + scriptManager->setStateFlags(_key, scriptManager->getStateFlags(_key) | ScriptManager::DISABLED); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionPlayAnimation +////////////////////////////////////////////////////////////////////////////// + +ActionPlayAnimation::ActionPlayAnimation(const Common::String &line) { + char fileName[25]; + + // The two %*u are always 0 and dont seem to have a use + sscanf(line.c_str(), + "%*[^:]:%*[^:]:%u(%25s %u %u %u %u %u %u %u %*u %*u %u %u)", + &_key, fileName, &_x, &_y, &_width, &_height, &_start, &_end, &_loopCount, &_mask, &_framerate); + + _fileName = Common::String(fileName); +} + +bool ActionPlayAnimation::execute(ZVision *engine) { + // TODO: Implement + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionPlayPreloadAnimation +////////////////////////////////////////////////////////////////////////////// + +ActionPlayPreloadAnimation::ActionPlayPreloadAnimation(const Common::String &line) { + sscanf(line.c_str(), + "%*[^:]:%*[^:]:%u(%u %u %u %u %u %u %u %u)", + &_animationKey, &_controlKey, &_x1, &_y1, &_x2, &_y2, &_startFrame, &_endFrame, &_loopCount); +} + +bool ActionPlayPreloadAnimation::execute(ZVision *engine) { + // Find the control + AnimationControl *control = (AnimationControl *)engine->getScriptManager()->getControl(_controlKey); + + // Set the needed values within the control + control->setAnimationKey(_animationKey); + control->setLoopCount(_loopCount); + control->setXPos(_x1); + control->setYPost(_y1); + + // Enable the control. ScriptManager will take care of the rest + control->enable(); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionQuit +////////////////////////////////////////////////////////////////////////////// + +bool ActionQuit::execute(ZVision *engine) { + engine->quitGame(); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionRandom +////////////////////////////////////////////////////////////////////////////// + +ActionRandom::ActionRandom(const Common::String &line) { + sscanf(line.c_str(), "%*[^:]:%*[^:]:%u, %u)", &_key, &_max); +} + +bool ActionRandom::execute(ZVision *engine) { + uint randNumber = engine->getRandomSource()->getRandomNumber(_max); + engine->getScriptManager()->setStateValue(_key, randNumber); + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionSetPartialScreen +////////////////////////////////////////////////////////////////////////////// + +ActionSetPartialScreen::ActionSetPartialScreen(const Common::String &line) { + char fileName[25]; + uint color; + + sscanf(line.c_str(), "%*[^(](%u %u %25s %*u %u)", &_x, &_y, fileName, &color); + + _fileName = Common::String(fileName); + + if (color > 0xFFFF) { + warning("Background color for ActionSetPartialScreen is bigger than a uint16"); + } + _backgroundColor = color; +} + +bool ActionSetPartialScreen::execute(ZVision *engine) { + RenderManager *renderManager = engine->getRenderManager(); + + if (_backgroundColor > 0) { + renderManager->clearWorkingWindowTo555Color(_backgroundColor); + } + renderManager->renderImageToScreen(_fileName, _x, _y); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionSetScreen +////////////////////////////////////////////////////////////////////////////// + +ActionSetScreen::ActionSetScreen(const Common::String &line) { + char fileName[25]; + sscanf(line.c_str(), "%*[^(](%25[^)])", fileName); + + _fileName = Common::String(fileName); +} + +bool ActionSetScreen::execute(ZVision *engine) { + engine->getRenderManager()->setBackgroundImage(_fileName); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionStreamVideo +////////////////////////////////////////////////////////////////////////////// + +ActionStreamVideo::ActionStreamVideo(const Common::String &line) { + char fileName[25]; + uint skippable; + + sscanf(line.c_str(), "%*[^(](%25s %u %u %u %u %u %u)", fileName, &_x1, &_y1, &_x2, &_y2, &_flags, &skippable); + + _fileName = Common::String(fileName); + _skippable = (skippable == 0) ? false : true; +} + +bool ActionStreamVideo::execute(ZVision *engine) { + ZorkAVIDecoder decoder; + if (!decoder.loadFile(_fileName)) { + return true; + } + + Common::Rect destRect; + if ((_flags & DIFFERENT_DIMENSIONS) == DIFFERENT_DIMENSIONS) { + destRect = Common::Rect(_x1, _y1, _x2, _y2); + } + + engine->playVideo(decoder, destRect, _skippable); + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// ActionTimer +////////////////////////////////////////////////////////////////////////////// + +ActionTimer::ActionTimer(const Common::String &line) { + sscanf(line.c_str(), "%*[^:]:%*[^:]:%u(%u)", &_key, &_time); +} + +bool ActionTimer::execute(ZVision *engine) { + engine->getScriptManager()->addControl(new TimerNode(engine, _key, _time)); + return true; +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/actions.h b/engines/zvision/scripting/actions.h new file mode 100644 index 0000000000..afa3e3a2ac --- /dev/null +++ b/engines/zvision/scripting/actions.h @@ -0,0 +1,346 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_ACTIONS_H +#define ZVISION_ACTIONS_H + +#include "common/str.h" + +#include "audio/mixer.h" + + +namespace ZVision { + +// Forward declaration of ZVision. This file is included before ZVision is declared +class ZVision; + +/** + * The base class that represents any action that a Puzzle can take. + * This class is purely virtual. + */ +class ResultAction { +public: + virtual ~ResultAction() {} + /** + * This is called by the script system whenever a Puzzle's criteria are found to be true. + * It should execute any necessary actions and return a value indicating whether the script + * system should continue to test puzzles. In 99% of cases this will be 'true'. + * + * @param engine A pointer to the base engine so the ResultAction can access all the necessary methods + * @return Should the script system continue to test any remaining puzzles (true) or immediately break and go on to the next frame (false) + */ + virtual bool execute(ZVision *engine) = 0; +}; + + +// The different types of actions +// DEBUG, +// DISABLE_CONTROL, +// DISABLE_VENUS, +// DISPLAY_MESSAGE, +// DISSOLVE, +// DISTORT, +// ENABLE_CONTROL, +// FLUSH_MOUSE_EVENTS, +// INVENTORY, +// KILL, +// MENU_BAR_ENABLE, +// MUSIC, +// PAN_TRACK, +// PLAY_PRELOAD, +// PREFERENCES, +// QUIT, +// RANDOM, +// REGION, +// RESTORE_GAME, +// ROTATE_TO, +// SAVE_GAME, +// SET_PARTIAL_SCREEN, +// SET_SCREEN, +// SET_VENUS, +// STOP, +// STREAM_VIDEO, +// SYNC_SOUND, +// TTY_TEXT, +// UNIVERSE_MUSIC, + +class ActionAdd : public ResultAction { +public: + ActionAdd(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + uint _value; +}; + +class ActionAssign : public ResultAction { +public: + ActionAssign(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + uint _value; +}; + +class ActionAttenuate : public ResultAction { +public: + ActionAttenuate(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + int _attenuation; +}; + +class ActionChangeLocation : public ResultAction { +public: + ActionChangeLocation(const Common::String &line); + bool execute(ZVision *engine); + +private: + char _world; + char _room; + char _node; + char _view; + uint32 _offset; +}; + +class ActionCrossfade : public ResultAction { +public: + ActionCrossfade(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _keyOne; + uint32 _keyTwo; + uint _oneStartVolume; + uint _twoStartVolume; + uint _oneEndVolume; + uint _twoEndVolume; + uint _timeInMillis; +}; + +class ActionDebug : public ResultAction { +public: + ActionDebug(const Common::String &line); + bool execute(ZVision *engine); + +private: +}; + +class ActionDelayRender : public ResultAction { +public: + ActionDelayRender(const Common::String &line); + bool execute(ZVision *engine); + +private: + // TODO: Check if this should actually be frames or if it should be milliseconds/seconds + uint32 framesToDelay; +}; + +class ActionDisableControl : public ResultAction { +public: + ActionDisableControl(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; +}; + +class ActionDisableVenus : public ResultAction { +public: + ActionDisableVenus(const Common::String &line); + bool execute(ZVision *engine); + +private: +}; + +class ActionDisplayMessage : public ResultAction { +public: + ActionDisplayMessage(const Common::String &line); + bool execute(ZVision *engine); + +private: +}; + +class ActionDissolve : public ResultAction { +public: + ActionDissolve(); + bool execute(ZVision *engine); +}; + +class ActionDistort : public ResultAction { +public: + ActionDistort(const Common::String &line); + bool execute(ZVision *engine); + +private: +}; + +class ActionEnableControl : public ResultAction { +public: + ActionEnableControl(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; +}; + +class ActionMusic : public ResultAction { +public: + ActionMusic(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + Audio::Mixer::SoundType _soundType; + Common::String _fileName; + bool _loop; + byte _volume; +}; + +class ActionPlayAnimation : public ResultAction { +public: + ActionPlayAnimation(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + Common::String _fileName; + uint32 _x; + uint32 _y; + uint32 _width; + uint32 _height; + uint32 _start; + uint32 _end; + uint _mask; + uint _framerate; + uint _loopCount; +}; + +class ActionPlayPreloadAnimation : public ResultAction { +public: + ActionPlayPreloadAnimation(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _animationKey; + uint32 _controlKey; + uint32 _x1; + uint32 _y1; + uint32 _x2; + uint32 _y2; + uint _startFrame; + uint _endFrame; + uint _loopCount; +}; + +class ActionPreloadAnimation : public ResultAction { +public: + ActionPreloadAnimation(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + Common::String _fileName; + uint _mask; + uint _framerate; +}; + +class ActionQuit : public ResultAction { +public: + ActionQuit() {} + bool execute(ZVision *engine); +}; + +// TODO: See if this exists in ZGI. It doesn't in ZNem +class ActionUnloadAnimation : public ResultAction { +public: + ActionUnloadAnimation(const Common::String &line); + bool execute(ZVision *engine); +}; + +class ActionRandom : public ResultAction { +public: + ActionRandom(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + uint _max; +}; + +class ActionSetPartialScreen : public ResultAction { +public: + ActionSetPartialScreen(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint _x; + uint _y; + Common::String _fileName; + uint16 _backgroundColor; +}; + +class ActionSetScreen : public ResultAction { +public: + ActionSetScreen(const Common::String &line); + bool execute(ZVision *engine); + +private: + Common::String _fileName; +}; + +class ActionStreamVideo : public ResultAction { +public: + ActionStreamVideo(const Common::String &line); + bool execute(ZVision *engine); + +private: + enum { + DIFFERENT_DIMENSIONS = 0x1 // 0x1 flags that the destRect dimensions are different from the original video dimensions + }; + + Common::String _fileName; + uint _x1; + uint _y1; + uint _x2; + uint _y2; + uint _flags; + bool _skippable; +}; + +class ActionTimer : public ResultAction { +public: + ActionTimer(const Common::String &line); + bool execute(ZVision *engine); + +private: + uint32 _key; + uint _time; +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/control.cpp b/engines/zvision/scripting/control.cpp new file mode 100644 index 0000000000..35c4ea1441 --- /dev/null +++ b/engines/zvision/scripting/control.cpp @@ -0,0 +1,124 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/control.h" + +#include "zvision/zvision.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/utility/utility.h" + +#include "common/stream.h" + + +namespace ZVision { + +void Control::enable() { + if (!_enabled) { + _enabled = true; + return; + } + + debug("Control %u is already enabled", _key); +} + +void Control::disable() { + if (_enabled) { + _enabled = false; + return; + } + + debug("Control %u is already disabled", _key); +} + +void Control::parseFlatControl(ZVision *engine) { + engine->getRenderManager()->getRenderTable()->setRenderState(RenderTable::FLAT); +} + +void Control::parsePanoramaControl(ZVision *engine, Common::SeekableReadStream &stream) { + RenderTable *renderTable = engine->getRenderManager()->getRenderTable(); + renderTable->setRenderState(RenderTable::PANORAMA); + + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("angle*", true)) { + float fov; + sscanf(line.c_str(), "angle(%f)", &fov); + renderTable->setPanoramaFoV(fov); + } else if (line.matchString("linscale*", true)) { + float scale; + sscanf(line.c_str(), "linscale(%f)", &scale); + renderTable->setPanoramaScale(scale); + } else if (line.matchString("reversepana*", true)) { + uint reverse; + sscanf(line.c_str(), "reversepana(%u)", &reverse); + if (reverse == 1) { + renderTable->setPanoramaReverse(true); + } + } else if (line.matchString("zeropoint*", true)) { + // TODO: Implement + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + renderTable->generateRenderTable(); +} + +void Control::parseTiltControl(ZVision *engine, Common::SeekableReadStream &stream) { + RenderTable *renderTable = engine->getRenderManager()->getRenderTable(); + renderTable->setRenderState(RenderTable::TILT); + + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("angle*", true)) { + float fov; + sscanf(line.c_str(), "angle(%f)", &fov); + renderTable->setTiltFoV(fov); + } else if (line.matchString("linscale*", true)) { + float scale; + sscanf(line.c_str(), "linscale(%f)", &scale); + renderTable->setTiltScale(scale); + } else if (line.matchString("reversepana*", true)) { + uint reverse; + sscanf(line.c_str(), "reversepana(%u)", &reverse); + if (reverse == 1) { + renderTable->setTiltReverse(true); + } + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + renderTable->generateRenderTable(); +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/control.h b/engines/zvision/scripting/control.h new file mode 100644 index 0000000000..770c540a12 --- /dev/null +++ b/engines/zvision/scripting/control.h @@ -0,0 +1,146 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_CONTROL_H +#define ZVISION_CONTROL_H + +#include "common/keyboard.h" + + +namespace Common { +class SeekableReadStream; +struct Point; +class WriteStream; +} + +namespace ZVision { + +class ZVision; + +class Control { +public: + Control() : _engine(0), _key(0), _enabled(false) {} + Control(ZVision *engine, uint32 key) : _engine(engine), _key(key), _enabled(false) {} + virtual ~Control() {} + + uint32 getKey() { return _key; } + + virtual void enable(); + virtual void disable(); + virtual void focus() {} + virtual void unfocus() {} + /** + * Called when LeftMouse is pushed. Default is NOP. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + */ + virtual void onMouseDown(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {} + /** + * Called when LeftMouse is lifted. Default is NOP. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + */ + virtual void onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {} + /** + * Called on every MouseMove. Default is NOP. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + * @return Was the cursor changed? + */ + virtual bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { return false; } + /** + * Called when a key is pressed. Default is NOP. + * + * @param keycode The key that was pressed + */ + virtual void onKeyDown(Common::KeyState keyState) {} + /** + * Called when a key is released. Default is NOP. + * + * @param keycode The key that was pressed + */ + virtual void onKeyUp(Common::KeyState keyState) {} + /** + * Processes the node given the deltaTime since last frame. Default is NOP. + * + * @param deltaTimeInMillis The number of milliseconds that have passed since last frame + * @return If true, the node can be deleted after process() finishes + */ + virtual bool process(uint32 deltaTimeInMillis) { return false; } + /** + * Serialize a Control for save game use. This should only be used if a Control needs + * to save values that would be different from initialization. AKA a TimerNode needs to + * store the amount of time left on the timer. Any Controls overriding this *MUST* write + * their key as the first data outputted. The default implementation is NOP. + * + * NOTE: If this method is overridden, you MUST also override deserialize() + * and needsSerialization() + * + * @param stream Stream to write any needed data to + */ + virtual void serialize(Common::WriteStream *stream) {} + /** + * De-serialize data from a save game stream. This should only be implemented if the + * Control also implements serialize(). The calling method assumes the size of the + * data read from the stream exactly equals that written in serialize(). The default + * implementation is NOP. + * + * NOTE: If this method is overridden, you MUST also override serialize() + * and needsSerialization() + * + * @param stream Save game file stream + */ + virtual void deserialize(Common::SeekableReadStream *stream) {} + /** + * If a Control overrides serialize() and deserialize(), this should return true + * + * @return Does the Control need save game serialization? + */ + virtual inline bool needsSerialization() { return false; } + +protected: + ZVision * _engine; + uint32 _key; + bool _enabled; + +// Static member functions +public: + static void parseFlatControl(ZVision *engine); + static void parsePanoramaControl(ZVision *engine, Common::SeekableReadStream &stream); + static void parseTiltControl(ZVision *engine, Common::SeekableReadStream &stream); +}; + +// TODO: Implement InputControl +// TODO: Implement SaveControl +// TODO: Implement SlotControl +// TODO: Implement SafeControl +// TODO: Implement FistControl +// TODO: Implement HotMovieControl +// TODO: Implement PaintControl +// TODO: Implement TilterControl + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/controls/animation_control.cpp b/engines/zvision/scripting/controls/animation_control.cpp new file mode 100644 index 0000000000..ec8f7a9647 --- /dev/null +++ b/engines/zvision/scripting/controls/animation_control.cpp @@ -0,0 +1,263 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/controls/animation_control.h" + +#include "zvision/zvision.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/scripting/script_manager.h" +#include "zvision/animation/rlf_animation.h" +#include "zvision/video/zork_avi_decoder.h" + +#include "video/video_decoder.h" + +#include "graphics/surface.h" + + +namespace ZVision { + +AnimationControl::AnimationControl(ZVision *engine, uint32 controlKey, const Common::String &fileName) + : Control(engine, controlKey), + _fileType(RLF), + _loopCount(1), + _currentLoop(0), + _accumulatedTime(0), + _cachedFrame(0), + _cachedFrameNeedsDeletion(false) { + if (fileName.hasSuffix(".rlf")) { + _fileType = RLF; + _animation.rlf = new RlfAnimation(fileName, false); + } else if (fileName.hasSuffix(".avi")) { + _fileType = AVI; + _animation.avi = new ZorkAVIDecoder(); + _animation.avi->loadFile(fileName); + } else { + warning("Unrecognized animation file type: %s", fileName.c_str()); + } + + _cachedFrame = new Graphics::Surface(); +} + +AnimationControl::~AnimationControl() { + if (_fileType == RLF) { + delete _animation.rlf; + } else if (_fileType == AVI) { + delete _animation.avi; + } + + _cachedFrame->free(); + delete _cachedFrame; +} + +bool AnimationControl::process(uint32 deltaTimeInMillis) { + if (!_enabled) { + return false; + } + + bool finished = false; + + if (_fileType == RLF) { + _accumulatedTime += deltaTimeInMillis; + + uint32 frameTime = _animation.rlf->frameTime(); + if (_accumulatedTime >= frameTime) { + while (_accumulatedTime >= frameTime) { + _accumulatedTime -= frameTime; + + // Make sure the frame is inside the working window + // If it's not, then just return + + RenderManager *renderManager = _engine->getRenderManager(); + Common::Point workingWindowPoint = renderManager->imageSpaceToWorkingWindowSpace(Common::Point(_x, _y)); + Common::Rect subRect(workingWindowPoint.x, workingWindowPoint.y, workingWindowPoint.x + _animation.rlf->width(), workingWindowPoint.y + _animation.rlf->height()); + + // If the clip returns false, it means the animation is outside the working window + if (!renderManager->clipRectToWorkingWindow(subRect)) { + return false; + } + + const Graphics::Surface *frame = _animation.rlf->getNextFrame(); + + // Animation frames for PANORAMAs are transposed, so un-transpose them + RenderTable::RenderState state = renderManager->getRenderTable()->getRenderState(); + if (state == RenderTable::PANORAMA) { + Graphics::Surface *tranposedFrame = RenderManager::tranposeSurface(frame); + + renderManager->copyRectToWorkingWindow((uint16 *)tranposedFrame->getBasePtr(tranposedFrame->w - subRect.width(), tranposedFrame->h - subRect.height()), subRect.left, subRect.top, _animation.rlf->width(), subRect.width(), subRect.height()); + + // If the background can move, we need to cache the last frame so it can be rendered during background movement + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + if (_cachedFrameNeedsDeletion) { + _cachedFrame->free(); + delete _cachedFrame; + _cachedFrameNeedsDeletion = false; + } + _cachedFrame = tranposedFrame; + _cachedFrameNeedsDeletion = true; + } else { + // Cleanup + tranposedFrame->free(); + delete tranposedFrame; + } + } else { + renderManager->copyRectToWorkingWindow((const uint16 *)frame->getBasePtr(frame->w - subRect.width(), frame->h - subRect.height()), subRect.left, subRect.top, _animation.rlf->width(), subRect.width(), subRect.height()); + + // If the background can move, we need to cache the last frame so it can be rendered during background movement + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + if (_cachedFrameNeedsDeletion) { + _cachedFrame->free(); + delete _cachedFrame; + _cachedFrameNeedsDeletion = false; + } + _cachedFrame->copyFrom(*frame); + } + } + + // Check if we should continue looping + if (_animation.rlf->endOfAnimation()) { + _animation.rlf->seekToFrame(-1); + if (_loopCount > 0) { + _currentLoop++; + if (_currentLoop >= _loopCount) { + finished = true; + } + } + } + } + } else { + // If the background can move, we have to keep rendering animation frames, otherwise the animation flickers during background movement + RenderManager *renderManager = _engine->getRenderManager(); + RenderTable::RenderState state = renderManager->getRenderTable()->getRenderState(); + + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + Common::Point workingWindowPoint = renderManager->imageSpaceToWorkingWindowSpace(Common::Point(_x, _y)); + Common::Rect subRect(workingWindowPoint.x, workingWindowPoint.y, workingWindowPoint.x + _cachedFrame->w, workingWindowPoint.y + _cachedFrame->h); + + // If the clip returns false, it means the animation is outside the working window + if (!renderManager->clipRectToWorkingWindow(subRect)) { + return false; + } + + renderManager->copyRectToWorkingWindow((uint16 *)_cachedFrame->getBasePtr(_cachedFrame->w - subRect.width(), _cachedFrame->h - subRect.height()), subRect.left, subRect.top, _cachedFrame->w, subRect.width(), subRect.height()); + } + } + } else if (_fileType == AVI) { + if (!_animation.avi->isPlaying()) { + _animation.avi->start(); + } + + if (_animation.avi->needsUpdate()) { + const Graphics::Surface *frame = _animation.avi->decodeNextFrame(); + + if (frame) { + // Make sure the frame is inside the working window + // If it's not, then just return + + RenderManager *renderManager = _engine->getRenderManager(); + Common::Point workingWindowPoint = renderManager->imageSpaceToWorkingWindowSpace(Common::Point(_x, _y)); + Common::Rect subRect(workingWindowPoint.x, workingWindowPoint.y, workingWindowPoint.x + frame->w, workingWindowPoint.y + frame->h); + + // If the clip returns false, it means the animation is outside the working window + if (!renderManager->clipRectToWorkingWindow(subRect)) { + return false; + } + + // Animation frames for PANORAMAs are transposed, so un-transpose them + RenderTable::RenderState state = renderManager->getRenderTable()->getRenderState(); + if (state == RenderTable::PANORAMA) { + Graphics::Surface *tranposedFrame = RenderManager::tranposeSurface(frame); + + renderManager->copyRectToWorkingWindow((uint16 *)tranposedFrame->getBasePtr(tranposedFrame->w - subRect.width(), tranposedFrame->h - subRect.height()), subRect.left, subRect.top, frame->w, subRect.width(), subRect.height()); + + // If the background can move, we need to cache the last frame so it can be rendered during background movement + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + if (_cachedFrameNeedsDeletion) { + _cachedFrame->free(); + delete _cachedFrame; + _cachedFrameNeedsDeletion = false; + } + _cachedFrame = tranposedFrame; + _cachedFrameNeedsDeletion = true; + } else { + // Cleanup + tranposedFrame->free(); + delete tranposedFrame; + } + } else { + renderManager->copyRectToWorkingWindow((const uint16 *)frame->getBasePtr(frame->w - subRect.width(), frame->h - subRect.height()), subRect.left, subRect.top, frame->w, subRect.width(), subRect.height()); + + // If the background can move, we need to cache the last frame so it can be rendered during background movement + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + if (_cachedFrameNeedsDeletion) { + _cachedFrame->free(); + delete _cachedFrame; + _cachedFrameNeedsDeletion = false; + } + _cachedFrame->copyFrom(*frame); + } + } + } else { + // If the background can move, we have to keep rendering animation frames, otherwise the animation flickers during background movement + RenderManager *renderManager = _engine->getRenderManager(); + RenderTable::RenderState state = renderManager->getRenderTable()->getRenderState(); + + if (state == RenderTable::PANORAMA || state == RenderTable::TILT) { + Common::Point workingWindowPoint = renderManager->imageSpaceToWorkingWindowSpace(Common::Point(_x, _y)); + Common::Rect subRect(workingWindowPoint.x, workingWindowPoint.y, workingWindowPoint.x + _cachedFrame->w, workingWindowPoint.y + _cachedFrame->h); + + // If the clip returns false, it means the animation is outside the working window + if (!renderManager->clipRectToWorkingWindow(subRect)) { + return false; + } + + renderManager->copyRectToWorkingWindow((uint16 *)_cachedFrame->getBasePtr(_cachedFrame->w - subRect.width(), _cachedFrame->h - subRect.height()), subRect.left, subRect.top, _cachedFrame->w, subRect.width(), subRect.height()); + } + } + } + + // Check if we should continue looping + if (_animation.avi->endOfVideo()) { + _animation.avi->rewind(); + if (_loopCount > 0) { + _currentLoop++; + if (_currentLoop >= _loopCount) { + _animation.avi->stop(); + finished = true; + } + } + } + } + + // If we're done, set _animation key = 2 (Why 2? I don't know. It's just the value that they used) + // Then disable the control. DON'T delete it. It can be re-used + if (finished) { + _engine->getScriptManager()->setStateValue(_animationKey, 2); + disable(); + _currentLoop = 0; + } + + return false; +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/controls/animation_control.h b/engines/zvision/scripting/controls/animation_control.h new file mode 100644 index 0000000000..77663aaf1e --- /dev/null +++ b/engines/zvision/scripting/controls/animation_control.h @@ -0,0 +1,87 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_ANIMATION_CONTROL_H +#define ZVISION_ANIMATION_CONTROL_H + +#include "zvision/scripting/control.h" + + +namespace Common { +class String; +} + +namespace Video { +class VideoDecoder; +} + +namespace Graphics { +struct Surface; +} + +namespace ZVision { + +class ZVision; +class RlfAnimation; + +class AnimationControl : public Control { +public: + AnimationControl(ZVision *engine, uint32 controlKey, const Common::String &fileName); + ~AnimationControl(); + +private: + enum FileType { + RLF = 1, + AVI = 2 + }; + +private: + uint32 _animationKey; + + union { + RlfAnimation *rlf; + Video::VideoDecoder *avi; + } _animation; + + FileType _fileType; + uint _loopCount; + int32 _x; + int32 _y; + + uint _accumulatedTime; + uint _currentLoop; + + Graphics::Surface *_cachedFrame; + bool _cachedFrameNeedsDeletion; + +public: + bool process(uint32 deltaTimeInMillis); + + void setAnimationKey(uint32 animationKey) { _animationKey = animationKey; } + void setLoopCount(uint loopCount) { _loopCount = loopCount; } + void setXPos(int32 x) { _x = x; } + void setYPost(int32 y) { _y = y; } +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/controls/input_control.cpp b/engines/zvision/scripting/controls/input_control.cpp new file mode 100644 index 0000000000..2685b01312 --- /dev/null +++ b/engines/zvision/scripting/controls/input_control.cpp @@ -0,0 +1,142 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/controls/input_control.h" + +#include "zvision/zvision.h" +#include "zvision/scripting/script_manager.h" +#include "zvision/strings/string_manager.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/utility/utility.h" + +#include "common/str.h" +#include "common/stream.h" +#include "common/rect.h" + + +namespace ZVision { + +InputControl::InputControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream) + : Control(engine, key), + _nextTabstop(0), + _focused(false), + _textChanged(false), + _cursorOffset(0) { + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("*rectangle*", true)) { + int x1; + int y1; + int x2; + int y2; + + sscanf(line.c_str(), "%*[^(](%d %d %d %d)", &x1, &y1, &x2, &y2); + + _textRectangle = Common::Rect(x1, y1, x2, y2); + } else if (line.matchString("*aux_hotspot*", true)) { + int x1; + int y1; + int x2; + int y2; + + sscanf(line.c_str(), "%*[^(](%d %d %d %d)", &x1, &y1, &x2, &y2); + + _headerRectangle = Common::Rect(x1, y1, x2, y2); + } else if (line.matchString("*string_init*", true)) { + uint fontFormatNumber; + + sscanf(line.c_str(), "%*[^(](%u)", &fontFormatNumber); + + _textStyle = _engine->getStringManager()->getTextStyle(fontFormatNumber); + } else if (line.matchString("*next_tabstop*", true)) { + sscanf(line.c_str(), "%*[^(](%u)", &_nextTabstop); + } else if (line.matchString("*cursor_animation*", true)) { + char fileName[25]; + + sscanf(line.c_str(), "%*[^(](%25s %*u)", fileName); + + _cursorAnimationFileName = Common::String(fileName); + } else if (line.matchString("*cursor_dimensions*", true)) { + // Ignore, use the dimensions in the animation file + } else if (line.matchString("*cursor_animation_frames*", true)) { + // Ignore, use the frame count in the animation file + } else if (line.matchString("*focus*", true)) { + _focused = true; + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } +} + +void InputControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + _engine->getScriptManager()->focusControl(_key); +} + +void InputControl::onKeyDown(Common::KeyState keyState) { + if (!_focused) { + return; + } + + if (keyState.keycode == Common::KEYCODE_BACKSPACE) { + _currentInputText.deleteLastChar(); + } else if (keyState.keycode == Common::KEYCODE_TAB) { + _focused = false; + // Focus the next input control + _engine->getScriptManager()->focusControl(_nextTabstop); + } else { + // Otherwise, append the new character to the end of the current text + + uint16 asciiValue = keyState.ascii; + // We only care about text values + if (asciiValue >= 32 && asciiValue <= 126) { + _currentInputText += (char)asciiValue; + _textChanged = true; + } + } +} + +bool InputControl::process(uint32 deltaTimeInMillis) { + if (!_focused) { + return false; + } + + // First see if we need to render the text + if (_textChanged) { + // Blit the text using the RenderManager + Common::Rect destRect = _engine->getRenderManager()->renderTextToWorkingWindow(_key, _currentInputText, _textStyle.font, _textRectangle.left, _textRectangle.top, _textStyle.color, _textRectangle.width()); + + _cursorOffset = destRect.left - _textRectangle.left; + } + + // Render the next frame of the animation + // TODO: Implement animation handling + + return false; +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/controls/input_control.h b/engines/zvision/scripting/controls/input_control.h new file mode 100644 index 0000000000..f0fd8b502d --- /dev/null +++ b/engines/zvision/scripting/controls/input_control.h @@ -0,0 +1,60 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_INPUT_CONTROL_H +#define ZVISION_INPUT_CONTROL_H + +#include "zvision/scripting/control.h" +#include "zvision/strings/string_manager.h" + +#include "common/rect.h" + + +namespace ZVision { + +class InputControl : public Control { +public: + InputControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream); + +private: + Common::Rect _textRectangle; + Common::Rect _headerRectangle; + StringManager::TextStyle _textStyle; + uint32 _nextTabstop; + Common::String _cursorAnimationFileName; + bool _focused; + + Common::String _currentInputText; + bool _textChanged; + uint _cursorOffset; + +public: + void focus() { _focused = true; } + void unfocus() { _focused = false; } + void onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + void onKeyDown(Common::KeyState keyState); + bool process(uint32 deltaTimeInMillis); +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/controls/lever_control.cpp b/engines/zvision/scripting/controls/lever_control.cpp new file mode 100644 index 0000000000..e08fdd10f3 --- /dev/null +++ b/engines/zvision/scripting/controls/lever_control.cpp @@ -0,0 +1,402 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/controls/lever_control.h" + +#include "zvision/zvision.h" +#include "zvision/scripting/script_manager.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/cursors/cursor_manager.h" +#include "zvision/animation/rlf_animation.h" +#include "zvision/video/zork_avi_decoder.h" +#include "zvision/utility/utility.h" + +#include "common/stream.h" +#include "common/file.h" +#include "common/tokenizer.h" +#include "common/system.h" + +#include "graphics/surface.h" + + +namespace ZVision { + +LeverControl::LeverControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream) + : Control(engine, key), + _frameInfo(0), + _frameCount(0), + _startFrame(0), + _currentFrame(0), + _lastRenderedFrame(0), + _mouseIsCaptured(false), + _isReturning(false), + _accumulatedTime(0), + _returnRoutesCurrentFrame(0) { + + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("*descfile*", true)) { + char levFileName[25]; + sscanf(line.c_str(), "%*[^(](%25[^)])", levFileName); + + parseLevFile(levFileName); + } else if (line.matchString("*cursor*", true)) { + char cursorName[25]; + sscanf(line.c_str(), "%*[^(](%25[^)])", cursorName); + + _cursorName = Common::String(cursorName); + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + renderFrame(_currentFrame); +} + +LeverControl::~LeverControl() { + if (_fileType == AVI) { + delete _animation.avi; + } else if (_fileType == RLF) { + delete _animation.rlf; + } + + delete[] _frameInfo; +} + +void LeverControl::parseLevFile(const Common::String &fileName) { + Common::File file; + if (!file.open(fileName)) { + warning("LEV file %s could could be opened", fileName.c_str()); + return; + } + + Common::String line = file.readLine(); + + while (!file.eos()) { + if (line.matchString("*animation_id*", true)) { + // Not used + } else if (line.matchString("*filename*", true)) { + char fileNameBuffer[25]; + sscanf(line.c_str(), "%*[^:]:%25[^~]~", fileNameBuffer); + + Common::String animationFileName(fileNameBuffer); + + if (animationFileName.hasSuffix(".avi")) { + _animation.avi = new ZorkAVIDecoder(); + _animation.avi->loadFile(animationFileName); + _fileType = AVI; + } else if (animationFileName.hasSuffix(".rlf")) { + _animation.rlf = new RlfAnimation(animationFileName, false); + _fileType = RLF; + } + } else if (line.matchString("*skipcolor*", true)) { + // Not used + } else if (line.matchString("*anim_coords*", true)) { + int left, top, right, bottom; + sscanf(line.c_str(), "%*[^:]:%d %d %d %d~", &left, &top, &right, &bottom); + + _animationCoords.left = left; + _animationCoords.top = top; + _animationCoords.right = right; + _animationCoords.bottom = bottom; + } else if (line.matchString("*mirrored*", true)) { + uint mirrored; + sscanf(line.c_str(), "%*[^:]:%u~", &mirrored); + + _mirrored = mirrored == 0 ? false : true; + } else if (line.matchString("*frames*", true)) { + sscanf(line.c_str(), "%*[^:]:%u~", &_frameCount); + + _frameInfo = new FrameInfo[_frameCount]; + } else if (line.matchString("*elsewhere*", true)) { + // Not used + } else if (line.matchString("*out_of_control*", true)) { + // Not used + } else if (line.matchString("*start_pos*", true)) { + sscanf(line.c_str(), "%*[^:]:%u~", &_startFrame); + _currentFrame = _startFrame; + } else if (line.matchString("*hotspot_deltas*", true)) { + uint x; + uint y; + sscanf(line.c_str(), "%*[^:]:%u %u~", &x, &y); + + _hotspotDelta.x = x; + _hotspotDelta.y = y; + } else { + uint frameNumber; + uint x, y; + + if (sscanf(line.c_str(), "%u:%u %u", &frameNumber, &x, &y) == 3) { + _frameInfo[frameNumber].hotspot.left = x; + _frameInfo[frameNumber].hotspot.top = y; + _frameInfo[frameNumber].hotspot.right = x + _hotspotDelta.x; + _frameInfo[frameNumber].hotspot.bottom = y + _hotspotDelta.y; + } + + Common::StringTokenizer tokenizer(line, " ^=()"); + tokenizer.nextToken(); + tokenizer.nextToken(); + + Common::String token = tokenizer.nextToken(); + while (!tokenizer.empty()) { + if (token == "D") { + token = tokenizer.nextToken(); + + uint angle; + uint toFrame; + sscanf(token.c_str(), "%u,%u", &toFrame, &angle); + + _frameInfo[frameNumber].directions.push_back(Direction(angle, toFrame)); + } else if (token.hasPrefix("P")) { + // Format: P(<from> to <to>) + tokenizer.nextToken(); + tokenizer.nextToken(); + token = tokenizer.nextToken(); + uint to = atoi(token.c_str()); + + _frameInfo[frameNumber].returnRoute.push_back(to); + } + + token = tokenizer.nextToken(); + } + } + + line = file.readLine(); + } +} + +void LeverControl::onMouseDown(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + if (!_enabled) { + return; + } + + if (_frameInfo[_currentFrame].hotspot.contains(backgroundImageSpacePos)) { + _mouseIsCaptured = true; + _lastMousePos = backgroundImageSpacePos; + } +} + +void LeverControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + if (!_enabled) { + return; + } + + if (_mouseIsCaptured) { + _mouseIsCaptured = false; + _engine->getScriptManager()->setStateValue(_key, _currentFrame); + + _isReturning = true; + _returnRoutesCurrentProgress = _frameInfo[_currentFrame].returnRoute.begin(); + _returnRoutesCurrentFrame = _currentFrame; + } +} + +bool LeverControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + if (!_enabled) { + return false; + } + + bool cursorWasChanged = false; + + if (_mouseIsCaptured) { + // Make sure the square distance between the last point and the current point is greater than 64 + // This is a heuristic. This determines how responsive the lever is to mouse movement. + // TODO: Fiddle with the heuristic to get a good lever responsiveness 'feel' + if (_lastMousePos.sqrDist(backgroundImageSpacePos) >= 64) { + int angle = calculateVectorAngle(_lastMousePos, backgroundImageSpacePos); + _lastMousePos = backgroundImageSpacePos; + + for (Common::List<Direction>::iterator iter = _frameInfo[_currentFrame].directions.begin(); iter != _frameInfo[_currentFrame].directions.end(); ++iter) { + if (angle >= (int)iter->angle - ANGLE_DELTA && angle <= (int)iter->angle + ANGLE_DELTA) { + _currentFrame = iter->toFrame; + renderFrame(_currentFrame); + break; + } + } + } + } else if (_frameInfo[_currentFrame].hotspot.contains(backgroundImageSpacePos)) { + _engine->getCursorManager()->changeCursor(_cursorName); + cursorWasChanged = true; + } + + return cursorWasChanged; +} + +bool LeverControl::process(uint32 deltaTimeInMillis) { + if (!_enabled) { + return false; + } + + if (_isReturning) { + _accumulatedTime += deltaTimeInMillis; + while (_accumulatedTime >= ANIMATION_FRAME_TIME) { + _accumulatedTime -= ANIMATION_FRAME_TIME; + if (_returnRoutesCurrentFrame == *_returnRoutesCurrentProgress) { + _returnRoutesCurrentProgress++; + } + if (_returnRoutesCurrentProgress == _frameInfo[_currentFrame].returnRoute.end()) { + _isReturning = false; + _currentFrame = _returnRoutesCurrentFrame; + return false; + } + + uint toFrame = *_returnRoutesCurrentProgress; + if (_returnRoutesCurrentFrame < toFrame) { + _returnRoutesCurrentFrame++; + } else if (_returnRoutesCurrentFrame > toFrame) { + _returnRoutesCurrentFrame--; + } + + _engine->getScriptManager()->setStateValue(_key, _returnRoutesCurrentFrame); + renderFrame(_returnRoutesCurrentFrame); + } + } + + return false; +} + +int LeverControl::calculateVectorAngle(const Common::Point &pointOne, const Common::Point &pointTwo) { + // Check for the easy angles first + if (pointOne.x == pointTwo.x && pointOne.y == pointTwo.y) + return -1; // This should never happen + else if (pointOne.x == pointTwo.x) { + if (pointTwo.y < pointOne.y) + return 90; + else + return 270; + } else if (pointOne.y == pointTwo.y) { + if (pointTwo.x > pointOne.x) + return 0; + else + return 180; + } else { + // Calculate the angle with trig + int16 xDist = pointTwo.x - pointOne.x; + int16 yDist = pointTwo.y - pointOne.y; + + // Calculate the angle using arctan + // Then convert to degrees. (180 / 3.14159 = 57.2958) + int angle = int(atan((float)yDist / (float)xDist) * 57); + + // Calculate what quadrant pointTwo is in + uint quadrant = ((yDist > 0 ? 1 : 0) << 1) | (xDist < 0 ? 1 : 0); + + // Explanation of quadrants: + // + // yDist > 0 | xDist < 0 | Quadrant number + // 0 | 0 | 0 + // 0 | 1 | 1 + // 1 | 0 | 2 + // 1 | 1 | 3 + // + // Note: I know this doesn't line up with traditional mathematical quadrants + // but doing it this way allows you can use a switch and is a bit cleaner IMO. + // + // The graph below shows the 4 quadrants pointTwo can end up in as well + // as what the angle as calculated above refers to. + // Note: The calculated angle in quadrants 0 and 3 is negative + // due to arctan(-x) = -theta + // + // Origin => (pointOne.x, pointOne.y) + // * => (pointTwo.x, pointTwo.y) + // + // 90 | + // ^ | + // * | * | + // \ | / | + // \ | / | + // \ | / | + // Quadrant 1 \ | / Quadrant 0 | + // \ | / | + // \ | / | + // angle ( \|/ ) -angle | + // 180 <----------------------------------------> 0 | + // -angle ( /|\ ) angle | + // / | \ | + // / | \ | + // Quadrant 3 / | \ Quadrant 2 | + // / | \ | + // / | \ | + // / | \ | + // * | * | + // ^ | + // 270 | + + // Convert the local angles to unit circle angles + switch (quadrant) { + case 0: + angle = 180 + angle; + break; + case 1: + // Do nothing + break; + case 2: + angle = 180 + angle; + break; + case 3: + angle = 360 + angle; + break; + } + + return angle; + } +} + +void LeverControl::renderFrame(uint frameNumber) { + if (frameNumber == 0) { + _lastRenderedFrame = frameNumber; + } else if (frameNumber < _lastRenderedFrame && _mirrored) { + _lastRenderedFrame = frameNumber; + frameNumber = (_frameCount * 2) - frameNumber - 1; + } else { + _lastRenderedFrame = frameNumber; + } + + const uint16 *frameData = 0; + int x = _animationCoords.left; + int y = _animationCoords.top; + int width = 0; + int height = 0; + + if (_fileType == RLF) { + // getFrameData() will automatically optimize to getNextFrame() / getPreviousFrame() if it can + frameData = (const uint16 *)_animation.rlf->getFrameData(frameNumber)->getPixels(); + width = _animation.rlf->width(); // Use the animation width instead of _animationCoords.width() + height = _animation.rlf->height(); // Use the animation height instead of _animationCoords.height() + } else if (_fileType == AVI) { + _animation.avi->seekToFrame(frameNumber); + const Graphics::Surface *surface = _animation.avi->decodeNextFrame(); + frameData = (const uint16 *)surface->getPixels(); + width = surface->w; + height = surface->h; + } + + _engine->getRenderManager()->copyRectToWorkingWindow(frameData, x, y, width, width, height); +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/controls/lever_control.h b/engines/zvision/scripting/controls/lever_control.h new file mode 100644 index 0000000000..69473cf119 --- /dev/null +++ b/engines/zvision/scripting/controls/lever_control.h @@ -0,0 +1,127 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_LEVER_CONTROL_H +#define ZVISION_LEVER_CONTROL_H + +#include "zvision/scripting/control.h" + +#include "common/list.h" +#include "common/rect.h" + + +namespace ZVision { + +class ZorkAVIDecoder; +class RlfAnimation; + +class LeverControl : public Control { +public: + LeverControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream); + ~LeverControl(); + +private: + enum FileType { + RLF = 1, + AVI = 2 + }; + + struct Direction { + Direction(uint a, uint t) : angle(a), toFrame(t) {} + + uint angle; + uint toFrame; + }; + + struct FrameInfo { + Common::Rect hotspot; + Common::List<Direction> directions; + Common::List<uint> returnRoute; + }; + + enum { + ANGLE_DELTA = 30, // How far off a mouse angle can be and still be considered valid. This is in both directions, so the total buffer zone is (2 * ANGLE_DELTA) + ANIMATION_FRAME_TIME = 30 // In millis + }; + +private: + union { + RlfAnimation *rlf; + ZorkAVIDecoder *avi; + } _animation; + FileType _fileType; + + Common::String _cursorName; + Common::Rect _animationCoords; + bool _mirrored; + uint _frameCount; + uint _startFrame; + Common::Point _hotspotDelta; + FrameInfo *_frameInfo; + + uint _currentFrame; + uint _lastRenderedFrame; + bool _mouseIsCaptured; + bool _isReturning; + Common::Point _lastMousePos; + Common::List<uint>::iterator _returnRoutesCurrentProgress; + uint _returnRoutesCurrentFrame; + uint32 _accumulatedTime; + +public: + void onMouseDown(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + void onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + bool process(uint32 deltaTimeInMillis); + +private: + void parseLevFile(const Common::String &fileName); + /** + * Calculates the angle a vector makes with the negative y-axis + * + * 90 + * pointTwo * ^ + * \ | + * \ | + * \ | + * \ | + * angle ( \|pointOne + * 180 <-----------*-----------> 0 + * | + * | + * | + * | + * | + * ^ + * 270 + * + * @param pointOne The origin of the vector + * @param pointTwo The end of the vector + * @return The angle the vector makes with the negative y-axis + */ + static int calculateVectorAngle(const Common::Point &pointOne, const Common::Point &pointTwo); + void renderFrame(uint frameNumber); +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/controls/push_toggle_control.cpp b/engines/zvision/scripting/controls/push_toggle_control.cpp new file mode 100644 index 0000000000..11ec4bb73f --- /dev/null +++ b/engines/zvision/scripting/controls/push_toggle_control.cpp @@ -0,0 +1,98 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/controls/push_toggle_control.h" + +#include "zvision/zvision.h" +#include "zvision/scripting/script_manager.h" +#include "zvision/cursors/cursor_manager.h" +#include "zvision/utility/utility.h" + +#include "common/stream.h" + + +namespace ZVision { + +PushToggleControl::PushToggleControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream) + : Control(engine, key) { + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("*_hotspot*", true)) { + uint x; + uint y; + uint width; + uint height; + + sscanf(line.c_str(), "%*[^(](%u,%u,%u,%u)", &x, &y, &width, &height); + + _hotspot = Common::Rect(x, y, x + width, y + height); + } else if (line.matchString("cursor*", true)) { + char nameBuffer[25]; + + sscanf(line.c_str(), "%*[^(](%25[^)])", nameBuffer); + + _hoverCursor = Common::String(nameBuffer); + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + if (_hotspot.isEmpty() || _hoverCursor.empty()) { + warning("Push_toggle cursor %u was parsed incorrectly", key); + } +} + +PushToggleControl::~PushToggleControl() { + // Clear the state value back to 0 + _engine->getScriptManager()->setStateValue(_key, 0); +} + +void PushToggleControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + if (!_enabled) { + return; + } + + if (_hotspot.contains(backgroundImageSpacePos)) { + _engine->getScriptManager()->setStateValue(_key, 1); + } +} + +bool PushToggleControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + if (!_enabled) { + return false; + } + + if (_hotspot.contains(backgroundImageSpacePos)) { + _engine->getCursorManager()->changeCursor(_hoverCursor); + return true; + } + + return false; +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/controls/push_toggle_control.h b/engines/zvision/scripting/controls/push_toggle_control.h new file mode 100644 index 0000000000..13dc54a65f --- /dev/null +++ b/engines/zvision/scripting/controls/push_toggle_control.h @@ -0,0 +1,67 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_PUSH_TOGGLE_CONTROL_H +#define ZVISION_PUSH_TOGGLE_CONTROL_H + +#include "zvision/scripting/control.h" + +#include "common/rect.h" + + +namespace ZVision { + +class PushToggleControl : public Control { +public: + PushToggleControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream); + ~PushToggleControl(); + + /** + * Called when LeftMouse is lifted. Calls ScriptManager::setStateValue(_key, 1); + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + */ + void onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + /** + * Called on every MouseMove. Tests if the mouse is inside _hotspot, and if so, sets the cursor. + * + * @param engine The base engine + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + * @return Was the cursor changed? + */ + bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + +private: + /** + * The area that will trigger the event + * This is in image space coordinates, NOT screen space + */ + Common::Rect _hotspot; + /** The cursor to use when hovering over _hotspot */ + Common::String _hoverCursor; +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/controls/timer_node.cpp b/engines/zvision/scripting/controls/timer_node.cpp new file mode 100644 index 0000000000..3b691be275 --- /dev/null +++ b/engines/zvision/scripting/controls/timer_node.cpp @@ -0,0 +1,73 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/controls/timer_node.h" + +#include "zvision/zvision.h" +#include "zvision/scripting/script_manager.h" + +#include "common/stream.h" + + +namespace ZVision { + +TimerNode::TimerNode(ZVision *engine, uint32 key, uint timeInSeconds) + : Control(engine, key) { + if (_engine->getGameId() == GID_NEMESIS) { + _timeLeft = timeInSeconds * 1000; + } else if (_engine->getGameId() == GID_GRANDINQUISITOR) { + _timeLeft = timeInSeconds * 100; + } + + _engine->getScriptManager()->setStateValue(_key, 1); +} + +TimerNode::~TimerNode() { + if (_timeLeft <= 0) + _engine->getScriptManager()->setStateValue(_key, 2); + else + _engine->getScriptManager()->setStateValue(_key, _timeLeft); // If timer was stopped by stop or kill +} + +bool TimerNode::process(uint32 deltaTimeInMillis) { + _timeLeft -= deltaTimeInMillis; + + if (_timeLeft <= 0) { + // Let the destructor reset the state value + return true; + } + + return false; +} + +void TimerNode::serialize(Common::WriteStream *stream) { + stream->writeUint32LE(_key); + stream->writeUint32LE(_timeLeft); +} + +void TimerNode::deserialize(Common::SeekableReadStream *stream) { + _timeLeft = stream->readUint32LE(); +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/controls/timer_node.h b/engines/zvision/scripting/controls/timer_node.h new file mode 100644 index 0000000000..a8e579cbe4 --- /dev/null +++ b/engines/zvision/scripting/controls/timer_node.h @@ -0,0 +1,55 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_TIMER_NODE_H +#define ZVISION_TIMER_NODE_H + +#include "zvision/scripting/control.h" + +namespace ZVision { + +class ZVision; + +class TimerNode : public Control { +public: + TimerNode(ZVision *engine, uint32 key, uint timeInSeconds); + ~TimerNode(); + + /** + * Decrement the timer by the delta time. If the timer is finished, set the status + * in _globalState and let this node be deleted + * + * @param deltaTimeInMillis The number of milliseconds that have passed since last frame + * @return If true, the node can be deleted after process() finishes + */ + bool process(uint32 deltaTimeInMillis); + void serialize(Common::WriteStream *stream); + void deserialize(Common::SeekableReadStream *stream); + inline bool needsSerialization() { return true; } + +private: + int32 _timeLeft; +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/puzzle.h b/engines/zvision/scripting/puzzle.h new file mode 100644 index 0000000000..0d717f1fa9 --- /dev/null +++ b/engines/zvision/scripting/puzzle.h @@ -0,0 +1,74 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_PUZZLE_H +#define ZVISION_PUZZLE_H + +#include "zvision/scripting/actions.h" + +#include "common/list.h" +#include "common/ptr.h" + + +namespace ZVision { + +struct Puzzle { + Puzzle() : key(0) {} + + ~Puzzle() { + for (Common::List<ResultAction *>::iterator iter = resultActions.begin(); iter != resultActions.end(); ++iter) { + delete *iter; + } + } + + /** How criteria should be decided */ + enum CriteriaOperator { + EQUAL_TO, + NOT_EQUAL_TO, + GREATER_THAN, + LESS_THAN + }; + + /** Criteria for a Puzzle result to be fired */ + struct CriteriaEntry { + /** The key of a global state */ + uint32 key; + /** + * What we're comparing the value of the global state against + * This can either be a pure value or it can be the key of another global state + */ + uint32 argument; + /** How to do the comparison */ + CriteriaOperator criteriaOperator; + /** Whether 'argument' is the key of a global state (true) or a pure value (false) */ + bool argumentIsAKey; + }; + + uint32 key; + Common::List<Common::List <CriteriaEntry> > criteriaList; + // This has to be list of pointers because ResultAction is abstract + Common::List<ResultAction *> resultActions; +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/scripting/scr_file_handling.cpp b/engines/zvision/scripting/scr_file_handling.cpp new file mode 100644 index 0000000000..d93094a3b2 --- /dev/null +++ b/engines/zvision/scripting/scr_file_handling.cpp @@ -0,0 +1,302 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/scummsys.h" + +#include "zvision/scripting/script_manager.h" + +#include "zvision/utility/utility.h" +#include "zvision/scripting/puzzle.h" +#include "zvision/scripting/actions.h" +#include "zvision/scripting/controls/push_toggle_control.h" +#include "zvision/scripting/controls/lever_control.h" + +#include "common/textconsole.h" +#include "common/file.h" +#include "common/tokenizer.h" + + +namespace ZVision { + +void ScriptManager::parseScrFile(const Common::String &fileName, bool isGlobal) { + Common::File file; + if (!file.open(fileName)) { + warning("Script file not found: %s", fileName.c_str()); + return; + } + + while(!file.eos()) { + Common::String line = file.readLine(); + if (file.err()) { + warning("Error parsing scr file: %s", fileName.c_str()); + return; + } + + trimCommentsAndWhiteSpace(&line); + if (line.empty()) + continue; + + if (line.matchString("puzzle:*", true)) { + Puzzle *puzzle = new Puzzle(); + sscanf(line.c_str(),"puzzle:%u",&(puzzle->key)); + + parsePuzzle(puzzle, file); + if (isGlobal) { + _globalPuzzles.push_back(puzzle); + } else { + _activePuzzles.push_back(puzzle); + } + } else if (line.matchString("control:*", true)) { + parseControl(line, file); + } + } +} + +void ScriptManager::parsePuzzle(Puzzle *puzzle, Common::SeekableReadStream &stream) { + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("criteria {", true)) { + parseCriteria(stream, puzzle->criteriaList); + } else if (line.matchString("results {", true)) { + parseResults(stream, puzzle->resultActions); + } else if (line.matchString("flags {", true)) { + setStateFlags(puzzle->key, parseFlags(stream)); + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } +} + +bool ScriptManager::parseCriteria(Common::SeekableReadStream &stream, Common::List<Common::List<Puzzle::CriteriaEntry> > &criteriaList) const { + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + // Criteria can be empty + if (line.contains('}')) { + return false; + } + + // Create a new List to hold the CriteriaEntries + criteriaList.push_back(Common::List<Puzzle::CriteriaEntry>()); + + while (!stream.eos() && !line.contains('}')) { + Puzzle::CriteriaEntry entry; + + // Split the string into tokens using ' ' as a delimiter + Common::StringTokenizer tokenizer(line); + Common::String token; + + // Parse the id out of the first token + token = tokenizer.nextToken(); + sscanf(token.c_str(), "[%u]", &(entry.key)); + + // Parse the operator out of the second token + token = tokenizer.nextToken(); + if (token.c_str()[0] == '=') + entry.criteriaOperator = Puzzle::EQUAL_TO; + else if (token.c_str()[0] == '!') + entry.criteriaOperator = Puzzle::NOT_EQUAL_TO; + else if (token.c_str()[0] == '>') + entry.criteriaOperator = Puzzle::GREATER_THAN; + else if (token.c_str()[0] == '<') + entry.criteriaOperator = Puzzle::LESS_THAN; + + // First determine if the last token is an id or a value + // Then parse it into 'argument' + token = tokenizer.nextToken(); + if (token.contains('[')) { + sscanf(token.c_str(), "[%u]", &(entry.argument)); + entry.argumentIsAKey = true; + } else { + sscanf(token.c_str(), "%u", &(entry.argument)); + entry.argumentIsAKey = false; + } + + criteriaList.back().push_back(entry); + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + return true; +} + +void ScriptManager::parseResults(Common::SeekableReadStream &stream, Common::List<ResultAction *> &actionList) const { + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + // TODO: Re-order the if-then statements in order of highest occurrence + while (!stream.eos() && !line.contains('}')) { + if (line.empty()) { + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + continue; + } + + // Parse for the action type + if (line.matchString("*:add*", true)) { + actionList.push_back(new ActionAdd(line)); + } else if (line.matchString("*:animplay*", true)) { + actionList.push_back(new ActionPlayAnimation(line)); + } else if (line.matchString("*:animpreload*", true)) { + actionList.push_back(new ActionPreloadAnimation(line)); + } else if (line.matchString("*:animunload*", true)) { + //actionList.push_back(new ActionUnloadAnimation(line)); + } else if (line.matchString("*:attenuate*", true)) { + // TODO: Implement ActionAttenuate + } else if (line.matchString("*:assign*", true)) { + actionList.push_back(new ActionAssign(line)); + } else if (line.matchString("*:change_location*", true)) { + actionList.push_back(new ActionChangeLocation(line)); + } else if (line.matchString("*:crossfade*", true)) { + // TODO: Implement ActionCrossfade + } else if (line.matchString("*:debug*", true)) { + // TODO: Implement ActionDebug + } else if (line.matchString("*:delay_render*", true)) { + // TODO: Implement ActionDelayRender + } else if (line.matchString("*:disable_control*", true)) { + actionList.push_back(new ActionDisableControl(line)); + } else if (line.matchString("*:disable_venus*", true)) { + // TODO: Implement ActionDisableVenus + } else if (line.matchString("*:display_message*", true)) { + // TODO: Implement ActionDisplayMessage + } else if (line.matchString("*:dissolve*", true)) { + // TODO: Implement ActionDissolve + } else if (line.matchString("*:distort*", true)) { + // TODO: Implement ActionDistort + } else if (line.matchString("*:enable_control*", true)) { + actionList.push_back(new ActionEnableControl(line)); + } else if (line.matchString("*:flush_mouse_events*", true)) { + // TODO: Implement ActionFlushMouseEvents + } else if (line.matchString("*:inventory*", true)) { + // TODO: Implement ActionInventory + } else if (line.matchString("*:kill*", true)) { + // TODO: Implement ActionKill + } else if (line.matchString("*:menu_bar_enable*", true)) { + // TODO: Implement ActionMenuBarEnable + } else if (line.matchString("*:music*", true)) { + actionList.push_back(new ActionMusic(line)); + } else if (line.matchString("*:pan_track*", true)) { + // TODO: Implement ActionPanTrack + } else if (line.matchString("*:playpreload*", true)) { + actionList.push_back(new ActionPlayPreloadAnimation(line)); + } else if (line.matchString("*:preferences*", true)) { + // TODO: Implement ActionPreferences + } else if (line.matchString("*:quit*", true)) { + actionList.push_back(new ActionQuit()); + } else if (line.matchString("*:random*", true)) { + actionList.push_back(new ActionRandom(line)); + } else if (line.matchString("*:region*", true)) { + // TODO: Implement ActionRegion + } else if (line.matchString("*:restore_game*", true)) { + // TODO: Implement ActionRestoreGame + } else if (line.matchString("*:rotate_to*", true)) { + // TODO: Implement ActionRotateTo + } else if (line.matchString("*:save_game*", true)) { + // TODO: Implement ActionSaveGame + } else if (line.matchString("*:set_partial_screen*", true)) { + actionList.push_back(new ActionSetPartialScreen(line)); + } else if (line.matchString("*:set_screen*", true)) { + actionList.push_back(new ActionSetScreen(line)); + } else if (line.matchString("*:set_venus*", true)) { + // TODO: Implement ActionSetVenus + } else if (line.matchString("*:stop*", true)) { + // TODO: Implement ActionStop + } else if (line.matchString("*:streamvideo*", true)) { + actionList.push_back(new ActionStreamVideo(line)); + } else if (line.matchString("*:syncsound*", true)) { + // TODO: Implement ActionSyncSound + } else if (line.matchString("*:timer*", true)) { + actionList.push_back(new ActionTimer(line)); + } else if (line.matchString("*:ttytext*", true)) { + // TODO: Implement ActionTTYText + } else if (line.matchString("*:universe_music*", true)) { + // TODO: Implement ActionUniverseMusic + } else if (line.matchString("*:copy_file*", true)) { + // Not used. Purposely left empty + } else { + warning("Unhandled result action type: %s", line.c_str()); + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + return; +} + +uint ScriptManager::parseFlags(Common::SeekableReadStream &stream) const { + uint flags = 0; + + // Loop until we find the closing brace + Common::String line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + + while (!stream.eos() && !line.contains('}')) { + if (line.matchString("ONCE_PER_INST", true)) { + flags |= ONCE_PER_INST; + } else if (line.matchString("DO_ME_NOW", true)) { + flags |= DO_ME_NOW; + } else if (line.matchString("DISABLED", true)) { + flags |= DISABLED; + } + + line = stream.readLine(); + trimCommentsAndWhiteSpace(&line); + } + + return flags; +} + +void ScriptManager::parseControl(Common::String &line, Common::SeekableReadStream &stream) { + uint32 key; + char controlTypeBuffer[20]; + + sscanf(line.c_str(), "control:%u %s {", &key, controlTypeBuffer); + + Common::String controlType(controlTypeBuffer); + + if (controlType.equalsIgnoreCase("push_toggle")) { + _activeControls.push_back(new PushToggleControl(_engine, key, stream)); + return; + } else if (controlType.equalsIgnoreCase("flat")) { + Control::parseFlatControl(_engine); + return; + } else if (controlType.equalsIgnoreCase("pana")) { + Control::parsePanoramaControl(_engine, stream); + return; + } else if (controlType.equalsIgnoreCase("tilt")) { + Control::parseTiltControl(_engine, stream); + return; + } else if (controlType.equalsIgnoreCase("lever")) { + _activeControls.push_back(new LeverControl(_engine, key, stream)); + return; + } +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/script_manager.cpp b/engines/zvision/scripting/script_manager.cpp new file mode 100644 index 0000000000..adcc5c7545 --- /dev/null +++ b/engines/zvision/scripting/script_manager.cpp @@ -0,0 +1,444 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#include "common/scummsys.h" + +#include "zvision/scripting/script_manager.h" + +#include "zvision/zvision.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/cursors/cursor_manager.h" +#include "zvision/core/save_manager.h" +#include "zvision/scripting/actions.h" +#include "zvision/utility/utility.h" + +#include "common/algorithm.h" +#include "common/hashmap.h" +#include "common/debug.h" +#include "common/stream.h" + + +namespace ZVision { + +ScriptManager::ScriptManager(ZVision *engine) + : _engine(engine), + _currentlyFocusedControl(0) { +} + +ScriptManager::~ScriptManager() { + for (PuzzleList::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); ++iter) { + delete (*iter); + } + for (PuzzleList::iterator iter = _globalPuzzles.begin(); iter != _globalPuzzles.end(); ++iter) { + delete (*iter); + } + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + delete (*iter); + } +} + +void ScriptManager::initialize() { + parseScrFile("universe.scr", true); + changeLocation('g', 'a', 'r', 'y', 0); +} + +void ScriptManager::update(uint deltaTimeMillis) { + updateNodes(deltaTimeMillis); + checkPuzzleCriteria(); +} + +void ScriptManager::createReferenceTable() { + // Iterate through each local Puzzle + for (PuzzleList::iterator activePuzzleIter = _activePuzzles.begin(); activePuzzleIter != _activePuzzles.end(); ++activePuzzleIter) { + Puzzle *puzzlePtr = (*activePuzzleIter); + + // Iterate through each CriteriaEntry and add a reference from the criteria key to the Puzzle + for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = (*activePuzzleIter)->criteriaList.begin(); criteriaIter != (*activePuzzleIter)->criteriaList.end(); ++criteriaIter) { + for (Common::List<Puzzle::CriteriaEntry>::iterator entryIter = criteriaIter->begin(); entryIter != criteriaIter->end(); ++entryIter) { + _referenceTable[entryIter->key].push_back(puzzlePtr); + + // If the argument is a key, add a reference to it as well + if (entryIter->argumentIsAKey) { + _referenceTable[entryIter->argument].push_back(puzzlePtr); + } + } + } + } + + // Iterate through each global Puzzle + for (PuzzleList::iterator globalPuzzleIter = _globalPuzzles.begin(); globalPuzzleIter != _globalPuzzles.end(); ++globalPuzzleIter) { + Puzzle *puzzlePtr = (*globalPuzzleIter); + + // Iterate through each CriteriaEntry and add a reference from the criteria key to the Puzzle + for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = (*globalPuzzleIter)->criteriaList.begin(); criteriaIter != (*globalPuzzleIter)->criteriaList.end(); ++criteriaIter) { + for (Common::List<Puzzle::CriteriaEntry>::iterator entryIter = criteriaIter->begin(); entryIter != criteriaIter->end(); ++entryIter) { + _referenceTable[entryIter->key].push_back(puzzlePtr); + + // If the argument is a key, add a reference to it as well + if (entryIter->argumentIsAKey) { + _referenceTable[entryIter->argument].push_back(puzzlePtr); + } + } + } + } + + // Remove duplicate entries + for (PuzzleMap::iterator referenceTableIter = _referenceTable.begin(); referenceTableIter != _referenceTable.end(); ++referenceTableIter) { + removeDuplicateEntries(referenceTableIter->_value); + } +} + +void ScriptManager::updateNodes(uint deltaTimeMillis) { + // If process() returns true, it means the node can be deleted + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end();) { + if ((*iter)->process(deltaTimeMillis)) { + delete (*iter); + // Remove the node + iter = _activeControls.erase(iter); + } else { + ++iter; + } + } +} + +void ScriptManager::checkPuzzleCriteria() { + while (!_puzzlesToCheck.empty()) { + Puzzle *puzzle = _puzzlesToCheck.pop(); + + // Check if the puzzle is already finished + // Also check that the puzzle isn't disabled + if (getStateValue(puzzle->key) == 1 && (getStateFlags(puzzle->key) & DISABLED) == 0) { + continue; + } + + // Check each Criteria + + bool criteriaMet = false; + for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = puzzle->criteriaList.begin(); criteriaIter != puzzle->criteriaList.end(); ++criteriaIter) { + criteriaMet = false; + + for (Common::List<Puzzle::CriteriaEntry>::iterator entryIter = criteriaIter->begin(); entryIter != criteriaIter->end(); ++entryIter) { + // Get the value to compare against + uint argumentValue; + if (entryIter->argumentIsAKey) + argumentValue = getStateValue(entryIter->argument); + else + argumentValue = entryIter->argument; + + // Do the comparison + switch (entryIter->criteriaOperator) { + case Puzzle::EQUAL_TO: + criteriaMet = getStateValue(entryIter->key) == argumentValue; + break; + case Puzzle::NOT_EQUAL_TO: + criteriaMet = getStateValue(entryIter->key) != argumentValue; + break; + case Puzzle::GREATER_THAN: + criteriaMet = getStateValue(entryIter->key) > argumentValue; + break; + case Puzzle::LESS_THAN: + criteriaMet = getStateValue(entryIter->key) < argumentValue; + break; + } + + // If one check returns false, don't keep checking + if (!criteriaMet) { + break; + } + } + + // If any of the Criteria are *fully* met, then execute the results + if (criteriaMet) { + break; + } + } + + // criteriaList can be empty. Aka, the puzzle should be executed immediately + if (puzzle->criteriaList.empty() || criteriaMet) { + debug(1, "Puzzle %u criteria passed. Executing its ResultActions", puzzle->key); + + // Set the puzzle as completed + setStateValue(puzzle->key, 1); + + bool shouldContinue = true; + for (Common::List<ResultAction *>::iterator resultIter = puzzle->resultActions.begin(); resultIter != puzzle->resultActions.end(); ++resultIter) { + shouldContinue = shouldContinue && (*resultIter)->execute(_engine); + if (!shouldContinue) { + break; + } + } + + if (!shouldContinue) { + break; + } + } + } +} + +void ScriptManager::cleanStateTable() { + for (StateMap::iterator iter = _globalState.begin(); iter != _globalState.end(); ++iter) { + // If the value is equal to zero, we can purge it since getStateValue() + // will return zero if _globalState doesn't contain a key + if (iter->_value == 0) { + // Remove the node + _globalState.erase(iter); + } + } +} + +uint ScriptManager::getStateValue(uint32 key) { + if (_globalState.contains(key)) + return _globalState[key]; + else + return 0; +} + +void ScriptManager::setStateValue(uint32 key, uint value) { + _globalState[key] = value; + + if (_referenceTable.contains(key)) { + for (Common::Array<Puzzle *>::iterator iter = _referenceTable[key].begin(); iter != _referenceTable[key].end(); ++iter) { + _puzzlesToCheck.push((*iter)); + } + } +} + +uint ScriptManager::getStateFlags(uint32 key) { + if (_globalStateFlags.contains(key)) + return _globalStateFlags[key]; + else + return 0; +} + +void ScriptManager::setStateFlags(uint32 key, uint flags) { + _globalStateFlags[key] = flags; + + if (_referenceTable.contains(key)) { + for (Common::Array<Puzzle *>::iterator iter = _referenceTable[key].begin(); iter != _referenceTable[key].end(); ++iter) { + _puzzlesToCheck.push((*iter)); + } + } +} + +void ScriptManager::addToStateValue(uint32 key, uint valueToAdd) { + _globalState[key] += valueToAdd; +} + +void ScriptManager::addControl(Control *control) { + _activeControls.push_back(control); +} + +Control *ScriptManager::getControl(uint32 key) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + if ((*iter)->getKey() == key) { + return (*iter); + } + } + + return nullptr; +} + +void ScriptManager::focusControl(uint32 key) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + uint32 controlKey = (*iter)->getKey(); + + if (controlKey == key) { + (*iter)->focus(); + } else if (controlKey == _currentlyFocusedControl) { + (*iter)->unfocus(); + } + } + + _currentlyFocusedControl = key; +} + +void ScriptManager::onMouseDown(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->onMouseDown(screenSpacePos, backgroundImageSpacePos); + } +} + +void ScriptManager::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->onMouseUp(screenSpacePos, backgroundImageSpacePos); + } +} + +bool ScriptManager::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) { + bool cursorWasChanged = false; + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + cursorWasChanged = cursorWasChanged || (*iter)->onMouseMove(screenSpacePos, backgroundImageSpacePos); + } + + return cursorWasChanged; +} + +void ScriptManager::onKeyDown(Common::KeyState keyState) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->onKeyDown(keyState); + } +} + +void ScriptManager::onKeyUp(Common::KeyState keyState) { + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->onKeyUp(keyState); + } +} + +void ScriptManager::changeLocation(char world, char room, char node, char view, uint32 offset) { + assert(world != 0); + debug(1, "Changing location to: %c %c %c %c %u", world, room, node, view, offset); + + // Auto save + _engine->getSaveManager()->autoSave(); + + // Clear all the containers + _referenceTable.clear(); + _puzzlesToCheck.clear(); + for (PuzzleList::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); ++iter) { + delete (*iter); + } + _activePuzzles.clear(); + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + delete (*iter); + } + _activeControls.clear(); + + // Revert to the idle cursor + _engine->getCursorManager()->revertToIdle(); + + // Reset the background velocity + _engine->getRenderManager()->setBackgroundVelocity(0); + + // Remove any alphaEntries + _engine->getRenderManager()->clearAlphaEntries(); + + // Clean the global state table + cleanStateTable(); + + // Parse into puzzles and controls + Common::String fileName = Common::String::format("%c%c%c%c.scr", world, room, node, view); + parseScrFile(fileName); + + // Change the background position + _engine->getRenderManager()->setBackgroundPosition(offset); + + // Enable all the controls + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->enable(); + } + + // Add all the local puzzles to the queue to be checked + for (PuzzleList::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); ++iter) { + // Reset any Puzzles that have the flag ONCE_PER_INST + if ((getStateFlags((*iter)->key) & ONCE_PER_INST) == ONCE_PER_INST) { + setStateValue((*iter)->key, 0); + } + + _puzzlesToCheck.push((*iter)); + } + + // Add all the global puzzles to the queue to be checked + for (PuzzleList::iterator iter = _globalPuzzles.begin(); iter != _globalPuzzles.end(); ++iter) { + // Reset any Puzzles that have the flag ONCE_PER_INST + if ((getStateFlags((*iter)->key) & ONCE_PER_INST) == ONCE_PER_INST) { + setStateValue((*iter)->key, 0); + } + + _puzzlesToCheck.push((*iter)); + } + + // Create the puzzle reference table + createReferenceTable(); + + // Update _currentLocation + _currentLocation.world = world; + _currentLocation.room = room; + _currentLocation.node = node; + _currentLocation.view = view; + _currentLocation.offset = offset; +} + +void ScriptManager::serializeStateTable(Common::WriteStream *stream) { + // Write the number of state value entries + stream->writeUint32LE(_globalState.size()); + + for (StateMap::iterator iter = _globalState.begin(); iter != _globalState.end(); ++iter) { + // Write out the key/value pair + stream->writeUint32LE(iter->_key); + stream->writeUint32LE(iter->_value); + } +} + +void ScriptManager::deserializeStateTable(Common::SeekableReadStream *stream) { + // Clear out the current table values + _globalState.clear(); + + // Read the number of key/value pairs + uint32 numberOfPairs = stream->readUint32LE(); + + for (uint32 i = 0; i < numberOfPairs; ++i) { + uint32 key = stream->readUint32LE(); + uint32 value = stream->readUint32LE(); + // Directly access the state table so we don't trigger Puzzle checks + _globalState[key] = value; + } +} + +void ScriptManager::serializeControls(Common::WriteStream *stream) { + // Count how many controls need to save their data + // Because WriteStream isn't seekable + uint32 numberOfControlsNeedingSerialization = 0; + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + if ((*iter)->needsSerialization()) { + numberOfControlsNeedingSerialization++; + } + } + stream->writeUint32LE(numberOfControlsNeedingSerialization); + + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + (*iter)->serialize(stream); + } +} + +void ScriptManager::deserializeControls(Common::SeekableReadStream *stream) { + uint32 numberOfControls = stream->readUint32LE(); + + for (uint32 i = 0; i < numberOfControls; ++i) { + uint32 key = stream->readUint32LE(); + for (ControlList::iterator iter = _activeControls.begin(); iter != _activeControls.end(); ++iter) { + if ((*iter)->getKey() == key) { + (*iter)->deserialize(stream); + break; + } + } + } +} + +Location ScriptManager::getCurrentLocation() const { + Location location = _currentLocation; + location.offset = _engine->getRenderManager()->getCurrentBackgroundOffset(); + + return location; +} + +} // End of namespace ZVision diff --git a/engines/zvision/scripting/script_manager.h b/engines/zvision/scripting/script_manager.h new file mode 100644 index 0000000000..ab9b03ed30 --- /dev/null +++ b/engines/zvision/scripting/script_manager.h @@ -0,0 +1,225 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef ZVISION_SCRIPT_MANAGER_H +#define ZVISION_SCRIPT_MANAGER_H + +#include "zvision/scripting/puzzle.h" +#include "zvision/scripting/control.h" + +#include "common/hashmap.h" +#include "common/queue.h" + + +namespace Common { +class String; +class SeekableReadStream; +} + +namespace ZVision { + +class ZVision; + +struct Location { + Location() : world('g'), room('a'), node('r'), view('y'), offset(0) {} + + char world; + char room; + char node; + char view; + uint32 offset; +}; + +typedef Common::HashMap<uint32, Common::Array<Puzzle *> > PuzzleMap; +typedef Common::List<Puzzle *> PuzzleList; +typedef Common::Queue<Puzzle *> PuzzleQueue; +typedef Common::List<Control *> ControlList; +typedef Common::HashMap<uint32, uint32> StateMap; +typedef Common::HashMap<uint32, uint> StateFlagMap; + +class ScriptManager { +public: + ScriptManager(ZVision *engine); + ~ScriptManager(); + +public: + enum StateFlags { + ONCE_PER_INST = 0x01, + DO_ME_NOW = 0x02, // Somewhat useless flag since anything that needs to be done immediately has no criteria + DISABLED = 0x04 + }; + +private: + ZVision *_engine; + /** + * Holds the global state variable. Do NOT directly modify this. Use the accessors and + * mutators getStateValue() and setStateValue(). This ensures that Puzzles that reference a + * particular state key are checked after the key is modified. + */ + StateMap _globalState; + /** + * Holds the flags for the global states. This is used to enable/disable puzzles and/or + * controls as well as which puzzles should are allowed to be re-executed + */ + StateFlagMap _globalStateFlags; + /** References _globalState keys to Puzzles */ + PuzzleMap _referenceTable; + /** Holds the Puzzles that should be checked this frame */ + PuzzleQueue _puzzlesToCheck; + /** Holds the currently active puzzles */ + PuzzleList _activePuzzles; + /** Holds the global puzzles */ + PuzzleList _globalPuzzles; + /** Holds the currently active controls */ + ControlList _activeControls; + + Location _currentLocation; + + uint32 _currentlyFocusedControl; + +public: + void initialize(); + void update(uint deltaTimeMillis); + + uint getStateValue(uint32 key); + void setStateValue(uint32 key, uint value); + void addToStateValue(uint32 key, uint valueToAdd); + + uint getStateFlags(uint32 key); + void setStateFlags(uint32 key, uint flags); + + void addControl(Control *control); + Control *getControl(uint32 key); + + void focusControl(uint32 key); + + /** + * Called when LeftMouse is pushed. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + */ + void onMouseDown(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + /** + * Called when LeftMouse is lifted. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + */ + void onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + /** + * Called on every MouseMove. + * + * @param screenSpacePos The position of the mouse in screen space + * @param backgroundImageSpacePos The position of the mouse in background image space + * @return Was the cursor changed? + */ + bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos); + /** + * Called when a key is pressed. + * + * @param keycode The key that was pressed + */ + void onKeyDown(Common::KeyState keyState); + /** + * Called when a key is released. + * + * @param keycode The key that was pressed + */ + void onKeyUp(Common::KeyState keyState); + + void changeLocation(char world, char room, char node, char view, uint32 offset); + + void serializeStateTable(Common::WriteStream *stream); + void deserializeStateTable(Common::SeekableReadStream *stream); + void serializeControls(Common::WriteStream *stream); + void deserializeControls(Common::SeekableReadStream *stream); + + Location getCurrentLocation() const; + +private: + void createReferenceTable(); + void updateNodes(uint deltaTimeMillis); + void checkPuzzleCriteria(); + void cleanStateTable(); + +// TODO: Make this private. It was only made public so Console::cmdParseAllScrFiles() could use it +public: + /** + * Parses a script file into triggers and events + * + * @param fileName Name of the .scr file + * @param isGlobal Are the puzzles included in the file global (true). AKA, the won't be purged during location changes + */ + void parseScrFile(const Common::String &fileName, bool isGlobal = false); + +private: + /** + * Parses the stream into a Puzzle object + * Helper method for parseScrFile. + * + * @param puzzle The object to store what is parsed + * @param stream Scr file stream + */ + void parsePuzzle(Puzzle *puzzle, Common::SeekableReadStream &stream); + + /** + * Parses the stream into a Criteria object + * Helper method for parsePuzzle. + * + * @param criteria Pointer to the Criteria object to fill + * @param stream Scr file stream + * @return Whether any criteria were read + */ + bool parseCriteria(Common::SeekableReadStream &stream, Common::List<Common::List<Puzzle::CriteriaEntry> > &criteriaList) const; + + /** + * Parses the stream into a ResultAction objects + * Helper method for parsePuzzle. + * + * @param stream Scr file stream + * @param actionList The list where the results will be added + * @return Created Results object + */ + void parseResults(Common::SeekableReadStream &stream, Common::List<ResultAction *> &actionList) const; + + /** + * Helper method for parsePuzzle. Parses the stream into a bitwise or of the StateFlags enum + * + * @param stream Scr file stream + * @return Bitwise OR of all the flags set within the puzzle + */ + uint parseFlags(Common::SeekableReadStream &stream) const; + + /** + * Helper method for parseScrFile. Parses the stream into a Control object + * + * @param line The line initially read + * @param stream Scr file stream + */ + void parseControl(Common::String &line, Common::SeekableReadStream &stream); +}; + + +} // End of namespace ZVision + +#endif |