-
Notifications
You must be signed in to change notification settings - Fork 1
/
OnlinePlayer.cpp
325 lines (289 loc) · 7.42 KB
/
OnlinePlayer.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <fstream>
#include "common\DirectInput.h"
#include "common\Graphics.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "OnlinePlayer.h"
#include "Board.h"
#include "Piece.h"
#include "StatusText.h"
#include "PlayingOnline.h"
#include "MenuState.h"
#include "Input.h"
#include "resource1.h"
#include <Richedit.h>
#include <Commctrl.h>
#include "Textures.h"
#include "PiecesCaptured.h"
#include "Database.h"
#include "Enums.h"
#include "GUI.h"
#include "Sound.h"
#include <time.h>
void setFont(HWND hwnd, int height, int weight = FW_DONTCARE, string family="Arial");
//! Constructor.
/**
@param color The color of the player. Black or white.
*/
OnlinePlayer::OnlinePlayer(Color color) : Player(color)
{
mCheckMate = false;
mWaitingOnAnswer= false;
mWinner = false;
mDelay = 0;
srand(time(0));
}
//! Destructor
OnlinePlayer::~OnlinePlayer()
{
delete getBoard();
}
//! Sets waiting status and color.
/**
@param type The type - either HOST or CLIENT.
@note Host is white and client black.
@note The host start (white starts).
*/
void OnlinePlayer::init(PeerType type)
{
if(type == HOST)
{
setBoard(new Board(80, false));
mWaitingForOpponent = true;
mWaitingOnMove = false;
setColor(WHITE);
mGui->setStatus("Waiting for opponent!", RED, 300.0f);
}
else if(type == CLIENT)
{
setBoard(new Board(80, true));
mWaitingForOpponent = false;
mWaitingOnMove = true;
setColor(BLACK);
}
}
//! Polls action from player and sends it to the opponent.
/**
@param dt Time since last frame.
*/
void OnlinePlayer::update(float dt)
{
// Is it the players turn?
if(!mWaitingOnMove && !mWaitingForOpponent && !mCheckMate)
{
// Get action from the player
ActionResult action = performAction();
switch(action.result)
{
case PIECE_MOVED: {
// Place them off screen - not needed
setSelected(-10, -10);
setInvalid(-10, -10);
// Remove the opponents last move
setLastMove();
// Send the move to the opponent
// ID - piece.x - piece.y - new.x - new.y
RakNet::BitStream bitstream;
bitstream.Write((unsigned char)ID_PIECE_MOVED);
bitstream.Write(action.from.x);
bitstream.Write(action.from.y);
bitstream.Write(action.position.x);
bitstream.Write(action.position.y);
PlayingOnline::Instance()->getPeer()->Send(&bitstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
// Is now waiting on move
mWaitingOnMove = true;
// Checkmate? NOTE: Important to check after the move is sent to opponent
if(getBoard()->checkMate(Color(getColor()*-1))) {
mGui->setStatus("Check mate!", GREEN, 100.0f);
gGame->drawAll();
mGui->displayCheckMate(true);
mCheckMate = true;
gDatabase->addWin(getName());
}
else
mGui->setStatus("#NOVALUE", RED, 0.0f);
break;
}
// Invalid position
case INVALID_POSITION:
setInvalid(action.position.x, action.position.y);
setSelected(-10, -10);
mGui->setStatus("Invalid position!", RED, 2.0f);
break;
// Wrong color
case WRONG_COLOR:
setInvalid(action.position.x, action.position.y);
if(getColor() == WHITE)
mGui->setStatus("You are white", RED, 2.0f);
else
mGui->setStatus("You are black", RED, 2.0f);
break;
// Gets checked
case GETS_CHECKED:
setInvalid(action.position.x, action.position.y);
setSelected(-10, -10);
mGui->setStatus("Check!", RED, 2.0f);
break;
// Piece was selected
case PIECE_SELECTED:
setSelected(action.position.x, action.position.y);
setInvalid(-10, -10);
break;
}
}
// Promt "Not your turn" if the player tries to move when he's waiting on the other player to move
else if(!mWaitingForOpponent && !mCheckMate && mWaitingOnMove) {
if(gInput->keyPressed(VK_LBUTTON) && gInput->mousePosition().x < 660) {
mGui->setStatus("Not your turn!", RED, 2.0f);
gSound->playEffect(ILLEGAL_SOUND);
}
}
}
//! Draws the board and gui.
void OnlinePlayer::draw()
{
Player::draw();
mGui->draw();
}
//! Called when the opponent moved.
/**
@param from The old position.
@param to The new position.
*/
void OnlinePlayer::opponentMoved(Position from, Position to)
{
// Get the piece perhaps gets captured
Piece* piece = getBoard()->getPieceAt(to.x, to.y);
Piece* movedPiece = getBoard()->getPieceAt(from.x, from.y);
if(piece != NULL) {
mGui->addCapture(piece->getColor(), piece->getType());
getBoard()->removePiece(piece);
pieceCapturedSound();
}
else {
// Castling?
if(movedPiece->getType() == KING && abs(from.x - to.x) == 2)
gSound->playEffect(CASTLE_SOUND);
else
pieceMovedSound();
}
movedPiece->setPos(to.x, to.y);
movedPiece->moved();
// No longer waiting for the opponent to do a move
mWaitingOnMove = false;
// Set the last move position
setLastMove(from, to);
// Checkmate?
if(getBoard()->checkMate(getColor())) {
mGui->setStatus("Check mate!", RED, 100.0f);
gGame->drawAll();
mCheckMate = true;
mGui->displayCheckMate(false);
gDatabase->addLoss(getName());
}
else
mGui->setStatus("Your turn!", GREEN, 300.0f);
}
void OnlinePlayer::handleCastling(Piece* king)
{
// Send the move to the opponent.
RakNet::BitStream bitstream;
bitstream.Write((unsigned char)ID_PIECE_MOVED);
Position from, to;
// Hack...
if(getColor() == WHITE)
{
if(king->getPos().x == 2) {
from = Position(0, 7);
to = Position(3, 7);
}
else if(king->getPos().x == 6) {
from = Position(7, 7);
to = Position(5, 7);
}
}
else if(getColor() == BLACK)
{
if(king->getPos().x == 2) {
from = Position(0, 0);
to = Position(3, 0);
}
else if(king->getPos().x == 6) {
from = Position(7, 0);
to = Position(5, 0);
}
}
// Setup and send the message
bitstream.Write(from.x);
bitstream.Write(from.y);
bitstream.Write(to.x);
bitstream.Write(to.y);
PlayingOnline::Instance()->getPeer()->Send(&bitstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
void OnlinePlayer::handleCapture(Color color, PieceType type)
{
mGui->addCapture(color, type);
pieceCapturedSound();
}
//! Ask the opponent for a rematch.
void OnlinePlayer::requestRematch()
{
// Send rematch request to opponent.
RakNet::BitStream bitstream;
bitstream.Write((unsigned char)ID_REQUEST_REMATCH);
PlayingOnline::Instance()->getPeer()->Send(&bitstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
//! Restart the match.
void OnlinePlayer::restartMatch()
{
// Reset data
mCheckMate = false;
mWaitingOnAnswer = false;
mDelay = 0;
setLastMove();
getBoard()->reset();
if(getColor() == WHITE)
mWaitingOnMove = false;
else
mWaitingOnMove = true;
mGui->setStatus("Nothing", RED, 0.0f);
if(getColor() == WHITE)
mGui->setStatus("Your turn!", GREEN, 300.0f);
mGui->clearCaptures();
mGui->clearStats();
mGui->initStats();
}
//! Sets waiting on opponent or not.
void OnlinePlayer::setWaitingForOpponent(bool waiting)
{
mWaitingForOpponent = waiting;
}
//! Sets waiting on request answer or not.
void OnlinePlayer::setWaitingOnAnswer(bool waiting)
{
mWaitingOnAnswer = waiting;
}
//! Set the gui.
void OnlinePlayer::setGui(GUI* gui)
{
mGui = gui;
}
//! Returns waiting on opponent or not.
bool OnlinePlayer::waitingOnOpponent()
{
return mWaitingForOpponent;
}
//! Returns waiting on request answer or not.
bool OnlinePlayer::waitingOnAnswer()
{
return mWaitingOnAnswer;
}
//! Resturns if check mate or not.
bool OnlinePlayer::getCheckMate()
{
return mCheckMate;
}
bool OnlinePlayer::waitingOnMove()
{
return mWaitingOnMove;
}