1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
9 | * the terms of the Affero GNU General Public License as published by the |
… | |
… | |
255 | return spop; |
255 | return spop; |
256 | |
256 | |
257 | return 0; |
257 | return 0; |
258 | } |
258 | } |
259 | |
259 | |
260 | /* |
260 | /* |
261 | * Look at object 'op' and see if they know the spell |
261 | * Look at object 'op' and see if they know the spell |
262 | * spname. This is pretty close to check_spell_known |
262 | * spname. This is pretty close to check_spell_known |
263 | * above, but it uses a looser matching mechanism. |
263 | * above, but it uses a looser matching mechanism. |
264 | * returns the matching spell object, or NULL. |
264 | * returns the matching spell object, or NULL. |
265 | * If we match multiple spells but don't get an |
265 | * If we match multiple spells but don't get an |
266 | * exact match, we also return NULL. |
266 | * exact match, we also return NULL. |
267 | */ |
267 | */ |
268 | object * |
268 | object * |
269 | lookup_spell_by_name (object *op, const char *spname) |
269 | object::find_spell (const_utf8_string prefix) const |
270 | { |
270 | { |
271 | object *spob1 = 0, *spob2 = 0; |
271 | if (shstr_cmp name = prefix) |
272 | int nummatch = 0; |
272 | { |
273 | |
273 | // case 1, match spell name exactly |
274 | if (!spname) |
|
|
275 | return 0; |
|
|
276 | |
|
|
277 | /* Try to find the spell. We store the results in spob1 |
|
|
278 | * and spob2 - spob1 is only taking the length of |
|
|
279 | * the past spname, spob2 uses the length of the spell name. |
|
|
280 | */ |
|
|
281 | for (object *spob = op->inv; spob; spob = spob->below) |
274 | for (object *spob = inv; spob; spob = spob->below) |
|
|
275 | if (spob->name == name && spob->type == SPELL) |
|
|
276 | return spob; |
282 | { |
277 | } |
|
|
278 | else |
|
|
279 | { |
|
|
280 | // case 2, match prefix |
|
|
281 | int len = strlen (prefix); |
|
|
282 | |
|
|
283 | for (object *spob = inv; spob; spob = spob->below) |
283 | if (spob->type == SPELL) |
284 | if (spob->type == SPELL |
284 | { |
285 | && spob->name.length () < len |
285 | // TODO: WTF? |
286 | && prefix [spob->name.length ()] <= ' ' |
286 | if (!strncmp (spob->name, spname, strlen (spname))) |
287 | && !memcmp (prefix, spob->name, spob->name.length ())) |
287 | { |
|
|
288 | nummatch++; |
|
|
289 | spob1 = spob; |
|
|
290 | } |
|
|
291 | else if (!strncmp (spob->name, spname, strlen (spob->name))) |
|
|
292 | { |
|
|
293 | /* if spells have ambiguous names, it makes matching |
|
|
294 | * really difficult. (eg, fire and fireball would |
|
|
295 | * fall into this category). It shouldn't be hard to |
|
|
296 | * make sure spell names don't overlap in that fashion. |
|
|
297 | */ |
|
|
298 | if (spob2) |
|
|
299 | LOG (llevError, "Found multiple spells with overlapping base names: %s, %s\n", &spob2->name, &spob->name); |
|
|
300 | |
|
|
301 | spob2 = spob; |
|
|
302 | } |
|
|
303 | } |
|
|
304 | } |
|
|
305 | /* if we have best match, return it. Otherwise, if we have one match |
|
|
306 | * on the loser match, return that, otehrwise null |
|
|
307 | */ |
|
|
308 | if (spob2) |
|
|
309 | return spob2; |
288 | return spob; |
|
|
289 | } |
310 | |
290 | |
311 | if (spob1 && nummatch == 1) |
|
|
312 | return spob1; |
|
|
313 | |
|
|
314 | return NULL; |
291 | return 0; |
315 | } |
292 | } |
316 | |
293 | |
317 | /* reflwall - decides weither the (spell-)object sp_op will |
294 | /* reflwall - decides weither the (spell-)object sp_op will |
318 | * be reflected from the given mapsquare. Returns 1 if true. |
295 | * be reflected from the given mapsquare. Returns 1 if true. |
319 | * (Note that for living creatures there is a small chance that |
296 | * (Note that for living creatures there is a small chance that |