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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* 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.
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#include "engines/wintermute/dcgf.h"
#include "engines/wintermute/Ad/AdTalkHolder.h"
#include "engines/wintermute/Base/BDynBuffer.h"
#include "engines/wintermute/Base/scriptables/ScValue.h"
#include "engines/wintermute/Base/scriptables/ScScript.h"
#include "engines/wintermute/Base/scriptables/ScStack.h"
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/Base/BSprite.h"
#include "engines/wintermute/PlatformSDL.h"
#include "engines/wintermute/wintermute.h"
#include "common/str.h"
namespace WinterMute {
IMPLEMENT_PERSISTENT(CAdTalkHolder, false)
//////////////////////////////////////////////////////////////////////////
CAdTalkHolder::CAdTalkHolder(CBGame *inGame): CAdObject(inGame) {
_sprite = NULL;
}
//////////////////////////////////////////////////////////////////////////
CAdTalkHolder::~CAdTalkHolder() {
delete _sprite;
_sprite = NULL;
for (int i = 0; i < _talkSprites.getSize(); i++)
delete _talkSprites[i];
_talkSprites.removeAll();
for (int i = 0; i < _talkSpritesEx.getSize(); i++)
delete _talkSpritesEx[i];
_talkSpritesEx.removeAll();
}
//////////////////////////////////////////////////////////////////////////
CBSprite *CAdTalkHolder::getTalkStance(const char *stance) {
CBSprite *ret = NULL;
// forced stance?
if (_forcedTalkAnimName && !_forcedTalkAnimUsed) {
_forcedTalkAnimUsed = true;
delete _animSprite;
_animSprite = new CBSprite(_gameRef, this);
if (_animSprite) {
ERRORCODE res = _animSprite->loadFile(_forcedTalkAnimName);
if (DID_FAIL(res)) {
_gameRef->LOG(res, "CAdTalkHolder::GetTalkStance: error loading talk sprite (object:\"%s\" sprite:\"%s\")", _name, _forcedTalkAnimName);
delete _animSprite;
_animSprite = NULL;
} else return _animSprite;
}
}
if (stance != NULL) {
// search special talk stances
for (int i = 0; i < _talkSpritesEx.getSize(); i++) {
if (scumm_stricmp(_talkSpritesEx[i]->_name, stance) == 0) {
ret = _talkSpritesEx[i];
break;
}
}
if (ret == NULL) {
// serach generic talk stances
for (int i = 0; i < _talkSprites.getSize(); i++) {
if (scumm_stricmp(_talkSprites[i]->_name, stance) == 0) {
ret = _talkSprites[i];
break;
}
}
}
}
// not a valid stance? get a random one
if (ret == NULL) {
if (_talkSprites.getSize() < 1) ret = _sprite;
else {
// TODO: remember last
int rnd = g_wintermute->randInt(0, _talkSprites.getSize() - 1);
ret = _talkSprites[rnd];
}
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
ERRORCODE CAdTalkHolder::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) {
//////////////////////////////////////////////////////////////////////////
// SetSprite
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "SetSprite") == 0) {
stack->correctParams(1);
CScValue *val = stack->pop();
bool setCurrent = false;
if (_currentSprite && _currentSprite == _sprite) setCurrent = true;
delete _sprite;
_sprite = NULL;
if (val->isNULL()) {
_sprite = NULL;
if (setCurrent) _currentSprite = NULL;
stack->pushBool(true);
} else {
const char *filename = val->getString();
CBSprite *spr = new CBSprite(_gameRef, this);
if (!spr || DID_FAIL(spr->loadFile(filename))) {
script->runtimeError("SetSprite method failed for file '%s'", filename);
stack->pushBool(false);
} else {
_sprite = spr;
if (setCurrent) _currentSprite = _sprite;
stack->pushBool(true);
}
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetSprite
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetSprite") == 0) {
stack->correctParams(0);
if (!_sprite || !_sprite->_filename) stack->pushNULL();
else stack->pushString(_sprite->_filename);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetSpriteObject
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetSpriteObject") == 0) {
stack->correctParams(0);
if (!_sprite) stack->pushNULL();
else stack->pushNative(_sprite, true);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// AddTalkSprite
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "AddTalkSprite") == 0) {
stack->correctParams(2);
const char *filename = stack->pop()->getString();
bool Ex = stack->pop()->getBool();
CBSprite *spr = new CBSprite(_gameRef, this);
if (!spr || DID_FAIL(spr->loadFile(filename))) {
stack->pushBool(false);
script->runtimeError("AddTalkSprite method failed for file '%s'", filename);
} else {
if (Ex) _talkSpritesEx.add(spr);
else _talkSprites.add(spr);
stack->pushBool(true);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// RemoveTalkSprite
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "RemoveTalkSprite") == 0) {
stack->correctParams(2);
const char *filename = stack->pop()->getString();
bool ex = stack->pop()->getBool();
bool setCurrent = false;
bool setTemp2 = false;
if (ex) {
for (int i = 0; i < _talkSpritesEx.getSize(); i++) {
if (scumm_stricmp(_talkSpritesEx[i]->_filename, filename) == 0) {
if (_currentSprite == _talkSpritesEx[i])
setCurrent = true;
if (_tempSprite2 == _talkSpritesEx[i])
setTemp2 = true;
delete _talkSpritesEx[i];
_talkSpritesEx.removeAt(i);
break;
}
}
} else {
for (int i = 0; i < _talkSprites.getSize(); i++) {
if (scumm_stricmp(_talkSprites[i]->_filename, filename) == 0) {
if (_currentSprite == _talkSprites[i])
setCurrent = true;
if (_tempSprite2 == _talkSprites[i])
setTemp2 = true;
delete _talkSprites[i];
_talkSprites.removeAt(i);
break;
}
}
}
stack->pushBool(true);
if (setCurrent)
_currentSprite = _sprite;
if (setTemp2)
_tempSprite2 = _sprite;
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetTalkSprite
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetTalkSprite") == 0) {
stack->correctParams(2);
const char *filename = stack->pop()->getString();
bool ex = stack->pop()->getBool();
bool setCurrent = false;
bool setTemp2 = false;
CBSprite *spr = new CBSprite(_gameRef, this);
if (!spr || DID_FAIL(spr->loadFile(filename))) {
stack->pushBool(false);
script->runtimeError("SetTalkSprite method failed for file '%s'", filename);
} else {
// delete current
if (ex) {
for (int i = 0; i < _talkSpritesEx.getSize(); i++) {
if (_talkSpritesEx[i] == _currentSprite)
setCurrent = true;
if (_talkSpritesEx[i] == _tempSprite2)
setTemp2 = true;
delete _talkSpritesEx[i];
}
_talkSpritesEx.removeAll();
} else {
for (int i = 0; i < _talkSprites.getSize(); i++) {
if (_talkSprites[i] == _currentSprite) setCurrent = true;
if (_talkSprites[i] == _tempSprite2) setTemp2 = true;
delete _talkSprites[i];
}
_talkSprites.removeAll();
}
// set new
if (ex)
_talkSpritesEx.add(spr);
else
_talkSprites.add(spr);
stack->pushBool(true);
if (setCurrent)
_currentSprite = spr;
if (setTemp2)
_tempSprite2 = spr;
}
return STATUS_OK;
}
else return CAdObject::scCallMethod(script, stack, thisStack, name);
}
//////////////////////////////////////////////////////////////////////////
CScValue *CAdTalkHolder::scGetProperty(const char *name) {
_scValue->setNULL();
//////////////////////////////////////////////////////////////////////////
// Type (RO)
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "Type") == 0) {
_scValue->setString("talk-holder");
return _scValue;
}
else return CAdObject::scGetProperty(name);
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CAdTalkHolder::scSetProperty(const char *name, CScValue *value) {
/*
//////////////////////////////////////////////////////////////////////////
// Item
//////////////////////////////////////////////////////////////////////////
if(strcmp(name, "Item")==0){
SetItem(value->getString());
return STATUS_OK;
}
else*/ return CAdObject::scSetProperty(name, value);
}
//////////////////////////////////////////////////////////////////////////
const char *CAdTalkHolder::scToString() {
return "[talk-holder object]";
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CAdTalkHolder::saveAsText(CBDynBuffer *buffer, int indent) {
for (int i = 0; i < _talkSprites.getSize(); i++) {
if (_talkSprites[i]->_filename)
buffer->putTextIndent(indent + 2, "TALK=\"%s\"\n", _talkSprites[i]->_filename);
}
for (int i = 0; i < _talkSpritesEx.getSize(); i++) {
if (_talkSpritesEx[i]->_filename)
buffer->putTextIndent(indent + 2, "TALK_SPECIAL=\"%s\"\n", _talkSpritesEx[i]->_filename);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CAdTalkHolder::persist(CBPersistMgr *persistMgr) {
CAdObject::persist(persistMgr);
persistMgr->transfer(TMEMBER(_sprite));
_talkSprites.persist(persistMgr);
_talkSpritesEx.persist(persistMgr);
return STATUS_OK;
}
} // end of namespace WinterMute
|