ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.41
Committed: Fri May 11 21:07:14 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.40: +0 -5 lines
Log Message:
closer, still hackish

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