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.36 by root, Thu May 6 22:35:41 2010 UTC

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 if (level <= 0)
154 return 0;
155 else
156 return levels [min (level, settings.max_level)];
157}
158
139/* This loads the experience table from the exp_table 159/* This loads the experience table from the exp_table
140 * file. This tends to exit on any errors, since it 160 * 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 */ 161 */
145void 162void
146init_experience () 163reload_exp_table ()
147{ 164{
148 char buf[MAX_BUF], *cp;
149 int lastlevel = 0, comp; 165 int lastlevel = 0;
150 sint64 lastexp = -1, tmpexp; 166 sint64 lastexp = -1;
151 FILE *fp;
152 167
153 sprintf (buf, "%s/exp_table", settings.confdir); 168 object_thawer thawer (settings.datadir, "exp_table");
154 169
155 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) 170 if (!thawer)
156 { 171 {
172 LOG (llevError, "unable to load experience table file");
157 return; 173 return;
158 } 174 }
159 while (fgets (buf, MAX_BUF - 1, fp) != NULL) 175
176 if (thawer.kw != KW_max_level)
177 {
178 thawer.parse_error ("experience table file");
179 return;
160 { 180 }
161 if (buf[0] == '#')
162 continue;
163 181
164 /* eliminate newline */ 182 thawer.get (settings.max_level);
165 if ((cp = strrchr (buf, '\n')) != NULL)
166 *cp = '\0';
167 183
168 /* Skip over empty lines */ 184 sint64 newlevels [MAXNUMLEVELS];
169 if (buf[0] == 0) 185
170 continue; 186 while (thawer.next_line ())
171 cp = buf; 187 {
172 while (isspace (*cp) && *cp != 0) 188 sint64 tmpexp;
173 cp++; 189 thawer.get (tmpexp);
174 if (!strncasecmp (cp, "max_level", 9)) 190
191 /* Do some sanity checking - if value is bogus, just exit because
192 * the table otherwise is probably in an inconsistent state
193 */
194 if (tmpexp <= lastexp)
175 { 195 {
176 if (settings.max_level) 196 LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp);
177 { 197 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 } 198 }
191 while (isdigit (*cp) && *cp != 0) 199
200 lastlevel++;
201
202 if (lastlevel > settings.max_level)
192 { 203 {
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); 204 LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level);
212 exit (1); 205 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 } 206 }
207
208 newlevels [lastlevel] = tmpexp;
209 lastexp = tmpexp;
224 } 210 }
225 close_and_delete (fp, comp); 211
226 if (lastlevel != settings.max_level && lastlevel != 0) 212 if (lastlevel != settings.max_level && lastlevel != 0)
227 { 213 {
228 LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel); 214 LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel);
229 exit (1); 215 return;
230 } 216 }
231}
232 217
218 memcpy (levels, newlevels, sizeof (levels));
219}
220

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines