1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
7 | * |
7 | * |
… | |
… | |
324 | sum_weight (object *op) |
324 | sum_weight (object *op) |
325 | { |
325 | { |
326 | long sum; |
326 | long sum; |
327 | object *inv; |
327 | object *inv; |
328 | |
328 | |
329 | for (sum = 0, inv = op->inv; inv != NULL; inv = inv->below) |
329 | for (sum = 0, inv = op->inv; inv; inv = inv->below) |
330 | { |
330 | { |
331 | if (inv->inv) |
331 | if (inv->inv) |
332 | sum_weight (inv); |
332 | sum_weight (inv); |
|
|
333 | |
333 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
334 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
334 | } |
335 | } |
335 | |
336 | |
336 | if (op->type == CONTAINER && op->stats.Str) |
337 | if (op->type == CONTAINER && op->stats.Str) |
337 | sum = (sum * (100 - op->stats.Str)) / 100; |
338 | sum = (sum * (100 - op->stats.Str)) / 100; |
… | |
… | |
454 | * refcounts and freeing the links. |
455 | * refcounts and freeing the links. |
455 | */ |
456 | */ |
456 | static void |
457 | static void |
457 | free_key_values (object *op) |
458 | free_key_values (object *op) |
458 | { |
459 | { |
459 | for (key_value *i = op->key_values; i != 0;) |
460 | for (key_value *i = op->key_values; i; ) |
460 | { |
461 | { |
461 | key_value *next = i->next; |
462 | key_value *next = i->next; |
462 | delete i; |
463 | delete i; |
463 | |
464 | |
464 | i = next; |
465 | i = next; |
465 | } |
466 | } |
466 | |
467 | |
467 | op->key_values = 0; |
468 | op->key_values = 0; |
468 | } |
469 | } |
469 | |
470 | |
470 | /* |
471 | object & |
471 | * copy_to first frees everything allocated by the dst object, |
472 | object::operator =(const object &src) |
472 | * and then copies the contents of itself into the second |
|
|
473 | * object, allocating what needs to be allocated. Basically, any |
|
|
474 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
|
|
475 | * if the first object is freed, the pointers in the new object |
|
|
476 | * will point at garbage. |
|
|
477 | */ |
|
|
478 | void |
|
|
479 | object::copy_to (object *dst) |
|
|
480 | { |
473 | { |
481 | bool is_freed = QUERY_FLAG (dst, FLAG_FREED); |
474 | bool is_freed = flag [FLAG_FREED]; |
482 | bool is_removed = QUERY_FLAG (dst, FLAG_REMOVED); |
475 | bool is_removed = flag [FLAG_REMOVED]; |
483 | |
476 | |
484 | *(object_copy *)dst = *this; |
477 | *(object_copy *)this = src; |
485 | |
478 | |
486 | if (is_freed) |
479 | flag [FLAG_FREED] = is_freed; |
487 | SET_FLAG (dst, FLAG_FREED); |
480 | flag [FLAG_REMOVED] = is_removed; |
488 | |
|
|
489 | if (is_removed) |
|
|
490 | SET_FLAG (dst, FLAG_REMOVED); |
|
|
491 | |
|
|
492 | if (speed < 0) |
|
|
493 | dst->speed_left = speed_left - rndm (); |
|
|
494 | |
481 | |
495 | /* Copy over key_values, if any. */ |
482 | /* Copy over key_values, if any. */ |
496 | if (key_values) |
483 | if (src.key_values) |
497 | { |
484 | { |
498 | key_value *tail = 0; |
485 | key_value *tail = 0; |
499 | key_value *i; |
|
|
500 | |
|
|
501 | dst->key_values = 0; |
486 | key_values = 0; |
502 | |
487 | |
503 | for (i = key_values; i; i = i->next) |
488 | for (key_value *i = src.key_values; i; i = i->next) |
504 | { |
489 | { |
505 | key_value *new_link = new key_value; |
490 | key_value *new_link = new key_value; |
506 | |
491 | |
507 | new_link->next = 0; |
492 | new_link->next = 0; |
508 | new_link->key = i->key; |
493 | new_link->key = i->key; |
509 | new_link->value = i->value; |
494 | new_link->value = i->value; |
510 | |
495 | |
511 | /* Try and be clever here, too. */ |
496 | /* Try and be clever here, too. */ |
512 | if (!dst->key_values) |
497 | if (!key_values) |
513 | { |
498 | { |
514 | dst->key_values = new_link; |
499 | key_values = new_link; |
515 | tail = new_link; |
500 | tail = new_link; |
516 | } |
501 | } |
517 | else |
502 | else |
518 | { |
503 | { |
519 | tail->next = new_link; |
504 | tail->next = new_link; |
520 | tail = new_link; |
505 | tail = new_link; |
521 | } |
506 | } |
522 | } |
507 | } |
523 | } |
508 | } |
|
|
509 | } |
|
|
510 | |
|
|
511 | /* |
|
|
512 | * copy_to first frees everything allocated by the dst object, |
|
|
513 | * and then copies the contents of itself into the second |
|
|
514 | * object, allocating what needs to be allocated. Basically, any |
|
|
515 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
|
|
516 | * if the first object is freed, the pointers in the new object |
|
|
517 | * will point at garbage. |
|
|
518 | */ |
|
|
519 | void |
|
|
520 | object::copy_to (object *dst) |
|
|
521 | { |
|
|
522 | *dst = *this; |
|
|
523 | |
|
|
524 | if (speed < 0) |
|
|
525 | dst->speed_left = speed_left - rndm (); |
524 | |
526 | |
525 | dst->set_speed (dst->speed); |
527 | dst->set_speed (dst->speed); |
526 | } |
528 | } |
527 | |
529 | |
528 | void |
530 | void |
… | |
… | |
606 | * UP_OBJ_FACE: only the objects face has changed. |
608 | * UP_OBJ_FACE: only the objects face has changed. |
607 | */ |
609 | */ |
608 | void |
610 | void |
609 | update_object (object *op, int action) |
611 | update_object (object *op, int action) |
610 | { |
612 | { |
611 | MoveType move_on, move_off, move_block, move_slow; |
|
|
612 | |
|
|
613 | if (op == NULL) |
613 | if (op == NULL) |
614 | { |
614 | { |
615 | /* this should never happen */ |
615 | /* this should never happen */ |
616 | LOG (llevDebug, "update_object() called for NULL object.\n"); |
616 | LOG (llevDebug, "update_object() called for NULL object.\n"); |
617 | return; |
617 | return; |
… | |
… | |
836 | |
836 | |
837 | if (flag [FLAG_IS_LINKED]) |
837 | if (flag [FLAG_IS_LINKED]) |
838 | remove_button_link (this); |
838 | remove_button_link (this); |
839 | |
839 | |
840 | if (flag [FLAG_FRIENDLY]) |
840 | if (flag [FLAG_FRIENDLY]) |
841 | { |
|
|
842 | remove_friendly_object (this); |
841 | remove_friendly_object (this); |
843 | |
|
|
844 | if (type == GOLEM |
|
|
845 | && owner |
|
|
846 | && owner->type == PLAYER |
|
|
847 | && owner->contr->ranges[range_golem] == this) |
|
|
848 | owner->contr->ranges[range_golem] = 0; |
|
|
849 | } |
|
|
850 | |
842 | |
851 | if (!flag [FLAG_REMOVED]) |
843 | if (!flag [FLAG_REMOVED]) |
852 | remove (); |
844 | remove (); |
853 | |
845 | |
854 | destroy_inv (true); |
846 | destroy_inv (true); |
… | |
… | |
1111 | return top; |
1103 | return top; |
1112 | } |
1104 | } |
1113 | } |
1105 | } |
1114 | |
1106 | |
1115 | return 0; |
1107 | return 0; |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | object::expand_tail () |
|
|
1112 | { |
|
|
1113 | if (more) |
|
|
1114 | return; |
|
|
1115 | |
|
|
1116 | object *prev = this; |
|
|
1117 | |
|
|
1118 | for (archetype *at = arch->more; at; at = at->more) |
|
|
1119 | { |
|
|
1120 | object *op = arch_to_object (at); |
|
|
1121 | |
|
|
1122 | op->name = name; |
|
|
1123 | op->name_pl = name_pl; |
|
|
1124 | op->title = title; |
|
|
1125 | |
|
|
1126 | op->head = this; |
|
|
1127 | prev->more = op; |
|
|
1128 | |
|
|
1129 | prev = op; |
|
|
1130 | } |
1116 | } |
1131 | } |
1117 | |
1132 | |
1118 | /* |
1133 | /* |
1119 | * same as insert_ob_in_map except it handles separate coordinates and does a clean |
1134 | * same as insert_ob_in_map except it handles separate coordinates and does a clean |
1120 | * job preparing multi-part monsters. |
1135 | * job preparing multi-part monsters. |
… | |
… | |
1152 | * just 'op' otherwise |
1167 | * just 'op' otherwise |
1153 | */ |
1168 | */ |
1154 | object * |
1169 | object * |
1155 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1170 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1156 | { |
1171 | { |
|
|
1172 | assert (!op->flag [FLAG_FREED]); |
|
|
1173 | |
1157 | object *tmp, *top, *floor = NULL; |
1174 | object *tmp, *top, *floor = NULL; |
1158 | |
1175 | |
1159 | if (QUERY_FLAG (op, FLAG_FREED)) |
|
|
1160 | { |
|
|
1161 | LOG (llevError, "Trying to insert freed object!\n"); |
|
|
1162 | return NULL; |
|
|
1163 | } |
|
|
1164 | |
|
|
1165 | if (!QUERY_FLAG (op, FLAG_REMOVED)) |
|
|
1166 | LOG (llevError, "Trying to insert already inserted object %s\n", op->debug_desc ()); |
|
|
1167 | |
|
|
1168 | op->remove (); |
1176 | op->remove (); |
1169 | |
1177 | |
1170 | if (!m) |
1178 | #if 0 |
1171 | { |
1179 | if (!m->active != !op->active) |
1172 | char *dump = dump_object (op); |
1180 | if (m->active) |
1173 | LOG (llevError, "Trying to insert in null-map!\n%s\n", dump); |
1181 | op->activate_recursive (); |
1174 | free (dump); |
1182 | else |
1175 | return op; |
1183 | op->deactivate_recursive (); |
1176 | } |
1184 | #endif |
1177 | |
1185 | |
1178 | if (out_of_map (m, op->x, op->y)) |
1186 | if (out_of_map (m, op->x, op->y)) |
1179 | { |
1187 | { |
1180 | char *dump = dump_object (op); |
|
|
1181 | LOG (llevError, "Trying to insert object outside the map.\n%s\n", dump); |
1188 | LOG (llevError, "Trying to insert object outside the map.\n%s\n", op->debug_desc ()); |
1182 | #ifdef MANY_CORES |
1189 | #ifdef MANY_CORES |
1183 | /* Better to catch this here, as otherwise the next use of this object |
1190 | /* Better to catch this here, as otherwise the next use of this object |
1184 | * is likely to cause a crash. Better to find out where it is getting |
1191 | * is likely to cause a crash. Better to find out where it is getting |
1185 | * improperly inserted. |
1192 | * improperly inserted. |
1186 | */ |
1193 | */ |
1187 | abort (); |
1194 | abort (); |
1188 | #endif |
1195 | #endif |
1189 | free (dump); |
|
|
1190 | return op; |
1196 | return op; |
1191 | } |
1197 | } |
1192 | |
1198 | |
1193 | if (object *more = op->more) |
1199 | if (object *more = op->more) |
1194 | { |
1200 | { |
… | |
… | |
1291 | * looks like instead of lots of conditions here. |
1297 | * looks like instead of lots of conditions here. |
1292 | * makes things faster, and effectively the same result. |
1298 | * makes things faster, and effectively the same result. |
1293 | */ |
1299 | */ |
1294 | |
1300 | |
1295 | /* Have object 'fall below' other objects that block view. |
1301 | /* Have object 'fall below' other objects that block view. |
1296 | * Unless those objects are exits, type 66 |
1302 | * Unless those objects are exits. |
1297 | * If INS_ON_TOP is used, don't do this processing |
1303 | * If INS_ON_TOP is used, don't do this processing |
1298 | * Need to find the object that in fact blocks view, otherwise |
1304 | * Need to find the object that in fact blocks view, otherwise |
1299 | * stacking is a bit odd. |
1305 | * stacking is a bit odd. |
1300 | */ |
1306 | */ |
1301 | if (!(flag & INS_ON_TOP) |
1307 | if (!(flag & INS_ON_TOP) |
1302 | && ms.flags () & P_BLOCKSVIEW |
1308 | && ms.flags () & P_BLOCKSVIEW |
1303 | && (op->face && !op->face->visibility)) |
1309 | && (op->face && !faces [op->face].visibility)) |
1304 | { |
1310 | { |
1305 | for (last = top; last != floor; last = last->below) |
1311 | for (last = top; last != floor; last = last->below) |
1306 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1312 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1307 | break; |
1313 | break; |
1308 | |
1314 | |
… | |
… | |
1434 | } |
1440 | } |
1435 | |
1441 | |
1436 | object * |
1442 | object * |
1437 | object::insert_at (object *where, object *originator, int flags) |
1443 | object::insert_at (object *where, object *originator, int flags) |
1438 | { |
1444 | { |
1439 | where->map->insert (this, where->x, where->y, originator, flags); |
1445 | return where->map->insert (this, where->x, where->y, originator, flags); |
1440 | } |
1446 | } |
1441 | |
1447 | |
1442 | /* |
1448 | /* |
1443 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1449 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1444 | * is returned contains nr objects, and the remaining parts contains |
1450 | * is returned contains nr objects, and the remaining parts contains |
… | |
… | |
1484 | * the amount of an object. If the amount reaches 0, the object |
1490 | * the amount of an object. If the amount reaches 0, the object |
1485 | * is subsequently removed and freed. |
1491 | * is subsequently removed and freed. |
1486 | * |
1492 | * |
1487 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1493 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1488 | */ |
1494 | */ |
1489 | |
|
|
1490 | object * |
1495 | object * |
1491 | decrease_ob_nr (object *op, uint32 i) |
1496 | decrease_ob_nr (object *op, uint32 i) |
1492 | { |
1497 | { |
1493 | object *tmp; |
1498 | object *tmp; |
1494 | |
1499 | |
… | |
… | |
1569 | |
1574 | |
1570 | /* |
1575 | /* |
1571 | * add_weight(object, weight) adds the specified weight to an object, |
1576 | * add_weight(object, weight) adds the specified weight to an object, |
1572 | * and also updates how much the environment(s) is/are carrying. |
1577 | * and also updates how much the environment(s) is/are carrying. |
1573 | */ |
1578 | */ |
1574 | |
|
|
1575 | void |
1579 | void |
1576 | add_weight (object *op, signed long weight) |
1580 | add_weight (object *op, signed long weight) |
1577 | { |
1581 | { |
1578 | while (op != NULL) |
1582 | while (op != NULL) |
1579 | { |
1583 | { |
… | |
… | |
1611 | * inside the object environment. |
1615 | * inside the object environment. |
1612 | * |
1616 | * |
1613 | * The function returns now pointer to inserted item, and return value can |
1617 | * The function returns now pointer to inserted item, and return value can |
1614 | * be != op, if items are merged. -Tero |
1618 | * be != op, if items are merged. -Tero |
1615 | */ |
1619 | */ |
1616 | |
|
|
1617 | object * |
1620 | object * |
1618 | object::insert (object *op) |
1621 | object::insert (object *op) |
1619 | { |
1622 | { |
1620 | object *tmp, *otmp; |
1623 | object *tmp, *otmp; |
1621 | |
1624 | |
… | |
… | |
2414 | return link->value; |
2417 | return link->value; |
2415 | |
2418 | |
2416 | return 0; |
2419 | return 0; |
2417 | } |
2420 | } |
2418 | |
2421 | |
2419 | |
|
|
2420 | /* |
2422 | /* |
2421 | * Updates the canonical_key in op to value. |
2423 | * Updates the canonical_key in op to value. |
2422 | * |
2424 | * |
2423 | * canonical_key is a shared string (value doesn't have to be). |
2425 | * canonical_key is a shared string (value doesn't have to be). |
2424 | * |
2426 | * |
… | |
… | |
2582 | } |
2584 | } |
2583 | |
2585 | |
2584 | const char * |
2586 | const char * |
2585 | object::debug_desc () const |
2587 | object::debug_desc () const |
2586 | { |
2588 | { |
2587 | static char info[256 * 4]; |
2589 | static char info[3][256 * 4]; |
|
|
2590 | static int info_idx; |
|
|
2591 | |
2588 | return debug_desc (info); |
2592 | return debug_desc (info [++info_idx % 3]); |
2589 | } |
|
|
2590 | |
|
|
2591 | const char * |
|
|
2592 | object::debug_desc2 () const |
|
|
2593 | { |
|
|
2594 | static char info[256 * 4]; |
|
|
2595 | return debug_desc (info); |
|
|
2596 | } |
2593 | } |
2597 | |
2594 | |
2598 | struct region * |
2595 | struct region * |
2599 | object::region () const |
2596 | object::region () const |
2600 | { |
2597 | { |