1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992 Frank Tore Johansen |
7 |
* |
8 |
* Deliantra is free software: you can redistribute it and/or modify it under |
9 |
* the terms of the Affero GNU General Public License as published by the |
10 |
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
* option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the Affero GNU General Public License |
19 |
* and the GNU General Public License along with this program. If not, see |
20 |
* <http://www.gnu.org/licenses/>. |
21 |
* |
22 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
*/ |
24 |
|
25 |
/* This file deals with range related commands (casting, shooting, |
26 |
* throwing, etc. |
27 |
*/ |
28 |
|
29 |
#include <global.h> |
30 |
#include <sproto.h> |
31 |
#include <spells.h> |
32 |
#include <skills.h> |
33 |
#include <commands.h> |
34 |
|
35 |
/* Shows all spells that op knows. If params is supplied, the must match |
36 |
* that. Given there is more than one skill, we can't supply break |
37 |
* them down to cleric/wizardry. |
38 |
*/ |
39 |
static void |
40 |
show_matching_spells (object *op, char *params) |
41 |
{ |
42 |
object *spell; |
43 |
char spell_sort[NROFREALSPELLS][MAX_BUF], tmp[MAX_BUF], *cp; |
44 |
int num_found = 0, i; |
45 |
|
46 |
/* We go and see what spells the player has. We put them |
47 |
* into the spell_sort array so that we can sort them - |
48 |
* we prefix the skill in the name so that the sorting |
49 |
* works better. |
50 |
*/ |
51 |
for (spell = op->inv; spell; spell = spell->below) |
52 |
{ |
53 |
/* If it is a spell, and no params are passed, or they |
54 |
* match the name, process this spell. |
55 |
*/ |
56 |
if (spell->type == SPELL && (!params || !strncmp (params, spell->name, strlen (params)))) |
57 |
{ |
58 |
if (spell->path_attuned & op->path_denied) |
59 |
{ |
60 |
sprintf (spell_sort[num_found++], "%s:%-22s %3s %3s", spell->skill ? &spell->skill : "generic", &spell->name, "den", "den"); |
61 |
} |
62 |
else |
63 |
{ |
64 |
sprintf (spell_sort[num_found++], |
65 |
"%s:%-22s %3d %3d", spell->skill ? &spell->skill : "generic", |
66 |
&spell->name, spell->level, SP_level_spellpoint_cost (op, spell, SPELL_HIGHEST)); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
if (!num_found) |
72 |
{ |
73 |
/* If a matching string was passed along, now try it without that |
74 |
* string. It is odd to do something like 'cast trans', |
75 |
* and it say you have no spells, when really, you do, but just |
76 |
* nothing that matches. |
77 |
*/ |
78 |
if (params) |
79 |
show_matching_spells (op, NULL); |
80 |
else |
81 |
new_draw_info (NDI_UNIQUE, 0, op, "You know no spells"); |
82 |
} |
83 |
else |
84 |
{ |
85 |
/* Note in the code below that we make some |
86 |
* presumptions that there will be a colon in the |
87 |
* string. given the code above, this is always |
88 |
* the case. |
89 |
*/ |
90 |
qsort (spell_sort, num_found, MAX_BUF, (int (*)(const void *, const void *)) std::strcmp); |
91 |
strcpy (tmp, "asdfg"); /* Dummy string so initial compare fails */ |
92 |
for (i = 0; i < num_found; i++) |
93 |
{ |
94 |
/* Different skill name, so print banner */ |
95 |
if (strncmp (tmp, spell_sort[i], strlen (tmp))) |
96 |
{ |
97 |
strcpy (tmp, spell_sort[i]); |
98 |
cp = strchr (tmp, ':'); |
99 |
*cp = '\0'; |
100 |
new_draw_info (NDI_UNIQUE, 0, op, ""); |
101 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s spells %.*s [lvl] [sp]", tmp, 12 - strlen (tmp), " "); |
102 |
} |
103 |
new_draw_info (NDI_UNIQUE, 0, op, strchr (spell_sort[i], ':') + 1); |
104 |
} |
105 |
} |
106 |
} |
107 |
|
108 |
/* sets up to cast a spell. op is the caster, params is the spell name, |
109 |
* and command is the first letter of the spell type (c=cast, i=invoke, |
110 |
* p=prepare). Invoke casts a spell immediately, where as cast (and I believe |
111 |
* prepare) just set up the range type. |
112 |
*/ |
113 |
static int |
114 |
command_cast_spell (object *op, char *params, char command) |
115 |
{ |
116 |
int castnow = 0; |
117 |
char *cp; |
118 |
object *spob; |
119 |
|
120 |
if (command == 'i') |
121 |
castnow = 1; |
122 |
|
123 |
/* Remove control of the golem */ |
124 |
if (object *golem = op->contr->golem) |
125 |
golem->drop_and_destroy (); |
126 |
|
127 |
if (params) |
128 |
{ |
129 |
int spellnumber = 0; |
130 |
|
131 |
if ((spellnumber = atoi (params))) |
132 |
for (spob = op->inv; spob && spob->count != spellnumber; spob = spob->below) |
133 |
/* nop */; |
134 |
else |
135 |
spob = lookup_spell_by_name (op, params); |
136 |
|
137 |
if (spob && spob->type == SPELL) |
138 |
{ |
139 |
/* Now grab any extra data, if there is any. Forward pass |
140 |
* any 'of' delimiter |
141 |
*/ |
142 |
if (spellnumber) |
143 |
{ |
144 |
/* if we passed a number, the options start at the second word */ |
145 |
cp = strchr (params, ' '); |
146 |
if (cp) |
147 |
{ |
148 |
cp++; |
149 |
if (!strncmp (cp, "of ", 3)) |
150 |
cp += 3; |
151 |
} |
152 |
} |
153 |
else if (strlen (params) > strlen (spob->name)) |
154 |
{ |
155 |
cp = params + strlen (spob->name); |
156 |
*cp = 0; |
157 |
cp++; |
158 |
if (!strncmp (cp, "of ", 3)) |
159 |
cp += 3; |
160 |
} |
161 |
else |
162 |
cp = NULL; |
163 |
|
164 |
if (!spob->skill) |
165 |
{ |
166 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s is a weird spell, please report it to the dungeon master!", &spob->name); |
167 |
LOG (llevError, "spell without skill found: %s", spob->debug_desc ()); |
168 |
return 1; |
169 |
} |
170 |
|
171 |
object *skill = find_skill_by_name (op, spob->skill); |
172 |
|
173 |
if (!skill) |
174 |
{ |
175 |
op->failmsg (format ("You need the skill %s to cast %s!", &spob->skill, &spob->name)); |
176 |
return 1; |
177 |
} |
178 |
|
179 |
splay (spob); |
180 |
|
181 |
if (castnow) |
182 |
cast_spell (op, op, op->facing, spob, cp); |
183 |
else |
184 |
{ |
185 |
if (op->contr->ranged_ob) |
186 |
if (op->contr->ranged_ob->flag [FLAG_APPLIED]) |
187 |
apply_special (op, op->contr->ranged_ob, AP_UNAPPLY); |
188 |
else |
189 |
op->contr->ranged_ob = 0; |
190 |
|
191 |
if (op->contr->ranged_ob) |
192 |
op->failmsg (format ("You have to unapply the %s first to ready a spell.", &op->contr->ranged_ob->name)); |
193 |
else |
194 |
{ |
195 |
op->change_weapon (op->contr->ranged_ob = spob); |
196 |
|
197 |
assign (op->contr->spellparam, cp ? cp : ""); |
198 |
op->statusmsg (format ("You ready the spell %s", &spob->name)); |
199 |
} |
200 |
} |
201 |
|
202 |
return 0; |
203 |
} /* else fall through to below and print spells */ |
204 |
} /* params supplied */ |
205 |
|
206 |
/* We get here if cast was given without options or we could not find |
207 |
* the requested spell. List all the spells the player knows. |
208 |
*/ |
209 |
new_draw_info (NDI_UNIQUE, 0, op, "Cast what spell? Choose one of:"); |
210 |
show_matching_spells (op, params); |
211 |
|
212 |
return 1; |
213 |
} |
214 |
|
215 |
int |
216 |
command_invoke (object *op, char *params) |
217 |
{ |
218 |
return command_cast_spell (op, params, 'i'); |
219 |
} |
220 |
|
221 |
int |
222 |
command_cast (object *op, char *params) |
223 |
{ |
224 |
return command_cast_spell (op, params, 'c'); |
225 |
} |
226 |
|
227 |
int |
228 |
command_prepare (object *op, char *params) |
229 |
{ |
230 |
return command_cast_spell (op, params, 'p'); |
231 |
} |
232 |
|
233 |
/**************************************************************************/ |
234 |
|
235 |
static void |
236 |
change_spell (object *op, char k) |
237 |
{ |
238 |
if (op->contr->combat_ob == op->current_weapon) |
239 |
{ |
240 |
if (op->contr->ranged_ob) |
241 |
op->change_weapon (op->contr->ranged_ob); |
242 |
} |
243 |
else if (op->contr->ranged_ob == op->current_weapon) |
244 |
{ |
245 |
if (op->contr->combat_ob) |
246 |
op->change_weapon (op->contr->combat_ob); |
247 |
} |
248 |
|
249 |
//TODO: maybe switch to golem, if any? |
250 |
} |
251 |
|
252 |
int |
253 |
command_rotateshoottype (object *op, char *params) |
254 |
{ |
255 |
if (!params) |
256 |
change_spell (op, '+'); |
257 |
else |
258 |
change_spell (op, params[0]); |
259 |
|
260 |
return 0; |
261 |
} |