/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002,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
*/
/* This file deals with range related commands (casting, shooting,
* throwing, etc.
*/
#include
#include
#include
#include
#include
int
command_invoke (object *op, char *params)
{
return command_cast_spell (op, params, 'i');
}
int
command_cast (object *op, char *params)
{
return command_cast_spell (op, params, 'c');
}
int
command_prepare (object *op, char *params)
{
return command_cast_spell (op, params, 'p');
}
/* Shows all spells that op knows. If params is supplied, the must match
* that. Given there is more than one skill, we can't supply break
* them down to cleric/wizardry.
*/
static void
show_matching_spells (object *op, char *params)
{
object *spell;
char spell_sort[NROFREALSPELLS][MAX_BUF], tmp[MAX_BUF], *cp;
int num_found = 0, i;
/* We go and see what spells the player has. We put them
* into the spell_sort array so that we can sort them -
* we prefix the skill in the name so that the sorting
* works better.
*/
for (spell = op->inv; spell; spell = spell->below)
{
/* If it is a spell, and no params are passed, or they
* match the name, process this spell.
*/
if (spell->type == SPELL && (!params || !strncmp (params, spell->name, strlen (params))))
{
if (spell->path_attuned & op->path_denied)
{
sprintf (spell_sort[num_found++], "%s:%-22s %3s %3s", spell->skill ? &spell->skill : "generic", &spell->name, "den", "den");
}
else
{
sprintf (spell_sort[num_found++],
"%s:%-22s %3d %3d", spell->skill ? &spell->skill : "generic",
&spell->name, spell->level, SP_level_spellpoint_cost (op, spell, SPELL_HIGHEST));
}
}
}
if (!num_found)
{
/* If a matching string was passed along, now try it without that
* string. It is odd to do something like 'cast trans',
* and it say you have no spells, when really, you do, but just
* nothing that matches.
*/
if (params)
show_matching_spells (op, NULL);
else
new_draw_info (NDI_UNIQUE, 0, op, "You know no spells");
}
else
{
/* Note in the code below that we make some
* presumptions that there will be a colon in the
* string. given the code above, this is always
* the case.
*/
qsort (spell_sort, num_found, MAX_BUF, (int (*)(const void *, const void *)) std::strcmp);
strcpy (tmp, "asdfg"); /* Dummy string so initial compare fails */
for (i = 0; i < num_found; i++)
{
/* Different skill name, so print banner */
if (strncmp (tmp, spell_sort[i], strlen (tmp)))
{
strcpy (tmp, spell_sort[i]);
cp = strchr (tmp, ':');
*cp = '\0';
new_draw_info (NDI_UNIQUE, 0, op, "");
new_draw_info_format (NDI_UNIQUE, 0, op, "%s spells %.*s [lvl] [sp]", tmp, 12 - strlen (tmp), " ");
}
new_draw_info (NDI_UNIQUE, 0, op, strchr (spell_sort[i], ':') + 1);
}
}
}
/* sets up to cast a spell. op is the caster, params is the spell name,
* and command is the first letter of the spell type (c=cast, i=invoke,
* p=prepare). Invoke casts a spell immediately, where as cast (and I believe
* prepare) just set up the range type.
*/
int
command_cast_spell (object *op, char *params, char command)
{
int castnow = 0;
char *cp;
object *spob;
if (command == 'i')
castnow = 1;
/* Remove control of the golem */
if (object *golem = op->contr->golem)
golem->destroy ();
if (params)
{
int spellnumber = 0;
if ((spellnumber = atoi (params)))
for (spob = op->inv; spob && spob->count != spellnumber; spob = spob->below)
/* nop */;
else
spob = lookup_spell_by_name (op, params);
if (spob && spob->type == SPELL)
{
/* Now grab any extra data, if there is any. Forward pass
* any 'of' delimiter
*/
if (spellnumber)
{
/* if we passed a number, the options start at the second word */
cp = strchr (params, ' ');
if (cp)
{
cp++;
if (!strncmp (cp, "of ", 3))
cp += 3;
}
}
else if (strlen (params) > strlen (spob->name))
{
cp = params + strlen (spob->name);
*cp = 0;
cp++;
if (!strncmp (cp, "of ", 3))
cp += 3;
}
else
cp = NULL;
if (!spob->skill)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "%s is a weird spell, please report it to the dungeon master!", &spob->name);
LOG (llevError, "spell without skill found: %s", spob->debug_desc ());
return 1;
}
object *skill = find_skill_by_name (op, spob->skill);
if (!skill)
{
op->failmsg (format ("You need the skill %s to cast %s!", &spob->skill, &spob->name));
return 1;
}
splay (spob);
if (castnow)
cast_spell (op, op, op->facing, spob, cp);
else
{
if (op->contr->ranged_ob)
if (op->contr->ranged_ob->flag [FLAG_APPLIED])
apply_special (op, op->contr->ranged_ob, AP_UNAPPLY);
else
op->contr->ranged_ob = 0;
if (op->contr->ranged_ob)
op->failmsg (format ("You have to unapply the %s first to ready a spell.", &op->contr->ranged_ob->name));
else
{
op->change_weapon (op->contr->ranged_ob = spob);
assign (op->contr->spellparam, cp ? cp : "");
op->statusmsg (format ("You ready the spell %s", &spob->name));
}
}
return 0;
} /* else fall through to below and print spells */
} /* params supplied */
/* We get here if cast was given without options or we could not find
* the requested spell. List all the spells the player knows.
*/
new_draw_info (NDI_UNIQUE, 0, op, "Cast what spell? Choose one of:");
show_matching_spells (op, params);
return 1;
}
/**************************************************************************/
void
change_spell (object *op, char k)
{
if (op->contr->combat_ob == op->current_weapon)
{
if (op->contr->ranged_ob)
op->change_weapon (op->contr->ranged_ob);
}
else if (op->contr->ranged_ob == op->current_weapon)
{
if (op->contr->combat_ob)
op->change_weapon (op->contr->combat_ob);
}
//TODO: maybe switch to golem, if any?
}
int
command_rotateshoottype (object *op, char *params)
{
if (!params)
change_spell (op, '+');
else
change_spell (op, params[0]);
return 0;
}