File: MultiPlayerSelect.cpp

package info (click to toggle)
csmash 0.6.6-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,548 kB
  • ctags: 1,687
  • sloc: cpp: 19,531; sh: 3,515; makefile: 439; ansic: 120; sed: 16
file content (247 lines) | stat: -rw-r--r-- 5,793 bytes parent folder | download | duplicates (6)
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
/* $Id: MultiPlayerSelect.cpp,v 1.8 2003/11/19 17:02:25 nan Exp $ */

// Copyright (C) 2003  神南 吉宏(Kanna Yoshihiro)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include "ttinc.h"
#include "MultiPlayerSelect.h"
#include "BaseView.h"
#include "Player.h"
#include "Sound.h"
#include "PracticeSelectView.h"
#include "Event.h"
#include "MultiPlay.h"
#include "Network.h"
#include "RCFile.h"

#ifdef LOGGING
#include "Logging.h"
#endif

extern long mode;

extern bool isComm;

extern long wins;

extern RCFile *theRC;
extern SDL_mutex *networkMutex;
extern int theSocket;

MultiPlayerSelect::MultiPlayerSelect() {
  m_rotate = 0; m_opponentRotate = 0;
  m_View = NULL;
  m_selected = 0; m_opponentSelected = 0;

  m_lastRotate = m_lastOpponentRotate = 0;

  m_isConnected = false;
}

MultiPlayerSelect::~MultiPlayerSelect() {
}

bool
MultiPlayerSelect::Init() {
  m_View = (PracticeSelectView *)View::CreateView( VIEW_PRACTICESELECT );

  m_View->Init( this );

  BaseView::TheView()->AddView( m_View );

  networkMutex = SDL_CreateMutex();
  SDL_CreateThread( MultiPlayerSelect::Connect, NULL );

  return true;
}

void
MultiPlayerSelect::Create() {
  Control::ClearControl();

  m_theControl = new MultiPlayerSelect();
  m_theControl->Init();

  SDL_ShowCursor(0);
}

bool
MultiPlayerSelect::Move( SDL_keysym *KeyHistory, long *MouseXHistory,
			 long *MouseYHistory, unsigned long *MouseBHistory,
			 int Histptr ) {
  if ( m_selected > 500 && m_opponentSelected > 500 ) {
    mode = MODE_MULTIPLAY;
    return true;
  }

  if ( MouseBHistory[Histptr]&BUTTON_LEFT && 
       ( (Histptr > 0 && !(MouseBHistory[Histptr-1]&BUTTON_LEFT)) ||
	 (Histptr == 0 && !(MouseBHistory[MAX_HISTORY]&BUTTON_LEFT)) ) ) {
    if ( m_selected == 0 ) {
      m_selected = 1;
      Sound::TheSound()->Play( SOUND_CLICK, 0, 0 );
      SendPT(1);
    }
  }

  if ( m_selected > 0 ) {
    m_selected++;
  } else {
    if ( m_lastRotate == 0 ) {
      if ( MouseXHistory[Histptr] - BaseView::GetWinWidth()/2 > 10 ) {
	m_lastRotate = 2;
      } else if ( MouseXHistory[Histptr] - BaseView::GetWinWidth()/2 < -10 ) {
	m_lastRotate = -2;
      }

      if ( m_lastRotate != 0 ) {
	m_rotate += m_lastRotate;
	if ( m_rotate < 0 )
	  m_rotate += 360;
	else
	  m_rotate %= 360;
	SendPT(0);
      }
    } else {
      long nextRotate = m_rotate + m_lastRotate;

      if ( nextRotate < 0 )
	nextRotate += 360;
      else
	nextRotate %= 360;

      if ( (m_rotate)/(360/PLAYERS) != nextRotate/(360/PLAYERS) ) {
	m_rotate = (nextRotate+360/PLAYERS/2)/(360/PLAYERS)*(360/PLAYERS);
	m_lastRotate = 0;
	Sound::TheSound()->Play( SOUND_CLICK, 0, 0 );
	SendPT(0);
      } else
	m_rotate = nextRotate;
    }
  }

  if ( m_opponentSelected > 0 ) {
    m_opponentSelected++;
  } else {
    if ( m_lastOpponentRotate != 0 ) {
      long nextRotate = m_opponentRotate + m_lastOpponentRotate;

      if ( nextRotate < 0 )
	nextRotate += 360;
      else
	nextRotate %= 360;

      if ( (m_opponentRotate)/(360/PLAYERS) != nextRotate/(360/PLAYERS) ) {
	m_opponentRotate = (nextRotate+360/PLAYERS/2)/(360/PLAYERS)*(360/PLAYERS);
	m_lastOpponentRotate = 0;
      } else
	m_opponentRotate = nextRotate;
    }
  }

  return true;
}

long
MultiPlayerSelect::GetOpponentNum() {
  if ( GetOpponentRotate() < 0 )
    return (360+(GetOpponentRotate()%360))/(360/PLAYERS);
  else
    return (GetOpponentRotate()%360)/(360/PLAYERS);
}

// Make connection with opponent
int
MultiPlayerSelect::Connect( void *dum ) {
  long side;

  if ( !(theRC->serverName[0]) )
    side = 1;	// server side
  else
    side = -1;	// client side

  if ( side == 1 ) {
    try {
      WaitForClient();
    } catch ( NetworkError ) {
      xerror("%s(%d) WaitForClient", __FILE__, __LINE__);
      exit(1);
    }
    ServerAdjustClock();
  } else {
    try {
      ConnectToServer();
    } catch ( NetworkError ) {
      xerror("%s(%d) ConnectToServer", __FILE__, __LINE__);
      exit(1);
    }

    ClientAdjustClock();
  }

  ((MultiPlayerSelect *)m_theControl)->m_isConnected = true;

  if ( ((MultiPlayerSelect *)m_theControl)->m_selected > 0 )
    ((MultiPlayerSelect *)m_theControl)->SendPT(1);
  else
    ((MultiPlayerSelect *)m_theControl)->SendPT(0);

  SDL_CreateThread( MultiPlay::WaitForData, NULL );

  return 0;
}

void
MultiPlayerSelect::ReadPT( char *data ) {
  long rotate;

  // get player type
  ReadLong( &(data[1]), rotate );

  m_lastOpponentRotate = rotate%(360/PLAYERS);

  while ( m_lastOpponentRotate > 10 )
    m_lastOpponentRotate -= (360/PLAYERS);

  while ( m_lastOpponentRotate < -10 )
    m_lastOpponentRotate += (360/PLAYERS);

  m_opponentRotate = rotate;

  if ( data[0] != 0 )
    m_opponentSelected = 1;
}

void
MultiPlayerSelect::SendPT( char fixed ) {
  long rotate;

  if ( theSocket < 0 || !m_isConnected )
    return;

  send( theSocket, "PT", 2, 0 );
  send( theSocket, &fixed, 1, 0 );
  rotate = GetRotate();
  SendLong( theSocket, rotate );

#ifdef LOGGING
  char buf[256];

  sprintf( buf, "fixed=%d rotate=%d\n", fixed, rotate );
  Logging::GetLogging()->LogTime( LOG_COMMISC );
  Logging::GetLogging()->Log( LOG_COMMISC, buf );
#endif
}