ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.16
Committed: Wed Dec 6 13:59:01 2006 UTC (17 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +39 -72 lines
Log Message:
- implement and modernize op->insert and op->remove
- simplify find_hth_skill to always use first found
- change change_skill to move recently used skills to top of inventory

File Contents

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