ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

pattable.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/resources/pattable.h"
00024 #include "engines/stark/resources/item.h"
00025 #include "engines/stark/resources/script.h"
00026 #include "engines/stark/resources/string.h"
00027 
00028 #include "engines/stark/services/stateprovider.h"
00029 
00030 #include "engines/stark/formats/xrc.h"
00031 
00032 namespace Stark {
00033 namespace Resources {
00034 
00035 PATTable::~PATTable() {
00036 }
00037 
00038 PATTable::PATTable(Object *parent, byte subType, uint16 index, const Common::String &name) :
00039         Object(parent, subType, index, name),
00040         _defaultAction(-1),
00041         _tooltipOverrideIndex(-1) {
00042     _type = TYPE;
00043 }
00044 
00045 void PATTable::readData(Formats::XRCReadStream *stream) {
00046     uint32 entryCount = stream->readUint32LE();
00047     for (uint i = 0; i < entryCount; i++) {
00048         Entry entry;
00049 
00050         entry._actionType = stream->readSint32LE();
00051         entry._scriptIndex = stream->readSint32LE();
00052         entry._script = nullptr;
00053 
00054         _ownEntries.push_back(entry);
00055     }
00056 
00057     _defaultAction = stream->readSint32LE();
00058 }
00059 
00060 void PATTable::printData() {
00061     for (uint i = 0; i < _ownEntries.size(); i++) {
00062         debug("entry[%d].actionType: %d", i, _ownEntries[i]._actionType);
00063         debug("entry[%d].scriptIndex: %d", i, _ownEntries[i]._scriptIndex);
00064     }
00065     debug("defaultAction: %d", _defaultAction);
00066 }
00067 
00068 void PATTable::onAllLoaded() {
00069     Object::onAllLoaded();
00070 
00071     _itemEntries.clear();
00072     addOwnEntriesToItemEntries();
00073 }
00074 
00075 void PATTable::onEnterLocation() {
00076     Object::onEnterLocation();
00077 
00078     _itemEntries.clear();
00079 
00080     // Add our own entries to the list of available actions
00081     addOwnEntriesToItemEntries();
00082 
00083     // If the PAT's owning item has a template, find it
00084     ItemTemplate *itemTemplate = findItemTemplate();
00085 
00086     // Add the item template actions to the list of available actions
00087     if (itemTemplate) {
00088         PATTable *templatePAT = itemTemplate->findChild<PATTable>();
00089 
00090         Common::Array<Entry> templateEntries = templatePAT->listItemEntries();
00091         for (uint i = 0; i < templateEntries.size(); i++) {
00092             if (!_itemEntries.contains(templateEntries[i]._actionType)) {
00093                 _itemEntries[templateEntries[i]._actionType] = templateEntries[i];
00094             }
00095         }
00096     }
00097 }
00098 
00099 void PATTable::saveLoad(ResourceSerializer *serializer) {
00100     serializer->syncAsSint32LE(_tooltipOverrideIndex);
00101 
00102     if (serializer->isLoading() && _tooltipOverrideIndex >= 0) {
00103         String *string = findChildWithIndex<String>(_tooltipOverrideIndex);
00104         setTooltip(string);
00105     }
00106 }
00107 
00108 ItemTemplate *PATTable::findItemTemplate() {
00109     Item *parent = findParent<Item>();
00110 
00111     ItemTemplate *itemTemplate = nullptr;
00112     if (parent->getSubType() == Item::kItemModel) {
00113         ModelItem *item = Object::cast<ModelItem>(parent);
00114         itemTemplate = item->getItemTemplate();
00115 
00116     } else if (parent->getSubType() == Item::kItemLevelTemplate) {
00117         LevelItemTemplate *item = Object::cast<LevelItemTemplate>(parent);
00118         itemTemplate = item->getItemTemplate();
00119     }
00120 
00121     return itemTemplate;
00122 }
00123 
00124 void PATTable::addOwnEntriesToItemEntries() {
00125     for (uint i = 0; i < _ownEntries.size(); i++) {
00126         if (_ownEntries[i]._scriptIndex != -1) {
00127             Entry entry = _ownEntries[i];
00128             entry._script = findChildWithIndex<Script>(_ownEntries[i]._scriptIndex);
00129             _itemEntries[entry._actionType] = entry;
00130         }
00131     }
00132 }
00133 
00134 Common::Array<PATTable::Entry> PATTable::listItemEntries() const {
00135     Common::Array<PATTable::Entry> entries;
00136 
00137     for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
00138         entries.push_back(it->_value);
00139     }
00140 
00141     return entries;
00142 }
00143 
00144 ActionArray PATTable::listPossibleActions() const {
00145     ActionArray actions;
00146 
00147     for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
00148         // Check the script can be launched
00149         if (it->_value._script->shouldExecute(Script::kCallModePlayerAction)) {
00150             actions.push_back(it->_key);
00151         }
00152     }
00153 
00154     return actions;
00155 }
00156 
00157 bool PATTable::canPerformAction(uint32 action) const {
00158     if (_itemEntries.contains(action)) {
00159         return _itemEntries[action]._script->shouldExecute(Script::kCallModePlayerAction);
00160     }
00161 
00162     return false;
00163 }
00164 
00165 int32 PATTable::getDefaultAction() const {
00166     if (_defaultAction != -1 && canPerformAction(_defaultAction)) {
00167         return _defaultAction;
00168     } else {
00169         return -1;
00170     }
00171 }
00172 
00173 bool PATTable::runScriptForAction(uint32 action) {
00174     if (_itemEntries.contains(action)) {
00175         _itemEntries[action]._script->execute(Script::kCallModePlayerAction);
00176         return true;
00177     }
00178 
00179     return false;
00180 }
00181 
00182 void PATTable::setTooltip(String *string) {
00183     _name = string->getName();
00184     _tooltipOverrideIndex = string->getIndex();
00185 }
00186 
00187 } // End of namespace Resources
00188 } // End of namespace Stark


Generated on Sat May 25 2019 05:00:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge