ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/resurrection.C
Revision: 1.20
Committed: Mon Apr 16 06:23:43 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.19: +5 -1 lines
Log Message:
VERY EXPERIMENTAL

- change the way archetypes and treasurelists are being loaded:
  - referring to a nonexisting treasurelist will create an empty one
  - referring to a nonexisting archetype will create an empty one
  - archetypes/treasurelists will overwrite any existing object
    of the same name.

- net effect should be to allow reloading of archetypes and treasurelists
  at runtime at a later stage.

File Contents

# User Rev Content
1 elmex 1.1 /*
2 pippijn 1.19 * 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 elmex 1.1
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 root 1.14 #include <sproto.h>
30 elmex 1.1 #include <spells.h>
31     #include <errno.h>
32 root 1.5
33 elmex 1.1 /* name of the person to resurrect and which spell was used
34     * to resurrect
35     */
36 root 1.5 static int
37     resurrect_player (object *op, char *playername, object *spell)
38 elmex 1.1 {
39 root 1.5 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 root 1.20 treasurelist *tl = treasurelist::find (spell->race);
54 root 1.5 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 root 1.20
63 root 1.5 value = RANDOM () % tl->total_chance;
64 root 1.20
65 root 1.5 for (t = tl->items; t; t = t->next)
66     {
67     value -= t->chance;
68     if (value < 0)
69     break;
70     }
71 root 1.20
72 elmex 1.16 if (!t || !t->item)
73 root 1.5 {
74     LOG (llevError, "resurrect_player: got null treasure from treasurelist %s!\n", &spell->race);
75     return 0;
76     }
77 root 1.20
78 root 1.5 race = t->item->name;
79     }
80    
81     /* set up our paths/strings... */
82     sprintf (path, "%s/%s/%s/%s", settings.localdir, settings.playerdir, playername, playername);
83    
84     strcpy (newname, path);
85     strcat (newname, ".pl");
86    
87     strcpy (oldname, newname);
88     strcat (oldname, ".dead");
89 elmex 1.1
90 root 1.5 if (!(deadplayer = fopen (oldname, "r")))
91     {
92     new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s cannot be reached.", playername);
93     return 0;
94     }
95    
96     if (!access (newname, 0))
97     {
98     new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s has already been reborn!", playername);
99     fclose (deadplayer);
100     return 0;
101     }
102    
103     if (!(liveplayer = fopen (newname, "w")))
104     {
105     new_draw_info_format (NDI_UNIQUE, 0, op, "The soul of %s cannot be re-embodied at the moment.", playername);
106     LOG (llevError, "Cannot write player in resurrect_player!\n");
107     fclose (deadplayer);
108     return 0;
109     }
110    
111     while (!feof (deadplayer))
112     {
113     fgets (buf, 255, deadplayer);
114     sscanf (buf, "%s", buf2);
115     if (!(strcmp (buf2, "exp")))
116     {
117 root 1.10 sscanf (buf, "%s %" SCNd64, buf2, &exp);
118 root 1.5 if (spell->stats.exp)
119     {
120     exp -= exp / spell->stats.exp;
121 root 1.10 sprintf (buf, "exp %" PRId64 "\n", exp);
122 root 1.2 }
123     }
124 root 1.5 if (!(strcmp (buf2, "Con")))
125     {
126     sscanf (buf, "%s %d", buf2, &Con);
127     Con -= spell->stats.Con;
128     if (Con < 1)
129     Con = 1;
130     sprintf (buf, "Con %d\n", Con);
131     }
132     if (race && !strcmp (buf2, "race"))
133     {
134     sprintf (buf, "race %s\n", race);
135     }
136     fputs (buf, liveplayer);
137     }
138     fclose (liveplayer);
139     fclose (deadplayer);
140     unlink (oldname);
141     new_draw_info_format (NDI_UNIQUE, 0, op, "%s lives again!", playername);
142 elmex 1.1
143 root 1.5 return 1;
144 elmex 1.1 }
145    
146    
147     /* raise_dead by peterm and mehlhaff@soda.berkeley.edu
148     * op -- who is doing the resurrecting
149     * spell - spell object
150     * dir -- direction the spell is cast
151     * corpseobj - corpse to raise - can be null, in which case this function will find it
152     */
153 root 1.5 int
154     cast_raise_dead_spell (object *op, object *caster, object *spell, int dir, const char *arg)
155 elmex 1.1 {
156 root 1.5 object *temp, *newob;
157     char name_to_resurrect[MAX_BUF];
158     int leveldead = 25, mflags, clevel;
159     sint16 sx, sy;
160 root 1.8 maptile *m;
161 root 1.5
162     clevel = caster_level (caster, spell);
163    
164     if (spell->last_heal)
165     {
166     if (!arg)
167     {
168     new_draw_info_format (NDI_UNIQUE, 0, op, "Cast %s on who?", &spell->name);
169     return 0;
170 root 1.2 }
171 root 1.5 strcpy (name_to_resurrect, arg);
172     temp = NULL;
173     }
174     else
175     {
176     sx = op->x + freearr_x[dir];
177     sy = op->y + freearr_y[dir];
178     m = op->map;
179     mflags = get_map_flags (m, &m, sx, sy, &sx, &sy);
180     if (mflags & P_OUT_OF_MAP)
181 root 1.2 temp = NULL;
182 root 1.5 else
183     {
184     /* First we need to find a corpse, if any. */
185     /* If no object, temp will be set to NULL */
186 root 1.13 for (temp = GET_MAP_OB (m, sx, sy); temp != NULL; temp = temp->above)
187 root 1.5 /* If it is corpse, this must be what we want to raise */
188     if (temp->type == CORPSE)
189     break;
190     }
191    
192     if (temp == NULL)
193     {
194     new_draw_info (NDI_UNIQUE, 0, op, "You need a body for this spell.");
195     return 0;
196     }
197     strcpy (name_to_resurrect, temp->name);
198     }
199 elmex 1.1
200 root 1.5 /* no matter what, we fry the corpse. */
201     if (temp && temp->map)
202     {
203     /* replace corpse object with a burning object */
204 root 1.6 newob = arch_to_object (archetype::find ("burnout"));
205 root 1.15 if (newob)
206     newob->insert_at (temp, op);
207    
208 root 1.5 leveldead = temp->level;
209 root 1.12 temp->destroy ();
210 root 1.5 }
211    
212     if (resurrection_fails (clevel, leveldead))
213     {
214     if (spell->randomitems)
215 root 1.15 for (treasure *t = spell->randomitems->items; t; t = t->next)
216 elmex 1.16 if (t->item)
217     summon_hostile_monsters (op, t->nrof, t->item->name);
218 elmex 1.1
219 root 1.5 return 1;
220 elmex 1.1
221 root 1.5 }
222     else
223 root 1.15 return resurrect_player (op, name_to_resurrect, spell);
224 elmex 1.1 }
225    
226    
227 root 1.5 int
228     resurrection_fails (int levelcaster, int leveldead)
229 elmex 1.1 {
230 root 1.5 int chance = 9;
231    
232     /* scheme: equal in level, 50% success.
233     * +5 % for each level below, -5% for each level above.
234     * minimum 20%
235     */
236     chance += levelcaster - leveldead;
237     if (chance < 4)
238     chance = 4;
239     if (chance > rndm (0, 19))
240     return 0; /* resurrection succeeds */
241     return 1;
242 elmex 1.1 }
243    
244 root 1.5 void
245     dead_player (object *op)
246 elmex 1.1 {
247 root 1.5 char filename[MAX_BUF];
248     char newname[MAX_BUF];
249     char path[MAX_BUF];
250    
251     /* set up our paths/strings... */
252     sprintf (path, "%s/%s/%s/%s", settings.localdir, settings.playerdir, &op->name, &op->name);
253    
254     strcpy (filename, path);
255     strcat (filename, ".pl");
256     strcpy (newname, filename);
257     strcat (newname, ".dead");
258    
259     if (rename (filename, newname) != 0)
260     {
261     LOG (llevError, "Cannot rename dead player's file %s into %s: %s\n", filename, newname, strerror (errno));
262 elmex 1.1 }
263     }
264    
265    
266    
267 root 1.5 void
268     dead_character (const char *name)
269     {
270     char buf[MAX_BUF];
271     char buf2[MAX_BUF];
272    
273     sprintf (buf, "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, name, name);
274     /* peterm: create a .dead filename.... ***.pl.dead */
275     strcpy (buf2, buf);
276     strcat (buf, ".dead");
277     if (rename (buf2, buf) == -1)
278     {
279     LOG (llevError, "Cannot rename dead player's file %s into %s: %s\n", buf2, buf, strerror (errno));
280 elmex 1.1 }
281     }
282    
283    
284 root 1.5 int
285     dead_player_exists (const char *name)
286     {
287     char buf[MAX_BUF];
288 elmex 1.1
289 root 1.5 sprintf (buf, "%s/%s/%s/%s", settings.localdir, settings.playerdir, name, name);
290     strcat (buf, ".pl.dead");
291     return !(access (buf, 0));
292 elmex 1.1 }