File: motion.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (320 lines) | stat: -rw-r--r-- 10,390 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
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "agi/agi.h"
#include "common/random.h"

namespace Agi {

int AgiEngine::checkStep(int delta, int step) {
	return (-step >= delta) ? 0 : (step <= delta) ? 2 : 1;
}

bool AgiEngine::checkBlock(int16 x, int16 y) {
	if (x <= _game.block.x1 || x >= _game.block.x2)
		return false;

	if (y <= _game.block.y1 || y >= _game.block.y2)
		return false;

	return true;
}

void AgiEngine::changePos(ScreenObjEntry *screenObj) {
	const int dx[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
	const int dy[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };

	int16 x = screenObj->xPos;
	int16 y = screenObj->yPos;
	bool insideBlock = checkBlock(x, y);

	x += screenObj->stepSize * dx[screenObj->direction];
	y += screenObj->stepSize * dy[screenObj->direction];

	if (checkBlock(x, y) == insideBlock) {
		screenObj->flags &= ~fMotion;
	} else {
		screenObj->flags |= fMotion;
		screenObj->direction = 0;
		if (isEgoView(screenObj))
			setVar(VM_VAR_EGO_DIRECTION, 0);
	}
}

// WORKAROUND:
// Overwrite cycler state with motion data as original AGI did, almost.
//
// The original AGI interpreter stored motion and cycler data in the same four
// bytes of the screen object struct. If a script activated a motion while a
// cycler is active, or the opposite, then the previous action's state was
// overwritten. Some game scripts rely on this behavior, although probably
// unintentionally. We store motion and cycler data in separate fields, so
// in order to produce the original behavior that games depend on, we implement
// the overwrite behavior.
//
// However, we make an exception: when cycler data is overwritten, the original
// would set an unintended game flag when it completed. It doesn't seem like
// anything good can come from setting an unintended flag, so we do not set any
// flag when an overwritten cycler completes.
//
// This affects at least:
// - KQ1: when the eagle grabs ego (room 22), Bug #7046
// - BC:  when the witches disappear at the end of the game (room 12, screen object 13)
// - DDP: introduction when the ducks jump, Bug #14170
// - KQ2: happened somewhere in the game, LordHoto couldn't remember exactly where
void AgiEngine::motionActivated(ScreenObjEntry *screenObj) {
	if (screenObj->flags & fCycling) { // Is a cycler active?
		switch (screenObj->cycle) {
		case kCycleEndOfLoop: // "end.of.loop"
		case kCycleRevLoop:   // "reverse.loop"
			// This would have overwritten the cycler's flag with wander_count
			// or follow_stepSize or move_x, and that would cause the cycler
			// to set an unintended flag if it completes.
			// We skip setting the unintended flag with ignoreLoopFlag.
			// Jumping at the eagle in KQ1 room 22 depends on the overwritten
			// flag not being set. Bug #7046
			screenObj->ignoreLoopFlag = true;
			warning("Motion activated for screen object %d while cycler is active", screenObj->objectNr);
			warning("Original AGI would overwrite flag %d, we skip setting it", screenObj->loop_flag);
			break;
		default:
			break;
		}
	}
}

// WORKAROUND:
// Overwrite motion state with cycler data as original AGI did.
// This is necessary because we use a different data structure than the
// original, but games relied on undefined behavior when activating a
// cycler while a motion was in progress.
// See comment for motionActivated()
void AgiEngine::cyclerActivated(ScreenObjEntry *screenObj) {
	const char *fieldName;
	uint8 previousValue;
	switch (screenObj->motionType) {
	case kMotionWander:
		// Overwrite wander count with the cycler's flag.
		fieldName = "wander_count";
		previousValue = screenObj->wander_count;
		screenObj->wander_count = screenObj->loop_flag;
		break;
	case kMotionFollowEgo:
		// Overwrite follow step size with the cycler's flag.
		fieldName = "follow_stepSize";
		previousValue = screenObj->follow_stepSize;
		screenObj->follow_stepSize = screenObj->loop_flag;
		break;
	case kMotionMoveObj:
		// Overwrite move_x with the cycler's flag.
		// Required for witches to disappear at the end of Black Cauldron, room 12.
		fieldName = "move_x";
		previousValue = screenObj->move_x;
		screenObj->move_x = screenObj->loop_flag;
		break;
	default:
		return;
	}
	warning("Cycler activated for screen object %d while motion is active", screenObj->objectNr);
	warning("Overwriting %s: %d with flag number %d, as original AGI did", fieldName, previousValue, screenObj->loop_flag);
}

void AgiEngine::motionWander(ScreenObjEntry *screenObj) {
	uint8 originalWanderCount = screenObj->wander_count;

	screenObj->wander_count--;
	if ((originalWanderCount == 0) || (screenObj->flags & fDidntMove)) {
		screenObj->direction = _rnd->getRandomNumber(8);

		if (isEgoView(screenObj)) {
			setVar(VM_VAR_EGO_DIRECTION, screenObj->direction);
		}

		while (screenObj->wander_count < 6) {
			screenObj->wander_count = _rnd->getRandomNumber(50);    // huh?
		}
	}
}

void AgiEngine::motionFollowEgo(ScreenObjEntry *screenObj) {
	ScreenObjEntry *screenObjEgo = &_game.screenObjTable[SCREENOBJECTS_EGO_ENTRY];

	int egoX = screenObjEgo->xPos + screenObjEgo->xSize / 2;
	int egoY = screenObjEgo->yPos;

	int objX = screenObj->xPos + screenObj->xSize / 2;
	int objY = screenObj->yPos;

	// Get direction to reach ego
	int dir = getDirection(objX, objY, egoX, egoY, screenObj->follow_stepSize);

	// Already at ego coordinates
	if (dir == 0) {
		screenObj->direction = 0;
		screenObj->motionType = kMotionNormal;
		setFlag(screenObj->follow_flag, true);
		return;
	}

	if (screenObj->follow_count == 0xff) {
		screenObj->follow_count = 0;
	} else if (screenObj->flags & fDidntMove) {
		int d;

		while ((screenObj->direction = _rnd->getRandomNumber(8)) == 0) {
		}

		d = (ABS(egoY - objY) + ABS(egoX - objX)) / 2;

		if (d < screenObj->stepSize) {
			screenObj->follow_count = screenObj->stepSize;
			return;
		}

		while ((screenObj->follow_count = _rnd->getRandomNumber(d)) < screenObj->stepSize) {
		}
		return;
	}

	if (screenObj->follow_count != 0) {
		// DF: this is ugly and I dont know why this works, but
		// other line does not! (watcom complained about lvalue)
		//
		// if (((int8)v->parm3 -= v->step_size) < 0)
		//      v->parm3 = 0;

		int k = screenObj->follow_count;
		k -= screenObj->stepSize;
		screenObj->follow_count = k;

		if ((int8) screenObj->follow_count < 0)
			screenObj->follow_count = 0;
	} else {
		screenObj->direction = dir;
	}
}

void AgiEngine::motionMoveObj(ScreenObjEntry *screenObj) {
	screenObj->direction = getDirection(screenObj->xPos, screenObj->yPos, screenObj->move_x, screenObj->move_y, screenObj->stepSize);

	// Update V6 if ego
	if (isEgoView(screenObj))
		setVar(VM_VAR_EGO_DIRECTION, screenObj->direction);

	if (screenObj->direction == 0)
		motionMoveObjStop(screenObj);
}

void AgiEngine::checkMotion(ScreenObjEntry *screenObj) {
	switch (screenObj->motionType) {
	case kMotionNormal:
		break;
	case kMotionWander:
		motionWander(screenObj);
		break;
	case kMotionFollowEgo:
		motionFollowEgo(screenObj);
		break;
	case kMotionEgo:
	case kMotionMoveObj:
		motionMoveObj(screenObj);
		break;
	default:
		break;
	}

	if ((_game.block.active && (~screenObj->flags & fIgnoreBlocks)) && screenObj->direction)
		changePos(screenObj);
}

/*
 * Public functions
 */

void AgiEngine::checkAllMotions() {
	ScreenObjEntry *screenObj;

	for (screenObj = _game.screenObjTable; screenObj < &_game.screenObjTable[SCREENOBJECTS_MAX]; screenObj++) {
		if ((screenObj->flags & (fAnimated | fUpdate | fDrawn)) == (fAnimated | fUpdate | fDrawn)
		        && screenObj->stepTimeCount == 1) {
			checkMotion(screenObj);
		}
	}
}

/**
 * Check if given entry is at destination point.
 * This function is used to updated the flags of an object with move.obj
 * type motion that * has reached its final destination coordinates.
 * @param  v  Pointer to view table entry
 */
void AgiEngine::inDestination(ScreenObjEntry *screenObj) {
	if (screenObj->motionType == kMotionMoveObj) {
		screenObj->stepSize = screenObj->move_stepSize;
		setFlag(screenObj->move_flag, true);
	}
	screenObj->motionType = kMotionNormal;
	if (isEgoView(screenObj))
		_game.playerControl = true;
}

void AgiEngine::motionMoveObjStop(ScreenObjEntry *screenObj) {
	screenObj->stepSize = screenObj->move_stepSize;

	// This check for motionType was only done in AGI3.
	// But we use this motion type for mouse movement, so we need to check in any case, otherwise it will cause glitches.
	if (screenObj->motionType != kMotionEgo) {
		setFlag(screenObj->move_flag, true);
	}

	screenObj->motionType = kMotionNormal;
	if (isEgoView(screenObj)) {
		_game.playerControl = true;
		setVar(VM_VAR_EGO_DIRECTION, 0);
	}
}

/**
 * Wrapper for static function motion_moveobj().
 * This function is used by cmd_move_object() in the first motion cycle
 * after setting the motion mode to kMotionMoveObj.
 * @param  v  Pointer to view table entry
 */
void AgiEngine::moveObj(ScreenObjEntry *screenObj) {
	motionMoveObj(screenObj);
}

/**
 * Get direction from motion coordinates
 * This function gets the motion direction from the current and previous
 * object coordinates and the step size.
 * @param  x0  Original x coordinate of the object
 * @param  y0  Original y coordinate of the object
 * @param  x   x coordinate of the object
 * @param  y   y coordinate of the object
 * @param  s   step size
 */
int AgiEngine::getDirection(int16 objX, int16 objY, int16 destX, int16 destY, int16 stepSize) {
	const int dirTable[9] = { 8, 1, 2, 7, 0, 3, 6, 5, 4 };
	return dirTable[checkStep(destX - objX, stepSize) + 3 * checkStep(destY - objY, stepSize)];
}

} // End of namespace Agi