aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/use.cpp
blob: 78d82335a5ed8cf1a305dd4a595c0afdec12ee00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <memory>

#include "backend/ConsumableObject.h"
#include "backend/Exception.h"
#include "backend/print.h"

#include "../GameController.h"
#include "../util.h"

using namespace std;

void GameController::cmd_use(string & target_name) {
	Player & player = this->dungeon->get_player();
	Location & location = player.get_location();

	auto it = find_if_range(player.inventory.range(), by_name_case_insensitive(target_name));
	if (!it)
		throw Exception("object \"%s\" niet gevonden", target_name.c_str());
	Object & object = **it;

	try {
		auto consumable = unique_ptr<ConsumableObject>(&dynamic_cast<ConsumableObject &>(object));
		player.inventory.remove(consumable.get());

		lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str());
		consumable->consume(player);
	} catch (std::bad_cast &) {
		throw Exception("%s is niet consumeerbaar", object.get_name().c_str());
	}
}