Crossfire Server, Trunk  R20513
resurrection.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
21 /* the contents of this file were create solely by peterm@soda.berkeley.edu
22  all of the above disclaimers apply. */
23 
24 #include "global.h"
25 
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "output_file.h"
31 #include "sproto.h"
32 #include "spells.h"
33 
34 static int resurrection_fails(int levelcaster, int leveldead);
35 
52 static int resurrect_player(object *op, char *playername, object *spell, char *accountname) {
53  FILE *deadplayer, *liveplayer;
54  OutputFile of;
55  char oldname[MAX_BUF];
56  char newname[MAX_BUF];
57  char path[MAX_BUF];
58  char buf[MAX_BUF];
59  char buf2[MAX_BUF];
60  const char *race = NULL;
61  int64_t exp;
62  int Con;
63 
64  /* reincarnation, which changes the race */
65  if (spell->race) {
66  treasurelist *tl = find_treasurelist(spell->race);
67  treasure *t;
68  int value;
69 
70  if (!tl) {
71  LOG(llevError, "resurrect_player: race set to %s, but no treasurelist of that name!\n", spell->race);
72  return 0;
73  }
74  value = RANDOM()%tl->total_chance;
75  for (t = tl->items; t; t = t->next) {
76  value -= t->chance;
77  if (value < 0)
78  break;
79  }
80  if (!t) {
81  LOG(llevError, "resurrect_player: got null treasure from treasurelist %s!\n", spell->race);
82  return 0;
83  }
84  race = t->item->name;
85  }
86 
87  /* set up our paths/strings... */
88  snprintf(path, sizeof(path), "%s/%s/%s/%s", settings.localdir, settings.playerdir, playername, playername);
89 
90  strcpy(newname, path);
91  strcat(newname, ".pl");
92 
93  strcpy(oldname, newname);
94  strcat(oldname, ".dead");
95 
96  if (!(deadplayer = fopen(oldname, "r"))) {
98  "The soul of %s cannot be reached.",
99  playername);
100  return 0;
101  }
102 
103  if (!access(newname, 0)) {
105  "The soul of %s has already been reborn!",
106  playername);
107  fclose(deadplayer);
108  return 0;
109  }
110 
111  liveplayer = of_open(&of, newname);
112  if (liveplayer == NULL) {
114  "The soul of %s cannot be re-embodied at the moment.",
115  playername);
116  fclose(deadplayer);
117  return 0;
118  }
119 
120  while (!feof(deadplayer)) {
121  fgets(buf, 255, deadplayer);
122  sscanf(buf, "%s", buf2);
123  if (!(strcmp(buf2, "exp"))) {
124  sscanf(buf, "%s %"FMT64, buf2, &exp);
125  if (spell->stats.exp) {
126  exp -= exp/spell->stats.exp;
127  snprintf(buf, sizeof(buf), "exp %"FMT64"\n", exp);
128  }
129  }
130  if (!(strcmp(buf2, "Con"))) {
131  sscanf(buf, "%s %d", buf2, &Con);
132  Con -= spell->stats.Con;
133  if (Con < 1)
134  Con = 1;
135  snprintf(buf, sizeof(buf), "Con %d\n", Con);
136  }
137  if (race && !strcmp(buf2, "race")) {
138  snprintf(buf, sizeof(buf), "race %s\n", race);
139  }
140  fputs(buf, liveplayer);
141  }
142  if (!of_close(&of)) {
144  "The soul of %s cannot be re-embodied at the moment.",
145  playername);
146  fclose(deadplayer);
147  return 0;
148  }
149  fclose(deadplayer);
150  unlink(oldname);
152  "%s lives again!",
153  playername);
154  /* Needs to go into the account file and change the player back to isDead = 0 */
155  /* I'm fairly certain the old login did not have the segfault, so just account for the new account system */
156  if (accountname)
157  unmake_perma_dead(accountname, playername);
158  return 1;
159 }
160 
181 int cast_raise_dead_spell(object *op, object *caster, object *spell, int dir, const char *arg) {
182  object *temp, *newob;
183  char name_to_resurrect[MAX_BUF];
184  char *corpse_account = NULL;
185  int leveldead = 25, mflags, clevel;
186  int16_t sx, sy;
187  mapstruct *m;
188  int spell_success = 0;
189 
190  clevel = caster_level(caster, spell);
191 
192  if (spell->last_heal) {
193  if (!arg) {
195  "Cast %s on who?",
196  spell->name);
197  return 0;
198  }
199  safe_strncpy(name_to_resurrect, arg, sizeof(name_to_resurrect));
200  temp = NULL;
201  } else {
202  sx = op->x+freearr_x[dir];
203  sy = op->y+freearr_y[dir];
204  m = op->map;
205  mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
206  if (mflags&P_OUT_OF_MAP)
207  temp = NULL;
208  else {
209  /* First we need to find a corpse, if any. */
210  /* If no object, temp will be set to NULL */
211  temp = NULL;
212  FOR_MAP_PREPARE(m, sx, sy, tmp)
213  /* If it is corpse, this must be what we want to raise */
214  if (tmp->type == CORPSE) {
215  temp = tmp;
216  break;
217  }
218  FOR_MAP_FINISH();
219  }
220 
221  if (temp == NULL) {
223  "You need a body for this spell.");
224  return 0;
225  }
226  safe_strncpy(name_to_resurrect, temp->name, sizeof(name_to_resurrect));
227  /* Only try to copy if dead body is as recent as the patch this is from. */
228  if (temp->slaying){
229  corpse_account = (char *)CALLOC(sizeof(char), MAX_BUF);
230  /* Make sure that the allocation worked. */
231  if (corpse_account) strcpy(corpse_account, temp->slaying);
232  }
233  }
234  /* no matter what, we fry the corpse. */
235  if (temp && temp->map) {
236  /* replace corpse object with a burning object */
237  newob = arch_to_object(find_archetype("burnout"));
238  if (newob != NULL)
239  object_insert_in_map_at(newob, temp->map, op, 0, temp->x, temp->y);
240  leveldead = temp->level;
241  object_remove(temp);
243  }
244 
245  if (resurrection_fails(clevel, leveldead)) {
246  if (spell->randomitems) {
247  treasure *t;
248 
249  for (t = spell->randomitems->items; t; t = t->next) {
250  summon_hostile_monsters(op, t->nrof, t->item->name);
251  }
252  }
253  spell_success = 1;
254  } else {
255  spell_success = resurrect_player(op, name_to_resurrect, spell, corpse_account);
256  }
257  /* Reorganized so corpse_account could be deallocated if needed */
258  free(corpse_account);
259  return spell_success;
260 }
261 
277 static int resurrection_fails(int levelcaster, int leveldead) {
278  int chance = 9;
279 
280  chance += levelcaster-leveldead;
281  if (chance < 4)
282  chance = 4;
283  if (chance > rndm(0, 19))
284  return 0; /* resurrection succeeds */
285  return 1;
286 }
287 
294 void dead_player(object *op) {
295  char filename[MAX_BUF];
296  char newname[MAX_BUF];
297  char path[MAX_BUF];
298 
299  /* set up our paths/strings... */
300  snprintf(path, sizeof(path), "%s/%s/%s/%s", settings.localdir, settings.playerdir, op->name, op->name);
301 
302  strcpy(filename, path);
303  strcat(filename, ".pl");
304  strcpy(newname, filename);
305  strcat(newname, ".dead");
306 
307  if (rename(filename, newname) != 0) {
308  LOG(llevError, "Cannot rename dead player's file %s into %s: %s\n", filename, newname, strerror(errno));
309  }
310  /* Go into the account file and change isDead for this character to 1. */
311  if (make_perma_dead(op)){
312  /* Make an error message saying that the character could not be changed in the proper account */
313  LOG(llevError, "Could not edit the account to indicate permanent death for %s!\n", op->name);
314  }
315 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Sends message to player(s).
Definition: main.c:315
Error, serious thing.
Definition: logger.h:11
int make_perma_dead(object *op)
This will edit the character account information so that the character that just died in permadeath w...
Definition: account_char.c:357
archetype * find_archetype(const char *name)
Finds, using the hashtable, which archetype matches the given name.
Definition: arch.c:695
Spell-related defines: spellpath, subtypes, ...
treasureliststruct represents one logical group of items to be generated together.
Definition: treasure.h:82
const char * race
Human, goblin, dragon, etc.
Definition: object.h:318
int caster_level(const object *caster, const object *spell)
This function returns the effective level the spell is being cast at.
Definition: spell_util.c:233
static int resurrect_player(object *op, char *playername, object *spell, char *accountname)
Resurrect a player.
Definition: resurrection.c:52
#define MSG_TYPE_SPELL
Spell related info.
Definition: newclient.h:387
treasurelist * find_treasurelist(const char *name)
Searches for the given treasurelist in the globally linked list of treasurelists which has been built...
Definition: treasure.c:292
const char * playerdir
Where the player files are.
Definition: global.h:246
int32_t last_heal
Last healed.
Definition: object.h:357
#define MSG_TYPE_SPELL_FAILURE
Spell failure messages.
Definition: newclient.h:628
struct treasureliststruct * randomitems
Items to be generated.
Definition: object.h:385
short freearr_x[SIZEOFFREE]
X offset when searching around a spot.
Definition: object.c:65
const char * slaying
Which race to do double damage to.
Definition: object.h:319
int64_t exp
Experience.
Definition: living.h:46
treasure is one element in a linked list, which together consist of a complete treasure-list.
Definition: treasure.h:63
Global type definitions and header inclusions.
#define safe_strncpy
Definition: compat.h:23
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Sends message to player(s).
Definition: main.c:310
struct archt * item
Which item this link can be.
Definition: treasure.h:64
int8_t Con
Definition: living.h:35
#define CALLOC(x, y)
Definition: compat.h:27
short freearr_y[SIZEOFFREE]
Y offset when searching around a spot.
Definition: object.c:71
int rndm(int min, int max)
Returns a number between min and max.
Definition: utils.c:161
int16_t total_chance
If non-zero, only 1 item on this list should be generated.
Definition: treasure.h:84
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1368
int16_t y
Position in the map for this object.
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
#define MSG_TYPE_SPELL_SUCCESS
Spell succeeded messages.
Definition: newclient.h:630
int unmake_perma_dead(char *account, char *player)
This will edit the character account information so that the character that was just resurrected in p...
Definition: account_char.c:397
signed short int16_t
Definition: win32.h:160
struct mapdef * map
Pointer to the map in which this object is present.
Definition: object.h:297
#define snprintf
Definition: win32.h:46
int summon_hostile_monsters(object *op, int n, const char *monstername)
Summons hostile monsters and places them in nearby squares.
Definition: spell_util.c:1047
static int resurrection_fails(int levelcaster, int leveldead)
Will the resurrection succeed?
Definition: resurrection.c:277
#define FMT64
Definition: compat.h:12
int of_close(OutputFile *of)
Closes an output file.
Definition: output_file.c:61
const char * name
The name of the object, obviously...
Definition: object.h:311
#define P_OUT_OF_MAP
This space is outside the map.
Definition: map.h:251
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
uint16_t nrof
random 1 to nrof items are generated
Definition: treasure.h:76
int16_t x
Definition: object.h:326
struct treasurestruct * items
Items in this list, linked.
Definition: treasure.h:90
void dead_player(object *op)
Kill a player on a permanent death server with resurrection.
Definition: resurrection.c:294
signed __int64 int64_t
Definition: win32.h:168
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:765
uint8_t chance
Percent chance for this item.
Definition: treasure.h:70
#define RANDOM()
Definition: define.h:679
Definition: object.h:243
const char * localdir
Read/write data files.
Definition: global.h:245
living stats
Str, Con, Dex, etc.
Definition: object.h:368
#define unlink(__a)
Definition: win32.h:56
struct Settings settings
Server settings.
Definition: init.c:40
FILE * of_open(OutputFile *of, const char *fname)
Opens an output file.
Definition: output_file.c:30
Functions for creating text output files.
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
This rolls up wall, blocks_magic, blocks_view, etc, all into one function that just returns a P_...
Definition: map.c:302
#define NDI_UNIQUE
Print immediately, don&#39;t buffer.
Definition: newclient.h:245
int cast_raise_dead_spell(object *op, object *caster, object *spell, int dir, const char *arg)
This handles the raise dead / resurrection spells.
Definition: resurrection.c:181
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:758
This is a game-map.
Definition: map.h:325
struct treasurestruct * next
Next treasure-item in a linked list.
Definition: treasure.h:66
int16_t level
Level of creature or object.
Definition: object.h:351
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.c:571
const char * name
More definite name, like "generate_kobold".
Definition: object.h:466
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.c:1654