ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.74
Committed: Sun Jan 4 16:30:39 2009 UTC (15 years, 4 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.73: +24 -14 lines
Log Message:
sanatized alchemy skill.

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.59 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.26 *
4 root 1.62 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.54 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.26 *
8 root 1.59 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.58 * 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.26 *
13 root 1.58 * 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.26 *
18 root 1.58 * 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.54 *
21 root 1.59 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.26 */
23 elmex 1.1
24     /* Created July 95 to separate skill utilities from actual skills -b.t. */
25    
26     /* Reconfigured skills code to allow linking of skills to experience
27     * categories. This is done solely through the init_new_exp_system() fctn.
28     * June/July 1995 -b.t. thomas@astro.psu.edu
29     */
30    
31     /* July 1995 - Initial associated skills coding. Experience gains
32     * come solely from the use of skills. Overcoming an opponent (in combat,
33     * finding/disarming a trap, stealing from somebeing, etc) gains
34     * experience. Calc_skill_exp() handles the gained experience using
35     * modifications in the skills[] table. - b.t.
36     */
37    
38     /* define the following for skills utility debuging */
39 root 1.9
40 elmex 1.1 /* #define SKILL_UTIL_DEBUG */
41    
42     #include <global.h>
43     #include <object.h>
44 root 1.23 #include <sproto.h>
45 root 1.9 #include <living.h> /* for defs of STR,CON,DEX,etc. -b.t. */
46 elmex 1.1 #include <spells.h>
47    
48 root 1.33 const uint8_t skill_flags[NUM_SKILLS] = {
49 root 1.34 0, // SK_NONE
50 root 1.33 # define def(uc, flags) flags,
51     # include "skillinc.h"
52     # undef def
53 root 1.11 };
54    
55 root 1.9 static int attack_hth (object *pl, int dir, const char *string, object *skill);
56     static int attack_melee_weapon (object *op, int dir, const char *string, object *skill);
57 elmex 1.1
58     /* init_skills basically just sets up the skill_names table
59     * above. The index into the array is set up by the
60     * subtypes.
61     */
62 root 1.9 void
63     init_skills (void)
64     {
65 root 1.56 for_all_archetypes (at)
66     if (at->type == SKILL)
67 root 1.10 {
68 root 1.56 if (skill_names[at->subtype])
69 root 1.10 LOG (llevError, "init_skills: multiple skill using same subtype %d, %s, %s\n",
70 root 1.56 at->subtype, &skill_names[at->subtype], &at->skill);
71 root 1.10 else
72 root 1.56 skill_names[at->subtype] = at->skill;
73 root 1.10 }
74 elmex 1.1
75 root 1.9 /* This isn't really an error if there is no skill subtype set, but
76     * checking for this may catch some user errors.
77     */
78 root 1.33 for (int i = 1; i < NUM_SKILLS; i++)
79     if (!skill_names[i])
80     LOG (llevError, "init_skills: skill subtype %d doesn't have a name?\n", i);
81 elmex 1.1 }
82    
83     /* This function goes through the player inventory and sets
84     * up the last_skills[] array in the player object.
85     * the last_skills[] is used to more quickly lookup skills -
86     * mostly used for sending exp.
87     */
88 root 1.9 void
89 root 1.67 player::link_skills ()
90 elmex 1.1 {
91 root 1.67 for (int i = 0; i < NUM_SKILLS; ++i)
92     last_skill_ob [i] = 0;
93    
94     for (object *tmp = ob->inv; tmp; tmp = tmp->below)
95 root 1.25 if (tmp->type == SKILL)
96     {
97 root 1.67 assert (IN_RANGE_EXC (tmp->subtype, 0, NUM_SKILLS));
98 root 1.25
99 root 1.67 if (last_skill_ob [tmp->subtype] != tmp)
100 root 1.25 {
101 root 1.67 /* This is really a warning, hence no else below */
102     if (last_skill_ob [tmp->subtype])
103     LOG (llevError, "Multiple skills with the same subtype? %s, %s\n",
104     &last_skill_ob [tmp->subtype]->skill, &tmp->skill);
105    
106     last_skill_ob [tmp->subtype] = tmp;
107     if (ns)
108     ns->last_skill_exp [tmp->subtype] = -1;
109 root 1.25 }
110     }
111 elmex 1.1 }
112    
113 root 1.49 static object *
114 root 1.73 find_skill (object *who, shstr_cmp name)
115 root 1.49 {
116 root 1.68 if (who->chosen_skill
117     && who->chosen_skill->skill == name
118     && who->chosen_skill->type == SKILL)
119     return who->chosen_skill;
120    
121 root 1.49 for (object *tmp = who->inv; tmp; tmp = tmp->below)
122 root 1.68 if (tmp->skill == name && tmp->type == SKILL)
123     return splay (tmp);
124 root 1.49
125     return 0;
126     }
127    
128 root 1.73 object *player::find_skill (shstr_cmp name) const
129 root 1.68 {
130     // might want to use last_skill_obj at one point, or maybe not
131     return ::find_skill (ob, name);
132     }
133    
134 root 1.50 /* This returns the skill pointer of the given name (the
135     * one that accumulates exp, has the level, etc).
136     *
137     * It is presumed that the player will be needing to actually
138     * use the skill, so thus if use of the skill requires a skill
139     * tool, this code will equip it.
140     */
141 root 1.49 object *
142 root 1.73 find_skill_by_name (object *who, shstr_cmp sh)
143 root 1.49 {
144     object *skill_tool = 0;
145    
146     for (object *tmp = who->inv; tmp; tmp = tmp->below)
147     if (tmp->skill == sh)
148     {
149     if (tmp->type == SKILL && (tmp->flag [FLAG_CAN_USE_SKILL] || tmp->flag [FLAG_APPLIED]))
150     /* If this is a skill that can be used without applying tool, return it */
151 root 1.53 return splay (tmp);
152 root 1.49 /* Try to find appropriate skilltool. If the player has one already
153     * applied, we try to keep using that one.
154     */
155     else if (tmp->type == SKILL_TOOL && !skill_tool)
156     skill_tool = tmp;
157     }
158    
159     if (!skill_tool)
160     return 0;
161    
162     /* Player has a tool to use the skill. If not applied, apply it -
163     * if not successful, return null. If they do have the skill tool
164     * but not the skill itself, give it to them.
165     */
166     object *skill = find_skill (who, skill_tool->skill);
167    
168     if (!skill)
169 root 1.67 skill = give_skill_by_name (who, skill_tool->skill);
170 root 1.49
171     if (!skill_tool->flag [FLAG_APPLIED])
172 root 1.53 if (apply_special (who, splay (skill_tool), AP_APPLY | AP_NO_READY))
173 root 1.49 return 0;
174    
175 root 1.53 return splay (skill);
176 root 1.49 }
177    
178 root 1.50 object *
179 root 1.73 find_skill_by_name_fuzzy (object *who, const char *name)
180 root 1.50 {
181     if (!name)
182     return 0;
183    
184     for (object *tmp = who->inv; tmp; tmp = tmp->below)
185     if ((tmp->type == SKILL || tmp->type == SKILL_TOOL)
186 root 1.70 && tmp->skill.starts_with (name))
187 root 1.50 if (object *skop = find_skill_by_name (who, tmp->skill))
188     return skop;
189    
190     return 0;
191     }
192    
193 elmex 1.1 /* This returns the skill pointer of the given name (the
194 root 1.49 * one that accumulates exp, has the level, etc).
195 elmex 1.1 *
196     * It is presumed that the player will be needing to actually
197     * use the skill, so thus if use of the skill requires a skill
198     * tool, this code will equip it.
199     *
200     * This code is basically the same as find_skill_by_name() above,
201     * but instead a skill name, we search by matching number.
202     * this replaces find_skill.
203     */
204 root 1.9 object *
205     find_skill_by_number (object *who, int skillno)
206 elmex 1.1 {
207 root 1.47 for (object *tmp = who->inv; tmp; tmp = tmp->below)
208 root 1.50 if (tmp->type == SKILL && tmp->subtype == skillno)
209     if (object *skop = find_skill_by_name (who, tmp->skill))
210     return skop;
211 elmex 1.1
212 root 1.50 return 0;
213 elmex 1.1 }
214    
215 root 1.51 /* This changes the objects chosen_skill to new_skill.
216 elmex 1.1 * return 1 on success, 0 on error
217     */
218 root 1.51 bool
219     object::change_skill (object *new_skill)
220 elmex 1.1 {
221 root 1.51 if (type != PLAYER)
222 root 1.9 return 0;
223    
224 root 1.40 // optimise this supposedly common case
225 root 1.51 if (new_skill == chosen_skill)
226 root 1.40 return 1;
227    
228 root 1.51 if (chosen_skill)
229 root 1.32 {
230 root 1.51 chosen_skill->flag [FLAG_APPLIED] = false;
231     change_abil (this, chosen_skill);
232 root 1.32 }
233 elmex 1.1
234 root 1.51 chosen_skill = new_skill;
235 root 1.33
236 root 1.51 if (chosen_skill)
237     {
238     chosen_skill->flag [FLAG_APPLIED] = true;
239     change_abil (this, chosen_skill);
240     }
241 root 1.16
242 root 1.52 // always clear current weapon, as the selected skill could
243     // conflict with the current weapon skill, which would go
244     // undetected and exp would be given to the wrong skill.
245     current_weapon = 0;
246    
247 root 1.51 update_stats ();
248 root 1.9 return 1;
249 elmex 1.1 }
250    
251     /* do_skill() - Main skills use function-similar in scope to cast_spell().
252     * We handle all requests for skill use outside of some combat here.
253     * We require a separate routine outside of fire() so as to allow monsters
254     * to utilize skills. Returns 1 on use of skill, otherwise 0.
255     * This is changed (2002-11-30) from the old method that returned
256     * exp - no caller needed that info, but it also prevented the callers
257     * from know if a skill was actually used, as many skills don't
258     * give any exp for their direct use (eg, throwing).
259     * It returns 0 if no skill was used.
260     */
261 root 1.9 int
262     do_skill (object *op, object *part, object *skill, int dir, const char *string)
263     {
264     int success = 0, exp = 0;
265    
266     if (!skill)
267     return 0;
268    
269     /* The code below presumes that the skill points to the object that
270     * holds the exp, level, etc of the skill. So if this is a player
271     * go and try to find the actual real skill pointer, and if the
272     * the player doesn't have a bucket for that, create one.
273     */
274     if (skill->type != SKILL && op->type == PLAYER)
275     {
276 root 1.43 for (object *tmp = op->inv; tmp; tmp = tmp->below)
277 root 1.28 if (tmp->type == SKILL && tmp->skill == skill->skill)
278 root 1.43 {
279     skill = tmp;
280     goto found;
281     }
282 root 1.28
283 root 1.43 skill = give_skill_by_name (op, skill->skill);
284     found: ;
285 elmex 1.1 }
286    
287 root 1.9 // skill, by_whom, on_which_object, which direction, skill_argument
288     if (INVOKE_OBJECT (USE_SKILL, skill, ARG_OBJECT (op), ARG_OBJECT (part), ARG_INT (dir), ARG_STRING (string)))
289     return 0;
290 elmex 1.1
291 root 1.9 switch (skill->subtype)
292     {
293 root 1.28 case SK_LEVITATION:
294     /* Not 100% sure if this will work with new movement code -
295     * the levitation skill has move_type for flying, so when
296     * equipped, that should transfer to player, when not,
297     * shouldn't.
298     */
299     if (QUERY_FLAG (skill, FLAG_APPLIED))
300     {
301     CLEAR_FLAG (skill, FLAG_APPLIED);
302     new_draw_info (NDI_UNIQUE, 0, op, "You come to earth.");
303     }
304     else
305     {
306     SET_FLAG (skill, FLAG_APPLIED);
307 root 1.61 new_draw_info (NDI_UNIQUE, 0, op, "You rise into the air!");
308 root 1.28 }
309    
310     op->update_stats ();
311     success = 1;
312     break;
313 root 1.5
314 root 1.28 case SK_STEALING:
315     exp = success = steal (op, dir, skill);
316     break;
317 root 1.5
318 root 1.28 case SK_LOCKPICKING:
319     exp = success = pick_lock (op, dir, skill);
320     break;
321 root 1.5
322 root 1.28 case SK_HIDING:
323     exp = success = hide (op, skill);
324     break;
325 root 1.5
326 root 1.28 case SK_JUMPING:
327 root 1.63 exp = success = jump (op, dir, skill);
328 root 1.28 break;
329 root 1.5
330 root 1.28 case SK_INSCRIPTION:
331     exp = success = write_on_item (op, string, skill);
332     break;
333 root 1.5
334 root 1.28 case SK_MEDITATION:
335     meditate (op, skill);
336     success = 1;
337     break;
338     /* note that the following 'attack' skills gain exp through hit_player() */
339 root 1.5
340 root 1.28 case SK_KARATE:
341 root 1.30 attack_hth (op, dir, "karate-chopped", skill);
342 root 1.28 break;
343 root 1.5
344 root 1.28 case SK_PUNCHING:
345 root 1.30 attack_hth (op, dir, "punched", skill);
346 root 1.28 break;
347 root 1.5
348 root 1.28 case SK_FLAME_TOUCH:
349 root 1.30 attack_hth (op, dir, "flamed", skill);
350 root 1.28 break;
351 elmex 1.1
352 root 1.28 case SK_SPARK_TOUCH:
353 root 1.30 attack_hth (op, dir, "zapped", skill);
354 root 1.28 break;
355 elmex 1.1
356 root 1.28 case SK_SHIVER:
357 root 1.30 attack_hth (op, dir, "froze", skill);
358 root 1.28 break;
359 elmex 1.1
360 root 1.28 case SK_ACID_SPLASH:
361 root 1.30 attack_hth (op, dir, "dissolved", skill);
362 root 1.28 break;
363 elmex 1.1
364 root 1.28 case SK_POISON_NAIL:
365 root 1.30 attack_hth (op, dir, "injected poison into", skill);
366 root 1.28 break;
367 elmex 1.1
368 root 1.28 case SK_CLAWING:
369 root 1.30 attack_hth (op, dir, "clawed", skill);
370 root 1.28 break;
371 root 1.5
372 root 1.28 case SK_ONE_HANDED_WEAPON:
373     case SK_TWO_HANDED_WEAPON:
374 root 1.30 attack_melee_weapon (op, dir, NULL, skill);
375 root 1.28 break;
376 root 1.5
377 root 1.28 case SK_FIND_TRAPS:
378     exp = success = find_traps (op, skill);
379     break;
380 root 1.5
381 root 1.28 case SK_SINGING:
382     exp = success = singing (op, dir, skill);
383     break;
384 root 1.5
385 root 1.28 case SK_ORATORY:
386     exp = success = use_oratory (op, dir, skill);
387     break;
388 root 1.5
389 root 1.28 case SK_SMITHERY:
390     case SK_BOWYER:
391     case SK_JEWELER:
392     case SK_ALCHEMY:
393     case SK_THAUMATURGY:
394     case SK_LITERACY:
395     case SK_WOODSMAN:
396     /* first, we try to find a cauldron, and do the alchemy thing.
397     * failing that, we go and identify stuff.
398     */
399 elmex 1.74 {
400     bool found_cauldron = false;
401    
402     for (object *next, *tmp = GET_MAP_OB (op->map, op->x, op->y); tmp; tmp = next)
403     {
404     next = tmp->above;
405 root 1.17
406 elmex 1.74 if (QUERY_FLAG (tmp, FLAG_IS_CAULDRON))
407     {
408     found_cauldron = true;
409 root 1.17
410 elmex 1.74 if (tmp->skill != skill->skill)
411     {
412     op->failmsg (format ("You can't use the %s with the %s skill!",
413     query_name (tmp),
414     query_name (skill)));
415     break;
416     }
417 root 1.17
418 elmex 1.74 attempt_do_alchemy (op, tmp, skill);
419 root 1.17
420 elmex 1.74 if (QUERY_FLAG (tmp, FLAG_APPLIED))
421     esrv_send_inventory (op, tmp);
422     }
423     }
424 root 1.17
425 elmex 1.74 if (!found_cauldron)
426     exp = success = skill_ident (op, skill);
427     }
428 root 1.28 break;
429 root 1.5
430 root 1.28 case SK_DET_MAGIC:
431     case SK_DET_CURSE:
432     exp = success = skill_ident (op, skill);
433     break;
434 root 1.5
435 root 1.28 case SK_DISARM_TRAPS:
436     exp = success = remove_trap (op, dir, skill);
437     break;
438 root 1.5
439 root 1.28 case SK_THROWING:
440     success = skill_throw (op, part, dir, string, skill);
441     break;
442 root 1.5
443 root 1.28 case SK_SET_TRAP:
444     new_draw_info (NDI_UNIQUE, 0, op, "This skill is not currently implemented.");
445     break;
446 root 1.5
447 root 1.28 case SK_USE_MAGIC_ITEM:
448     case SK_MISSILE_WEAPON:
449     new_draw_info (NDI_UNIQUE, 0, op, "There is no special attack for this skill.");
450     break;
451 root 1.5
452 root 1.28 case SK_PRAYING:
453     success = pray (op, skill);
454     break;
455 root 1.5
456 root 1.28 case SK_BARGAINING:
457     success = describe_shop (op);
458     break;
459 elmex 1.1
460 root 1.28 case SK_SORCERY:
461     case SK_EVOCATION:
462     case SK_PYROMANCY:
463     case SK_SUMMONING:
464     case SK_CLIMBING:
465     new_draw_info (NDI_UNIQUE, 0, op, "This skill is already in effect.");
466     break;
467 root 1.5
468 root 1.28 default:
469     LOG (llevDebug, "%s attempted to use unknown skill: %d\n", query_name (op), op->chosen_skill->stats.sp);
470     break;
471 elmex 1.1 }
472    
473 root 1.9 /* For players we now update the speed_left from using the skill.
474     * Monsters have no skill use time because of the random nature in
475     * which use_monster_skill is called already simulates this.
476     * If certain skills should take more/less time, that should be
477     * in the code for the skill itself.
478     */
479     if (op->type == PLAYER)
480 root 1.45 op->speed_left -= 1.f;
481 root 1.9
482     /* this is a good place to add experience for successfull use of skills.
483     * Note that add_exp() will figure out player/monster experience
484     * gain problems.
485     */
486    
487     if (success && exp)
488     change_exp (op, exp, skill->skill, 0);
489    
490     return success;
491 elmex 1.1 }
492    
493     /* calc_skill_exp() - calculates amount of experience can be gained for
494     * successfull use of a skill. Returns value of experience gain.
495     * Here we take the view that a player must 'overcome an opponent'
496     * in order to gain experience. Examples include foes killed combat,
497     * finding/disarming a trap, stealing from somebeing, etc.
498     * The gained experience is based primarily on the difference in levels,
499     * exp point value of vanquished foe, the relevent stats of the skill being
500     * used and modifications in the skills[] table.
501     *
502     * For now, monsters and players will be treated differently. Below I give
503     * the algorithm for *PLAYER* experience gain. Monster exp gain is simpler.
504     * Monsters just get 10% of the exp of the opponent.
505     *
506     * players get a ratio, eg, opponent lvl / player level. This is then
507     * multiplied by various things. If simple exp is true, then
508     * this multiplier, include the level difference, is always 1.
509     * This revised method prevents some cases where there are big gaps
510     * in the amount you get just because you are now equal level vs lower
511     * level
512     * who is player/creature that used the skill.
513     * op is the object that was 'defeated'.
514     * skill is the skill used. If no skill is used, it should just
515     * point back to who.
516     *
517     */
518 root 1.9 int
519     calc_skill_exp (object *who, object *op, object *skill)
520     {
521     int op_exp = 0, op_lvl = 0;
522     float base, value, lvl_mult = 0.0;
523    
524     if (!skill)
525     skill = who;
526    
527     /* Oct 95 - where we have an object, I expanded our treatment
528     * to 3 cases:
529     * non-living magic obj, runes and everything else.
530     *
531     * If an object is not alive and magical we set the base exp higher to
532     * help out exp awards for skill_ident skills. Also, if
533     * an item is type RUNE, we give out exp based on stats.Cha
534     * and level (this was the old system) -b.t.
535     */
536     if (!op)
537     { /* no item/creature */
538 root 1.60 op_lvl = max (1, who->map->difficulty);
539 root 1.9 op_exp = 0;
540     }
541     else if (op->type == RUNE || op->type == TRAP)
542     { /* all traps. If stats.Cha > 1 we use that
543 root 1.5 * for the amount of experience */
544 root 1.9 op_exp = op->stats.Cha > 1 ? op->stats.Cha : op->stats.exp;
545     op_lvl = op->level;
546     }
547     else
548     { /* all other items/living creatures */
549     op_exp = op->stats.exp;
550     op_lvl = op->level;
551     if (!QUERY_FLAG (op, FLAG_ALIVE))
552 root 1.60 op_lvl += 5 * abs (op->magic); /* for ident/make items */
553 root 1.9 }
554    
555     if (op_lvl < 1)
556     op_lvl = 1;
557    
558     if (who->type != PLAYER)
559     { /* for monsters only */
560     return ((int) (op_exp * 0.1) + 1); /* we add one to insure positive value is returned */
561     }
562     else
563     { /* for players */
564     base = op_exp;
565     /* if skill really is a skill, then we can look at the skill archetype for
566     * bse reward value (exp) and level multiplier factor.
567     */
568     if (skill->type == SKILL)
569     {
570 root 1.56 base += skill->arch->stats.exp;
571 root 1.9 if (settings.simple_exp)
572     {
573 root 1.56 if (skill->arch->level)
574     lvl_mult = (float) skill->arch->level / 100.0;
575 root 1.9 else
576     lvl_mult = 1.0; /* no adjustment */
577 root 1.5 }
578 root 1.9 else
579     {
580     if (skill->level)
581 root 1.56 lvl_mult = ((float) skill->arch->level * (float) op_lvl) / ((float) skill->level * 100.0);
582 root 1.9 else
583     lvl_mult = 1.0;
584 root 1.5 }
585 root 1.9 }
586     else
587     {
588     /* Don't divide by zero here! */
589 root 1.60 lvl_mult = (float) op_lvl / (float) max (1, skill->level);
590 root 1.5 }
591 elmex 1.1 }
592 root 1.9
593     /* assemble the exp total, and return value */
594    
595     value = base * lvl_mult;
596     if (value < 1)
597     value = 1; /* Always give at least 1 exp point */
598    
599 elmex 1.1 #ifdef SKILL_UTIL_DEBUG
600 root 1.9 LOG (llevDebug, "calc_skill_exp(): who: %s(lvl:%d) op:%s(lvl:%d)\n", who->name, skill->level, op->name, op_lvl);
601 elmex 1.1 #endif
602 root 1.9 return ((int) value);
603 elmex 1.1 }
604    
605     /* Learn skill. This inserts the requested skill in the player's
606     * inventory. The skill field of the scroll should have the
607     * exact name of the requested skill.
608     * This one actually teaches the player the skill as something
609     * they can equip.
610     * Return 0 if the player knows the skill, 1 if the
611     * player learns the skill, 2 otherwise.
612     */
613     int
614 root 1.9 learn_skill (object *pl, object *scroll)
615     {
616     if (!scroll->skill)
617     {
618     LOG (llevError, "skill scroll %s does not have skill pointer set.\n", &scroll->name);
619     return 2;
620 elmex 1.1 }
621    
622 root 1.50 object *tmp = find_skill (pl, scroll->skill);
623 elmex 1.1
624 root 1.9 /* player already knows it */
625     if (tmp && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL))
626     return 0;
627 elmex 1.1
628 root 1.9 /* now a random change to learn, based on player Int.
629     * give bonus based on level - otherwise stupid characters
630     * might never be able to learn anything.
631     */
632     if (random_roll (0, 99, pl, PREFER_LOW) > (learn_spell[pl->stats.Int] + (pl->level / 5)))
633     return 2; /* failure :< */
634 elmex 1.1
635 root 1.9 if (!tmp)
636     tmp = give_skill_by_name (pl, scroll->skill);
637 elmex 1.1
638 root 1.9 if (!tmp)
639     {
640     LOG (llevError, "skill scroll %s does not have valid skill name (%s).\n", &scroll->name, &scroll->skill);
641     return 2;
642 elmex 1.1 }
643    
644 root 1.9 SET_FLAG (tmp, FLAG_CAN_USE_SKILL);
645 root 1.67
646 root 1.9 return 1;
647 elmex 1.1 }
648    
649     /* Gives a percentage clipped to 0% -> 100% of a/b. */
650     /* Probably belongs in some global utils-type file? */
651 root 1.9 static int
652     clipped_percent (sint64 a, sint64 b)
653 elmex 1.1 {
654     int rv;
655    
656     if (b <= 0)
657     return 0;
658    
659 root 1.9 rv = (int) ((100.0f * ((float) a) / ((float) b)) + 0.5f);
660    
661 elmex 1.1 if (rv < 0)
662     return 0;
663     else if (rv > 100)
664     return 100;
665 root 1.9
666 elmex 1.1 return rv;
667     }
668    
669     /* show_skills() - Meant to allow players to examine
670     * their current skill list.
671     * This shows the amount of exp they have in the skills.
672     * we also include some other non skill related info (god,
673     * max weapon improvments, item power).
674     * Note this function is a bit more complicated becauase we
675     * we want ot sort the skills before printing them. If we
676     * just dumped this as we found it, this would be a bit
677     * simpler.
678     */
679 root 1.57 //TODO: egad, do it in perl, do not suffer from the big buffer on stack, make it one single drawinfo.
680 root 1.9 void
681 root 1.66 show_skills (object *pl, const char *search)
682 root 1.9 {
683     const char *cp;
684     int i, num_skills_found = 0;
685 root 1.64 const char *const periods = ".............................."; // 30
686 root 1.9
687     /* Need to have a pointer and use strdup for qsort to work properly */
688 root 1.64 char skills[NUM_SKILLS][128]; // d'oh
689 root 1.9
690 root 1.66 object *op = pl->contr->ob;
691    
692 root 1.64 for (object *tmp = op->inv; tmp; tmp = tmp->below)
693 root 1.9 {
694     if (tmp->type == SKILL)
695     {
696 root 1.72 if (search && !tmp->name.contains (search))
697 root 1.9 continue;
698 root 1.64
699     char buf[30];
700    
701     /* Basically want to fill this out to 30 spaces with periods */
702     snprintf (buf, sizeof (buf), "%s%s", &tmp->name, periods);
703 root 1.9
704     if (settings.permanent_exp_ratio)
705 root 1.64 snprintf (skills[num_skills_found++], sizeof (skills [0]), "%slvl:%3d (xp:%" PRId64 "/%" PRId64 "/%d%%)",
706     buf, tmp->level, tmp->stats.exp,
707     level_exp (tmp->level + 1, op->expmul), clipped_percent (tmp->perm_exp, tmp->stats.exp));
708 root 1.9 else
709 root 1.64 snprintf (skills[num_skills_found++], sizeof (skills [0]), "%slvl:%3d (xp:%" PRId64 "/%" PRId64 ")",
710     buf, tmp->level, tmp->stats.exp, level_exp (tmp->level + 1, op->expmul));
711 root 1.57
712 root 1.9 /* I don't know why some characters get a bunch of skills, but
713     * it sometimes happens (maybe a leftover from bugier earlier code
714     * and those character are still about). In any case, lets handle
715     * it so it doesn't crash the server - otherwise, one character may
716     * crash the server numerous times.
717     */
718     if (num_skills_found >= NUM_SKILLS)
719     {
720     new_draw_info (NDI_RED, 0, op, "Your character has too many skills.");
721     new_draw_info (NDI_RED, 0, op, "Something isn't right - contact the server admin");
722     break;
723 root 1.5 }
724     }
725 elmex 1.1 }
726    
727 root 1.64 dynbuf_text msg (4096, 1024);
728    
729     msg << "T<Player skills:>\n\n";
730 root 1.9 if (num_skills_found > 1)
731 root 1.64 qsort (skills, num_skills_found, sizeof (skills [0]), (int (*)(const void *, const void *)) std::strcmp);
732 elmex 1.1
733 root 1.9 for (i = 0; i < num_skills_found; i++)
734 root 1.64 msg << " C<" << skills [i] << ">\n";
735 elmex 1.1
736 root 1.64 msg << "\nYou can handle " << op->level / 5 + 5 << " weapon improvements.\r";
737 elmex 1.1
738 root 1.9 cp = determine_god (op);
739 root 1.64 msg << "You worship " << (cp ? cp : "no god at current time") << ".\r";
740    
741     msg << "Your equipped item power is " << (int)op->contr->item_power
742     << " out of " << int (op->level * settings.item_power_factor)
743 root 1.65 << ".\n";
744 elmex 1.1
745 root 1.66 pl->contr->infobox (MSG_CHANNEL ("skills"), msg);
746 elmex 1.1 }
747    
748     /* use_skill() - similar to invoke command, it executes the skill in the
749     * direction that the user is facing. Returns false if we are unable to
750     * change to the requested skill, or were unable to use the skill properly.
751     * This is tricky because skills can have spaces. We basically roll
752     * our own find_skill_by_name so we can try to do better string matching.
753     */
754 root 1.9 int
755     use_skill (object *op, const char *string)
756     {
757     object *skop;
758     size_t len;
759    
760     if (!string)
761     return 0;
762    
763 root 1.50 for (skop = op->inv; skop; skop = skop->below)
764 root 1.9 {
765 root 1.50 if (skop->type == SKILL
766     && QUERY_FLAG (skop, FLAG_CAN_USE_SKILL)
767     && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill))))
768 root 1.9 break;
769 root 1.50 else if (skop->type == SKILL_TOOL
770     && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill))))
771 root 1.9 break;
772 elmex 1.1 }
773 root 1.50
774 root 1.9 if (!skop)
775     {
776     new_draw_info_format (NDI_UNIQUE, 0, op, "Unable to find skill %s", string);
777     return 0;
778 elmex 1.1 }
779    
780 root 1.9 len = strlen (skop->skill);
781 elmex 1.1
782 root 1.9 /* All this logic goes and skips over the skill name to find any
783     * options given to the skill. Its pretty simple - if there
784     * are extra parameters (as deteremined by string length), we
785     * want to skip over any leading spaces.
786     */
787     if (len >= strlen (string))
788 root 1.50 string = NULL;
789 root 1.9 else
790     {
791     string += len;
792     while (*string == 0x20)
793     string++;
794 root 1.50
795 root 1.9 if (strlen (string) == 0)
796     string = NULL;
797     }
798    
799 elmex 1.1 #ifdef SKILL_UTIL_DEBUG
800 root 1.9 LOG (llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none");
801 elmex 1.1 #endif
802    
803 root 1.9 if (do_skill (op, op, skop, op->facing, string))
804     return 1;
805    
806     return 0;
807 elmex 1.1 }
808    
809 root 1.16 static bool
810     hth_skill_p (object *skill)
811     {
812 root 1.33 return (skill_flags [skill->subtype] & (SF_COMBAT | SF_NEED_WEAPON)) == SF_COMBAT;
813 root 1.16 }
814    
815     /* This finds the first unarmed skill the player has, and returns it.
816 elmex 1.1 */
817 root 1.9 static object *
818 root 1.16 find_player_hth_skill (object *op)
819 elmex 1.1 {
820 root 1.16 for (object *tmp = op->inv; tmp; tmp = tmp->below)
821     if (tmp->type == SKILL && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL) && hth_skill_p (tmp))
822     return tmp;
823 elmex 1.1
824 root 1.16 return 0;
825 elmex 1.1 }
826    
827     /* do_skill_attack() - We have got an appropriate opponent from either
828     * move_player_attack() or skill_attack(). In this part we get on with
829     * attacking, take care of messages from the attack and changes in invisible.
830     * Returns true if the attack damaged the opponent.
831     * tmp is the targetted monster.
832     * op is what is attacking
833     * string is passed along to describe what messages to describe
834     * the damage.
835     */
836 root 1.9 static int
837     do_skill_attack (object *tmp, object *op, const char *string, object *skill)
838     {
839     if (INVOKE_OBJECT (SKILL_ATTACK, op, ARG_OBJECT (tmp), ARG_STRING (string), ARG_OBJECT (skill)))
840     return RESULT_INT (0);
841    
842     /* For Players only: if there is no ready weapon, and no "attack" skill
843     * is readied either then try to find a skill for the player to use.
844     * it is presumed that if skill is set, it is a valid attack skill (eg,
845     * the caller should have set it appropriately). We still want to pass
846     * through that code if skill is set to change to the skill.
847     */
848 root 1.35 if (player *pl = op->contr)
849 root 1.9 {
850 root 1.63 if (skill)
851     op->change_skill (skill);
852     else
853 root 1.9 {
854 root 1.63 if (!pl->combat_ob)
855 root 1.9 {
856 root 1.63 if (QUERY_FLAG (op, FLAG_READY_WEAPON))
857     {
858     for (tmp = op->inv; tmp; tmp = tmp->below)
859     if (tmp->type == WEAPON && QUERY_FLAG (tmp, FLAG_APPLIED))
860     break;
861 root 1.35
862 root 1.63 if (!tmp)
863     LOG (llevError, "Could not find applied weapon on %s\n", &op->name);
864 root 1.9
865 root 1.63 pl->combat_ob = tmp;
866     }
867 root 1.52
868 root 1.63 if (!pl->combat_ob)
869 root 1.35 {
870     /* See if the players chosen skill is a combat skill, and use
871     * it if appropriate.
872     */
873     if (op->chosen_skill && hth_skill_p (op->chosen_skill))
874     skill = op->chosen_skill;
875     else
876 root 1.9 {
877 root 1.35 skill = find_player_hth_skill (op);
878    
879     if (!skill)
880 root 1.52 new_draw_info (NDI_BLACK, 0, op, "You have no unarmed combat skills!");
881 root 1.5 }
882 root 1.63
883     if (skill)
884     {
885     op->change_skill (0);
886     apply_special (op, skill, AP_APPLY);
887     }
888 root 1.5 }
889 root 1.35
890 root 1.63 if (!pl->combat_ob)
891 root 1.55 {
892 root 1.63 LOG (llevError, "Could not find anything to attack on %s\n", &op->name);
893     return 0;
894 root 1.55 }
895 root 1.5 }
896 root 1.16
897 root 1.63 if (!op->change_weapon (pl->combat_ob))
898     return 0;
899 root 1.9 }
900 root 1.35
901 root 1.38 /* lose invisiblity/hiding status for running attacks */
902 root 1.35 if (pl->tmp_invis)
903     {
904     pl->tmp_invis = 0;
905     op->invisible = 0;
906 root 1.69 op->flag [FLAG_HIDDEN] = 0;
907 root 1.35 update_object (op, UP_OBJ_CHANGE);
908 root 1.5 }
909 elmex 1.1 }
910    
911 root 1.31 int success = attack_ob (tmp, op);
912 root 1.9
913     /* print appropriate messages to the player */
914    
915 root 1.31 if (success && string && tmp && !QUERY_FLAG (tmp, FLAG_FREED))
916 root 1.9 {
917     if (op->type == PLAYER)
918     new_draw_info_format (NDI_UNIQUE, 0, op, "You %s %s!", string, query_name (tmp));
919     else if (tmp->type == PLAYER)
920     new_draw_info_format (NDI_UNIQUE, 0, tmp, "%s %s you!", query_name (op), string);
921     }
922 root 1.30
923 root 1.9 return success;
924     }
925 elmex 1.1
926     /* skill_attack() - Core routine for use when we attack using a skills
927     * system. In essence, this code handles
928 root 1.63 * all skill-based attacks, i.e. hth, missile and melee weapons should be
929 elmex 1.1 * treated here. If an opponent is already supplied by move_player(),
930     * we move right onto do_skill_attack(), otherwise we find if an
931     * appropriate opponent exists.
932     *
933     * This is called by move_player() and attack_hth()
934     *
935     * Initial implementation by -bt thomas@astro.psu.edu
936     */
937 root 1.9 int
938     skill_attack (object *tmp, object *pl, int dir, const char *string, object *skill)
939     {
940     sint16 tx, ty;
941 root 1.13 maptile *m;
942 root 1.9 int mflags;
943    
944     if (!dir)
945     dir = pl->facing;
946 root 1.16
947 root 1.9 tx = freearr_x[dir];
948     ty = freearr_y[dir];
949    
950     /* If we don't yet have an opponent, find if one exists, and attack.
951     * Legal opponents are the same as outlined in move_player_attack()
952     */
953 root 1.31 if (!tmp)
954 root 1.9 {
955     m = pl->map;
956     tx = pl->x + freearr_x[dir];
957     ty = pl->y + freearr_y[dir];
958    
959     mflags = get_map_flags (m, &m, tx, ty, &tx, &ty);
960     if (mflags & P_OUT_OF_MAP)
961     return 0;
962    
963     /* space must be blocked for there to be anything interesting to do */
964     if (!OB_TYPE_MOVE_BLOCK (pl, GET_MAP_MOVE_BLOCK (m, tx, ty)))
965     return 0;
966    
967 root 1.21 for (tmp = GET_MAP_OB (m, tx, ty); tmp; tmp = tmp->above)
968 root 1.9 if ((QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->stats.hp >= 0) || QUERY_FLAG (tmp, FLAG_CAN_ROLL) || tmp->type == LOCKED_DOOR)
969     {
970     /* Don't attack party members */
971     if ((pl->type == PLAYER && tmp->type == PLAYER) && (pl->contr->party != NULL && pl->contr->party == tmp->contr->party))
972     return 0;
973 root 1.31
974 root 1.9 break;
975     }
976 elmex 1.1 }
977 root 1.31
978 root 1.9 if (!tmp)
979     {
980     if (pl->type == PLAYER)
981     new_draw_info (NDI_UNIQUE, 0, pl, "There is nothing to attack!");
982 root 1.31
983 root 1.9 return 0;
984 elmex 1.1 }
985    
986 root 1.9 return do_skill_attack (tmp, pl, string, skill);
987 elmex 1.1 }
988    
989     /* attack_hth() - this handles all hand-to-hand attacks -b.t. */
990 root 1.9
991 elmex 1.1 /* July 5, 1995 - I broke up attack_hth() into 2 parts. In the first
992     * (attack_hth) we check for weapon use, etc in the second (the new
993     * function skill_attack() we actually attack.
994     */
995 root 1.9 static int
996     attack_hth (object *pl, int dir, const char *string, object *skill)
997     {
998     object *enemy = NULL, *weapon;
999 elmex 1.1
1000 root 1.9 if (QUERY_FLAG (pl, FLAG_READY_WEAPON))
1001     for (weapon = pl->inv; weapon; weapon = weapon->below)
1002     {
1003     if (weapon->type == WEAPON && QUERY_FLAG (weapon, FLAG_APPLIED))
1004     {
1005     CLEAR_FLAG (weapon, FLAG_APPLIED);
1006     CLEAR_FLAG (pl, FLAG_READY_WEAPON);
1007 root 1.22 pl->update_stats ();
1008 root 1.9 if (pl->type == PLAYER)
1009     {
1010     new_draw_info (NDI_UNIQUE, 0, pl, "You unwield your weapon in order to attack.");
1011     esrv_update_item (UPD_FLAGS, pl, weapon);
1012     }
1013 root 1.37
1014 root 1.9 break;
1015     }
1016     }
1017 root 1.30
1018 root 1.9 return skill_attack (enemy, pl, dir, string, skill);
1019 elmex 1.1 }
1020    
1021     /* attack_melee_weapon() - this handles melee weapon attacks -b.t.
1022     * For now we are just checking to see if we have a ready weapon here.
1023     * But there is a real neato possible feature of this scheme which
1024     * bears mentioning:
1025     * Since we are only calling this from do_skill() in the future
1026     * we may make this routine handle 'special' melee weapons attacks
1027     * (like disarming manuever with sai) based on player SK_level and
1028     * weapon type.
1029     */
1030 root 1.9 static int
1031     attack_melee_weapon (object *op, int dir, const char *string, object *skill)
1032     {
1033 elmex 1.1
1034 root 1.9 if (!QUERY_FLAG (op, FLAG_READY_WEAPON))
1035     {
1036     if (op->type == PLAYER)
1037     new_draw_info (NDI_UNIQUE, 0, op, "You have no ready weapon to attack with!");
1038 root 1.30
1039 root 1.9 return 0;
1040 elmex 1.1 }
1041 root 1.30
1042 root 1.9 return skill_attack (NULL, op, dir, string, skill);
1043 root 1.51 }
1044 elmex 1.1