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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines