-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclanmember.cpp
More file actions
190 lines (171 loc) · 5.57 KB
/
clanmember.cpp
File metadata and controls
190 lines (171 loc) · 5.57 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
#include "clanmember.h"
#include "pathfinder.h"
#include "robot.h"
#include "clan.h"
#include "warrior.h"
unsigned ClanMember::_Count = 0;
ClanMember::ClanMember(Position current, Position objectif,Planet* inPlanet,Alliance inAlliance) : _current(current), _objectif(objectif),_planet(inPlanet),_alliance(inAlliance)
{
_id=++_Count;
_vector = Position(0,0);
}
ClanMember* ClanMember::Create(Position current, Position objectif, Member_type type,Planet * inPlanet,Alliance inAlliance)
{
//Factory
switch(type)
{
case Member_type(0) :
return new Warrior(current,objectif,inPlanet,inAlliance);
case Member_type(1) :
if(!inPlanet->allResourceBusy())
return new Pathfinder(current,objectif,inPlanet,inAlliance);
else
{
if(genrand_int32()%100 < PROBA_NAISS_MERGE)
return new Pathfinder(current,objectif,inPlanet,inAlliance,true);
else
return new Pathfinder(current,objectif,inPlanet,inAlliance);
}
case Member_type(2) :
return new Robot(current,objectif,inPlanet,inAlliance);
default :
return NULL;
}
}
void ClanMember::moveVector()
{
if(abs(_vector.x) != _cpt.x)
{
++_cpt.x;
if(_vector.x != 0)
_current.x+=_vector.x/abs(_vector.x);
if(_current.x < 0)
_current.x = _current.x+HAUTEUR;
else
_current.x = _current.x%HAUTEUR;
}
else
{
++_cpt.y;
if(_vector.y !=0)
_current.y+=_vector.y/(abs(_vector.y));
if(_current.y < 0)
_current.y = _current.y+LARGEUR;
else
_current.y = _current.y%LARGEUR;
if(_cpt.y == abs(_vector.y)) _cpt=Position(0,0);
}
if(_current == _objectif) _vector=Position(0,0);
}
void ClanMember::movePosition()
{
if(_objectif != _current)
{
int x,y;
x = (abs(HAUTEUR-_objectif.x+_current.x) < abs(_objectif.x-_current.x))?HAUTEUR-_objectif.x+_current.x:_objectif.x-_current.x;
if(x != 0)
{
_current.x += (x < 0)?-1:1;
if(_current.x < 0)
_current.x += HAUTEUR;
else
_current.x = _current.x%HAUTEUR;
}
else
{
y = (abs(LARGEUR-_objectif.y+_current.y) < abs(_objectif.y-_current.y))?LARGEUR-_objectif.y+_current.y:_objectif.y-_current.y;
_current.y += (y < 0)?-1:1;
if(_current.y < 0)
_current.y += LARGEUR;
else
_current.y = _current.y%LARGEUR;
}
}
}
void ClanMember::receiveShot(int inShotValue)
{
_nbLife-=inShotValue;
_planet->getFightingMap()[_current.x][_current.y]=true;
if(_nbLife <= 0) //destruction du ClanMember
{
foreach(ClanMember* members, _shootersList)
members->setVise(NULL);
_planet->getClan(_alliance)->removeMember(_id);
}
}
void ClanMember::getNearestVise(int view)
{
bool finded = false;
if(!finded)
{
/// On cherche l'ennemie
int x,y,tmp;
for(int i = _current.x - view; i < view + _current.x && !finded ; ++i)
{
for(int j = _current.y - view; j < view + _current.y && !finded ; ++j)
{
x = i-_current.x;
y = j-_current.y;
tmp = x*x+y*y;
if( tmp < view*view )
{
if(i < 0)
x = i + HAUTEUR;
else
x = i % HAUTEUR;
if(j < 0)
y = j + LARGEUR;
else
y = j % LARGEUR;
unsigned adv;
if(_alliance == JEDI)
adv = _planet->getMap()[x][y]->sith.nbEclaireur +
_planet->getMap()[x][y]->sith.nbGuerrier +
_planet->getMap()[x][y]->sith.nbGuerrier;
else
adv = _planet->getMap()[x][y]->jedi.nbEclaireur +
_planet->getMap()[x][y]->jedi.nbGuerrier +
_planet->getMap()[x][y]->jedi.nbGuerrier;
if(adv > 0)
{
if(_alliance == JEDI)
{
_vise = _planet->getMember(Position(x,y),1);
}
else
{
_vise = _planet->getMember(Position(x,y),0);
}
if(_vise != NULL)
{
finded = true;
_vise->getShootersList().push_back(this);
}
}
}
}
}
}
if(!finded)
{
_vise = NULL;
}
}
//renvoie un Warrior disponible pour suivre un pathfinder
Warrior* ClanMember::getFollowerWarrior()
{
Warrior* ret=NULL;
QVector<ClanMember*>::iterator ite= _planet->getClan(_alliance)->getMembers().begin();
for(;ret==NULL && ite!= _planet->getClan(_alliance)->getMembers().end();++ite)
{
if((*ite)->getType() == warrior)
{
Warrior* w=(Warrior*)(*ite);
if(w->getFollowing() == NULL)
{
ret=w;
}
}
}
return ret;
}