--- deliantra/server/server/spell_attack.C 2007/07/22 14:17:58 1.44 +++ deliantra/server/server/spell_attack.C 2012/01/03 11:25:37 1.108 @@ -1,24 +1,25 @@ /* - * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. + * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team - * Copyright (©) 2002-2003,2007 Mark Wedel & Crossfire Development Team - * Copyright (©) 1992,2007 Frank Tore Johansen + * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992 Frank Tore Johansen * - * Crossfire TRT is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Deliantra is free software: you can redistribute it and/or modify it under + * the terms of the Affero GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the Affero GNU General Public License + * and the GNU General Public License along with this program. If not, see + * . * - * The authors can be reached via e-mail to + * The authors can be reached via e-mail to */ /* This file contains all the spell attack code. Grouping this code @@ -38,11 +39,9 @@ * but moved here so it could be applied to bolts too * op is the spell object. */ - -void +static void check_spell_knockback (object *op) { - object *tmp, *tmp2; /* object on the map */ int weight_move; int frictionmod = 2; /*poor man's physics - multipy targets weight by this amount */ @@ -57,12 +56,12 @@ /*LOG (llevDebug, "DEBUG: arch weighs %d and masses %d (%s,level %d)\n", op->weight,weight_move,op->name,op->level); */ } - for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) + for (object *tmp = op->ms ().bot; tmp; tmp = tmp->above) { int num_sections = 1; /* don't move DM */ - if (QUERY_FLAG (tmp, FLAG_WIZ)) + if (tmp->flag [FLAG_WIZ]) return; /* don't move parts of objects */ @@ -70,11 +69,11 @@ continue; /* don't move floors or immobile objects */ - if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) || (!QUERY_FLAG (tmp, FLAG_ALIVE) && QUERY_FLAG (tmp, FLAG_NO_PICK))) + if (tmp->flag [FLAG_IS_FLOOR] || (!tmp->flag [FLAG_ALIVE] && tmp->flag [FLAG_NO_PICK])) continue; /* count the object's sections */ - for (tmp2 = tmp; tmp2 != NULL; tmp2 = tmp2->more) + for (object *tmp2 = tmp; tmp2; tmp2 = tmp2->more) num_sections++; /* I'm not sure if it makes sense to divide by num_sections - bigger @@ -96,7 +95,7 @@ * I don't see us doing anything useful with that information * right now. */ - move_object (tmp, absdir (op->stats.sp)); + tmp->move (absdir (op->stats.sp)); } } @@ -108,11 +107,10 @@ * ***************************************************************************/ -/* Causes op to fork. op is the original bolt, tmp +/* Causes op to fork. op is the original bolt, tmp * is the first piece of the fork. */ - -void +static void forklightning (object *op, object *tmp) { int new_dir = 1; /* direction or -1 for left, +1 for right 0 if no new bolt */ @@ -153,6 +151,7 @@ new_bolt->stats.dam++; tmp->stats.dam /= 2; /* reduce father bolt damage */ tmp->stats.dam++; + if ((new_bolt = m->insert (new_bolt, sx, sy, op))) update_turn_face (new_bolt); } @@ -160,7 +159,6 @@ /* move_bolt: moves bolt 'op'. Basically, it just advances a space, * and checks for various things that may stop it. */ - void move_bolt (object *op) { @@ -170,7 +168,7 @@ if (--op->duration < 0) { - op->destroy (); + op->drop_and_destroy (); return; } @@ -198,7 +196,7 @@ */ if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y)) || ((mflags & P_IS_ALIVE) && reflwall (m, x, y, op))) { - if (!QUERY_FLAG (op, FLAG_REFLECTING)) + if (!op->flag [FLAG_REFLECTING]) return; /* Since walls don't run diagonal, if the bolt is in @@ -253,10 +251,8 @@ /* New forking code. Possibly create forks of this object * going off in other directions. */ - if (rndm (0, 99) < tmp->stats.Dex) - { /* stats.Dex % of forking */ - forklightning (op, tmp); - } + if (tmp->stats.Dex && rndm (0, 99) < tmp->stats.Dex) + forklightning (op, tmp); /* stats.Dex % of forking */ /* In this way, the object left behind sticks on the space, but * doesn't create any bolts that continue to move onward. @@ -283,22 +279,24 @@ if (!spob->other_arch) return 0; - tmp = arch_to_object (spob->other_arch); + tmp = spob->other_arch->instance (); if (tmp == NULL) return 0; /* peterm: level dependency for bolts */ tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob); tmp->attacktype = spob->attacktype; + if (spob->slaying) tmp->slaying = spob->slaying; + tmp->range = spob->range + SP_level_range_adjust (caster, spob); tmp->duration = spob->duration + SP_level_duration_adjust (caster, spob); tmp->stats.Dex = spob->stats.Dex; tmp->stats.Con = spob->stats.Con; tmp->direction = dir; - if (QUERY_FLAG (tmp, FLAG_IS_TURNABLE)) + if (tmp->flag [FLAG_IS_TURNABLE]) SET_ANIMATION (tmp, dir); tmp->set_owner (op); @@ -312,7 +310,7 @@ mflags = get_map_flags (tmp->map, &newmap, tmp->x, tmp->y, &tmp->x, &tmp->y); if (mflags & P_OUT_OF_MAP) { - tmp->destroy (); + tmp->drop_and_destroy (); return 0; } @@ -320,9 +318,9 @@ if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (tmp->map, tmp->x, tmp->y))) { - if (!QUERY_FLAG (tmp, FLAG_REFLECTING)) + if (!tmp->flag [FLAG_REFLECTING]) { - tmp->destroy (); + tmp->drop_and_destroy (); return 0; } @@ -345,7 +343,7 @@ ***************************************************************************/ /* expands an explosion. op is a piece of the - * explosion - this expans it in the different directions. + * explosion - this expands it in the different directions. * At least that is what I think this does. */ void @@ -393,12 +391,12 @@ * poison cloud ball, etc. op is the object to * explode. */ -void +static void explode_bullet (object *op) { object *tmp, *owner; - if (op->other_arch == NULL) + if (!op->other_arch) { LOG (llevError, "BUG: explode_bullet(): op without other_arch\n"); op->destroy (); @@ -407,8 +405,9 @@ if (op->env) { - object *env = object_get_env_recursive (op); - if (env->map == NULL || out_of_map (env->map, env->x, env->y)) + object *env = op->outer_env (); + + if (!env->map || out_of_map (env->map, env->x, env->y)) { LOG (llevError, "BUG: explode_bullet(): env out of map\n"); op->destroy (); @@ -436,19 +435,23 @@ if (op->attacktype) { hit_map (op, 0, op->attacktype, 1); + if (op->destroyed ()) return; } /* other_arch contains what this explodes into */ - tmp = arch_to_object (op->other_arch); + tmp = op->other_arch->instance (); tmp->set_owner (op); tmp->skill = op->skill; owner = op->owner; - if ((tmp->attacktype & AT_HOLYWORD || tmp->attacktype & AT_GODPOWER) && owner && !tailor_god_spell (tmp, owner)) + if ((tmp->attacktype & AT_HOLYWORD + || tmp->attacktype & AT_GODPOWER) + && owner + && !tailor_god_spell (tmp, owner)) { op->destroy (); return; @@ -485,6 +488,8 @@ op->move_on = 0; tmp->insert_at (op, op); + tmp->play_sound (tmp->sound); + /* remove the firebullet */ op->destroy (); } @@ -518,12 +523,14 @@ for (tmp = op->ms ().bot; tmp; tmp = tmp->above) { - if (QUERY_FLAG (tmp, FLAG_ALIVE)) + if (tmp->flag [FLAG_ALIVE]) { dam = hit_player (tmp, op->stats.dam, op, op->attacktype, 1); + + // TODO: can't understand the following if's if (op->destroyed () || !tmp->destroyed () || (op->stats.dam -= dam) < 0) { - if (!QUERY_FLAG (op, FLAG_REMOVED)) + if (!op->flag [FLAG_REMOVED]) { op->destroy (); return; @@ -541,10 +548,6 @@ void move_bullet (object *op) { - sint16 new_x, new_y; - int mflags; - maptile *m; - #if 0 /* We need a better general purpose way to do this */ @@ -569,18 +572,20 @@ return; } - new_x = op->x + DIRX (op); - new_y = op->y + DIRY (op); - m = op->map; - mflags = get_map_flags (m, &m, new_x, new_y, &new_x, &new_y); + mapxy pos (op); + pos.move (op->direction); - if (mflags & P_OUT_OF_MAP) + if (!pos.normalise ()) { op->destroy (); return; } - if (!op->direction || OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, new_x, new_y))) + mapspace &ms = pos.ms (); + + ms.update (); + + if (!op->direction || OB_TYPE_MOVE_BLOCK (op, ms.move_block)) { if (op->other_arch) explode_bullet (op); @@ -590,7 +595,7 @@ return; } - if (!(op = m->insert (op, new_x, new_y, op))) + if (!(op = pos.insert (op, op))) return; if (reflwall (op->map, op->x, op->y, op)) @@ -619,11 +624,11 @@ if (!spob->other_arch) return 0; - tmp = arch_to_object (spob->other_arch); - if (tmp == NULL) + tmp = spob->other_arch->instance (); + if (!tmp) return 0; - /* peterm: level dependency for bolts */ + /* peterm: level dependency for bolts */ tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob); tmp->attacktype = spob->attacktype; if (spob->slaying) @@ -637,14 +642,14 @@ tmp->dam_modifier = spob->stats.food + SP_level_dam_adjust (caster, spob); tmp->direction = dir; - if (QUERY_FLAG (tmp, FLAG_IS_TURNABLE)) + if (tmp->flag [FLAG_IS_TURNABLE]) SET_ANIMATION (tmp, dir); tmp->set_owner (op); set_spell_skill (op, caster, spob, tmp); - tmp->x = op->x + freearr_x[dir]; - tmp->y = op->y + freearr_y[dir]; + tmp->x = op->x + freearr_x[dir]; + tmp->y = op->y + freearr_y[dir]; tmp->map = op->map; maptile *newmap; @@ -657,9 +662,22 @@ tmp->map = newmap; + // in case the bullet has direction 0 we explode it in place. + // direction 0 is possible for instance when a poison cloud trap springs. + if (tmp->direction == 0) + { + if (tmp->other_arch + && (tmp = tmp->insert_at (tmp, op))) // insert before explode cleanly + explode_bullet (tmp); // explode object will/should remove tmp + else + tmp->destroy (); + + return 0; + } + if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (tmp->map, tmp->x, tmp->y))) { - if (!QUERY_FLAG (tmp, FLAG_REFLECTING)) + if (!tmp->flag [FLAG_REFLECTING]) { tmp->destroy (); return 0; @@ -684,10 +702,10 @@ *****************************************************************************/ /* drops an object based on what is in the cone's "other_arch" */ -void +static void cone_drop (object *op) { - object *new_ob = arch_to_object (op->other_arch); + object *new_ob = op->other_arch->instance (); new_ob->level = op->level; new_ob->set_owner (op->owner); @@ -704,8 +722,6 @@ void move_cone (object *op) { - int i; - /* if no map then hit_map will crash so just ignore object */ if (!op->map) { @@ -715,7 +731,7 @@ } /* lava saves it's life, but not yours :) */ - if (QUERY_FLAG (op, FLAG_LIFESAVE)) + if (op->flag [FLAG_LIFESAVE]) { hit_map (op, 0, op->attacktype, 0); return; @@ -735,17 +751,22 @@ hit_map (op, 0, op->attacktype, 0); + if (!op->is_on_map ()) + return; + /* Check to see if we should push anything. * Spell objects with weight push whatever they encounter to some * degree. */ if (op->weight) - check_spell_knockback (op); + { + check_spell_knockback (op); - if (op->destroyed ()) - return; + if (!op->is_on_map ()) + return; + } - if ((op->duration--) < 0) + if (op->duration-- < 0) { op->destroy (); return; @@ -760,7 +781,7 @@ return; } - for (i = -1; i < 2; i++) + for (int i = -1; i <= 1; i++) { sint16 x = op->x + freearr_x[absdir (op->stats.sp + i)], y = op->y + freearr_y[absdir (op->stats.sp + i)]; @@ -801,9 +822,9 @@ if (!spell->other_arch) return 0; - if (op->type == PLAYER && QUERY_FLAG (op, FLAG_UNDEAD) && op->attacktype & AT_TURN_UNDEAD) + if (op->type == PLAYER && op->flag [FLAG_UNDEAD] && op->attacktype & AT_TURN_UNDEAD) { - new_draw_info (NDI_UNIQUE, 0, op, "Your undead nature prevents you from turning undead!"); + op->failmsg ("Your undead nature prevents you from turning undead!"); return 0; } @@ -821,18 +842,14 @@ for (i = range_min; i <= range_max; i++) { - sint16 x, y, d; + sint16 x, y; /* We can't use absdir here, because it never returns - * 0. If this is a rune, we want to hit the person on top + * 0. If this is a rune, we want to hit the person on top * of the trap (d==0). If it is not a rune, then we don't want * to hit that person. */ - d = dir + i; - while (d < 0) - d += 8; - while (d > 8) - d -= 8; + int d = dir ? absdir (dir + i) : i; /* If it's not a rune, we don't want to blast the caster. * In that case, we have to see - if dir is specified, @@ -859,10 +876,10 @@ continue; success = 1; - tmp = arch_to_object (spell->other_arch); + tmp = spell->other_arch->instance (); tmp->set_owner (op); set_spell_skill (op, caster, spell, tmp); - tmp->level = caster_level (caster, spell); + tmp->level = casting_level (caster, spell); tmp->attacktype = spell->attacktype; /* holy word stuff */ @@ -937,22 +954,16 @@ void animate_bomb (object *op) { - int i; - object *env, *tmp; - if (op->state != NUM_ANIMATIONS (op) - 1) return; - env = object_get_env_recursive (op); + object *env = op->outer_env (); if (op->env) { - if (env->map == NULL) + if (!env->map) return; - if (env->type == PLAYER) - esrv_del_item (env->contr, op->count); - if (!(op = op->insert_at (env, op))) return; } @@ -960,7 +971,7 @@ // elmex Tue Aug 15 17:46:51 CEST 2006: Prevent bomb from exploding // on a safe map. I don't like this special casing, but it seems to be neccessary // as bombs can be carried. - if (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_SAFE) + if (op->ms ().flags () & P_SAFE) { op->destroy (); return; @@ -972,12 +983,12 @@ */ if (archetype *at = archetype::find (SPLINT)) { - for (i = 1; i < 9; i++) + for (int i = 1; i < 9; i++) { if (out_of_map (op->map, op->x + freearr_x[i], op->y + freearr_x[i])) continue; - tmp = arch_to_object (at); + object *tmp = at->instance (); tmp->direction = i; tmp->range = op->range; tmp->stats.dam = op->stats.dam; @@ -987,7 +998,7 @@ if (op->skill && op->skill != tmp->skill) tmp->skill = op->skill; - if (QUERY_FLAG (tmp, FLAG_IS_TURNABLE)) + if (tmp->flag [FLAG_IS_TURNABLE]) SET_ANIMATION (tmp, i); op->map->insert (tmp, op->x + freearr_x[i], op->y + freearr_x[i], op); @@ -1001,19 +1012,27 @@ int create_bomb (object *op, object *caster, int dir, object *spell) { - object *tmp; int mflags; sint16 dx = op->x + freearr_x[dir], dy = op->y + freearr_y[dir]; maptile *m; mflags = get_map_flags (op->map, &m, dx, dy, &dx, &dy); - if ((mflags & P_OUT_OF_MAP) || (GET_MAP_MOVE_BLOCK (m, dx, dy) & MOVE_WALK)) + + // when creating a bomb below ourself it should always work, even + // when movement is blocked (somehow we got here, somehow we are here, + // so we should also be able to make a bomb here). (originally added + // to fix create bomb traps in doors, which cast with dir=0). + if (dir) { - new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way."); - return 0; + if ((mflags & P_OUT_OF_MAP) || (GET_MAP_MOVE_BLOCK (m, dx, dy) & MOVE_WALK)) + { + op->failmsg ("There is something in the way."); + return 0; + } } - tmp = arch_to_object (spell->other_arch); + + tmp = spell->other_arch->instance (); /* level dependencies for bomb */ tmp->range = spell->range + SP_level_range_adjust (caster, spell); @@ -1043,7 +1062,7 @@ * type is the type of spell - either SPELL_MANA or SPELL_GRACE. * this info is used for blocked magic/unholy spaces. */ -object * +static object * get_pointed_target (object *op, int dir, int range, int type) { object *target; @@ -1072,7 +1091,7 @@ if (mflags & P_IS_ALIVE) for (target = GET_MAP_OB (mp, x, y); target; target = target->above) - if (QUERY_FLAG (target, FLAG_MONSTER)) + if (target->flag [FLAG_MONSTER]) return target; } @@ -1106,29 +1125,31 @@ * can't be friendly to your god. */ - if (!target || QUERY_FLAG (target, FLAG_REFL_SPELL) + if (!target + || target->flag [FLAG_REFL_SPELL] || (!god && spell->stats.grace) - || (target->title && god && !strcmp (target->title, god->name)) || (target->race && god && strstr (target->race, god->race))) + || (god && target->title == god->name) + || (god && target->race.contains (god->race))) { - new_draw_info (NDI_UNIQUE, 0, op, "Your request is unheeded."); + op->failmsg ("Your request is unheeded."); return 0; } if (spell->other_arch) - effect = arch_to_object (spell->other_arch); + effect = spell->other_arch->instance (); else return 0; /* tailor the effect by priest level and worshipped God */ - effect->level = caster_level (caster, spell); + effect->level = casting_level (caster, spell); effect->attacktype = spell->attacktype; if (effect->attacktype & (AT_HOLYWORD | AT_GODPOWER)) { if (tailor_god_spell (effect, op)) - new_draw_info_format (NDI_UNIQUE, 0, op, "%s answers your call!", determine_god (op)); + new_draw_info_format (NDI_UNIQUE, 0, op, "%s answers your call!", (const char *)determine_god (op)); else { - new_draw_info (NDI_UNIQUE, 0, op, "Your request is ignored."); + op->failmsg ("Your request is ignored."); return 0; } } @@ -1142,7 +1163,7 @@ effect->level = spell->stats.dam + SP_level_dam_adjust (caster, spell); /* casting death spells at undead isn't a good thing */ - if (QUERY_FLAG (target, FLAG_UNDEAD)) + if (target->flag [FLAG_UNDEAD]) { if (random_roll (0, 2, op, PREFER_LOW)) { @@ -1174,7 +1195,6 @@ return 1; } - /**************************************************************************** * * MAGIC MISSILE code. @@ -1186,62 +1206,47 @@ void move_missile (object *op) { - int i, mflags; - object *owner; - sint16 new_x, new_y; - maptile *m; - if (op->range-- <= 0) { - op->destroy (); + op->drop_and_destroy (); return; } - owner = op->owner; -#if 0 - /* It'd make things nastier if this wasn't here - spells cast by - * monster that are then killed would continue to survive - */ - if (owner == NULL) + mapxy pos (op); + pos.move (op->direction); + + if (!pos.normalise ()) { op->destroy (); return; } -#endif - - new_x = op->x + DIRX (op); - new_y = op->y + DIRY (op); - mflags = get_map_flags (op->map, &m, new_x, new_y, &new_x, &new_y); + mapspace &ms = pos.ms (); - if (!(mflags & P_OUT_OF_MAP) && ((mflags & P_IS_ALIVE) || OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, new_x, new_y)))) + if (ms.flags () & P_IS_ALIVE || ms.blocks (op)) { hit_map (op, op->direction, AT_MAGIC, 1); /* Basically, missile only hits one thing then goes away. * we need to remove it if someone hasn't already done so. */ - if (!op->destroyed ()) - op->destroy (); - + op->destroy (); return; } - op->remove (); - - if (!op->direction || (mflags & P_OUT_OF_MAP)) + if (!op->direction) { op->destroy (); return; } - i = spell_find_dir (m, new_x, new_y, op->owner); + int i = spell_find_dir (pos.m, pos.x, pos.y, op->owner); if (i > 0 && i != op->direction) { op->direction = i; SET_ANIMATION (op, op->direction); } - m->insert (op, new_x, new_y, op); + pos.insert (op, op); } /**************************************************************************** @@ -1254,25 +1259,22 @@ * make this work for non-living objects, we would have to * give them the capability to have an inventory. b.t. */ -int +static int make_object_glow (object *op, int radius, int time) { /* some things are unaffected... */ if (op->path_denied & PATH_LIGHT) return 0; - object *tmp = get_archetype (FORCE_NAME); - tmp->speed = 0.01; + object *tmp = archetype::get (FORCE_NAME); + tmp->set_speed (0.01); tmp->stats.food = time; - SET_FLAG (tmp, FLAG_IS_USED_UP); - tmp->glow_radius = radius; - if (tmp->glow_radius > MAX_LIGHT_RADII) - tmp->glow_radius = MAX_LIGHT_RADII; - + tmp->set_flag (FLAG_IS_USED_UP); + tmp->set_glow_radius (min (MAX_LIGHT_RADIUS, radius)); tmp = insert_ob_in_ob (tmp, op); if (tmp->glow_radius > op->glow_radius) - op->glow_radius = tmp->glow_radius; + op->set_glow_radius (tmp->glow_radius); return 1; } @@ -1280,78 +1282,46 @@ int cast_destruction (object *op, object *caster, object *spell_ob) { - int i, j, range, mflags, friendly = 0, dam, dur; - sint16 sx, sy; - maptile *m; - object *tmp; - const char *skill; + int range = spell_ob->range + SP_level_range_adjust (caster, spell_ob); + int dam = spell_ob->stats.dam + SP_level_dam_adjust (caster, spell_ob); + int dur = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob); - range = spell_ob->range + SP_level_range_adjust (caster, spell_ob); - dam = spell_ob->stats.dam + SP_level_dam_adjust (caster, spell_ob); - dur = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob); - if (QUERY_FLAG (op, FLAG_FRIENDLY) || op->type == PLAYER) - friendly = 1; - - /* destruction doesn't use another spell object, so we need - * update op's skill pointer so that exp is properly awarded. - * We do some shortcuts here - since this is just temporary - * and we'll reset the values back, we don't need to go through - * the full share string/free_string route. - */ - skill = op->skill; - if (caster == op) - op->skill = spell_ob->skill; - else if (caster->skill) - op->skill = caster->skill; - else - op->skill = NULL; - - op->change_skill (find_skill_by_name (op, op->skill)); + bool friendly = op->flag [FLAG_FRIENDLY] || op->is_player (); - for (i = -range; i < range; i++) + dynbuf buf; + unordered_mapwalk (buf, op, -range, -range, range, range) { - for (j = -range; j < range; j++) - { - m = op->map; - sx = op->x + i; - sy = op->y + j; + mapspace &ms = m->at (nx, ny); - mflags = get_map_flags (m, &m, sx, sy, &sx, &sy); - if (mflags & P_OUT_OF_MAP) - continue; - - if (mflags & P_IS_ALIVE) - { - for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above) - if (QUERY_FLAG (tmp, FLAG_ALIVE) || tmp->type == PLAYER) - break; + if (ms.flags () & P_IS_ALIVE) + for (object *next, *tmp = ms.bot; tmp; tmp = next) + { + next = tmp->above; - if (tmp) - { - if (tmp->head) - tmp = tmp->head; + if (tmp->flag [FLAG_ALIVE] || tmp->is_player ()) + { + tmp = tmp->head_ (); - if ((friendly && !QUERY_FLAG (tmp, FLAG_FRIENDLY) && tmp->type != PLAYER) || - (!friendly && (QUERY_FLAG (tmp, FLAG_FRIENDLY) || tmp->type == PLAYER))) - { - if (spell_ob->subtype == SP_DESTRUCTION) - { - hit_player (tmp, dam, op, spell_ob->attacktype, 0); - if (spell_ob->other_arch) - m->insert (arch_to_object (spell_ob->other_arch), sx, sy, op); - } - else if (spell_ob->subtype == SP_FAERY_FIRE && tmp->resist[ATNR_MAGIC] != 100) - { - if (make_object_glow (tmp, 1, dur) && spell_ob->other_arch) - m->insert (arch_to_object (spell_ob->other_arch), sx, sy, op); - } - } - } - } - } + if ((friendly && !tmp->flag [FLAG_FRIENDLY] && !tmp->is_player ()) + || (!friendly && (tmp->flag [FLAG_FRIENDLY] || tmp->is_player ()))) + { + if (spell_ob->subtype == SP_DESTRUCTION) + { + hit_player (tmp, dam, op, spell_ob->attacktype, 0); + + if (spell_ob->other_arch) + m->insert (spell_ob->other_arch->instance (), nx, ny, op); + } + else if (spell_ob->subtype == SP_FAERY_FIRE && tmp->resist [ATNR_MAGIC] != 100) + { + if (make_object_glow (tmp, 1, dur) && spell_ob->other_arch) + m->insert (spell_ob->other_arch->instance (), nx, ny, op); + } + } + } + } } - op->skill = skill; return 1; } @@ -1360,7 +1330,6 @@ * CURSE * ***************************************************************************/ - int cast_curse (object *op, object *caster, object *spell_ob, int dir) { @@ -1370,12 +1339,14 @@ tmp = get_pointed_target (op, (dir == 0) ? op->direction : dir, spell_ob->range, SPELL_GRACE); if (!tmp) { - new_draw_info (NDI_UNIQUE, 0, op, "There is no one in that direction to curse."); + op->failmsg ("There is no one in that direction to curse."); return 0; } + tmp = tmp->head_ (); + /* If we've already got a force of this type, don't add a new one. */ - for (force = tmp->inv; force != NULL; force = force->below) + for (force = tmp->inv; force; force = force->below) { if (force->type == FORCE && force->subtype == FORCE_CHANGE_ABILITY) { @@ -1391,10 +1362,11 @@ } } - if (force == NULL) + if (!force) { - force = get_archetype (FORCE_NAME); + force = archetype::get (FORCE_NAME); force->subtype = FORCE_CHANGE_ABILITY; + if (spell_ob->race) force->name = spell_ob->race; else @@ -1414,15 +1386,15 @@ new_draw_info (NDI_UNIQUE, 0, op, "You recast the spell while in effect."); } else - { - new_draw_info (NDI_UNIQUE, 0, op, "Recasting the spell had no effect."); - } + new_draw_info (NDI_UNIQUE, 0, op, "Recasting the spell had no effect."); + return 1; } + force->duration = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob) * 50; - force->speed = 1.f; force->speed_left = -1.f; - SET_FLAG (force, FLAG_APPLIED); + force->set_speed (1.f); + force->set_flag (FLAG_APPLIED); if (god) { @@ -1445,8 +1417,8 @@ change_abil (tmp, force); /* Mostly to display any messages */ insert_ob_in_ob (force, tmp); tmp->update_stats (); - return 1; + return 1; } /********************************************************************** @@ -1462,16 +1434,14 @@ mood_change (object *op, object *caster, object *spell) { object *tmp, *god, *head; - int done_one, range, mflags, level, at, best_at; - sint16 x, y, nx, ny; - maptile *m; + int done_one, range, level, at, best_at; const char *race; /* We precompute some values here so that we don't have to keep * doing it over and over again. */ god = find_god (determine_god (op)); - level = caster_level (caster, spell); + level = casting_level (caster, spell); range = spell->range + SP_level_range_adjust (caster, spell); /* On the bright side, no monster should ever have a race of GOD_... @@ -1480,156 +1450,150 @@ */ if (!spell->race) race = NULL; - else if (god && !strcmp (spell->race, "GOD_SLAYING")) + else if (god && spell->race == shstr_GOD_SLAYING) race = god->slaying; - else if (god && !strcmp (spell->race, "GOD_FRIEND")) + else if (god && spell->race == shstr_GOD_FRIEND) race = god->race; else race = spell->race; - for (x = op->x - range; x <= op->x + range; x++) - for (y = op->y - range; y <= op->y + range; y++) - { - done_one = 0; - m = op->map; - nx = x; - ny = y; - mflags = get_map_flags (m, &m, x, y, &nx, &ny); - if (mflags & P_OUT_OF_MAP) - continue; - - /* If there is nothing living on this space, no need to go further */ - if (!(mflags & P_IS_ALIVE)) - continue; - - // players can only affect spaces that they can actually see - if (caster && caster->contr - && caster->contr->visibility_at (m, nx, ny) < 70) - continue; - - for (tmp = GET_MAP_TOP (m, nx, ny); tmp; tmp = tmp->below) - if (QUERY_FLAG (tmp, FLAG_MONSTER)) - break; - - /* There can be living objects that are not monsters */ - if (!tmp || tmp->type == PLAYER) - continue; - - /* Only the head has meaningful data, so resolve to that */ - if (tmp->head) - head = tmp->head; - else - head = tmp; - - /* Make sure the race is OK. Likewise, only effect undead if spell specifically allows it */ - if (race && head->race && !strstr (race, head->race)) - continue; - - if (QUERY_FLAG (head, FLAG_UNDEAD) && !QUERY_FLAG (spell, FLAG_UNDEAD)) - continue; - - /* Now do a bunch of stuff related to saving throws */ - best_at = -1; - if (spell->attacktype) - { - for (at = 0; at < NROFATTACKS; at++) - if (spell->attacktype & (1 << at)) - if (best_at == -1 || head->resist[at] > head->resist[best_at]) - best_at = at; - - if (best_at == -1) - at = 0; - else - { - if (head->resist[best_at] == 100) - continue; - else - at = head->resist[best_at] / 5; - } - at -= level / 5; - if (did_make_save (head, head->level, at)) - continue; - } - else /* spell->attacktype */ - { - /* - Spell has no attacktype (charm & such), so we'll have a specific saving: - * if spell level < monster level, no go - * else, chance of effect = 20 + min( 50, 2 * ( spell level - monster level ) ) - - The chance will then be in the range [20-70] percent, not too bad. - - This is required to fix the 'charm monster' abuse, where a player level 1 can - charm a level 125 monster... - - Ryo, august 14th - */ - if (head->level > level) - continue; - - if (random_roll (0, 100, caster, PREFER_LOW) >= (20 + MIN (50, 2 * (level - head->level)))) - /* Failed, no effect */ - continue; - } + dynbuf buf; + unordered_mapwalk (buf, op, -range, -range, range, range) + { + mapspace &ms = m->at (nx, ny); - /* Done with saving throw. Now start affecting the monster */ + /* If there is nothing living on this space, no need to go further */ + if (!ms.flags () & P_IS_ALIVE) + continue; - /* aggravation */ - if (QUERY_FLAG (spell, FLAG_MONSTER)) - { - CLEAR_FLAG (head, FLAG_SLEEP); - remove_friendly_object (head); - done_one = 1; - head->enemy = op; - } + // players can only affect spaces that they can actually see + if (caster + && caster->contr + && caster->contr->darkness_at (m, nx, ny) == LOS_BLOCKED) + continue; - /* calm monsters */ - if (QUERY_FLAG (spell, FLAG_UNAGGRESSIVE) && !QUERY_FLAG (head, FLAG_UNAGGRESSIVE)) - { - SET_FLAG (head, FLAG_UNAGGRESSIVE); - head->enemy = NULL; - done_one = 1; - } + for (tmp = ms.top; tmp; tmp = tmp->below) + if (tmp->flag [FLAG_MONSTER]) + break; - /* berserk monsters */ - if (QUERY_FLAG (spell, FLAG_BERSERK) && !QUERY_FLAG (head, FLAG_BERSERK)) - { - SET_FLAG (head, FLAG_BERSERK); - done_one = 1; - } + /* There can be living objects that are not monsters */ + if (!tmp) + continue; - /* charm */ - if (QUERY_FLAG (spell, FLAG_NO_ATTACK) && !QUERY_FLAG (head, FLAG_FRIENDLY)) - { - /* Prevent uncontolled outbreaks of self replicating monsters. - Typical use case is charm, go somwhere, use aggravation to make hostile. - This could lead to fun stuff like mice outbreak in bigworld and server crawl. */ - CLEAR_FLAG (head, FLAG_GENERATOR); - head->set_owner (op); - set_spell_skill (op, caster, spell, head); - add_friendly_object (head); - head->attack_movement = PETMOVE; - done_one = 1; - change_exp (op, head->stats.exp / 2, head->skill, SK_EXP_ADD_SKILL); - head->stats.exp = 0; - } + /* Only the head has meaningful data, so resolve to that */ + head = tmp->head_ (); + + /* Make sure the race is OK. Likewise, only effect undead if spell specifically allows it */ + if (race && head->race && !strstr (race, head->race)) + continue; + + if (head->flag [FLAG_UNDEAD] && !spell->flag [FLAG_UNDEAD]) + continue; + + /* Now do a bunch of stuff related to saving throws */ + best_at = -1; + if (spell->attacktype) + { + for (at = 0; at < NROFATTACKS; at++) + if (spell->attacktype & (1 << at)) + if (best_at == -1 || head->resist[at] > head->resist[best_at]) + best_at = at; + + if (best_at == -1) + at = 0; + else + { + if (head->resist[best_at] == 100) + continue; + else + at = head->resist[best_at] / 5; + } + + at -= level / 5; + if (did_make_save (head, head->level, at)) + continue; + } + else /* spell->attacktype */ + { + /* + Spell has no attacktype (charm & such), so we'll have a specific saving: + * if spell level < monster level, no go + * else, chance of effect = 20 + min( 50, 2 * ( spell level - monster level ) ) + + The chance will then be in the range [20-70] percent, not too bad. + + This is required to fix the 'charm monster' abuse, where a player level 1 can + charm a level 125 monster... + + Ryo, august 14th + */ + if (head->level > level) + continue; + + if (random_roll (0, 100, caster, PREFER_LOW) >= (20 + min (50, 2 * (level - head->level)))) + /* Failed, no effect */ + continue; + } + + /* Done with saving throw. Now start affecting the monster */ + done_one = 0; + + /* aggravation */ + if (spell->flag [FLAG_MONSTER]) + { + head->clr_flag (FLAG_SLEEP); + remove_friendly_object (head); + done_one = 1; + head->enemy = op; + } + + /* calm monsters */ + if (spell->flag [FLAG_UNAGGRESSIVE] && !head->flag [FLAG_UNAGGRESSIVE]) + { + head->set_flag (FLAG_UNAGGRESSIVE); + head->enemy = NULL; + done_one = 1; + } - /* If a monster was effected, put an effect in */ - if (done_one && spell->other_arch) - m->insert (arch_to_object (spell->other_arch), nx, ny, op); - } /* for y */ + /* berserk monsters */ + if (spell->flag [FLAG_BERSERK] && !head->flag [FLAG_BERSERK]) + { + head->set_flag (FLAG_BERSERK); + done_one = 1; + } + + /* charm */ + if (spell->flag [FLAG_NO_ATTACK] && !head->flag [FLAG_FRIENDLY]) + { + INVOKE_OBJECT (KILL, head, ARG_OBJECT (caster)); + + /* Prevent uncontrolled outbreaks of self replicating monsters. + Typical use case is charm, go somwhere, use aggravation to make hostile. + This could lead to fun stuff like mice outbreak in bigworld and server crawl. */ + head->clr_flag (FLAG_GENERATOR); + head->set_owner (op); + set_spell_skill (op, caster, spell, head); + add_friendly_object (head); + head->attack_movement = PETMOVE; + done_one = 1; + change_exp (op, head->stats.exp / 2, head->skill, SK_EXP_ADD_SKILL); + head->stats.exp = 0; + } + + /* If a monster was effected, put an effect in */ + if (done_one && spell->other_arch) + m->insert (spell->other_arch->instance (), nx, ny, op); + } return 1; } - /* Move_ball_spell: This handles ball type spells that just sort of wander * about. was called move_ball_lightning, but since more than the ball * lightning spell used it, that seemed misnamed. * op is the spell effect. * note that duration is handled by process_object() in time.c */ - void move_ball_spell (object *op) { @@ -1658,7 +1622,6 @@ /* i bit 0: alters sign of offset * other bits (i / 2): absolute value of offset */ - int offset = ((i ^ j) & 1) ? (i / 2) : -(i / 2); int tmpdir = absdir (op->direction + offset); @@ -1670,6 +1633,7 @@ break; } } + if (dir == 0) { nx = op->x; @@ -1705,13 +1669,13 @@ { if (j) op->stats.dam = dam_save / 2; - hit_map (op, j, op->attacktype, 1); + hit_map (op, j, op->attacktype, 1); } /* insert the other arch */ if (op->other_arch && !(OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, hx, hy)))) - m->insert (arch_to_object (op->other_arch), hx, hy, op); + m->insert (op->other_arch->instance (), hx, hy, op); } /* restore to the center location and damage */ @@ -1730,15 +1694,13 @@ } } - -/* move_swarm_spell: peterm +/* move_swarm_spell: peterm * This is an implementation of the swarm spell. It was written for - * meteor swarm, but it could be used for any swarm. A swarm spell + * meteor swarm, but it could be used for any swarm. A swarm spell * is a special type of object that casts swarms of other types - * of spells. Which spell it casts is flexible. It fires the spells + * of spells. Which spell it casts is flexible. It fires the spells * from a set of squares surrounding the caster, in a given direction. */ - void move_swarm_spell (object *op) { @@ -1749,23 +1711,29 @@ int adjustdir; maptile *m; #endif - int basedir; - object *owner; + object *owner = op->env; - owner = op->owner; - if (op->duration == 0 || owner == NULL) + if (!owner) // MUST not happen, remove when true TODO { + LOG (llevError, "swarm spell found outside inventory: %s\n", op->debug_desc ()); op->destroy (); return; } + if (!op->duration || !owner->is_on_map ()) + { + op->drop_and_destroy (); + return; + } + op->duration--; - basedir = op->direction; - if (basedir == 0) + int basedir = op->direction; + if (!basedir) { /* spray in all directions! 8) */ - basedir = rndm (1, 8); + op->facing = (op->facing + op->state) & 7; + basedir = op->facing + 1; } #if 0 @@ -1836,13 +1804,10 @@ if (op->spell->subtype == SP_BULLET) fire_bullet (owner, op, basedir, op->spell); else if (op->spell->subtype == SP_MAGIC_MISSILE) - fire_arch_from_position (owner, op, op->x, op->y, basedir, op->spell); + fire_arch_from_position (owner, op, owner->x, owner->y, basedir, op->spell); } } - - - /* fire_swarm: * The following routine creates a swarm of objects. It actually * sets up a specific swarm object, which then fires off all @@ -1854,23 +1819,17 @@ * spell - the spell that is this spell. * n: the number to be fired. */ - int fire_swarm (object *op, object *caster, object *spell, int dir) { - object *tmp; - int i; - if (!spell->other_arch) return 0; - tmp = get_archetype (SWARM_SPELL); - tmp->set_owner (op); /* needed so that if swarm elements kill, caster gets xp. */ - set_spell_skill (op, caster, spell, tmp); - - tmp->level = caster_level (caster, spell); /*needed later, to get level dep. right. */ - tmp->spell = arch_to_object (spell->other_arch); + object *tmp = archetype::get (SWARM_SPELL); + set_spell_skill (op, caster, spell, tmp); + tmp->level = casting_level (caster, spell); /* needed later, to get level dep. right. */ + tmp->spell = spell->other_arch->instance (); tmp->attacktype = tmp->spell->attacktype; if (tmp->attacktype & AT_HOLYWORD || tmp->attacktype & AT_GODPOWER) @@ -1878,17 +1837,20 @@ return 1; tmp->duration = SP_level_duration_adjust (caster, spell); - for (i = 0; i < spell->duration; i++) + for (int i = 0; i < spell->duration; i++) tmp->duration += die_roll (1, 3, op, PREFER_HIGH); - tmp->direction = dir; tmp->invisible = 1; + tmp->flag [FLAG_NO_DROP] = 1; // make sure it stays in inv, or else + tmp->direction = dir; + tmp->facing = rndm (1, 8); // initial firing direction + tmp->state = rndm (4) * 2 + 1; // direction increment + + op->insert (tmp); - tmp->insert_at (op, op); return 1; } - /* See the spells documentation file for why this is its own * function. */ @@ -1902,73 +1864,71 @@ dam = spell->stats.dam + SP_level_dam_adjust (caster, spell); - if (!dir) + if (dir) { - new_draw_info (NDI_UNIQUE, 0, op, "In what direction?"); - return 0; - } + x = op->x + freearr_x[dir]; + y = op->y + freearr_y[dir]; + m = op->map; - x = op->x + freearr_x[dir]; - y = op->y + freearr_y[dir]; - m = op->map; + mflags = get_map_flags (m, &m, x, y, &x, &y); - mflags = get_map_flags (m, &m, x, y, &x, &y); + if (mflags & P_OUT_OF_MAP) + { + op->failmsg ("Nothing is there."); + return 0; + } - if (mflags & P_OUT_OF_MAP) - { - new_draw_info (NDI_UNIQUE, 0, op, "Nothing is there."); - return 0; - } + if (mflags & P_IS_ALIVE && spell->attacktype) + { + for (target = GET_MAP_OB (m, x, y); target; target = target->above) + if (target->flag [FLAG_MONSTER]) + { + /* oky doky. got a target monster. Lets make a blinding attack */ + if (target->head) + target = target->head; - if (mflags & P_IS_ALIVE && spell->attacktype) - { - for (target = GET_MAP_OB (m, x, y); target; target = target->above) - if (QUERY_FLAG (target, FLAG_MONSTER)) - { - /* oky doky. got a target monster. Lets make a blinding attack */ - if (target->head) - target = target->head; - (void) hit_player (target, dam, op, spell->attacktype, 1); - return 1; /* one success only! */ - } - } + hit_player (target, dam, op, spell->attacktype, 1); + return 1; /* one success only! */ + } + } - /* no live target, perhaps a wall is in the way? */ - if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y))) - { - new_draw_info (NDI_UNIQUE, 0, op, "Something is in the way."); - return 0; + /* no live target, perhaps a wall is in the way? */ + if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y))) + { + op->failmsg ("Something is in the way."); + return 0; + } } /* ok, looks groovy to just insert a new light on the map */ - tmp = arch_to_object (spell->other_arch); + tmp = spell->other_arch->instance (); if (!tmp) { LOG (llevError, "Error: spell arch for cast_light() missing.\n"); return 0; } + tmp->stats.food = spell->duration + SP_level_duration_adjust (caster, spell); + if (tmp->glow_radius) - { - tmp->glow_radius = spell->range + SP_level_range_adjust (caster, spell); - if (tmp->glow_radius > MAX_LIGHT_RADII) - tmp->glow_radius = MAX_LIGHT_RADII; - } + tmp->set_glow_radius ( + clamp (spell->range + SP_level_range_adjust (caster, spell), 1, MAX_LIGHT_RADIUS) + ); + + if (dir) + m->insert (tmp, x, y, op); + else + caster->outer_env_or_self ()->insert (tmp); - m->insert (tmp, x, y, op); return 1; } - - - /* cast_cause_disease: this spell looks along from the * player and infects someone. * op is the player/monster, caster is the object, dir is the direction * to cast, disease_arch is the specific disease, and type is the spell number * perhaps this should actually be in disease.c? */ - int cast_cause_disease (object *op, object *caster, object *spell, int dir) { @@ -1985,6 +1945,7 @@ */ if (!dir) dir = op->facing; + if (!dir) return 0; /* won't find anything if casting on ourself, so just return */ @@ -2014,69 +1975,42 @@ { /* search this square for a victim */ for (walk = GET_MAP_OB (m, x, y); walk; walk = walk->above) - if (QUERY_FLAG (walk, FLAG_MONSTER) || (walk->type == PLAYER)) + if (walk->flag [FLAG_MONSTER] || (walk->type == PLAYER)) { /* found a victim */ - object *disease = arch_to_object (spell->other_arch); + object *disease = spell->other_arch->instance (); disease->set_owner (op); set_spell_skill (op, caster, spell, disease); disease->stats.exp = 0; - disease->level = caster_level (caster, spell); + disease->level = casting_level (caster, spell); /* do level adjustments */ - if (disease->stats.wc) - disease->stats.wc += dur_mod / 2; - - if (disease->magic > 0) - disease->magic += dur_mod / 4; - - if (disease->stats.maxhp > 0) - disease->stats.maxhp += dur_mod; - - if (disease->stats.maxgrace > 0) - disease->stats.maxgrace += dur_mod; - - if (disease->stats.dam) - { - if (disease->stats.dam > 0) - disease->stats.dam += dam_mod; - else - disease->stats.dam -= dam_mod; - } + if (disease->stats.wc ) disease->stats.wc += dur_mod / 2; + if (disease->magic > 0) disease->magic += dur_mod / 8; + if (disease->stats.maxhp > 0) disease->stats.maxhp += dur_mod; + if (disease->stats.maxgrace > 0) disease->stats.maxgrace += dur_mod; if (disease->last_sp) { disease->last_sp -= 2 * dam_mod; + if (disease->last_sp < 1) disease->last_sp = 1; } - if (disease->stats.maxsp) - { - if (disease->stats.maxsp > 0) - disease->stats.maxsp += dam_mod; - else - disease->stats.maxsp -= dam_mod; - } - - if (disease->stats.ac) - disease->stats.ac += dam_mod; - - if (disease->last_eat) - disease->last_eat -= dam_mod; - - if (disease->stats.hp) - disease->stats.hp -= dam_mod; - - if (disease->stats.sp) - disease->stats.sp -= dam_mod; + if (disease->stats.dam ) disease->stats.dam += copysign (disease->stats.dam , dam_mod); + if (disease->stats.maxsp) disease->stats.maxsp += copysign (disease->stats.maxsp, dam_mod); + if (disease->stats.ac ) disease->stats.ac += dam_mod; + if (disease->last_eat ) disease->last_eat -= dam_mod; + if (disease->stats.hp ) disease->stats.hp -= dam_mod; + if (disease->stats.sp ) disease->stats.sp -= dam_mod; if (infect_object (walk, disease, 1)) { - new_draw_info_format (NDI_UNIQUE, 0, op, "You inflict %s on %s!", &disease->name, &walk->name); + op->statusmsg (format ("You inflict %s on %s!", &disease->name, &walk->name)); disease->destroy (); /* don't need this one anymore */ - walk->map->insert (get_archetype ("detect_magic"), x, y, op); + walk->map->insert (archetype::get (shstr_detect_magic), x, y, op); return 1; }