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.14 by root, Tue Dec 12 21:39:57 2006 UTC vs.
Revision 1.38 by root, Sun Jul 1 05:00:20 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * Crossfire TRT is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <crossfire@schmorp.de>
22*/ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#ifndef __CEXTRACT__
26# include <sproto.h> 25#include <sproto.h>
27#endif
28 26
29/* given that 'pet' is a friendly object, this function returns a 27/* given that 'pet' is a friendly object, this function returns a
30 * monster the pet should attack, NULL if nothing appropriate is 28 * monster the pet should attack, NULL if nothing appropriate is
31 * found. it basically looks for nasty things around the owner 29 * found. it basically looks for nasty things around the owner
32 * of the pet to attack. 30 * of the pet to attack.
33 * this is now tilemap aware. 31 * this is now tilemap aware.
34 */ 32 */
35
36object * 33object *
37get_pet_enemy (object *pet, rv_vector * rv) 34get_pet_enemy (object *pet, rv_vector * rv)
38{ 35{
39 object *owner, *tmp, *attacker, *tmp3; 36 object *owner, *tmp, *attacker, *tmp3;
40 int i; 37 int i;
44 int mflags; 41 int mflags;
45 42
46 attacker = pet->attacked_by; /*pointer to attacking enemy */ 43 attacker = pet->attacked_by; /*pointer to attacking enemy */
47 pet->attacked_by = NULL; /*clear this, since we are dealing with it */ 44 pet->attacked_by = NULL; /*clear this, since we are dealing with it */
48 45
49 if ((owner = get_owner (pet)) != NULL) 46 if (owner = pet->owner)
50 { 47 {
51 /* If the owner has turned on the pet, make the pet 48 /* If the owner has turned on the pet, make the pet
52 * unfriendly. 49 * unfriendly.
53 */ 50 */
54 if ((check_enemy (owner, rv)) == pet) 51 if (check_enemy (owner, rv) == pet)
55 { 52 {
56 CLEAR_FLAG (pet, FLAG_FRIENDLY);
57 remove_friendly_object (pet); 53 remove_friendly_object (pet);
58 pet->attack_movement &= ~PETMOVE; 54 pet->attack_movement &= ~PETMOVE;
59 return owner; 55 return owner;
60 } 56 }
61 } 57 }
62 else 58 else
63 { 59 {
64 /* else the owner is no longer around, so the 60 /* else the owner is no longer around, so the
65 * pet no longer needs to be friendly. 61 * pet no longer needs to be friendly.
66 */ 62 */
67 CLEAR_FLAG (pet, FLAG_FRIENDLY);
68 remove_friendly_object (pet); 63 remove_friendly_object (pet);
69 pet->attack_movement &= ~PETMOVE; 64 pet->attack_movement &= ~PETMOVE;
70 return NULL; 65 return 0;
71 } 66 }
67
72 /* If they are not on the same map, the pet won't be agressive */ 68 /* If they are not on the same map, the pet won't be agressive */
73 if (!on_same_map (pet, owner)) 69 //if (!on_same_map (pet, owner))
74 return NULL; 70 // return 0;
75 71
76 /* See if the pet has an existing enemy. If so, don't start a new one */ 72 /* See if the pet has an existing enemy. If so, don't start a new one */
77 if ((tmp = check_enemy (pet, rv)) != NULL) 73 if (tmp = check_enemy (pet, rv))
78 { 74 {
79 if (tmp == owner && !QUERY_FLAG (pet, FLAG_CONFUSED) && QUERY_FLAG (pet, FLAG_FRIENDLY)) 75 if (tmp == owner && !QUERY_FLAG (pet, FLAG_CONFUSED) && QUERY_FLAG (pet, FLAG_FRIENDLY))
80 /* without this check, you can actually get pets with 76 /* without this check, you can actually get pets with
81 * enemy set to owner! 77 * enemy set to owner!
82 */ 78 */
83 pet->enemy = NULL; 79 pet->enemy = 0;
84 else 80 else
85 return tmp; 81 return tmp;
86 } 82 }
83
87 get_search_arr (search_arr); 84 get_search_arr (search_arr);
88 85
89 if (owner->type == PLAYER && owner->contr->petmode > pet_normal) 86 if (owner->type == PLAYER && owner->contr->petmode > pet_normal)
90 { 87 {
91 if (owner->contr->petmode == pet_sad) 88 if (owner->contr->petmode == pet_sad)
92 { 89 {
93 tmp = find_nearest_living_creature (pet); 90 tmp = find_nearest_living_creature (pet);
94 if (tmp != NULL) 91 if (tmp != 0)
95 { 92 {
96 get_rangevector (pet, tmp, rv, 0); 93 get_rangevector (pet, tmp, rv, 0);
97 if (check_enemy (pet, rv) != NULL) 94 if (check_enemy (pet, rv) != 0)
98 return tmp; 95 return tmp;
99 else 96 else
100 pet->enemy = NULL; 97 pet->enemy = 0;
101 } 98 }
102 /* if we got here we have no enemy */ 99 /* if we got here we have no enemy */
103 /* we return NULL to avoid heading back to the owner */ 100 /* we return 0 to avoid heading back to the owner */
104 pet->enemy = NULL; 101 pet->enemy = 0;
105 return NULL; 102 return 0;
106 } 103 }
107 } 104 }
108 105
109 /* Since the pet has no existing enemy, look for anything nasty 106 /* Since the pet has no existing enemy, look for anything nasty
110 * around the owner that it should go and attack. 107 * around the owner that it should go and attack. (if the owner is
108 * still on a map)
111 */ 109 */
112 tmp3 = NULL; 110 // owners sometimes are not on a map but in the inventory of somehting else
111 if (!owner->flag [FLAG_REMOVED] && owner->map)
112 {
113 tmp3 = 0;
114
113 for (i = 0; i < SIZEOFFREE; i++) 115 for (i = 0; i < SIZEOFFREE; i++)
114 { 116 {
115 x = owner->x + freearr_x[search_arr[i]]; 117 x = owner->x + freearr_x[search_arr[i]];
116 y = owner->y + freearr_y[search_arr[i]]; 118 y = owner->y + freearr_y[search_arr[i]];
117 nm = owner->map; 119 nm = owner->map;
118 /* Only look on the space if there is something alive there. */ 120 /* Only look on the space if there is something alive there. */
119 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 121 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
122
120 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 123 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
121 { 124 {
122 for (tmp = get_map_ob (nm, x, y); tmp != NULL; tmp = tmp->above) 125 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
123 {
124 object *tmp2 = tmp->head == NULL ? tmp : tmp->head;
125
126 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
127 (tmp2->type != PLAYER)) ||
128 should_arena_attack (pet, owner, tmp2))
129 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
130 { 126 {
127 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
131 128
132 if (!can_see_enemy (pet, tmp2)) 129 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
130 (tmp2->type != PLAYER)) ||
131 should_arena_attack (pet, owner, tmp2))
132 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
133 { 133 {
134
135 if (!can_see_enemy (pet, tmp2))
136 {
134 if (tmp3 != NULL) 137 if (tmp3 != 0)
135 tmp3 = tmp2; 138 tmp3 = tmp2;
136 } 139 }
137 else
138 {
139 pet->enemy = tmp2;
140 if (check_enemy (pet, rv) != NULL)
141 return tmp2;
142 else 140 else
141 {
142 pet->enemy = tmp2;
143 if (check_enemy (pet, rv) != 0)
144 return tmp2;
145 else
143 pet->enemy = NULL; 146 pet->enemy = 0;
144 } 147 }
145 } /* if this is a valid enemy */ 148 } /* if this is a valid enemy */
146 } /* for objects on this space */ 149 } /* for objects on this space */
147 } /* if there is something living on this space */ 150 } /* if there is something living on this space */
148 } /* for loop of spaces around the owner */ 151 } /* for loop of spaces around the owner */
149 152
150 /* fine, we went through the whole loop and didn't find one we could 153 /* fine, we went through the whole loop and didn't find one we could
151 see, take what we have */ 154 see, take what we have */
152 if (tmp3 != NULL) 155 if (tmp3 != 0)
153 { 156 {
154 pet->enemy = tmp3; 157 pet->enemy = tmp3;
155 if (check_enemy (pet, rv) != NULL) 158 if (check_enemy (pet, rv) != 0)
156 return tmp3; 159 return tmp3;
157 else 160 else
158 pet->enemy = NULL; 161 pet->enemy = 0;
162 }
159 } 163 }
160 164
161 /* No threat to owner, check to see if the pet has an attacker */ 165 /* No threat to owner, check to see if the pet has an attacker */
162 if (attacker) 166 if (attacker)
163 { 167 {
165 /* or otherwise non-hostile, and is an appropriate target */ 169 /* or otherwise non-hostile, and is an appropriate target */
166 if (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && on_same_map (pet, attacker)) 170 if (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && on_same_map (pet, attacker))
167 { 171 {
168 pet->enemy = attacker; 172 pet->enemy = attacker;
169 173
170 if (check_enemy (pet, rv) != NULL) 174 if (check_enemy (pet, rv) != 0)
171 return attacker; 175 return attacker;
172 else 176 else
173 pet->enemy = NULL; 177 pet->enemy = 0;
174 } 178 }
175 } 179 }
176 180
177 /* Don't have an attacker or legal enemy, so look for a new one!. 181 /* Don't have an attacker or legal enemy, so look for a new one!.
178 * This looks for one around where the pet is. Thus, you could lead 182 * This looks for one around where the pet is. Thus, you could lead
179 * a pet to danger, then take a few steps back. This code is basically 183 * a pet to danger, then take a few steps back. This code is basically
180 * the same as the code that looks around the owner. 184 * the same as the code that looks around the owner.
181 */ 185 */
182 if (owner->type == PLAYER && owner->contr->petmode != pet_defend) 186 if (owner->type == PLAYER && owner->contr->petmode != pet_defend)
183 { 187 {
184 tmp3 = NULL; 188 tmp3 = 0;
185 for (i = 0; i < SIZEOFFREE; i++) 189 for (i = 0; i < SIZEOFFREE; i++)
186 { 190 {
187 x = pet->x + freearr_x[search_arr[i]]; 191 x = pet->x + freearr_x[search_arr[i]];
188 y = pet->y + freearr_y[search_arr[i]]; 192 y = pet->y + freearr_y[search_arr[i]];
189 nm = pet->map; 193 nm = pet->map;
190 /* Only look on the space if there is something alive there. */ 194 /* Only look on the space if there is something alive there. */
191 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 195 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
192 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE) 196 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
193 { 197 {
194 for (tmp = get_map_ob (nm, x, y); tmp != NULL; tmp = tmp->above) 198 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
195 { 199 {
196 object *tmp2 = tmp->head == NULL ? tmp : tmp->head; 200 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
197 201
198 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) && 202 if (QUERY_FLAG (tmp2, FLAG_ALIVE) && ((!QUERY_FLAG (tmp2, FLAG_FRIENDLY) &&
199 (tmp2->type != PLAYER)) || 203 (tmp2->type != PLAYER)) ||
200 should_arena_attack (pet, owner, tmp2)) 204 should_arena_attack (pet, owner, tmp2))
201 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv)) 205 && !QUERY_FLAG (tmp2, FLAG_UNAGGRESSIVE) && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
202 { 206 {
203 207
204 if (!can_see_enemy (pet, tmp2)) 208 if (!can_see_enemy (pet, tmp2))
205 { 209 {
206 if (tmp3 != NULL) 210 if (tmp3 != 0)
207 tmp3 = tmp2; 211 tmp3 = tmp2;
208 } 212 }
209 else 213 else
210 { 214 {
211 pet->enemy = tmp2; 215 pet->enemy = tmp2;
212 if (check_enemy (pet, rv) != NULL) 216 if (check_enemy (pet, rv) != 0)
213 return tmp2; 217 return tmp2;
214 else 218 else
215 pet->enemy = NULL; 219 pet->enemy = 0;
216 } 220 }
217 } /* make sure we can get to the bugger */ 221 } /* make sure we can get to the bugger */
218 } /* for objects on this space */ 222 } /* for objects on this space */
219 } /* if there is something living on this space */ 223 } /* if there is something living on this space */
220 } /* for loop of spaces around the pet */ 224 } /* for loop of spaces around the pet */
225
226 /* fine, we went through the whole loop and didn't find one we could
227 see, take what we have */
228 if (tmp3 != 0)
229 {
230 pet->enemy = tmp3;
231 if (check_enemy (pet, rv) != 0)
232 return tmp3;
233 else
234 pet->enemy = 0;
235 }
221 } /* pet in defence mode */ 236 } /* pet in defence mode */
222 237
223 /* fine, we went through the whole loop and didn't find one we could 238 object *enemy = check_enemy (pet, rv);
224 see, take what we have */ 239 // we have a summoned pet here and the owners enemy isn't set or can't
225 if (tmp3 != NULL) 240 // be reached => search for a player around us and set it as our new enemy!!
226 { 241 if (!enemy && pet->owner && pet->owner->type != PLAYER)
227 pet->enemy = tmp3; 242 enemy = get_nearest_player (pet);
228 if (check_enemy (pet, rv) != NULL)
229 return tmp3;
230 else
231 pet->enemy = NULL;
232 }
233 243
234 /* Didn't find anything - return the owner's enemy or NULL */ 244 /* Didn't find anything - return the owner's enemy or 0 */
235 return check_enemy (pet, rv); 245 return enemy;
236} 246}
237 247
238void 248void
239terminate_all_pets (object *owner) 249terminate_all_pets (object *owner)
240{ 250{
241 objectlink *obl, *next; 251 objectlink *obl, *next;
242 252
243 for (obl = first_friendly_object; obl != NULL; obl = next) 253 for (obl = first_friendly_object; obl; obl = next)
244 { 254 {
245 object *ob = obl->ob; 255 object *ob = obl->ob;
246
247 next = obl->next; 256 next = obl->next;
257
248 if (get_owner (ob) == owner) 258 if (ob->owner == owner)
249 {
250 if (!QUERY_FLAG (ob, FLAG_REMOVED))
251 ob->remove ();
252 remove_friendly_object (ob);
253 ob->destroy (); 259 ob->destroy ();
254 }
255 } 260 }
256} 261}
257 262
258/* 263/*
259 * Unfortunately, sometimes, the owner of a pet is in the 264 * Unfortunately, sometimes, the owner of a pet is in the
261 * Thus the map isn't loaded yet, and we have to remove 266 * Thus the map isn't loaded yet, and we have to remove
262 * the pet... 267 * the pet...
263 * Interesting enough, we don't use the passed map structure in 268 * Interesting enough, we don't use the passed map structure in
264 * this function. 269 * this function.
265 */ 270 */
266
267void 271void
268remove_all_pets (maptile *map) 272remove_all_pets (maptile *map)
269{ 273{
270 objectlink *obl, *next; 274 objectlink *obl, *next;
271 object *owner; 275 object *owner;
272 276
273 for (obl = first_friendly_object; obl != NULL; obl = next) 277 for (obl = first_friendly_object; obl; obl = next)
274 { 278 {
275 next = obl->next; 279 next = obl->next;
276 if (obl->ob->type != PLAYER && QUERY_FLAG (obl->ob, FLAG_FRIENDLY) && 280
277 (owner = get_owner (obl->ob)) != NULL && !on_same_map (owner, obl->ob)) 281 if (obl->ob->type != PLAYER
282 && QUERY_FLAG (obl->ob, FLAG_FRIENDLY)
283 && (owner = obl->ob->owner) != 0
284 && !on_same_map (owner, obl->ob))
278 { 285 {
279 /* follow owner checks map status for us */ 286 /* follow owner checks map status for us */
280 follow_owner (obl->ob, owner); 287 follow_owner (obl->ob, owner);
281 } 288 }
282 } 289 }
283} 290}
284 291
285int 292int
286follow_owner (object *ob, object *owner) 293follow_owner (object *ob, object *owner)
287{ 294{
288 object *tmp; 295 if (owner->flag [FLAG_REMOVED])
289 int dir; 296 return 0; // do nothing if the owner is removed
290
291 if (!QUERY_FLAG (ob, FLAG_REMOVED))
292 ob->remove ();
293
294 if (owner->map == NULL)
295 {
296 LOG (llevError, "Can't follow owner (%d): no map.\n", &owner->name);
297 goto fail;
298 }
299 if (owner->map->in_memory != MAP_IN_MEMORY) 297 else if (!owner->map || owner->map->in_memory != MAP_IN_MEMORY)
300 {
301 LOG (llevError, "Owner of the pet not on a map in memory!?\n"); 298 LOG (llevError, "owner '%s' of the pet '%s' not on a map in memory!?\n", &owner->name, &ob->name);
302 goto fail; 299 else
303 } 300 {
304
305 dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE); 301 int dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
306 302
307 if (dir == -1) 303 if (dir >= 0)
308 {
309 LOG (llevMonster, "No space for pet to follow, freeing %s.\n", &ob->name);
310 goto fail;
311 }
312 for (tmp = ob; tmp != NULL; tmp = tmp->more)
313 {
314 tmp->x = owner->x + freearr_x[dir] + (tmp->arch == NULL ? 0 : tmp->arch->clone.x);
315 tmp->y = owner->y + freearr_y[dir] + (tmp->arch == NULL ? 0 : tmp->arch->clone.y);
316 tmp->map = owner->map;
317 if (OUT_OF_REAL_MAP (tmp->map, tmp->x, tmp->y))
318 {
319 tmp->map = get_map_from_coord (tmp->map, &tmp->x, &tmp->y);
320 } 304 {
321 } 305 owner->map->insert (ob, owner->x + freearr_x[dir], owner->y + freearr_y[dir]);
322 insert_ob_in_map (ob, ob->map, NULL, 0); 306
323 if (owner->type == PLAYER) /* Uh, I hope this is always true... */ 307 if (owner->type == PLAYER) /* Uh, I hope this is always true... */
324 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you"); 308 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you");
325 309
326 return 0; 310 return 0;
311 }
312 }
327 313
328fail:
329 remove_friendly_object (ob);
330 ob->destroy (); 314 ob->destroy ();
331
332 return 1; 315 return 1;
333} 316}
334 317
335void 318void
336pet_move (object *ob) 319pet_move (object *ob)
337{ 320{
338 int dir, i; 321 int dir = 0, i;
339 sint16 dx, dy; 322 sint16 dx, dy;
340 object *ob2, *owner; 323 object *ob2, *owner;
341 maptile *m; 324 maptile *m;
342 325
343 /* Check to see if player pulled out */ 326 /* Check to see if player pulled out */
344 if ((owner = get_owner (ob)) == NULL) 327 if ((owner = ob->owner) == NULL)
345 { 328 {
346 ob->remove (); /* Will be freed when returning */
347 remove_friendly_object (ob);
348 ob->destroy (); 329 ob->destroy ();
349 LOG (llevMonster, "Pet: no owner, leaving.\n"); 330 LOG (llevMonster, "Pet: no owner, leaving.\n");
350 return; 331 return;
351 } 332 }
352 333
353 /* 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
354 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])
355 { 338 {
356 follow_owner (ob, owner); 339 follow_owner (ob, owner);
357 return; 340 return;
358 } 341 }
359 /* Calculate Direction */ 342 /* Calculate Direction */
372 continue; 355 continue;
373 else 356 else
374 break; 357 break;
375 } 358 }
376 } 359 }
377 else 360 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
378 { 361 {
379 struct rv_vector rv; 362 struct rv_vector rv;
380 363
381 get_rangevector (ob, ob->owner, &rv, 0); 364 get_rangevector (ob, ob->owner, &rv, 0);
382 dir = rv.direction; 365 dir = rv.direction;
400 dy = part->y + freearr_y[dir]; 383 dy = part->y + freearr_y[dir];
401 m = get_map_from_coord (part->map, &dx, &dy); 384 m = get_map_from_coord (part->map, &dx, &dy);
402 if (!m) 385 if (!m)
403 continue; 386 continue;
404 387
405 for (ob2 = get_map_ob (m, dx, dy); ob2 != NULL; ob2 = ob2->above) 388 for (ob2 = GET_MAP_OB (m, dx, dy); ob2 != NULL; ob2 = ob2->above)
406 { 389 {
407 object *new_ob; 390 object *new_ob;
408 391
409 new_ob = ob2->head ? ob2->head : ob2; 392 new_ob = ob2->head ? ob2->head : ob2;
410 if (new_ob == ob) 393 if (new_ob == ob)
411 break; 394 break;
412 if (new_ob == ob->owner) 395 if (new_ob == ob->owner)
413 return; 396 return;
414 if (get_owner (new_ob) == ob->owner) 397 if (new_ob->owner == ob->owner)
415 break; 398 break;
416 399
417 /* Hmm. Did we try to move into an enemy monster? If so, 400 /* Hmm. Did we try to move into an enemy monster? If so,
418 * make it our enemy. 401 * make it our enemy.
419 */ 402 */
432 return; 415 return;
433 } 416 }
434 } 417 }
435 } 418 }
436 /* Try a different course */ 419 /* Try a different course */
437 dir = absdir (dir + 4 - (RANDOM () % 5) - (RANDOM () % 5)); 420 dir = absdir (dir + 4 - (rndm (5)) - (rndm (5)));
438 (void) move_ob (ob, dir, ob); 421 (void) move_ob (ob, dir, ob);
439 } 422 }
440 return; 423 return;
441} 424}
442 425
453 * is_golem is to note that this is a golem spell. 436 * is_golem is to note that this is a golem spell.
454 */ 437 */
455object * 438object *
456fix_summon_pet (archetype *at, object *op, int dir, int is_golem) 439fix_summon_pet (archetype *at, object *op, int dir, int is_golem)
457{ 440{
458 archetype *atmp;
459 object *tmp = NULL, *prev = NULL, *head = NULL; 441 object *tmp = NULL, *prev = NULL, *head = NULL;
460 442
461 for (atmp = at; atmp != NULL; atmp = atmp->more) 443 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
462 { 444 {
463 tmp = arch_to_object (atmp); 445 tmp = arch_to_object (atmp);
446
464 if (atmp == at) 447 if (atmp == at)
465 { 448 {
466 if (!is_golem) 449 if (!is_golem)
467 SET_FLAG (tmp, FLAG_MONSTER); 450 SET_FLAG (tmp, FLAG_MONSTER);
451
468 set_owner (tmp, op); 452 tmp->set_owner (op);
453
469 if (op->type == PLAYER) 454 if (op->type == PLAYER)
470 { 455 {
471 tmp->stats.exp = 0; 456 tmp->stats.exp = 0;
472 add_friendly_object (tmp); 457 add_friendly_object (tmp);
473 SET_FLAG (tmp, FLAG_FRIENDLY);
474 if (is_golem) 458 if (is_golem)
475 CLEAR_FLAG (tmp, FLAG_MONSTER); 459 CLEAR_FLAG (tmp, FLAG_MONSTER);
476 } 460 }
477 else 461 else
478 { 462 {
479 if (QUERY_FLAG (op, FLAG_FRIENDLY)) 463 if (QUERY_FLAG (op, FLAG_FRIENDLY))
480 { 464 {
481 object *owner = get_owner (op); 465 object *owner = op->owner;
482 466
483 if (owner != NULL) 467 if (owner)
484 { /* For now, we transfer ownership */ 468 { /* For now, we transfer ownership */
485 set_owner (tmp, owner); 469 tmp->set_owner (owner);
486 tmp->attack_movement = PETMOVE; 470 tmp->attack_movement = PETMOVE;
487 add_friendly_object (tmp); 471 add_friendly_object (tmp);
488 SET_FLAG (tmp, FLAG_FRIENDLY);
489 } 472 }
490 } 473 }
491 } 474 }
475
492 if (op->type != PLAYER || !is_golem) 476 if (op->type != PLAYER || !is_golem)
493 { 477 {
494 tmp->attack_movement = PETMOVE; 478 tmp->attack_movement = PETMOVE;
495 tmp->speed_left = -1; 479 tmp->speed_left = -1;
496 tmp->type = 0; 480 tmp->type = 0;
498 } 482 }
499 else 483 else
500 tmp->type = GOLEM; 484 tmp->type = GOLEM;
501 485
502 } 486 }
487
503 if (head == NULL) 488 if (!head)
504 head = tmp; 489 head = tmp;
490
505 tmp->x = op->x + freearr_x[dir] + tmp->arch->clone.x; 491 tmp->x = op->x + freearr_x[dir] + tmp->arch->x;
506 tmp->y = op->y + freearr_y[dir] + tmp->arch->clone.y; 492 tmp->y = op->y + freearr_y[dir] + tmp->arch->y;
507 tmp->map = op->map; 493 tmp->map = op->map;
494
508 if (tmp->invisible) 495 if (tmp->invisible)
509 tmp->invisible = 0; 496 tmp->invisible = 0;
497
510 if (head != tmp) 498 if (head != tmp)
511 tmp->head = head, prev->more = tmp; 499 tmp->head = head, prev->more = tmp;
500
512 prev = tmp; 501 prev = tmp;
513 } 502 }
503
514 head->direction = dir; 504 head->direction = dir;
515 505
516 /* need to change some monster attr to prevent problems/crashing */ 506 /* need to change some monster attr to prevent problems/crashing */
517 head->last_heal = 0; 507 head->last_heal = 0;
518 head->last_eat = 0; 508 head->last_eat = 0;
529 519
530 return head; 520 return head;
531} 521}
532 522
533/* updated this to allow more than the golem 'head' to attack */ 523/* updated this to allow more than the golem 'head' to attack */
534
535/* op is the golem to be moved. */ 524/* op is the golem to be moved. */
536
537void 525void
538move_golem (object *op) 526move_golem (object *op)
539{ 527{
540 int made_attack = 0; 528 int made_attack = 0;
541 object *tmp; 529 object *tmp;
542 530
543 if (QUERY_FLAG (op, FLAG_MONSTER)) 531 if (QUERY_FLAG (op, FLAG_MONSTER))
544 return; /* Has already been moved */ 532 return; /* Has already been moved */
545 533
546 if (get_owner (op) == NULL) 534 if (!op->owner)
547 { 535 {
548 LOG (llevDebug, "Golem without owner destructed.\n"); 536 LOG (llevDebug, "Golem without owner destructed.\n");
549 op->remove (); 537 op->remove ();
550 op->destroy (); 538 op->destroy ();
551 return; 539 return;
559 if (--op->stats.hp < 0) 547 if (--op->stats.hp < 0)
560 { 548 {
561 if (op->msg) 549 if (op->msg)
562 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg); 550 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
563 551
564 op->owner->contr->ranges[range_golem] = 0;
565 remove_friendly_object (op);
566 op->remove ();
567 op->destroy (); 552 op->destroy ();
568 return; 553 return;
569 } 554 }
570 555
571 /* Do golem attacks/movement for single & multisq golems. 556 /* Do golem attacks/movement for single & multisq golems.
590 mflags = get_map_flags (m, &m, x, y, &x, &y); 575 mflags = get_map_flags (m, &m, x, y, &x, &y);
591 576
592 if (mflags & P_OUT_OF_MAP) 577 if (mflags & P_OUT_OF_MAP)
593 continue; 578 continue;
594 579
595 for (victim = get_map_ob (op->map, x, y); victim; victim = victim->above) 580 for (victim = GET_MAP_OB (op->map, x, y); victim; victim = victim->above)
596 if (QUERY_FLAG (victim, FLAG_ALIVE)) 581 if (QUERY_FLAG (victim, FLAG_ALIVE))
597 break; 582 break;
598 583
599 /* We used to call will_hit_self to make sure we don't 584 /* We used to call will_hit_self to make sure we don't
600 * hit ourselves, but that didn't work, and I don't really 585 * hit ourselves, but that didn't work, and I don't really
603 * but since we are not trying to dereferance that pointer, 588 * but since we are not trying to dereferance that pointer,
604 * that isn't a problem. 589 * that isn't a problem.
605 */ 590 */
606 if (victim && victim != op && victim->head != op) 591 if (victim && victim != op && victim->head != op)
607 { 592 {
608
609 /* for golems with race fields, we don't attack 593 /* for golems with race fields, we don't attack
610 * aligned races 594 * aligned races
611 */ 595 */
612 596
613 if (victim->race && op->race && strstr (op->race, victim->race)) 597 if (victim->race && op->race && strstr (op->race, victim->race))
625 attack_ob (victim, op); 609 attack_ob (victim, op);
626 made_attack = 1; 610 made_attack = 1;
627 } 611 }
628 } /* If victim */ 612 } /* If victim */
629 } 613 }
614
630 if (made_attack) 615 if (made_attack)
631 update_object (op, UP_OBJ_FACE); 616 update_object (op, UP_OBJ_FACE);
632} 617}
633 618
634/* this is a really stupid function when you get down and 619/* this is a really stupid function when you get down and
656 char buf[MAX_BUF]; 641 char buf[MAX_BUF];
657 642
658 /* Because there can be different golem spells, player may want to 643 /* Because there can be different golem spells, player may want to
659 * 'lose' their old golem. 644 * 'lose' their old golem.
660 */ 645 */
661 if (op->type == PLAYER && op->contr->ranges[range_golem]) 646 if (op->type == PLAYER && op->contr->golem)
662 { 647 {
663 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem."); 648 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem.");
664 op->contr->ranges[range_golem]->remove (); 649 op->contr->golem->remove ();
665 op->contr->ranges[range_golem]->destroy (); 650 op->contr->golem->destroy ();
666 op->contr->ranges[range_golem] = 0; 651 op->contr->golem = 0;
667 } 652 }
668 653
669 if (spob->other_arch) 654 if (spob->other_arch)
670 at = spob->other_arch; 655 at = spob->other_arch;
671 else if (spob->race) 656 else if (spob->race)
691 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name); 676 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
692 return 0; 677 return 0;
693 } 678 }
694 679
695 if (!dir) 680 if (!dir)
696 dir = find_free_spot (NULL, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1); 681 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
697 682
698 if (dir == -1 || ob_blocked (&at->clone, op->map, op->x + freearr_x[dir], op->y + freearr_y[dir])) 683 if (dir < 0 || ob_blocked (at, op->map, op->x + freearr_x[dir], op->y + freearr_y[dir]))
699 { 684 {
700 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 685 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
701 return 0; 686 return 0;
702 } 687 }
688
703 /* basically want to get proper map/coordinates for this object */ 689 /* basically want to get proper map/coordinates for this object */
704 690
705 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM))) 691 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM)))
706 { 692 {
707 new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails."); 693 new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails.");
709 } 695 }
710 696
711 if (op->type == PLAYER) 697 if (op->type == PLAYER)
712 { 698 {
713 tmp->type = GOLEM; 699 tmp->type = GOLEM;
714 set_owner (tmp, op); 700 tmp->set_owner (op);
701 op->contr->golem = tmp;
702 /* give the player control of the golem */
715 set_spell_skill (op, caster, spob, tmp); 703 set_spell_skill (op, caster, spob, tmp);
716 op->contr->ranges[range_golem] = tmp;
717 /* give the player control of the golem */
718 op->contr->shoottype = range_golem;
719 } 704 }
720 else 705 else
721 { 706 {
722 if (QUERY_FLAG (op, FLAG_FRIENDLY)) 707 if (QUERY_FLAG (op, FLAG_FRIENDLY))
723 { 708 {
724 object *owner = get_owner (op); 709 object *owner = op->owner;
725 710
726 if (owner != NULL) 711 if (owner)
727 { /* For now, we transfer ownership */ 712 { /* For now, we transfer ownership */
728 set_owner (tmp, owner); 713 tmp->set_owner (owner);
729 tmp->attack_movement = PETMOVE; 714 tmp->attack_movement = PETMOVE;
730 add_friendly_object (tmp); 715 add_friendly_object (tmp);
731 SET_FLAG (tmp, FLAG_FRIENDLY);
732 } 716 }
733 } 717 }
734 718
735 SET_FLAG (tmp, FLAG_MONSTER); 719 SET_FLAG (tmp, FLAG_MONSTER);
736 } 720 }
905 * with all the checks in the 'for' portion itself. Much 889 * with all the checks in the 'for' portion itself. Much
906 * more readable to break some of the conditions out. 890 * more readable to break some of the conditions out.
907 */ 891 */
908 for (tr = spell_ob->randomitems->items; tr; tr = tr->next) 892 for (tr = spell_ob->randomitems->items; tr; tr = tr->next)
909 { 893 {
894 if (!tr->item)
895 continue;
896
910 if (level < tr->magic) 897 if (level < tr->magic)
911 break; 898 break;
912 899
913 lasttr = tr; 900 lasttr = tr;
914 901
915 if (tr->item->name == sparam) 902 if (tr->item->archname == sparam)
916 break;
917
918 if (!tr->next || !tr->next->item)
919 break; 903 break;
920 } 904 }
921 905
922 if (!lasttr) 906 if (!lasttr)
923 { 907 {
925 new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters."); 909 new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters.");
926 return 0; 910 return 0;
927 } 911 }
928 912
929 summon_arch = lasttr->item; 913 summon_arch = lasttr->item;
930 nrof = lasttr->nrof; 914 nrof = lasttr->nrof;
931 } 915 }
932 else if (spell_ob->race && !strcmp (spell_ob->race, "GODCULTMON")) 916 else if (spell_ob->race && !strcmp (spell_ob->race, "GODCULTMON"))
933 { 917 {
934 object *god = find_god (determine_god (op)), *mon, *owner; 918 object *god = find_god (determine_god (op)), *mon, *owner;
935 int summon_level, tries; 919 int summon_level, tries;
936 920
937 if (!god && ((owner = get_owner (op)) != NULL)) 921 if (!god && ((owner = op->owner) != NULL))
938 god = find_god (determine_god (owner)); 922 god = find_god (determine_god (owner));
939 923
940 /* If we can't find a god, can't get what monster to summon */ 924 /* If we can't find a god, can't get what monster to summon */
941 if (!god) 925 if (!god)
942 return 0; 926 return 0;
965 ndir = dir; 949 ndir = dir;
966 950
967 if (!ndir) 951 if (!ndir)
968 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE); 952 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE);
969 953
970 if (ndir == -1 || ob_blocked (mon, op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir])) 954 if (ndir < 0 || ob_blocked (mon, op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir]))
971 { 955 {
972 ndir = -1; 956 ndir = -1;
973 if (++tries == 5) 957 if (++tries == 5)
974 { 958 {
975 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 959 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
998 return 0; 982 return 0;
999 } 983 }
1000 984
1001 for (i = 1; i <= nrof; i++) 985 for (i = 1; i <= nrof; i++)
1002 { 986 {
1003 archetype *atmp;
1004 object *prev = NULL, *head = NULL, *tmp; 987 object *prev = NULL, *head = NULL, *tmp;
1005 988
1006 if (dir) 989 if (dir)
1007 { 990 {
1008 ndir = dir; 991 ndir = dir;
1009 dir = absdir (dir + 1); 992 dir = absdir (dir + 1);
1010 } 993 }
1011 else 994 else
1012 ndir = find_free_spot (&summon_arch->clone, op->map, op->x, op->y, 1, SIZEOFFREE); 995 ndir = find_free_spot (summon_arch, op->map, op->x, op->y, 1, SIZEOFFREE);
1013 996
1014 if (ndir > 0) 997 if (ndir > 0)
1015 { 998 {
1016 x = freearr_x[ndir]; 999 x = freearr_x[ndir];
1017 y = freearr_y[ndir]; 1000 y = freearr_y[ndir];
1018 } 1001 }
1019 1002
1020 if (ndir == -1 || ob_blocked (&summon_arch->clone, op->map, op->x + x, op->y + y)) 1003 if (ndir < 0 || ob_blocked (summon_arch, op->map, op->x + x, op->y + y))
1021 { 1004 {
1022 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 1005 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
1023 if (nrof > 1) 1006 if (nrof > 1)
1024 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting."); 1007 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting.");
1025 1008
1026 return nrof > 1; 1009 return nrof > 1;
1027 } 1010 }
1028 1011
1029 for (atmp = summon_arch; atmp != NULL; atmp = atmp->more) 1012 for (archetype *atmp = summon_arch; atmp != NULL; atmp = (archetype *)atmp->more)
1030 { 1013 {
1031 tmp = arch_to_object (atmp); 1014 tmp = arch_to_object (atmp);
1032 if (atmp == summon_arch) 1015 if (atmp == summon_arch)
1033 { 1016 {
1034 if (QUERY_FLAG (tmp, FLAG_MONSTER)) 1017 if (QUERY_FLAG (tmp, FLAG_MONSTER))
1035 { 1018 {
1036 set_owner (tmp, op); 1019 tmp->set_owner (op);
1037 set_spell_skill (op, caster, spell_ob, tmp); 1020 set_spell_skill (op, caster, spell_ob, tmp);
1038 tmp->enemy = op->enemy; 1021 tmp->enemy = op->enemy;
1039 tmp->type = 0; 1022 tmp->type = 0;
1040 CLEAR_FLAG (tmp, FLAG_SLEEP); 1023 CLEAR_FLAG (tmp, FLAG_SLEEP);
1041 1024
1042 if (op->type == PLAYER || QUERY_FLAG (op, FLAG_FRIENDLY)) 1025 if (op->type == PLAYER || QUERY_FLAG (op, FLAG_FRIENDLY))
1043 { 1026 {
1044 /* If this is not set, we make it friendly */ 1027 /* If this is not set, we make it friendly */
1045 if (!QUERY_FLAG (spell_ob, FLAG_MONSTER)) 1028 if (!QUERY_FLAG (spell_ob, FLAG_MONSTER))
1046 { 1029 {
1047 SET_FLAG (tmp, FLAG_FRIENDLY);
1048 add_friendly_object (tmp); 1030 add_friendly_object (tmp);
1049 tmp->stats.exp = 0; 1031 tmp->stats.exp = 0;
1050 1032
1051 if (spell_ob->attack_movement) 1033 if (spell_ob->attack_movement)
1052 tmp->attack_movement = spell_ob->attack_movement; 1034 tmp->attack_movement = spell_ob->attack_movement;
1053 1035
1054 if (get_owner (op)) 1036 if (op->owner)
1055 set_owner (tmp, get_owner (op)); 1037 tmp->set_owner (op->owner);
1056 } 1038 }
1057 } 1039 }
1058 } 1040 }
1059 1041
1060 if (tmp->speed > MIN_ACTIVE_SPEED) 1042 if (tmp->speed > MIN_ACTIVE_SPEED)
1068 tmp->head = head; 1050 tmp->head = head;
1069 prev->more = tmp; 1051 prev->more = tmp;
1070 } 1052 }
1071 1053
1072 prev = tmp; 1054 prev = tmp;
1073 tmp->x = op->x + x + tmp->arch->clone.x; 1055 tmp->x = op->x + x + tmp->arch->x;
1074 tmp->y = op->y + y + tmp->arch->clone.y; 1056 tmp->y = op->y + y + tmp->arch->y;
1075 tmp->map = op->map; 1057 tmp->map = op->map;
1076 } 1058 }
1077 1059
1078 head->direction = freedir[ndir]; 1060 head->direction = freedir[ndir];
1079 head->stats.exp = 0; 1061 head->stats.exp = 0;
1080 head = insert_ob_in_map (head, head->map, op, 0); 1062 head = insert_ob_in_map (head, head->map, op, 0);
1081 1063
1082 if (head && head->randomitems) 1064 if (head && head->randomitems)
1083 { 1065 {
1084 object *tmp;
1085
1086 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0); 1066 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0);
1067
1087 for (tmp = head->inv; tmp; tmp = tmp->below) 1068 for (object *tmp = head->inv; tmp; tmp = tmp->below)
1088 if (!tmp->nrof)
1089 SET_FLAG (tmp, FLAG_NO_DROP); 1069 SET_FLAG (tmp, FLAG_DESTROY_ON_DEATH);
1090 } 1070 }
1091 } /* for i < nrof */ 1071 } /* for i < nrof */
1092 1072
1093 return 1; 1073 return 1;
1094} 1074}
1101 object *realowner = ob; 1081 object *realowner = ob;
1102 1082
1103 if (realowner == NULL) 1083 if (realowner == NULL)
1104 return NULL; 1084 return NULL;
1105 1085
1106 while (get_owner (realowner) != NULL) 1086 while (realowner->owner != NULL)
1107 { 1087 {
1108 realowner = get_owner (realowner); 1088 realowner = realowner->owner;
1109 } 1089 }
1110 return realowner; 1090 return realowner;
1111} 1091}
1112 1092
1113/* determines if checks so pets don't attack players or other pets should be 1093/* determines if checks so pets don't attack players or other pets should be
1122 return 0; 1102 return 0;
1123 1103
1124 /* get the owners of itself and the target, this is to deal with pets of 1104 /* get the owners of itself and the target, this is to deal with pets of
1125 pets */ 1105 pets */
1126 rowner = get_real_owner (owner); 1106 rowner = get_real_owner (owner);
1127 if (target->type != PLAYER) 1107 towner = target->type == PLAYER ? 0 : get_real_owner (target);
1128 {
1129 towner = get_real_owner (target);
1130 }
1131 else
1132 {
1133 towner = 0;
1134 }
1135 1108
1136 /* if the pet has now owner, exit with error */ 1109 /* if the pet has now owner, exit with error */
1137 if (rowner == NULL) 1110 if (!rowner)
1138 { 1111 {
1139 LOG (llevError, "Pet has no owner.\n"); 1112 LOG (llevError, "Pet has no owner.\n");
1140 return 0; 1113 return 0;
1141 } 1114 }
1142 1115

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines