… | |
… | |
115 | * tool, this code will equip it. |
115 | * tool, this code will equip it. |
116 | */ |
116 | */ |
117 | object * |
117 | object * |
118 | find_skill_by_name (object *who, const char *name) |
118 | find_skill_by_name (object *who, const char *name) |
119 | { |
119 | { |
120 | object *skill = NULL, *skill_tool = NULL, *tmp; |
|
|
121 | |
|
|
122 | if (!name) |
120 | if (!name) |
123 | return NULL; |
121 | return 0; |
|
|
122 | |
|
|
123 | object *skill = 0, *skill_tool = 0; |
124 | |
124 | |
125 | /* We make sure the length of the string in the object is greater |
125 | /* We make sure the length of the string in the object is greater |
126 | * in length than the passed string. Eg, if we have a skill called |
126 | * in length than the passed string. Eg, if we have a skill called |
127 | * 'hi', we don't want to match if the user passed 'high' |
127 | * 'hi', we don't want to match if the user passed 'high' |
128 | */ |
128 | */ |
129 | for (tmp = who->inv; tmp != NULL; tmp = tmp->below) |
129 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
130 | { |
130 | { |
131 | if (tmp->type == SKILL && !strncasecmp (name, tmp->skill, strlen (name)) && (size_t) strlen (tmp->skill) >= strlen (name)) |
131 | if (tmp->type == SKILL && !strncasecmp (name, tmp->skill, strlen (name)) && strlen (tmp->skill) >= strlen (name)) |
132 | skill = tmp; |
132 | skill = tmp; |
133 | |
133 | |
134 | /* Try to find appropriate skilltool. If the player has one already |
134 | /* Try to find appropriate skilltool. If the player has one already |
135 | * applied, we try to keep using that one. |
135 | * applied, we try to keep using that one. |
136 | */ |
136 | */ |
137 | else if (tmp->type == SKILL_TOOL && !strncasecmp (name, tmp->skill, strlen (name)) && (size_t) strlen (tmp->skill) >= strlen (name)) |
137 | else if (tmp->type == SKILL_TOOL && !strncasecmp (name, tmp->skill, strlen (name)) && strlen (tmp->skill) >= strlen (name)) |
138 | { |
138 | { |
139 | if (QUERY_FLAG (tmp, FLAG_APPLIED)) |
139 | if (QUERY_FLAG (tmp, FLAG_APPLIED)) |
140 | skill_tool = tmp; |
140 | skill_tool = tmp; |
141 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
141 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
142 | skill_tool = tmp; |
142 | skill_tool = tmp; |
143 | } |
143 | } |
144 | } |
144 | } |
|
|
145 | |
145 | /* If this is a skill that can be used without a tool, return it */ |
146 | /* If this is a skill that can be used without a tool, return it */ |
146 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
147 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
147 | return skill; |
148 | return skill; |
148 | |
149 | |
149 | /* Player has a tool to use the skill. IF not applied, apply it - |
150 | /* Player has a tool to use the skill. If not applied, apply it - |
150 | * if not successful, return null. If they do have the skill tool |
151 | * if not successful, return null. If they do have the skill tool |
151 | * but not the skill itself, give it to them. |
152 | * but not the skill itself, give it to them. |
152 | */ |
153 | */ |
153 | if (skill_tool) |
154 | if (skill_tool) |
154 | { |
155 | { |
155 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
156 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
156 | { |
|
|
157 | if (apply_special (who, skill_tool, 0)) |
157 | if (apply_special (who, skill_tool, 0)) |
158 | return NULL; |
158 | return 0; |
159 | } |
|
|
160 | |
159 | |
161 | if (!skill) |
160 | if (!skill) |
162 | { |
161 | { |
163 | skill = give_skill_by_name (who, skill_tool->skill); |
162 | skill = give_skill_by_name (who, skill_tool->skill); |
164 | link_player_skills (who); |
163 | link_player_skills (who); |
165 | } |
164 | } |
166 | |
165 | |
167 | return skill; |
166 | return skill; |
168 | } |
167 | } |
169 | |
168 | |
170 | return NULL; |
169 | return 0; |
171 | } |
170 | } |
172 | |
|
|
173 | |
171 | |
174 | /* This returns the skill pointer of the given name (the |
172 | /* This returns the skill pointer of the given name (the |
175 | * one that accumlates exp, has the level, etc). |
173 | * one that accumlates exp, has the level, etc). |
176 | * |
174 | * |
177 | * It is presumed that the player will be needing to actually |
175 | * It is presumed that the player will be needing to actually |
… | |
… | |
183 | * this replaces find_skill. |
181 | * this replaces find_skill. |
184 | */ |
182 | */ |
185 | object * |
183 | object * |
186 | find_skill_by_number (object *who, int skillno) |
184 | find_skill_by_number (object *who, int skillno) |
187 | { |
185 | { |
|
|
186 | if (!IN_RANGE_EXC (skillno, 1, NUM_SKILLS)) |
|
|
187 | return 0; |
|
|
188 | |
188 | object *skill = NULL, *skill_tool = NULL, *tmp; |
189 | object *skill = NULL, *skill_tool = NULL; |
189 | |
190 | |
190 | if (skillno < 1 || skillno >= NUM_SKILLS) |
|
|
191 | return NULL; |
|
|
192 | |
|
|
193 | for (tmp = who->inv; tmp != NULL; tmp = tmp->below) |
191 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
194 | { |
192 | { |
195 | if (tmp->type == SKILL && tmp->subtype == skillno) |
193 | if (tmp->type == SKILL && tmp->subtype == skillno) |
196 | skill = tmp; |
194 | skill = tmp; |
197 | |
195 | |
198 | /* Try to find appropriate skilltool. If the player has one already |
196 | /* Try to find appropriate skilltool. If the player has one already |
… | |
… | |
204 | skill_tool = tmp; |
202 | skill_tool = tmp; |
205 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
203 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
206 | skill_tool = tmp; |
204 | skill_tool = tmp; |
207 | } |
205 | } |
208 | } |
206 | } |
|
|
207 | |
209 | /* If this is a skill that can be used without a tool, return it */ |
208 | /* If this is a skill that can be used without a tool, return it */ |
210 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
209 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
211 | return skill; |
210 | return skill; |
212 | |
211 | |
213 | /* Player has a tool to use the skill. IF not applied, apply it - |
212 | /* Player has a tool to use the skill. If not applied, apply it - |
214 | * if not successful, return null. If they do have the skill tool |
213 | * if not successful, return null. If they do have the skill tool |
215 | * but not the skill itself, give it to them. |
214 | * but not the skill itself, give it to them. |
216 | */ |
215 | */ |
217 | if (skill_tool) |
216 | if (skill_tool) |
218 | { |
217 | { |
219 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
218 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
… | |
… | |
235 | } |
234 | } |
236 | |
235 | |
237 | /* This changes the objects skill to new_skill. |
236 | /* This changes the objects skill to new_skill. |
238 | * note that this function doesn't always need to get used - |
237 | * note that this function doesn't always need to get used - |
239 | * you can now add skill exp to the player without the chosen_skill being |
238 | * you can now add skill exp to the player without the chosen_skill being |
240 | * set. This function is of most interest to players to update |
239 | * set. This function is of most interest to players to update |
241 | * the various range information. |
240 | * the various range information. |
242 | * return 1 on success, 0 on error |
241 | * return 1 on success, 0 on error |
243 | */ |
242 | */ |
244 | int |
243 | int |
245 | change_skill (object *who, object *new_skill, int flag) |
244 | change_skill (object *who, object *new_skill, int flag) |
246 | { |
245 | { |
247 | if (who->type != PLAYER) |
246 | if (who->type != PLAYER) |
248 | return 0; |
247 | return 0; |
|
|
248 | |
|
|
249 | #if 0 |
|
|
250 | // optimise this supposedly common case |
|
|
251 | if (new_skill == who->chosen_skill) |
|
|
252 | return 1; |
|
|
253 | #endif |
249 | |
254 | |
250 | if (!new_skill) |
255 | if (!new_skill) |
251 | { |
256 | { |
252 | LOG (llevError | logBacktrace, "change_skill called on %s with NULL skill\n", |
257 | LOG (llevError | logBacktrace, "change_skill called on %s with NULL skill\n", |
253 | who->debug_desc ()); |
258 | who->debug_desc ()); |
254 | return 0; |
259 | return 0; |
255 | } |
260 | } |
256 | |
261 | |
257 | player *pl = who->contr; |
262 | // the skill could be readied already because it is used by a weapon. |
258 | |
263 | who->change_weapon (0); |
259 | // move skill to front, so it will be preferred next time |
264 | new_skill->inv_splay (); |
260 | new_skill->remove (); |
|
|
261 | who->insert (new_skill); |
|
|
262 | |
265 | |
263 | if (apply_special (who, new_skill, AP_APPLY)) |
266 | if (apply_special (who, new_skill, AP_APPLY)) |
264 | return 0; |
267 | return 0; |
265 | |
268 | |
266 | return 1; |
269 | return 1; |
… | |
… | |
293 | int |
296 | int |
294 | do_skill (object *op, object *part, object *skill, int dir, const char *string) |
297 | do_skill (object *op, object *part, object *skill, int dir, const char *string) |
295 | { |
298 | { |
296 | int success = 0, exp = 0; |
299 | int success = 0, exp = 0; |
297 | int did_alc = 0; |
300 | int did_alc = 0; |
298 | object *tmp, *next; |
|
|
299 | |
301 | |
300 | if (!skill) |
302 | if (!skill) |
301 | return 0; |
303 | return 0; |
302 | |
304 | |
303 | /* The code below presumes that the skill points to the object that |
305 | /* The code below presumes that the skill points to the object that |
… | |
… | |
305 | * go and try to find the actual real skill pointer, and if the |
307 | * go and try to find the actual real skill pointer, and if the |
306 | * the player doesn't have a bucket for that, create one. |
308 | * the player doesn't have a bucket for that, create one. |
307 | */ |
309 | */ |
308 | if (skill->type != SKILL && op->type == PLAYER) |
310 | if (skill->type != SKILL && op->type == PLAYER) |
309 | { |
311 | { |
310 | for (tmp = op->inv; tmp != NULL; tmp = tmp->below) |
312 | for (object *tmp = op->inv; tmp; tmp = tmp->below) |
311 | if (tmp->type == SKILL && tmp->skill == skill->skill) |
313 | if (tmp->type == SKILL && tmp->skill == skill->skill) |
312 | break; |
314 | { |
|
|
315 | skill = tmp; |
|
|
316 | goto found; |
|
|
317 | } |
313 | |
318 | |
314 | if (!tmp) |
|
|
315 | tmp = give_skill_by_name (op, skill->skill); |
319 | skill = give_skill_by_name (op, skill->skill); |
316 | |
320 | found: ; |
317 | skill = tmp; |
|
|
318 | } |
321 | } |
319 | |
322 | |
320 | // skill, by_whom, on_which_object, which direction, skill_argument |
323 | // skill, by_whom, on_which_object, which direction, skill_argument |
321 | if (INVOKE_OBJECT (USE_SKILL, skill, ARG_OBJECT (op), ARG_OBJECT (part), ARG_INT (dir), ARG_STRING (string))) |
324 | if (INVOKE_OBJECT (USE_SKILL, skill, ARG_OBJECT (op), ARG_OBJECT (part), ARG_INT (dir), ARG_STRING (string))) |
322 | return 0; |
325 | return 0; |
… | |
… | |
427 | case SK_LITERACY: |
430 | case SK_LITERACY: |
428 | case SK_WOODSMAN: |
431 | case SK_WOODSMAN: |
429 | /* first, we try to find a cauldron, and do the alchemy thing. |
432 | /* first, we try to find a cauldron, and do the alchemy thing. |
430 | * failing that, we go and identify stuff. |
433 | * failing that, we go and identify stuff. |
431 | */ |
434 | */ |
432 | for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = next) |
435 | for (object *next, *tmp = GET_MAP_OB (op->map, op->x, op->y); tmp; tmp = next) |
433 | { |
436 | { |
434 | next = tmp->above; |
437 | next = tmp->above; |
435 | |
438 | |
436 | if (QUERY_FLAG (tmp, FLAG_IS_CAULDRON)) |
439 | if (QUERY_FLAG (tmp, FLAG_IS_CAULDRON)) |
437 | { |
440 | { |
… | |
… | |
498 | * If certain skills should take more/less time, that should be |
501 | * If certain skills should take more/less time, that should be |
499 | * in the code for the skill itself. |
502 | * in the code for the skill itself. |
500 | */ |
503 | */ |
501 | |
504 | |
502 | if (op->type == PLAYER) |
505 | if (op->type == PLAYER) |
503 | op->speed_left -= 1.0; |
506 | op->speed_left -= 1.f; |
504 | |
507 | |
505 | /* this is a good place to add experience for successfull use of skills. |
508 | /* this is a good place to add experience for successfull use of skills. |
506 | * Note that add_exp() will figure out player/monster experience |
509 | * Note that add_exp() will figure out player/monster experience |
507 | * gain problems. |
510 | * gain problems. |
508 | */ |
511 | */ |
… | |
… | |
679 | link_player_skills (pl); |
682 | link_player_skills (pl); |
680 | return 1; |
683 | return 1; |
681 | } |
684 | } |
682 | |
685 | |
683 | /* Gives a percentage clipped to 0% -> 100% of a/b. */ |
686 | /* Gives a percentage clipped to 0% -> 100% of a/b. */ |
684 | |
|
|
685 | /* Probably belongs in some global utils-type file? */ |
687 | /* Probably belongs in some global utils-type file? */ |
686 | static int |
688 | static int |
687 | clipped_percent (sint64 a, sint64 b) |
689 | clipped_percent (sint64 a, sint64 b) |
688 | { |
690 | { |
689 | int rv; |
691 | int rv; |
… | |
… | |
782 | * direction that the user is facing. Returns false if we are unable to |
784 | * direction that the user is facing. Returns false if we are unable to |
783 | * change to the requested skill, or were unable to use the skill properly. |
785 | * change to the requested skill, or were unable to use the skill properly. |
784 | * This is tricky because skills can have spaces. We basically roll |
786 | * This is tricky because skills can have spaces. We basically roll |
785 | * our own find_skill_by_name so we can try to do better string matching. |
787 | * our own find_skill_by_name so we can try to do better string matching. |
786 | */ |
788 | */ |
787 | |
|
|
788 | int |
789 | int |
789 | use_skill (object *op, const char *string) |
790 | use_skill (object *op, const char *string) |
790 | { |
791 | { |
791 | object *skop; |
792 | object *skop; |
792 | size_t len; |
793 | size_t len; |
… | |
… | |
930 | LOG (llevError, "Could not find anything to attack on %s\n", &op->name); |
931 | LOG (llevError, "Could not find anything to attack on %s\n", &op->name); |
931 | return 0; |
932 | return 0; |
932 | } |
933 | } |
933 | } |
934 | } |
934 | |
935 | |
935 | op->set_weapon (pl->combat_ob); |
936 | if (!op->change_weapon (pl->combat_ob)) |
|
|
937 | return 0; |
936 | |
938 | |
937 | /* lose invisiblity/hiding status for running attacks */ |
939 | /* lose invisiblity/hiding status for running attacks */ |
938 | if (pl->tmp_invis) |
940 | if (pl->tmp_invis) |
939 | { |
941 | { |
940 | pl->tmp_invis = 0; |
942 | pl->tmp_invis = 0; |