1 /* $Id: PlayGame.cpp,v 1.19 2003/08/03 10:10:18 nan Exp $ */
2 
3 // Copyright (C) 2000, 2002  $B?@Fn(B $B5H9((B(Kanna Yoshihiro)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 #include "ttinc.h"
20 #include "PlayGame.h"
21 #include "Player.h"
22 #include "Ball.h"
23 #include "BaseView.h"
24 #include "Sound.h"
25 #include "RCFile.h"
26 
27 extern RCFile *theRC;
28 
29 extern Ball theBall;
30 
31 extern long mode;
32 extern long wins;
33 
PlayGame()34 PlayGame::PlayGame() {
35   m_View = NULL;
36 
37   m_Score1 = 0;
38   m_Score2 = 0;
39   m_Game1 = 0;
40   m_Game2 = 0;
41 
42   m_pause = false;
43 }
44 
~PlayGame()45 PlayGame::~PlayGame() {
46   if ( m_View ){
47     BaseView::TheView()->RemoveView( m_View );
48     delete m_View;
49   }
50 }
51 
52 long
GetService()53 PlayGame::GetService() {
54   long ret = 0;
55   switch ( theRC->gameMode ) {
56   case GAME_5PTS:
57     ret = ((m_Score1+m_Score2) & 1 ? -1 : 1);
58     break;
59   case GAME_11PTS:
60     if ( m_Score1 > 9 && m_Score2 > 9 ) {	// Deuce
61       ret = ((m_Score1+m_Score2) & 1 ? -1 : 1);
62     } else {
63       if ( (m_Score1 + m_Score2)%4 >= 2 )
64 	ret = -1;
65       else
66 	ret = 1;
67     }
68     break;
69   case GAME_21PTS:
70     if ( m_Score1 > 19 && m_Score2 > 19 ) {	// Deuce
71       ret = ((m_Score1+m_Score2) & 1 ? -1 : 1);
72     } else {
73       if ( (m_Score1 + m_Score2)%10 >= 5 )
74 	ret = -1;
75       else
76 	ret = 1;
77     }
78   }
79 
80   if ( (m_Game1+m_Game2)%2 )
81     ret = -ret;
82 
83   return ret;
84 }
85 
86 long
GetScore(Player * p)87 PlayGame::GetScore( Player *p ) {
88   if ( mode == MODE_SOLOPLAY || mode == MODE_MULTIPLAY ||
89        mode == MODE_PRACTICE ) {
90     if ( p->GetSide() > 0 )
91       return m_Score1;
92     else
93       return m_Score2;
94   } else {	// Training
95     return m_Score1;
96   }
97 }
98 
99 long
GetScore(long side)100 PlayGame::GetScore( long side ) {
101   if ( mode == MODE_SOLOPLAY || mode == MODE_MULTIPLAY ||
102        mode == MODE_PRACTICE) {
103     if ( side > 0 )
104       return m_Score1;
105     else
106       return m_Score2;
107   } else {	// Training
108     return m_Score1;
109   }
110 }
111 
112 long
GetGame(Player * p)113 PlayGame::GetGame( Player *p ) {
114   if ( mode == MODE_SOLOPLAY || mode == MODE_MULTIPLAY ||
115        mode == MODE_PRACTICE ) {
116     if ( p->GetSide() > 0 )
117       return m_Game1;
118     else
119       return m_Game2;
120   } else {	// Training
121     return m_Game1;
122   }
123 }
124 
125 long
GetGame(long side)126 PlayGame::GetGame( long side ) {
127   if ( mode == MODE_SOLOPLAY || mode == MODE_MULTIPLAY ||
128        mode == MODE_PRACTICE) {
129     if ( side > 0 )
130       return m_Game1;
131     else
132       return m_Game2;
133   } else {	// Training
134     return m_Game1;
135   }
136 }
137 
138 void
ChangeScore()139 PlayGame::ChangeScore() {
140   if ( mode == MODE_SOLOPLAY || mode == MODE_MULTIPLAY ||
141        mode == MODE_PRACTICE) {
142     if ( theBall.GetStatus() == 0 || theBall.GetStatus() == 3 ||
143 	 theBall.GetStatus() == 4 || theBall.GetStatus() == 6 ) {
144       m_Score2++;
145     } else {
146       m_Score1++;
147     }
148   }
149 
150   Sound::TheSound()->PlayScore( m_Score1, m_Score2 );
151 
152   if ( theRC->gmode == GMODE_SIMPLE )
153     printf( _("You : %d -  %d : Opponent\n"), GetScore(m_thePlayer),
154 	    GetScore(m_comPlayer) );
155 }
156 
157 void
ChangeScore(long score1,long score2)158 PlayGame::ChangeScore( long score1, long score2 ) {
159   m_Score1 = score1;
160   m_Score2 = score2;
161 }
162 
163 bool
IsGameEnd()164 PlayGame::IsGameEnd() {
165   switch ( theRC->gameMode ) {
166   case GAME_5PTS:
167     if ( (m_Score1 > 4 || m_Score2 > 4) )
168       return true;
169     else
170       return false;
171   case GAME_11PTS:
172     if ( (m_Score1 > 10 || m_Score2 > 10) && abs( m_Score1-m_Score2 ) > 1 )
173       return true;
174     else
175       return false;
176   case GAME_21PTS:
177     if ( (m_Score1 > 20 || m_Score2 > 20) && abs( m_Score1-m_Score2 ) > 1 )
178       return true;
179     else
180       return false;
181   }
182 
183   return false;
184 }
185 
186 void
EndGame()187 PlayGame::EndGame() {
188   // Re-initialize
189   if ( GetScore(m_thePlayer) > GetScore(m_comPlayer) )
190     wins++;
191   else
192     wins = 0;
193 
194   if ( wins > 0 )
195     mode = MODE_SELECT;
196   else
197     mode = MODE_TITLE;
198 
199   m_Score1 = m_Score2 = 0;
200 }
201 
202 void
SetPause(bool pause)203 PlayGame::SetPause( bool pause ) {
204   m_pause = pause;
205 }
206