ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.c
Revision: 1.5
Committed: Mon May 1 12:56:18 2006 UTC (18 years ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
Renaming skills and fixing non-static declaration of attack_hth in
include/sproto.h.

File Contents

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