File: comPenDrive.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 (374 lines) | stat: -rw-r--r-- 9,542 bytes parent folder | download | duplicates (7)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* $Id: comPenDrive.cpp,v 1.21 2003/07/25 17:28:27 nan Exp $ */

// Copyright (C) 2000-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 "comPenDrive.h"
#include "Control.h"
#include "Ball.h"
#include "Player.h"
#include "PlayGame.h"
#include "RCFile.h"

extern RCFile *theRC;

extern Ball   theBall;

ComPenDrive::ComPenDrive() : PenDrive(), ComPlayer() {
}

ComPenDrive::ComPenDrive(long side) : PenDrive(side), ComPlayer() {
}

ComPenDrive::ComPenDrive( long playerType, long side,
			  double x, double y, double z,
			  double vx, double vy, double vz,
			  long status, long swing,
			  long swingType, bool swingSide,
			  long afterSwing, long swingError,
			  double targetX, double targetY,
			  double eyeX, double eyeY, double eyeZ,
			  long pow, double spin, double stamina,
			  long statusMax ) :
  PenDrive( playerType, side, x, y, z, vx, vy, vz, status, swing, swingType,
	    swingSide, afterSwing, swingError, targetX, targetY,
	    eyeX, eyeY, eyeZ, pow, spin, stamina, statusMax ), ComPlayer() {
}

ComPenDrive::~ComPenDrive() {
}

bool
ComPenDrive::Move( SDL_keysym *KeyHistory, long *MouseXHistory,
		 long *MouseYHistory, unsigned long *MouseBHistory,
		 int Histptr ) {
  double prevVx = m_vx;
  double prevVy = m_vy;

  PenDrive::Move( KeyHistory, MouseXHistory, MouseYHistory, MouseBHistory,
		Histptr );

  Think();

// Calc status
  if ( hypot( m_vx-prevVx, m_vy-prevVy ) > 0.8-theRC->gameLevel*0.1 ) {
    AddStatus(-1);
  }

  return true;
}

bool
ComPenDrive::Think() {
  double hitT;	// estimation time until ball reaches _hitX, _hitY
  double mx;

  // If the ball status changes, change _hitX, _hitY
  if ( _prevBallstatus != theBall.GetStatus() && theBall.GetStatus() >= 0 ){
    Hitarea( _hitX, _hitY );

    _prevBallstatus = theBall.GetStatus();
  }

  if ( theBall.GetVY() != 0.0 )
    hitT = (_hitY - theBall.GetY())/theBall.GetVY();
  else
    hitT = -1.0;

  mx = m_x+m_side*0.3;

  if ( m_swing > 10 && m_swing <= 20 ) {
  } else {
    if ( hitT > 0.0 ) {
      double vx = (_hitX-mx)/hitT;
      if ( vx > m_vx+0.1 )
	m_vx += 0.1;
      else if ( vx < m_vx-0.1 )
	m_vx -= 0.1;
      else
	m_vx = vx;
    } else {
      if ( m_vx*fabs(m_vx*0.1)/2 < _hitX - mx )
	m_vx += 0.1;
      else
	m_vx -= 0.1;
    }
  }

  if ( m_swing > 10 && m_swing <= 20 ) {
  } else {
    if ( hitT > 0.0 ) {
      double vy = (_hitY-m_y)/hitT;
      if ( vy > m_vy+0.1 )
	m_vy += 0.1;
      else if ( vy < m_vy-0.1 )
	m_vy -= 0.1;
      else
	m_vy = vy;
    } else {
      if ( m_vy*fabs(m_vy*0.1)/2 < _hitY - m_y )
	m_vy += 0.1;
      else
	m_vy -= 0.1;
    }
  }

  if ( m_swing == 19 ) {
    Player *opponent;
    if ( m_side == -1 )
      opponent = Control::TheControl()->GetThePlayer();
    else
      opponent = Control::TheControl()->GetComPlayer();

    SetTargetX( opponent );
  }

  if ( (theBall.GetStatus() == 0 && m_side == -1) ||
       (theBall.GetStatus() == 1 && m_side == -1) ||
       (theBall.GetStatus() == 2 && m_side == 1) ||
       (theBall.GetStatus() == 3 && m_side == 1) ||
       (theBall.GetStatus() == 4 && m_side == -1) ||
       (theBall.GetStatus() == 5 && m_side == 1) ) {
    if ( m_vx > 5.0 )
      m_vx = 5.0;
    else if ( m_vx < -5.0 )
      m_vx = -5.0;
    if ( m_vy > 5.0 )
      m_vy = 5.0;
    else if ( m_vy < -5.0 )
      m_vy = -5.0;
  } else {
    if ( hypot( m_vx, m_vy ) >= 1.5 ) {
      double v = hypot( m_vx, m_vy );
      m_vx = m_vx/v*1.5;
      m_vy = m_vy/v*1.5;
    }
  }

  // Toss
#ifdef SCREENSHOT
  if ( theBall.GetStatus() == 8 &&
       ( (Control::TheControl()->IsPlaying() &&
	  ((PlayGame *)Control::TheControl())->GetService() == GetSide()) ) &&
       fabs(m_vx) < 0.1 && fabs(m_vy) < 0.1 &&
       fabs(m_x+m_side*0.3-_hitX) < 0.1 && fabs(m_y-_hitY) < 0.1 &&
       m_swing == 0 ){
#else
  if ( theBall.GetStatus() == 8 &&
       ( (Control::TheControl()->IsPlaying() &&
	  ((PlayGame *)Control::TheControl())->GetService() == GetSide()) ||
	 (!Control::TheControl()->IsPlaying() && GetSide() == 1) ) &&
       fabs(m_vx) < 0.1 && fabs(m_vy) < 0.1 &&
       fabs(m_x+m_side*0.3-_hitX) < 0.1 && fabs(m_y-_hitY) < 0.1 &&
       m_swing == 0 ){
#endif
    theBall.Toss( this, 2 );
    StartSwing( 3 );
    m_targetY = TABLELENGTH/6*m_side;

    return true;
  }

  if ( fabs( theBall.GetY()+theBall.GetVY()*0.1 - _hitY ) < 0.2 /*&&
	  m_swing == 0*/ ){
    // Calc the ball location of 0.1 second later. 
    // This part seems to be the same as Swing(). Consider again. 
    Ball *tmpBall;
    double tmpBallX, tmpBallY, tmpBallZ;
    double tmpX, tmpY;

    tmpBall = new Ball( theBall.GetX(), theBall.GetY(), theBall.GetZ(),
			theBall.GetVX(), theBall.GetVY(), theBall.GetVZ(),
			theBall.GetSpin(), theBall.GetStatus() );

    for ( int i = 0 ; i < 9 ; i++ )
      tmpBall->Move();
    tmpX = m_x+m_vx*0.08;
    tmpY = m_y+m_vy*0.08;

    if ( ((tmpBall->GetStatus() == 3 && m_side == 1) ||
	  (tmpBall->GetStatus() == 1 && m_side == -1)) &&
	 (tmpY-tmpBall->GetY())*m_side < 0.3 &&
	 (tmpY-tmpBall->GetY())*m_side > -0.6 &&
	 m_swing <= 10 ) {

      tmpBallX = tmpBall->GetX();
      tmpBallY = tmpBall->GetY();
      tmpBallZ = tmpBall->GetZ();

      // If the ball location becomes better at 1/100 second later, wait. 
      tmpBall->Move();
      if ( fabs(tmpY+m_vy*0.01-tmpBall->GetY()) < fabs(tmpY-tmpBallY) &&
	   fabs(tmpY-tmpBallY) > LEVELMARGIN ) {
	delete tmpBall;
	return true;
      }

      _hitX = tmpBallX;
      _hitY = tmpBallY;

      if ( (tmpBallZ-TABLEHEIGHT)/fabs(tmpBallY - m_targetY) < 0.0 )
	m_targetY = TABLELENGTH/4*m_side;
      else if ( (tmpBallZ-TABLEHEIGHT)/fabs(tmpBallY-m_targetY) < 0.1 )
	m_targetY = TABLELENGTH/3*m_side;
      else
	m_targetY = TABLELENGTH/16*6*m_side;

      if ( (m_x-tmpBallX)*m_side < 0 )
	Swing(3);
      else
	Swing(1);

      m_pow = 7 + theRC->gameLevel;
    }
    delete tmpBall;
  }

  return true;
}

// Calc hit point
// (1) If the ball haven't bound, calc bound point
// (2) Calc hit point from current ball location or bound location
bool
ComPenDrive::Hitarea( double &hitX, double &hitY ) {
  double max = -1.0;             /* highest point of the ball */
  double maxX = 0.0, maxY = 0.0;

  if ( (theBall.GetStatus() == 3 && m_side == 1) ||
       (theBall.GetStatus() == 2 && m_side == 1) ||
       (theBall.GetStatus() == 0 && m_side == -1) ||
       (theBall.GetStatus() == 1 && m_side == -1) ||
       (theBall.GetStatus() == 4 && m_side == -1) ||
       (theBall.GetStatus() == 5 && m_side == 1) ) {
    max = GetBallTop( maxX, maxY, this );

    if ( max > 0 ) {
      hitX = maxX;
      hitY = maxY;
    }
  } else if ( theBall.GetStatus() == 8 ) {
    if ( (Control::TheControl()->IsPlaying() &&
	  ((PlayGame *)Control::TheControl())->GetService() == GetSide()) ||
	 GetSide() == 1 ) {
      if ( RAND(2) )
	hitX = m_targetX;
      else
	hitX = -m_targetX;
    } else
      hitX = 0.0;
    hitY = -(TABLELENGTH/2+0.2)*m_side;
  } else if ( theBall.GetStatus() < 6 ) {
    hitX = 0.0;
    hitY = -(TABLELENGTH/2+1.0)*m_side;
  }

  return true;
}

bool
ComPenDrive::SetTargetX( Player *opponent ) {
  double width;

  switch ( theRC->gameLevel ) {
  case LEVEL_EASY:
    width = TABLEWIDTH/4;
    break;
  case LEVEL_NORMAL:
    width = TABLEWIDTH/2;
    break;
  case LEVEL_HARD:
  case LEVEL_TSUBORISH:
    width = TABLEWIDTH;
    break;
  default:
    return false;
  }

  if ( opponent->GetPlayerType() == PLAYER_PENDRIVE ) {
    switch ( RAND(4) ) {
    case 0:
      m_targetX = -width*7/16;
      break;
    case 1:
      m_targetX = -width*5/16;
      break;
    case 2:
      m_targetX = -width*3/16;
      break;
    case 3:
      m_targetX = -width*1/16;
      break;
    }
  } else {
    switch ( RAND(8) ) {
    case 0:
      m_targetX = -width*7/16;
      break;
    case 1:
      m_targetX = width*5/16;
      break;
    case 2:
      m_targetX = -width*3/16;
      break;
    case 3:
      m_targetX = -width*1/16;
      break;
    case 4:
      m_targetX = width*1/16;
      break;
    case 5:
      m_targetX = width*3/16;
      break;
    case 6:
      m_targetX = width*5/16;
      break;
    case 7:
      m_targetX = width*7/16;
      break;
    }
  }

  if ( theRC->gameLevel == LEVEL_TSUBORISH ) {
    if ( opponent->GetX()+opponent->GetVX()*0.5 < 0.0 ) {
      m_targetX = width*7/16;
    } else {
      m_targetX = -width*7/16;
    }

    if ( RAND(4) == 0 ) {
      m_targetX = -m_targetX;
    }
  }

  if ( m_vx > 1.5 ) {
    m_targetX += TABLEWIDTH/2;
  } else if ( m_vx > 0.5 ) {
    m_targetX += TABLEWIDTH/4;
  } else if ( m_vx < -1.5 ) {
    m_targetX -= TABLEWIDTH/2;
  } else if ( m_vx < -0.5 ) {
    m_targetX -= TABLEWIDTH/4;
  }

  if ( m_targetX > TABLEWIDTH/2 )
    m_targetX = TABLEWIDTH*7/16;
  if ( m_targetX < -TABLEWIDTH/2 )
    m_targetX = -TABLEWIDTH*7/16;

  return true;
}