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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/* 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 "hdb/hdb.h"
namespace HDB {
/*
Adds a tile to an animation list
*/
void AI::addAnimateTarget(int x, int y, int start, int end, AnimSpeed speed, bool killAuto, bool inMap, const char *tileName) {
AnimTarget *at = new AnimTarget;
at->x = x;
at->y = y;
at->start = start;
at->end = end;
at->killAuto = killAuto; // Doesn't actually kill it, deactivates it
// Set animCycle and animFrame as per speed
switch (speed) {
case ANIM_SLOW:
at->animCycle = 10;
at->animFrame = 10;
break;
case ANIM_NORMAL:
at->animCycle = 6;
at->animFrame = 6;
break;
case ANIM_FAST:
at->animCycle = 2;
at->animFrame = 2;
break;
}
// Set +1/-1 for tile anim direction
if ((end - start) > 0)
at->vel = 1;
else
at->vel = -1;
// Set Info if this is not an inMap animation
at->inMap = inMap;
if (!inMap) {
char name[32];
uint32 size;
for (int i = start;i <= end;i++) {
if (i < 10)
snprintf(name, 32, "%s0%d", tileName, i + 1);
else
snprintf(name, 32, "%s%d", tileName, i + 1);
debug("AddAnimateTarget: %s", name);
size = g_hdb->_fileMan->getLength(name, TYPE_TILE32);
at->gfxList[i] = g_hdb->_drawMan->getTileGfx(name, size);
}
}
// Insert in the beginning
_animTargets->insert_at(0, at);
}
/*
Animate _animTargets
Called every frame
*/
void AI::animateTargets() {
AnimTarget *at;
int mx, my;
int layer;
g_hdb->_map->getMapXY(&mx, &my);
for (Common::Array<AnimTarget *>::iterator it = _animTargets->begin(); it != _animTargets->end(); it++) {
at = *it;
// Draw Non-Map stuff every frame
if (!at->inMap)
at->gfxList[at->start]->drawMasked(at->x - mx, at->y - my);
// Frame Timer
if (at->animFrame-- < 1) {
at->animFrame = at->animCycle;
if (at->inMap) {
// Animate Map Tiles
layer = 0; // BG layer
warning("FIXME: Ambigious animateTargets() code.");
if (!at->start == g_hdb->_map->getMapBGTileIndex(at->x, at->y))
layer = 1;
// Change Tile Anim
at->start += at->vel;
// Set it back in map
if (!layer)
g_hdb->_map->setMapBGTileIndex(at->x, at->y, at->start);
else
g_hdb->_map->setMapFGTileIndex(at->x, at->y, at->start);
} else {
// Animate non-map tiles
at->start++;
}
// Animation Finished ?
if (at->start == at->end) {
if (at->killAuto)
autoDeactivate(at->x, at->y);
_animTargets->erase(it);
continue;
}
}
}
}
// Add an action location to the list of possible actions
// Each action must be paired with another of the same number
void AI::addToActionList(int actionIndex, int x, int y, char *luaFuncInt, char *luaFuncUse) {
if (!_actions[actionIndex].x1) {
_actions[actionIndex].x1 = x;
_actions[actionIndex].y1 = y;
if (luaFuncInt[0] != '*')
strcpy(_actions[actionIndex].luaFuncInit, luaFuncInt);
if (luaFuncInt[0] != '*')
strcpy(_actions[actionIndex].luaFuncUse, luaFuncUse);
if (_actions[actionIndex].luaFuncInit[0]) {
g_hdb->_lua->callFunction(_actions[actionIndex].luaFuncInit, 2);
strcpy(_actions[actionIndex].entityName, g_hdb->_lua->getStringOffStack());
strcpy(_actions[actionIndex].entityName, g_hdb->_lua->getStringOffStack());
}
return;
}
if (!_actions[actionIndex].x2) {
_actions[actionIndex].x2 = x;
_actions[actionIndex].y2 = y;
if (luaFuncInt[0] != '*')
strcpy(_actions[actionIndex].luaFuncInit, luaFuncInt);
if (luaFuncInt[0] != '*')
strcpy(_actions[actionIndex].luaFuncUse, luaFuncUse);
if (_actions[actionIndex].luaFuncInit[0]) {
g_hdb->_lua->callFunction(_actions[actionIndex].luaFuncInit, 2);
strcpy(_actions[actionIndex].entityName, g_hdb->_lua->getStringOffStack());
strcpy(_actions[actionIndex].entityName, g_hdb->_lua->getStringOffStack());
}
return;
}
warning("Adding a 3rd action to ACTION-%d is illegal", actionIndex);
}
// Checks if the location passed-in matches an action pair.
// If so, activate it if possible. Returns TRUE for finding pair.
bool AI::checkActionList(AIEntity *e, int x, int y, bool lookAndGrab) {
for (int i = 0;i < kMaxActions;i++) {
if ((_actions[i].x1 == x && _actions[i].y1 == y) || (_actions[i].x2 == x && _actions[i].y2 == y)) {
int targetX = _actions[i].x2;
int targetY = _actions[i].y2;
bool success;
// Choose target co-ordinates
if (x == targetX && y == targetY) {
targetX = _actions[i].x1;
targetY = _actions[i].y1;
}
// Is this an actual switch?
uint32 flags = g_hdb->_map->getMapFGTileFlags(x, y);
if (!flags)
flags = g_hdb->_map->getMapBGTileFlags(x, y);
if (!(flags & kFlagSolid) && (_player->tileX != x && _player->tileY != y))
return false;
// Closing on something?
if (findEntity(targetX, targetY))
return false;
success = activateAction(e, x, y, targetX, targetY);
// If successful, remove action from list
if (success) {
_actions[i].x1 = _actions[i].y1 = _actions[i].x2 = _actions[i].y2 = 0;
// Call Lua Use function if it exists
if (_actions[i].luaFuncUse[0])
g_hdb->_lua->callFunction(_actions[i].luaFuncUse, 0);
} else if (e == _player && !checkForTouchplate(x, y))
addWaypoint(e->tileX, e->tileY, x, y, e->level);
if (lookAndGrab && e == _player) {
lookAtXY(x, y);
animGrabbing();
}
return true;
}
}
return false;
}
void AI::addToAutoList(int x, int y, const char *luaFuncInit, const char *luaFuncUse) {
const char *get;
for (int i = 0;i < kMaxAutoActions;i++) {
if (!_autoActions[i].x) {
_autoActions[i].x = x;
_autoActions[i].y = y;
_autoActions[i].activated = false;
if (luaFuncInit[0] != '*')
strcpy(&_autoActions[i].luaFuncInit[0], luaFuncInit);
if (luaFuncUse[0] != '*')
strcpy(&_autoActions[i].luaFuncUse[0], luaFuncUse);
if (_autoActions[i].luaFuncInit[0]) {
g_hdb->_lua->callFunction(_autoActions[i].luaFuncInit, 2);
get = g_hdb->_lua->getStringOffStack();
if (!get)
return;
strcpy(&_autoActions[i].entityName[0], get);
get = g_hdb->_lua->getStringOffStack();
if (!get)
return;
strcpy(&_autoActions[i].entityName[0], get);
}
return;
}
}
}
void AI::autoDeactivate(int x, int y) {
for (int i = 0; i < kMaxAutoActions;i++) {
if (_autoActions[i].x == x && _autoActions[i].y == y) {
_autoActions[i].activated = false;
return;
}
}
}
bool AI::activateAction(AIEntity *e, int x, int y, int targetX, int targetY) {
bool success = false;
int tileIndex = g_hdb->_map->getMapFGTileIndex(x, y);
// If FG tile invisivle or grating, ignore if
int fgFlags = g_hdb->_map->getMapFGTileFlags(x, y);
if (fgFlags & (kFlagInvisible | kFlagGrating))
tileIndex = -1;
if (tileIndex < 0)
tileIndex = g_hdb->_map->getMapBGTileIndex(x, y);
// Check which tile is going to activate
if ( tileIndex == _useSwitchOff || tileIndex == _useSwitchOff + 1 )
success = useSwitch( e, x, y, targetX, targetY, _useSwitchOn );
else
if ( tileIndex == _useSwitchOn )
success = useSwitchOn( e, x, y, targetX, targetY, _useSwitchOff );
else
//-------------------------------------------------------------------
if ( tileIndex == _useHandswitchOff || tileIndex == _useHandswitchOff + 1 )
success = useSwitch( e, x, y, targetX, targetY, _useHandswitchOn );
else
if ( tileIndex == _useHandswitchOn )
success = useSwitchOn( e, x, y, targetX, targetY, _useHandswitchOff );
else
//-------------------------------------------------------------------
if ( tileIndex == _kcHolderWhiteOff || tileIndex == _kcHolderWhiteOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderWhiteOn, ITEM_KEYCARD_WHITE, "I need a White Keycard." );
else
if ( tileIndex == _kcHolderWhiteOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderWhiteOff, ITEM_KEYCARD_WHITE );
else
if ( tileIndex == _kcHolderBlueOff || tileIndex == _kcHolderBlueOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderBlueOn, ITEM_KEYCARD_BLUE, "I need a Blue Keycard." );
else
if ( tileIndex == _kcHolderBlueOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderBlueOff, ITEM_KEYCARD_BLUE );
else
if ( tileIndex == _kcHolderRedOff || tileIndex == _kcHolderRedOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderRedOn, ITEM_KEYCARD_RED, "I need a Red Keycard." );
else
if ( tileIndex == _kcHolderRedOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderRedOff, ITEM_KEYCARD_RED );
else
if ( tileIndex == _kcHolderGreenOff || tileIndex == _kcHolderGreenOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderGreenOn, ITEM_KEYCARD_GREEN, "I need a Green Keycard." );
else
if ( tileIndex == _kcHolderGreenOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderGreenOff, ITEM_KEYCARD_GREEN );
else
if ( tileIndex == _kcHolderPurpleOff || tileIndex == _kcHolderPurpleOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderPurpleOn, ITEM_KEYCARD_PURPLE, "I need a Purple Keycard." );
else
if ( tileIndex == _kcHolderPurpleOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderPurpleOff, ITEM_KEYCARD_PURPLE );
else
if ( tileIndex == _kcHolderBlackOff || tileIndex == _kcHolderBlackOff + 1 )
success = useLockedSwitch( e, x, y, targetX, targetY, _kcHolderBlackOn, ITEM_KEYCARD_BLACK, "I need a Black Keycard." );
else
if ( tileIndex == _kcHolderBlackOn )
success = useLockedSwitchOn( e, x, y, targetX, targetY, _kcHolderBlackOff, ITEM_KEYCARD_BLACK );
else
//-------------------------------------------------------------------
if ( tileIndex == _useSwitch2Off || tileIndex == _useSwitch2Off + 1 )
success = useSwitch2( e, x, y, targetX, targetY );
else
if ( tileIndex == _useHolderEmpty || tileIndex == _useHolderEmpty + 1 )
success = useCellHolder( e, x, y, targetX, targetY );
else
//-------------------------------------------------------------------
if ( tileIndex == _targetDoorN || tileIndex == _targetDoorN + 3 )
success = useAutoDoorOpenClose( e, x, y );
else
if ( tileIndex == _targetDoorP || tileIndex == _targetDoorP + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorS || tileIndex == _targetDoorS + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorNv || tileIndex == _targetDoorNv + 3 )
success = useAutoDoorOpenClose( e, x, y );
else
if ( tileIndex == _targetDoorPv || tileIndex == _targetDoorPv + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorSv || tileIndex == _targetDoorSv + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
//-------------------------------------------------------------------
if ( tileIndex == _targetDoorN || tileIndex == _targetDoor2N + 3 )
success = useAutoDoorOpenClose( e, x, y );
else
if ( tileIndex == _targetDoorP || tileIndex == _targetDoor2P + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorS || tileIndex == _targetDoor2S + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorNv || tileIndex == _targetDoor2Nv + 3 )
success = useAutoDoorOpenClose( e, x, y );
else
if ( tileIndex == _targetDoorPv || tileIndex == _targetDoor2Pv + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
if ( tileIndex == _targetDoorSv || tileIndex == _targetDoor2Sv + 3 )
success = useDoorOpenCloseBot( e, x, y );
else
//-------------------------------------------------------------------
if ( tileIndex == _touchplateOff )
success = useTouchplate( e, x, y, targetX, targetY, _touchplateOn );
else
if ( tileIndex == _touchplateOn )
success = useTouchplateOn( e, x, y, targetX, targetY, _touchplateOff );
else
if ( tileIndex == _templeTouchpOff )
success = useTouchplate( e, x, y, targetX, targetY, _templeTouchpOn );
else
if ( tileIndex == _templeTouchpOn )
success = useTouchplateOn( e, x, y, targetX, targetY, _templeTouchpOff );
return success;
}
bool AI::checkAutoList(AIEntity *e, int x, int y) {
for (int i = 0;i < kMaxAutoActions;i++) {
if (_autoActions[i].x == x && _autoActions[i].y == y && !_autoActions[i].activated) {
debug(1, "Activating action for Entity: %s, x: %d, y: %d", e->entityName, x, y);
bool success = activateAction(e, x, y, 0, 0);
_autoActions[i].activated = true;
if (success && _autoActions[i].luaFuncUse[0])
g_hdb->_lua->callFunction(_autoActions[i].luaFuncUse, 0);
if (e == _player) {
lookAtXY(x, y);
animGrabbing();
}
return true;
}
}
return false;
}
bool AI::autoActive(int x, int y) {
for (int i = 0;i < kMaxAutoActions;i++) {
if (_autoActions[i].x == x && _autoActions[i].y == y) {
if (!_autoActions[i].activated)
return false;
return true;
}
}
return false;
}
} // End of Namespace
|