ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.52
Committed: Fri May 18 01:01:02 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.51: +10 -28 lines
Log Message:
more skill tuning:
- clear_skill is gone, use change_skill (0) instead.
- change_skill now clears the current_weapon.
- more intelligent weapon switching on unapply.
- be more clever when initially applying weapons
  on player load/connect.
- survive wrong setting of READY_WEAPON.

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