undead.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. /*
  2. * undead: Implementation of Haunted Mirror Mazes
  3. *
  4. * http://www.janko.at/Raetsel/Spukschloss/index.htm
  5. *
  6. * Puzzle definition is the total number of each monster type, the
  7. * grid definition, and the list of sightings (clockwise, starting
  8. * from top left corner)
  9. *
  10. * Example: (Janko puzzle No. 1,
  11. * http://www.janko.at/Raetsel/Spukschloss/001.a.htm )
  12. *
  13. * Ghosts: 0 Vampires: 2 Zombies: 6
  14. *
  15. * 2 1 1 1
  16. * 1 \ \ . / 2
  17. * 0 \ . / . 2
  18. * 0 / . / . 2
  19. * 3 . . . \ 2
  20. * 3 3 2 2
  21. *
  22. * would be encoded into:
  23. * 4x4:0,2,6,LLaRLaRaRaRdL,2,1,1,1,2,2,2,2,2,2,3,3,3,0,0,1
  24. *
  25. * Additionally, the game description can contain monsters fixed at a
  26. * certain grid position. The internal generator does not (yet) use
  27. * this feature, but this is needed to enter puzzles like Janko No.
  28. * 14, which is encoded as:
  29. * 8x5:12,12,0,LaRbLaRaLaRLbRaVaVaGRaRaRaLbLaRbRLb,0,2,0,2,2,1,2,1,3,1,0,1,8,4,3,0,0,2,3,2,7,2,1,6,2,1
  30. *
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <ctype.h>
  37. #ifdef NO_TGMATH_H
  38. # include <math.h>
  39. #else
  40. # include <tgmath.h>
  41. #endif
  42. #include "puzzles.h"
  43. enum {
  44. COL_BACKGROUND,
  45. COL_GRID,
  46. COL_TEXT,
  47. COL_ERROR,
  48. COL_HIGHLIGHT,
  49. COL_FLASH,
  50. COL_GHOST,
  51. COL_ZOMBIE,
  52. COL_VAMPIRE,
  53. COL_DONE,
  54. NCOLOURS
  55. };
  56. #define DIFFLIST(A) \
  57. A(EASY,Easy,e) \
  58. A(NORMAL,Normal,n) \
  59. A(TRICKY,Tricky,t)
  60. #define ENUM(upper,title,lower) DIFF_ ## upper,
  61. #define TITLE(upper,title,lower) #title,
  62. #define ENCODE(upper,title,lower) #lower
  63. #define CONFIG(upper,title,lower) ":" #title
  64. enum { DIFFLIST(ENUM) DIFFCOUNT };
  65. static char const *const undead_diffnames[] = { DIFFLIST(TITLE) "(count)" };
  66. static char const undead_diffchars[] = DIFFLIST(ENCODE);
  67. #define DIFFCONFIG DIFFLIST(CONFIG)
  68. struct game_params {
  69. int w; /* Grid width */
  70. int h; /* Grid height */
  71. int diff; /* Puzzle difficulty */
  72. };
  73. static const struct game_params undead_presets[] = {
  74. { 4, 4, DIFF_EASY },
  75. { 4, 4, DIFF_NORMAL },
  76. { 4, 4, DIFF_TRICKY },
  77. { 5, 5, DIFF_EASY },
  78. { 5, 5, DIFF_NORMAL },
  79. { 5, 5, DIFF_TRICKY },
  80. { 7, 7, DIFF_EASY },
  81. { 7, 7, DIFF_NORMAL }
  82. };
  83. #define DEFAULT_PRESET 1
  84. static game_params *default_params(void) {
  85. game_params *ret = snew(game_params);
  86. *ret = undead_presets[DEFAULT_PRESET];
  87. return ret;
  88. }
  89. static bool game_fetch_preset(int i, char **name, game_params **params) {
  90. game_params *ret;
  91. char buf[64];
  92. if (i < 0 || i >= lenof(undead_presets)) return false;
  93. ret = default_params();
  94. *ret = undead_presets[i]; /* struct copy */
  95. *params = ret;
  96. sprintf(buf, "%dx%d %s",
  97. undead_presets[i].w, undead_presets[i].h,
  98. undead_diffnames[undead_presets[i].diff]);
  99. *name = dupstr(buf);
  100. return true;
  101. }
  102. static void free_params(game_params *params) {
  103. sfree(params);
  104. }
  105. static game_params *dup_params(const game_params *params)
  106. {
  107. game_params *ret = snew(game_params);
  108. *ret = *params; /* structure copy */
  109. return ret;
  110. }
  111. static void decode_params(game_params *params, char const *string) {
  112. params->w = params->h = atoi(string);
  113. while (*string && isdigit((unsigned char) *string)) ++string;
  114. if (*string == 'x') {
  115. string++;
  116. params->h = atoi(string);
  117. while (*string && isdigit((unsigned char)*string)) string++;
  118. }
  119. params->diff = DIFF_NORMAL;
  120. if (*string == 'd') {
  121. int i;
  122. string++;
  123. for (i = 0; i < DIFFCOUNT; i++)
  124. if (*string == undead_diffchars[i])
  125. params->diff = i;
  126. if (*string) string++;
  127. }
  128. return;
  129. }
  130. static char *encode_params(const game_params *params, bool full)
  131. {
  132. char buf[256];
  133. sprintf(buf, "%dx%d", params->w, params->h);
  134. if (full)
  135. sprintf(buf + strlen(buf), "d%c", undead_diffchars[params->diff]);
  136. return dupstr(buf);
  137. }
  138. static config_item *game_configure(const game_params *params)
  139. {
  140. config_item *ret;
  141. char buf[64];
  142. ret = snewn(4, config_item);
  143. ret[0].name = "Width";
  144. ret[0].type = C_STRING;
  145. sprintf(buf, "%d", params->w);
  146. ret[0].u.string.sval = dupstr(buf);
  147. ret[1].name = "Height";
  148. ret[1].type = C_STRING;
  149. sprintf(buf, "%d", params->h);
  150. ret[1].u.string.sval = dupstr(buf);
  151. ret[2].name = "Difficulty";
  152. ret[2].type = C_CHOICES;
  153. ret[2].u.choices.choicenames = DIFFCONFIG;
  154. ret[2].u.choices.selected = params->diff;
  155. ret[3].name = NULL;
  156. ret[3].type = C_END;
  157. return ret;
  158. }
  159. static game_params *custom_params(const config_item *cfg)
  160. {
  161. game_params *ret = snew(game_params);
  162. ret->w = atoi(cfg[0].u.string.sval);
  163. ret->h = atoi(cfg[1].u.string.sval);
  164. ret->diff = cfg[2].u.choices.selected;
  165. return ret;
  166. }
  167. static const char *validate_params(const game_params *params, bool full)
  168. {
  169. if (params->w < 3) return "Width must be at least 3";
  170. if (params->h < 3) return "Height must be at least 3";
  171. if (params->w > 54 / params->h) return "Grid is too big";
  172. if (params->diff >= DIFFCOUNT) return "Unknown difficulty rating";
  173. return NULL;
  174. }
  175. /* --------------------------------------------------------------- */
  176. /* Game state allocation, deallocation. */
  177. struct path {
  178. int length;
  179. int *p;
  180. int grid_start;
  181. int grid_end;
  182. int num_monsters;
  183. int *mapping;
  184. int sightings_start;
  185. int sightings_end;
  186. int *xy;
  187. };
  188. struct game_common {
  189. int refcount;
  190. struct game_params params;
  191. int wh;
  192. int num_ghosts,num_vampires,num_zombies,num_total;
  193. int num_paths;
  194. struct path *paths;
  195. int *grid;
  196. int *xinfo;
  197. bool *fixed;
  198. };
  199. struct game_state {
  200. struct game_common *common;
  201. int *guess;
  202. unsigned char *pencils;
  203. bool *cell_errors;
  204. bool *hint_errors;
  205. bool *hints_done;
  206. bool count_errors[3];
  207. bool solved;
  208. bool cheated;
  209. };
  210. static game_state *new_state(const game_params *params) {
  211. int i;
  212. game_state *state = snew(game_state);
  213. state->common = snew(struct game_common);
  214. state->common->refcount = 1;
  215. state->common->params.w = params->w;
  216. state->common->params.h = params->h;
  217. state->common->params.diff = params->diff;
  218. state->common->wh = (state->common->params.w +2) * (state->common->params.h +2);
  219. state->common->num_ghosts = 0;
  220. state->common->num_vampires = 0;
  221. state->common->num_zombies = 0;
  222. state->common->num_total = 0;
  223. state->common->grid = snewn(state->common->wh, int);
  224. state->common->xinfo = snewn(state->common->wh, int);
  225. state->common->fixed = NULL;
  226. state->common->num_paths =
  227. state->common->params.w + state->common->params.h;
  228. state->common->paths = snewn(state->common->num_paths, struct path);
  229. for (i=0;i<state->common->num_paths;i++) {
  230. state->common->paths[i].length = 0;
  231. state->common->paths[i].grid_start = -1;
  232. state->common->paths[i].grid_end = -1;
  233. state->common->paths[i].num_monsters = 0;
  234. state->common->paths[i].sightings_start = 0;
  235. state->common->paths[i].sightings_end = 0;
  236. state->common->paths[i].p = snewn(state->common->wh,int);
  237. state->common->paths[i].xy = snewn(state->common->wh,int);
  238. state->common->paths[i].mapping = snewn(state->common->wh,int);
  239. }
  240. state->guess = NULL;
  241. state->pencils = NULL;
  242. state->cell_errors = snewn(state->common->wh, bool);
  243. for (i=0;i<state->common->wh;i++)
  244. state->cell_errors[i] = false;
  245. state->hint_errors = snewn(2*state->common->num_paths, bool);
  246. for (i=0;i<2*state->common->num_paths;i++)
  247. state->hint_errors[i] = false;
  248. state->hints_done = snewn(2 * state->common->num_paths, bool);
  249. memset(state->hints_done, 0,
  250. 2 * state->common->num_paths * sizeof(bool));
  251. for (i=0;i<3;i++)
  252. state->count_errors[i] = false;
  253. state->solved = false;
  254. state->cheated = false;
  255. return state;
  256. }
  257. static game_state *dup_game(const game_state *state)
  258. {
  259. game_state *ret = snew(game_state);
  260. ret->common = state->common;
  261. ret->common->refcount++;
  262. if (state->guess != NULL) {
  263. ret->guess = snewn(ret->common->num_total,int);
  264. memcpy(ret->guess, state->guess, ret->common->num_total*sizeof(int));
  265. }
  266. else ret->guess = NULL;
  267. if (state->pencils != NULL) {
  268. ret->pencils = snewn(ret->common->num_total,unsigned char);
  269. memcpy(ret->pencils, state->pencils,
  270. ret->common->num_total*sizeof(unsigned char));
  271. }
  272. else ret->pencils = NULL;
  273. if (state->cell_errors != NULL) {
  274. ret->cell_errors = snewn(ret->common->wh,bool);
  275. memcpy(ret->cell_errors, state->cell_errors,
  276. ret->common->wh*sizeof(bool));
  277. }
  278. else ret->cell_errors = NULL;
  279. if (state->hint_errors != NULL) {
  280. ret->hint_errors = snewn(2*ret->common->num_paths,bool);
  281. memcpy(ret->hint_errors, state->hint_errors,
  282. 2*ret->common->num_paths*sizeof(bool));
  283. }
  284. else ret->hint_errors = NULL;
  285. if (state->hints_done != NULL) {
  286. ret->hints_done = snewn(2 * state->common->num_paths, bool);
  287. memcpy(ret->hints_done, state->hints_done,
  288. 2 * state->common->num_paths * sizeof(bool));
  289. }
  290. else ret->hints_done = NULL;
  291. ret->count_errors[0] = state->count_errors[0];
  292. ret->count_errors[1] = state->count_errors[1];
  293. ret->count_errors[2] = state->count_errors[2];
  294. ret->solved = state->solved;
  295. ret->cheated = state->cheated;
  296. return ret;
  297. }
  298. static void free_game(game_state *state) {
  299. int i;
  300. state->common->refcount--;
  301. if (state->common->refcount == 0) {
  302. for (i=0;i<state->common->num_paths;i++) {
  303. sfree(state->common->paths[i].mapping);
  304. sfree(state->common->paths[i].xy);
  305. sfree(state->common->paths[i].p);
  306. }
  307. sfree(state->common->paths);
  308. sfree(state->common->xinfo);
  309. sfree(state->common->grid);
  310. if (state->common->fixed != NULL) sfree(state->common->fixed);
  311. sfree(state->common);
  312. }
  313. if (state->hints_done != NULL) sfree(state->hints_done);
  314. if (state->hint_errors != NULL) sfree(state->hint_errors);
  315. if (state->cell_errors != NULL) sfree(state->cell_errors);
  316. if (state->pencils != NULL) sfree(state->pencils);
  317. if (state->guess != NULL) sfree(state->guess);
  318. sfree(state);
  319. return;
  320. }
  321. /* --------------------------------------------------------------- */
  322. /* Puzzle generator */
  323. /* cell states */
  324. enum {
  325. CELL_EMPTY,
  326. CELL_MIRROR_L,
  327. CELL_MIRROR_R,
  328. CELL_GHOST,
  329. CELL_VAMPIRE,
  330. CELL_ZOMBIE,
  331. CELL_UNDEF
  332. };
  333. /* grid walk directions */
  334. enum {
  335. DIRECTION_NONE,
  336. DIRECTION_UP,
  337. DIRECTION_RIGHT,
  338. DIRECTION_LEFT,
  339. DIRECTION_DOWN
  340. };
  341. static int range2grid(int rangeno, int width, int height, int *x, int *y) {
  342. if (rangeno < 0) {
  343. *x = 0; *y = 0; return DIRECTION_NONE;
  344. }
  345. if (rangeno < width) {
  346. *x = rangeno+1; *y = 0; return DIRECTION_DOWN;
  347. }
  348. rangeno = rangeno - width;
  349. if (rangeno < height) {
  350. *x = width+1; *y = rangeno+1; return DIRECTION_LEFT;
  351. }
  352. rangeno = rangeno - height;
  353. if (rangeno < width) {
  354. *x = width-rangeno; *y = height+1; return DIRECTION_UP;
  355. }
  356. rangeno = rangeno - width;
  357. if (rangeno < height) {
  358. *x = 0; *y = height-rangeno; return DIRECTION_RIGHT;
  359. }
  360. *x = 0; *y = 0;
  361. return DIRECTION_NONE;
  362. }
  363. static int grid2range(int x, int y, int w, int h) {
  364. if (x>0 && x<w+1 && y>0 && y<h+1) return -1;
  365. if (x<0 || x>w+1 || y<0 || y>h+1) return -1;
  366. if ((x == 0 || x==w+1) && (y==0 || y==h+1)) return -1;
  367. if (y==0) return x-1;
  368. if (x==(w+1)) return y-1+w;
  369. if (y==(h+1)) return 2*w + h - x;
  370. return 2*(w+h) - y;
  371. }
  372. static void make_paths(game_state *state) {
  373. int i;
  374. int count = 0;
  375. for (i=0;i<2*(state->common->params.w + state->common->params.h);i++) {
  376. int x,y,dir;
  377. int j,k,num_monsters;
  378. bool found;
  379. int c,p;
  380. found = false;
  381. /* Check whether inverse path is already in list */
  382. for (j=0;j<count;j++) {
  383. if (i == state->common->paths[j].grid_end) {
  384. found = true;
  385. break;
  386. }
  387. }
  388. if (found) continue;
  389. /* We found a new path through the mirror maze */
  390. state->common->paths[count].grid_start = i;
  391. dir = range2grid(i, state->common->params.w,
  392. state->common->params.h,&x,&y);
  393. state->common->paths[count].sightings_start =
  394. state->common->grid[x+y*(state->common->params.w +2)];
  395. while (true) {
  396. int c,r;
  397. if (dir == DIRECTION_DOWN) y++;
  398. else if (dir == DIRECTION_LEFT) x--;
  399. else if (dir == DIRECTION_UP) y--;
  400. else if (dir == DIRECTION_RIGHT) x++;
  401. r = grid2range(x, y, state->common->params.w,
  402. state->common->params.h);
  403. if (r != -1) {
  404. state->common->paths[count].grid_end = r;
  405. state->common->paths[count].sightings_end =
  406. state->common->grid[x+y*(state->common->params.w +2)];
  407. break;
  408. }
  409. c = state->common->grid[x+y*(state->common->params.w+2)];
  410. state->common->paths[count].xy[state->common->paths[count].length] =
  411. x+y*(state->common->params.w+2);
  412. if (c == CELL_MIRROR_L) {
  413. state->common->paths[count].p[state->common->paths[count].length] = -1;
  414. if (dir == DIRECTION_DOWN) dir = DIRECTION_RIGHT;
  415. else if (dir == DIRECTION_LEFT) dir = DIRECTION_UP;
  416. else if (dir == DIRECTION_UP) dir = DIRECTION_LEFT;
  417. else if (dir == DIRECTION_RIGHT) dir = DIRECTION_DOWN;
  418. }
  419. else if (c == CELL_MIRROR_R) {
  420. state->common->paths[count].p[state->common->paths[count].length] = -1;
  421. if (dir == DIRECTION_DOWN) dir = DIRECTION_LEFT;
  422. else if (dir == DIRECTION_LEFT) dir = DIRECTION_DOWN;
  423. else if (dir == DIRECTION_UP) dir = DIRECTION_RIGHT;
  424. else if (dir == DIRECTION_RIGHT) dir = DIRECTION_UP;
  425. }
  426. else {
  427. state->common->paths[count].p[state->common->paths[count].length] =
  428. state->common->xinfo[x+y*(state->common->params.w+2)];
  429. }
  430. state->common->paths[count].length++;
  431. }
  432. /* Count unique monster entries in each path */
  433. state->common->paths[count].num_monsters = 0;
  434. for (j=0;j<state->common->num_total;j++) {
  435. num_monsters = 0;
  436. for (k=0;k<state->common->paths[count].length;k++)
  437. if (state->common->paths[count].p[k] == j)
  438. num_monsters++;
  439. if (num_monsters > 0)
  440. state->common->paths[count].num_monsters++;
  441. }
  442. /* Generate mapping vector */
  443. c = 0;
  444. for (p=0;p<state->common->paths[count].length;p++) {
  445. int m;
  446. m = state->common->paths[count].p[p];
  447. if (m == -1) continue;
  448. found = false;
  449. for (j=0; j<c; j++)
  450. if (state->common->paths[count].mapping[j] == m) found = true;
  451. if (!found) state->common->paths[count].mapping[c++] = m;
  452. }
  453. count++;
  454. }
  455. return;
  456. }
  457. struct guess {
  458. int length;
  459. int *guess;
  460. int *possible;
  461. };
  462. static bool next_list(struct guess *g, int pos) {
  463. if (pos == 0) {
  464. if ((g->guess[pos] == 1 && g->possible[pos] == 1) ||
  465. (g->guess[pos] == 2 && (g->possible[pos] == 3 ||
  466. g->possible[pos] == 2)) ||
  467. g->guess[pos] == 4)
  468. return false;
  469. if (g->guess[pos] == 1 && (g->possible[pos] == 3 ||
  470. g->possible[pos] == 7)) {
  471. g->guess[pos] = 2; return true;
  472. }
  473. if (g->guess[pos] == 1 && g->possible[pos] == 5) {
  474. g->guess[pos] = 4; return true;
  475. }
  476. if (g->guess[pos] == 2 && (g->possible[pos] == 6 || g->possible[pos] == 7)) {
  477. g->guess[pos] = 4; return true;
  478. }
  479. }
  480. if (g->guess[pos] == 1) {
  481. if (g->possible[pos] == 1) {
  482. return next_list(g,pos-1);
  483. }
  484. if (g->possible[pos] == 3 || g->possible[pos] == 7) {
  485. g->guess[pos] = 2; return true;
  486. }
  487. if (g->possible[pos] == 5) {
  488. g->guess[pos] = 4; return true;
  489. }
  490. }
  491. if (g->guess[pos] == 2) {
  492. if (g->possible[pos] == 2) {
  493. return next_list(g,pos-1);
  494. }
  495. if (g->possible[pos] == 3) {
  496. g->guess[pos] = 1; return next_list(g,pos-1);
  497. }
  498. if (g->possible[pos] == 6 || g->possible[pos] == 7) {
  499. g->guess[pos] = 4; return true;
  500. }
  501. }
  502. if (g->guess[pos] == 4) {
  503. if (g->possible[pos] == 5 || g->possible[pos] == 7) {
  504. g->guess[pos] = 1; return next_list(g,pos-1);
  505. }
  506. if (g->possible[pos] == 6) {
  507. g->guess[pos] = 2; return next_list(g,pos-1);
  508. }
  509. if (g->possible[pos] == 4) {
  510. return next_list(g,pos-1);
  511. }
  512. }
  513. return false;
  514. }
  515. static void get_unique(game_state *state, int counter, random_state *rs) {
  516. int p,i,c,pathlimit,count_uniques;
  517. struct guess path_guess;
  518. int *view_count;
  519. struct entry {
  520. struct entry *link;
  521. int *guess;
  522. int start_view;
  523. int end_view;
  524. };
  525. struct {
  526. struct entry *head;
  527. struct entry *node;
  528. } views, single_views, test_views;
  529. struct entry test_entry;
  530. path_guess.length = state->common->paths[counter].num_monsters;
  531. path_guess.guess = snewn(path_guess.length,int);
  532. path_guess.possible = snewn(path_guess.length,int);
  533. for (i=0;i<path_guess.length;i++)
  534. path_guess.guess[i] = path_guess.possible[i] = 0;
  535. for (p=0;p<path_guess.length;p++) {
  536. path_guess.possible[p] =
  537. state->guess[state->common->paths[counter].mapping[p]];
  538. switch (path_guess.possible[p]) {
  539. case 1: path_guess.guess[p] = 1; break;
  540. case 2: path_guess.guess[p] = 2; break;
  541. case 3: path_guess.guess[p] = 1; break;
  542. case 4: path_guess.guess[p] = 4; break;
  543. case 5: path_guess.guess[p] = 1; break;
  544. case 6: path_guess.guess[p] = 2; break;
  545. case 7: path_guess.guess[p] = 1; break;
  546. }
  547. }
  548. views.head = NULL;
  549. views.node = NULL;
  550. pathlimit = state->common->paths[counter].length + 1;
  551. view_count = snewn(pathlimit*pathlimit, int);
  552. for (i = 0; i < pathlimit*pathlimit; i++)
  553. view_count[i] = 0;
  554. do {
  555. bool mirror;
  556. int start_view, end_view;
  557. mirror = false;
  558. start_view = 0;
  559. for (p=0;p<state->common->paths[counter].length;p++) {
  560. if (state->common->paths[counter].p[p] == -1) mirror = true;
  561. else {
  562. for (i=0;i<path_guess.length;i++) {
  563. if (state->common->paths[counter].p[p] ==
  564. state->common->paths[counter].mapping[i]) {
  565. if (path_guess.guess[i] == 1 && mirror)
  566. start_view++;
  567. if (path_guess.guess[i] == 2 && !mirror)
  568. start_view++;
  569. if (path_guess.guess[i] == 4)
  570. start_view++;
  571. break;
  572. }
  573. }
  574. }
  575. }
  576. mirror = false;
  577. end_view = 0;
  578. for (p=state->common->paths[counter].length-1;p>=0;p--) {
  579. if (state->common->paths[counter].p[p] == -1) mirror = true;
  580. else {
  581. for (i=0;i<path_guess.length;i++) {
  582. if (state->common->paths[counter].p[p] ==
  583. state->common->paths[counter].mapping[i]) {
  584. if (path_guess.guess[i] == 1 && mirror)
  585. end_view++;
  586. if (path_guess.guess[i] == 2 && !mirror)
  587. end_view++;
  588. if (path_guess.guess[i] == 4)
  589. end_view++;
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. assert(start_view >= 0 && start_view < pathlimit);
  596. assert(end_view >= 0 && end_view < pathlimit);
  597. i = start_view * pathlimit + end_view;
  598. view_count[i]++;
  599. if (view_count[i] == 1) {
  600. views.node = snewn(1,struct entry);
  601. views.node->link = views.head;
  602. views.node->guess = snewn(path_guess.length,int);
  603. views.head = views.node;
  604. views.node->start_view = start_view;
  605. views.node->end_view = end_view;
  606. memcpy(views.node->guess, path_guess.guess,
  607. path_guess.length*sizeof(int));
  608. }
  609. } while (next_list(&path_guess, path_guess.length-1));
  610. /* extract single entries from view list */
  611. test_views.head = views.head;
  612. test_views.node = views.node;
  613. test_entry.guess = snewn(path_guess.length,int);
  614. single_views.head = NULL;
  615. single_views.node = NULL;
  616. count_uniques = 0;
  617. while (test_views.head != NULL) {
  618. test_views.node = test_views.head;
  619. test_views.head = test_views.head->link;
  620. i = test_views.node->start_view * pathlimit + test_views.node->end_view;
  621. if (view_count[i] == 1) {
  622. single_views.node = snewn(1,struct entry);
  623. single_views.node->link = single_views.head;
  624. single_views.node->guess = snewn(path_guess.length,int);
  625. single_views.head = single_views.node;
  626. single_views.node->start_view = test_views.node->start_view;
  627. single_views.node->end_view = test_views.node->end_view;
  628. memcpy(single_views.node->guess, test_views.node->guess,
  629. path_guess.length*sizeof(int));
  630. count_uniques++;
  631. }
  632. }
  633. sfree(view_count);
  634. if (count_uniques > 0) {
  635. test_entry.start_view = 0;
  636. test_entry.end_view = 0;
  637. /* Choose one unique guess per random */
  638. /* While we are busy with looping through single_views, we
  639. * conveniently free the linked list single_view */
  640. c = random_upto(rs,count_uniques);
  641. while(single_views.head != NULL) {
  642. single_views.node = single_views.head;
  643. single_views.head = single_views.head->link;
  644. if (c-- == 0) {
  645. memcpy(test_entry.guess, single_views.node->guess,
  646. path_guess.length*sizeof(int));
  647. test_entry.start_view = single_views.node->start_view;
  648. test_entry.end_view = single_views.node->end_view;
  649. }
  650. sfree(single_views.node->guess);
  651. sfree(single_views.node);
  652. }
  653. /* Modify state_guess according to path_guess.mapping */
  654. for (i=0;i<path_guess.length;i++)
  655. state->guess[state->common->paths[counter].mapping[i]] =
  656. test_entry.guess[i];
  657. }
  658. sfree(test_entry.guess);
  659. while (views.head != NULL) {
  660. views.node = views.head;
  661. views.head = views.head->link;
  662. sfree(views.node->guess);
  663. sfree(views.node);
  664. }
  665. sfree(path_guess.possible);
  666. sfree(path_guess.guess);
  667. return;
  668. }
  669. static int count_monsters(game_state *state,
  670. int *cGhost, int *cVampire, int *cZombie) {
  671. int cNone;
  672. int i;
  673. *cGhost = *cVampire = *cZombie = cNone = 0;
  674. for (i=0;i<state->common->num_total;i++) {
  675. if (state->guess[i] == 1) (*cGhost)++;
  676. else if (state->guess[i] == 2) (*cVampire)++;
  677. else if (state->guess[i] == 4) (*cZombie)++;
  678. else cNone++;
  679. }
  680. return cNone;
  681. }
  682. static bool check_numbers(game_state *state, int *guess) {
  683. bool valid;
  684. int i;
  685. int count_ghosts, count_vampires, count_zombies;
  686. count_ghosts = count_vampires = count_zombies = 0;
  687. for (i=0;i<state->common->num_total;i++) {
  688. if (guess[i] == 1) count_ghosts++;
  689. if (guess[i] == 2) count_vampires++;
  690. if (guess[i] == 4) count_zombies++;
  691. }
  692. valid = true;
  693. if (count_ghosts > state->common->num_ghosts) valid = false;
  694. if (count_vampires > state->common->num_vampires) valid = false;
  695. if (count_zombies > state->common->num_zombies) valid = false;
  696. return valid;
  697. }
  698. static bool check_solution(int *g, struct path path) {
  699. int i;
  700. bool mirror;
  701. int count;
  702. count = 0;
  703. mirror = false;
  704. for (i=0;i<path.length;i++) {
  705. if (path.p[i] == -1) mirror = true;
  706. else {
  707. if (g[path.p[i]] == 1 && mirror) count++;
  708. else if (g[path.p[i]] == 2 && !mirror) count++;
  709. else if (g[path.p[i]] == 4) count++;
  710. }
  711. }
  712. if (count != path.sightings_start) return false;
  713. count = 0;
  714. mirror = false;
  715. for (i=path.length-1;i>=0;i--) {
  716. if (path.p[i] == -1) mirror = true;
  717. else {
  718. if (g[path.p[i]] == 1 && mirror) count++;
  719. else if (g[path.p[i]] == 2 && !mirror) count++;
  720. else if (g[path.p[i]] == 4) count++;
  721. }
  722. }
  723. if (count != path.sightings_end) return false;
  724. return true;
  725. }
  726. static bool solve_iterative(game_state *state, struct path *paths) {
  727. bool solved;
  728. int p,i,j,count;
  729. int *guess;
  730. int *possible;
  731. struct guess loop;
  732. solved = true;
  733. loop.length = state->common->num_total;
  734. guess = snewn(state->common->num_total,int);
  735. possible = snewn(state->common->num_total,int);
  736. for (i=0;i<state->common->num_total;i++) {
  737. guess[i] = state->guess[i];
  738. possible[i] = 0;
  739. }
  740. for (p=0;p<state->common->num_paths;p++) {
  741. if (paths[p].num_monsters > 0) {
  742. loop.length = paths[p].num_monsters;
  743. loop.guess = snewn(paths[p].num_monsters,int);
  744. loop.possible = snewn(paths[p].num_monsters,int);
  745. for (i=0;i<paths[p].num_monsters;i++) {
  746. switch (state->guess[paths[p].mapping[i]]) {
  747. case 1: loop.guess[i] = 1; break;
  748. case 2: loop.guess[i] = 2; break;
  749. case 3: loop.guess[i] = 1; break;
  750. case 4: loop.guess[i] = 4; break;
  751. case 5: loop.guess[i] = 1; break;
  752. case 6: loop.guess[i] = 2; break;
  753. case 7: loop.guess[i] = 1; break;
  754. }
  755. loop.possible[i] = state->guess[paths[p].mapping[i]];
  756. possible[paths[p].mapping[i]] = 0;
  757. }
  758. while(true) {
  759. for (i=0;i<state->common->num_total;i++) {
  760. guess[i] = state->guess[i];
  761. }
  762. count = 0;
  763. for (i=0;i<paths[p].num_monsters;i++)
  764. guess[paths[p].mapping[i]] = loop.guess[count++];
  765. if (check_numbers(state,guess) &&
  766. check_solution(guess,paths[p]))
  767. for (j=0;j<paths[p].num_monsters;j++)
  768. possible[paths[p].mapping[j]] |= loop.guess[j];
  769. if (!next_list(&loop,loop.length-1)) break;
  770. }
  771. for (i=0;i<paths[p].num_monsters;i++)
  772. state->guess[paths[p].mapping[i]] &=
  773. possible[paths[p].mapping[i]];
  774. sfree(loop.possible);
  775. sfree(loop.guess);
  776. }
  777. }
  778. for (i=0;i<state->common->num_total;i++) {
  779. if (state->guess[i] == 3 || state->guess[i] == 5 ||
  780. state->guess[i] == 6 || state->guess[i] == 7) {
  781. solved = false; break;
  782. }
  783. }
  784. sfree(possible);
  785. sfree(guess);
  786. return solved;
  787. }
  788. static bool solve_bruteforce(game_state *state, struct path *paths) {
  789. bool solved, correct;
  790. int number_solutions;
  791. int p,i;
  792. struct guess loop;
  793. loop.guess = snewn(state->common->num_total,int);
  794. loop.possible = snewn(state->common->num_total,int);
  795. for (i=0;i<state->common->num_total;i++) {
  796. loop.possible[i] = state->guess[i];
  797. switch (state->guess[i]) {
  798. case 1: loop.guess[i] = 1; break;
  799. case 2: loop.guess[i] = 2; break;
  800. case 3: loop.guess[i] = 1; break;
  801. case 4: loop.guess[i] = 4; break;
  802. case 5: loop.guess[i] = 1; break;
  803. case 6: loop.guess[i] = 2; break;
  804. case 7: loop.guess[i] = 1; break;
  805. }
  806. }
  807. solved = false;
  808. number_solutions = 0;
  809. while (true) {
  810. correct = true;
  811. if (!check_numbers(state,loop.guess)) correct = false;
  812. else
  813. for (p=0;p<state->common->num_paths;p++)
  814. if (!check_solution(loop.guess,paths[p])) {
  815. correct = false; break;
  816. }
  817. if (correct) {
  818. number_solutions++;
  819. solved = true;
  820. if(number_solutions > 1) {
  821. solved = false;
  822. break;
  823. }
  824. for (i=0;i<state->common->num_total;i++)
  825. state->guess[i] = loop.guess[i];
  826. }
  827. if (!next_list(&loop,state->common->num_total -1)) {
  828. break;
  829. }
  830. }
  831. sfree(loop.possible);
  832. sfree(loop.guess);
  833. return solved;
  834. }
  835. static int path_cmp(const void *a, const void *b) {
  836. const struct path *pa = (const struct path *)a;
  837. const struct path *pb = (const struct path *)b;
  838. return pa->num_monsters - pb->num_monsters;
  839. }
  840. static char *new_game_desc(const game_params *params, random_state *rs,
  841. char **aux, bool interactive) {
  842. int count,c,w,h,r,p,g;
  843. game_state *new;
  844. /* Variables for puzzle generation algorithm */
  845. int filling;
  846. int max_length;
  847. int count_ghosts, count_vampires, count_zombies;
  848. bool abort;
  849. float ratio;
  850. /* Variables for solver algorithm */
  851. bool solved_iterative, solved_bruteforce, contains_inconsistency;
  852. int count_ambiguous;
  853. int iterative_depth;
  854. int *old_guess;
  855. /* Variables for game description generation */
  856. int x,y;
  857. char *e;
  858. char *desc;
  859. while (true) {
  860. new = new_state(params);
  861. abort = false;
  862. /* Fill grid with random mirrors and (later to be populated)
  863. * empty monster cells */
  864. count = 0;
  865. for (h=1;h<new->common->params.h+1;h++)
  866. for (w=1;w<new->common->params.w+1;w++) {
  867. c = random_upto(rs,5);
  868. if (c >= 2) {
  869. new->common->grid[w+h*(new->common->params.w+2)] = CELL_EMPTY;
  870. new->common->xinfo[w+h*(new->common->params.w+2)] = count++;
  871. }
  872. else if (c == 0) {
  873. new->common->grid[w+h*(new->common->params.w+2)] =
  874. CELL_MIRROR_L;
  875. new->common->xinfo[w+h*(new->common->params.w+2)] = -1;
  876. }
  877. else {
  878. new->common->grid[w+h*(new->common->params.w+2)] =
  879. CELL_MIRROR_R;
  880. new->common->xinfo[w+h*(new->common->params.w+2)] = -1;
  881. }
  882. }
  883. new->common->num_total = count; /* Total number of monsters in maze */
  884. /* Puzzle is boring if it has too few monster cells. Discard
  885. * grid, make new grid */
  886. if (new->common->num_total <= 4) {
  887. free_game(new);
  888. continue;
  889. }
  890. /* Monsters / Mirrors ratio should be balanced */
  891. ratio = (float)new->common->num_total /
  892. (float)(new->common->params.w * new->common->params.h);
  893. if (ratio < 0.48F || ratio > 0.78F) {
  894. free_game(new);
  895. continue;
  896. }
  897. /* Assign clue identifiers */
  898. for (r=0;r<2*(new->common->params.w+new->common->params.h);r++) {
  899. int x,y,gridno;
  900. gridno = range2grid(r,new->common->params.w,new->common->params.h,
  901. &x,&y);
  902. new->common->grid[x+y*(new->common->params.w +2)] = gridno;
  903. new->common->xinfo[x+y*(new->common->params.w +2)] = 0;
  904. }
  905. /* The four corners don't matter at all for the game. Set them
  906. * all to zero, just to have a nice data structure */
  907. new->common->grid[0] = 0;
  908. new->common->xinfo[0] = 0;
  909. new->common->grid[new->common->params.w+1] = 0;
  910. new->common->xinfo[new->common->params.w+1] = 0;
  911. new->common->grid[new->common->params.w+1 + (new->common->params.h+1)*(new->common->params.w+2)] = 0;
  912. new->common->xinfo[new->common->params.w+1 + (new->common->params.h+1)*(new->common->params.w+2)] = 0;
  913. new->common->grid[(new->common->params.h+1)*(new->common->params.w+2)] = 0;
  914. new->common->xinfo[(new->common->params.h+1)*(new->common->params.w+2)] = 0;
  915. /* Initialize solution vector */
  916. new->guess = snewn(new->common->num_total,int);
  917. for (g=0;g<new->common->num_total;g++) new->guess[g] = 7;
  918. /* Initialize fixed flag from common. Not needed for the
  919. * puzzle generator; initialize it for having clean code */
  920. new->common->fixed = snewn(new->common->num_total, bool);
  921. for (g=0;g<new->common->num_total;g++)
  922. new->common->fixed[g] = false;
  923. /* paths generation */
  924. make_paths(new);
  925. /* Grid is invalid if max. path length > threshold. Discard
  926. * grid, make new one */
  927. switch (new->common->params.diff) {
  928. case DIFF_EASY: max_length = min(new->common->params.w,new->common->params.h) + 1; break;
  929. case DIFF_NORMAL: max_length = (max(new->common->params.w,new->common->params.h) * 3) / 2; break;
  930. case DIFF_TRICKY: max_length = 9; break;
  931. default: max_length = 9; break;
  932. }
  933. for (p=0;p<new->common->num_paths;p++) {
  934. if (new->common->paths[p].num_monsters > max_length) {
  935. abort = true;
  936. }
  937. }
  938. if (abort) {
  939. free_game(new);
  940. continue;
  941. }
  942. qsort(new->common->paths, new->common->num_paths,
  943. sizeof(struct path), path_cmp);
  944. /* Grid monster initialization */
  945. /* For easy puzzles, we try to fill nearly the whole grid
  946. with unique solution paths (up to 2) For more difficult
  947. puzzles, we fill only roughly half the grid, and choose
  948. random monsters for the rest For hard puzzles, we fill
  949. even less paths with unique solutions */
  950. switch (new->common->params.diff) {
  951. case DIFF_EASY: filling = 2; break;
  952. case DIFF_NORMAL: filling = min( (new->common->params.w+new->common->params.h) , (new->common->num_total)/2 ); break;
  953. case DIFF_TRICKY: filling = max( (new->common->params.w+new->common->params.h) , (new->common->num_total)/2 ); break;
  954. default: filling = 0; break;
  955. }
  956. count = 0;
  957. while ( (count_monsters(new, &count_ghosts, &count_vampires,
  958. &count_zombies)) > filling) {
  959. if ((count) >= new->common->num_paths) break;
  960. if (new->common->paths[count].num_monsters == 0) {
  961. count++;
  962. continue;
  963. }
  964. get_unique(new,count,rs);
  965. count++;
  966. }
  967. /* Fill any remaining ambiguous entries with random monsters */
  968. for(g=0;g<new->common->num_total;g++) {
  969. if (new->guess[g] == 7) {
  970. r = random_upto(rs,3);
  971. new->guess[g] = (r == 0) ? 1 : ( (r == 1) ? 2 : 4 );
  972. }
  973. }
  974. /* Determine all hints */
  975. count_monsters(new, &new->common->num_ghosts,
  976. &new->common->num_vampires, &new->common->num_zombies);
  977. /* Puzzle is trivial if it has only one type of monster. Discard. */
  978. if ((new->common->num_ghosts == 0 && new->common->num_vampires == 0) ||
  979. (new->common->num_ghosts == 0 && new->common->num_zombies == 0) ||
  980. (new->common->num_vampires == 0 && new->common->num_zombies == 0)) {
  981. free_game(new);
  982. continue;
  983. }
  984. /* Discard puzzle if difficulty Tricky, and it has only 1
  985. * member of any monster type */
  986. if (new->common->params.diff == DIFF_TRICKY &&
  987. (new->common->num_ghosts <= 1 ||
  988. new->common->num_vampires <= 1 || new->common->num_zombies <= 1)) {
  989. free_game(new);
  990. continue;
  991. }
  992. for (w=1;w<new->common->params.w+1;w++)
  993. for (h=1;h<new->common->params.h+1;h++) {
  994. c = new->common->xinfo[w+h*(new->common->params.w+2)];
  995. if (c >= 0) {
  996. if (new->guess[c] == 1) new->common->grid[w+h*(new->common->params.w+2)] = CELL_GHOST;
  997. if (new->guess[c] == 2) new->common->grid[w+h*(new->common->params.w+2)] = CELL_VAMPIRE;
  998. if (new->guess[c] == 4) new->common->grid[w+h*(new->common->params.w+2)] = CELL_ZOMBIE;
  999. }
  1000. }
  1001. /* Prepare path information needed by the solver (containing all hints) */
  1002. for (p=0;p<new->common->num_paths;p++) {
  1003. bool mirror;
  1004. int x,y;
  1005. new->common->paths[p].sightings_start = 0;
  1006. new->common->paths[p].sightings_end = 0;
  1007. mirror = false;
  1008. for (g=0;g<new->common->paths[p].length;g++) {
  1009. if (new->common->paths[p].p[g] == -1) mirror = true;
  1010. else {
  1011. if (new->guess[new->common->paths[p].p[g]] == 1 && mirror) (new->common->paths[p].sightings_start)++;
  1012. else if (new->guess[new->common->paths[p].p[g]] == 2 && !mirror) (new->common->paths[p].sightings_start)++;
  1013. else if (new->guess[new->common->paths[p].p[g]] == 4) (new->common->paths[p].sightings_start)++;
  1014. }
  1015. }
  1016. mirror = false;
  1017. for (g=new->common->paths[p].length-1;g>=0;g--) {
  1018. if (new->common->paths[p].p[g] == -1) mirror = true;
  1019. else {
  1020. if (new->guess[new->common->paths[p].p[g]] == 1 && mirror) (new->common->paths[p].sightings_end)++;
  1021. else if (new->guess[new->common->paths[p].p[g]] == 2 && !mirror) (new->common->paths[p].sightings_end)++;
  1022. else if (new->guess[new->common->paths[p].p[g]] == 4) (new->common->paths[p].sightings_end)++;
  1023. }
  1024. }
  1025. range2grid(new->common->paths[p].grid_start,
  1026. new->common->params.w,new->common->params.h,&x,&y);
  1027. new->common->grid[x+y*(new->common->params.w +2)] =
  1028. new->common->paths[p].sightings_start;
  1029. range2grid(new->common->paths[p].grid_end,
  1030. new->common->params.w,new->common->params.h,&x,&y);
  1031. new->common->grid[x+y*(new->common->params.w +2)] =
  1032. new->common->paths[p].sightings_end;
  1033. }
  1034. /* Try to solve the puzzle with the iterative solver */
  1035. old_guess = snewn(new->common->num_total,int);
  1036. for (p=0;p<new->common->num_total;p++) {
  1037. new->guess[p] = 7;
  1038. old_guess[p] = 7;
  1039. }
  1040. iterative_depth = 0;
  1041. solved_iterative = false;
  1042. contains_inconsistency = false;
  1043. count_ambiguous = 0;
  1044. while (true) {
  1045. bool no_change = true;
  1046. solved_iterative = solve_iterative(new,new->common->paths);
  1047. iterative_depth++;
  1048. for (p=0;p<new->common->num_total;p++) {
  1049. if (new->guess[p] != old_guess[p]) no_change = false;
  1050. old_guess[p] = new->guess[p];
  1051. if (new->guess[p] == 0) contains_inconsistency = true;
  1052. }
  1053. if (solved_iterative || no_change) break;
  1054. }
  1055. /* If necessary, try to solve the puzzle with the brute-force solver */
  1056. solved_bruteforce = false;
  1057. if (new->common->params.diff != DIFF_EASY &&
  1058. !solved_iterative && !contains_inconsistency) {
  1059. for (p=0;p<new->common->num_total;p++)
  1060. if (new->guess[p] != 1 && new->guess[p] != 2 &&
  1061. new->guess[p] != 4) count_ambiguous++;
  1062. solved_bruteforce = solve_bruteforce(new, new->common->paths);
  1063. }
  1064. /* Determine puzzle difficulty level */
  1065. if (new->common->params.diff == DIFF_EASY && solved_iterative &&
  1066. iterative_depth <= 3 && !contains_inconsistency) {
  1067. /* printf("Puzzle level: EASY Level %d Ratio %f Ambiguous %d (Found after %i tries)\n",iterative_depth, ratio, count_ambiguous, i); */
  1068. break;
  1069. }
  1070. if (new->common->params.diff == DIFF_NORMAL &&
  1071. ((solved_iterative && iterative_depth > 3) ||
  1072. (solved_bruteforce && count_ambiguous < 4)) &&
  1073. !contains_inconsistency) {
  1074. /* printf("Puzzle level: NORMAL Level %d Ratio %f Ambiguous %d (Found after %d tries)\n", iterative_depth, ratio, count_ambiguous, i); */
  1075. break;
  1076. }
  1077. if (new->common->params.diff == DIFF_TRICKY &&
  1078. solved_bruteforce && iterative_depth > 0 &&
  1079. count_ambiguous >= 4 && !contains_inconsistency) {
  1080. /* printf("Puzzle level: TRICKY Level %d Ratio %f Ambiguous %d (Found after %d tries)\n", iterative_depth, ratio, count_ambiguous, i); */
  1081. break;
  1082. }
  1083. /* If puzzle is not solvable or does not satisfy the desired
  1084. * difficulty level, free memory and start from scratch */
  1085. sfree(old_guess);
  1086. free_game(new);
  1087. }
  1088. /* We have a valid puzzle! */
  1089. desc = snewn(10 + new->common->wh +
  1090. 6*(new->common->params.w + new->common->params.h), char);
  1091. e = desc;
  1092. /* Encode monster counts */
  1093. e += sprintf(e, "%d,", new->common->num_ghosts);
  1094. e += sprintf(e, "%d,", new->common->num_vampires);
  1095. e += sprintf(e, "%d,", new->common->num_zombies);
  1096. /* Encode grid */
  1097. count = 0;
  1098. for (y=1;y<new->common->params.h+1;y++)
  1099. for (x=1;x<new->common->params.w+1;x++) {
  1100. c = new->common->grid[x+y*(new->common->params.w+2)];
  1101. if (count > 25) {
  1102. *e++ = 'z';
  1103. count -= 26;
  1104. }
  1105. if (c != CELL_MIRROR_L && c != CELL_MIRROR_R) {
  1106. count++;
  1107. }
  1108. else if (c == CELL_MIRROR_L) {
  1109. if (count > 0) *e++ = (count-1 + 'a');
  1110. *e++ = 'L';
  1111. count = 0;
  1112. }
  1113. else {
  1114. if (count > 0) *e++ = (count-1 + 'a');
  1115. *e++ = 'R';
  1116. count = 0;
  1117. }
  1118. }
  1119. if (count > 0) *e++ = (count-1 + 'a');
  1120. /* Encode hints */
  1121. for (p=0;p<2*(new->common->params.w + new->common->params.h);p++) {
  1122. range2grid(p,new->common->params.w,new->common->params.h,&x,&y);
  1123. e += sprintf(e, ",%d", new->common->grid[x+y*(new->common->params.w+2)]);
  1124. }
  1125. *e++ = '\0';
  1126. desc = sresize(desc, e - desc, char);
  1127. sfree(old_guess);
  1128. free_game(new);
  1129. return desc;
  1130. }
  1131. static void num2grid(int num, int width, int height, int *x, int *y) {
  1132. *x = 1+(num%width);
  1133. *y = 1+(num/width);
  1134. return;
  1135. }
  1136. static key_label *game_request_keys(const game_params *params, int *nkeys)
  1137. {
  1138. key_label *keys = snewn(4, key_label);
  1139. *nkeys = 4;
  1140. keys[0].button = 'G';
  1141. keys[0].label = dupstr("Ghost");
  1142. keys[1].button = 'V';
  1143. keys[1].label = dupstr("Vampire");
  1144. keys[2].button = 'Z';
  1145. keys[2].label = dupstr("Zombie");
  1146. keys[3].button = '\b';
  1147. keys[3].label = NULL;
  1148. return keys;
  1149. }
  1150. static game_state *new_game(midend *me, const game_params *params,
  1151. const char *desc)
  1152. {
  1153. int i;
  1154. int n;
  1155. int count;
  1156. game_state *state = new_state(params);
  1157. state->common->num_ghosts = atoi(desc);
  1158. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1159. desc++;
  1160. state->common->num_vampires = atoi(desc);
  1161. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1162. desc++;
  1163. state->common->num_zombies = atoi(desc);
  1164. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1165. desc++;
  1166. state->common->num_total = state->common->num_ghosts + state->common->num_vampires + state->common->num_zombies;
  1167. state->guess = snewn(state->common->num_total,int);
  1168. state->pencils = snewn(state->common->num_total,unsigned char);
  1169. state->common->fixed = snewn(state->common->num_total, bool);
  1170. for (i=0;i<state->common->num_total;i++) {
  1171. state->guess[i] = 7;
  1172. state->pencils[i] = 0;
  1173. state->common->fixed[i] = false;
  1174. }
  1175. for (i=0;i<state->common->wh;i++)
  1176. state->cell_errors[i] = false;
  1177. for (i=0;i<2*state->common->num_paths;i++)
  1178. state->hint_errors[i] = false;
  1179. for (i=0;i<3;i++)
  1180. state->count_errors[i] = false;
  1181. count = 0;
  1182. n = 0;
  1183. while (*desc != ',') {
  1184. int c;
  1185. int x,y;
  1186. if (*desc == 'L') {
  1187. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1188. state->common->grid[x+y*(state->common->params.w +2)] = CELL_MIRROR_L;
  1189. state->common->xinfo[x+y*(state->common->params.w+2)] = -1;
  1190. n++;
  1191. }
  1192. else if (*desc == 'R') {
  1193. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1194. state->common->grid[x+y*(state->common->params.w +2)] = CELL_MIRROR_R;
  1195. state->common->xinfo[x+y*(state->common->params.w+2)] = -1;
  1196. n++;
  1197. }
  1198. else if (*desc == 'G') {
  1199. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1200. state->common->grid[x+y*(state->common->params.w +2)] = CELL_GHOST;
  1201. state->common->xinfo[x+y*(state->common->params.w+2)] = count;
  1202. state->guess[count] = 1;
  1203. state->common->fixed[count++] = true;
  1204. n++;
  1205. }
  1206. else if (*desc == 'V') {
  1207. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1208. state->common->grid[x+y*(state->common->params.w +2)] = CELL_VAMPIRE;
  1209. state->common->xinfo[x+y*(state->common->params.w+2)] = count;
  1210. state->guess[count] = 2;
  1211. state->common->fixed[count++] = true;
  1212. n++;
  1213. }
  1214. else if (*desc == 'Z') {
  1215. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1216. state->common->grid[x+y*(state->common->params.w +2)] = CELL_ZOMBIE;
  1217. state->common->xinfo[x+y*(state->common->params.w+2)] = count;
  1218. state->guess[count] = 4;
  1219. state->common->fixed[count++] = true;
  1220. n++;
  1221. }
  1222. else {
  1223. c = *desc - ('a' -1);
  1224. while (c-- > 0) {
  1225. num2grid(n,state->common->params.w,state->common->params.h,&x,&y);
  1226. state->common->grid[x+y*(state->common->params.w +2)] = CELL_EMPTY;
  1227. state->common->xinfo[x+y*(state->common->params.w+2)] = count;
  1228. state->guess[count] = 7;
  1229. state->common->fixed[count++] = false;
  1230. n++;
  1231. }
  1232. }
  1233. desc++;
  1234. }
  1235. desc++;
  1236. for (i=0;i<2*(state->common->params.w + state->common->params.h);i++) {
  1237. int x,y;
  1238. int sights;
  1239. sights = atoi(desc);
  1240. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1241. desc++;
  1242. range2grid(i,state->common->params.w,state->common->params.h,&x,&y);
  1243. state->common->grid[x+y*(state->common->params.w +2)] = sights;
  1244. state->common->xinfo[x+y*(state->common->params.w +2)] = -2;
  1245. }
  1246. state->common->grid[0] = 0;
  1247. state->common->xinfo[0] = -2;
  1248. state->common->grid[state->common->params.w+1] = 0;
  1249. state->common->xinfo[state->common->params.w+1] = -2;
  1250. state->common->grid[state->common->params.w+1 + (state->common->params.h+1)*(state->common->params.w+2)] = 0;
  1251. state->common->xinfo[state->common->params.w+1 + (state->common->params.h+1)*(state->common->params.w+2)] = -2;
  1252. state->common->grid[(state->common->params.h+1)*(state->common->params.w+2)] = 0;
  1253. state->common->xinfo[(state->common->params.h+1)*(state->common->params.w+2)] = -2;
  1254. make_paths(state);
  1255. qsort(state->common->paths, state->common->num_paths, sizeof(struct path), path_cmp);
  1256. return state;
  1257. }
  1258. static const char *validate_desc(const game_params *params, const char *desc)
  1259. {
  1260. int i;
  1261. int w = params->w, h = params->h;
  1262. int wh = w*h;
  1263. int area;
  1264. int monsters;
  1265. int monster_count;
  1266. const char *desc_s = desc;
  1267. for (i=0;i<3;i++) {
  1268. if (!*desc) return "Faulty game description";
  1269. while (*desc && isdigit((unsigned char)*desc)) { desc++; }
  1270. if (*desc != ',') return "Invalid character in number list";
  1271. desc++;
  1272. }
  1273. desc = desc_s;
  1274. area = monsters = monster_count = 0;
  1275. for (i=0;i<3;i++) {
  1276. monster_count += atoi(desc);
  1277. while (*desc && isdigit((unsigned char)*desc)) desc++;
  1278. desc++;
  1279. }
  1280. while (*desc && *desc != ',') {
  1281. if (*desc >= 'a' && *desc <= 'z') {
  1282. area += *desc - 'a' +1; monsters += *desc - 'a' +1;
  1283. } else if (*desc == 'G' || *desc == 'V' || *desc == 'Z') {
  1284. area++; monsters++;
  1285. } else if (*desc == 'L' || *desc == 'R') {
  1286. area++;
  1287. } else
  1288. return "Invalid character in grid specification";
  1289. desc++;
  1290. }
  1291. if (area < wh) return "Not enough data to fill grid";
  1292. else if (area > wh) return "Too much data to fill grid";
  1293. if (monsters != monster_count)
  1294. return "Monster numbers do not match grid spaces";
  1295. for (i = 0; i < 2*(w+h); i++) {
  1296. if (!*desc) return "Not enough numbers given after grid specification";
  1297. else if (*desc != ',') return "Invalid character in number list";
  1298. desc++;
  1299. while (*desc && isdigit((unsigned char)*desc)) { desc++; }
  1300. }
  1301. if (*desc) return "Unexpected additional data at end of game description";
  1302. return NULL;
  1303. }
  1304. static char *solve_game(const game_state *state_start, const game_state *currstate,
  1305. const char *aux, const char **error)
  1306. {
  1307. int p;
  1308. int *old_guess;
  1309. int iterative_depth;
  1310. bool solved_iterative, solved_bruteforce, contains_inconsistency;
  1311. int count_ambiguous;
  1312. int i;
  1313. char *move, *c;
  1314. game_state *solve_state = dup_game(currstate);
  1315. old_guess = snewn(solve_state->common->num_total,int);
  1316. for (p=0;p<solve_state->common->num_total;p++) {
  1317. if (solve_state->common->fixed[p]) {
  1318. old_guess[p] = solve_state->guess[p] = state_start->guess[p];
  1319. }
  1320. else {
  1321. old_guess[p] = solve_state->guess[p] = 7;
  1322. }
  1323. }
  1324. iterative_depth = 0;
  1325. solved_iterative = false;
  1326. contains_inconsistency = false;
  1327. count_ambiguous = 0;
  1328. /* Try to solve the puzzle with the iterative solver */
  1329. while (true) {
  1330. bool no_change = true;
  1331. solved_iterative =
  1332. solve_iterative(solve_state,solve_state->common->paths);
  1333. iterative_depth++;
  1334. for (p=0;p<solve_state->common->num_total;p++) {
  1335. if (solve_state->guess[p] != old_guess[p]) no_change = false;
  1336. old_guess[p] = solve_state->guess[p];
  1337. if (solve_state->guess[p] == 0) contains_inconsistency = true;
  1338. }
  1339. if (solved_iterative || no_change || contains_inconsistency) break;
  1340. }
  1341. if (contains_inconsistency) {
  1342. *error = "Puzzle is inconsistent";
  1343. sfree(old_guess);
  1344. free_game(solve_state);
  1345. return NULL;
  1346. }
  1347. /* If necessary, try to solve the puzzle with the brute-force solver */
  1348. solved_bruteforce = false;
  1349. if (!solved_iterative) {
  1350. for (p=0;p<solve_state->common->num_total;p++)
  1351. if (solve_state->guess[p] != 1 && solve_state->guess[p] != 2 &&
  1352. solve_state->guess[p] != 4) count_ambiguous++;
  1353. solved_bruteforce =
  1354. solve_bruteforce(solve_state, solve_state->common->paths);
  1355. }
  1356. if (!solved_iterative && !solved_bruteforce) {
  1357. *error = "Puzzle is unsolvable";
  1358. sfree(old_guess);
  1359. free_game(solve_state);
  1360. return NULL;
  1361. }
  1362. /* printf("Puzzle solved at level %s, iterations %d, ambiguous %d\n", (solved_bruteforce ? "TRICKY" : "NORMAL"), iterative_depth, count_ambiguous); */
  1363. (void)iterative_depth;
  1364. (void)count_ambiguous;
  1365. move = snewn(solve_state->common->num_total * 4 +2, char);
  1366. c = move;
  1367. *c++='S';
  1368. for (i = 0; i < solve_state->common->num_total; i++) {
  1369. if (solve_state->guess[i] == 1) c += sprintf(c, ";G%d", i);
  1370. if (solve_state->guess[i] == 2) c += sprintf(c, ";V%d", i);
  1371. if (solve_state->guess[i] == 4) c += sprintf(c, ";Z%d", i);
  1372. }
  1373. *c++ = '\0';
  1374. move = sresize(move, c - move, char);
  1375. sfree(old_guess);
  1376. free_game(solve_state);
  1377. return move;
  1378. }
  1379. static bool game_can_format_as_text_now(const game_params *params)
  1380. {
  1381. return true;
  1382. }
  1383. static char *game_text_format(const game_state *state)
  1384. {
  1385. int w,h,c,r,xi,g;
  1386. char *ret;
  1387. char buf[120];
  1388. ret = snewn(50 + 6*(state->common->params.w +2) +
  1389. 6*(state->common->params.h+2) +
  1390. 3*(state->common->params.w * state->common->params.h), char);
  1391. sprintf(ret,"G: %d V: %d Z: %d\n\n",state->common->num_ghosts,
  1392. state->common->num_vampires, state->common->num_zombies);
  1393. for (h=0;h<state->common->params.h+2;h++) {
  1394. for (w=0;w<state->common->params.w+2;w++) {
  1395. c = state->common->grid[w+h*(state->common->params.w+2)];
  1396. xi = state->common->xinfo[w+h*(state->common->params.w+2)];
  1397. r = grid2range(w,h,state->common->params.w,state->common->params.h);
  1398. if (r != -1) {
  1399. sprintf(buf,"%2d", c); strcat(ret,buf);
  1400. } else if (c == CELL_MIRROR_L) {
  1401. sprintf(buf," \\"); strcat(ret,buf);
  1402. } else if (c == CELL_MIRROR_R) {
  1403. sprintf(buf," /"); strcat(ret,buf);
  1404. } else if (xi >= 0) {
  1405. g = state->guess[xi];
  1406. if (g == 1) { sprintf(buf," G"); strcat(ret,buf); }
  1407. else if (g == 2) { sprintf(buf," V"); strcat(ret,buf); }
  1408. else if (g == 4) { sprintf(buf," Z"); strcat(ret,buf); }
  1409. else { sprintf(buf," ."); strcat(ret,buf); }
  1410. } else {
  1411. sprintf(buf," "); strcat(ret,buf);
  1412. }
  1413. }
  1414. sprintf(buf,"\n"); strcat(ret,buf);
  1415. }
  1416. return ret;
  1417. }
  1418. struct game_ui {
  1419. int hx, hy; /* as for solo.c, highlight pos */
  1420. bool hshow, hpencil, hcursor; /* show state, type, and ?cursor. */
  1421. bool ascii;
  1422. };
  1423. static game_ui *new_ui(const game_state *state)
  1424. {
  1425. game_ui *ui = snew(game_ui);
  1426. ui->hpencil = false;
  1427. ui->hx = ui->hy = ui->hshow = ui->hcursor =
  1428. getenv_bool("PUZZLES_SHOW_CURSOR", false);
  1429. ui->ascii = false;
  1430. return ui;
  1431. }
  1432. static config_item *get_prefs(game_ui *ui)
  1433. {
  1434. config_item *ret;
  1435. ret = snewn(2, config_item);
  1436. ret[0].name = "Monster representation";
  1437. ret[0].kw = "monsters";
  1438. ret[0].type = C_CHOICES;
  1439. ret[0].u.choices.choicenames = ":Pictures:Letters";
  1440. ret[0].u.choices.choicekws = ":pictures:letters";
  1441. ret[0].u.choices.selected = ui->ascii;
  1442. ret[1].name = NULL;
  1443. ret[1].type = C_END;
  1444. return ret;
  1445. }
  1446. static void set_prefs(game_ui *ui, const config_item *cfg)
  1447. {
  1448. ui->ascii = cfg[0].u.choices.selected;
  1449. }
  1450. static void free_ui(game_ui *ui) {
  1451. sfree(ui);
  1452. return;
  1453. }
  1454. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  1455. const game_state *newstate)
  1456. {
  1457. /* See solo.c; if we were pencil-mode highlighting and
  1458. * somehow a square has just been properly filled, cancel
  1459. * pencil mode. */
  1460. if (ui->hshow && ui->hpencil && !ui->hcursor) {
  1461. int g = newstate->guess[newstate->common->xinfo[ui->hx + ui->hy*(newstate->common->params.w+2)]];
  1462. if (g == 1 || g == 2 || g == 4)
  1463. ui->hshow = false;
  1464. }
  1465. }
  1466. static const char *current_key_label(const game_ui *ui,
  1467. const game_state *state, int button)
  1468. {
  1469. int xi;
  1470. if (ui->hshow && button == CURSOR_SELECT)
  1471. return ui->hpencil ? "Ink" : "Pencil";
  1472. if (button == CURSOR_SELECT2) {
  1473. xi = state->common->xinfo[ui->hx + ui->hy*(state->common->params.w+2)];
  1474. if (xi >= 0 && !state->common->fixed[xi]) return "Clear";
  1475. }
  1476. return "";
  1477. }
  1478. struct game_drawstate {
  1479. int tilesize;
  1480. bool started, solved;
  1481. int w, h;
  1482. int *monsters;
  1483. unsigned char *pencils;
  1484. bool count_errors[3];
  1485. bool *cell_errors;
  1486. bool *hint_errors;
  1487. bool *hints_done;
  1488. int hx, hy;
  1489. bool hshow, hpencil; /* as for game_ui. */
  1490. bool hflash;
  1491. bool ascii;
  1492. };
  1493. static bool is_clue(const game_state *state, int x, int y)
  1494. {
  1495. int h = state->common->params.h, w = state->common->params.w;
  1496. if (((x == 0 || x == w + 1) && y > 0 && y <= h) ||
  1497. ((y == 0 || y == h + 1) && x > 0 && x <= w))
  1498. return true;
  1499. return false;
  1500. }
  1501. static int clue_index(const game_state *state, int x, int y)
  1502. {
  1503. int h = state->common->params.h, w = state->common->params.w;
  1504. if (y == 0)
  1505. return x - 1;
  1506. else if (x == w + 1)
  1507. return w + y - 1;
  1508. else if (y == h + 1)
  1509. return 2 * w + h - x;
  1510. else if (x == 0)
  1511. return 2 * (w + h) - y;
  1512. return -1;
  1513. }
  1514. #define TILESIZE (ds->tilesize)
  1515. #define BORDER (TILESIZE/4)
  1516. static char *interpret_move(const game_state *state, game_ui *ui,
  1517. const game_drawstate *ds,
  1518. int x, int y, int button)
  1519. {
  1520. int gx,gy;
  1521. int g,xi;
  1522. char buf[80];
  1523. gx = ((x-BORDER-1) / TILESIZE );
  1524. gy = ((y-BORDER-2) / TILESIZE ) - 1;
  1525. if (button == 'a' || button == 'A') {
  1526. ui->ascii = !ui->ascii;
  1527. return MOVE_UI_UPDATE;
  1528. }
  1529. if (button == 'm' || button == 'M') {
  1530. return dupstr("M");
  1531. }
  1532. if (ui->hshow && !ui->hpencil) {
  1533. xi = state->common->xinfo[ui->hx + ui->hy*(state->common->params.w+2)];
  1534. if (xi >= 0 && !state->common->fixed[xi]) {
  1535. if (button == 'g' || button == 'G' || button == '1') {
  1536. if (!ui->hcursor) ui->hshow = false;
  1537. if (state->guess[xi] == 1)
  1538. return ui->hcursor ? NULL : MOVE_UI_UPDATE;
  1539. sprintf(buf,"G%d",xi);
  1540. return dupstr(buf);
  1541. }
  1542. if (button == 'v' || button == 'V' || button == '2') {
  1543. if (!ui->hcursor) ui->hshow = false;
  1544. if (state->guess[xi] == 2)
  1545. return ui->hcursor ? NULL : MOVE_UI_UPDATE;
  1546. sprintf(buf,"V%d",xi);
  1547. return dupstr(buf);
  1548. }
  1549. if (button == 'z' || button == 'Z' || button == '3') {
  1550. if (!ui->hcursor) ui->hshow = false;
  1551. if (state->guess[xi] == 4)
  1552. return ui->hcursor ? NULL : MOVE_UI_UPDATE;
  1553. sprintf(buf,"Z%d",xi);
  1554. return dupstr(buf);
  1555. }
  1556. if (button == 'e' || button == 'E' || button == CURSOR_SELECT2 ||
  1557. button == '0' || button == '\b' ) {
  1558. if (!ui->hcursor) ui->hshow = false;
  1559. if (state->guess[xi] == 7 && state->pencils[xi] == 0)
  1560. return ui->hcursor ? NULL : MOVE_UI_UPDATE;
  1561. sprintf(buf,"E%d",xi);
  1562. return dupstr(buf);
  1563. }
  1564. }
  1565. }
  1566. if (IS_CURSOR_MOVE(button)) {
  1567. if (ui->hx == 0 && ui->hy == 0) {
  1568. ui->hx = 1;
  1569. ui->hy = 1;
  1570. }
  1571. else switch (button) {
  1572. case CURSOR_UP: ui->hy -= (ui->hy > 1) ? 1 : 0; break;
  1573. case CURSOR_DOWN: ui->hy += (ui->hy < ds->h) ? 1 : 0; break;
  1574. case CURSOR_RIGHT: ui->hx += (ui->hx < ds->w) ? 1 : 0; break;
  1575. case CURSOR_LEFT: ui->hx -= (ui->hx > 1) ? 1 : 0; break;
  1576. }
  1577. ui->hshow = true;
  1578. ui->hcursor = true;
  1579. return MOVE_UI_UPDATE;
  1580. }
  1581. if (ui->hshow && button == CURSOR_SELECT) {
  1582. ui->hpencil = !ui->hpencil;
  1583. ui->hcursor = true;
  1584. return MOVE_UI_UPDATE;
  1585. }
  1586. if (ui->hshow && ui->hpencil) {
  1587. xi = state->common->xinfo[ui->hx + ui->hy*(state->common->params.w+2)];
  1588. if (xi >= 0 && !state->common->fixed[xi]) {
  1589. if (button == 'g' || button == 'G' || button == '1') {
  1590. sprintf(buf,"g%d",xi);
  1591. if (!ui->hcursor) {
  1592. ui->hpencil = false;
  1593. ui->hshow = false;
  1594. }
  1595. return dupstr(buf);
  1596. }
  1597. if (button == 'v' || button == 'V' || button == '2') {
  1598. sprintf(buf,"v%d",xi);
  1599. if (!ui->hcursor) {
  1600. ui->hpencil = false;
  1601. ui->hshow = false;
  1602. }
  1603. return dupstr(buf);
  1604. }
  1605. if (button == 'z' || button == 'Z' || button == '3') {
  1606. sprintf(buf,"z%d",xi);
  1607. if (!ui->hcursor) {
  1608. ui->hpencil = false;
  1609. ui->hshow = false;
  1610. }
  1611. return dupstr(buf);
  1612. }
  1613. if (button == 'e' || button == 'E' || button == CURSOR_SELECT2 ||
  1614. button == '0' || button == '\b') {
  1615. if (!ui->hcursor) {
  1616. ui->hpencil = false;
  1617. ui->hshow = false;
  1618. }
  1619. if (state->pencils[xi] == 0)
  1620. return ui->hcursor ? NULL : MOVE_UI_UPDATE;
  1621. sprintf(buf,"E%d",xi);
  1622. return dupstr(buf);
  1623. }
  1624. }
  1625. }
  1626. if (gx > 0 && gx < ds->w+1 && gy > 0 && gy < ds->h+1) {
  1627. xi = state->common->xinfo[gx+gy*(state->common->params.w+2)];
  1628. if (xi >= 0 && !state->common->fixed[xi]) {
  1629. g = state->guess[xi];
  1630. if (!ui->hshow) {
  1631. if (button == LEFT_BUTTON) {
  1632. ui->hshow = true;
  1633. ui->hpencil = false;
  1634. ui->hcursor = false;
  1635. ui->hx = gx; ui->hy = gy;
  1636. return MOVE_UI_UPDATE;
  1637. }
  1638. else if (button == RIGHT_BUTTON && g == 7) {
  1639. ui->hshow = true;
  1640. ui->hpencil = true;
  1641. ui->hcursor = false;
  1642. ui->hx = gx; ui->hy = gy;
  1643. return MOVE_UI_UPDATE;
  1644. }
  1645. }
  1646. else if (ui->hshow) {
  1647. if (button == LEFT_BUTTON) {
  1648. if (!ui->hpencil) {
  1649. if (gx == ui->hx && gy == ui->hy) {
  1650. ui->hshow = false;
  1651. ui->hpencil = false;
  1652. ui->hcursor = false;
  1653. ui->hx = 0; ui->hy = 0;
  1654. return MOVE_UI_UPDATE;
  1655. }
  1656. else {
  1657. ui->hshow = true;
  1658. ui->hpencil = false;
  1659. ui->hcursor = false;
  1660. ui->hx = gx; ui->hy = gy;
  1661. return MOVE_UI_UPDATE;
  1662. }
  1663. }
  1664. else {
  1665. ui->hshow = true;
  1666. ui->hpencil = false;
  1667. ui->hcursor = false;
  1668. ui->hx = gx; ui->hy = gy;
  1669. return MOVE_UI_UPDATE;
  1670. }
  1671. }
  1672. else if (button == RIGHT_BUTTON) {
  1673. if (!ui->hpencil && g == 7) {
  1674. ui->hshow = true;
  1675. ui->hpencil = true;
  1676. ui->hcursor = false;
  1677. ui->hx = gx; ui->hy = gy;
  1678. return MOVE_UI_UPDATE;
  1679. }
  1680. else {
  1681. if (gx == ui->hx && gy == ui->hy) {
  1682. ui->hshow = false;
  1683. ui->hpencil = false;
  1684. ui->hcursor = false;
  1685. ui->hx = 0; ui->hy = 0;
  1686. return MOVE_UI_UPDATE;
  1687. }
  1688. else if (g == 7) {
  1689. ui->hshow = true;
  1690. ui->hpencil = true;
  1691. ui->hcursor = false;
  1692. ui->hx = gx; ui->hy = gy;
  1693. return MOVE_UI_UPDATE;
  1694. }
  1695. }
  1696. }
  1697. }
  1698. }
  1699. } else if (button == LEFT_BUTTON) {
  1700. if (is_clue(state, gx, gy)) {
  1701. sprintf(buf, "D%d,%d", gx, gy);
  1702. return dupstr(buf);
  1703. }
  1704. }
  1705. return NULL;
  1706. }
  1707. static bool check_numbers_draw(game_state *state, int *guess) {
  1708. bool valid, filled;
  1709. int i,x,y,xy;
  1710. int count_ghosts, count_vampires, count_zombies;
  1711. count_ghosts = count_vampires = count_zombies = 0;
  1712. for (i=0;i<state->common->num_total;i++) {
  1713. if (guess[i] == 1) count_ghosts++;
  1714. if (guess[i] == 2) count_vampires++;
  1715. if (guess[i] == 4) count_zombies++;
  1716. }
  1717. valid = true;
  1718. filled = (count_ghosts + count_vampires + count_zombies >=
  1719. state->common->num_total);
  1720. if (count_ghosts > state->common->num_ghosts ||
  1721. (filled && count_ghosts != state->common->num_ghosts) ) {
  1722. valid = false;
  1723. state->count_errors[0] = true;
  1724. for (x=1;x<state->common->params.w+1;x++)
  1725. for (y=1;y<state->common->params.h+1;y++) {
  1726. xy = x+y*(state->common->params.w+2);
  1727. if (state->common->xinfo[xy] >= 0 &&
  1728. guess[state->common->xinfo[xy]] == 1)
  1729. state->cell_errors[xy] = true;
  1730. }
  1731. }
  1732. if (count_vampires > state->common->num_vampires ||
  1733. (filled && count_vampires != state->common->num_vampires) ) {
  1734. valid = false;
  1735. state->count_errors[1] = true;
  1736. for (x=1;x<state->common->params.w+1;x++)
  1737. for (y=1;y<state->common->params.h+1;y++) {
  1738. xy = x+y*(state->common->params.w+2);
  1739. if (state->common->xinfo[xy] >= 0 &&
  1740. guess[state->common->xinfo[xy]] == 2)
  1741. state->cell_errors[xy] = true;
  1742. }
  1743. }
  1744. if (count_zombies > state->common->num_zombies ||
  1745. (filled && count_zombies != state->common->num_zombies) ) {
  1746. valid = false;
  1747. state->count_errors[2] = true;
  1748. for (x=1;x<state->common->params.w+1;x++)
  1749. for (y=1;y<state->common->params.h+1;y++) {
  1750. xy = x+y*(state->common->params.w+2);
  1751. if (state->common->xinfo[xy] >= 0 &&
  1752. guess[state->common->xinfo[xy]] == 4)
  1753. state->cell_errors[xy] = true;
  1754. }
  1755. }
  1756. return valid;
  1757. }
  1758. static bool check_path_solution(game_state *state, int p) {
  1759. int i;
  1760. bool mirror;
  1761. int count;
  1762. bool correct;
  1763. int unfilled;
  1764. count = 0;
  1765. mirror = false;
  1766. correct = true;
  1767. unfilled = 0;
  1768. for (i=0;i<state->common->paths[p].length;i++) {
  1769. if (state->common->paths[p].p[i] == -1) mirror = true;
  1770. else {
  1771. if (state->guess[state->common->paths[p].p[i]] == 1 && mirror)
  1772. count++;
  1773. else if (state->guess[state->common->paths[p].p[i]] == 2 && !mirror)
  1774. count++;
  1775. else if (state->guess[state->common->paths[p].p[i]] == 4)
  1776. count++;
  1777. else if (state->guess[state->common->paths[p].p[i]] == 7)
  1778. unfilled++;
  1779. }
  1780. }
  1781. if (count > state->common->paths[p].sightings_start ||
  1782. count + unfilled < state->common->paths[p].sightings_start)
  1783. {
  1784. correct = false;
  1785. state->hint_errors[state->common->paths[p].grid_start] = true;
  1786. }
  1787. count = 0;
  1788. mirror = false;
  1789. unfilled = 0;
  1790. for (i=state->common->paths[p].length-1;i>=0;i--) {
  1791. if (state->common->paths[p].p[i] == -1) mirror = true;
  1792. else {
  1793. if (state->guess[state->common->paths[p].p[i]] == 1 && mirror)
  1794. count++;
  1795. else if (state->guess[state->common->paths[p].p[i]] == 2 && !mirror)
  1796. count++;
  1797. else if (state->guess[state->common->paths[p].p[i]] == 4)
  1798. count++;
  1799. else if (state->guess[state->common->paths[p].p[i]] == 7)
  1800. unfilled++;
  1801. }
  1802. }
  1803. if (count > state->common->paths[p].sightings_end ||
  1804. count + unfilled < state->common->paths[p].sightings_end)
  1805. {
  1806. correct = false;
  1807. state->hint_errors[state->common->paths[p].grid_end] = true;
  1808. }
  1809. if (!correct) {
  1810. for (i=0;i<state->common->paths[p].length;i++)
  1811. state->cell_errors[state->common->paths[p].xy[i]] = true;
  1812. }
  1813. return correct;
  1814. }
  1815. static game_state *execute_move(const game_state *state, const char *move)
  1816. {
  1817. int x,y,n,p,i;
  1818. char c;
  1819. bool correct;
  1820. bool solver;
  1821. game_state *ret = dup_game(state);
  1822. solver = false;
  1823. while (*move) {
  1824. c = *move;
  1825. if (c == 'S') {
  1826. move++;
  1827. solver = true;
  1828. } else if (c == 'G' || c == 'V' || c == 'Z' || c == 'E' ||
  1829. c == 'g' || c == 'v' || c == 'z') {
  1830. move++;
  1831. if (sscanf(move, "%d%n", &x, &n) != 1) goto badmove;
  1832. if (x < 0 || x >= ret->common->num_total) goto badmove;
  1833. if (c == 'G') ret->guess[x] = 1;
  1834. if (c == 'V') ret->guess[x] = 2;
  1835. if (c == 'Z') ret->guess[x] = 4;
  1836. if (c == 'E') { ret->guess[x] = 7; ret->pencils[x] = 0; }
  1837. if (c == 'g') ret->pencils[x] ^= 1;
  1838. if (c == 'v') ret->pencils[x] ^= 2;
  1839. if (c == 'z') ret->pencils[x] ^= 4;
  1840. move += n;
  1841. } else if (c == 'D' && sscanf(move + 1, "%d,%d%n", &x, &y, &n) == 2 &&
  1842. is_clue(ret, x, y)) {
  1843. ret->hints_done[clue_index(ret, x, y)] ^= 1;
  1844. move += n + 1;
  1845. } else if (c == 'M') {
  1846. /*
  1847. * Fill in absolutely all pencil marks in unfilled
  1848. * squares, for those who like to play by the rigorous
  1849. * approach of starting off in that state and eliminating
  1850. * things.
  1851. */
  1852. for (i = 0; i < ret->common->num_total; i++)
  1853. if (ret->guess[i] == 7)
  1854. ret->pencils[i] = 7;
  1855. move++;
  1856. } else {
  1857. /* Unknown move type. */
  1858. badmove:
  1859. free_game(ret);
  1860. return NULL;
  1861. }
  1862. if (*move == ';') move++;
  1863. }
  1864. correct = true;
  1865. for (i=0;i<ret->common->wh;i++) ret->cell_errors[i] = false;
  1866. for (i=0;i<2*ret->common->num_paths;i++) ret->hint_errors[i] = false;
  1867. for (i=0;i<3;i++) ret->count_errors[i] = false;
  1868. if (!check_numbers_draw(ret,ret->guess)) correct = false;
  1869. for (p=0;p<state->common->num_paths;p++)
  1870. if (!check_path_solution(ret,p)) correct = false;
  1871. for (i=0;i<state->common->num_total;i++)
  1872. if (!(ret->guess[i] == 1 || ret->guess[i] == 2 ||
  1873. ret->guess[i] == 4)) correct = false;
  1874. if (correct && !solver) ret->solved = true;
  1875. if (solver) ret->cheated = true;
  1876. return ret;
  1877. }
  1878. /* ----------------------------------------------------------------------
  1879. * Drawing routines.
  1880. */
  1881. #define PREFERRED_TILE_SIZE 64
  1882. static void game_compute_size(const game_params *params, int tilesize,
  1883. const game_ui *ui, int *x, int *y)
  1884. {
  1885. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  1886. struct { int tilesize; } ads, *ds = &ads;
  1887. ads.tilesize = tilesize;
  1888. *x = 2*BORDER+(params->w+2)*TILESIZE;
  1889. *y = 2*BORDER+(params->h+3)*TILESIZE;
  1890. return;
  1891. }
  1892. static void game_set_size(drawing *dr, game_drawstate *ds,
  1893. const game_params *params, int tilesize)
  1894. {
  1895. ds->tilesize = tilesize;
  1896. return;
  1897. }
  1898. #define COLOUR(ret, i, r, g, b) ((ret[3*(i)+0] = (r)), (ret[3*(i)+1] = (g)), (ret[3*(i)+2] = (b)))
  1899. static float *game_colours(frontend *fe, int *ncolours)
  1900. {
  1901. float *ret = snewn(3 * NCOLOURS, float);
  1902. frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
  1903. ret[COL_GRID * 3 + 0] = 0.0F;
  1904. ret[COL_GRID * 3 + 1] = 0.0F;
  1905. ret[COL_GRID * 3 + 2] = 0.0F;
  1906. ret[COL_TEXT * 3 + 0] = 0.0F;
  1907. ret[COL_TEXT * 3 + 1] = 0.0F;
  1908. ret[COL_TEXT * 3 + 2] = 0.0F;
  1909. ret[COL_ERROR * 3 + 0] = 1.0F;
  1910. ret[COL_ERROR * 3 + 1] = 0.0F;
  1911. ret[COL_ERROR * 3 + 2] = 0.0F;
  1912. ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0];
  1913. ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1];
  1914. ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2];
  1915. ret[COL_FLASH * 3 + 0] = 1.0F;
  1916. ret[COL_FLASH * 3 + 1] = 1.0F;
  1917. ret[COL_FLASH * 3 + 2] = 1.0F;
  1918. ret[COL_GHOST * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.5F;
  1919. ret[COL_GHOST * 3 + 1] = ret[COL_BACKGROUND * 3 + 0];
  1920. ret[COL_GHOST * 3 + 2] = ret[COL_BACKGROUND * 3 + 0];
  1921. ret[COL_ZOMBIE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.5F;
  1922. ret[COL_ZOMBIE * 3 + 1] = ret[COL_BACKGROUND * 3 + 0];
  1923. ret[COL_ZOMBIE * 3 + 2] = ret[COL_BACKGROUND * 3 + 0] * 0.5F;
  1924. ret[COL_VAMPIRE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
  1925. ret[COL_VAMPIRE * 3 + 1] = ret[COL_BACKGROUND * 3 + 0] * 0.9F;
  1926. ret[COL_VAMPIRE * 3 + 2] = ret[COL_BACKGROUND * 3 + 0] * 0.9F;
  1927. ret[COL_DONE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 1.5F;
  1928. ret[COL_DONE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 1.5F;
  1929. ret[COL_DONE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 1.5F;
  1930. *ncolours = NCOLOURS;
  1931. return ret;
  1932. }
  1933. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1934. {
  1935. int i;
  1936. struct game_drawstate *ds = snew(struct game_drawstate);
  1937. ds->tilesize = 0;
  1938. ds->started = ds->solved = false;
  1939. ds->w = state->common->params.w;
  1940. ds->h = state->common->params.h;
  1941. ds->ascii = false;
  1942. ds->count_errors[0] = false;
  1943. ds->count_errors[1] = false;
  1944. ds->count_errors[2] = false;
  1945. ds->monsters = snewn(state->common->num_total,int);
  1946. for (i=0;i<(state->common->num_total);i++)
  1947. ds->monsters[i] = 7;
  1948. ds->pencils = snewn(state->common->num_total,unsigned char);
  1949. for (i=0;i<state->common->num_total;i++)
  1950. ds->pencils[i] = 0;
  1951. ds->cell_errors = snewn(state->common->wh,bool);
  1952. for (i=0;i<state->common->wh;i++)
  1953. ds->cell_errors[i] = false;
  1954. ds->hint_errors = snewn(2*state->common->num_paths,bool);
  1955. for (i=0;i<2*state->common->num_paths;i++)
  1956. ds->hint_errors[i] = false;
  1957. ds->hints_done = snewn(2 * state->common->num_paths, bool);
  1958. memset(ds->hints_done, 0,
  1959. 2 * state->common->num_paths * sizeof(bool));
  1960. ds->hshow = false;
  1961. ds->hpencil = false;
  1962. ds->hflash = false;
  1963. ds->hx = ds->hy = 0;
  1964. return ds;
  1965. }
  1966. static void game_free_drawstate(drawing *dr, game_drawstate *ds) {
  1967. sfree(ds->hints_done);
  1968. sfree(ds->hint_errors);
  1969. sfree(ds->cell_errors);
  1970. sfree(ds->pencils);
  1971. sfree(ds->monsters);
  1972. sfree(ds);
  1973. return;
  1974. }
  1975. static void draw_cell_background(drawing *dr, game_drawstate *ds,
  1976. const game_state *state, const game_ui *ui,
  1977. int x, int y) {
  1978. bool hon;
  1979. int dx,dy;
  1980. dx = BORDER+(x* ds->tilesize)+(TILESIZE/2);
  1981. dy = BORDER+(y* ds->tilesize)+(TILESIZE/2)+TILESIZE;
  1982. hon = (ui->hshow && x == ui->hx && y == ui->hy);
  1983. draw_rect(dr,dx-(TILESIZE/2)+1,dy-(TILESIZE/2)+1,TILESIZE-1,TILESIZE-1,(hon && !ui->hpencil) ? COL_HIGHLIGHT : COL_BACKGROUND);
  1984. if (hon && ui->hpencil) {
  1985. int coords[6];
  1986. coords[0] = dx-(TILESIZE/2)+1;
  1987. coords[1] = dy-(TILESIZE/2)+1;
  1988. coords[2] = coords[0] + TILESIZE/2;
  1989. coords[3] = coords[1];
  1990. coords[4] = coords[0];
  1991. coords[5] = coords[1] + TILESIZE/2;
  1992. draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
  1993. }
  1994. draw_update(dr,dx-(TILESIZE/2)+1,dy-(TILESIZE/2)+1,TILESIZE-1,TILESIZE-1);
  1995. return;
  1996. }
  1997. static void draw_circle_or_point(drawing *dr, int cx, int cy, int radius,
  1998. int colour)
  1999. {
  2000. if (radius > 0)
  2001. draw_circle(dr, cx, cy, radius, colour, colour);
  2002. else
  2003. draw_rect(dr, cx, cy, 1, 1, colour);
  2004. }
  2005. static void draw_monster(drawing *dr, game_drawstate *ds, int x, int y,
  2006. int tilesize, bool hflash, int monster)
  2007. {
  2008. int black = (hflash ? COL_FLASH : COL_TEXT);
  2009. if (monster == 1) { /* ghost */
  2010. int poly[80], i, j;
  2011. clip(dr,x-(tilesize/2)+2,y-(tilesize/2)+2,tilesize-3,tilesize/2+1);
  2012. draw_circle(dr,x,y,2*tilesize/5, COL_GHOST,black);
  2013. unclip(dr);
  2014. i = 0;
  2015. poly[i++] = x - 2*tilesize/5;
  2016. poly[i++] = y-2;
  2017. poly[i++] = x - 2*tilesize/5;
  2018. poly[i++] = y + 2*tilesize/5;
  2019. for (j = 0; j < 3; j++) {
  2020. int total = (2*tilesize/5) * 2;
  2021. int before = total * j / 3;
  2022. int after = total * (j+1) / 3;
  2023. int mid = (before + after) / 2;
  2024. poly[i++] = x - 2*tilesize/5 + mid;
  2025. poly[i++] = y + 2*tilesize/5 - (total / 6);
  2026. poly[i++] = x - 2*tilesize/5 + after;
  2027. poly[i++] = y + 2*tilesize/5;
  2028. }
  2029. poly[i++] = x + 2*tilesize/5;
  2030. poly[i++] = y-2;
  2031. clip(dr,x-(tilesize/2)+2,y,tilesize-3,tilesize-(tilesize/2)-1);
  2032. draw_polygon(dr, poly, i/2, COL_GHOST, black);
  2033. unclip(dr);
  2034. draw_circle(dr,x-tilesize/6,y-tilesize/12,tilesize/10,
  2035. COL_BACKGROUND,black);
  2036. draw_circle(dr,x+tilesize/6,y-tilesize/12,tilesize/10,
  2037. COL_BACKGROUND,black);
  2038. draw_circle_or_point(dr,x-tilesize/6+1+tilesize/48,y-tilesize/12,
  2039. tilesize/48,black);
  2040. draw_circle_or_point(dr,x+tilesize/6+1+tilesize/48,y-tilesize/12,
  2041. tilesize/48,black);
  2042. } else if (monster == 2) { /* vampire */
  2043. int poly[80], i;
  2044. clip(dr,x-(tilesize/2)+2,y-(tilesize/2)+2,tilesize-3,tilesize/2);
  2045. draw_circle(dr,x,y,2*tilesize/5,black,black);
  2046. unclip(dr);
  2047. clip(dr,x-(tilesize/2)+2,y-(tilesize/2)+2,tilesize/2+1,tilesize/2);
  2048. draw_circle(dr,x-tilesize/7,y,2*tilesize/5-tilesize/7,
  2049. COL_VAMPIRE,black);
  2050. unclip(dr);
  2051. clip(dr,x,y-(tilesize/2)+2,tilesize/2+1,tilesize/2);
  2052. draw_circle(dr,x+tilesize/7,y,2*tilesize/5-tilesize/7,
  2053. COL_VAMPIRE,black);
  2054. unclip(dr);
  2055. clip(dr,x-(tilesize/2)+2,y,tilesize-3,tilesize/2);
  2056. draw_circle(dr,x,y,2*tilesize/5, COL_VAMPIRE,black);
  2057. unclip(dr);
  2058. draw_circle(dr, x-tilesize/7, y-tilesize/16, tilesize/16,
  2059. COL_BACKGROUND, black);
  2060. draw_circle(dr, x+tilesize/7, y-tilesize/16, tilesize/16,
  2061. COL_BACKGROUND, black);
  2062. draw_circle_or_point(dr, x-tilesize/7, y-tilesize/16, tilesize/48,
  2063. black);
  2064. draw_circle_or_point(dr, x+tilesize/7, y-tilesize/16, tilesize/48,
  2065. black);
  2066. clip(dr, x-(tilesize/2)+2, y+tilesize/8, tilesize-3, tilesize/4);
  2067. i = 0;
  2068. poly[i++] = x-3*tilesize/16;
  2069. poly[i++] = y+1*tilesize/8;
  2070. poly[i++] = x-2*tilesize/16;
  2071. poly[i++] = y+7*tilesize/24;
  2072. poly[i++] = x-1*tilesize/16;
  2073. poly[i++] = y+1*tilesize/8;
  2074. draw_polygon(dr, poly, i/2, COL_BACKGROUND, black);
  2075. i = 0;
  2076. poly[i++] = x+3*tilesize/16;
  2077. poly[i++] = y+1*tilesize/8;
  2078. poly[i++] = x+2*tilesize/16;
  2079. poly[i++] = y+7*tilesize/24;
  2080. poly[i++] = x+1*tilesize/16;
  2081. poly[i++] = y+1*tilesize/8;
  2082. draw_polygon(dr, poly, i/2, COL_BACKGROUND, black);
  2083. draw_circle(dr, x, y-tilesize/5, 2*tilesize/5, COL_VAMPIRE, black);
  2084. unclip(dr);
  2085. } else if (monster == 4) { /* zombie */
  2086. draw_circle(dr,x,y,2*tilesize/5, COL_ZOMBIE,black);
  2087. draw_line(dr,
  2088. x-tilesize/7-tilesize/16, y-tilesize/12-tilesize/16,
  2089. x-tilesize/7+tilesize/16, y-tilesize/12+tilesize/16,
  2090. black);
  2091. draw_line(dr,
  2092. x-tilesize/7+tilesize/16, y-tilesize/12-tilesize/16,
  2093. x-tilesize/7-tilesize/16, y-tilesize/12+tilesize/16,
  2094. black);
  2095. draw_line(dr,
  2096. x+tilesize/7-tilesize/16, y-tilesize/12-tilesize/16,
  2097. x+tilesize/7+tilesize/16, y-tilesize/12+tilesize/16,
  2098. black);
  2099. draw_line(dr,
  2100. x+tilesize/7+tilesize/16, y-tilesize/12-tilesize/16,
  2101. x+tilesize/7-tilesize/16, y-tilesize/12+tilesize/16,
  2102. black);
  2103. clip(dr, x-tilesize/5, y+tilesize/6, 2*tilesize/5+1, tilesize/2);
  2104. draw_circle(dr, x-tilesize/15, y+tilesize/6, tilesize/12,
  2105. COL_BACKGROUND, black);
  2106. unclip(dr);
  2107. draw_line(dr, x-tilesize/5, y+tilesize/6, x+tilesize/5, y+tilesize/6,
  2108. black);
  2109. }
  2110. draw_update(dr,x-(tilesize/2)+2,y-(tilesize/2)+2,tilesize-3,tilesize-3);
  2111. }
  2112. static void draw_monster_count(drawing *dr, game_drawstate *ds,
  2113. const game_state *state, int c, bool hflash) {
  2114. int dx,dy;
  2115. char buf[MAX_DIGITS(int) + 1];
  2116. char bufm[8];
  2117. dy = TILESIZE/4;
  2118. dx = BORDER+(ds->w+2)*TILESIZE/2+TILESIZE/4;
  2119. switch (c) {
  2120. case 0:
  2121. sprintf(buf,"%d",state->common->num_ghosts);
  2122. sprintf(bufm,"G");
  2123. dx -= 3*TILESIZE/2;
  2124. break;
  2125. case 1:
  2126. sprintf(buf,"%d",state->common->num_vampires);
  2127. sprintf(bufm,"V");
  2128. break;
  2129. case 2:
  2130. sprintf(buf,"%d",state->common->num_zombies);
  2131. sprintf(bufm,"Z");
  2132. dx += 3*TILESIZE/2;
  2133. break;
  2134. }
  2135. draw_rect(dr, dx-2*TILESIZE/3, dy, 3*TILESIZE/2, TILESIZE,
  2136. COL_BACKGROUND);
  2137. if (!ds->ascii) {
  2138. draw_monster(dr, ds, dx-TILESIZE/3, dy+TILESIZE/2,
  2139. 2*TILESIZE/3, hflash, 1<<c);
  2140. } else {
  2141. draw_text(dr, dx-TILESIZE/3,dy+TILESIZE/2,FONT_VARIABLE,TILESIZE/2,
  2142. ALIGN_HCENTRE|ALIGN_VCENTRE,
  2143. hflash ? COL_FLASH : COL_TEXT, bufm);
  2144. }
  2145. draw_text(dr, dx, dy+TILESIZE/2, FONT_VARIABLE, TILESIZE/2,
  2146. ALIGN_HLEFT|ALIGN_VCENTRE,
  2147. (state->count_errors[c] ? COL_ERROR :
  2148. hflash ? COL_FLASH : COL_TEXT), buf);
  2149. draw_update(dr, dx-2*TILESIZE/3, dy, 3*TILESIZE/2, TILESIZE);
  2150. return;
  2151. }
  2152. static void draw_path_hint(drawing *dr, game_drawstate *ds,
  2153. const struct game_params *params,
  2154. int hint_index, bool hflash, int hint) {
  2155. int x, y, color, dx, dy, text_dx, text_dy, text_size;
  2156. char buf[MAX_DIGITS(int) + 1];
  2157. if (ds->hint_errors[hint_index])
  2158. color = COL_ERROR;
  2159. else if (hflash)
  2160. color = COL_FLASH;
  2161. else if (ds->hints_done[hint_index])
  2162. color = COL_DONE;
  2163. else
  2164. color = COL_TEXT;
  2165. range2grid(hint_index, params->w, params->h, &x, &y);
  2166. /* Upper-left corner of the "tile" */
  2167. dx = BORDER + x * TILESIZE;
  2168. dy = BORDER + y * TILESIZE + TILESIZE;
  2169. /* Center of the "tile" */
  2170. text_dx = dx + TILESIZE / 2;
  2171. text_dy = dy + TILESIZE / 2;
  2172. /* Avoid wiping out the borders of the puzzle */
  2173. dx += 2;
  2174. dy += 2;
  2175. text_size = TILESIZE - 3;
  2176. sprintf(buf,"%d", hint);
  2177. draw_rect(dr, dx, dy, text_size, text_size, COL_BACKGROUND);
  2178. draw_text(dr, text_dx, text_dy, FONT_FIXED, TILESIZE / 2,
  2179. ALIGN_HCENTRE | ALIGN_VCENTRE, color, buf);
  2180. draw_update(dr, dx, dy, text_size, text_size);
  2181. return;
  2182. }
  2183. static void draw_mirror(drawing *dr, game_drawstate *ds,
  2184. const game_state *state, int x, int y,
  2185. bool hflash, int mirror) {
  2186. int dx,dy,mx1,my1,mx2,my2;
  2187. dx = BORDER+(x* ds->tilesize)+(TILESIZE/2);
  2188. dy = BORDER+(y* ds->tilesize)+(TILESIZE/2)+TILESIZE;
  2189. if (mirror == CELL_MIRROR_L) {
  2190. mx1 = dx-(TILESIZE/4);
  2191. my1 = dy-(TILESIZE/4);
  2192. mx2 = dx+(TILESIZE/4);
  2193. my2 = dy+(TILESIZE/4);
  2194. }
  2195. else {
  2196. mx1 = dx-(TILESIZE/4);
  2197. my1 = dy+(TILESIZE/4);
  2198. mx2 = dx+(TILESIZE/4);
  2199. my2 = dy-(TILESIZE/4);
  2200. }
  2201. draw_thick_line(dr,(float)(TILESIZE/16),mx1,my1,mx2,my2,
  2202. hflash ? COL_FLASH : COL_TEXT);
  2203. draw_update(dr,dx-(TILESIZE/2)+1,dy-(TILESIZE/2)+1,TILESIZE-1,TILESIZE-1);
  2204. return;
  2205. }
  2206. static void draw_big_monster(drawing *dr, game_drawstate *ds,
  2207. const game_state *state, int x, int y,
  2208. bool hflash, int monster)
  2209. {
  2210. int dx,dy;
  2211. char buf[10];
  2212. dx = BORDER+(x* ds->tilesize)+(TILESIZE/2);
  2213. dy = BORDER+(y* ds->tilesize)+(TILESIZE/2)+TILESIZE;
  2214. if (ds->ascii) {
  2215. if (monster == 1) sprintf(buf,"G");
  2216. else if (monster == 2) sprintf(buf,"V");
  2217. else if (monster == 4) sprintf(buf,"Z");
  2218. else sprintf(buf," ");
  2219. draw_text(dr,dx,dy,FONT_FIXED,TILESIZE/2,ALIGN_HCENTRE|ALIGN_VCENTRE,
  2220. hflash ? COL_FLASH : COL_TEXT,buf);
  2221. draw_update(dr,dx-(TILESIZE/2)+2,dy-(TILESIZE/2)+2,TILESIZE-3,
  2222. TILESIZE-3);
  2223. }
  2224. else {
  2225. draw_monster(dr, ds, dx, dy, 3*TILESIZE/4, hflash, monster);
  2226. }
  2227. return;
  2228. }
  2229. static void draw_pencils(drawing *dr, game_drawstate *ds,
  2230. const game_state *state, int x, int y, int pencil)
  2231. {
  2232. int dx, dy;
  2233. int monsters[4];
  2234. int i, j, px, py;
  2235. char buf[10];
  2236. dx = BORDER+(x* ds->tilesize)+(TILESIZE/4);
  2237. dy = BORDER+(y* ds->tilesize)+(TILESIZE/4)+TILESIZE;
  2238. for (i = 0, j = 1; j < 8; j *= 2)
  2239. if (pencil & j)
  2240. monsters[i++] = j;
  2241. while (i < 4)
  2242. monsters[i++] = 0;
  2243. for (py = 0; py < 2; py++)
  2244. for (px = 0; px < 2; px++)
  2245. if (monsters[py*2+px]) {
  2246. if (!ds->ascii) {
  2247. draw_monster(dr, ds,
  2248. dx + TILESIZE/2 * px, dy + TILESIZE/2 * py,
  2249. TILESIZE/2, false, monsters[py*2+px]);
  2250. }
  2251. else {
  2252. switch (monsters[py*2+px]) {
  2253. case 1: sprintf(buf,"G"); break;
  2254. case 2: sprintf(buf,"V"); break;
  2255. case 4: sprintf(buf,"Z"); break;
  2256. }
  2257. draw_text(dr,dx + TILESIZE/2 * px,dy + TILESIZE/2 * py,
  2258. FONT_FIXED,TILESIZE/4,ALIGN_HCENTRE|ALIGN_VCENTRE,
  2259. COL_TEXT,buf);
  2260. }
  2261. }
  2262. draw_update(dr,dx-(TILESIZE/4)+2,dy-(TILESIZE/4)+2,
  2263. (TILESIZE/2)-3,(TILESIZE/2)-3);
  2264. return;
  2265. }
  2266. #define FLASH_TIME 0.7F
  2267. static bool is_hint_stale(const game_drawstate *ds, bool hflash,
  2268. const game_state *state, int index)
  2269. {
  2270. bool ret = false;
  2271. if (!ds->started) ret = true;
  2272. if (ds->hflash != hflash) ret = true;
  2273. if (ds->hint_errors[index] != state->hint_errors[index]) {
  2274. ds->hint_errors[index] = state->hint_errors[index];
  2275. ret = true;
  2276. }
  2277. if (ds->hints_done[index] != state->hints_done[index]) {
  2278. ds->hints_done[index] = state->hints_done[index];
  2279. ret = true;
  2280. }
  2281. return ret;
  2282. }
  2283. static void game_redraw(drawing *dr, game_drawstate *ds,
  2284. const game_state *oldstate, const game_state *state,
  2285. int dir, const game_ui *ui,
  2286. float animtime, float flashtime)
  2287. {
  2288. int i,j,x,y,xy;
  2289. int xi, c;
  2290. bool stale, hflash, hchanged, changed_ascii;
  2291. hflash = (int)(flashtime * 5 / FLASH_TIME) % 2;
  2292. /* Draw static grid components at startup */
  2293. if (!ds->started) {
  2294. draw_rect(dr, BORDER+TILESIZE-1, BORDER+2*TILESIZE-1,
  2295. (ds->w)*TILESIZE +3, (ds->h)*TILESIZE +3, COL_GRID);
  2296. for (i=0;i<ds->w;i++)
  2297. for (j=0;j<ds->h;j++)
  2298. draw_rect(dr, BORDER+(ds->tilesize*(i+1))+1,
  2299. BORDER+(ds->tilesize*(j+2))+1, ds->tilesize-1,
  2300. ds->tilesize-1, COL_BACKGROUND);
  2301. draw_update(dr, 0, 0, 2*BORDER+(ds->w+2)*TILESIZE,
  2302. 2*BORDER+(ds->h+3)*TILESIZE);
  2303. }
  2304. hchanged = false;
  2305. if (ds->hx != ui->hx || ds->hy != ui->hy ||
  2306. ds->hshow != ui->hshow || ds->hpencil != ui->hpencil)
  2307. hchanged = true;
  2308. if (ds->ascii != ui->ascii) {
  2309. ds->ascii = ui->ascii;
  2310. changed_ascii = true;
  2311. } else
  2312. changed_ascii = false;
  2313. /* Draw monster count hints */
  2314. for (i=0;i<3;i++) {
  2315. stale = false;
  2316. if (!ds->started) stale = true;
  2317. if (ds->hflash != hflash) stale = true;
  2318. if (changed_ascii) stale = true;
  2319. if (ds->count_errors[i] != state->count_errors[i]) {
  2320. stale = true;
  2321. ds->count_errors[i] = state->count_errors[i];
  2322. }
  2323. if (stale) {
  2324. draw_monster_count(dr, ds, state, i, hflash);
  2325. }
  2326. }
  2327. /* Draw path count hints */
  2328. for (i=0;i<state->common->num_paths;i++) {
  2329. struct path *path = &state->common->paths[i];
  2330. if (is_hint_stale(ds, hflash, state, path->grid_start)) {
  2331. draw_path_hint(dr, ds, &state->common->params, path->grid_start,
  2332. hflash, path->sightings_start);
  2333. }
  2334. if (is_hint_stale(ds, hflash, state, path->grid_end)) {
  2335. draw_path_hint(dr, ds, &state->common->params, path->grid_end,
  2336. hflash, path->sightings_end);
  2337. }
  2338. }
  2339. /* Draw puzzle grid contents */
  2340. for (x = 1; x < ds->w+1; x++)
  2341. for (y = 1; y < ds->h+1; y++) {
  2342. stale = false;
  2343. xy = x+y*(state->common->params.w+2);
  2344. xi = state->common->xinfo[xy];
  2345. c = state->common->grid[xy];
  2346. if (!ds->started) stale = true;
  2347. if (ds->hflash != hflash) stale = true;
  2348. if (changed_ascii) stale = true;
  2349. if (hchanged) {
  2350. if ((x == ui->hx && y == ui->hy) ||
  2351. (x == ds->hx && y == ds->hy))
  2352. stale = true;
  2353. }
  2354. if (xi >= 0 && (state->guess[xi] != ds->monsters[xi]) ) {
  2355. stale = true;
  2356. ds->monsters[xi] = state->guess[xi];
  2357. }
  2358. if (xi >= 0 && (state->pencils[xi] != ds->pencils[xi]) ) {
  2359. stale = true;
  2360. ds->pencils[xi] = state->pencils[xi];
  2361. }
  2362. if (state->cell_errors[xy] != ds->cell_errors[xy]) {
  2363. stale = true;
  2364. ds->cell_errors[xy] = state->cell_errors[xy];
  2365. }
  2366. if (stale) {
  2367. draw_cell_background(dr, ds, state, ui, x, y);
  2368. if (xi < 0)
  2369. draw_mirror(dr, ds, state, x, y, hflash, c);
  2370. else if (state->guess[xi] == 1 || state->guess[xi] == 2 ||
  2371. state->guess[xi] == 4)
  2372. draw_big_monster(dr, ds, state, x, y, hflash, state->guess[xi]);
  2373. else
  2374. draw_pencils(dr, ds, state, x, y, state->pencils[xi]);
  2375. }
  2376. }
  2377. ds->hx = ui->hx; ds->hy = ui->hy;
  2378. ds->hshow = ui->hshow;
  2379. ds->hpencil = ui->hpencil;
  2380. ds->hflash = hflash;
  2381. ds->started = true;
  2382. return;
  2383. }
  2384. static float game_anim_length(const game_state *oldstate,
  2385. const game_state *newstate, int dir, game_ui *ui)
  2386. {
  2387. return 0.0F;
  2388. }
  2389. static float game_flash_length(const game_state *oldstate,
  2390. const game_state *newstate, int dir, game_ui *ui)
  2391. {
  2392. return (!oldstate->solved && newstate->solved && !oldstate->cheated &&
  2393. !newstate->cheated) ? FLASH_TIME : 0.0F;
  2394. }
  2395. static void game_get_cursor_location(const game_ui *ui,
  2396. const game_drawstate *ds,
  2397. const game_state *state,
  2398. const game_params *params,
  2399. int *x, int *y, int *w, int *h)
  2400. {
  2401. if(ui->hshow) {
  2402. *x = BORDER + (ui->hx) * TILESIZE;
  2403. *y = BORDER + (ui->hy + 1) * TILESIZE;
  2404. *w = *h = TILESIZE;
  2405. }
  2406. }
  2407. static int game_status(const game_state *state)
  2408. {
  2409. return state->solved;
  2410. }
  2411. #ifdef COMBINED
  2412. #define thegame undead
  2413. #endif
  2414. const struct game thegame = {
  2415. "Undead", "games.undead", "undead",
  2416. default_params,
  2417. game_fetch_preset, NULL,
  2418. decode_params,
  2419. encode_params,
  2420. free_params,
  2421. dup_params,
  2422. true, game_configure, custom_params,
  2423. validate_params,
  2424. new_game_desc,
  2425. validate_desc,
  2426. new_game,
  2427. dup_game,
  2428. free_game,
  2429. true, solve_game,
  2430. true, game_can_format_as_text_now, game_text_format,
  2431. get_prefs, set_prefs,
  2432. new_ui,
  2433. free_ui,
  2434. NULL, /* encode_ui */
  2435. NULL, /* decode_ui */
  2436. game_request_keys,
  2437. game_changed_state,
  2438. current_key_label,
  2439. interpret_move,
  2440. execute_move,
  2441. PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
  2442. game_colours,
  2443. game_new_drawstate,
  2444. game_free_drawstate,
  2445. game_redraw,
  2446. game_anim_length,
  2447. game_flash_length,
  2448. game_get_cursor_location,
  2449. game_status,
  2450. false, false, NULL, NULL, /* print_size, print */
  2451. false, /* wants_statusbar */
  2452. false, NULL, /* timing_state */
  2453. 0, /* flags */
  2454. };