… | |
… | |
105 | op->contr->ns->last_skill_exp[tmp->subtype] = -1; //TODO: this should go |
105 | op->contr->ns->last_skill_exp[tmp->subtype] = -1; //TODO: this should go |
106 | } |
106 | } |
107 | } |
107 | } |
108 | } |
108 | } |
109 | |
109 | |
|
|
110 | static object * |
|
|
111 | find_skill (object *who, const shstr &sh) |
|
|
112 | { |
|
|
113 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
114 | if (tmp->skill == sh && tmp->type == SKILL) |
|
|
115 | return tmp; |
|
|
116 | |
|
|
117 | return 0; |
|
|
118 | } |
|
|
119 | |
110 | /* This returns the skill pointer of the given name (the |
120 | /* This returns the skill pointer of the given name (the |
111 | * one that accumlates exp, has the level, etc). |
121 | * one that accumulates exp, has the level, etc). |
112 | * |
122 | * |
113 | * It is presumed that the player will be needing to actually |
123 | * It is presumed that the player will be needing to actually |
114 | * use the skill, so thus if use of the skill requires a skill |
124 | * use the skill, so thus if use of the skill requires a skill |
115 | * tool, this code will equip it. |
125 | * tool, this code will equip it. |
116 | */ |
126 | */ |
117 | object * |
127 | object * |
118 | find_skill_by_name (object *who, const char *name) |
128 | find_skill_by_name (object *who, const shstr &sh) |
119 | { |
129 | { |
120 | if (!name) |
130 | object *skill_tool = 0; |
|
|
131 | |
|
|
132 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
133 | if (tmp->skill == sh) |
|
|
134 | { |
|
|
135 | if (tmp->type == SKILL && (tmp->flag [FLAG_CAN_USE_SKILL] || tmp->flag [FLAG_APPLIED])) |
|
|
136 | /* If this is a skill that can be used without applying tool, return it */ |
|
|
137 | return tmp->inv_splay (); |
|
|
138 | /* Try to find appropriate skilltool. If the player has one already |
|
|
139 | * applied, we try to keep using that one. |
|
|
140 | */ |
|
|
141 | else if (tmp->type == SKILL_TOOL && !skill_tool) |
|
|
142 | skill_tool = tmp; |
|
|
143 | } |
|
|
144 | |
|
|
145 | if (!skill_tool) |
121 | return 0; |
146 | return 0; |
122 | |
|
|
123 | object *skill = 0, *skill_tool = 0; |
|
|
124 | |
|
|
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 |
|
|
127 | * 'hi', we don't want to match if the user passed 'high' |
|
|
128 | */ |
|
|
129 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
130 | { |
|
|
131 | if (tmp->type == SKILL && !strncasecmp (name, tmp->skill, strlen (name)) && strlen (tmp->skill) >= strlen (name)) |
|
|
132 | skill = tmp; |
|
|
133 | |
|
|
134 | /* Try to find appropriate skilltool. If the player has one already |
|
|
135 | * applied, we try to keep using that one. |
|
|
136 | */ |
|
|
137 | else if (tmp->type == SKILL_TOOL && !strncasecmp (name, tmp->skill, strlen (name)) && strlen (tmp->skill) >= strlen (name)) |
|
|
138 | { |
|
|
139 | if (QUERY_FLAG (tmp, FLAG_APPLIED)) |
|
|
140 | skill_tool = tmp; |
|
|
141 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
|
|
142 | skill_tool = tmp; |
|
|
143 | } |
|
|
144 | } |
|
|
145 | |
|
|
146 | /* If this is a skill that can be used without a tool, return it */ |
|
|
147 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
|
|
148 | return skill; |
|
|
149 | |
147 | |
150 | /* Player has a tool to use the skill. If not applied, apply it - |
148 | /* Player has a tool to use the skill. If not applied, apply it - |
151 | * if not successful, return null. If they do have the skill tool |
149 | * if not successful, return null. If they do have the skill tool |
152 | * but not the skill itself, give it to them. |
150 | * but not the skill itself, give it to them. |
153 | */ |
151 | */ |
154 | if (skill_tool) |
152 | object *skill = find_skill (who, skill_tool->skill); |
155 | { |
|
|
156 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
|
|
157 | if (apply_special (who, skill_tool, 0)) |
|
|
158 | return 0; |
|
|
159 | |
153 | |
160 | if (!skill) |
154 | if (!skill) |
161 | { |
155 | { |
162 | skill = give_skill_by_name (who, skill_tool->skill); |
156 | skill = give_skill_by_name (who, skill_tool->skill); |
163 | link_player_skills (who); |
157 | link_player_skills (who); |
164 | } |
158 | } |
165 | |
159 | |
|
|
160 | skill->inv_splay (); |
|
|
161 | |
|
|
162 | if (!skill_tool->flag [FLAG_APPLIED]) |
|
|
163 | if (apply_special (who, skill_tool, AP_APPLY | AP_NO_READY)) |
|
|
164 | return 0; |
|
|
165 | |
166 | return skill; |
166 | return skill; |
167 | } |
167 | } |
|
|
168 | |
|
|
169 | object * |
|
|
170 | find_skill_by_name (object *who, const char *name) |
|
|
171 | { |
|
|
172 | if (!name) |
|
|
173 | return 0; |
|
|
174 | |
|
|
175 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
176 | if ((tmp->type == SKILL || tmp->type == SKILL_TOOL) |
|
|
177 | && tmp->skill.begins_with (name)) |
|
|
178 | if (object *skop = find_skill_by_name (who, tmp->skill)) |
|
|
179 | return skop; |
168 | |
180 | |
169 | return 0; |
181 | return 0; |
170 | } |
182 | } |
171 | |
183 | |
172 | /* This returns the skill pointer of the given name (the |
184 | /* This returns the skill pointer of the given name (the |
173 | * one that accumlates exp, has the level, etc). |
185 | * one that accumulates exp, has the level, etc). |
174 | * |
186 | * |
175 | * It is presumed that the player will be needing to actually |
187 | * 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 |
188 | * use the skill, so thus if use of the skill requires a skill |
177 | * tool, this code will equip it. |
189 | * tool, this code will equip it. |
178 | * |
190 | * |
… | |
… | |
181 | * this replaces find_skill. |
193 | * this replaces find_skill. |
182 | */ |
194 | */ |
183 | object * |
195 | object * |
184 | find_skill_by_number (object *who, int skillno) |
196 | find_skill_by_number (object *who, int skillno) |
185 | { |
197 | { |
186 | if (!IN_RANGE_EXC (skillno, 1, NUM_SKILLS)) |
198 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
199 | if (tmp->type == SKILL && tmp->subtype == skillno) |
|
|
200 | if (object *skop = find_skill_by_name (who, tmp->skill)) |
|
|
201 | return skop; |
|
|
202 | |
|
|
203 | return 0; |
|
|
204 | } |
|
|
205 | |
|
|
206 | /* This changes the objects chosen_skill to new_skill. |
|
|
207 | * return 1 on success, 0 on error |
|
|
208 | */ |
|
|
209 | bool |
|
|
210 | object::change_skill (object *new_skill) |
|
|
211 | { |
|
|
212 | if (type != PLAYER) |
187 | return 0; |
213 | return 0; |
188 | |
214 | |
189 | object *skill = NULL, *skill_tool = NULL; |
|
|
190 | |
|
|
191 | for (object *tmp = who->inv; tmp; tmp = tmp->below) |
|
|
192 | { |
|
|
193 | if (tmp->type == SKILL && tmp->subtype == skillno) |
|
|
194 | skill = tmp; |
|
|
195 | |
|
|
196 | /* Try to find appropriate skilltool. If the player has one already |
|
|
197 | * applied, we try to keep using that one. |
|
|
198 | */ |
|
|
199 | else if (tmp->type == SKILL_TOOL && tmp->subtype == skillno) |
|
|
200 | { |
|
|
201 | if (QUERY_FLAG (tmp, FLAG_APPLIED)) |
|
|
202 | skill_tool = tmp; |
|
|
203 | else if (!skill_tool || !QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
|
|
204 | skill_tool = tmp; |
|
|
205 | } |
|
|
206 | } |
|
|
207 | |
|
|
208 | /* If this is a skill that can be used without a tool, return it */ |
|
|
209 | if (skill && QUERY_FLAG (skill, FLAG_CAN_USE_SKILL)) |
|
|
210 | return skill; |
|
|
211 | |
|
|
212 | /* Player has a tool to use the skill. If not applied, apply it - |
|
|
213 | * if not successful, return null. If they do have the skill tool |
|
|
214 | * but not the skill itself, give it to them. |
|
|
215 | */ |
|
|
216 | if (skill_tool) |
|
|
217 | { |
|
|
218 | if (!QUERY_FLAG (skill_tool, FLAG_APPLIED)) |
|
|
219 | { |
|
|
220 | if (apply_special (who, skill_tool, 0)) |
|
|
221 | return NULL; |
|
|
222 | } |
|
|
223 | |
|
|
224 | if (!skill) |
|
|
225 | { |
|
|
226 | skill = give_skill_by_name (who, skill_tool->skill); |
|
|
227 | link_player_skills (who); |
|
|
228 | } |
|
|
229 | |
|
|
230 | return skill; |
|
|
231 | } |
|
|
232 | |
|
|
233 | return NULL; |
|
|
234 | } |
|
|
235 | |
|
|
236 | /* This changes the objects skill to new_skill. |
|
|
237 | * note that this function doesn't always need to get used - |
|
|
238 | * you can now add skill exp to the player without the chosen_skill being |
|
|
239 | * set. This function is of most interest to players to update |
|
|
240 | * the various range information. |
|
|
241 | * return 1 on success, 0 on error |
|
|
242 | */ |
|
|
243 | int |
|
|
244 | change_skill (object *who, object *new_skill, int flag) |
|
|
245 | { |
|
|
246 | if (who->type != PLAYER) |
|
|
247 | return 0; |
|
|
248 | |
|
|
249 | #if 0 |
|
|
250 | // optimise this supposedly common case |
215 | // optimise this supposedly common case |
251 | if (new_skill == who->chosen_skill) |
216 | if (new_skill == chosen_skill) |
252 | return 1; |
217 | return 1; |
253 | #endif |
|
|
254 | |
218 | |
255 | if (!new_skill) |
219 | if (chosen_skill) |
256 | { |
|
|
257 | LOG (llevError | logBacktrace, "change_skill called on %s with NULL skill\n", |
|
|
258 | who->debug_desc ()); |
|
|
259 | return 0; |
|
|
260 | } |
220 | { |
|
|
221 | chosen_skill->flag [FLAG_APPLIED] = false; |
|
|
222 | change_abil (this, chosen_skill); |
|
|
223 | } |
261 | |
224 | |
262 | // the skill could be readied already because it is used by a weapon. |
225 | chosen_skill = new_skill; |
263 | who->change_weapon (0); |
226 | |
|
|
227 | if (chosen_skill) |
|
|
228 | { |
|
|
229 | chosen_skill->flag [FLAG_APPLIED] = true; |
|
|
230 | change_abil (this, chosen_skill); |
264 | new_skill->inv_splay (); |
231 | chosen_skill->inv_splay (); |
|
|
232 | } |
265 | |
233 | |
266 | if (apply_special (who, new_skill, AP_APPLY)) |
234 | update_stats (); |
267 | return 0; |
|
|
268 | |
|
|
269 | return 1; |
235 | return 1; |
270 | } |
236 | } |
271 | |
237 | |
272 | /* This function just clears the chosen_skill and range_skill values |
238 | /* This function just clears the chosen_skill and range_skill values |
273 | * in the player. |
239 | * in the player. |
274 | */ |
240 | */ |
275 | void |
241 | void |
276 | clear_skill (object *who) |
242 | clear_skill (object *who) |
277 | { |
243 | { |
|
|
244 | if (who->chosen_skill) |
|
|
245 | { |
|
|
246 | who->chosen_skill->flag [FLAG_APPLIED] = false; |
278 | who->chosen_skill = 0; |
247 | who->chosen_skill = 0; |
279 | CLEAR_FLAG (who, FLAG_READY_SKILL); |
248 | CLEAR_FLAG (who, FLAG_READY_SKILL); |
|
|
249 | } |
280 | |
250 | |
281 | if (who->type == PLAYER) |
251 | if (player *pl = who->contr) |
282 | if (who->contr->ranged_ob && who->contr->ranged_ob->type == SKILL) |
252 | if (pl->ranged_ob && pl->ranged_ob->type == SKILL) |
283 | who->contr->ranged_ob = 0; |
253 | pl->ranged_ob = 0; |
284 | } |
254 | } |
285 | |
255 | |
286 | /* do_skill() - Main skills use function-similar in scope to cast_spell(). |
256 | /* do_skill() - Main skills use function-similar in scope to cast_spell(). |
287 | * We handle all requests for skill use outside of some combat here. |
257 | * We handle all requests for skill use outside of some combat here. |
288 | * We require a separate routine outside of fire() so as to allow monsters |
258 | * We require a separate routine outside of fire() so as to allow monsters |
… | |
… | |
640 | * player learns the skill, 2 otherwise. |
610 | * player learns the skill, 2 otherwise. |
641 | */ |
611 | */ |
642 | int |
612 | int |
643 | learn_skill (object *pl, object *scroll) |
613 | learn_skill (object *pl, object *scroll) |
644 | { |
614 | { |
645 | object *tmp; |
|
|
646 | |
|
|
647 | if (!scroll->skill) |
615 | if (!scroll->skill) |
648 | { |
616 | { |
649 | LOG (llevError, "skill scroll %s does not have skill pointer set.\n", &scroll->name); |
617 | LOG (llevError, "skill scroll %s does not have skill pointer set.\n", &scroll->name); |
650 | return 2; |
618 | return 2; |
651 | } |
619 | } |
652 | |
620 | |
653 | /* can't use find_skill_by_name because we want skills the player knows |
621 | object *tmp = find_skill (pl, scroll->skill); |
654 | * but can't use natively. |
|
|
655 | */ |
|
|
656 | |
|
|
657 | for (tmp = pl->inv; tmp != NULL; tmp = tmp->below) |
|
|
658 | if (tmp->type == SKILL && !strncasecmp (scroll->skill, tmp->skill, strlen (scroll->skill))) |
|
|
659 | break; |
|
|
660 | |
622 | |
661 | /* player already knows it */ |
623 | /* player already knows it */ |
662 | if (tmp && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL)) |
624 | if (tmp && QUERY_FLAG (tmp, FLAG_CAN_USE_SKILL)) |
663 | return 0; |
625 | return 0; |
664 | |
626 | |
… | |
… | |
793 | size_t len; |
755 | size_t len; |
794 | |
756 | |
795 | if (!string) |
757 | if (!string) |
796 | return 0; |
758 | return 0; |
797 | |
759 | |
798 | for (skop = op->inv; skop != NULL; skop = skop->below) |
760 | for (skop = op->inv; skop; skop = skop->below) |
799 | { |
761 | { |
|
|
762 | if (skop->type == SKILL |
800 | if (skop->type == SKILL && QUERY_FLAG (skop, FLAG_CAN_USE_SKILL) && |
763 | && QUERY_FLAG (skop, FLAG_CAN_USE_SKILL) |
801 | !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill)))) |
764 | && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill)))) |
802 | break; |
765 | break; |
|
|
766 | else if (skop->type == SKILL_TOOL |
803 | else if (skop->type == SKILL_TOOL && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill)))) |
767 | && !strncasecmp (string, skop->skill, MIN (strlen (string), (size_t) strlen (skop->skill)))) |
804 | break; |
768 | break; |
805 | } |
769 | } |
|
|
770 | |
806 | if (!skop) |
771 | if (!skop) |
807 | { |
772 | { |
808 | new_draw_info_format (NDI_UNIQUE, 0, op, "Unable to find skill %s", string); |
773 | new_draw_info_format (NDI_UNIQUE, 0, op, "Unable to find skill %s", string); |
809 | return 0; |
774 | return 0; |
810 | } |
775 | } |
… | |
… | |
815 | * options given to the skill. Its pretty simple - if there |
780 | * options given to the skill. Its pretty simple - if there |
816 | * are extra parameters (as deteremined by string length), we |
781 | * are extra parameters (as deteremined by string length), we |
817 | * want to skip over any leading spaces. |
782 | * want to skip over any leading spaces. |
818 | */ |
783 | */ |
819 | if (len >= strlen (string)) |
784 | if (len >= strlen (string)) |
820 | { |
|
|
821 | string = NULL; |
785 | string = NULL; |
822 | } |
|
|
823 | else |
786 | else |
824 | { |
787 | { |
825 | string += len; |
788 | string += len; |
826 | while (*string == 0x20) |
789 | while (*string == 0x20) |
827 | string++; |
790 | string++; |
|
|
791 | |
828 | if (strlen (string) == 0) |
792 | if (strlen (string) == 0) |
829 | string = NULL; |
793 | string = NULL; |
830 | } |
794 | } |
831 | |
795 | |
832 | #ifdef SKILL_UTIL_DEBUG |
796 | #ifdef SKILL_UTIL_DEBUG |
833 | LOG (llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none"); |
797 | LOG (llevDebug, "use_skill() got skill: %s\n", sknum > -1 ? skills[sknum].name : "none"); |
834 | #endif |
798 | #endif |
835 | |
799 | |
836 | /* Change to the new skill, then execute it. */ |
|
|
837 | if (do_skill (op, op, skop, op->facing, string)) |
800 | if (do_skill (op, op, skop, op->facing, string)) |
838 | return 1; |
801 | return 1; |
839 | |
802 | |
840 | return 0; |
803 | return 0; |
841 | } |
804 | } |
… | |
… | |
893 | { |
856 | { |
894 | LOG (llevError, "Could not find applied weapon on %s\n", &op->name); |
857 | LOG (llevError, "Could not find applied weapon on %s\n", &op->name); |
895 | return 0; |
858 | return 0; |
896 | } |
859 | } |
897 | |
860 | |
898 | change_skill (op, find_skill_by_name (op, tmp->skill), 1); |
861 | pl->combat_ob = tmp; |
899 | } |
862 | } |
900 | else |
863 | else |
901 | { |
864 | { |
902 | if (!skill) |
865 | if (!skill) |
903 | { |
866 | { |
… | |
… | |
916 | return 0; |
879 | return 0; |
917 | } |
880 | } |
918 | } |
881 | } |
919 | } |
882 | } |
920 | |
883 | |
921 | /* now try to ready the new skill */ |
884 | pl->combat_ob = skill; |
922 | if (!change_skill (op, skill, 0)) |
885 | apply_special (op, skill, AP_APPLY); |
923 | { /* oh oh, trouble! */ |
|
|
924 | new_draw_info_format (NDI_UNIQUE, 0, op, "Couldn't change to skill %s", &skill->name); |
|
|
925 | return 0; |
|
|
926 | } |
|
|
927 | } |
886 | } |
928 | |
887 | |
929 | if (!pl->combat_ob) |
888 | if (!pl->combat_ob) |
930 | { |
889 | { |
931 | LOG (llevError, "Could not find anything to attack on %s\n", &op->name); |
890 | LOG (llevError, "Could not find anything to attack on %s\n", &op->name); |
… | |
… | |
1076 | |
1035 | |
1077 | return 0; |
1036 | return 0; |
1078 | } |
1037 | } |
1079 | |
1038 | |
1080 | return skill_attack (NULL, op, dir, string, skill); |
1039 | return skill_attack (NULL, op, dir, string, skill); |
1081 | |
|
|
1082 | } |
1040 | } |
|
|
1041 | |