aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/get.cpp
blob: ea1f69b62f7bcde2743d861dd9a512ef332f7440 (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
32
33
34
35
36
37
#include <memory>

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

#include "backend/Exception.h"
#include "backend/print.h"
#include "backend/GoldObject.h"
#include "backend/Location.h"
#include "backend/Dungeon.h"

using namespace std;

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

	auto it = find_if_range(location.get_visible_objects(), by_name_case_insensitive(target_name));
	if (!it)
		throw Exception("object \"%s\" niet gevonden", target_name.c_str());
	auto object = unique_ptr<Object>(*it);
	location.remove_visible_object(object.get());

	// gold objects are collected and (implicitly) destroyed
	try {
		GoldObject & gold = dynamic_cast<GoldObject &>(*object);
		int count = gold.get_count();
		player.gold += count;
		lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken");
		return;
	} catch (std::bad_cast &) {};

	// other objects go in the inventory
	lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str());
	player.inventory.push_back(object.release());
}