Debian Codegraph @

server/skill_util.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Various skill-related functions.
17 *
18 * Created July 95 to separate skill utilities from actual skills -b.t.
19 *
20 * Reconfigured skills code to allow linking of skills to experience
21 * categories. This is done solely through the init_new_exp_system() fctn.
22 * June/July 1995 -b.t. [email protected]
23 *
24 *
25 * July 1995 - Initial associated skills coding. Experience gains
26 * come solely from the use of skills. Overcoming an opponent (in combat,
27 * finding/disarming a trap, stealing from somebeing, etc) gains
28 * experience. Calc_skill_exp() handles the gained experience using
29 * modifications in the skills[] table. - b.t.
30 */
31
32 /* define the following for skills utility debugging */
33 /* #define SKILL_UTIL_DEBUG */
34
35 #define WANT_UNARMED_SKILLS
36
37 #include "global.h"
38
39 #include <assert.h>
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <math.h>
44
45 #include "living.h"
46 #include "object.h"
47 #include "shop.h"
48 #include "skills.h"
49 #include "spells.h"
50 #include "sproto.h"
51
52 static void attack_hth(object *pl, int dir, const char *string, object *skill);
53 static void attack_melee_weapon(object *op, int dir, const char *string, object *skill);
54
55 /**
56 * Will contain a number-name mapping for skills, initialized by init_skills().
57 */
58 const char *skill_names[MAX_SKILLS];
59 /**
60 * Will contain the face numbers for the skills, initialized by init_skill().
61 */
62 const Face *skill_faces[MAX_SKILLS];
63
64 /**
65 * This just sets up the ::skill_names table above. The index into the array
66 * is simply the order the skill is found.
67 */
68 void init_skills(void) {
69 int i;
70 archetype *at;
71
72 for (i = 0; i < MAX_SKILLS; i++) {
73 skill_names[i] = NULL;
74 skill_faces[i] = NULL;
75 }
76 i = 0;
77
78 for (at = first_archetype; at != NULL; at = at->next) {
79 if (at->clone.type == SKILL) {
80 if (i == MAX_SKILLS) {
81 LOG(llevError, "init_skills: too many skills, increase MAX_SKILLS and rebuild server!");
82 fatal(SEE_LAST_ERROR);
83 }
84 skill_names[i] = add_refcount(at->clone.skill);
85 if (at->clone.face != NULL)
86 skill_faces[i] = at->clone.face;
87 i++;
88 }
89 }
90 }
91
92 /**
93 * Return the code of the skill for a client, the index in the skill_names array.
94 * @param skill_name skill name.
95 * @return code, 0 if invalid and asserts.
96 */
97 int get_skill_client_code(const char *skill_name)
98 {
99 int index;
100 for (index = 0; index < MAX_SKILLS && skill_names[index] != NULL; index++)
101 if (strcmp(skill_names[index], skill_name) == 0)
102 return index;
103
104 assert("invalid skill!");
105 return 0;
106 }
107
108 /**
109 * This returns specified skill if it can be used, potentially using tool to help.
110 *
111 * Skill and tool can't be NULL at the same time.
112 *
113 * Factored from find_skill_by_name() and find_skill_by_number().
114 *
115 * The skill tool will be applied if not NULL, so the player can benefit from its bonuses.
116 *
117 * @param who
118 * player trying to ready a skill.
119 * @param skill
120 * skill to ready. Can be NULL.
121 * @param skill_tool
122 * skill tool. Can be NULL.
123 * @return
124 * skill object if it can be used, NULL else.
125 * @note
126 * clawing skill is special, as claws are declared as SKILLTOOL but shouldn't be applied.
127 * @todo
128 * rewrite some.
129 */
130 static object *adjust_skill_tool(object *who, object *skill, object *skill_tool) {
131 if (!skill && !skill_tool)
132 return NULL;
133
134 /* If this is a skill that can be used without a tool and no tool found, return it */
135 if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL) && (!skill_tool || QUERY_FLAG(skill_tool, FLAG_APPLIED) || strcmp(skill->skill, "clawing") == 0))
136 return skill;
137
138 /* Player has a tool to use the skill. If not applied, apply it -
139 * if not successful, return skill if can be used. If they do have the skill tool
140 * but not the skill itself, give it to them.
141 */
142 if (skill_tool) {
143 if (!QUERY_FLAG(skill_tool, FLAG_APPLIED)) {
144 if (apply_special(who, skill_tool, 0)) {
145 if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL))
146 return skill;
147 else
148 return NULL;
149 }
150 }
151 if (!skill) {
152 skill = give_skill_by_name(who, skill_tool->skill);
153 link_player_skills(who);
154 }
155 return skill;
156 }
157 if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL))
158 return skill;
159 else
160 return NULL;
161 }
162
163 /**
164 * This returns the skill pointer of the given name (the
165 * one that accumulates exp, has the level, etc).
166 *
167 * It is presumed that the player will be needing to actually
168 * use the skill, so a skill tool will be equipped if one if found
169 * to benefit from its bonuses.
170 *
171 * This code is basically the same as find_skill_by_number() below,
172 * but instead of a skill number, we search by the name.
173 *
174 * Because a multiple skill names are allowed, we have to use arrays
175 * to store the intermediate results - we can't have skill_tool
176 * point to praying with skill pointing to fire magic.
177 *
178 * @param who
179 * Player to get skill.
180 * @param name
181 * skill to find. Needs not to be a shared string. name can be a comma
182 * separated list of skill names - in that case, we will find the skill
183 * of the highest level.
184 * @return
185 * pointer to skill object, or NULL if player doesn't have it.
186 *
187 * @todo
188 * Maybe better selection of skill when choice of multiple skills is in
189 * use (highest level may not be the best answer?)
190 *
191 */
192 object *find_skill_by_name(object *who, const char *name) {
193 object *skill = NULL, *skills[MAX_SKILLS], *skill_tools[MAX_SKILLS];
194 const char *skill_names[MAX_SKILLS];
195 char *ourname=NULL;
196 int num_names, highest_level_skill=0, i;
197
198 if (!name)
199 return NULL;
200
201 /* Simple case - no commas in past in name, so don't need to tokenize */
202 if (!strchr(name, ',')) {
203 skill_names[0] = name;
204 skill_tools[0] = NULL;
205 skills[0] = NULL;
206 num_names = 1;
207 } else {
208 /* strtok_r is destructive, so we need our own copy */
209 char *lasts;
210 ourname = strdup(name);
211
212 if ((skill_names[0] = strtok_r(ourname, ",", &lasts)) == NULL) {
213 /* This should really never happen */
214 LOG(llevError, "find_skill_by_name: strtok_r returned null, but strchr did not?\n");
215 free(ourname);
216 return NULL;
217 } else {
218 skill_tools[0] = NULL;
219 skills[0] = NULL;
220 /* we already have the first name from the strtok_r above */
221 num_names=1;
222 while ((skill_names[num_names] = strtok_r(NULL, ",", &lasts)) != NULL) {
223 /* Clean out any leading spacing. typical string would be
224 * skill1, skill2, skill3, ...
225 */
226 while (isspace(*skill_names[num_names]))
227 skill_names[num_names]++;
228 skills[num_names] = NULL;
229 skill_tools[num_names] = NULL;
230 num_names++;
231 }
232 }
233 /* While we don't use ourname below this point, the skill_names[] points into
234 * it, so we can't free it yet.
235 */
236 }
237
238 FOR_INV_PREPARE(who, tmp) {
239 /* We make sure the length of the string in the object is greater
240 * in length than the passed string. Eg, if we have a skill called
241 * 'hi', we don't want to match if the user passed 'high'
242 */
243 if (tmp->type == SKILL || (tmp->type == SKILL_TOOL && !QUERY_FLAG(tmp, FLAG_UNPAID))) {
244 for (i = 0; i<num_names; i++) {
245 if (!strncasecmp(skill_names[i], tmp->skill, strlen(skill_names[i])) &&
246 strlen(tmp->skill) >= strlen(skill_names[i])) {
247 if (tmp->type == SKILL) {
248 skills[i] = tmp;
249 if (!skill || tmp->level > skill->level) {
250 skill = tmp;
251 highest_level_skill=i;
252 }
253 }
254 else {
255 /* Skill tools don't have levels, so we basically find the
256 * 'best' skill tool for this skill.
257 */
258 if (QUERY_FLAG(tmp, FLAG_APPLIED) || !skill_tools[i] ||
259 !QUERY_FLAG(skill_tools[i], FLAG_APPLIED)) {
260 skill_tools[i] = tmp;
261 }
262 }
263 /* Got a matching name - no reason to look through rest of names */
264 break;
265 }
266 }
267 }
268 } FOR_INV_FINISH();
269 free(ourname);
270 return adjust_skill_tool(who, skills[highest_level_skill], skill_tools[highest_level_skill]);
271 }
272
273 /**
274 * This returns the first skill pointer of the given subtype (the
275 * one that accumulates exp, has the level, etc).
276 *
277 * It is presumed that the player will be needing to actually
278 * use the skill, so a skill tool will be equipped if one if found
279 * to benefit from its bonuses.
280 *
281 * This code is basically the same as find_skill_by_name() above,
282 * but instead of a skill name, we search by matching subtype.
283 *
284 * Warning: skill subtypes are not unique to skills, various skills (eg
285 * harvesting-like) will share the same subtype, so this function should
286 * only be used if the skill's subtype is known to be used only by one skill.
287 *
288 * @param who
289 * player applying a skill.
290 * @param skillno
291 * skill subtype.
292 * @return
293 * skill object if player can use it, NULL else.
294 */
295 object *find_skill_by_number(object *who, int skillno) {
296 object *skill = NULL, *skill_tool = NULL;
297
298 if (skillno <= 0 || skillno > MAX_SKILLS) {
299 return NULL;
300 }
301
302 FOR_INV_PREPARE(who, tmp) {
303 if (tmp->type == SKILL && tmp->subtype == skillno)
304 skill = tmp;
305
306 /* Try to find appropriate skilltool. If the player has one already
307 * applied, we try to keep using that one.
308 */
309 else if (tmp->type == SKILL_TOOL && tmp->subtype == skillno) {
310 if (QUERY_FLAG(tmp, FLAG_APPLIED))
311 skill_tool = tmp;
312 else if (!skill_tool || !QUERY_FLAG(skill_tool, FLAG_APPLIED))
313 skill_tool = tmp;
314 }
315 } FOR_INV_FINISH();
316
317 return adjust_skill_tool(who, skill, skill_tool);
318 }
319
320 /**
321 * This changes the object's skill to new_skill.
322 * Note that this function doesn't always need to get used -
323 * you can now add skill exp to the player without the chosen_skill being
324 * set. This function is of most interest to players to update
325 * the various range information.
326 *
327 * @param who
328 * living to change skill for.
329 * @param new_skill
330 * skill to use. If NULL, this just unapplies the current skill.
331 * @param flag
332 * has the current meaning:
333 * - 0x1: If set, don't update the range pointer. This is useful when we
334 * need to ready a new skill, but don't want to clobber range.
335 * @retval 0
336 * change failure.
337 * @retval 1
338 * success
339 */
340 int change_skill(object *who, object *new_skill, int flag) {
341 rangetype old_range;
342
343 if (who->type != PLAYER)
344 return 0;
345
346 old_range = who->contr->shoottype;
347
348 /* The readied skill can be a skill tool, so check on actual skill instead of object. */
349 if (who->chosen_skill && who->chosen_skill->skill == new_skill->skill) {
350 /* optimization for changing skill to current skill */
351 if (!(flag&0x1))
352 who->contr->shoottype = range_skill;
353 return 1;
354 }
355
356 if (who->chosen_skill)
357 apply_special(who, who->chosen_skill, AP_UNAPPLY|(flag&AP_NOPRINT));
358
359 /* Only goal in this case was to unapply a skill */
360 if (!new_skill)
361 return 0;
362
363 if (apply_special(who, new_skill, AP_APPLY|(flag&AP_NOPRINT))) {
364 return 0;
365 }
366 if (flag&0x1)
367 who->contr->shoottype = old_range;
368
369 return 1;
370 }
371
372 /**
373 * This function just clears the chosen_skill and range_skill values
374 * in the player.
375 *
376 * @param who
377 * living to clear.
378 */
379 void clear_skill(object *who) {
380 who->chosen_skill = NULL;
381 CLEAR_FLAG(who, FLAG_READY_SKILL);
382 if (who->type == PLAYER) {
383 who->contr->ranges[range_skill] = NULL;
384 if (who->contr->shoottype == range_skill)
385 who->contr->shoottype = range_none;
386 }
387 }
388
389 /**
390 * Main skills use function-similar in scope to cast_spell().
391 * We handle all requests for skill use outside of some combat here.
392 * We require a separate routine outside of fire() so as to allow monsters
393 * to utilize skills.
394 *
395 * This is changed (2002-11-30) from the old method that returned
396 * exp - no caller needed that info, but it also prevented the callers
397 * from know if a skill was actually used, as many skills don't
398 * give any exp for their direct use (eg, throwing).
399 *
400 * Gives experience if appropriate.
401 *
402 * @param op The object actually using the skill
403 * @param part actual part using the skill, used by throwing for monsters
404 * @param skill The skill used by op
405 * @param dir The direction in which the skill is used
406 * @param string A parameter string, necessary to use some skills
407 * @retval 0
408 * skill failure.
409 * @retval 1
410 * use of the skill was successful.
411 */
412 int do_skill(object *op, object *part, object *skill, int dir, const char *string) {
413 int success = 0, exp = 0;
414
415 if (!skill)
416 return 0;
417
418 /* The code below presumes that the skill points to the object that
419 * holds the exp, level, etc of the skill. So if this is a player
420 * go and try to find the actual real skill pointer, and if the
421 * the player doesn't have a bucket for that, create one.
422 */
423 if (skill->type != SKILL && op->type == PLAYER) {
424 object *tmp;
425
426 tmp = object_find_by_type_and_skill(op, SKILL, skill->skill);
427 if (!tmp) {
428 tmp = give_skill_by_name(op, skill->skill);
429 if (!tmp) {
430 LOG(llevError, "do_skill: asked for skill %s but couldn't find matching SKILL archetype.\n", skill->skill);
431 return 0;
432 }
433 }
434 skill = tmp;
435 }
436
437 if (skill->anim_suffix)
438 apply_anim_suffix(op, skill->anim_suffix);
439
440 switch (skill->subtype) {
441 case SK_LEVITATION:
442 /* Not 100% sure if this will work with new movement code -
443 * the levitation skill has move_type for flying, so when
444 * equipped, that should transfer to player, when not,
445 * shouldn't.
446 */
447 if (QUERY_FLAG(skill, FLAG_APPLIED)) {
448 CLEAR_FLAG(skill, FLAG_APPLIED);
449 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
450 "You come to earth.");
451 } else {
452 SET_FLAG(skill, FLAG_APPLIED);
453 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
454 "You rise into the air!.");
455 }
456 fix_object(op);
457 success = 1;
458 break;
459
460 case SK_STEALING:
461 exp = success = steal(op, dir, skill);
462 break;
463
464 case SK_LOCKPICKING:
465 exp = success = pick_lock(op, dir, skill);
466 break;
467
468 case SK_HIDING:
469 exp = success = hide(op, skill);
470 break;
471
472 case SK_JUMPING:
473 success = jump(op, dir, skill);
474 break;
475
476 case SK_INSCRIPTION:
477 exp = success = write_on_item(op, string, skill);
478 break;
479
480 case SK_MEDITATION:
481 meditate(op, skill);
482 success = 1;
483 break;
484 /* note that the following 'attack' skills gain exp through hit_player() */
485
486 case SK_KARATE:
487 attack_hth(op, dir, "karate-chopped", skill);
488 break;
489
490 case SK_PUNCHING:
491 attack_hth(op, dir, "punched", skill);
492 break;
493
494 case SK_FLAME_TOUCH:
495 attack_hth(op, dir, "flamed", skill);
496 break;
497
498 case SK_CLAWING:
499 attack_hth(op, dir, "clawed", skill);
500 break;
501
502 case SK_WRAITH_FEED:
503 attack_hth(op, dir, "fed upon", skill);
504 break;
505
506 case SK_ONE_HANDED_WEAPON:
507 case SK_TWO_HANDED_WEAPON:
508 (void)attack_melee_weapon(op, dir, NULL, skill);
509 break;
510
511 case SK_FIND_TRAPS:
512 exp = success = find_traps(op, skill);
513 break;
514
515 case SK_SINGING:
516 exp = success = singing(op, dir, skill);
517 break;
518
519 case SK_ORATORY:
520 exp = success = use_oratory(op, dir, skill);
521 break;
522
523 case SK_SMITHERY:
524 case SK_BOWYER:
525 case SK_JEWELER:
526 case SK_ALCHEMY:
527 case SK_THAUMATURGY:
528 case SK_LITERACY:
529 case SK_WOODSMAN:
530 if (use_alchemy(op) == 0)
531 exp = success = skill_ident(op, skill);
532 break;
533
534 case SK_DET_MAGIC:
535 case SK_DET_CURSE:
536 exp = success = skill_ident(op, skill);
537 break;
538
539 case SK_DISARM_TRAPS:
540 exp = success = remove_trap(op, skill);
541 break;
542
543 case SK_THROWING:
544 success = skill_throw(op, part, dir, skill);
545 break;
546
547 case SK_SET_TRAP:
548 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
549 "This skill is not currently implemented.");
550 break;
551
552 case SK_USE_MAGIC_ITEM:
553 case SK_MISSILE_WEAPON:
554 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
555 "There is no special attack for this skill.");
556 break;
557
558 case SK_PRAYING:
559 success = pray(op, skill);
560 break;
561
562 case SK_BARGAINING:
563 success = shop_describe(op);
564 break;
565
566 case SK_SORCERY:
567 case SK_EVOCATION:
568 case SK_PYROMANCY:
569 case SK_SUMMONING:
570 case SK_CLIMBING:
571 case SK_EARTH_MAGIC:
572 case SK_AIR_MAGIC:
573 case SK_FIRE_MAGIC:
574 case SK_WATER_MAGIC:
575 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
576 "This skill is already in effect.");
577 break;
578
579 case SK_HARVESTING:
580 do_harvest(op, dir, skill);
581 success = 0;
582 break;
583
584 default: {
585 char name[MAX_BUF];
586
587 query_name(op, name, MAX_BUF);
588 LOG(llevDebug, "%s attempted to use unknown skill: %d\n", name, op->chosen_skill->stats.sp);
589 break;
590 }
591 }
592
593 /* For players we now update the speed_left from using the skill.
594 * Monsters have no skill use time because of the random nature in
595 * which use_monster_skill is called already simulates this.
596 * If certain skills should take more/less time, that should be
597 * in the code for the skill itself.
598 */
599
600 if (op->type == PLAYER)
601 op->speed_left -= 1.0;
602
603 /* this is a good place to add experience for successful use of skills.
604 * Note that add_exp() will figure out player/monster experience
605 * gain problems.
606 */
607
608 if (success && exp)
609 change_exp(op, exp, skill->skill, SK_SUBTRACT_SKILL_EXP);
610
611 return success;
612 }
613
614 /**
615 * Calculates amount of experience can be gained for
616 * successful use of a skill.
617 *
618 * Here we take the view that a player must 'overcome an opponent'
619 * in order to gain experience. Examples include foes killed combat,
620 * finding/disarming a trap, stealing from somebeing, etc.
621 *
622 * The gained experience is based primarily on the difference in levels,
623 * exp point value of vanquished foe, the relevant stats of the skill being
624 * used and modifications in the skills[] table.
625 *
626 * For now, monsters and players will be treated differently. Below I give
627 * the algorithm for *PLAYER *experience gain. Monster exp gain is simpler.
628 * Monsters just get 10% of the exp of the opponent.
629 *
630 * Players get a ratio, eg, opponent lvl / player level. This is then
631 * multiplied by various things. If simple exp is true, then
632 * this multiplier, include the level difference, is always 1.
633 * This revised method prevents some cases where there are big gaps
634 * in the amount you get just because you are now equal level vs lower
635 * level
636 *
637 * @param who
638 * player/creature that used the skill.
639 * @param op
640 * object that was 'defeated'.
641 * @param skill
642 * used skill. If none, it should just point back to who or be NULL.
643 * @return
644 * experience for the skill use.
645 */
646 int64_t calc_skill_exp(const object *who, const object *op, const object *skill) {
647 int64_t op_exp;
648 int op_lvl;
649 float base, value, lvl_mult;
650
651 if (!skill)
652 skill = who;
653
654 /* Oct 95 - where we have an object, I expanded our treatment
655 * to 3 cases:
656 * non-living magic obj, runes and everything else.
657 *
658 * If an object is not alive and magical we set the base exp higher to
659 * help out exp awards for skill_ident skills. Also, if
660 * an item is type RUNE, we give out exp based on stats.Cha
661 * and level (this was the old system) -b.t.
662 */
663
664 if (!op) { /* no item/creature */
665 op_lvl = MAX(who->map->difficulty, 1);
666 op_exp = 0;
667 } else if (op->type == RUNE || op->type == TRAP) { /* all traps. If stats.Cha > 1 we use that
668 * for the amount of experience */
669 op_exp = op->stats.Cha > 1 ? op->stats.Cha : op->stats.exp;
670 op_lvl = op->level;
671 } else { /* all other items/living creatures */
672 op_exp = op->stats.exp;
673 op_lvl = op->level;
674 if (!QUERY_FLAG(op, FLAG_ALIVE)) { /* for ident/make items */
675 op_lvl += 5*abs(op->magic);
676 }
677 }
678
679 if (op_lvl < 1)
680 op_lvl = 1;
681
682 if (who->type != PLAYER) { /* for monsters only */
683 return ((int64_t)(op_exp*0.1)+1); /* we add one to insure positive value is returned */
684 }
685
686 /* for players */
687 base = op_exp;
688 /* if skill really is a skill, then we can look at the skill archetype for
689 * base reward value (exp) and level multiplier factor.
690 */
691 if (skill->type == SKILL) {
692 base += skill->arch->clone.stats.exp;
693 if (settings.simple_exp) {
694 if (skill->arch->clone.level)
695 lvl_mult = (float)skill->arch->clone.level/100.0;
696 else
697 lvl_mult = 1.0; /* no adjustment */
698 } else {
699 if (skill->level)
700 lvl_mult = ((float)skill->arch->clone.level*(float)op_lvl)/((float)skill->level*100.0);
701 else
702 lvl_mult = 1.0;
703 }
704 } else {
705 /* Don't divide by zero here! */
706 lvl_mult = (float)op_lvl/(float)(skill->level ? skill->level : 1);
707 }
708
709 /* assemble the exp total, and return value */
710
711 value = base*lvl_mult;
712 if (value < 1)
713 value = 1; /* Always give at least 1 exp point */
714
715 #ifdef SKILL_UTIL_DEBUG
716 LOG(llevDebug, "calc_skill_exp(): who: %s(lvl:%d) op:%s(lvl:%d)\n", who->name, skill->level, op->name, op_lvl);
717 #endif
718 return ((int64_t)value);
719 }
720
721 /**
722 * Player is trying to learn a skill. Success is based on Int.
723 *
724 * This inserts the requested skill in the player's
725 * inventory. The skill field of the scroll should have the
726 * exact name of the requested skill.
727 *
728 * This one actually teaches the player the skill as something
729 * they can equip.
730 *
731 * @retval 0
732 * player already knows the skill.
733 * @retval 1
734 * the player learns the skill.
735 * @retval 2
736 * some failure.
737 */
738 int learn_skill(object *pl, object *scroll) {
739 object *tmp;
740
741 if (!scroll->skill) {
742 LOG(llevError, "skill scroll %s does not have skill pointer set.\n", scroll->name);
743 return 2;
744 }
745
746 /* can't use find_skill_by_name because we want skills the player knows
747 * but can't use natively.
748 */
749
750 tmp = NULL;
751 FOR_INV_PREPARE(pl, inv)
752 if (inv->type == SKILL && !strncasecmp(scroll->skill, inv->skill, strlen(scroll->skill))) {
753 tmp = inv;
754 break;
755 }
756 FOR_INV_FINISH();
757
758 /* player already knows it */
759 if (tmp && QUERY_FLAG(tmp, FLAG_CAN_USE_SKILL))
760 return 0;
761
762 /* now a random change to learn, based on player Int.
763 * give bonus based on level - otherwise stupid characters
764 * might never be able to learn anything.
765 */
766 if (random_roll(0, 99, pl, PREFER_LOW) > (get_learn_spell(pl->stats.Int)+(pl->level/5)))
767 return 2; /* failure :< */
768
769 if (!tmp)
770 tmp = give_skill_by_name(pl, scroll->skill);
771
772 if (!tmp) {
773 LOG(llevError, "skill scroll %s does not have valid skill name (%s).\n", scroll->name, scroll->skill);
774 return 2;
775 }
776
777 SET_FLAG(tmp, FLAG_CAN_USE_SKILL);
778 link_player_skills(pl);
779 return 1;
780 }
781
782 /**
783 * Gives a percentage clipped to 0% -> 100% of a/b.
784 *
785 * @param a
786 * current value
787 * @param b
788 * max value
789 * @return
790 * value between 0 and 100.
791 * @todo Probably belongs in some global utils-type file?
792 */
793 static int clipped_percent(int64_t a, int64_t b) {
794 int rv;
795
796 if (b <= 0)
797 return 0;
798
799 rv = (int)((100.0f*((float)a)/((float)b))+0.5f);
800
801 if (rv < 0)
802 return 0;
803 else if (rv > 100)
804 return 100;
805
806 return rv;
807 }
808
809
810 /**
811 * Gives the number of digits that %ld will print for a long int
812 *
813 * @param num
814 * any integer (negative values not supported)
815 * @return
816 * number of characters to expect
817 */
818 static int digits_in_long(int64_t num)
819 {
820 return num == 0 ? 1 : floor( log10( labs(num) ) ) + 1;
821 }
822
823 /**
824 * Displays a player's skill list, and some other non skill related info (god,
825 * max weapon improvements, item power).
826 *
827 * This shows the amount of exp they have in the skills.
828 *
829 * Note this function is a bit more complicated because we
830 * we want ot sort the skills before printing them. If we
831 * just dumped this as we found it, this would be a bit
832 * simpler.
833 *
834 * @param op
835 * player wanting to examine skills.
836 * @param parms
837 * optional parameters, usually a string to restrict skills to show.
838 */
839 void show_skills(object *op, const char *parms) {
840 const char *cp;
841 int i, num_skills_found = 0;
842 /* Need to have a pointer and use strdup for qsort to work properly */
843 char skills[MAX_SKILLS][MAX_BUF];
844 const char *search = parms;
845 bool long_format = false;
846
847 if ( parms && strncmp(parms,"-l",2) == 0 ) {
848 long_format = true;
849 search += 2;
850 while ( *search && *search != ' ' ) ++search; /* move past other parameters */
851 while ( *search == ' ' ) ++search;
852 }
853 FOR_INV_PREPARE(op, tmp) {
854 if (tmp->type == SKILL) {
855 if (search && strstr(tmp->name, search) == NULL)
856 continue;
857
858 if (settings.permanent_exp_ratio) {
859 if ( long_format ) {
860 int perm_level = exp_level(tmp->total_exp * settings.permanent_exp_ratio / 100);
861 snprintf(skills[num_skills_found++], MAX_BUF, "[fixed]%-20slvl:%3d (xp:%"FMT64"/%"FMT64")%-*sperm lvl:%3d (xp:%"FMT64"/%"FMT64") ",
862 tmp->name, tmp->level,
863 tmp->stats.exp,
864 level_exp(tmp->level+1, op->expmul),
865 1+2*digits_in_long(op->stats.exp)-digits_in_long(tmp->stats.exp)-digits_in_long(level_exp(tmp->level+1, op->expmul)),
866 "",
867 perm_level,
868 tmp->total_exp * settings.permanent_exp_ratio / 100,
869 level_exp(perm_level+1, op->expmul));
870 } else {
871 snprintf(skills[num_skills_found++], MAX_BUF, "[fixed]%-20slvl:%3d (xp:%"FMT64"/%"FMT64"/%d%%)",
872 tmp->name, tmp->level,
873 tmp->stats.exp,
874 level_exp(tmp->level+1, op->expmul),
875 clipped_percent(PERM_EXP(tmp->total_exp), tmp->stats.exp));
876 }
877 } else {
878 snprintf(skills[num_skills_found++], MAX_BUF, "[fixed]%-20slvl:%3d (xp:%"FMT64"/%"FMT64")",
879 tmp->name, tmp->level,
880 tmp->stats.exp,
881 level_exp(tmp->level+1, op->expmul));
882 }
883 /* I don't know why some characters get a bunch of skills, but
884 * it sometimes happens (maybe a leftover from buggier earlier code
885 * and those character are still about). In any case, lets handle
886 * it so it doesn't crash the server - otherwise, one character may
887 * crash the server numerous times.
888 */
889 if (num_skills_found >= MAX_SKILLS) {
890 draw_ext_info(NDI_RED, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
891 "Your character has too many skills.\n"
892 "Something isn't right - contact the server admin");
893 break;
894 }
895 }
896 } FOR_INV_FINISH();
897
898 if ( search && *search ) {
899 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_LIST,
900 "Player skills%s: (matching '%s')", long_format ? " (long format)":"",search);
901 } else {
902 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_LIST,
903 "Player skills%s:", long_format ? " (long format)":"");
904 }
905
906 if (num_skills_found > 1)
907 qsort(skills, num_skills_found, MAX_BUF, (int (*)(const void *, const void *))strcmp);
908
909 for (i = 0; i < num_skills_found; i++) {
910 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_LIST,
911 skills[i]);
912 }
913
914 cp = determine_god(op);
915 if (strcmp(cp, "none") == 0)
916 cp = NULL;
917
918 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_LIST,
919 "You can handle %d weapon improvements.\n"
920 "You worship %s.\n"
921 "Your equipped item power is %d out of %d\n",
922 op->level/5+5,
923 cp ? cp : "no god at current time",
924 op->contr->item_power, op->level);
925 }
926
927 /**
928 * Similar to invoke command, it executes the skill in the
929 * direction that the user is facing.
930 *
931 * This is tricky because skills can have spaces. We basically roll
932 * our own find_skill_by_name so we can try to do better string matching.
933 *
934 * @param op
935 * player trying to use a skill.
936 * @param string
937 * parameter for the skill to use.
938 * @retval 0
939 * unable to change to the requested skill, or unable to use the skill properly.
940 * @retval 1
941 * skill correctly used.
942 */
943 int use_skill(object *op, const char *string) {
944 object *skop;
945 size_t len;
946
947 if (!string)
948 return 0;
949
950 skop = NULL;
951 FOR_INV_PREPARE(op, tmp) {
952 if (tmp->type == SKILL
953 && QUERY_FLAG(tmp, FLAG_CAN_USE_SKILL)
954 && !strncasecmp(string, tmp->skill, MIN(strlen(string), strlen(tmp->skill)))) {
955 skop = tmp;
956 break;
957 } else if (tmp->type == SKILL_TOOL
958 && !QUERY_FLAG(tmp, FLAG_UNPAID) /* Holy symbols could be used unpaid w/o this */
959 && !strncasecmp(string, tmp->skill, MIN(strlen(string), strlen(tmp->skill)))) {
960 skop = tmp;
961 break;
962 }
963 } FOR_INV_FINISH();
964 if (!skop) {
965 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
966 "Unable to find skill %s",
967 string);
968 return 0;
969 }
970
971 len = strlen(skop->skill);
972
973 /* All this logic goes and skips over the skill name to find any
974 * options given to the skill. Its pretty simple - if there
975 * are extra parameters (as determined by string length), we
976 * want to skip over any leading spaces.
977 */
978 if (len >= strlen(string)) {
979 string = NULL;
980 } else {
981 string += len;
982 while (*string == 0x20)
983 string++;
984 if (strlen(string) == 0)
985 string = NULL;
986 }
987
988 #ifdef SKILL_UTIL_DEBUG
989 LOG(llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none");
990 #endif
991
992 /* Change to the new skill, then execute it. */
993 if (do_skill(op, op, skop, op->facing, string))
994 return 1;
995
996 return 0;
997 }
998
999 /**
1000 * Finds the best unarmed skill the player has, and returns
1001 * it. Best can vary a little - we consider clawing to always
1002 * be the best for dragons.
1003 *
1004 * This could be more intelligent, eg, look at the skill level
1005 * of the skill and go from there (eg, a level 40 puncher is
1006 * is probably better than level 1 karate). OTOH, if you
1007 * don't bother to set up your skill properly, that is the players
1008 * problem (although, it might be nice to have a preferred skill
1009 * field the player can set.
1010 *
1011 * Unlike the old code, we don't give out any skills - it is
1012 * possible you just don't have any ability to get into unarmed
1013 * combat. If everyone race/class should have one, this should
1014 * be handled in the starting treasurelists, not in the code.
1015 *
1016 * @param op
1017 * player to get skill for.
1018 * @return
1019 * attack skill, NULL if no suitable found.
1020 */
1021 static object *find_best_player_hth_skill(object *op) {
1022 object *best_skill = NULL;
1023 int last_skill;
1024
1025 if (op->contr->unarmed_skill) {
1026 /* command_unarmed_skill() already does these checks, and right
1027 * now I do not think there is any way to lose unarmed skills.
1028 * But maybe in the future there will be (polymorph?) so handle
1029 * it appropriately. MSW 2009-07-03
1030 *
1031 * Note that the error messages should only print out once when
1032 * the initial failure to switch skills happens, so the player
1033 * should not get spammed with tons of messages unless they have
1034 * no valid unarmed skill
1035 */
1036
1037 best_skill = find_skill_by_name(op, op->contr->unarmed_skill);
1038
1039 if (!best_skill) {
1040 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
1041 "Unable to find skill %s - using default unarmed skill",
1042 op->contr->unarmed_skill);
1043 } else {
1044 size_t i;
1045
1046 for (i = 0; i < sizeof(unarmed_skills); i++)
1047 if (best_skill->subtype == unarmed_skills[i])
1048 break;
1049 if (i < sizeof(unarmed_skills))
1050 return(best_skill);
1051 }
1052 /* If for some reason the unarmed_skill is not valid, we fall
1053 * through the processing below.
1054 */
1055 }
1056
1057
1058 /* Dragons are a special case - gros 25th July 2006 */
1059 /* Perhaps this special case should be removed and unarmed_skill
1060 * set to clawing for dragon characters? MSW 2009-07-03
1061 */
1062 if (is_dragon_pl(op)) {
1063 object *tmp;
1064
1065 tmp = find_skill_by_number(op, SK_CLAWING);
1066 if (tmp) /* I suppose it should always be true - but maybe there's
1067 * draconic toothache ? :) */
1068 return tmp;
1069 }
1070
1071 last_skill = sizeof(unarmed_skills);
1072 FOR_INV_PREPARE(op, tmp) {
1073 if (tmp->type == SKILL) {
1074 int i;
1075
1076 /* The order in the array is preferred order. So basically,
1077 * we just cut down the number to search - eg, if we find a skill
1078 * early on in flame touch, then we only need to look into the unarmed_array
1079 * to the entry before flame touch - don't care about the entries afterward,
1080 * because they are inferior skills.
1081 * if we end up finding the best skill (i==0) might as well return
1082 * right away - can't get any better than that.
1083 */
1084 for (i = 0; i < last_skill; i++) {
1085 if (tmp->subtype == unarmed_skills[i] && QUERY_FLAG(tmp, FLAG_CAN_USE_SKILL)) {
1086 best_skill = tmp;
1087 last_skill = i;
1088 if (i == 0)
1089 return best_skill;
1090 }
1091 }
1092 }
1093 } FOR_INV_FINISH();
1094 return best_skill;
1095 }
1096
1097 /**
1098 * We have got an appropriate opponent from either
1099 * move_player_attack() or skill_attack(). In this part we get on with
1100 * attacking, take care of messages from the attack and changes in invisible.
1101 * @param tmp
1102 * targeted monster.
1103 * @param op
1104 * what is attacking.
1105 * @param string
1106 * describes the damage ("claw", "punch", ...).
1107 * @param skill
1108 * skill used to damage.
1109 */
1110 static void do_skill_attack(object *tmp, object *op, const char *string, object *skill) {
1111 int success;
1112
1113 /* For Players only: if there is no ready weapon, and no "attack" skill
1114 * is readied either then try to find a skill for the player to use.
1115 * it is presumed that if skill is set, it is a valid attack skill (eg,
1116 * the caller should have set it appropriately). We still want to pass
1117 * through that code if skill is set to change to the skill.
1118 */
1119 if (op->type == PLAYER) {
1120 if (!QUERY_FLAG(op, FLAG_READY_WEAPON)) {
1121 size_t i;
1122
1123 if (!skill) {
1124 /* See if the players chosen skill is a combat skill, and use
1125 * it if appropriate.
1126 */
1127 if (op->chosen_skill) {
1128 /* the list is 0-terminated, and talismans, which can be in chosen_skill,
1129 * have a subtype of 0, therefore don't check the 0 */
1130 for (i = 0; unarmed_skills[i] != 0; i++)
1131 if (op->chosen_skill->subtype == unarmed_skills[i]) {
1132 skill = op->chosen_skill;
1133 break;
1134 }
1135 }
1136 /* If we didn't find a skill above, look harder for a good skill */
1137 if (!skill) {
1138 skill = find_best_player_hth_skill(op);
1139
1140 if (!skill) {
1141 draw_ext_info(NDI_BLACK, 0, op,
1142 MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
1143 "You have no unarmed combat skills!");
1144 return;
1145 }
1146 }
1147 }
1148 if (skill != op->chosen_skill) {
1149 /* now try to ready the new skill */
1150 if (!change_skill(op, skill, 1)) { /* oh oh, trouble! */
1151 draw_ext_info_format(NDI_UNIQUE, 0, tmp,
1152 MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1153 "Couldn't change to skill %s",
1154 skill->name);
1155 return;
1156 }
1157 }
1158 } else {
1159 /* Seen some crashes below where current_weapon is not set,
1160 * even though the flag says it is. So if current weapon isn't set,
1161 * do some work in trying to find the object to use.
1162 */
1163 if (!op->current_weapon) {
1164 object *tmp;
1165
1166 LOG(llevError, "Player %s does not have current weapon set but flag_ready_weapon is set\n", op->name);
1167 tmp = object_find_by_type_applied(op, WEAPON);
1168 if (!tmp) {
1169 LOG(llevError, "Could not find applied weapon on %s\n", op->name);
1170 op->current_weapon = NULL;
1171 return;
1172 } else {
1173 char weapon[MAX_BUF];
1174
1175 query_name(tmp, weapon, MAX_BUF);
1176 op->current_weapon = tmp;
1177 }
1178 }
1179
1180 /* Has ready weapon - make sure chosen_skill is set up properly */
1181 if (!op->chosen_skill || op->current_weapon->skill != op->chosen_skill->skill) {
1182 object *found_skill = find_skill_by_name(op, op->current_weapon->skill);
1183 assert(found_skill != NULL);
1184 change_skill(op, found_skill, 1);
1185 }
1186 }
1187 }
1188
1189 /* lose invisibility/hiding status for running attacks */
1190
1191 if (op->type == PLAYER && op->contr->tmp_invis) {
1192 op->contr->tmp_invis = 0;
1193 op->invisible = 0;
1194 op->hide = 0;
1195 object_update(op, UP_OBJ_FACE);
1196 }
1197
1198 success = attack_ob(tmp, op);
1199
1200 /*
1201 * print appropriate messages to the player
1202 *
1203 * If no string, then we aren't dealing with the
1204 * skill attack directly, so the message is printed elsewhere.
1205 *
1206 * Only print hit/miss if we've got a Fire+Attack.
1207 * Otherwise, don't print here at all.
1208 */
1209
1210 if (string != NULL && tmp && !QUERY_FLAG(tmp, FLAG_FREED)) {
1211 char op_name[MAX_BUF];
1212 if (success){
1213 if (op->type == PLAYER) {
1214 query_name(tmp, op_name, MAX_BUF);
1215 draw_ext_info_format(NDI_UNIQUE, 0, op,
1216 MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
1217 "You %s %s!",
1218 string, op_name);
1219 } else if (tmp->type == PLAYER) {
1220 query_name(op, op_name, MAX_BUF);
1221 draw_ext_info_format(NDI_UNIQUE, 0, tmp,
1222 MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
1223 "%s %s you!",
1224 op_name, string);
1225 }
1226 }
1227 else{
1228 query_name(tmp, op_name, MAX_BUF);
1229 draw_ext_info_format(NDI_UNIQUE, 0, op,
1230 MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_MISS,
1231 "You miss %s!",
1232 op_name);
1233 }
1234 }
1235 }
1236
1237 /**
1238 * Core routine for use when we attack using a skills system.
1239 * In essence, this code handles
1240 * all skill-based attacks, ie hth, missile and melee weapons should be
1241 * treated here. If an opponent is already supplied by move_player(),
1242 * we move right onto do_skill_attack(), otherwise we find if an
1243 * appropriate opponent exists.
1244 *
1245 * This is called by move_player() and attack_hth()
1246 *
1247 * Initial implementation by -bt [email protected]
1248 *
1249 * @param tmp
1250 * victim. Can be NULL.
1251 * @param pl
1252 * who is attacking.
1253 * @param dir
1254 * direction to attack.
1255 * @param string
1256 * describes the damage ("claw", "punch", ...).
1257 * @param skill
1258 * attack skill.
1259 */
1260 void skill_attack(object *tmp, object *pl, int dir, const char *string, object *skill) {
1261 int16_t tx, ty;
1262 mapstruct *m;
1263 int mflags;
1264
1265 if (!dir)
1266 dir = pl->facing;
1267 tx = freearr_x[dir];
1268 ty = freearr_y[dir];
1269
1270 /* If we don't yet have an opponent, find if one exists, and attack.
1271 * Legal opponents are the same as outlined in move_player_attack()
1272 */
1273
1274 if (tmp == NULL) {
1275 m = pl->map;
1276 tx = pl->x+freearr_x[dir];
1277 ty = pl->y+freearr_y[dir];
1278
1279 mflags = get_map_flags(m, &m, tx, ty, &tx, &ty);
1280 if (mflags&P_OUT_OF_MAP)
1281 return;
1282
1283 /* space must be blocked for there to be anything interesting to do */
1284 if (!(mflags&P_IS_ALIVE)
1285 && !OB_TYPE_MOVE_BLOCK(pl, GET_MAP_MOVE_BLOCK(m, tx, ty))) {
1286 return;
1287 }
1288
1289 FOR_MAP_PREPARE(m, tx, ty, tmp2)
1290 if ((QUERY_FLAG(tmp2, FLAG_ALIVE) && tmp2->stats.hp >= 0)
1291 || QUERY_FLAG(tmp2, FLAG_CAN_ROLL)
1292 || tmp2->type == LOCKED_DOOR) {
1293 /* Don't attack party members */
1294 if ((pl->type == PLAYER && tmp2->type == PLAYER)
1295 && (pl->contr->party != NULL && pl->contr->party == tmp2->contr->party))
1296 return;
1297 tmp = tmp2;
1298 break;
1299 }
1300 FOR_MAP_FINISH();
1301 }
1302 if (!tmp) {
1303 if (pl->type == PLAYER)
1304 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1305 "There is nothing to attack!");
1306 return;
1307 }
1308
1309 do_skill_attack(tmp, pl, string, skill);
1310 }
1311
1312 /**
1313 * This handles all hand-to-hand attacks.
1314 *
1315 * Will unapply equipped weapons if needed.
1316 *
1317 * July 5, 1995 - I broke up attack_hth() into 2 parts. In the first
1318 * (attack_hth) we check for weapon use, etc in the second (the new
1319 * function skill_attack() we actually attack.
1320 *
1321 * @param pl
1322 * object attacking.
1323 * @param dir
1324 * attack direction.
1325 * @param string
1326 * describes the attack ("claw", "punch", ...).
1327 * @param skill
1328 * attack skill used.
1329 */
1330 static void attack_hth(object *pl, int dir, const char *string, object *skill) {
1331 object *weapon;
1332
1333 if (QUERY_FLAG(pl, FLAG_READY_WEAPON)) {
1334 weapon = object_find_by_type_applied(pl, WEAPON);
1335 if (weapon != NULL) {
1336 if (apply_special(pl, weapon, AP_UNAPPLY|AP_NOPRINT)) {
1337 char weaponname[MAX_BUF];
1338
1339 query_name(weapon, weaponname, MAX_BUF);
1340 draw_ext_info_format(NDI_UNIQUE, 0, pl,
1341 MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1342 "You are unable to unwield %s in order to attack with %s.",
1343 weaponname, skill->name);
1344 return;
1345 } else {
1346 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1347 "You unwield your weapon in order to attack.");
1348 }
1349 }
1350 }
1351 skill_attack(NULL, pl, dir, string, skill);
1352 }
1353
1354 /**
1355 * This handles melee weapon attacks -b.t.
1356 *
1357 * For now we are just checking to see if we have a ready weapon here.
1358 * But there is a real neato possible feature of this scheme which
1359 * bears mentioning:
1360 * Since we are only calling this from do_skill() in the future
1361 * we may make this routine handle 'special' melee weapons attacks
1362 * (like disarming maneuver with sai) based on player SK_level and
1363 * weapon type.
1364 *
1365 * @param op
1366 * living thing attacking.
1367 * @param dir
1368 * attack direction.
1369 * @param string
1370 * describes the attack ("claw", "punch", ...).
1371 * @param skill
1372 * attack skill used.
1373 */
1374 static void attack_melee_weapon(object *op, int dir, const char *string, object *skill) {
1375 if (!QUERY_FLAG(op, FLAG_READY_WEAPON)) {
1376 if (op->type == PLAYER)
1377 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1378 "You have no ready weapon to attack with!");
1379 return;
1380 }
1381 skill_attack(NULL, op, dir, string, skill);
1382 }