--- deliantra/server/common/exp.C 2006/08/29 08:01:35 1.2 +++ deliantra/server/common/exp.C 2012/01/03 11:25:30 1.39 @@ -1,85 +1,90 @@ /* - * static char *rcsid_arch_c = - * "$Id: exp.C,v 1.2 2006/08/29 08:01:35 root Exp $"; + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * 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 */ -/* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2002 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at crossfire-devel@real-time.com -*/ - #include #include -sint64 *levels; +sint64 levels[MAXNUMLEVELS]; -#define TRUE 1 -#define FALSE 0 +static const float exp_att_mult[NROFATTACKS + 2] = { + 0.0, /* AT_PHYSICAL */ + 0.0, /* AT_MAGIC */ + 0.0, /* AT_FIRE */ + 0.0, /* AT_ELECTRICITY */ + 0.0, /* AT_COLD */ + 0.0, /* AT_WATER *//*AT_CONFUSION! */ + 0.4, /* AT_ACID */ + 1.5, /* AT_DRAIN */ + 0.0, /* AT_WEAPONMAGIC */ + 0.1, /* AT_GHOSTHIT */ + 0.3, /* AT_POISON */ + 0.2, /* AT_DISEASE */ + 0.3, /* AT_PARALYZE */ + 0.0, /* AT_TURN_UNDEAD */ + 0.0, /* AT_FEAR */ + 0.0, /* AT_CANCELLATION */ + 0.0, /* AT_DEPLETE */ + 0.0, /* AT_DEATH */ + 0.0, /* AT_CHAOS */ + 0.0 /* AT_COUNTERSPELL */ +}; -float exp_att_mult[NROFATTACKS+2] = { - 0.0, /* AT_PHYSICAL */ - 0.0, /* AT_MAGIC */ - 0.0, /* AT_FIRE */ - 0.0, /* AT_ELECTRICITY */ - 0.0, /* AT_COLD */ - 0.0, /* AT_WATER *//*AT_CONFUSION!*/ - 0.4, /* AT_ACID */ - 1.5, /* AT_DRAIN */ - 0.0, /* AT_WEAPONMAGIC */ - 0.1, /* AT_GHOSTHIT */ - 0.3, /* AT_POISON */ - 0.2, /* AT_DISEASE */ - 0.3, /* AT_PARALYZE */ - 0.0, /* AT_TURN_UNDEAD */ - 0.0, /* AT_FEAR */ - 0.0, /* AT_CANCELLATION */ - 0.0, /* AT_DEPLETE */ - 0.0, /* AT_DEATH */ - 0.0, /* AT_CHAOS */ - 0.0 /* AT_COUNTERSPELL */ +static const float exp_prot_mult[NROFATTACKS + 2] = { + 0.4, /* AT_PHYSICAL */ + 0.5, /* AT_MAGIC */ + 0.1, /* AT_FIRE */ + 0.1, /* AT_ELECTRICITY */ + 0.1, /* AT_COLD */ + 0.1, /* AT_WATER */ + 0.1, /* AT_ACID */ + 0.1, /* AT_DRAIN */ + 0.1, /* AT_WEAPONMAGIC */ + 0.1, /* AT_GHOSTHIT */ + 0.1, /* AT_POISON */ + 0.1, /* AT_DISEASE */ + 0.1, /* AT_PARALYZE */ + 0.1, /* AT_TURN_UNDEAD */ + 0.1, /* AT_FEAR */ + 0.0, /* AT_CANCELLATION */ + 0.0, /* AT_DEPLETE */ + 0.0, /* AT_DEATH */ + 0.0, /* AT_CHAOS */ + 0.0 /* AT_COUNTERSPELL */ }; -float exp_prot_mult[NROFATTACKS+2] = { - 0.4, /* AT_PHYSICAL */ - 0.5, /* AT_MAGIC */ - 0.1, /* AT_FIRE */ - 0.1, /* AT_ELECTRICITY */ - 0.1, /* AT_COLD */ - 0.1, /* AT_WATER */ - 0.1, /* AT_ACID */ - 0.1, /* AT_DRAIN */ - 0.1, /* AT_WEAPONMAGIC */ - 0.1, /* AT_GHOSTHIT */ - 0.1, /* AT_POISON */ - 0.1, /* AT_DISEASE */ - 0.1, /* AT_PARALYZE */ - 0.1, /* AT_TURN_UNDEAD */ - 0.1, /* AT_FEAR */ - 0.0, /* AT_CANCELLATION */ - 0.0, /* AT_DEPLETE */ - 0.0, /* AT_DEATH */ - 0.0, /* AT_CHAOS */ - 0.0 /* AT_COUNTERSPELL */ +/* + * Returns true if the monster specified has any innate abilities. + */ +static int +has_ability (const object *ob) +{ + for (object *tmp = ob->inv; tmp; tmp = tmp->below) + if (tmp->type == SPELL || tmp->type == SPELLBOOK) + return true; -}; + return false; +} /* * new_exp() is an alternative way to calculate experience based @@ -89,154 +94,124 @@ * this is only used with one of the dumpflags, * and not anyplace in the code. */ - -int new_exp(const object *ob) { - double att_mult, prot_mult, spec_mult; - double exp; - int i; - long mask = 1; - - att_mult = prot_mult =spec_mult = 1.0; - for(i=0;iattacktype&mask) != FALSE)); - /* We multiply & then divide to prevent roundoffs on the floats. - * the doubling is to take into account the table and resistances - * are lower than they once were. - */ - prot_mult += (exp_prot_mult[i] * 200*ob->resist[i]) / 100.0; - } - spec_mult += (0.3*(QUERY_FLAG(ob,FLAG_SEE_INVISIBLE)!= FALSE)) + - (0.5*(QUERY_FLAG(ob,FLAG_SPLITTING)!= FALSE))+ - (0.3*(QUERY_FLAG(ob,FLAG_HITBACK)!= FALSE)) + - (0.1*(QUERY_FLAG(ob,FLAG_REFL_MISSILE)!= FALSE)) + - (0.3*(QUERY_FLAG(ob,FLAG_REFL_SPELL)!= FALSE)) + - (1.0*(QUERY_FLAG(ob,FLAG_NO_MAGIC)!= FALSE)) + - (0.1*(QUERY_FLAG(ob,FLAG_PICK_UP)!= FALSE)) + - (0.1*(QUERY_FLAG(ob,FLAG_USE_SCROLL)!= FALSE)) + - (0.2*(QUERY_FLAG(ob,FLAG_USE_RANGE)!= FALSE)) + - (0.1*(QUERY_FLAG(ob,FLAG_USE_BOW)!= FALSE)); - - exp = (ob->stats.maxhp<5) ? 5 : ob->stats.maxhp; - exp *= (QUERY_FLAG(ob,FLAG_CAST_SPELL) && has_ability(ob)) - ? (40+(ob->stats.maxsp>80?80:ob->stats.maxsp))/40 : 1; - exp *= (80.0/(70.0+ob->stats.wc)) * (80.0/(70.0+ob->stats.ac)) * (50.0+ob->stats.dam)/50.0; +int +new_exp (const object *ob) +{ + double att_mult, prot_mult, spec_mult; + double exp; + int i; + long mask = 1; + + att_mult = prot_mult = spec_mult = 1.0; + for (i = 0; i < NROFATTACKS; i++) + { + mask = 1 << i; + att_mult += (exp_att_mult[i] * ((ob->attacktype & mask) != 0)); + /* We multiply & then divide to prevent roundoffs on the floats. + * the doubling is to take into account the table and resistances + * are lower than they once were. + */ + prot_mult += (exp_prot_mult[i] * 200 * ob->resist[i]) / 100.0; + } + + spec_mult += (0.3 * (ob->flag [FLAG_SEE_INVISIBLE] != 0)) + + (0.5 * (ob->flag [FLAG_SPLITTING] != 0)) + + (0.3 * (ob->flag [FLAG_HITBACK] != 0)) + + (0.1 * (ob->flag [FLAG_REFL_MISSILE] != 0)) + + (0.3 * (ob->flag [FLAG_REFL_SPELL] != 0)) + + (1.0 * (ob->flag [FLAG_NO_MAGIC] != 0)) + + (0.1 * (ob->flag [FLAG_PICK_UP] != 0)) + + (0.1 * (ob->flag [FLAG_USE_SCROLL] != 0)) + + (0.2 * (ob->flag [FLAG_USE_RANGE] != 0)) + (0.1 * (ob->flag [FLAG_USE_BOW] != 0)); + + exp = (ob->stats.maxhp < 5) ? 5 : ob->stats.maxhp; + exp *= (ob->flag [FLAG_CAST_SPELL] && has_ability (ob)) ? (40 + (ob->stats.maxsp > 80 ? 80 : ob->stats.maxsp)) / 40 : 1; + exp *= (80.0 / (70.0 + ob->stats.wc)) * (80.0 / (70.0 + ob->stats.ac)) * (50.0 + ob->stats.dam) / 50.0; exp *= att_mult * prot_mult * spec_mult; - exp *= 2.0/(2.0-((FABS(ob->speed)<0.95)?FABS(ob->speed):0.95)); - exp *= (20.0+ob->stats.Con)/20.0; - if (QUERY_FLAG(ob, FLAG_STAND_STILL)) + exp *= 2.0 / (2.0 - min (ob->speed, 0.95)); + exp *= (20.0 + ob->stats.Con) / 20.0; + if (ob->flag [FLAG_STAND_STILL]) exp /= 2; - return (int) exp; + return exp; } -/* - * Returns true if the monster specified has any innate abilities. - */ +//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; -int has_ability(const object *ob) { - object *tmp; + return settings.max_level; +} - for(tmp=ob->inv;tmp!=NULL;tmp=tmp->below) - if(tmp->type==SPELL||tmp->type==SPELLBOOK) - return TRUE; - return FALSE; +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(void) +void +reload_exp_table () { - char buf[MAX_BUF], *cp; - int lastlevel=0, comp; - sint64 lastexp=-1, tmpexp; - FILE *fp; - + int lastlevel = 0; + sint64 lastexp = -1; - sprintf(buf,"%s/exp_table",settings.confdir); + object_thawer thawer (settings.datadir, "exp_table"); - if ((fp = open_and_uncompress(buf, 0, &comp)) == NULL) { - return; + if (!thawer) + { + LOG (llevError, "unable to load experience table file"); + return; } - while (fgets(buf, MAX_BUF-1, fp) != NULL) { - if (buf[0] == '#') continue; - /* eliminate newline */ - if ((cp=strrchr(buf,'\n'))!=NULL) *cp='\0'; + if (thawer.kw != KW_max_level) + { + thawer.parse_error ("experience table file"); + return; + } - /* Skip over empty lines */ - if (buf[0] == 0) continue; - cp = buf; - while (isspace(*cp) && *cp!=0) cp++; - if (!strncasecmp(cp, "max_level",9)) { - 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)); - } - } - while (isdigit(*cp) && *cp!=0) { - 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) { -#ifndef WIN32 - LOG(llevError,"Experience for level %d is lower than previous level (%lld <= %lld)\n", - lastlevel + 1, tmpexp, lastexp); -#else - LOG(llevError,"Experience for level %d is lower than previous level (%I64d <= %I64d)\n", - lastlevel + 1, tmpexp, lastexp); -#endif - 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++; + thawer.get (settings.max_level); + + sint64 newlevels [MAXNUMLEVELS]; + + while (thawer.next_line ()) + { + sint64 tmpexp; + thawer.get (tmpexp); + + /* 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); + return; } - } - 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); - } -} -/* Dump the table - useful in terms of debugging to make sure the - * format of the exp_table is correct. - */ + lastlevel++; -void dump_experience() -{ - int i; + if (lastlevel > settings.max_level) + { + LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level); + exit (1); + } - for (i=1; i<= settings.max_level; i++) { - fprintf(logfile,"%4d %20lld\n", i, levels[i]); + newlevels [lastlevel] = tmpexp; + lastexp = tmpexp; } - exit(0); + + if (lastlevel != settings.max_level && lastlevel != 0) + { + LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel); + return; + } + + memcpy (levels, newlevels, sizeof (levels)); } +