| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <global.h> |
| 29 |
#include <ob_methods.h> |
| 30 |
#include <ob_types.h> |
| 31 |
#include <sounds.h> |
| 32 |
#include <sproto.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include "living.h" |
| 37 |
|
| 38 |
static method_ret spellbook_type_apply(ob_methods *context, object *book, object *applier, int aflags); |
| 39 |
static void spellbook_type_describe( |
| 40 |
const ob_methods *context, const object *book, const object *observer, |
| 41 |
const int use_media_tags, char *buf, const size_t size); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
void init_type_spellbook(void) { |
| 47 |
register_apply(SPELLBOOK, spellbook_type_apply); |
| 48 |
register_describe(SPELLBOOK, spellbook_type_describe); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
static const char *const numbers_10[] = { |
| 53 |
"zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", |
| 54 |
"eighty", "ninety" |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
static const char *const ordinals[] = { |
| 59 |
"zeroth", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", |
| 60 |
"eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", |
| 61 |
"fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", |
| 62 |
"nineteenth", "twentieth" |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
static const char *const ordinals_10[] = { |
| 67 |
"zeroth", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", |
| 68 |
"seventieth", "eightieth", "ninetieth" |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void stringbuffer_append_ordinal(StringBuffer *sb, const int n) { |
| 83 |
if (n > 99 || n < 0) { |
| 84 |
stringbuffer_append_printf(sb, "%d.", n); |
| 85 |
} else if (n <= 20) { |
| 86 |
stringbuffer_append_string(sb, ordinals[n]); |
| 87 |
} else if (n % 10 == 0) { |
| 88 |
stringbuffer_append_string(sb, ordinals_10[n / 10]); |
| 89 |
} else { |
| 90 |
stringbuffer_append_printf(sb, "%s%s", ordinals_10[n/10], ordinals[n%10]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static void stringbuffer_append_spelldesc(StringBuffer *sb, const object *spell) { |
| 109 |
stringbuffer_append_string(sb, "(a "); |
| 110 |
stringbuffer_append_ordinal(sb, spell->level); |
| 111 |
stringbuffer_append_string(sb, " level "); |
| 112 |
|
| 113 |
if (!spell->skill) { |
| 114 |
|
| 115 |
stringbuffer_append_string(sb, "mystery"); |
| 116 |
} else if (spell->stats.grace) { |
| 117 |
|
| 118 |
|
| 119 |
stringbuffer_append_string(sb, "prayer"); |
| 120 |
} else { |
| 121 |
stringbuffer_append_string(sb, spell->skill); |
| 122 |
} |
| 123 |
|
| 124 |
if (spell->path_attuned) { |
| 125 |
stringbuffer_append_string(sb, ") "); |
| 126 |
describe_spellpath_attenuation("paths", spell->path_attuned, sb); |
| 127 |
} else { |
| 128 |
stringbuffer_append_string(sb, ")"); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static void spellbook_type_describe( |
| 151 |
const ob_methods *context, const object *book, const object *observer, |
| 152 |
const int use_media_tags, char *buf, size_t size) { |
| 153 |
if (!is_identified(book)) { |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
query_name(book, buf, size-1); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
size_t len; |
| 165 |
|
| 166 |
|
| 167 |
common_ob_describe(context, book, observer, use_media_tags, buf, size); |
| 168 |
len = strlen(buf); |
| 169 |
|
| 170 |
const object *spell = book->inv; |
| 171 |
if (!spell) { |
| 172 |
snprintf(buf+len, size-len, " (blank)"); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
StringBuffer *sb = stringbuffer_new(); |
| 177 |
stringbuffer_append_string(sb, " "); |
| 178 |
stringbuffer_append_spelldesc(sb, spell); |
| 179 |
char *const desc = stringbuffer_finish(sb); |
| 180 |
safe_strcat(buf, desc, &len, size); |
| 181 |
free(desc); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static method_ret spellbook_type_apply(ob_methods *context, object *book, object *applier, int aflags) { |
| 206 |
object *skapplier, *spell, *spell_skill; |
| 207 |
|
| 208 |
|
| 209 |
if (applier->type == PLAYER) { |
| 210 |
if (QUERY_FLAG(applier, FLAG_BLIND) && !QUERY_FLAG(applier, FLAG_WIZ)) { |
| 211 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 212 |
"You are unable to read while blind."); |
| 213 |
return METHOD_OK; |
| 214 |
} |
| 215 |
|
| 216 |
spell = book->inv; |
| 217 |
if (!spell) { |
| 218 |
LOG(llevError, "apply_spellbook: Book %s has no spell in it!\n", book->name); |
| 219 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 220 |
"The spellbook symbols make no sense."); |
| 221 |
return METHOD_OK; |
| 222 |
} |
| 223 |
|
| 224 |
if (QUERY_FLAG(book, FLAG_CURSED) || QUERY_FLAG(book, FLAG_DAMNED)) { |
| 225 |
char name[MAX_BUF]; |
| 226 |
|
| 227 |
int failure = -35; |
| 228 |
|
| 229 |
if (settings.spell_failure_effects == TRUE) |
| 230 |
failure = -rndm(35, 100); |
| 231 |
query_name(book, name, MAX_BUF); |
| 232 |
draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 233 |
"The %s was %s!", |
| 234 |
name, QUERY_FLAG(book, FLAG_DAMNED) ? "damned" : "cursed"); |
| 235 |
scroll_failure(applier, failure, (spell->level+4)*7); |
| 236 |
if (QUERY_FLAG(book, FLAG_DAMNED) |
| 237 |
&& check_spell_known(applier, spell->name) |
| 238 |
&& die_roll(1, 10, applier, 1) < 2) |
| 239 |
|
| 240 |
do_forget_spell(applier, spell->name); |
| 241 |
book = object_decrease_nrof_by_one(book); |
| 242 |
if (book && (!QUERY_FLAG(book, FLAG_IDENTIFIED))) { |
| 243 |
|
| 244 |
|
| 245 |
book = identify(book); |
| 246 |
if (book->env) |
| 247 |
esrv_update_item(UPD_FLAGS|UPD_NAME, applier, book); |
| 248 |
else |
| 249 |
applier->contr->socket.update_look = 1; |
| 250 |
} |
| 251 |
return METHOD_OK; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
if (check_spell_known(applier, spell->name)) { |
| 256 |
|
| 257 |
if (book && (!QUERY_FLAG(book, FLAG_IDENTIFIED))) { |
| 258 |
book = identify(book); |
| 259 |
if (book->env) |
| 260 |
esrv_update_item(UPD_FLAGS|UPD_NAME, applier, book); |
| 261 |
else |
| 262 |
applier->contr->socket.update_look = 1; |
| 263 |
} |
| 264 |
draw_ext_info_format(NDI_UNIQUE, 0, applier, |
| 265 |
MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 266 |
"You already know the spell %s.\n", spell->name); |
| 267 |
return METHOD_OK; |
| 268 |
} |
| 269 |
|
| 270 |
if (spell->skill) { |
| 271 |
spell_skill = find_skill_by_name(applier, spell->skill); |
| 272 |
if (!spell_skill) { |
| 273 |
draw_ext_info_format(NDI_UNIQUE, 0, applier, |
| 274 |
MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 275 |
"You lack the skill %s to use this spell", |
| 276 |
spell->skill); |
| 277 |
return METHOD_OK; |
| 278 |
} |
| 279 |
|
| 280 |
int skill_lev_diff = spell->level - spell_skill->level; |
| 281 |
if (skill_lev_diff > 0) { |
| 282 |
if (skill_lev_diff < 2) |
| 283 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 284 |
"The spell described in this book is just beyond your skill in %s.", spell->skill); |
| 285 |
else if (skill_lev_diff < 3) |
| 286 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 287 |
"The spell described in this book is slightly beyond your skill in %s.", spell->skill); |
| 288 |
else if (skill_lev_diff < 5) |
| 289 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 290 |
"The spell described in this book is beyond your skill in %s.", spell->skill); |
| 291 |
else if (skill_lev_diff < 8) |
| 292 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 293 |
"The spell described in this book is quite a bit beyond your skill in %s.", spell->skill); |
| 294 |
else if (skill_lev_diff < 15) |
| 295 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 296 |
"The spell described in this book is way beyond your skill in %s.", spell->skill); |
| 297 |
else |
| 298 |
draw_ext_info_format(NDI_UNIQUE, 0, applier,MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 299 |
"The spell described in this book is totally beyond your skill in %s.", spell->skill); |
| 300 |
return METHOD_OK; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
skapplier = find_skill_by_name(applier, book->skill); |
| 307 |
if (!skapplier) { |
| 308 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR, |
| 309 |
"You can't read! You will need this skill before you can comprehend the ideas written within."); |
| 310 |
return METHOD_OK; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
int read_level; |
| 315 |
read_level = skapplier->level; |
| 316 |
|
| 317 |
|
| 318 |
if (QUERY_FLAG(book, FLAG_BLESSED)) |
| 319 |
read_level += 5; |
| 320 |
|
| 321 |
|
| 322 |
int lev_diff; |
| 323 |
lev_diff = spell->level - (read_level+10); |
| 324 |
if (!QUERY_FLAG(applier, FLAG_WIZ) && lev_diff > 0) { |
| 325 |
if (lev_diff < 2) |
| 326 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 327 |
"You recognise most of the words but this book is just beyond your comprehension."); |
| 328 |
else if (lev_diff < 3) |
| 329 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 330 |
"You recognise many of the words but this book is slightly beyond your comprehension."); |
| 331 |
else if (lev_diff < 5) |
| 332 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 333 |
"You recognise some of the words but this book is slightly beyond your comprehension."); |
| 334 |
else if (lev_diff < 8) |
| 335 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 336 |
"You recognise some of the words but this book is beyond your comprehension."); |
| 337 |
else if (lev_diff < 15) |
| 338 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 339 |
"You recognise a few of the words but this book is beyond your comprehension."); |
| 340 |
else |
| 341 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 342 |
"You recognise a few of the words but this book is totally beyond your comprehension."); |
| 343 |
return METHOD_OK; |
| 344 |
} |
| 345 |
|
| 346 |
if (!QUERY_FLAG(book, FLAG_IDENTIFIED)) { |
| 347 |
book = identify(book); |
| 348 |
if (book->env) |
| 349 |
esrv_update_item(UPD_FLAGS|UPD_NAME, applier, book); |
| 350 |
else |
| 351 |
applier->contr->socket.update_look = 1; |
| 352 |
spell = book->inv; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
StringBuffer *sb = stringbuffer_new(); |
| 358 |
stringbuffer_append_spelldesc(sb, spell); |
| 359 |
char *const desc = stringbuffer_finish(sb); |
| 360 |
draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, |
| 361 |
"The spellbook contains %s %s.", spell->name, desc); |
| 362 |
free(desc); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
char desc[MAX_BUF]; |
| 378 |
const readable_message_type *msgType = get_readable_message_type(book); |
| 379 |
|
| 380 |
if (QUERY_FLAG(applier, FLAG_CONFUSED)) { |
| 381 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 382 |
"In your confused state you flub the wording of the text!"); |
| 383 |
scroll_failure(applier, 0-random_roll(0, spell->level, applier, PREFER_LOW), MAX(spell->stats.sp, spell->stats.grace)); |
| 384 |
} else if (QUERY_FLAG(book, FLAG_STARTEQUIP) |
| 385 |
|| (random_roll(0, 100, applier, PREFER_LOW)-(5*read_level)) < get_learn_spell(spell->stats.grace ? applier->stats.Wis : applier->stats.Int)) { |
| 386 |
query_short_name(book, desc, sizeof(desc)); |
| 387 |
draw_ext_info_format(NDI_UNIQUE|NDI_NAVY, 0, applier, |
| 388 |
msgType->message_type, msgType->message_subtype, |
| 389 |
"You open the %s and start reading.", desc); |
| 390 |
if (spell->msg != NULL) { |
| 391 |
StringBuffer *sb = stringbuffer_new(); |
| 392 |
stringbuffer_append_string(sb, spell->msg); |
| 393 |
stringbuffer_trim_whitespace(sb); |
| 394 |
char *const fluff = stringbuffer_finish(sb); |
| 395 |
draw_ext_info(NDI_UNIQUE|NDI_BLUE, 0, applier, MSG_TYPE_SPELL, MSG_TYPE_SPELL_INFO, fluff); |
| 396 |
free(fluff); |
| 397 |
} |
| 398 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, |
| 399 |
"You succeed in learning the spell!"); |
| 400 |
do_learn_spell(applier, spell, 0); |
| 401 |
|
| 402 |
|
| 403 |
if (!QUERY_FLAG(book, FLAG_STARTEQUIP)) |
| 404 |
change_exp(applier, calc_skill_exp(applier, book, skapplier), skapplier->skill, 0); |
| 405 |
} else { |
| 406 |
play_sound_player_only(applier->contr, SOUND_TYPE_SPELL, book, 0, "fumble"); |
| 407 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 408 |
"You fail to learn the spell.\n"); |
| 409 |
} |
| 410 |
object_decrease_nrof_by_one(book); |
| 411 |
} |
| 412 |
return METHOD_OK; |
| 413 |
} |