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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 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 |
… | |
… | |
23 | */ |
23 | */ |
24 | |
24 | |
25 | #include <stdio.h> |
25 | #include <stdio.h> |
26 | #include <global.h> |
26 | #include <global.h> |
27 | |
27 | |
28 | sint64 *levels; |
28 | sint64 levels[MAXNUMLEVELS]; |
29 | |
29 | |
30 | static float exp_att_mult[NROFATTACKS + 2] = { |
30 | static 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 | |
53 | static float exp_prot_mult[NROFATTACKS + 2] = { |
53 | static 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 */ |
… | |
… | |
77 | * Returns true if the monster specified has any innate abilities. |
77 | * Returns true if the monster specified has any innate abilities. |
78 | */ |
78 | */ |
79 | static int |
79 | static int |
80 | has_ability (const object *ob) |
80 | has_ability (const object *ob) |
81 | { |
81 | { |
82 | object *tmp; |
|
|
83 | |
|
|
84 | for (tmp = ob->inv; tmp != NULL; tmp = tmp->below) |
82 | for (object *tmp = ob->inv; tmp; tmp = tmp->below) |
85 | if (tmp->type == SPELL || tmp->type == SPELLBOOK) |
83 | if (tmp->type == SPELL || tmp->type == SPELLBOOK) |
86 | return true; |
84 | return true; |
|
|
85 | |
87 | return false; |
86 | return false; |
88 | } |
87 | } |
89 | |
88 | |
90 | /* |
89 | /* |
91 | * new_exp() is an alternative way to calculate experience based |
90 | * new_exp() is an alternative way to calculate experience based |
… | |
… | |
113 | * are lower than they once were. |
112 | * are lower than they once were. |
114 | */ |
113 | */ |
115 | prot_mult += (exp_prot_mult[i] * 200 * ob->resist[i]) / 100.0; |
114 | prot_mult += (exp_prot_mult[i] * 200 * ob->resist[i]) / 100.0; |
116 | } |
115 | } |
117 | |
116 | |
118 | spec_mult += (0.3 * (QUERY_FLAG (ob, FLAG_SEE_INVISIBLE) != 0)) + |
117 | spec_mult += (0.3 * (ob->flag [FLAG_SEE_INVISIBLE] != 0)) + |
119 | (0.5 * (QUERY_FLAG (ob, FLAG_SPLITTING) != 0)) + |
118 | (0.5 * (ob->flag [FLAG_SPLITTING] != 0)) + |
120 | (0.3 * (QUERY_FLAG (ob, FLAG_HITBACK) != 0)) + |
119 | (0.3 * (ob->flag [FLAG_HITBACK] != 0)) + |
121 | (0.1 * (QUERY_FLAG (ob, FLAG_REFL_MISSILE) != 0)) + |
120 | (0.1 * (ob->flag [FLAG_REFL_MISSILE] != 0)) + |
122 | (0.3 * (QUERY_FLAG (ob, FLAG_REFL_SPELL) != 0)) + |
121 | (0.3 * (ob->flag [FLAG_REFL_SPELL] != 0)) + |
123 | (1.0 * (QUERY_FLAG (ob, FLAG_NO_MAGIC) != 0)) + |
122 | (1.0 * (ob->flag [FLAG_NO_MAGIC] != 0)) + |
124 | (0.1 * (QUERY_FLAG (ob, FLAG_PICK_UP) != 0)) + |
123 | (0.1 * (ob->flag [FLAG_PICK_UP] != 0)) + |
125 | (0.1 * (QUERY_FLAG (ob, FLAG_USE_SCROLL) != 0)) + |
124 | (0.1 * (ob->flag [FLAG_USE_SCROLL] != 0)) + |
126 | (0.2 * (QUERY_FLAG (ob, FLAG_USE_RANGE) != 0)) + (0.1 * (QUERY_FLAG (ob, FLAG_USE_BOW) != 0)); |
125 | (0.2 * (ob->flag [FLAG_USE_RANGE] != 0)) + (0.1 * (ob->flag [FLAG_USE_BOW] != 0)); |
127 | |
126 | |
128 | exp = (ob->stats.maxhp < 5) ? 5 : ob->stats.maxhp; |
127 | exp = (ob->stats.maxhp < 5) ? 5 : ob->stats.maxhp; |
129 | exp *= (QUERY_FLAG (ob, FLAG_CAST_SPELL) && has_ability (ob)) ? (40 + (ob->stats.maxsp > 80 ? 80 : ob->stats.maxsp)) / 40 : 1; |
128 | exp *= (ob->flag [FLAG_CAST_SPELL] && has_ability (ob)) ? (40 + (ob->stats.maxsp > 80 ? 80 : ob->stats.maxsp)) / 40 : 1; |
130 | exp *= (80.0 / (70.0 + ob->stats.wc)) * (80.0 / (70.0 + ob->stats.ac)) * (50.0 + ob->stats.dam) / 50.0; |
129 | exp *= (80.0 / (70.0 + ob->stats.wc)) * (80.0 / (70.0 + ob->stats.ac)) * (50.0 + ob->stats.dam) / 50.0; |
131 | exp *= att_mult * prot_mult * spec_mult; |
130 | exp *= att_mult * prot_mult * spec_mult; |
132 | exp *= 2.0 / (2.0 - min (ob->speed, 0.95)); |
131 | exp *= 2.0 / (2.0 - min (ob->speed, 0.95)); |
133 | exp *= (20.0 + ob->stats.Con) / 20.0; |
132 | exp *= (20.0 + ob->stats.Con) / 20.0; |
134 | if (QUERY_FLAG (ob, FLAG_STAND_STILL)) |
133 | if (ob->flag [FLAG_STAND_STILL]) |
135 | exp /= 2; |
134 | exp /= 2; |
136 | |
135 | |
137 | return exp; |
136 | return exp; |
|
|
137 | } |
|
|
138 | |
|
|
139 | //TODO: binary search... |
|
|
140 | int |
|
|
141 | exp_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 | |
|
|
150 | sint64 |
|
|
151 | level_to_min_exp (int level) |
|
|
152 | { |
|
|
153 | if (level <= 0) |
|
|
154 | return 0; |
|
|
155 | else |
|
|
156 | return levels [min (level, settings.max_level)]; |
138 | } |
157 | } |
139 | |
158 | |
140 | /* This loads the experience table from the exp_table |
159 | /* This loads the experience table from the exp_table |
141 | * file. This tends to exit on any errors, since it |
160 | * file. This tends to exit on any errors, since it |
142 | * populates the table as it goes along, so if there |
161 | * populates the table as it goes along, so if there |
… | |
… | |
144 | * state. |
163 | * state. |
145 | */ |
164 | */ |
146 | void |
165 | void |
147 | init_experience () |
166 | init_experience () |
148 | { |
167 | { |
149 | char buf[MAX_BUF], *cp; |
|
|
150 | int lastlevel = 0, comp; |
168 | int lastlevel = 0; |
151 | sint64 lastexp = -1, tmpexp; |
169 | sint64 lastexp = -1; |
152 | FILE *fp; |
|
|
153 | |
170 | |
154 | sprintf (buf, "%s/exp_table", settings.confdir); |
171 | object_thawer thawer (settings.datadir, "exp_table"); |
155 | |
172 | |
156 | if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) |
173 | if (!thawer) |
157 | { |
174 | { |
|
|
175 | LOG (llevError, "unable to parse experience table file"); |
158 | return; |
176 | return; |
159 | } |
177 | } |
160 | while (fgets (buf, MAX_BUF - 1, fp) != NULL) |
178 | |
|
|
179 | if (thawer.kw != KW_max_level) |
|
|
180 | { |
|
|
181 | thawer.parse_error ("experience table file"); |
|
|
182 | return; |
161 | { |
183 | } |
162 | if (buf[0] == '#') |
|
|
163 | continue; |
|
|
164 | |
184 | |
165 | /* eliminate newline */ |
185 | thawer.get (settings.max_level); |
166 | if ((cp = strrchr (buf, '\n')) != NULL) |
|
|
167 | *cp = '\0'; |
|
|
168 | |
186 | |
169 | /* Skip over empty lines */ |
187 | sint64 newlevels [MAXNUMLEVELS]; |
170 | if (buf[0] == 0) |
188 | |
171 | continue; |
189 | while (thawer.next_line ()) |
172 | cp = buf; |
190 | { |
173 | while (isspace (*cp) && *cp != 0) |
191 | sint64 tmpexp; |
174 | cp++; |
192 | thawer.get (tmpexp); |
175 | if (!strncasecmp (cp, "max_level", 9)) |
193 | |
|
|
194 | /* Do some sanity checking - if value is bogus, just exit because |
|
|
195 | * the table otherwise is probably in an inconsistent state |
|
|
196 | */ |
|
|
197 | if (tmpexp <= lastexp) |
176 | { |
198 | { |
177 | if (settings.max_level) |
199 | LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp); |
178 | { |
200 | return; |
179 | LOG (llevDebug, "Got more than one max_level value from exp_table file?\n"); |
|
|
180 | free (levels); |
|
|
181 | } |
|
|
182 | settings.max_level = atoi (cp + 9); |
|
|
183 | if (!settings.max_level) |
|
|
184 | { |
|
|
185 | LOG (llevDebug, "Got invalid max_level from exp_table file? %s\n", buf); |
|
|
186 | } |
|
|
187 | else |
|
|
188 | { |
|
|
189 | levels = (sint64 *) calloc (settings.max_level + 1, sizeof (sint64)); |
|
|
190 | } |
|
|
191 | } |
201 | } |
192 | while (isdigit (*cp) && *cp != 0) |
202 | |
|
|
203 | lastlevel++; |
|
|
204 | |
|
|
205 | if (lastlevel > settings.max_level) |
193 | { |
206 | { |
194 | if (!settings.max_level) |
|
|
195 | { |
|
|
196 | LOG (llevError, "max_level is not set in exp_table file. Did you remember to update it?\n"); |
|
|
197 | exit (1); |
|
|
198 | } |
|
|
199 | |
|
|
200 | tmpexp = atoll (cp); |
|
|
201 | /* Do some sanity checking - if value is bogus, just exit because |
|
|
202 | * the table otherwise is probably in an inconsistent state |
|
|
203 | */ |
|
|
204 | if (tmpexp <= lastexp) |
|
|
205 | { |
|
|
206 | LOG (llevError, "Experience for level %d is lower than previous level (%" PRId64 " <= %" PRId64 ")\n", lastlevel + 1, tmpexp, lastexp); |
|
|
207 | exit (1); |
|
|
208 | } |
|
|
209 | lastlevel++; |
|
|
210 | if (lastlevel > settings.max_level) |
|
|
211 | { |
|
|
212 | LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level); |
207 | LOG (llevError, "Too many levels specified in table (%d > %d)\n", lastlevel, settings.max_level); |
213 | exit (1); |
208 | exit (1); |
214 | } |
|
|
215 | levels[lastlevel] = tmpexp; |
|
|
216 | lastexp = tmpexp; |
|
|
217 | /* First, skip over the number we just processed. Then skip over |
|
|
218 | * any spaces, commas, etc. |
|
|
219 | */ |
|
|
220 | while (isdigit (*cp) && *cp != 0) |
|
|
221 | cp++; |
|
|
222 | while (!isdigit (*cp) && *cp != 0) |
|
|
223 | cp++; |
|
|
224 | } |
209 | } |
|
|
210 | |
|
|
211 | newlevels [lastlevel] = tmpexp; |
|
|
212 | lastexp = tmpexp; |
225 | } |
213 | } |
226 | close_and_delete (fp, comp); |
214 | |
227 | if (lastlevel != settings.max_level && lastlevel != 0) |
215 | if (lastlevel != settings.max_level && lastlevel != 0) |
228 | { |
216 | { |
229 | LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel); |
217 | LOG (llevError, "Warning: exp_table does not have %d entries (%d)\n", settings.max_level, lastlevel); |
230 | exit (1); |
218 | return; |
231 | } |
219 | } |
232 | } |
|
|
233 | |
220 | |
|
|
221 | memcpy (levels, newlevels, sizeof (levels)); |
|
|
222 | } |
|
|
223 | |