-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpet.cpp
More file actions
239 lines (212 loc) · 6.59 KB
/
pet.cpp
File metadata and controls
239 lines (212 loc) · 6.59 KB
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
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "main.h"
#include "item.h"
#include "prop.h"
#include "unit.h"
#include "hero.h"
#include "pet.h"
#include "monster.h"
#include "boss.h"
#include "board.h"
#include "inventory.h"
Pet::Pet() : Unit(PET_SHAPE, PET_ROW, PET_COL, PET_DIR, PET_HP, PET_MAX_HP, PET_MP, PET_MAX_MP, PET_ATK, PET_DEF, PET_GOLD, PET_EXP) {
findBossRange = PET_FIND_RANGE;
stayRange = PET_RANGE;
bossFound = false;
adjacentBoss = false;
}
Pet::Pet(char shape, int row, int col, int dir, int hp, int maxHp, int mp, int maxMp, int atk, int def, int gold, int exp)
: Unit(shape, row, col, dir, hp, maxHp, mp, maxMp, atk, def, gold, exp) {
findBossRange = PET_FIND_RANGE;
stayRange = PET_RANGE;
bossFound = false;
adjacentBoss = false;
}
Pet::~Pet() {
}
bool Pet::isPet() {
return true;
}
void Pet::bodyguard() {
int bossRow = -1;
int bossCol = -1;
Boss *boss = NULL;
bossFound = false;
for (int i = row - findBossRange; i < row + findBossRange && !bossFound; i++) {
for (int j = col - findBossRange; j < col + findBossRange && !bossFound; j++) {
if (board->validate(i, j) &&
board->getUnit(i, j) != NULL &&
board->getUnit(i, j)->isBoss()) {
bossRow = i;
bossCol = j;
boss = (Boss *)board->getUnit(i, j); // 슬픈이야기 3번 case의 2번 방식.
bossFound = true;
}
}
}
if (bossFound) { // found!!
// 그 방향을 찾아서 그 방향을 dir에 set해주면 되고, 만약 붙어 있으면 한대 때린다.
// 일단 같은 수평선상에 있는지 보자.
if (row == boss->getRow()) {
if (col > boss->getCol()) { // left side에 있다.
if (col == boss->getCol()+1) { // adjacent?
boss->decHp(atk);
adjacentBoss = true;
return;
}
else {
dir = DIR_W;
}
}
else { // right side에 있다.
if (col == boss->getCol()-1) { // adjacent?
boss->decHp(atk);
adjacentBoss = true;
return;
}
else {
dir = DIR_E;
}
}
}
// 일단 같은 수직선상에 있는지 보자.
else if (col == boss->getCol()) {
if (row > boss->getRow()) { // up side에 있다.
if (row == boss->getRow()+1) { // adjacent?
boss->decHp(atk);
adjacentBoss = true;
return;
}
else {
dir = DIR_N;
}
}
else { // down side에 있다.
if (row == boss->getRow()-1) { // adjacent?
boss->decHp(atk);
adjacentBoss = true;
return;
}
else {
dir = DIR_S;
}
}
}
// 그 외는 4개의 대각선 블럭에 boss가 들어 있다.
else {
int rowDiff = abs(row - boss->getRow());
int colDiff = abs(col - boss->getCol());
if (row > boss->getRow() && col > boss->getCol()) { // top-left block
if (rowDiff == colDiff) {
dir = (rand() % 2 == 0)?DIR_N:DIR_W;
}
else if (rowDiff > colDiff) {
dir = DIR_N;
}
else {
dir = DIR_W;
}
}
else if (row > boss->getRow() && col < boss->getCol()) { // top-right block
if (rowDiff == colDiff) {
dir = (rand() % 2 == 0)?DIR_N:DIR_E;
}
else if (rowDiff > colDiff) {
dir = DIR_N;
}
else {
dir = DIR_E;
}
}
else if (row < boss->getRow() && col < boss->getCol()) { // bottom-right block
if (rowDiff == colDiff) {
dir = (rand() % 2 == 0)?DIR_S:DIR_E;
}
else if (rowDiff > colDiff) {
dir = DIR_S;
}
else {
dir = DIR_E;
}
}
else { // if (row < boss->getRow() && col > boss->getCol()) { // bottom-left block
if (rowDiff == colDiff) {
dir = (rand() % 2 == 0)?DIR_S:DIR_W;
}
else if (rowDiff > colDiff) {
dir = DIR_S;
}
else {
dir = DIR_W;
}
}
}
}
}
void Pet::move(int dir) {
if (died || frozen) {
return;
}
this->dir = dir;
bodyguard();
if (adjacentBoss) {
adjacentBoss = false;
return;
}
bool moved = false;
int prevRow = row;
int prevCol = col;
int nextRow = row;
int nextCol = col;
if (!bossFound) {
Hero *hero = NULL;
hero = (Hero *)board->getHero();
//nextRow = hero->getRow() + (rand() % (2*stayRange+1) + (-stayRange));
//nextCol = hero->getCol() + (rand() % (2*stayRange+1) + (-stayRange));
nextRow = hero->getRow() -1;
nextCol = hero->getCol() + (rand() % (2*stayRange+1) + (-stayRange));
}
else {
if (this->dir == DIR_N) {
nextRow--;
}
else if (this->dir == DIR_E) {
nextCol++;
}
else if (this->dir == DIR_S) {
nextRow++;
}
else if (this->dir == DIR_W) {
nextCol--;
}
}
if (nextRow >= 0 && nextRow < board->getRowSize() &&
nextCol >= 0 && nextCol < board->getColSize() &&
board->getUnit(nextRow, nextCol) == NULL &&
board->getProp(nextRow, nextCol) == NULL) {
board->setUnit(prevRow, prevCol, NULL);
board->setUnit(nextRow, nextCol, this);
moved = true;
}
}
void Pet::interact(Unit *unit) {
if (unit == NULL) {
cout << "Pet::interact(): error: unit == NULL" << endl;
exit(1);
}
if (isDead()) {
return;
}
decHp(unit->getAtk()); //ADD hero hits mon
if (!isDead()) {
unit->decHp(atk);
}
else {
unit->incGold(gold);
unit->incExp(exp);
decGold(gold);
decGold(exp);
}
}