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.34 by root, Tue Sep 12 01:09:53 2006 UTC vs.
Revision 1.45 by root, Thu Sep 14 18:13:01 2006 UTC

32#include <object.h> 32#include <object.h>
33#include <funcpoint.h> 33#include <funcpoint.h>
34#include <loader.h> 34#include <loader.h>
35 35
36int nrofallocobjects = 0; 36int nrofallocobjects = 0;
37static UUID uuid;
38const uint64 UUID_SKIP = 1<<19;
37 39
38object *objects; /* Pointer to the list of used objects */
39object *active_objects; /* List of active objects that need to be processed */ 40object *active_objects; /* List of active objects that need to be processed */
40 41
41short 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, 42short 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 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
43}; 44};
49}; 50};
50int freedir[SIZEOFFREE] = { 51int freedir[SIZEOFFREE] = {
51 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, 52 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,
52 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 53 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
53}; 54};
55
56static void
57write_uuid (void)
58{
59 char filename1[MAX_BUF], filename2[MAX_BUF];
60
61 sprintf (filename1, "%s/uuid", settings.localdir);
62 sprintf (filename2, "%s/uuid~", settings.localdir);
63
64 FILE *fp;
65
66 if (!(fp = fopen (filename2, "w")))
67 {
68 LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2);
69 return;
70 }
71
72 fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2);
73 fclose (fp);
74 rename (filename2, filename1);
75}
76
77static void
78read_uuid (void)
79{
80 char filename[MAX_BUF];
81
82 sprintf (filename, "%s/uuid", settings.localdir);
83
84 FILE *fp;
85
86 if (!(fp = fopen (filename, "r")))
87 {
88 if (errno == ENOENT)
89 {
90 LOG (llevInfo, "RESET uid to 1\n");
91 uuid.seq = 0;
92 write_uuid ();
93 return;
94 }
95
96 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
97 _exit (1);
98 }
99
100 int version;
101 unsigned long long uid;
102 if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1)
103 {
104 LOG (llevError, "FATAL: error reading uid from %s!\n", filename);
105 _exit (1);
106 }
107
108 uuid.seq = uid;
109 write_uuid ();
110 LOG (llevDebug, "read UID: %lld\n", uid);
111 fclose (fp);
112}
113
114UUID
115gen_uuid ()
116{
117 UUID uid;
118
119 uid.seq = ++uuid.seq;
120
121 if (!(uuid.seq & (UUID_SKIP - 1)))
122 write_uuid ();
123
124 return uid;
125}
126
127void
128init_uuid ()
129{
130 read_uuid ();
131}
54 132
55/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 133/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
56static int 134static int
57compare_ob_value_lists_one (const object *wants, const object *has) 135compare_ob_value_lists_one (const object *wants, const object *has)
58{ 136{
254 { 332 {
255 if (inv->inv) 333 if (inv->inv)
256 sum_weight (inv); 334 sum_weight (inv);
257 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 335 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
258 } 336 }
337
259 if (op->type == CONTAINER && op->stats.Str) 338 if (op->type == CONTAINER && op->stats.Str)
260 sum = (sum * (100 - op->stats.Str)) / 100; 339 sum = (sum * (100 - op->stats.Str)) / 100;
340
261 if (op->carrying != sum) 341 if (op->carrying != sum)
262 op->carrying = sum; 342 op->carrying = sum;
343
263 return sum; 344 return sum;
264} 345}
265 346
266/** 347/**
267 * Return the outermost environment object for a given object. 348 * Return the outermost environment object for a given object.
365void 446void
366dump_all_objects (void) 447dump_all_objects (void)
367{ 448{
368 object *op; 449 object *op;
369 450
370 for (op = objects; op != NULL; op = op->next) 451 for (op = object::first; op != NULL; op = op->next)
371 { 452 {
372 dump_object (op); 453 dump_object (op);
373 fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); 454 fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg);
374 } 455 }
375} 456}
401object * 482object *
402find_object (tag_t i) 483find_object (tag_t i)
403{ 484{
404 object *op; 485 object *op;
405 486
406 for (op = objects; op != NULL; op = op->next) 487 for (op = object::first; op != NULL; op = op->next)
407 if (op->count == i) 488 if (op->count == i)
408 break; 489 break;
409 return op; 490 return op;
410} 491}
411 492
416 */ 497 */
417 498
418object * 499object *
419find_object_name (const char *str) 500find_object_name (const char *str)
420{ 501{
421 const char *name = shstr::find (str); 502 shstr_cmp str_ (str);
422 object *op; 503 object *op;
423 504
424 for (op = objects; op != NULL; op = op->next) 505 for (op = object::first; op != NULL; op = op->next)
425 if (&op->name == name) 506 if (op->name == str_)
426 break; 507 break;
427 508
428 return op; 509 return op;
429} 510}
430 511
431void 512void
432free_all_object_data () 513free_all_object_data ()
433{ 514{
434 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); 515 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
435}
436
437/*
438 * Returns the object which this object marks as being the owner.
439 * A id-scheme is used to avoid pointing to objects which have been
440 * freed and are now reused. If this is detected, the owner is
441 * set to NULL, and NULL is returned.
442 * Changed 2004-02-12 - if the player is setting at the play again
443 * prompt, he is removed, and we don't want to treat him as an owner of
444 * anything, so check removed flag. I don't expect that this should break
445 * anything - once an object is removed, it is basically dead anyways.
446 */
447object *
448object::get_owner ()
449{
450 if (!owner
451 || QUERY_FLAG (owner, FLAG_FREED)
452 || QUERY_FLAG (owner, FLAG_REMOVED))
453 owner = 0;
454
455 return owner;
456} 516}
457 517
458/* 518/*
459 * 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
460 * skill and experience objects. 520 * skill and experience objects.
530 590
531 /* What is not cleared is next, prev, and count */ 591 /* What is not cleared is next, prev, and count */
532 592
533 expmul = 1.0; 593 expmul = 1.0;
534 face = blank_face; 594 face = blank_face;
535 attacked_by_count = -1;
536 595
537 if (settings.casting_time) 596 if (settings.casting_time)
538 casting_time = -1; 597 casting_time = -1;
539} 598}
540 599
835 894
836 if (op->more != NULL) 895 if (op->more != NULL)
837 update_object (op->more, action); 896 update_object (op->more, action);
838} 897}
839 898
840static unordered_vector<object *> mortals; 899object::vector object::mortals;
841static std::vector<object *> freed; 900object::vector object::objects; // not yet used
901object *object::first;
842 902
843void object::free_mortals () 903void object::free_mortals ()
844{ 904{
845 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 905 for (AUTODECL (i, mortals.begin ()); i != mortals.end ();)
846 if ((*i)->refcnt) 906 if ((*i)->refcnt)
847 ++i; // further delay freeing 907 ++i; // further delay freeing
848 else 908 else
849 { 909 {
850 freed.push_back (*i);//D
851 //delete *i; 910 delete *i;
852 mortals.erase (i); 911 mortals.erase (i);
853 } 912 }
854 913
914 static int lastmortals = 0;//D
915
855 if (mortals.size())//D 916 if (mortals.size() != lastmortals)//D
917 {
918 lastmortals = mortals.size ();//D
856 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 919 LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D
920 }
857} 921}
858 922
859object::object () 923object::object ()
860{ 924{
861 SET_FLAG (this, FLAG_REMOVED); 925 SET_FLAG (this, FLAG_REMOVED);
862 926
863 expmul = 1.0; 927 expmul = 1.0;
864 face = blank_face; 928 face = blank_face;
865 attacked_by_count = -1;
866} 929}
867 930
868object::~object () 931object::~object ()
869{ 932{
870 free_key_values (this); 933 free_key_values (this);
871} 934}
872 935
873void object::link () 936void object::link ()
874{ 937{
875 count = ++ob_count; 938 count = ++ob_count;
939 uuid = gen_uuid ();
876 940
877 prev = 0; 941 prev = 0;
878 next = objects; 942 next = object::first;
879 943
880 if (objects) 944 if (object::first)
881 objects->prev = this; 945 object::first->prev = this;
882 946
883 objects = this; 947 object::first = this;
884} 948}
885 949
886void object::unlink () 950void object::unlink ()
887{ 951{
888 count = 0; 952 //count = 0;//D
953 if (!prev && !next) return;//D
954
955 if (this == object::first)
956 object::first = next;
889 957
890 /* Remove this object from the list of used objects */ 958 /* Remove this object from the list of used objects */
891 if (prev)
892 {
893 prev->next = next; 959 if (prev) prev->next = next;
960 if (next) next->prev = prev;
961
894 prev = 0; 962 prev = 0;
895 }
896
897 if (next)
898 {
899 next->prev = prev;
900 next = 0; 963 next = 0;
901 }
902
903 if (this == objects)
904 objects = next;
905} 964}
906 965
907object *object::create () 966object *object::create ()
908{ 967{
909 object *op;
910
911 if (freed.empty ())
912 op = new object; 968 object *op = new object;
913 else
914 {
915 // highly annoying, but the only way to get it stable right now
916 op = freed.back ();
917 freed.pop_back ();
918 op->~object ();
919 new ((void *) op) object;
920 }
921
922 op->link (); 969 op->link ();
923 return op; 970 return op;
924} 971}
925 972
926/* 973/*
992 op = tmp; 1039 op = tmp;
993 } 1040 }
994 } 1041 }
995 } 1042 }
996 1043
1044 // clear those pointers that likely might have circular references to us
997 owner = 0; 1045 owner = 0;
1046 enemy = 0;
1047 attacked_by = 0;
998 1048
999 /* Remove object from the active list */ 1049 /* Remove object from the active list */
1000 speed = 0; 1050 speed = 0;
1001 update_ob_speed (this); 1051 update_ob_speed (this);
1002 1052
1014sub_weight (object *op, signed long weight) 1064sub_weight (object *op, signed long weight)
1015{ 1065{
1016 while (op != NULL) 1066 while (op != NULL)
1017 { 1067 {
1018 if (op->type == CONTAINER) 1068 if (op->type == CONTAINER)
1019 {
1020 weight = (signed long) (weight * (100 - op->stats.Str) / 100); 1069 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1021 } 1070
1022 op->carrying -= weight; 1071 op->carrying -= weight;
1023 op = op->env; 1072 op = op->env;
1024 } 1073 }
1025} 1074}
1026 1075
1034 */ 1083 */
1035 1084
1036void 1085void
1037remove_ob (object *op) 1086remove_ob (object *op)
1038{ 1087{
1088 object *tmp, *last = 0;
1039 object * 1089 object *otmp;
1040 tmp, *
1041 last = NULL;
1042 object *
1043 otmp;
1044 1090
1045 tag_t 1091 tag_t tag;
1046 tag;
1047 int
1048 check_walk_off; 1092 int check_walk_off;
1049 mapstruct * 1093 mapstruct *m;
1050 m;
1051 1094
1052 sint16 1095 sint16 x, y;
1053 x,
1054 y;
1055 1096
1056 if (QUERY_FLAG (op, FLAG_REMOVED)) 1097 if (QUERY_FLAG (op, FLAG_REMOVED))
1057 return; 1098 return;
1058 1099
1059 SET_FLAG (op, FLAG_REMOVED); 1100 SET_FLAG (op, FLAG_REMOVED);
1222 * This function goes through all objects below and including top, and 1263 * This function goes through all objects below and including top, and
1223 * merges op to the first matching object. 1264 * merges op to the first matching object.
1224 * If top is NULL, it is calculated. 1265 * If top is NULL, it is calculated.
1225 * Returns pointer to object if it succeded in the merge, otherwise NULL 1266 * Returns pointer to object if it succeded in the merge, otherwise NULL
1226 */ 1267 */
1227
1228object * 1268object *
1229merge_ob (object *op, object *top) 1269merge_ob (object *op, object *top)
1230{ 1270{
1231 if (!op->nrof) 1271 if (!op->nrof)
1232 return 0; 1272 return 0;
1248 free_object (op); 1288 free_object (op);
1249 return top; 1289 return top;
1250 } 1290 }
1251 } 1291 }
1252 1292
1253 return NULL; 1293 return 0;
1254} 1294}
1255 1295
1256/* 1296/*
1257 * 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
1258 * job preparing multi-part monsters 1298 * job preparing multi-part monsters
2653 2693
2654 if (tempfile == NULL) 2694 if (tempfile == NULL)
2655 { 2695 {
2656 LOG (llevError, "Error - Unable to access load object temp file\n"); 2696 LOG (llevError, "Error - Unable to access load object temp file\n");
2657 return NULL; 2697 return NULL;
2658 }; 2698 }
2699
2659 fprintf (tempfile, obstr); 2700 fprintf (tempfile, obstr);
2660 fclose (tempfile); 2701 fclose (tempfile);
2661 2702
2662 op = get_object (); 2703 op = get_object ();
2663 2704
2720 * The returned string is shared. 2761 * The returned string is shared.
2721 */ 2762 */
2722const char * 2763const char *
2723get_ob_key_value (const object *op, const char *const key) 2764get_ob_key_value (const object *op, const char *const key)
2724{ 2765{
2725 key_value * 2766 key_value *link;
2726 link; 2767 shstr_cmp canonical_key (key);
2727 const char *
2728 canonical_key;
2729 2768
2730 canonical_key = shstr::find (key);
2731
2732 if (canonical_key == NULL) 2769 if (!canonical_key)
2733 { 2770 {
2734 /* 1. There being a field named key on any object 2771 /* 1. There being a field named key on any object
2735 * implies there'd be a shared string to find. 2772 * implies there'd be a shared string to find.
2736 * 2. Since there isn't, no object has this field. 2773 * 2. Since there isn't, no object has this field.
2737 * 3. Therefore, *this* object doesn't have this field. 2774 * 3. Therefore, *this* object doesn't have this field.
2738 */ 2775 */
2739 return NULL; 2776 return 0;
2740 } 2777 }
2741 2778
2742 /* This is copied from get_ob_key_link() above - 2779 /* This is copied from get_ob_key_link() above -
2743 * only 4 lines, and saves the function call overhead. 2780 * only 4 lines, and saves the function call overhead.
2744 */ 2781 */
2745 for (link = op->key_values; link != NULL; link = link->next) 2782 for (link = op->key_values; link; link = link->next)
2746 {
2747 if (link->key == canonical_key) 2783 if (link->key == canonical_key)
2748 {
2749 return link->value; 2784 return link->value;
2750 } 2785
2751 }
2752 return NULL; 2786 return 0;
2753} 2787}
2754 2788
2755 2789
2756/* 2790/*
2757 * Updates the canonical_key in op to value. 2791 * Updates the canonical_key in op to value.
2864 } 2898 }
2865 else 2899 else
2866 item = item->env; 2900 item = item->env;
2867} 2901}
2868 2902
2903// return a suitable string describing an objetc in enough detail to find it
2904const char *
2905object::debug_desc (char *info) const
2906{
2907 char info2[256 * 3];
2908 char *p = info;
2909
2910 p += snprintf (p, 256, "%d=\"%s%s%s\"",
2911 count,
2912 &name,
2913 title ? " " : "",
2914 title ? (const char *)title : "");
2915
2916 if (env)
2917 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2918
2919 if (map)
2920 p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y);
2921
2922 return info;
2923}
2924
2925const char *
2926object::debug_desc () const
2927{
2928 static char info[256 * 3];
2929 return debug_desc (info);
2930}
2931

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines