Debian Codegraph @

types/spellbook/spellbook.c

raw symbols SCIP
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2007 Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at [email protected]
22 */
23
24 /**
25 * @file spellbook.c
26 * Implimentation of spellbooks.
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 * Initializer for the SPELLBOOK object type.
45 */
46 void init_type_spellbook(void) {
47 register_apply(SPELLBOOK, spellbook_type_apply);
48 register_describe(SPELLBOOK, spellbook_type_describe);
49 }
50
51 /** Tens */
52 static const char *const numbers_10[] = {
53 "zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
54 "eighty", "ninety"
55 };
56
57 /** Levels as a full name and not a number. */
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 /** Tens for levels */
66 static const char *const ordinals_10[] = {
67 "zeroth", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth",
68 "seventieth", "eightieth", "ninetieth"
69 };
70
71 /**
72 * Turns a cardinal number (e.g. 7) into an ordinal number (e.g. "seventh") and
73 * appends it to a caller-supplied StringBuffer.
74 * Works only on numbers 0 <= n < 100; outside that range just returns the
75 * cardinal number as a string.
76 *
77 * @param sb
78 * StringBuffer to append the result to.
79 * @param n
80 * Number to ordinalize.
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 * Append a terse description of the spell's name, level, discipline, and paths
97 * to a stringbuffer. What comes out is something like:
98 * "medium fireball (a third level pyromancy) (paths: Fire)"
99 * this is used in both spellbook_type_describe() to generate the one-line
100 * description for a spellbook, and in spellbook_type_apply to summarize what
101 * the spellbook contains if you don't ID it until you start reading it.
102 *
103 * @param sb
104 * The StringBuffer to append to
105 * @param spell
106 * The spell to describe
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 /* Can this even happen? */
115 stringbuffer_append_string(sb, "mystery");
116 } else if (spell->stats.grace) {
117 /* Otherwise we get "a second level praying" when it should be "a second
118 * level prayer". */
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 * Describe a spellbook.
134 *
135 * If identified, displays the level and description of the spell inside it.
136 *
137 * @param context
138 * method context.
139 * @param book
140 * Spellbook to describe
141 * @param observer
142 * Player examining the spellbook
143 * @param use_media_tags
144 * True if we should use mediatags in the output
145 * @param buf
146 * Output buffer to append description to
147 * @param size
148 * Total output buffer size
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 /* Without querying the name, spellbooks end up examining
155 * as "That is:", with no name at all
156 * This should tell the player just as little as the inventory view.
157 *
158 * SilverNexus 2020-11-28
159 */
160 query_name(book, buf, size-1);
161 return;
162 }
163
164 size_t len;
165 /* TODO check if this generates the "of foo" so we don't end up with
166 "spellbook of medium fireball of medium fireball" I think it probably does */
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 * Applies a spellbook.
186 * Checks whether player has knowledge of required skill, doesn't
187 * already know the spell, stuff like that. Random learning failure too.
188 *
189 * @param context
190 * method context.
191 * @param book
192 * Spellbook to apply.
193 * @param applier
194 * object attempting to apply the spellbook. Should be a player.
195 * @param aflags
196 * special flags (always apply/unapply).
197 * @return
198 * METHOD_OK always.
199 *
200 * @todo
201 * handle failure differently for praying/magic.
202 * @todo
203 * split into multiple functions
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 /* Must be applied by a player. */
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 /* Player made a mistake, let's shake her/him :) */
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 /* Really unlucky player, better luck next time */
240 do_forget_spell(applier, spell->name);
241 book = object_decrease_nrof_by_one(book);
242 if (book && (!QUERY_FLAG(book, FLAG_IDENTIFIED))) {
243 /* Well, not everything is lost, player now knows the
244 * book is cursed/damned. */
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 /* This section moved before literacy check */
255 if (check_spell_known(applier, spell->name)) {
256 // If we already know the spell, it makes sense we know what the spell is.
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 /* check they have the right skills to learn the spell in the first place */
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 /* need a literacy skill to learn spells. Also, having a literacy level
305 * lower than the spell will make learning the spell more difficult */
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 /* We know the player has all the right skills so check how well they can read. */
314 int read_level;
315 read_level = skapplier->level;
316
317 /* blessed books are easier to read */
318 if (QUERY_FLAG(book, FLAG_BLESSED))
319 read_level += 5;
320
321 /* If the players read level is less than 10 levels lower than the spellbook, they can't read it */
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 /* If they hadn't previously IDed the book, they didn't know what
355 * spell it contained, so tell them here.
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 /* Player has the right skills and enough skill to attempt to learn the spell with the logic as follows:
366 *
367 * 1- MU spells use Int to learn, Cleric spells use Wisdom
368 *
369 * 2- The learner's skill level in literacy adjusts the chance
370 * to learn a spell.
371 *
372 * 3 -Automatically fail to learn if you read while confused
373 *
374 * Overall, chances are the same but a player will find having a high
375 * literacy rate very useful! -b.t.
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 /* xp gain to literacy for spell learning */
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 }