1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2018 Marc Alexander Lehmann / the Deliantra team |
5 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
6 |
* Copyright (©) 2003 Mark Wedel & Crossfire Development Team |
7 |
* Copyright (©) 1992 Frank Tore Johansen |
8 |
* |
9 |
* Deliantra is free software: you can redistribute it and/or modify it under |
10 |
* the terms of the Affero GNU General Public License as published by the |
11 |
* Free Software Foundation, either version 3 of the License, or (at your |
12 |
* option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the Affero GNU General Public License |
20 |
* and the GNU General Public License along with this program. If not, see |
21 |
* <http://www.gnu.org/licenses/>. |
22 |
* |
23 |
* The authors can be reached via e-mail to <support@deliantra.net> |
24 |
*/ |
25 |
|
26 |
#include <global.h> |
27 |
#include <sproto.h> |
28 |
#include <spells.h> |
29 |
|
30 |
#ifndef sqr |
31 |
# define sqr(x) ((x)*(x)) |
32 |
#endif |
33 |
|
34 |
/* peterm: |
35 |
* write_rune: |
36 |
* op: rune writer |
37 |
* skop: skill object used for casting this rune |
38 |
* dir: orientation of rune, direction rune's contained spell will |
39 |
* be cast in, if applicable |
40 |
* spell: spell object to put into the 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 |
int |
46 |
write_rune (object *op, object *caster, object *spell, int dir, const char *runename) |
47 |
{ |
48 |
object *tmp, *rune_spell, *rune; |
49 |
|
50 |
if (!dir) |
51 |
dir = 1; |
52 |
|
53 |
mapxy pos (op); |
54 |
pos.move (dir); |
55 |
|
56 |
if (!pos.normalise () || pos.ms ().flags () & (P_SAFE | P_NO_MAGIC | P_NO_CLERIC)) |
57 |
{ |
58 |
new_draw_info (NDI_UNIQUE, 0, op, "Your rune doesn't stick! H<You cannot place a rune on safe ground or outside the map.>"); |
59 |
return 0; |
60 |
} |
61 |
|
62 |
if (pos->blocks (op)) |
63 |
{ |
64 |
new_draw_info (NDI_UNIQUE, 0, op, "You cannot reach there! H<You cannot place a rune where you can't go.>"); |
65 |
return 0; |
66 |
} |
67 |
|
68 |
for (tmp = pos->bot; tmp; tmp = tmp->above) |
69 |
if (tmp->type == RUNE) |
70 |
break; |
71 |
|
72 |
if (tmp) |
73 |
{ |
74 |
new_draw_info (NDI_UNIQUE, 0, op, "You can't write a rune on top of another rune."); |
75 |
return 0; |
76 |
} |
77 |
|
78 |
if (spell->other_arch) |
79 |
rune_spell = spell->other_arch->instance (); |
80 |
else |
81 |
{ |
82 |
/* Player specified spell. The player has to know the spell, so |
83 |
* lets just look through the players inventory see if they know it |
84 |
* use the item_matched_string for our typical matching method. |
85 |
*/ |
86 |
int bestmatch = 0, ms; |
87 |
|
88 |
if (!runename || *runename == 0) |
89 |
{ |
90 |
new_draw_info (NDI_UNIQUE, 0, op, "Write a rune of what?"); |
91 |
return 0; |
92 |
} |
93 |
|
94 |
rune_spell = NULL; |
95 |
for (tmp = op->inv; tmp; tmp = tmp->below) |
96 |
if (tmp->type == SPELL) |
97 |
{ |
98 |
ms = item_matched_string (op, tmp, runename); |
99 |
if (ms > bestmatch) |
100 |
{ |
101 |
bestmatch = ms; |
102 |
rune_spell = tmp; |
103 |
} |
104 |
} |
105 |
|
106 |
if (!rune_spell) |
107 |
{ |
108 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You don't know any spell named %s.", runename); |
109 |
return 0; |
110 |
} |
111 |
|
112 |
if (rune_spell->skill != spell->skill) |
113 |
{ |
114 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You can't cast %s with %s. H<The spell must be of the %s spell school.>", &rune_spell->name, &spell->name, &spell->skill); |
115 |
return 0; |
116 |
} |
117 |
|
118 |
if (caster->path_denied & spell->path_attuned && !caster->flag [FLAG_WIZCAST]) |
119 |
{ |
120 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s belongs to a spell path denied to you.", &rune_spell->name); |
121 |
return 0; |
122 |
} |
123 |
|
124 |
if (casting_level (caster, rune_spell) < rune_spell->level && !caster->flag [FLAG_WIZCAST]) |
125 |
{ |
126 |
new_draw_info_format (NDI_UNIQUE, 0, op, "%s is beyond your ability to cast! H<Your effetcive casting level is too low.>", &rune_spell->name); |
127 |
return 0; |
128 |
} |
129 |
|
130 |
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA) > op->stats.sp) |
131 |
{ |
132 |
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough mana."); |
133 |
return 0; |
134 |
} |
135 |
|
136 |
if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE) > op->stats.grace) |
137 |
{ |
138 |
new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough grace."); |
139 |
return 0; |
140 |
} |
141 |
|
142 |
op->stats.grace -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE); |
143 |
op->stats.sp -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA); |
144 |
} |
145 |
|
146 |
/* already proper rune. Note this should only be the case if other_arch was set */ |
147 |
if (rune_spell->type == RUNE) |
148 |
rune = rune_spell; |
149 |
else |
150 |
{ |
151 |
rune = archetype::get (GENERIC_RUNE); |
152 |
rune->msg = format ("You set off a rune of %s\n", &rune_spell->name); |
153 |
rune->insert (rune_spell->clone ()); |
154 |
|
155 |
if (spell->face != blank_face) |
156 |
rune->face = spell->face; |
157 |
} |
158 |
|
159 |
rune->level = casting_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 |
pos.insert (rune, 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 (0, 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_CHANGE); |
200 |
} |
201 |
|
202 |
/* peterm: rune_attack |
203 |
* function handles those runes which detonate but do not cast spells. |
204 |
*/ |
205 |
static void |
206 |
rune_attack (object *op, object *victim) |
207 |
{ |
208 |
if (victim) |
209 |
{ |
210 |
hit_player (victim, op->stats.dam, op, op->attacktype, 1); |
211 |
|
212 |
if (victim->destroyed ()) |
213 |
return; |
214 |
|
215 |
/* if there's a disease in the needle, put it in the player */ |
216 |
if (op->has_random_items ()) |
217 |
create_treasure (op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0); |
218 |
|
219 |
if (op->inv && op->inv->type == DISEASE) |
220 |
{ |
221 |
object *disease = op->inv; |
222 |
|
223 |
infect_object (victim, disease, 1); |
224 |
disease->destroy (); |
225 |
} |
226 |
} |
227 |
else |
228 |
hit_map (op, 0, op->attacktype, 1); |
229 |
} |
230 |
|
231 |
/* This function generalizes attacks by runes/traps. This ought to make |
232 |
* it possible for runes to attack from the inventory, |
233 |
* it'll spring the trap on the victim. |
234 |
*/ |
235 |
void |
236 |
spring_trap (object *trap, object *victim) |
237 |
{ |
238 |
object *env; |
239 |
rv_vector rv; |
240 |
int i; |
241 |
|
242 |
/* Prevent recursion */ |
243 |
if (trap->stats.hp <= 0) |
244 |
return; |
245 |
|
246 |
if (trap->flag [FLAG_IS_LINKED]) |
247 |
use_trigger (trap, victim); |
248 |
|
249 |
/* Only living objects can trigger runes that don't cast spells, as |
250 |
* doing direct damage to a non-living object doesn't work anyway. |
251 |
* Typical example is an arrow attacking a door. |
252 |
*/ |
253 |
if (!victim->flag [FLAG_ALIVE] && !trap->inv && !trap->other_arch) |
254 |
return; |
255 |
|
256 |
trap->stats.hp--; /*decrement detcount */ |
257 |
|
258 |
if (victim && victim->type == PLAYER) |
259 |
new_draw_info (NDI_UNIQUE, 0, victim, trap->msg); |
260 |
|
261 |
/* Flash an image of the trap on the map so the poor sod |
262 |
* knows what hit him. |
263 |
*/ |
264 |
env = trap->outer_env_or_self (); |
265 |
|
266 |
/* If the victim is not next to this trap, don't set it off. |
267 |
* players shouldn't get hit by firing arrows at a door for example. |
268 |
* At the same time, the trap will stick around until detonated |
269 |
*/ |
270 |
get_rangevector (env, victim, &rv, 0); |
271 |
if (rv.distance > 1) |
272 |
return; |
273 |
|
274 |
env->play_sound (trap->sound ? trap->sound : sound_find ("trap_spring")); |
275 |
|
276 |
trap_show (trap, env); |
277 |
|
278 |
if (victim->type == PLAYER) // only count players as enemies |
279 |
trap->enemy = victim; // set the victim as the traps enemy, so that summoned |
280 |
// creatures know who to attack. |
281 |
|
282 |
/* Only if it is a spell do we proceed here */ |
283 |
if ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->type == SPELL)) |
284 |
{ |
285 |
if (trap->destroyed ()) |
286 |
return; |
287 |
|
288 |
// breaks summon golem spells, which, for inexplicable reasons, |
289 |
// do not work like summon golem spells at all but still require |
290 |
// direction "0" to work at all. |
291 |
//if (trap->direction) |
292 |
// rv.direction = trap->direction; |
293 |
|
294 |
for (i = 0; i < max (1, trap->stats.maxhp); i++) |
295 |
{ |
296 |
if (trap->inv) |
297 |
cast_spell (env, trap, trap->direction, trap->inv, NULL); |
298 |
else |
299 |
{ |
300 |
object *spell = trap->other_arch->instance (); |
301 |
|
302 |
cast_spell (env, trap, trap->direction, spell, NULL); |
303 |
spell->destroy (); |
304 |
} |
305 |
} |
306 |
} |
307 |
else |
308 |
{ |
309 |
rune_attack (trap, victim); |
310 |
if (trap->destroyed ()) |
311 |
return; |
312 |
} |
313 |
|
314 |
if (trap->stats.hp <= 0) |
315 |
{ |
316 |
trap->type = SIGN; /* make the trap impotent */ |
317 |
trap->stats.food = 20; /* make it stick around until its spells are gone */ |
318 |
trap->set_flag (FLAG_IS_USED_UP); |
319 |
} |
320 |
} |
321 |
|
322 |
/* dispel_rune: by peterm |
323 |
* dispels the target rune, depending on the level of the actor |
324 |
* and the level of the rune risk flag, if true, means that there is |
325 |
* a chance that the trap/rune will detonate |
326 |
*/ |
327 |
int |
328 |
dispel_rune (object *op, object *caster, object *spell, object *skill, int dir) |
329 |
{ |
330 |
object *tmp, *tmp2; |
331 |
int searchflag = 1, mflags; |
332 |
sint16 x, y; |
333 |
maptile *m; |
334 |
|
335 |
x = op->x + DIRX (dir); |
336 |
y = op->y + DIRY (dir); |
337 |
m = op->map; |
338 |
|
339 |
mflags = get_map_flags (m, &m, x, y, &x, &y); |
340 |
|
341 |
/* Should we perhaps not allow player to disable traps if a monster/ |
342 |
* player is standing on top? |
343 |
*/ |
344 |
if (mflags & P_OUT_OF_MAP) |
345 |
{ |
346 |
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!"); |
347 |
return 0; |
348 |
} |
349 |
|
350 |
for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above) |
351 |
{ |
352 |
if (tmp->type == RUNE || tmp->type == TRAP) |
353 |
break; |
354 |
|
355 |
/* we could put a probability chance here, but since nothing happens |
356 |
* if you fail, no point on that. I suppose we could do a level |
357 |
* comparison so low level players can't erase high level players runes. |
358 |
*/ |
359 |
if (tmp->type == SIGN && tmp->arch->archname == shstr_rune_mark) |
360 |
{ |
361 |
tmp->destroy (); |
362 |
new_draw_info (NDI_UNIQUE, 0, op, "You wipe out the rune of marking!"); |
363 |
return 1; |
364 |
} |
365 |
|
366 |
/* now search tmp's inventory for traps |
367 |
* This is for chests, where the rune is in the chests inventory. |
368 |
*/ |
369 |
for (tmp2 = tmp->inv; tmp2; tmp2 = tmp2->below) |
370 |
{ |
371 |
if (tmp2->type == RUNE || tmp2->type == TRAP) |
372 |
{ |
373 |
tmp = tmp2; |
374 |
searchflag = 0; |
375 |
break; |
376 |
} |
377 |
} |
378 |
|
379 |
if (!searchflag) |
380 |
break; |
381 |
} |
382 |
|
383 |
/* no rune there. */ |
384 |
if (tmp == NULL) |
385 |
{ |
386 |
new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!"); |
387 |
return 0; |
388 |
} |
389 |
|
390 |
trap_disarm (op, tmp, 0, skill); |
391 |
return 1; |
392 |
} |
393 |
|
394 |
int |
395 |
trap_see (object *op, object *trap) |
396 |
{ |
397 |
int chance = random_roll (0, 99, op, PREFER_HIGH); |
398 |
|
399 |
/* decide if we see the rune or not */ |
400 |
if ((trap->stats.Cha == 1) || (chance > min (95, max (5, ((int) ((float) (op->map->difficulty |
401 |
+ trap->level + trap->stats.Cha - op->level) / 10.0 * 50.0)))))) |
402 |
{ |
403 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You spot a %s!", &trap->name); |
404 |
return 1; |
405 |
} |
406 |
|
407 |
return 0; |
408 |
} |
409 |
|
410 |
int |
411 |
trap_show (object *trap, object *where) |
412 |
{ |
413 |
if (where == NULL) |
414 |
return 0; |
415 |
|
416 |
object *tmp = archetype::get (shstr_runedet); |
417 |
tmp->face = trap->get_anim_frame (0); |
418 |
tmp->insert_at (where, 0); |
419 |
|
420 |
return 1; |
421 |
} |
422 |
|
423 |
int |
424 |
trap_disarm (object *disarmer, object *trap, int risk, object *skill) |
425 |
{ |
426 |
int trapworth; /* need to compute the experience worth of the trap before we kill it */ |
427 |
|
428 |
/* this formula awards a more reasonable amount of exp */ |
429 |
trapworth = max (1, trap->level) |
430 |
* disarmer->map->difficulty |
431 |
* sqr (max (trap->stats.dam, trap->inv ? trap->inv->level : 1)) |
432 |
/ skill->level; |
433 |
|
434 |
if (!(random_roll (0, (max (2, min (20, trap->level - skill->level + 5 - disarmer->stats.Dex / 2)) - 1), disarmer, PREFER_LOW))) |
435 |
{ |
436 |
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You successfully disarm the %s!", &trap->name); |
437 |
trap->destroy (); |
438 |
|
439 |
/* If it is your own trap, (or any players trap), don't you don't |
440 |
* get exp for it. |
441 |
*/ |
442 |
if (trap->owner && trap->owner->type != PLAYER && risk) |
443 |
return trapworth; |
444 |
else |
445 |
return 1; /* give minimal exp and say success */ |
446 |
} |
447 |
else |
448 |
{ |
449 |
new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You fail to disarm the %s.", &trap->name); |
450 |
if (!(random_roll (0, (max (2, skill->level - trap->level + disarmer->stats.Dex / 2 - 6)) - 1, disarmer, PREFER_LOW)) && risk) |
451 |
{ |
452 |
new_draw_info (NDI_UNIQUE, 0, disarmer, "In fact, you set it off!"); |
453 |
spring_trap (trap, disarmer); |
454 |
} |
455 |
return 0; |
456 |
} |
457 |
} |
458 |
|
459 |
/* traps need to be adjusted for the difficulty of the map. The |
460 |
* default traps are too strong for wimpy level 1 players, and |
461 |
* unthreatening to anyone of high level |
462 |
*/ |
463 |
void |
464 |
trap_adjust (object *trap, int difficulty) |
465 |
{ |
466 |
int i; |
467 |
|
468 |
/* now we set the trap level to match the difficulty of the level |
469 |
* the formula below will give a level from 1 to (2*difficulty) with |
470 |
* a peak probability at difficulty |
471 |
*/ |
472 |
|
473 |
trap->level = rndm (0, difficulty - 1) + rndm (0, difficulty - 1); |
474 |
if (trap->level < 1) |
475 |
trap->level = 1; |
476 |
|
477 |
/* set the hiddenness of the trap, similar formula to above */ |
478 |
trap->stats.Cha = rndm (0, 19) + rndm (0, difficulty - 1) + rndm (0, difficulty - 1); |
479 |
|
480 |
if (!trap->other_arch && !trap->inv) |
481 |
{ |
482 |
/* set the damage of the trap. |
483 |
* we get 0-4 pts of damage per level of difficulty of the map in |
484 |
* the trap |
485 |
*/ |
486 |
|
487 |
trap->stats.dam = 0; |
488 |
for (i = 0; i < difficulty; i++) |
489 |
trap->stats.dam += rndm (0, 4); |
490 |
|
491 |
/* the poison trap special case */ |
492 |
if (trap->attacktype & AT_POISON) |
493 |
{ |
494 |
trap->stats.dam = rndm (0, difficulty - 1); |
495 |
if (trap->stats.dam < 1) |
496 |
trap->stats.dam = 1; |
497 |
} |
498 |
|
499 |
/* so we get an appropriate amnt of exp for AT_DEATH traps */ |
500 |
if (trap->attacktype & AT_DEATH) |
501 |
trap->stats.dam = 127; |
502 |
} |
503 |
|
504 |
} |