-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
245 lines (215 loc) · 5.86 KB
/
Player.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "Player.h"
#include <cstring>
#include "display.h"
#include "Potion.h"
Player* Player::player = 0; /*TODO: not necessary since static objects are set to 0 by default*/
Player* Player::getPlayer(){
if (!player) //player has not been created
player = new Player();
return player;
}
Player::Player() : sight(0), carry(), equip(), potions(), weight(0), max_weight(75), pesos(100000) {
damage = "0+1d4";
hp = 500;
}
Pair Player::getPos() const{
Pair position = {this->getX(), this->getY()};
return position;
}
int Player::getWeight() const{
return weight;
}
int Player::getMaxWeight() const{
return max_weight;
}
int Player::getPesos() const{
return pesos;
}
Player::Player(Player const& player_copy){}//TODO why?
int** Player::setSight(int height, int width){
if (width < 1 || height < 1 || sight){
return sight; /*TODO: handle this in a better way, remove fprintf, consider merging both if blocks*/
}
if ((sight = (int **)malloc(sizeof(int*) * height))){
for (int i = 0; i < height; i++)
*(sight + i) = (int* )malloc(sizeof(int) * width);
}
return sight;
}
Item* Player::buy(int idx, Item* item){
Item* new_item;// = new Item(idx, *item);
if (dynamic_cast<Potion*>(item)){
new_item = new Potion(idx, *static_cast<Potion*>(item));
}else{
new_item = new Item(idx, *item);
}
if (pick(new_item)){
pesos -= item->getValue();
new_item->equip();
return new_item;
}
delete new_item;
return 0;
}
// void Character::updateSight(int height, int width, char map[][width]){
// // char** sight = csetSight(pc, nRows, nCols); /*TODO: check value of sight*/
// if (sight && id == 0){ /*Checks that Character is PC*/
// int startX = (x - 5 > 1) ? x - 5 : 1;
// int startY = (y - 5 > 1) ? y - 5 : 1;
// int endX = (x + 5 < width - 1) ? x + 5 : width - 1;
// int endY = (y + 5 < height - 1) ? y + 5 : height - 1;
// for (int i = startY; i < endY; i++){
// for (int j = startX; j < endX; j++){
// sight[i][j] = map[i][j];
// }
// }
// }
// }
Item ** Player::inventory(){ //TODO: player drops inventory when using the stairs
return carry;
}
Item ** Player::equipment(){
return equip;
}
Item ** Player::purse(){
return potions;
}
bool Player::pick(Item* item){
for (int i = 0; i < 10; i++){
if (!carry[i]){
if (item->getWeight() + weight > max_weight) break;
carry[i] = item;
carry[i]->equip();
weight += item->getWeight();
return true;
}
}
return false;
}
int Player::drop(int itm_idx){
if (!carry[itm_idx]) return -1; //NOTE: -1 not 0 swap maybe?
carry[itm_idx]->unequip();
int itm_id = carry[itm_idx]->getId();
weight -= carry[itm_idx]->getWeight();
carry[itm_idx] = 0;
return itm_id;
}
void Player::expunge(int itm_idx){
if (carry[itm_idx]){
carry[itm_idx]->unequip();
weight -= carry[itm_idx]->getWeight();
}
carry[itm_idx] = 0;
}
void Player::wear(int itm_idx){
if (!carry[itm_idx]) return;
Item* add = carry[itm_idx];
int slot = object_parser::private_wrapper::item_number(add->getSymbol());
if (slot != -1 && slot <= 10){
if (slot == 8){ /*TODO: item 8 is never swapped out except if there's nothing there*/
slot = equip[slot] ? 11 : 8;
}
Item* current = equip[slot];
equip[slot] = add;
speed += add->getSpeedBonus();
carry[itm_idx] = current;
}else if (slot >= 0){ //wear potion. TODO: wrong condition, just a quick workaround
Item* current = 0;//potions[0];
unsigned int i = 0;
for (i = 0; i < sizeof(potions)/sizeof(potions[0]); i++){
if (!potions[i]){
potions[i] = add;
i = 0;
break; //found empty slot
}
}
if (i){ //no empty slot was found
current = potions[0];
potions[0] = add;
}
// speed += add->getSpeedBonus();
carry[itm_idx] = current;
}
}
bool Player::take_off(int index){
if (pick(equip[index])){
weight -= equip[index]->getWeight();
speed -= equip[index]->getSpeedBonus();
equip[index] = 0;
return true;
}
return false;
}
int Player::attack() const{
int dam = Character::attack();
double hit_bonus = 0;
for (unsigned int i = 0; i < sizeof(equip)/sizeof(equip[0]); i++){
if (equip[i]) {
dam += equip[i]->getDamageBonus();
hit_bonus += equip[i]->getHit();
}
}
return dam * ((100 + hit_bonus)/100.0);
}
bool Player::takeDamage(int damage_in){
for (unsigned int i = 0; i < sizeof(equip)/sizeof(equip[0]); i++){
if (equip[i]) {
damage_in -= (equip[i]->getDefenseBonus() * ((equip[i]->getDodge() + 100.0)/100.0));
}
}
return (damage_in < 0) ? false : Character::takeDamage(damage_in);
}
void Player::freeSight(int height){
if (sight){
for (int i = 0; i < height; i++)
free(*(sight + i)); //vs free(*(sight + i*sizeof(sight[0]));
free(sight);
sight = 0;
}
}
void Player::clearSlots(bool inventory, bool equipment){
if (inventory) memset(carry, 0, sizeof(carry));
if (equipment){
speed = 10;
memset(equip, 0, sizeof(equip));
}
}
void Player::earn(int amount){
pesos += amount;
}
void Player::unequip_all(){
for (auto item : carry){
if (item)
item->unequip();
}
for (auto item : equip){
if (item)
item->unequip();
}
for (auto item : potions){
if (item)
item->unequip();
}
}
// static void unequip(Item ** items){
// for (auto item : items){
// if (item)
// item->unequip();
// }
// }
void Player::deletePlayer(){ //TODO: is it standard to have such a member?
delete player;
player = 0;
}
/*C Wrapper functions*/
int** csetSight(Character* p, int height, int width){
return static_cast<Player*>(p)->setSight(height, width);//TODO: check if type is player
}
void cfreeSight(Character* p, int height){
return static_cast<Player*>(p)->freeSight(height);
}
/**
* TODO readings:
* Look into why protected members don't work in the initization lists
*
*/