Debian Codegraph @

server/rune.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 *
17 * All rune-related functions.
18 */
19
20 #include "global.h"
21
22 #include <string.h>
23
24 #include "sproto.h"
25 #include "spells.h"
26
27 #ifndef sqr
28 #define sqr(x) ((x)*(x))
29 #endif
30
31 /**
32 * Player is attempting to write a magical rune.
33 * This function does all checks for paths, sp/gr, ...
34 *
35 * @param op
36 * rune writer.
37 * @param caster
38 * object used for casting this rune.
39 * @param spell
40 * writing spell.
41 * @param dir
42 * orientation of rune, direction rune's contained spell will be cast in, if applicable
43 * @param runename
44 * name of the rune or message displayed by the rune for a rune of marking.
45 * @retval 0
46 * no rune was written.
47 * @retval 1
48 * rune written.
49 */
50 int write_rune(object *op, object *caster, object *spell, int dir, const char *runename) {
51 object *rune_spell, *rune;
52 char buf[MAX_BUF];
53 mapstruct *m;
54 int16_t nx, ny, gr;
55
56 if (!dir) {
57 dir = 1;
58 }
59
60 nx = op->x+freearr_x[dir];
61 ny = op->y+freearr_y[dir];
62 m = op->map;
63
64 if (get_map_flags(m, &m, nx, ny, &nx, &ny)) {
65 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
66 "Can't make a rune there!");
67 return 0;
68 }
69 FOR_MAP_PREPARE(m, nx, ny, tmp)
70 if (tmp->type == RUNE) {
71 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
72 "You can't write a rune there.");
73 return 0;
74 }
75 FOR_MAP_FINISH();
76
77 if (spell->other_arch) {
78 rune_spell = arch_to_object(spell->other_arch);
79 } else {
80 /* Player specified spell. The player has to know the spell, so
81 * lets just look through the players inventory see if they know it
82 * use the object_matches_string() for our typical matching method.
83 */
84 int bestmatch = 0, ms;
85
86 if (!runename || *runename == 0) {
87 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
88 "Write a rune of what?");
89 return 0;
90 }
91
92 rune_spell = NULL;
93 FOR_INV_PREPARE(op, tmp) {
94 if (tmp->type == SPELL) {
95 ms = object_matches_string(op, tmp, runename);
96 if (ms > bestmatch) {
97 bestmatch = ms;
98 rune_spell = tmp;
99 }
100 }
101 } FOR_INV_FINISH();
102 if (!rune_spell) {
103 draw_ext_info_format(NDI_UNIQUE, 0, op,
104 MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
105 "You don't know any spell named %s",
106 runename);
107 return 0;
108 }
109 if (rune_spell->skill != spell->skill) {
110 draw_ext_info_format(NDI_UNIQUE, 0, op,
111 MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
112 "You can't cast %s with %s",
113 rune_spell->name, spell->name);
114 return 0;
115 }
116 if (caster->path_denied&spell->path_attuned) {
117 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
118 "%s belongs to a spell path denied to you.",
119 rune_spell->name);
120 return 0;
121 }
122 if (caster_level(caster, rune_spell) < rune_spell->level) {
123 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
124 "%s is beyond your ability to cast!",
125 rune_spell->name);
126 return 0;
127 }
128 if (SP_level_spellpoint_cost(caster, rune_spell, SPELL_MANA) > op->stats.sp) {
129 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
130 "You don't have enough mana.");
131 return 0;
132 }
133 gr = SP_level_spellpoint_cost(caster, rune_spell, SPELL_GRACE);
134 if ((gr > 0) && (gr > op->stats.grace)) {
135 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
136 "You don't have enough grace.");
137 return 0;
138 }
139 op->stats.grace -= gr;
140 op->stats.sp -= SP_level_spellpoint_cost(caster, rune_spell, SPELL_MANA);
141 }
142 /* already proper rune. Note this should only be the case if other_arch was set */
143 if (rune_spell->type == RUNE) {
144 rune = rune_spell;
145 } else {
146 object *tmp;
147
148 rune = create_archetype(GENERIC_RUNE);
149 snprintf(buf, sizeof(buf), "You set off a rune of %s\n", rune_spell->name);
150 object_set_msg(rune, buf);
151 tmp = object_new();
152 object_copy(rune_spell, tmp);
153 object_insert_in_ob(tmp, rune);
154 if (spell->face != blank_face)
155 rune->face = spell->face;
156 }
157 rune->level = caster_level(caster, spell);
158 rune->stats.Cha = rune->level/2; /* the invisibility parameter */
159 rune->direction = dir; /* where any spell will go upon detonation */
160 object_set_owner(rune, op); /* runes without need no owner */
161 set_spell_skill(op, caster, spell, rune);
162 object_insert_in_map_at(rune, m, op, 0, nx, ny);
163 return 1;
164 }
165
166 /**
167 * This function handles those runes which detonate but do not cast spells.
168 * Typically, poisoned or diseased runes.
169 *
170 * @param op
171 * rune.
172 * @param victim
173 * victim of the rune.
174 */
175 static void rune_attack(object *op, object *victim) {
176 if (victim) {
177 tag_t tag = victim->count;
178 hit_player(victim, op->stats.dam, op, op->attacktype, 1);
179 if (object_was_destroyed(victim, tag))
180 return;
181 /* if there's a disease in the needle, put it in the player */
182 if (HAS_RANDOM_ITEMS(op))
183 create_treasure(op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0);
184 if (op->inv && op->inv->type == DISEASE) {
185 object *disease = op->inv;
186
187 infect_object(victim, disease, 1);
188 object_remove(disease);
189 object_free_drop_inventory(disease);
190 }
191 } else
192 hit_map(op, 0, op->attacktype, 1);
193 }
194
195 /**
196 * This function generalizes attacks by runes/traps. This ought to make
197 * it possible for runes to attack from the inventory,
198 * it'll spring the trap on the victim.
199 *
200 * @param trap
201 * trap that activates.
202 * @param victim
203 * victim of the trap.
204 */
205 void spring_trap(object *trap, object *victim) {
206 object *env;
207 tag_t trap_tag = trap->count;
208 rv_vector rv;
209 int i, has_spell;
210
211 /* Prevent recursion */
212 if (trap->stats.hp <= 0)
213 return;
214
215 if (QUERY_FLAG(trap, FLAG_IS_LINKED))
216 use_trigger(trap);
217
218 /* Check if this trap casts a spell */
219 has_spell = ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->clone.type == SPELL));
220
221 env = object_get_env_recursive(trap);
222
223 /* If the victim is not next to this trap, and the trap doesn't cast
224 * a spell, don't set it off.
225 */
226 if (!get_rangevector(env, victim, &rv, 0) || (rv.distance > 1 && !has_spell))
227 return;
228
229 /* Only living objects can trigger runes that don't cast spells, as
230 * doing direct damage to a non-living object doesn't work anyway.
231 * Typical example is an arrow attacking a door.
232 */
233 if (!QUERY_FLAG(victim, FLAG_ALIVE) && !has_spell)
234 return;
235
236 if (!QUERY_FLAG(trap, FLAG_LIFESAVE))
237 trap->stats.hp--; /*decrement detcount */
238
239 if (victim && victim->type == PLAYER && trap->msg != NULL && trap->msg[0] != '\0')
240 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_APPLY, MSG_TYPE_APPLY_TRAP,
241 trap->msg);
242
243 /* Flash an image of the trap on the map so the poor sod
244 * knows what hit him.
245 */
246 trap_show(trap, env);
247
248 /* Only if it is a spell do we proceed here */
249 if (has_spell) {
250 object *spell;
251
252 /* This is necessary if the trap is inside something else */
253 object_remove(trap);
254 object_insert_in_map_at(trap, victim->map, trap, 0, victim->x, victim->y);
255
256 if (object_was_destroyed(trap, trap_tag))
257 return;
258
259 for (i = 0; i < MAX(1, trap->stats.maxhp); i++) {
260 if (trap->inv)
261 cast_spell(trap, trap, trap->direction, trap->inv, NULL);
262 else {
263 spell = arch_to_object(trap->other_arch);
264 cast_spell(trap, trap, trap->direction, spell, NULL);
265 object_free_drop_inventory(spell);
266 }
267 }
268 } else {
269 rune_attack(trap, victim);
270 if (object_was_destroyed(trap, trap_tag))
271 return;
272 }
273
274 if (trap->stats.hp <= 0) {
275 trap->type = SIGN; /* make the trap impotent */
276 trap->stats.food = 20; /* make it stick around until its spells are gone */
277 SET_FLAG(trap, FLAG_IS_USED_UP);
278 }
279 }
280
281 /**
282 * Someone is trying to disarm a rune. The actual attempt is done in trap_disarm().
283 *
284 * @param op
285 * object trying to disarm.
286 * @param caster
287 * object casting the disarm spell.
288 * @param spell
289 * actual spell for casting.
290 * @param skill
291 * skill to disarm runes.
292 * @param dir
293 * direction to disarm.
294 * @retval 0
295 * rune wasn't disarmed.
296 * @retval 1
297 * a rune was disarmed.
298 */
299 int dispel_rune(object *op, object *caster, object *spell, object *skill, int dir) {
300 object *rune;
301 int mflags;
302 int16_t x, y;
303 mapstruct *m;
304
305 x = op->x+freearr_x[dir];
306 y = op->y+freearr_y[dir];
307 m = op->map;
308
309 mflags = get_map_flags(m, &m, x, y, &x, &y);
310
311 /* Should we perhaps not allow player to disable traps if a monster/
312 * player is standing on top?
313 */
314 if (mflags&P_OUT_OF_MAP) {
315 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
316 "There's nothing there!");
317 return 0;
318 }
319
320 /* This can happen if a player does a 'magic rune of dispel'. Without
321 * any skill, chance of success is zero, and we don't know who to tell
322 * (as otherwise we would have a skill pointer). Plus, trap_disarm()
323 * presumes skill is not null and will crash if it is.
324 */
325 if (!skill)
326 return 0;
327
328 rune = NULL;
329 FOR_MAP_PREPARE(m, x, y, tmp) {
330 object *tmp2;
331
332 if (tmp->type == RUNE || tmp->type == TRAP) {
333 rune = tmp;
334 break;
335 }
336
337 /* we could put a probability chance here, but since nothing happens
338 * if you fail, no point on that. I suppose we could do a level
339 * comparison so low level players can't erase high level players runes.
340 */
341 if (tmp->type == SIGN && !strcmp(tmp->arch->name, "rune_mark")) {
342 object_remove(tmp);
343 object_free_drop_inventory(tmp);
344 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
345 "You wipe out the rune of marking!");
346 return 1;
347 }
348
349 /* now search tmp's inventory for traps
350 * This is for chests, where the rune is in the chests inventory.
351 */
352 tmp2 = object_find_by_type2(tmp, RUNE, TRAP);
353 if (tmp2 != NULL) {
354 rune = tmp2;
355 break;
356 }
357 } FOR_MAP_FINISH();
358
359 /* no rune there. */
360 if (rune == NULL) {
361 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
362 "There's nothing there!");
363 return 0;
364 }
365 trap_disarm(op, rune, 0, skill);
366 return 1;
367 }
368
369 /**
370 * Should op see trap?
371 * @param op
372 * living that could spot the trap.
373 * @param trap
374 * trap that is invisible.
375 * @retval 0
376 * trap wasn't spotted.
377 * @retval 1
378 * trap was spotted.
379 */
380 int trap_see(object *op, object *trap) {
381 int chance;
382
383 chance = random_roll(0, 99, op, PREFER_HIGH);
384
385 /* decide if we see the rune or not */
386 if ((trap->stats.Cha == 1)
387 || (chance > MIN(95, MAX(5, ((int)((float)(op->map->difficulty+trap->level+trap->stats.Cha-op->level)/10.0*50.0)))))) {
388 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
389 "You spot a %s!",
390 trap->name);
391 return 1;
392 }
393 return 0;
394 }
395
396 /**
397 * Handles showing a trap/rune detonation.
398 * @param trap
399 * trap that detonates.
400 * @param where
401 * object at the location to detonate.
402 * @retval 0
403 * no animation inserted.
404 * @retval 1
405 * animation inserted.
406 */
407 int trap_show(object *trap, object *where) {
408 object *tmp2;
409
410 if (where == NULL)
411 return 0;
412 tmp2 = create_archetype("runedet");
413 // Custom-made traps can have no animation.
414 // The GET_ANIMATION macro does not handle this scenario
415 if (trap->animation || trap->temp_animation)
416 tmp2->face = GET_ANIMATION(trap, 0);
417 else
418 tmp2->face = trap->face;
419 object_insert_in_map_at(tmp2, where->map, NULL, 0, where->x, where->y);
420 return 1;
421 }
422
423 /**
424 * Try to disarm a trap/rune.
425 *
426 * @param disarmer
427 * object disarming the trap/rune.
428 * @param trap
429 * trap to disarm.
430 * @param risk
431 * if 0, trap/rune won't spring if disarm failure. Else it will spring.
432 * @param skill
433 * spell used to disarm.
434 * @return
435 * experience to award, 0 for failure.
436 */
437 int trap_disarm(object *disarmer, object *trap, int risk, object *skill) {
438 int trapworth; /* need to compute the experience worth of the trap
439 before we kill it */
440
441 /* this formula awards a more reasonable amount of exp */
442 trapworth = MAX(1, trap->level)*disarmer->map->difficulty*
443 sqr(MAX(trap->stats.dam, trap->inv ? trap->inv->level : 1))/skill->level;
444
445 if (!(random_roll(0, (MAX(2, MIN(20, trap->level-skill->level+5-disarmer->stats.Dex/2))-1), disarmer, PREFER_LOW))) {
446 object *owner;
447
448 draw_ext_info_format(NDI_UNIQUE, 0, disarmer,
449 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
450 "You successfully disarm the %s!",
451 trap->name);
452 destroy_object(trap);
453 /* If it is your own trap, (or any players trap), don't you don't
454 * get exp for it.
455 */
456 owner = object_get_owner(trap);
457 if (owner != NULL && owner->type != PLAYER && risk)
458 return trapworth;
459 else
460 return 1; /* give minimal exp and say success */
461 } else {
462 draw_ext_info_format(NDI_UNIQUE, 0, disarmer,
463 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE,
464 "You fail to disarm the %s.",
465 trap->name);
466 if (!(random_roll(0, (MAX(2, skill->level-trap->level+disarmer->stats.Dex/2-6))-1, disarmer, PREFER_LOW))
467 && risk) {
468 draw_ext_info(NDI_UNIQUE, 0, disarmer,
469 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
470 "In fact, you set it off!");
471 spring_trap(trap, disarmer);
472 }
473 return 0;
474 }
475 }