blob: 9c2c81e9c483ffc241dd7b6e5213964c97d36ab1 (
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <math.h>
#include <typeinfo>
// Programmer: Brett Weiland
// Date: 4/30/23
// File: cop.hpp
// Assignment: SP-B-finalProject
// Purpose: contains member functions for robber class; see robber.h
#include "cop.h"
template <class T> void robber<T>::setGreedy(const bool greedy_value) { greedy = greedy_value; }
template <class T> unsigned int robber<T>::get_drawPriority() const { return 0; }
template <class T> void robber<T>::free() { this->active = true; }
template <class T> bool robber<T>::isGreedy() const { return greedy; }
template <class T> bool robber<T>::iterateInteractables() {
for(gameObject *peer = this->parentCity->findObjsByLocation(this->location);
peer != NULL; peer = peer->get_top()) {
return handleInteractable(*peer);
}
return false;
}
template <class T> bool robber<T>::handleInteractable(gameObject &peer) {
const std::type_info &peer_type = typeid(peer);
if(peer_type == typeid(robber<T>)) {
if(!greedy) return false;
this->inventory_qty /= 2;
for(size_t loot_i = this->inventory_qty;
(loot_i < BAG_SIZE) && (this->inventory[loot_i] != NULL); loot_i++) {
total_loot_value -= this->inventory[loot_i]->value();
this->inventory[loot_i]->drop();
this->inventory[loot_i] = NULL;
}
}
if(peer_type == typeid(cop<T>)) {
cop<T> &popo = dynamic_cast<cop<T>&>(peer);
if(greedy && (this->inventory_qty >= BAG_SIZE)) {
this->inventory_qty--;
popo.takeBribe(this->inventory[this->inventory_qty]);
total_loot_value -= this->inventory[this->inventory_qty]->value();
this->inventory[this->inventory_qty] = NULL;
} else { popo.arrest(*this); }
return false;
}
if(peer_type == typeid(T)) {
if(this->inventory_qty >= BAG_SIZE) return false;
this->inventory[this->inventory_qty] = dynamic_cast<T*>(&peer);
this->inventory[this->inventory_qty]->pickup();
total_loot_value += this->inventory[this->inventory_qty]->value();
if(!(this->inventory[this->inventory_qty++]->value() % 2)) return true;
}
return false;
}
template <class T> void robber<T>::move() {
for(unsigned int moves = 0; moves < MAX_MOVES; moves++) {
if(greedy) {
if(this->inventory_qty >= BAG_SIZE)
this->location.stepDirection(this->parentCity->nearestCopDirection(this->location));
else
this->location.stepDirection(this->parentCity->directionToAnyJewl(this->location));
}
else {
this->location.stepDirection(this->parentCity->randomDirection(this->location));
}
if(!iterateInteractables()) break;
}
}
template <class T> T **robber<T>::getArrested() {
this->active = false;
total_loot_value -= this->get_inventoryValue();
this->inventory_qty = 0;
return this->inventory;
}
template <class T> char robber<T>::getIcon() const {
if(this->bottom_obj == NULL) return 'r';
if((typeid(*this->bottom_obj) == typeid(robber<T>)) && (dynamic_cast<robber<T>*>(this->bottom_obj)->active)) {
return 'R';
}
return 'r';
}
template <class T> unsigned int robber<T>::get_totalRobberValue() const { return total_loot_value; }
|