ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.40
Committed: Fri May 11 20:06:33 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.39: +12 -2 lines
Log Message:
fix typoes

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