/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2003,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 Frank Tore Johansen
*
* Deliantra 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.
*
* 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 .
*
* The authors can be reached via e-mail to
*/
#include
#include
#include
#ifndef sqr
# define sqr(x) ((x)*(x))
#endif
/* peterm:
* write_rune:
* op: rune writer
* skop: skill object used for casting this rune
* dir: orientation of rune, direction rune's contained spell will
* be cast in, if applicable
* inspell: spell object to put into the rune, can be null if doing
* a marking rune.
* level: level of casting of the rune
* runename: name of the rune or message displayed by the rune for
* a rune of marking
*/
int
write_rune (object *op, object *caster, object *spell, int dir, const char *runename)
{
object *tmp, *rune_spell, *rune;
char buf[MAX_BUF];
maptile *m;
sint16 nx, ny;
if (!dir)
dir = 1;
nx = op->x + freearr_x[dir];
ny = op->y + freearr_y[dir];
m = op->map;
if (get_map_flags (m, &m, nx, ny, &nx, &ny) & (P_OUT_OF_MAP | P_SAFE | P_NO_MAGIC | P_NO_CLERIC))
{
new_draw_info (NDI_UNIQUE, 0, op, "Can't make a rune there!");
return 0;
}
for (tmp = m->at (nx, ny).bot; tmp; tmp = tmp->above)
if (tmp->type == RUNE)
break;
if (tmp)
{
new_draw_info (NDI_UNIQUE, 0, op, "You can't write a rune on top of another rune.");
return 0;
}
if (spell->other_arch)
rune_spell = arch_to_object (spell->other_arch);
else
{
/* Player specified spell. The player has to know the spell, so
* lets just look through the players inventory see if they know it
* use the item_matched_string for our typical matching method.
*/
int bestmatch = 0, ms;
if (!runename || *runename == 0)
{
new_draw_info (NDI_UNIQUE, 0, op, "Write a rune of what?");
return 0;
}
rune_spell = NULL;
for (tmp = op->inv; tmp; tmp = tmp->below)
if (tmp->type == SPELL)
{
ms = item_matched_string (op, tmp, runename);
if (ms > bestmatch)
{
bestmatch = ms;
rune_spell = tmp;
}
}
if (!rune_spell)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "You don't know any spell named %s", runename);
return 0;
}
if (rune_spell->skill != spell->skill)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "You can't cast %s with %s", &rune_spell->name, &spell->name);
return 0;
}
if (caster->path_denied & spell->path_attuned && !caster->flag [FLAG_WIZCAST])
{
new_draw_info_format (NDI_UNIQUE, 0, op, "%s belongs to a spell path denied to you.", &rune_spell->name);
return 0;
}
if (caster_level (caster, rune_spell) < rune_spell->level && !caster->flag [FLAG_WIZCAST])
{
new_draw_info_format (NDI_UNIQUE, 0, op, "%s is beyond your ability to cast!", &rune_spell->name);
return 0;
}
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA) > op->stats.sp)
{
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough mana.");
return 0;
}
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE) > op->stats.grace)
{
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough grace.");
return 0;
}
op->stats.grace -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE);
op->stats.sp -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA);
}
/* already proper rune. Note this should only be the case if other_arch was set */
if (rune_spell->type == RUNE)
rune = rune_spell;
else
{
rune = get_archetype (GENERIC_RUNE);
sprintf (buf, "You set off a rune of %s\n", &rune_spell->name);
rune->msg = buf;
rune->insert (rune_spell->clone ());
if (spell->face != blank_face)
rune->face = spell->face;
}
rune->level = caster_level (caster, spell);
rune->stats.Cha = rune->level / 2; /* the invisibility parameter */
rune->direction = dir; /* where any spell will go upon detonation */
rune->set_owner (op); /* runes without need no owner */
set_spell_skill (op, caster, spell, rune);
m->insert (rune, nx, ny, op);
return 1;
}
/* move_rune: peterm
comments on runes:
rune->level : level at which rune will cast its spell.
rune->hp : number of detonations before rune goes away
rune->msg : message the rune displays when it goes off
rune->direction : direction it will cast a spell in
rune->dam : damage the rune will do if it doesn't cast spells
rune->attacktype: type of damage it does, if not casting spells
rune->other_arch: spell in the rune
rune->Cha : how hidden the rune is
rune->maxhp : number of spells the rune casts
*/
void
move_rune (object *op)
{
/* runes of level zero cannot detonate. */
if (!op->level)
return;
int det = op->invisible;
if (!(rndm (0, MAX (1, (op->stats.Cha)) - 1)))
{
op->invisible = 0;
op->speed_left -= 1;
}
else
op->invisible = 1;
if (op->invisible != det)
update_object (op, UP_OBJ_CHANGE);
}
/* peterm: rune_attack
* function handles those runes which detonate but do not cast spells.
*/
void
rune_attack (object *op, object *victim)
{
if (victim)
{
hit_player (victim, op->stats.dam, op, op->attacktype, 1);
if (victim->destroyed ())
return;
/* if there's a disease in the needle, put it in the player */
if (op->has_random_items ())
create_treasure (op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0);
if (op->inv && op->inv->type == DISEASE)
{
object *disease = op->inv;
infect_object (victim, disease, 1);
disease->destroy ();
}
}
else
hit_map (op, 0, op->attacktype, 1);
}
/* This function generalizes attacks by runes/traps. This ought to make
* it possible for runes to attack from the inventory,
* it'll spring the trap on the victim.
*/
void
spring_trap (object *trap, object *victim)
{
object *env;
rv_vector rv;
int i;
/* Prevent recursion */
if (trap->stats.hp <= 0)
return;
if (QUERY_FLAG (trap, FLAG_IS_LINKED))
use_trigger (trap);
/* Only living objects can trigger runes that don't cast spells, as
* doing direct damage to a non-living object doesn't work anyway.
* Typical example is an arrow attacking a door.
*/
if (!QUERY_FLAG (victim, FLAG_ALIVE) && !trap->inv && !trap->other_arch)
return;
trap->stats.hp--; /*decrement detcount */
if (victim && victim->type == PLAYER)
new_draw_info (NDI_UNIQUE, 0, victim, trap->msg);
/* Flash an image of the trap on the map so the poor sod
* knows what hit him.
*/
env = trap->outer_env ();
/* If the victim is not next to this trap, don't set it off.
* players shouldn't get hit by firing arrows at a door for example.
* At the same time, the trap will stick around until detonated
*/
get_rangevector (env, victim, &rv, 0);
if (rv.distance > 1)
return;
env->play_sound (trap->sound ? trap->sound : sound_find ("trap_spring"));
trap_show (trap, env);
if (victim->type == PLAYER) // only count players as enemies
trap->enemy = victim; // set the victim as the traps enemy, so that summoned
// creatures know who to attack.
/* Only if it is a spell do we proceed here */
if ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->type == SPELL))
{
if (trap->destroyed ())
return;
// breaks summon golem spells, which, for inexplicable reasons,
// do not work like summon golem spells at all but still require
// direction "0" to work at all.
//if (trap->direction)
// rv.direction = trap->direction;
for (i = 0; i < MAX (1, trap->stats.maxhp); i++)
{
if (trap->inv)
cast_spell (env, trap, trap->direction, trap->inv, NULL);
else
{
object *spell = arch_to_object (trap->other_arch);
cast_spell (env, trap, trap->direction, spell, NULL);
spell->destroy ();
}
}
}
else
{
rune_attack (trap, victim);
if (trap->destroyed ())
return;
}
if (trap->stats.hp <= 0)
{
trap->type = SIGN; /* make the trap impotent */
trap->stats.food = 20; /* make it stick around until its spells are gone */
SET_FLAG (trap, FLAG_IS_USED_UP);
}
}
/* dispel_rune: by peterm
* dispels the target rune, depending on the level of the actor
* and the level of the rune risk flag, if true, means that there is
* a chance that the trap/rune will detonate
*/
int
dispel_rune (object *op, object *caster, object *spell, object *skill, int dir)
{
object *tmp, *tmp2;
int searchflag = 1, mflags;
sint16 x, y;
maptile *m;
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);
/* Should we perhaps not allow player to disable traps if a monster/
* player is standing on top?
*/
if (mflags & P_OUT_OF_MAP)
{
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!");
return 0;
}
for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
{
if (tmp->type == RUNE || tmp->type == TRAP)
break;
/* we could put a probability chance here, but since nothing happens
* if you fail, no point on that. I suppose we could do a level
* comparison so low level players can't erase high level players runes.
*/
if (tmp->type == SIGN && !strcmp (tmp->arch->archname, "rune_mark"))
{
tmp->destroy ();
new_draw_info (NDI_UNIQUE, 0, op, "You wipe out the rune of marking!");
return 1;
}
/* now search tmp's inventory for traps
* This is for chests, where the rune is in the chests inventory.
*/
for (tmp2 = tmp->inv; tmp2 != NULL; tmp2 = tmp2->below)
{
if (tmp2->type == RUNE || tmp2->type == TRAP)
{
tmp = tmp2;
searchflag = 0;
break;
}
}
if (!searchflag)
break;
}
/* no rune there. */
if (tmp == NULL)
{
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!");
return 0;
}
trap_disarm (op, tmp, 0, skill);
return 1;
}
int
trap_see (object *op, object *trap)
{
int chance;
chance = random_roll (0, 99, op, PREFER_HIGH);;
/* decide if we see the rune or not */
if ((trap->stats.Cha == 1) || (chance > MIN (95, MAX (5, ((int) ((float) (op->map->difficulty
+ trap->level + trap->stats.Cha - op->level) / 10.0 * 50.0))))))
{
new_draw_info_format (NDI_UNIQUE, 0, op, "You spot a %s!", &trap->name);
return 1;
}
return 0;
}
int
trap_show (object *trap, object *where)
{
if (where == NULL)
return 0;
object *tmp = get_archetype ("runedet");
tmp->face = GET_ANIMATION (trap, 0);
tmp->insert_at (where, 0);
return 1;
}
int
trap_disarm (object *disarmer, object *trap, int risk, object *skill)
{
int trapworth; /* need to compute the experience worth of the trap
before we kill it */
/* this formula awards a more reasonable amount of exp */
trapworth = MAX (1, trap->level) * disarmer->map->difficulty *
sqr (MAX (trap->stats.dam, trap->inv ? trap->inv->level : 1)) / skill->level;
if (!(random_roll (0, (MAX (2, MIN (20, trap->level - skill->level + 5 - disarmer->stats.Dex / 2)) - 1), disarmer, PREFER_LOW)))
{
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You successfully disarm the %s!", &trap->name);
trap->destroy (1);
/* If it is your own trap, (or any players trap), don't you don't
* get exp for it.
*/
if (trap->owner && trap->owner->type != PLAYER && risk)
return trapworth;
else
return 1; /* give minimal exp and say success */
}
else
{
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You fail to disarm the %s.", &trap->name);
if (!(random_roll (0, (MAX (2, skill->level - trap->level + disarmer->stats.Dex / 2 - 6)) - 1, disarmer, PREFER_LOW)) && risk)
{
new_draw_info (NDI_UNIQUE, 0, disarmer, "In fact, you set it off!");
spring_trap (trap, disarmer);
}
return 0;
}
}
/* traps need to be adjusted for the difficulty of the map. The
* default traps are too strong for wimpy level 1 players, and
* unthreatening to anyone of high level
*/
void
trap_adjust (object *trap, int difficulty)
{
int i;
/* now we set the trap level to match the difficulty of the level
* the formula below will give a level from 1 to (2*difficulty) with
* a peak probability at difficulty
*/
trap->level = rndm (0, difficulty - 1) + rndm (0, difficulty - 1);
if (trap->level < 1)
trap->level = 1;
/* set the hiddenness of the trap, similar formula to above */
trap->stats.Cha = rndm (0, 19) + rndm (0, difficulty - 1) + rndm (0, difficulty - 1);
if (!trap->other_arch && !trap->inv)
{
/* set the damage of the trap.
* we get 0-4 pts of damage per level of difficulty of the map in
* the trap
*/
trap->stats.dam = 0;
for (i = 0; i < difficulty; i++)
trap->stats.dam += rndm (0, 4);
/* the poison trap special case */
if (trap->attacktype & AT_POISON)
{
trap->stats.dam = rndm (0, difficulty - 1);
if (trap->stats.dam < 1)
trap->stats.dam = 1;
}
/* so we get an appropriate amnt of exp for AT_DEATH traps */
if (trap->attacktype & AT_DEATH)
trap->stats.dam = 127;
}
}