ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.68
Committed: Thu Sep 25 22:58:13 2008 UTC (15 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.67: +19 -3 lines
Log Message:
*** empty log message ***

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