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 | * |
… | |
… | |
454 | * refcounts and freeing the links. |
454 | * refcounts and freeing the links. |
455 | */ |
455 | */ |
456 | static void |
456 | static void |
457 | free_key_values (object *op) |
457 | free_key_values (object *op) |
458 | { |
458 | { |
459 | for (key_value *i = op->key_values; i != 0;) |
459 | for (key_value *i = op->key_values; i; ) |
460 | { |
460 | { |
461 | key_value *next = i->next; |
461 | key_value *next = i->next; |
462 | delete i; |
462 | delete i; |
463 | |
463 | |
464 | i = next; |
464 | i = next; |
465 | } |
465 | } |
466 | |
466 | |
467 | op->key_values = 0; |
467 | op->key_values = 0; |
468 | } |
468 | } |
469 | |
469 | |
470 | /* |
470 | object & |
471 | * copy_to first frees everything allocated by the dst object, |
471 | 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 | { |
472 | { |
481 | bool is_freed = QUERY_FLAG (dst, FLAG_FREED); |
473 | bool is_freed = flag [FLAG_FREED]; |
482 | bool is_removed = QUERY_FLAG (dst, FLAG_REMOVED); |
474 | bool is_removed = flag [FLAG_REMOVED]; |
483 | |
475 | |
484 | *(object_copy *)dst = *this; |
476 | *(object_copy *)this = src; |
485 | |
477 | |
486 | if (is_freed) |
478 | flag [FLAG_FREED] = is_freed; |
487 | SET_FLAG (dst, FLAG_FREED); |
479 | 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 | |
480 | |
495 | /* Copy over key_values, if any. */ |
481 | /* Copy over key_values, if any. */ |
496 | if (key_values) |
482 | if (src.key_values) |
497 | { |
483 | { |
498 | key_value *tail = 0; |
484 | key_value *tail = 0; |
499 | key_value *i; |
|
|
500 | |
|
|
501 | dst->key_values = 0; |
485 | key_values = 0; |
502 | |
486 | |
503 | for (i = key_values; i; i = i->next) |
487 | for (key_value *i = src.key_values; i; i = i->next) |
504 | { |
488 | { |
505 | key_value *new_link = new key_value; |
489 | key_value *new_link = new key_value; |
506 | |
490 | |
507 | new_link->next = 0; |
491 | new_link->next = 0; |
508 | new_link->key = i->key; |
492 | new_link->key = i->key; |
509 | new_link->value = i->value; |
493 | new_link->value = i->value; |
510 | |
494 | |
511 | /* Try and be clever here, too. */ |
495 | /* Try and be clever here, too. */ |
512 | if (!dst->key_values) |
496 | if (!key_values) |
513 | { |
497 | { |
514 | dst->key_values = new_link; |
498 | key_values = new_link; |
515 | tail = new_link; |
499 | tail = new_link; |
516 | } |
500 | } |
517 | else |
501 | else |
518 | { |
502 | { |
519 | tail->next = new_link; |
503 | tail->next = new_link; |
520 | tail = new_link; |
504 | tail = new_link; |
521 | } |
505 | } |
522 | } |
506 | } |
523 | } |
507 | } |
|
|
508 | } |
|
|
509 | |
|
|
510 | /* |
|
|
511 | * copy_to first frees everything allocated by the dst object, |
|
|
512 | * and then copies the contents of itself into the second |
|
|
513 | * object, allocating what needs to be allocated. Basically, any |
|
|
514 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
|
|
515 | * if the first object is freed, the pointers in the new object |
|
|
516 | * will point at garbage. |
|
|
517 | */ |
|
|
518 | void |
|
|
519 | object::copy_to (object *dst) |
|
|
520 | { |
|
|
521 | *dst = *this; |
|
|
522 | |
|
|
523 | if (speed < 0) |
|
|
524 | dst->speed_left = speed_left - rndm (); |
524 | |
525 | |
525 | dst->set_speed (dst->speed); |
526 | dst->set_speed (dst->speed); |
526 | } |
527 | } |
527 | |
528 | |
528 | void |
529 | void |
… | |
… | |
834 | |
835 | |
835 | if (flag [FLAG_IS_LINKED]) |
836 | if (flag [FLAG_IS_LINKED]) |
836 | remove_button_link (this); |
837 | remove_button_link (this); |
837 | |
838 | |
838 | if (flag [FLAG_FRIENDLY]) |
839 | if (flag [FLAG_FRIENDLY]) |
839 | { |
|
|
840 | remove_friendly_object (this); |
840 | remove_friendly_object (this); |
841 | |
|
|
842 | if (type == GOLEM |
|
|
843 | && owner |
|
|
844 | && owner->type == PLAYER |
|
|
845 | && owner->contr->ranges[range_golem] == this) |
|
|
846 | owner->contr->ranges[range_golem] = 0; |
|
|
847 | } |
|
|
848 | |
841 | |
849 | if (!flag [FLAG_REMOVED]) |
842 | if (!flag [FLAG_REMOVED]) |
850 | remove (); |
843 | remove (); |
851 | |
844 | |
852 | destroy_inv (true); |
845 | destroy_inv (true); |
… | |
… | |
1109 | return top; |
1102 | return top; |
1110 | } |
1103 | } |
1111 | } |
1104 | } |
1112 | |
1105 | |
1113 | return 0; |
1106 | return 0; |
|
|
1107 | } |
|
|
1108 | |
|
|
1109 | void |
|
|
1110 | object::expand_tail () |
|
|
1111 | { |
|
|
1112 | if (more) |
|
|
1113 | return; |
|
|
1114 | |
|
|
1115 | object *prev = this; |
|
|
1116 | |
|
|
1117 | for (archetype *at = arch->more; at; at = at->more) |
|
|
1118 | { |
|
|
1119 | object *op = arch_to_object (at); |
|
|
1120 | |
|
|
1121 | op->name = name; |
|
|
1122 | op->name_pl = name_pl; |
|
|
1123 | op->title = title; |
|
|
1124 | |
|
|
1125 | op->head = this; |
|
|
1126 | prev->more = op; |
|
|
1127 | |
|
|
1128 | prev = op; |
|
|
1129 | } |
1114 | } |
1130 | } |
1115 | |
1131 | |
1116 | /* |
1132 | /* |
1117 | * same as insert_ob_in_map except it handles separate coordinates and does a clean |
1133 | * same as insert_ob_in_map except it handles separate coordinates and does a clean |
1118 | * job preparing multi-part monsters. |
1134 | * job preparing multi-part monsters. |
… | |
… | |
1150 | * just 'op' otherwise |
1166 | * just 'op' otherwise |
1151 | */ |
1167 | */ |
1152 | object * |
1168 | object * |
1153 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1169 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1154 | { |
1170 | { |
|
|
1171 | assert (!op->flag [FLAG_FREED]); |
|
|
1172 | |
1155 | object *tmp, *top, *floor = NULL; |
1173 | object *tmp, *top, *floor = NULL; |
1156 | |
1174 | |
1157 | if (QUERY_FLAG (op, FLAG_FREED)) |
|
|
1158 | { |
|
|
1159 | LOG (llevError, "Trying to insert freed object!\n"); |
|
|
1160 | return NULL; |
|
|
1161 | } |
|
|
1162 | |
|
|
1163 | if (!QUERY_FLAG (op, FLAG_REMOVED)) |
|
|
1164 | LOG (llevError, "Trying to insert already inserted object %s\n", op->debug_desc ()); |
|
|
1165 | |
|
|
1166 | op->remove (); |
1175 | op->remove (); |
1167 | |
1176 | |
1168 | if (!m) |
1177 | #if 0 |
1169 | { |
1178 | if (!m->active != !op->active) |
1170 | char *dump = dump_object (op); |
1179 | if (m->active) |
1171 | LOG (llevError, "Trying to insert in null-map!\n%s\n", dump); |
1180 | op->activate_recursive (); |
1172 | free (dump); |
1181 | else |
1173 | return op; |
1182 | op->deactivate_recursive (); |
1174 | } |
1183 | #endif |
1175 | |
1184 | |
1176 | if (out_of_map (m, op->x, op->y)) |
1185 | if (out_of_map (m, op->x, op->y)) |
1177 | { |
1186 | { |
1178 | char *dump = dump_object (op); |
|
|
1179 | LOG (llevError, "Trying to insert object outside the map.\n%s\n", dump); |
1187 | LOG (llevError, "Trying to insert object outside the map.\n%s\n", op->debug_desc ()); |
1180 | #ifdef MANY_CORES |
1188 | #ifdef MANY_CORES |
1181 | /* Better to catch this here, as otherwise the next use of this object |
1189 | /* Better to catch this here, as otherwise the next use of this object |
1182 | * is likely to cause a crash. Better to find out where it is getting |
1190 | * is likely to cause a crash. Better to find out where it is getting |
1183 | * improperly inserted. |
1191 | * improperly inserted. |
1184 | */ |
1192 | */ |
1185 | abort (); |
1193 | abort (); |
1186 | #endif |
1194 | #endif |
1187 | free (dump); |
|
|
1188 | return op; |
1195 | return op; |
1189 | } |
1196 | } |
1190 | |
1197 | |
1191 | if (object *more = op->more) |
1198 | if (object *more = op->more) |
1192 | { |
1199 | { |
… | |
… | |
1429 | tmp1->x = op->x; |
1436 | tmp1->x = op->x; |
1430 | tmp1->y = op->y; |
1437 | tmp1->y = op->y; |
1431 | insert_ob_in_map (tmp1, op->map, op, 0); |
1438 | insert_ob_in_map (tmp1, op->map, op, 0); |
1432 | } |
1439 | } |
1433 | |
1440 | |
1434 | // XXX: function not returning object* |
|
|
1435 | object * |
1441 | object * |
1436 | object::insert_at (object *where, object *originator, int flags) |
1442 | object::insert_at (object *where, object *originator, int flags) |
1437 | { |
1443 | { |
1438 | where->map->insert (this, where->x, where->y, originator, flags); |
1444 | return where->map->insert (this, where->x, where->y, originator, flags); |
1439 | } |
1445 | } |
1440 | |
1446 | |
1441 | /* |
1447 | /* |
1442 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1448 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1443 | * is returned contains nr objects, and the remaining parts contains |
1449 | * is returned contains nr objects, and the remaining parts contains |
… | |
… | |
1483 | * the amount of an object. If the amount reaches 0, the object |
1489 | * the amount of an object. If the amount reaches 0, the object |
1484 | * is subsequently removed and freed. |
1490 | * is subsequently removed and freed. |
1485 | * |
1491 | * |
1486 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1492 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1487 | */ |
1493 | */ |
1488 | |
|
|
1489 | object * |
1494 | object * |
1490 | decrease_ob_nr (object *op, uint32 i) |
1495 | decrease_ob_nr (object *op, uint32 i) |
1491 | { |
1496 | { |
1492 | object *tmp; |
1497 | object *tmp; |
1493 | |
1498 | |
… | |
… | |
1568 | |
1573 | |
1569 | /* |
1574 | /* |
1570 | * add_weight(object, weight) adds the specified weight to an object, |
1575 | * add_weight(object, weight) adds the specified weight to an object, |
1571 | * and also updates how much the environment(s) is/are carrying. |
1576 | * and also updates how much the environment(s) is/are carrying. |
1572 | */ |
1577 | */ |
1573 | |
|
|
1574 | void |
1578 | void |
1575 | add_weight (object *op, signed long weight) |
1579 | add_weight (object *op, signed long weight) |
1576 | { |
1580 | { |
1577 | while (op != NULL) |
1581 | while (op != NULL) |
1578 | { |
1582 | { |
… | |
… | |
1610 | * inside the object environment. |
1614 | * inside the object environment. |
1611 | * |
1615 | * |
1612 | * The function returns now pointer to inserted item, and return value can |
1616 | * The function returns now pointer to inserted item, and return value can |
1613 | * be != op, if items are merged. -Tero |
1617 | * be != op, if items are merged. -Tero |
1614 | */ |
1618 | */ |
1615 | |
|
|
1616 | object * |
1619 | object * |
1617 | object::insert (object *op) |
1620 | object::insert (object *op) |
1618 | { |
1621 | { |
1619 | object *tmp, *otmp; |
1622 | object *tmp, *otmp; |
1620 | |
1623 | |