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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines