File: pattable.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (188 lines) | stat: -rw-r--r-- 5,321 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * 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 "engines/stark/resources/pattable.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/script.h"
#include "engines/stark/resources/string.h"

#include "engines/stark/services/stateprovider.h"

#include "engines/stark/formats/xrc.h"

namespace Stark {
namespace Resources {

PATTable::~PATTable() {
}

PATTable::PATTable(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_defaultAction(-1),
		_tooltipOverrideIndex(-1) {
	_type = TYPE;
}

void PATTable::readData(Formats::XRCReadStream *stream) {
	uint32 entryCount = stream->readUint32LE();
	for (uint i = 0; i < entryCount; i++) {
		Entry entry;

		entry._actionType = stream->readSint32LE();
		entry._scriptIndex = stream->readSint32LE();
		entry._script = nullptr;

		_ownEntries.push_back(entry);
	}

	_defaultAction = stream->readSint32LE();
}

void PATTable::printData() {
	for (uint i = 0; i < _ownEntries.size(); i++) {
		debug("entry[%d].actionType: %d", i, _ownEntries[i]._actionType);
		debug("entry[%d].scriptIndex: %d", i, _ownEntries[i]._scriptIndex);
	}
	debug("defaultAction: %d", _defaultAction);
}

void PATTable::onAllLoaded() {
	Object::onAllLoaded();

	_itemEntries.clear();
	addOwnEntriesToItemEntries();
}

void PATTable::onEnterLocation() {
	Object::onEnterLocation();

	_itemEntries.clear();

	// Add our own entries to the list of available actions
	addOwnEntriesToItemEntries();

	// If the PAT's owning item has a template, find it
	ItemTemplate *itemTemplate = findItemTemplate();

	// Add the item template actions to the list of available actions
	if (itemTemplate) {
		PATTable *templatePAT = itemTemplate->findChild<PATTable>();

		Common::Array<Entry> templateEntries = templatePAT->listItemEntries();
		for (uint i = 0; i < templateEntries.size(); i++) {
			if (!_itemEntries.contains(templateEntries[i]._actionType)) {
				_itemEntries[templateEntries[i]._actionType] = templateEntries[i];
			}
		}
	}
}

void PATTable::saveLoad(ResourceSerializer *serializer) {
	serializer->syncAsSint32LE(_tooltipOverrideIndex);

	if (serializer->isLoading() && _tooltipOverrideIndex >= 0) {
		String *string = findChildWithIndex<String>(_tooltipOverrideIndex);
		setTooltip(string);
	}
}

ItemTemplate *PATTable::findItemTemplate() {
	Item *parent = findParent<Item>();

	ItemTemplate *itemTemplate = nullptr;
	if (parent->getSubType() == Item::kItemModel) {
		ModelItem *item = Object::cast<ModelItem>(parent);
		itemTemplate = item->getItemTemplate();

	} else if (parent->getSubType() == Item::kItemLevelTemplate) {
		LevelItemTemplate *item = Object::cast<LevelItemTemplate>(parent);
		itemTemplate = item->getItemTemplate();
	}

	return itemTemplate;
}

void PATTable::addOwnEntriesToItemEntries() {
	for (uint i = 0; i < _ownEntries.size(); i++) {
		if (_ownEntries[i]._scriptIndex != -1) {
			Entry entry = _ownEntries[i];
			entry._script = findChildWithIndex<Script>(_ownEntries[i]._scriptIndex);
			_itemEntries[entry._actionType] = entry;
		}
	}
}

Common::Array<PATTable::Entry> PATTable::listItemEntries() const {
	Common::Array<PATTable::Entry> entries;

	for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
		entries.push_back(it->_value);
	}

	return entries;
}

ActionArray PATTable::listPossibleActions() const {
	ActionArray actions;

	for (EntryMap::const_iterator it = _itemEntries.begin(); it != _itemEntries.end(); it++) {
		// Check the script can be launched
		if (it->_value._script->shouldExecute(Script::kCallModePlayerAction)) {
			actions.push_back(it->_key);
		}
	}

	return actions;
}

bool PATTable::canPerformAction(uint32 action) const {
	if (_itemEntries.contains(action)) {
		return _itemEntries[action]._script->shouldExecute(Script::kCallModePlayerAction);
	}

	return false;
}

int32 PATTable::getDefaultAction() const {
	if (_defaultAction != -1 && canPerformAction(_defaultAction)) {
		return _defaultAction;
	} else {
		return -1;
	}
}

bool PATTable::runScriptForAction(uint32 action) {
	if (_itemEntries.contains(action)) {
		_itemEntries[action]._script->execute(Script::kCallModePlayerAction);
		return true;
	}

	return false;
}

void PATTable::setTooltip(String *string) {
	_name = string->getName();
	_tooltipOverrideIndex = string->getIndex();
}

} // End of namespace Resources
} // End of namespace Stark