aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/hit.cpp
blob: f4a78fdf406f2bfcf9608d6a2489f00152b682fe (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
#include "backend/Enemy.h"
#include "backend/Location.h"
#include "backend/RNG.h"
#include "backend/print.h"
#include "backend/Dungeon.h"
#include "backend/Exception.h"

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

using namespace std;

void GameController::cmd_hit(string & target_name) {
	RNG & rng = RNG::get();
	Player & player = this->dungeon->get_player();

	auto it = find_if_range(player.get_location().get_enemies(), by_name_case_insensitive(target_name));
	if (!it)
		throw Exception("vijand \"%s\" niet gevonden", target_name.c_str());
	Enemy & enemy = **it;

	if (enemy.is_dead())
		throw Exception("%s is al dood!", enemy.get_name().c_str());

	if (rng.rand_double() > player.get_attack()) {
		lprtf("Je hebt gemist!\n");
	} else {
		unsigned damage = rng.rand_int(player.weapon->get_damage());
		enemy.take_damage(damage);
		lprtf("Je hebt %s geraakt en %d schade aangericht!\n", enemy.get_displayname().c_str(), damage);
	}

	this->dungeon->update();
}