1 |
/* |
2 |
* CrossFire, A Multiplayer game |
3 |
* |
4 |
* Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 |
* Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (C) 1992 Frank Tore Johansen |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
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 |
11 |
* (at your 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 GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
* |
22 |
* The authors can be reached via e-mail at <crossfire@schmorp.de> |
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 |
int |
36 |
command_invoke (object *op, char *params) |
37 |
{ |
38 |
return command_cast_spell (op, params, 'i'); |
39 |
} |
40 |
|
41 |
int |
42 |
command_cast (object *op, char *params) |
43 |
{ |
44 |
return command_cast_spell (op, params, 'c'); |
45 |
} |
46 |
|
47 |
int |
48 |
command_prepare (object *op, char *params) |
49 |
{ |
50 |
return command_cast_spell (op, params, 'p'); |
51 |
} |
52 |
|
53 |
/* Shows all spells that op knows. If params is supplied, the must match |
54 |
* that. Given there is more than one skill, we can't supply break |
55 |
* them down to cleric/wizardry. |
56 |
*/ |
57 |
static void |
58 |
show_matching_spells (object *op, char *params) |
59 |
{ |
60 |
object *spell; |
61 |
char spell_sort[NROFREALSPELLS][MAX_BUF], tmp[MAX_BUF], *cp; |
62 |
int num_found = 0, i; |
63 |
|
64 |
/* We go and see what spells the player has. We put them |
65 |
* into the spell_sort array so that we can sort them - |
66 |
* we prefix the skill in the name so that the sorting |
67 |
* works better. |
68 |
*/ |
69 |
for (spell = op->inv; spell; spell = spell->below) |
70 |
{ |
71 |
/* If it is a spell, and no params are passed, or they |
72 |
* match the name, process this spell. |
73 |
*/ |
74 |
if (spell->type == SPELL && (!params || !strncmp (params, spell->name, strlen (params)))) |
75 |
{ |
76 |
if (spell->path_attuned & op->path_denied) |
77 |
{ |
78 |
sprintf (spell_sort[num_found++], "%s:%-22s %3s %3s", spell->skill ? &spell->skill : "generic", &spell->name, "den", "den"); |
79 |
} |
80 |
else |
81 |
{ |
82 |
sprintf (spell_sort[num_found++], |
83 |
"%s:%-22s %3d %3d", spell->skill ? &spell->skill : "generic", |
84 |
&spell->name, spell->level, SP_level_spellpoint_cost (op, spell, SPELL_HIGHEST)); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
if (!num_found) |
90 |
{ |
91 |
/* If a matching string was passed along, now try it without that |
92 |
* string. It is odd to do something like 'cast trans', |
93 |
* and it say you have no spells, when really, you do, but just |
94 |
* nothing that matches. |
95 |
*/ |
96 |
if (params) |
97 |
show_matching_spells (op, NULL); |
98 |
else |
99 |
new_draw_info (NDI_UNIQUE, 0, op, "You know no spells"); |
100 |
} |
101 |
else |
102 |
{ |
103 |
/* Note in the code below that we make some |
104 |
* presumptions that there will be a colon in the |
105 |
* string. given the code above, this is always |
106 |
* the case. |
107 |
*/ |
108 |
qsort (spell_sort, num_found, MAX_BUF, (int (*)(const void *, const void *)) std::strcmp); |
109 |
strcpy (tmp, "asdfg"); /* Dummy string so initial compare fails */ |
110 |
for (i = 0; i < num_found; i++) |
111 |
{ |
112 |
/* Different skill name, so print banner */ |
113 |
if (strncmp (tmp, spell_sort[i], strlen (tmp))) |
114 |
{ |
115 |
strcpy (tmp, spell_sort[i]); |
116 |
cp = strchr (tmp, ':'); |
117 |
*cp = '\0'; |
118 |
new_draw_info (NDI_UNIQUE, 0, op, ""); |
119 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s spells %.*s [lvl] [sp]", tmp, 12 - strlen (tmp), " "); |
120 |
} |
121 |
new_draw_info (NDI_UNIQUE, 0, op, strchr (spell_sort[i], ':') + 1); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
/* sets up to cast a spell. op is the caster, params is the spell name, |
127 |
* and command is the first letter of the spell type (c=cast, i=invoke, |
128 |
* p=prepare). Invoke casts a spell immediately, where as cast (and I believe |
129 |
* prepare) just set up the range type. |
130 |
*/ |
131 |
int |
132 |
command_cast_spell (object *op, char *params, char command) |
133 |
{ |
134 |
int castnow = 0; |
135 |
char *cp; |
136 |
object *spob; |
137 |
|
138 |
if (command == 'i') |
139 |
castnow = 1; |
140 |
|
141 |
/* Remove control of the golem */ |
142 |
if (object *golem = op->contr->ranges[range_golem]) |
143 |
golem->destroy (); |
144 |
|
145 |
if (params) |
146 |
{ |
147 |
int spellnumber = 0; |
148 |
|
149 |
if ((spellnumber = atoi (params))) |
150 |
for (spob = op->inv; spob && spob->count != spellnumber; spob = spob->below) |
151 |
/* nop */; |
152 |
else |
153 |
spob = lookup_spell_by_name (op, params); |
154 |
|
155 |
if (spob && spob->type == SPELL) |
156 |
{ |
157 |
/* Now grab any extra data, if there is any. Forward pass |
158 |
* any 'of' delimiter |
159 |
*/ |
160 |
if (spellnumber) |
161 |
{ |
162 |
/* if we passed a number, the options start at the second word */ |
163 |
cp = strchr (params, ' '); |
164 |
if (cp) |
165 |
{ |
166 |
cp++; |
167 |
if (!strncmp (cp, "of ", 3)) |
168 |
cp += 3; |
169 |
} |
170 |
} |
171 |
else if (strlen (params) > (size_t) strlen (spob->name)) |
172 |
{ |
173 |
cp = params + strlen (spob->name); |
174 |
*cp = 0; |
175 |
cp++; |
176 |
if (!strncmp (cp, "of ", 3)) |
177 |
cp += 3; |
178 |
} |
179 |
else |
180 |
cp = NULL; |
181 |
|
182 |
if (spob->skill && !find_skill_by_name (op, spob->skill)) |
183 |
{ |
184 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You need the skill %s to cast %s!", &spob->skill, &spob->name); |
185 |
return 1; |
186 |
} |
187 |
|
188 |
if (castnow) |
189 |
cast_spell (op, op, op->facing, spob, cp); |
190 |
else |
191 |
{ |
192 |
op->contr->ranges[range_magic] = spob; |
193 |
op->contr->shoottype = range_magic; |
194 |
assign (op->contr->spellparam, cp ? cp : ""); |
195 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You ready the spell %s", &spob->name); |
196 |
} |
197 |
|
198 |
return 0; |
199 |
} /* else fall through to below and print spells */ |
200 |
} /* params supplied */ |
201 |
|
202 |
/* We get here if cast was given without options or we could not find |
203 |
* the requested spell. List all the spells the player knows. |
204 |
*/ |
205 |
new_draw_info (NDI_UNIQUE, 0, op, "Cast what spell? Choose one of:"); |
206 |
show_matching_spells (op, params); |
207 |
return 1; |
208 |
} |
209 |
|
210 |
/**************************************************************************/ |
211 |
|
212 |
void |
213 |
change_spell (object *op, char k) |
214 |
{ |
215 |
int r = op->contr->shoottype; |
216 |
|
217 |
do |
218 |
r = (r + ((k == '+') ? 1 : -1) + range_size) % range_size; |
219 |
while (!op->contr->legal_range ((rangetype) r)); |
220 |
|
221 |
op->contr->set_range ((rangetype) r); |
222 |
|
223 |
/* Legal range has already checked that we have an appropriate item |
224 |
* that uses the slot, so we don't need to be too careful about |
225 |
* checking the status of the object. |
226 |
*/ |
227 |
switch (op->contr->shoottype) |
228 |
{ |
229 |
case range_none: |
230 |
new_draw_info (NDI_UNIQUE, 0, op, "No ranged attack chosen."); |
231 |
break; |
232 |
|
233 |
case range_golem: |
234 |
new_draw_info (NDI_UNIQUE, 0, op, "You regain control of your golem."); |
235 |
break; |
236 |
|
237 |
case range_bow: |
238 |
new_draw_info_format (NDI_UNIQUE, 0, op, "Switched to %s and %s.", query_name (op->contr->ranges[range_bow]), |
239 |
op->contr->ranges[range_bow]->race ? &op->contr->ranges[range_bow]->race : "nothing"); |
240 |
break; |
241 |
|
242 |
case range_magic: |
243 |
new_draw_info_format (NDI_UNIQUE, 0, op, "Switched to spells (%s).", &op->contr->ranges[range_magic]->name); |
244 |
break; |
245 |
|
246 |
case range_misc: |
247 |
new_draw_info_format (NDI_UNIQUE, 0, op, "Switched to %s.", query_base_name (op->contr->ranges[range_misc], 0)); |
248 |
break; |
249 |
|
250 |
case range_skill: |
251 |
new_draw_info_format (NDI_UNIQUE, 0, op, "Switched to skill: %s", &op->chosen_skill->name); |
252 |
break; |
253 |
} |
254 |
} |
255 |
|
256 |
int |
257 |
command_rotateshoottype (object *op, char *params) |
258 |
{ |
259 |
if (!params) |
260 |
change_spell (op, '+'); |
261 |
else |
262 |
change_spell (op, params[0]); |
263 |
|
264 |
return 0; |
265 |
} |