--- deliantra/server/common/exp.C 2010/04/11 00:34:05 1.29
+++ deliantra/server/common/exp.C 2012/10/29 23:55:52 1.40
@@ -1,33 +1,33 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ *
+ * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992 Frank Tore Johansen
- *
+ *
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
- *
+ *
* The authors can be reached via e-mail to
*/
#include
#include
-sint64 *levels;
+sint64 levels[MAXNUMLEVELS];
-static float exp_att_mult[NROFATTACKS + 2] = {
+static const float exp_att_mult[NROFATTACKS + 2] = {
0.0, /* AT_PHYSICAL */
0.0, /* AT_MAGIC */
0.0, /* AT_FIRE */
@@ -50,7 +50,7 @@
0.0 /* AT_COUNTERSPELL */
};
-static float exp_prot_mult[NROFATTACKS + 2] = {
+static const float exp_prot_mult[NROFATTACKS + 2] = {
0.4, /* AT_PHYSICAL */
0.5, /* AT_MAGIC */
0.1, /* AT_FIRE */
@@ -136,97 +136,82 @@
return exp;
}
+//TODO: binary search...
+int
+exp_to_level (sint64 exp)
+{
+ for (int i = 1; i <= settings.max_level; i++)
+ if (levels [i] > exp)
+ return i - 1;
+
+ return settings.max_level;
+}
+
+sint64
+level_to_min_exp (int level)
+{
+ return levels [clamp (level, 0, settings.max_level)];
+}
+
/* This loads the experience table from the exp_table
- * file. This tends to exit on any errors, since it
- * populates the table as it goes along, so if there
- * are errors, the table is likely in an inconsistent
- * state.
+ * file.
*/
void
-init_experience ()
+reload_exp_table ()
{
- char buf[MAX_BUF], *cp;
- int lastlevel = 0, comp;
- sint64 lastexp = -1, tmpexp;
- FILE *fp;
+ int lastlevel = 0;
+ sint64 lastexp = -1;
+
+ object_thawer thawer (settings.datadir, "exp_table");
- sprintf (buf, "%s/exp_table", settings.confdir);
+ if (!thawer)
+ {
+ LOG (llevError, "unable to load experience table file");
+ return;
+ }
- if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
+ if (thawer.kw != KW_max_level)
{
+ thawer.parse_error ("experience table file");
return;
}
- while (fgets (buf, MAX_BUF - 1, fp) != NULL)
+
+ thawer.get (settings.max_level);
+
+ sint64 newlevels [MAXNUMLEVELS];
+
+ while (thawer.next_line ())
{
- if (buf[0] == '#')
- continue;
+ sint64 tmpexp;
+ thawer.get (tmpexp);
- /* eliminate newline */
- if ((cp = strrchr (buf, '\n')) != NULL)
- *cp = '\0';
-
- /* Skip over empty lines */
- if (buf[0] == 0)
- continue;
- cp = buf;
- while (isspace (*cp) && *cp != 0)
- cp++;
- if (!strncasecmp (cp, "max_level", 9))
+ /* Do some sanity checking - if value is bogus, just exit because
+ * the table otherwise is probably in an inconsistent state
+ */
+ if (tmpexp <= lastexp)
{
- if (settings.max_level)
- {
- LOG (llevDebug, "Got more than one max_level value from exp_table file?\n");
- free (levels);
- }
- settings.max_level = atoi (cp + 9);
- if (!settings.max_level)
- {
- LOG (llevDebug, "Got invalid max_level from exp_table file? %s\n", buf);
- }
- else
- {
- levels = (sint64 *) calloc (settings.max_level + 1, sizeof (sint64));
- }
+ LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp);
+ return;
}
- while (isdigit (*cp) && *cp != 0)
+
+ lastlevel++;
+
+ if (lastlevel > settings.max_level)
{
- if (!settings.max_level)
- {
- LOG (llevError, "max_level is not set in exp_table file. Did you remember to update it?\n");
- exit (1);
- }
-
- tmpexp = atoll (cp);
- /* Do some sanity checking - if value is bogus, just exit because
- * the table otherwise is probably in an inconsistent state
- */
- if (tmpexp <= lastexp)
- {
- LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp);
- exit (1);
- }
- lastlevel++;
- if (lastlevel > settings.max_level)
- {
- LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level);
- exit (1);
- }
- levels[lastlevel] = tmpexp;
- lastexp = tmpexp;
- /* First, skip over the number we just processed. Then skip over
- * any spaces, commas, etc.
- */
- while (isdigit (*cp) && *cp != 0)
- cp++;
- while (!isdigit (*cp) && *cp != 0)
- cp++;
+ LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level);
+ exit (1);
}
+
+ newlevels [lastlevel] = tmpexp;
+ lastexp = tmpexp;
}
- close_and_delete (fp, comp);
+
if (lastlevel != settings.max_level && lastlevel != 0)
{
LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel);
- exit (1);
+ return;
}
+
+ memcpy (levels, newlevels, sizeof (levels));
}