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