aboutsummaryrefslogtreecommitdiff
path: root/engines/agi/motion.cpp
blob: f408ba35e66c249e4115e468f2e4c86de2db66e6 (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
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
/* 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 "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) {
	bool insideBlock;
	int16 x, y;
	int dx[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
	int dy[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };

	x = screenObj->xPos;
	y = screenObj->yPos;
	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:
// A motion was just activated, check if "end.of.loop"/"reverse.loop" is currently active for the same screen object
// If this is the case, it would result in some random flag getting overwritten in original AGI after the loop was
// completed, because in original AGI loop_flag + wander_count/follow_stepSize/move_X shared the same memory location.
// This is basically an implementation error in the original interpreter.
// Happens in at least:
// - BC: right at the end when the witches disappear at least on Apple IIgs (room 12, screen object 13, view 84)
// - KQ1: when grabbing the eagle (room 22).
// - KQ2: happened somewhere in the game, LordHoto couldn't remember exactly where
void AgiEngine::motionActivated(ScreenObjEntry *screenObj) {
	if (screenObj->flags & fCycling) {
		// Cycling active too
		switch (screenObj->cycle) {
		case kCycleEndOfLoop: // "end.of.loop"
		case kCycleRevLoop: // "reverse.loop"
			// Disable it
			screenObj->flags &= ~fCycling;
			screenObj->cycle = kCycleNormal;

			warning("Motion activated for screen object %d, but cycler also active", screenObj->objectNr);
			warning("This would have resulted in flag corruption in original AGI. Cycler disabled.");
			break;
		default:
			break;
		}
	}
}

// WORKAROUND:
// See comment for motionActivated()
// This way no flag would have been overwritten, but certain other variables of the motions.
void AgiEngine::cyclerActivated(ScreenObjEntry *screenObj) {
	switch (screenObj->motionType) {
	case kMotionWander:
		// this would have resulted in wander_count to get corrupted
		// We don't stop it.
		break;
	case kMotionFollowEgo:
		// this would have resulted in follow_stepSize to get corrupted
		// do not stop motion atm - screenObj->direction = 0;
		// do not stop motion atm - screenObj->motionType = kMotionNormal;
		break;
	case kMotionMoveObj:
		// this would have resulted in move_x to get corrupted
		// do not stop motion atm - motionMoveObjStop(screenObj);
		break;
	default:
		return;
		break;
	}
	warning("Cycler activated for screen object %d, but motion also active", screenObj->objectNr);
	warning("This would have resulted in corruption in original AGI. Motion disabled.");
}

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, egoY;
	int objX, objY;
	int dir;

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

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

	// Get direction to reach ego
	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) {
		int k;

		// 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;

		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;
	}

	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) {
	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