ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/exp.C
(Generate patch)

Comparing deliantra/server/common/exp.C (file contents):
Revision 1.29 by root, Sun Apr 11 00:34:05 2010 UTC vs.
Revision 1.40 by root, Mon Oct 29 23:55:52 2012 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the 9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the Affero GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25#include <stdio.h> 25#include <stdio.h>
26#include <global.h> 26#include <global.h>
27 27
28sint64 *levels; 28sint64 levels[MAXNUMLEVELS];
29 29
30static float exp_att_mult[NROFATTACKS + 2] = { 30static const float exp_att_mult[NROFATTACKS + 2] = {
31 0.0, /* AT_PHYSICAL */ 31 0.0, /* AT_PHYSICAL */
32 0.0, /* AT_MAGIC */ 32 0.0, /* AT_MAGIC */
33 0.0, /* AT_FIRE */ 33 0.0, /* AT_FIRE */
34 0.0, /* AT_ELECTRICITY */ 34 0.0, /* AT_ELECTRICITY */
35 0.0, /* AT_COLD */ 35 0.0, /* AT_COLD */
48 0.0, /* AT_DEATH */ 48 0.0, /* AT_DEATH */
49 0.0, /* AT_CHAOS */ 49 0.0, /* AT_CHAOS */
50 0.0 /* AT_COUNTERSPELL */ 50 0.0 /* AT_COUNTERSPELL */
51}; 51};
52 52
53static float exp_prot_mult[NROFATTACKS + 2] = { 53static const float exp_prot_mult[NROFATTACKS + 2] = {
54 0.4, /* AT_PHYSICAL */ 54 0.4, /* AT_PHYSICAL */
55 0.5, /* AT_MAGIC */ 55 0.5, /* AT_MAGIC */
56 0.1, /* AT_FIRE */ 56 0.1, /* AT_FIRE */
57 0.1, /* AT_ELECTRICITY */ 57 0.1, /* AT_ELECTRICITY */
58 0.1, /* AT_COLD */ 58 0.1, /* AT_COLD */
134 exp /= 2; 134 exp /= 2;
135 135
136 return exp; 136 return exp;
137} 137}
138 138
139//TODO: binary search...
140int
141exp_to_level (sint64 exp)
142{
143 for (int i = 1; i <= settings.max_level; i++)
144 if (levels [i] > exp)
145 return i - 1;
146
147 return settings.max_level;
148}
149
150sint64
151level_to_min_exp (int level)
152{
153 return levels [clamp (level, 0, settings.max_level)];
154}
155
139/* This loads the experience table from the exp_table 156/* This loads the experience table from the exp_table
140 * file. This tends to exit on any errors, since it 157 * file.
141 * populates the table as it goes along, so if there
142 * are errors, the table is likely in an inconsistent
143 * state.
144 */ 158 */
145void 159void
146init_experience () 160reload_exp_table ()
147{ 161{
148 char buf[MAX_BUF], *cp;
149 int lastlevel = 0, comp; 162 int lastlevel = 0;
150 sint64 lastexp = -1, tmpexp; 163 sint64 lastexp = -1;
151 FILE *fp;
152 164
153 sprintf (buf, "%s/exp_table", settings.confdir); 165 object_thawer thawer (settings.datadir, "exp_table");
154 166
155 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) 167 if (!thawer)
156 { 168 {
169 LOG (llevError, "unable to load experience table file");
157 return; 170 return;
158 } 171 }
159 while (fgets (buf, MAX_BUF - 1, fp) != NULL) 172
173 if (thawer.kw != KW_max_level)
174 {
175 thawer.parse_error ("experience table file");
176 return;
160 { 177 }
161 if (buf[0] == '#')
162 continue;
163 178
164 /* eliminate newline */ 179 thawer.get (settings.max_level);
165 if ((cp = strrchr (buf, '\n')) != NULL)
166 *cp = '\0';
167 180
168 /* Skip over empty lines */ 181 sint64 newlevels [MAXNUMLEVELS];
169 if (buf[0] == 0) 182
170 continue; 183 while (thawer.next_line ())
171 cp = buf; 184 {
172 while (isspace (*cp) && *cp != 0) 185 sint64 tmpexp;
173 cp++; 186 thawer.get (tmpexp);
174 if (!strncasecmp (cp, "max_level", 9)) 187
188 /* Do some sanity checking - if value is bogus, just exit because
189 * the table otherwise is probably in an inconsistent state
190 */
191 if (tmpexp <= lastexp)
175 { 192 {
176 if (settings.max_level) 193 LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp);
177 { 194 return;
178 LOG (llevDebug, "Got more than one max_level value from exp_table file?\n");
179 free (levels);
180 }
181 settings.max_level = atoi (cp + 9);
182 if (!settings.max_level)
183 {
184 LOG (llevDebug, "Got invalid max_level from exp_table file? %s\n", buf);
185 }
186 else
187 {
188 levels = (sint64 *) calloc (settings.max_level + 1, sizeof (sint64));
189 }
190 } 195 }
191 while (isdigit (*cp) && *cp != 0) 196
197 lastlevel++;
198
199 if (lastlevel > settings.max_level)
192 { 200 {
193 if (!settings.max_level)
194 {
195 LOG (llevError, "max_level is not set in exp_table file. Did you remember to update it?\n");
196 exit (1);
197 }
198
199 tmpexp = atoll (cp);
200 /* Do some sanity checking - if value is bogus, just exit because
201 * the table otherwise is probably in an inconsistent state
202 */
203 if (tmpexp <= lastexp)
204 {
205 LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp);
206 exit (1);
207 }
208 lastlevel++;
209 if (lastlevel > settings.max_level)
210 {
211 LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level); 201 LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level);
212 exit (1); 202 exit (1);
213 }
214 levels[lastlevel] = tmpexp;
215 lastexp = tmpexp;
216 /* First, skip over the number we just processed. Then skip over
217 * any spaces, commas, etc.
218 */
219 while (isdigit (*cp) && *cp != 0)
220 cp++;
221 while (!isdigit (*cp) && *cp != 0)
222 cp++;
223 } 203 }
204
205 newlevels [lastlevel] = tmpexp;
206 lastexp = tmpexp;
224 } 207 }
225 close_and_delete (fp, comp); 208
226 if (lastlevel != settings.max_level && lastlevel != 0) 209 if (lastlevel != settings.max_level && lastlevel != 0)
227 { 210 {
228 LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel); 211 LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel);
229 exit (1); 212 return;
230 } 213 }
231}
232 214
215 memcpy (levels, newlevels, sizeof (levels));
216}
217

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines