blackbox.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*
  2. * blackbox.c: implementation of 'Black Box'.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #ifdef NO_TGMATH_H
  10. # include <math.h>
  11. #else
  12. # include <tgmath.h>
  13. #endif
  14. #include "puzzles.h"
  15. #define PREFERRED_TILE_SIZE 32
  16. #define FLASH_FRAME 0.2F
  17. /* Terminology, for ease of reading various macros scattered about the place.
  18. *
  19. * The 'arena' is the inner area where the balls are placed. This is
  20. * indexed from (0,0) to (w-1,h-1) but its offset in the grid is (1,1).
  21. *
  22. * The 'range' (firing range) is the bit around the edge where
  23. * the lasers are fired from. This is indexed from 0 --> (2*(w+h) - 1),
  24. * starting at the top left ((1,0) on the grid) and moving clockwise.
  25. *
  26. * The 'grid' is just the big array containing arena and range;
  27. * locations (0,0), (0,w+1), (h+1,w+1) and (h+1,0) are unused.
  28. */
  29. enum {
  30. COL_BACKGROUND, COL_COVER, COL_LOCK,
  31. COL_TEXT, COL_FLASHTEXT,
  32. COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
  33. COL_BALL, COL_WRONG, COL_BUTTON,
  34. COL_CURSOR,
  35. NCOLOURS
  36. };
  37. struct game_params {
  38. int w, h;
  39. int minballs, maxballs;
  40. };
  41. static game_params *default_params(void)
  42. {
  43. game_params *ret = snew(game_params);
  44. ret->w = ret->h = 8;
  45. ret->minballs = ret->maxballs = 5;
  46. return ret;
  47. }
  48. static const game_params blackbox_presets[] = {
  49. { 5, 5, 3, 3 },
  50. { 8, 8, 5, 5 },
  51. { 8, 8, 3, 6 },
  52. { 10, 10, 5, 5 },
  53. { 10, 10, 4, 10 }
  54. };
  55. static bool game_fetch_preset(int i, char **name, game_params **params)
  56. {
  57. char str[80];
  58. game_params *ret;
  59. if (i < 0 || i >= lenof(blackbox_presets))
  60. return false;
  61. ret = snew(game_params);
  62. *ret = blackbox_presets[i];
  63. if (ret->minballs == ret->maxballs)
  64. sprintf(str, "%dx%d, %d balls",
  65. ret->w, ret->h, ret->minballs);
  66. else
  67. sprintf(str, "%dx%d, %d-%d balls",
  68. ret->w, ret->h, ret->minballs, ret->maxballs);
  69. *name = dupstr(str);
  70. *params = ret;
  71. return true;
  72. }
  73. static void free_params(game_params *params)
  74. {
  75. sfree(params);
  76. }
  77. static game_params *dup_params(const game_params *params)
  78. {
  79. game_params *ret = snew(game_params);
  80. *ret = *params; /* structure copy */
  81. return ret;
  82. }
  83. static void decode_params(game_params *params, char const *string)
  84. {
  85. char const *p = string;
  86. game_params *defs = default_params();
  87. *params = *defs; free_params(defs);
  88. while (*p) {
  89. switch (*p++) {
  90. case 'w':
  91. params->w = atoi(p);
  92. while (*p && isdigit((unsigned char)*p)) p++;
  93. break;
  94. case 'h':
  95. params->h = atoi(p);
  96. while (*p && isdigit((unsigned char)*p)) p++;
  97. break;
  98. case 'm':
  99. params->minballs = atoi(p);
  100. while (*p && isdigit((unsigned char)*p)) p++;
  101. break;
  102. case 'M':
  103. params->maxballs = atoi(p);
  104. while (*p && isdigit((unsigned char)*p)) p++;
  105. break;
  106. default:
  107. ;
  108. }
  109. }
  110. }
  111. static char *encode_params(const game_params *params, bool full)
  112. {
  113. char str[256];
  114. sprintf(str, "w%dh%dm%dM%d",
  115. params->w, params->h, params->minballs, params->maxballs);
  116. return dupstr(str);
  117. }
  118. static config_item *game_configure(const game_params *params)
  119. {
  120. config_item *ret;
  121. char buf[80];
  122. ret = snewn(4, config_item);
  123. ret[0].name = "Width";
  124. ret[0].type = C_STRING;
  125. sprintf(buf, "%d", params->w);
  126. ret[0].u.string.sval = dupstr(buf);
  127. ret[1].name = "Height";
  128. ret[1].type = C_STRING;
  129. sprintf(buf, "%d", params->h);
  130. ret[1].u.string.sval = dupstr(buf);
  131. ret[2].name = "No. of balls";
  132. ret[2].type = C_STRING;
  133. if (params->minballs == params->maxballs)
  134. sprintf(buf, "%d", params->minballs);
  135. else
  136. sprintf(buf, "%d-%d", params->minballs, params->maxballs);
  137. ret[2].u.string.sval = dupstr(buf);
  138. ret[3].name = NULL;
  139. ret[3].type = C_END;
  140. return ret;
  141. }
  142. static game_params *custom_params(const config_item *cfg)
  143. {
  144. game_params *ret = snew(game_params);
  145. ret->w = atoi(cfg[0].u.string.sval);
  146. ret->h = atoi(cfg[1].u.string.sval);
  147. /* Allow 'a-b' for a range, otherwise assume a single number. */
  148. if (sscanf(cfg[2].u.string.sval, "%d-%d",
  149. &ret->minballs, &ret->maxballs) < 2)
  150. ret->minballs = ret->maxballs = atoi(cfg[2].u.string.sval);
  151. return ret;
  152. }
  153. static const char *validate_params(const game_params *params, bool full)
  154. {
  155. if (params->w < 2 || params->h < 2)
  156. return "Width and height must both be at least two";
  157. /* next one is just for ease of coding stuff into 'char'
  158. * types, and could be worked around if required. */
  159. if (params->w > 255 || params->h > 255)
  160. return "Widths and heights greater than 255 are not supported";
  161. if (params->minballs < 0)
  162. return "Negative number of balls";
  163. if (params->minballs > params->maxballs)
  164. return "Minimum number of balls may not be greater than maximum";
  165. if (params->minballs >= params->w * params->h)
  166. return "Too many balls to fit in grid";
  167. return NULL;
  168. }
  169. /*
  170. * We store: width | height | ball1x | ball1y | [ ball2x | ball2y | [...] ]
  171. * all stored as unsigned chars; validate_params has already
  172. * checked this won't overflow an 8-bit char.
  173. * Then we obfuscate it.
  174. */
  175. static char *new_game_desc(const game_params *params, random_state *rs,
  176. char **aux, bool interactive)
  177. {
  178. int nballs = params->minballs, i;
  179. char *grid, *ret;
  180. unsigned char *bmp;
  181. if (params->maxballs > params->minballs)
  182. nballs += random_upto(rs, params->maxballs - params->minballs + 1);
  183. grid = snewn(params->w*params->h, char);
  184. memset(grid, 0, params->w * params->h * sizeof(char));
  185. bmp = snewn(nballs*2 + 2, unsigned char);
  186. memset(bmp, 0, (nballs*2 + 2) * sizeof(unsigned char));
  187. bmp[0] = params->w;
  188. bmp[1] = params->h;
  189. for (i = 0; i < nballs; i++) {
  190. int x, y;
  191. do {
  192. x = random_upto(rs, params->w);
  193. y = random_upto(rs, params->h);
  194. } while (grid[y*params->w + x]);
  195. grid[y*params->w + x] = 1;
  196. bmp[(i+1)*2 + 0] = x;
  197. bmp[(i+1)*2 + 1] = y;
  198. }
  199. sfree(grid);
  200. obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, false);
  201. ret = bin2hex(bmp, nballs*2 + 2);
  202. sfree(bmp);
  203. return ret;
  204. }
  205. static const char *validate_desc(const game_params *params, const char *desc)
  206. {
  207. int nballs, dlen = strlen(desc), i;
  208. unsigned char *bmp;
  209. const char *ret;
  210. /* the bitmap is 2+(nballs*2) long; the hex version is double that. */
  211. nballs = ((dlen/2)-2)/2;
  212. if (dlen < 4 || dlen % 4 ||
  213. nballs < params->minballs || nballs > params->maxballs)
  214. return "Game description is wrong length";
  215. bmp = hex2bin(desc, nballs*2 + 2);
  216. obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, true);
  217. ret = "Game description is corrupted";
  218. /* check general grid size */
  219. if (bmp[0] != params->w || bmp[1] != params->h)
  220. goto done;
  221. /* check each ball will fit on that grid */
  222. for (i = 0; i < nballs; i++) {
  223. int x = bmp[(i+1)*2 + 0], y = bmp[(i+1)*2 + 1];
  224. if (x < 0 || y < 0 || x >= params->w || y >= params->h)
  225. goto done;
  226. }
  227. ret = NULL;
  228. done:
  229. sfree(bmp);
  230. return ret;
  231. }
  232. #define BALL_CORRECT 0x01
  233. #define BALL_GUESS 0x02
  234. #define BALL_LOCK 0x04
  235. #define LASER_FLAGMASK 0x1f800
  236. #define LASER_OMITTED 0x0800
  237. #define LASER_REFLECT 0x1000
  238. #define LASER_HIT 0x2000
  239. #define LASER_WRONG 0x4000
  240. #define LASER_FLASHED 0x8000
  241. #define LASER_EMPTY (~0)
  242. #define FLAG_CURSOR 0x10000 /* needs to be disjoint from both sets */
  243. struct game_state {
  244. int w, h, minballs, maxballs, nballs, nlasers;
  245. unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */
  246. unsigned int *exits; /* one per laser */
  247. bool done; /* user has finished placing his own balls. */
  248. int laserno; /* number of next laser to be fired. */
  249. int nguesses, nright, nwrong, nmissed;
  250. bool reveal, justwrong;
  251. };
  252. #define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
  253. #define RANGECHECK(s,x) ((x) >= 0 && (x) < (s)->nlasers)
  254. /* specify numbers because they must match array indexes. */
  255. enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
  256. struct offset { int x, y; };
  257. static const struct offset offsets[] = {
  258. { 0, -1 }, /* up */
  259. { 1, 0 }, /* right */
  260. { 0, 1 }, /* down */
  261. { -1, 0 } /* left */
  262. };
  263. #ifdef DEBUGGING
  264. static const char *dirstrs[] = {
  265. "UP", "RIGHT", "DOWN", "LEFT"
  266. };
  267. #endif
  268. static bool range2grid(const game_state *state, int rangeno, int *x, int *y,
  269. int *direction)
  270. {
  271. if (rangeno < 0)
  272. return false;
  273. if (rangeno < state->w) {
  274. /* top row; from (1,0) to (w,0) */
  275. *x = rangeno + 1;
  276. *y = 0;
  277. *direction = DIR_DOWN;
  278. return true;
  279. }
  280. rangeno -= state->w;
  281. if (rangeno < state->h) {
  282. /* RHS; from (w+1, 1) to (w+1, h) */
  283. *x = state->w+1;
  284. *y = rangeno + 1;
  285. *direction = DIR_LEFT;
  286. return true;
  287. }
  288. rangeno -= state->h;
  289. if (rangeno < state->w) {
  290. /* bottom row; from (1, h+1) to (w, h+1); counts backwards */
  291. *x = (state->w - rangeno);
  292. *y = state->h+1;
  293. *direction = DIR_UP;
  294. return true;
  295. }
  296. rangeno -= state->w;
  297. if (rangeno < state->h) {
  298. /* LHS; from (0, 1) to (0, h); counts backwards */
  299. *x = 0;
  300. *y = (state->h - rangeno);
  301. *direction = DIR_RIGHT;
  302. return true;
  303. }
  304. return false;
  305. }
  306. static bool grid2range(const game_state *state, int x, int y, int *rangeno)
  307. {
  308. int ret, x1 = state->w+1, y1 = state->h+1;
  309. if (x > 0 && x < x1 && y > 0 && y < y1) return false; /* in arena */
  310. if (x < 0 || x > x1 || y < 0 || y > y1) return false; /* outside grid */
  311. if ((x == 0 || x == x1) && (y == 0 || y == y1))
  312. return false; /* one of 4 corners */
  313. if (y == 0) { /* top line */
  314. ret = x - 1;
  315. } else if (x == x1) { /* RHS */
  316. ret = y - 1 + state->w;
  317. } else if (y == y1) { /* Bottom [and counts backwards] */
  318. ret = (state->w - x) + state->w + state->h;
  319. } else { /* LHS [and counts backwards ] */
  320. ret = (state->h-y) + state->w + state->w + state->h;
  321. }
  322. *rangeno = ret;
  323. debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
  324. return true;
  325. }
  326. static game_state *new_game(midend *me, const game_params *params,
  327. const char *desc)
  328. {
  329. game_state *state = snew(game_state);
  330. int dlen = strlen(desc), i;
  331. unsigned char *bmp;
  332. state->minballs = params->minballs;
  333. state->maxballs = params->maxballs;
  334. state->nballs = ((dlen/2)-2)/2;
  335. bmp = hex2bin(desc, state->nballs*2 + 2);
  336. obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, true);
  337. state->w = bmp[0]; state->h = bmp[1];
  338. state->nlasers = 2 * (state->w + state->h);
  339. state->grid = snewn((state->w+2)*(state->h+2), unsigned int);
  340. memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int));
  341. state->exits = snewn(state->nlasers, unsigned int);
  342. memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int));
  343. for (i = 0; i < state->nballs; i++) {
  344. GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT;
  345. }
  346. sfree(bmp);
  347. state->done = false;
  348. state->justwrong = false;
  349. state->reveal = false;
  350. state->nguesses = state->nright = state->nwrong = state->nmissed = 0;
  351. state->laserno = 1;
  352. return state;
  353. }
  354. #define XFER(x) ret->x = state->x
  355. static game_state *dup_game(const game_state *state)
  356. {
  357. game_state *ret = snew(game_state);
  358. XFER(w); XFER(h);
  359. XFER(minballs); XFER(maxballs);
  360. XFER(nballs); XFER(nlasers);
  361. ret->grid = snewn((ret->w+2)*(ret->h+2), unsigned int);
  362. memcpy(ret->grid, state->grid, (ret->w+2)*(ret->h+2) * sizeof(unsigned int));
  363. ret->exits = snewn(ret->nlasers, unsigned int);
  364. memcpy(ret->exits, state->exits, ret->nlasers * sizeof(unsigned int));
  365. XFER(done);
  366. XFER(laserno);
  367. XFER(nguesses);
  368. XFER(reveal);
  369. XFER(justwrong);
  370. XFER(nright); XFER(nwrong); XFER(nmissed);
  371. return ret;
  372. }
  373. #undef XFER
  374. static void free_game(game_state *state)
  375. {
  376. sfree(state->exits);
  377. sfree(state->grid);
  378. sfree(state);
  379. }
  380. static char *solve_game(const game_state *state, const game_state *currstate,
  381. const char *aux, const char **error)
  382. {
  383. return dupstr("S");
  384. }
  385. struct game_ui {
  386. int flash_laserno;
  387. int errors;
  388. bool newmove;
  389. int cur_x, cur_y;
  390. bool cur_visible;
  391. int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */
  392. };
  393. static game_ui *new_ui(const game_state *state)
  394. {
  395. game_ui *ui = snew(game_ui);
  396. ui->flash_laserno = LASER_EMPTY;
  397. ui->errors = 0;
  398. ui->newmove = false;
  399. ui->cur_x = ui->cur_y = 1;
  400. ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  401. ui->flash_laser = 0;
  402. return ui;
  403. }
  404. static void free_ui(game_ui *ui)
  405. {
  406. sfree(ui);
  407. }
  408. static char *encode_ui(const game_ui *ui)
  409. {
  410. char buf[80];
  411. /*
  412. * The error counter needs preserving across a serialisation.
  413. */
  414. sprintf(buf, "E%d", ui->errors);
  415. return dupstr(buf);
  416. }
  417. static void decode_ui(game_ui *ui, const char *encoding,
  418. const game_state *state)
  419. {
  420. sscanf(encoding, "E%d", &ui->errors);
  421. }
  422. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  423. const game_state *newstate)
  424. {
  425. /*
  426. * If we've encountered a `justwrong' state as a result of
  427. * actually making a move, increment the ui error counter.
  428. */
  429. if (newstate->justwrong && ui->newmove)
  430. ui->errors++;
  431. ui->newmove = false;
  432. }
  433. static const char *current_key_label(const game_ui *ui,
  434. const game_state *state, int button)
  435. {
  436. if (IS_CURSOR_SELECT(button) && ui->cur_visible && !state->reveal) {
  437. int gx = ui->cur_x, gy = ui->cur_y, rangeno = -1;
  438. if (gx == 0 && gy == 0 && button == CURSOR_SELECT) return "Check";
  439. if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
  440. /* Cursor somewhere in the arena. */
  441. if (button == CURSOR_SELECT && !(GRID(state, gx,gy) & BALL_LOCK))
  442. return (GRID(state, gx, gy) & BALL_GUESS) ? "Clear" : "Ball";
  443. if (button == CURSOR_SELECT2)
  444. return (GRID(state, gx, gy) & BALL_LOCK) ? "Unlock" : "Lock";
  445. }
  446. if (grid2range(state, gx, gy, &rangeno)) {
  447. if (button == CURSOR_SELECT &&
  448. state->exits[rangeno] == LASER_EMPTY)
  449. return "Fire";
  450. if (button == CURSOR_SELECT2) {
  451. int n = 0;
  452. /* Row or column lock or unlock. */
  453. if (gy == 0 || gy > state->h) { /* Column lock */
  454. for (gy = 1; gy <= state->h; gy++)
  455. n += !!(GRID(state, gx, gy) & BALL_LOCK);
  456. return n > state->h/2 ? "Unlock" : "Lock";
  457. } else { /* Row lock */
  458. for (gx = 1; gx <= state->w; gx++)
  459. n += !!(GRID(state, gx, gy) & BALL_LOCK);
  460. return n > state->w/2 ? "Unlock" : "Lock";
  461. }
  462. }
  463. }
  464. }
  465. return "";
  466. }
  467. #define OFFSET(gx,gy,o) do { \
  468. int off = (4 + (o) % 4) % 4; \
  469. (gx) += offsets[off].x; \
  470. (gy) += offsets[off].y; \
  471. } while(0)
  472. enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
  473. /* Given a position and a direction, check whether we can see a ball in front
  474. * of us, or to our front-left or front-right. */
  475. static bool isball(game_state *state, int gx, int gy, int direction, int lookwhere)
  476. {
  477. debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction],
  478. lookwhere == LOOK_LEFT ? "LEFT" :
  479. lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT"));
  480. OFFSET(gx,gy,direction);
  481. if (lookwhere == LOOK_LEFT)
  482. OFFSET(gx,gy,direction-1);
  483. else if (lookwhere == LOOK_RIGHT)
  484. OFFSET(gx,gy,direction+1);
  485. else if (lookwhere != LOOK_FORWARD)
  486. assert(!"unknown lookwhere");
  487. debug(("isball, new (%d, %d)\n", gx, gy));
  488. /* if we're off the grid (into the firing range) there's never a ball. */
  489. if (gx < 1 || gy < 1 || gx > state->w || gy > state->h)
  490. return false;
  491. if (GRID(state, gx,gy) & BALL_CORRECT)
  492. return true;
  493. return false;
  494. }
  495. static int fire_laser_internal(game_state *state, int x, int y, int direction)
  496. {
  497. int unused, lno;
  498. bool tmp;
  499. tmp = grid2range(state, x, y, &lno);
  500. assert(tmp);
  501. /* deal with strange initial reflection rules (that stop
  502. * you turning down the laser range) */
  503. /* I've just chosen to prioritise instant-hit over instant-reflection;
  504. * I can't find anywhere that gives me a definite algorithm for this. */
  505. if (isball(state, x, y, direction, LOOK_FORWARD)) {
  506. debug(("Instant hit at (%d, %d)\n", x, y));
  507. return LASER_HIT; /* hit */
  508. }
  509. if (isball(state, x, y, direction, LOOK_LEFT) ||
  510. isball(state, x, y, direction, LOOK_RIGHT)) {
  511. debug(("Instant reflection at (%d, %d)\n", x, y));
  512. return LASER_REFLECT; /* reflection */
  513. }
  514. /* move us onto the grid. */
  515. OFFSET(x, y, direction);
  516. while (1) {
  517. debug(("fire_laser: looping at (%d, %d) pointing %s\n",
  518. x, y, dirstrs[direction]));
  519. if (grid2range(state, x, y, &unused)) {
  520. int exitno;
  521. tmp = grid2range(state, x, y, &exitno);
  522. assert(tmp);
  523. return (lno == exitno ? LASER_REFLECT : exitno);
  524. }
  525. /* paranoia. This obviously should never happen */
  526. assert(!(GRID(state, x, y) & BALL_CORRECT));
  527. if (isball(state, x, y, direction, LOOK_FORWARD)) {
  528. /* we're facing a ball; send back a reflection. */
  529. debug(("Ball ahead of (%d, %d)", x, y));
  530. return LASER_HIT; /* hit */
  531. }
  532. if (isball(state, x, y, direction, LOOK_LEFT)) {
  533. /* ball to our left; rotate clockwise and look again. */
  534. debug(("Ball to left; turning clockwise.\n"));
  535. direction += 1; direction %= 4;
  536. continue;
  537. }
  538. if (isball(state, x, y, direction, LOOK_RIGHT)) {
  539. /* ball to our right; rotate anti-clockwise and look again. */
  540. debug(("Ball to rightl turning anti-clockwise.\n"));
  541. direction += 3; direction %= 4;
  542. continue;
  543. }
  544. /* ... otherwise, we have no balls ahead of us so just move one step. */
  545. debug(("No balls; moving forwards.\n"));
  546. OFFSET(x, y, direction);
  547. }
  548. }
  549. static int laser_exit(game_state *state, int entryno)
  550. {
  551. int x, y, direction;
  552. bool tmp;
  553. tmp = range2grid(state, entryno, &x, &y, &direction);
  554. assert(tmp);
  555. return fire_laser_internal(state, x, y, direction);
  556. }
  557. static void fire_laser(game_state *state, int entryno)
  558. {
  559. int exitno, x, y, direction;
  560. bool tmp;
  561. tmp = range2grid(state, entryno, &x, &y, &direction);
  562. assert(tmp);
  563. exitno = fire_laser_internal(state, x, y, direction);
  564. if (exitno == LASER_HIT || exitno == LASER_REFLECT) {
  565. GRID(state, x, y) = state->exits[entryno] = exitno;
  566. } else {
  567. int newno = state->laserno++;
  568. int xend, yend, unused;
  569. tmp = range2grid(state, exitno, &xend, &yend, &unused);
  570. assert(tmp);
  571. GRID(state, x, y) = GRID(state, xend, yend) = newno;
  572. state->exits[entryno] = exitno;
  573. state->exits[exitno] = entryno;
  574. }
  575. }
  576. /* Checks that the guessed balls in the state match up with the real balls
  577. * for all possible lasers (i.e. not just the ones that the player might
  578. * have already guessed). This is required because any layout with >4 balls
  579. * might have multiple valid solutions. Returns non-zero for a 'correct'
  580. * (i.e. consistent) layout. */
  581. static int check_guesses(game_state *state, bool cagey)
  582. {
  583. game_state *solution, *guesses;
  584. int i, x, y, n, unused, tmp;
  585. bool tmpb;
  586. int ret = 0;
  587. if (cagey) {
  588. /*
  589. * First, check that each laser the player has already
  590. * fired is consistent with the layout. If not, show them
  591. * one error they've made and reveal no further
  592. * information.
  593. *
  594. * Failing that, check to see whether the player would have
  595. * been able to fire any laser which distinguished the real
  596. * solution from their guess. If so, show them one such
  597. * laser and reveal no further information.
  598. */
  599. guesses = dup_game(state);
  600. /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
  601. for (x = 1; x <= state->w; x++) {
  602. for (y = 1; y <= state->h; y++) {
  603. GRID(guesses, x, y) &= ~BALL_CORRECT;
  604. if (GRID(guesses, x, y) & BALL_GUESS)
  605. GRID(guesses, x, y) |= BALL_CORRECT;
  606. }
  607. }
  608. n = 0;
  609. for (i = 0; i < guesses->nlasers; i++) {
  610. if (guesses->exits[i] != LASER_EMPTY &&
  611. guesses->exits[i] != laser_exit(guesses, i))
  612. n++;
  613. }
  614. if (n) {
  615. /*
  616. * At least one of the player's existing lasers
  617. * contradicts their ball placement. Pick a random one,
  618. * highlight it, and return.
  619. *
  620. * A temporary random state is created from the current
  621. * grid, so that repeating the same marking will give
  622. * the same answer instead of a different one.
  623. */
  624. random_state *rs = random_new((char *)guesses->grid,
  625. (state->w+2)*(state->h+2) *
  626. sizeof(unsigned int));
  627. n = random_upto(rs, n);
  628. random_free(rs);
  629. for (i = 0; i < guesses->nlasers; i++) {
  630. if (guesses->exits[i] != LASER_EMPTY &&
  631. guesses->exits[i] != laser_exit(guesses, i) &&
  632. n-- == 0) {
  633. state->exits[i] |= LASER_WRONG;
  634. tmp = laser_exit(state, i);
  635. if (RANGECHECK(state, tmp))
  636. state->exits[tmp] |= LASER_WRONG;
  637. state->justwrong = true;
  638. free_game(guesses);
  639. return 0;
  640. }
  641. }
  642. }
  643. n = 0;
  644. for (i = 0; i < guesses->nlasers; i++) {
  645. if (guesses->exits[i] == LASER_EMPTY &&
  646. laser_exit(state, i) != laser_exit(guesses, i))
  647. n++;
  648. }
  649. if (n) {
  650. /*
  651. * At least one of the player's unfired lasers would
  652. * demonstrate their ball placement to be wrong. Pick a
  653. * random one, highlight it, and return.
  654. *
  655. * A temporary random state is created from the current
  656. * grid, so that repeating the same marking will give
  657. * the same answer instead of a different one.
  658. */
  659. random_state *rs = random_new((char *)guesses->grid,
  660. (state->w+2)*(state->h+2) *
  661. sizeof(unsigned int));
  662. n = random_upto(rs, n);
  663. random_free(rs);
  664. for (i = 0; i < guesses->nlasers; i++) {
  665. if (guesses->exits[i] == LASER_EMPTY &&
  666. laser_exit(state, i) != laser_exit(guesses, i) &&
  667. n-- == 0) {
  668. fire_laser(state, i);
  669. state->exits[i] |= LASER_OMITTED;
  670. tmp = laser_exit(state, i);
  671. if (RANGECHECK(state, tmp))
  672. state->exits[tmp] |= LASER_OMITTED;
  673. state->justwrong = true;
  674. free_game(guesses);
  675. return 0;
  676. }
  677. }
  678. }
  679. free_game(guesses);
  680. }
  681. /* duplicate the state (to solution) */
  682. solution = dup_game(state);
  683. /* clear out the lasers of solution */
  684. for (i = 0; i < solution->nlasers; i++) {
  685. tmpb = range2grid(solution, i, &x, &y, &unused);
  686. assert(tmpb);
  687. GRID(solution, x, y) = 0;
  688. solution->exits[i] = LASER_EMPTY;
  689. }
  690. /* duplicate solution to guess. */
  691. guesses = dup_game(solution);
  692. /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
  693. for (x = 1; x <= state->w; x++) {
  694. for (y = 1; y <= state->h; y++) {
  695. GRID(guesses, x, y) &= ~BALL_CORRECT;
  696. if (GRID(guesses, x, y) & BALL_GUESS)
  697. GRID(guesses, x, y) |= BALL_CORRECT;
  698. }
  699. }
  700. /* for each laser (on both game_states), fire it if it hasn't been fired.
  701. * If one has been fired (or received a hit) and another hasn't, we know
  702. * the ball layouts didn't match and can short-circuit return. */
  703. for (i = 0; i < solution->nlasers; i++) {
  704. if (solution->exits[i] == LASER_EMPTY)
  705. fire_laser(solution, i);
  706. if (guesses->exits[i] == LASER_EMPTY)
  707. fire_laser(guesses, i);
  708. }
  709. /* check each game_state's laser against the other; if any differ, return 0 */
  710. ret = 1;
  711. for (i = 0; i < solution->nlasers; i++) {
  712. tmpb = range2grid(solution, i, &x, &y, &unused);
  713. assert(tmpb);
  714. if (solution->exits[i] != guesses->exits[i]) {
  715. /* If the original state didn't have this shot fired,
  716. * and it would be wrong between the guess and the solution,
  717. * add it. */
  718. if (state->exits[i] == LASER_EMPTY) {
  719. state->exits[i] = solution->exits[i];
  720. if (state->exits[i] == LASER_REFLECT ||
  721. state->exits[i] == LASER_HIT)
  722. GRID(state, x, y) = state->exits[i];
  723. else {
  724. /* add a new shot, incrementing state's laser count. */
  725. int ex, ey, newno = state->laserno++;
  726. tmpb = range2grid(state, state->exits[i], &ex, &ey, &unused);
  727. assert(tmpb);
  728. GRID(state, x, y) = newno;
  729. GRID(state, ex, ey) = newno;
  730. }
  731. state->exits[i] |= LASER_OMITTED;
  732. } else {
  733. state->exits[i] |= LASER_WRONG;
  734. }
  735. ret = 0;
  736. }
  737. }
  738. if (ret == 0 ||
  739. state->nguesses < state->minballs ||
  740. state->nguesses > state->maxballs) goto done;
  741. /* fix up original state so the 'correct' balls end up matching the guesses,
  742. * as we've just proved that they were equivalent. */
  743. for (x = 1; x <= state->w; x++) {
  744. for (y = 1; y <= state->h; y++) {
  745. if (GRID(state, x, y) & BALL_GUESS)
  746. GRID(state, x, y) |= BALL_CORRECT;
  747. else
  748. GRID(state, x, y) &= ~BALL_CORRECT;
  749. }
  750. }
  751. done:
  752. /* fill in nright and nwrong. */
  753. state->nright = state->nwrong = state->nmissed = 0;
  754. for (x = 1; x <= state->w; x++) {
  755. for (y = 1; y <= state->h; y++) {
  756. int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT);
  757. if (bs == (BALL_GUESS | BALL_CORRECT))
  758. state->nright++;
  759. else if (bs == BALL_GUESS)
  760. state->nwrong++;
  761. else if (bs == BALL_CORRECT)
  762. state->nmissed++;
  763. }
  764. }
  765. free_game(solution);
  766. free_game(guesses);
  767. state->reveal = true;
  768. return ret;
  769. }
  770. #define TILE_SIZE (ds->tilesize)
  771. #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
  772. #define FROMDRAW(x) (((x) + (TILE_SIZE / 2)) / TILE_SIZE - 1)
  773. #define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
  774. (state)->nguesses <= (state)->maxballs && \
  775. !(state)->reveal && !(state)->justwrong)
  776. struct game_drawstate {
  777. int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
  778. unsigned int *grid; /* as the game_state grid */
  779. bool started, reveal, isflash;
  780. int flash_laserno;
  781. };
  782. static char *interpret_move(const game_state *state, game_ui *ui,
  783. const game_drawstate *ds,
  784. int x, int y, int button)
  785. {
  786. int gx = -1, gy = -1, rangeno = -1, wouldflash = 0;
  787. enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
  788. TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
  789. char buf[80], *nullret = NULL;
  790. if (IS_CURSOR_MOVE(button)) {
  791. int cx = ui->cur_x, cy = ui->cur_y;
  792. move_cursor(button, &cx, &cy, state->w+2, state->h+2, false);
  793. if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) ||
  794. (cx == 0 && cy == state->h+1) ||
  795. (cx == state->w+1 && cy == 0) ||
  796. (cx == state->w+1 && cy == state->h+1))
  797. return NULL; /* disallow moving cursor to corners. */
  798. ui->cur_x = cx;
  799. ui->cur_y = cy;
  800. ui->cur_visible = true;
  801. return MOVE_UI_UPDATE;
  802. }
  803. if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
  804. gx = FROMDRAW(x);
  805. gy = FROMDRAW(y);
  806. ui->cur_visible = false;
  807. wouldflash = 1;
  808. } else if (button == LEFT_RELEASE) {
  809. ui->flash_laser = 0;
  810. return MOVE_UI_UPDATE;
  811. } else if (IS_CURSOR_SELECT(button)) {
  812. if (ui->cur_visible) {
  813. gx = ui->cur_x;
  814. gy = ui->cur_y;
  815. ui->flash_laser = 0;
  816. wouldflash = 2;
  817. } else {
  818. ui->cur_visible = true;
  819. return MOVE_UI_UPDATE;
  820. }
  821. /* Fix up 'button' for the below logic. */
  822. if (button == CURSOR_SELECT2) button = RIGHT_BUTTON;
  823. else button = LEFT_BUTTON;
  824. }
  825. if (gx != -1 && gy != -1) {
  826. if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
  827. action = REVEAL;
  828. if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
  829. if (button == LEFT_BUTTON) {
  830. if (!(GRID(state, gx,gy) & BALL_LOCK))
  831. action = TOGGLE_BALL;
  832. } else
  833. action = TOGGLE_LOCK;
  834. }
  835. if (grid2range(state, gx, gy, &rangeno)) {
  836. if (button == LEFT_BUTTON)
  837. action = FIRE;
  838. else if (gy == 0 || gy > state->h)
  839. action = TOGGLE_COLUMN_LOCK; /* and use gx */
  840. else
  841. action = TOGGLE_ROW_LOCK; /* and use gy */
  842. }
  843. }
  844. switch (action) {
  845. case TOGGLE_BALL:
  846. sprintf(buf, "T%d,%d", gx, gy);
  847. break;
  848. case TOGGLE_LOCK:
  849. sprintf(buf, "LB%d,%d", gx, gy);
  850. break;
  851. case TOGGLE_COLUMN_LOCK:
  852. sprintf(buf, "LC%d", gx);
  853. break;
  854. case TOGGLE_ROW_LOCK:
  855. sprintf(buf, "LR%d", gy);
  856. break;
  857. case FIRE:
  858. if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
  859. return nullret;
  860. ui->flash_laserno = rangeno;
  861. ui->flash_laser = wouldflash;
  862. nullret = MOVE_UI_UPDATE;
  863. if (state->exits[rangeno] != LASER_EMPTY)
  864. return MOVE_UI_UPDATE;
  865. sprintf(buf, "F%d", rangeno);
  866. break;
  867. case REVEAL:
  868. if (!CAN_REVEAL(state)) return nullret;
  869. if (ui->cur_visible) ui->cur_x = ui->cur_y = 1;
  870. sprintf(buf, "R");
  871. break;
  872. default:
  873. return nullret;
  874. }
  875. if (state->reveal) return nullret;
  876. ui->newmove = true;
  877. return dupstr(buf);
  878. }
  879. static game_state *execute_move(const game_state *from, const char *move)
  880. {
  881. game_state *ret = dup_game(from);
  882. int gx = -1, gy = -1, rangeno = -1;
  883. if (ret->justwrong) {
  884. int i;
  885. ret->justwrong = false;
  886. for (i = 0; i < ret->nlasers; i++)
  887. if (ret->exits[i] != LASER_EMPTY)
  888. ret->exits[i] &= ~(LASER_OMITTED | LASER_WRONG);
  889. }
  890. if (!strcmp(move, "S")) {
  891. check_guesses(ret, false);
  892. return ret;
  893. }
  894. if (from->reveal) goto badmove;
  895. if (!*move) goto badmove;
  896. switch (move[0]) {
  897. case 'T':
  898. sscanf(move+1, "%d,%d", &gx, &gy);
  899. if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
  900. goto badmove;
  901. if (GRID(ret, gx, gy) & BALL_GUESS) {
  902. ret->nguesses--;
  903. GRID(ret, gx, gy) &= ~BALL_GUESS;
  904. } else {
  905. ret->nguesses++;
  906. GRID(ret, gx, gy) |= BALL_GUESS;
  907. }
  908. break;
  909. case 'F':
  910. sscanf(move+1, "%d", &rangeno);
  911. if (!RANGECHECK(ret, rangeno))
  912. goto badmove;
  913. if (ret->exits[rangeno] != LASER_EMPTY)
  914. goto badmove;
  915. fire_laser(ret, rangeno);
  916. break;
  917. case 'R':
  918. if (ret->nguesses < ret->minballs ||
  919. ret->nguesses > ret->maxballs)
  920. goto badmove;
  921. check_guesses(ret, true);
  922. break;
  923. case 'L':
  924. {
  925. int lcount = 0;
  926. if (strlen(move) < 2) goto badmove;
  927. switch (move[1]) {
  928. case 'B':
  929. sscanf(move+2, "%d,%d", &gx, &gy);
  930. if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
  931. goto badmove;
  932. GRID(ret, gx, gy) ^= BALL_LOCK;
  933. break;
  934. #define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0)
  935. #define SETLOCKIF(c) do { \
  936. if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \
  937. else GRID(ret, gx, gy) |= BALL_LOCK; \
  938. } while(0)
  939. case 'C':
  940. sscanf(move+2, "%d", &gx);
  941. if (gx < 1 || gx > ret->w) goto badmove;
  942. for (gy = 1; gy <= ret->h; gy++) { COUNTLOCK; }
  943. for (gy = 1; gy <= ret->h; gy++) { SETLOCKIF(ret->h/2); }
  944. break;
  945. case 'R':
  946. sscanf(move+2, "%d", &gy);
  947. if (gy < 1 || gy > ret->h) goto badmove;
  948. for (gx = 1; gx <= ret->w; gx++) { COUNTLOCK; }
  949. for (gx = 1; gx <= ret->w; gx++) { SETLOCKIF(ret->w/2); }
  950. break;
  951. #undef COUNTLOCK
  952. #undef SETLOCKIF
  953. default:
  954. goto badmove;
  955. }
  956. }
  957. break;
  958. default:
  959. goto badmove;
  960. }
  961. return ret;
  962. badmove:
  963. free_game(ret);
  964. return NULL;
  965. }
  966. static void game_get_cursor_location(const game_ui *ui,
  967. const game_drawstate *ds,
  968. const game_state *state,
  969. const game_params *params,
  970. int *x, int *y, int *w, int *h)
  971. {
  972. if(ui->cur_visible) {
  973. *x = TODRAW(ui->cur_x);
  974. *y = TODRAW(ui->cur_y);
  975. *w = *h = TILE_SIZE;
  976. }
  977. }
  978. /* ----------------------------------------------------------------------
  979. * Drawing routines.
  980. */
  981. static void game_compute_size(const game_params *params, int tilesize,
  982. const game_ui *ui, int *x, int *y)
  983. {
  984. /* Border is ts/2, to make things easier.
  985. * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
  986. * across, and similarly height + 2 + 1 tiles down. */
  987. *x = (params->w + 3) * tilesize;
  988. *y = (params->h + 3) * tilesize;
  989. }
  990. static void game_set_size(drawing *dr, game_drawstate *ds,
  991. const game_params *params, int tilesize)
  992. {
  993. ds->tilesize = tilesize;
  994. ds->crad = (tilesize-1)/2;
  995. ds->rrad = (3*tilesize)/8;
  996. }
  997. static float *game_colours(frontend *fe, int *ncolours)
  998. {
  999. float *ret = snewn(3 * NCOLOURS, float);
  1000. int i;
  1001. game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
  1002. ret[COL_BALL * 3 + 0] = 0.0F;
  1003. ret[COL_BALL * 3 + 1] = 0.0F;
  1004. ret[COL_BALL * 3 + 2] = 0.0F;
  1005. ret[COL_WRONG * 3 + 0] = 1.0F;
  1006. ret[COL_WRONG * 3 + 1] = 0.0F;
  1007. ret[COL_WRONG * 3 + 2] = 0.0F;
  1008. ret[COL_BUTTON * 3 + 0] = 0.0F;
  1009. ret[COL_BUTTON * 3 + 1] = 1.0F;
  1010. ret[COL_BUTTON * 3 + 2] = 0.0F;
  1011. ret[COL_CURSOR * 3 + 0] = 1.0F;
  1012. ret[COL_CURSOR * 3 + 1] = 0.0F;
  1013. ret[COL_CURSOR * 3 + 2] = 0.0F;
  1014. for (i = 0; i < 3; i++) {
  1015. ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
  1016. ret[COL_LOCK * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.7F;
  1017. ret[COL_COVER * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.5F;
  1018. ret[COL_TEXT * 3 + i] = 0.0F;
  1019. }
  1020. ret[COL_FLASHTEXT * 3 + 0] = 0.0F;
  1021. ret[COL_FLASHTEXT * 3 + 1] = 1.0F;
  1022. ret[COL_FLASHTEXT * 3 + 2] = 0.0F;
  1023. *ncolours = NCOLOURS;
  1024. return ret;
  1025. }
  1026. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1027. {
  1028. struct game_drawstate *ds = snew(struct game_drawstate);
  1029. ds->tilesize = 0;
  1030. ds->w = state->w; ds->h = state->h;
  1031. ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
  1032. memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
  1033. ds->started = false;
  1034. ds->reveal = false;
  1035. ds->flash_laserno = LASER_EMPTY;
  1036. ds->isflash = false;
  1037. return ds;
  1038. }
  1039. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1040. {
  1041. sfree(ds->grid);
  1042. sfree(ds);
  1043. }
  1044. static void draw_square_cursor(drawing *dr, game_drawstate *ds, int dx, int dy)
  1045. {
  1046. int coff = TILE_SIZE/8;
  1047. draw_rect_outline(dr, dx + coff, dy + coff,
  1048. TILE_SIZE - coff*2,
  1049. TILE_SIZE - coff*2,
  1050. COL_CURSOR);
  1051. }
  1052. static void draw_arena_tile(drawing *dr, const game_state *gs,
  1053. game_drawstate *ds, const game_ui *ui,
  1054. int ax, int ay, bool force, bool isflash)
  1055. {
  1056. int gx = ax+1, gy = ay+1;
  1057. int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
  1058. int dx = TODRAW(gx), dy = TODRAW(gy);
  1059. if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
  1060. gs_tile |= FLAG_CURSOR;
  1061. if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
  1062. int bcol, ocol, bg;
  1063. bg = (gs->reveal ? COL_BACKGROUND :
  1064. (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
  1065. draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg);
  1066. draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
  1067. if (gs->reveal) {
  1068. /* Guessed balls are always black; if they're incorrect they'll
  1069. * have a red cross added later.
  1070. * Missing balls are red. */
  1071. if (gs_tile & BALL_GUESS) {
  1072. bcol = isflash ? bg : COL_BALL;
  1073. } else if (gs_tile & BALL_CORRECT) {
  1074. bcol = isflash ? bg : COL_WRONG;
  1075. } else {
  1076. bcol = bg;
  1077. }
  1078. } else {
  1079. /* guesses are black/black, all else background. */
  1080. if (gs_tile & BALL_GUESS) {
  1081. bcol = COL_BALL;
  1082. } else {
  1083. bcol = bg;
  1084. }
  1085. }
  1086. ocol = (gs_tile & FLAG_CURSOR && bcol != bg) ? COL_CURSOR : bcol;
  1087. draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
  1088. ocol, ocol);
  1089. draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-3,
  1090. bcol, bcol);
  1091. if (gs_tile & FLAG_CURSOR && bcol == bg)
  1092. draw_square_cursor(dr, ds, dx, dy);
  1093. if (gs->reveal &&
  1094. (gs_tile & BALL_GUESS) &&
  1095. !(gs_tile & BALL_CORRECT)) {
  1096. int x1 = dx + 3, y1 = dy + 3;
  1097. int x2 = dx + TILE_SIZE - 3, y2 = dy + TILE_SIZE-3;
  1098. int coords[8];
  1099. /* Incorrect guess; draw a red cross over the ball. */
  1100. coords[0] = x1-1;
  1101. coords[1] = y1+1;
  1102. coords[2] = x1+1;
  1103. coords[3] = y1-1;
  1104. coords[4] = x2+1;
  1105. coords[5] = y2-1;
  1106. coords[6] = x2-1;
  1107. coords[7] = y2+1;
  1108. draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
  1109. coords[0] = x2+1;
  1110. coords[1] = y1+1;
  1111. coords[2] = x2-1;
  1112. coords[3] = y1-1;
  1113. coords[4] = x1-1;
  1114. coords[5] = y2-1;
  1115. coords[6] = x1+1;
  1116. coords[7] = y2+1;
  1117. draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
  1118. }
  1119. draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
  1120. }
  1121. GRID(ds,gx,gy) = gs_tile;
  1122. }
  1123. static void draw_laser_tile(drawing *dr, const game_state *gs,
  1124. game_drawstate *ds, const game_ui *ui,
  1125. int lno, bool force)
  1126. {
  1127. int gx, gy, dx, dy, unused;
  1128. int wrong, omitted, laserval;
  1129. bool tmp, reflect, hit, flash = false;
  1130. unsigned int gs_tile, ds_tile, exitno;
  1131. tmp = range2grid(gs, lno, &gx, &gy, &unused);
  1132. assert(tmp);
  1133. gs_tile = GRID(gs, gx, gy);
  1134. ds_tile = GRID(ds, gx, gy);
  1135. dx = TODRAW(gx);
  1136. dy = TODRAW(gy);
  1137. wrong = gs->exits[lno] & LASER_WRONG;
  1138. omitted = gs->exits[lno] & LASER_OMITTED;
  1139. exitno = gs->exits[lno] & ~LASER_FLAGMASK;
  1140. reflect = gs_tile & LASER_REFLECT;
  1141. hit = gs_tile & LASER_HIT;
  1142. laserval = gs_tile & ~LASER_FLAGMASK;
  1143. if (lno == ds->flash_laserno)
  1144. gs_tile |= LASER_FLASHED;
  1145. else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
  1146. if (exitno == ds->flash_laserno)
  1147. gs_tile |= LASER_FLASHED;
  1148. }
  1149. if (gs_tile & LASER_FLASHED) flash = true;
  1150. gs_tile |= wrong | omitted;
  1151. if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
  1152. gs_tile |= FLAG_CURSOR;
  1153. if (gs_tile != ds_tile || force) {
  1154. draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
  1155. draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
  1156. if (gs_tile &~ (LASER_WRONG | LASER_OMITTED | FLAG_CURSOR)) {
  1157. char str[32];
  1158. int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
  1159. if (reflect || hit)
  1160. sprintf(str, "%s", reflect ? "R" : "H");
  1161. else
  1162. sprintf(str, "%d", laserval);
  1163. if (wrong) {
  1164. draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
  1165. ds->rrad,
  1166. COL_WRONG, COL_WRONG);
  1167. draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
  1168. ds->rrad - TILE_SIZE/16,
  1169. COL_BACKGROUND, COL_WRONG);
  1170. }
  1171. draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
  1172. FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
  1173. tcol, str);
  1174. }
  1175. if (gs_tile & FLAG_CURSOR)
  1176. draw_square_cursor(dr, ds, dx, dy);
  1177. draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
  1178. }
  1179. GRID(ds, gx, gy) = gs_tile;
  1180. }
  1181. #define CUR_ANIM 0.2F
  1182. static void game_redraw(drawing *dr, game_drawstate *ds,
  1183. const game_state *oldstate, const game_state *state,
  1184. int dir, const game_ui *ui,
  1185. float animtime, float flashtime)
  1186. {
  1187. int i, x, y, ts = TILE_SIZE;
  1188. bool isflash = false, force = false;
  1189. if (flashtime > 0) {
  1190. int frame = (int)(flashtime / FLASH_FRAME);
  1191. isflash = (frame % 2) == 0;
  1192. debug(("game_redraw: flashtime = %f", flashtime));
  1193. }
  1194. if (!ds->started) {
  1195. int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
  1196. int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
  1197. /* clockwise around the outline starting at pt behind (1,1). */
  1198. draw_line(dr, x0+ts, y0+ts, x0+ts, y0, COL_HIGHLIGHT);
  1199. draw_line(dr, x0+ts, y0, x1-ts, y0, COL_HIGHLIGHT);
  1200. draw_line(dr, x1-ts, y0, x1-ts, y0+ts, COL_LOWLIGHT);
  1201. draw_line(dr, x1-ts, y0+ts, x1, y0+ts, COL_HIGHLIGHT);
  1202. draw_line(dr, x1, y0+ts, x1, y1-ts, COL_LOWLIGHT);
  1203. draw_line(dr, x1, y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
  1204. draw_line(dr, x1-ts, y1-ts, x1-ts, y1, COL_LOWLIGHT);
  1205. draw_line(dr, x1-ts, y1, x0+ts, y1, COL_LOWLIGHT);
  1206. draw_line(dr, x0+ts, y1, x0+ts, y1-ts, COL_HIGHLIGHT);
  1207. draw_line(dr, x0+ts, y1-ts, x0, y1-ts, COL_LOWLIGHT);
  1208. draw_line(dr, x0, y1-ts, x0, y0+ts, COL_HIGHLIGHT);
  1209. draw_line(dr, x0, y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
  1210. /* phew... */
  1211. draw_update(dr, 0, 0,
  1212. TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
  1213. force = true;
  1214. ds->started = true;
  1215. }
  1216. if (isflash != ds->isflash) force = true;
  1217. /* draw the arena */
  1218. for (x = 0; x < state->w; x++) {
  1219. for (y = 0; y < state->h; y++) {
  1220. draw_arena_tile(dr, state, ds, ui, x, y, force, isflash);
  1221. }
  1222. }
  1223. /* draw the lasers */
  1224. ds->flash_laserno = LASER_EMPTY;
  1225. if (ui->flash_laser == 1)
  1226. ds->flash_laserno = ui->flash_laserno;
  1227. else if (ui->flash_laser == 2 && animtime > 0)
  1228. ds->flash_laserno = ui->flash_laserno;
  1229. for (i = 0; i < 2*(state->w+state->h); i++) {
  1230. draw_laser_tile(dr, state, ds, ui, i, force);
  1231. }
  1232. /* draw the 'finish' button */
  1233. if (CAN_REVEAL(state)) {
  1234. int outline = (ui->cur_visible && ui->cur_x == 0 && ui->cur_y == 0)
  1235. ? COL_CURSOR : COL_BALL;
  1236. clip(dr, TODRAW(0)-1, TODRAW(0)-1, TILE_SIZE+1, TILE_SIZE+1);
  1237. draw_circle(dr, TODRAW(0) + ds->crad-1, TODRAW(0) + ds->crad-1, ds->crad-1,
  1238. outline, outline);
  1239. draw_circle(dr, TODRAW(0) + ds->crad-1, TODRAW(0) + ds->crad-1, ds->crad-3,
  1240. COL_BUTTON, COL_BUTTON);
  1241. unclip(dr);
  1242. } else {
  1243. draw_rect(dr, TODRAW(0)-1, TODRAW(0)-1,
  1244. TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
  1245. }
  1246. draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
  1247. ds->reveal = state->reveal;
  1248. ds->isflash = isflash;
  1249. {
  1250. char buf[256];
  1251. if (ds->reveal) {
  1252. if (state->nwrong == 0 &&
  1253. state->nmissed == 0 &&
  1254. state->nright >= state->minballs)
  1255. sprintf(buf, "CORRECT!");
  1256. else
  1257. sprintf(buf, "%d wrong and %d missed balls.",
  1258. state->nwrong, state->nmissed);
  1259. } else if (state->justwrong) {
  1260. sprintf(buf, "Wrong! Guess again.");
  1261. } else {
  1262. if (state->nguesses > state->maxballs)
  1263. sprintf(buf, "%d too many balls marked.",
  1264. state->nguesses - state->maxballs);
  1265. else if (state->nguesses <= state->maxballs &&
  1266. state->nguesses >= state->minballs)
  1267. sprintf(buf, "Click button to verify guesses.");
  1268. else if (state->maxballs == state->minballs)
  1269. sprintf(buf, "Balls marked: %d / %d",
  1270. state->nguesses, state->minballs);
  1271. else
  1272. sprintf(buf, "Balls marked: %d / %d-%d.",
  1273. state->nguesses, state->minballs, state->maxballs);
  1274. }
  1275. if (ui->errors) {
  1276. sprintf(buf + strlen(buf), " (%d error%s)",
  1277. ui->errors, ui->errors > 1 ? "s" : "");
  1278. }
  1279. status_bar(dr, buf);
  1280. }
  1281. }
  1282. static float game_anim_length(const game_state *oldstate,
  1283. const game_state *newstate, int dir, game_ui *ui)
  1284. {
  1285. return (ui->flash_laser == 2) ? CUR_ANIM : 0.0F;
  1286. }
  1287. static float game_flash_length(const game_state *oldstate,
  1288. const game_state *newstate, int dir, game_ui *ui)
  1289. {
  1290. if (!oldstate->reveal && newstate->reveal)
  1291. return 4.0F * FLASH_FRAME;
  1292. else
  1293. return 0.0F;
  1294. }
  1295. static int game_status(const game_state *state)
  1296. {
  1297. if (state->reveal) {
  1298. /*
  1299. * We return nonzero whenever the solution has been revealed,
  1300. * even (on spoiler grounds) if it wasn't guessed correctly.
  1301. */
  1302. if (state->nwrong == 0 &&
  1303. state->nmissed == 0 &&
  1304. state->nright >= state->minballs)
  1305. return +1;
  1306. else
  1307. return -1;
  1308. }
  1309. return 0;
  1310. }
  1311. #ifdef COMBINED
  1312. #define thegame blackbox
  1313. #endif
  1314. const struct game thegame = {
  1315. "Black Box", "games.blackbox", "blackbox",
  1316. default_params,
  1317. game_fetch_preset, NULL,
  1318. decode_params,
  1319. encode_params,
  1320. free_params,
  1321. dup_params,
  1322. true, game_configure, custom_params,
  1323. validate_params,
  1324. new_game_desc,
  1325. validate_desc,
  1326. new_game,
  1327. dup_game,
  1328. free_game,
  1329. true, solve_game,
  1330. false, NULL, NULL, /* can_format_as_text_now, text_format */
  1331. NULL, NULL, /* get_prefs, set_prefs */
  1332. new_ui,
  1333. free_ui,
  1334. encode_ui,
  1335. decode_ui,
  1336. NULL, /* game_request_keys */
  1337. game_changed_state,
  1338. current_key_label,
  1339. interpret_move,
  1340. execute_move,
  1341. PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
  1342. game_colours,
  1343. game_new_drawstate,
  1344. game_free_drawstate,
  1345. game_redraw,
  1346. game_anim_length,
  1347. game_flash_length,
  1348. game_get_cursor_location,
  1349. game_status,
  1350. false, false, NULL, NULL, /* print_size, print */
  1351. true, /* wants_statusbar */
  1352. false, NULL, /* timing_state */
  1353. REQUIRE_RBUTTON, /* flags */
  1354. };
  1355. /* vim: set shiftwidth=4 tabstop=8: */