1c74e0ceb18b3a76c8ddbcc53c92ad92aa8f3ec6
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

1) /* Blinker
2)    Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
3)    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4)    a blinkenarea.org project */
5) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

6) #include <cmath>
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

7) #include <stdlib.h>
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

8) #include <string>
9) #include <vector>
10) 
11) #include <BlinkenLib/BlinkenFrame.h>
12) 
13) #include "File.h"
14) #include "Format.h"
15) #include "FormatFile.h"
16) #include "Game.h"
17) #include "Mgrs.h"
18) #include "Module.h"
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

19) #include "OpConn.h"
20) #include "OpConnIf.h"
21) #include "OpReqIf.h"
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

22) #include "OutStreamFile.h"
23) #include "Pong.h"
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

24) #include "Time.h"
25) #include "TimeCallee.h"
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

26) #include "UIntFile.h"
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

27) 
28) namespace Blinker {
29) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

30) /// descriptor for delay value
31) Pong::ValueDescr const Pong::c_delayDescr = { 200, 100, 500 };
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

32) /// descriptor for maximum score value
33) Pong::ValueDescr const Pong::c_maxScoreDescr = { 9, 1, 99 };
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

34) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

35) /// operator connection name suffix for left player's connection
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

36) std::string const Pong::c_opConnSuffixLeft = "/left";
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

37) /// operator connection name suffix for right player's connection
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

38) std::string const Pong::c_opConnSuffixRight = "/right";
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

39) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

40) /**
41)  * @brief constructor
42)  * @param[in] name module name
43)  * @param[in] mgrs managers
44)  * @param[in] dirBase base directory
45)  */
46) Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
47)   Game(name, mgrs, dirBase),
48)   m_fileBallColor(dirBase.getFile("ballColor")),
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

49)   m_fileLineColor(dirBase.getFile("lineColor")),
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

50)   m_filePadColor(dirBase.getFile("padColor")),
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

51)   m_fileComputerColor(dirBase.getFile("computerColor")),
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

52)   m_fileScoreColor(dirBase.getFile("scoreColor")),
53)   m_fileGoalColor(dirBase.getFile("goalColor")),
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

54)   m_fileDelay(dirBase.getFile("delay")),
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

55)   m_fileMaxScore(dirBase.getFile("maxScore")),
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

56)   m_ballColor(), m_lineColor(), m_padColor(), m_computerColor(),
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

57)   m_scoreColor(), m_goalColor(),
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

58)   m_delay(c_delayDescr.default_), m_maxScore(c_maxScoreDescr.default_),
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

59)   m_pConnLeft(NULL), m_pConnRight(NULL),
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

60)   m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0),
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

61)   m_padSize(0), m_leftPosY(0), m_rightPosY(0), m_leftDelay(0), m_rightDelay(0),
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

62)   m_goalDelay(0), m_bounceCnt(0), m_scoreLeft(0), m_scoreRight(0)
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

63) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

64)   // open operator connection interfaces for left and right player
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

65)   m_mgrs.m_opMgr.open(m_name + c_opConnSuffixLeft, this);
66)   m_mgrs.m_opMgr.open(m_name + c_opConnSuffixRight, this);
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

67) }
68) 
69) /// virtual destructor
70) Pong::~Pong()
71) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

72)   // close operator connection interfaces
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

73)   m_mgrs.m_opMgr.close(m_name + c_opConnSuffixLeft);
74)   m_mgrs.m_opMgr.close(m_name + c_opConnSuffixRight);
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

75) 
76)   // close open operator connections
77)   if (m_pConnLeft) {
78)     m_pConnLeft->close();
79)     m_pConnLeft = NULL;
80)   }
81)   if (m_pConnRight) {
82)     m_pConnRight->close();
83)     m_pConnRight = NULL;
84)   }
85) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

86)   // cancel time callback request
87)   m_mgrs.m_callMgr.cancelTimeCall(this);
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

88) }
89) 
90) /// check for update of configuration (derived game), return true on update
91) bool Pong::updateConfigGame()
92) {
93)   bool ret = false;
94) 
95)   // color file was modified -> convert color, return true for update
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

96)   if (colorUpdate(m_fileBallColor, m_ballColor) ||
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

97)       colorUpdate(m_fileLineColor, m_lineColor) ||
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

98)       colorUpdate(m_filePadColor, m_padColor) ||
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

99)       colorUpdate(m_fileComputerColor, m_computerColor) ||
100)       colorUpdate(m_fileScoreColor, m_scoreColor) ||
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

101)       colorUpdate(m_fileGoalColor, m_goalColor) ||
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

102)       valueUpdate(m_fileDelay, c_delayDescr, m_delay) ||
103)       valueUpdate(m_fileMaxScore, c_maxScoreDescr, m_maxScore)) {
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

104)     ret = true;
105)   }
106) 
107)   return ret;
108) }
109) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

110) /**
111)  * @brief check if accepting new operator connction is possible
112)  * @param[in] name operator interface name
113)  * @return if accepting new connection is possible
114)  */
115) bool Pong::acceptNewOpConn(const std::string &name)
116) {
117)   // left player can join if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

118)   if (name == m_name + c_opConnSuffixLeft && ! m_pConnLeft) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

119)     return true;
120)   }
121)   // right player can join if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

122)   if (name == m_name + c_opConnSuffixRight && ! m_pConnRight) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

123)     return true;
124)   }
125)   // default: reject connection
126)   return false;
127) }
128) 
129) /**
130)  * @brief new operator connection
131)  * @param[in] name operator interface name
132)  * @param[in] pConn operator connection object
133)  *
134)  * The new connection may not yet be used for sending inside this callback.
135)  */
136) void Pong::newOpConn(const std::string &name, OpConn *pConn)
137) {
138)   // left player joins if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

139)   if (name == m_name + c_opConnSuffixLeft && ! m_pConnLeft) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

140)     m_pConnLeft = pConn;
141)   }
142)   // right player joins if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

143)   else if (name == m_name + c_opConnSuffixRight && ! m_pConnRight) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

144)     m_pConnRight = pConn;
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

145)   }
146)   // nothing happens
147)   else {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

148)     return;
149)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

150) 
151)   // already active
152)   if (isActive()) {
153)     redraw(); // player color is different for phone / computer
154)   }
155)   // first player joined
156)   else {
157)     activate();
158)   }
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

159) }
160) 
161) /**
162)  * @brief key command received on operator connection
163)  * @param[in] pConn operator connection object
164)  * @param[in] key key that was pressed
165)  */
166) void Pong::opConnRecvKey(OpConn *pConn, char key)
167) {
168)   // left player
169)   if (pConn == m_pConnLeft) {
170)     processKey(key, m_leftPosY);
171)   }
172)   // right player
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

173)   else if (pConn == m_pConnRight) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

174)     processKey(key, m_rightPosY);
175)   }
176) }
177) 
178) /**
179)  * @brief play command received on operator connection
180)  * @param[in] pConn operator connection object
181)  * @param[in] sound name of sound to play
182)  */
183) void Pong::opConnRecvPlay(OpConn *pConn, const std::string &sound)
184) {
185)   (void)pConn;
186)   (void)sound;
187) }
188) 
189) /**
190)  * @brief operator connection is closed
191)  * @param[in] pConn operator connection object
192)  *
193)  * The connection may not be used for sending any more in this callback.
194)  */
195) void Pong::opConnClose(OpConn *pConn)
196) {
197)   // left player leaves
198)   if (pConn == m_pConnLeft) {
199)     m_pConnLeft = NULL;
200)   }
201)   // right player leaves
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

202)   else if (pConn == m_pConnRight) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

203)     m_pConnRight = NULL;
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

204)   }
205)   // nothing happens
206)   else {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

207)     return;
208)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

209) 
210)   // still a phone player there
211)   if (m_pConnLeft || m_pConnRight) {
212)     redraw(); // player color is different for phone / computer
213)   }
214)   // no phone players left
215)   else {
216)     deactivate();
217)   }
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

218) }
219) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

220) /// re-initialize game (e.g. due to config change)
221) void Pong::reinitialize()
222) {
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

223)   // compute parameters
224)   m_padSize = (m_height + 1) / 3;
225)   m_leftPosY = (m_height - m_padSize) / 2;
226)   m_rightPosY = (m_height - m_padSize) / 2;
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

227)   m_leftDelay = 0;
228)   m_rightDelay = 0;
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

229) 
230)   // convert colors
231)   color2data(m_fileBallColor, m_ballColor);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

232)   color2data(m_fileLineColor, m_lineColor);
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

233)   color2data(m_filePadColor, m_padColor);
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

234)   color2data(m_fileComputerColor, m_computerColor);
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

235)   color2data(m_fileScoreColor, m_scoreColor);
236)   color2data(m_fileGoalColor, m_goalColor);
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

237)   // get values
238)   valueFromFile(m_fileDelay, c_delayDescr, m_delay);
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

239)   valueFromFile(m_fileMaxScore, c_maxScoreDescr, m_maxScore);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

240) 
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

241)   // reset scores
242)   m_scoreLeft = 0;
243)   m_scoreRight = 0;
244) 
245)   // start first ball
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

246)   startBall();
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

247) }
248) 
249) /// redraw current game image, expected to call sendFrame() at end
250) void Pong::redraw()
251) {
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

252)   int y, x;
253) 
254)   // draw background
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

255)   rectFill(0, m_height, 0, m_width, m_backgroundColor);
256) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

257)   // draw middle line: single line on odd width, two dashed lines at even width
258)   for (y = 0; y < m_height; ++y) {
259)     x = (m_width - (y & 1)) / 2;
260)     pixel(y, x, m_lineColor);
261)   }
262) 
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

263)   // draw score
264)   ColorData const &scoreColor = m_goalDelay <= 0 ? m_scoreColor : m_goalColor;
265)   y = (m_height - 1) / 2;
266)   x = m_width / 4;
267)   number3x5(y, x, 0, 0, m_scoreLeft, scoreColor);
268)   number3x5(y, m_width - 1 - x, 0, 0, m_scoreRight, scoreColor);
269) 
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

270)   // draw pads
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

271)   lineVert(m_leftPosY, m_leftPosY + m_padSize - 1, 0,
272)            m_pConnLeft ? m_padColor : m_computerColor);
273)   lineVert(m_rightPosY, m_rightPosY + m_padSize - 1, m_width - 1,
274)            m_pConnRight ? m_padColor : m_computerColor);
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

275) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

276)   // draw ball (blinking if goal delay is active)
277)   if (m_goalDelay <= 0 || (m_goalDelay & 4) == 0) {
278)       pixel(m_ballPosY, m_ballPosX, m_ballColor);
279)   }
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

280) 
281)   // send updated image buffer as frame
282)   sendFrame();
283) }
284) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

285) /// callback when requested time reached
286) void Pong::timeCall()
287) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

288)   // game is running
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

289)   if (m_goalDelay <= 0) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

290) 
291)     // computer player: move pads
292)     if (! m_pConnLeft) {
293)       computerLeft();
294)     }
295)     if (! m_pConnRight) {
296)       computerRight();
297)     }
298) 
299)     // bounce ball
300)     bounceBall();
301) 
302)     // move ball
303)     m_ballPosX += m_ballDirX;
304)     m_ballPosY += m_ballDirY;
305) 
306)     // detect goal
307)     detectGoal();
308) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

309)   }
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

310)   // goal delay (blinking ball)
311)   else if (m_goalDelay > 0) {
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

312) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

313)     --m_goalDelay;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

314) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

315)     // delay is over
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

316)     if (m_goalDelay <= 0) {
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

317)         // maximum score reached
318)         if (m_scoreLeft >= (int)m_maxScore ||
319)             m_scoreRight >= (int)m_maxScore) {
320)           gameOver();
321)           return; // no gameOver() calls deactivate(), no need to redraw
322)         }
323)         // game continues with new ball
324)         else {
325)           startBall();
326)           return; // startBall() calls redraw() and planTimeCall()
327)         }
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

328)     }
329) 
330)   }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

331) 
332)   // draw and send frame
333)   redraw();
334) 
335)   // request next call if needed
336)   planTimeCall();
337) }
338) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

339) /**
340)  * @brief process key received from phone player
341)  * @param[in] key received key from player
342)  * @param[in,out] padPosY y position of player's pad
343)  */
344) void Pong::processKey(char key, int &padPosY)
345) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

346)   // do not move pad if goal delay is active
347)   if (m_goalDelay > 0) {
348)     return;
349)   }
350) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

351)   // move pad (2 = up, 8 = down), do not move pad out of field
352)   if (key == '2' && padPosY > 0) {
353)     --padPosY;
354)     redraw();
355)   }
356)   else if (key == '8' && padPosY < m_height - m_padSize) {
357)     ++padPosY;
358)     redraw();
359)   }
360) }
361) 
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

362) /**
363)  * @brief delay processing for computer players
364)  * @param[in,out] delay delay variable of computer player
365)  * @return whether computer player is allowed to move
366)  */
367) bool Pong::computerDelay(int &delay) const
368) 
369) {
370)   // zero delay: generate new delay
371)   if (delay <= 0) {
372)     int avg_steps = (m_height - m_padSize) / 2;
373)     int delay_range = avg_steps > 1 ? m_width / avg_steps: m_width;
374)     delay = rand() % delay_range + delay_range;
375)   }
376) 
377)   // count down delay
378)   --delay;
379) 
380)   // moving allowd if delay expired
381)   return delay <= 0;
382) }
383) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

384) /**
385)  * @brief computation of ideal pad position for computer players
386)  * @param[in] padBallX x coordinate of position of ball when hitting the pad
387)  * @param[in] padY current y coordinate of pad
388)  * @param[out] padYmin minimum ideal y position of pad
389)  * @param[out] padYmax maximum ideal y position of pad
390)  */
391) void Pong::computerComputePadPos(int padBallX, int padY,
392)                                  int &padYmin, int &padYmax) const
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

393) {
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

394)   // ball not moving towards pad
395)   if ((padBallX - m_ballPosX) * m_ballDirX <= 0) {
396)     // do not move if ball is still close to pad
397)     if (abs(padBallX - m_ballPosX) <= 2) {
398)       padYmin = padY;
399)       padYmax = padY;
400)       return;
401)     }
402)     // move pad to middle (might be 2 pixels wide)
403)     padYmin = (m_height - m_padSize) / 2;
404)     padYmax = (m_height - m_padSize + 1) / 2;
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

405)     return;
406)   }
407) 
408)   // compute expected ball position at pad
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

409)   int ballPosX = m_ballPosX; // simulate were ball is going
410)   int ballPosY = m_ballPosY;
411)   int ballDirY = m_ballDirY;
412)   while ((padBallX - ballPosX) * m_ballDirX > 0) { // while moving to pad
413)     int deltaX = padBallX - ballPosX;
414)     int deltaY = deltaX * m_ballDirX * ballDirY;
415)     if (deltaY < -ballPosY) {
416)       deltaY = -ballPosY;
417)       ballPosX += deltaY * m_ballDirX * ballDirY;
418)       ballPosY = 0;
419)       ballDirY = 1;
420)     }
421)     else if (deltaY > m_height - 1 - ballPosY) {
422)       deltaY = m_height - 1 - ballPosY;
423)       ballPosX += deltaY * m_ballDirX * ballDirY;
424)       ballPosY = m_height - 1;
425)       ballDirY = -1;
426)     } else {
427)       ballPosX += deltaX;
428)       ballPosY += deltaY;
429)     }
430)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

431) 
432)   // compute pad position to hit ball with center of pad
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

433)   padYmin = ballPosY - m_padSize / 2;
434)   padYmax = ballPosY - (m_padSize - 1) / 2;
435) }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

436) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

437) /**
438)  * @brief move pad for computer players
439)  * @param[in] padYmin minimum desired y position of pad
440)  * @param[in] padYmax maximum desired y position of pad
441)  * @param[in,out] padPosY y position of pad
442)  */
443) void Pong::computerMovePad(int padYmin, int padYmax, int &padPosY) const
444) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

445)   // do not move pad if goal delay is active
446)   if (m_goalDelay > 0) {
447)     return;
448)   }
449) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

450)   // move pad, do not move pad out of field
451)   if (padPosY > padYmax && padPosY > 0) {
452)     --padPosY;
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

453)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

454)   else if (padPosY < padYmin && padPosY < m_height - m_padSize) {
455)     ++padPosY;
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

456)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

457) }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

458) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

459) /// computer player for left pad
460) void Pong::computerLeft()
461) {
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

462)   if (computerDelay(m_leftDelay)) {
463)     int padYmin, padYmax;
464)     computerComputePadPos(1, m_leftPosY, padYmin, padYmax);
465)     computerMovePad(padYmin, padYmax, m_leftPosY);
466)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

467) }
468) 
469) /// computer player for right pad
470) void Pong::computerRight()
471) {
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

472)   if (computerDelay(m_rightDelay)) {
473)     int padYmin, padYmax;
474)     computerComputePadPos(m_width - 2, m_rightPosY, padYmin, padYmax);
475)     computerMovePad(padYmin, padYmax, m_rightPosY);
476)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

477) }
478) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

479) /// bounce ball
480) void Pong::bounceBall()
481) {
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

482)   bounceBallSide(); // must be done before player bounce to be safe
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

483)   bounceBallLeft();
484)   bounceBallRight();
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

485)   bounceBallSide(); // must also be done after player bounce to be safe
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

486) }
487) 
488) /// bounce ball at sides
489) void Pong::bounceBallSide()
490) {
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

491)   if (m_ballPosY <= 0 && m_ballDirY < 0) {
492)     m_ballDirY = 1;
493)   }
494)   if (m_ballPosY >= m_height - 1 && m_ballDirY > 0) {
495)     m_ballDirY = -1;
496)   }
497)   if (m_ballPosX <= 0 && m_ballDirX < 0) {
498)     m_ballDirX = 1;
499)   }
500)   if (m_ballPosX >= m_width - 1 && m_ballDirX > 0) {
501)     m_ballDirX = -1;
502)   }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

503) }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

504) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

505) /// bounce ball at left pad
506) void Pong::bounceBallLeft()
507) {
508)   if (m_ballPosX != 1 || m_ballDirX >= 0) {
509)     return;
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

510)   }
511) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

512)   // top corner
513)   if (m_ballPosY == m_leftPosY - 1 && m_ballDirY > 0) {
514)     m_ballDirX = 1;
515)     m_ballDirY = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

516)     ++m_bounceCnt;
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

517)   }
518) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

519)   // bottom corner
520)   else if (m_ballPosY == m_leftPosY + m_padSize && m_ballDirY < 0) {
521)     m_ballDirX = 1;
522)     m_ballDirY = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

523)     ++m_bounceCnt;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

524)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

525) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

526)   // pad edge
527)   else if (m_ballPosY >= m_leftPosY &&
528)            m_ballPosY < m_leftPosY + m_padSize) {
529)     m_ballDirX = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

530)     ++m_bounceCnt;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

531)   }
532) }
533) 
534) /// bounce ball at right pad
535) void Pong::bounceBallRight()
536) {
537)   if (m_ballPosX != m_width - 2 || m_ballDirX <= 0) {
538)     return;
539)   }
540) 
541)   // top corner
542)   if (m_ballPosY == m_rightPosY - 1 && m_ballDirY > 0) {
543)     m_ballDirX = -1;
544)     m_ballDirY = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

545)     ++m_bounceCnt;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

546)   }
547) 
548)   // bottom corner
549)   else if (m_ballPosY == m_rightPosY + m_padSize && m_ballDirY < 0) {
550)     m_ballDirX = -1;
551)     m_ballDirY = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

552)     ++m_bounceCnt;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

553)   }
554) 
555)   // pad edge
556)   else if (m_ballPosY >= m_rightPosY &&
557)            m_ballPosY < m_rightPosY + m_padSize) {
558)     m_ballDirX = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

559)     ++m_bounceCnt;
560)   }
561) }
562) 
563) /// detect goal
564) void Pong::detectGoal()
565) {
566)   static int goalBlinkCnt = 3;
567)   static int goalDelay = goalBlinkCnt * 8 + 3;
568) 
569)   // ball at far left - goal for right player
570)   if (m_ballPosX == 0) {
571)     m_goalDelay = goalDelay;
572)     ++m_scoreRight;
573)   }
574) 
575)   // ball at far right - goal for left player
576)   else if (m_ballPosX == m_width - 1) {
577)     m_goalDelay = goalDelay;
578)     ++m_scoreLeft;
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

579)   }
580) }
581) 
582) /// request next time call - or cancel request if not needed
583) void Pong::planTimeCall()
584) {
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

585)   // no time call needed if not active
586)   if (! isActive()) {
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

587)     m_mgrs.m_callMgr.cancelTimeCall(this);
588)     return;
589)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

590) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

591)   // compute interval based on score and bounce count
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

592)   int speedup = 10 * (m_scoreLeft + m_scoreRight) + m_bounceCnt;
593)   float scale = 0.3f + 0.7f * expf(-0.03f * speedup);
594)   float interval = 1e-3f * m_delay * scale;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

595) 
596)   // request next time call
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

597)   Time stepTime;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

598)   stepTime.fromFloatSec(interval);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

599)   m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime);
600) }
601) 
602) /// start ball
603) void Pong::startBall()
604) {
605)   // ball starts horizontally at middle of field, vertically random
606)   m_ballPosX = (m_width - (rand() & 1)) / 2;
607)   m_ballPosY = rand() % m_height;
608)   // random diagonal direction
609)   m_ballDirX = (rand() & 1) * 2 - 1;
610)   m_ballDirY = (rand() & 1) * 2 - 1;
611) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

612)   // no delays, ball has not bounced at pad
613)   m_leftDelay = 0;
614)   m_rightDelay = 0;
615)   m_goalDelay = 0;
616)   m_bounceCnt = 0;
617) 
618)   // draw and send frame
619)   redraw();
620) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

621)   // request first time call if needed
622)   planTimeCall();
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

623) }
624) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

625) 
626) /// game over: close player connections and deactivate
627) void Pong::gameOver()
628) {
629)   // close open operator connections
630)   if (m_pConnLeft) {
631)     m_pConnLeft->close();
632)     m_pConnLeft = NULL;
633)   }
634)   if (m_pConnRight) {
635)     m_pConnRight->close();
636)     m_pConnRight = NULL;
637)   }
638) 
639)   // deactivate game
640)   deactivate();
641) }
642)