e897205a2f2e4ef710f8b48d0a3b8c7e93cd8632
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 sound support for pong game

Stefan Schuermans authored 5 years ago

19) #include "NameFile.h"
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

28) 
29) namespace Blinker {
30) 
31) /**
32)  * @brief constructor
33)  * @param[in] name module name
34)  * @param[in] mgrs managers
35)  * @param[in] dirBase base directory
36)  */
37) Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
38)   Game(name, mgrs, dirBase),
39)   m_fileBallColor(dirBase.getFile("ballColor")),
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

46)   m_fileMaxScore(dirBase.getFile("maxScore")),
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

47)   m_fileLeftPlayerSound(dirBase.getFile("leftPlayerSound")),
48)   m_fileRightPlayerSound(dirBase.getFile("rightPlayerSound")),
49)   m_fileScoreSound(dirBase.getFile("scoreSound")),
50)   m_fileOtherScoreSound(dirBase.getFile("otherScoreSound")),
51)   m_fileWinSound(dirBase.getFile("winSound")),
52)   m_fileLoseSound(dirBase.getFile("loseSound")),
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

58)   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

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

Stefan Schuermans authored 5 years ago

60) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

64) }
65) 
66) /// virtual destructor
67) Pong::~Pong()
68) {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

72) 
73)   // close open operator connections
74)   if (m_pConnLeft) {
75)     m_pConnLeft->close();
76)     m_pConnLeft = NULL;
77)   }
78)   if (m_pConnRight) {
79)     m_pConnRight->close();
80)     m_pConnRight = NULL;
81)   }
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

82) }
83) 
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

84) /**
85)  * @brief check for update of configuration (derived game)
86)  * @param[in,out] doReinit set to true to ask for reinitialization
87)  * @param[in,out] doRedraw set to true to ask for redrawing
88)  */
89) void Pong::updateConfigGame(bool &doReinit, bool &doRedraw)
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

90) {
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

91)   (void)doReinit;
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

92) 
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

93)   // color file was modified -> convert color, ask for redraw
94)   if (colorUpdate(m_fileBallColor, m_ballColor)) { doRedraw = true; }
95)   if (colorUpdate(m_fileLineColor, m_lineColor)) { doRedraw = true; }
96)   if (colorUpdate(m_filePadColor, m_padColor)) { doRedraw = true; }
97)   if (colorUpdate(m_fileComputerColor, m_computerColor)) { doRedraw = true; }
98)   if (colorUpdate(m_fileScoreColor, m_scoreColor)) { doRedraw = true; }
99)   if (colorUpdate(m_fileGoalColor, m_goalColor)) { doRedraw = true; }
100) 
101)   // cfg value file was updated -> read new cfg value, no reinit/redraw
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) 
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

105)   // sound name file was modified -> re-read sound name, no other update needed
106)   soundUpdate(m_fileLeftPlayerSound);
107)   soundUpdate(m_fileRightPlayerSound);
108)   soundUpdate(m_fileScoreSound);
109)   soundUpdate(m_fileOtherScoreSound);
110)   soundUpdate(m_fileWinSound);
111)   soundUpdate(m_fileLoseSound);
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

112) }
113) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

114) /**
Stefan Schuermans fix comment typo

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

116)  * @param[in] name operator interface name
117)  * @return if accepting new connection is possible
118)  */
119) bool Pong::acceptNewOpConn(const std::string &name)
120) {
121)   // left player can join if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

123)     return true;
124)   }
125)   // right player can join if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

144)     m_pConnLeft = pConn;
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

145)     requestOpConnSound(m_pConnLeft, m_fileLeftPlayerSound);
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

146)   }
147)   // right player joins if none there yet
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

149)     m_pConnRight = pConn;
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

150)     requestOpConnSound(m_pConnRight, m_fileRightPlayerSound);
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

151)   }
Stefan Schuermans add op conn close requests

Stefan Schuermans authored 5 years ago

152)   // close imcoming connection as soon as possible, nothing else happens
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

153)   else {
Stefan Schuermans add op conn close requests

Stefan Schuermans authored 5 years ago

154)     requestOpConnClose(pConn);
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

155)     return;
156)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

157) 
158)   // already active
159)   if (isActive()) {
160)     redraw(); // player color is different for phone / computer
161)   }
162)   // first player joined
163)   else {
164)     activate();
165)   }
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

166) }
167) 
168) /**
169)  * @brief key command received on operator connection
170)  * @param[in] pConn operator connection object
171)  * @param[in] key key that was pressed
172)  */
173) void Pong::opConnRecvKey(OpConn *pConn, char key)
174) {
Stefan Schuermans pong: hash -> hangup, star...

Stefan Schuermans authored 5 years ago

175)   // hash -> hang up
176)   if (key == '#') {
177)     opConnClose(pConn);
178)     pConn->close();
179)     return;
180)   }
181) 
182)   // star -> inform player about it side
183)   if (key == '*') {
184)     if (pConn == m_pConnLeft) {
185)       playOpConnSound(pConn, m_fileLeftPlayerSound);
186)     } else if (pConn == m_pConnRight) {
187)       playOpConnSound(pConn, m_fileRightPlayerSound);
188)     }
189)     return;
190)   }
191) 
192)   // normal keys for controlling game
193) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

194)   // left player
195)   if (pConn == m_pConnLeft) {
196)     processKey(key, m_leftPosY);
197)   }
198)   // right player
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)     processKey(key, m_rightPosY);
201)   }
202) }
203) 
204) /**
205)  * @brief play command received on operator connection
206)  * @param[in] pConn operator connection object
207)  * @param[in] sound name of sound to play
208)  */
209) void Pong::opConnRecvPlay(OpConn *pConn, const std::string &sound)
210) {
211)   (void)pConn;
212)   (void)sound;
213) }
214) 
215) /**
216)  * @brief operator connection is closed
217)  * @param[in] pConn operator connection object
218)  *
219)  * The connection may not be used for sending any more in this callback.
220)  */
221) void Pong::opConnClose(OpConn *pConn)
222) {
Stefan Schuermans add op conn close requests

Stefan Schuermans authored 5 years ago

223)   // remove coperator connection from requests (if it was in)
224)   forgetOpConn(pConn);
225) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

226)   // left player leaves
227)   if (pConn == m_pConnLeft) {
228)     m_pConnLeft = NULL;
229)   }
230)   // right player leaves
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

233)   }
234)   // nothing happens
235)   else {
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

236)     return;
237)   }
Stefan Schuermans pong: activate when player...

Stefan Schuermans authored 5 years ago

238) 
239)   // still a phone player there
240)   if (m_pConnLeft || m_pConnRight) {
241)     redraw(); // player color is different for phone / computer
242)   }
243)   // no phone players left
244)   else {
245)     deactivate();
246)   }
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

247) }
248) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

252)   // compute parameters
253)   m_padSize = (m_height + 1) / 3;
254)   m_leftPosY = (m_height - m_padSize) / 2;
255)   m_rightPosY = (m_height - m_padSize) / 2;
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

256)   m_leftDelay = 0;
257)   m_rightDelay = 0;
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

258) 
259)   // convert colors
260)   color2data(m_fileBallColor, m_ballColor);
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

264)   color2data(m_fileScoreColor, m_scoreColor);
265)   color2data(m_fileGoalColor, m_goalColor);
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

270)   // reset scores
271)   m_scoreLeft = 0;
272)   m_scoreRight = 0;
273) 
274)   // start first ball
Stefan Schuermans pong: start ball at side

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

277) }
278) 
279) /// redraw current game image, expected to call sendFrame() at end
280) void Pong::redraw()
281) {
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

282)   int y, x;
283) 
284)   // draw background
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

287)   // draw middle line: single line on odd width, two dashed lines at even width
288)   for (y = 0; y < m_height; ++y) {
289)     x = (m_width - (y & 1)) / 2;
290)     pixel(y, x, m_lineColor);
291)   }
292) 
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

293)   // draw score
294)   ColorData const &scoreColor = m_goalDelay <= 0 ? m_scoreColor : m_goalColor;
295)   y = (m_height - 1) / 2;
296)   x = m_width / 4;
297)   number3x5(y, x, 0, 0, m_scoreLeft, scoreColor);
298)   number3x5(y, m_width - 1 - x, 0, 0, m_scoreRight, scoreColor);
299) 
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

301)   lineVert(m_leftPosY, m_leftPosY + m_padSize - 1, 0,
302)            m_pConnLeft ? m_padColor : m_computerColor);
303)   lineVert(m_rightPosY, m_rightPosY + m_padSize - 1, m_width - 1,
304)            m_pConnRight ? m_padColor : m_computerColor);
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

305) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

306)   // draw ball (blinking if goal delay is active)
307)   if (m_goalDelay <= 0 || (m_goalDelay & 4) == 0) {
Stefan Schuermans fix indentation

Stefan Schuermans authored 5 years ago

308)     pixel(m_ballPosY, m_ballPosX, m_ballColor);
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

309)   }
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

310) 
311)   // send updated image buffer as frame
312)   sendFrame();
313) }
314) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

320) 
321)     // computer player: move pads
322)     if (! m_pConnLeft) {
323)       computerLeft();
324)     }
325)     if (! m_pConnRight) {
326)       computerRight();
327)     }
328) 
329)     // bounce ball
330)     bounceBall();
331) 
332)     // move ball
333)     m_ballPosX += m_ballDirX;
334)     m_ballPosY += m_ballDirY;
335) 
336)     // detect goal
337)     detectGoal();
338) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

342) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

347)         // maximum score reached
348)         if (m_scoreLeft >= (int)m_maxScore ||
349)             m_scoreRight >= (int)m_maxScore) {
350)           gameOver();
351)           return; // no gameOver() calls deactivate(), no need to redraw
352)         }
353)         // game continues with new ball
354)         else {
355)           startBall();
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

358)     }
359) 
360)   }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

361) 
362)   // draw and send frame
363)   redraw();
364) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

367) }
368) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

369) /**
370)  * @brief process key received from phone player
371)  * @param[in] key received key from player
372)  * @param[in,out] padPosY y position of player's pad
373)  */
374) void Pong::processKey(char key, int &padPosY)
375) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

376)   // do not move pad if goal delay is active
377)   if (m_goalDelay > 0) {
378)     return;
379)   }
380) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

381)   // move pad (2 = up, 8 = down), do not move pad out of field
382)   if (key == '2' && padPosY > 0) {
383)     --padPosY;
384)     redraw();
385)   }
386)   else if (key == '8' && padPosY < m_height - m_padSize) {
387)     ++padPosY;
388)     redraw();
389)   }
390) }
391) 
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

392) /**
393)  * @brief delay processing for computer players
394)  * @param[in,out] delay delay variable of computer player
395)  * @return whether computer player is allowed to move
396)  */
397) bool Pong::computerDelay(int &delay) const
398) 
399) {
400)   // zero delay: generate new delay
401)   if (delay <= 0) {
402)     int avg_steps = (m_height - m_padSize) / 2;
403)     int delay_range = avg_steps > 1 ? m_width / avg_steps: m_width;
404)     delay = rand() % delay_range + delay_range;
405)   }
406) 
407)   // count down delay
408)   --delay;
409) 
410)   // moving allowd if delay expired
411)   return delay <= 0;
412) }
413) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

414) /**
415)  * @brief computation of ideal pad position for computer players
416)  * @param[in] padBallX x coordinate of position of ball when hitting the pad
417)  * @param[in] padY current y coordinate of pad
418)  * @param[out] padYmin minimum ideal y position of pad
419)  * @param[out] padYmax maximum ideal y position of pad
420)  */
421) void Pong::computerComputePadPos(int padBallX, int padY,
422)                                  int &padYmin, int &padYmax) const
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

423) {
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

424)   // ball not moving towards pad
425)   if ((padBallX - m_ballPosX) * m_ballDirX <= 0) {
426)     // do not move if ball is still close to pad
427)     if (abs(padBallX - m_ballPosX) <= 2) {
428)       padYmin = padY;
429)       padYmax = padY;
430)       return;
431)     }
432)     // move pad to middle (might be 2 pixels wide)
433)     padYmin = (m_height - m_padSize) / 2;
434)     padYmax = (m_height - m_padSize + 1) / 2;
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

435)     return;
436)   }
437) 
438)   // compute expected ball position at pad
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

439)   int ballPosX = m_ballPosX; // simulate were ball is going
440)   int ballPosY = m_ballPosY;
441)   int ballDirY = m_ballDirY;
442)   while ((padBallX - ballPosX) * m_ballDirX > 0) { // while moving to pad
443)     int deltaX = padBallX - ballPosX;
444)     int deltaY = deltaX * m_ballDirX * ballDirY;
445)     if (deltaY < -ballPosY) {
446)       deltaY = -ballPosY;
447)       ballPosX += deltaY * m_ballDirX * ballDirY;
448)       ballPosY = 0;
449)       ballDirY = 1;
450)     }
451)     else if (deltaY > m_height - 1 - ballPosY) {
452)       deltaY = m_height - 1 - ballPosY;
453)       ballPosX += deltaY * m_ballDirX * ballDirY;
454)       ballPosY = m_height - 1;
455)       ballDirY = -1;
456)     } else {
457)       ballPosX += deltaX;
458)       ballPosY += deltaY;
459)     }
460)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

463)   padYmin = ballPosY - m_padSize / 2;
464)   padYmax = ballPosY - (m_padSize - 1) / 2;
465) }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

466) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

467) /**
468)  * @brief move pad for computer players
469)  * @param[in] padYmin minimum desired y position of pad
470)  * @param[in] padYmax maximum desired y position of pad
471)  * @param[in,out] padPosY y position of pad
472)  */
473) void Pong::computerMovePad(int padYmin, int padYmax, int &padPosY) const
474) {
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

475)   // do not move pad if goal delay is active
476)   if (m_goalDelay > 0) {
477)     return;
478)   }
479) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

483)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

486)   }
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

488) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

489) /// computer player for left pad
490) void Pong::computerLeft()
491) {
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

492)   if (computerDelay(m_leftDelay)) {
493)     int padYmin, padYmax;
494)     computerComputePadPos(1, m_leftPosY, padYmin, padYmax);
495)     computerMovePad(padYmin, padYmax, m_leftPosY);
496)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

497) }
498) 
499) /// computer player for right pad
500) void Pong::computerRight()
501) {
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

502)   if (computerDelay(m_rightDelay)) {
503)     int padYmin, padYmax;
504)     computerComputePadPos(m_width - 2, m_rightPosY, padYmin, padYmax);
505)     computerMovePad(padYmin, padYmax, m_rightPosY);
506)   }
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

509) /// bounce ball
510) void Pong::bounceBall()
511) {
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

513)   bounceBallLeft();
514)   bounceBallRight();
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

516) }
517) 
518) /// bounce ball at sides
519) void Pong::bounceBallSide()
520) {
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

521)   if (m_ballPosY <= 0 && m_ballDirY < 0) {
522)     m_ballDirY = 1;
523)   }
524)   if (m_ballPosY >= m_height - 1 && m_ballDirY > 0) {
525)     m_ballDirY = -1;
526)   }
527)   if (m_ballPosX <= 0 && m_ballDirX < 0) {
528)     m_ballDirX = 1;
529)   }
530)   if (m_ballPosX >= m_width - 1 && m_ballDirX > 0) {
531)     m_ballDirX = -1;
532)   }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

533) }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

535) /// bounce ball at left pad
536) void Pong::bounceBallLeft()
537) {
538)   if (m_ballPosX != 1 || m_ballDirX >= 0) {
539)     return;
Stefan Schuermans pong: add pads

Stefan Schuermans authored 5 years ago

540)   }
541) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

542)   // top corner
543)   if (m_ballPosY == m_leftPosY - 1 && m_ballDirY > 0) {
544)     m_ballDirX = 1;
545)     m_ballDirY = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

547)   }
548) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

549)   // bottom corner
550)   else if (m_ballPosY == m_leftPosY + m_padSize && m_ballDirY < 0) {
551)     m_ballDirX = 1;
552)     m_ballDirY = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

554)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

556)   // pad edge
557)   else if (m_ballPosY >= m_leftPosY &&
558)            m_ballPosY < m_leftPosY + m_padSize) {
559)     m_ballDirX = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

561)   }
562) }
563) 
564) /// bounce ball at right pad
565) void Pong::bounceBallRight()
566) {
567)   if (m_ballPosX != m_width - 2 || m_ballDirX <= 0) {
568)     return;
569)   }
570) 
571)   // top corner
572)   if (m_ballPosY == m_rightPosY - 1 && m_ballDirY > 0) {
573)     m_ballDirX = -1;
574)     m_ballDirY = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

576)   }
577) 
578)   // bottom corner
579)   else if (m_ballPosY == m_rightPosY + m_padSize && m_ballDirY < 0) {
580)     m_ballDirX = -1;
581)     m_ballDirY = 1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

583)   }
584) 
585)   // pad edge
586)   else if (m_ballPosY >= m_rightPosY &&
587)            m_ballPosY < m_rightPosY + m_padSize) {
588)     m_ballDirX = -1;
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

589)     ++m_bounceCnt;
590)   }
591) }
592) 
593) /// detect goal
594) void Pong::detectGoal()
595) {
596)   static int goalBlinkCnt = 3;
597)   static int goalDelay = goalBlinkCnt * 8 + 3;
598) 
599)   // ball at far left - goal for right player
600)   if (m_ballPosX == 0) {
601)     m_goalDelay = goalDelay;
602)     ++m_scoreRight;
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

603)     // play sound - score or win
604)     if (m_scoreRight >= (int)m_maxScore) {
605)       playOpConnSound(m_pConnRight, m_fileWinSound);
606)       playOpConnSound(m_pConnLeft, m_fileLoseSound);
607)     } else {
608)       playOpConnSound(m_pConnRight, m_fileScoreSound);
609)       playOpConnSound(m_pConnLeft, m_fileOtherScoreSound);
610)     }
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

611)   }
612) 
613)   // ball at far right - goal for left player
614)   else if (m_ballPosX == m_width - 1) {
615)     m_goalDelay = goalDelay;
616)     ++m_scoreLeft;
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

617)     // play sound - score or win
618)     if (m_scoreLeft >= (int)m_maxScore) {
619)       playOpConnSound(m_pConnLeft, m_fileWinSound);
620)       playOpConnSound(m_pConnRight, m_fileLoseSound);
621)     } else {
622)       playOpConnSound(m_pConnLeft, m_fileScoreSound);
623)       playOpConnSound(m_pConnRight, m_fileOtherScoreSound);
624)     }
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

625)   }
626) }
627) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

634)     return;
635)   }
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

636) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

641) 
642)   // request next time call
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

646) }
647) 
648) /// start ball
649) void Pong::startBall()
650) {
Stefan Schuermans pong: start ball at side

Stefan Schuermans authored 5 years ago

651)   // horizontal start direction opposite as before goal or random
652)   if (m_ballDirX > 0) {
653)     m_ballDirX = -1;
654)   } else if (m_ballDirX < 0) {
655)     m_ballDirX = 1;
656)   } else {
657)     m_ballDirX = (rand() & 1) * 2 - 1;
658)   }
659)   // random vertical start direction
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

662)   // horizontal start postion at side of field (depending on direction)
663)   m_ballPosX = m_ballDirX > 0 ? 0 : m_width - 1;
664)   // random vertical start position
665)   m_ballPosY = rand() % m_height;
666) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

667)   // no delays, ball has not bounced at pad
668)   m_leftDelay = 0;
669)   m_rightDelay = 0;
670)   m_goalDelay = 0;
671)   m_bounceCnt = 0;
672) 
673)   // draw and send frame
674)   redraw();
675) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

678) }
679) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

680) /// game over: close player connections and deactivate
681) void Pong::gameOver()
682) {
683)   // close open operator connections
684)   if (m_pConnLeft) {
Stefan Schuermans add op conn close requests

Stefan Schuermans authored 5 years ago

685)     forgetOpConn(m_pConnLeft); // remove from requests (if it was in)
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

686)     m_pConnLeft->close();
687)     m_pConnLeft = NULL;
688)   }
689)   if (m_pConnRight) {
Stefan Schuermans add op conn close requests

Stefan Schuermans authored 5 years ago

690)     forgetOpConn(m_pConnRight); // remove from requests (if it was in)
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

691)     m_pConnRight->close();
692)     m_pConnRight = NULL;
693)   }
694) 
695)   // deactivate game
696)   deactivate();
697) }
698) 
Stefan Schuermans use same odering in source...

Stefan Schuermans authored 5 years ago

699) /// descriptor for delay value
700) Pong::ValueDescr const Pong::c_delayDescr = { 200, 100, 500 };
701) /// descriptor for maximum score value
702) Pong::ValueDescr const Pong::c_maxScoreDescr = { 9, 1, 99 };
703) 
704) /// operator connection name suffix for left player's connection
705) std::string const Pong::c_opConnSuffixLeft = "/left";
706) /// operator connection name suffix for right player's connection
707) std::string const Pong::c_opConnSuffixRight = "/right";
708)