|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_friend_c = |
3 | * static char *rcsid_friend_c = |
3 | * "$Id: friend.C,v 1.4 2006/09/03 00:18:39 root Exp $"; |
4 | * "$Id: friend.C,v 1.5 2006/09/10 16:00:23 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
31 | /* |
32 | /* |
32 | * Add a new friendly object to the linked list of friendly objects. |
33 | * Add a new friendly object to the linked list of friendly objects. |
33 | * No checking to see if the object is already in the linked list is done. |
34 | * No checking to see if the object is already in the linked list is done. |
34 | */ |
35 | */ |
35 | |
36 | |
|
|
37 | void |
36 | void add_friendly_object(object *op) { |
38 | add_friendly_object (object *op) |
|
|
39 | { |
37 | objectlink *ol; |
40 | objectlink *ol; |
38 | |
41 | |
39 | /* Add some error checking. This shouldn't happen, but the friendly |
42 | /* Add some error checking. This shouldn't happen, but the friendly |
40 | * object list usually isn't very long, and remove_friendly_object |
43 | * object list usually isn't very long, and remove_friendly_object |
41 | * won't remove it either. Plus, it is easier to put a breakpoint in |
44 | * won't remove it either. Plus, it is easier to put a breakpoint in |
42 | * the debugger here and see where the problem is happening. |
45 | * the debugger here and see where the problem is happening. |
43 | */ |
46 | */ |
44 | for (ol=first_friendly_object; ol!=NULL; ol=ol->next) { |
47 | for (ol = first_friendly_object; ol != NULL; ol = ol->next) |
|
|
48 | { |
45 | if (ol->ob == op) { |
49 | if (ol->ob == op) |
|
|
50 | { |
46 | LOG(llevError, "add_friendly_object: Trying to add object already on list (%s)\n", |
51 | LOG (llevError, "add_friendly_object: Trying to add object already on list (%s)\n", &op->name); |
47 | &op->name); |
|
|
48 | return; |
52 | return; |
49 | } |
53 | } |
50 | } |
54 | } |
51 | |
55 | |
52 | ol=first_friendly_object; |
56 | ol = first_friendly_object; |
53 | first_friendly_object=get_objectlink(); |
57 | first_friendly_object = get_objectlink (); |
54 | first_friendly_object->ob = op; |
58 | first_friendly_object->ob = op; |
55 | first_friendly_object->id = op->count; |
59 | first_friendly_object->id = op->count; |
56 | first_friendly_object->next=ol; |
60 | first_friendly_object->next = ol; |
57 | } |
61 | } |
58 | |
62 | |
59 | /* |
63 | /* |
60 | * Removes the specified object from the linked list of friendly objects. |
64 | * Removes the specified object from the linked list of friendly objects. |
61 | */ |
65 | */ |
62 | |
66 | |
|
|
67 | void |
63 | void remove_friendly_object(object *op) { |
68 | remove_friendly_object (object *op) |
|
|
69 | { |
64 | objectlink *obj; |
70 | objectlink *obj; |
65 | |
71 | |
66 | CLEAR_FLAG(op,FLAG_FRIENDLY); |
72 | CLEAR_FLAG (op, FLAG_FRIENDLY); |
67 | |
73 | |
68 | if (!first_friendly_object) { |
74 | if (!first_friendly_object) |
|
|
75 | { |
69 | LOG(llevError,"remove_friendly_object called with empty friendly list, remove ob=%s\n", &op->name); |
76 | LOG (llevError, "remove_friendly_object called with empty friendly list, remove ob=%s\n", &op->name); |
70 | return; |
77 | return; |
71 | } |
78 | } |
72 | /* if the first object happens to be the one, processing is pretty |
79 | /* if the first object happens to be the one, processing is pretty |
73 | * easy. |
80 | * easy. |
74 | */ |
81 | */ |
75 | if(first_friendly_object->ob==op) { |
82 | if (first_friendly_object->ob == op) |
|
|
83 | { |
76 | obj=first_friendly_object; |
84 | obj = first_friendly_object; |
77 | first_friendly_object=obj->next; |
85 | first_friendly_object = obj->next; |
78 | free(obj); |
86 | free (obj); |
79 | } else { |
87 | } |
|
|
88 | else |
|
|
89 | { |
80 | objectlink *prev=first_friendly_object; |
90 | objectlink *prev = first_friendly_object; |
81 | |
91 | |
82 | for (obj=first_friendly_object->next; obj!=NULL; obj=obj->next) { |
92 | for (obj = first_friendly_object->next; obj != NULL; obj = obj->next) |
|
|
93 | { |
83 | if (obj->ob == op) break; |
94 | if (obj->ob == op) |
|
|
95 | break; |
84 | prev=obj; |
96 | prev = obj; |
85 | } |
97 | } |
86 | if (obj) { |
98 | if (obj) |
|
|
99 | { |
87 | /* This should not happen. But if it does, presumably the |
100 | /* This should not happen. But if it does, presumably the |
88 | * call to remove it is still valid. |
101 | * call to remove it is still valid. |
89 | */ |
102 | */ |
90 | if (obj->id != op->count) { |
103 | if (obj->id != op->count) |
|
|
104 | { |
91 | LOG(llevError,"remove_friendly_object, tags do no match, %s, %d != %d\n", |
105 | LOG (llevError, "remove_friendly_object, tags do no match, %s, %d != %d\n", |
92 | op->name?(const char *)op->name:"none", op->count, obj->id); |
106 | op->name ? (const char *) op->name : "none", op->count, obj->id); |
93 | } |
107 | } |
94 | prev->next = obj->next; |
108 | prev->next = obj->next; |
95 | free(obj); |
109 | free (obj); |
96 | } |
110 | } |
97 | } |
111 | } |
98 | } |
112 | } |
99 | |
113 | |
100 | /* |
114 | /* |
101 | * Dumps all friendly objects. Invoked in DM-mode with the G key. |
115 | * Dumps all friendly objects. Invoked in DM-mode with the G key. |
102 | */ |
116 | */ |
103 | |
117 | |
|
|
118 | void |
104 | void dump_friendly_objects(void) { |
119 | dump_friendly_objects (void) |
|
|
120 | { |
105 | objectlink *ol; |
121 | objectlink *ol; |
106 | |
122 | |
107 | for(ol=first_friendly_object;ol!=NULL;ol=ol->next) |
123 | for (ol = first_friendly_object; ol != NULL; ol = ol->next) |
108 | LOG(llevError, "%s (%d)\n", &ol->ob->name, ol->ob->count); |
124 | LOG (llevError, "%s (%d)\n", &ol->ob->name, ol->ob->count); |
109 | } |
125 | } |
110 | |
126 | |
111 | /* New function, MSW 2000-1-14 |
127 | /* New function, MSW 2000-1-14 |
112 | * It traverses the friendly list removing objects that should not be here |
128 | * It traverses the friendly list removing objects that should not be here |
113 | * (ie, do not have friendly flag set, freed, etc) |
129 | * (ie, do not have friendly flag set, freed, etc) |
114 | */ |
130 | */ |
|
|
131 | void |
115 | void clean_friendly_list(void) { |
132 | clean_friendly_list (void) |
|
|
133 | { |
116 | objectlink *obj, *prev=NULL, *next; |
134 | objectlink *obj, *prev = NULL, *next; |
117 | int count=0; |
135 | int count = 0; |
118 | |
136 | |
119 | for (obj=first_friendly_object; obj!=NULL; obj=next) { |
137 | for (obj = first_friendly_object; obj != NULL; obj = next) |
|
|
138 | { |
120 | next=obj->next; |
139 | next = obj->next; |
121 | if (QUERY_FLAG(obj->ob, FLAG_FREED) || |
140 | if (QUERY_FLAG (obj->ob, FLAG_FREED) || !QUERY_FLAG (obj->ob, FLAG_FRIENDLY) || (obj->id != obj->ob->count)) |
122 | !QUERY_FLAG(obj->ob, FLAG_FRIENDLY) || |
141 | { |
123 | (obj->id != obj->ob->count)) { |
|
|
124 | if (prev) { |
142 | if (prev) |
|
|
143 | { |
125 | prev->next = obj->next; |
144 | prev->next = obj->next; |
126 | } |
145 | } |
127 | else { |
146 | else |
|
|
147 | { |
128 | first_friendly_object = obj->next; |
148 | first_friendly_object = obj->next; |
129 | } |
149 | } |
130 | count++; |
150 | count++; |
131 | free(obj); |
151 | free (obj); |
132 | } |
152 | } |
133 | /* If we removed the object, then prev is still valid. */ |
153 | /* If we removed the object, then prev is still valid. */ |
|
|
154 | else |
134 | else prev=obj; |
155 | prev = obj; |
135 | } |
156 | } |
136 | if (count) |
157 | if (count) |
137 | LOG(llevDebug,"clean_friendly_list: Removed %d bogus links\n", count); |
158 | LOG (llevDebug, "clean_friendly_list: Removed %d bogus links\n", count); |
138 | } |
159 | } |
139 | |
160 | |
140 | /* Checks if the given object is already in the friendly list or not |
161 | /* Checks if the given object is already in the friendly list or not |
141 | * Lauwenmark - 31/07/05 |
162 | * Lauwenmark - 31/07/05 |
142 | */ |
163 | */ |
|
|
164 | int |
143 | int is_friendly(const object* op) |
165 | is_friendly (const object *op) |
144 | { |
166 | { |
145 | objectlink *ol; |
167 | objectlink *ol; |
146 | |
168 | |
147 | for(ol=first_friendly_object;ol!=NULL;ol=ol->next) |
169 | for (ol = first_friendly_object; ol != NULL; ol = ol->next) |
148 | if (ol->ob == op) |
170 | if (ol->ob == op) |
149 | return 1; |
171 | return 1; |
150 | |
172 | |
151 | return 0; |
173 | return 0; |
152 | } |
174 | } |
153 | |
|
|