My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

invaders.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../inc/MarlinConfigPre.h"
  23. #if ENABLED(MARLIN_INVADERS)
  24. #include "game.h"
  25. #define CANNON_W 11
  26. #define CANNON_H 8
  27. #define CANNON_VEL 4
  28. #define CANNON_Y (LCD_PIXEL_HEIGHT - 1 - CANNON_H)
  29. #define INVADER_VEL 3
  30. #define INVADER_TOP MENU_FONT_ASCENT
  31. #define INVADERS_WIDE ((INVADER_COL_W) * (INVADER_COLS))
  32. #define INVADERS_HIGH ((INVADER_ROW_H) * (INVADER_ROWS))
  33. #define UFO_H 5
  34. #define UFO_W 13
  35. #define LASER_H 4
  36. #define SHOT_H 3
  37. #define EXPL_W 11
  38. #define LIFE_W 8
  39. #define LIFE_H 5
  40. #define INVADER_RIGHT ((INVADER_COLS) * (INVADER_COL_W))
  41. // 11x8
  42. const unsigned char invader[3][2][16] PROGMEM = {
  43. { { B00000110,B00000000,
  44. B00001111,B00000000,
  45. B00011111,B10000000,
  46. B00110110,B11000000,
  47. B00111111,B11000000,
  48. B00001001,B00000000,
  49. B00010110,B10000000,
  50. B00101001,B01000000
  51. }, {
  52. B00000110,B00000000,
  53. B00001111,B00000000,
  54. B00011111,B10000000,
  55. B00110110,B11000000,
  56. B00111111,B11000000,
  57. B00010110,B10000000,
  58. B00100000,B01000000,
  59. B00010000,B10000000
  60. }
  61. }, {
  62. { B00010000,B01000000,
  63. B00001000,B10000000,
  64. B00011111,B11000000,
  65. B00110111,B01100000,
  66. B01111111,B11110000,
  67. B01011111,B11010000,
  68. B01010000,B01010000,
  69. B00001101,B10000000
  70. }, {
  71. B00010000,B01000000,
  72. B01001000,B10010000,
  73. B01011111,B11010000,
  74. B01110111,B01110000,
  75. B01111111,B11110000,
  76. B00011111,B11000000,
  77. B00010000,B01000000,
  78. B00100000,B00100000
  79. }
  80. }, {
  81. { B00001111,B00000000,
  82. B01111111,B11100000,
  83. B11111111,B11110000,
  84. B11100110,B01110000,
  85. B11111111,B11110000,
  86. B00011001,B10000000,
  87. B00110110,B11000000,
  88. B11000000,B00110000
  89. }, {
  90. B00001111,B00000000,
  91. B01111111,B11100000,
  92. B11111111,B11110000,
  93. B11100110,B01110000,
  94. B11111111,B11110000,
  95. B00011001,B10000000,
  96. B00110110,B11000000,
  97. B00011001,B10000000
  98. }
  99. }
  100. };
  101. const unsigned char cannon[] PROGMEM = {
  102. B00000100,B00000000,
  103. B00001110,B00000000,
  104. B00001110,B00000000,
  105. B01111111,B11000000,
  106. B11111111,B11100000,
  107. B11111111,B11100000,
  108. B11111111,B11100000,
  109. B11111111,B11100000
  110. };
  111. const unsigned char life[] PROGMEM = {
  112. B00010000,
  113. B01111100,
  114. B11111110,
  115. B11111110,
  116. B11111110
  117. };
  118. const unsigned char explosion[] PROGMEM = {
  119. B01000100,B01000000,
  120. B00100100,B10000000,
  121. B00000000,B00000000,
  122. B00110001,B10000000,
  123. B00000000,B00000000,
  124. B00100100,B10000000,
  125. B01000100,B01000000
  126. };
  127. const unsigned char ufo[] PROGMEM = {
  128. B00011111,B11000000,
  129. B01111111,B11110000,
  130. B11011101,B11011000,
  131. B11111111,B11111000,
  132. B01111111,B11110000
  133. };
  134. constexpr uint8_t inv_type[] = {
  135. #if INVADER_ROWS == 5
  136. 0, 1, 1, 2, 2
  137. #elif INVADER_ROWS == 4
  138. 0, 1, 1, 2
  139. #elif INVADER_ROWS == 3
  140. 0, 1, 2
  141. #else
  142. #error "INVASION_SIZE must be 3, 4, or 5."
  143. #endif
  144. };
  145. invaders_data_t &idat = marlin_game_data.invaders;
  146. #define INV_X_LEFT(C,T) (idat.pos.x + (C) * (INVADER_COL_W) + inv_off[T])
  147. #define INV_X_CTR(C,T) (INV_X_LEFT(C,T) + inv_wide[T] / 2)
  148. #define INV_Y_BOT(R) (idat.pos.y + (R + 1) * (INVADER_ROW_H) - 2)
  149. constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 };
  150. inline void update_invader_data() {
  151. uint8_t inv_mask = 0;
  152. // Get a list of all active invaders
  153. uint8_t sc = 0;
  154. LOOP_L_N(y, INVADER_ROWS) {
  155. uint8_t m = idat.bugs[y];
  156. if (m) idat.botmost = y + 1;
  157. inv_mask |= m;
  158. LOOP_L_N(x, INVADER_COLS)
  159. if (TEST(m, x)) idat.shooters[sc++] = (y << 4) | x;
  160. }
  161. idat.leftmost = 0;
  162. LOOP_L_N(i, INVADER_COLS) { if (TEST(inv_mask, i)) break; idat.leftmost -= INVADER_COL_W; }
  163. idat.rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE);
  164. for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; idat.rightmost += INVADER_COL_W; }
  165. if (idat.count == 2) idat.dir = idat.dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1);
  166. }
  167. inline void reset_bullets() {
  168. LOOP_L_N(i, COUNT(idat.bullet)) idat.bullet[i].v = 0;
  169. }
  170. inline void reset_invaders() {
  171. idat.pos.x = 0; idat.pos.y = INVADER_TOP;
  172. idat.dir = INVADER_VEL;
  173. idat.count = (INVADER_COLS) * (INVADER_ROWS);
  174. LOOP_L_N(i, INVADER_ROWS) idat.bugs[i] = _BV(INVADER_COLS) - 1;
  175. update_invader_data();
  176. reset_bullets();
  177. }
  178. inline void spawn_ufo() {
  179. idat.ufov = random(0, 2) ? 1 : -1;
  180. idat.ufox = idat.ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1;
  181. }
  182. inline void reset_player() {
  183. idat.cannon_x = 0;
  184. ui.encoderPosition = 0;
  185. }
  186. inline void fire_cannon() {
  187. idat.laser.x = idat.cannon_x + CANNON_W / 2;
  188. idat.laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H);
  189. idat.laser.v = -(LASER_H);
  190. }
  191. inline void explode(const int8_t x, const int8_t y, const int8_t v=4) {
  192. idat.explod.x = x - (EXPL_W) / 2;
  193. idat.explod.y = y;
  194. idat.explod.v = v;
  195. }
  196. inline void kill_cannon(uint8_t &game_state, const uint8_t st) {
  197. reset_bullets();
  198. explode(idat.cannon_x + (CANNON_W) / 2, CANNON_Y, 6);
  199. _BUZZ(1000, 10);
  200. if (--idat.cannons_left) {
  201. idat.laser.v = 0;
  202. game_state = st;
  203. reset_player();
  204. }
  205. else
  206. game_state = 0;
  207. }
  208. void InvadersGame::game_screen() {
  209. ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible
  210. // Run game logic once per full screen
  211. if (ui.first_page) {
  212. // Update Cannon Position
  213. int16_t ep = constrain(int16_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (CANNON_W)) / (CANNON_VEL));
  214. ui.encoderPosition = ep;
  215. ep *= (CANNON_VEL);
  216. if (ep > idat.cannon_x) { idat.cannon_x += CANNON_VEL - 1; if (ep - idat.cannon_x < 2) idat.cannon_x = ep; }
  217. if (ep < idat.cannon_x) { idat.cannon_x -= CANNON_VEL - 1; if (idat.cannon_x - ep < 2) idat.cannon_x = ep; }
  218. // Run the game logic
  219. if (game_state) do {
  220. // Move the UFO, if any
  221. if (idat.ufov) { idat.ufox += idat.ufov; if (!WITHIN(idat.ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) idat.ufov = 0; }
  222. if (game_state > 1) { if (--game_state == 2) { reset_invaders(); } else if (game_state == 100) { game_state = 1; } break; }
  223. const bool did_blink = (++idat.blink_count > idat.count >> 1);
  224. if (did_blink) {
  225. idat.game_blink = !idat.game_blink;
  226. idat.blink_count = 0;
  227. }
  228. if (idat.count && did_blink) {
  229. const int8_t newx = idat.pos.x + idat.dir;
  230. if (!WITHIN(newx, idat.leftmost, idat.rightmost)) { // Invaders reached the edge?
  231. idat.dir *= -1; // Invaders change direction
  232. idat.pos.y += (INVADER_ROW_H) / 2; // Invaders move down
  233. idat.pos.x -= idat.dir; // ...and only move down this time.
  234. if (idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom?
  235. kill_cannon(game_state, 20); // Kill the cannon. Reset invaders.
  236. }
  237. idat.pos.x += idat.dir; // Invaders take one step left/right
  238. // Randomly shoot if invaders are listed
  239. if (idat.count && !random(0, 20)) {
  240. // Find a free bullet
  241. laser_t *b = nullptr;
  242. LOOP_L_N(i, COUNT(idat.bullet)) if (!idat.bullet[i].v) { b = &idat.bullet[i]; break; }
  243. if (b) {
  244. // Pick a random shooter and update the bullet
  245. //SERIAL_ECHOLNPGM("free bullet found");
  246. const uint8_t inv = idat.shooters[random(0, idat.count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row];
  247. b->x = INV_X_CTR(col, type);
  248. b->y = INV_Y_BOT(row);
  249. b->v = 2 + random(0, 2);
  250. }
  251. }
  252. }
  253. // Update the laser position
  254. if (idat.laser.v) {
  255. idat.laser.y += idat.laser.v;
  256. if (idat.laser.y < 0) idat.laser.v = 0;
  257. }
  258. // Did the laser collide with an invader?
  259. if (idat.laser.v && WITHIN(idat.laser.y, idat.pos.y, idat.pos.y + INVADERS_HIGH - 1)) {
  260. const int8_t col = idat.laser_col();
  261. if (WITHIN(col, 0, INVADER_COLS - 1)) {
  262. const int8_t row = idat.laser_row();
  263. if (WITHIN(row, 0, INVADER_ROWS - 1)) {
  264. const uint8_t mask = _BV(col);
  265. if (idat.bugs[row] & mask) {
  266. const uint8_t type = inv_type[row];
  267. const int8_t invx = INV_X_LEFT(col, type);
  268. if (WITHIN(idat.laser.x, invx, invx + inv_wide[type] - 1)) {
  269. // Turn off laser
  270. idat.laser.v = 0;
  271. // Remove the invader!
  272. idat.bugs[row] &= ~mask;
  273. // Score!
  274. score += INVADER_ROWS - row;
  275. // Explode sound!
  276. _BUZZ(40, 10);
  277. // Explosion bitmap!
  278. explode(invx + inv_wide[type] / 2, idat.pos.y + row * (INVADER_ROW_H));
  279. // If invaders are gone, go to reset invaders state
  280. if (--idat.count) update_invader_data(); else { game_state = 20; reset_bullets(); }
  281. } // laser x hit
  282. } // invader exists
  283. } // good row
  284. } // good col
  285. } // laser in invader zone
  286. // Handle alien bullets
  287. LOOP_L_N(s, COUNT(idat.bullet)) {
  288. laser_t *b = &idat.bullet[s];
  289. if (b->v) {
  290. // Update alien bullet position
  291. b->y += b->v;
  292. if (b->y >= LCD_PIXEL_HEIGHT)
  293. b->v = 0; // Offscreen
  294. else if (b->y >= CANNON_Y && WITHIN(b->x, idat.cannon_x, idat.cannon_x + CANNON_W - 1))
  295. kill_cannon(game_state, 120); // Hit the cannon
  296. }
  297. }
  298. // Randomly spawn a UFO
  299. if (!idat.ufov && !random(0,500)) spawn_ufo();
  300. // Did the laser hit a ufo?
  301. if (idat.laser.v && idat.ufov && idat.laser.y < UFO_H + 2 && WITHIN(idat.laser.x, idat.ufox, idat.ufox + UFO_W - 1)) {
  302. // Turn off laser and UFO
  303. idat.laser.v = idat.ufov = 0;
  304. // Score!
  305. score += 10;
  306. // Explode!
  307. _BUZZ(40, 10);
  308. // Explosion bitmap
  309. explode(idat.ufox + (UFO_W) / 2, 1);
  310. }
  311. } while (false);
  312. }
  313. // Click-and-hold to abort
  314. if (ui.button_pressed()) --idat.quit_count; else idat.quit_count = 10;
  315. // Click to fire or exit
  316. if (ui.use_click()) {
  317. if (!game_state)
  318. idat.quit_count = 0;
  319. else if (game_state == 1 && !idat.laser.v)
  320. fire_cannon();
  321. }
  322. if (!idat.quit_count) exit_game();
  323. u8g.setColorIndex(1);
  324. // Draw invaders
  325. if (PAGE_CONTAINS(idat.pos.y, idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 - 1)) {
  326. int8_t yy = idat.pos.y;
  327. LOOP_L_N(y, INVADER_ROWS) {
  328. const uint8_t type = inv_type[y];
  329. if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) {
  330. int8_t xx = idat.pos.x;
  331. LOOP_L_N(x, INVADER_COLS) {
  332. if (TEST(idat.bugs[y], x))
  333. u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][idat.game_blink]);
  334. xx += INVADER_COL_W;
  335. }
  336. }
  337. yy += INVADER_ROW_H;
  338. }
  339. }
  340. // Draw UFO
  341. if (idat.ufov && PAGE_UNDER(UFO_H + 2))
  342. u8g.drawBitmapP(idat.ufox, 2, 2, UFO_H, ufo);
  343. // Draw cannon
  344. if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02)))
  345. u8g.drawBitmapP(idat.cannon_x, CANNON_Y, 2, CANNON_H, cannon);
  346. // Draw laser
  347. if (idat.laser.v && PAGE_CONTAINS(idat.laser.y, idat.laser.y + LASER_H - 1))
  348. u8g.drawVLine(idat.laser.x, idat.laser.y, LASER_H);
  349. // Draw invader bullets
  350. LOOP_L_N (i, COUNT(idat.bullet)) {
  351. if (idat.bullet[i].v && PAGE_CONTAINS(idat.bullet[i].y - (SHOT_H - 1), idat.bullet[i].y))
  352. u8g.drawVLine(idat.bullet[i].x, idat.bullet[i].y - (SHOT_H - 1), SHOT_H);
  353. }
  354. // Draw explosion
  355. if (idat.explod.v && PAGE_CONTAINS(idat.explod.y, idat.explod.y + 7 - 1)) {
  356. u8g.drawBitmapP(idat.explod.x, idat.explod.y, 2, 7, explosion);
  357. --idat.explod.v;
  358. }
  359. // Blink GAME OVER when game is over
  360. if (!game_state) draw_game_over();
  361. if (PAGE_UNDER(MENU_FONT_ASCENT - 1)) {
  362. // Draw Score
  363. //const uint8_t sx = (LCD_PIXEL_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * MENU_FONT_WIDTH) / 2;
  364. constexpr uint8_t sx = 0;
  365. lcd_put_int(sx, MENU_FONT_ASCENT - 1, score);
  366. // Draw lives
  367. if (idat.cannons_left)
  368. for (uint8_t i = 1; i <= idat.cannons_left; ++i)
  369. u8g.drawBitmapP(LCD_PIXEL_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life);
  370. }
  371. }
  372. void InvadersGame::enter_game() {
  373. init_game(20, game_screen); // countdown to reset invaders
  374. idat.cannons_left = 3;
  375. idat.quit_count = 10;
  376. idat.laser.v = 0;
  377. reset_invaders();
  378. reset_player();
  379. }
  380. #endif // MARLIN_INVADERS