aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/true_talk/tt_script_base.cpp
blob: 5162f4dde64b3d238696d21d949b692cec2ef1df (plain)
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
/* 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/textconsole.h"
#include "titanic/true_talk/tt_script_base.h"
#include "titanic/titanic.h"

namespace Titanic {

TTscriptBase::TTscriptBase(int scriptId, const char *charClass, int state,
		const char *charName, int v3, int v4, int v5, int v6, int v7) :
		_charName(charName), _charClass(charClass), _status(0),
		_nodesP(nullptr), _id(0), _hist1P(nullptr),
		_field20(0), _field24(0), _field28(0), _field2C(0),
		_field30(0), _state(0), _hist2P(nullptr), _field3C(0),
		_respHeadP(nullptr), _respTailP(nullptr), _oldResponseP(nullptr) {
	if (isValid()) {
		if (!v7 || !getStatus()) {
			_id = scriptId;
			_field20 = v3;
			_field24 = v4;
			_field28 = v5;
			_field2C = v6;
			_field30 = v7;
			_state = state;
		} else {
			_status = SS_5;
		}
	}

	if (_status)
		reset();
}

TTscriptBase::~TTscriptBase() {
	deleteResponses();
	delete _oldResponseP;

	delete _hist1P;
	delete _hist2P;

	if (_nodesP) {
		_nodesP->deleteSiblings();
		delete _nodesP;
	}
}

bool TTscriptBase::isValid() {
	_status = SS_VALID;
	return true;
}

void TTscriptBase::reset() {
	_nodesP = nullptr;
	_id = 4;
	_hist1P = nullptr;
	_field20 = 0;
	_field24 = -1;
	_field28 = -1;
	_field2C = -1;
	_field30 = 0;
	_state = 0;
	_hist2P = nullptr;
	_field3C = 0;
	_respHeadP = nullptr;
	_respTailP = nullptr;
	_oldResponseP = nullptr;
}

int TTscriptBase::scriptPreprocess(TTsentence *sentence) {
	delete _hist1P;
	_hist1P = new TTscriptHist(sentence);

	return _hist1P ? SS_VALID : SS_7;
}

void TTscriptBase::addResponse(const TTstring &str) {
	appendResponse2(-1, nullptr, str);
}

void TTscriptBase::addResponse(int id) {
	appendResponse(-1, nullptr, id);
}

void TTscriptBase::applyResponse() {
	delete _oldResponseP;
	_oldResponseP = nullptr;

	if (_respHeadP) {
		g_vm->_scriptHandler->setResponse(this, _respHeadP);
		_oldResponseP = _respHeadP->copyChain();
		TTresponse *oldRespP = _respHeadP;
		_respHeadP = _respHeadP->getLink();
		_respTailP = nullptr;

		delete oldRespP;
	}
}

void TTscriptBase::deleteResponses() {
	while (_respHeadP) {
		_respTailP = _respHeadP;
		_respHeadP = _respTailP->getLink();
		delete _respTailP;
	}
}

void TTscriptBase::appendResponse(int index, int *maxP, int id) {
	if (id && (!maxP || index <= *maxP)) {
		if (_respTailP) {
			// Prior fragments already exist, so append to end of chain
			_respTailP = _respTailP->appendResponse(id);
		} else {
			// Currently no tail
			_respTailP = new TTresponse(id, 3);
			if (_respHeadP)
				_respHeadP->addLink(_respTailP);
			else
				_respHeadP = _respTailP;
		}
	}
}

void TTscriptBase::appendResponse(int index, int *maxP, const TTstring &str) {
	if (!maxP || index <= *maxP) {
		if (_respTailP) {
			// Prior fragments already exist, so append to end of chain
			_respTailP = new TTresponse(str);
		} else {
			// Currently no tail
			_respTailP = new TTresponse(str);
			if (_respHeadP)
				_respHeadP->addLink(_respTailP);
			else
				_respHeadP = _respTailP;
		}
	}
}

} // End of namespace Titanic