ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.47
Committed: Sun May 13 19:56:57 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.46: +7 -6 lines
Log Message:
*** empty log message ***

File Contents

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