92f7925256891c6a5ceb984ba2ac403b89b008fe
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)   }
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

96)       colorUpdate(m_fileComputerColor, m_computerColor) ||
97)       colorUpdate(m_fileScoreColor, m_scoreColor) ||
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

101)     ret = true;
102)   }
103) 
104)   return ret;
105) }
106) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

107) /**
Stefan Schuermans fix comment typo

Stefan Schuermans authored 5 years ago

108)  * @brief check if accepting new operator connection is possible
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

116)     return true;
117)   }
118)   // right player can join if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

137)     m_pConnLeft = pConn;
138)   }
139)   // right player joins if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

142)   }
143)   // nothing happens
144)   else {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

145)     return;
146)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

201)   }
202)   // nothing happens
203)   else {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

204)     return;
205)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

215) }
216) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

224)   m_leftDelay = 0;
225)   m_rightDelay = 0;
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

226) 
227)   // convert colors
228)   color2data(m_fileBallColor, m_ballColor);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

232)   color2data(m_fileScoreColor, m_scoreColor);
233)   color2data(m_fileGoalColor, m_goalColor);
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

238)   // reset scores
239)   m_scoreLeft = 0;
240)   m_scoreRight = 0;
241) 
242)   // start first ball
Stefan Schuermans pong: start ball at side

Stefan Schuermans authored 5 years ago

243)   m_ballDirX = 0; // start at random side
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

273) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

278) 
279)   // send updated image buffer as frame
280)   sendFrame();
281) }
282) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

283) /// process next time step of game
284) void Pong::timeStep()
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

285) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

310) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

315)         // maximum score reached
316)         if (m_scoreLeft >= (int)m_maxScore ||
317)             m_scoreRight >= (int)m_maxScore) {
318)           gameOver();
319)           return; // no gameOver() calls deactivate(), no need to redraw
320)         }
321)         // game continues with new ball
322)         else {
323)           startBall();
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

324)           return; // startBall() calls redraw() and planTimeStep()
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

326)     }
327) 
328)   }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

329) 
330)   // draw and send frame
331)   redraw();
332) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

333)   // request next time step
334)   planTimeStep();
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

335) }
336) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

391) {
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

434) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

451)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

454)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

455) }
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) /// computer player for left pad
458) void Pong::computerLeft()
459) {
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

475) }
476) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

481)   bounceBallLeft();
482)   bounceBallRight();
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

501) }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

508)   }
509) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

515)   }
516) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

522)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

577)   }
578) }
579) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

580) /// set time for next time step of game - or unset if not needed
581) void Pong::planTimeStep()
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

582) {
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

583)   // no time call needed if not active
584)   if (! isActive()) {
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

585)     unsetTimeStep();
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

586)     return;
587)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

588) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

593) 
594)   // request next time call
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

596)   stepTime.fromFloatSec(interval);
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

597)   setTimeStep(Time::now() + stepTime);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

598) }
599) 
600) /// start ball
601) void Pong::startBall()
602) {
Stefan Schuermans pong: start ball at side

Stefan Schuermans authored 5 years ago

603)   // horizontal start direction opposite as before goal or random
604)   if (m_ballDirX > 0) {
605)     m_ballDirX = -1;
606)   } else if (m_ballDirX < 0) {
607)     m_ballDirX = 1;
608)   } else {
609)     m_ballDirX = (rand() & 1) * 2 - 1;
610)   }
611)   // random vertical start direction
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

612)   m_ballDirY = (rand() & 1) * 2 - 1;
613) 
Stefan Schuermans pong: start ball at side

Stefan Schuermans authored 5 years ago

614)   // horizontal start postion at side of field (depending on direction)
615)   m_ballPosX = m_ballDirX > 0 ? 0 : m_width - 1;
616)   // random vertical start position
617)   m_ballPosY = rand() % m_height;
618) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

619)   // no delays, ball has not bounced at pad
620)   m_leftDelay = 0;
621)   m_rightDelay = 0;
622)   m_goalDelay = 0;
623)   m_bounceCnt = 0;
624) 
625)   // draw and send frame
626)   redraw();
627) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

628)   // request first time step if needed
629)   planTimeStep();
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

630) }
631) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

632) /// game over: close player connections and deactivate
633) void Pong::gameOver()
634) {
635)   // close open operator connections
636)   if (m_pConnLeft) {
637)     m_pConnLeft->close();
638)     m_pConnLeft = NULL;
639)   }
640)   if (m_pConnRight) {
641)     m_pConnRight->close();
642)     m_pConnRight = NULL;
643)   }
644) 
645)   // deactivate game
646)   deactivate();
647) }
648)