File: contain.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 (677 lines) | stat: -rw-r--r-- 16,075 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
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
/*
 *	contain.cc - Container objects.
 *
 *  Copyright (C) 1998-1999  Jeffrey S. Freedman
 *  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 "contain.h"
#include "gamewin.h"
#include "gamemap.h"
#include "objiter.h"
#include "game.h"
#include "ucmachine.h"
#include "utils.h"
#include "Gump_manager.h"

using std::rand;
using std::ostream;

/*
 *	Delete all contents.
 */

Container_game_object::~Container_game_object
	(
	)
	{
	}

/*
 *	Remove an object.
 */

void Container_game_object::remove
	(
	Game_object *obj
	)
	{
	if (objects.is_empty())
		return;
	volume_used -= obj->get_volume();
	obj->set_owner(0);
	objects.remove(obj);
	}

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

int Container_game_object::add
	(
	Game_object *obj,
	int dont_check			// 1 to skip volume/recursion check.
	)
	{
	if (obj->get_shapenum() == get_shapenum() && !dont_check)
		return (0);		// Can't put a bag in a bag.

	// ugly hack for SI urn (shouldn't be a container)
	if (Game::get_game_type() == SERPENT_ISLE && get_shapenum() == 914) {
		return 0;
	}

	int objvol = obj->get_volume();

	// Always check this. ALWAYS!
	Game_object *parent = this;
	do			// Watch for snake eating itself.
		if (obj == parent)
			return 0;
	while ((parent = parent->get_owner()) != 0);

	if (!dont_check)
		{
		int maxvol = get_max_volume();
		// maxvol = 0 means infinite (ship's hold, hollow tree, etc...)
		if (maxvol > 0 && objvol + volume_used > maxvol)
			return (0);	// Doesn't fit.
		}
	volume_used += objvol;
	obj->set_owner(this);		// Set us as the owner.
	objects.append(obj);		// Append to chain.
					// Guessing:
	if (get_flag(Obj_flags::okay_to_take))
		obj->set_flag(Obj_flags::okay_to_take);
	return 1;
	}

/*
 *	Change shape of a member.
 */

void Container_game_object::change_member_shape
	(
	Game_object *obj,
	int newshape
	)
	{
	int oldvol = obj->get_volume();
	obj->set_shape(newshape);
					// Update total volume.
	volume_used += obj->get_volume() - oldvol;
	}

/*
 *	Recursively add a quantity of an item to those existing in
 *	this container, and create new objects if necessary.
 *
 *	Output:	Delta decremented # added.
 */

int Container_game_object::add_quantity
	(
	int delta,			// Quantity to add.
	int shapenum,			// Shape #.
	int qual,			// Quality, or c_any_qual for any.
	int framenum,			// Frame, or c_any_framenum for any.
	int dontcreate			// If 1, don't create new objs.
	)
{
	if (delta <= 0)
		return delta;

	int cant_add = 0;		// # we can't add due to weight.
	int maxweight = get_max_weight();// Check weight.
	if (maxweight)
		{	
		maxweight *= 10;	// Work in .1 stones.
		int avail = maxweight - get_outermost()->get_weight();
		int objweight = Ireg_game_object::get_weight(shapenum, delta);
		if (objweight && objweight > avail)
			{		// Limit what we can add.
					// Work in 1/100ths.
			int weight1 = (10*objweight)/delta;
			cant_add = delta - (10*avail)/(weight1 ? weight1 : 1);
			if (cant_add >= delta)
				return delta;	// Can't add any.
			delta -= cant_add;
			}
		}
					// Note:  quantity is ignored for
					//   figuring volume.
	Game_object *obj;
	if (!objects.is_empty())
		{			// First try existing items.
		Object_iterator next(objects);
		while (delta && (obj = next.get_next()) != 0)
			{
			if (obj->get_shapenum() == shapenum &&
		    	 (framenum == c_any_framenum || 
					obj->get_framenum() == framenum))

				delta = obj->modify_quantity(delta);
			}
		next.reset();			// Now try recursively.
		while ((obj = next.get_next()) != 0)
			delta = obj->add_quantity(
					delta, shapenum, qual, framenum, 1);
		}
	if (!delta || dontcreate)	// All added?
		return (delta + cant_add);
	else
		return cant_add + create_quantity(delta, shapenum, qual,
				framenum == c_any_framenum ? 0 : framenum);
}

/*
 *	Recursively create a quantity of an item.  Assumes weight check has
 *	already been done.
 *
 *	Output:	Delta decremented # added.
 */

int Container_game_object::create_quantity
	(
	int delta,			// Quantity to add.
	int shnum,			// Shape #.
	int qual,			// Quality, or c_any_qual for any.
	int frnum,			// Frame.
	bool temporary			// Create temporary quantity
	)
	{
					// Usecode container?
	if (get_shapenum() == 486 && Game::get_game_type() == SERPENT_ISLE)
		return delta;
	Shape_info& shp_info=Game_window::get_game_window()->get_info(
								shnum);
	if (!shp_info.has_quality())	// Not a quality object?
		qual = c_any_qual;	// Then don't set it.
	while (delta)			// Create them here first.
		{
		Game_object *newobj = Game_window::get_game_window()->
			create_ireg_object(shp_info, shnum, frnum,0,0,0);

		if (!add(newobj))
			{
			delete newobj;
			break;
			}

		// Set temporary
		if (temporary) newobj->set_flag (Obj_flags::is_temporary);

		if (qual != c_any_qual)	// Set desired quality.
			newobj->set_quality(qual);
		delta--;
		if (delta > 0)
			delta =  newobj->modify_quantity(delta);
		}
	if (!delta)			// All done?
		return (0);
					// Now try those below.
	Game_object *obj;
	if (objects.is_empty())
		return (delta);
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		delta = obj->create_quantity(delta, shnum, qual, frnum);
	return (delta);
	}		

/*
 *	Recursively remove a quantity of an item from those existing in
 *	this container.
 *
 *	Output:	Delta decremented by # removed.
 */

int Container_game_object::remove_quantity
	(
	int delta,			// Quantity to remove.
	int shapenum,			// Shape #.
	int qual,			// Quality, or c_any_qual for any.
	int framenum			// Frame, or c_any_framenum for any.
	)
	{
	if (objects.is_empty())
		return delta;		// Empty.
	Game_object *obj = objects.get_first();
	Game_object *last = obj->get_prev();	// Save last.
	Game_object *next;
	while (obj && delta)
		{
					// Might be deleting obj.
		next = obj == last ? 0 : obj->get_next();
		bool del = false;	// Gets 'deleted' flag.
		if (obj->get_shapenum() == shapenum &&
		    (qual == c_any_qual || obj->get_quality() == qual) &&
		    (framenum == c_any_framenum || 
					obj->get_framenum() == framenum))
			delta = -obj->modify_quantity(-delta, &del);

		if (!del)		// Still there?
					// Do it recursively.
			delta = obj->remove_quantity(delta, shapenum, 
							qual, framenum);
		obj = next;
		}
	return (delta);
	}

/*
 *	Find and return a desired item.
 *
 *	Output:	->object if found, else 0.
 */

Game_object *Container_game_object::find_item
	(
	int shapenum,			// Shape #.
	int qual,			// Quality, or c_any_qual for any.
	int framenum			// Frame, or c_any_framenum for any.
	)
	{
	if (objects.is_empty())
		return 0;		// Empty.
	Game_object *obj;
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		{
		if (obj->get_shapenum() == shapenum &&
		    (framenum == c_any_framenum || 
					obj->get_framenum() == framenum) &&
		    (qual == c_any_qual || obj->get_quality() == qual))
			return (obj);

					// Do it recursively.
		Game_object *found = obj->find_item(shapenum, qual, framenum);
		if (found)
			return (found);
		}
	return (0);
	}

/*
 *	Run usecode when double-clicked.
 */

void Container_game_object::activate
	(
	Usecode_machine *umachine,
	int event
	)
	{
	if (edit())
		return;			// Map-editing.
	int shnum = get_shapenum();
	Gump_manager *gump_man = Game_window::get_game_window()->get_gump_man();

	if (Game::get_game_type() == BLACK_GATE)  switch(shnum)	// Watch for gumps.
	{
		case 405:			// Ship's hold
		gump_man->add_gump(this, game->get_shape("gumps/shipshold"));
		return;

		case 406:			// Nightstand.
		case 407:			// Desk.
		case 283:
		case 203:
		case 416:			// Chest of drawers.
		case 679:
		gump_man->add_gump(this, game->get_shape("gumps/drawer"));
		return;

		case 400:			// Bodies.
		case 414:
		case 762:
		case 778:
		case 892:
		case 507: 			// Bones
		gump_man->add_gump(this, game->get_shape("gumps/body"));
		return;

		case 800:			// Chest.
		gump_man->add_gump(this, game->get_shape("gumps/chest"));
		return;

		case 801:			// Backpack.
		gump_man->add_gump(this, game->get_shape("gumps/backpack"));
		return;

		case 799:			// Unsealed box
		gump_man->add_gump(this, game->get_shape("gumps/box"));
		return;

		case 802:			// Bag.
		gump_man->add_gump(this, game->get_shape("gumps/bag"));
		return;

		case 803:			// Basket.
		gump_man->add_gump(this, game->get_shape("gumps/basket"));
		return;
	
		case 804:			// Crate.
		gump_man->add_gump(this, game->get_shape("gumps/crate"));
		return;

		case 819:			// Barrel.
		gump_man->add_gump(this, game->get_shape("gumps/barrel"));
		return;
	}
	else if (Game::get_game_type() == SERPENT_ISLE) switch(shnum)	// Watch for gumps.
	{
		case 405:			// Ship's hold
		gump_man->add_gump(this, game->get_shape("gumps/shipshold"));
		return;

		case 406:			// Nightstand.
		case 407:			// Desk.
		case 283:
		case 416:			// Chest of drawers.
		case 679:
		gump_man->add_gump(this, game->get_shape("gumps/drawer"));
		return;

		case 400:			// Bodies.
		case 402:
		case 414:
		case 762:
		case 778:
		case 892:
		case 507: 			// Bones
		gump_man->add_gump(this, game->get_shape("gumps/body"));
		return;

		case 800:			// Chest.
		if (get_quality() >= 251)	// Trapped?
			{		// Run normal usecode fun.
			umachine->call_usecode(shnum, this,
				(Usecode_machine::Usecode_events) event);
			return;
			}
						// FALL THROUGH to 486.
		case 486:			// Usecode container.
		gump_man->add_gump(this, game->get_shape("gumps/chest"));
		return;

		case 801:			// Backpack.
		gump_man->add_gump(this, game->get_shape("gumps/backpack"));
		return;

		case 799:			// Unsealed box
		gump_man->add_gump(this, game->get_shape("gumps/box"));
		return;

		case 802:			// Bag.
		gump_man->add_gump(this, game->get_shape("gumps/bag"));
		return;

		case 803:			// Basket.
		gump_man->add_gump(this, game->get_shape("gumps/basket"));
		return;
	
		case 804:			// Crate.
		gump_man->add_gump(this, game->get_shape("gumps/crate"));
		return;

		case 819:			// Barrel.
		gump_man->add_gump(this, game->get_shape("gumps/barrel"));
		return;

		case 297:			// Hollow Tree
		gump_man->add_gump(this, game->get_shape("gumps/tree"));
		return;

		case 555:			// Serpent Jawbone
		gump_man->add_gump(this, game->get_shape("gumps/jawbone"));
		return;
	}

					// Try to run normal usecode fun.
	umachine->call_usecode(shnum, this,
				(Usecode_machine::Usecode_events) event);
	}

/*
 *	Get (total) weight.
 */

int Container_game_object::get_weight
	(
	)
	{
	int wt = Ireg_game_object::get_weight();
	Game_object *obj;
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		wt += obj->get_weight();
	return wt;
	}

/*
 *	Drop another onto this.
 *
 *	Output:	0 to reject, 1 to accept.
 */

int Container_game_object::drop
	(
	Game_object *obj
	)
	{
	if (!get_owner())		// Only accept if inside another.
		return (0);
	return (add(obj));		// We'll take it.
	}

/*
 *	Recursively count all objects of a given shape.
 */

int Container_game_object::count_objects
	(
	int shapenum,			// Shape#, or c_any_shapenum for any.
	int qual,			// Quality, or c_any_qual for any.
	int framenum			// Frame#, or c_any_framenum for any.
	)
	{
	int total = 0;
	Game_object *obj;
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		{
		if ((shapenum == c_any_shapenum || obj->get_shapenum() == shapenum) &&
		    (framenum == c_any_framenum || obj->get_framenum() == framenum) &&
		    (qual == c_any_qual || obj->get_quality() == qual))
			{		// Check quantity.
			int quant = obj->get_quantity();
			total += quant;
			}
					// Count recursively.
		total += obj->count_objects(shapenum, qual, framenum);
		}
	return (total);
	}

/*
 *	Recursively get all objects of a given shape.
 */

int Container_game_object::get_objects
	(
	Game_object_vector& vec,	// Objects returned here.
	int shapenum,			// Shape#, or c_any_shapenum for any.
	int qual,			// Quality, or c_any_qual for any.
	int framenum			// Frame#, or c_any_framenum for any.
	)
	{
	int vecsize = vec.size();
	Game_object *obj;
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		{
		if ((shapenum == c_any_shapenum || obj->get_shapenum() == shapenum) &&
		    (qual == c_any_qual || obj->get_quality() == qual) &&
		    (framenum == c_any_framenum || obj->get_framenum() == framenum))
			vec.push_back(obj);
					// Search recursively.
		obj->get_objects(vec, shapenum, qual, framenum);
		}
	return (vec.size() - vecsize);
	}


/*
 *	Set a flag on this and all contents.
 */

void Container_game_object::set_flag_recursively
	(
	int flag
	)
	{
	set_flag(flag);
	Game_object *obj;
	Object_iterator next(objects);
	while ((obj = next.get_next()) != 0)
		obj->set_flag_recursively(flag);
	}

/*
 *	Write out container and its members.
 */

void Container_game_object::write_ireg
	(
	ostream& out
	)
	{
	unsigned char buf[13];		// 13-byte entry + length-byte.
	buf[0] = 12;
	uint8 *ptr = &buf[1];	// To avoid confusion about offsets.
	write_common_ireg(ptr);		// Fill in bytes 1-4.
	ptr += 4;
	Game_object *first = objects.get_first(); // Guessing: +++++
	unsigned short tword = first ? first->get_prev()->get_shapenum() 
									: 0;
	Write2(ptr, tword);
	*ptr++ = 0;			// Unknown.
	*ptr++ = get_quality();
	int npc = get_live_npc_num();	// If body, get source.
	int quant;
	if (Game::get_game_type() == SERPENT_ISLE)
		quant = npc + 0x80;
	else
		quant = (npc >= 0 && npc <= 127) ? (npc + 0x80) : 0;

	*ptr++ = quant&0xff;		// "Quantity".
	*ptr++ = (get_lift()&15)<<4;	// Lift 
	*ptr++ = (unsigned char)resistance;		// Resistance.
					// Flags:  B0=invis. B3=okay_to_take.
	*ptr++ = get_flag((Obj_flags::invisible) != 0) +
		 ((get_flag(Obj_flags::okay_to_take) != 0) << 3);
	out.write((char*)buf, sizeof(buf));
	write_contents(out);		// Write what's contained within.
					// Write scheduled usecode.
	Game_map::write_scheduled(out, this);	
	}

/*
 *	Write contents (if there is any).
 */

void Container_game_object::write_contents
	(
	ostream& out
	)
	{
	if (!objects.is_empty())	// Now write out what's inside.
		{
		Game_object *obj;
		Object_iterator next(objects);
		while ((obj = next.get_next()) != 0)
			obj->write_ireg(out);
		out.put(0x01);		// A 01 terminates the list.
		}
	}


bool Container_game_object::extract_contents()
{
	if (objects.is_empty())
		return true;

	bool status = true;

	Container_game_object *owner = get_owner();
	Game_object *obj;

	while ((obj = objects.get_first())) {
		remove(obj);

		if (owner) {
			owner->add(obj,1); // add without checking volume
		} else {
			obj->set_invalid(); // set to invalid chunk so move() doesn't fail
			if ((get_cx() == 255) && (get_cy() == 255)) {
				obj->remove_this(0);
				status = false;
			} else {
				obj->move(get_tile());
			}
		}
	}

	return status;
}

void Container_game_object::delete_contents()
{
	if (objects.is_empty())
		return;

	Game_object *obj;
	while ((obj = objects.get_first())) {
		remove(obj);

		obj->delete_contents(); // recurse into contained containers
		obj->remove_this(0);
	}
}

void Container_game_object::remove_this(int nodel)
{
					// Special case to avoid recursion.
	if (Container_game_object::get_owner())
		{			// First remove from owner.
		Ireg_game_object::remove_this(1);
		if (nodel)		// Not deleting?  Then done.
			return;
		}
	if (!nodel)
		extract_contents();

	Ireg_game_object::remove_this(nodel);
}