File: monsters.cc

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (562 lines) | stat: -rw-r--r-- 13,651 bytes parent folder | download
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
/*
 *	monsters.cc - Monsters.
 *
 *  Copyright (C) 2000-2001  The Exult Team
 *
 *  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 Library 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 "monsters.h"
#include "monstinf.h"
#include "gamewin.h"
#include "animate.h"
#include "schedule.h"
#include "chunks.h"
#include "Audio.h"

using std::rand;

Monster_actor *Monster_actor::in_world = 0;
int Monster_actor::in_world_cnt = 0;

/*
 *	Initialize a new monster.
 */

void Monster_actor::init
	(
	)
	{
	if (in_world)
		in_world->prev_monster = this;
	next_monster = in_world;
	in_world = this;
	in_world_cnt++;
					// Check for animated shape.
	Shape_info& info = Game_window::get_game_window()->get_info(this);
	if (info.is_animated())
		animator = Animator::create(this, 1);
	}

/*
 *	Create monster.
 */

Monster_actor::Monster_actor
	(
	const std::string &nm, 
	int shapenum, 
	int num,			// Generally -1.
	int uc
	) : Npc_actor(nm, shapenum, num, uc), prev_monster(0),
	    animator(0)
	{
	init();
	}

/*
 *	Delete.
 */

Monster_actor::~Monster_actor
	(
	)
	{
	delete animator;
					// Remove from chain.
	if (next_monster)
		next_monster->prev_monster = prev_monster;
	if (prev_monster)
		prev_monster->next_monster = next_monster;
	else				// We're at start of list.
		in_world = next_monster;
	if (!is_dead())			// Dying decrements count.
		in_world_cnt--;
	}

/*
 *	Another set of constants that should be in a data file (or that's
 *	probably already in one of the U7 data files), this one correlating
 *	a 'monster' shape with the food frame you get when you kill it.
 */
static int Monster_food[] = {
	498, 10,			// Chicken.
	500,  9,			// Cow - beef steaks.
	502,  14,			// Deer - meat.
	509, 12,			// Fish.
	811, 14,			// Rabbit - small leg.
	970, 8,				// Sheep - mutton.
	727, 23				// Horse - ribs.
	};

/*
 *	Find food frame for given monster shape.
 *
 *	Output:	Frame if found, else a random frame (0-31).
 */

static int Find_monster_food
	(
	int shnum			// Monster shape.
	)
	{
	const int cnt = sizeof(Monster_food)/(2*sizeof(Monster_food[0]));
	for (int i = 0; i < cnt; i++)
		if (Monster_food[2*i] == shnum)
			return Monster_food[2*i + 1];
	return rand()%32;
	}

/*
 *	Create an instance of a monster.
 */

Monster_actor *Monster_actor::create
	(
	int shnum			// Shape to use.
	)
	{
	if (shnum == 529)		// Slime?
					// Usecode = shape.
		return new Slime_actor("", shnum, -1, shnum);
	else
		return new Monster_actor("", shnum, -1, shnum);
	}

/*
 *	Create an instance of a monster and initialize from monstinf.dat.
 */

Monster_actor *Monster_actor::create
	(
	int shnum,			// Shape to use.
	Tile_coord pos,			// Where to place it.  If pos.tx < 0,
					//   it's not placed in the world.
	int sched,			// Schedule type.
	int align,			// Alignment.
	bool temporary,
	bool equipment
	)
	{
	Game_window *gwin = Game_window::get_game_window();
					// Get 'monsters.dat' info.
	const Monster_info *inf = gwin->get_info(shnum).get_monster_info();
	if (!inf)
		inf = Monster_info::get_default();
	Monster_actor *monster = create(shnum);
	monster->set_alignment(align == static_cast<int>(Actor::neutral)
						? inf->alignment : align);
	// Movement flags
	if ((inf->flags >> Monster_info::fly)&1)
	{
		monster->set_type_flag(Actor::tf_fly);
	}
	if ((inf->flags >> Monster_info::swim)&1)
	{
		monster->set_type_flag(Actor::tf_swim);
	}
	if ((inf->flags >> Monster_info::walk)&1)
	{
		monster->set_type_flag(Actor::tf_walk);
	}
	if ((inf->flags >> Monster_info::ethereal)&1)
	{
		monster->set_type_flag(Actor::tf_ethereal);
	}
	monster->set_property(Actor::strength, inf->strength);
					// Max. health = strength.
	monster->set_property(Actor::health, inf->strength);
	monster->set_property(Actor::dexterity, inf->dexterity);
	monster->set_property(Actor::intelligence, inf->intelligence);
	monster->set_property(Actor::combat, inf->combat);

	// Set temporary
	if (temporary) monster->set_flag (Obj_flags::is_temporary);
	monster->set_invalid();		// Place in world.
	if (pos.tx >= 0)
		monster->move(pos.tx, pos.ty, pos.tz);
	if (equipment) {
					// Get equipment.
		int equip_offset = inf->equip_offset;
		Equip_record *equip = inf->equip;
		if (equip_offset && equip_offset - 1 < inf->equip_cnt)
		{
			Equip_record& rec = equip[equip_offset - 1];
			for (size_t i = 0;
				 i < sizeof(equip->elements)/sizeof(
							equip->elements[0]);
				 i++)
			{		// Give equipment.
				Equip_element& elem = rec.elements[i];
				if (!elem.shapenum || 1 + rand()%100 > 
						elem.probability)
					continue;// You lose.
				int frnum = (elem.shapenum == 377) ? 
					Find_monster_food(shnum) : 0;
				monster->create_quantity(elem.quantity, 
						elem.shapenum, c_any_qual, 
							frnum, temporary);
			}
		}
	}

	if (sched < 0)			// Set sched. AFTER equipping.
		sched = static_cast<int>(Schedule::loiter);
	monster->set_schedule_type(sched);
	return (monster);
	}

/*
 *	Delete all monsters.  (Should only be called after deleting chunks.)
 */

void Monster_actor::delete_all
	(
	)
	{
	while (in_world)
		delete in_world;
	in_world_cnt = 0;
	}

/*
 *	Render.
 */

void Monster_actor::paint
	(
	Game_window *gwin
	)
	{
	// Animate first
	if (animator)			// Be sure animation is on.
		animator->want_animation();
	Npc_actor::paint(gwin);		// Draw on screen.
	}

/*
 *	Step onto an adjacent tile.
 *
 *	Output:	0 if blocked.
 *		Dormant is set if off screen.
 */

int Monster_actor::step
	(
	Tile_coord t,			// Tile to step onto.
	int frame			// New frame #.
	)
	{
	Game_window *gwin = Game_window::get_game_window();
	
	// If move not allowed do I remove or change destination?
	// I'll do nothing for now
	if (!gwin->emulate_is_move_allowed(t.tx, t.ty))
		return (0);
	if (get_flag(Obj_flags::paralyzed))
		return 0;
					// Store old chunk.
	int old_cx = get_cx(), old_cy = get_cy();
					// Get chunk.
	int cx = t.tx/c_tiles_per_chunk, cy = t.ty/c_tiles_per_chunk;
					// Get ->new chunk.
	Map_chunk *nlist = gwin->get_chunk(cx, cy);
	nlist->setup_cache();		// Setup cache if necessary.
					// Blocked?
	if (is_blocked(t))
		{
		if (schedule)		// Tell scheduler.
			schedule->set_blocked(t);
		stop();
		if (!gwin->add_dirty(this))
			dormant = true;	// Off-screen.
		return (0);		// Done.
		}
					// Check for scrolling.
	gwin->scroll_if_needed(this, t);
	add_dirty(gwin);		// Set to repaint old area.
					// Get old chunk.
	Map_chunk *olist = gwin->get_chunk(old_cx, old_cy);
					// Move it.
					// Get rel. tile coords.
	int tx = t.tx%c_tiles_per_chunk, ty = t.ty%c_tiles_per_chunk;
	movef(olist, nlist, tx, ty, frame, t.tz);
	if (!add_dirty(gwin, 1) &&
					// And > a screenful away?
	    distance(gwin->get_camera_actor()) > 1 + 320/c_tilesize)
		{			// No longer on screen.
		stop();
		dormant = true;
		return (0);
		}
	return (1);			// Add back to queue for next time.
	}

/*
 *	Add an object.
 *
 *	Output:	1, meaning object is completely contained in this,
 *		0 if not enough space.
 */

int Monster_actor::add
	(
	Game_object *obj,
	int dont_check			// 1 to skip volume check.
	)
	{
	if (Npc_actor::add(obj, 1))	// Try to add to 'readied' spot.
		return (1);		// Successful.
					// Just add anything.
	return Container_game_object::add(obj, 1);
	}

/*
 *	Get total value of armor being worn.
 */

int Monster_actor::get_armor_points
	(
	)
	{
	Monster_info *inf =
	    Game_window::get_game_window()->get_info(this).get_monster_info();
					// Kind of guessing here.
	return Actor::get_armor_points() + (inf ? inf->armor : 0);
	}

/*
 *	Get weapon value.
 */

Weapon_info *Monster_actor::get_weapon
	(
	int& points,
	int& shape
	)
	{
	Game_window *gwin = Game_window::get_game_window();
	Monster_info *inf = gwin->get_info(this).get_monster_info();
					// Kind of guessing here.
	Weapon_info *winf = Actor::get_weapon(points, shape);
	if (!winf)			// No readied weapon?
		{			// Look up monster itself.
		shape = 0;
		winf = gwin->get_info(get_shapenum()).get_weapon_info();
		if (winf)
			points = winf->get_damage();
		else			// Guessing:
			points = inf ? inf->weapon : 0;
		}
	return winf;
	}

/*
 *	We're dead.  We're removed from the world, but not deleted.
 */

void Monster_actor::die
	(
	)
	{
	Actor::die();
	Audio::get_ptr()->start_music_combat ( CSVictory, 0);
					// Got to delete this somewhere, but
					//   doing it here crashes.
	in_world_cnt--;			// So... Decrement 'live' count here.
	}

/*
 *	Get the tiles where slimes adjacent to one in a given position should
 *	be found.
 */

static void Get_slime_neighbors
	(
	Tile_coord pos,			// Position to look around.
	Tile_coord *neighbors		// N,E,S,W tiles returned.
	)
	{
					// Offsets to neighbors 2 tiles away.
	static int offsets[8] = {0,-2, 2,0, 0,2, -2,0};
	for (int dir = 0; dir < 4; dir++)
		neighbors[dir] = pos +Tile_coord(offsets[2*dir],
						    offsets[2*dir + 1], 0);
	}

/*
 *	Find whether a slime is a neighbor of a given spot.
 *
 *	Output:	Direction (0-3 for N,E,S,W), or -1 if not found.
 */

int Find_neighbor
	(
	Game_object *slime,
	Tile_coord *neighbors		// Neighboring spots to check.
	)
	{
	Tile_coord pos = slime->get_tile();
	for (int dir = 0; dir < 4; dir++)
		if (pos == neighbors[dir])
			return dir;
	return -1;			// Not found.
	}

/*
 *	Update the frame of a slime and its neighbors after it has been moved.
 *	The assumption is that slimes are 2x2 tiles, and that framenum/2 is
 *	based on whether there are adjoining slimes to the N, W, S, or E, with
 *	bit 0 being random.
 */

void Slime_actor::update_frames
	(
	Tile_coord src,			// May be invalid (tx = -1).
	Tile_coord dest			// May be invalid.  If src & dest are
					//   both valid, we assume they're at
					//   most 2 tiles apart.
	)
	{
	Tile_coord neighbors[4];	// Gets surrounding spots for slimes.
	int dir;			// Get direction of neighbor.
	Game_window *gwin = Game_window::get_game_window();
	Game_object_vector nearby;		// Get nearby slimes.
	if (src.tx != -1)
		if (dest.tx != -1)	// Assume within 2 tiles.
			Game_object::find_nearby(nearby, dest, 529, 4, 8);
		else
			Game_object::find_nearby(nearby, src, 529, 2, 8);
	else				// Assume they're both not invalid.
		Game_object::find_nearby(nearby, dest, 529, 2, 8);
	if (src.tx != -1)		// Update neighbors we moved from.
		{
		Get_slime_neighbors(src, neighbors);
		for (Game_object_vector::const_iterator it = nearby.begin();
						it != nearby.end(); ++it)
			{
			Game_object *slime = *it;
			if (slime != this && 
			    (dir = Find_neighbor(slime, neighbors)) >= 0)
				{
				int ndir = (dir+2)%4;
					// Turn off bit (1<<ndir)*2, and set
					//   bit 0 randomly.
				gwin->add_dirty(slime);
				slime->set_frame((slime->get_framenum()&
					~(((1<<ndir)*2)|1)) |(rand()%2));
				gwin->add_dirty(slime);
				}
			}
		}
	if (dest.tx != -1)		// Update neighbors we moved to.
		{
		int frnum = 0;		// Figure our new frame too.
		Get_slime_neighbors(dest, neighbors);
		for (Game_object_vector::const_iterator it = nearby.begin();
						it != nearby.end(); ++it)
			{
			Game_object *slime = *it;
			if (slime != this && 
			    (dir = Find_neighbor(slime, neighbors)) >= 0)
				{	// In a neighboring spot?
				frnum |= (1<<dir)*2;
				int ndir = (dir+2)%4;
				gwin->add_dirty(slime);
					// Turn on bit (1<<ndir)*2, and set
					//   bit 0 randomly.
				slime->set_frame((slime->get_framenum()&~1)|
						((1<<ndir)*2)|(rand()%2));
				gwin->add_dirty(slime);
				}
			}
		gwin->add_dirty(this);
		set_frame(frnum|(rand()%2));
		gwin->add_dirty(this);
		}
	}

/*
 *	Step onto an adjacent tile.
 *
 *	Output:	0 if blocked.
 *		Dormant is set if off screen.
 */

int Slime_actor::step
	(
	Tile_coord t,			// Tile to step onto.
	int /* frame */			// New frame # (ignored).
	)
	{
					// Save old pos.
	Tile_coord oldpos = get_tile();
	int ret = Monster_actor::step(t, -1);
					// Update surrounding frames (& this).
	Tile_coord newpos = get_tile();
	update_frames(oldpos, newpos);
	Game_object_vector blood;	// Place blood in old spot.
	if (newpos != oldpos && rand()%9 == 0 &&
	    !find_nearby(blood, oldpos, 912, 1, 0))
		{
		Game_window *gwin = Game_window::get_game_window();
					// Frames 4-11 are green.
		Game_object *b = gwin->create_ireg_object(912, 4 + rand()%8);
		b->set_flag(Obj_flags::is_temporary);
		b->move(oldpos);
		}
	return ret;
	}

/*
 *	Remove an object from its container, or from the world.
 *	The object is deleted.
 */

void Slime_actor::remove_this
	(
	int nodel			// 1 to not delete.
	)
	{
	Tile_coord pos = get_tile();
	Monster_actor::remove_this(nodel);
					// Update surrounding slimes.
	update_frames(pos, Tile_coord(-1, -1, -1));
	}

/*
 *	Move (teleport) to a new spot.  I assume slimes only use this when
 *	being added to the world.
 */

void Slime_actor::move
	(
	int newtx, 
	int newty, 
	int newlift
	)
	{
					// Save old pos.
	Tile_coord pos = get_tile();
	if (get_cx() == 255)		// Invalid?
		pos = Tile_coord(-1, -1, -1);
	Monster_actor::move(newtx, newty, newlift);
					// Update surrounding frames (& this).
	update_frames(pos, get_tile());
	}