1 |
elmex |
1.1 |
/* |
2 |
root |
1.44 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
pippijn |
1.31 |
* |
4 |
root |
1.76 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.75 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
|
|
* Copyright (©) 1992 Frank Tore Johansen |
7 |
pippijn |
1.31 |
* |
8 |
root |
1.70 |
* 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 |
10 |
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
|
|
* option) any later version. |
12 |
pippijn |
1.31 |
* |
13 |
root |
1.40 |
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
pippijn |
1.31 |
* |
18 |
root |
1.70 |
* You should have received a copy of the Affero GNU General Public License |
19 |
|
|
* and the GNU General Public License along with this program. If not, see |
20 |
|
|
* <http://www.gnu.org/licenses/>. |
21 |
root |
1.37 |
* |
22 |
root |
1.44 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
pippijn |
1.31 |
*/ |
24 |
elmex |
1.1 |
|
25 |
|
|
#include <global.h> |
26 |
|
|
#include <spells.h> |
27 |
|
|
#include <skills.h> |
28 |
|
|
#include <living.h> |
29 |
root |
1.24 |
#include <sproto.h> |
30 |
elmex |
1.1 |
|
31 |
|
|
/* this is a measure of how effective store specialisation is. A general store |
32 |
|
|
* will offer this proportion of the 'maximum' price, a specialised store will |
33 |
|
|
* offer a range of prices around it such that the maximum price is always one |
34 |
|
|
* therefore making this number higher, makes specialisation less effective. |
35 |
|
|
* setting this value above 1 or to a negative value would have interesting, |
36 |
|
|
* (though not useful) effects. |
37 |
|
|
*/ |
38 |
root |
1.41 |
#define SPECIALISATION_EFFECT .5 |
39 |
elmex |
1.1 |
|
40 |
root |
1.42 |
// price a shopkeeper will give someone that is not of their race |
41 |
|
|
#define DISLIKE_RATIO 0.8 |
42 |
elmex |
1.1 |
|
43 |
root |
1.12 |
static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); |
44 |
|
|
static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); |
45 |
root |
1.18 |
static double shop_specialisation_ratio (const object *item, const maptile *map); |
46 |
|
|
static double shop_greed (const maptile *map); |
47 |
elmex |
1.1 |
|
48 |
|
|
/* Added F_TRUE flag to define.h to mean that the price should not |
49 |
|
|
* be adjusted by players charisma. With F_TRUE, it returns the amount |
50 |
|
|
* that the item is worth, if it was sold, but unadjusted by charisma. |
51 |
|
|
* This is needed for alchemy, to to determine what value of gold nuggets |
52 |
|
|
* should be given (the gold nuggets, when sold, will have the adjustment |
53 |
|
|
* by charisma done at that time). NULL could have been passed as the |
54 |
|
|
* who parameter, but then the adjustment for expensive items (>10000) |
55 |
|
|
* would not be done. |
56 |
|
|
* |
57 |
|
|
* Added F_APPROX flag, which means that the price returned should be wrong by |
58 |
|
|
* an amount related to the player's bargaining skill. |
59 |
|
|
* |
60 |
|
|
* Added F_SHOP flag to mean that the specialisation of the shop on the player's |
61 |
|
|
* current map should be taken into account when determining the price. Shops that |
62 |
|
|
* specialise in what is being traded will give better prices than those that do not. |
63 |
|
|
* |
64 |
|
|
* CF 0.91.4 - This function got changed around a bit. Now the |
65 |
|
|
* number of object is multiplied by the value early on. This fixes problems |
66 |
|
|
* with items worth very little. What happened before is that various |
67 |
|
|
* divisions took place, the value got rounded to 0 (Being an int), and |
68 |
|
|
* thus remained 0. |
69 |
|
|
* |
70 |
|
|
* Mark Wedel (mwedel@pyramid.com) |
71 |
|
|
*/ |
72 |
|
|
|
73 |
root |
1.12 |
static sint64 approx_range; |
74 |
elmex |
1.1 |
|
75 |
root |
1.12 |
sint64 |
76 |
root |
1.10 |
query_cost (const object *tmp, object *who, int flag) |
77 |
|
|
{ |
78 |
|
|
double val; |
79 |
|
|
int no_bargain; |
80 |
|
|
int identified; |
81 |
|
|
int not_cursed; |
82 |
|
|
int approximate; |
83 |
|
|
int shop; |
84 |
|
|
double diff; |
85 |
|
|
|
86 |
|
|
approx_range = 0; |
87 |
|
|
|
88 |
|
|
no_bargain = flag & F_NO_BARGAIN; |
89 |
|
|
identified = flag & F_IDENTIFIED; |
90 |
|
|
not_cursed = flag & F_NOT_CURSED; |
91 |
|
|
approximate = flag & F_APPROX; |
92 |
|
|
shop = flag & F_SHOP; |
93 |
|
|
flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP); |
94 |
|
|
|
95 |
root |
1.80 |
int number = tmp->number_of (); |
96 |
|
|
|
97 |
root |
1.10 |
if (tmp->type == MONEY) |
98 |
root |
1.80 |
return number * tmp->value; |
99 |
root |
1.14 |
|
100 |
root |
1.10 |
if (tmp->type == GEM) |
101 |
|
|
{ |
102 |
|
|
if (flag == F_TRUE) |
103 |
root |
1.80 |
return number * tmp->value; |
104 |
root |
1.14 |
|
105 |
root |
1.10 |
if (flag == F_BUY) |
106 |
root |
1.80 |
return 1.03 * number * tmp->value; |
107 |
root |
1.14 |
|
108 |
root |
1.10 |
if (flag == F_SELL) |
109 |
root |
1.80 |
return 0.97 * number * tmp->value; |
110 |
root |
1.14 |
|
111 |
|
|
LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ()); |
112 |
root |
1.10 |
return 0; |
113 |
|
|
} |
114 |
root |
1.14 |
|
115 |
root |
1.79 |
if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified) |
116 |
root |
1.10 |
{ |
117 |
root |
1.78 |
if (!not_cursed && (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])) |
118 |
root |
1.6 |
return 0; |
119 |
root |
1.10 |
else |
120 |
root |
1.80 |
val = number * tmp->value; |
121 |
elmex |
1.1 |
} |
122 |
root |
1.10 |
/* This area deals with objects that are not identified, but can be */ |
123 |
|
|
else |
124 |
|
|
{ |
125 |
root |
1.63 |
if (flag == F_BUY) |
126 |
root |
1.10 |
{ |
127 |
root |
1.63 |
LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object: %s\n", tmp->debug_desc ()); |
128 |
|
|
val = tmp->arch->value * 50 * number; |
129 |
root |
1.10 |
} |
130 |
|
|
else |
131 |
root |
1.63 |
{ /* Trying to sell something, or get true value */ |
132 |
|
|
if (tmp->type == POTION) |
133 |
|
|
val = number * 40; /* Don't want to give anything away */ |
134 |
|
|
else |
135 |
root |
1.10 |
{ |
136 |
root |
1.63 |
/* Get 2/3'rd value for applied objects, 1/3'rd for totally |
137 |
|
|
* unknown objects |
138 |
|
|
*/ |
139 |
root |
1.78 |
if (tmp->flag [FLAG_BEEN_APPLIED]) |
140 |
root |
1.63 |
val = number * tmp->arch->value * 2 / 3; |
141 |
|
|
else |
142 |
root |
1.80 |
val = number * tmp->arch->value / 3; |
143 |
root |
1.6 |
} |
144 |
|
|
} |
145 |
elmex |
1.1 |
} |
146 |
|
|
|
147 |
root |
1.10 |
/* If the item has been applied or identifed or does not need to be |
148 |
|
|
* identified, AND the object is magical and the archetype is non |
149 |
|
|
* magical, then change values accordingly. The tmp->arch==NULL is |
150 |
|
|
* really just a check to prevent core dumps for when it checks |
151 |
root |
1.39 |
* tmp->arch->magic for any magic. The check for archetype |
152 |
root |
1.10 |
* magic is to not give extra money for archetypes that are by |
153 |
|
|
* default magical. This is because the archetype value should have |
154 |
|
|
* already figured in that value. |
155 |
|
|
*/ |
156 |
root |
1.79 |
if ((tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified || tmp->flag [FLAG_BEEN_APPLIED]) |
157 |
|
|
&& tmp->magic && (!tmp->arch || !tmp->arch->magic)) |
158 |
root |
1.10 |
{ |
159 |
|
|
if (tmp->magic > 0) |
160 |
root |
1.80 |
val *= 3 * tmp->magic * tmp->magic * tmp->magic; |
161 |
root |
1.10 |
else |
162 |
|
|
/* Note that tmp->magic is negative, so that this |
163 |
|
|
* will actually be something like val /=2, /=3, etc. |
164 |
root |
1.6 |
*/ |
165 |
root |
1.80 |
val /= 1 - tmp->magic; |
166 |
root |
1.10 |
} |
167 |
|
|
|
168 |
|
|
if (tmp->type == WAND) |
169 |
|
|
{ |
170 |
|
|
/* Value of the wand is multiplied by the number of |
171 |
|
|
* charges. the treasure code already sets up the value |
172 |
|
|
* 50 charges is used as the baseline. |
173 |
|
|
*/ |
174 |
root |
1.79 |
if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified) |
175 |
root |
1.80 |
val *= tmp->stats.food / 50; |
176 |
root |
1.10 |
else /* if not identified, presume one charge */ |
177 |
|
|
val /= 50; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/* Limit amount of money you can get for really great items. */ |
181 |
|
|
if (flag == F_SELL) |
182 |
root |
1.80 |
val = value_limit (val, number, who, shop); |
183 |
root |
1.10 |
|
184 |
|
|
// use a nonlinear price adjustment. as my predecessor said, don't change |
185 |
|
|
// the archetypes, its work required for balancing, and we don't care. |
186 |
|
|
//val = pow (val, 1.05); |
187 |
|
|
|
188 |
|
|
/* This modification is for bargaining skill. |
189 |
|
|
* Now only players with max level in bargaining |
190 |
|
|
* AND Cha = 30 will get optimal price. |
191 |
|
|
* Thus charisma will never get useless. |
192 |
|
|
* -b.e. edler@heydernet.de |
193 |
|
|
*/ |
194 |
|
|
|
195 |
root |
1.51 |
if (who && who->type == PLAYER) |
196 |
root |
1.10 |
{ |
197 |
|
|
int lev_bargain = 0; |
198 |
|
|
int lev_identify = 0; |
199 |
|
|
|
200 |
|
|
if (find_skill_by_number (who, SK_BARGAINING)) |
201 |
root |
1.14 |
lev_bargain = find_skill_by_number (who, SK_BARGAINING)->level; |
202 |
|
|
|
203 |
root |
1.51 |
if (const typedata *tmptype = get_typedata (tmp->type)) |
204 |
root |
1.10 |
{ |
205 |
root |
1.51 |
if (int idskill1 = tmptype->identifyskill) |
206 |
root |
1.10 |
{ |
207 |
root |
1.51 |
int idskill2 = tmptype->identifyskill2; |
208 |
root |
1.14 |
|
209 |
root |
1.10 |
if (find_skill_by_number (who, idskill1)) |
210 |
root |
1.14 |
lev_identify = find_skill_by_number (who, idskill1)->level; |
211 |
|
|
|
212 |
root |
1.10 |
if (idskill2 && find_skill_by_number (who, idskill2)) |
213 |
root |
1.14 |
lev_identify += find_skill_by_number (who, idskill2)->level; |
214 |
root |
1.6 |
} |
215 |
|
|
} |
216 |
root |
1.10 |
|
217 |
|
|
/* ratio determines how much of the price modification |
218 |
|
|
* will come from the basic stat charisma |
219 |
|
|
* the rest will come from the level in bargaining skill |
220 |
|
|
*/ |
221 |
|
|
const double cha_ratio = 0.40; |
222 |
elmex |
1.1 |
|
223 |
root |
1.80 |
diff = no_bargain ? 1.0 : 1. - pow (lev_bargain / MAXLEVEL_TREASURE, 0.25); |
224 |
root |
1.10 |
diff = (1. - cha_ratio) * diff + cha_ratio * (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); |
225 |
|
|
diff = .02 + (.80 - .02) * diff; |
226 |
|
|
|
227 |
|
|
if (flag == F_BUY) |
228 |
root |
1.34 |
val += val * diff; |
229 |
root |
1.10 |
else if (flag == F_SELL) |
230 |
root |
1.34 |
val -= val * diff; |
231 |
root |
1.10 |
|
232 |
|
|
// now find a price range. the less good we can judge, the larger the range is |
233 |
|
|
// then the range is adjusted randomly around the correct value |
234 |
|
|
if (approximate) |
235 |
root |
1.80 |
approx_range = val / sqrt (lev_identify * 3 + 1); |
236 |
root |
1.10 |
} |
237 |
|
|
|
238 |
|
|
/* I don't think this should really happen - if it does, it indicates and |
239 |
root |
1.34 |
* overflow of diff above. That should only happen if |
240 |
root |
1.10 |
* we are selling objects - in that case, the person just |
241 |
|
|
* gets no money. |
242 |
|
|
*/ |
243 |
|
|
if ((sint64) val < 0) |
244 |
|
|
val = 0; |
245 |
|
|
|
246 |
root |
1.80 |
/* Unidentified stuff won't sell for more than 10gp */ |
247 |
root |
1.79 |
if (flag == F_SELL && !tmp->flag [FLAG_IDENTIFIED] && tmp->need_identify () && !identified) |
248 |
root |
1.80 |
min_it (val, 1000); |
249 |
elmex |
1.1 |
|
250 |
root |
1.10 |
/* if we are in a shop, check how the type of shop should affect the price */ |
251 |
|
|
if (shop && who) |
252 |
|
|
{ |
253 |
|
|
if (flag == F_SELL) |
254 |
root |
1.80 |
val *= shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who) / shop_greed (who->map); |
255 |
root |
1.10 |
else if (flag == F_BUY) |
256 |
|
|
{ |
257 |
|
|
/* |
258 |
|
|
* when buying, if the item was sold by another player, it is ok to |
259 |
|
|
* let the item be sold cheaper, according to the specialisation of |
260 |
|
|
* the shop. If a player sold an item here, then his sale price was |
261 |
|
|
* multiplied by the specialisation ratio, to do the same to the buy |
262 |
|
|
* price will not generate extra money. However, the |
263 |
|
|
* same is not true of generated items, these have to /divide/ by the |
264 |
|
|
* specialisation, so that the price is never less than what they could |
265 |
|
|
* be sold for (otherwise players could camp map resets to make money). |
266 |
|
|
* In game terms, a non-specialist shop, might not recognise the true |
267 |
|
|
* value of the items they sell (much like how people sometimes find |
268 |
|
|
* antiques in a junk shop in real life). |
269 |
|
|
*/ |
270 |
root |
1.78 |
if (tmp->flag [FLAG_PLAYER_SOLD]) |
271 |
root |
1.80 |
val *= shop_specialisation_ratio (tmp, who->map); |
272 |
root |
1.10 |
else |
273 |
root |
1.80 |
val /= shop_specialisation_ratio (tmp, who->map); |
274 |
|
|
|
275 |
|
|
val *= shop_greed (who->map) / shopkeeper_approval (who->map, who); |
276 |
root |
1.10 |
} |
277 |
root |
1.34 |
|
278 |
root |
1.10 |
/* we will also have an extra 0-5% variation between shops of the same type |
279 |
|
|
* for valuable items (below a value of 50 this effect wouldn't be very |
280 |
|
|
* pointful, and could give fun with rounding. |
281 |
|
|
*/ |
282 |
root |
1.33 |
//TODO: why use cosf at all, just % and scale linearly, gives more even distribution |
283 |
root |
1.32 |
if (val > 50) |
284 |
root |
1.80 |
val *= 1 + .05f * cosf ((tmp->uuid.seq & 0xffff) * M_PI * 2. / 0x10000); |
285 |
elmex |
1.1 |
} |
286 |
root |
1.32 |
|
287 |
root |
1.80 |
return val; |
288 |
elmex |
1.1 |
} |
289 |
|
|
|
290 |
|
|
/* Find the coin type that is worth more the 'c'. Starts at the |
291 |
|
|
* cointype placement. |
292 |
|
|
*/ |
293 |
|
|
|
294 |
root |
1.10 |
static archetype * |
295 |
root |
1.12 |
find_next_coin (sint64 c, int *cointype) |
296 |
root |
1.10 |
{ |
297 |
elmex |
1.1 |
archetype *coin; |
298 |
|
|
|
299 |
pippijn |
1.9 |
do |
300 |
root |
1.10 |
{ |
301 |
root |
1.80 |
if (!coins [*cointype]) |
302 |
|
|
return 0; |
303 |
|
|
|
304 |
|
|
coin = archetype::find (coins [*cointype]); |
305 |
|
|
|
306 |
|
|
if (!coin) |
307 |
|
|
return 0; |
308 |
|
|
|
309 |
root |
1.10 |
*cointype += 1; |
310 |
|
|
} |
311 |
root |
1.39 |
while (coin->value > c); |
312 |
elmex |
1.1 |
|
313 |
|
|
return coin; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
/* This returns a string of how much something is worth based on |
317 |
|
|
* an integer being passed. |
318 |
|
|
* cost is the cost we need to represent. |
319 |
|
|
* While cost is 64 bit, the number of any coin is still really |
320 |
|
|
* limited to 32 bit (size of nrof field). If it turns out players |
321 |
|
|
* have so much money that they have more than 2 billion platinum |
322 |
|
|
* coins, there are certainly issues - the easiest fix at that |
323 |
|
|
* time is to add a higher denomination (mithril piece with |
324 |
|
|
* 10,000 silver or something) |
325 |
|
|
*/ |
326 |
root |
1.10 |
const char * |
327 |
root |
1.12 |
cost_string_from_value (sint64 cost, int approx) |
328 |
elmex |
1.1 |
{ |
329 |
root |
1.10 |
static char buf[MAX_BUF]; |
330 |
|
|
archetype *coin, *next_coin; |
331 |
|
|
int num, cointype = 0; |
332 |
|
|
|
333 |
|
|
coin = find_next_coin (cost, &cointype); |
334 |
|
|
if (coin == NULL) |
335 |
|
|
return "nothing"; |
336 |
|
|
|
337 |
root |
1.39 |
num = cost / coin->value; |
338 |
root |
1.10 |
/* so long as nrof is 32 bit, this is true. |
339 |
|
|
* If it takes more coins than a person can possibly carry, this |
340 |
|
|
* is basically true. |
341 |
|
|
*/ |
342 |
root |
1.39 |
if ((cost / coin->value) > UINT32_MAX) |
343 |
root |
1.10 |
{ |
344 |
|
|
strcpy (buf, "an unimaginable sum of money"); |
345 |
|
|
return buf; |
346 |
|
|
} |
347 |
|
|
|
348 |
root |
1.39 |
cost -= num * (sint64)coin->value; |
349 |
elmex |
1.1 |
|
350 |
root |
1.39 |
sprintf (buf, "%d %s", num, num > 1 ? &coin->object::name_pl : &coin->object::name); |
351 |
root |
1.10 |
|
352 |
|
|
next_coin = find_next_coin (cost, &cointype); |
353 |
|
|
if (next_coin == NULL || approx) |
354 |
elmex |
1.1 |
return buf; |
355 |
root |
1.10 |
|
356 |
|
|
coin = next_coin; |
357 |
root |
1.39 |
num = cost / coin->value; |
358 |
|
|
cost -= num * (sint64)coin->value; |
359 |
root |
1.10 |
|
360 |
root |
1.39 |
sprintf (buf + strlen (buf), " and %d %s", num, num > 1 ? &coin->object::name_pl : &coin->object::name); |
361 |
root |
1.10 |
|
362 |
|
|
return buf; |
363 |
elmex |
1.1 |
} |
364 |
|
|
|
365 |
root |
1.10 |
const char * |
366 |
|
|
query_cost_string (const object *tmp, object *who, int flag) |
367 |
|
|
{ |
368 |
root |
1.12 |
sint64 real_value = query_cost (tmp, who, flag); |
369 |
root |
1.10 |
int idskill1 = 0; |
370 |
|
|
int idskill2 = 0; |
371 |
|
|
const typedata *tmptype; |
372 |
|
|
|
373 |
|
|
tmptype = get_typedata (tmp->type); |
374 |
|
|
if (tmptype) |
375 |
|
|
{ |
376 |
|
|
idskill1 = tmptype->identifyskill; |
377 |
|
|
idskill2 = tmptype->identifyskill2; |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
/* we show an approximate price if |
381 |
|
|
* 1) we are approximating |
382 |
|
|
* 2) there either is no id skill(s) for the item, or we don't have them |
383 |
|
|
* 3) we don't have bargaining skill either |
384 |
|
|
*/ |
385 |
|
|
if (flag & F_APPROX) |
386 |
|
|
{ |
387 |
|
|
if (!idskill1 || !find_skill_by_number (who, idskill1)) |
388 |
|
|
{ |
389 |
|
|
if (!idskill2 || !find_skill_by_number (who, idskill2)) |
390 |
|
|
{ |
391 |
|
|
if (!find_skill_by_number (who, SK_BARGAINING)) |
392 |
|
|
{ |
393 |
|
|
static char buf[MAX_BUF]; |
394 |
|
|
int num, cointype = 0; |
395 |
|
|
archetype *coin = find_next_coin (real_value, &cointype); |
396 |
|
|
|
397 |
|
|
if (coin == NULL) |
398 |
|
|
return "nothing"; |
399 |
|
|
|
400 |
root |
1.39 |
num = real_value / coin->value; |
401 |
root |
1.51 |
|
402 |
root |
1.10 |
if (num == 1) |
403 |
root |
1.39 |
sprintf (buf, "about one %s", &coin->object::name); |
404 |
root |
1.10 |
else if (num < 5) |
405 |
root |
1.39 |
sprintf (buf, "a few %s", &coin->object::name_pl); |
406 |
root |
1.10 |
else if (num < 10) |
407 |
root |
1.39 |
sprintf (buf, "several %s", &coin->object::name_pl); |
408 |
root |
1.10 |
else if (num < 25) |
409 |
root |
1.39 |
sprintf (buf, "a moderate amount of %s", &coin->object::name_pl); |
410 |
root |
1.10 |
else if (num < 100) |
411 |
root |
1.39 |
sprintf (buf, "lots of %s", &coin->object::name_pl); |
412 |
root |
1.10 |
else if (num < 1000) |
413 |
root |
1.39 |
sprintf (buf, "a great many %s", &coin->object::name_pl); |
414 |
root |
1.10 |
else |
415 |
root |
1.39 |
sprintf (buf, "a vast quantity of %s", &coin->object::name_pl); |
416 |
root |
1.51 |
|
417 |
root |
1.10 |
return buf; |
418 |
root |
1.6 |
} |
419 |
|
|
} |
420 |
|
|
} |
421 |
elmex |
1.1 |
|
422 |
root |
1.10 |
int hash = ((unsigned int) tmp->count * 174364621) & 1023; |
423 |
elmex |
1.1 |
|
424 |
root |
1.10 |
if (approx_range) |
425 |
|
|
{ |
426 |
root |
1.12 |
sint64 lo = (sint64) real_value - (approx_range * hash >> 10); |
427 |
root |
1.10 |
static char buf[MAX_BUF]; |
428 |
elmex |
1.1 |
|
429 |
root |
1.10 |
sprintf (buf, "between %s", cost_string_from_value (lo, 1)); |
430 |
|
|
sprintf (buf + strlen (buf), " and %s", cost_string_from_value (lo + approx_range, 1)); |
431 |
elmex |
1.1 |
|
432 |
root |
1.10 |
return buf; |
433 |
|
|
} |
434 |
elmex |
1.1 |
} |
435 |
|
|
|
436 |
root |
1.10 |
return cost_string_from_value (real_value, 0); |
437 |
elmex |
1.1 |
} |
438 |
|
|
|
439 |
|
|
/* This function finds out how much money the player is carrying, |
440 |
|
|
* including what is in containers. |
441 |
|
|
*/ |
442 |
root |
1.12 |
sint64 |
443 |
root |
1.10 |
query_money (const object *op) |
444 |
|
|
{ |
445 |
|
|
object *tmp; |
446 |
root |
1.12 |
sint64 total = 0; |
447 |
elmex |
1.1 |
|
448 |
root |
1.10 |
if (op->type != PLAYER && op->type != CONTAINER) |
449 |
|
|
{ |
450 |
|
|
LOG (llevError, "Query money called with non player/container\n"); |
451 |
|
|
return 0; |
452 |
elmex |
1.1 |
} |
453 |
root |
1.12 |
|
454 |
root |
1.10 |
for (tmp = op->inv; tmp; tmp = tmp->below) |
455 |
root |
1.12 |
if (tmp->type == MONEY) |
456 |
|
|
total += tmp->nrof * (sint64)tmp->value; |
457 |
root |
1.78 |
else if (tmp->type == CONTAINER && tmp->flag [FLAG_APPLIED] && (!tmp->race || tmp->race.contains ("gold"))) |
458 |
root |
1.12 |
total += query_money (tmp); |
459 |
|
|
|
460 |
root |
1.10 |
return total; |
461 |
elmex |
1.1 |
} |
462 |
root |
1.10 |
|
463 |
elmex |
1.1 |
/* TCHIZE: This function takes the amount of money from the |
464 |
|
|
* the player inventory and from it's various pouches using the |
465 |
|
|
* pay_from_container function. |
466 |
|
|
* returns 0 if not possible. 1 if success |
467 |
|
|
*/ |
468 |
root |
1.10 |
int |
469 |
root |
1.12 |
pay_for_amount (sint64 to_pay, object *pl) |
470 |
root |
1.10 |
{ |
471 |
|
|
object *pouch; |
472 |
elmex |
1.1 |
|
473 |
root |
1.10 |
if (to_pay == 0) |
474 |
|
|
return 1; |
475 |
root |
1.12 |
|
476 |
root |
1.10 |
if (to_pay > query_money (pl)) |
477 |
|
|
return 0; |
478 |
elmex |
1.1 |
|
479 |
root |
1.12 |
pay_from_container (pl, pl, to_pay); |
480 |
|
|
|
481 |
|
|
for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
482 |
root |
1.78 |
if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains ("gold"))) |
483 |
root |
1.12 |
pay_from_container (pl, pouch, to_pay); |
484 |
elmex |
1.1 |
|
485 |
root |
1.26 |
pl->update_stats (); |
486 |
root |
1.10 |
return 1; |
487 |
elmex |
1.1 |
} |
488 |
|
|
|
489 |
|
|
/* DAMN: This is now a wrapper for pay_from_container, which is |
490 |
|
|
* called for the player, then for each active container that can hold |
491 |
|
|
* money until op is paid for. Change will be left wherever the last |
492 |
|
|
* of the price was paid from. |
493 |
|
|
*/ |
494 |
root |
1.10 |
int |
495 |
|
|
pay_for_item (object *op, object *pl) |
496 |
|
|
{ |
497 |
root |
1.12 |
sint64 to_pay = query_cost (op, pl, F_BUY | F_SHOP); |
498 |
root |
1.10 |
object *pouch; |
499 |
root |
1.12 |
sint64 saved_money; |
500 |
root |
1.10 |
|
501 |
|
|
if (to_pay == 0) |
502 |
elmex |
1.1 |
return 1; |
503 |
root |
1.12 |
|
504 |
root |
1.10 |
if (to_pay > query_money (pl)) |
505 |
|
|
return 0; |
506 |
|
|
|
507 |
|
|
/* We compare the paid price with the one for a player |
508 |
|
|
* without bargaining skill. |
509 |
|
|
* This determins the amount of exp (if any) gained for bargaining. |
510 |
|
|
*/ |
511 |
|
|
saved_money = query_cost (op, pl, F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay; |
512 |
|
|
|
513 |
|
|
if (saved_money > 0) |
514 |
|
|
change_exp (pl, saved_money, "bargaining", SK_EXP_NONE); |
515 |
|
|
|
516 |
root |
1.12 |
pay_from_container (pl, pl, to_pay); |
517 |
|
|
|
518 |
|
|
for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) |
519 |
root |
1.78 |
if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains ("gold"))) |
520 |
root |
1.12 |
pay_from_container (pl, pouch, to_pay); |
521 |
root |
1.10 |
|
522 |
root |
1.36 |
pl->update_stats (); |
523 |
root |
1.12 |
|
524 |
root |
1.10 |
return 1; |
525 |
elmex |
1.1 |
} |
526 |
|
|
|
527 |
|
|
/* This pays for the item, and takes the proper amount of money off |
528 |
|
|
* the player. |
529 |
|
|
* CF 0.91.4 - this function is mostly redone in order to fix a bug |
530 |
|
|
* with weight not be subtracted properly. We now remove and |
531 |
|
|
* insert the coin objects - this should update the weight |
532 |
|
|
* appropriately |
533 |
|
|
* |
534 |
|
|
* DAMN: This function is used for the player, then for any active |
535 |
|
|
* containers that can hold money. |
536 |
|
|
* |
537 |
|
|
* pouch is the container (pouch or player) to remove the coins from. |
538 |
|
|
* to_pay is the required amount. |
539 |
|
|
* returns the amount still missing after using "pouch". |
540 |
|
|
*/ |
541 |
root |
1.12 |
static void |
542 |
|
|
pay_from_container (object *pl, object *pouch, sint64 &to_pay) |
543 |
root |
1.10 |
{ |
544 |
|
|
int count, i; |
545 |
root |
1.12 |
object *tmp, *next; |
546 |
root |
1.10 |
archetype *at; |
547 |
|
|
|
548 |
|
|
if (pouch->type != PLAYER && pouch->type != CONTAINER) |
549 |
root |
1.12 |
return; |
550 |
root |
1.10 |
|
551 |
root |
1.12 |
object *coin_objs[NUM_COINS] = { 0 }; |
552 |
root |
1.10 |
|
553 |
|
|
/* This hunk should remove all the money objects from the player/container */ |
554 |
|
|
for (tmp = pouch->inv; tmp; tmp = next) |
555 |
|
|
{ |
556 |
|
|
next = tmp->below; |
557 |
|
|
|
558 |
|
|
if (tmp->type == MONEY) |
559 |
|
|
{ |
560 |
|
|
for (i = 0; i < NUM_COINS; i++) |
561 |
|
|
{ |
562 |
root |
1.39 |
if (tmp->value == tmp->arch->value && !strcmp (coins[NUM_COINS - 1 - i], tmp->arch->archname)) |
563 |
root |
1.10 |
{ |
564 |
root |
1.12 |
// This should not happen, but if it does, just merge the two. |
565 |
|
|
if (coin_objs [i]) |
566 |
root |
1.10 |
{ |
567 |
|
|
LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); |
568 |
|
|
coin_objs[i]->nrof += tmp->nrof; |
569 |
root |
1.57 |
tmp->destroy (); |
570 |
root |
1.6 |
} |
571 |
root |
1.10 |
else |
572 |
|
|
{ |
573 |
root |
1.21 |
tmp->remove (); |
574 |
root |
1.10 |
coin_objs[i] = tmp; |
575 |
root |
1.6 |
} |
576 |
root |
1.12 |
|
577 |
root |
1.10 |
break; |
578 |
root |
1.6 |
} |
579 |
|
|
} |
580 |
root |
1.12 |
|
581 |
root |
1.10 |
if (i == NUM_COINS) |
582 |
root |
1.38 |
LOG (llevError, "in pay_for_item: Did not find string match for %s\n", &tmp->arch->archname); |
583 |
root |
1.6 |
} |
584 |
elmex |
1.1 |
} |
585 |
|
|
|
586 |
root |
1.10 |
/* Fill in any gaps in the coin_objs array - needed to make change. */ |
587 |
|
|
/* Note that the coin_objs array goes from least value to greatest value */ |
588 |
|
|
for (i = 0; i < NUM_COINS; i++) |
589 |
root |
1.12 |
if (!coin_objs[i]) |
590 |
root |
1.10 |
{ |
591 |
root |
1.15 |
at = archetype::find (coins[NUM_COINS - 1 - i]); |
592 |
root |
1.12 |
|
593 |
root |
1.10 |
if (at == NULL) |
594 |
|
|
LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]); |
595 |
root |
1.12 |
|
596 |
root |
1.74 |
coin_objs[i] = at->instance (); |
597 |
root |
1.10 |
coin_objs[i]->nrof = 0; |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
for (i = 0; i < NUM_COINS; i++) |
601 |
|
|
{ |
602 |
root |
1.12 |
object &coin = *coin_objs[i]; |
603 |
elmex |
1.68 |
sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, (sint64) coin.nrof); |
604 |
root |
1.12 |
to_pay -= num_coins * coin.value; |
605 |
root |
1.10 |
|
606 |
root |
1.12 |
coin.nrof -= num_coins; |
607 |
root |
1.10 |
/* Now start making change. Start at the coin value |
608 |
|
|
* below the one we just did, and work down to |
609 |
|
|
* the lowest value. |
610 |
|
|
*/ |
611 |
|
|
count = i - 1; |
612 |
root |
1.12 |
|
613 |
|
|
while (to_pay < 0 && count >= 0) |
614 |
root |
1.10 |
{ |
615 |
root |
1.12 |
num_coins = (-to_pay) / coin_objs[count]->value; |
616 |
root |
1.10 |
coin_objs[count]->nrof += num_coins; |
617 |
root |
1.12 |
to_pay += num_coins * coin_objs[count]->value; |
618 |
root |
1.10 |
count--; |
619 |
|
|
} |
620 |
|
|
} |
621 |
root |
1.12 |
|
622 |
root |
1.10 |
for (i = 0; i < NUM_COINS; i++) |
623 |
root |
1.56 |
if (coin_objs[i]->nrof) |
624 |
|
|
insert_ob_in_ob (coin_objs [i], pouch); |
625 |
|
|
else |
626 |
root |
1.57 |
coin_objs[i]->destroy (); |
627 |
elmex |
1.1 |
} |
628 |
|
|
|
629 |
|
|
/* Checks all unpaid items in op's inventory, adds up all the money they |
630 |
|
|
* have, and checks that they can actually afford what they want to buy. |
631 |
|
|
* Returns 1 if they can, and 0 if they can't. also prints an appropriate message |
632 |
|
|
* to the player |
633 |
|
|
*/ |
634 |
root |
1.10 |
int |
635 |
|
|
can_pay (object *pl) |
636 |
|
|
{ |
637 |
root |
1.12 |
int unpaid_count = 0; |
638 |
|
|
sint64 unpaid_price = 0; |
639 |
|
|
sint64 player_wealth = query_money (pl); |
640 |
root |
1.10 |
|
641 |
|
|
if (!pl || pl->type != PLAYER) |
642 |
|
|
{ |
643 |
|
|
LOG (llevError, "can_pay(): called against something that isn't a player\n"); |
644 |
|
|
return 0; |
645 |
elmex |
1.1 |
} |
646 |
root |
1.11 |
|
647 |
root |
1.13 |
for (object::depth_iterator item = pl->begin (); item != pl->end (); ++item) |
648 |
root |
1.78 |
if (item->flag [FLAG_UNPAID]) |
649 |
root |
1.12 |
{ |
650 |
|
|
unpaid_count++; |
651 |
|
|
unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); |
652 |
|
|
} |
653 |
root |
1.11 |
|
654 |
root |
1.10 |
if (unpaid_price > player_wealth) |
655 |
|
|
{ |
656 |
root |
1.67 |
dynbuf_text &buf = msg_dynbuf; buf.clear (); |
657 |
root |
1.54 |
|
658 |
|
|
buf << "You have " << unpaid_count |
659 |
|
|
<< " unpaid item(s) that would cost you " << cost_string_from_value (unpaid_price, 0) |
660 |
|
|
<< ". You need another " << cost_string_from_value (unpaid_price - player_wealth, 0) |
661 |
root |
1.58 |
<< " to be able to afford that. " |
662 |
root |
1.59 |
"H<You cannot leave a shop without paying - drop unpaid items first to be able to leave.>"; |
663 |
root |
1.54 |
|
664 |
|
|
pl->failmsg (buf); |
665 |
root |
1.12 |
|
666 |
root |
1.10 |
return 0; |
667 |
elmex |
1.1 |
} |
668 |
root |
1.10 |
else |
669 |
|
|
return 1; |
670 |
elmex |
1.1 |
} |
671 |
|
|
|
672 |
|
|
/* Better get_payment, descends containers looking for |
673 |
|
|
* unpaid items, and pays for them. |
674 |
|
|
* returns 0 if the player still has unpaid items. |
675 |
|
|
* returns 1 if the player has paid for everything. |
676 |
|
|
* pl is the player buying the stuff. |
677 |
|
|
*/ |
678 |
root |
1.10 |
int |
679 |
root |
1.12 |
get_payment (object *pl) |
680 |
root |
1.10 |
{ |
681 |
root |
1.12 |
for (;;) |
682 |
|
|
{ |
683 |
|
|
next_item: |
684 |
root |
1.10 |
|
685 |
root |
1.13 |
for (object::depth_iterator op = pl->begin (); op != pl->end (); ++op) |
686 |
root |
1.12 |
{ |
687 |
root |
1.78 |
if (op->flag [FLAG_UNPAID]) |
688 |
root |
1.12 |
{ |
689 |
|
|
char buf[MAX_BUF]; |
690 |
|
|
snprintf (buf, MAX_BUF, "%s", query_cost_string (op, pl, F_BUY | F_SHOP)); |
691 |
root |
1.10 |
|
692 |
root |
1.12 |
if (!pay_for_item (op, pl)) |
693 |
|
|
{ |
694 |
|
|
sint64 i = query_cost (op, pl, F_BUY | F_SHOP) - query_money (pl); |
695 |
elmex |
1.1 |
|
696 |
root |
1.78 |
op->clr_flag (FLAG_UNPAID); |
697 |
root |
1.12 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You lack %s to buy %s.", cost_string_from_value (i, 0), query_name (op)); |
698 |
root |
1.78 |
op->set_flag (FLAG_UNPAID); |
699 |
root |
1.12 |
return 0; |
700 |
|
|
} |
701 |
|
|
else |
702 |
|
|
{ |
703 |
root |
1.78 |
op->clr_flag (FLAG_UNPAID); |
704 |
|
|
op->clr_flag (FLAG_PLAYER_SOLD); |
705 |
root |
1.12 |
new_draw_info_format (NDI_UNIQUE, 0, op, "You paid %s for %s.", buf, query_name (op)); |
706 |
root |
1.10 |
|
707 |
root |
1.49 |
if (!merge_ob (op, op->env->inv)) |
708 |
|
|
esrv_update_item (UPD_FLAGS, pl, op); |
709 |
elmex |
1.1 |
|
710 |
root |
1.12 |
goto next_item; |
711 |
root |
1.6 |
} |
712 |
|
|
} |
713 |
|
|
} |
714 |
root |
1.12 |
|
715 |
|
|
return 1; |
716 |
elmex |
1.1 |
} |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
/* written by elmex: |
720 |
|
|
* moved this code from sell_item () here to have a function |
721 |
|
|
* that pays the player an amount. Mainly put the code here to |
722 |
|
|
* be able to call it from a plugin. |
723 |
|
|
* |
724 |
|
|
* If the player can't carry all the money that is paid, it gets inserted |
725 |
|
|
* in his inventory anyway. This is the best alternative to not pay any money |
726 |
|
|
* or put it on the ground under the player. This way the player can still |
727 |
|
|
* go somewhere and unload the money at a safe place. |
728 |
|
|
* |
729 |
|
|
*/ |
730 |
root |
1.10 |
void |
731 |
root |
1.12 |
pay_player (object *pl, sint64 amount) |
732 |
root |
1.10 |
{ |
733 |
elmex |
1.1 |
int count = 0; |
734 |
|
|
archetype *at = 0; |
735 |
|
|
object *pouch = 0, *tmp = 0; |
736 |
|
|
|
737 |
root |
1.43 |
for (count = 0; coins[count]; count++) |
738 |
elmex |
1.1 |
{ |
739 |
root |
1.15 |
at = archetype::find (coins[count]); |
740 |
elmex |
1.1 |
|
741 |
|
|
if (at == NULL) |
742 |
root |
1.10 |
LOG (llevError, "Could not find %s archetype\n", coins[count]); |
743 |
root |
1.39 |
else if ((amount / at->value) > 0) |
744 |
elmex |
1.1 |
{ |
745 |
root |
1.10 |
for (pouch = pl->inv; pouch; pouch = pouch->below) |
746 |
elmex |
1.1 |
{ |
747 |
root |
1.78 |
if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && pouch->race.contains ("gold")) |
748 |
elmex |
1.1 |
{ |
749 |
root |
1.39 |
int w = at->weight * (100 - pouch->stats.Str) / 100; |
750 |
|
|
int n = amount / at->value; |
751 |
elmex |
1.1 |
|
752 |
|
|
if (w == 0) |
753 |
root |
1.10 |
w = 1; /* Prevent divide by zero */ |
754 |
elmex |
1.1 |
|
755 |
|
|
if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) |
756 |
|
|
{ |
757 |
|
|
if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) |
758 |
|
|
n = (pouch->weight_limit - pouch->carrying) / w; |
759 |
|
|
|
760 |
root |
1.74 |
object *tmp = at->instance (); |
761 |
elmex |
1.1 |
tmp->nrof = n; |
762 |
root |
1.12 |
amount -= tmp->nrof * tmp->value; |
763 |
root |
1.49 |
pouch->insert (tmp); |
764 |
elmex |
1.1 |
} |
765 |
|
|
} |
766 |
|
|
} |
767 |
|
|
|
768 |
root |
1.39 |
if (amount / at->value > 0) |
769 |
elmex |
1.1 |
{ |
770 |
root |
1.74 |
object *tmp = at->instance (); |
771 |
elmex |
1.1 |
tmp->nrof = amount / tmp->value; |
772 |
root |
1.12 |
amount -= tmp->nrof * tmp->value; |
773 |
root |
1.49 |
pl->insert (tmp); |
774 |
elmex |
1.1 |
} |
775 |
|
|
} |
776 |
|
|
} |
777 |
|
|
|
778 |
root |
1.10 |
if (amount != 0) |
779 |
|
|
LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount); |
780 |
elmex |
1.1 |
} |
781 |
|
|
|
782 |
|
|
/* elmex: this is for the bank plugin :( */ |
783 |
root |
1.12 |
sint64 |
784 |
|
|
pay_player_arch (object *pl, const char *arch, sint64 amount) |
785 |
root |
1.10 |
{ |
786 |
root |
1.54 |
if (amount) |
787 |
|
|
{ |
788 |
|
|
object *ob = archetype::get (arch); |
789 |
elmex |
1.1 |
|
790 |
root |
1.54 |
if (!ob) |
791 |
|
|
return 0; |
792 |
elmex |
1.1 |
|
793 |
root |
1.54 |
ob->nrof = amount; |
794 |
|
|
pl->insert (ob); |
795 |
elmex |
1.1 |
} |
796 |
|
|
|
797 |
|
|
return 1; |
798 |
|
|
} |
799 |
|
|
|
800 |
|
|
/* Modified function to give out platinum coins. This function uses |
801 |
|
|
* the coins[] array to know what coins are available, just like |
802 |
|
|
* buy item. |
803 |
|
|
* |
804 |
|
|
* Modified to fill available race: gold containers before dumping |
805 |
|
|
* remaining coins in character's inventory. |
806 |
|
|
*/ |
807 |
elmex |
1.50 |
bool |
808 |
root |
1.10 |
sell_item (object *op, object *pl) |
809 |
|
|
{ |
810 |
root |
1.12 |
sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain; |
811 |
elmex |
1.1 |
|
812 |
root |
1.10 |
if (pl == NULL || pl->type != PLAYER) |
813 |
elmex |
1.1 |
{ |
814 |
root |
1.10 |
LOG (llevDebug, "Object other than player tried to sell something.\n"); |
815 |
elmex |
1.50 |
return false; |
816 |
elmex |
1.1 |
} |
817 |
|
|
|
818 |
root |
1.8 |
op->custom_name = 0; |
819 |
elmex |
1.1 |
|
820 |
root |
1.10 |
if (!amount) |
821 |
elmex |
1.1 |
{ |
822 |
elmex |
1.50 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.", |
823 |
|
|
query_name (op)); |
824 |
|
|
// elmex: change: the player now gets the item back if the shop is not |
825 |
|
|
// interested in it. |
826 |
|
|
return false; |
827 |
elmex |
1.1 |
} |
828 |
|
|
|
829 |
|
|
/* We compare the price with the one for a player |
830 |
|
|
* without bargaining skill. |
831 |
|
|
* This determins the amount of exp (if any) gained for bargaining. |
832 |
|
|
* exp/10 -> 1 for each gold coin |
833 |
|
|
*/ |
834 |
|
|
extra_gain = amount - query_cost (op, pl, F_SELL | F_NO_BARGAIN | F_SHOP); |
835 |
|
|
|
836 |
|
|
if (extra_gain > 0) |
837 |
root |
1.10 |
change_exp (pl, extra_gain / 10, "bargaining", SK_EXP_NONE); |
838 |
elmex |
1.1 |
|
839 |
|
|
pay_player (pl, amount); |
840 |
|
|
|
841 |
elmex |
1.50 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.", |
842 |
|
|
query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op)); |
843 |
root |
1.43 |
pl->play_sound (sound_find ("shop_sell")); |
844 |
elmex |
1.1 |
|
845 |
root |
1.78 |
op->set_flag (FLAG_UNPAID); |
846 |
elmex |
1.1 |
identify (op); |
847 |
elmex |
1.50 |
|
848 |
|
|
return true; |
849 |
elmex |
1.1 |
} |
850 |
|
|
|
851 |
|
|
/* returns a double that is the ratio of the price that a shop will offer for |
852 |
|
|
* item based on the shops specialisation. Does not take account of greed, |
853 |
root |
1.41 |
* returned value is between SPECIALISATION_EFFECT and 1. |
854 |
elmex |
1.1 |
*/ |
855 |
root |
1.10 |
static double |
856 |
root |
1.18 |
shop_specialisation_ratio (const object *item, const maptile *map) |
857 |
root |
1.10 |
{ |
858 |
|
|
shopitems *items = map->shopitems; |
859 |
root |
1.73 |
int likedness = 0; |
860 |
root |
1.10 |
int i; |
861 |
elmex |
1.1 |
|
862 |
root |
1.10 |
if (item == NULL) |
863 |
|
|
{ |
864 |
root |
1.28 |
LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", &map->path); |
865 |
root |
1.10 |
return 0; |
866 |
|
|
} |
867 |
root |
1.12 |
|
868 |
root |
1.10 |
if (!item->type) |
869 |
|
|
{ |
870 |
|
|
LOG (llevError, "shop_specialisation_ratio: passed an item with an invalid type\n"); |
871 |
|
|
/* |
872 |
|
|
* I'm not really sure what the /right/ thing to do here is, these types of |
873 |
|
|
* item shouldn't exist anyway, but returning the ratio is probably the best bet.." |
874 |
|
|
*/ |
875 |
root |
1.41 |
return SPECIALISATION_EFFECT; |
876 |
root |
1.10 |
} |
877 |
root |
1.12 |
|
878 |
root |
1.10 |
if (map->shopitems) |
879 |
root |
1.73 |
for (i = 0; i < items[0].index; i++) |
880 |
|
|
if (items[i].typenum == item->type || (!items[i].typenum && !likedness)) |
881 |
|
|
likedness = items[i].strength; |
882 |
root |
1.12 |
|
883 |
root |
1.73 |
if (likedness > 100) |
884 |
root |
1.10 |
{ /* someone has been rather silly with the map headers. */ |
885 |
root |
1.28 |
LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, &map->path); |
886 |
root |
1.73 |
likedness = 100; |
887 |
elmex |
1.1 |
} |
888 |
root |
1.12 |
|
889 |
root |
1.73 |
if (likedness < -100) |
890 |
root |
1.10 |
{ |
891 |
root |
1.28 |
LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, &map->path); |
892 |
root |
1.73 |
likedness = -100; |
893 |
elmex |
1.1 |
} |
894 |
root |
1.12 |
|
895 |
root |
1.73 |
return lerp (double (likedness), -100., 100., SPECIALISATION_EFFECT, 1.); |
896 |
elmex |
1.1 |
} |
897 |
|
|
|
898 |
|
|
/*returns the greed of the shop on map, or 1 if it isn't specified. */ |
899 |
root |
1.10 |
static double |
900 |
root |
1.18 |
shop_greed (const maptile *map) |
901 |
root |
1.10 |
{ |
902 |
root |
1.42 |
return map->shopgreed |
903 |
|
|
? map->shopgreed |
904 |
|
|
: 1.; |
905 |
elmex |
1.1 |
} |
906 |
|
|
|
907 |
|
|
/* Returns a double based on how much the shopkeeper approves of the player. |
908 |
|
|
* this is based on the race of the shopkeeper and that of the player. |
909 |
|
|
*/ |
910 |
root |
1.10 |
double |
911 |
root |
1.18 |
shopkeeper_approval (const maptile *map, const object *player) |
912 |
root |
1.10 |
{ |
913 |
root |
1.42 |
return map->shoprace && player->race != map->shoprace |
914 |
|
|
? DISLIKE_RATIO |
915 |
|
|
: 1.; |
916 |
elmex |
1.1 |
} |
917 |
|
|
|
918 |
|
|
/* limit the value of items based on the wealth of the shop. If the item is close |
919 |
|
|
* to the maximum value a shop will offer, we start to reduce it, if the item is |
920 |
|
|
* below the minimum value the shop is prepared to trade in, then we don't |
921 |
|
|
* want it and offer nothing. If it isn't a shop, check whether we should do generic |
922 |
|
|
* value reduction. |
923 |
|
|
* |
924 |
|
|
*/ |
925 |
root |
1.12 |
static sint64 |
926 |
|
|
value_limit (sint64 val, int quantity, const object *who, int isshop) |
927 |
root |
1.10 |
{ |
928 |
root |
1.12 |
sint64 newval, unit_price, tmpshopmax; |
929 |
root |
1.18 |
maptile *map; |
930 |
elmex |
1.1 |
|
931 |
|
|
unit_price = val / quantity; |
932 |
|
|
|
933 |
|
|
if (!isshop || !who) |
934 |
|
|
{ |
935 |
|
|
if (unit_price > 250000) |
936 |
root |
1.12 |
newval = (sint64) (250000. - pow (250000., .75) * 65. + pow (unit_price, .75) * 65.); |
937 |
elmex |
1.1 |
else |
938 |
|
|
newval = unit_price; |
939 |
|
|
} |
940 |
|
|
else |
941 |
|
|
{ |
942 |
|
|
if (!who->map) |
943 |
|
|
{ |
944 |
root |
1.10 |
LOG (llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name); |
945 |
elmex |
1.1 |
return val; |
946 |
|
|
} |
947 |
|
|
|
948 |
|
|
map = who->map; |
949 |
|
|
|
950 |
root |
1.10 |
tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default |
951 |
elmex |
1.1 |
|
952 |
|
|
if (map->shopmin && unit_price < map->shopmin) |
953 |
|
|
return 0; |
954 |
|
|
else if (unit_price > tmpshopmax / 2) |
955 |
root |
1.73 |
newval = min ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax); |
956 |
elmex |
1.1 |
else |
957 |
root |
1.10 |
newval = unit_price; |
958 |
elmex |
1.1 |
} |
959 |
|
|
|
960 |
|
|
newval *= quantity; |
961 |
|
|
|
962 |
|
|
return newval; |
963 |
|
|
} |
964 |
|
|
|
965 |
|
|
/* gives a desciption of the shop on their current map to the player op. */ |
966 |
root |
1.10 |
int |
967 |
|
|
describe_shop (const object *op) |
968 |
|
|
{ |
969 |
root |
1.71 |
dynbuf_text buf; |
970 |
root |
1.18 |
maptile *map = op->map; |
971 |
root |
1.10 |
|
972 |
|
|
/*shopitems *items=map->shopitems; */ |
973 |
|
|
int pos = 0, i; |
974 |
|
|
double opinion = 0; |
975 |
|
|
|
976 |
|
|
if (op->type != PLAYER) |
977 |
|
|
return 0; |
978 |
|
|
|
979 |
|
|
/*check if there is a shop specified for this map */ |
980 |
|
|
if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) |
981 |
|
|
{ |
982 |
root |
1.71 |
buf << "From looking at the nearby shop you determine that it trades in "; |
983 |
root |
1.72 |
int lastcomma = 0, prevcomma = 0; |
984 |
root |
1.12 |
|
985 |
root |
1.10 |
if (map->shopitems) |
986 |
root |
1.12 |
for (i = 0; i < map->shopitems[0].index; i++) |
987 |
|
|
if (map->shopitems[i].name && map->shopitems[i].strength > 10) |
988 |
root |
1.10 |
{ |
989 |
root |
1.72 |
buf << map->shopitems[i].name_pl; |
990 |
|
|
prevcomma = lastcomma; |
991 |
|
|
lastcomma = buf.size (); // remember offset |
992 |
|
|
buf << ", "; |
993 |
root |
1.6 |
} |
994 |
root |
1.12 |
|
995 |
root |
1.72 |
if (lastcomma) |
996 |
|
|
{ |
997 |
|
|
buf.splice (lastcomma, 2); |
998 |
|
|
|
999 |
|
|
if (prevcomma) |
1000 |
|
|
buf.splice (prevcomma, 2, " and "); |
1001 |
|
|
} |
1002 |
root |
1.71 |
else |
1003 |
root |
1.72 |
buf << "a little of everything."; |
1004 |
root |
1.6 |
|
1005 |
root |
1.72 |
buf << ".\n\n"; |
1006 |
root |
1.10 |
|
1007 |
|
|
if (map->shopmax) |
1008 |
root |
1.71 |
buf << "It won't trade for items above " << cost_string_from_value (map->shopmax, 0) << ".\n\n"; |
1009 |
root |
1.12 |
|
1010 |
root |
1.10 |
if (map->shopmin) |
1011 |
root |
1.71 |
buf << "It won't trade in items worth less than " << cost_string_from_value (map->shopmin, 0) << ".\n\n"; |
1012 |
root |
1.12 |
|
1013 |
root |
1.10 |
if (map->shopgreed) |
1014 |
|
|
{ |
1015 |
|
|
if (map->shopgreed > 2.0) |
1016 |
root |
1.71 |
buf << "It tends to overcharge massively.\n\n"; |
1017 |
root |
1.10 |
else if (map->shopgreed > 1.5) |
1018 |
root |
1.71 |
buf << "It tends to overcharge substantially.\n\n"; |
1019 |
root |
1.10 |
else if (map->shopgreed > 1.1) |
1020 |
root |
1.71 |
buf << "It tends to overcharge slightly.\n\n"; |
1021 |
root |
1.10 |
else if (map->shopgreed < 0.9) |
1022 |
root |
1.71 |
buf << "It tends to undercharge.\n\n"; |
1023 |
root |
1.10 |
} |
1024 |
root |
1.12 |
|
1025 |
root |
1.10 |
if (map->shoprace) |
1026 |
|
|
{ |
1027 |
|
|
opinion = shopkeeper_approval (map, op); |
1028 |
root |
1.71 |
|
1029 |
root |
1.10 |
if (opinion > 0.8) |
1030 |
root |
1.71 |
buf << "You think the shopkeeper likes you.\n\n"; |
1031 |
root |
1.10 |
else if (opinion > 0.5) |
1032 |
root |
1.71 |
buf << "The shopkeeper seems unconcerned by you.\n\n"; |
1033 |
root |
1.10 |
else |
1034 |
root |
1.71 |
buf << "The shopkeeper seems to have taken a dislike to you.\n\n"; |
1035 |
root |
1.6 |
} |
1036 |
elmex |
1.1 |
} |
1037 |
root |
1.10 |
else |
1038 |
root |
1.71 |
buf << "There is no shop nearby.\n\n"; |
1039 |
|
|
|
1040 |
|
|
op->contr->infobox (MSG_CHANNEL ("shopinfo"), buf); |
1041 |
elmex |
1.1 |
|
1042 |
root |
1.10 |
return 1; |
1043 |
elmex |
1.1 |
} |
1044 |
root |
1.12 |
|
1045 |
|
|
struct shopinv |
1046 |
root |
1.10 |
{ |
1047 |
|
|
char *item_sort; |
1048 |
|
|
char *item_real; |
1049 |
|
|
uint16 type; |
1050 |
|
|
uint32 nrof; |
1051 |
root |
1.12 |
}; |
1052 |
elmex |
1.1 |
|
1053 |
|
|
/* There are a lot fo extra casts in here just to suppress warnings - it |
1054 |
|
|
* makes it look uglier than it really it. |
1055 |
|
|
* The format of the strings we get is type:name. So we first want to |
1056 |
|
|
* sort by type (numerical) - if the same type, then sort by name. |
1057 |
|
|
*/ |
1058 |
root |
1.10 |
static int |
1059 |
|
|
shop_sort (const void *a1, const void *a2) |
1060 |
elmex |
1.1 |
{ |
1061 |
root |
1.10 |
shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; |
1062 |
elmex |
1.1 |
|
1063 |
root |
1.10 |
if (s1->type < s2->type) |
1064 |
|
|
return -1; |
1065 |
root |
1.52 |
|
1066 |
root |
1.10 |
if (s1->type > s2->type) |
1067 |
|
|
return 1; |
1068 |
root |
1.12 |
|
1069 |
root |
1.10 |
/* the type is the same (what atoi gets), so do a strcasecmp to sort |
1070 |
|
|
* via alphabetical order |
1071 |
|
|
*/ |
1072 |
|
|
return strcasecmp (s1->item_sort, s2->item_sort); |
1073 |
elmex |
1.1 |
} |
1074 |
|
|
|
1075 |
root |
1.10 |
static void |
1076 |
|
|
add_shop_item (object *tmp, shopinv * items, int *numitems, int *numallocated) |
1077 |
elmex |
1.1 |
{ |
1078 |
|
|
#if 0 |
1079 |
root |
1.10 |
char buf[MAX_BUF]; |
1080 |
elmex |
1.1 |
#endif |
1081 |
root |
1.10 |
/* clear unpaid flag so that doesn't come up in query |
1082 |
|
|
* string. We clear nrof so that we can better sort |
1083 |
|
|
* the object names. |
1084 |
|
|
*/ |
1085 |
elmex |
1.1 |
|
1086 |
root |
1.78 |
tmp->clr_flag (FLAG_UNPAID); |
1087 |
root |
1.10 |
items[*numitems].nrof = tmp->nrof; |
1088 |
|
|
/* Non mergable items have nrof of 0, but count them as one |
1089 |
|
|
* so the display is properly. |
1090 |
|
|
*/ |
1091 |
|
|
if (tmp->nrof == 0) |
1092 |
|
|
items[*numitems].nrof++; |
1093 |
|
|
items[*numitems].type = tmp->type; |
1094 |
|
|
|
1095 |
|
|
switch (tmp->type) |
1096 |
|
|
{ |
1097 |
elmex |
1.1 |
#if 0 |
1098 |
root |
1.6 |
case BOOTS: |
1099 |
|
|
case GLOVES: |
1100 |
|
|
case RING: |
1101 |
|
|
case AMULET: |
1102 |
|
|
case BRACERS: |
1103 |
|
|
case GIRDLE: |
1104 |
root |
1.10 |
sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL)); |
1105 |
root |
1.23 |
items[*numitems].item_sort = strdup (buf); |
1106 |
root |
1.10 |
sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL)); |
1107 |
root |
1.23 |
items[*numitems].item_real = strdup (buf); |
1108 |
root |
1.10 |
(*numitems)++; |
1109 |
|
|
break; |
1110 |
elmex |
1.1 |
#endif |
1111 |
|
|
|
1112 |
root |
1.6 |
default: |
1113 |
root |
1.23 |
items[*numitems].item_sort = strdup (query_base_name (tmp, 0)); |
1114 |
|
|
items[*numitems].item_real = strdup (query_base_name (tmp, 1)); |
1115 |
root |
1.10 |
(*numitems)++; |
1116 |
|
|
break; |
1117 |
|
|
} |
1118 |
root |
1.69 |
|
1119 |
root |
1.78 |
tmp->set_flag (FLAG_UNPAID); |
1120 |
root |
1.10 |
} |
1121 |
|
|
|
1122 |
|
|
void |
1123 |
elmex |
1.35 |
shop_listing (object *sign, object *op) |
1124 |
root |
1.10 |
{ |
1125 |
root |
1.65 |
int i, j, x1, x2, y1, y2; |
1126 |
root |
1.53 |
const char *shop_coords = sign->kv (shstr_shop_coords); |
1127 |
root |
1.10 |
object *stack; |
1128 |
|
|
shopinv *items; |
1129 |
|
|
|
1130 |
|
|
/* Should never happen, but just in case a monster does apply a sign */ |
1131 |
root |
1.65 |
if (!op->is_player ()) |
1132 |
root |
1.10 |
return; |
1133 |
|
|
|
1134 |
root |
1.67 |
dynbuf_text &buf = msg_dynbuf; buf.clear (); |
1135 |
root |
1.65 |
|
1136 |
elmex |
1.35 |
if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) |
1137 |
|
|
{ |
1138 |
|
|
x1 = 0; |
1139 |
|
|
y1 = 0; |
1140 |
|
|
x2 = op->map->width - 1; |
1141 |
|
|
y2 = op->map->height - 1; |
1142 |
|
|
} |
1143 |
root |
1.10 |
|
1144 |
root |
1.65 |
int numallocated = 40; |
1145 |
|
|
int numitems = 0; |
1146 |
|
|
items = (shopinv *)malloc (sizeof (shopinv) * numallocated); |
1147 |
root |
1.10 |
|
1148 |
|
|
/* Find all the appropriate items */ |
1149 |
elmex |
1.35 |
for (i = x1; i <= x2; i++) |
1150 |
root |
1.65 |
for (j = y1; j < y2; j++) |
1151 |
root |
1.77 |
if (op->map->is_in_shop (i, j)) |
1152 |
root |
1.10 |
{ |
1153 |
root |
1.65 |
stack = GET_MAP_OB (op->map, i, j); |
1154 |
|
|
|
1155 |
|
|
while (stack) |
1156 |
root |
1.10 |
{ |
1157 |
root |
1.78 |
if (stack->flag [FLAG_UNPAID]) |
1158 |
root |
1.10 |
{ |
1159 |
root |
1.65 |
if (numitems == numallocated) |
1160 |
|
|
items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2)); |
1161 |
root |
1.30 |
|
1162 |
root |
1.65 |
add_shop_item (stack, items, &numitems, &numallocated); |
1163 |
|
|
} |
1164 |
root |
1.30 |
|
1165 |
root |
1.65 |
stack = stack->above; |
1166 |
root |
1.6 |
} |
1167 |
|
|
} |
1168 |
root |
1.12 |
|
1169 |
root |
1.65 |
buf << (numitems ? "T<This shop contains:>\n\n" |
1170 |
|
|
: "T<This shop is currently empty.>"); |
1171 |
root |
1.12 |
|
1172 |
root |
1.10 |
qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); |
1173 |
|
|
|
1174 |
|
|
for (i = 0; i < numitems; i++) |
1175 |
|
|
{ |
1176 |
|
|
/* Collapse items of the same name together */ |
1177 |
|
|
if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) |
1178 |
root |
1.65 |
items[i + 1].nrof += items[i].nrof; |
1179 |
root |
1.10 |
else |
1180 |
root |
1.66 |
buf.printf (" %4d %s\n", items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real); |
1181 |
root |
1.65 |
|
1182 |
|
|
free (items[i].item_sort); |
1183 |
|
|
free (items[i].item_real); |
1184 |
elmex |
1.1 |
} |
1185 |
root |
1.30 |
|
1186 |
root |
1.65 |
op->contr->infobox (MSG_CHANNEL ("shopitems"), buf); |
1187 |
|
|
|
1188 |
root |
1.10 |
free (items); |
1189 |
elmex |
1.1 |
} |
1190 |
elmex |
1.7 |
|
1191 |
elmex |
1.45 |
/* elmex: this function checks whether we are in a shop or not |
1192 |
|
|
- change 2007-11-26: enhanced the O(n) case by stopping at the first |
1193 |
|
|
floor tile. this possibly will make map bugs where shopfloors are above |
1194 |
|
|
floors more obvious. |
1195 |
|
|
*/ |
1196 |
root |
1.10 |
bool |
1197 |
root |
1.77 |
maptile::is_in_shop (int x, int y) const |
1198 |
elmex |
1.7 |
{ |
1199 |
root |
1.77 |
for (object *floor = at (x, y).bot; floor; floor = floor->above) |
1200 |
|
|
if (floor->flag [FLAG_IS_FLOOR]) |
1201 |
elmex |
1.45 |
return floor->type == SHOP_FLOOR; |
1202 |
root |
1.52 |
|
1203 |
elmex |
1.7 |
return false; |
1204 |
|
|
} |
1205 |
root |
1.43 |
|