Debian Codegraph @

common/holy.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file holy.c
16 * God-related common functions.
17 */
18
19 #include "global.h"
20
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "libproto.h"
26 #include "living.h"
27 #include "spells.h"
28
29 static void add_god_to_list(archetype *god_arch);
30
31 /**
32 * Initializes a god structure.
33 *
34 * @note
35 * Will never return NULL.
36 */
37 static godlink *init_godslist(void) {
38 godlink *gl = (godlink *)malloc(sizeof(godlink));
39 if (gl == NULL)
40 fatal(OUT_OF_MEMORY);
41 gl->name = NULL; /* how to describe the god to the player */
42 gl->arch = NULL; /* pointer to the archetype of this god */
43 gl->id = 0; /* id of the god */
44 gl->next = NULL; /* next god in this linked list */
45
46 return gl;
47 }
48
49 /**
50 * This takes a look at all of the archetypes to find
51 * the objects which correspond to the GODS (type GOD)
52 */
53 void init_gods(void) {
54 archetype *at = NULL;
55
56 LOG(llevDebug, "Initializing gods...\n");
57 for (at = first_archetype; at != NULL; at = at->next)
58 if (at->clone.type == GOD)
59 add_god_to_list(at);
60
61 LOG(llevDebug, "done.\n");
62 }
63
64 /**
65 * Adds specified god to linked list, gives it an id.
66 *
67 * @param god_arch
68 * God to add. If NULL, will log an error.
69 */
70 static void add_god_to_list(archetype *god_arch) {
71 godlink *god;
72
73 if (!god_arch) {
74 LOG(llevError, "ERROR: Tried to add null god to list!\n");
75 return;
76 }
77
78 god = init_godslist();
79
80 god->arch = god_arch;
81 god->name = add_string(god_arch->clone.name);
82 if (!first_god)
83 god->id = 1;
84 else {
85 god->id = first_god->id+1;
86 god->next = first_god;
87 }
88 first_god = god;
89
90 #ifdef DEBUG_GODS
91 LOG(llevDebug, "Adding god %s (%d) to list\n", god->name, god->id);
92 #endif
93 }
94
95 /**
96 * Returns a random god.
97 *
98 * @return
99 * a random god, or NULL if no god was found.
100 */
101 godlink *get_rand_god(void) {
102 godlink *god = first_god;
103 int i;
104
105 if (god)
106 for (i = RANDOM()%(god->id)+1; god; god = god->next)
107 if (god->id == i)
108 break;
109
110 if (!god)
111 LOG(llevError, "get_rand_god(): can't find a random god!\n");
112 return god;
113 }
114
115 /**
116 * Returns a pointer to the object
117 * We need to be VERY careful about using this, as we
118 * are returning a pointer to the CLONE object. -b.t.
119 *
120 * @param godlnk
121 * god to get object.
122 */
123 const object *pntr_to_god_obj(godlink *godlnk) {
124 if (godlnk && godlnk->arch)
125 return &godlnk->arch->clone;
126 return NULL;
127 }
128
129 /**
130 * Describe a god. The reason we return a combination is to know what exactly was written for knowledge management.
131 * @param god which god to describe.
132 * @param what information to describe, combination of @ref GOD_xxx "GOD_xxx" flags.
133 * @param buf where to describe, must not be NULL.
134 * @param maxlen maximum wanted length of the description, if 0 no maximum length.
135 * @return information actually written, combination of @ref GOD_xxx "GOD_xxx", based on maxlen.
136 */
137 int describe_god(const object *god, int what, StringBuffer *buf, size_t maxlen) {
138 int hassomething = 0, real = 0;
139 char temp[HUGE_BUF];
140 StringBuffer *add;
141 const char *name;
142
143 name = god->name;
144
145 /* preamble.. */
146 assert(buf);
147 stringbuffer_append_printf(buf, "This document contains knowledge concerning the diety %s", name);
148
149 /* Always have as default information the god's descriptive terms. */
150 if (nstrtok(god->msg, ",") > 0) {
151 stringbuffer_append_string(buf, ", known as");
152 stringbuffer_append_string(buf, strtoktolin(god->msg, ",", temp, sizeof(temp)));
153 } else
154 stringbuffer_append_string(buf, "...");
155
156 stringbuffer_append_string(buf, "\n ---\n");
157
158 if (what & GOD_PATHS) {
159 /* spell paths */
160 int has_effect = 0;
161
162 add = stringbuffer_new();
163 stringbuffer_append_printf(add, "It is rarely known fact that the priests of %s are mystically transformed. Effects of this include:\n", name);
164
165 if (god->path_attuned) {
166 has_effect = 1;
167 describe_spellpath_attenuation("Attuned", god->path_attuned, add);
168 }
169 if (god->path_repelled) {
170 has_effect = 1;
171 describe_spellpath_attenuation("Repelled", god->path_repelled, add);
172 }
173 if (god->path_denied) {
174 has_effect = 1;
175 describe_spellpath_attenuation("Denied", god->path_denied, add);
176 }
177 if (has_effect) {
178 stringbuffer_append_string(add, "\n ---\n");
179
180 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
181 stringbuffer_append_stringbuffer(buf, add);
182 free(stringbuffer_finish(add));
183 hassomething = 1;
184 } else {
185 free(stringbuffer_finish(add));
186 return real;
187 }
188 } else {
189 free(stringbuffer_finish(add));
190 }
191 what |= GOD_PATHS;
192 }
193
194 if (what & GOD_IMMUNITIES) {
195 int has_effect = 0, tmpvar;
196 char tmpbuf[MAX_BUF];
197
198 add = stringbuffer_new();
199 stringbuffer_append_printf(add, "\nThe priests of %s are known to make cast a mighty prayer of possession which gives the recipient ", name);
200
201 for (tmpvar = 0; tmpvar < NROFATTACKS; tmpvar++) {
202 if (god->resist[tmpvar] == 100) {
203 has_effect = 1;
204 snprintf(tmpbuf, MAX_BUF, "Immunity to %s", attacktype_desc[tmpvar]);
205 }
206 }
207 if (has_effect) {
208 stringbuffer_append_printf(add, "%s\n ---\n", tmpbuf);
209 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
210 stringbuffer_append_stringbuffer(buf, add);
211 free(stringbuffer_finish(add));
212 hassomething = 1;
213 } else {
214 free(stringbuffer_finish(add));
215 return real;
216 }
217 } else {
218 free(stringbuffer_finish(add));
219 }
220 real |= GOD_IMMUNITIES;
221 }
222
223 if (what & GOD_BLESSED) {
224 char *cp = stringbuffer_finish(describe_resistance(god, 1, 1, NULL));
225
226 if (*cp) { /* This god does have protections */
227 add = stringbuffer_new();
228 stringbuffer_append_printf(add, "\nThe priests of %s are known to be able to bestow a blessing which makes the recipient %s\n ---\n", name, cp);
229
230 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
231 stringbuffer_append_stringbuffer(buf, add);
232 free(stringbuffer_finish(add));
233 hassomething = 1;
234 } else {
235 free(stringbuffer_finish(add));
236 return real;
237 }
238 }
239 free(cp);
240 real |= GOD_BLESSED;
241 }
242
243 if (what & GOD_SACRED) {
244 if (god->race && !(god->path_denied&PATH_SUMMON)) {
245 int i = nstrtok(god->race, ",");
246 add = stringbuffer_new();
247 stringbuffer_append_printf(add, "Creatures sacred to %s include the\n", name);
248
249 if (i > 1)
250 stringbuffer_append_printf(add, "following races:%s\n ---\n", strtoktolin(god->race, ",", temp, sizeof(temp)));
251 else
252 stringbuffer_append_printf(add, "race of %s\n ---\n", strtoktolin(god->race, ",", temp, sizeof(temp)));
253
254 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
255 stringbuffer_append_stringbuffer(buf, add);
256 free(stringbuffer_finish(add));
257 hassomething = 1;
258 } else {
259 free(stringbuffer_finish(add));
260 return real;
261 }
262 }
263 real |= GOD_SACRED;
264 }
265
266 if (what & GOD_RESISTANCES) {
267 char *cp;
268 cp = stringbuffer_finish(describe_resistance(god, 1, 1, NULL));
269
270 if (*cp) { /* This god does have protections */
271 add = stringbuffer_new();
272 stringbuffer_append_printf(add, "%s has a potent aura which is extended to faithful priests. The effects of this aura include:\n%s\n ---\n", name, cp);
273
274 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
275 stringbuffer_append_stringbuffer(buf, add);
276 free(stringbuffer_finish(add));
277 hassomething = 1;
278 } else {
279 free(stringbuffer_finish(add));
280 return real;
281 }
282 }
283 free(cp);
284 real |= GOD_RESISTANCES;
285 }
286
287 if (what & GOD_HOLYWORD) {
288 if ((god->slaying) && !(god->path_denied&PATH_TURNING)) {
289 add = stringbuffer_new();
290 stringbuffer_append_printf(add, "The holy words of %s have the power to slay creatures belonging to the ", name);
291
292 if (nstrtok(god->slaying, ",") > 1)
293 stringbuffer_append_printf(add, "following races:%s\n ---\n", strtoktolin(god->slaying, ",", temp, sizeof(temp)));
294 else
295 stringbuffer_append_printf(add, "race of%s\n ---\n", strtoktolin(god->slaying, ",", temp, sizeof(temp)));
296
297 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
298 stringbuffer_append_stringbuffer(buf, add);
299 free(stringbuffer_finish(add));
300 hassomething = 1;
301 } else {
302 free(stringbuffer_finish(add));
303 return real;
304 }
305 }
306 real |= GOD_HOLYWORD;
307 }
308
309 if (what & GOD_ENEMY) {
310 if (god->title) {
311 add = stringbuffer_new();
312 stringbuffer_append_printf(add, "The gods %s and %s are enemies.\n ---\n", name, god->title);
313
314 if ((maxlen == 0) || (stringbuffer_length(add) + stringbuffer_length(buf) < maxlen)) {
315 stringbuffer_append_stringbuffer(buf, add);
316 free(stringbuffer_finish(add));
317 hassomething = 1;
318 } else {
319 free(stringbuffer_finish(add));
320 return real;
321 }
322 }
323 real |= GOD_ENEMY;
324 }
325
326 if (hassomething == 0) {
327 /* we got no information beyond the preamble! */
328 stringbuffer_append_string(buf, " Unfortunately the rest of the information is hopelessly garbled!\n ---\n");
329 }
330
331 return real;
332 }
333
334 /**
335 * Frees all god information.
336 */
337 void free_all_god(void) {
338 godlink *god, *godnext;
339
340 LOG(llevDebug, "Freeing god information\n");
341 for (god = first_god; god; god = godnext) {
342 godnext = god->next;
343 if (god->name)
344 free_string(god->name);
345 free(god);
346 }
347 first_god = NULL;
348 }
349
350 #define DESCRIBE_ABILITY(retbuf, variable, name) \
351 if (variable) { \
352 int i, j = 0; \
353 strcat(retbuf, "(" name ": "); \
354 for (i = 0; i < NROFATTACKS; i++) \
355 if (variable&(1<<i)) { \
356 if (j) \
357 strcat(retbuf, ", "); \
358 else \
359 j = 1; \
360 strcat(retbuf, attacks[i]); \
361 } \
362 strcat(retbuf, ")"); \
363 }
364
365 #define DESCRIBE_PATH(retbuf, variable, name) \
366 if (variable) { \
367 int i, j = 0; \
368 strcat(retbuf, "(" name ": "); \
369 for (i = 0; i < NRSPELLPATHS; i++) \
370 if (variable&(1<<i)) { \
371 if (j) \
372 strcat(retbuf, ", "); \
373 else \
374 j = 1; \
375 strcat(retbuf, spellpathnames[i]); \
376 } \
377 strcat(retbuf, ")"); \
378 }
379
380 /**
381 * Prints all gods to stderr.
382 *
383 * @todo
384 * use LOG instead of fprintf().
385 */
386 void dump_gods(void) {
387 godlink *glist;
388
389 fprintf(stderr, "\n");
390 for (glist = first_god; glist; glist = glist->next) {
391 const object *god = pntr_to_god_obj(glist);
392 char tmpbuf[HUGE_BUF], *final;
393 int tmpvar, gifts = 0;
394
395 fprintf(stderr, "GOD: %s\n", god->name);
396 fprintf(stderr, " avatar stats:\n");
397 fprintf(stderr, " S:%d C:%d D:%d I:%d W:%d P:%d\n", god->stats.Str, god->stats.Con, god->stats.Dex, god->stats.Int, god->stats.Wis, god->stats.Pow);
398 fprintf(stderr, " lvl:%d speed:%4.2f\n", god->level, god->speed);
399 fprintf(stderr, " wc:%d ac:%d hp:%d dam:%d\n", god->stats.wc, god->stats.ac, god->stats.hp, god->stats.dam);
400 fprintf(stderr, " enemy: %s\n", god->title ? god->title : "NONE");
401 if (god->other_arch) {
402 object *serv = &god->other_arch->clone;
403 fprintf(stderr, " servant stats: (%s)\n", god->other_arch->name);
404 fprintf(stderr, " S:%d C:%d D:%d I:%d W:%d P:%d\n", serv->stats.Str, serv->stats.Con, serv->stats.Dex, serv->stats.Int, serv->stats.Wis, serv->stats.Pow);
405 fprintf(stderr, " lvl:%d speed:%4.2f\n", serv->level, serv->speed);
406 fprintf(stderr, " wc:%d ac:%d hp:%d dam:%d\n", serv->stats.wc, serv->stats.ac, serv->stats.hp, serv->stats.dam);
407 } else
408 fprintf(stderr, " servant: NONE\n");
409 fprintf(stderr, " aligned_race(s): %s\n", god->race);
410 fprintf(stderr, " enemy_race(s): %s\n", (god->slaying ? god->slaying : "none"));
411 final = stringbuffer_finish(describe_resistance(god, 1, 1, NULL));
412 fprintf(stderr, "%s", final);
413 free(final);
414 snprintf(tmpbuf, sizeof(tmpbuf), " attacktype:");
415 if ((tmpvar = god->attacktype)) {
416 strcat(tmpbuf, "\n ");
417 DESCRIBE_ABILITY(tmpbuf, tmpvar, "Attacks");
418 }
419 strcat(tmpbuf, "\n aura:");
420
421 strcat(tmpbuf, "\n paths:");
422 if ((tmpvar = god->path_attuned)) {
423 strcat(tmpbuf, "\n ");
424 DESCRIBE_PATH(tmpbuf, tmpvar, "Attuned");
425 }
426 if ((tmpvar = god->path_repelled)) {
427 strcat(tmpbuf, "\n ");
428 DESCRIBE_PATH(tmpbuf, tmpvar, "Repelled");
429 }
430 if ((tmpvar = god->path_denied)) {
431 strcat(tmpbuf, "\n ");
432 DESCRIBE_PATH(tmpbuf, tmpvar, "Denied");
433 }
434 fprintf(stderr, "%s\n", tmpbuf);
435 fprintf(stderr, " Desc: %s", god->msg ? god->msg : "---\n");
436 fprintf(stderr, " Priest gifts/limitations: ");
437 if (!QUERY_FLAG(god, FLAG_USE_WEAPON)) { gifts = 1; fprintf(stderr, "\n weapon use is forbidden"); }
438 if (!QUERY_FLAG(god, FLAG_USE_ARMOUR)) { gifts = 1; fprintf(stderr, "\n no armour may be worn"); }
439 if (QUERY_FLAG(god, FLAG_UNDEAD)) { gifts = 1; fprintf(stderr, "\n is undead"); }
440 if (QUERY_FLAG(god, FLAG_SEE_IN_DARK)) { gifts = 1; fprintf(stderr, "\n has infravision "); }
441 if (QUERY_FLAG(god, FLAG_XRAYS)) { gifts = 1; fprintf(stderr, "\n has X-ray vision"); }
442 if (QUERY_FLAG(god, FLAG_REFL_MISSILE)) { gifts = 1; fprintf(stderr, "\n reflect missiles"); }
443 if (QUERY_FLAG(god, FLAG_REFL_SPELL)) { gifts = 1; fprintf(stderr, "\n reflect spells"); }
444 if (QUERY_FLAG(god, FLAG_STEALTH)) { gifts = 1; fprintf(stderr, "\n is stealthy"); }
445 if (QUERY_FLAG(god, FLAG_MAKE_INVIS)) { gifts = 1; fprintf(stderr, "\n is (permanently) invisible"); }
446 if (QUERY_FLAG(god, FLAG_BLIND)) { gifts = 1; fprintf(stderr, "\n is blind"); }
447 if (god->last_heal) { gifts = 1; fprintf(stderr, "\n hp regenerate at %d", god->last_heal); }
448 if (god->last_sp) { gifts = 1; fprintf(stderr, "\n sp regenerate at %d", god->last_sp); }
449 if (god->last_eat) { gifts = 1; fprintf(stderr, "\n digestion is %s (%d)", god->last_eat < 0 ? "slowed" : "faster", god->last_eat); }
450 if (god->last_grace) { gifts = 1; fprintf(stderr, "\n grace regenerates at %d", god->last_grace); }
451 if (god->stats.luck) { gifts = 1; fprintf(stderr, "\n luck is %d", god->stats.luck); }
452 if (!gifts) fprintf(stderr, "NONE");
453 fprintf(stderr, "\n\n");
454 }
455 }