ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skill_util.C
(Generate patch)

Comparing deliantra/server/server/skill_util.C (file contents):
Revision 1.2 by root, Fri Aug 25 17:11:53 2006 UTC vs.
Revision 1.102 by root, Sat Apr 23 04:56:57 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines