ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.74
Committed: Sun Jan 4 16:30:39 2009 UTC (15 years, 4 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.73: +24 -14 lines
Log Message:
sanatized alchemy skill.

File Contents

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