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.74 by root, Sat Nov 17 23:40:04 2018 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
6 8 *
7 This program is free software; you can redistribute it and/or modify 9 * 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 10 * 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 11 * Free Software Foundation, either version 3 of the License, or (at your
10 (at your option) any later version. 12 * option) any later version.
11 13 *
12 This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 17 * GNU General Public License for more details.
16 18 *
17 You should have received a copy of the GNU General Public License 19 * 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 20 * and the GNU General Public License along with this program. If not, see
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * <http://www.gnu.org/licenses/>.
20 22 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 23 * The authors can be reached via e-mail to <support@deliantra.net>
22*/ 24 */
23 25
24#include <global.h> 26#include <global.h>
25#include <sproto.h> 27#include <sproto.h>
26 28
27/* given that 'pet' is a friendly object, this function returns a 29/* given that 'pet' is a friendly object, this function returns a
31 * this is now tilemap aware. 33 * this is now tilemap aware.
32 */ 34 */
33object * 35object *
34get_pet_enemy (object *pet, rv_vector * rv) 36get_pet_enemy (object *pet, rv_vector * rv)
35{ 37{
36 object *owner, *tmp, *attacker, *tmp3; 38 object *tmp, *attacker, *tmp3;
37 int i; 39 int i;
38 sint16 x, y; 40 sint16 x, y;
39 maptile *nm; 41 maptile *nm;
40 int search_arr[SIZEOFFREE]; 42 int search_arr[SIZEOFFREE];
41 int mflags; 43 int mflags;
42 44
43 attacker = pet->attacked_by; /*pointer to attacking enemy */ 45 attacker = pet->attacked_by; /*pointer to attacking enemy */
44 pet->attacked_by = NULL; /*clear this, since we are dealing with it */ 46 pet->attacked_by = NULL; /*clear this, since we are dealing with it */
45 47
46 if ((owner = pet->owner) != NULL) 48 object *owner = pet->owner;
47 { 49
48 /* If the owner has turned on the pet, make the pet 50 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 } 51 {
59 else
60 {
61 /* else the owner is no longer around, so the 52 /* the owner is no longer around, so the
62 * pet no longer needs to be friendly. 53 * pet no longer needs to be friendly.
63 */ 54 */
64 CLEAR_FLAG (pet, FLAG_FRIENDLY);
65 remove_friendly_object (pet); 55 remove_friendly_object (pet);
66 pet->attack_movement &= ~PETMOVE; 56 pet->attack_movement &= ~PETMOVE;
67 return NULL; 57 return 0;
58 }
59
60 /* If the owner has turned on the pet, make the pet
61 * unfriendly.
62 */
63 if (check_enemy (owner, rv) == pet)
68 } 64 {
65 remove_friendly_object (pet);
66 pet->attack_movement &= ~PETMOVE;
67 return owner;
68 }
69
69 /* If they are not on the same map, the pet won't be agressive */ 70 /* If they are not on the same map, the pet won't be agressive */
70 if (!on_same_map (pet, owner)) 71 //if (!on_same_map (pet, owner))
71 return NULL; 72 // return 0;
72 73
73 /* See if the pet has an existing enemy. If so, don't start a new one */ 74 /* See if the pet has an existing enemy. If so, don't start a new one */
74 if ((tmp = check_enemy (pet, rv)) != NULL) 75 if (tmp = check_enemy (pet, rv))
75 { 76 {
76 if (tmp == owner && !QUERY_FLAG (pet, FLAG_CONFUSED) && QUERY_FLAG (pet, FLAG_FRIENDLY)) 77 if (tmp == owner && !pet->flag [FLAG_CONFUSED] && pet->flag [FLAG_FRIENDLY])
77 /* without this check, you can actually get pets with 78 /* without this check, you can actually get pets with
78 * enemy set to owner! 79 * enemy set to owner!
79 */ 80 */
80 pet->enemy = NULL; 81 pet->enemy = 0;
81 else 82 else
82 return tmp; 83 return tmp;
83 } 84 }
84 85
85 get_search_arr (search_arr); 86 get_search_arr (search_arr);
87 if (owner->type == PLAYER && owner->contr->petmode > pet_normal) 88 if (owner->type == PLAYER && owner->contr->petmode > pet_normal)
88 { 89 {
89 if (owner->contr->petmode == pet_sad) 90 if (owner->contr->petmode == pet_sad)
90 { 91 {
91 tmp = find_nearest_living_creature (pet); 92 tmp = find_nearest_living_creature (pet);
92 if (tmp != NULL) 93 if (tmp != 0)
93 { 94 {
94 get_rangevector (pet, tmp, rv, 0); 95 get_rangevector (pet, tmp, rv, 0);
95 if (check_enemy (pet, rv) != NULL) 96 if (check_enemy (pet, rv) != 0)
96 return tmp; 97 return tmp;
97 else 98 else
98 pet->enemy = NULL; 99 pet->enemy = 0;
99 } 100 }
100 /* if we got here we have no enemy */ 101 /* if we got here we have no enemy */
101 /* we return NULL to avoid heading back to the owner */ 102 /* we return 0 to avoid heading back to the owner */
102 pet->enemy = NULL; 103 pet->enemy = 0;
103 return NULL; 104 return 0;
104 } 105 }
105 } 106 }
106 107
107 /* Since the pet has no existing enemy, look for anything nasty 108 /* Since the pet has no existing enemy, look for anything nasty
108 * around the owner that it should go and attack. 109 * around the owner that it should go and attack. (if the owner is
110 * still on a map)
109 */ 111 */
110 tmp3 = NULL; 112 // owners sometimes are not on a map but in the inventory of somehting else
113 if (!owner->flag [FLAG_REMOVED] && owner->map)
114 {
115 tmp3 = 0;
116
111 for (i = 0; i < SIZEOFFREE; i++) 117 for (i = 0; i < SIZEOFFREE; i++)
112 { 118 {
113 x = owner->x + freearr_x[search_arr[i]]; 119 x = owner->x + DIRX (search_arr[i]);
114 y = owner->y + freearr_y[search_arr[i]]; 120 y = owner->y + DIRY (search_arr[i]);
115 nm = owner->map; 121 nm = owner->map;
116 /* Only look on the space if there is something alive there. */ 122 /* Only look on the space if there is something alive there. */
117 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 123 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
118 124
119 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 125 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
120 { 126 {
121 for (tmp = GET_MAP_OB (nm, x, y); tmp != NULL; tmp = tmp->above) 127 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 { 128 {
129 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
130 130
131 if (!can_see_enemy (pet, tmp2)) 131 if (tmp2->flag [FLAG_ALIVE] && ((!tmp2->flag [FLAG_FRIENDLY] &&
132 (tmp2->type != PLAYER)) ||
133 should_arena_attack (pet, owner, tmp2))
134 && !tmp2->flag [FLAG_UNAGGRESSIVE] && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
132 { 135 {
136
137 if (!can_see_enemy (pet, tmp2))
138 {
133 if (tmp3 != NULL) 139 if (tmp3 != 0)
134 tmp3 = tmp2; 140 tmp3 = tmp2;
135 } 141 }
136 else
137 {
138 pet->enemy = tmp2;
139 if (check_enemy (pet, rv) != NULL)
140 return tmp2;
141 else 142 else
143 {
144 pet->enemy = tmp2;
145 if (check_enemy (pet, rv) != 0)
146 return tmp2;
147 else
142 pet->enemy = NULL; 148 pet->enemy = 0;
143 } 149 }
144 } /* if this is a valid enemy */ 150 } /* if this is a valid enemy */
145 } /* for objects on this space */ 151 } /* for objects on this space */
146 } /* if there is something living on this space */ 152 } /* if there is something living on this space */
147 } /* for loop of spaces around the owner */ 153 } /* for loop of spaces around the owner */
148 154
149 /* fine, we went through the whole loop and didn't find one we could 155 /* fine, we went through the whole loop and didn't find one we could
150 see, take what we have */ 156 see, take what we have */
151 if (tmp3 != NULL) 157 if (tmp3 != 0)
152 { 158 {
153 pet->enemy = tmp3; 159 pet->enemy = tmp3;
154 if (check_enemy (pet, rv) != NULL) 160 if (check_enemy (pet, rv) != 0)
155 return tmp3; 161 return tmp3;
156 else 162 else
157 pet->enemy = NULL; 163 pet->enemy = 0;
164 }
158 } 165 }
159 166
160 /* No threat to owner, check to see if the pet has an attacker */ 167 /* No threat to owner, check to see if the pet has an attacker */
161 if (attacker) 168 if (attacker)
162 { 169 {
163 /* also need to check to make sure it is not freindly */ 170 /* also need to check to make sure it is not freindly */
164 /* or otherwise non-hostile, and is an appropriate target */ 171 /* or otherwise non-hostile, and is an appropriate target */
165 if (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && on_same_map (pet, attacker)) 172 if (!attacker->flag [FLAG_FRIENDLY] && on_same_map (pet, attacker))
166 { 173 {
167 pet->enemy = attacker; 174 pet->enemy = attacker;
168 175
169 if (check_enemy (pet, rv) != NULL) 176 if (check_enemy (pet, rv) != 0)
170 return attacker; 177 return attacker;
171 else 178 else
172 pet->enemy = NULL; 179 pet->enemy = 0;
173 } 180 }
174 } 181 }
175 182
176 /* Don't have an attacker or legal enemy, so look for a new one!. 183 /* 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 184 * 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 185 * 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. 186 * the same as the code that looks around the owner.
180 */ 187 */
181 if (owner->type == PLAYER && owner->contr->petmode != pet_defend) 188 if (owner->type == PLAYER && owner->contr->petmode != pet_defend)
182 { 189 {
183 tmp3 = NULL; 190 tmp3 = 0;
184 for (i = 0; i < SIZEOFFREE; i++) 191 for (i = 0; i < SIZEOFFREE; i++)
185 { 192 {
186 x = pet->x + freearr_x[search_arr[i]]; 193 x = pet->x + DIRX (search_arr[i]);
187 y = pet->y + freearr_y[search_arr[i]]; 194 y = pet->y + DIRY (search_arr[i]);
188 nm = pet->map; 195 nm = pet->map;
189 /* Only look on the space if there is something alive there. */ 196 /* Only look on the space if there is something alive there. */
190 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 197 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
191 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 198 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
192 { 199 {
193 for (tmp = GET_MAP_OB (nm, x, y); tmp != NULL; tmp = tmp->above) 200 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
194 { 201 {
195 object *tmp2 = tmp->head == NULL ? tmp : tmp->head; 202 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
196 203
197 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) && 204 if (tmp2->flag [FLAG_ALIVE] && ((!tmp2->flag [FLAG_FRIENDLY] &&
198 (tmp2->type != PLAYER)) || 205 (tmp2->type != PLAYER)) ||
199 should_arena_attack (pet, owner, tmp2)) 206 should_arena_attack (pet, owner, tmp2))
200 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv)) 207 && !tmp2->flag [FLAG_UNAGGRESSIVE] && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
201 { 208 {
202 209
203 if (!can_see_enemy (pet, tmp2)) 210 if (!can_see_enemy (pet, tmp2))
204 { 211 {
205 if (tmp3 != NULL) 212 if (tmp3 != 0)
206 tmp3 = tmp2; 213 tmp3 = tmp2;
207 } 214 }
208 else 215 else
209 { 216 {
210 pet->enemy = tmp2; 217 pet->enemy = tmp2;
211 if (check_enemy (pet, rv) != NULL) 218 if (check_enemy (pet, rv) != 0)
212 return tmp2; 219 return tmp2;
213 else 220 else
214 pet->enemy = NULL; 221 pet->enemy = 0;
215 } 222 }
216 } /* make sure we can get to the bugger */ 223 } /* make sure we can get to the bugger */
217 } /* for objects on this space */ 224 } /* for objects on this space */
218 } /* if there is something living on this space */ 225 } /* if there is something living on this space */
219 } /* for loop of spaces around the pet */ 226 } /* for loop of spaces around the pet */
227
228 /* fine, we went through the whole loop and didn't find one we could
229 see, take what we have */
230 if (tmp3 != 0)
231 {
232 pet->enemy = tmp3;
233 if (check_enemy (pet, rv) != 0)
234 return tmp3;
235 else
236 pet->enemy = 0;
237 }
220 } /* pet in defence mode */ 238 } /* pet in defence mode */
221 239
222 /* fine, we went through the whole loop and didn't find one we could 240 object *enemy = check_enemy (pet, rv);
223 see, take what we have */ 241 // we have a summoned pet here and the owners enemy isn't set or can't
224 if (tmp3 != NULL) 242 // be reached => search for a player around us and set it as our new enemy!!
225 { 243 if (!enemy && pet->owner && pet->owner->type != PLAYER)
226 pet->enemy = tmp3; 244 enemy = get_nearest_player (pet);
227 if (check_enemy (pet, rv) != NULL)
228 return tmp3;
229 else
230 pet->enemy = NULL;
231 }
232 245
233 /* Didn't find anything - return the owner's enemy or NULL */ 246 /* Didn't find anything - return the owner's enemy or 0 */
234 return check_enemy (pet, rv); 247 return enemy;
235} 248}
236 249
237void 250void
238terminate_all_pets (object *owner) 251terminate_all_pets (object *owner)
239{ 252{
240 objectlink *obl, *next; 253 objectlink *obl, *next;
241 254
242 for (obl = first_friendly_object; obl != NULL; obl = next) 255 for (obl = first_friendly_object; obl; obl = next)
243 { 256 {
244 object *ob = obl->ob; 257 object *ob = obl->ob;
245
246 next = obl->next; 258 next = obl->next;
259
247 if (ob->owner == owner) 260 if (ob->owner == owner)
248 {
249 if (!QUERY_FLAG (ob, FLAG_REMOVED))
250 ob->remove ();
251 remove_friendly_object (ob);
252 ob->destroy (); 261 ob->drop_and_destroy ();
253 }
254 } 262 }
255} 263}
256 264
257/* 265/*
258 * Unfortunately, sometimes, the owner of a pet is in the 266 * Unfortunately, sometimes, the owner of a pet is in the
259 * process of entering a new map when this is called. 267 * 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 268 * Interesting enough, we don't use the passed map structure in
263 * this function. 269 * this function.
264 */ 270 */
265void 271void
266remove_all_pets (maptile *map) 272move_all_pets ()
267{ 273{
268 objectlink *obl, *next; 274 objectlink *obl, *next;
269 object *owner; 275 object *owner;
270 276
271 for (obl = first_friendly_object; obl != NULL; obl = next) 277 for (obl = first_friendly_object; obl; obl = next)
272 { 278 {
273 next = obl->next; 279 next = obl->next;
274 if (obl->ob->type != PLAYER && QUERY_FLAG (obl->ob, FLAG_FRIENDLY) && 280
275 (owner = obl->ob->owner) != NULL && !on_same_map (owner, obl->ob)) 281 if (obl->ob->type != PLAYER
282 && obl->ob->flag [FLAG_FRIENDLY]
283 && (owner = obl->ob->owner)
284 && !on_same_map (owner, obl->ob))
276 { 285 {
277 /* follow owner checks map status for us */ 286 /* follow owner checks map status for us */
278 follow_owner (obl->ob, owner); 287 follow_owner (obl->ob, owner);
279 } 288 }
280 } 289 }
281} 290}
282 291
283int 292int
284follow_owner (object *ob, object *owner) 293follow_owner (object *ob, object *owner)
285{ 294{
286 object *tmp; 295 if (owner->flag [FLAG_REMOVED])
287 int dir; 296 return 0; // do nothing if the owner is removed
288 297 else if (!owner->map || owner->map->state != MAP_ACTIVE)
289 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);
290 ob->remove (); 299 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 } 300 {
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); 301 int dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
304 302
305 if (dir == -1) 303 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 } 304 {
319 } 305 owner->map->insert (ob, owner->x + DIRX (dir), owner->y + DIRY (dir));
320 insert_ob_in_map (ob, ob->map, NULL, 0); 306
321 if (owner->type == PLAYER) /* Uh, I hope this is always true... */ 307 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"); 308 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you");
323 309
324 return 0; 310 return 0;
311 }
312 }
325 313
326fail:
327 remove_friendly_object (ob);
328 ob->destroy (); 314 ob->drop_and_destroy ();
329 315
330 return 1; 316 return 1;
331} 317}
332 318
333void 319void
334pet_move (object *ob) 320pet_move (object *ob)
335{ 321{
336 int dir, i; 322 int dir = 0, i;
337 sint16 dx, dy; 323 sint16 dx, dy;
338 object *ob2, *owner; 324 object *ob2, *owner;
339 maptile *m; 325 maptile *m;
340 326
341 /* Check to see if player pulled out */ 327 /* Check to see if player pulled out */
342 if ((owner = ob->owner) == NULL) 328 if ((owner = ob->owner) == NULL)
343 { 329 {
344 ob->remove (); /* Will be freed when returning */
345 remove_friendly_object (ob);
346 ob->destroy (); 330 ob->drop_and_destroy ();
347 LOG (llevMonster, "Pet: no owner, leaving.\n"); 331 LOG (llevTrace, "Pet: no owner, leaving.\n");
348 return; 332 return;
349 } 333 }
350 334
351 /* 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
352 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])
353 { 339 {
354 follow_owner (ob, owner); 340 follow_owner (ob, owner);
355 return; 341 return;
356 } 342 }
343
357 /* Calculate Direction */ 344 /* Calculate Direction */
358 if (owner->type == PLAYER && owner->contr->petmode == pet_sad) 345 if (owner->type == PLAYER && owner->contr->petmode == pet_sad)
359 { 346 {
360 /* in S&D mode, if we have no enemy, run randomly about. */ 347 /* in S&D mode, if we have no enemy, run randomly about. */
361 for (i = 0; i < 15; i++) 348 for (i = 0; i < 15; i++)
362 { 349 {
363 dir = rndm (1, 8); 350 dir = rndm (1, 8);
364 dx = ob->x + freearr_x[dir]; 351 dx = ob->x + DIRX (dir);
365 dy = ob->y + freearr_y[dir]; 352 dy = ob->y + DIRY (dir);
366 m = ob->map; 353 m = ob->map;
367 if (get_map_flags (ob->map, &m, dx, dy, &dx, &dy) & P_OUT_OF_MAP) 354 if (get_map_flags (ob->map, &m, dx, dy, &dx, &dy) & P_OUT_OF_MAP)
368 continue; 355 continue;
369 else if (OB_TYPE_MOVE_BLOCK (ob, GET_MAP_MOVE_BLOCK (m, dx, dy))) 356 else if (OB_TYPE_MOVE_BLOCK (ob, GET_MAP_MOVE_BLOCK (m, dx, dy)))
370 continue; 357 continue;
371 else 358 else
372 break; 359 break;
373 } 360 }
374 } 361 }
375 else 362 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
376 { 363 {
377 struct rv_vector rv; 364 struct rv_vector rv;
378 365
379 get_rangevector (ob, ob->owner, &rv, 0); 366 get_rangevector (ob, ob->owner, &rv, 0);
380 dir = rv.direction; 367 dir = rv.direction;
381 } 368 }
369
382 ob->direction = dir; 370 ob->direction = dir;
383 371
384 /* move_ob returns 0 if the object couldn't move. If that is the 372 /* move_ob returns 0 if the object couldn't move. If that is the
385 * case, lets do some other work. 373 * case, lets do some other work.
386 */ 374 */
387 if (!(move_ob (ob, dir, ob))) 375 if (!(ob->move (dir)))
388 { 376 {
389 object *part; 377 object *part;
390 378
391 /* the failed move_ob above may destroy the pet, so check here */ 379 /* the failed move_ob above may destroy the pet, so check here */
392 if (ob->destroyed ()) 380 if (ob->destroyed ())
393 return; 381 return;
394 382
395 for (part = ob; part != NULL; part = part->more) 383 for (part = ob; part != NULL; part = part->more)
396 { 384 {
397 dx = part->x + freearr_x[dir]; 385 dx = part->x + DIRX (dir);
398 dy = part->y + freearr_y[dir]; 386 dy = part->y + DIRY (dir);
399 m = get_map_from_coord (part->map, &dx, &dy); 387 m = get_map_from_coord (part->map, &dx, &dy);
400 if (!m) 388 if (!m)
401 continue; 389 continue;
402 390
403 for (ob2 = GET_MAP_OB (m, dx, dy); ob2 != NULL; ob2 = ob2->above) 391 for (ob2 = GET_MAP_OB (m, dx, dy); ob2 != NULL; ob2 = ob2->above)
404 { 392 {
405 object *new_ob; 393 object *new_ob;
406 394
407 new_ob = ob2->head ? ob2->head : ob2; 395 new_ob = ob2->head ? ob2->head : ob2;
396
408 if (new_ob == ob) 397 if (new_ob == ob)
409 break; 398 break;
399
410 if (new_ob == ob->owner) 400 if (new_ob == ob->owner)
411 return; 401 return;
402
412 if (new_ob->owner == ob->owner) 403 if (new_ob->owner == ob->owner)
413 break; 404 break;
414 405
415 /* Hmm. Did we try to move into an enemy monster? If so, 406 /* Hmm. Did we try to move into an enemy monster? If so,
416 * make it our enemy. 407 * make it our enemy.
417 */ 408 */
418 if (QUERY_FLAG (new_ob, FLAG_ALIVE) && !QUERY_FLAG (ob, FLAG_UNAGGRESSIVE) 409 if (new_ob->flag [FLAG_ALIVE] && !ob->flag [FLAG_UNAGGRESSIVE]
419 && !QUERY_FLAG (new_ob, FLAG_UNAGGRESSIVE) && !QUERY_FLAG (new_ob, FLAG_FRIENDLY)) 410 && !new_ob->flag [FLAG_UNAGGRESSIVE] && !new_ob->flag [FLAG_FRIENDLY])
420 { 411 {
421 412
422 ob->enemy = new_ob; 413 ob->enemy = new_ob;
423 if (new_ob->enemy == NULL) 414 if (new_ob->enemy == NULL)
424 new_ob->enemy = ob; 415 new_ob->enemy = ob;
430 return; 421 return;
431 } 422 }
432 } 423 }
433 } 424 }
434 /* Try a different course */ 425 /* Try a different course */
435 dir = absdir (dir + 4 - (RANDOM () % 5) - (RANDOM () % 5)); 426 dir = absdir (dir + 4 - (rndm (5)) - (rndm (5)));
436 (void) move_ob (ob, dir, ob); 427 ob->move (dir);
437 } 428 }
438 return;
439} 429}
440 430
441/**************************************************************************** 431/****************************************************************************
442 * 432 *
443 * GOLEM SPELL CODE 433 * GOLEM SPELL CODE
444 * 434 *
445 ****************************************************************************/ 435 ****************************************************************************/
446 436
447/* fix_summon_pet() - this makes multisquare/single square monsters 437/* fix_summon_pet() - this makes multisquare/single square monsters
448 * proper for map insertion. 438 * proper for map insertion.
449 * at is the archetype, op is the caster of the spell, dir is the 439 * at is the archetype, op is the caster of the spell, dir is the
450 * direction the monster should be placed in. 440 * direction the monster should be placed in.
451 * is_golem is to note that this is a golem spell. 441 * is_golem is to note that this is a golem spell.
452 */ 442 */
453object * 443static object *
454fix_summon_pet (archetype *at, object *op, int dir, int is_golem) 444fix_summon_pet (archetype *at, object *op, int dir, int is_golem)
455{ 445{
456 archetype *atmp;
457 object *tmp = NULL, *prev = NULL, *head = NULL; 446 object *tmp = NULL, *prev = NULL, *head = NULL;
458 447
459 for (atmp = at; atmp; atmp = atmp->more) 448 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
460 { 449 {
461 tmp = arch_to_object (atmp); 450 tmp = atmp->instance ();
462 451
463 if (atmp == at) 452 if (atmp == at)
464 { 453 {
465 if (!is_golem) 454 if (!is_golem)
466 SET_FLAG (tmp, FLAG_MONSTER); 455 tmp->set_flag (FLAG_MONSTER);
467 456
468 tmp->set_owner (op); 457 tmp->set_owner (op);
458
469 if (op->type == PLAYER) 459 if (op->type == PLAYER)
470 { 460 {
471 tmp->stats.exp = 0; 461 tmp->stats.exp = 0;
472 add_friendly_object (tmp); 462 add_friendly_object (tmp);
473 SET_FLAG (tmp, FLAG_FRIENDLY);
474 if (is_golem) 463 if (is_golem)
475 CLEAR_FLAG (tmp, FLAG_MONSTER); 464 tmp->clr_flag (FLAG_MONSTER);
476 } 465 }
477 else 466 else
478 { 467 {
479 if (QUERY_FLAG (op, FLAG_FRIENDLY)) 468 if (op->flag [FLAG_FRIENDLY])
480 { 469 {
481 object *owner = op->owner; 470 object *owner = op->owner;
482 471
483 if (owner) 472 if (owner)
484 { /* For now, we transfer ownership */ 473 { /* For now, we transfer ownership */
485 tmp->set_owner (owner); 474 tmp->set_owner (owner);
486 tmp->attack_movement = PETMOVE; 475 tmp->attack_movement = PETMOVE;
487 add_friendly_object (tmp); 476 add_friendly_object (tmp);
488 SET_FLAG (tmp, FLAG_FRIENDLY);
489 } 477 }
490 } 478 }
491 } 479 }
492 480
493 if (op->type != PLAYER || !is_golem) 481 if (op->type != PLAYER || !is_golem)
503 } 491 }
504 492
505 if (!head) 493 if (!head)
506 head = tmp; 494 head = tmp;
507 495
508 tmp->x = op->x + freearr_x[dir] + tmp->arch->clone.x; 496 tmp->x = op->x + DIRX (dir) + tmp->arch->x;
509 tmp->y = op->y + freearr_y[dir] + tmp->arch->clone.y; 497 tmp->y = op->y + DIRY (dir) + tmp->arch->y;
510 tmp->map = op->map; 498 tmp->map = op->map;
511 499
512 if (tmp->invisible) 500 if (tmp->invisible)
513 tmp->invisible = 0; 501 tmp->invisible = 0;
514 502
525 head->last_eat = 0; 513 head->last_eat = 0;
526 head->last_grace = 0; 514 head->last_grace = 0;
527 head->last_sp = 0; 515 head->last_sp = 0;
528 head->other_arch = NULL; 516 head->other_arch = NULL;
529 head->stats.exp = 0; 517 head->stats.exp = 0;
530 CLEAR_FLAG (head, FLAG_CHANGING); 518 head->clr_flag (FLAG_CHANGING);
531 CLEAR_FLAG (head, FLAG_STAND_STILL); 519 head->clr_flag (FLAG_STAND_STILL);
532 CLEAR_FLAG (head, FLAG_GENERATOR); 520 head->clr_flag (FLAG_GENERATOR);
533 CLEAR_FLAG (head, FLAG_SPLITTING); 521 head->clr_flag (FLAG_SPLITTING);
534 if (head->attacktype & AT_GHOSTHIT) 522 if (head->attacktype & AT_GHOSTHIT)
535 head->attacktype = (AT_PHYSICAL | AT_DRAIN); 523 head->attacktype = (AT_PHYSICAL | AT_DRAIN);
536 524
537 return head; 525 return head;
538} 526}
543move_golem (object *op) 531move_golem (object *op)
544{ 532{
545 int made_attack = 0; 533 int made_attack = 0;
546 object *tmp; 534 object *tmp;
547 535
548 if (QUERY_FLAG (op, FLAG_MONSTER)) 536 if (op->flag [FLAG_MONSTER])
549 return; /* Has already been moved */ 537 return; /* Has already been moved */
550 538
551 if (op->owner == NULL) 539 if (!op->owner)
552 { 540 {
553 LOG (llevDebug, "Golem without owner destructed.\n"); 541 LOG (llevDebug, "Golem without owner destructed.\n");
554 op->remove ();
555 op->destroy (); 542 op->drop_and_destroy ();
556 return; 543 return;
557 } 544 }
558 545
559 /* It would be nice to have a cleaner way of what message to print 546 /* It would be nice to have a cleaner way of what message to print
560 * when the golem expires than these hard coded entries. 547 * when the golem expires than these hard coded entries.
564 if (--op->stats.hp < 0) 551 if (--op->stats.hp < 0)
565 { 552 {
566 if (op->msg) 553 if (op->msg)
567 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg); 554 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
568 555
569 op->owner->contr->ranges[range_golem] = 0;
570 remove_friendly_object (op);
571 op->remove ();
572 op->destroy (); 556 op->drop_and_destroy ();
573 return; 557 return;
574 } 558 }
575 559
576 /* Do golem attacks/movement for single & multisq golems. 560 /* Do golem attacks/movement for single & multisq golems.
577 * Assuming here that op is the 'head' object. Pass only op to 561 * Assuming here that op is the 'head' object. Pass only op to
578 * move_ob (makes recursive calls to other parts) 562 * ->move (makes recursive calls to other parts)
579 * move_ob returns 0 if the creature was not able to move. 563 * move_ob returns 0 if the creature was not able to move.
580 */ 564 */
581 if (move_ob (op, op->direction, op)) 565 if (op->move (op->direction, op))
582 return; 566 return;
583 567
584 if (op->destroyed ()) 568 if (op->destroyed ())
585 return; 569 return;
586 570
587 for (tmp = op; tmp; tmp = tmp->more) 571 for (tmp = op; tmp; tmp = tmp->more)
588 { 572 {
589 sint16 x = tmp->x + freearr_x[op->direction], y = tmp->y + freearr_y[op->direction]; 573 sint16 x = tmp->x + DIRX (op->direction), y = tmp->y + DIRY (op->direction);
590 object *victim; 574 object *victim;
591 maptile *m; 575 maptile *m;
592 int mflags; 576 int mflags;
593 577
594 m = op->map; 578 m = op->map;
595 mflags = get_map_flags (m, &m, x, y, &x, &y); 579 mflags = get_map_flags (m, &m, x, y, &x, &y);
596 580
597 if (mflags & P_OUT_OF_MAP) 581 if (mflags & P_OUT_OF_MAP)
598 continue; 582 continue;
599 583
600 for (victim = GET_MAP_OB (op->map, x, y); victim; victim = victim->above) 584 for (victim = GET_MAP_OB (m, x, y); victim; victim = victim->above)
601 if (QUERY_FLAG (victim, FLAG_ALIVE)) 585 if (victim->flag [FLAG_ALIVE])
602 break; 586 break;
603 587
604 /* We used to call will_hit_self to make sure we don't 588 /* 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 589 * hit ourselves, but that didn't work, and I don't really
606 * know if that was more efficient anyways than this. 590 * know if that was more efficient anyways than this.
608 * but since we are not trying to dereferance that pointer, 592 * but since we are not trying to dereferance that pointer,
609 * that isn't a problem. 593 * that isn't a problem.
610 */ 594 */
611 if (victim && victim != op && victim->head != op) 595 if (victim && victim != op && victim->head != op)
612 { 596 {
613
614 /* for golems with race fields, we don't attack 597 /* for golems with race fields, we don't attack
615 * aligned races 598 * aligned races
616 */ 599 */
617 600
618 if (victim->race && op->race && strstr (op->race, victim->race)) 601 if (victim->race && op->race && op->race.contains (victim->race))
619 { 602 {
620 if (op->owner) 603 if (op->owner)
621 new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging %s.", &op->name, &victim->name); 604 new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging %s.", &op->name, &victim->name);
622 } 605 }
623 else if (victim == op->owner) 606 else if (victim == op->owner)
630 attack_ob (victim, op); 613 attack_ob (victim, op);
631 made_attack = 1; 614 made_attack = 1;
632 } 615 }
633 } /* If victim */ 616 } /* If victim */
634 } 617 }
618
635 if (made_attack) 619 if (made_attack)
636 update_object (op, UP_OBJ_FACE); 620 update_object (op, UP_OBJ_FACE);
637} 621}
638 622
639/* this is a really stupid function when you get down and 623/* this is a really stupid function when you get down and
645control_golem (object *op, int dir) 629control_golem (object *op, int dir)
646{ 630{
647 op->direction = dir; 631 op->direction = dir;
648} 632}
649 633
650/* summon golem: summons a monster for 'op'. caster is the object 634/* summon golem: summons a monster for 'op'. caster is the object
651 * casting the spell, dir is the direction to place the monster, 635 * casting the spell, dir is the direction to place the monster,
652 * at is the archetype of the monster, and spob is the spell 636 * 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 637 * object. At this stage, all spob is really used for is to
654 * adjust some values in the monster. 638 * adjust some values in the monster.
655 */ 639 */
656int 640int
657summon_golem (object *op, object *caster, int dir, object *spob) 641summon_golem (object *op, object *caster, int dir, object *spob)
658{ 642{
659 object *tmp, *god = NULL; 643 object *tmp, *god = NULL;
660 archetype *at; 644 archetype *at;
661 char buf[MAX_BUF];
662 645
663 /* Because there can be different golem spells, player may want to 646 /* Because there can be different golem spells, player may want to
664 * 'lose' their old golem. 647 * 'lose' their old golem.
665 */ 648 */
666 if (op->type == PLAYER && op->contr->ranges[range_golem]) 649 if (op->type == PLAYER && op->contr->golem)
667 { 650 {
668 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem."); 651 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 (); 652 op->contr->golem->drop_and_destroy ();
671 op->contr->ranges[range_golem] = 0; 653 op->contr->golem = 0;
672 } 654 }
673 655
674 if (spob->other_arch) 656 if (spob->other_arch)
675 at = spob->other_arch; 657 at = spob->other_arch;
676 else if (spob->race) 658 else if (spob->race)
696 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name); 678 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
697 return 0; 679 return 0;
698 } 680 }
699 681
700 if (!dir) 682 if (!dir)
701 dir = find_free_spot (NULL, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1); 683 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
702 684
703 if (dir == -1 || ob_blocked (&at->clone, op->map, op->x + freearr_x[dir], op->y + freearr_y[dir])) 685 if (dir < 0 || at->blocked (op->map, op->x + DIRX (dir), op->y + DIRY (dir)))
704 { 686 {
705 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 687 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
706 return 0; 688 return 0;
707 } 689 }
708 690
715 } 697 }
716 698
717 if (op->type == PLAYER) 699 if (op->type == PLAYER)
718 { 700 {
719 tmp->type = GOLEM; 701 tmp->type = GOLEM;
720 tmp->set_owner (op); 702 op->contr->golem = tmp;
703 /* give the player control of the golem */
721 set_spell_skill (op, caster, spob, tmp); 704 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 { 705 }
728 if (QUERY_FLAG (op, FLAG_FRIENDLY))
729 {
730 object *owner = op->owner;
731 706
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 */ 707 /* This sets the level dependencies on dam and hp for monsters */
748 /* players can't cope with too strong summonings. */ 708 /* players can't cope with too strong summonings. */
749 /* but monsters can. reserve these for players. */ 709 /* but monsters can. reserve these for players. */
750 if (op->type == PLAYER) 710 if (op->type == PLAYER)
751 { 711 {
752 tmp->stats.hp += spob->duration + SP_level_duration_adjust (caster, spob); 712 tmp->stats.hp += spob->duration + SP_level_duration_adjust (caster, spob);
754 if (!spob->stats.dam) 714 if (!spob->stats.dam)
755 tmp->stats.dam += SP_level_dam_adjust (caster, spob); 715 tmp->stats.dam += SP_level_dam_adjust (caster, spob);
756 else 716 else
757 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);
758 718
759 tmp->speed += .02 * SP_level_range_adjust (caster, spob); 719 tmp->set_speed (min (1.f, tmp->speed + .02f * SP_level_range_adjust (caster, spob)));
760 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,
911 * with all the checks in the 'for' portion itself. Much 870 * with all the checks in the 'for' portion itself. Much
912 * more readable to break some of the conditions out. 871 * more readable to break some of the conditions out.
913 */ 872 */
914 for (tr = spell_ob->randomitems->items; tr; tr = tr->next) 873 for (tr = spell_ob->randomitems->items; tr; tr = tr->next)
915 { 874 {
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 + DIRX (ndir), op->y + DIRY (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) 978 sint16 x = DIRX (ndir);
1021 { 979 sint16 y = DIRY (ndir);
1022 x = freearr_x[ndir];
1023 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;
1061 tmp->set_owner (op->owner); 1015 tmp->set_owner (op->owner);
1062 } 1016 }
1063 } 1017 }
1064 } 1018 }
1065 1019
1066 if (tmp->speed > MIN_ACTIVE_SPEED) 1020 if (tmp->has_active_speed ())
1067 tmp->speed_left = -1; 1021 tmp->speed_left = -1;
1068 } 1022 }
1069 1023
1070 if (head == NULL) 1024 if (head == NULL)
1071 head = tmp; 1025 head = tmp;
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