ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.110
Committed: Sun Nov 18 00:37:11 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.109: +1 -1 lines
Log Message:
some range based for loops

File Contents

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