File: character_info_engine.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 (468 lines) | stat: -rw-r--r-- 15,023 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* 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 "ags/shared/ac/character_info.h"
#include "ags/shared/ac/common.h"
#include "ags/shared/ac/game_setup_struct.h"
#include "ags/engine/ac/character.h"
#include "ags/engine/ac/character_extras.h"
#include "ags/engine/ac/game_state.h"
#include "ags/engine/ac/global_character.h"
#include "ags/engine/ac/global_game.h"
#include "ags/engine/ac/math.h"
#include "ags/engine/ac/object.h"
#include "ags/engine/ac/view_frame.h"
#include "ags/engine/debugging/debug_log.h"
#include "ags/shared/game/room_struct.h"
#include "ags/engine/main/update.h"
#include "ags/engine/media/audio/audio_system.h"
#include "ags/ags.h"
#include "ags/globals.h"

namespace AGS3 {

using namespace AGS::Shared;

#define Random __Rand

int CharacterInfo::get_baseline() const {
	if (baseline < 1)
		return y;
	return baseline;
}

int CharacterInfo::get_blocking_top() const {
	if (blocking_height > 0)
		return y - blocking_height / 2;
	return y - 2;
}

int CharacterInfo::get_blocking_bottom() const {
	// the blocking_bottom should be 1 less than the top + height
	// since the code does <= checks on it rather than < checks
	if (blocking_height > 0)
		return (y + (blocking_height + 1) / 2) - 1;
	return y + 3;
}

void CharacterInfo::FixupCurrentLoopAndFrame() {
	// If current loop property exceeds number of loops,
	// or if selected loop has no frames, then try select any first loop that has frames.
	// NOTE: although this may seem like a weird solution to a problem,
	// we do so for backwards compatibility; this approximately emulates older games behavior.

	if (view >= 0 &&
		(loop >= _GP(views)[view].numLoops || _GP(views)[view].loops[loop].numFrames == 0)) {
		for (loop = 0;
			(loop < _GP(views)[view].numLoops) && (_GP(views)[view].loops[loop].numFrames == 0); ++loop) {
		}
		if (loop == _GP(views)[view].numLoops) {
			// view has no frames?!
			// amazingly enough there are old games that allow this to happen...
			if (_G(loaded_game_file_version) >= kGameVersion_300)
				quitprintf("!Character %s is assigned view %d that has no frames!", scrname, view);
			loop = 0;
		}
	}

	// If the last saved frame exceeds a new loop, then switch to frame 1
	// (first walking frame) if walking, or frame 0 otherwise or if there's less than 2 frames.
	int frames_in_loop = _GP(views)[view].loops[loop].numFrames;
	if (frame >= frames_in_loop) {
		frame = (walking > 0 && frames_in_loop > 1) ? 1 : 0;
	}
}

void CharacterInfo::UpdateMoveAndAnim(int &char_index, CharacterExtras *chex, std::vector<int> &followingAsSheep) {
	int res;

	if (on != 1)
		return;

	// Turn around during walk
	res = update_character_walkturning(chex);
	// Fixup character's loop prior to any further logic updates
	FixupCurrentLoopAndFrame();

	// FIXME: refactor this nonsense!
	// [IKM] Yes, it should return! upon getting RETURN_CONTINUE here
	if (res == RETURN_CONTINUE) { // [IKM] now, this is one of those places...
		return;                   //  must be careful not to screw things up
	}

	int doing_nothing = 1;
	update_character_moving(char_index, chex, doing_nothing);

	// [IKM] 2012-06-28:
	// Character index value is used to set up some variables in there, so I cannot just cease using it
	res = update_character_animating(char_index, doing_nothing);
	// [IKM] Yes, it should return! upon getting RETURN_CONTINUE here
	if (res == RETURN_CONTINUE) { // [IKM] now, this is one of those places...
		return;                   //  must be careful not to screw things up
	}

	update_character_follower(char_index, followingAsSheep, doing_nothing);

	update_character_idle(chex, doing_nothing);

	chex->process_idle_this_time = 0;
}

void CharacterInfo::UpdateFollowingExactlyCharacter() {
	x = _GP(game).chars[following].x;
	y = _GP(game).chars[following].y;
	z = _GP(game).chars[following].z;
	room = _GP(game).chars[following].room;
	prevroom = _GP(game).chars[following].prevroom;

	int usebase = _GP(game).chars[following].get_baseline();

	if (flags & CHF_BEHINDSHEPHERD)
		baseline = usebase - 1;
	else
		baseline = usebase + 1;
}

int CharacterInfo::update_character_walkturning(CharacterExtras *chex) {
	if (walking >= TURNING_AROUND) {
		// Currently rotating to correct direction
		if (walkwait > 0) walkwait--;
		else {
			// Work out which direction is next
			int wantloop = find_looporder_index(loop) + 1;
			// going anti-clockwise, take one before instead
			if (walking >= TURNING_BACKWARDS)
				wantloop -= 2;
			while (1) {
				if (wantloop >= 8)
					wantloop = 0;
				if (wantloop < 0)
					wantloop = 7;
				if ((turnlooporder[wantloop] >= _GP(views)[view].numLoops) ||
				        (_GP(views)[view].loops[turnlooporder[wantloop]].numFrames < 1) ||
				        ((turnlooporder[wantloop] >= 4) && ((flags & CHF_NODIAGONAL) != 0))) {
					if (walking >= TURNING_BACKWARDS)
						wantloop--;
					else
						wantloop++;
				} else break;
			}
			loop = turnlooporder[wantloop];
			walking -= TURNING_AROUND;
			// if still turning, wait for next frame
			if (walking % TURNING_BACKWARDS >= TURNING_AROUND)
				walkwait = animspeed;
			else
				walking = walking % TURNING_BACKWARDS;
			chex->animwait = 0;
		}
		return RETURN_CONTINUE;
		//continue;
	}

	return 0;
}

void CharacterInfo::update_character_moving(int &char_index, CharacterExtras *chex, int &doing_nothing) {
	if ((walking > 0) && (room == _G(displayed_room))) {
		if (walkwait > 0) walkwait--;
		else {
			flags &= ~CHF_AWAITINGMOVE;

			// Move the character
			int numSteps = wantMoveNow(this, chex);

			if ((numSteps) && (chex->xwas != INVALID_X)) {
				// if the zoom level changed mid-move, the walkcounter
				// might not have come round properly - so sort it out
				x = chex->xwas;
				y = chex->ywas;
				chex->xwas = INVALID_X;
			}

			int oldxp = x, oldyp = y;

			for (int ff = 0; ff < abs(numSteps); ff++) {
				if (doNextCharMoveStep(this, char_index, chex))
					break;
				if ((walking == 0) || (walking >= TURNING_AROUND))
					break;
			}

			if (numSteps < 0) {
				// very small scaling, intersperse the movement
				// to stop it being jumpy
				chex->xwas = x;
				chex->ywas = y;
				x = ((x) - oldxp) / 2 + oldxp;
				y = ((y) - oldyp) / 2 + oldyp;
			} else if (numSteps > 0)
				chex->xwas = INVALID_X;

			if ((flags & CHF_ANTIGLIDE) == 0)
				walkwaitcounter++;
		}

		// Fixup character's loop, it may be changed when making a walk-move
		FixupCurrentLoopAndFrame();

		doing_nothing = 0; // still walking?

		if (walking < 1) {
			// Finished walking, stop and reset state
			chex->process_idle_this_time = 1;
			doing_nothing = 1;
			walkwait = 0;
			Character_StopMoving(this);
			if ((flags & CHF_MOVENOTWALK) == 0) {
				// use standing pic
				chex->animwait = 0;
				frame = 0;
				chex->CheckViewFrame(this);
			}
		} else if (chex->animwait > 0) {
			chex->animwait--;
		} else {
			if (flags & CHF_ANTIGLIDE)
				walkwaitcounter++;

			if ((flags & CHF_MOVENOTWALK) == 0) {
				frame++;
				if (frame >= _GP(views)[view].loops[loop].numFrames) {
					// end of loop, so loop back round skipping the standing frame
					frame = 1;

					if (_GP(views)[view].loops[loop].numFrames < 2)
						frame = 0;
				}

				chex->animwait = _GP(views)[view].loops[loop].frames[frame].speed + animspeed;

				if (flags & CHF_ANTIGLIDE)
					walkwait = chex->animwait;
				else
					walkwait = 0;

				chex->CheckViewFrame(this);
			}
		}
	}
}

int CharacterInfo::update_character_animating(int &aa, int &doing_nothing) {
	CharacterExtras *chex = &_GP(charextra)[index_id];

	// not moving, but animating
	// idleleft is <0 while idle view is playing (.animating is 0)
	if (((animating != 0) || (idleleft < 0)) &&
	        ((walking == 0) || ((flags & CHF_MOVENOTWALK) != 0)) &&
	        (room == _G(displayed_room))) {
		doing_nothing = 0;
		// idle anim doesn't count as doing something
		if (idleleft < 0)
			doing_nothing = 1;

		if (wait > 0) wait--;
		else if ((_G(char_speaking) == aa) && (_GP(game).options[OPT_LIPSYNCTEXT] != 0)) {
			// currently talking with lip-sync speech
			int fraa = frame;
			wait = update_lip_sync(view, loop, &fraa) - 1;
			// closed mouth at end of sentence
			// NOTE: standard lip-sync is synchronized with text timer, not voice file
			if (_GP(play).speech_in_post_state ||
			        ((_GP(play).messagetime >= 0) && (_GP(play).messagetime < _GP(play).close_mouth_speech_time)))
				frame = 0;

			if (frame != fraa) {
				frame = fraa;
				chex->CheckViewFrame(this);
			}

			//continue;
			return RETURN_CONTINUE;
		} else {
			// Normal view animation
			const int oldframe = frame;

			bool done_anim = false;
			if ((aa == _G(char_speaking)) &&
				(_GP(play).speech_in_post_state ||
				((!_GP(play).speech_has_voice) &&
					(_GP(play).close_mouth_speech_time > 0) &&
					(_GP(play).messagetime < _GP(play).close_mouth_speech_time)))) {
				// finished talking - stop animation
				done_anim = true;
				frame = 0;
			} else {
				if (!CycleViewAnim(view, loop, frame, get_anim_forwards(), get_anim_repeat())) {
					done_anim = true; // finished animating
					// end of idle anim
					if (idleleft < 0) {
						// constant anim, reset (need this cos animating==0)
						if (idletime == 0)
							frame = 0;
						// one-off anim, stop
						else {
							ReleaseCharacterView(aa);
							idleleft = idletime;
						}
					}
				}
			}

			wait = _GP(views)[view].loops[loop].frames[frame].speed;
			// idle anim doesn't have speed stored cos animating==0 (TODO: investigate why?)
			if (idleleft < 0)
				wait += idle_anim_speed;
			else
				wait += get_anim_delay();

			if (frame != oldframe)
				chex->CheckViewFrame(this);

			if (done_anim)
				stop_character_anim(this);
		}
	}

	return 0;
}

void CharacterInfo::update_character_follower(int &aa, std::vector<int> &followingAsSheep, int &doing_nothing) {
	if ((following >= 0) && (followinfo == FOLLOW_ALWAYSONTOP)) {
		// an always-on-top follow
		followingAsSheep.push_back(aa);
	}

	// not moving, but should be following another character
	else if ((following >= 0) && (doing_nothing == 1)) {
		short distaway = (followinfo >> 8) & 0x00ff;
		// no character in this room
		if ((_GP(game).chars[following].on == 0) || (on == 0));
		else if (room < 0) {
			room++;
			if (room == 0) {
				// appear in the new room
				room = _GP(game).chars[following].room;
				x = _GP(play).entered_at_x;
				y = _GP(play).entered_at_y;
			}
		}
		// wait a bit, so we're not constantly walking
		else if (Random(100) < (followinfo & 0x00ff));
		// the followed character has changed room
		else if ((room != _GP(game).chars[following].room)
		         && (_GP(game).chars[following].on == 0))
			;  // do nothing if the player isn't visible
		else if (room != _GP(game).chars[following].room) {
			prevroom = room;
			room = _GP(game).chars[following].room;

			if (room == _G(displayed_room)) {
				// only move to the room-entered position if coming into
				// the current room
				if (_GP(play).entered_at_x > (_GP(thisroom).Width - 8)) {
					x = _GP(thisroom).Width + 8;
					y = _GP(play).entered_at_y;
				} else if (_GP(play).entered_at_x < 8) {
					x = -8;
					y = _GP(play).entered_at_y;
				} else if (_GP(play).entered_at_y > (_GP(thisroom).Height - 8)) {
					y = _GP(thisroom).Height + 8;
					x = _GP(play).entered_at_x;
				} else if (_GP(play).entered_at_y < _GP(thisroom).Edges.Top + 8) {
					y = _GP(thisroom).Edges.Top + 1;
					x = _GP(play).entered_at_x;
				} else {
					// not at one of the edges
					// delay for a few seconds to let the player move
					room = -_GP(play).follow_change_room_timer;
				}
				if (room >= 0) {
					walk_character(aa, _GP(play).entered_at_x, _GP(play).entered_at_y, 1, true);
					doing_nothing = 0;
				}
			}
		} else if (room != _G(displayed_room)) {
			// if the character is following another character and
			// neither is in the current room, don't try to move
		} else if ((abs(_GP(game).chars[following].x - x) > distaway + 30) ||
		           (abs(_GP(game).chars[following].y - y) > distaway + 30) ||
		           ((followinfo & 0x00ff) == 0)) {
			// in same room
			int goxoffs = (Random(50) - 25);
			// make sure he's not standing on top of the other man
			if (goxoffs < 0) goxoffs -= distaway;
			else goxoffs += distaway;
			walk_character(aa, _GP(game).chars[following].x + goxoffs,
			               _GP(game).chars[following].y + (Random(50) - 25), 0, true);
			doing_nothing = 0;
		}
	}
}

void CharacterInfo::update_character_idle(CharacterExtras *chex, int &doing_nothing) {
	// no idle animation, so skip this bit
	if (idleview < 1);
	// currently playing idle anim
	else if (idleleft < 0);
	// not in the current room
	else if (room != _G(displayed_room));
	// they are moving or animating (or the view is locked), so
	// reset idle timeout
	else if ((doing_nothing == 0) || ((flags & CHF_FIXVIEW) != 0))
		idleleft = idletime;
	// count idle time
	else if ((_G(loopcounter) % GetGameSpeed() == 0) || (chex->process_idle_this_time == 1)) {
		idleleft--;
		if (idleleft == -1) {
			int useloop = loop;
			debug_script_log("%s: Now idle (view %d)", scrname, idleview + 1);
			Character_LockView(this, idleview + 1);
			// SetCharView resets it to 0
			idleleft = -2;
			int maxLoops = _GP(views)[idleview].numLoops;
			// if the char is set to "no diagonal loops", don't try
			// to use diagonal idle loops either
			if ((maxLoops > 4) && (useDiagonal(this)))
				maxLoops = 4;
			// If it's not a "swimming"-type idleanim, choose a random loop
			// if there arent enough loops to do the current one.
			if ((idletime > 0) && (useloop >= maxLoops)) {
				do {
					useloop = ::AGS::g_vm->getRandomNumber(maxLoops - 1);
					// don't select a loop which is a continuation of a previous one
				} while ((useloop > 0) && (_GP(views)[idleview].loops[useloop - 1].RunNextLoop()));
			}
			// Normal idle anim - just reset to loop 0 if not enough to
			// use the current one
			else if (useloop >= maxLoops)
				useloop = 0;

			animate_character(this, useloop, idle_anim_speed, (idletime == 0) ? 1 : 0 /* repeat */);

			// don't set Animating while the idle anim plays (TODO: investigate why?)
			animating = 0;
		}
	}  // end do idle animation
}

} // namespace AGS3