1 |
/* |
2 |
* CrossFire, A Multiplayer game for X-windows |
3 |
* |
4 |
* Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 |
* Copyright (C) 2003 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 |
#include <global.h> |
26 |
#include <sproto.h> |
27 |
#include <spells.h> |
28 |
|
29 |
#ifndef sqr |
30 |
# define sqr(x) ((x)*(x)) |
31 |
#endif |
32 |
|
33 |
/* peterm: |
34 |
* write_rune: |
35 |
* op: rune writer |
36 |
* skop: skill object used for casting this rune |
37 |
* dir: orientation of rune, direction rune's contained spell will |
38 |
* be cast in, if applicable |
39 |
* inspell: spell object to put into the rune, can be null if doing |
40 |
* a marking rune. |
41 |
* level: level of casting of the rune |
42 |
* runename: name of the rune or message displayed by the rune for |
43 |
* a rune of marking |
44 |
*/ |
45 |
|
46 |
int |
47 |
write_rune (object *op, object *caster, object *spell, int dir, const char *runename) |
48 |
{ |
49 |
object *tmp, *rune_spell, *rune; |
50 |
char buf[MAX_BUF]; |
51 |
maptile *m; |
52 |
sint16 nx, ny; |
53 |
|
54 |
if (!dir) |
55 |
dir = 1; |
56 |
|
57 |
nx = op->x + freearr_x[dir]; |
58 |
ny = op->y + freearr_y[dir]; |
59 |
m = op->map; |
60 |
|
61 |
if (get_map_flags (m, &m, nx, ny, &nx, &ny) & (P_OUT_OF_MAP | P_SAFE | P_NO_MAGIC | P_NO_CLERIC)) |
62 |
{ |
63 |
new_draw_info (NDI_UNIQUE, 0, op, "Can't make a rune there!"); |
64 |
return 0; |
65 |
} |
66 |
|
67 |
for (tmp = m->at (nx, ny).bot; tmp; tmp = tmp->above) |
68 |
if (tmp->type == RUNE) |
69 |
break; |
70 |
|
71 |
if (tmp) |
72 |
{ |
73 |
new_draw_info (NDI_UNIQUE, 0, op, "You can't write a rune on top of another rune."); |
74 |
return 0; |
75 |
} |
76 |
|
77 |
if (spell->other_arch) |
78 |
rune_spell = arch_to_object (spell->other_arch); |
79 |
else |
80 |
{ |
81 |
/* Player specified spell. The player has to know the spell, so |
82 |
* lets just look through the players inventory see if they know it |
83 |
* use the item_matched_string for our typical matching method. |
84 |
*/ |
85 |
int bestmatch = 0, ms; |
86 |
|
87 |
if (!runename || *runename == 0) |
88 |
{ |
89 |
new_draw_info (NDI_UNIQUE, 0, op, "Write a rune of what?"); |
90 |
return 0; |
91 |
} |
92 |
|
93 |
rune_spell = NULL; |
94 |
for (tmp = op->inv; tmp; tmp = tmp->below) |
95 |
if (tmp->type == SPELL) |
96 |
{ |
97 |
ms = item_matched_string (op, tmp, runename); |
98 |
if (ms > bestmatch) |
99 |
{ |
100 |
bestmatch = ms; |
101 |
rune_spell = tmp; |
102 |
} |
103 |
} |
104 |
|
105 |
if (!rune_spell) |
106 |
{ |
107 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You don't know any spell named %s", runename); |
108 |
return 0; |
109 |
} |
110 |
|
111 |
if (rune_spell->skill != spell->skill) |
112 |
{ |
113 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You can't cast %s with %s", &rune_spell->name, &spell->name); |
114 |
return 0; |
115 |
} |
116 |
|
117 |
if (caster->path_denied & spell->path_attuned && !caster->flag [FLAG_WIZCAST]) |
118 |
{ |
119 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s belongs to a spell path denied to you.", &rune_spell->name); |
120 |
return 0; |
121 |
} |
122 |
|
123 |
if (caster_level (caster, rune_spell) < rune_spell->level && !caster->flag [FLAG_WIZCAST]) |
124 |
{ |
125 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s is beyond your ability to cast!", &rune_spell->name); |
126 |
return 0; |
127 |
} |
128 |
|
129 |
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA) > op->stats.sp) |
130 |
{ |
131 |
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough mana."); |
132 |
return 0; |
133 |
} |
134 |
|
135 |
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE) > op->stats.grace) |
136 |
{ |
137 |
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough grace."); |
138 |
return 0; |
139 |
} |
140 |
|
141 |
op->stats.grace -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE); |
142 |
op->stats.sp -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA); |
143 |
} |
144 |
|
145 |
/* already proper rune. Note this should only be the case if other_arch was set */ |
146 |
if (rune_spell->type == RUNE) |
147 |
rune = rune_spell; |
148 |
else |
149 |
{ |
150 |
rune = get_archetype (GENERIC_RUNE); |
151 |
sprintf (buf, "You set off a rune of %s\n", &rune_spell->name); |
152 |
rune->msg = buf; |
153 |
rune->insert (rune_spell->clone ()); |
154 |
|
155 |
if (spell->face != blank_face) |
156 |
rune->face = spell->face; |
157 |
} |
158 |
|
159 |
rune->level = caster_level (caster, spell); |
160 |
rune->stats.Cha = rune->level / 2; /* the invisibility parameter */ |
161 |
rune->direction = dir; /* where any spell will go upon detonation */ |
162 |
rune->set_owner (op); /* runes without need no owner */ |
163 |
set_spell_skill (op, caster, spell, rune); |
164 |
|
165 |
m->insert (rune, nx, ny, op); |
166 |
return 1; |
167 |
} |
168 |
|
169 |
/* move_rune: peterm |
170 |
comments on runes: |
171 |
rune->level : level at which rune will cast its spell. |
172 |
rune->hp : number of detonations before rune goes away |
173 |
rune->msg : message the rune displays when it goes off |
174 |
rune->direction : direction it will cast a spell in |
175 |
rune->dam : damage the rune will do if it doesn't cast spells |
176 |
rune->attacktype: type of damage it does, if not casting spells |
177 |
rune->other_arch: spell in the rune |
178 |
rune->Cha : how hidden the rune is |
179 |
rune->maxhp : number of spells the rune casts |
180 |
*/ |
181 |
void |
182 |
move_rune (object *op) |
183 |
{ |
184 |
/* runes of level zero cannot detonate. */ |
185 |
if (!op->level) |
186 |
return; |
187 |
|
188 |
int det = op->invisible; |
189 |
|
190 |
if (!(rndm (0, MAX (1, (op->stats.Cha)) - 1))) |
191 |
{ |
192 |
op->invisible = 0; |
193 |
op->speed_left -= 1; |
194 |
} |
195 |
else |
196 |
op->invisible = 1; |
197 |
|
198 |
if (op->invisible != det) |
199 |
update_object (op, UP_OBJ_FACE); |
200 |
} |
201 |
|
202 |
|
203 |
/* peterm: rune_attack |
204 |
* function handles those runes which detonate but do not cast spells. |
205 |
*/ |
206 |
|
207 |
|
208 |
void |
209 |
rune_attack (object *op, object *victim) |
210 |
{ |
211 |
if (victim) |
212 |
{ |
213 |
hit_player (victim, op->stats.dam, op, op->attacktype, 1); |
214 |
|
215 |
if (victim->destroyed ()) |
216 |
return; |
217 |
|
218 |
/* if there's a disease in the needle, put it in the player */ |
219 |
if (op->has_random_items ()) |
220 |
create_treasure (op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0); |
221 |
|
222 |
if (op->inv && op->inv->type == DISEASE) |
223 |
{ |
224 |
object *disease = op->inv; |
225 |
|
226 |
infect_object (victim, disease, 1); |
227 |
disease->destroy (); |
228 |
} |
229 |
} |
230 |
else |
231 |
hit_map (op, 0, op->attacktype, 1); |
232 |
} |
233 |
|
234 |
/* This function generalizes attacks by runes/traps. This ought to make |
235 |
* it possible for runes to attack from the inventory, |
236 |
* it'll spring the trap on the victim. |
237 |
*/ |
238 |
void |
239 |
spring_trap (object *trap, object *victim) |
240 |
{ |
241 |
object *env; |
242 |
rv_vector rv; |
243 |
int i; |
244 |
|
245 |
/* Prevent recursion */ |
246 |
if (trap->stats.hp <= 0) |
247 |
return; |
248 |
|
249 |
if (QUERY_FLAG (trap, FLAG_IS_LINKED)) |
250 |
use_trigger (trap); |
251 |
|
252 |
/* Only living objects can trigger runes that don't cast spells, as |
253 |
* doing direct damage to a non-living object doesn't work anyway. |
254 |
* Typical example is an arrow attacking a door. |
255 |
*/ |
256 |
if (!QUERY_FLAG (victim, FLAG_ALIVE) && !trap->inv && !trap->other_arch) |
257 |
return; |
258 |
|
259 |
trap->stats.hp--; /*decrement detcount */ |
260 |
|
261 |
if (victim && victim->type == PLAYER) |
262 |
new_draw_info (NDI_UNIQUE, 0, victim, trap->msg); |
263 |
|
264 |
/* Flash an image of the trap on the map so the poor sod |
265 |
* knows what hit him. |
266 |
*/ |
267 |
env = object_get_env_recursive (trap); |
268 |
|
269 |
/* If the victim is not next to this trap, don't set it off. |
270 |
* players shouldn't get hit by firing arrows at a door for example. |
271 |
* At the same time, the trap will stick around until detonated |
272 |
*/ |
273 |
get_rangevector (env, victim, &rv, 0); |
274 |
if (rv.distance > 1) |
275 |
return; |
276 |
|
277 |
trap_show (trap, env); |
278 |
|
279 |
/* Only if it is a spell do we proceed here */ |
280 |
if ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->clone.type == SPELL)) |
281 |
{ |
282 |
|
283 |
if (trap->destroyed ()) |
284 |
return; |
285 |
|
286 |
// breaks summon golem spells, which, for inexplicable reasons, |
287 |
// do not work like summon golem spells at all but still require |
288 |
// direction "0" to work at all. |
289 |
//if (trap->direction) |
290 |
// rv.direction = trap->direction; |
291 |
|
292 |
for (i = 0; i < MAX (1, trap->stats.maxhp); i++) |
293 |
{ |
294 |
if (trap->inv) |
295 |
cast_spell (env, trap, trap->direction, trap->inv, NULL); |
296 |
else |
297 |
{ |
298 |
object *spell = arch_to_object (trap->other_arch); |
299 |
|
300 |
cast_spell (env, trap, trap->direction, spell, NULL); |
301 |
spell->destroy (); |
302 |
} |
303 |
} |
304 |
} |
305 |
else |
306 |
{ |
307 |
rune_attack (trap, victim); |
308 |
if (trap->destroyed ()) |
309 |
return; |
310 |
} |
311 |
|
312 |
if (trap->stats.hp <= 0) |
313 |
{ |
314 |
trap->type = SIGN; /* make the trap impotent */ |
315 |
trap->stats.food = 20; /* make it stick around until its spells are gone */ |
316 |
SET_FLAG (trap, FLAG_IS_USED_UP); |
317 |
} |
318 |
} |
319 |
|
320 |
/* dispel_rune: by peterm |
321 |
* dispels the target rune, depending on the level of the actor |
322 |
* and the level of the rune risk flag, if true, means that there is |
323 |
* a chance that the trap/rune will detonate |
324 |
*/ |
325 |
int |
326 |
dispel_rune (object *op, object *caster, object *spell, object *skill, int dir) |
327 |
{ |
328 |
object *tmp, *tmp2; |
329 |
int searchflag = 1, mflags; |
330 |
sint16 x, y; |
331 |
maptile *m; |
332 |
|
333 |
x = op->x + freearr_x[dir]; |
334 |
y = op->y + freearr_y[dir]; |
335 |
m = op->map; |
336 |
|
337 |
mflags = get_map_flags (m, &m, x, y, &x, &y); |
338 |
|
339 |
/* Should we perhaps not allow player to disable traps if a monster/ |
340 |
* player is standing on top? |
341 |
*/ |
342 |
if (mflags & P_OUT_OF_MAP) |
343 |
{ |
344 |
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!"); |
345 |
return 0; |
346 |
} |
347 |
|
348 |
for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) |
349 |
{ |
350 |
if (tmp->type == RUNE || tmp->type == TRAP) |
351 |
break; |
352 |
|
353 |
/* we could put a probability chance here, but since nothing happens |
354 |
* if you fail, no point on that. I suppose we could do a level |
355 |
* comparison so low level players can't erase high level players runes. |
356 |
*/ |
357 |
if (tmp->type == SIGN && !strcmp (tmp->arch->name, "rune_mark")) |
358 |
{ |
359 |
tmp->destroy (); |
360 |
new_draw_info (NDI_UNIQUE, 0, op, "You wipe out the rune of marking!"); |
361 |
return 1; |
362 |
} |
363 |
|
364 |
/* now search tmp's inventory for traps |
365 |
* This is for chests, where the rune is in the chests inventory. |
366 |
*/ |
367 |
for (tmp2 = tmp->inv; tmp2 != NULL; tmp2 = tmp2->below) |
368 |
{ |
369 |
if (tmp2->type == RUNE || tmp2->type == TRAP) |
370 |
{ |
371 |
tmp = tmp2; |
372 |
searchflag = 0; |
373 |
break; |
374 |
} |
375 |
} |
376 |
if (!searchflag) |
377 |
break; |
378 |
} |
379 |
|
380 |
/* no rune there. */ |
381 |
if (tmp == NULL) |
382 |
{ |
383 |
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!"); |
384 |
return 0; |
385 |
} |
386 |
trap_disarm (op, tmp, 0, skill); |
387 |
return 1; |
388 |
|
389 |
} |
390 |
|
391 |
int |
392 |
trap_see (object *op, object *trap) |
393 |
{ |
394 |
int chance; |
395 |
|
396 |
chance = random_roll (0, 99, op, PREFER_HIGH);; |
397 |
|
398 |
/* decide if we see the rune or not */ |
399 |
if ((trap->stats.Cha == 1) || (chance > MIN (95, MAX (5, ((int) ((float) (op->map->difficulty |
400 |
+ trap->level + trap->stats.Cha - op->level) / 10.0 * 50.0)))))) |
401 |
{ |
402 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You spot a %s!", &trap->name); |
403 |
return 1; |
404 |
} |
405 |
return 0; |
406 |
} |
407 |
|
408 |
int |
409 |
trap_show (object *trap, object *where) |
410 |
{ |
411 |
if (where == NULL) |
412 |
return 0; |
413 |
|
414 |
object *tmp = get_archetype ("runedet"); |
415 |
tmp->face = GET_ANIMATION (trap, 0); |
416 |
tmp->insert_at (where, 0); |
417 |
|
418 |
return 1; |
419 |
} |
420 |
|
421 |
int |
422 |
trap_disarm (object *disarmer, object *trap, int risk, object *skill) |
423 |
{ |
424 |
int trapworth; /* need to compute the experience worth of the trap |
425 |
before we kill it */ |
426 |
|
427 |
/* this formula awards a more reasonable amount of exp */ |
428 |
trapworth = MAX (1, trap->level) * disarmer->map->difficulty * |
429 |
sqr (MAX (trap->stats.dam, trap->inv ? trap->inv->level : 1)) / skill->level; |
430 |
|
431 |
if (!(random_roll (0, (MAX (2, MIN (20, trap->level - skill->level + 5 - disarmer->stats.Dex / 2)) - 1), disarmer, PREFER_LOW))) |
432 |
{ |
433 |
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You successfully disarm the %s!", &trap->name); |
434 |
trap->destroy (1); |
435 |
|
436 |
/* If it is your own trap, (or any players trap), don't you don't |
437 |
* get exp for it. |
438 |
*/ |
439 |
if (trap->owner && trap->owner->type != PLAYER && risk) |
440 |
return trapworth; |
441 |
else |
442 |
return 1; /* give minimal exp and say success */ |
443 |
} |
444 |
else |
445 |
{ |
446 |
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You fail to disarm the %s.", &trap->name); |
447 |
if (!(random_roll (0, (MAX (2, skill->level - trap->level + disarmer->stats.Dex / 2 - 6)) - 1, disarmer, PREFER_LOW)) && risk) |
448 |
{ |
449 |
new_draw_info (NDI_UNIQUE, 0, disarmer, "In fact, you set it off!"); |
450 |
spring_trap (trap, disarmer); |
451 |
} |
452 |
return 0; |
453 |
} |
454 |
} |
455 |
|
456 |
|
457 |
/* traps need to be adjusted for the difficulty of the map. The |
458 |
* default traps are too strong for wimpy level 1 players, and |
459 |
* unthreatening to anyone of high level |
460 |
*/ |
461 |
|
462 |
void |
463 |
trap_adjust (object *trap, int difficulty) |
464 |
{ |
465 |
int i; |
466 |
|
467 |
/* now we set the trap level to match the difficulty of the level |
468 |
* the formula below will give a level from 1 to (2*difficulty) with |
469 |
* a peak probability at difficulty |
470 |
*/ |
471 |
|
472 |
trap->level = rndm (0, difficulty - 1) + rndm (0, difficulty - 1); |
473 |
if (trap->level < 1) |
474 |
trap->level = 1; |
475 |
|
476 |
/* set the hiddenness of the trap, similar formula to above */ |
477 |
trap->stats.Cha = rndm (0, 19) + rndm (0, difficulty - 1) + rndm (0, difficulty - 1); |
478 |
|
479 |
if (!trap->other_arch && !trap->inv) |
480 |
{ |
481 |
/* set the damage of the trap. |
482 |
* we get 0-4 pts of damage per level of difficulty of the map in |
483 |
* the trap |
484 |
*/ |
485 |
|
486 |
trap->stats.dam = 0; |
487 |
for (i = 0; i < difficulty; i++) |
488 |
trap->stats.dam += rndm (0, 4); |
489 |
|
490 |
/* the poison trap special case */ |
491 |
if (trap->attacktype & AT_POISON) |
492 |
{ |
493 |
trap->stats.dam = rndm (0, difficulty - 1); |
494 |
if (trap->stats.dam < 1) |
495 |
trap->stats.dam = 1; |
496 |
} |
497 |
|
498 |
/* so we get an appropriate amnt of exp for AT_DEATH traps */ |
499 |
if (trap->attacktype & AT_DEATH) |
500 |
trap->stats.dam = 127; |
501 |
} |
502 |
|
503 |
} |