-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
179 lines (156 loc) · 4.34 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
#include "Lib.h"
#include "Player.h"
#include "MoveEvent.h"
#include "DiscardEvent.h"
#include "CardType.h"
#include "WeaponCard.h"
#include "NeedUseEvent.h"
#include "NeedYieldEvent.h"
Player::Player(ClientPlayer *c,Role r):role(r),alive(true),maxHp(4),hp(maxHp),
hand("hand",this,false),equipArea("equipArea",this),judgeArea("judgeArea",this),
onGeneral("onGeneral",this),byGeneral("byGeneral",this)
//TODO: should need General
{
client=c;
}
string Player::toString()
{
for(int i=0;i<game->nPlayer;i++) if(game->players[i]==this)
{
ostringstream s;
s<<"Player "<<i;
return s.str();
}
return "";
//TODO: should return General name
}
void Player::inform(string message)
{
client->inform(message);
}
int Player::getChoice(string reason, vector<string> choices)
{
return client->getChoice(reason, choices);
}
vector<int> Player::getMultiChoice(string reason, const vector<string> &choices, int max)
{
return getMultiChoice(reason, choices, max, vector<bool>(choices.size(),false));
}
vector<int> Player::getMultiChoice(string reason, const vector<string> &choices, int max, const vector<bool> &mandatory)
{
return client->getMultiChoice(reason, choices, max, mandatory);
}
Card* Player::chooseCard(string reason, const vector<Card*> &cards, string cancelString)
{
vector<string> choices;
vector<int> results;
int i;
string choice;
for(i=0;i<cards.size();i++) choices.push_back(cards[i]->toString(this));
if(!cancelString.empty()) choices.push_back("cancel@"+cancelString);
i=getChoice("card:"+reason,choices);
if(i<cards.size())
{
choice=choices[i];
for(int i=0;i<cards.size();i++) if(choices[i]==choice) results.push_back(i);
return cards[results[rand()%results.size()]];
}
return NULL;
}
int Player::getCurrentHp()
{
return hp>0?int(hp):0;
}
int Player::getLostHp()
{
return maxHp-getCurrentHp();
}
int Player::getMaxHandSize()
{
return getCurrentHp();
}
Card *Player::getEquip(EquipType et)
{
for(Card *c=equipArea.next;c!=&equipArea;c=c->next)
{
EquipCard *e=dynamic_cast<EquipCard*>(c->getCardInfo().name);
if(e->equipType==et) return c;
}
return NULL;
}
EquipCard *Player::getEquipName(EquipType et)
{
for(Card *c=equipArea.next;c!=&equipArea;c=c->next)
{
EquipCard *e=dynamic_cast<EquipCard*>(c->getCardInfo().name);
if(e->equipType==et) return e;
}
return NULL;
}
Player* Player::getNextPlayer()
{
Player* res=nextPlayer;
while(!res->alive) res=res->nextPlayer;
return res;
}
int Player::getDistanceTo(Player *p)
{
if(!alive||!p->alive) return -1;
if(p==this) return 0;
int dist1=0,dist2=0;
Player *t;
for(t=this;t!=p;t=t->getNextPlayer()) dist1++;
for(t=p;t!=this;t=t->getNextPlayer()) dist2++;
int res=dist1<dist2?dist1:dist2;
pair<Player*,Player*> data=make_pair(this,p);
game->applyStatics(distanceValue,&res,&data);
if(res<1) return 1;
return res;
}
int Player::getRange()
{
WeaponCard *w=dynamic_cast<WeaponCard*>(getEquipName(Weapon));
if(w) return w->range;
return 1;//TODO: Skills like 吴六剑
}
bool Player::isMale()
{
return toString()[7]%2;//test
}
void Player::draw(int n)
{
if(!alive) return;
vector<Card*> cards=game->getTopCards(n);
vector<MoveStruct*> d;
for(int i=0;i<n;i++) d.push_back(new MoveStruct(cards[i],&hand));
(new MoveEvent(d))->happen();
}
bool Player::canUse(Card *c,Timing t,Event *e)
{
return canUseAs(vector<Card*>(1,c),c->getCardInfo(),t,e);
}
bool Player::canYield(Card *c, Event *e)
{
return canYieldAs(vector<Card*>(1,c),c->getCardInfo(),e);
}
bool Player::canUseAs(vector<Card*> c,CardInfo i,Timing t,Event *e)
{
bool res;
if(NeedUseEvent *need=dynamic_cast<NeedUseEvent*>(e)) res=!need->fulfilled&&need->player==this&&need->filter->filter(i);
else res=i.name->legalToUse(t,e,this);
return res;//TODO: Skills like 鸡肋
}
bool Player::canYieldAs(vector<Card*> c,CardInfo i,Event *e)
{
bool res=true;
if(NeedYieldEvent *need=dynamic_cast<NeedYieldEvent*>(e)) res=!need->fulfilled&&need->player==this&&need->filter->filter(i);
return res;//TODO: Skills like 鸡肋
}
bool Player::canDiscard(Card *c)
{
return c&&alive;//TODO: Skills like 鸡肋
}
void Player::discard(vector<Card*> cards)
{
(new DiscardEvent(this,cards))->happen();
}