ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
Revision: 1.109
Committed: Sat Nov 17 23:40:04 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.108: +1 -0 lines
Log Message:
copyright update 2018

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