Beispiel #1
0
/**
 * Check if there is enough currency to buy the given stack, and if so remove it from the current total and add the stack.
 * (Handle the drop into the equipment area, but add() don't handle it well in all circonstances. MenuManager::logic() allow only into the carried area.)
 */
bool MenuInventory::buy(ItemStack stack, int tab, bool dragging) {
	if (stack.empty()) {
		return true;
	}

	int value_each;
	if (tab == VENDOR_BUY) value_each = items->items[stack.item].getPrice();
	else value_each = items->items[stack.item].getSellPrice();

	int count = value_each * stack.quantity;
	if( inventory[CARRIED].count(CURRENCY_ID) >= count) {
		if (dragging) {
			drop(inpt->mouse, stack);
		}
		else {
			add(stack, CARRIED, -1, true, true);
		}

		removeCurrency(count);
		items->playSound(CURRENCY_ID);
		return true;
	}
	else {
		pc->logMsg(msg->get("Not enough %s.", CURRENCY), true);
		drop_stack.push(stack);
		return false;
	}
}
/**
 * Check if there is enough currency to buy the given stack, and if so remove it from the current total and add the stack.
 * (Handle the drop into the equipment area, but add() don't handle it well in all circonstances. MenuManager::logic() allow only into the carried area.)
 */
bool MenuInventory::buy(ItemStack stack, int tab) {
	int value_each;
	if (tab == VENDOR_BUY) value_each = items->items[stack.item].price;
	else value_each = items->items[stack.item].getSellPrice();

	int count = value_each * stack.quantity;
	if( getCurrency() >= count) {
		removeCurrency(count);
		items->playSound(CURRENCY_ID);
		return true;
	}
	else {
		return false;
	}
}
void MenuInventory::logic() {

	// if the player has just died, the penalty is half his current currency.
	if (stats->death_penalty && DEATH_PENALTY) {
		std::string death_message = "";

		// remove a % of currency
		if (DEATH_PENALTY_CURRENCY > 0) {
			if (currency > 0)
				removeCurrency((currency * DEATH_PENALTY_CURRENCY) / 100);
			death_message += msg->get("Lost %d%% of %s. ", DEATH_PENALTY_CURRENCY, CURRENCY);
		}

		// remove a % of either total xp or xp since the last level
		if (DEATH_PENALTY_XP > 0) {
			if (stats->xp > 0)
				stats->xp -= (stats->xp * DEATH_PENALTY_XP) / 100;
			death_message += msg->get("Lost %d%% of total XP. ", DEATH_PENALTY_XP);
		}
		else if (DEATH_PENALTY_XP_CURRENT > 0) {
			if (stats->xp - stats->xp_table[stats->level-1] > 0)
				stats->xp -= ((stats->xp - stats->xp_table[stats->level-1]) * DEATH_PENALTY_XP_CURRENT) / 100;
			death_message += msg->get("Lost %d%% of current level XP. ", DEATH_PENALTY_XP_CURRENT);
		}

		// prevent down-leveling from removing too much xp
		if (stats->xp < stats->xp_table[stats->level-1])
			stats->xp = stats->xp_table[stats->level-1];

		// remove a random carried item
		if (DEATH_PENALTY_ITEM) {
			std::vector<int> removable_items;
			removable_items.clear();
			for (int i=0; i < MAX_EQUIPPED; i++) {
				if (!inventory[EQUIPMENT][i].empty()) {
					if (items->items[inventory[EQUIPMENT][i].item].type != "quest")
						removable_items.push_back(inventory[EQUIPMENT][i].item);
				}
			}
			for (int i=0; i < MAX_CARRIED; i++) {
				if (!inventory[CARRIED][i].empty()) {
					if (items->items[inventory[CARRIED][i].item].type != "quest")
						removable_items.push_back(inventory[CARRIED][i].item);
				}
			}
			if (!removable_items.empty()) {
				int random_item = rand() % removable_items.size();
				remove(removable_items[random_item]);
				death_message += msg->get("Lost %s.",items->items[removable_items[random_item]].name);
			}
		}

		log_msg = death_message;

		stats->death_penalty = false;
	}

	// a copy of currency is kept in stats, to help with various situations
	stats->currency = currency = getCurrency();

	// check close button
	if (visible) {
		tablist.logic();

		if (closeButton->checkClick()) {
			visible = false;
			snd->play(sfx_close);
		}
		if (drag_prev_src == -1) {
			clearHighlight();
		}
	}
}