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.32 by root, Mon Sep 11 23:53:30 2006 UTC vs.
Revision 1.46 by root, Thu Sep 14 21:16:11 2006 UTC

29#include <stdio.h> 29#include <stdio.h>
30#include <sys/types.h> 30#include <sys/types.h>
31#include <sys/uio.h> 31#include <sys/uio.h>
32#include <object.h> 32#include <object.h>
33#include <funcpoint.h> 33#include <funcpoint.h>
34#include <skills.h>
35#include <loader.h> 34#include <loader.h>
36 35
37int nrofallocobjects = 0; 36int nrofallocobjects = 0;
37static UUID uuid;
38const uint64 UUID_SKIP = 1<<19;
38 39
39object *objects; /* Pointer to the list of used objects */
40object *active_objects; /* List of active objects that need to be processed */ 40object *active_objects; /* List of active objects that need to be processed */
41 41
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, 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,
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 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
44}; 44};
50}; 50};
51int freedir[SIZEOFFREE] = { 51int freedir[SIZEOFFREE] = {
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 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,
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 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
54}; 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}
55 132
56/* 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. */
57static int 134static int
58compare_ob_value_lists_one (const object *wants, const object *has) 135compare_ob_value_lists_one (const object *wants, const object *has)
59{ 136{
255 { 332 {
256 if (inv->inv) 333 if (inv->inv)
257 sum_weight (inv); 334 sum_weight (inv);
258 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 335 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
259 } 336 }
337
260 if (op->type == CONTAINER && op->stats.Str) 338 if (op->type == CONTAINER && op->stats.Str)
261 sum = (sum * (100 - op->stats.Str)) / 100; 339 sum = (sum * (100 - op->stats.Str)) / 100;
340
262 if (op->carrying != sum) 341 if (op->carrying != sum)
263 op->carrying = sum; 342 op->carrying = sum;
343
264 return sum; 344 return sum;
265} 345}
266 346
267/** 347/**
268 * Return the outermost environment object for a given object. 348 * Return the outermost environment object for a given object.
366void 446void
367dump_all_objects (void) 447dump_all_objects (void)
368{ 448{
369 object *op; 449 object *op;
370 450
371 for (op = objects; op != NULL; op = op->next) 451 for (op = object::first; op != NULL; op = op->next)
372 { 452 {
373 dump_object (op); 453 dump_object (op);
374 fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); 454 fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg);
375 } 455 }
376} 456}
402object * 482object *
403find_object (tag_t i) 483find_object (tag_t i)
404{ 484{
405 object *op; 485 object *op;
406 486
407 for (op = objects; op != NULL; op = op->next) 487 for (op = object::first; op != NULL; op = op->next)
408 if (op->count == i) 488 if (op->count == i)
409 break; 489 break;
410 return op; 490 return op;
411} 491}
412 492
417 */ 497 */
418 498
419object * 499object *
420find_object_name (const char *str) 500find_object_name (const char *str)
421{ 501{
422 const char *name = shstr::find (str); 502 shstr_cmp str_ (str);
423 object *op; 503 object *op;
424 504
425 for (op = objects; op != NULL; op = op->next) 505 for (op = object::first; op != NULL; op = op->next)
426 if (&op->name == name) 506 if (op->name == str_)
427 break; 507 break;
428 508
429 return op; 509 return op;
430} 510}
431 511
432void 512void
433free_all_object_data () 513free_all_object_data ()
434{ 514{
435 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); 515 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
436}
437
438/*
439 * Returns the object which this object marks as being the owner.
440 * A id-scheme is used to avoid pointing to objects which have been
441 * freed and are now reused. If this is detected, the owner is
442 * set to NULL, and NULL is returned.
443 * Changed 2004-02-12 - if the player is setting at the play again
444 * prompt, he is removed, and we don't want to treat him as an owner of
445 * anything, so check removed flag. I don't expect that this should break
446 * anything - once an object is removed, it is basically dead anyways.
447 */
448object *
449object::get_owner ()
450{
451 if (!owner
452 || QUERY_FLAG (owner, FLAG_FREED)
453 || QUERY_FLAG (owner, FLAG_REMOVED))
454 owner = 0;
455
456 return owner;
457} 516}
458 517
459/* 518/*
460 * 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
461 * skill and experience objects. 520 * skill and experience objects.
531 590
532 /* What is not cleared is next, prev, and count */ 591 /* What is not cleared is next, prev, and count */
533 592
534 expmul = 1.0; 593 expmul = 1.0;
535 face = blank_face; 594 face = blank_face;
536 attacked_by_count = -1;
537 595
538 if (settings.casting_time) 596 if (settings.casting_time)
539 casting_time = -1; 597 casting_time = -1;
540} 598}
541 599
836 894
837 if (op->more != NULL) 895 if (op->more != NULL)
838 update_object (op->more, action); 896 update_object (op->more, action);
839} 897}
840 898
841static unordered_vector<object *> mortals; 899object::vector object::mortals;
842static std::vector<object *> freed; 900object::vector object::objects; // not yet used
901object *object::first;
843 902
844void object::free_mortals () 903void object::free_mortals ()
845{ 904{
846 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 905 for (AUTODECL (i, mortals.begin ()); i != mortals.end ();)
847 if ((*i)->refcnt) 906 if ((*i)->refcnt)
848 ++i; // further delay freeing 907 ++i; // further delay freeing
849 else 908 else
850 { 909 {
851 freed.push_back (*i);//D
852 //delete *i; 910 delete *i;
853 mortals.erase (i); 911 mortals.erase (i);
854 } 912 }
855 913
914 static int lastmortals = 0;//D
915
856 if (mortals.size())//D 916 if (mortals.size() != lastmortals)//D
917 {
918 lastmortals = mortals.size ();//D
857 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 919 LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D
920 }
858} 921}
859 922
860object::object () 923object::object ()
861{ 924{
862 SET_FLAG (this, FLAG_REMOVED); 925 SET_FLAG (this, FLAG_REMOVED);
863 926
864 expmul = 1.0; 927 expmul = 1.0;
865 face = blank_face; 928 face = blank_face;
866 attacked_by_count = -1;
867} 929}
868 930
869object::~object () 931object::~object ()
870{ 932{
871 free_key_values (this); 933 free_key_values (this);
872} 934}
873 935
874void object::link () 936void object::link ()
875{ 937{
876 count = ++ob_count; 938 count = ++ob_count;
939 uuid = gen_uuid ();
877 940
878 prev = 0; 941 prev = 0;
879 next = objects; 942 next = object::first;
880 943
881 if (objects) 944 if (object::first)
882 objects->prev = this; 945 object::first->prev = this;
883 946
884 objects = this; 947 object::first = this;
885} 948}
886 949
887void object::unlink () 950void object::unlink ()
888{ 951{
889 count = 0; 952 //count = 0;//D
953 if (!prev && !next) return;//D
954
955 if (this == object::first)
956 object::first = next;
890 957
891 /* Remove this object from the list of used objects */ 958 /* Remove this object from the list of used objects */
892 if (prev)
893 {
894 prev->next = next; 959 if (prev) prev->next = next;
960 if (next) next->prev = prev;
961
895 prev = 0; 962 prev = 0;
896 }
897
898 if (next)
899 {
900 next->prev = prev;
901 next = 0; 963 next = 0;
902 }
903
904 if (this == objects)
905 objects = next;
906} 964}
907 965
908object *object::create () 966object *object::create ()
909{ 967{
910 object *op;
911
912 if (freed.empty ())
913 op = new object; 968 object *op = new object;
914 else
915 {
916 // highly annoying, but the only way to get it stable right now
917 op = freed.back ();
918 freed.pop_back ();
919 op->~object ();
920 new ((void *) op) object;
921 }
922
923 op->link (); 969 op->link ();
924 return op; 970 return op;
925} 971}
926 972
927/* 973/*
993 op = tmp; 1039 op = tmp;
994 } 1040 }
995 } 1041 }
996 } 1042 }
997 1043
1044 // clear those pointers that likely might have circular references to us
998 owner = 0; 1045 owner = 0;
1046 enemy = 0;
1047 attacked_by = 0;
999 1048
1000 /* Remove object from the active list */ 1049 /* Remove object from the active list */
1001 speed = 0; 1050 speed = 0;
1002 update_ob_speed (this); 1051 update_ob_speed (this);
1003 1052
1015sub_weight (object *op, signed long weight) 1064sub_weight (object *op, signed long weight)
1016{ 1065{
1017 while (op != NULL) 1066 while (op != NULL)
1018 { 1067 {
1019 if (op->type == CONTAINER) 1068 if (op->type == CONTAINER)
1020 {
1021 weight = (signed long) (weight * (100 - op->stats.Str) / 100); 1069 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1022 } 1070
1023 op->carrying -= weight; 1071 op->carrying -= weight;
1024 op = op->env; 1072 op = op->env;
1025 } 1073 }
1026} 1074}
1027 1075
1035 */ 1083 */
1036 1084
1037void 1085void
1038remove_ob (object *op) 1086remove_ob (object *op)
1039{ 1087{
1088 object *tmp, *last = 0;
1040 object * 1089 object *otmp;
1041 tmp, *
1042 last = NULL;
1043 object *
1044 otmp;
1045 1090
1046 tag_t 1091 tag_t tag;
1047 tag;
1048 int
1049 check_walk_off; 1092 int check_walk_off;
1050 mapstruct * 1093 mapstruct *m;
1051 m;
1052 1094
1053 sint16 1095 sint16 x, y;
1054 x,
1055 y;
1056 1096
1057 if (QUERY_FLAG (op, FLAG_REMOVED)) 1097 if (QUERY_FLAG (op, FLAG_REMOVED))
1058 return; 1098 return;
1059 1099
1060 SET_FLAG (op, FLAG_REMOVED); 1100 SET_FLAG (op, FLAG_REMOVED);
1223 * This function goes through all objects below and including top, and 1263 * This function goes through all objects below and including top, and
1224 * merges op to the first matching object. 1264 * merges op to the first matching object.
1225 * If top is NULL, it is calculated. 1265 * If top is NULL, it is calculated.
1226 * 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
1227 */ 1267 */
1228
1229object * 1268object *
1230merge_ob (object *op, object *top) 1269merge_ob (object *op, object *top)
1231{ 1270{
1232 if (!op->nrof) 1271 if (!op->nrof)
1233 return 0; 1272 return 0;
1249 free_object (op); 1288 free_object (op);
1250 return top; 1289 return top;
1251 } 1290 }
1252 } 1291 }
1253 1292
1254 return NULL; 1293 return 0;
1255} 1294}
1256 1295
1257/* 1296/*
1258 * 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
1259 * job preparing multi-part monsters 1298 * job preparing multi-part monsters
1586 remove_ob (tmp); 1625 remove_ob (tmp);
1587 free_object (tmp); 1626 free_object (tmp);
1588 } 1627 }
1589 } 1628 }
1590 1629
1591 tmp1 = arch_to_object (find_archetype (arch_string)); 1630 tmp1 = arch_to_object (archetype::find (arch_string));
1592 1631
1593 tmp1->x = op->x; 1632 tmp1->x = op->x;
1594 tmp1->y = op->y; 1633 tmp1->y = op->y;
1595 insert_ob_in_map (tmp1, op->map, op, 0); 1634 insert_ob_in_map (tmp1, op->map, op, 0);
1596} 1635}
2654 2693
2655 if (tempfile == NULL) 2694 if (tempfile == NULL)
2656 { 2695 {
2657 LOG (llevError, "Error - Unable to access load object temp file\n"); 2696 LOG (llevError, "Error - Unable to access load object temp file\n");
2658 return NULL; 2697 return NULL;
2659 }; 2698 }
2699
2660 fprintf (tempfile, obstr); 2700 fprintf (tempfile, obstr);
2661 fclose (tempfile); 2701 fclose (tempfile);
2662 2702
2663 op = get_object (); 2703 op = get_object ();
2664 2704
2721 * The returned string is shared. 2761 * The returned string is shared.
2722 */ 2762 */
2723const char * 2763const char *
2724get_ob_key_value (const object *op, const char *const key) 2764get_ob_key_value (const object *op, const char *const key)
2725{ 2765{
2726 key_value * 2766 key_value *link;
2727 link; 2767 shstr_cmp canonical_key (key);
2728 const char *
2729 canonical_key;
2730 2768
2731 canonical_key = shstr::find (key);
2732
2733 if (canonical_key == NULL) 2769 if (!canonical_key)
2734 { 2770 {
2735 /* 1. There being a field named key on any object 2771 /* 1. There being a field named key on any object
2736 * implies there'd be a shared string to find. 2772 * implies there'd be a shared string to find.
2737 * 2. Since there isn't, no object has this field. 2773 * 2. Since there isn't, no object has this field.
2738 * 3. Therefore, *this* object doesn't have this field. 2774 * 3. Therefore, *this* object doesn't have this field.
2739 */ 2775 */
2740 return NULL; 2776 return 0;
2741 } 2777 }
2742 2778
2743 /* This is copied from get_ob_key_link() above - 2779 /* This is copied from get_ob_key_link() above -
2744 * only 4 lines, and saves the function call overhead. 2780 * only 4 lines, and saves the function call overhead.
2745 */ 2781 */
2746 for (link = op->key_values; link != NULL; link = link->next) 2782 for (link = op->key_values; link; link = link->next)
2747 {
2748 if (link->key == canonical_key) 2783 if (link->key == canonical_key)
2749 {
2750 return link->value; 2784 return link->value;
2751 } 2785
2752 }
2753 return NULL; 2786 return 0;
2754} 2787}
2755 2788
2756 2789
2757/* 2790/*
2758 * Updates the canonical_key in op to value. 2791 * Updates the canonical_key in op to value.
2844 shstr key_ (key); 2877 shstr key_ (key);
2845 2878
2846 return set_ob_key_value_s (op, key_, value, add_key); 2879 return set_ob_key_value_s (op, key_, value, add_key);
2847} 2880}
2848 2881
2882object::depth_iterator::depth_iterator (object *container)
2883: iterator_base (container)
2884{
2885 while (item->inv)
2886 item = item->inv;
2887}
2888
2849void 2889void
2850object::deep_iterator::next () 2890object::depth_iterator::next ()
2851{ 2891{
2852 if (item->inv)
2853 item = item->inv;
2854 else if (item->below) 2892 if (item->below)
2893 {
2855 item = item->below; 2894 item = item->below;
2895
2896 while (item->inv)
2897 item = item->inv;
2898 }
2856 else 2899 else
2857 item = item->env->below; 2900 item = item->env;
2858} 2901}
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