ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.106
Committed: Wed Nov 23 06:05:38 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.105: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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