… | |
… | |
35 | |
35 | |
36 | int nrofallocobjects = 0; |
36 | int nrofallocobjects = 0; |
37 | static UUID uuid; |
37 | static UUID uuid; |
38 | const uint64 UUID_SKIP = 1<<19; |
38 | const uint64 UUID_SKIP = 1<<19; |
39 | |
39 | |
40 | object *objects; /* Pointer to the list of used objects */ |
|
|
41 | object *active_objects; /* List of active objects that need to be processed */ |
40 | object *active_objects; /* List of active objects that need to be processed */ |
42 | |
41 | |
43 | short 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, |
42 | short 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, |
44 | 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 |
43 | 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 |
45 | }; |
44 | }; |
… | |
… | |
447 | void |
446 | void |
448 | dump_all_objects (void) |
447 | dump_all_objects (void) |
449 | { |
448 | { |
450 | object *op; |
449 | object *op; |
451 | |
450 | |
452 | for (op = objects; op != NULL; op = op->next) |
451 | for (op = object::first; op != NULL; op = op->next) |
453 | { |
452 | { |
454 | dump_object (op); |
453 | dump_object (op); |
455 | fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); |
454 | fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); |
456 | } |
455 | } |
457 | } |
456 | } |
… | |
… | |
483 | object * |
482 | object * |
484 | find_object (tag_t i) |
483 | find_object (tag_t i) |
485 | { |
484 | { |
486 | object *op; |
485 | object *op; |
487 | |
486 | |
488 | for (op = objects; op != NULL; op = op->next) |
487 | for (op = object::first; op != NULL; op = op->next) |
489 | if (op->count == i) |
488 | if (op->count == i) |
490 | break; |
489 | break; |
491 | return op; |
490 | return op; |
492 | } |
491 | } |
493 | |
492 | |
… | |
… | |
501 | find_object_name (const char *str) |
500 | find_object_name (const char *str) |
502 | { |
501 | { |
503 | shstr_cmp str_ (str); |
502 | shstr_cmp str_ (str); |
504 | object *op; |
503 | object *op; |
505 | |
504 | |
506 | for (op = objects; op != NULL; op = op->next) |
505 | for (op = object::first; op != NULL; op = op->next) |
507 | if (op->name == str_) |
506 | if (op->name == str_) |
508 | break; |
507 | break; |
509 | |
508 | |
510 | return op; |
509 | return op; |
511 | } |
510 | } |
512 | |
511 | |
513 | void |
512 | void |
514 | free_all_object_data () |
513 | free_all_object_data () |
515 | { |
514 | { |
516 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
515 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
517 | } |
|
|
518 | |
|
|
519 | /* |
|
|
520 | * Returns the object which this object marks as being the owner. |
|
|
521 | * A id-scheme is used to avoid pointing to objects which have been |
|
|
522 | * freed and are now reused. If this is detected, the owner is |
|
|
523 | * set to NULL, and NULL is returned. |
|
|
524 | * Changed 2004-02-12 - if the player is setting at the play again |
|
|
525 | * prompt, he is removed, and we don't want to treat him as an owner of |
|
|
526 | * anything, so check removed flag. I don't expect that this should break |
|
|
527 | * anything - once an object is removed, it is basically dead anyways. |
|
|
528 | */ |
|
|
529 | object * |
|
|
530 | object::get_owner () |
|
|
531 | { |
|
|
532 | if (!owner |
|
|
533 | || QUERY_FLAG (owner, FLAG_FREED) |
|
|
534 | || QUERY_FLAG (owner, FLAG_REMOVED)) |
|
|
535 | owner = 0; |
|
|
536 | |
|
|
537 | return owner; |
|
|
538 | } |
516 | } |
539 | |
517 | |
540 | /* |
518 | /* |
541 | * Sets the owner and sets the skill and exp pointers to owner's current |
519 | * Sets the owner and sets the skill and exp pointers to owner's current |
542 | * skill and experience objects. |
520 | * skill and experience objects. |
… | |
… | |
612 | |
590 | |
613 | /* What is not cleared is next, prev, and count */ |
591 | /* What is not cleared is next, prev, and count */ |
614 | |
592 | |
615 | expmul = 1.0; |
593 | expmul = 1.0; |
616 | face = blank_face; |
594 | face = blank_face; |
617 | attacked_by_count = -1; |
|
|
618 | |
595 | |
619 | if (settings.casting_time) |
596 | if (settings.casting_time) |
620 | casting_time = -1; |
597 | casting_time = -1; |
621 | } |
598 | } |
622 | |
599 | |
… | |
… | |
917 | |
894 | |
918 | if (op->more != NULL) |
895 | if (op->more != NULL) |
919 | update_object (op->more, action); |
896 | update_object (op->more, action); |
920 | } |
897 | } |
921 | |
898 | |
922 | static unordered_vector<object *> mortals; |
899 | object::vector object::mortals; |
923 | static std::vector<object *, slice_allocator <object *> > freed; |
900 | object::vector object::objects; // not yet used |
|
|
901 | object *object::first; |
924 | |
902 | |
925 | void object::free_mortals () |
903 | void object::free_mortals () |
926 | { |
904 | { |
927 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
905 | for (AUTODECL (i, mortals.begin ()); i != mortals.end ();) |
928 | if ((*i)->refcnt) |
906 | if ((*i)->refcnt) |
929 | ++i; // further delay freeing |
907 | ++i; // further delay freeing |
930 | else |
908 | else |
931 | { |
909 | { |
932 | //freed.push_back (*i);//D |
|
|
933 | delete *i; |
910 | delete *i; |
934 | mortals.erase (i); |
911 | mortals.erase (i); |
935 | } |
912 | } |
936 | |
913 | |
937 | if (mortals.size() && 0)//D |
914 | static int lastmortals = 0;//D |
|
|
915 | |
|
|
916 | if (mortals.size() != lastmortals)//D |
|
|
917 | { |
|
|
918 | lastmortals = mortals.size ();//D |
938 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
919 | LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D |
|
|
920 | } |
939 | } |
921 | } |
940 | |
922 | |
941 | object::object () |
923 | object::object () |
942 | { |
924 | { |
943 | SET_FLAG (this, FLAG_REMOVED); |
925 | SET_FLAG (this, FLAG_REMOVED); |
944 | |
926 | |
945 | expmul = 1.0; |
927 | expmul = 1.0; |
946 | face = blank_face; |
928 | face = blank_face; |
947 | attacked_by_count = -1; |
|
|
948 | } |
929 | } |
949 | |
930 | |
950 | object::~object () |
931 | object::~object () |
951 | { |
932 | { |
952 | free_key_values (this); |
933 | free_key_values (this); |
953 | } |
934 | } |
954 | |
935 | |
955 | void object::link () |
936 | void object::link () |
956 | { |
937 | { |
957 | count = ++ob_count; |
938 | count = ++ob_count; |
|
|
939 | uuid = gen_uuid (); |
958 | |
940 | |
959 | prev = 0; |
941 | prev = 0; |
960 | next = objects; |
942 | next = object::first; |
961 | |
943 | |
962 | if (objects) |
944 | if (object::first) |
963 | objects->prev = this; |
945 | object::first->prev = this; |
964 | |
946 | |
965 | objects = this; |
947 | object::first = this; |
966 | } |
948 | } |
967 | |
949 | |
968 | void object::unlink () |
950 | void object::unlink () |
969 | { |
951 | { |
970 | count = 0; |
952 | //count = 0;//D |
971 | uuid = gen_uuid (); |
953 | if (!prev && !next) return;//D |
972 | |
954 | |
973 | if (this == objects) |
955 | if (this == object::first) |
974 | objects = next; |
956 | object::first = next; |
975 | |
957 | |
976 | /* Remove this object from the list of used objects */ |
958 | /* Remove this object from the list of used objects */ |
977 | if (prev) |
|
|
978 | { |
|
|
979 | prev->next = next; |
959 | if (prev) prev->next = next; |
|
|
960 | if (next) next->prev = prev; |
|
|
961 | |
980 | prev = 0; |
962 | prev = 0; |
981 | } |
|
|
982 | |
|
|
983 | if (next) |
|
|
984 | { |
|
|
985 | next->prev = prev; |
|
|
986 | next = 0; |
963 | next = 0; |
987 | } |
|
|
988 | } |
964 | } |
989 | |
965 | |
990 | object *object::create () |
966 | object *object::create () |
991 | { |
967 | { |
992 | object *op; |
|
|
993 | |
|
|
994 | if (freed.empty ()) |
|
|
995 | op = new object; |
968 | object *op = new object; |
996 | else |
|
|
997 | { |
|
|
998 | // highly annoying, but the only way to get it stable right now |
|
|
999 | op = freed.back (); |
|
|
1000 | freed.pop_back (); |
|
|
1001 | op->~object (); |
|
|
1002 | new ((void *) op) object; |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | op->link (); |
969 | op->link (); |
1006 | return op; |
970 | return op; |
1007 | } |
971 | } |
1008 | |
972 | |
1009 | /* |
973 | /* |
… | |
… | |
1075 | op = tmp; |
1039 | op = tmp; |
1076 | } |
1040 | } |
1077 | } |
1041 | } |
1078 | } |
1042 | } |
1079 | |
1043 | |
|
|
1044 | // clear those pointers that likely might have circular references to us |
1080 | owner = 0; |
1045 | owner = 0; |
|
|
1046 | enemy = 0; |
|
|
1047 | attacked_by = 0; |
1081 | |
1048 | |
1082 | /* Remove object from the active list */ |
1049 | /* Remove object from the active list */ |
1083 | speed = 0; |
1050 | speed = 0; |
1084 | update_ob_speed (this); |
1051 | update_ob_speed (this); |
1085 | |
1052 | |
… | |
… | |
1097 | sub_weight (object *op, signed long weight) |
1064 | sub_weight (object *op, signed long weight) |
1098 | { |
1065 | { |
1099 | while (op != NULL) |
1066 | while (op != NULL) |
1100 | { |
1067 | { |
1101 | if (op->type == CONTAINER) |
1068 | if (op->type == CONTAINER) |
1102 | { |
|
|
1103 | weight = (signed long) (weight * (100 - op->stats.Str) / 100); |
1069 | weight = (signed long) (weight * (100 - op->stats.Str) / 100); |
1104 | } |
1070 | |
1105 | op->carrying -= weight; |
1071 | op->carrying -= weight; |
1106 | op = op->env; |
1072 | op = op->env; |
1107 | } |
1073 | } |
1108 | } |
1074 | } |
1109 | |
1075 | |
… | |
… | |
1117 | */ |
1083 | */ |
1118 | |
1084 | |
1119 | void |
1085 | void |
1120 | remove_ob (object *op) |
1086 | remove_ob (object *op) |
1121 | { |
1087 | { |
|
|
1088 | object *tmp, *last = 0; |
1122 | object * |
1089 | object *otmp; |
1123 | tmp, * |
|
|
1124 | last = NULL; |
|
|
1125 | object * |
|
|
1126 | otmp; |
|
|
1127 | |
1090 | |
1128 | tag_t |
1091 | tag_t tag; |
1129 | tag; |
|
|
1130 | int |
|
|
1131 | check_walk_off; |
1092 | int check_walk_off; |
1132 | mapstruct * |
1093 | mapstruct *m; |
1133 | m; |
|
|
1134 | |
1094 | |
1135 | sint16 |
1095 | sint16 x, y; |
1136 | x, |
|
|
1137 | y; |
|
|
1138 | |
1096 | |
1139 | if (QUERY_FLAG (op, FLAG_REMOVED)) |
1097 | if (QUERY_FLAG (op, FLAG_REMOVED)) |
1140 | return; |
1098 | return; |
1141 | |
1099 | |
1142 | SET_FLAG (op, FLAG_REMOVED); |
1100 | SET_FLAG (op, FLAG_REMOVED); |
… | |
… | |
1330 | free_object (op); |
1288 | free_object (op); |
1331 | return top; |
1289 | return top; |
1332 | } |
1290 | } |
1333 | } |
1291 | } |
1334 | |
1292 | |
1335 | return NULL; |
1293 | return 0; |
1336 | } |
1294 | } |
1337 | |
1295 | |
1338 | /* |
1296 | /* |
1339 | * same as insert_ob_in_map except it handle separate coordinates and do a clean |
1297 | * same as insert_ob_in_map except it handle separate coordinates and do a clean |
1340 | * job preparing multi-part monsters |
1298 | * job preparing multi-part monsters |
… | |
… | |
1667 | remove_ob (tmp); |
1625 | remove_ob (tmp); |
1668 | free_object (tmp); |
1626 | free_object (tmp); |
1669 | } |
1627 | } |
1670 | } |
1628 | } |
1671 | |
1629 | |
1672 | tmp1 = arch_to_object (find_archetype (arch_string)); |
1630 | tmp1 = arch_to_object (archetype::find (arch_string)); |
1673 | |
1631 | |
1674 | tmp1->x = op->x; |
1632 | tmp1->x = op->x; |
1675 | tmp1->y = op->y; |
1633 | tmp1->y = op->y; |
1676 | insert_ob_in_map (tmp1, op->map, op, 0); |
1634 | insert_ob_in_map (tmp1, op->map, op, 0); |
1677 | } |
1635 | } |
… | |
… | |
2735 | |
2693 | |
2736 | if (tempfile == NULL) |
2694 | if (tempfile == NULL) |
2737 | { |
2695 | { |
2738 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2696 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2739 | return NULL; |
2697 | return NULL; |
2740 | }; |
2698 | } |
|
|
2699 | |
2741 | fprintf (tempfile, obstr); |
2700 | fprintf (tempfile, obstr); |
2742 | fclose (tempfile); |
2701 | fclose (tempfile); |
2743 | |
2702 | |
2744 | op = get_object (); |
2703 | op = get_object (); |
2745 | |
2704 | |