ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/rune.C
Revision: 1.59
Committed: Wed Nov 14 20:34:58 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.58: +13 -9 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.30 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.55 *
4 root 1.59 * Copyright (©) 2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.56 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.47 * Copyright (©) 2003 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.55 *
9 root 1.43 * 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 root 1.55 *
14 root 1.28 * 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 root 1.55 *
19 root 1.43 * 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 root 1.55 *
23 root 1.30 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.17 */
25 elmex 1.1
26     #include <global.h>
27 root 1.14 #include <sproto.h>
28 elmex 1.1 #include <spells.h>
29    
30     #ifndef sqr
31 root 1.4 # define sqr(x) ((x)*(x))
32 elmex 1.1 #endif
33    
34 root 1.57 /* peterm:
35 elmex 1.1 * 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 root 1.39 * spell: spell object to put into the rune
41 elmex 1.1 * level: level of casting of the rune
42     * runename: name of the rune or message displayed by the rune for
43 root 1.57 * a rune of marking
44 elmex 1.1 */
45 root 1.4 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 root 1.10 dir = 1;
52 root 1.4
53 root 1.59 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 root 1.4
62 root 1.59 if (pos->blocks (op))
63 root 1.4 {
64 root 1.59 new_draw_info (NDI_UNIQUE, 0, op, "You cannot reach there! H<You cannot place a rune where you can't go.>");
65 root 1.4 return 0;
66 elmex 1.1 }
67 root 1.10
68 root 1.59 for (tmp = pos->bot; tmp; tmp = tmp->above)
69 root 1.4 if (tmp->type == RUNE)
70     break;
71    
72     if (tmp)
73     {
74 root 1.20 new_draw_info (NDI_UNIQUE, 0, op, "You can't write a rune on top of another rune.");
75 elmex 1.1 return 0;
76     }
77    
78 root 1.4 if (spell->other_arch)
79 root 1.46 rune_spell = spell->other_arch->instance ();
80 root 1.4 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 root 1.20 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 root 1.10
106 root 1.4 if (!rune_spell)
107     {
108 root 1.37 new_draw_info_format (NDI_UNIQUE, 0, op, "You don't know any spell named %s.", runename);
109 root 1.4 return 0;
110     }
111 root 1.10
112 root 1.4 if (rune_spell->skill != spell->skill)
113     {
114 root 1.37 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 root 1.4 return 0;
116     }
117 root 1.10
118 root 1.20 if (caster->path_denied & spell->path_attuned && !caster->flag [FLAG_WIZCAST])
119 root 1.4 {
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 root 1.10
124 root 1.34 if (casting_level (caster, rune_spell) < rune_spell->level && !caster->flag [FLAG_WIZCAST])
125 root 1.4 {
126 root 1.37 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 root 1.4 return 0;
128     }
129 root 1.10
130 root 1.4 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 root 1.10
136 root 1.4 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 root 1.10
142 root 1.4 op->stats.grace -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE);
143 root 1.39 op->stats.sp -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA);
144 root 1.4 }
145 root 1.10
146 root 1.4 /* already proper rune. Note this should only be the case if other_arch was set */
147     if (rune_spell->type == RUNE)
148 root 1.15 rune = rune_spell;
149 root 1.4 else
150     {
151 root 1.52 rune = archetype::get (GENERIC_RUNE);
152 root 1.39 rune->msg = format ("You set off a rune of %s\n", &rune_spell->name);
153 root 1.15 rune->insert (rune_spell->clone ());
154 root 1.10
155 root 1.4 if (spell->face != blank_face)
156     rune->face = spell->face;
157     }
158 root 1.10
159 root 1.34 rune->level = casting_level (caster, spell);
160 root 1.4 rune->stats.Cha = rune->level / 2; /* the invisibility parameter */
161     rune->direction = dir; /* where any spell will go upon detonation */
162 root 1.11 rune->set_owner (op); /* runes without need no owner */
163 root 1.4 set_spell_skill (op, caster, spell, rune);
164 root 1.15
165 root 1.59 pos.insert (rune, op);
166 root 1.4 return 1;
167 elmex 1.1 }
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 root 1.4 void
182     move_rune (object *op)
183     {
184 root 1.18 /* runes of level zero cannot detonate. */
185     if (!op->level)
186     return;
187    
188     int det = op->invisible;
189 root 1.4
190 root 1.53 if (!(rndm (0, max (0, op->stats.Cha - 1))))
191 root 1.4 {
192     op->invisible = 0;
193     op->speed_left -= 1;
194     }
195     else
196     op->invisible = 1;
197 root 1.18
198 root 1.4 if (op->invisible != det)
199 root 1.22 update_object (op, UP_OBJ_CHANGE);
200 elmex 1.1 }
201    
202     /* peterm: rune_attack
203 root 1.57 * function handles those runes which detonate but do not cast spells.
204 elmex 1.1 */
205 root 1.44 static void
206 root 1.4 rune_attack (object *op, object *victim)
207 elmex 1.1 {
208 root 1.4 if (victim)
209     {
210 root 1.6 hit_player (victim, op->stats.dam, op, op->attacktype, 1);
211 root 1.4
212 root 1.6 if (victim->destroyed ())
213 root 1.4 return;
214 root 1.6
215 root 1.4 /* if there's a disease in the needle, put it in the player */
216 root 1.12 if (op->has_random_items ())
217 root 1.4 create_treasure (op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0);
218 root 1.6
219 root 1.4 if (op->inv && op->inv->type == DISEASE)
220     {
221     object *disease = op->inv;
222    
223     infect_object (victim, disease, 1);
224 root 1.36 disease->destroy ();
225 root 1.4 }
226 elmex 1.1 }
227 root 1.4 else
228     hit_map (op, 0, op->attacktype, 1);
229 elmex 1.1 }
230    
231     /* This function generalizes attacks by runes/traps. This ought to make
232 root 1.57 * it possible for runes to attack from the inventory,
233 elmex 1.1 * it'll spring the trap on the victim.
234     */
235 root 1.4 void
236     spring_trap (object *trap, object *victim)
237 elmex 1.1 {
238 root 1.4 object *env;
239     rv_vector rv;
240     int i;
241    
242     /* Prevent recursion */
243     if (trap->stats.hp <= 0)
244     return;
245    
246 root 1.49 if (trap->flag [FLAG_IS_LINKED])
247 root 1.40 use_trigger (trap, victim);
248 root 1.4
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 root 1.49 if (!victim->flag [FLAG_ALIVE] && !trap->inv && !trap->other_arch)
254 root 1.4 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 root 1.57 * knows what hit him.
263 root 1.4 */
264 root 1.42 env = trap->outer_env_or_self ();
265 root 1.4
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 root 1.29 env->play_sound (trap->sound ? trap->sound : sound_find ("trap_spring"));
275    
276 root 1.4 trap_show (trap, env);
277    
278 elmex 1.23 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 root 1.4 /* Only if it is a spell do we proceed here */
283 root 1.27 if ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->type == SPELL))
284 root 1.4 {
285 root 1.6 if (trap->destroyed ())
286 root 1.2 return;
287 elmex 1.1
288 root 1.4 // 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 elmex 1.1
294 root 1.45 for (i = 0; i < max (1, trap->stats.maxhp); i++)
295 root 1.4 {
296     if (trap->inv)
297     cast_spell (env, trap, trap->direction, trap->inv, NULL);
298     else
299     {
300 root 1.46 object *spell = trap->other_arch->instance ();
301 elmex 1.1
302 root 1.4 cast_spell (env, trap, trap->direction, spell, NULL);
303 root 1.36 spell->destroy ();
304 root 1.2 }
305     }
306 root 1.4 }
307     else
308     {
309     rune_attack (trap, victim);
310 root 1.6 if (trap->destroyed ())
311 root 1.4 return;
312 elmex 1.1 }
313    
314 root 1.4 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 root 1.49 trap->set_flag (FLAG_IS_USED_UP);
319 elmex 1.1 }
320     }
321    
322 root 1.57 /* dispel_rune: by peterm
323 elmex 1.1 * 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 root 1.57 * a chance that the trap/rune will detonate
326 elmex 1.1 */
327 root 1.4 int
328     dispel_rune (object *op, object *caster, object *spell, object *skill, int dir)
329 elmex 1.1 {
330 root 1.4 object *tmp, *tmp2;
331     int searchflag = 1, mflags;
332     sint16 x, y;
333 root 1.7 maptile *m;
334 root 1.4
335 root 1.58 x = op->x + DIRX (dir);
336     y = op->y + DIRY (dir);
337 root 1.4 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 root 1.57 * player is standing on top?
343 root 1.4 */
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 root 1.39 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
351 root 1.4 {
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 root 1.38 if (tmp->type == SIGN && tmp->arch->archname == shstr_rune_mark)
360 root 1.4 {
361 root 1.36 tmp->destroy ();
362 root 1.4 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 root 1.39 for (tmp2 = tmp->inv; tmp2; tmp2 = tmp2->below)
370 root 1.4 {
371     if (tmp2->type == RUNE || tmp2->type == TRAP)
372     {
373     tmp = tmp2;
374     searchflag = 0;
375     break;
376 root 1.2 }
377     }
378 root 1.39
379 root 1.4 if (!searchflag)
380     break;
381 elmex 1.1 }
382 root 1.4
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 root 1.32
390 root 1.4 trap_disarm (op, tmp, 0, skill);
391     return 1;
392 elmex 1.1 }
393    
394 root 1.4 int
395     trap_see (object *op, object *trap)
396     {
397 root 1.50 int chance = random_roll (0, 99, op, PREFER_HIGH);
398 elmex 1.1
399 root 1.4 /* decide if we see the rune or not */
400 root 1.45 if ((trap->stats.Cha == 1) || (chance > min (95, max (5, ((int) ((float) (op->map->difficulty
401 root 1.4 + 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 elmex 1.1 }
406 root 1.32
407 root 1.4 return 0;
408 elmex 1.1 }
409    
410 root 1.4 int
411     trap_show (object *trap, object *where)
412     {
413     if (where == NULL)
414     return 0;
415 root 1.15
416 root 1.52 object *tmp = archetype::get (shstr_runedet);
417 root 1.41 tmp->face = trap->get_anim_frame (0);
418 root 1.15 tmp->insert_at (where, 0);
419    
420 root 1.4 return 1;
421 elmex 1.1 }
422    
423 root 1.4 int
424     trap_disarm (object *disarmer, object *trap, int risk, object *skill)
425     {
426 root 1.32 int trapworth; /* need to compute the experience worth of the trap before we kill it */
427 root 1.4
428     /* this formula awards a more reasonable amount of exp */
429 root 1.45 trapworth = max (1, trap->level)
430 root 1.32 * disarmer->map->difficulty
431 root 1.45 * sqr (max (trap->stats.dam, trap->inv ? trap->inv->level : 1))
432 root 1.32 / skill->level;
433 root 1.4
434 root 1.45 if (!(random_roll (0, (max (2, min (20, trap->level - skill->level + 5 - disarmer->stats.Dex / 2)) - 1), disarmer, PREFER_LOW)))
435 root 1.4 {
436     new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You successfully disarm the %s!", &trap->name);
437 root 1.36 trap->destroy ();
438 root 1.9
439 root 1.4 /* 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 root 1.45 if (!(random_roll (0, (max (2, skill->level - trap->level + disarmer->stats.Dex / 2 - 6)) - 1, disarmer, PREFER_LOW)) && risk)
451 root 1.4 {
452     new_draw_info (NDI_UNIQUE, 0, disarmer, "In fact, you set it off!");
453     spring_trap (trap, disarmer);
454 elmex 1.1 }
455 root 1.4 return 0;
456     }
457 elmex 1.1 }
458    
459     /* traps need to be adjusted for the difficulty of the map. The
460 root 1.57 * default traps are too strong for wimpy level 1 players, and
461     * unthreatening to anyone of high level
462 elmex 1.1 */
463 root 1.4 void
464     trap_adjust (object *trap, int difficulty)
465     {
466     int i;
467 elmex 1.1
468 root 1.4 /* now we set the trap level to match the difficulty of the level
469 root 1.57 * the formula below will give a level from 1 to (2*difficulty) with
470     * a peak probability at difficulty
471 root 1.4 */
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 root 1.2 }
498 elmex 1.1
499 root 1.4 /* so we get an appropriate amnt of exp for AT_DEATH traps */
500     if (trap->attacktype & AT_DEATH)
501     trap->stats.dam = 127;
502 elmex 1.1 }
503    
504     }