ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/resurrection.C
Revision: 1.5
Committed: Sun Sep 10 15:59:57 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +234 -195 lines
Log Message:
indent

File Contents

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