Debian Codegraph @

server/c_range.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Range related commands (casting, shooting, throwing, etc.).
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "commands.h"
25 #include "shared/newclient.h"
26 #include "skills.h"
27 #include "spells.h"
28 #include "sproto.h"
29
30 /**
31 * 'invoke' command, fires a spell immediately.
32 *
33 * @param op
34 * player.
35 * @param params
36 * spell.
37 */
38 void command_invoke(object *op, const char *params) {
39 command_cast_spell(op, params, 'i');
40 }
41
42 /**
43 * 'cast' command, prepares a spell for laster casting.
44 *
45 * @param op
46 * player.
47 * @param params
48 * spell.
49 */
50 void command_cast(object *op, const char *params) {
51 command_cast_spell(op, params, 'c');
52 }
53
54 /**
55 * Equivalent to command_cast().
56 *
57 * @param op
58 * player.
59 * @param params
60 * spell.
61 * @todo remove.
62 */
63 void command_prepare(object *op, const char *params) {
64 command_cast_spell(op, params, 'p');
65 }
66
67 /**
68 * Shows all spells that op knows.
69 *
70 * Given there is more than one skill, we can't supply break
71 * them down to cleric/wizardry.
72 *
73 * @param op
74 * player wanting to knows her spells.
75 * @param params
76 * if supplied, the spell name must match that.
77 */
78 static void show_matching_spells(object *op, const char *params) {
79 char spell_sort[NROFREALSPELLS][MAX_BUF], tmp[MAX_BUF], *cp;
80 int num_found = 0, i;
81
82 /* We go and see what spells the player has. We put them
83 * into the spell_sort array so that we can sort them -
84 * we prefix the skill in the name so that the sorting
85 * works better.
86 */
87 FOR_INV_PREPARE(op, spell) {
88 /* If it is a spell, and no params are passed, or they
89 * match the name, process this spell.
90 */
91 if (spell->type == SPELL
92 && (*params == '\0' || !strncmp(params, spell->name, strlen(params)))) {
93 if (spell->path_attuned&op->path_denied) {
94 snprintf(spell_sort[num_found++], sizeof(spell_sort[0]),
95 "%s:%-30s %3s %3s", spell->skill ? spell->skill : "generic",
96 spell->name, "den", "den");
97 } else {
98 snprintf(spell_sort[num_found++], sizeof(spell_sort[0]),
99 "%s:%-30s %3d %3d", spell->skill ? spell->skill : "generic",
100 spell->name, spell->level,
101 SP_level_spellpoint_cost(op, spell, SPELL_HIGHEST));
102 }
103 }
104 } FOR_INV_FINISH();
105 if (!num_found) {
106 if (*params != '\0')
107 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
108 "You know no spells like '%s'.", params);
109 else
110 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
111 "You know no spells.");
112
113 return;
114 }
115
116 if (*params != '\0')
117 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
118 "You know the following '%s' spells:", params);
119 else
120 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
121 "You know the following spells:");
122
123 /* Note in the code below that we make some
124 * presumptions that there will be a colon in the
125 * string. given the code above, this is always
126 * the case.
127 */
128 qsort(spell_sort, num_found, MAX_BUF, (int (*)(const void *, const void *))strcmp);
129 strcpy(tmp, "asdfg"); /* Dummy string so initial compare fails */
130 for (i = 0; i < num_found; i++) {
131 /* Different skill name, so print banner */
132 if (strncmp(tmp, spell_sort[i], strlen(tmp))) {
133 strcpy(tmp, spell_sort[i]);
134 cp = strchr(tmp, ':');
135 *cp = '\0';
136
137 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
138 "\n[fixed]%s spells %.*s <lvl> <sp>",
139 tmp, (int)(20-strlen(tmp)), " ");
140 }
141 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
142 "[fixed]%s",
143 strchr(spell_sort[i], ':')+1);
144 }
145 }
146
147 /**
148 * Sets up to cast a spell.
149 *
150 * Invoke casts a spell immediately, whereas cast just set up the range type.
151 *
152 * @param op
153 * caster.
154 * @param params
155 * spell name.
156 * @param command
157 * first letter of the spell type (c=cast, i=invoke, p=prepare).
158 */
159 void command_cast_spell(object *op, const char *params, char command) {
160 int castnow = 0;
161 char *cp, cpy[MAX_BUF];
162 object *spob;
163
164 safe_strncpy(cpy, params, sizeof(cpy));
165
166 if (command == 'i')
167 castnow = 1;
168
169 if (*cpy != '\0') {
170 tag_t spellnumber = 0;
171 if ((spellnumber = atoi(cpy)) != 0)
172 spob = object_find_by_tag(op, spellnumber);
173 else
174 spob = lookup_spell_by_name(op, cpy);
175
176 if (spob && spob->type == SPELL) {
177 /* Now grab any extra data, if there is any. Forward pass
178 * any 'of' delimiter
179 */
180 if (spellnumber) {
181 /* if we passed a number, the options start at the second word */
182 cp = strchr(cpy, ' ');
183 if (cp) {
184 cp++;
185 if (!strncmp(cp, "of ", 3))
186 cp += 3;
187 }
188 } else if (strlen(cpy) > strlen(spob->name)) {
189 cp = cpy+strlen(spob->name);
190 *cp = 0;
191 cp++;
192 if (!strncmp(cp, "of ", 3))
193 cp += 3;
194 } else
195 cp = NULL;
196
197 if (spob->skill && !find_skill_by_name(op, spob->skill)) {
198 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
199 "You need the skill %s to cast %s!",
200 spob->skill, spob->name);
201 return;
202 }
203
204 /* Remove control of the golem */
205 if (op->contr->ranges[range_golem] != NULL) {
206 if (op->contr->golem_count == op->contr->ranges[range_golem]->count) {
207 remove_friendly_object(op->contr->ranges[range_golem]);
208 object_remove(op->contr->ranges[range_golem]);
209 object_free_drop_inventory(op->contr->ranges[range_golem]);
210 }
211 op->contr->ranges[range_golem] = NULL;
212 op->contr->golem_count = 0;
213 }
214
215 /* This assignment is need for casting_time logic */
216 op->spell = spob;
217 if (castnow) {
218 cast_spell(op, op, op->facing, spob, cp);
219 } else {
220 /** @todo present the list nicely instead of comma-separated simply */
221 sstring required = object_get_value(spob, "casting_requirements");
222 op->contr->ranges[range_magic] = spob;
223 op->contr->shoottype = range_magic;
224
225 if (cp != NULL) {
226 strncpy(op->contr->spellparam, cp, MAX_BUF);
227 op->contr->spellparam[MAX_BUF-1] = '\0';
228 } else {
229 op->contr->spellparam[0] = '\0';
230 }
231 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
232 "You ready the spell %s%s%s",
233 spob->name, required ? " which consumes for each invocation " : "", required ? required : "");
234 }
235 return;
236 } /* else fall through to below and print spells */
237 } /* params supplied */
238
239 /* We get here if cast was given without options or we could not find
240 * the requested spell. List all the spells the player knows.
241 */
242 show_matching_spells(op, cpy);
243 }
244
245 /**************************************************************************/
246
247 /**
248 * Check for the validity of a player range.
249 *
250 * This function could probably be simplified, eg, everything
251 * should index into the ranges[] array.
252 *
253 * @param op
254 * player to check.
255 * @param r
256 * range to check.
257 *
258 * @retval 1
259 * range specified is legal - that is, the character has an item that is equipped for that range type.
260 * @retval 0
261 * no item of that range type that is usable.
262 */
263
264 int legal_range(object *op, int r) {
265 switch (r) {
266 case range_none: /* "Nothing" is always legal */
267 return 1;
268
269 case range_bow:
270 case range_misc:
271 case range_magic: /* cast spells */
272 if (op->contr->ranges[r])
273 return 1;
274 else
275 return 0;
276
277 case range_golem: /* Use scrolls */
278 if (op->contr->ranges[range_golem]
279 && op->contr->ranges[range_golem]->count == op->contr->golem_count)
280 return 1;
281 else
282 return 0;
283
284 case range_skill:
285 if (op->chosen_skill)
286 return 1;
287 else
288 return 0;
289 }
290 /* No match above, must not be valid */
291 return 0;
292 }
293
294 /**
295 * Rotate the selected range attack.
296 *
297 * @param op
298 * player.
299 * @param k
300 * '+' selects next range, other values previous range.
301 */
302 static void change_spell(object *op, char k) {
303 char name[MAX_BUF];
304
305 do {
306 op->contr->shoottype += ((k == '+') ? 1 : -1);
307 if (op->contr->shoottype >= range_size)
308 op->contr->shoottype = range_none;
309 else if (op->contr->shoottype <= range_bottom)
310 op->contr->shoottype = (rangetype)(range_size-1);
311 } while (!legal_range(op, op->contr->shoottype));
312
313 /* Legal range has already checked that we have an appropriate item
314 * that uses the slot, so we don't need to be too careful about
315 * checking the status of the object.
316 */
317 switch (op->contr->shoottype) {
318 case range_none:
319 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
320 "No ranged attack chosen.");
321 break;
322
323 case range_golem:
324 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
325 "You regain control of your golem.");
326 break;
327
328 case range_bow:
329 query_name(op->contr->ranges[range_bow], name, MAX_BUF);
330 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
331 "Switched to %s and %s.",
332 name,
333 op->contr->ranges[range_bow]->race ? op->contr->ranges[range_bow]->race : "nothing");
334 break;
335
336 case range_magic:
337 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
338 "Switched to spells (%s).",
339 op->contr->ranges[range_magic]->name);
340 break;
341
342 case range_misc:
343 query_base_name(op->contr->ranges[range_misc], 0, name, MAX_BUF);
344 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
345 "Switched to %s.",
346 name);
347 break;
348
349 case range_skill:
350 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
351 "Switched to skill: %s",
352 op->chosen_skill ? op->chosen_skill->name : "none");
353 break;
354
355 default:
356 break;
357 }
358 }
359
360 /**
361 * 'rotateshoottype' command, switch range attack.
362 *
363 * @param op
364 * player.
365 * @param params
366 * arguments to the command.
367 */
368 void command_rotateshoottype(object *op, const char *params) {
369 if (*params == '\0')
370 change_spell(op, '+');
371 else
372 change_spell(op, params[0]);
373 }