ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.51
Committed: Thu May 17 20:27:02 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.50: +27 -31 lines
Log Message:
- more rewrite:
  change_skill now only changes the chosen_skill, nothing else.
  new flag to suppress skill tool behaviour of readying the skill
  when applying the tool, for use in find_skill.

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