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.26 by root, Wed Mar 14 04:12:29 2007 UTC vs.
Revision 1.49 by root, Mon Dec 29 07:43:36 2008 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * 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
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * 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
19 * along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 20 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 22 */
24 23
25#include <global.h> 24#include <global.h>
26#include <sproto.h> 25#include <sproto.h>
27 26
32 * this is now tilemap aware. 31 * this is now tilemap aware.
33 */ 32 */
34object * 33object *
35get_pet_enemy (object *pet, rv_vector * rv) 34get_pet_enemy (object *pet, rv_vector * rv)
36{ 35{
37 object *owner, *tmp, *attacker, *tmp3; 36 object *tmp, *attacker, *tmp3;
38 int i; 37 int i;
39 sint16 x, y; 38 sint16 x, y;
40 maptile *nm; 39 maptile *nm;
41 int search_arr[SIZEOFFREE]; 40 int search_arr[SIZEOFFREE];
42 int mflags; 41 int mflags;
43 42
44 attacker = pet->attacked_by; /*pointer to attacking enemy */ 43 attacker = pet->attacked_by; /*pointer to attacking enemy */
45 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 */
46 45
47 if ((owner = pet->owner) != NULL) 46 object *owner = pet->owner;
48 { 47
49 /* If the owner has turned on the pet, make the pet 48 if (!owner)
50 * unfriendly.
51 */
52 if ((check_enemy (owner, rv)) == pet)
53 {
54 CLEAR_FLAG (pet, FLAG_FRIENDLY);
55 remove_friendly_object (pet);
56 pet->attack_movement &= ~PETMOVE;
57 return owner;
58 }
59 } 49 {
60 else
61 {
62 /* else the owner is no longer around, so the 50 /* the owner is no longer around, so the
63 * pet no longer needs to be friendly. 51 * pet no longer needs to be friendly.
64 */ 52 */
65 CLEAR_FLAG (pet, FLAG_FRIENDLY);
66 remove_friendly_object (pet); 53 remove_friendly_object (pet);
67 pet->attack_movement &= ~PETMOVE; 54 pet->attack_movement &= ~PETMOVE;
68 return NULL; 55 return 0;
56 }
57
58 /* If the owner has turned on the pet, make the pet
59 * unfriendly.
60 */
61 if (check_enemy (owner, rv) == pet)
69 } 62 {
63 remove_friendly_object (pet);
64 pet->attack_movement &= ~PETMOVE;
65 return owner;
66 }
67
70 /* 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 */
71 if (!on_same_map (pet, owner)) 69 //if (!on_same_map (pet, owner))
72 return NULL; 70 // return 0;
73 71
74 /* 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 */
75 if ((tmp = check_enemy (pet, rv)) != NULL) 73 if (tmp = check_enemy (pet, rv))
76 { 74 {
77 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))
78 /* without this check, you can actually get pets with 76 /* without this check, you can actually get pets with
79 * enemy set to owner! 77 * enemy set to owner!
80 */ 78 */
81 pet->enemy = NULL; 79 pet->enemy = 0;
82 else 80 else
83 return tmp; 81 return tmp;
84 } 82 }
85 83
86 get_search_arr (search_arr); 84 get_search_arr (search_arr);
88 if (owner->type == PLAYER && owner->contr->petmode > pet_normal) 86 if (owner->type == PLAYER && owner->contr->petmode > pet_normal)
89 { 87 {
90 if (owner->contr->petmode == pet_sad) 88 if (owner->contr->petmode == pet_sad)
91 { 89 {
92 tmp = find_nearest_living_creature (pet); 90 tmp = find_nearest_living_creature (pet);
93 if (tmp != NULL) 91 if (tmp != 0)
94 { 92 {
95 get_rangevector (pet, tmp, rv, 0); 93 get_rangevector (pet, tmp, rv, 0);
96 if (check_enemy (pet, rv) != NULL) 94 if (check_enemy (pet, rv) != 0)
97 return tmp; 95 return tmp;
98 else 96 else
99 pet->enemy = NULL; 97 pet->enemy = 0;
100 } 98 }
101 /* if we got here we have no enemy */ 99 /* if we got here we have no enemy */
102 /* we return NULL to avoid heading back to the owner */ 100 /* we return 0 to avoid heading back to the owner */
103 pet->enemy = NULL; 101 pet->enemy = 0;
104 return NULL; 102 return 0;
105 } 103 }
106 } 104 }
107 105
108 /* Since the pet has no existing enemy, look for anything nasty 106 /* Since the pet has no existing enemy, look for anything nasty
109 * 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)
110 */ 109 */
111 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
112 for (i = 0; i < SIZEOFFREE; i++) 115 for (i = 0; i < SIZEOFFREE; i++)
113 { 116 {
114 x = owner->x + freearr_x[search_arr[i]]; 117 x = owner->x + freearr_x[search_arr[i]];
115 y = owner->y + freearr_y[search_arr[i]]; 118 y = owner->y + freearr_y[search_arr[i]];
116 nm = owner->map; 119 nm = owner->map;
117 /* Only look on the space if there is something alive there. */ 120 /* Only look on the space if there is something alive there. */
118 mflags = get_map_flags (nm, &nm, x, y, &x, &y); 121 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
119 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 (ob->owner == 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->drop_and_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
285} 290}
286 291
287int 292int
288follow_owner (object *ob, object *owner) 293follow_owner (object *ob, object *owner)
289{ 294{
290 if (!owner->map) 295 if (owner->flag [FLAG_REMOVED])
291 LOG (llevError, "Can't follow owner (%d): no map.\n", &owner->name); 296 return 0; // do nothing if the owner is removed
292 else if (owner->map->in_memory != MAP_IN_MEMORY) 297 else if (!owner->map || owner->map->in_memory != MAP_ACTIVE)
293 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);
294 else 299 else
295 { 300 {
296 int 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);
297 302
298 if (dir >= 0) 303 if (dir >= 0)
304 309
305 return 0; 310 return 0;
306 } 311 }
307 } 312 }
308 313
309 ob->destroy (); 314 ob->drop_and_destroy ();
315
310 return 1; 316 return 1;
311} 317}
312 318
313void 319void
314pet_move (object *ob) 320pet_move (object *ob)
315{ 321{
316 int dir, i; 322 int dir = 0, i;
317 sint16 dx, dy; 323 sint16 dx, dy;
318 object *ob2, *owner; 324 object *ob2, *owner;
319 maptile *m; 325 maptile *m;
320 326
321 /* Check to see if player pulled out */ 327 /* Check to see if player pulled out */
322 if ((owner = ob->owner) == NULL) 328 if ((owner = ob->owner) == NULL)
323 { 329 {
324 ob->remove (); /* Will be freed when returning */
325 remove_friendly_object (ob);
326 ob->destroy (); 330 ob->drop_and_destroy ();
327 LOG (llevMonster, "Pet: no owner, leaving.\n"); 331 LOG (llevMonster, "Pet: no owner, leaving.\n");
328 return; 332 return;
329 } 333 }
330 334
331 /* 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
332 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])
333 { 339 {
334 follow_owner (ob, owner); 340 follow_owner (ob, owner);
335 return; 341 return;
336 } 342 }
337 /* Calculate Direction */ 343 /* Calculate Direction */
350 continue; 356 continue;
351 else 357 else
352 break; 358 break;
353 } 359 }
354 } 360 }
355 else 361 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
356 { 362 {
357 struct rv_vector rv; 363 struct rv_vector rv;
358 364
359 get_rangevector (ob, ob->owner, &rv, 0); 365 get_rangevector (ob, ob->owner, &rv, 0);
360 dir = rv.direction; 366 dir = rv.direction;
431 * is_golem is to note that this is a golem spell. 437 * is_golem is to note that this is a golem spell.
432 */ 438 */
433object * 439object *
434fix_summon_pet (archetype *at, object *op, int dir, int is_golem) 440fix_summon_pet (archetype *at, object *op, int dir, int is_golem)
435{ 441{
436 archetype *atmp;
437 object *tmp = NULL, *prev = NULL, *head = NULL; 442 object *tmp = NULL, *prev = NULL, *head = NULL;
438 443
439 for (atmp = at; atmp; atmp = atmp->more) 444 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
440 { 445 {
441 tmp = arch_to_object (atmp); 446 tmp = arch_to_object (atmp);
442 447
443 if (atmp == at) 448 if (atmp == at)
444 { 449 {
445 if (!is_golem) 450 if (!is_golem)
446 SET_FLAG (tmp, FLAG_MONSTER); 451 SET_FLAG (tmp, FLAG_MONSTER);
447 452
448 tmp->set_owner (op); 453 tmp->set_owner (op);
454
449 if (op->type == PLAYER) 455 if (op->type == PLAYER)
450 { 456 {
451 tmp->stats.exp = 0; 457 tmp->stats.exp = 0;
452 add_friendly_object (tmp); 458 add_friendly_object (tmp);
453 if (is_golem) 459 if (is_golem)
481 } 487 }
482 488
483 if (!head) 489 if (!head)
484 head = tmp; 490 head = tmp;
485 491
486 tmp->x = op->x + freearr_x[dir] + tmp->arch->clone.x; 492 tmp->x = op->x + freearr_x[dir] + tmp->arch->x;
487 tmp->y = op->y + freearr_y[dir] + tmp->arch->clone.y; 493 tmp->y = op->y + freearr_y[dir] + tmp->arch->y;
488 tmp->map = op->map; 494 tmp->map = op->map;
489 495
490 if (tmp->invisible) 496 if (tmp->invisible)
491 tmp->invisible = 0; 497 tmp->invisible = 0;
492 498
524 object *tmp; 530 object *tmp;
525 531
526 if (QUERY_FLAG (op, FLAG_MONSTER)) 532 if (QUERY_FLAG (op, FLAG_MONSTER))
527 return; /* Has already been moved */ 533 return; /* Has already been moved */
528 534
529 if (op->owner == NULL) 535 if (!op->owner)
530 { 536 {
531 LOG (llevDebug, "Golem without owner destructed.\n"); 537 LOG (llevDebug, "Golem without owner destructed.\n");
532 op->remove ();
533 op->destroy (); 538 op->drop_and_destroy ();
534 return; 539 return;
535 } 540 }
536 541
537 /* It would be nice to have a cleaner way of what message to print 542 /* It would be nice to have a cleaner way of what message to print
538 * when the golem expires than these hard coded entries. 543 * when the golem expires than these hard coded entries.
542 if (--op->stats.hp < 0) 547 if (--op->stats.hp < 0)
543 { 548 {
544 if (op->msg) 549 if (op->msg)
545 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg); 550 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
546 551
547 op->owner->contr->ranges[range_golem] = 0;
548 remove_friendly_object (op);
549 op->remove ();
550 op->destroy (); 552 op->drop_and_destroy ();
551 return; 553 return;
552 } 554 }
553 555
554 /* Do golem attacks/movement for single & multisq golems. 556 /* Do golem attacks/movement for single & multisq golems.
555 * Assuming here that op is the 'head' object. Pass only op to 557 * Assuming here that op is the 'head' object. Pass only op to
573 mflags = get_map_flags (m, &m, x, y, &x, &y); 575 mflags = get_map_flags (m, &m, x, y, &x, &y);
574 576
575 if (mflags & P_OUT_OF_MAP) 577 if (mflags & P_OUT_OF_MAP)
576 continue; 578 continue;
577 579
578 for (victim = GET_MAP_OB (op->map, x, y); victim; victim = victim->above) 580 for (victim = GET_MAP_OB (m, x, y); victim; victim = victim->above)
579 if (QUERY_FLAG (victim, FLAG_ALIVE)) 581 if (QUERY_FLAG (victim, FLAG_ALIVE))
580 break; 582 break;
581 583
582 /* 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
583 * 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
586 * but since we are not trying to dereferance that pointer, 588 * but since we are not trying to dereferance that pointer,
587 * that isn't a problem. 589 * that isn't a problem.
588 */ 590 */
589 if (victim && victim != op && victim->head != op) 591 if (victim && victim != op && victim->head != op)
590 { 592 {
591
592 /* for golems with race fields, we don't attack 593 /* for golems with race fields, we don't attack
593 * aligned races 594 * aligned races
594 */ 595 */
595 596
596 if (victim->race && op->race && strstr (op->race, victim->race)) 597 if (victim->race && op->race && strstr (op->race, victim->race))
624control_golem (object *op, int dir) 625control_golem (object *op, int dir)
625{ 626{
626 op->direction = dir; 627 op->direction = dir;
627} 628}
628 629
629/* summon golem: summons a monster for 'op'. caster is the object 630/* summon golem: summons a monster for 'op'. caster is the object
630 * casting the spell, dir is the direction to place the monster, 631 * casting the spell, dir is the direction to place the monster,
631 * at is the archetype of the monster, and spob is the spell 632 * at is the archetype of the monster, and spob is the spell
632 * object. At this stage, all spob is really used for is to 633 * object. At this stage, all spob is really used for is to
633 * adjust some values in the monster. 634 * adjust some values in the monster.
634 */ 635 */
640 char buf[MAX_BUF]; 641 char buf[MAX_BUF];
641 642
642 /* Because there can be different golem spells, player may want to 643 /* Because there can be different golem spells, player may want to
643 * 'lose' their old golem. 644 * 'lose' their old golem.
644 */ 645 */
645 if (op->type == PLAYER && op->contr->ranges[range_golem]) 646 if (op->type == PLAYER && op->contr->golem)
646 { 647 {
647 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.");
648 op->contr->ranges[range_golem]->remove ();
649 op->contr->ranges[range_golem]->destroy (); 649 op->contr->golem->drop_and_destroy ();
650 op->contr->ranges[range_golem] = 0; 650 op->contr->golem = 0;
651 } 651 }
652 652
653 if (spob->other_arch) 653 if (spob->other_arch)
654 at = spob->other_arch; 654 at = spob->other_arch;
655 else if (spob->race) 655 else if (spob->race)
675 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name); 675 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
676 return 0; 676 return 0;
677 } 677 }
678 678
679 if (!dir) 679 if (!dir)
680 dir = find_free_spot (NULL, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1); 680 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
681 681
682 if (dir < 0 || ob_blocked (&at->clone, op->map, op->x + freearr_x[dir], op->y + freearr_y[dir])) 682 if (dir < 0 || at->blocked (op->map, op->x + freearr_x[dir], op->y + freearr_y[dir]))
683 { 683 {
684 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 684 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
685 return 0; 685 return 0;
686 } 686 }
687 687
694 } 694 }
695 695
696 if (op->type == PLAYER) 696 if (op->type == PLAYER)
697 { 697 {
698 tmp->type = GOLEM; 698 tmp->type = GOLEM;
699 tmp->set_owner (op); 699 op->contr->golem = tmp;
700 /* give the player control of the golem */
700 set_spell_skill (op, caster, spob, tmp); 701 set_spell_skill (op, caster, spob, tmp);
701 op->contr->ranges[range_golem] = tmp;
702 /* give the player control of the golem */
703 op->contr->shoottype = range_golem;
704 }
705 else
706 {
707 if (QUERY_FLAG (op, FLAG_FRIENDLY))
708 {
709 object *owner = op->owner;
710
711 if (owner)
712 { /* For now, we transfer ownership */
713 tmp->set_owner (owner);
714 tmp->attack_movement = PETMOVE;
715 add_friendly_object (tmp);
716 }
717 }
718
719 SET_FLAG (tmp, FLAG_MONSTER);
720 } 702 }
721 703
722 /* make the speed positive. */ 704 /* make the speed positive. */
723 tmp->speed = FABS (tmp->speed); 705 tmp->speed = fabs (tmp->speed);
724 706
725 /* This sets the level dependencies on dam and hp for monsters */ 707 /* This sets the level dependencies on dam and hp for monsters */
726 /* players can't cope with too strong summonings. */ 708 /* players can't cope with too strong summonings. */
727 /* but monsters can. reserve these for players. */ 709 /* but monsters can. reserve these for players. */
728 if (op->type == PLAYER) 710 if (op->type == PLAYER)
733 tmp->stats.dam += SP_level_dam_adjust (caster, spob); 715 tmp->stats.dam += SP_level_dam_adjust (caster, spob);
734 else 716 else
735 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);
736 718
737 tmp->speed += .02 * SP_level_range_adjust (caster, spob); 719 tmp->speed += .02 * SP_level_range_adjust (caster, spob);
738 tmp->speed = MIN (tmp->speed, 1.0); 720 tmp->speed = min (tmp->speed, 1.0);
739 721
740 if (spob->attacktype) 722 if (spob->attacktype)
741 tmp->attacktype = spob->attacktype; 723 tmp->attacktype = spob->attacktype;
742 } 724 }
743 725
747 729
748 /* make experience increase in proportion to the strength. 730 /* make experience increase in proportion to the strength.
749 * this is a bit simplistic - we are basically just looking at how 731 * this is a bit simplistic - we are basically just looking at how
750 * often the sp doubles and use that as the ratio. 732 * often the sp doubles and use that as the ratio.
751 */ 733 */
752 tmp->stats.exp *= 1 + (MAX (spob->stats.maxgrace, spob->stats.sp) / caster_level (caster, spob)); 734 tmp->stats.exp *= 1 + max (spob->stats.maxgrace, spob->stats.sp) / casting_level (caster, spob);
753 tmp->speed_left = 0; 735 tmp->speed_left = 0;
754 tmp->direction = dir; 736 tmp->direction = dir;
755 737
756 /* Holy spell - some additional tailoring */ 738 /* Holy spell - some additional tailoring */
757 if (god) 739 if (god)
773 if (!(tmp->attacktype & AT_PHYSICAL)) 755 if (!(tmp->attacktype & AT_PHYSICAL))
774 tmp->attacktype |= AT_PHYSICAL; 756 tmp->attacktype |= AT_PHYSICAL;
775 } 757 }
776 758
777 insert_ob_in_map (tmp, tmp->map, op, 0); 759 insert_ob_in_map (tmp, tmp->map, op, 0);
760
778 return 1; 761 return 1;
779} 762}
780 763
781 764
782/*************************************************************************** 765/***************************************************************************
788/* Returns a monster (chosen at random) that this particular player (and his 771/* Returns a monster (chosen at random) that this particular player (and his
789 * god) find acceptable. This checks level, races allowed by god, etc 772 * god) find acceptable. This checks level, races allowed by god, etc
790 * to determine what is acceptable. 773 * to determine what is acceptable.
791 * This returns NULL if no match was found. 774 * This returns NULL if no match was found.
792 */ 775 */
793
794object * 776object *
795choose_cult_monster (object *pl, object *god, int summon_level) 777choose_cult_monster (object *pl, object *god, int summon_level)
796{ 778{
797 char buf[MAX_BUF]; 779 char buf[MAX_BUF];
798 const char *race; 780 const char *race;
853 * a valid entry, assuming nothing is available and quit. 835 * a valid entry, assuming nothing is available and quit.
854 */ 836 */
855 if (!mon_nr) 837 if (!mon_nr)
856 return NULL; 838 return NULL;
857 839
858 mon_nr = rndm (0, mon_nr - 1); 840 mon_nr = rndm (mon_nr - 1);
859 for (tobl = list->member; tobl; tobl = tobl->next) 841 for (tobl = list->member; tobl; tobl = tobl->next)
860 { 842 {
861 otmp = tobl->ob; 843 otmp = tobl->ob;
862 if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER)) 844 if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER))
863 continue; 845 continue;
846
864 if (otmp->level <= summon_level && !mon_nr--) 847 if (otmp->level <= summon_level && !mon_nr--)
865 return otmp; 848 return otmp;
866 } 849 }
850
867 /* This should not happen */ 851 /* This should not happen */
868 LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n"); 852 LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n");
869 return NULL; 853 return NULL;
870} 854}
871 855
878 862
879 if (spell_ob->other_arch) 863 if (spell_ob->other_arch)
880 summon_arch = spell_ob->other_arch; 864 summon_arch = spell_ob->other_arch;
881 else if (spell_ob->randomitems) 865 else if (spell_ob->randomitems)
882 { 866 {
883 int level = caster_level (caster, spell_ob); 867 int level = casting_level (caster, spell_ob);
884 treasure *tr, *lasttr = NULL; 868 treasure *tr, *lasttr = NULL;
885 869
886 shstr_cmp sparam (stringarg); 870 shstr_cmp sparam (stringarg);
887 871
888 /* In old code, this was a very convoluted for statement, 872 /* In old code, this was a very convoluted for statement,
897 if (level < tr->magic) 881 if (level < tr->magic)
898 break; 882 break;
899 883
900 lasttr = tr; 884 lasttr = tr;
901 885
902 if (tr->item->name == sparam) 886 if (tr->item->archname == sparam)
903 break; 887 break;
904 } 888 }
905 889
906 if (!lasttr) 890 if (!lasttr)
907 { 891 {
930 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name); 914 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name);
931 return 0; 915 return 0;
932 } 916 }
933 917
934 /* the summon level */ 918 /* the summon level */
935 summon_level = caster_level (caster, spell_ob); 919 summon_level = casting_level (caster, spell_ob);
936 if (summon_level == 0) 920 if (summon_level == 0)
937 summon_level = 1; 921 summon_level = 1;
938 922
939 tries = 0; 923 tries = 0;
940 do 924 do
949 ndir = dir; 933 ndir = dir;
950 934
951 if (!ndir) 935 if (!ndir)
952 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE); 936 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE);
953 937
954 if (ndir < 0 || ob_blocked (mon, op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir])) 938 if (ndir < 0 || mon->blocked (op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir]))
955 { 939 {
956 ndir = -1; 940 ndir = -1;
957 if (++tries == 5) 941 if (++tries == 5)
958 { 942 {
959 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 943 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
982 return 0; 966 return 0;
983 } 967 }
984 968
985 for (i = 1; i <= nrof; i++) 969 for (i = 1; i <= nrof; i++)
986 { 970 {
987 archetype *atmp;
988 object *prev = NULL, *head = NULL, *tmp; 971 object *prev = NULL, *head = NULL, *tmp;
989 972
990 if (dir) 973 if (dir)
991 { 974 {
992 ndir = dir; 975 ndir = dir;
993 dir = absdir (dir + 1); 976 dir = absdir (dir + 1);
994 } 977 }
995 else 978 else
996 ndir = find_free_spot (&summon_arch->clone, op->map, op->x, op->y, 1, SIZEOFFREE); 979 ndir = find_free_spot (summon_arch, op->map, op->x, op->y, 1, SIZEOFFREE);
997 980
998 if (ndir > 0) 981 if (ndir > 0)
999 { 982 {
1000 x = freearr_x[ndir]; 983 x = freearr_x[ndir];
1001 y = freearr_y[ndir]; 984 y = freearr_y[ndir];
1002 } 985 }
1003 986
1004 if (ndir < 0 || ob_blocked (&summon_arch->clone, op->map, op->x + x, op->y + y)) 987 if (ndir < 0 || summon_arch->blocked (op->map, op->x + x, op->y + y))
1005 { 988 {
1006 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); 989 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
1007 if (nrof > 1) 990 if (nrof > 1)
1008 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting."); 991 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting.");
1009 992
1010 return nrof > 1; 993 return nrof > 1;
1011 } 994 }
1012 995
1013 for (atmp = summon_arch; atmp != NULL; atmp = atmp->more) 996 for (archetype *atmp = summon_arch; atmp != NULL; atmp = (archetype *)atmp->more)
1014 { 997 {
1015 tmp = arch_to_object (atmp); 998 tmp = arch_to_object (atmp);
1016 if (atmp == summon_arch) 999 if (atmp == summon_arch)
1017 { 1000 {
1018 if (QUERY_FLAG (tmp, FLAG_MONSTER)) 1001 if (QUERY_FLAG (tmp, FLAG_MONSTER))
1051 tmp->head = head; 1034 tmp->head = head;
1052 prev->more = tmp; 1035 prev->more = tmp;
1053 } 1036 }
1054 1037
1055 prev = tmp; 1038 prev = tmp;
1056 tmp->x = op->x + x + tmp->arch->clone.x; 1039 tmp->x = op->x + x + tmp->arch->x;
1057 tmp->y = op->y + y + tmp->arch->clone.y; 1040 tmp->y = op->y + y + tmp->arch->y;
1058 tmp->map = op->map; 1041 tmp->map = op->map;
1059 } 1042 }
1060 1043
1061 head->direction = freedir[ndir]; 1044 head->direction = freedir[ndir];
1062 head->stats.exp = 0; 1045 head->stats.exp = 0;
1070 SET_FLAG (tmp, FLAG_DESTROY_ON_DEATH); 1053 SET_FLAG (tmp, FLAG_DESTROY_ON_DEATH);
1071 } 1054 }
1072 } /* for i < nrof */ 1055 } /* for i < nrof */
1073 1056
1074 return 1; 1057 return 1;
1075}
1076
1077/* recursively look through the owner property of objects until the real owner
1078is found */
1079object *
1080get_real_owner (object *ob)
1081{
1082 object *realowner = ob;
1083
1084 if (realowner == NULL)
1085 return NULL;
1086
1087 while (realowner->owner != NULL)
1088 {
1089 realowner = realowner->owner;
1090 }
1091 return realowner;
1092} 1058}
1093 1059
1094/* determines if checks so pets don't attack players or other pets should be 1060/* determines if checks so pets don't attack players or other pets should be
1095overruled by the arena petmode */ 1061overruled by the arena petmode */
1096int 1062int
1102 if ((target == NULL) || (pet == NULL) || (owner == NULL)) 1068 if ((target == NULL) || (pet == NULL) || (owner == NULL))
1103 return 0; 1069 return 0;
1104 1070
1105 /* get the owners of itself and the target, this is to deal with pets of 1071 /* get the owners of itself and the target, this is to deal with pets of
1106 pets */ 1072 pets */
1107 rowner = get_real_owner (owner); 1073 rowner = owner->outer_owner ();
1108 if (target->type != PLAYER) 1074 towner = target->is_player () ? 0 : target->outer_owner ();
1109 {
1110 towner = get_real_owner (target);
1111 }
1112 else
1113 {
1114 towner = 0;
1115 }
1116 1075
1117 /* if the pet has now owner, exit with error */ 1076 /* if the pet has now owner, exit with error */
1118 if (rowner == NULL) 1077 if (!rowner)
1119 { 1078 {
1120 LOG (llevError, "Pet has no owner.\n"); 1079 LOG (llevError, "Pet has no owner.\n");
1121 return 0; 1080 return 0;
1122 } 1081 }
1123 1082
1124 /* if the target is not a player, and has no owner, we shouldn't be here 1083 /* if the target is not a player, and has no owner, we shouldn't be here
1125 */ 1084 */
1126 if ((towner == NULL) && (target->type != PLAYER)) 1085 if (!towner && !target->is_player ())
1127 { 1086 {
1128 LOG (llevError, "Target is not a player but has no owner. We should not be here.\n"); 1087 LOG (llevError, "Target is not a player but has no owner. We should not be here.\n");
1129 return 0; 1088 return 0;
1130 } 1089 }
1131 1090
1132 /* make sure that the owner is a player */ 1091 /* make sure that the owner is a player */
1133 if (rowner->type != PLAYER) 1092 if (!rowner->is_player ())
1134 return 0; 1093 return 0;
1135 1094
1136 /* abort if the petmode is not arena */ 1095 /* abort if the petmode is not arena */
1137 if (rowner->contr->petmode != pet_arena) 1096 if (rowner->contr->petmode != pet_arena)
1138 return 0; 1097 return 0;
1140 /* abort if the pet, it's owner, or the target is not on battleground */ 1099 /* abort if the pet, it's owner, or the target is not on battleground */
1141 if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL))) 1100 if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL)))
1142 return 0; 1101 return 0;
1143 1102
1144 /* if the target is a monster, make sure it's owner is not the same */ 1103 /* if the target is a monster, make sure it's owner is not the same */
1145 if ((target->type != PLAYER) && (rowner == towner)) 1104 if (!target->is_player () && rowner == towner)
1146 return 0; 1105 return 0;
1147 1106
1148 /* check if the target is a player which affects how it will handle 1107 /* check if the target is a player which affects how it will handle
1149 parties */ 1108 parties */
1150 if (target->type != PLAYER) 1109 if (!target->is_player ())
1151 { 1110 {
1152 /* if the target is owned by a player make sure than make sure 1111 /* if the target is owned by a player make sure than make sure
1153 it's not in the same party */ 1112 it's not in the same party */
1154 if ((towner->type == PLAYER) && (rowner->contr->party != NULL)) 1113 if (towner->is_player () && rowner->contr->party)
1155 {
1156 if (rowner->contr->party == towner->contr->party) 1114 if (rowner->contr->party == towner->contr->party)
1157 return 0; 1115 return 0;
1158 }
1159 } 1116 }
1160 else 1117 else
1161 { 1118 {
1162 /* if the target is a player make sure than make sure it's not 1119 /* if the target is a player make sure than make sure it's not
1163 in the same party */ 1120 in the same party */
1164 if (rowner->contr->party != NULL) 1121 if (rowner->contr->party)
1165 {
1166 if (rowner->contr->party == target->contr->party) 1122 if (rowner->contr->party == target->contr->party)
1167 return 0; 1123 return 0;
1168 }
1169 } 1124 }
1170 1125
1171 return 1; 1126 return 1;
1172} 1127}
1128

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines