1 /* ResidualVM - A 3D game interpreter
2 *
3 * ResidualVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the AUTHORS
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "engines/stark/resources/pattable.h"
24 #include "engines/stark/resources/item.h"
25 #include "engines/stark/resources/script.h"
26 #include "engines/stark/resources/string.h"
27
28 #include "engines/stark/services/stateprovider.h"
29
30 #include "engines/stark/formats/xrc.h"
31
32 namespace Stark {
33 namespace Resources {
34
~PATTable()35 PATTable::~PATTable() {
36 }
37
PATTable(Object * parent,byte subType,uint16 index,const Common::String & name)38 PATTable::PATTable(Object *parent, byte subType, uint16 index, const Common::String &name) :
39 Object(parent, subType, index, name),
40 _defaultAction(-1),
41 _tooltipOverrideIndex(-1) {
42 _type = TYPE;
43 }
44
readData(Formats::XRCReadStream * stream)45 void PATTable::readData(Formats::XRCReadStream *stream) {
46 uint32 entryCount = stream->readUint32LE();
47 for (uint i = 0; i < entryCount; i++) {
48 Entry entry;
49
50 entry._actionType = stream->readSint32LE();
51 entry._scriptIndex = stream->readSint32LE();
52 entry._script = nullptr;
53
54 _ownEntries.push_back(entry);
55 }
56
57 _defaultAction = stream->readSint32LE();
58 }
59
printData()60 void PATTable::printData() {
61 for (uint i = 0; i < _ownEntries.size(); i++) {
62 debug("entry[%d].actionType: %d", i, _ownEntries[i]._actionType);
63 debug("entry[%d].scriptIndex: %d", i, _ownEntries[i]._scriptIndex);
64 }
65 debug("defaultAction: %d", _defaultAction);
66 }
67
onAllLoaded()68 void PATTable::onAllLoaded() {
69 Object::onAllLoaded();
70
71 _itemEntries.clear();
72 addOwnEntriesToItemEntries();
73 }
74
onEnterLocation()75 void PATTable::onEnterLocation() {
76 Object::onEnterLocation();
77
78 _itemEntries.clear();
79
80 // Add our own entries to the list of available actions
81 addOwnEntriesToItemEntries();
82
83 // If the PAT's owning item has a template, find it
84 ItemTemplate *itemTemplate = findItemTemplate();
85
86 // Add the item template actions to the list of available actions
87 if (itemTemplate) {
88 PATTable *templatePAT = itemTemplate->findChild<PATTable>();
89
90 Common::Array<Entry> templateEntries = templatePAT->listItemEntries();
91 for (uint i = 0; i < templateEntries.size(); i++) {
92 if (!_itemEntries.contains(templateEntries[i]._actionType)) {
93 _itemEntries[templateEntries[i]._actionType] = templateEntries[i];
94 }
95 }
96 }
97 }
98
saveLoad(ResourceSerializer * serializer)99 void PATTable::saveLoad(ResourceSerializer *serializer) {
100 serializer->syncAsSint32LE(_tooltipOverrideIndex);
101
102 if (serializer->isLoading() && _tooltipOverrideIndex >= 0) {
103 String *string = findChildWithIndex<String>(_tooltipOverrideIndex);
104 setTooltip(string);
105 }
106 }
107
findItemTemplate()108 ItemTemplate *PATTable::findItemTemplate() {
109 Item *parent = findParent<Item>();
110
111 ItemTemplate *itemTemplate = nullptr;
112 if (parent->getSubType() == Item::kItemModel) {
113 ModelItem *item = Object::cast<ModelItem>(parent);
114 itemTemplate = item->getItemTemplate();
115
116 } else if (parent->getSubType() == Item::kItemLevelTemplate) {
117 LevelItemTemplate *item = Object::cast<LevelItemTemplate>(parent);
118 itemTemplate = item->getItemTemplate();
119 }
120
121 return itemTemplate;
122 }
123
addOwnEntriesToItemEntries()124 void PATTable::addOwnEntriesToItemEntries() {
125 for (uint i = 0; i < _ownEntries.size(); i++) {
126 if (_ownEntries[i]._scriptIndex != -1) {
127 Entry entry = _ownEntries[i];
128 entry._script = findChildWithIndex<Script>(_ownEntries[i]._scriptIndex);
129 _itemEntries[entry._actionType] = entry;
130 }
131 }
132 }
133
listItemEntries() const134 Common::Array<PATTable::Entry> PATTable::listItemEntries() const {
135 Common::Array<PATTable::Entry> entries;
136
137 for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
138 entries.push_back(it->_value);
139 }
140
141 return entries;
142 }
143
listPossibleActions() const144 ActionArray PATTable::listPossibleActions() const {
145 ActionArray actions;
146
147 for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
148 // Check the script can be launched
149 if (it->_value._script->shouldExecute(Script::kCallModePlayerAction)) {
150 actions.push_back(it->_key);
151 }
152 }
153
154 return actions;
155 }
156
canPerformAction(uint32 action) const157 bool PATTable::canPerformAction(uint32 action) const {
158 if (_itemEntries.contains(action)) {
159 return _itemEntries[action]._script->shouldExecute(Script::kCallModePlayerAction);
160 }
161
162 return false;
163 }
164
getDefaultAction() const165 int32 PATTable::getDefaultAction() const {
166 if (_defaultAction != -1 && canPerformAction(_defaultAction)) {
167 return _defaultAction;
168 } else {
169 return -1;
170 }
171 }
172
runScriptForAction(uint32 action)173 bool PATTable::runScriptForAction(uint32 action) {
174 if (_itemEntries.contains(action)) {
175 _itemEntries[action]._script->execute(Script::kCallModePlayerAction);
176 return true;
177 }
178
179 return false;
180 }
181
setTooltip(String * string)182 void PATTable::setTooltip(String *string) {
183 _name = string->getName();
184 _tooltipOverrideIndex = string->getIndex();
185 }
186
187 } // End of namespace Resources
188 } // End of namespace Stark
189