File: animate.cc

package info (click to toggle)
exult 1.2-4
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,040 kB
  • ctags: 10,543
  • sloc: cpp: 99,373; sh: 8,333; ansic: 4,659; makefile: 988; yacc: 769; lex: 313; xml: 19
file content (777 lines) | stat: -rw-r--r-- 14,973 bytes parent folder | download | duplicates (8)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/**
 **	Animate.cc - Animated game objects.
 **
 **	Written: 7/27/2000 - JSF
 **/

/*
Copyright (C) 2000  Jeffrey S. Freedman

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "animate.h"
#include "gamewin.h"
#include "game.h"
#include "gameclk.h"
#include "Audio.h"
#include "actors.h"			/* Only need this for Object_sfx. */
#include "dir.h"
#include "Flex.h"
#include <map>
#include <string>

#ifndef UNDER_CE
using std::map;
using std::ostream;
using std::rand;
using std::endl;
using std::cout;
#endif




/*
 *	A class for playing sound effects when certain objects are nearby.
 */
class Object_sfx : public Game_singletons
	{
	int sfxnum;			// Sound effect #.
	int sfx;			// ID of sound effect being played.
	Game_object *obj;		// Object that caused the sound.
	int distance;			// Distance in tiles from Avatar.
	int dir;			// Direction (0-15) from Avatar.
public:
					// Create & start playing sound.
	Object_sfx(int snum, Game_object *o) : sfxnum(snum), distance(0), sfx(-1)
		{ set_obj(o); }
//	bool is_active()		// Is sound still active?
//		{ return sfx.is_active(); }
	int get_sfxnum()
		{ return sfxnum; }
	int get_distance()
		{ return distance; }
	void set_obj(Game_object *o);	// Set to new object.
	void stop(Game_object *o)	// Stop if from given object.
		{
		if (o == obj)
			if(sfx >= 0)
				{
				Mix_HaltChannel(sfx);
				sfx = -1;
				}
		}
					// Get sfx to play for given shape.
	static int get_shape_sfx(int shapenum);
	static void play(Game_object *o, int snum, bool stop = false);
	};

/*
 *	Set to new object if it's closer than the old object, or we're
 *	inactive.
 */
void Object_sfx::set_obj
	(
	Game_object *o
	)
	{
	Tile_coord apos = gwin->get_main_actor()->get_tile();
	Tile_coord opos = o->get_tile();
	int active = 0;
	if(sfx != -1)
	 	active = Mix_Playing(sfx);
	int new_distance = apos.distance(opos);
	if (active && new_distance >= distance && o != obj)
		return;			// Farther than current source.
	obj = o;
	dir = 0;
	bool repeat = true;
	distance = new_distance;
	int volume = MIX_MAX_VOLUME;	// Set volume based on distance.
	if (distance)
		{			// 160/8 = 20 tiles. 20*20=400.
		volume = (MIX_MAX_VOLUME*64)/(distance*distance);
		if (!volume)		// Dead?
			repeat = false;	// Time to kill it.
		if (volume < 8)
			volume = 8;
		else if (volume > MIX_MAX_VOLUME)
			volume = MIX_MAX_VOLUME;
		dir = Get_direction16(apos.ty - opos.ty, opos.tx - apos.tx);
		}
	if (sfx == -1)		// First time?
		{	

					// Start playing, and repeat.
		sfx = Audio::get_ptr()->play_sound_effect(sfxnum, MIX_MAX_VOLUME, dir, -1);
		if(sfx >= 0)

			Mix_Volume(sfx, volume);
		}
	else				// Set new volume, position.
		{
		//Just change the "location" of the sound
		if(!repeat)
			{
			Mix_HaltChannel(sfx);
			sfx = -1;
			}
		else
			{
			Mix_Volume(sfx, volume);
			Mix_SetPosition(sfx, (dir * 22), 0);
			}
		}
	}

/*
 *	Get the sound-effect # for a given shape, or -1 if not found.
 */

int Object_sfx::get_shape_sfx
	(
	int shapenum
	)
	{
	static map<int, int> table;
	static int first = 1;

	if (first)			// First time?
		{
		first = 0;
					// Surf: (47 or 49)
		table[612] = 49;
		table[613] = 49;
		table[632] = 49;
		table[736] = 49;
		table[737] = 49;
		table[751] = 49;
		table[808] = 49;
		table[834] = 49;
		table[875] = 49;
		table[907] = 49;
		table[911] = 49;
		table[918] = 49;
		table[1012] = 49;
		table[1020] = 49;
		table[1022] = 49;
					// Bubbles: (54, 56).
		table[334] = 56;
		table[335] = 56;
		table[780] = 56;
					// Fountains:
		table[893] = 36;
					// Moongates:
		table[776] = 77;
		table[777] = 77;
		if (GAME_BG)
			{
			table[305] = 78;	// Black gate.
			table[786] = 79;	// Vortex cube.
			}
		// Grandfather clock tick tock, only in the SQSFX files,
		 if (Audio::get_ptr()->get_sfx_file() != 0)
			{
			std::string s = 
				Audio::get_ptr()->get_sfx_file()->filename;
			to_uppercase(s);
			if(s.find("SQSFX") != std::string::npos)
				{
				table[252] = 116;	// Grandfather clock 
				table[695] = 116;	// Grandfather clock 
	 			}
			}	
				
		}
	std::map<int, int>::iterator it = table.find(shapenum);
	if (it == table.end())
		return -1;
	int sfx = (*it).second;
	return Audio::game_sfx(sfx);
	return sfx;
	}

/*
 *	Play a sound, or modify its volume/position.
 */

void Object_sfx::play
	(
	Game_object *o,			// Object.
	int snum,			// Object's sound-effect #.
	bool stop			// Time to stop.
	)
	{
					// Play a given sfx only once.
	static map<int, Object_sfx*> play_table;
					// Already playing?
	std::map<int, Object_sfx*>::iterator it = play_table.find(snum);
	if (it == play_table.end())	// No.
		{			// Start new SFX for it.
		if (!stop)
			play_table[snum] = new Object_sfx(snum, o);
		return;
		}
	Object_sfx *sfx = (*it).second;
	if (stop)
	{
		sfx->stop(o);
	}
	else
		sfx->set_obj(o);	// Modify/restart.
	}

/*
 *	Create appropriate animator.
 */

Animator *Animator::create
	(
	Game_object *ob			// Animated object.
	)
	{
	int shnum = ob->get_shapenum();
	int frames = ob->get_num_frames();
	Shape_info& info = ob->get_info();
	if (!info.is_animated())	// Assume it's just SFX.
		return new Sfx_animator(ob);
	else if (frames > 1)
		return new Frame_animator(ob);
	else
		return new Wiggle_animator(ob);
	}


/*
 *	When we delete, better remove from queue.
 */

Animator::~Animator
	(
	)
	{
	while (gwin->get_tqueue()->remove(this))
		;
	}

/*
 *	Start animation.
 */

void Animator::start_animation
	(
	)
	{
					// Clean out old entry if there.
	gwin->get_tqueue()->remove(this);
	gwin->get_tqueue()->add(Game::get_ticks() + 20, this, (long) gwin);
	animating = 1;
	}

/*
 *	Retrieve current frame
 */

int Animator::get_framenum()
{
	return obj->get_framenum();
}

/*
 *	Create a frame animator.
 */

Frame_animator::Frame_animator
	(
	Game_object *o
	) : Animator(o, Object_sfx::get_shape_sfx(o->get_shapenum()))
{
	Initialize();
}

/*
 *	Initialize a frame animator.
 */
void Frame_animator::Initialize()
{
	first_frame = 0;
	created = 0;
	delay = 100;
	type = FA_LOOPING;

	last_shape = obj->get_shapenum();
	last_frame = obj->get_framenum();
	frames = obj->get_num_frames();

	// Serpent Isle
	if (Game::get_game_type() == SERPENT_ISLE)
	{
		switch (last_shape)
		{
		
		case 284:		// Sundial is a special case.
			type = FA_SUNDIAL;
			break;
		
		case 768:		// Energy field.  Stop at top.
			type = FA_ENERGY_FIELD;
			created = Game::get_ticks();
			first_frame = last_frame;
			break;
		
		case 153:		// Fountain
		case 184:		// Lava
		case 222:		// Pennant
		case 289:		// Fire 
		case 305:		// Serpent Statue
		case 326:		// Fountain
		case 456:		// Flux Analyzer
		case 614:		// Magic music box
		case 655:		// Planets 
		case 695:		// Grandfather clock
		case 726:		// Pulsating Object
		case 743:		// Statue??
		case 794:		// Severed limb
		case 992:		// Burning urn
			first_frame = 6*(last_frame/6);
			frames = 6;
			created = last_frame%6;
			break;

		case 779:		// Magic Wave.
			first_frame = 6*(last_frame/6);
			frames = 6;
			if (last_frame < 6) created = 1;
			else created = 0;
			break;

		case 335:		// Bubbles
			created = last_frame;
			if (last_frame < 6)
			{
				first_frame = 0;
				frames = 6;
			}
			else
			{
				first_frame = 6;
				frames = frames-6;
			}
			break;

		case 322:		// Basin
		case 714:		// Basin
			created = last_frame;
			if (last_frame != frames-1)
			{
				first_frame = 0;
				frames = frames - 1;
			}
			else
			{
				first_frame = frames -1;
				frames = 1;
			}
			break;


		default:
			break;
		}
	}
	// Black Gate
	else
	{
		switch (last_shape)
		{
		
		case 284:		// Sundial is a special case.
			type = FA_SUNDIAL;
			break;
		
		case 768:		// Energy field.  Stop at top.
			type = FA_ENERGY_FIELD;
			created = Game::get_ticks();
			first_frame = last_frame;
			break;
		
					// First frame isn't animated
		case 862:		// Shafts
		case 880:
		case 933:		// Millsaw
			if (last_frame == 0)
			{
				first_frame = 0;
				frames = 1;
			}
			else
			{
				frames = frames -1;
				first_frame = 1;
			}
			break;

		default:
			break;
		}
	}
}

/*
 *	Retrieve current frame
 */

int Frame_animator::get_framenum()
{
	unsigned int ticks = Game::get_ticks();
	int framenum = 0;

	if (last_shape != obj->get_shapenum() || last_frame != obj->get_framenum())
		Initialize();

	bool dirty_first = gwin->add_dirty(obj);

	if (last_shape != obj->get_shapenum() || last_frame != obj->get_framenum())
		Initialize();

	switch (type)
	{
	case FA_SUNDIAL:
		framenum = gclock->get_hour() % frames;  
		break;

	case FA_ENERGY_FIELD:
		framenum = (ticks - created) / delay + first_frame;
		if (framenum >= frames) framenum = frames-1;
		break;

	case FA_LOOPING:
		framenum = (ticks / delay) + created;
		framenum %= frames;
		framenum += first_frame;
		break;
	}

	return framenum;
}

/*
 *	Animation.
 */

void Frame_animator::handle_event
	(
	unsigned long curtime,		// Current time of day.
	long udata			// Game window.
	)
{
	unsigned int ticks = Game::get_ticks();

	Game_window *gwin = (Game_window *) udata;

	bool dirty_first = gwin->add_dirty(obj);

	int framenum = get_framenum();

	obj->set_frame(last_frame = framenum);

	if (!dirty_first && !gwin->add_dirty(obj))
	{				// No longer on screen.
		animating = 0;
					// Stop playing sound.
		Object_sfx::play(obj, sfxnum, true);
		return;
	}

	if (!framenum && sfxnum >= 0)	// Sound effect?
		Object_sfx::play(obj, sfxnum);
					// Add back to queue for next time.
	if (animating)
	{
		// Ensure all animations are synced
		gwin->get_tqueue()->add(ticks  + delay- (ticks%delay), this, udata);
	}
}

/*
 *	Create a pure SFX player.
 */

Sfx_animator::Sfx_animator
	(
	Game_object *o
	) : Animator(o, Object_sfx::get_shape_sfx(o->get_shapenum()))
{
}

/*
 *	Play SFX.
 */

void Sfx_animator::handle_event
	(
	unsigned long curtime,		// Current time of day.
	long udata			// Game window.
	)
{
	const int delay = 200;		// Guessing this will be enough.
	unsigned int ticks = Game::get_ticks();

	Game_window *gwin = (Game_window *) udata;
	Rectangle rect = gwin->clip_to_win(gwin->get_shape_rect(obj));
	if (rect.w <= 0 || rect.h <= 0)
	{				// No longer on screen.
		animating = 0;
					// Stop playing sound.
		Object_sfx::play(obj, sfxnum, true);
		return;
	}

	if (sfxnum >= 0)		// Sound effect?
		Object_sfx::play(obj, sfxnum);
					// Add back to queue for next time.
	if (animating)
		// Ensure all animations are synced
		gwin->get_tqueue()->add(ticks  + delay- (ticks%delay), 
								this, udata);
}

/*
 *	Create a field frame animator.
 */

Field_frame_animator::Field_frame_animator
	(
	Game_object *o,
	int rcy				// Frame to start recycling at.
	) : Animator(o), recycle(rcy), activated(true)
	{
	int shapenum = obj->get_shapenum();
	frames = obj->get_num_frames();
	}

/*
 *	Animation.
 */

void Field_frame_animator::handle_event
	(
	unsigned long curtime,		// Current time of day.
	long udata			// Game window.
	)
	{
	int delay = 100;		// Delay between frames.
	Game_window *gwin = (Game_window *) udata;
	if (!gwin->add_dirty(obj))
		{			// No longer on screen.
		animating = 0;
		return;
		}
	int framenum = obj->get_framenum() + 1;
	if (framenum == frames)
		framenum = recycle;	// Restart cycle here.
	obj->set_frame(framenum);
	gwin->add_dirty(obj);
					// Add back to queue for next time.
	if (animating)
		gwin->get_tqueue()->add(curtime + delay, this, udata);
	if (activated && rand()%10 == 0)// Check for damage?
		obj->activate(0);
	}

/*
 *	Animation.
 */

void Wiggle_animator::handle_event
	(
	unsigned long curtime,		// Current time of day.
	long udata			// Game window.
	)
	{
	int delay = 100;		// Delay between frames.
	Game_window *gwin = (Game_window *) udata;
	if (!gwin->add_dirty(obj))
		{			// No longer on screen.
		animating = 0;
		return;
		}
	Tile_coord t = obj->get_tile();	// Get current position.
	int newdx = rand()%3;
	int newdy = rand()%3;
	t.tx += -deltax + newdx;
	t.ty += -deltay + newdy;
	deltax = newdx;
	deltay = newdy;
	obj->Game_object::move(t.tx, t.ty, t.tz);
					// Add back to queue for next time.
	if (animating)
		gwin->get_tqueue()->add(curtime + delay, this, udata);
	}

/*
 *	Create at given position.
 */

Animated_object::Animated_object
	(
	int shapenum, 
	int framenum, 
	unsigned int tilex, unsigned int tiley, 
	unsigned int lft
	) : Terrain_game_object(shapenum, framenum, tilex, tiley, lft)
	{
	animator = Animator::create(this);
	}

/*
 *	When we delete, better remove from queue.
 */

Animated_object::~Animated_object
	(
	)
	{
	delete animator;
	}

/*
 *	Render.
 */

void Animated_object::paint
	(
	)
	{
	animator->want_animation();	// Be sure animation is on.
	Game_object::paint();
	}

/*
 *	Create at given position.
 */

Animated_ireg_object::Animated_ireg_object
	(
	int shapenum, 
	int framenum, 
	unsigned int tilex, unsigned int tiley, 
	unsigned int lft
	) : Ireg_game_object(shapenum, framenum, tilex, tiley, lft)
	{
	animator = Animator::create(this);
	}

/*
 *	When we delete, better remove from queue.
 */

Animated_ireg_object::~Animated_ireg_object
	(
	)
	{
	delete animator;
	}

/*
 *	Render.
 */

void Animated_ireg_object::paint
	(
	)
	{
	animator->want_animation();	// Be sure animation is on.
	Ireg_game_object::paint();
	}

/*
 *	Write out.
 */

void Animated_ireg_object::write_ireg(DataSource *out)
{
	int oldframe = get_framenum();
	set_frame(animator->get_framenum());
	Ireg_game_object::write_ireg(out);
	set_frame(oldframe);
}

/*
 *	Create at given position.
 */

Animated_ifix_object::Animated_ifix_object
	(
	int shapenum, 
	int framenum, 
	unsigned int tilex, unsigned int tiley, 
	unsigned int lft
	) : Ifix_game_object(shapenum, framenum, tilex, tiley, lft)
	{
	animator = Animator::create(this);
	}

/*
 *	Create from IFIX
 */

Animated_ifix_object::Animated_ifix_object(unsigned char *ifix) : Ifix_game_object(ifix)
	{
	animator = Animator::create(this);
	}

/*
 *	When we delete, better remove from queue.
 */

Animated_ifix_object::~Animated_ifix_object
	(
	)
	{
	delete animator;
	}

/*
 *	Render.
 */

void Animated_ifix_object::paint
	(
	)
	{
	animator->want_animation();	// Be sure animation is on.
	Ifix_game_object::paint();
	}

/*
 *	Write out an IFIX object.
 */

void Animated_ifix_object::write_ifix(DataSource *ifix)

{
	int oldframe = get_framenum();
	set_frame(animator->get_framenum());
	Ifix_game_object::write_ifix(ifix);
	set_frame(oldframe);
}