ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.50
Committed: Mon May 14 21:52:32 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.49: +36 -129 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.35 * CrossFire, A Multiplayer game
3 pippijn 1.26 *
4     * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5     * Copryight (C) 2002 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 author can be reached via e-mail to <crossfire@schmorp.de>
23     */
24 elmex 1.1
25     /* Created July 95 to separate skill utilities from actual skills -b.t. */
26    
27     /* Reconfigured skills code to allow linking of skills to experience
28     * categories. This is done solely through the init_new_exp_system() fctn.
29     * June/July 1995 -b.t. thomas@astro.psu.edu
30     */
31    
32     /* July 1995 - Initial associated skills coding. Experience gains
33     * come solely from the use of skills. Overcoming an opponent (in combat,
34     * finding/disarming a trap, stealing from somebeing, etc) gains
35     * experience. Calc_skill_exp() handles the gained experience using
36     * modifications in the skills[] table. - b.t.
37     */
38    
39     /* define the following for skills utility debuging */
40 root 1.9
41 elmex 1.1 /* #define SKILL_UTIL_DEBUG */
42    
43     #include <global.h>
44     #include <object.h>
45 root 1.23 #include <sproto.h>
46 root 1.9 #include <living.h> /* for defs of STR,CON,DEX,etc. -b.t. */
47 elmex 1.1 #include <spells.h>
48    
49 root 1.33 const uint8_t skill_flags[NUM_SKILLS] = {
50 root 1.34 0, // SK_NONE
51 root 1.33 # define def(uc, flags) flags,
52     # include "skillinc.h"
53     # undef def
54 root 1.11 };
55    
56 root 1.9 static int attack_hth (object *pl, int dir, const char *string, object *skill);
57     static int attack_melee_weapon (object *op, int dir, const char *string, object *skill);
58 elmex 1.1
59     /* init_skills basically just sets up the skill_names table
60     * above. The index into the array is set up by the
61     * subtypes.
62     */
63 root 1.9 void
64     init_skills (void)
65     {
66 root 1.33 for (archetype *at = first_archetype; at; at = at->next)
67 root 1.10 if (at->clone.type == SKILL)
68     {
69 root 1.33 if (skill_names[at->clone.subtype])
70 root 1.10 LOG (llevError, "init_skills: multiple skill using same subtype %d, %s, %s\n",
71     at->clone.subtype, &skill_names[at->clone.subtype], &at->clone.skill);
72     else
73     skill_names[at->clone.subtype] = at->clone.skill;
74     }
75 elmex 1.1
76 root 1.9 /* This isn't really an error if there is no skill subtype set, but
77     * checking for this may catch some user errors.
78     */
79 root 1.33 for (int i = 1; i < NUM_SKILLS; i++)
80     if (!skill_names[i])
81     LOG (llevError, "init_skills: skill subtype %d doesn't have a name?\n", i);
82 elmex 1.1 }
83    
84     /* This function goes through the player inventory and sets
85     * up the last_skills[] array in the player object.
86     * the last_skills[] is used to more quickly lookup skills -
87     * mostly used for sending exp.
88     */
89 root 1.9 void
90     link_player_skills (object *op)
91 elmex 1.1 {
92 root 1.25 for (object *tmp = op->inv; tmp; tmp = tmp->below)
93     if (tmp->type == SKILL)
94     {
95     /* This is really a warning, hence no else below */
96     if (op->contr->last_skill_ob[tmp->subtype] && op->contr->last_skill_ob[tmp->subtype] != tmp)
97     LOG (llevError, "Multiple skills with the same subtype? %s, %s\n",
98     &op->contr->last_skill_ob[tmp->subtype]->skill, &tmp->skill);
99    
100     if (tmp->subtype >= NUM_SKILLS)
101     LOG (llevError, "Invalid subtype number %d (range 0-%d)\n", tmp->subtype, NUM_SKILLS);
102     else
103     {
104     op->contr->last_skill_ob[tmp->subtype] = tmp;
105     op->contr->ns->last_skill_exp[tmp->subtype] = -1; //TODO: this should go
106     }
107     }
108 elmex 1.1 }
109    
110 root 1.49 static object *
111     find_skill (object *who, const shstr &sh)
112     {
113     for (object *tmp = who->inv; tmp; tmp = tmp->below)
114     if (tmp->skill == sh && tmp->type == SKILL)
115     return tmp;
116    
117     return 0;
118     }
119    
120 root 1.50 /* This returns the skill pointer of the given name (the
121     * one that accumulates exp, has the level, etc).
122     *
123     * It is presumed that the player will be needing to actually
124     * use the skill, so thus if use of the skill requires a skill
125     * tool, this code will equip it.
126     */
127 root 1.49 object *
128     find_skill_by_name (object *who, const shstr &sh)
129     {
130     object *skill_tool = 0;
131    
132     for (object *tmp = who->inv; tmp; tmp = tmp->below)
133     if (tmp->skill == sh)
134     {
135     if (tmp->type == SKILL && (tmp->flag [FLAG_CAN_USE_SKILL] || tmp->flag [FLAG_APPLIED]))
136     /* If this is a skill that can be used without applying tool, return it */
137     return tmp->inv_splay ();
138     /* Try to find appropriate skilltool. If the player has one already
139     * applied, we try to keep using that one.
140     */
141     else if (tmp->type == SKILL_TOOL && !skill_tool)
142     skill_tool = tmp;
143     }
144    
145     if (!skill_tool)
146     return 0;
147    
148     /* Player has a tool to use the skill. If not applied, apply it -
149     * if not successful, return null. If they do have the skill tool
150     * but not the skill itself, give it to them.
151     */
152     object *skill = find_skill (who, skill_tool->skill);
153    
154     if (!skill)
155     {
156     skill = give_skill_by_name (who, skill_tool->skill);
157     link_player_skills (who);
158     }
159    
160     skill->inv_splay ();
161    
162     if (!skill_tool->flag [FLAG_APPLIED])
163     if (apply_special (who, skill_tool, AP_APPLY))
164     return 0;
165    
166     return skill;
167     }
168    
169 root 1.50 object *
170     find_skill_by_name (object *who, const char *name)
171     {
172     if (!name)
173     return 0;
174    
175     for (object *tmp = who->inv; tmp; tmp = tmp->below)
176     if ((tmp->type == SKILL || tmp->type == SKILL_TOOL)
177     && tmp->skill.begins_with (name))
178     if (object *skop = find_skill_by_name (who, tmp->skill))
179     return skop;
180    
181     return 0;
182     }
183    
184 elmex 1.1 /* This returns the skill pointer of the given name (the
185 root 1.49 * one that accumulates exp, has the level, etc).
186 elmex 1.1 *
187     * It is presumed that the player will be needing to actually
188     * use the skill, so thus if use of the skill requires a skill
189     * tool, this code will equip it.
190     *
191     * This code is basically the same as find_skill_by_name() above,
192     * but instead a skill name, we search by matching number.
193     * this replaces find_skill.
194     */
195 root 1.9 object *
196     find_skill_by_number (object *who, int skillno)
197 elmex 1.1 {
198 root 1.47 for (object *tmp = who->inv; tmp; tmp = tmp->below)
199 root 1.50 if (tmp->type == SKILL && tmp->subtype == skillno)
200     if (object *skop = find_skill_by_name (who, tmp->skill))
201     return skop;
202 elmex 1.1
203 root 1.50 return 0;
204 elmex 1.1 }
205    
206     /* This changes the objects skill to new_skill.
207     * note that this function doesn't always need to get used -
208     * you can now add skill exp to the player without the chosen_skill being
209 root 1.40 * set. This function is of most interest to players to update
210 elmex 1.1 * the various range information.
211     * return 1 on success, 0 on error
212     */
213 root 1.9 int
214     change_skill (object *who, object *new_skill, int flag)
215 elmex 1.1 {
216 root 1.9 if (who->type != PLAYER)
217     return 0;
218    
219 root 1.46 #if 0
220 root 1.40 // optimise this supposedly common case
221     if (new_skill == who->chosen_skill)
222     return 1;
223 root 1.46 #endif
224 root 1.40
225 root 1.32 if (!new_skill)
226     {
227 root 1.35 LOG (llevError | logBacktrace, "change_skill called on %s with NULL skill\n",
228     who->debug_desc ());
229 root 1.32 return 0;
230     }
231 elmex 1.1
232 root 1.46 // the skill could be readied already because it is used by a weapon.
233     who->change_weapon (0);
234 root 1.43 new_skill->inv_splay ();
235 root 1.33
236 root 1.9 if (apply_special (who, new_skill, AP_APPLY))
237 root 1.16 return 0;
238    
239 root 1.9 return 1;
240 elmex 1.1 }
241    
242     /* This function just clears the chosen_skill and range_skill values
243 root 1.33 * in the player.
244 elmex 1.1 */
245 root 1.9 void
246     clear_skill (object *who)
247 elmex 1.1 {
248 root 1.33 who->chosen_skill = 0;
249 root 1.9 CLEAR_FLAG (who, FLAG_READY_SKILL);
250 root 1.33
251 root 1.48 if (player *pl = who->contr)
252     if (pl->ranged_ob && pl->ranged_ob->type == SKILL)
253     pl->ranged_ob = 0;
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     new_draw_info (NDI_UNIQUE, 0, op, "You rise into the air!.");
314     }
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     success = jump (op, dir, skill);
334     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    
475     if (op->type == PLAYER)
476 root 1.45 op->speed_left -= 1.f;
477 root 1.9
478     /* this is a good place to add experience for successfull use of skills.
479     * Note that add_exp() will figure out player/monster experience
480     * gain problems.
481     */
482    
483     if (success && exp)
484     change_exp (op, exp, skill->skill, 0);
485    
486     return success;
487 elmex 1.1 }
488    
489     /* calc_skill_exp() - calculates amount of experience can be gained for
490     * successfull use of a skill. Returns value of experience gain.
491     * Here we take the view that a player must 'overcome an opponent'
492     * in order to gain experience. Examples include foes killed combat,
493     * finding/disarming a trap, stealing from somebeing, etc.
494     * The gained experience is based primarily on the difference in levels,
495     * exp point value of vanquished foe, the relevent stats of the skill being
496     * used and modifications in the skills[] table.
497     *
498     * For now, monsters and players will be treated differently. Below I give
499     * the algorithm for *PLAYER* experience gain. Monster exp gain is simpler.
500     * Monsters just get 10% of the exp of the opponent.
501     *
502     * players get a ratio, eg, opponent lvl / player level. This is then
503     * multiplied by various things. If simple exp is true, then
504     * this multiplier, include the level difference, is always 1.
505     * This revised method prevents some cases where there are big gaps
506     * in the amount you get just because you are now equal level vs lower
507     * level
508     * who is player/creature that used the skill.
509     * op is the object that was 'defeated'.
510     * skill is the skill used. If no skill is used, it should just
511     * point back to who.
512     *
513     */
514 root 1.9 int
515     calc_skill_exp (object *who, object *op, object *skill)
516     {
517     int op_exp = 0, op_lvl = 0;
518     float base, value, lvl_mult = 0.0;
519    
520     if (!skill)
521     skill = who;
522    
523     /* Oct 95 - where we have an object, I expanded our treatment
524     * to 3 cases:
525     * non-living magic obj, runes and everything else.
526     *
527     * If an object is not alive and magical we set the base exp higher to
528     * help out exp awards for skill_ident skills. Also, if
529     * an item is type RUNE, we give out exp based on stats.Cha
530     * and level (this was the old system) -b.t.
531     */
532    
533     if (!op)
534     { /* no item/creature */
535     op_lvl = who->map->difficulty < 1 ? 1 : who->map->difficulty;
536     op_exp = 0;
537     }
538     else if (op->type == RUNE || op->type == TRAP)
539     { /* all traps. If stats.Cha > 1 we use that
540 root 1.5 * for the amount of experience */
541 root 1.9 op_exp = op->stats.Cha > 1 ? op->stats.Cha : op->stats.exp;
542     op_lvl = op->level;
543     }
544     else
545     { /* all other items/living creatures */
546     op_exp = op->stats.exp;
547     op_lvl = op->level;
548     if (!QUERY_FLAG (op, FLAG_ALIVE))
549     { /* for ident/make items */
550     op_lvl += 5 * abs (op->magic);
551     }
552     }
553    
554     if (op_lvl < 1)
555     op_lvl = 1;
556    
557     if (who->type != PLAYER)
558     { /* for monsters only */
559     return ((int) (op_exp * 0.1) + 1); /* we add one to insure positive value is returned */
560     }
561     else
562     { /* for players */
563     base = op_exp;
564     /* if skill really is a skill, then we can look at the skill archetype for
565     * bse reward value (exp) and level multiplier factor.
566     */
567     if (skill->type == SKILL)
568     {
569     base += skill->arch->clone.stats.exp;
570     if (settings.simple_exp)
571     {
572     if (skill->arch->clone.level)
573     lvl_mult = (float) skill->arch->clone.level / 100.0;
574     else
575     lvl_mult = 1.0; /* no adjustment */
576 root 1.5 }
577 root 1.9 else
578     {
579     if (skill->level)
580     lvl_mult = ((float) skill->arch->clone.level * (float) op_lvl) / ((float) skill->level * 100.0);
581     else
582     lvl_mult = 1.0;
583 root 1.5 }
584 root 1.9 }
585     else
586     {
587     /* Don't divide by zero here! */
588     lvl_mult = (float) op_lvl / (float) (skill->level ? skill->level : 1);
589 root 1.5 }
590 elmex 1.1 }
591 root 1.9
592     /* assemble the exp total, and return value */
593    
594     value = base * lvl_mult;
595     if (value < 1)
596     value = 1; /* Always give at least 1 exp point */
597    
598 elmex 1.1 #ifdef SKILL_UTIL_DEBUG
599 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);
600 elmex 1.1 #endif
601 root 1.9 return ((int) value);
602 elmex 1.1 }
603    
604     /* Learn skill. This inserts the requested skill in the player's
605     * inventory. The skill field of the scroll should have the
606     * exact name of the requested skill.
607     * This one actually teaches the player the skill as something
608     * they can equip.
609     * Return 0 if the player knows the skill, 1 if the
610     * player learns the skill, 2 otherwise.
611     */
612     int
613 root 1.9 learn_skill (object *pl, object *scroll)
614     {
615     if (!scroll->skill)
616     {
617     LOG (llevError, "skill scroll %s does not have skill pointer set.\n", &scroll->name);
618     return 2;
619 elmex 1.1 }
620    
621 root 1.50 object *tmp = find_skill (pl, scroll->skill);
622 elmex 1.1
623 root 1.9 /* player already knows it */
624     if (tmp && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL))
625     return 0;
626 elmex 1.1
627 root 1.9 /* now a random change to learn, based on player Int.
628     * give bonus based on level - otherwise stupid characters
629     * might never be able to learn anything.
630     */
631     if (random_roll (0, 99, pl, PREFER_LOW) > (learn_spell[pl->stats.Int] + (pl->level / 5)))
632     return 2; /* failure :< */
633 elmex 1.1
634 root 1.9 if (!tmp)
635     tmp = give_skill_by_name (pl, scroll->skill);
636 elmex 1.1
637 root 1.9 if (!tmp)
638     {
639     LOG (llevError, "skill scroll %s does not have valid skill name (%s).\n", &scroll->name, &scroll->skill);
640     return 2;
641 elmex 1.1 }
642    
643 root 1.9 SET_FLAG (tmp, FLAG_CAN_USE_SKILL);
644     link_player_skills (pl);
645     return 1;
646 elmex 1.1 }
647    
648     /* Gives a percentage clipped to 0% -> 100% of a/b. */
649     /* Probably belongs in some global utils-type file? */
650 root 1.9 static int
651     clipped_percent (sint64 a, sint64 b)
652 elmex 1.1 {
653     int rv;
654    
655     if (b <= 0)
656     return 0;
657    
658 root 1.9 rv = (int) ((100.0f * ((float) a) / ((float) b)) + 0.5f);
659    
660 elmex 1.1 if (rv < 0)
661     return 0;
662     else if (rv > 100)
663     return 100;
664 root 1.9
665 elmex 1.1 return rv;
666     }
667    
668     /* show_skills() - Meant to allow players to examine
669     * their current skill list.
670     * This shows the amount of exp they have in the skills.
671     * we also include some other non skill related info (god,
672     * max weapon improvments, item power).
673     * Note this function is a bit more complicated becauase we
674     * we want ot sort the skills before printing them. If we
675     * just dumped this as we found it, this would be a bit
676     * simpler.
677     */
678 root 1.9
679     void
680     show_skills (object *op, const char *search)
681     {
682     object *tmp = NULL;
683     char buf[MAX_BUF];
684     const char *cp;
685     int i, num_skills_found = 0;
686     static const char *const periods = "........................................";
687    
688     /* Need to have a pointer and use strdup for qsort to work properly */
689     char skills[NUM_SKILLS][MAX_BUF];
690    
691    
692     for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
693     {
694     if (tmp->type == SKILL)
695     {
696     if (search && strstr (tmp->name, search) == NULL)
697     continue;
698     /* Basically want to fill this out to 40 spaces with periods */
699     sprintf (buf, "%s%s", &tmp->name, periods);
700     buf[40] = 0;
701    
702     if (settings.permanent_exp_ratio)
703     {
704 root 1.20 sprintf (skills[num_skills_found++], "%slvl:%3d (xp:%" PRId64 "/%" PRId64 "/%d%%)",
705     buf, tmp->level, tmp->stats.exp,
706     level_exp (tmp->level + 1, op->expmul), clipped_percent (tmp->perm_exp, tmp->stats.exp));
707 root 1.9 }
708     else
709     {
710 root 1.20 sprintf (skills[num_skills_found++], "%slvl:%3d (xp:%" PRId64 "/%" PRId64 ")",
711     buf, tmp->level, tmp->stats.exp, level_exp (tmp->level + 1, op->expmul));
712 root 1.5 }
713 root 1.9 /* I don't know why some characters get a bunch of skills, but
714     * it sometimes happens (maybe a leftover from bugier earlier code
715     * and those character are still about). In any case, lets handle
716     * it so it doesn't crash the server - otherwise, one character may
717     * crash the server numerous times.
718     */
719     if (num_skills_found >= NUM_SKILLS)
720     {
721     new_draw_info (NDI_RED, 0, op, "Your character has too many skills.");
722     new_draw_info (NDI_RED, 0, op, "Something isn't right - contact the server admin");
723     break;
724 root 1.5 }
725     }
726 elmex 1.1 }
727    
728 root 1.9 clear_win_info (op);
729     new_draw_info (NDI_UNIQUE, 0, op, "Player skills:");
730     if (num_skills_found > 1)
731 root 1.27 qsort (skills, num_skills_found, MAX_BUF, (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.27 new_draw_info (NDI_UNIQUE, 0, op, skills[i]);
735 elmex 1.1
736 root 1.9 new_draw_info_format (NDI_UNIQUE, 0, op, "You can handle %d weapon improvements.", op->level / 5 + 5);
737 elmex 1.1
738 root 1.9 cp = determine_god (op);
739     new_draw_info_format (NDI_UNIQUE, 0, op, "You worship %s.", cp ? cp : "no god at current time");
740 elmex 1.1
741 root 1.9 new_draw_info_format (NDI_UNIQUE, 0, op, "Your equipped item power is %d out of %d\n",
742     op->contr->item_power, (int) (op->level * settings.item_power_factor));
743 elmex 1.1 }
744    
745     /* use_skill() - similar to invoke command, it executes the skill in the
746     * direction that the user is facing. Returns false if we are unable to
747     * change to the requested skill, or were unable to use the skill properly.
748     * This is tricky because skills can have spaces. We basically roll
749     * our own find_skill_by_name so we can try to do better string matching.
750     */
751 root 1.9 int
752     use_skill (object *op, const char *string)
753     {
754     object *skop;
755     size_t len;
756    
757     if (!string)
758     return 0;
759    
760 root 1.50 for (skop = op->inv; skop; skop = skop->below)
761 root 1.9 {
762 root 1.50 if (skop->type == SKILL
763     && QUERY_FLAG (skop, FLAG_CAN_USE_SKILL)
764     && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill))))
765 root 1.9 break;
766 root 1.50 else if (skop->type == SKILL_TOOL
767     && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill))))
768 root 1.9 break;
769 elmex 1.1 }
770 root 1.50
771 root 1.9 if (!skop)
772     {
773     new_draw_info_format (NDI_UNIQUE, 0, op, "Unable to find skill %s", string);
774     return 0;
775 elmex 1.1 }
776    
777 root 1.9 len = strlen (skop->skill);
778 elmex 1.1
779 root 1.9 /* All this logic goes and skips over the skill name to find any
780     * options given to the skill. Its pretty simple - if there
781     * are extra parameters (as deteremined by string length), we
782     * want to skip over any leading spaces.
783     */
784     if (len >= strlen (string))
785 root 1.50 string = NULL;
786 root 1.9 else
787     {
788     string += len;
789     while (*string == 0x20)
790     string++;
791 root 1.50
792 root 1.9 if (strlen (string) == 0)
793     string = NULL;
794     }
795    
796 elmex 1.1 #ifdef SKILL_UTIL_DEBUG
797 root 1.9 LOG (llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none");
798 elmex 1.1 #endif
799    
800 root 1.9 if (do_skill (op, op, skop, op->facing, string))
801     return 1;
802    
803     return 0;
804 elmex 1.1 }
805    
806 root 1.16 static bool
807     hth_skill_p (object *skill)
808     {
809 root 1.33 return (skill_flags [skill->subtype] & (SF_COMBAT | SF_NEED_WEAPON)) == SF_COMBAT;
810 root 1.16 }
811    
812     /* This finds the first unarmed skill the player has, and returns it.
813 elmex 1.1 */
814 root 1.9 static object *
815 root 1.16 find_player_hth_skill (object *op)
816 elmex 1.1 {
817 root 1.16 for (object *tmp = op->inv; tmp; tmp = tmp->below)
818     if (tmp->type == SKILL && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL) && hth_skill_p (tmp))
819     return tmp;
820 elmex 1.1
821 root 1.16 return 0;
822 elmex 1.1 }
823    
824     /* do_skill_attack() - We have got an appropriate opponent from either
825     * move_player_attack() or skill_attack(). In this part we get on with
826     * attacking, take care of messages from the attack and changes in invisible.
827     * Returns true if the attack damaged the opponent.
828     * tmp is the targetted monster.
829     * op is what is attacking
830     * string is passed along to describe what messages to describe
831     * the damage.
832     */
833 root 1.9 static int
834     do_skill_attack (object *tmp, object *op, const char *string, object *skill)
835     {
836     if (INVOKE_OBJECT (SKILL_ATTACK, op, ARG_OBJECT (tmp), ARG_STRING (string), ARG_OBJECT (skill)))
837     return RESULT_INT (0);
838    
839     /* For Players only: if there is no ready weapon, and no "attack" skill
840     * is readied either then try to find a skill for the player to use.
841     * it is presumed that if skill is set, it is a valid attack skill (eg,
842     * the caller should have set it appropriately). We still want to pass
843     * through that code if skill is set to change to the skill.
844     */
845 root 1.35 if (player *pl = op->contr)
846 root 1.9 {
847 root 1.35 if (!pl->combat_ob)
848 root 1.9 {
849 root 1.35 if (QUERY_FLAG (op, FLAG_READY_WEAPON))
850 root 1.9 {
851 root 1.35 for (tmp = op->inv; tmp; tmp = tmp->below)
852     if (tmp->type == WEAPON && QUERY_FLAG (tmp, FLAG_APPLIED))
853     break;
854    
855     if (!tmp)
856 root 1.9 {
857 root 1.35 LOG (llevError, "Could not find applied weapon on %s\n", &op->name);
858     return 0;
859     }
860 root 1.9
861 root 1.35 change_skill (op, find_skill_by_name (op, tmp->skill), 1);
862     }
863     else
864     {
865     if (!skill)
866     {
867     /* See if the players chosen skill is a combat skill, and use
868     * it if appropriate.
869     */
870     if (op->chosen_skill && hth_skill_p (op->chosen_skill))
871     skill = op->chosen_skill;
872     else
873 root 1.9 {
874 root 1.35 skill = find_player_hth_skill (op);
875    
876     if (!skill)
877     {
878     new_draw_info (NDI_BLACK, 0, op, "You have no unarmed combat skills!");
879     return 0;
880     }
881 root 1.5 }
882     }
883 root 1.35
884     /* now try to ready the new skill */
885     if (!change_skill (op, skill, 0))
886     { /* oh oh, trouble! */
887 root 1.37 new_draw_info_format (NDI_UNIQUE, 0, op, "Couldn't change to skill %s", &skill->name);
888 root 1.35 return 0;
889     }
890 root 1.5 }
891 root 1.16
892 root 1.35 if (!pl->combat_ob)
893     {
894     LOG (llevError, "Could not find anything to attack on %s\n", &op->name);
895 root 1.16 return 0;
896 root 1.5 }
897 root 1.9 }
898 root 1.35
899 root 1.44 if (!op->change_weapon (pl->combat_ob))
900     return 0;
901 root 1.9
902 root 1.38 /* lose invisiblity/hiding status for running attacks */
903 root 1.35 if (pl->tmp_invis)
904     {
905     pl->tmp_invis = 0;
906     op->invisible = 0;
907     op->hide = 0;
908     update_object (op, UP_OBJ_CHANGE);
909 root 1.5 }
910 elmex 1.1 }
911    
912 root 1.31 int success = attack_ob (tmp, op);
913 root 1.9
914     /* print appropriate messages to the player */
915    
916 root 1.31 if (success && string && tmp && !QUERY_FLAG (tmp, FLAG_FREED))
917 root 1.9 {
918     if (op->type == PLAYER)
919     new_draw_info_format (NDI_UNIQUE, 0, op, "You %s %s!", string, query_name (tmp));
920     else if (tmp->type == PLAYER)
921     new_draw_info_format (NDI_UNIQUE, 0, tmp, "%s %s you!", query_name (op), string);
922     }
923 root 1.30
924 root 1.9 return success;
925     }
926 elmex 1.1
927     /* skill_attack() - Core routine for use when we attack using a skills
928     * system. In essence, this code handles
929     * all skill-based attacks, ie hth, missile and melee weapons should be
930     * treated here. If an opponent is already supplied by move_player(),
931     * we move right onto do_skill_attack(), otherwise we find if an
932     * appropriate opponent exists.
933     *
934     * This is called by move_player() and attack_hth()
935     *
936     * Initial implementation by -bt thomas@astro.psu.edu
937     */
938 root 1.9 int
939     skill_attack (object *tmp, object *pl, int dir, const char *string, object *skill)
940     {
941     sint16 tx, ty;
942 root 1.13 maptile *m;
943 root 1.9 int mflags;
944    
945     if (!dir)
946     dir = pl->facing;
947 root 1.16
948 root 1.9 tx = freearr_x[dir];
949     ty = freearr_y[dir];
950    
951     /* If we don't yet have an opponent, find if one exists, and attack.
952     * Legal opponents are the same as outlined in move_player_attack()
953     */
954 root 1.31 if (!tmp)
955 root 1.9 {
956     m = pl->map;
957     tx = pl->x + freearr_x[dir];
958     ty = pl->y + freearr_y[dir];
959    
960     mflags = get_map_flags (m, &m, tx, ty, &tx, &ty);
961     if (mflags & P_OUT_OF_MAP)
962     return 0;
963    
964     /* space must be blocked for there to be anything interesting to do */
965     if (!OB_TYPE_MOVE_BLOCK (pl, GET_MAP_MOVE_BLOCK (m, tx, ty)))
966     return 0;
967    
968 root 1.21 for (tmp = GET_MAP_OB (m, tx, ty); tmp; tmp = tmp->above)
969 root 1.9 if ((QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->stats.hp >= 0) || QUERY_FLAG (tmp, FLAG_CAN_ROLL) || tmp->type == LOCKED_DOOR)
970     {
971     /* Don't attack party members */
972     if ((pl->type == PLAYER && tmp->type == PLAYER) && (pl->contr->party != NULL && pl->contr->party == tmp->contr->party))
973     return 0;
974 root 1.31
975 root 1.9 break;
976     }
977 elmex 1.1 }
978 root 1.31
979 root 1.9 if (!tmp)
980     {
981     if (pl->type == PLAYER)
982     new_draw_info (NDI_UNIQUE, 0, pl, "There is nothing to attack!");
983 root 1.31
984 root 1.9 return 0;
985 elmex 1.1 }
986    
987 root 1.9 return do_skill_attack (tmp, pl, string, skill);
988 elmex 1.1 }
989    
990     /* attack_hth() - this handles all hand-to-hand attacks -b.t. */
991 root 1.9
992 elmex 1.1 /* July 5, 1995 - I broke up attack_hth() into 2 parts. In the first
993     * (attack_hth) we check for weapon use, etc in the second (the new
994     * function skill_attack() we actually attack.
995     */
996 root 1.9 static int
997     attack_hth (object *pl, int dir, const char *string, object *skill)
998     {
999     object *enemy = NULL, *weapon;
1000 elmex 1.1
1001 root 1.9 if (QUERY_FLAG (pl, FLAG_READY_WEAPON))
1002     for (weapon = pl->inv; weapon; weapon = weapon->below)
1003     {
1004     if (weapon->type == WEAPON && QUERY_FLAG (weapon, FLAG_APPLIED))
1005     {
1006     CLEAR_FLAG (weapon, FLAG_APPLIED);
1007     CLEAR_FLAG (pl, FLAG_READY_WEAPON);
1008 root 1.22 pl->update_stats ();
1009 root 1.9 if (pl->type == PLAYER)
1010     {
1011     new_draw_info (NDI_UNIQUE, 0, pl, "You unwield your weapon in order to attack.");
1012     esrv_update_item (UPD_FLAGS, pl, weapon);
1013     }
1014 root 1.37
1015 root 1.9 break;
1016     }
1017     }
1018 root 1.30
1019 root 1.9 return skill_attack (enemy, pl, dir, string, skill);
1020 elmex 1.1 }
1021    
1022     /* attack_melee_weapon() - this handles melee weapon attacks -b.t.
1023     * For now we are just checking to see if we have a ready weapon here.
1024     * But there is a real neato possible feature of this scheme which
1025     * bears mentioning:
1026     * Since we are only calling this from do_skill() in the future
1027     * we may make this routine handle 'special' melee weapons attacks
1028     * (like disarming manuever with sai) based on player SK_level and
1029     * weapon type.
1030     */
1031 root 1.9 static int
1032     attack_melee_weapon (object *op, int dir, const char *string, object *skill)
1033     {
1034 elmex 1.1
1035 root 1.9 if (!QUERY_FLAG (op, FLAG_READY_WEAPON))
1036     {
1037     if (op->type == PLAYER)
1038     new_draw_info (NDI_UNIQUE, 0, op, "You have no ready weapon to attack with!");
1039 root 1.30
1040 root 1.9 return 0;
1041 elmex 1.1 }
1042 root 1.30
1043 root 1.9 return skill_attack (NULL, op, dir, string, skill);
1044 elmex 1.1
1045     }