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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines