Crossfire Server, Trunk  R21189
skill_util.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
32 /* define the following for skills utility debugging */
33 /* #define SKILL_UTIL_DEBUG */
34 
35 #define WANT_UNARMED_SKILLS
36 
37 #include "global.h"
38 
39 #include <assert.h>
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "living.h"
45 #include "object.h"
46 #include "shop.h"
47 #include "skills.h"
48 #include "spells.h"
49 #include "sproto.h"
50 
51 static void attack_hth(object *pl, int dir, const char *string, object *skill);
52 static void attack_melee_weapon(object *op, int dir, const char *string, object *skill);
53 
57 const char *skill_names[MAX_SKILLS];
62 
67 void init_skills(void) {
68  int i;
69  archetype *at;
70 
71  for (i = 0; i < MAX_SKILLS; i++) {
72  skill_names[i] = NULL;
73  skill_faces[i] = -1;
74  }
75  i = 0;
76 
77  for (at = first_archetype; at != NULL; at = at->next) {
78  if (at->clone.type == SKILL) {
79  if (i == MAX_SKILLS) {
80  LOG(llevError, "init_skills: too many skills, increase MAX_SKILLS and rebuild server!");
82  }
84  if (at->clone.face != NULL)
85  skill_faces[i] = at->clone.face->number;
86  i++;
87  }
88  }
89 }
90 
96 int get_skill_client_code(const char *skill_name)
97 {
98  int index;
99  for (index = 0; index < MAX_SKILLS && skill_names[index] != NULL; index++)
100  if (strcmp(skill_names[index], skill_name) == 0)
101  return index;
102 
103  assert("invalid skill!");
104  return 0;
105 }
106 
129 static object *adjust_skill_tool(object *who, object *skill, object *skill_tool) {
130  if (!skill && !skill_tool)
131  return NULL;
132 
133  /* If this is a skill that can be used without a tool and no tool found, return it */
134  if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL) && (!skill_tool || QUERY_FLAG(skill_tool, FLAG_APPLIED) || strcmp(skill->skill, "clawing") == 0))
135  return skill;
136 
137  /* Player has a tool to use the skill. If not applied, apply it -
138  * if not successful, return skill if can be used. If they do have the skill tool
139  * but not the skill itself, give it to them.
140  */
141  if (skill_tool) {
142  if (!QUERY_FLAG(skill_tool, FLAG_APPLIED)) {
143  if (apply_special(who, skill_tool, 0)) {
144  if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL))
145  return skill;
146  else
147  return NULL;
148  }
149  }
150  if (!skill) {
151  skill = give_skill_by_name(who, skill_tool->skill);
152  link_player_skills(who);
153  }
154  return skill;
155  }
156  if (skill && QUERY_FLAG(skill, FLAG_CAN_USE_SKILL))
157  return skill;
158  else
159  return NULL;
160 }
161 
191 object *find_skill_by_name(object *who, const char *name) {
192  object *skill = NULL, *skills[MAX_SKILLS], *skill_tools[MAX_SKILLS];
193  const char *skill_names[MAX_SKILLS];
194  char *ourname=NULL;
195  int num_names, highest_level_skill=0, i;
196 
197  if (!name)
198  return NULL;
199 
200  /* Simple case - no commas in past in name, so don't need to tokenize */
201  if (!strchr(name, ',')) {
202  skill_names[0] = name;
203  skill_tools[0] = NULL;
204  skills[0] = NULL;
205  num_names = 1;
206  } else {
207  /* strtok_r is destructive, so we need our own copy */
208  char *lasts;
209  ourname = strdup(name);
210 
211  if ((skill_names[0] = strtok_r(ourname, ",", &lasts)) == NULL) {
212  /* This should really never happen */
213  LOG(llevError, "find_skill_by_name: strtok_r returned null, but strchr did not?\n");
214  free(ourname);
215  return NULL;
216  } else {
217  skill_tools[0] = NULL;
218  skills[0] = NULL;
219  /* we already have the first name from the strtok_r above */
220  num_names=1;
221  while ((skill_names[num_names] = strtok_r(NULL, ",", &lasts)) != NULL) {
222  /* Clean out any leading spacing. typical string would be
223  * skill1, skill2, skill3, ...
224  */
225  while (isspace(*skill_names[num_names]))
226  skill_names[num_names]++;
227  skills[num_names] = NULL;
228  skill_tools[num_names] = NULL;
229  num_names++;
230  }
231  }
232  /* While we don't use ourname below this point, the skill_names[] points into
233  * it, so we can't free it yet.
234  */
235  }
236 
237  FOR_INV_PREPARE(who, tmp) {
238  /* We make sure the length of the string in the object is greater
239  * in length than the passed string. Eg, if we have a skill called
240  * 'hi', we don't want to match if the user passed 'high'
241  */
242  if (tmp->type == SKILL || tmp->type == SKILL_TOOL) {
243  for (i = 0; i<num_names; i++) {
244  if (!strncasecmp(skill_names[i], tmp->skill, strlen(skill_names[i])) &&
245  strlen(tmp->skill) >= strlen(skill_names[i])) {
246  if (tmp->type == SKILL) {
247  skills[i] = tmp;
248  if (!skill || tmp->level > skill->level) {
249  skill = tmp;
250  highest_level_skill=i;
251  }
252  }
253  else {
254  /* Skill tools don't have levels, so we basically find the
255  * 'best' skill tool for this skill.
256  */
257  if (QUERY_FLAG(tmp, FLAG_APPLIED) || !skill_tools[i] ||
258  !QUERY_FLAG(skill_tools[i], FLAG_APPLIED)) {
259  skill_tools[i] = tmp;
260  }
261  }
262  /* Got a matching name - no reason to look through rest of names */
263  break;
264  }
265  }
266  }
267  } FOR_INV_FINISH();
268  free(ourname);
269  return adjust_skill_tool(who, skills[highest_level_skill], skill_tools[highest_level_skill]);
270 }
271 
290 object *find_skill_by_number(object *who, int skillno) {
291  object *skill = NULL, *skill_tool = NULL;
292 
293  if (skillno <= 0 || skillno > MAX_SKILLS) {
294  return NULL;
295  }
296 
297  FOR_INV_PREPARE(who, tmp) {
298  if (tmp->type == SKILL && tmp->subtype == skillno)
299  skill = tmp;
300 
301  /* Try to find appropriate skilltool. If the player has one already
302  * applied, we try to keep using that one.
303  */
304  else if (tmp->type == SKILL_TOOL && tmp->subtype == skillno) {
305  if (QUERY_FLAG(tmp, FLAG_APPLIED))
306  skill_tool = tmp;
307  else if (!skill_tool || !QUERY_FLAG(skill_tool, FLAG_APPLIED))
308  skill_tool = tmp;
309  }
310  } FOR_INV_FINISH();
311 
312  return adjust_skill_tool(who, skill, skill_tool);
313 }
314 
335 int change_skill(object *who, object *new_skill, int flag) {
336  rangetype old_range;
337 
338  if (who->type != PLAYER)
339  return 0;
340 
341  old_range = who->contr->shoottype;
342 
343  /* The readied skill can be a skill tool, so check on actual skill instead of object. */
344  if (who->chosen_skill && who->chosen_skill->skill == new_skill->skill) {
345  /* optimization for changing skill to current skill */
346  if (!(flag&0x1))
347  who->contr->shoottype = range_skill;
348  return 1;
349  }
350 
351  if (who->chosen_skill)
352  apply_special(who, who->chosen_skill, AP_UNAPPLY|(flag&AP_NOPRINT));
353 
354  /* Only goal in this case was to unapply a skill */
355  if (!new_skill)
356  return 0;
357 
358  if (apply_special(who, new_skill, AP_APPLY|(flag&AP_NOPRINT))) {
359  return 0;
360  }
361  if (flag&0x1)
362  who->contr->shoottype = old_range;
363 
364  return 1;
365 }
366 
374 void clear_skill(object *who) {
375  who->chosen_skill = NULL;
377  if (who->type == PLAYER) {
378  who->contr->ranges[range_skill] = NULL;
379  if (who->contr->shoottype == range_skill)
380  who->contr->shoottype = range_none;
381  }
382 }
383 
407 int do_skill(object *op, object *part, object *skill, int dir, const char *string) {
408  int success = 0, exp = 0;
409 
410  if (!skill)
411  return 0;
412 
413  /* The code below presumes that the skill points to the object that
414  * holds the exp, level, etc of the skill. So if this is a player
415  * go and try to find the actual real skill pointer, and if the
416  * the player doesn't have a bucket for that, create one.
417  */
418  if (skill->type != SKILL && op->type == PLAYER) {
419  object *tmp;
420 
421  tmp = object_find_by_type_and_skill(op, SKILL, skill->skill);
422  if (!tmp) {
423  tmp = give_skill_by_name(op, skill->skill);
424  if (!tmp) {
425  LOG(llevError, "do_skill: asked for skill %s but couldn't find matching SKILL archetype.\n", skill->skill);
426  return 0;
427  }
428  }
429  skill = tmp;
430  }
431 
432  if (skill->anim_suffix)
433  apply_anim_suffix(op, skill->anim_suffix);
434 
435  switch (skill->subtype) {
436  case SK_LEVITATION:
437  /* Not 100% sure if this will work with new movement code -
438  * the levitation skill has move_type for flying, so when
439  * equipped, that should transfer to player, when not,
440  * shouldn't.
441  */
442  if (QUERY_FLAG(skill, FLAG_APPLIED)) {
443  CLEAR_FLAG(skill, FLAG_APPLIED);
445  "You come to earth.");
446  } else {
447  SET_FLAG(skill, FLAG_APPLIED);
449  "You rise into the air!.");
450  }
451  fix_object(op);
452  success = 1;
453  break;
454 
455  case SK_STEALING:
456  exp = success = steal(op, dir, skill);
457  break;
458 
459  case SK_LOCKPICKING:
460  exp = success = pick_lock(op, dir, skill);
461  break;
462 
463  case SK_HIDING:
464  exp = success = hide(op, skill);
465  break;
466 
467  case SK_JUMPING:
468  success = jump(op, dir, skill);
469  break;
470 
471  case SK_INSCRIPTION:
472  exp = success = write_on_item(op, string, skill);
473  break;
474 
475  case SK_MEDITATION:
476  meditate(op, skill);
477  success = 1;
478  break;
479  /* note that the following 'attack' skills gain exp through hit_player() */
480 
481  case SK_KARATE:
482  attack_hth(op, dir, "karate-chopped", skill);
483  break;
484 
485  case SK_PUNCHING:
486  attack_hth(op, dir, "punched", skill);
487  break;
488 
489  case SK_FLAME_TOUCH:
490  attack_hth(op, dir, "flamed", skill);
491  break;
492 
493  case SK_CLAWING:
494  attack_hth(op, dir, "clawed", skill);
495  break;
496 
497  case SK_WRAITH_FEED:
498  attack_hth(op, dir, "fed upon", skill);
499  break;
500 
503  (void)attack_melee_weapon(op, dir, NULL, skill);
504  break;
505 
506  case SK_FIND_TRAPS:
507  exp = success = find_traps(op, skill);
508  break;
509 
510  case SK_SINGING:
511  exp = success = singing(op, dir, skill);
512  break;
513 
514  case SK_ORATORY:
515  exp = success = use_oratory(op, dir, skill);
516  break;
517 
518  case SK_SMITHERY:
519  case SK_BOWYER:
520  case SK_JEWELER:
521  case SK_ALCHEMY:
522  case SK_THAUMATURGY:
523  case SK_LITERACY:
524  case SK_WOODSMAN:
525  if (use_alchemy(op) == 0)
526  exp = success = skill_ident(op, skill);
527  break;
528 
529  case SK_DET_MAGIC:
530  case SK_DET_CURSE:
531  exp = success = skill_ident(op, skill);
532  break;
533 
534  case SK_DISARM_TRAPS:
535  exp = success = remove_trap(op, skill);
536  break;
537 
538  case SK_THROWING:
539  success = skill_throw(op, part, dir, skill);
540  break;
541 
542  case SK_SET_TRAP:
544  "This skill is not currently implemented.");
545  break;
546 
547  case SK_USE_MAGIC_ITEM:
548  case SK_MISSILE_WEAPON:
550  "There is no special attack for this skill.");
551  break;
552 
553  case SK_PRAYING:
554  success = pray(op, skill);
555  break;
556 
557  case SK_BARGAINING:
558  success = shop_describe(op);
559  break;
560 
561  case SK_SORCERY:
562  case SK_EVOCATION:
563  case SK_PYROMANCY:
564  case SK_SUMMONING:
565  case SK_CLIMBING:
566  case SK_EARTH_MAGIC:
567  case SK_AIR_MAGIC:
568  case SK_FIRE_MAGIC:
569  case SK_WATER_MAGIC:
571  "This skill is already in effect.");
572  break;
573 
574  case SK_HARVESTING:
575  do_harvest(op, dir, skill);
576  success = 0;
577  break;
578 
579  default: {
580  char name[MAX_BUF];
581 
582  query_name(op, name, MAX_BUF);
583  LOG(llevDebug, "%s attempted to use unknown skill: %d\n", name, op->chosen_skill->stats.sp);
584  break;
585  }
586  }
587 
588  /* For players we now update the speed_left from using the skill.
589  * Monsters have no skill use time because of the random nature in
590  * which use_monster_skill is called already simulates this.
591  * If certain skills should take more/less time, that should be
592  * in the code for the skill itself.
593  */
594 
595  if (op->type == PLAYER)
596  op->speed_left -= 1.0;
597 
598  /* this is a good place to add experience for successful use of skills.
599  * Note that add_exp() will figure out player/monster experience
600  * gain problems.
601  */
602 
603  if (success && exp)
604  change_exp(op, exp, skill->skill, SK_SUBTRACT_SKILL_EXP);
605 
606  return success;
607 }
608 
641 int64_t calc_skill_exp(const object *who, const object *op, const object *skill) {
642  int64_t op_exp;
643  int op_lvl;
644  float base, value, lvl_mult;
645 
646  if (!skill)
647  skill = who;
648 
649  /* Oct 95 - where we have an object, I expanded our treatment
650  * to 3 cases:
651  * non-living magic obj, runes and everything else.
652  *
653  * If an object is not alive and magical we set the base exp higher to
654  * help out exp awards for skill_ident skills. Also, if
655  * an item is type RUNE, we give out exp based on stats.Cha
656  * and level (this was the old system) -b.t.
657  */
658 
659  if (!op) { /* no item/creature */
660  op_lvl = MAX(who->map->difficulty, 1);
661  op_exp = 0;
662  } else if (op->type == RUNE || op->type == TRAP) { /* all traps. If stats.Cha > 1 we use that
663  * for the amount of experience */
664  op_exp = op->stats.Cha > 1 ? op->stats.Cha : op->stats.exp;
665  op_lvl = op->level;
666  } else { /* all other items/living creatures */
667  op_exp = op->stats.exp;
668  op_lvl = op->level;
669  if (!QUERY_FLAG(op, FLAG_ALIVE)) { /* for ident/make items */
670  op_lvl += 5*abs(op->magic);
671  }
672  }
673 
674  if (op_lvl < 1)
675  op_lvl = 1;
676 
677  if (who->type != PLAYER) { /* for monsters only */
678  return ((int64_t)(op_exp*0.1)+1); /* we add one to insure positive value is returned */
679  }
680 
681  /* for players */
682  base = op_exp;
683  /* if skill really is a skill, then we can look at the skill archetype for
684  * base reward value (exp) and level multiplier factor.
685  */
686  if (skill->type == SKILL) {
687  base += skill->arch->clone.stats.exp;
688  if (settings.simple_exp) {
689  if (skill->arch->clone.level)
690  lvl_mult = (float)skill->arch->clone.level/100.0;
691  else
692  lvl_mult = 1.0; /* no adjustment */
693  } else {
694  if (skill->level)
695  lvl_mult = ((float)skill->arch->clone.level*(float)op_lvl)/((float)skill->level*100.0);
696  else
697  lvl_mult = 1.0;
698  }
699  } else {
700  /* Don't divide by zero here! */
701  lvl_mult = (float)op_lvl/(float)(skill->level ? skill->level : 1);
702  }
703 
704  /* assemble the exp total, and return value */
705 
706  value = base*lvl_mult;
707  if (value < 1)
708  value = 1; /* Always give at least 1 exp point */
709 
710 #ifdef SKILL_UTIL_DEBUG
711  LOG(llevDebug, "calc_skill_exp(): who: %s(lvl:%d) op:%s(lvl:%d)\n", who->name, skill->level, op->name, op_lvl);
712 #endif
713  return ((int64_t)value);
714 }
715 
733 int learn_skill(object *pl, object *scroll) {
734  object *tmp;
735 
736  if (!scroll->skill) {
737  LOG(llevError, "skill scroll %s does not have skill pointer set.\n", scroll->name);
738  return 2;
739  }
740 
741  /* can't use find_skill_by_name because we want skills the player knows
742  * but can't use natively.
743  */
744 
745  tmp = NULL;
746  FOR_INV_PREPARE(pl, inv)
747  if (inv->type == SKILL && !strncasecmp(scroll->skill, inv->skill, strlen(scroll->skill))) {
748  tmp = inv;
749  break;
750  }
751  FOR_INV_FINISH();
752 
753  /* player already knows it */
754  if (tmp && QUERY_FLAG(tmp, FLAG_CAN_USE_SKILL))
755  return 0;
756 
757  /* now a random change to learn, based on player Int.
758  * give bonus based on level - otherwise stupid characters
759  * might never be able to learn anything.
760  */
761  if (random_roll(0, 99, pl, PREFER_LOW) > (get_learn_spell(pl->stats.Int)+(pl->level/5)))
762  return 2; /* failure :< */
763 
764  if (!tmp)
765  tmp = give_skill_by_name(pl, scroll->skill);
766 
767  if (!tmp) {
768  LOG(llevError, "skill scroll %s does not have valid skill name (%s).\n", scroll->name, scroll->skill);
769  return 2;
770  }
771 
773  link_player_skills(pl);
774  return 1;
775 }
776 
788 static int clipped_percent(int64_t a, int64_t b) {
789  int rv;
790 
791  if (b <= 0)
792  return 0;
793 
794  rv = (int)((100.0f*((float)a)/((float)b))+0.5f);
795 
796  if (rv < 0)
797  return 0;
798  else if (rv > 100)
799  return 100;
800 
801  return rv;
802 }
803 
820 void show_skills(object *op, const char *search) {
821  const char *cp;
822  int i, num_skills_found = 0;
823  static const char *const periods = "........................................";
824  /* Need to have a pointer and use strdup for qsort to work properly */
825  char skills[MAX_SKILLS][MAX_BUF];
826 
827  FOR_INV_PREPARE(op, tmp) {
828  if (tmp->type == SKILL) {
829  char buf[MAX_BUF];
830 
831  if (search && strstr(tmp->name, search) == NULL)
832  continue;
833  /* Basically want to fill this out to 40 spaces with periods */
834  snprintf(buf, sizeof(buf), "%s%s", tmp->name, periods);
835  buf[40] = 0;
836 
838  snprintf(skills[num_skills_found++], MAX_BUF, "%slvl:%3d (xp:%"FMT64"/%"FMT64"/%d%%)",
839  buf, tmp->level,
840  tmp->stats.exp,
841  level_exp(tmp->level+1, op->expmul),
842  clipped_percent(tmp->perm_exp, tmp->stats.exp));
843  } else {
844  snprintf(skills[num_skills_found++], MAX_BUF, "%slvl:%3d (xp:%"FMT64"/%"FMT64")",
845  buf, tmp->level,
846  tmp->stats.exp,
847  level_exp(tmp->level+1, op->expmul));
848  }
849  /* I don't know why some characters get a bunch of skills, but
850  * it sometimes happens (maybe a leftover from buggier earlier code
851  * and those character are still about). In any case, lets handle
852  * it so it doesn't crash the server - otherwise, one character may
853  * crash the server numerous times.
854  */
855  if (num_skills_found >= MAX_SKILLS) {
857  "Your character has too many skills. "
858  "Something isn't right - contact the server admin");
859  break;
860  }
861  }
862  } FOR_INV_FINISH();
863 
865  "Player skills:");
866 
867  if (num_skills_found > 1)
868  qsort(skills, num_skills_found, MAX_BUF, (int (*)(const void *, const void *))strcmp);
869 
870  for (i = 0; i < num_skills_found; i++) {
872  skills[i]);
873  }
874 
876  "You can handle %d weapon improvements.",
877  op->level/5+5);
878 
879  cp = determine_god(op);
880  if (strcmp(cp, "none") == 0)
881  cp = NULL;
883  "You worship %s.",
884  cp ? cp : "no god at current time");
885 
887  "Your equipped item power is %d out of %d\n",
888  op->contr->item_power, op->level);
889 }
890 
907 int use_skill(object *op, const char *string) {
908  object *skop;
909  size_t len;
910 
911  if (!string)
912  return 0;
913 
914  skop = NULL;
915  FOR_INV_PREPARE(op, tmp) {
916  if (tmp->type == SKILL
918  && !strncasecmp(string, tmp->skill, MIN(strlen(string), strlen(tmp->skill)))) {
919  skop = tmp;
920  break;
921  } else if (tmp->type == SKILL_TOOL
922  && !strncasecmp(string, tmp->skill, MIN(strlen(string), strlen(tmp->skill)))) {
923  skop = tmp;
924  break;
925  }
926  } FOR_INV_FINISH();
927  if (!skop) {
929  "Unable to find skill %s",
930  string);
931  return 0;
932  }
933 
934  len = strlen(skop->skill);
935 
936  /* All this logic goes and skips over the skill name to find any
937  * options given to the skill. Its pretty simple - if there
938  * are extra parameters (as determined by string length), we
939  * want to skip over any leading spaces.
940  */
941  if (len >= strlen(string)) {
942  string = NULL;
943  } else {
944  string += len;
945  while (*string == 0x20)
946  string++;
947  if (strlen(string) == 0)
948  string = NULL;
949  }
950 
951 #ifdef SKILL_UTIL_DEBUG
952  LOG(llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none");
953 #endif
954 
955  /* Change to the new skill, then execute it. */
956  if (do_skill(op, op, skop, op->facing, string))
957  return 1;
958 
959  return 0;
960 }
961 
984 static object *find_best_player_hth_skill(object *op) {
985  object *best_skill = NULL;
986  int last_skill;
987 
988  if (op->contr->unarmed_skill) {
989  /* command_unarmed_skill() already does these checks, and right
990  * now I do not think there is any way to lose unarmed skills.
991  * But maybe in the future there will be (polymorph?) so handle
992  * it appropriately. MSW 2009-07-03
993  *
994  * Note that the error messages should only print out once when
995  * the initial failure to switch skills happens, so the player
996  * should not get spammed with tons of messages unless they have
997  * no valid unarmed skill
998  */
999 
1000  best_skill = find_skill_by_name(op, op->contr->unarmed_skill);
1001 
1002  if (!best_skill) {
1004  "Unable to find skill %s - using default unarmed skill",
1005  op->contr->unarmed_skill);
1006  } else {
1007  size_t i;
1008 
1009  for (i = 0; i < sizeof(unarmed_skills); i++)
1010  if (best_skill->subtype == unarmed_skills[i])
1011  break;
1012  if (i < sizeof(unarmed_skills))
1013  return(best_skill);
1014  }
1015  /* If for some reason the unarmed_skill is not valid, we fall
1016  * through the processing below.
1017  */
1018  }
1019 
1020 
1021  /* Dragons are a special case - gros 25th July 2006 */
1022  /* Perhaps this special case should be removed and unarmed_skill
1023  * set to clawing for dragon characters? MSW 2009-07-03
1024  */
1025  if (is_dragon_pl(op)) {
1026  object *tmp;
1027 
1028  tmp = find_skill_by_number(op, SK_CLAWING);
1029  if (tmp) /* I suppose it should always be true - but maybe there's
1030  * draconic toothache ? :) */
1031  return tmp;
1032  }
1033 
1034  last_skill = sizeof(unarmed_skills);
1035  FOR_INV_PREPARE(op, tmp) {
1036  if (tmp->type == SKILL) {
1037  int i;
1038 
1039  /* The order in the array is preferred order. So basically,
1040  * we just cut down the number to search - eg, if we find a skill
1041  * early on in flame touch, then we only need to look into the unarmed_array
1042  * to the entry before flame touch - don't care about the entries afterward,
1043  * because they are inferior skills.
1044  * if we end up finding the best skill (i==0) might as well return
1045  * right away - can't get any better than that.
1046  */
1047  for (i = 0; i < last_skill; i++) {
1048  if (tmp->subtype == unarmed_skills[i] && QUERY_FLAG(tmp, FLAG_CAN_USE_SKILL)) {
1049  best_skill = tmp;
1050  last_skill = i;
1051  if (i == 0)
1052  return best_skill;
1053  }
1054  }
1055  }
1056  } FOR_INV_FINISH();
1057  return best_skill;
1058 }
1059 
1073 static void do_skill_attack(object *tmp, object *op, const char *string, object *skill) {
1074  int success;
1075 
1076  /* For Players only: if there is no ready weapon, and no "attack" skill
1077  * is readied either then try to find a skill for the player to use.
1078  * it is presumed that if skill is set, it is a valid attack skill (eg,
1079  * the caller should have set it appropriately). We still want to pass
1080  * through that code if skill is set to change to the skill.
1081  */
1082  if (op->type == PLAYER) {
1083  if (!QUERY_FLAG(op, FLAG_READY_WEAPON)) {
1084  size_t i;
1085 
1086  if (!skill) {
1087  /* See if the players chosen skill is a combat skill, and use
1088  * it if appropriate.
1089  */
1090  if (op->chosen_skill) {
1091  /* the list is 0-terminated, and talismans, which can be in chosen_skill,
1092  * have a subtype of 0, therefore don't check the 0 */
1093  for (i = 0; unarmed_skills[i] != 0; i++)
1094  if (op->chosen_skill->subtype == unarmed_skills[i]) {
1095  skill = op->chosen_skill;
1096  break;
1097  }
1098  }
1099  /* If we didn't find a skill above, look harder for a good skill */
1100  if (!skill) {
1101  skill = find_best_player_hth_skill(op);
1102 
1103  if (!skill) {
1104  draw_ext_info(NDI_BLACK, 0, op,
1106  "You have no unarmed combat skills!");
1107  return;
1108  }
1109  }
1110  }
1111  if (skill != op->chosen_skill) {
1112  /* now try to ready the new skill */
1113  if (!change_skill(op, skill, 1)) { /* oh oh, trouble! */
1116  "Couldn't change to skill %s",
1117  skill->name);
1118  return;
1119  }
1120  }
1121  } else {
1122  /* Seen some crashes below where current_weapon is not set,
1123  * even though the flag says it is. So if current weapon isn't set,
1124  * do some work in trying to find the object to use.
1125  */
1126  if (!op->current_weapon) {
1127  object *tmp;
1128 
1129  LOG(llevError, "Player %s does not have current weapon set but flag_ready_weapon is set\n", op->name);
1131  if (!tmp) {
1132  LOG(llevError, "Could not find applied weapon on %s\n", op->name);
1133  op->current_weapon = NULL;
1134  return;
1135  } else {
1136  char weapon[MAX_BUF];
1137 
1138  query_name(tmp, weapon, MAX_BUF);
1139  op->current_weapon = tmp;
1140  }
1141  }
1142 
1143  /* Has ready weapon - make sure chosen_skill is set up properly */
1144  if (!op->chosen_skill || op->current_weapon->skill != op->chosen_skill->skill) {
1145  object *found_skill = find_skill_by_name(op, op->current_weapon->skill);
1146  assert(found_skill != NULL);
1147  change_skill(op, found_skill, 1);
1148  }
1149  }
1150  }
1151 
1152  /* lose invisibility/hiding status for running attacks */
1153 
1154  if (op->type == PLAYER && op->contr->tmp_invis) {
1155  op->contr->tmp_invis = 0;
1156  op->invisible = 0;
1157  op->hide = 0;
1159  }
1160 
1161  success = attack_ob(tmp, op);
1162 
1163  /*
1164  * print appropriate messages to the player
1165  *
1166  * If no string, then we aren't dealing with the
1167  * skill attack directly, so the message is printed elsewhere.
1168  *
1169  * Only print hit/miss if we've got a Fire+Attack.
1170  * Otherwise, don't print here at all.
1171  */
1172 
1173  if (string != NULL && tmp && !QUERY_FLAG(tmp, FLAG_FREED)) {
1174  char op_name[MAX_BUF];
1175  if (success){
1176  if (op->type == PLAYER) {
1177  query_name(tmp, op_name, MAX_BUF);
1180  "You %s %s!",
1181  string, op_name);
1182  } else if (tmp->type == PLAYER) {
1183  query_name(op, op_name, MAX_BUF);
1186  "%s %s you!",
1187  op_name, string);
1188  }
1189  }
1190  else{
1191  query_name(tmp, op_name, MAX_BUF);
1194  "You miss %s!",
1195  op_name);
1196  }
1197  }
1198 }
1199 
1223 void skill_attack(object *tmp, object *pl, int dir, const char *string, object *skill) {
1224  int16_t tx, ty;
1225  mapstruct *m;
1226  int mflags;
1227 
1228  if (!dir)
1229  dir = pl->facing;
1230  tx = freearr_x[dir];
1231  ty = freearr_y[dir];
1232 
1233  /* If we don't yet have an opponent, find if one exists, and attack.
1234  * Legal opponents are the same as outlined in move_player_attack()
1235  */
1236 
1237  if (tmp == NULL) {
1238  m = pl->map;
1239  tx = pl->x+freearr_x[dir];
1240  ty = pl->y+freearr_y[dir];
1241 
1242  mflags = get_map_flags(m, &m, tx, ty, &tx, &ty);
1243  if (mflags&P_OUT_OF_MAP)
1244  return;
1245 
1246  /* space must be blocked for there to be anything interesting to do */
1247  if (!(mflags&P_IS_ALIVE)
1248  && !OB_TYPE_MOVE_BLOCK(pl, GET_MAP_MOVE_BLOCK(m, tx, ty))) {
1249  return;
1250  }
1251 
1252  FOR_MAP_PREPARE(m, tx, ty, tmp2)
1253  if ((QUERY_FLAG(tmp2, FLAG_ALIVE) && tmp2->stats.hp >= 0)
1254  || QUERY_FLAG(tmp2, FLAG_CAN_ROLL)
1255  || tmp2->type == LOCKED_DOOR) {
1256  /* Don't attack party members */
1257  if ((pl->type == PLAYER && tmp2->type == PLAYER)
1258  && (pl->contr->party != NULL && pl->contr->party == tmp2->contr->party))
1259  return;
1260  tmp = tmp2;
1261  break;
1262  }
1263  FOR_MAP_FINISH();
1264  }
1265  if (!tmp) {
1266  if (pl->type == PLAYER)
1268  "There is nothing to attack!");
1269  return;
1270  }
1271 
1272  do_skill_attack(tmp, pl, string, skill);
1273 }
1274 
1293 static void attack_hth(object *pl, int dir, const char *string, object *skill) {
1294  object *weapon;
1295 
1296  if (QUERY_FLAG(pl, FLAG_READY_WEAPON)) {
1297  weapon = object_find_by_type_applied(pl, WEAPON);
1298  if (weapon != NULL) {
1299  if (apply_special(pl, weapon, AP_UNAPPLY|AP_NOPRINT)) {
1300  char weaponname[MAX_BUF];
1301 
1302  query_name(weapon, weaponname, MAX_BUF);
1305  "You are unable to unwield %s in order to attack with %s.",
1306  weaponname, skill->name);
1307  return;
1308  } else {
1310  "You unwield your weapon in order to attack.");
1311  }
1312  }
1313  }
1314  skill_attack(NULL, pl, dir, string, skill);
1315 }
1316 
1337 static void attack_melee_weapon(object *op, int dir, const char *string, object *skill) {
1338  if (!QUERY_FLAG(op, FLAG_READY_WEAPON)) {
1339  if (op->type == PLAYER)
1341  "You have no ready weapon to attack with!");
1342  return;
1343  }
1344  skill_attack(NULL, op, dir, string, skill);
1345 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:315
#define AP_UNAPPLY
Definition: define.h:613
int8_t Int
Definition: living.h:36
Definition: player.h:92
int apply_special(object *who, object *op, int aflags)
Definition: apply.c:1078
const char * determine_god(object *op)
Definition: gods.c:106
#define UP_OBJ_FACE
Definition: object.h:517
int skill_ident(object *pl, object *skill)
Definition: skills.c:895
int64_t level_exp(int level, double expmul)
Definition: living.c:1847
static void attack_melee_weapon(object *op, int dir, const char *string, object *skill)
Definition: skill_util.c:1337
#define SET_FLAG(xyz, p)
Definition: define.h:223
#define MSG_TYPE_SKILL_LIST
Definition: newclient.h:587
sstring add_refcount(sstring str)
Definition: shstr.c:210
object * object_find_by_type_applied(const object *who, int type)
Definition: object.c:3997
void link_player_skills(object *op)
Definition: player.c:279
static int clipped_percent(int64_t a, int64_t b)
Definition: skill_util.c:788
int attack_ob(object *op, object *hitter)
Definition: attack.c:905
const char * skill_names[MAX_SKILLS]
Definition: skill_util.c:57
void fatal(enum fatal_error err)
Definition: utils.c:597
int shop_describe(const object *op)
Definition: shop.c:1169
int use_skill(object *op, const char *string)
Definition: skill_util.c:907
object clone
Definition: object.h:470
int16_t invisible
Definition: object.h:360
short freearr_x[SIZEOFFREE]
Definition: object.c:65
#define PREFER_LOW
Definition: define.h:602
int pick_lock(object *pl, int dir, object *skill)
Definition: skills.c:386
static object * adjust_skill_tool(object *who, object *skill, object *skill_tool)
Definition: skill_util.c:129
rangetype shoottype
Definition: player.h:99
Definition: object.h:240
Definition: object.h:119
int change_skill(object *who, object *new_skill, int flag)
Definition: skill_util.c:335
#define MSG_TYPE_SKILL_MISSING
Definition: newclient.h:582
object * ranges[range_size]
Definition: player.h:103
uint8_t subtype
Definition: object.h:339
int do_skill(object *op, object *part, object *skill, int dir, const char *string)
Definition: skill_util.c:407
uint8_t hide
Definition: object.h:387
int64_t exp
Definition: living.h:47
double expmul
Definition: object.h:395
#define MSG_TYPE_SKILL_ERROR
Definition: newclient.h:583
int jump(object *pl, int dir, object *skill)
Definition: skills.c:658
int remove_trap(object *op, object *skill)
Definition: skills.c:1283
#define MAX(x, y)
Definition: compat.h:20
void object_update(object *op, int action)
Definition: object.c:1260
int skill_faces[MAX_SKILLS]
Definition: skill_util.c:61
int64_t calc_skill_exp(const object *who, const object *op, const object *skill)
Definition: skill_util.c:641
int16_t sp
Definition: living.h:42
#define NDI_BLACK
Definition: newclient.h:221
#define AP_APPLY
Definition: define.h:612
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
Definition: object.h:465
#define MIN(x, y)
Definition: compat.h:17
#define FLAG_CAN_ROLL
Definition: define.h:254
short freearr_y[SIZEOFFREE]
Definition: object.c:71
partylist * party
Definition: player.h:186
uint16_t number
Definition: face.h:15
object * give_skill_by_name(object *op, const char *skill_name)
Definition: living.c:1752
int use_alchemy(object *op)
Definition: alchemy.c:1034
#define FLAG_READY_SKILL
Definition: define.h:334
Definition: object.h:241
struct obj * chosen_skill
Definition: object.h:386
int16_t y
Definition: object.h:326
#define NDI_RED
Definition: newclient.h:224
int skill_throw(object *op, object *part, int dir, object *skill)
Definition: skills.c:2244
#define strtok_r(x, y, z)
Definition: win32.h:62
#define FLAG_ALIVE
Definition: define.h:230
#define MSG_TYPE_VICTIM
Definition: newclient.h:392
float speed_left
Definition: object.h:329
signed short int16_t
Definition: win32.h:160
#define FLAG_CAN_USE_SKILL
Definition: define.h:322
const char * anim_suffix
Definition: object.h:316
uint32_t tmp_invis
Definition: player.h:125
int hide(object *op, object *skill)
Definition: skills.c:491
struct mapdef * map
Definition: object.h:297
int is_dragon_pl(const object *op)
Definition: player.c:114
#define snprintf
Definition: win32.h:46
static void attack_hth(object *pl, int dir, const char *string, object *skill)
Definition: skill_util.c:1293
#define FOR_INV_FINISH()
Definition: define.h:714
#define FMT64
Definition: compat.h:12
const char * name
Definition: object.h:311
#define MSG_TYPE_VICTIM_WAS_HIT
Definition: newclient.h:647
int steal(object *op, int dir, object *skill)
Definition: skills.c:276
struct obj * current_weapon
Definition: object.h:370
int8_t Cha
Definition: living.h:36
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:447
#define AP_NOPRINT
Definition: define.h:623
#define P_OUT_OF_MAP
Definition: map.h:251
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:192
#define MSG_TYPE_ATTACK_MISS
Definition: newclient.h:615
static object * find_best_player_hth_skill(object *op)
Definition: skill_util.c:984
struct pl * contr
Definition: object.h:276
#define MSG_TYPE_SKILL_FAILURE
Definition: newclient.h:585
uint8_t simple_exp
Definition: global.h:259
void do_harvest(object *pl, int dir, object *skill)
Definition: c_misc.c:2246
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
void change_exp(object *op, int64_t exp, const char *skill_name, int flag)
Definition: living.c:2101
#define MAX_BUF
Definition: define.h:35
#define MSG_TYPE_ATTACK
Definition: newclient.h:385
#define MSG_TYPE_ATTACK_DID_HIT
Definition: newclient.h:607
int16_t x
Definition: object.h:326
int strncasecmp(const char *s1, const char *s2, int n)
Definition: porting.c:224
const char * skill
Definition: object.h:321
void init_skills(void)
Definition: skill_util.c:67
int learn_skill(object *pl, object *scroll)
Definition: skill_util.c:733
uint16_t difficulty
Definition: map.h:343
signed __int64 int64_t
Definition: win32.h:168
#define FLAG_READY_WEAPON
Definition: define.h:335
#define FOR_MAP_FINISH()
Definition: define.h:767
uint8_t permanent_exp_ratio
Definition: global.h:254
Definition: object.h:107
int get_skill_client_code(const char *skill_name)
Definition: skill_util.c:96
int pray(object *pl, object *skill)
Definition: skills.c:1356
object * object_find_by_type_and_skill(const object *who, int type, const char *skill)
Definition: object.c:4097
Definition: object.h:143
void show_skills(object *op, const char *search)
Definition: skill_util.c:820
living stats
Definition: object.h:368
struct archt * arch
Definition: object.h:412
int singing(object *pl, int dir, object *skill)
Definition: skills.c:1119
uint8_t type
Definition: object.h:338
struct Settings settings
Definition: init.c:40
struct archt * next
Definition: object.h:467
rangetype
Definition: player.h:15
#define FLAG_APPLIED
Definition: define.h:235
int find_traps(object *pl, object *skill)
Definition: skills.c:1212
#define MSG_TYPE_SKILL
Definition: newclient.h:383
char * strdup(const char *str)
Definition: porting.c:200
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:310
#define NDI_UNIQUE
Definition: newclient.h:245
void apply_anim_suffix(object *who, sstring suffix)
Definition: anim.c:319
int use_oratory(object *pl, int dir, object *skill)
Definition: skills.c:965
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
int16_t item_power
Definition: player.h:117
int get_learn_spell(int stat)
Definition: living.c:2284
object * find_skill_by_name(object *who, const char *name)
Definition: skill_util.c:191
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
#define MSG_TYPE_SKILL_SUCCESS
Definition: newclient.h:584
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:623
#define MAX_SKILLS
Definition: skills.h:70
#define SK_SUBTRACT_SKILL_EXP
Definition: skills.h:81
Definition: map.h:325
#define P_IS_ALIVE
Definition: map.h:237
int random_roll(int min, int max, const object *op, int goodbad)
Definition: utils.c:42
const New_Face * face
Definition: object.h:332
void clear_skill(object *who)
Definition: skill_util.c:374
object * find_skill_by_number(object *who, int skillno)
Definition: skill_util.c:290
int write_on_item(object *pl, const char *params, object *skill)
Definition: skills.c:1735
const char * unarmed_skill
Definition: player.h:202
int16_t level
Definition: object.h:351
int8_t facing
Definition: object.h:335
void fix_object(object *op)
Definition: living.c:1119
void meditate(object *pl, object *skill)
Definition: skills.c:1404
EXTERN archetype * first_archetype
Definition: global.h:122
void skill_attack(object *tmp, object *pl, int dir, const char *string, object *skill)
Definition: skill_util.c:1223
int8_t magic
Definition: object.h:348
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
static void do_skill_attack(object *tmp, object *op, const char *string, object *skill)
Definition: skill_util.c:1073
#define FLAG_FREED
Definition: define.h:233