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