ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
(Generate patch)

Comparing deliantra/server/server/pets.C (file contents):
Revision 1.17 by root, Tue Dec 26 08:55:00 2006 UTC vs.
Revision 1.49 by root, Mon Dec 29 07:43:36 2008 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
20 21 * The authors can be reached via e-mail to <support@deliantra.net>
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#include <sproto.h> 25#include <sproto.h>
26 26
27/* given that 'pet' is a friendly object, this function returns a 27/* given that 'pet' is a friendly object, this function returns a
31 * this is now tilemap aware. 31 * this is now tilemap aware.
32 */ 32 */
33object * 33object *
34get_pet_enemy (object *pet, rv_vector * rv) 34get_pet_enemy (object *pet, rv_vector * rv)
35{ 35{
36 object *owner, *tmp, *attacker, *tmp3; 36 object *tmp, *attacker, *tmp3;
37 int i; 37 int i;
38 sint16 x, y; 38 sint16 x, y;
39 maptile *nm; 39 maptile *nm;
40 int search_arr[SIZEOFFREE]; 40 int search_arr[SIZEOFFREE];
41 int mflags; 41 int mflags;
42 42
43 attacker = pet->attacked_by; /*pointer to attacking enemy */ 43 attacker = pet->attacked_by; /*pointer to attacking enemy */
44 pet->attacked_by = NULL; /*clear this, since we are dealing with it */ 44 pet->attacked_by = NULL; /*clear this, since we are dealing with it */
45 45
46 if ((owner = pet->owner) != NULL) 46 object *owner = pet->owner;
47 { 47
48 /* If the owner has turned on the pet, make the pet 48 if (!owner)
49 * unfriendly.
50 */
51 if ((check_enemy (owner, rv)) == pet)
52 {
53 CLEAR_FLAG (pet, FLAG_FRIENDLY);
54 remove_friendly_object (pet);
55 pet->attack_movement &= ~PETMOVE;
56 return owner;
57 }
58 } 49 {
59 else
60 {
61 /* else the owner is no longer around, so the 50 /* the owner is no longer around, so the
62 * pet no longer needs to be friendly. 51 * pet no longer needs to be friendly.
63 */ 52 */
64 CLEAR_FLAG (pet, FLAG_FRIENDLY);
65 remove_friendly_object (pet); 53 remove_friendly_object (pet);
66 pet->attack_movement &= ~PETMOVE; 54 pet->attack_movement &= ~PETMOVE;
67 return NULL; 55 return 0;
56 }
57
58 /* If the owner has turned on the pet, make the pet
59 * unfriendly.
60 */
61 if (check_enemy (owner, rv) == pet)
68 } 62 {
63 remove_friendly_object (pet);
64 pet->attack_movement &= ~PETMOVE;
65 return owner;
66 }
67
69 /* If they are not on the same map, the pet won't be agressive */ 68 /* If they are not on the same map, the pet won't be agressive */
70 if (!on_same_map (pet, owner)) 69 //if (!on_same_map (pet, owner))
71 return NULL; 70 // return 0;
72 71
73 /* See if the pet has an existing enemy. If so, don't start a new one */ 72 /* See if the pet has an existing enemy. If so, don't start a new one */
74 if ((tmp = check_enemy (pet, rv)) != NULL) 73 if (tmp = check_enemy (pet, rv))
75 { 74 {
76 if (tmp == owner && !QUERY_FLAG (pet, FLAG_CONFUSED) && QUERY_FLAG (pet, FLAG_FRIENDLY)) 75 if (tmp == owner && !QUERY_FLAG (pet, FLAG_CONFUSED) && QUERY_FLAG (pet, FLAG_FRIENDLY))
77 /* without this check, you can actually get pets with 76 /* without this check, you can actually get pets with
78 * enemy set to owner! 77 * enemy set to owner!
79 */ 78 */
80 pet->enemy = NULL; 79 pet->enemy = 0;
81 else 80 else
82 return tmp; 81 return tmp;
83 } 82 }
83
84 get_search_arr (search_arr); 84 get_search_arr (search_arr);
85 85
86 if (owner->type == PLAYER && owner->contr->petmode > pet_normal) 86 if (owner->type == PLAYER && owner->contr->petmode > pet_normal)
87 { 87 {
88 if (owner->contr->petmode == pet_sad) 88 if (owner->contr->petmode == pet_sad)
89 { 89 {
90 tmp = find_nearest_living_creature (pet); 90 tmp = find_nearest_living_creature (pet);
91 if (tmp != NULL) 91 if (tmp != 0)
92 { 92 {
93 get_rangevector (pet, tmp, rv, 0); 93 get_rangevector (pet, tmp, rv, 0);
94 if (check_enemy (pet, rv) != NULL) 94 if (check_enemy (pet, rv) != 0)
95 return tmp; 95 return tmp;
96 else 96 else
97 pet->enemy = NULL; 97 pet->enemy = 0;
98 } 98 }
99 /* if we got here we have no enemy */ 99 /* if we got here we have no enemy */
100 /* we return NULL to avoid heading back to the owner */ 100 /* we return 0 to avoid heading back to the owner */
101 pet->enemy = NULL; 101 pet->enemy = 0;
102 return NULL; 102 return 0;
103 } 103 }
104 } 104 }
105 105
106 /* Since the pet has no existing enemy, look for anything nasty 106 /* Since the pet has no existing enemy, look for anything nasty
107 * around the owner that it should go and attack. 107 * around the owner that it should go and attack. (if the owner is
108 * still on a map)
108 */ 109 */
109 tmp3 = NULL; 110 // owners sometimes are not on a map but in the inventory of somehting else
111 if (!owner->flag [FLAG_REMOVED] && owner->map)
112 {
113 tmp3 = 0;
114
110 for (i = 0; i < SIZEOFFREE; i++) 115 for (i = 0; i < SIZEOFFREE; i++)
111 { 116 {
112 x = owner->x + freearr_x[search_arr[i]]; 117 x = owner->x + freearr_x[search_arr[i]];
113 y = owner->y + freearr_y[search_arr[i]]; 118 y = owner->y + freearr_y[search_arr[i]];
114 nm = owner->map; 119 nm = owner->map;
115 /* Only look on the space if there is something alive there. */ 120 /* Only look on the space if there is something alive there. */
116 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 121 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
122
117 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 123 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
118 { 124 {
119 for (tmp = GET_MAP_OB (nm, x, y); tmp != NULL; tmp = tmp->above) 125 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
120 {
121 object *tmp2 = tmp->head == NULL ? tmp : tmp->head;
122
123 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
124 (tmp2->type != PLAYER)) ||
125 should_arena_attack (pet, owner, tmp2))
126 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
127 { 126 {
127 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
128 128
129 if (!can_see_enemy (pet, tmp2)) 129 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
130 (tmp2->type != PLAYER)) ||
131 should_arena_attack (pet, owner, tmp2))
132 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
130 { 133 {
134
135 if (!can_see_enemy (pet, tmp2))
136 {
131 if (tmp3 != NULL) 137 if (tmp3 != 0)
132 tmp3 = tmp2; 138 tmp3 = tmp2;
133 } 139 }
134 else
135 {
136 pet->enemy = tmp2;
137 if (check_enemy (pet, rv) != NULL)
138 return tmp2;
139 else 140 else
141 {
142 pet->enemy = tmp2;
143 if (check_enemy (pet, rv) != 0)
144 return tmp2;
145 else
140 pet->enemy = NULL; 146 pet->enemy = 0;
141 } 147 }
142 } /* if this is a valid enemy */ 148 } /* if this is a valid enemy */
143 } /* for objects on this space */ 149 } /* for objects on this space */
144 } /* if there is something living on this space */ 150 } /* if there is something living on this space */
145 } /* for loop of spaces around the owner */ 151 } /* for loop of spaces around the owner */
146 152
147 /* fine, we went through the whole loop and didn't find one we could 153 /* fine, we went through the whole loop and didn't find one we could
148 see, take what we have */ 154 see, take what we have */
149 if (tmp3 != NULL) 155 if (tmp3 != 0)
150 { 156 {
151 pet->enemy = tmp3; 157 pet->enemy = tmp3;
152 if (check_enemy (pet, rv) != NULL) 158 if (check_enemy (pet, rv) != 0)
153 return tmp3; 159 return tmp3;
154 else 160 else
155 pet->enemy = NULL; 161 pet->enemy = 0;
162 }
156 } 163 }
157 164
158 /* No threat to owner, check to see if the pet has an attacker */ 165 /* No threat to owner, check to see if the pet has an attacker */
159 if (attacker) 166 if (attacker)
160 { 167 {
162 /* or otherwise non-hostile, and is an appropriate target */ 169 /* or otherwise non-hostile, and is an appropriate target */
163 if (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && on_same_map (pet, attacker)) 170 if (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && on_same_map (pet, attacker))
164 { 171 {
165 pet->enemy = attacker; 172 pet->enemy = attacker;
166 173
167 if (check_enemy (pet, rv) != NULL) 174 if (check_enemy (pet, rv) != 0)
168 return attacker; 175 return attacker;
169 else 176 else
170 pet->enemy = NULL; 177 pet->enemy = 0;
171 } 178 }
172 } 179 }
173 180
174 /* Don't have an attacker or legal enemy, so look for a new one!. 181 /* Don't have an attacker or legal enemy, so look for a new one!.
175 * This looks for one around where the pet is. Thus, you could lead 182 * This looks for one around where the pet is. Thus, you could lead
176 * a pet to danger, then take a few steps back. This code is basically 183 * a pet to danger, then take a few steps back. This code is basically
177 * the same as the code that looks around the owner. 184 * the same as the code that looks around the owner.
178 */ 185 */
179 if (owner->type == PLAYER && owner->contr->petmode != pet_defend) 186 if (owner->type == PLAYER && owner->contr->petmode != pet_defend)
180 { 187 {
181 tmp3 = NULL; 188 tmp3 = 0;
182 for (i = 0; i < SIZEOFFREE; i++) 189 for (i = 0; i < SIZEOFFREE; i++)
183 { 190 {
184 x = pet->x + freearr_x[search_arr[i]]; 191 x = pet->x + freearr_x[search_arr[i]];
185 y = pet->y + freearr_y[search_arr[i]]; 192 y = pet->y + freearr_y[search_arr[i]];
186 nm = pet->map; 193 nm = pet->map;
187 /* Only look on the space if there is something alive there. */ 194 /* Only look on the space if there is something alive there. */
188 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 195 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
189 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 196 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
190 { 197 {
191 for (tmp = GET_MAP_OB (nm, x, y); tmp != NULL; tmp = tmp->above) 198 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
192 { 199 {
193 object *tmp2 = tmp->head == NULL ? tmp : tmp->head; 200 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
194 201
195 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) && 202 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
196 (tmp2->type != PLAYER)) || 203 (tmp2->type != PLAYER)) ||
197 should_arena_attack (pet, owner, tmp2)) 204 should_arena_attack (pet, owner, tmp2))
198 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv)) 205 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
199 { 206 {
200 207
201 if (!can_see_enemy (pet, tmp2)) 208 if (!can_see_enemy (pet, tmp2))
202 { 209 {
203 if (tmp3 != NULL) 210 if (tmp3 != 0)
204 tmp3 = tmp2; 211 tmp3 = tmp2;
205 } 212 }
206 else 213 else
207 { 214 {
208 pet->enemy = tmp2; 215 pet->enemy = tmp2;
209 if (check_enemy (pet, rv) != NULL) 216 if (check_enemy (pet, rv) != 0)
210 return tmp2; 217 return tmp2;
211 else 218 else
212 pet->enemy = NULL; 219 pet->enemy = 0;
213 } 220 }
214 } /* make sure we can get to the bugger */ 221 } /* make sure we can get to the bugger */
215 } /* for objects on this space */ 222 } /* for objects on this space */
216 } /* if there is something living on this space */ 223 } /* if there is something living on this space */
217 } /* for loop of spaces around the pet */ 224 } /* for loop of spaces around the pet */
225
226 /* fine, we went through the whole loop and didn't find one we could
227 see, take what we have */
228 if (tmp3 != 0)
229 {
230 pet->enemy = tmp3;
231 if (check_enemy (pet, rv) != 0)
232 return tmp3;
233 else
234 pet->enemy = 0;
235 }
218 } /* pet in defence mode */ 236 } /* pet in defence mode */
219 237
220 /* fine, we went through the whole loop and didn't find one we could 238 object *enemy = check_enemy (pet, rv);
221 see, take what we have */ 239 // we have a summoned pet here and the owners enemy isn't set or can't
222 if (tmp3 != NULL) 240 // be reached => search for a player around us and set it as our new enemy!!
223 { 241 if (!enemy && pet->owner && pet->owner->type != PLAYER)
224 pet->enemy = tmp3; 242 enemy = get_nearest_player (pet);
225 if (check_enemy (pet, rv) != NULL)
226 return tmp3;
227 else
228 pet->enemy = NULL;
229 }
230 243
231 /* Didn't find anything - return the owner's enemy or NULL */ 244 /* Didn't find anything - return the owner's enemy or 0 */
232 return check_enemy (pet, rv); 245 return enemy;
233} 246}
234 247
235void 248void
236terminate_all_pets (object *owner) 249terminate_all_pets (object *owner)
237{ 250{
238 objectlink *obl, *next; 251 objectlink *obl, *next;
239 252
240 for (obl = first_friendly_object; obl != NULL; obl = next) 253 for (obl = first_friendly_object; obl; obl = next)
241 { 254 {
242 object *ob = obl->ob; 255 object *ob = obl->ob;
243
244 next = obl->next; 256 next = obl->next;
257
245 if (ob->owner == owner) 258 if (ob->owner == owner)
246 {
247 if (!QUERY_FLAG (ob, FLAG_REMOVED))
248 ob->remove ();
249 remove_friendly_object (ob);
250 ob->destroy (); 259 ob->drop_and_destroy ();
251 }
252 } 260 }
253} 261}
254 262
255/* 263/*
256 * Unfortunately, sometimes, the owner of a pet is in the 264 * Unfortunately, sometimes, the owner of a pet is in the
258 * Thus the map isn't loaded yet, and we have to remove 266 * Thus the map isn't loaded yet, and we have to remove
259 * the pet... 267 * the pet...
260 * Interesting enough, we don't use the passed map structure in 268 * Interesting enough, we don't use the passed map structure in
261 * this function. 269 * this function.
262 */ 270 */
263
264void 271void
265remove_all_pets (maptile *map) 272remove_all_pets (maptile *map)
266{ 273{
267 objectlink *obl, *next; 274 objectlink *obl, *next;
268 object *owner; 275 object *owner;
269 276
270 for (obl = first_friendly_object; obl != NULL; obl = next) 277 for (obl = first_friendly_object; obl; obl = next)
271 { 278 {
272 next = obl->next; 279 next = obl->next;
273 if (obl->ob->type != PLAYER && QUERY_FLAG (obl->ob, FLAG_FRIENDLY) && 280
274 (owner = obl->ob->owner) != NULL && !on_same_map (owner, obl->ob)) 281 if (obl->ob->type != PLAYER
282 && QUERY_FLAG (obl->ob, FLAG_FRIENDLY)
283 && (owner = obl->ob->owner) != 0
284 && !on_same_map (owner, obl->ob))
275 { 285 {
276 /* follow owner checks map status for us */ 286 /* follow owner checks map status for us */
277 follow_owner (obl->ob, owner); 287 follow_owner (obl->ob, owner);
278 } 288 }
279 } 289 }
280} 290}
281 291
282int 292int
283follow_owner (object *ob, object *owner) 293follow_owner (object *ob, object *owner)
284{ 294{
285 object *tmp; 295 if (owner->flag [FLAG_REMOVED])
286 int dir; 296 return 0; // do nothing if the owner is removed
287 297 else if (!owner->map || owner->map->in_memory != MAP_ACTIVE)
288 if (!QUERY_FLAG (ob, FLAG_REMOVED)) 298 LOG (llevError, "owner '%s' of the pet '%s' not on a map in memory!?\n", &owner->name, &ob->name);
289 ob->remove (); 299 else
290
291 if (owner->map == NULL)
292 {
293 LOG (llevError, "Can't follow owner (%d): no map.\n", &owner->name);
294 goto fail;
295 } 300 {
296 if (owner->map->in_memory != MAP_IN_MEMORY)
297 {
298 LOG (llevError, "Owner of the pet not on a map in memory!?\n");
299 goto fail;
300 }
301
302 dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE); 301 int dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
303 302
304 if (dir == -1) 303 if (dir >= 0)
305 {
306 LOG (llevMonster, "No space for pet to follow, freeing %s.\n", &ob->name);
307 goto fail;
308 }
309 for (tmp = ob; tmp != NULL; tmp = tmp->more)
310 {
311 tmp->x = owner->x + freearr_x[dir] + (tmp->arch == NULL ? 0 : tmp->arch->clone.x);
312 tmp->y = owner->y + freearr_y[dir] + (tmp->arch == NULL ? 0 : tmp->arch->clone.y);
313 tmp->map = owner->map;
314 if (OUT_OF_REAL_MAP (tmp->map, tmp->x, tmp->y))
315 {
316 tmp->map = get_map_from_coord (tmp->map, &tmp->x, &tmp->y);
317 } 304 {
318 } 305 owner->map->insert (ob, owner->x + freearr_x[dir], owner->y + freearr_y[dir]);
319 insert_ob_in_map (ob, ob->map, NULL, 0); 306
320 if (owner->type == PLAYER) /* Uh, I hope this is always true... */ 307 if (owner->type == PLAYER) /* Uh, I hope this is always true... */
321 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you"); 308 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you");
322 309
323 return 0; 310 return 0;
311 }
312 }
324 313
325fail:
326 remove_friendly_object (ob);
327 ob->destroy (); 314 ob->drop_and_destroy ();
328 315
329 return 1; 316 return 1;
330} 317}
331 318
332void 319void
333pet_move (object *ob) 320pet_move (object *ob)
334{ 321{
335 int dir, i; 322 int dir = 0, i;
336 sint16 dx, dy; 323 sint16 dx, dy;
337 object *ob2, *owner; 324 object *ob2, *owner;
338 maptile *m; 325 maptile *m;
339 326
340 /* Check to see if player pulled out */ 327 /* Check to see if player pulled out */
341 if ((owner = ob->owner) == NULL) 328 if ((owner = ob->owner) == NULL)
342 { 329 {
343 ob->remove (); /* Will be freed when returning */
344 remove_friendly_object (ob);
345 ob->destroy (); 330 ob->drop_and_destroy ();
346 LOG (llevMonster, "Pet: no owner, leaving.\n"); 331 LOG (llevMonster, "Pet: no owner, leaving.\n");
347 return; 332 return;
348 } 333 }
349 334
350 /* move monster into the owners map if not in the same map */ 335 /* move monster into the owners map if not in the same map
351 if (!on_same_map (ob, owner)) 336 * except when the owner is removed.
337 */
338 if (!on_same_map (ob, owner) && !owner->flag [FLAG_REMOVED])
352 { 339 {
353 follow_owner (ob, owner); 340 follow_owner (ob, owner);
354 return; 341 return;
355 } 342 }
356 /* Calculate Direction */ 343 /* Calculate Direction */
369 continue; 356 continue;
370 else 357 else
371 break; 358 break;
372 } 359 }
373 } 360 }
374 else 361 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
375 { 362 {
376 struct rv_vector rv; 363 struct rv_vector rv;
377 364
378 get_rangevector (ob, ob->owner, &rv, 0); 365 get_rangevector (ob, ob->owner, &rv, 0);
379 dir = rv.direction; 366 dir = rv.direction;
429 return; 416 return;
430 } 417 }
431 } 418 }
432 } 419 }
433 /* Try a different course */ 420 /* Try a different course */
434 dir = absdir (dir + 4 - (RANDOM () % 5) - (RANDOM () % 5)); 421 dir = absdir (dir + 4 - (rndm (5)) - (rndm (5)));
435 (void) move_ob (ob, dir, ob); 422 (void) move_ob (ob, dir, ob);
436 } 423 }
437 return; 424 return;
438} 425}
439 426
450 * is_golem is to note that this is a golem spell. 437 * is_golem is to note that this is a golem spell.
451 */ 438 */
452object * 439object *
453fix_summon_pet (archetype *at, object *op, int dir, int is_golem) 440fix_summon_pet (archetype *at, object *op, int dir, int is_golem)
454{ 441{
455 archetype *atmp;
456 object *tmp = NULL, *prev = NULL, *head = NULL; 442 object *tmp = NULL, *prev = NULL, *head = NULL;
457 443
458 for (atmp = at; atmp != NULL; atmp = atmp->more) 444 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
459 { 445 {
460 tmp = arch_to_object (atmp); 446 tmp = arch_to_object (atmp);
447
461 if (atmp == at) 448 if (atmp == at)
462 { 449 {
463 if (!is_golem) 450 if (!is_golem)
464 SET_FLAG (tmp, FLAG_MONSTER); 451 SET_FLAG (tmp, FLAG_MONSTER);
452
465 tmp->set_owner (op); 453 tmp->set_owner (op);
454
466 if (op->type == PLAYER) 455 if (op->type == PLAYER)
467 { 456 {
468 tmp->stats.exp = 0; 457 tmp->stats.exp = 0;
469 add_friendly_object (tmp); 458 add_friendly_object (tmp);
470 SET_FLAG (tmp, FLAG_FRIENDLY);
471 if (is_golem) 459 if (is_golem)
472 CLEAR_FLAG (tmp, FLAG_MONSTER); 460 CLEAR_FLAG (tmp, FLAG_MONSTER);
473 } 461 }
474 else 462 else
475 { 463 {
476 if (QUERY_FLAG (op, FLAG_FRIENDLY)) 464 if (QUERY_FLAG (op, FLAG_FRIENDLY))
477 { 465 {
478 object *owner = op->owner; 466 object *owner = op->owner;
479 467
480 if (owner != NULL) 468 if (owner)
481 { /* For now, we transfer ownership */ 469 { /* For now, we transfer ownership */
482 tmp->set_owner (owner); 470 tmp->set_owner (owner);
483 tmp->attack_movement = PETMOVE; 471 tmp->attack_movement = PETMOVE;
484 add_friendly_object (tmp); 472 add_friendly_object (tmp);
485 SET_FLAG (tmp, FLAG_FRIENDLY);
486 } 473 }
487 } 474 }
488 } 475 }
476
489 if (op->type != PLAYER || !is_golem) 477 if (op->type != PLAYER || !is_golem)
490 { 478 {
491 tmp->attack_movement = PETMOVE; 479 tmp->attack_movement = PETMOVE;
492 tmp->speed_left = -1; 480 tmp->speed_left = -1;
493 tmp->type = 0; 481 tmp->type = 0;
495 } 483 }
496 else 484 else
497 tmp->type = GOLEM; 485 tmp->type = GOLEM;
498 486
499 } 487 }
488
500 if (head == NULL) 489 if (!head)
501 head = tmp; 490 head = tmp;
491
502 tmp->x = op->x + freearr_x[dir] + tmp->arch->clone.x; 492 tmp->x = op->x + freearr_x[dir] + tmp->arch->x;
503 tmp->y = op->y + freearr_y[dir] + tmp->arch->clone.y; 493 tmp->y = op->y + freearr_y[dir] + tmp->arch->y;
504 tmp->map = op->map; 494 tmp->map = op->map;
495
505 if (tmp->invisible) 496 if (tmp->invisible)
506 tmp->invisible = 0; 497 tmp->invisible = 0;
498
507 if (head != tmp) 499 if (head != tmp)
508 tmp->head = head, prev->more = tmp; 500 tmp->head = head, prev->more = tmp;
501
509 prev = tmp; 502 prev = tmp;
510 } 503 }
504
511 head->direction = dir; 505 head->direction = dir;
512 506
513 /* need to change some monster attr to prevent problems/crashing */ 507 /* need to change some monster attr to prevent problems/crashing */
514 head->last_heal = 0; 508 head->last_heal = 0;
515 head->last_eat = 0; 509 head->last_eat = 0;
526 520
527 return head; 521 return head;
528} 522}
529 523
530/* updated this to allow more than the golem 'head' to attack */ 524/* updated this to allow more than the golem 'head' to attack */
531
532/* op is the golem to be moved. */ 525/* op is the golem to be moved. */
533
534void 526void
535move_golem (object *op) 527move_golem (object *op)
536{ 528{
537 int made_attack = 0; 529 int made_attack = 0;
538 object *tmp; 530 object *tmp;
539 531
540 if (QUERY_FLAG (op, FLAG_MONSTER)) 532 if (QUERY_FLAG (op, FLAG_MONSTER))
541 return; /* Has already been moved */ 533 return; /* Has already been moved */
542 534
543 if (op->owner == NULL) 535 if (!op->owner)
544 { 536 {
545 LOG (llevDebug, "Golem without owner destructed.\n"); 537 LOG (llevDebug, "Golem without owner destructed.\n");
546 op->remove ();
547 op->destroy (); 538 op->drop_and_destroy ();
548 return; 539 return;
549 } 540 }
550 541
551 /* It would be nice to have a cleaner way of what message to print 542 /* It would be nice to have a cleaner way of what message to print
552 * when the golem expires than these hard coded entries. 543 * when the golem expires than these hard coded entries.
556 if (--op->stats.hp < 0) 547 if (--op->stats.hp < 0)
557 { 548 {
558 if (op->msg) 549 if (op->msg)
559 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg); 550 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
560 551
561 op->owner->contr->ranges[range_golem] = 0;
562 remove_friendly_object (op);
563 op->remove ();
564 op->destroy (); 552 op->drop_and_destroy ();
565 return; 553 return;
566 } 554 }
567 555
568 /* Do golem attacks/movement for single & multisq golems. 556 /* Do golem attacks/movement for single & multisq golems.
569 * Assuming here that op is the 'head' object. Pass only op to 557 * Assuming here that op is the 'head' object. Pass only op to
587 mflags = get_map_flags (m, &m, x, y, &x, &y); 575 mflags = get_map_flags (m, &m, x, y, &x, &y);
588 576
589 if (mflags & P_OUT_OF_MAP) 577 if (mflags & P_OUT_OF_MAP)
590 continue; 578 continue;
591 579
592 for (victim = GET_MAP_OB (op->map, x, y); victim; victim = victim->above) 580 for (victim = GET_MAP_OB (m, x, y); victim; victim = victim->above)
593 if (QUERY_FLAG (victim, FLAG_ALIVE)) 581 if (QUERY_FLAG (victim, FLAG_ALIVE))
594 break; 582 break;
595 583
596 /* We used to call will_hit_self to make sure we don't 584 /* We used to call will_hit_self to make sure we don't
597 * hit ourselves, but that didn't work, and I don't really 585 * hit ourselves, but that didn't work, and I don't really
600 * but since we are not trying to dereferance that pointer, 588 * but since we are not trying to dereferance that pointer,
601 * that isn't a problem. 589 * that isn't a problem.
602 */ 590 */
603 if (victim && victim != op && victim->head != op) 591 if (victim && victim != op && victim->head != op)
604 { 592 {
605
606 /* for golems with race fields, we don't attack 593 /* for golems with race fields, we don't attack
607 * aligned races 594 * aligned races
608 */ 595 */
609 596
610 if (victim->race && op->race && strstr (op->race, victim->race)) 597 if (victim->race && op->race && strstr (op->race, victim->race))
622 attack_ob (victim, op); 609 attack_ob (victim, op);
623 made_attack = 1; 610 made_attack = 1;
624 } 611 }
625 } /* If victim */ 612 } /* If victim */
626 } 613 }
614
627 if (made_attack) 615 if (made_attack)
628 update_object (op, UP_OBJ_FACE); 616 update_object (op, UP_OBJ_FACE);
629} 617}
630 618
631/* this is a really stupid function when you get down and 619/* this is a really stupid function when you get down and
637control_golem (object *op, int dir) 625control_golem (object *op, int dir)
638{ 626{
639 op->direction = dir; 627 op->direction = dir;
640} 628}
641 629
642/* summon golem: summons a monster for 'op'. caster is the object 630/* summon golem: summons a monster for 'op'. caster is the object
643 * casting the spell, dir is the direction to place the monster, 631 * casting the spell, dir is the direction to place the monster,
644 * at is the archetype of the monster, and spob is the spell 632 * at is the archetype of the monster, and spob is the spell
645 * object. At this stage, all spob is really used for is to 633 * object. At this stage, all spob is really used for is to
646 * adjust some values in the monster. 634 * adjust some values in the monster.
647 */ 635 */
653 char buf[MAX_BUF]; 641 char buf[MAX_BUF];
654 642
655 /* Because there can be different golem spells, player may want to 643 /* Because there can be different golem spells, player may want to
656 * 'lose' their old golem. 644 * 'lose' their old golem.
657 */ 645 */
658 if (op->type == PLAYER && op->contr->ranges[range_golem]) 646 if (op->type == PLAYER && op->contr->golem)
659 { 647 {
660 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem."); 648 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem.");
661 op->contr->ranges[range_golem]->remove ();
662 op->contr->ranges[range_golem]->destroy (); 649 op->contr->golem->drop_and_destroy ();
663 op->contr->ranges[range_golem] = 0; 650 op->contr->golem = 0;
664 } 651 }
665 652
666 if (spob->other_arch) 653 if (spob->other_arch)
667 at = spob->other_arch; 654 at = spob->other_arch;
668 else if (spob->race) 655 else if (spob->race)
688 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name); 675 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
689 return 0; 676 return 0;
690 } 677 }
691 678
692 if (!dir) 679 if (!dir)
693 dir = find_free_spot (NULL, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1); 680 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
694 681
695 if (dir == -1 || ob_blocked (&at->clone, op->map, op->x + freearr_x[dir], op->y + freearr_y[dir])) 682 if (dir < 0 || at->blocked (op->map, op->x + freearr_x[dir], op->y + freearr_y[dir]))
696 { 683 {
697 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 684 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
698 return 0; 685 return 0;
699 } 686 }
687
700 /* basically want to get proper map/coordinates for this object */ 688 /* basically want to get proper map/coordinates for this object */
701 689
702 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM))) 690 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM)))
703 { 691 {
704 new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails."); 692 new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails.");
706 } 694 }
707 695
708 if (op->type == PLAYER) 696 if (op->type == PLAYER)
709 { 697 {
710 tmp->type = GOLEM; 698 tmp->type = GOLEM;
711 tmp->set_owner (op); 699 op->contr->golem = tmp;
700 /* give the player control of the golem */
712 set_spell_skill (op, caster, spob, tmp); 701 set_spell_skill (op, caster, spob, tmp);
713 op->contr->ranges[range_golem] = tmp;
714 /* give the player control of the golem */
715 op->contr->shoottype = range_golem;
716 }
717 else
718 {
719 if (QUERY_FLAG (op, FLAG_FRIENDLY))
720 {
721 object *owner = op->owner;
722
723 if (owner != NULL)
724 { /* For now, we transfer ownership */
725 tmp->set_owner (owner);
726 tmp->attack_movement = PETMOVE;
727 add_friendly_object (tmp);
728 SET_FLAG (tmp, FLAG_FRIENDLY);
729 }
730 }
731
732 SET_FLAG (tmp, FLAG_MONSTER);
733 } 702 }
734 703
735 /* make the speed positive. */ 704 /* make the speed positive. */
736 tmp->speed = FABS (tmp->speed); 705 tmp->speed = fabs (tmp->speed);
737 706
738 /* This sets the level dependencies on dam and hp for monsters */ 707 /* This sets the level dependencies on dam and hp for monsters */
739 /* players can't cope with too strong summonings. */ 708 /* players can't cope with too strong summonings. */
740 /* but monsters can. reserve these for players. */ 709 /* but monsters can. reserve these for players. */
741 if (op->type == PLAYER) 710 if (op->type == PLAYER)
746 tmp->stats.dam += SP_level_dam_adjust (caster, spob); 715 tmp->stats.dam += SP_level_dam_adjust (caster, spob);
747 else 716 else
748 tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob); 717 tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob);
749 718
750 tmp->speed += .02 * SP_level_range_adjust (caster, spob); 719 tmp->speed += .02 * SP_level_range_adjust (caster, spob);
751 tmp->speed = MIN (tmp->speed, 1.0); 720 tmp->speed = min (tmp->speed, 1.0);
752 721
753 if (spob->attacktype) 722 if (spob->attacktype)
754 tmp->attacktype = spob->attacktype; 723 tmp->attacktype = spob->attacktype;
755 } 724 }
756 725
760 729
761 /* make experience increase in proportion to the strength. 730 /* make experience increase in proportion to the strength.
762 * this is a bit simplistic - we are basically just looking at how 731 * this is a bit simplistic - we are basically just looking at how
763 * often the sp doubles and use that as the ratio. 732 * often the sp doubles and use that as the ratio.
764 */ 733 */
765 tmp->stats.exp *= 1 + (MAX (spob->stats.maxgrace, spob->stats.sp) / caster_level (caster, spob)); 734 tmp->stats.exp *= 1 + max (spob->stats.maxgrace, spob->stats.sp) / casting_level (caster, spob);
766 tmp->speed_left = 0; 735 tmp->speed_left = 0;
767 tmp->direction = dir; 736 tmp->direction = dir;
768 737
769 /* Holy spell - some additional tailoring */ 738 /* Holy spell - some additional tailoring */
770 if (god) 739 if (god)
786 if (!(tmp->attacktype & AT_PHYSICAL)) 755 if (!(tmp->attacktype & AT_PHYSICAL))
787 tmp->attacktype |= AT_PHYSICAL; 756 tmp->attacktype |= AT_PHYSICAL;
788 } 757 }
789 758
790 insert_ob_in_map (tmp, tmp->map, op, 0); 759 insert_ob_in_map (tmp, tmp->map, op, 0);
760
791 return 1; 761 return 1;
792} 762}
793 763
794 764
795/*************************************************************************** 765/***************************************************************************
801/* Returns a monster (chosen at random) that this particular player (and his 771/* Returns a monster (chosen at random) that this particular player (and his
802 * god) find acceptable. This checks level, races allowed by god, etc 772 * god) find acceptable. This checks level, races allowed by god, etc
803 * to determine what is acceptable. 773 * to determine what is acceptable.
804 * This returns NULL if no match was found. 774 * This returns NULL if no match was found.
805 */ 775 */
806
807object * 776object *
808choose_cult_monster (object *pl, object *god, int summon_level) 777choose_cult_monster (object *pl, object *god, int summon_level)
809{ 778{
810 char buf[MAX_BUF]; 779 char buf[MAX_BUF];
811 const char *race; 780 const char *race;
866 * a valid entry, assuming nothing is available and quit. 835 * a valid entry, assuming nothing is available and quit.
867 */ 836 */
868 if (!mon_nr) 837 if (!mon_nr)
869 return NULL; 838 return NULL;
870 839
871 mon_nr = rndm (0, mon_nr - 1); 840 mon_nr = rndm (mon_nr - 1);
872 for (tobl = list->member; tobl; tobl = tobl->next) 841 for (tobl = list->member; tobl; tobl = tobl->next)
873 { 842 {
874 otmp = tobl->ob; 843 otmp = tobl->ob;
875 if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER)) 844 if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER))
876 continue; 845 continue;
846
877 if (otmp->level <= summon_level && !mon_nr--) 847 if (otmp->level <= summon_level && !mon_nr--)
878 return otmp; 848 return otmp;
879 } 849 }
850
880 /* This should not happen */ 851 /* This should not happen */
881 LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n"); 852 LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n");
882 return NULL; 853 return NULL;
883} 854}
884 855
891 862
892 if (spell_ob->other_arch) 863 if (spell_ob->other_arch)
893 summon_arch = spell_ob->other_arch; 864 summon_arch = spell_ob->other_arch;
894 else if (spell_ob->randomitems) 865 else if (spell_ob->randomitems)
895 { 866 {
896 int level = caster_level (caster, spell_ob); 867 int level = casting_level (caster, spell_ob);
897 treasure *tr, *lasttr = NULL; 868 treasure *tr, *lasttr = NULL;
898 869
899 shstr_cmp sparam (stringarg); 870 shstr_cmp sparam (stringarg);
900 871
901 /* In old code, this was a very convoluted for statement, 872 /* In old code, this was a very convoluted for statement,
902 * with all the checks in the 'for' portion itself. Much 873 * with all the checks in the 'for' portion itself. Much
903 * more readable to break some of the conditions out. 874 * more readable to break some of the conditions out.
904 */ 875 */
905 for (tr = spell_ob->randomitems->items; tr; tr = tr->next) 876 for (tr = spell_ob->randomitems->items; tr; tr = tr->next)
906 { 877 {
878 if (!tr->item)
879 continue;
880
907 if (level < tr->magic) 881 if (level < tr->magic)
908 break; 882 break;
909 883
910 lasttr = tr; 884 lasttr = tr;
911 885
912 if (tr->item->name == sparam) 886 if (tr->item->archname == sparam)
913 break;
914
915 if (!tr->next || !tr->next->item)
916 break; 887 break;
917 } 888 }
918 889
919 if (!lasttr) 890 if (!lasttr)
920 { 891 {
922 new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters."); 893 new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters.");
923 return 0; 894 return 0;
924 } 895 }
925 896
926 summon_arch = lasttr->item; 897 summon_arch = lasttr->item;
927 nrof = lasttr->nrof; 898 nrof = lasttr->nrof;
928 } 899 }
929 else if (spell_ob->race && !strcmp (spell_ob->race, "GODCULTMON")) 900 else if (spell_ob->race && !strcmp (spell_ob->race, "GODCULTMON"))
930 { 901 {
931 object *god = find_god (determine_god (op)), *mon, *owner; 902 object *god = find_god (determine_god (op)), *mon, *owner;
932 int summon_level, tries; 903 int summon_level, tries;
943 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name); 914 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name);
944 return 0; 915 return 0;
945 } 916 }
946 917
947 /* the summon level */ 918 /* the summon level */
948 summon_level = caster_level (caster, spell_ob); 919 summon_level = casting_level (caster, spell_ob);
949 if (summon_level == 0) 920 if (summon_level == 0)
950 summon_level = 1; 921 summon_level = 1;
951 922
952 tries = 0; 923 tries = 0;
953 do 924 do
962 ndir = dir; 933 ndir = dir;
963 934
964 if (!ndir) 935 if (!ndir)
965 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE); 936 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE);
966 937
967 if (ndir == -1 || ob_blocked (mon, op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir])) 938 if (ndir < 0 || mon->blocked (op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir]))
968 { 939 {
969 ndir = -1; 940 ndir = -1;
970 if (++tries == 5) 941 if (++tries == 5)
971 { 942 {
972 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 943 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
995 return 0; 966 return 0;
996 } 967 }
997 968
998 for (i = 1; i <= nrof; i++) 969 for (i = 1; i <= nrof; i++)
999 { 970 {
1000 archetype *atmp;
1001 object *prev = NULL, *head = NULL, *tmp; 971 object *prev = NULL, *head = NULL, *tmp;
1002 972
1003 if (dir) 973 if (dir)
1004 { 974 {
1005 ndir = dir; 975 ndir = dir;
1006 dir = absdir (dir + 1); 976 dir = absdir (dir + 1);
1007 } 977 }
1008 else 978 else
1009 ndir = find_free_spot (&summon_arch->clone, op->map, op->x, op->y, 1, SIZEOFFREE); 979 ndir = find_free_spot (summon_arch, op->map, op->x, op->y, 1, SIZEOFFREE);
1010 980
1011 if (ndir > 0) 981 if (ndir > 0)
1012 { 982 {
1013 x = freearr_x[ndir]; 983 x = freearr_x[ndir];
1014 y = freearr_y[ndir]; 984 y = freearr_y[ndir];
1015 } 985 }
1016 986
1017 if (ndir == -1 || ob_blocked (&summon_arch->clone, op->map, op->x + x, op->y + y)) 987 if (ndir < 0 || summon_arch->blocked (op->map, op->x + x, op->y + y))
1018 { 988 {
1019 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 989 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
1020 if (nrof > 1) 990 if (nrof > 1)
1021 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting."); 991 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting.");
1022 992
1023 return nrof > 1; 993 return nrof > 1;
1024 } 994 }
1025 995
1026 for (atmp = summon_arch; atmp != NULL; atmp = atmp->more) 996 for (archetype *atmp = summon_arch; atmp != NULL; atmp = (archetype *)atmp->more)
1027 { 997 {
1028 tmp = arch_to_object (atmp); 998 tmp = arch_to_object (atmp);
1029 if (atmp == summon_arch) 999 if (atmp == summon_arch)
1030 { 1000 {
1031 if (QUERY_FLAG (tmp, FLAG_MONSTER)) 1001 if (QUERY_FLAG (tmp, FLAG_MONSTER))
1039 if (op->type == PLAYER || QUERY_FLAG (op, FLAG_FRIENDLY)) 1009 if (op->type == PLAYER || QUERY_FLAG (op, FLAG_FRIENDLY))
1040 { 1010 {
1041 /* If this is not set, we make it friendly */ 1011 /* If this is not set, we make it friendly */
1042 if (!QUERY_FLAG (spell_ob, FLAG_MONSTER)) 1012 if (!QUERY_FLAG (spell_ob, FLAG_MONSTER))
1043 { 1013 {
1044 SET_FLAG (tmp, FLAG_FRIENDLY);
1045 add_friendly_object (tmp); 1014 add_friendly_object (tmp);
1046 tmp->stats.exp = 0; 1015 tmp->stats.exp = 0;
1047 1016
1048 if (spell_ob->attack_movement) 1017 if (spell_ob->attack_movement)
1049 tmp->attack_movement = spell_ob->attack_movement; 1018 tmp->attack_movement = spell_ob->attack_movement;
1065 tmp->head = head; 1034 tmp->head = head;
1066 prev->more = tmp; 1035 prev->more = tmp;
1067 } 1036 }
1068 1037
1069 prev = tmp; 1038 prev = tmp;
1070 tmp->x = op->x + x + tmp->arch->clone.x; 1039 tmp->x = op->x + x + tmp->arch->x;
1071 tmp->y = op->y + y + tmp->arch->clone.y; 1040 tmp->y = op->y + y + tmp->arch->y;
1072 tmp->map = op->map; 1041 tmp->map = op->map;
1073 } 1042 }
1074 1043
1075 head->direction = freedir[ndir]; 1044 head->direction = freedir[ndir];
1076 head->stats.exp = 0; 1045 head->stats.exp = 0;
1077 head = insert_ob_in_map (head, head->map, op, 0); 1046 head = insert_ob_in_map (head, head->map, op, 0);
1078 1047
1079 if (head && head->randomitems) 1048 if (head && head->randomitems)
1080 { 1049 {
1081 object *tmp;
1082
1083 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0); 1050 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0);
1051
1084 for (tmp = head->inv; tmp; tmp = tmp->below) 1052 for (object *tmp = head->inv; tmp; tmp = tmp->below)
1085 if (!tmp->nrof)
1086 SET_FLAG (tmp, FLAG_NO_DROP); 1053 SET_FLAG (tmp, FLAG_DESTROY_ON_DEATH);
1087 } 1054 }
1088 } /* for i < nrof */ 1055 } /* for i < nrof */
1089 1056
1090 return 1; 1057 return 1;
1091}
1092
1093/* recursively look through the owner property of objects until the real owner
1094is found */
1095object *
1096get_real_owner (object *ob)
1097{
1098 object *realowner = ob;
1099
1100 if (realowner == NULL)
1101 return NULL;
1102
1103 while (realowner->owner != NULL)
1104 {
1105 realowner = realowner->owner;
1106 }
1107 return realowner;
1108} 1058}
1109 1059
1110/* determines if checks so pets don't attack players or other pets should be 1060/* determines if checks so pets don't attack players or other pets should be
1111overruled by the arena petmode */ 1061overruled by the arena petmode */
1112int 1062int
1118 if ((target == NULL) || (pet == NULL) || (owner == NULL)) 1068 if ((target == NULL) || (pet == NULL) || (owner == NULL))
1119 return 0; 1069 return 0;
1120 1070
1121 /* get the owners of itself and the target, this is to deal with pets of 1071 /* get the owners of itself and the target, this is to deal with pets of
1122 pets */ 1072 pets */
1123 rowner = get_real_owner (owner); 1073 rowner = owner->outer_owner ();
1124 if (target->type != PLAYER) 1074 towner = target->is_player () ? 0 : target->outer_owner ();
1125 {
1126 towner = get_real_owner (target);
1127 }
1128 else
1129 {
1130 towner = 0;
1131 }
1132 1075
1133 /* if the pet has now owner, exit with error */ 1076 /* if the pet has now owner, exit with error */
1134 if (rowner == NULL) 1077 if (!rowner)
1135 { 1078 {
1136 LOG (llevError, "Pet has no owner.\n"); 1079 LOG (llevError, "Pet has no owner.\n");
1137 return 0; 1080 return 0;
1138 } 1081 }
1139 1082
1140 /* if the target is not a player, and has no owner, we shouldn't be here 1083 /* if the target is not a player, and has no owner, we shouldn't be here
1141 */ 1084 */
1142 if ((towner == NULL) && (target->type != PLAYER)) 1085 if (!towner && !target->is_player ())
1143 { 1086 {
1144 LOG (llevError, "Target is not a player but has no owner. We should not be here.\n"); 1087 LOG (llevError, "Target is not a player but has no owner. We should not be here.\n");
1145 return 0; 1088 return 0;
1146 } 1089 }
1147 1090
1148 /* make sure that the owner is a player */ 1091 /* make sure that the owner is a player */
1149 if (rowner->type != PLAYER) 1092 if (!rowner->is_player ())
1150 return 0; 1093 return 0;
1151 1094
1152 /* abort if the petmode is not arena */ 1095 /* abort if the petmode is not arena */
1153 if (rowner->contr->petmode != pet_arena) 1096 if (rowner->contr->petmode != pet_arena)
1154 return 0; 1097 return 0;
1156 /* abort if the pet, it's owner, or the target is not on battleground */ 1099 /* abort if the pet, it's owner, or the target is not on battleground */
1157 if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL))) 1100 if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL)))
1158 return 0; 1101 return 0;
1159 1102
1160 /* if the target is a monster, make sure it's owner is not the same */ 1103 /* if the target is a monster, make sure it's owner is not the same */
1161 if ((target->type != PLAYER) && (rowner == towner)) 1104 if (!target->is_player () && rowner == towner)
1162 return 0; 1105 return 0;
1163 1106
1164 /* check if the target is a player which affects how it will handle 1107 /* check if the target is a player which affects how it will handle
1165 parties */ 1108 parties */
1166 if (target->type != PLAYER) 1109 if (!target->is_player ())
1167 { 1110 {
1168 /* if the target is owned by a player make sure than make sure 1111 /* if the target is owned by a player make sure than make sure
1169 it's not in the same party */ 1112 it's not in the same party */
1170 if ((towner->type == PLAYER) && (rowner->contr->party != NULL)) 1113 if (towner->is_player () && rowner->contr->party)
1171 {
1172 if (rowner->contr->party == towner->contr->party) 1114 if (rowner->contr->party == towner->contr->party)
1173 return 0; 1115 return 0;
1174 }
1175 } 1116 }
1176 else 1117 else
1177 { 1118 {
1178 /* if the target is a player make sure than make sure it's not 1119 /* if the target is a player make sure than make sure it's not
1179 in the same party */ 1120 in the same party */
1180 if (rowner->contr->party != NULL) 1121 if (rowner->contr->party)
1181 {
1182 if (rowner->contr->party == target->contr->party) 1122 if (rowner->contr->party == target->contr->party)
1183 return 0; 1123 return 0;
1184 }
1185 } 1124 }
1186 1125
1187 return 1; 1126 return 1;
1188} 1127}
1128

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines