ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/resurrection.C
Revision: 1.19
Committed: Mon Jan 15 21:06:20 2007 UTC (17 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.18: +22 -22 lines
Log Message:
comments

File Contents

# Content
1 /*
2 * CrossFire, A Multiplayer game for X-windows
3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
23 */
24
25 /* the contents of this file were create solely by peterm@soda.berkeley.edu
26 all of the above disclaimers apply. */
27
28 #include <global.h>
29 #include <sproto.h>
30 #include <spells.h>
31 #include <errno.h>
32
33 /* name of the person to resurrect and which spell was used
34 * to resurrect
35 */
36 static int
37 resurrect_player (object *op, char *playername, object *spell)
38 {
39 FILE *deadplayer, *liveplayer;
40
41 char oldname[MAX_BUF];
42 char newname[MAX_BUF];
43 char path[MAX_BUF];
44 char buf[MAX_BUF];
45 char buf2[MAX_BUF];
46 const char *race = NULL;
47 sint64 exp;
48 int Con;
49
50 /* reincarnation, which changes the race */
51 if (spell->race)
52 {
53 treasurelist *tl = find_treasurelist (spell->race);
54 treasure *t;
55 int value;
56
57 if (!tl)
58 {
59 LOG (llevError, "resurrect_player: race set to %s, but no treasurelist of that name!\n", &spell->race);
60 return 0;
61 }
62 value = RANDOM () % tl->total_chance;
63 for (t = tl->items; t; t = t->next)
64 {
65 value -= t->chance;
66 if (value < 0)
67 break;
68 }
69 if (!t || !t->item)
70 {
71 LOG (llevError, "resurrect_player: got null treasure from treasurelist %s!\n", &spell->race);
72 return 0;
73 }
74 race = t->item->name;
75 }
76
77 /* set up our paths/strings... */
78 sprintf (path, "%s/%s/%s/%s", settings.localdir, settings.playerdir, playername, playername);
79
80 strcpy (newname, path);
81 strcat (newname, ".pl");
82
83 strcpy (oldname, newname);
84 strcat (oldname, ".dead");
85
86 if (!(deadplayer = fopen (oldname, "r")))
87 {
88 new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s cannot be reached.", playername);
89 return 0;
90 }
91
92 if (!access (newname, 0))
93 {
94 new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s has already been reborn!", playername);
95 fclose (deadplayer);
96 return 0;
97 }
98
99 if (!(liveplayer = fopen (newname, "w")))
100 {
101 new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s cannot be re-embodied at the moment.", playername);
102 LOG (llevError, "Cannot write player in resurrect_player!\n");
103 fclose (deadplayer);
104 return 0;
105 }
106
107 while (!feof (deadplayer))
108 {
109 fgets (buf, 255, deadplayer);
110 sscanf (buf, "%s", buf2);
111 if (!(strcmp (buf2, "exp")))
112 {
113 sscanf (buf, "%s %" SCNd64, buf2, &exp);
114 if (spell->stats.exp)
115 {
116 exp -= exp / spell->stats.exp;
117 sprintf (buf, "exp %" PRId64 "\n", exp);
118 }
119 }
120 if (!(strcmp (buf2, "Con")))
121 {
122 sscanf (buf, "%s %d", buf2, &Con);
123 Con -= spell->stats.Con;
124 if (Con < 1)
125 Con = 1;
126 sprintf (buf, "Con %d\n", Con);
127 }
128 if (race && !strcmp (buf2, "race"))
129 {
130 sprintf (buf, "race %s\n", race);
131 }
132 fputs (buf, liveplayer);
133 }
134 fclose (liveplayer);
135 fclose (deadplayer);
136 unlink (oldname);
137 new_draw_info_format (NDI_UNIQUE, 0, op, "%s lives again!", playername);
138
139 return 1;
140 }
141
142
143 /* raise_dead by peterm and mehlhaff@soda.berkeley.edu
144 * op -- who is doing the resurrecting
145 * spell - spell object
146 * dir -- direction the spell is cast
147 * corpseobj - corpse to raise - can be null, in which case this function will find it
148 */
149 int
150 cast_raise_dead_spell (object *op, object *caster, object *spell, int dir, const char *arg)
151 {
152 object *temp, *newob;
153 char name_to_resurrect[MAX_BUF];
154 int leveldead = 25, mflags, clevel;
155 sint16 sx, sy;
156 maptile *m;
157
158 clevel = caster_level (caster, spell);
159
160 if (spell->last_heal)
161 {
162 if (!arg)
163 {
164 new_draw_info_format (NDI_UNIQUE, 0, op, "Cast %s on who?", &spell->name);
165 return 0;
166 }
167 strcpy (name_to_resurrect, arg);
168 temp = NULL;
169 }
170 else
171 {
172 sx = op->x + freearr_x[dir];
173 sy = op->y + freearr_y[dir];
174 m = op->map;
175 mflags = get_map_flags (m, &m, sx, sy, &sx, &sy);
176 if (mflags & P_OUT_OF_MAP)
177 temp = NULL;
178 else
179 {
180 /* First we need to find a corpse, if any. */
181 /* If no object, temp will be set to NULL */
182 for (temp = GET_MAP_OB (m, sx, sy); temp != NULL; temp = temp->above)
183 /* If it is corpse, this must be what we want to raise */
184 if (temp->type == CORPSE)
185 break;
186 }
187
188 if (temp == NULL)
189 {
190 new_draw_info (NDI_UNIQUE, 0, op, "You need a body for this spell.");
191 return 0;
192 }
193 strcpy (name_to_resurrect, temp->name);
194 }
195
196 /* no matter what, we fry the corpse. */
197 if (temp && temp->map)
198 {
199 /* replace corpse object with a burning object */
200 newob = arch_to_object (archetype::find ("burnout"));
201 if (newob)
202 newob->insert_at (temp, op);
203
204 leveldead = temp->level;
205 temp->destroy ();
206 }
207
208 if (resurrection_fails (clevel, leveldead))
209 {
210 if (spell->randomitems)
211 for (treasure *t = spell->randomitems->items; t; t = t->next)
212 if (t->item)
213 summon_hostile_monsters (op, t->nrof, t->item->name);
214
215 return 1;
216
217 }
218 else
219 return resurrect_player (op, name_to_resurrect, spell);
220 }
221
222
223 int
224 resurrection_fails (int levelcaster, int leveldead)
225 {
226 int chance = 9;
227
228 /* scheme: equal in level, 50% success.
229 * +5 % for each level below, -5% for each level above.
230 * minimum 20%
231 */
232 chance += levelcaster - leveldead;
233 if (chance < 4)
234 chance = 4;
235 if (chance > rndm (0, 19))
236 return 0; /* resurrection succeeds */
237 return 1;
238 }
239
240 void
241 dead_player (object *op)
242 {
243 char filename[MAX_BUF];
244 char newname[MAX_BUF];
245 char path[MAX_BUF];
246
247 /* set up our paths/strings... */
248 sprintf (path, "%s/%s/%s/%s", settings.localdir, settings.playerdir, &op->name, &op->name);
249
250 strcpy (filename, path);
251 strcat (filename, ".pl");
252 strcpy (newname, filename);
253 strcat (newname, ".dead");
254
255 if (rename (filename, newname) != 0)
256 {
257 LOG (llevError, "Cannot rename dead player's file %s into %s: %s\n", filename, newname, strerror (errno));
258 }
259 }
260
261
262
263 void
264 dead_character (const char *name)
265 {
266 char buf[MAX_BUF];
267 char buf2[MAX_BUF];
268
269 sprintf (buf, "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, name, name);
270 /* peterm: create a .dead filename.... ***.pl.dead */
271 strcpy (buf2, buf);
272 strcat (buf, ".dead");
273 if (rename (buf2, buf) == -1)
274 {
275 LOG (llevError, "Cannot rename dead player's file %s into %s: %s\n", buf2, buf, strerror (errno));
276 }
277 }
278
279
280 int
281 dead_player_exists (const char *name)
282 {
283 char buf[MAX_BUF];
284
285 sprintf (buf, "%s/%s/%s/%s", settings.localdir, settings.playerdir, name, name);
286 strcat (buf, ".pl.dead");
287 return !(access (buf, 0));
288 }