forked from bebjorn/hockey.server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHockeyGame.cpp
165 lines (138 loc) · 4.4 KB
/
HockeyGame.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
#include "HockeyGame.h"
#include "PracticalSocket.h"
#include "TeamConnection.h"
#include <iostream>
#include "GameState.h"
#include "SerialConnection.h"
#include "Gametime.h"
using namespace std;
bool HockeyGame::setUpConnections(){//upprättar anslutning AI-moduler
if(homeTeam==NULL||awayTeam==NULL){
listeningSocket = new UDPSocket(PORT);//objekt för udp-kommunikation
Connection homeSource=Connection();//objekt som innehåller information om anslutningar, se TeamConnection.cpp
Connection awaySource=Connection();
char* buf[BUFLENGTH];//skapar en buffert i minnet att lägga motagna meddelanden
int handshake[]={1};//skapar ett handskaknings meddelande
cout<<"waiting at homeplayer"<<endl;
listeningSocket->recvFrom(buf,BUFLENGTH,homeSource.adress,homeSource.port);//Väntar på handskakning från AI-modul
homeTeam = new Team(homeSource);//Sparar AImodulens plats
homeTeam->send(handshake,1);//Skickar hanskakning
cout<<"home aquired at port "<<homeSource.port<<endl;
cout<<"waiting at awayplayer"<<endl;
handshake[0]=2;//ny hanskakning för andra laget
listeningSocket->recvFrom(buf,BUFLENGTH,awaySource.adress,awaySource.port);
awayTeam = new Team(awaySource);
awayTeam->send(handshake,4);
cout<<"away aquired at port "<<awaySource.port<<endl;
if(homeTeam->fromSource(awaySource)){//kollar att int samma AI har anslutit igen, om så ge fel
cout<<"team already aquired, next team must be on another port"<<endl;
return false;
}
recieverThreadHandle=(HANDLE)_beginthreadex(NULL,0,recieverThread,(void*)listeningSocket,CREATE_SUSPENDED,NULL);//startar tråden pausad, se teamConnections.cpp
clientsAliveThreadHandle = (HANDLE)_beginthreadex(NULL, 0, checkClientsProc, (void *)this, CREATE_SUSPENDED, NULL);
}
return true;
}
bool HockeyGame::setUpGamestate(){
if(!initializeMicroControllers()){
return false;
}
senderThreadHandle=(HANDLE)_beginthreadex(NULL,0,senderThread,NULL,CREATE_SUSPENDED,NULL);//skapar tråden pausad
return true;
}
bool HockeyGame::setUpCamera(){
if(!initializeTracking()){ //startar bildhantering, se gamestate.cpp, definierar track_puck
return false;
}
cameraThreadHandle=(HANDLE)_beginthreadex(NULL,0,cameraThread,NULL,CREATE_SUSPENDED,NULL);//skapar kameratråden pausad
return true;
}
bool HockeyGame::initializeGame(){
//creates the gamethreads suspended
if(!running){
running=true;
if(!setUpCamera()){//avbryter starten om inte kameran kan startas
running= false;
return running;
}
if(!setUpGamestate()){
running= false;
return running;
}
if(!setUpConnections()){
running= false;
return running;
}
paused=true;
cout<<"successfully initialized game: starting gamethreads"<<endl;
startGametime();//definieras i Gametime.cpp
//starts the threads
resumeGame();
}else{
cout<<"can't initialize: game already running"<<endl;
}
return running;
}
void safeTerminateThread(HANDLE threadHandle) {
if (threadHandle != NULL)
TerminateThread(threadHandle, 0);
}
void HockeyGame::stopGame(){//avslutar spelet
if(running){
safeTerminateThread(senderThreadHandle);
safeTerminateThread(recieverThreadHandle);
safeTerminateThread(cameraThreadHandle);
// TODO: Make/find delete and null-function
if (homeTeam != NULL) {
delete homeTeam;
homeTeam = NULL;
}
if (awayTeam != NULL) {
delete awayTeam;
awayTeam = NULL;
}
if (listeningSocket != NULL) {
delete listeningSocket;
listeningSocket = NULL;
}
running=false;
cout << "stopped" << endl;
safeTerminateThread(clientsAliveThreadHandle);
}else{
cout<<"can't stop game: game isn't running"<<endl;
}
}
void safeSuspendThread(HANDLE handle) {
if (handle != NULL)
SuspendThread(handle);
}
void HockeyGame::pauseGame(){//pausa spelet
if(running&&!paused){
paused=true;
pauseGametime();
safeSuspendThread(senderThreadHandle);
safeSuspendThread(recieverThreadHandle);
safeSuspendThread(cameraThreadHandle);
safeSuspendThread(clientsAliveThreadHandle);
cout << "paused" << endl;
}else{
cout<<"can't pause game"<<endl;
}
}
void safeResumeThread(HANDLE handle) {
if (handle != NULL)
ResumeThread(handle);
}
void HockeyGame::resumeGame(){
if(running&&paused){
paused=false;
resumeGametime();
safeResumeThread(senderThreadHandle);
safeResumeThread(recieverThreadHandle);
safeResumeThread(cameraThreadHandle);
safeResumeThread(clientsAliveThreadHandle);
cout << "resumed" << endl;
}else{
cout<<"can't resume game"<<endl;
}
}