ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.49
Committed: Mon May 14 21:32:27 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.48: +56 -1 lines
Log Message:
breath life into completely broken skill tools

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