/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 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 #include #include #include /* this is a measure of how effective store specialisation is. A general store * will offer this proportion of the 'maximum' price, a specialised store will * offer a range of prices around it such that the maximum price is always one * therefore making this number higher, makes specialisation less effective. * setting this value above 1 or to a negative value would have interesting, * (though not useful) effects. */ #define SPECIALISATION_EFFECT .5 // price a shopkeeper will give someone that is not of their race #define DISLIKE_RATIO 0.8 static void pay_from_container (object *pl, object *pouch, sint64 &to_pay); static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop); static double shop_specialisation_ratio (const object *item, const maptile *map); static double shop_greed (const maptile *map); /* Added F_TRUE flag to define.h to mean that the price should not * be adjusted by players charisma. With F_TRUE, it returns the amount * that the item is worth, if it was sold, but unadjusted by charisma. * This is needed for alchemy, to to determine what value of gold nuggets * should be given (the gold nuggets, when sold, will have the adjustment * by charisma done at that time). NULL could have been passed as the * who parameter, but then the adjustment for expensive items (>10000) * would not be done. * * Added F_APPROX flag, which means that the price returned should be wrong by * an amount related to the player's bargaining skill. * * Added F_SHOP flag to mean that the specialisation of the shop on the player's * current map should be taken into account when determining the price. Shops that * specialise in what is being traded will give better prices than those that do not. * * CF 0.91.4 - This function got changed around a bit. Now the * number of object is multiplied by the value early on. This fixes problems * with items worth very little. What happened before is that various * divisions took place, the value got rounded to 0 (Being an int), and * thus remained 0. * * Mark Wedel (mwedel@pyramid.com) */ static sint64 approx_range; sint64 query_cost (const object *tmp, object *who, int flag) { double val; int no_bargain; int identified; int not_cursed; int approximate; int shop; approx_range = 0; no_bargain = flag & F_NO_BARGAIN; identified = flag & F_IDENTIFIED; not_cursed = flag & F_NOT_CURSED; approximate = flag & F_APPROX; shop = flag & F_SHOP; flag &= ~(F_NO_BARGAIN | F_IDENTIFIED | F_NOT_CURSED | F_APPROX | F_SHOP); int number = tmp->number_of (); if (tmp->type == MONEY) return number * tmp->value; if (tmp->type == GEM) { if (flag == F_TRUE) return number * tmp->value; if (flag == F_BUY) return 1.03 * number * tmp->value; if (flag == F_SELL) return 0.97 * number * tmp->value; LOG (llevError, "Query_cost: Gem type with unknown flag %d: %s\n", flag, tmp->debug_desc ()); return 0; } if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified) { if (!not_cursed && (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])) return 0; else val = number * tmp->value; } /* This area deals with objects that are not identified, but can be */ else { if (flag == F_BUY) { LOG (llevError | logBacktrace, "Asking for buy-value of unidentified object: %s\n", tmp->debug_desc ()); val = tmp->arch->value * 50 * number; } else { /* Trying to sell something, or get true value */ if (tmp->type == POTION) val = number * 40; /* Don't want to give anything away */ else { /* Get 2/3'rd value for applied objects, 1/3'rd for totally * unknown objects */ if (tmp->flag [FLAG_BEEN_APPLIED]) val = number * tmp->arch->value * 2 / 3; else val = number * tmp->arch->value / 3; } } } /* If the item has been applied or identifed or does not need to be * identified, AND the object is magical and the archetype is non * magical, then change values accordingly. The tmp->arch==NULL is * really just a check to prevent core dumps for when it checks * tmp->arch->magic for any magic. The check for archetype * magic is to not give extra money for archetypes that are by * default magical. This is because the archetype value should have * already figured in that value. */ if ((tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified || tmp->flag [FLAG_BEEN_APPLIED]) && tmp->magic && (!tmp->arch || !tmp->arch->magic)) { if (tmp->magic > 0) val *= 3 * tmp->magic * tmp->magic * tmp->magic; else /* Note that tmp->magic is negative, so that this * will actually be something like val /=2, /=3, etc. */ val /= 1 - tmp->magic; } if (tmp->type == WAND) { /* Value of the wand is multiplied by the number of * charges. the treasure code already sets up the value * 50 charges is used as the baseline. */ if (tmp->flag [FLAG_IDENTIFIED] || !tmp->need_identify () || identified) val *= tmp->stats.food; /* if not identified, presume one charge */ val /= 50; } /* Limit amount of money you can get for really great items. */ if (flag == F_SELL) val = value_limit (val, number, who, shop); // use a nonlinear price adjustment. as my predecessor said, don't change // the archetypes, its work required for balancing, and we don't care. //val = pow (val, 1.05); /* This modification is for bargaining skill. * Now only players with max level in bargaining * AND Cha = 30 will get optimal price. * Thus charisma will never get useless. * -b.e. edler@heydernet.de */ if (who && who->is_player ()) { int lev_bargain = 0; int lev_identify = 0; if (!no_bargain) if (object *skill = find_skill_by_name (who, shstr_bargaining)) lev_bargain = skill->level; if (const typedata *tmptype = get_typedata (tmp->type)) { if (int idskill1 = tmptype->identifyskill) { int idskill2 = tmptype->identifyskill2; if (find_skill_by_number (who, idskill1)) lev_identify = find_skill_by_number (who, idskill1)->level; if (idskill2 && find_skill_by_number (who, idskill2)) lev_identify += find_skill_by_number (who, idskill2)->level; } } /* ratio determines how much of the price modification * will come from the basic stat charisma * the rest will come from the level in bargaining skill */ const double cha_ratio = 0.40; double bargaining = max (0., 1. - powf (double (lev_bargain) / MAXLEVEL_TREASURE, 0.25)); double charisma = (cha_bonus[who->stats.Cha] - 1.) / (cha_bonus[who->stats.Cha] + 1.); double factor = (1. - cha_ratio) * bargaining + cha_ratio * charisma; // scale 0..1 to 2 .. 80% factor = lerp (factor, 0., 1., 0.02, 0.80); if (flag == F_BUY) val += val * factor; else if (flag == F_SELL) val -= val * factor; // now find a price range. the less good we can judge, the larger the range is // then the range is adjusted randomly around the correct value if (approximate) approx_range = val / sqrt (lev_identify * 3 + 1); } /* I don't think this should really happen - if it does, it indicates and * overflow of diff above. That should only happen if * we are selling objects - in that case, the person just * gets no money. */ if ((sint64) val < 0) val = 0; /* Unidentified stuff won't sell for more than 10gp */ if (flag == F_SELL && !tmp->flag [FLAG_IDENTIFIED] && tmp->need_identify () && !identified) min_it (val, 1000); /* if we are in a shop, check how the type of shop should affect the price */ if (shop && who) { if (flag == F_SELL) val *= shop_specialisation_ratio (tmp, who->map) * shopkeeper_approval (who->map, who) / shop_greed (who->map); else if (flag == F_BUY) { /* * when buying, if the item was sold by another player, it is ok to * let the item be sold cheaper, according to the specialisation of * the shop. If a player sold an item here, then his sale price was * multiplied by the specialisation ratio, to do the same to the buy * price will not generate extra money. However, the * same is not true of generated items, these have to /divide/ by the * specialisation, so that the price is never less than what they could * be sold for (otherwise players could camp map resets to make money). * In game terms, a non-specialist shop, might not recognise the true * value of the items they sell (much like how people sometimes find * antiques in a junk shop in real life). */ if (tmp->flag [FLAG_PLAYER_SOLD]) val *= shop_specialisation_ratio (tmp, who->map); else val /= shop_specialisation_ratio (tmp, who->map); val *= shop_greed (who->map) / shopkeeper_approval (who->map, who); } /* we will also have an extra 0-5% variation between shops of the same type * for valuable items (below a value of 50 this effect wouldn't be very * pointful, and could give fun with rounding. */ //TODO: why use cosf at all, just % and scale linearly, gives more even distribution if (val > 50) val *= 1 + .05f * cosf ((tmp->uuid.seq & 0xffff) * M_PI * 2. / 0x10000); } return val; } /* Find the coin type that is worth more the 'c'. Starts at the * cointype placement. */ static archetype * find_next_coin (sint64 c, int *cointype) { archetype *coin; do { if (!coins [*cointype]) return 0; coin = archetype::find (coins [*cointype]); if (!coin) return 0; *cointype += 1; } while (coin->value > c); return coin; } /* This returns a string of how much something is worth based on * an integer being passed. * cost is the cost we need to represent. * While cost is 64 bit, the number of any coin is still really * limited to 32 bit (size of nrof field). If it turns out players * have so much money that they have more than 2 billion platinum * coins, there are certainly issues - the easiest fix at that * time is to add a higher denomination (mithril piece with * 10,000 silver or something) */ const char * cost_string_from_value (sint64 cost, int approx) { archetype *coin, *next_coin; int num, cointype = 0; coin = find_next_coin (cost, &cointype); if (!coin) return "nothing"; num = cost / coin->value; /* so long as nrof is 32 bit, this is true. * If it takes more coins than a person can possibly carry, this * is basically true. */ if (cost / coin->value > UINT32_MAX) return "an unimaginable sum of money"; cost -= num * (sint64)coin->value; char *buf = format ("%d %s", num, num > 1 ? &coin->object::name_pl : &coin->object::name); next_coin = find_next_coin (cost, &cointype); if (!next_coin || approx) return buf; coin = next_coin; num = cost / coin->value; cost -= num * (sint64)coin->value; return format ("%s and %d %s", buf, num, num > 1 ? &coin->object::name_pl : &coin->object::name); } const char * query_cost_string (const object *tmp, object *who, int flag) { sint64 real_value = query_cost (tmp, who, flag); int idskill1 = 0; int idskill2 = 0; const typedata *tmptype; tmptype = get_typedata (tmp->type); if (tmptype) { idskill1 = tmptype->identifyskill; idskill2 = tmptype->identifyskill2; } /* we show an approximate price if * 1) we are approximating * 2) there either is no id skill(s) for the item, or we don't have them * 3) we don't have bargaining skill either */ if (flag & F_APPROX) { if (!idskill1 || !find_skill_by_number (who, idskill1)) { if (!idskill2 || !find_skill_by_number (who, idskill2)) { if (!find_skill_by_name (who, shstr_bargaining)) { int num, cointype = 0; archetype *coin = find_next_coin (real_value, &cointype); if (!coin) return "nothing"; num = real_value / coin->value; if (num == 1) return format ("about one %s", &coin->object::name); else if (num < 5) return format ("a few %s", &coin->object::name_pl); else if (num < 10) return format ("several %s", &coin->object::name_pl); else if (num < 25) return format ("a moderate amount of %s", &coin->object::name_pl); else if (num < 100) return format ("lots of %s", &coin->object::name_pl); else if (num < 1000) return format ("a great many %s", &coin->object::name_pl); else return format ("a vast quantity of %s", &coin->object::name_pl); } } } if (approx_range) { int hash = tmp->random_seed () & 1023; sint64 lo = real_value - (approx_range * hash >> 10); return format ("between %s and %s", cost_string_from_value (lo, 1), cost_string_from_value (lo + approx_range, 1)); } } return cost_string_from_value (real_value, 0); } /* This function finds out how much money the player is carrying, * including what is in containers. */ sint64 query_money (const object *op) { object *tmp; sint64 total = 0; if (op->type != PLAYER && op->type != CONTAINER) { LOG (llevError, "Query money called with non player/container\n"); return 0; } for (tmp = op->inv; tmp; tmp = tmp->below) if (tmp->type == MONEY) total += tmp->nrof * (sint64)tmp->value; else if (tmp->type == CONTAINER && tmp->flag [FLAG_APPLIED] && (!tmp->race || tmp->race.contains (shstr_gold))) total += query_money (tmp); return total; } /* TCHIZE: This function takes the amount of money from the * the player inventory and from it's various pouches using the * pay_from_container function. * returns 0 if not possible. 1 if success */ int pay_for_amount (sint64 to_pay, object *pl) { object *pouch; if (to_pay == 0) return 1; if (to_pay > query_money (pl)) return 0; pay_from_container (pl, pl, to_pay); for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold))) pay_from_container (pl, pouch, to_pay); pl->update_stats (); return 1; } /* DAMN: This is now a wrapper for pay_from_container, which is * called for the player, then for each active container that can hold * money until op is paid for. Change will be left wherever the last * of the price was paid from. */ int pay_for_item (object *op, object *pl) { sint64 to_pay = query_cost (op, pl, F_BUY | F_SHOP); object *pouch; sint64 saved_money; if (to_pay == 0) return 1; if (to_pay > query_money (pl)) return 0; /* We compare the paid price with the one for a player * without bargaining skill. * This determins the amount of exp (if any) gained for bargaining. */ saved_money = query_cost (op, pl, F_BUY | F_NO_BARGAIN | F_SHOP) - to_pay; if (saved_money > 0) change_exp (pl, saved_money, shstr_bargaining, SK_EXP_NONE); pay_from_container (pl, pl, to_pay); for (pouch = pl->inv; pouch && to_pay; pouch = pouch->below) if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && (!pouch->race || pouch->race.contains (shstr_gold))) pay_from_container (pl, pouch, to_pay); pl->update_stats (); return 1; } /* This pays for the item, and takes the proper amount of money off * the player. * CF 0.91.4 - this function is mostly redone in order to fix a bug * with weight not be subtracted properly. We now remove and * insert the coin objects - this should update the weight * appropriately * * DAMN: This function is used for the player, then for any active * containers that can hold money. * * pouch is the container (pouch or player) to remove the coins from. * to_pay is the required amount. * returns the amount still missing after using "pouch". */ static void pay_from_container (object *pl, object *pouch, sint64 &to_pay) { int count, i; object *tmp, *next; archetype *at; if (pouch->type != PLAYER && pouch->type != CONTAINER) return; object *coin_objs[NUM_COINS] = { 0 }; /* This hunk should remove all the money objects from the player/container */ for (tmp = pouch->inv; tmp; tmp = next) { next = tmp->below; if (tmp->type == MONEY) { for (i = 0; i < NUM_COINS; i++) { if (tmp->value == tmp->arch->value && !strcmp (coins[NUM_COINS - 1 - i], tmp->arch->archname)) { // This should not happen, but if it does, just merge the two. if (coin_objs [i]) { LOG (llevError, "%s has two money entries of (%s)\n", &pouch->name, coins[NUM_COINS - 1 - i]); coin_objs[i]->nrof += tmp->nrof; tmp->destroy (); } else { tmp->remove (); coin_objs[i] = tmp; } break; } } if (i == NUM_COINS) LOG (llevError, "in pay_for_item: Did not find string match for %s\n", &tmp->arch->archname); } } /* Fill in any gaps in the coin_objs array - needed to make change. */ /* Note that the coin_objs array goes from least value to greatest value */ for (i = 0; i < NUM_COINS; i++) if (!coin_objs[i]) { at = archetype::find (coins[NUM_COINS - 1 - i]); if (at == NULL) LOG (llevError, "Could not find %s archetype\n", coins[NUM_COINS - 1 - i]); coin_objs[i] = at->instance (); coin_objs[i]->nrof = 0; } for (i = 0; i < NUM_COINS; i++) { object &coin = *coin_objs[i]; sint64 num_coins = min ((to_pay + coin.value - 1) / coin.value, (sint64) coin.nrof); to_pay -= num_coins * coin.value; coin.nrof -= num_coins; /* Now start making change. Start at the coin value * below the one we just did, and work down to * the lowest value. */ count = i - 1; while (to_pay < 0 && count >= 0) { num_coins = (-to_pay) / coin_objs[count]->value; coin_objs[count]->nrof += num_coins; to_pay += num_coins * coin_objs[count]->value; count--; } } for (i = 0; i < NUM_COINS; i++) if (coin_objs[i]->nrof) insert_ob_in_ob (coin_objs [i], pouch); else coin_objs[i]->destroy (); } /* Checks all unpaid items in op's inventory, adds up all the money they * have, and checks that they can actually afford what they want to buy. * Returns 1 if they can, and 0 if they can't. also prints an appropriate message * to the player */ int can_pay (object *pl) { int unpaid_count = 0; sint64 unpaid_price = 0; sint64 player_wealth = query_money (pl); if (!pl || pl->type != PLAYER) { LOG (llevError, "can_pay(): called against something that isn't a player\n"); return 0; } for (object::depth_iterator item = pl->begin (); item != pl->end (); ++item) if (item->flag [FLAG_UNPAID]) { unpaid_count++; unpaid_price += query_cost (item, pl, F_BUY | F_SHOP); } if (unpaid_price > player_wealth) { dynbuf_text &buf = msg_dynbuf; buf.clear (); buf << "You have " << unpaid_count << " unpaid item(s) that would cost you " << cost_string_from_value (unpaid_price, 0) << ". You need another " << cost_string_from_value (unpaid_price - player_wealth, 0) << " to be able to afford that. " "H"; pl->failmsg (buf); return 0; } else return 1; } /* Better get_payment, descends containers looking for * unpaid items, and pays for them. * returns 0 if the player still has unpaid items. * returns 1 if the player has paid for everything. * pl is the player buying the stuff. */ int get_payment (object *pl) { for (;;) { next_item: for (object::depth_iterator op = pl->begin (); op != pl->end (); ++op) { if (op->flag [FLAG_UNPAID]) { const char *buf = query_cost_string (op, pl, F_BUY | F_SHOP); if (!pay_for_item (op, pl)) { sint64 i = query_cost (op, pl, F_BUY | F_SHOP) - query_money (pl); op->clr_flag (FLAG_UNPAID); new_draw_info_format (NDI_UNIQUE, 0, pl, "You lack %s to buy %s.", cost_string_from_value (i, 0), query_name (op)); op->set_flag (FLAG_UNPAID); return 0; } else { op->clr_flag (FLAG_UNPAID); op->clr_flag (FLAG_PLAYER_SOLD); new_draw_info_format (NDI_UNIQUE, 0, pl, "You paid %s for %s.", buf, query_name (op)); if (!merge_ob (op, op->env->inv)) esrv_update_item (UPD_FLAGS, pl, op); goto next_item; } } } return 1; } } /* written by elmex: * moved this code from sell_item () here to have a function * that pays the player an amount. Mainly put the code here to * be able to call it from a plugin. * * If the player can't carry all the money that is paid, it gets inserted * in his inventory anyway. This is the best alternative to not pay any money * or put it on the ground under the player. This way the player can still * go somewhere and unload the money at a safe place. * */ void pay_player (object *pl, sint64 amount) { int count = 0; archetype *at = 0; object *pouch = 0, *tmp = 0; for (count = 0; coins[count]; count++) { at = archetype::find (coins[count]); if (at == NULL) LOG (llevError, "Could not find %s archetype\n", coins[count]); else if ((amount / at->value) > 0) { for (pouch = pl->inv; pouch; pouch = pouch->below) { if (pouch->type == CONTAINER && pouch->flag [FLAG_APPLIED] && pouch->race.contains (shstr_gold)) { int w = at->weight * (100 - pouch->stats.Str) / 100; int n = amount / at->value; if (w == 0) w = 1; /* Prevent divide by zero */ if (n > 0 && (!pouch->weight_limit || pouch->carrying + w <= pouch->weight_limit)) { if (pouch->weight_limit && (pouch->weight_limit - pouch->carrying) / w < n) n = (pouch->weight_limit - pouch->carrying) / w; object *tmp = at->instance (); tmp->nrof = n; amount -= tmp->nrof * tmp->value; pouch->insert (tmp); } } } if (amount / at->value > 0) { object *tmp = at->instance (); tmp->nrof = amount / tmp->value; amount -= tmp->nrof * tmp->value; pl->insert (tmp); } } } if (amount != 0) LOG (llevError, "Warning - payment in pay_player () not zero: %llu\n", amount); } /* elmex: this is for the bank plugin :( */ sint64 pay_player_arch (object *pl, const char *arch, sint64 amount) { if (amount) { object *ob = archetype::get (arch); if (!ob) return 0; ob->nrof = amount; pl->insert (ob); } return 1; } /* Modified function to give out platinum coins. This function uses * the coins[] array to know what coins are available, just like * buy item. * * Modified to fill available race: gold containers before dumping * remaining coins in character's inventory. */ bool sell_item (object *op, object *pl) { sint64 amount = query_cost (op, pl, F_SELL | F_SHOP), extra_gain; if (pl == NULL || pl->type != PLAYER) { LOG (llevDebug, "Object other than player tried to sell something.\n"); return false; } op->custom_name = 0; if (!amount) { new_draw_info_format (NDI_UNIQUE, 0, pl, "We're not interested in %s.", query_name (op)); // elmex: change: the player now gets the item back if the shop is not // interested in it. return false; } /* We compare the price with the one for a player * without bargaining skill. * This determins the amount of exp (if any) gained for bargaining. * exp/10 -> 1 for each gold coin */ extra_gain = amount - query_cost (op, pl, F_SELL | F_NO_BARGAIN | F_SHOP); if (extra_gain > 0) change_exp (pl, extra_gain / 10, shstr_bargaining, SK_EXP_NONE); pay_player (pl, amount); new_draw_info_format (NDI_UNIQUE, 0, pl, "You receive %s for %s.", query_cost_string (op, pl, F_SELL | F_SHOP), query_name (op)); pl->play_sound (sound_find ("shop_sell")); op->set_flag (FLAG_UNPAID); identify (op); return true; } /* returns a double that is the ratio of the price that a shop will offer for * item based on the shops specialisation. Does not take account of greed, * returned value is between SPECIALISATION_EFFECT and 1. */ static double shop_specialisation_ratio (const object *item, const maptile *map) { shopitems *items = map->shopitems; int likedness = 0; int i; if (item == NULL) { LOG (llevError, "shop_specialisation_ratio: passed a NULL item for map %s\n", &map->path); return 0; } if (!item->type) { LOG (llevError, "shop_specialisation_ratio: passed an item with an invalid type\n"); /* * I'm not really sure what the /right/ thing to do here is, these types of * item shouldn't exist anyway, but returning the ratio is probably the best bet.." */ return SPECIALISATION_EFFECT; } if (map->shopitems) for (i = 0; i < items[0].index; i++) if (items[i].typenum == item->type || (!items[i].typenum && !likedness)) likedness = items[i].strength; if (likedness > 100) { /* someone has been rather silly with the map headers. */ LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is above 100%%\n", item->type, &map->path); likedness = 100; } if (likedness < -100) { LOG (llevDebug, "shop_specialisation ratio: item type %d on map %s is below -100%%\n", item->type, &map->path); likedness = -100; } return lerp (double (likedness), -100., 100., SPECIALISATION_EFFECT, 1.); } /*returns the greed of the shop on map, or 1 if it isn't specified. */ static double shop_greed (const maptile *map) { return map->shopgreed ? map->shopgreed : 1.; } /* Returns a double based on how much the shopkeeper approves of the player. * this is based on the race of the shopkeeper and that of the player. */ double shopkeeper_approval (const maptile *map, const object *player) { return map->shoprace && player->race != map->shoprace ? DISLIKE_RATIO : 1.; } /* limit the value of items based on the wealth of the shop. If the item is close * to the maximum value a shop will offer, we start to reduce it, if the item is * below the minimum value the shop is prepared to trade in, then we don't * want it and offer nothing. If it isn't a shop, check whether we should do generic * value reduction. * */ static sint64 value_limit (sint64 val, int quantity, const object *who, int isshop) { sint64 newval, unit_price, tmpshopmax; maptile *map; unit_price = val / quantity; if (!isshop || !who) { if (unit_price > 250000) newval = (sint64) (250000. - pow (250000., .75) * 65. + pow (unit_price, .75) * 65.); else newval = unit_price; } else { if (!who->map) { LOG (llevError, "value_limit: asked shop price for ob %s on NULL map\n", &who->name); return val; } map = who->map; tmpshopmax = map->shopmax ? map->shopmax : 100000; // 20 royalties default if (map->shopmin && unit_price < map->shopmin) return 0; else if (unit_price > tmpshopmax / 2) newval = min ((tmpshopmax / 2) + isqrt (unit_price - tmpshopmax / 2), tmpshopmax); else newval = unit_price; } newval *= quantity; return newval; } /* gives a desciption of the shop on their current map to the player op. */ int describe_shop (const object *op) { dynbuf_text buf; maptile *map = op->map; /*shopitems *items=map->shopitems; */ int pos = 0, i; double opinion = 0; if (op->type != PLAYER) return 0; /*check if there is a shop specified for this map */ if (map->shopitems || map->shopgreed || map->shoprace || map->shopmin || map->shopmax) { buf << "From looking at the nearby shop you determine that it trades in "; int lastcomma = 0, prevcomma = 0; if (map->shopitems) for (i = 0; i < map->shopitems[0].index; i++) if (map->shopitems[i].name && map->shopitems[i].strength > 10) { buf << map->shopitems[i].name_pl; prevcomma = lastcomma; lastcomma = buf.size (); // remember offset buf << ", "; } if (lastcomma) { buf.splice (lastcomma, 2); if (prevcomma) buf.splice (prevcomma, 2, " and "); } else buf << "a little of everything."; buf << ".\n\n"; if (map->shopmax) buf << "It won't trade for items above " << cost_string_from_value (map->shopmax, 0) << ".\n\n"; if (map->shopmin) buf << "It won't trade in items worth less than " << cost_string_from_value (map->shopmin, 0) << ".\n\n"; if (map->shopgreed) { if (map->shopgreed > 2.0) buf << "It tends to overcharge massively.\n\n"; else if (map->shopgreed > 1.5) buf << "It tends to overcharge substantially.\n\n"; else if (map->shopgreed > 1.1) buf << "It tends to overcharge slightly.\n\n"; else if (map->shopgreed < 0.9) buf << "It tends to undercharge.\n\n"; } if (map->shoprace) { opinion = shopkeeper_approval (map, op); if (opinion > 0.8) buf << "You think the shopkeeper likes you.\n\n"; else if (opinion > 0.5) buf << "The shopkeeper seems unconcerned by you.\n\n"; else buf << "The shopkeeper seems to have taken a dislike to you.\n\n"; } } else buf << "There is no shop nearby.\n\n"; op->contr->infobox (MSG_CHANNEL ("shopinfo"), buf); return 1; } struct shopinv { char *item_sort; char *item_real; sint64 value; uint16 type; uint32 nrof; }; /* There are a lot fo extra casts in here just to suppress warnings - it * makes it look uglier than it really it. * The format of the strings we get is type:name. So we first want to * sort by type (numerical) - if the same type, then sort by name. */ static int shop_sort (const void *a1, const void *a2) { shopinv *s1 = (shopinv *) a1, *s2 = (shopinv *) a2; if (s1->type < s2->type) return -1; if (s1->type > s2->type) return 1; /* the type is the same (what atoi gets), so do a strcasecmp to sort * via alphabetical order */ return strcasecmp (s1->item_sort, s2->item_sort); } static void add_shop_item (object *tmp, shopinv * items, int *numitems, int *numallocated) { /* clear unpaid flag so that doesn't come up in query * string. We clear nrof so that we can better sort * the object names. */ tmp->clr_flag (FLAG_UNPAID); items[*numitems].nrof = tmp->nrof; /* Non mergable items have nrof of 0, but count them as one * so the display is properly. */ if (tmp->nrof == 0) items[*numitems].nrof++; items[*numitems].type = tmp->type; items[*numitems].value = tmp->value; switch (tmp->type) { #if 0 case BOOTS: case GLOVES: case RING: case AMULET: case BRACERS: case GIRDLE: sprintf (buf, "%s %s", query_base_name (tmp, 0), describe_item (tmp, NULL)); items[*numitems].item_sort = strdup (buf); sprintf (buf, "%s %s", query_name (tmp), describe_item (tmp, NULL)); items[*numitems].item_real = strdup (buf); (*numitems)++; break; #endif default: items[*numitems].item_sort = strdup (query_base_name (tmp, 0)); items[*numitems].item_real = strdup (query_base_name (tmp, 1)); items[*numitems].value += tmp->value; (*numitems)++; break; } tmp->set_flag (FLAG_UNPAID); } void shop_listing (object *sign, object *op) { int i, j, x1, x2, y1, y2; const char *shop_coords = sign->kv [shstr_shop_coords]; object *stack; shopinv *items; /* Should never happen, but just in case a monster does apply a sign */ if (!op->is_player ()) return; dynbuf_text &buf = msg_dynbuf; buf.clear (); if (!(shop_coords && sscanf (shop_coords, "%d,%d,%d,%d", &x1, &y1, &x2, &y2))) { x1 = 0; y1 = 0; x2 = op->map->width - 1; y2 = op->map->height - 1; } int numallocated = 40; int numitems = 0; items = (shopinv *)malloc (sizeof (shopinv) * numallocated); /* Find all the appropriate items */ for (i = x1; i <= x2; i++) for (j = y1; j < y2; j++) if (op->map->is_in_shop (i, j)) { stack = GET_MAP_OB (op->map, i, j); while (stack) { if (stack->flag [FLAG_UNPAID]) { if (numitems == numallocated) items = (shopinv *)realloc (items, sizeof (shopinv) * (numallocated *= 2)); add_shop_item (stack, items, &numitems, &numallocated); } stack = stack->above; } } buf << (numitems ? "T\n\n" : "T"); qsort (items, numitems, sizeof (shopinv), (int (*)(const void *, const void *)) shop_sort); for (i = 0; i < numitems; i++) { /* Collapse items of the same name together */ if ((i + 1) < numitems && !strcmp (items[i].item_real, items[i + 1].item_real)) items[i + 1].nrof += items[i].nrof; else { buf.printf ( " %4d %s\n for %s\n", items[i].nrof ? items[i].nrof : 1, items[i].nrof == 1 ? items[i].item_sort : items[i].item_real, cost_string_from_value (items[i].value, op->flag [FLAG_WIZ] ? 0 : 1)); } free (items[i].item_sort); free (items[i].item_real); } op->contr->infobox (MSG_CHANNEL ("shopitems"), buf); free (items); } /* elmex: this function checks whether we are in a shop or not - change 2007-11-26: enhanced the O(n) case by stopping at the first floor tile. this possibly will make map bugs where shopfloors are above floors more obvious. */ bool maptile::is_in_shop (int x, int y) const { for (object *floor = at (x, y).bot; floor; floor = floor->above) if (floor->flag [FLAG_IS_FLOOR]) return floor->type == SHOP_FLOOR; return false; }