ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.38
Committed: Thu May 3 10:39:46 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.37: +2 -8 lines
Log Message:
beta it slowly into shape

File Contents

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