ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/rune.C
Revision: 1.17
Committed: Mon Jan 15 21:06:20 2007 UTC (17 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.16: +22 -22 lines
Log Message:
comments

File Contents

# User Rev Content
1 elmex 1.1 /*
2 pippijn 1.17 * 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 elmex 1.1
25     #include <global.h>
26 root 1.14 #include <sproto.h>
27 elmex 1.1 #include <spells.h>
28    
29     #ifndef sqr
30 root 1.4 # define sqr(x) ((x)*(x))
31 elmex 1.1 #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 root 1.4 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 root 1.7 maptile *m;
52 root 1.4 sint16 nx, ny;
53    
54     if (!dir)
55 root 1.10 dir = 1;
56 root 1.4
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))
62     {
63     new_draw_info (NDI_UNIQUE, 0, op, "Can't make a rune there!");
64     return 0;
65 elmex 1.1 }
66 root 1.10
67 root 1.13 for (tmp = GET_MAP_OB (m, nx, ny); tmp != NULL; tmp = tmp->above)
68 root 1.4 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 there.");
74 elmex 1.1 return 0;
75     }
76    
77 root 1.4 if (spell->other_arch)
78 root 1.10 rune_spell = arch_to_object (spell->other_arch);
79 root 1.4 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     {
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 root 1.2 }
104     }
105     }
106 root 1.10
107 root 1.4 if (!rune_spell)
108     {
109     new_draw_info_format (NDI_UNIQUE, 0, op, "You don't know any spell named %s", runename);
110     return 0;
111     }
112 root 1.10
113 root 1.4 if (rune_spell->skill != spell->skill)
114     {
115     new_draw_info_format (NDI_UNIQUE, 0, op, "You can't cast %s with %s", &rune_spell->name, &spell->name);
116     return 0;
117     }
118 root 1.10
119 root 1.4 if (caster->path_denied & spell->path_attuned)
120     {
121     new_draw_info_format (NDI_UNIQUE, 0, op, "%s belongs to a spell path denied to you.", &rune_spell->name);
122     return 0;
123     }
124 root 1.10
125 root 1.4 if (caster_level (caster, rune_spell) < rune_spell->level)
126     {
127     new_draw_info_format (NDI_UNIQUE, 0, op, "%s is beyond your ability to cast!", &rune_spell->name);
128     return 0;
129     }
130 root 1.10
131 root 1.4 if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA) > op->stats.sp)
132     {
133     new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough mana.");
134     return 0;
135     }
136 root 1.10
137 root 1.4 if (SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE) > op->stats.grace)
138     {
139     new_draw_info (NDI_UNIQUE, 0, op, "You don't have enough grace.");
140     return 0;
141     }
142 root 1.10
143 root 1.4 op->stats.grace -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_GRACE);
144     op->stats.sp -= SP_level_spellpoint_cost (caster, rune_spell, SPELL_MANA);
145     }
146 root 1.10
147 root 1.4 /* already proper rune. Note this should only be the case if other_arch was set */
148     if (rune_spell->type == RUNE)
149 root 1.15 rune = rune_spell;
150 root 1.4 else
151     {
152     rune = get_archetype (GENERIC_RUNE);
153     sprintf (buf, "You set off a rune of %s\n", &rune_spell->name);
154     rune->msg = buf;
155 root 1.15 rune->insert (rune_spell->clone ());
156 root 1.10
157 root 1.4 if (spell->face != blank_face)
158     rune->face = spell->face;
159     }
160 root 1.10
161 root 1.4 rune->level = caster_level (caster, spell);
162     rune->stats.Cha = rune->level / 2; /* the invisibility parameter */
163     rune->direction = dir; /* where any spell will go upon detonation */
164 root 1.11 rune->set_owner (op); /* runes without need no owner */
165 root 1.4 set_spell_skill (op, caster, spell, rune);
166 root 1.15
167     m->insert (rune, nx, ny, op);
168 root 1.4 return 1;
169 elmex 1.1 }
170    
171     /* move_rune: peterm
172     comments on runes:
173     rune->level : level at which rune will cast its spell.
174     rune->hp : number of detonations before rune goes away
175     rune->msg : message the rune displays when it goes off
176     rune->direction : direction it will cast a spell in
177     rune->dam : damage the rune will do if it doesn't cast spells
178     rune->attacktype: type of damage it does, if not casting spells
179     rune->other_arch: spell in the rune
180     rune->Cha : how hidden the rune is
181     rune->maxhp : number of spells the rune casts
182     */
183 root 1.4 void
184     move_rune (object *op)
185     {
186     int det = 0;
187    
188     if (!op->level)
189     {
190     return;
191     } /* runes of level zero cannot detonate. */
192     det = op->invisible;
193     if (!(rndm (0, MAX (1, (op->stats.Cha)) - 1)))
194     {
195     op->invisible = 0;
196     op->speed_left -= 1;
197     }
198     else
199     op->invisible = 1;
200     if (op->invisible != det)
201     update_object (op, UP_OBJ_FACE);
202 elmex 1.1 }
203    
204    
205     /* peterm: rune_attack
206     * function handles those runes which detonate but do not cast spells.
207     */
208    
209    
210 root 1.4 void
211     rune_attack (object *op, object *victim)
212 elmex 1.1 {
213 root 1.4 if (victim)
214     {
215 root 1.6 hit_player (victim, op->stats.dam, op, op->attacktype, 1);
216 root 1.4
217 root 1.6 if (victim->destroyed ())
218 root 1.4 return;
219 root 1.6
220 root 1.4 /* if there's a disease in the needle, put it in the player */
221 root 1.12 if (op->has_random_items ())
222 root 1.4 create_treasure (op->randomitems, op, 0, (victim->map ? victim->map->difficulty : 1), 0);
223 root 1.6
224 root 1.4 if (op->inv && op->inv->type == DISEASE)
225     {
226     object *disease = op->inv;
227    
228     infect_object (victim, disease, 1);
229 root 1.9 disease->destroy ();
230 root 1.4 }
231 elmex 1.1 }
232 root 1.4 else
233     hit_map (op, 0, op->attacktype, 1);
234 elmex 1.1 }
235    
236     /* This function generalizes attacks by runes/traps. This ought to make
237     * it possible for runes to attack from the inventory,
238     * it'll spring the trap on the victim.
239     */
240 root 1.4 void
241     spring_trap (object *trap, object *victim)
242 elmex 1.1 {
243 root 1.4 object *env;
244     rv_vector rv;
245     int i;
246    
247     /* Prevent recursion */
248     if (trap->stats.hp <= 0)
249     return;
250    
251     if (QUERY_FLAG (trap, FLAG_IS_LINKED))
252     use_trigger (trap);
253    
254     /* Only living objects can trigger runes that don't cast spells, as
255     * doing direct damage to a non-living object doesn't work anyway.
256     * Typical example is an arrow attacking a door.
257     */
258     if (!QUERY_FLAG (victim, FLAG_ALIVE) && !trap->inv && !trap->other_arch)
259     return;
260    
261     trap->stats.hp--; /*decrement detcount */
262    
263     if (victim && victim->type == PLAYER)
264     new_draw_info (NDI_UNIQUE, 0, victim, trap->msg);
265    
266     /* Flash an image of the trap on the map so the poor sod
267     * knows what hit him.
268     */
269     env = object_get_env_recursive (trap);
270    
271     /* If the victim is not next to this trap, don't set it off.
272     * players shouldn't get hit by firing arrows at a door for example.
273     * At the same time, the trap will stick around until detonated
274     */
275     get_rangevector (env, victim, &rv, 0);
276     if (rv.distance > 1)
277     return;
278    
279     trap_show (trap, env);
280    
281     /* Only if it is a spell do we proceed here */
282     if ((trap->inv && trap->inv->type == SPELL) || (trap->other_arch && trap->other_arch->clone.type == SPELL))
283     {
284 elmex 1.1
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.4 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 = arch_to_object (trap->other_arch);
301 elmex 1.1
302 root 1.4 cast_spell (env, trap, trap->direction, spell, NULL);
303 root 1.9 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     SET_FLAG (trap, FLAG_IS_USED_UP);
319 elmex 1.1 }
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 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     x = op->x + freearr_x[dir];
336     y = op->y + freearr_y[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 root 1.13 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; 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     if (tmp->type == SIGN && !strcmp (tmp->arch->name, "rune_mark"))
360     {
361 root 1.9 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     for (tmp2 = tmp->inv; tmp2 != NULL; tmp2 = tmp2->below)
370     {
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.4 if (!searchflag)
379     break;
380 elmex 1.1 }
381 root 1.4
382     /* no rune there. */
383     if (tmp == NULL)
384     {
385     new_draw_info (NDI_UNIQUE, 0, op, "There's nothing there!");
386     return 0;
387     }
388     trap_disarm (op, tmp, 0, skill);
389     return 1;
390    
391 elmex 1.1 }
392    
393 root 1.4 int
394     trap_see (object *op, object *trap)
395     {
396     int chance;
397    
398     chance = random_roll (0, 99, op, PREFER_HIGH);;
399 elmex 1.1
400 root 1.4 /* decide if we see the rune or not */
401     if ((trap->stats.Cha == 1) || (chance > MIN (95, MAX (5, ((int) ((float) (op->map->difficulty
402     + trap->level + trap->stats.Cha - op->level) / 10.0 * 50.0))))))
403     {
404     new_draw_info_format (NDI_UNIQUE, 0, op, "You spot a %s!", &trap->name);
405     return 1;
406 elmex 1.1 }
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     object *tmp = get_archetype ("runedet");
417     tmp->face = &new_faces[GET_ANIMATION (trap, 0)];
418     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     int trapworth; /* need to compute the experience worth of the trap
427     before we kill it */
428    
429     /* this formula awards a more reasonable amount of exp */
430     trapworth = MAX (1, trap->level) * disarmer->map->difficulty *
431     sqr (MAX (trap->stats.dam, trap->inv ? trap->inv->level : 1)) / skill->level;
432    
433     if (!(random_roll (0, (MAX (2, MIN (20, trap->level - skill->level + 5 - disarmer->stats.Dex / 2)) - 1), disarmer, PREFER_LOW)))
434     {
435     new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You successfully disarm the %s!", &trap->name);
436 root 1.9 trap->destroy (1);
437    
438 root 1.4 /* If it is your own trap, (or any players trap), don't you don't
439     * get exp for it.
440     */
441     if (trap->owner && trap->owner->type != PLAYER && risk)
442     return trapworth;
443     else
444     return 1; /* give minimal exp and say success */
445     }
446     else
447     {
448     new_draw_info_format (NDI_UNIQUE, 0, disarmer, "You fail to disarm the %s.", &trap->name);
449     if (!(random_roll (0, (MAX (2, skill->level - trap->level + disarmer->stats.Dex / 2 - 6)) - 1, disarmer, PREFER_LOW)) && risk)
450     {
451     new_draw_info (NDI_UNIQUE, 0, disarmer, "In fact, you set it off!");
452     spring_trap (trap, disarmer);
453 elmex 1.1 }
454 root 1.4 return 0;
455     }
456 elmex 1.1 }
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    
464 root 1.4 void
465     trap_adjust (object *trap, int difficulty)
466     {
467     int i;
468 elmex 1.1
469 root 1.4 /* now we set the trap level to match the difficulty of the level
470     * the formula below will give a level from 1 to (2*difficulty) with
471     * a peak probability at difficulty
472     */
473    
474     trap->level = rndm (0, difficulty - 1) + rndm (0, difficulty - 1);
475     if (trap->level < 1)
476     trap->level = 1;
477    
478     /* set the hiddenness of the trap, similar formula to above */
479     trap->stats.Cha = rndm (0, 19) + rndm (0, difficulty - 1) + rndm (0, difficulty - 1);
480    
481     if (!trap->other_arch && !trap->inv)
482     {
483     /* set the damage of the trap.
484     * we get 0-4 pts of damage per level of difficulty of the map in
485     * the trap
486     */
487    
488     trap->stats.dam = 0;
489     for (i = 0; i < difficulty; i++)
490     trap->stats.dam += rndm (0, 4);
491    
492     /* the poison trap special case */
493     if (trap->attacktype & AT_POISON)
494     {
495     trap->stats.dam = rndm (0, difficulty - 1);
496     if (trap->stats.dam < 1)
497     trap->stats.dam = 1;
498 root 1.2 }
499 elmex 1.1
500 root 1.4 /* so we get an appropriate amnt of exp for AT_DEATH traps */
501     if (trap->attacktype & AT_DEATH)
502     trap->stats.dam = 127;
503 elmex 1.1 }
504    
505     }