File: c_range.c

package info (click to toggle)
crossfire 1.71.0%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 28,076 kB
  • sloc: ansic: 85,126; sh: 11,978; perl: 2,659; lex: 2,044; makefile: 1,271
file content (371 lines) | stat: -rw-r--r-- 12,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Crossfire -- cooperative multi-player graphical RPG and adventure game
 *
 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
 * Copyright (c) 1992 Frank Tore Johansen
 *
 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
 * welcome to redistribute it under certain conditions. For details, please
 * see COPYING and LICENSE.
 *
 * The authors can be reached via e-mail at <crossfire@metalforge.org>.
 */

/**
 * @file
 * Range related commands (casting, shooting, throwing, etc.).
 */

#include <global.h>
#ifndef __CEXTRACT__
#include <sproto.h>
#endif
#include <spells.h>
#include <skills.h>
#include <shared/newclient.h>
#include <commands.h>

/**
 * 'invoke' command, fires a spell immediately.
 *
 * @param op
 * player.
 * @param params
 * spell.
 */
void command_invoke(object *op, const char *params) {
    command_cast_spell(op, params, 'i');
}

/**
 * 'cast' command, prepares a spell for laster casting.
 *
 * @param op
 * player.
 * @param params
 * spell.
 */
void command_cast(object *op, const char *params) {
    command_cast_spell(op, params, 'c');
}

/**
 * Equivalent to command_cast().
 *
 * @param op
 * player.
 * @param params
 * spell.
 * @todo remove.
 */
void command_prepare(object *op, const char *params) {
    command_cast_spell(op, params, 'p');
}

/**
 * Shows all spells that op knows.
 *
 * Given there is more than one skill, we can't supply break
 * them down to cleric/wizardry.
 *
 * @param op
 * player wanting to knows her spells.
 * @param params
 * if supplied, the spell name must match that.
 */
static void show_matching_spells(object *op, const char *params) {
    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_INV_PREPARE(op, spell) {
        /* If it is a spell, and no params are passed, or they
         * match the name, process this spell.
         */
        if (spell->type == SPELL
        && (*params == '\0' || !strncmp(params, spell->name, strlen(params)))) {
            if (spell->path_attuned&op->path_denied) {
                snprintf(spell_sort[num_found++], sizeof(spell_sort[0]),
                         "%s:%-22s %3s %3s", spell->skill ? spell->skill : "generic",
                         spell->name, "den", "den");
            } else {
                snprintf(spell_sort[num_found++], sizeof(spell_sort[0]),
                         "%s:%-22s %3d %3d", spell->skill ? spell->skill : "generic",
                         spell->name, spell->level,
                         SP_level_spellpoint_cost(op, spell, SPELL_HIGHEST));
            }
        }
    } FOR_INV_FINISH();
    if (!num_found) {
        if (*params != '\0')
            draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
                          "You know no spells like '%s'.", params);
        else
            draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
                          "You know no spells.");

        return;
    }

    if (*params != '\0')
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                      "You know the following '%s' spells:", params);
    else
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                      "You know the following spells:");

    /* 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 *))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';

            draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                                 "\n[fixed]%s spells %.*s <lvl> <sp>",
                                 tmp, (int)(12-strlen(tmp)), "              ");
        }
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                             "[fixed]%s",
                             strchr(spell_sort[i], ':')+1);
    }
}

/**
 * Sets up to cast a spell.
 *
 * Invoke casts a spell immediately, whereas cast just set up the range type.
 *
 * @param op
 * caster.
 * @param params
 * spell name.
 * @param command
 * first letter of the spell type (c=cast, i=invoke, p=prepare).
 */
void command_cast_spell(object *op, const char *params, char command) {
    int castnow = 0;
    char *cp, cpy[MAX_BUF];
    object *spob;

    strncpy(cpy, params, sizeof(cpy));

    if (command == 'i')
        castnow = 1;

    if (*cpy != '\0') {
        tag_t spellnumber = 0;
        if ((spellnumber = atoi(cpy)) != 0)
            spob = object_find_by_tag(op, spellnumber);
        else
            spob = lookup_spell_by_name(op, cpy);

        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(cpy, ' ');
                if (cp) {
                    cp++;
                    if (!strncmp(cp, "of ", 3))
                        cp += 3;
                }
            } else if (strlen(cpy) > strlen(spob->name)) {
                cp = cpy+strlen(spob->name);
                *cp = 0;
                cp++;
                if (!strncmp(cp, "of ", 3))
                    cp += 3;
            } else
                cp = NULL;

            if (spob->skill && !find_skill_by_name(op, spob->skill)) {
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
                                     "You need the skill %s to cast %s!",
                                     spob->skill, spob->name);
                return;
            }

            /* Remove control of the golem */
            if (op->contr->ranges[range_golem] != NULL) {
                if (op->contr->golem_count == op->contr->ranges[range_golem]->count) {
                    remove_friendly_object(op->contr->ranges[range_golem]);
                    object_remove(op->contr->ranges[range_golem]);
                    object_free_drop_inventory(op->contr->ranges[range_golem]);
                }
                op->contr->ranges[range_golem] = NULL;
                op->contr->golem_count = 0;
            }

            /* This assignment is need for casting_time logic */
            op->spell = spob;
            if (castnow) {
                cast_spell(op, op, op->facing, spob, cp);
            } else {
                /** @todo present the list nicely instead of comma-separated simply */
                sstring required = object_get_value(spob, "casting_requirements");
                op->contr->ranges[range_magic] = spob;
                op->contr->shoottype = range_magic;

                if (cp != NULL) {
                    strncpy(op->contr->spellparam, cp, MAX_BUF);
                    op->contr->spellparam[MAX_BUF-1] = '\0';
                } else {
                    op->contr->spellparam[0] = '\0';
                }
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                                     "You ready the spell %s%s%s",
                                     spob->name, required ? " which consumes for each invocation " : "", required ? required : "");
            }
            return;
        } /* 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.
     */
    show_matching_spells(op, cpy);
}

/**************************************************************************/

/**
 * Check for the validity of a player range.
 *
 * This function could probably be simplified, eg, everything
 * should index into the ranges[] array.
 *
 * @param op
 * player to check.
 * @param r
 * range to check.
 *
 * @retval 1
 * range specified is legal - that is, the character has an item that is equipped for that range type.
 * @retval 0
 * no item of that range type that is usable.
 */

int legal_range(object *op, int r) {
    switch (r) {
    case range_none: /* "Nothing" is always legal */
        return 1;

    case range_bow:
    case range_misc:
    case range_magic: /* cast spells */
        if (op->contr->ranges[r])
            return 1;
        else
            return 0;

    case range_golem: /* Use scrolls */
        if (op->contr->ranges[range_golem]
        && op->contr->ranges[range_golem]->count == op->contr->golem_count)
            return 1;
        else
            return 0;

    case range_skill:
        if (op->chosen_skill)
            return 1;
        else
            return 0;
    }
    /* No match above, must not be valid */
    return 0;
}

/**
 * Rotate the selected range attack.
 *
 * @param op
 * player.
 * @param k
 * '+' selects next range, other values previous range.
 */
static void change_spell(object *op, char k) {
    char name[MAX_BUF];

    do {
        op->contr->shoottype += ((k == '+') ? 1 : -1);
        if (op->contr->shoottype >= range_size)
            op->contr->shoottype = range_none;
        else if (op->contr->shoottype <= range_bottom)
            op->contr->shoottype = (rangetype)(range_size-1);
    } while (!legal_range(op, op->contr->shoottype));

    /* Legal range has already checked that we have an appropriate item
     * that uses the slot, so we don't need to be too careful about
     * checking the status of the object.
     */
    switch (op->contr->shoottype) {
    case range_none:
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
                      "No ranged attack chosen.");
        break;

    case range_golem:
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                      "You regain control of your golem.");
        break;

    case range_bow:
        query_name(op->contr->ranges[range_bow], name, MAX_BUF);
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                             "Switched to %s and %s.",
                             name,
                             op->contr->ranges[range_bow]->race ? op->contr->ranges[range_bow]->race : "nothing");
        break;

    case range_magic:
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                             "Switched to spells (%s).",
                             op->contr->ranges[range_magic]->name);
        break;

    case range_misc:
        query_base_name(op->contr->ranges[range_misc], 0, name, MAX_BUF);
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                             "Switched to %s.",
                             name);
        break;

    case range_skill:
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
                             "Switched to skill: %s",
                             op->chosen_skill ? op->chosen_skill->name : "none");
        break;

    default:
        break;
    }
}

/**
 * 'rotateshoottype' command, switch range attack.
 *
 * @param op
 * player.
 * @param params
 * arguments to the command.
 */
void command_rotateshoottype(object *op, const char *params) {
    if (*params == '\0')
        change_spell(op, '+');
    else
        change_spell(op, params[0]);
}