ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.C
(Generate patch)

Comparing deliantra/server/common/object.C (file contents):
Revision 1.194 by root, Tue Oct 16 00:30:24 2007 UTC vs.
Revision 1.207 by root, Sun Apr 20 23:25:09 2008 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Crossfire TRT is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. 24/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
25 sub/add_weight will transcend the environment updating the carrying 25 sub/add_weight will transcend the environment updating the carrying
26 variable. */ 26 variable. */
27
27#include <global.h> 28#include <global.h>
28#include <stdio.h> 29#include <stdio.h>
29#include <sys/types.h> 30#include <sys/types.h>
30#include <sys/uio.h> 31#include <sys/uio.h>
31#include <object.h> 32#include <object.h>
33#include <sproto.h> 34#include <sproto.h>
34#include <loader.h> 35#include <loader.h>
35 36
36#include <bitset> 37#include <bitset>
37 38
38int nrofallocobjects = 0; 39UUID UUID::cur;
39static UUID uuid; 40static uint64_t seq_next_save;
40const uint64 UUID_SKIP = 1<<19; 41static const uint64 UUID_GAP = 1<<19;
41 42
42objectvec objects; 43objectvec objects;
43activevec actives; 44activevec actives;
44 45
45short freearr_x[SIZEOFFREE] = { 0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 46short freearr_x[SIZEOFFREE] = {
47 0,
48 0, 1, 1, 1, 0, -1, -1, -1,
49 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1,
46 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 50 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
47}; 51};
48short freearr_y[SIZEOFFREE] = { 0, -1, -1, 0, 1, 1, 1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, 52short freearr_y[SIZEOFFREE] = {
53 0,
54 -1, -1, 0, 1, 1, 1, 0, -1,
55 -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2,
49 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3 56 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3
50}; 57};
51int maxfree[SIZEOFFREE] = { 0, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 58int maxfree[SIZEOFFREE] = {
59 0,
60 9, 10, 13, 14, 17, 18, 21, 22,
61 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 48,
52 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49 62 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
53}; 63};
54int freedir[SIZEOFFREE] = { 64int freedir[SIZEOFFREE] = {
55 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 65 0,
66 1, 2, 3, 4, 5, 6, 7, 8,
67 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8,
56 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 68 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
57}; 69};
58 70
59static void 71static void
60write_uuid (void) 72write_uuid (uval64 skip, bool sync)
61{ 73{
62 char filename1[MAX_BUF], filename2[MAX_BUF]; 74 CALL_BEGIN (2);
63 75 CALL_ARG_SV (newSVval64 (skip));
64 sprintf (filename1, "%s/uuid", settings.localdir); 76 CALL_ARG_SV (boolSV (sync));
65 sprintf (filename2, "%s/uuid~", settings.localdir); 77 CALL_CALL ("cf::write_uuid", G_DISCARD);
66 78 CALL_END;
67 FILE *fp;
68
69 if (!(fp = fopen (filename2, "w")))
70 {
71 LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2);
72 return;
73 }
74
75 fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2);
76 fclose (fp);
77 rename (filename2, filename1);
78} 79}
79 80
80static void 81static void
81read_uuid (void) 82read_uuid (void)
82{ 83{
83 char filename[MAX_BUF]; 84 char filename[MAX_BUF];
84 85
85 sprintf (filename, "%s/uuid", settings.localdir); 86 sprintf (filename, "%s/uuid", settings.localdir);
86 87
88 seq_next_save = 0;
89
87 FILE *fp; 90 FILE *fp;
88 91
89 if (!(fp = fopen (filename, "r"))) 92 if (!(fp = fopen (filename, "r")))
90 { 93 {
91 if (errno == ENOENT) 94 if (errno == ENOENT)
92 { 95 {
93 LOG (llevInfo, "RESET uid to 1\n"); 96 LOG (llevInfo, "RESET uid to 1\n");
94 uuid.seq = 0; 97 UUID::cur.seq = 0;
95 write_uuid (); 98 write_uuid (UUID_GAP, true);
96 return; 99 return;
97 } 100 }
98 101
99 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); 102 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
100 _exit (1); 103 _exit (1);
101 } 104 }
102 105
103 int version; 106 UUID::BUF buf;
104 unsigned long long uid; 107 buf[0] = 0;
105 if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1) 108 fgets (buf, sizeof (buf), fp);
109
110 if (!UUID::cur.parse (buf))
106 { 111 {
107 LOG (llevError, "FATAL: error reading uid from %s!\n", filename); 112 LOG (llevError, "FATAL: error reading uid from %s (%s)!\n", filename, buf);
108 _exit (1); 113 _exit (1);
109 } 114 }
110 115
111 uuid.seq = uid; 116 LOG (llevDebug, "read UUID: %s\n", UUID::cur.c_str ());
112 write_uuid (); 117
113 LOG (llevDebug, "read UID: %" PRId64 "\n", uid); 118 write_uuid (UUID_GAP, true);
114 fclose (fp); 119 fclose (fp);
115} 120}
116 121
117UUID 122UUID
118gen_uuid () 123UUID::gen ()
119{ 124{
120 UUID uid; 125 UUID uid;
121 126
122 uid.seq = ++uuid.seq; 127 uid.seq = ++cur.seq;
123 128
124 if (!(uuid.seq & (UUID_SKIP - 1))) 129 if (expect_false (cur.seq >= seq_next_save))
125 write_uuid (); 130 {
131 seq_next_save = UUID::cur.seq + (UUID_GAP >> 1);
132 write_uuid (UUID_GAP, false);
133 }
134
126 135
127 return uid; 136 return uid;
128} 137}
129 138
130void 139void
131init_uuid () 140UUID::init ()
132{ 141{
133 read_uuid (); 142 read_uuid ();
134} 143}
135 144
136/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 145/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
137static int 146static bool
138compare_ob_value_lists_one (const object *wants, const object *has) 147compare_ob_value_lists_one (const object *wants, const object *has)
139{ 148{
140 key_value *wants_field; 149 key_value *wants_field;
141 150
142 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both 151 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both
150 key_value *has_field; 159 key_value *has_field;
151 160
152 /* Look for a field in has with the same key. */ 161 /* Look for a field in has with the same key. */
153 has_field = get_ob_key_link (has, wants_field->key); 162 has_field = get_ob_key_link (has, wants_field->key);
154 163
155 if (has_field == NULL) 164 if (!has_field)
156 {
157 /* No field with that name. */ 165 return 0; /* No field with that name. */
158 return FALSE;
159 }
160 166
161 /* Found the matching field. */ 167 /* Found the matching field. */
162 if (has_field->value != wants_field->value) 168 if (has_field->value != wants_field->value)
163 {
164 /* Values don't match, so this half of the comparison is false. */ 169 return 0; /* Values don't match, so this half of the comparison is false. */
165 return FALSE;
166 }
167 170
168 /* If we get here, we found a match. Now for the next field in wants. */ 171 /* If we get here, we found a match. Now for the next field in wants. */
169 } 172 }
170 173
171 /* If we get here, every field in wants has a matching field in has. */ 174 /* If we get here, every field in wants has a matching field in has. */
172 return TRUE; 175 return 1;
173} 176}
174 177
175/* Returns TRUE if ob1 has the same key_values as ob2. */ 178/* Returns TRUE if ob1 has the same key_values as ob2. */
176static int 179static bool
177compare_ob_value_lists (const object *ob1, const object *ob2) 180compare_ob_value_lists (const object *ob1, const object *ob2)
178{ 181{
179 /* However, there may be fields in has which aren't partnered in wants, 182 /* However, there may be fields in has which aren't partnered in wants,
180 * so we need to run the comparison *twice*. :( 183 * so we need to run the comparison *twice*. :(
181 */ 184 */
202 || ob1->speed != ob2->speed 205 || ob1->speed != ob2->speed
203 || ob1->value != ob2->value 206 || ob1->value != ob2->value
204 || ob1->name != ob2->name) 207 || ob1->name != ob2->name)
205 return 0; 208 return 0;
206 209
207 //TODO: this ain't working well, use nicer and correct overflow check 210 /* Do not merge objects if nrof would overflow. First part checks
208 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 211 * for unsigned overflow (2c), second part checks wether the result
209 * value could not be stored in a sint32 (which unfortunately sometimes is 212 * would fit into a 32 bit signed int, which is often used to hold
210 * used to store nrof). 213 * nrof values.
211 */ 214 */
212 if (ob1->nrof + ob2->nrof >= 1UL << 31) 215 if (~ob1->nrof < ob2->nrof || ob1->nrof + ob2->nrof > (1UL << 31))
213 return 0; 216 return 0;
214 217
215 /* If the objects have been identified, set the BEEN_APPLIED flag. 218 /* If the objects have been identified, set the BEEN_APPLIED flag.
216 * This is to the comparison of the flags below will be OK. We 219 * This is to the comparison of the flags below will be OK. We
217 * just can't ignore the been applied or identified flags, as they 220 * just can't ignore the been applied or identified flags, as they
218 * are not equal - just if it has been identified, the been_applied 221 * are not equal - just if it has been identified, the been_applied
219 * flags lose any meaning. 222 * flags lose any meaning.
220 */ 223 */
221 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) 224 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
249 || ob1->move_off != ob2->move_off 252 || ob1->move_off != ob2->move_off
250 || ob1->move_slow != ob2->move_slow 253 || ob1->move_slow != ob2->move_slow
251 || ob1->move_slow_penalty != ob2->move_slow_penalty) 254 || ob1->move_slow_penalty != ob2->move_slow_penalty)
252 return 0; 255 return 0;
253 256
254 /* This is really a spellbook check - really, we should 257 /* This is really a spellbook check - we should in general
255 * check all objects in the inventory. 258 * not merge objects with real inventories, as splitting them
259 * is hard.
256 */ 260 */
257 if (ob1->inv || ob2->inv) 261 if (ob1->inv || ob2->inv)
258 { 262 {
259 if (!(ob1->inv && ob2->inv)) 263 if (!(ob1->inv && ob2->inv))
260 return 0; /* inventories differ in length */ 264 return 0; /* inventories differ in length */
261 265
262 if (ob1->inv->below || ob2->inv->below) 266 if (ob1->inv->below || ob2->inv->below)
263 return 0; /* more than one object in inv */ 267 return 0; /* more than one object in inv */
264 268
265 if (!object::can_merge (ob1->inv, ob2->inv)) 269 if (!object::can_merge (ob1->inv, ob2->inv))
266 return 0; /* inventory objexts differ */ 270 return 0; /* inventory objects differ */
267 271
268 /* inventory ok - still need to check rest of this object to see 272 /* inventory ok - still need to check rest of this object to see
269 * if it is valid. 273 * if it is valid.
270 */ 274 */
271 } 275 }
290 if (ob1->level != ob2->level) 294 if (ob1->level != ob2->level)
291 return 0; 295 return 0;
292 break; 296 break;
293 } 297 }
294 298
295 if (ob1->key_values != NULL || ob2->key_values != NULL) 299 if (ob1->key_values || ob2->key_values)
296 { 300 {
297 /* At least one of these has key_values. */ 301 /* At least one of these has key_values. */
298 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) 302 if ((!ob1->key_values) != (!ob2->key_values))
299 /* One has fields, but the other one doesn't. */ 303 /* One has fields, but the other one doesn't. */
300 return 0; 304 return 0;
301 else if (!compare_ob_value_lists (ob1, ob2)) 305 else if (!compare_ob_value_lists (ob1, ob2))
302 return 0; 306 return 0;
303 } 307 }
307 ob1->optimise (); 311 ob1->optimise ();
308 ob2->optimise (); 312 ob2->optimise ();
309 313
310 if (ob1->self || ob2->self) 314 if (ob1->self || ob2->self)
311 { 315 {
312 int k1 = ob1->self ? HvTOTALKEYS (SvRV (ob1->self)) : 0; 316 int k1 = ob1->self ? HvTOTALKEYS (ob1->self) : 0;
313 int k2 = ob2->self ? HvTOTALKEYS (SvRV (ob2->self)) : 0; 317 int k2 = ob2->self ? HvTOTALKEYS (ob2->self) : 0;
314 318
315 if (k1 != k2) 319 if (k1 != k2)
316 return 0; 320 return 0;
317 else if (k1 == 0) 321 else if (k1 == 0)
318 return 1; 322 return 1;
323 327
324 /* Everything passes, must be OK. */ 328 /* Everything passes, must be OK. */
325 return 1; 329 return 1;
326} 330}
327 331
332static sint32
333weight_adjust (object *op, sint32 weight)
334{
335 return op->type == CONTAINER
336 ? lerp (weight, 0, 100, 0, 100 - op->stats.Str)
337 : weight;
338}
339
328/* 340/*
341 * add_weight(object, weight) adds the specified weight to an object,
342 * and also updates how much the environment(s) is/are carrying.
343 */
344static void
345add_weight (object *op, sint32 weight)
346{
347 while (op)
348 {
349 weight = weight_adjust (op, weight);
350
351 op->carrying += weight;
352 op = op->env;
353 }
354}
355
356/*
357 * sub_weight() recursively (outwards) subtracts a number from the
358 * weight of an object (and what is carried by it's environment(s)).
359 */
360static void
361sub_weight (object *op, long weight)
362{
363 add_weight (op, -weight);
364}
365
366/*
329 * sum_weight() is a recursive function which calculates the weight 367 * this is a recursive function which calculates the weight
330 * an object is carrying. It goes through in figures out how much 368 * an object is carrying. It goes through op and figures out how much
331 * containers are carrying, and sums it up. 369 * containers are carrying, and sums it up.
332 */ 370 */
333long 371void
334sum_weight (object *op) 372object::update_weight ()
335{ 373{
336 long sum; 374 sint32 sum = 0;
337 object *inv;
338 375
339 for (sum = 0, inv = op->inv; inv; inv = inv->below) 376 for (object *op = inv; op; op = op->below)
340 { 377 {
341 if (inv->inv) 378 if (op->inv)
342 sum_weight (inv); 379 op->update_weight ();
343 380
344 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 381 sum += op->total_weight ();
345 } 382 }
346 383
347 if (op->type == CONTAINER && op->stats.Str) 384 carrying = weight_adjust (this, sum);
348 sum = (sum * (100 - op->stats.Str)) / 100;
349
350 if (op->carrying != sum)
351 op->carrying = sum;
352
353 return sum;
354}
355
356/**
357 * Return the outermost environment object for a given object.
358 */
359
360object *
361object_get_env_recursive (object *op)
362{
363 while (op->env != NULL)
364 op = op->env;
365 return op;
366} 385}
367 386
368/* 387/*
369 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 388 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
370 * Some error messages. 389 * Some error messages.
429 break; 448 break;
430 449
431 return op; 450 return op;
432} 451}
433 452
434void
435free_all_object_data ()
436{
437 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
438}
439
440/* 453/*
441 * Sets the owner and sets the skill and exp pointers to owner's current 454 * Sets the owner and sets the skill and exp pointers to owner's current
442 * skill and experience objects. 455 * skill and experience objects.
443 * ACTUALLY NO! investigate! TODO 456 * ACTUALLY NO! investigate! TODO
444 */ 457 */
447{ 460{
448 // allow objects which own objects 461 // allow objects which own objects
449 if (owner) 462 if (owner)
450 while (owner->owner) 463 while (owner->owner)
451 owner = owner->owner; 464 owner = owner->owner;
465
466 if (flag [FLAG_FREED])
467 {
468 LOG (llevError | logBacktrace, "tried to set owner of %s to %s\n", debug_desc (), owner->debug_desc ());
469 return;
470 }
452 471
453 this->owner = owner; 472 this->owner = owner;
454} 473}
455 474
456int 475int
597 616
598void 617void
599object::instantiate () 618object::instantiate ()
600{ 619{
601 if (!uuid.seq) // HACK 620 if (!uuid.seq) // HACK
602 uuid = gen_uuid (); 621 uuid = UUID::gen ();
603 622
604 speed_left = -0.1f; 623 speed_left = -0.1f;
605 /* copy the body_info to the body_used - this is only really 624 /* copy the body_info to the body_used - this is only really
606 * need for monsters, but doesn't hurt to do it for everything. 625 * need for monsters, but doesn't hurt to do it for everything.
607 * by doing so, when a monster is created, it has good starting 626 * by doing so, when a monster is created, it has good starting
766static int object_count; 785static int object_count;
767 786
768void object::link () 787void object::link ()
769{ 788{
770 assert (!index);//D 789 assert (!index);//D
771 uuid = gen_uuid (); 790 uuid = UUID::gen ();
772 count = ++object_count; 791 count = ++object_count;
773 792
774 refcnt_inc (); 793 refcnt_inc ();
775 objects.insert (this); 794 objects.insert (this);
776} 795}
860 * if some form of movement is allowed, let objects 879 * if some form of movement is allowed, let objects
861 * drop on that space. 880 * drop on that space.
862 */ 881 */
863 if (!drop_to_ground 882 if (!drop_to_ground
864 || !map 883 || !map
865 || map->in_memory != MAP_IN_MEMORY 884 || map->in_memory != MAP_ACTIVE
866 || map->nodrop 885 || map->nodrop
867 || ms ().move_block == MOVE_ALL) 886 || ms ().move_block == MOVE_ALL)
868 { 887 {
869 while (inv) 888 while (inv)
870 { 889 {
929 948
930 freed_map->path = "<freed objects map>"; 949 freed_map->path = "<freed objects map>";
931 freed_map->name = "/internal/freed_objects_map"; 950 freed_map->name = "/internal/freed_objects_map";
932 freed_map->width = 3; 951 freed_map->width = 3;
933 freed_map->height = 3; 952 freed_map->height = 3;
953 freed_map->nodrop = 1;
934 954
935 freed_map->alloc (); 955 freed_map->alloc ();
936 freed_map->in_memory = MAP_IN_MEMORY; 956 freed_map->in_memory = MAP_ACTIVE;
937 } 957 }
938 958
939 map = freed_map; 959 map = freed_map;
940 x = 1; 960 x = 1;
941 y = 1; 961 y = 1;
970 play_sound (sound_destroy); 990 play_sound (sound_destroy);
971 else if (flag [FLAG_MONSTER]) 991 else if (flag [FLAG_MONSTER])
972 play_sound (sound_find ("monster_destroy")); // quick hack, too lazy to create a generic mechanism 992 play_sound (sound_find ("monster_destroy")); // quick hack, too lazy to create a generic mechanism
973 993
974 attachable::destroy (); 994 attachable::destroy ();
975}
976
977/*
978 * sub_weight() recursively (outwards) subtracts a number from the
979 * weight of an object (and what is carried by it's environment(s)).
980 */
981void
982sub_weight (object *op, signed long weight)
983{
984 while (op != NULL)
985 {
986 if (op->type == CONTAINER)
987 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
988
989 op->carrying -= weight;
990 op = op->env;
991 }
992} 995}
993 996
994/* op->remove (): 997/* op->remove ():
995 * This function removes the object op from the linked list of objects 998 * This function removes the object op from the linked list of objects
996 * which it is currently tied to. When this function is done, the 999 * which it is currently tied to. When this function is done, the
1475} 1478}
1476 1479
1477object * 1480object *
1478object::insert_at (object *where, object *originator, int flags) 1481object::insert_at (object *where, object *originator, int flags)
1479{ 1482{
1483 if (where->env)
1484 return where->env->insert (this);
1485 else
1480 return where->map->insert (this, where->x, where->y, originator, flags); 1486 return where->map->insert (this, where->x, where->y, originator, flags);
1481} 1487}
1482 1488
1483/* 1489/*
1484 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1490 * get_split_ob(ob,nr) splits up ob into two parts. The part which
1485 * is returned contains nr objects, and the remaining parts contains 1491 * is returned contains nr objects, and the remaining parts contains
1505 orig_ob->destroy (1); 1511 orig_ob->destroy (1);
1506 else if (!is_removed) 1512 else if (!is_removed)
1507 { 1513 {
1508 if (orig_ob->env != NULL) 1514 if (orig_ob->env != NULL)
1509 sub_weight (orig_ob->env, orig_ob->weight * nr); 1515 sub_weight (orig_ob->env, orig_ob->weight * nr);
1510 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY) 1516 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_ACTIVE)
1511 { 1517 {
1512 strcpy (errmsg, "Tried to split object whose map is not in memory."); 1518 strcpy (errmsg, "Tried to split object whose map is not in memory.");
1513 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n"); 1519 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n");
1514 return NULL; 1520 return NULL;
1515 } 1521 }
1602 return op; 1608 return op;
1603 else 1609 else
1604 { 1610 {
1605 op->destroy (); 1611 op->destroy ();
1606 return 0; 1612 return 0;
1607 }
1608}
1609
1610/*
1611 * add_weight(object, weight) adds the specified weight to an object,
1612 * and also updates how much the environment(s) is/are carrying.
1613 */
1614void
1615add_weight (object *op, signed long weight)
1616{
1617 while (op != NULL)
1618 {
1619 if (op->type == CONTAINER)
1620 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1621
1622 op->carrying += weight;
1623 op = op->env;
1624 } 1613 }
1625} 1614}
1626 1615
1627object * 1616object *
1628insert_ob_in_ob (object *op, object *where) 1617insert_ob_in_ob (object *op, object *where)
1952 * activate recursively a flag on an object inventory 1941 * activate recursively a flag on an object inventory
1953 */ 1942 */
1954void 1943void
1955flag_inv (object *op, int flag) 1944flag_inv (object *op, int flag)
1956{ 1945{
1957 if (op->inv)
1958 for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below) 1946 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1959 { 1947 {
1960 SET_FLAG (tmp, flag); 1948 SET_FLAG (tmp, flag);
1961 flag_inv (tmp, flag); 1949 flag_inv (tmp, flag);
1962 } 1950 }
1963} 1951}
1964 1952
1965/* 1953/*
1966 * deactivate recursively a flag on an object inventory 1954 * deactivate recursively a flag on an object inventory
1967 */ 1955 */
1968void 1956void
1969unflag_inv (object *op, int flag) 1957unflag_inv (object *op, int flag)
1970{ 1958{
1971 if (op->inv)
1972 for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below) 1959 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1973 { 1960 {
1974 CLEAR_FLAG (tmp, flag); 1961 CLEAR_FLAG (tmp, flag);
1975 unflag_inv (tmp, flag); 1962 unflag_inv (tmp, flag);
1976 } 1963 }
1977} 1964}
1978 1965
1979/* 1966/*
1980 * find_free_spot(object, map, x, y, start, stop) will search for 1967 * find_free_spot(object, map, x, y, start, stop) will search for
1981 * a spot at the given map and coordinates which will be able to contain 1968 * a spot at the given map and coordinates which will be able to contain
1983 * to search (see the freearr_x/y[] definition). 1970 * to search (see the freearr_x/y[] definition).
1984 * It returns a random choice among the alternatives found. 1971 * It returns a random choice among the alternatives found.
1985 * start and stop are where to start relative to the free_arr array (1,9 1972 * start and stop are where to start relative to the free_arr array (1,9
1986 * does all 4 immediate directions). This returns the index into the 1973 * does all 4 immediate directions). This returns the index into the
1987 * array of the free spot, -1 if no spot available (dir 0 = x,y) 1974 * array of the free spot, -1 if no spot available (dir 0 = x,y)
1988 * Note - this only checks to see if there is space for the head of the
1989 * object - if it is a multispace object, this should be called for all
1990 * pieces.
1991 * Note2: This function does correctly handle tiled maps, but does not 1975 * Note: This function does correctly handle tiled maps, but does not
1992 * inform the caller. However, insert_ob_in_map will update as 1976 * inform the caller. However, insert_ob_in_map will update as
1993 * necessary, so the caller shouldn't need to do any special work. 1977 * necessary, so the caller shouldn't need to do any special work.
1994 * Note - updated to take an object instead of archetype - this is necessary 1978 * Note - updated to take an object instead of archetype - this is necessary
1995 * because arch_blocked (now ob_blocked) needs to know the movement type 1979 * because arch_blocked (now ob_blocked) needs to know the movement type
1996 * to know if the space in question will block the object. We can't use 1980 * to know if the space in question will block the object. We can't use
2017 2001
2018 /* However, often 2002 /* However, often
2019 * ob doesn't have any move type (when used to place exits) 2003 * ob doesn't have any move type (when used to place exits)
2020 * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work. 2004 * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work.
2021 */ 2005 */
2022 if (ob->move_type == 0 && ms.move_block != MOVE_ALL) 2006 if (ob && ob->move_type == 0 && ms.move_block != MOVE_ALL)
2023 { 2007 {
2024 altern [index++] = i; 2008 altern [index++] = i;
2025 continue; 2009 continue;
2026 } 2010 }
2027 2011
2041 2025
2042 /* Note it is intentional that we check ob - the movement type of the 2026 /* Note it is intentional that we check ob - the movement type of the
2043 * head of the object should correspond for the entire object. 2027 * head of the object should correspond for the entire object.
2044 */ 2028 */
2045 if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block)) 2029 if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block))
2030 continue;
2031
2032 if (ob->blocked (m, pos.x, pos.y))
2046 continue; 2033 continue;
2047 2034
2048 altern [index++] = i; 2035 altern [index++] = i;
2049 } 2036 }
2050 2037
2609{ 2596{
2610 char flagdesc[512]; 2597 char flagdesc[512];
2611 char info2[256 * 4]; 2598 char info2[256 * 4];
2612 char *p = info; 2599 char *p = info;
2613 2600
2614 p += snprintf (p, 512, "{cnt:%d,uuid:<1.%" PRIx64 ">,name:\"%s\"%s%s,flags:[%s],type:%d}", 2601 p += snprintf (p, 512, "{cnt:%d,uuid:%s,name:\"%s\"%s%s,flags:[%s],type:%d}",
2615 count, uuid.seq, 2602 count,
2603 uuid.c_str (),
2616 &name, 2604 &name,
2617 title ? "\",title:\"" : "", 2605 title ? "\",title:\"" : "",
2618 title ? (const char *)title : "", 2606 title ? (const char *)title : "",
2619 flag_desc (flagdesc, 512), type); 2607 flag_desc (flagdesc, 512), type);
2620 2608

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines