summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lua.h
blob: a0c57dc60b46e77be97d0ec6a3be7e8a99301f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
** $Id$
** Lua - An Extensible Extension Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
*/


#ifndef lua_h
#define lua_h

#include <stdarg.h>
#include <stddef.h>


#include "luaconf.h"


#define LUA_VERSION	"Lua 5.1"
#define LUA_RELEASE	"Lua 5.1.4"
#define LUA_VERSION_NUM	501
#define LUA_COPYRIGHT	"Copyright (C) 1994-2008 Lua.org, PUC-Rio"
#define LUA_AUTHORS 	"R. Ierusalimschy, L. H. de Figueiredo & W. Celes"


/* mark for precompiled code (`<esc>Lua') */
#define	LUA_SIGNATURE	"\033Lua"

/* option for multiple returns in `lua_pcall' and `lua_call' */
#define LUA_MULTRET	(-1)


/*
** pseudo-indices
*/
#define LUA_REGISTRYINDEX	(-10000)
#define LUA_ENVIRONINDEX	(-10001)
#define LUA_GLOBALSINDEX	(-10002)
#define lua_upvalueindex(i)	(LUA_GLOBALSINDEX-(i))


/* thread status; 0 is OK */
#define LUA_YIELD	1
#define LUA_ERRRUN	2
#define LUA_ERRSYNTAX	3
#define LUA_ERRMEM	4
#define LUA_ERRERR	5


typedef struct lua_State lua_State;

typedef int (*lua_CFunction) (lua_State *L);


/*
** functions that read/write blocks when loading/dumping Lua chunks
*/
typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);

typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);


/*
** prototype for memory-allocation functions
*/
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);


/*
** basic types
*/
#define LUA_TNONE		(-1)

#define LUA_TNIL		0
#define LUA_TBOOLEAN		1
#define LUA_TLIGHTUSERDATA	2
#define LUA_TNUMBER		3
#define LUA_TSTRING		4
#define LUA_TTABLE		5
#define LUA_TFUNCTION		6
#define LUA_TUSERDATA		7
#define LUA_TTHREAD		8



/* minimum Lua stack available to a C function */
#define LUA_MINSTACK	20


/*
** generic extra include file
*/
#if defined(LUA_USER_H)
#include LUA_USER_H
#endif


/* type of numbers in Lua */
typedef LUA_NUMBER lua_Number;


/* type for integer functions */
typedef LUA_INTEGER lua_Integer;



/*
** state manipulation
*/
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
LUA_API void       (lua_close) (lua_State *L);
LUA_API lua_State *(lua_newthread) (lua_State *L);

LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);


/*
** basic stack manipulation
*/
LUA_API int   (lua_gettop) (lua_State *L);
LUA_API void  (lua_settop) (lua_State *L, int idx);
LUA_API void  (lua_pushvalue) (lua_State *L, int idx);
LUA_API void  (lua_remove) (lua_State *L, int idx);
LUA_API void  (lua_insert) (lua_State *L, int idx);
LUA_API void  (lua_replace) (lua_State *L, int idx);
LUA_API int   (lua_checkstack) (lua_State *L, int sz);

LUA_API void  (lua_xmove) (lua_State *from, lua_State *to, int n);


/*
** access functions (stack -> C)
*/

LUA_API int             (lua_isnumber) (lua_State *L, int idx);
LUA_API int             (lua_isstring) (lua_State *L, int idx);
LUA_API int             (lua_iscfunction) (lua_State *L, int idx);
LUA_API int             (lua_isuserdata) (lua_State *L, int idx);
LUA_API int             (lua_type) (lua_State *L, int idx);
LUA_API const char     *(lua_typename) (lua_State *L, int tp);

LUA_API int            (lua_equal) (lua_State *L, int idx1, int idx2);
LUA_API int            (lua_rawequal) (lua_State *L, int idx1, int idx2);
LUA_API int            (lua_lessthan) (lua_State *L, int idx1, int idx2);

LUA_API lua_Number      (lua_tonumber) (lua_State *L, int idx);
LUA_API lua_Integer     (lua_tointeger) (lua_State *L, int idx);
LUA_API int             (lua_toboolean) (lua_State *L, int idx);
LUA_API const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);
LUA_API size_t          (lua_objlen) (lua_State *L, int idx);
LUA_API lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);
LUA_API void	       *(lua_touserdata) (lua_State *L, int idx);
LUA_API lua_State      *(lua_tothread) (lua_State *L, int idx);
LUA_API const void     *(lua_topointer) (lua_State *L, int idx);


/*
** push functions (C -> stack)
*/
LUA_API void  (lua_pushnil) (lua_State *L);
LUA_API void  (lua_pushnumber) (lua_State *L, lua_Number n);
LUA_API void  (lua_pushinteger) (lua_State *L, lua_Integer n);
LUA_API void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);
LUA_API void  (lua_pushstring) (lua_State *L, const char *s);
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
                                                      va_list argp);
LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
LUA_API void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
LUA_API void  (lua_pushboolean) (lua_State *L, int b);
LUA_API void  (lua_pushlightuserdata) (lua_State *L, void *p);
LUA_API int   (lua_pushthread) (lua_State *L);


/*
** get functions (Lua -> stack)
*/
LUA_API void  (lua_gettable) (lua_State *L, int idx);
LUA_API void  (lua_getfield) (lua_State *L, int idx, const char *k);
LUA_API void  (lua_rawget) (lua_State *L, int idx);
LUA_API void  (lua_rawgeti) (lua_State *L, int idx, int n);
LUA_API void  (lua_createtable) (lua_State *L, int narr, int nrec);
LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
LUA_API int   (lua_getmetatable) (lua_State *L, int objindex);
LUA_API void  (lua_getfenv) (lua_State *L, int idx);


/*
** set functions (stack -> Lua)
*/
LUA_API void  (lua_settable) (lua_State *L, int idx);
LUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);
LUA_API void  (lua_rawset) (lua_State *L, int idx);
LUA_API void  (lua_rawseti) (lua_State *L, int idx, int n);
LUA_API int   (lua_setmetatable) (lua_State *L, int objindex);
LUA_API int   (lua_setfenv) (lua_State *L, int idx);


/*
** `load' and `call' functions (load and run Lua code)
*/
LUA_API void  (lua_call) (lua_State *L, int nargs, int nresults);
LUA_API int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
LUA_API int   (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
LUA_API int   (lua_load) (lua_State *L, lua_Reader reader, void *dt,
                                        const char *chunkname);

LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);


/*
** coroutine functions
*/
LUA_API int  (lua_yield) (lua_State *L, int nresults);
LUA_API int  (lua_resume) (lua_State *L, int narg);
LUA_API int  (lua_status) (lua_State *L);

/*
** garbage-collection function and options
*/

#define LUA_GCSTOP		0
#define LUA_GCRESTART		1
#define LUA_GCCOLLECT		2
#define LUA_GCCOUNT		3
#define LUA_GCCOUNTB		4
#define LUA_GCSTEP		5
#define LUA_GCSETPAUSE		6
#define LUA_GCSETSTEPMUL	7

LUA_API int (lua_gc) (lua_State *L, int what, int data);


/*
** miscellaneous functions
*/

LUA_API int   (lua_error) (lua_State *L);

LUA_API int   (lua_next) (lua_State *L, int idx);

LUA_API void  (lua_concat) (lua_State *L, int n);

LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);



/* 
** ===============================================================
** some useful macros
** ===============================================================
*/

#define lua_pop(L,n)		lua_settop(L, -(n)-1)

#define lua_newtable(L)		lua_createtable(L, 0, 0)

#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))

#define lua_pushcfunction(L,f)	lua_pushcclosure(L, (f), 0)

#define lua_strlen(L,i)		lua_objlen(L, (i))

#define lua_isfunction(L,n)	(lua_type(L, (n)) == LUA_TFUNCTION)
#define lua_istable(L,n)	(lua_type(L, (n)) == LUA_TTABLE)
#define lua_islightuserdata(L,n)	(lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
#define lua_isnil(L,n)		(lua_type(L, (n)) == LUA_TNIL)
#define lua_isboolean(L,n)	(lua_type(L, (n)) == LUA_TBOOLEAN)
#define lua_isthread(L,n)	(lua_type(L, (n)) == LUA_TTHREAD)
#define lua_isnone(L,n)		(lua_type(L, (n)) == LUA_TNONE)
#define lua_isnoneornil(L, n)	(lua_type(L, (n)) <= 0)

#define lua_pushliteral(L, s)	\
	lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)

#define lua_setglobal(L,s)	lua_setfield(L, LUA_GLOBALSINDEX, (s))
#define lua_getglobal(L,s)	lua_getfield(L, LUA_GLOBALSINDEX, (s))

#define lua_tostring(L,i)	lua_tolstring(L, (i), NULL)



/*
** compatibility macros and functions
*/

#define lua_open()	luaL_newstate()

#define lua_getregistry(L)	lua_pushvalue(L, LUA_REGISTRYINDEX)

#define lua_getgccount(L)	lua_gc(L, LUA_GCCOUNT, 0)

#define lua_Chunkreader		lua_Reader
#define lua_Chunkwriter		lua_Writer


/* hack */
LUA_API void lua_setlevel	(lua_State *from, lua_State *to);


/*
** {======================================================================
** Debug API
** =======================================================================
*/


/*
** Event codes
*/
#define LUA_HOOKCALL	0
#define LUA_HOOKRET	1
#define LUA_HOOKLINE	2
#define LUA_HOOKCOUNT	3
#define LUA_HOOKTAILRET 4


/*
** Event masks
*/
#define LUA_MASKCALL	(1 << LUA_HOOKCALL)
#define LUA_MASKRET	(1 << LUA_HOOKRET)
#define LUA_MASKLINE	(1 << LUA_HOOKLINE)
#define LUA_MASKCOUNT	(1 << LUA_HOOKCOUNT)

typedef struct lua_Debug lua_Debug;  /* activation record */


/* Functions to be called by the debuger in specific events */
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);


LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);

LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
LUA_API lua_Hook lua_gethook (lua_State *L);
LUA_API int lua_gethookmask (lua_State *L);
LUA_API int lua_gethookcount (lua_State *L);


struct lua_Debug {
  int event;
  const char *name;	/* (n) */
  const char *namewhat;	/* (n) `global', `local', `field', `method' */
  const char *what;	/* (S) `Lua', `C', `main', `tail' */
  const char *source;	/* (S) */
  int currentline;	/* (l) */
  int nups;		/* (u) number of upvalues */
  int linedefined;	/* (S) */
  int lastlinedefined;	/* (S) */
  char short_src[LUA_IDSIZE]; /* (S) */
  /* private part */
  int i_ci;  /* active function */
};

/* }====================================================================== */


/******************************************************************************
* Copyright (C) 1994-2008 Lua.org, PUC-Rio.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/


#endif
n>h = 7; ret->type = TYPE_CROSS; return ret; } static const struct game_params pegs_presets[] = { {7, 7, TYPE_CROSS}, {7, 7, TYPE_OCTAGON}, {5, 5, TYPE_RANDOM}, {7, 7, TYPE_RANDOM}, {9, 9, TYPE_RANDOM}, }; static bool game_fetch_preset(int i, char **name, game_params **params) { game_params *ret; char str[80]; if (i < 0 || i >= lenof(pegs_presets)) return false; ret = snew(game_params); *ret = pegs_presets[i]; strcpy(str, pegs_titletypes[ret->type]); if (ret->type == TYPE_RANDOM) sprintf(str + strlen(str), " %dx%d", ret->w, ret->h); *name = dupstr(str); *params = ret; return true; } static void free_params(game_params *params) { sfree(params); } static game_params *dup_params(const game_params *params) { game_params *ret = snew(game_params); *ret = *params; /* structure copy */ return ret; } static void decode_params(game_params *params, char const *string) { char const *p = string; int i; params->w = atoi(p); while (*p && isdigit((unsigned char)*p)) p++; if (*p == 'x') { p++; params->h = atoi(p); while (*p && isdigit((unsigned char)*p)) p++; } else { params->h = params->w; } for (i = 0; i < lenof(pegs_lowertypes); i++) if (!strcmp(p, pegs_lowertypes[i])) params->type = i; } static char *encode_params(const game_params *params, bool full) { char str[80]; sprintf(str, "%dx%d", params->w, params->h); if (full) { assert(params->type >= 0 && params->type < lenof(pegs_lowertypes)); strcat(str, pegs_lowertypes[params->type]); } return dupstr(str); } static config_item *game_configure(const game_params *params) { config_item *ret = snewn(4, config_item); char buf[80]; ret[0].name = "Width"; ret[0].type = C_STRING; sprintf(buf, "%d", params->w); ret[0].u.string.sval = dupstr(buf); ret[1].name = "Height"; ret[1].type = C_STRING; sprintf(buf, "%d", params->h); ret[1].u.string.sval = dupstr(buf); ret[2].name = "Board type"; ret[2].type = C_CHOICES; ret[2].u.choices.choicenames = TYPECONFIG; ret[2].u.choices.selected = params->type; ret[3].name = NULL; ret[3].type = C_END; return ret; } static game_params *custom_params(const config_item *cfg) { game_params *ret = snew(game_params); ret->w = atoi(cfg[0].u.string.sval); ret->h = atoi(cfg[1].u.string.sval); ret->type = cfg[2].u.choices.selected; return ret; } static const char *validate_params(const game_params *params, bool full) { if (full && (params->w <= 3 || params->h <= 3)) return "Width and height must both be greater than three"; /* * It might be possible to implement generalisations of Cross * and Octagon, but only if I can find a proof that they're all * soluble. For the moment, therefore, I'm going to disallow * them at any size other than the standard one. */ if (full && (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON)) { if (params->w != 7 || params->h != 7) return "This board type is only supported at 7x7"; } return NULL; } /* ---------------------------------------------------------------------- * Beginning of code to generate random Peg Solitaire boards. * * This procedure is done with no aesthetic judgment, no effort at * symmetry, no difficulty grading and generally no finesse * whatsoever. We simply begin with an empty board containing a * single peg, and repeatedly make random reverse moves until it's * plausibly full. This typically yields a scrappy haphazard mess * with several holes, an uneven shape, and no redeeming features * except guaranteed solubility. * * My only concessions to sophistication are (a) to repeat the * generation process until I at least get a grid that touches * every edge of the specified board size, and (b) to try when * selecting moves to reuse existing space rather than expanding * into new space (so that non-rectangular board shape becomes a * factor during play). */ struct move { /* * x,y are the start point of the move during generation (hence * its endpoint during normal play). * * dx,dy are the direction of the move during generation. * Absolute value 1. Hence, for example, x=3,y=5,dx=1,dy=0 * means that the move during generation starts at (3,5) and * ends at (5,5), and vice versa during normal play. */ int x, y, dx, dy; /* * cost is 0, 1 or 2, depending on how many GRID_OBSTs we must * turn into GRID_HOLEs to play this move. */ int cost; }; static int movecmp(void *av, void *bv) { struct move *a = (struct move *)av; struct move *b = (struct move *)bv; if (a->y < b->y) return -1; else if (a->y > b->y) return +1; if (a->x < b->x) return -1; else if (a->x > b->x) return +1; if (a->dy < b->dy) return -1; else if (a->dy > b->dy) return +1; if (a->dx < b->dx) return -1; else if (a->dx > b->dx) return +1; return 0; } static int movecmpcost(void *av, void *bv) { struct move *a = (struct move *)av; struct move *b = (struct move *)bv; if (a->cost < b->cost) return -1; else if (a->cost > b->cost) return +1; return movecmp(av, bv); } struct movetrees { tree234 *bymove, *bycost; }; static void update_moves(unsigned char *grid, int w, int h, int x, int y, struct movetrees *trees) { struct move move; int dir, pos; /* * There are twelve moves that can include (x,y): three in each * of four directions. Check each one to see if it's possible. */ for (dir = 0; dir < 4; dir++) { int dx, dy; if (dir & 1) dx = 0, dy = dir - 2; else dy = 0, dx = dir - 1; assert(abs(dx) + abs(dy) == 1); for (pos = 0; pos < 3; pos++) { int v1, v2, v3; move.dx = dx; move.dy = dy; move.x = x - pos*dx; move.y = y - pos*dy; if (move.x < 0 || move.x >= w || move.y < 0 || move.y >= h) continue; /* completely invalid move */ if (move.x+2*move.dx < 0 || move.x+2*move.dx >= w || move.y+2*move.dy < 0 || move.y+2*move.dy >= h) continue; /* completely invalid move */ v1 = grid[move.y * w + move.x]; v2 = grid[(move.y+move.dy) * w + (move.x+move.dx)]; v3 = grid[(move.y+2*move.dy)*w + (move.x+2*move.dx)]; if (v1 == GRID_PEG && v2 != GRID_PEG && v3 != GRID_PEG) { struct move *m; move.cost = (v2 == GRID_OBST) + (v3 == GRID_OBST); /* * This move is possible. See if it's already in * the tree. */ m = find234(trees->bymove, &move, NULL); if (m && m->cost != move.cost) { /* * It's in the tree but listed with the wrong * cost. Remove the old version. */ #ifdef GENERATION_DIAGNOSTICS printf("correcting %d%+d,%d%+d at cost %d\n", m->x, m->dx, m->y, m->dy, m->cost); #endif del234(trees->bymove, m); del234(trees->bycost, m); sfree(m); m = NULL; } if (!m) { struct move *m, *m2; m = snew(struct move); *m = move; m2 = add234(trees->bymove, m); m2 = add234(trees->bycost, m); assert(m2 == m); #ifdef GENERATION_DIAGNOSTICS printf("adding %d%+d,%d%+d at cost %d\n", move.x, move.dx, move.y, move.dy, move.cost); #endif } else { #ifdef GENERATION_DIAGNOSTICS printf("not adding %d%+d,%d%+d at cost %d\n", move.x, move.dx, move.y, move.dy, move.cost); #endif } } else { /* * This move is impossible. If it is already in the * tree, delete it. * * (We make use here of the fact that del234 * doesn't have to be passed a pointer to the * _actual_ element it's deleting: it merely needs * one that compares equal to it, and it will * return the one it deletes.) */ struct move *m = del234(trees->bymove, &move); #ifdef GENERATION_DIAGNOSTICS printf("%sdeleting %d%+d,%d%+d\n", m ? "" : "not ", move.x, move.dx, move.y, move.dy); #endif if (m) { del234(trees->bycost, m); sfree(m); } } } } } static void pegs_genmoves(unsigned char *grid, int w, int h, random_state *rs) { struct movetrees atrees, *trees = &atrees; struct move *m; int x, y, i, nmoves; trees->bymove = newtree234(movecmp); trees->bycost = newtree234(movecmpcost); for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (grid[y*w+x] == GRID_PEG) update_moves(grid, w, h, x, y, trees); nmoves = 0; while (1) { int limit, maxcost, index; struct move mtmp, move, *m; /* * See how many moves we can make at zero cost. Make one, * if possible. Failing that, make a one-cost move, and * then a two-cost one. * * After filling at least half the input grid, we no longer * accept cost-2 moves: if that's our only option, we give * up and finish. */ mtmp.y = h+1; maxcost = (nmoves < w*h/2 ? 2 : 1); m = NULL; /* placate optimiser */ for (mtmp.cost = 0; mtmp.cost <= maxcost; mtmp.cost++) { limit = -1; m = findrelpos234(trees->bycost, &mtmp, NULL, REL234_LT, &limit); #ifdef GENERATION_DIAGNOSTICS printf("%d moves available with cost %d\n", limit+1, mtmp.cost); #endif if (m) break; } if (!m) break; index = random_upto(rs, limit+1); move = *(struct move *)index234(trees->bycost, index); #ifdef GENERATION_DIAGNOSTICS printf("selecting move %d%+d,%d%+d at cost %d\n", move.x, move.dx, move.y, move.dy, move.cost); #endif grid[move.y * w + move.x] = GRID_HOLE; grid[(move.y+move.dy) * w + (move.x+move.dx)] = GRID_PEG; grid[(move.y+2*move.dy)*w + (move.x+2*move.dx)] = GRID_PEG; for (i = 0; i <= 2; i++) { int tx = move.x + i*move.dx; int ty = move.y + i*move.dy; update_moves(grid, w, h, tx, ty, trees); } nmoves++; } while ((m = delpos234(trees->bymove, 0)) != NULL) { del234(trees->bycost, m); sfree(m); } freetree234(trees->bymove); freetree234(trees->bycost); } static void pegs_generate(unsigned char *grid, int w, int h, random_state *rs) { while (1) { int x, y, extremes; memset(grid, GRID_OBST, w*h); grid[(h/2) * w + (w/2)] = GRID_PEG; #ifdef GENERATION_DIAGNOSTICS printf("beginning move selection\n"); #endif pegs_genmoves(grid, w, h, rs); #ifdef GENERATION_DIAGNOSTICS printf("finished move selection\n"); #endif extremes = 0; for (y = 0; y < h; y++) { if (grid[y*w+0] != GRID_OBST) extremes |= 1; if (grid[y*w+w-1] != GRID_OBST) extremes |= 2; } for (x = 0; x < w; x++) { if (grid[0*w+x] != GRID_OBST) extremes |= 4; if (grid[(h-1)*w+x] != GRID_OBST) extremes |= 8; } if (extremes == 15) break; #ifdef GENERATION_DIAGNOSTICS printf("insufficient extent; trying again\n"); #endif } #ifdef GENERATION_DIAGNOSTICS fflush(stdout); #endif } /* ---------------------------------------------------------------------- * End of board generation code. Now for the client code which uses * it as part of the puzzle. */ static char *new_game_desc(const game_params *params, random_state *rs, char **aux, bool interactive) { int w = params->w, h = params->h; unsigned char *grid; char *ret; int i; grid = snewn(w*h, unsigned char); if (params->type == TYPE_RANDOM) { pegs_generate(grid, w, h, rs); } else { int x, y, cx, cy, v; for (y = 0; y < h; y++) for (x = 0; x < w; x++) { v = GRID_OBST; /* placate optimiser */ switch (params->type) { case TYPE_CROSS: cx = abs(x - w/2); cy = abs(y - h/2); if (cx == 0 && cy == 0) v = GRID_HOLE; else if (cx > 1 && cy > 1) v = GRID_OBST; else v = GRID_PEG; break; case TYPE_OCTAGON: cx = abs(x - w/2); cy = abs(y - h/2); if (cx + cy > 1 + max(w,h)/2) v = GRID_OBST; else v = GRID_PEG; break; } grid[y*w+x] = v; } if (params->type == TYPE_OCTAGON) { /* * The octagonal (European) solitaire layout is * actually _insoluble_ with the starting hole at the * centre. Here's a proof: * * Colour the squares of the board diagonally in * stripes of three different colours, which I'll call * A, B and C. So the board looks like this: * * A B C * A B C A B * A B C A B C A * B C A B C A B * C A B C A B C * B C A B C * A B C * * Suppose we keep running track of the number of pegs * occuping each colour of square. This colouring has * the property that any valid move whatsoever changes * all three of those counts by one (two of them go * down and one goes up), which means that the _parity_ * of every count flips on every move. * * If the centre square starts off unoccupied, then * there are twelve pegs on each colour and all three * counts start off even; therefore, after 35 moves all * three counts would have to be odd, which isn't * possible if there's only one peg left. [] * * This proof works just as well if the starting hole * is _any_ of the thirteen positions labelled B. Also, * we can stripe the board in the opposite direction * and rule out any square labelled B in that colouring * as well. This leaves: * * Y n Y * n n Y n n * Y n n Y n n Y * n Y Y n Y Y n * Y n n Y n n Y * n n Y n n * Y n Y * * where the ns are squares we've proved insoluble, and * the Ys are the ones remaining. * * That doesn't prove all those starting positions to * be soluble, of course; they're merely the ones we * _haven't_ proved to be impossible. Nevertheless, it * turns out that they are all soluble, so when the * user requests an Octagon board the simplest thing is * to pick one of these at random. * * Rather than picking equiprobably from those twelve * positions, we'll pick equiprobably from the three * equivalence classes */ switch (random_upto(rs, 3)) { case 0: /* Remove a random corner piece. */ { int dx, dy; dx = random_upto(rs, 2) * 2 - 1; /* +1 or -1 */ dy = random_upto(rs, 2) * 2 - 1; /* +1 or -1 */ if (random_upto(rs, 2)) dy *= 3; else dx *= 3; grid[(3+dy)*w+(3+dx)] = GRID_HOLE; } break; case 1: /* Remove a random piece two from the centre. */ { int dx, dy; dx = 2 * (random_upto(rs, 2) * 2 - 1); if (random_upto(rs, 2)) dy = 0; else dy = dx, dx = 0; grid[(3+dy)*w+(3+dx)] = GRID_HOLE; } break; default /* case 2 */: /* Remove a random piece one from the centre. */ { int dx, dy; dx = random_upto(rs, 2) * 2 - 1; if (random_upto(rs, 2)) dy = 0; else dy = dx, dx = 0; grid[(3+dy)*w+(3+dx)] = GRID_HOLE; } break; } } } /* * Encode a game description which is simply a long list of P * for peg, H for hole or O for obstacle. */ ret = snewn(w*h+1, char); for (i = 0; i < w*h; i++) ret[i] = (grid[i] == GRID_PEG ? 'P' : grid[i] == GRID_HOLE ? 'H' : 'O'); ret[w*h] = '\0'; sfree(grid); return ret; } static const char *validate_desc(const game_params *params, const char *desc) { int len = params->w * params->h; if (len != strlen(desc)) return "Game description is wrong length"; if (len != strspn(desc, "PHO")) return "Invalid character in game description"; return NULL; } static game_state *new_game(midend *me, const game_params *params, const char *desc) { int w = params->w, h = params->h; game_state *state = snew(game_state); int i; state->w = w; state->h = h; state->completed = false; state->grid = snewn(w*h, unsigned char); for (i = 0; i < w*h; i++) state->grid[i] = (desc[i] == 'P' ? GRID_PEG : desc[i] == 'H' ? GRID_HOLE : GRID_OBST); return state; } static game_state *dup_game(const game_state *state) { int w = state->w, h = state->h; game_state *ret = snew(game_state); ret->w = state->w; ret->h = state->h; ret->completed = state->completed; ret->grid = snewn(w*h, unsigned char); memcpy(ret->grid, state->grid, w*h); return ret; } static void free_game(game_state *state) { sfree(state->grid); sfree(state); } static char *solve_game(const game_state *state, const game_state *currstate, const char *aux, const char **error) { return NULL; } static bool game_can_format_as_text_now(const game_params *params) { return true; } static char *game_text_format(const game_state *state) { int w = state->w, h = state->h; int x, y; char *ret; ret = snewn((w+1)*h + 1, char); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) ret[y*(w+1)+x] = (state->grid[y*w+x] == GRID_HOLE ? '-' : state->grid[y*w+x] == GRID_PEG ? '*' : ' '); ret[y*(w+1)+w] = '\n'; } ret[h*(w+1)] = '\0'; return ret; } struct game_ui { bool dragging; /* is a drag in progress? */ int sx, sy; /* grid coords of drag start cell */ int dx, dy; /* pixel coords of current drag posn */ int cur_x, cur_y; bool cur_visible, cur_jumping; }; static game_ui *new_ui(const game_state *state) { game_ui *ui = snew(game_ui); int x, y, v; ui->sx = ui->sy = ui->dx = ui->dy = 0; ui->dragging = false; ui->cur_visible = false; ui->cur_jumping = false; /* make sure we start the cursor somewhere on the grid. */ for (x = 0; x < state->w; x++) { for (y = 0; y < state->h; y++) { v = state->grid[y*state->w+x]; if (v == GRID_PEG || v == GRID_HOLE) { ui->cur_x = x; ui->cur_y = y; goto found; } } } assert(!"new_ui found nowhere for cursor"); found: return ui; } static void free_ui(game_ui *ui) { sfree(ui); } static char *encode_ui(const game_ui *ui) { return NULL; } static void decode_ui(game_ui *ui, const char *encoding) { } static void game_changed_state(game_ui *ui, const game_state *oldstate, const game_state *newstate) { /* * Cancel a drag, in case the source square has become * unoccupied. */ ui->dragging = false; /* * Also, cancel a keyboard-driven jump if one is half way to being * input. */ ui->cur_jumping = false; } #define PREFERRED_TILE_SIZE 33 #define TILESIZE (ds->tilesize) #define BORDER (TILESIZE / 2) #define HIGHLIGHT_WIDTH (TILESIZE / 16) #define COORD(x) ( BORDER + (x) * TILESIZE ) #define FROMCOORD(x) ( ((x) + TILESIZE - BORDER) / TILESIZE - 1 ) struct game_drawstate { int tilesize; blitter *drag_background; bool dragging; int dragx, dragy; int w, h; unsigned char *grid; bool started; int bgcolour; }; static char *interpret_move(const game_state *state, game_ui *ui, const game_drawstate *ds, int x, int y, int button) { int w = state->w, h = state->h; char buf[80]; if (button == LEFT_BUTTON) { int tx, ty; /* * Left button down: we attempt to start a drag. */ /* * There certainly shouldn't be a current drag in progress, * unless the midend failed to send us button events in * order; it has a responsibility to always get that right, * so we can legitimately punish it by failing an * assertion. */ assert(!ui->dragging); tx = FROMCOORD(x); ty = FROMCOORD(y); if (tx >= 0 && tx < w && ty >= 0 && ty < h && state->grid[ty*w+tx] == GRID_PEG) { ui->dragging = true; ui->sx = tx; ui->sy = ty; ui->dx = x; ui->dy = y; ui->cur_visible = false; ui->cur_jumping = false; return UI_UPDATE; } } else if (button == LEFT_DRAG && ui->dragging) { /* * Mouse moved; just move the peg being dragged. */ ui->dx = x; ui->dy = y; return UI_UPDATE; } else if (button == LEFT_RELEASE && ui->dragging) { int tx, ty, dx, dy; /* * Button released. Identify the target square of the drag, * see if it represents a valid move, and if so make it. */ ui->dragging = false; /* cancel the drag no matter what */ tx = FROMCOORD(x); ty = FROMCOORD(y); if (tx < 0 || tx >= w || ty < 0 || ty >= h) return UI_UPDATE; /* target out of range */ dx = tx - ui->sx; dy = ty - ui->sy; if (max(abs(dx),abs(dy)) != 2 || min(abs(dx),abs(dy)) != 0) return UI_UPDATE; /* move length was wrong */ dx /= 2; dy /= 2; if (state->grid[ty*w+tx] != GRID_HOLE || state->grid[(ty-dy)*w+(tx-dx)] != GRID_PEG || state->grid[ui->sy*w+ui->sx] != GRID_PEG) return UI_UPDATE; /* grid contents were invalid */ /* * We have a valid move. Encode it simply as source and * destination coordinate pairs. */ sprintf(buf, "%d,%d-%d,%d", ui->sx, ui->sy, tx, ty); return dupstr(buf); } else if (IS_CURSOR_MOVE(button)) { if (!ui->cur_jumping) { /* Not jumping; move cursor as usual, making sure we don't * leave the gameboard (which may be an irregular shape) */ int cx = ui->cur_x, cy = ui->cur_y; move_cursor(button, &cx, &cy, w, h, false); ui->cur_visible = true; if (state->grid[cy*w+cx] == GRID_HOLE || state->grid[cy*w+cx] == GRID_PEG) { ui->cur_x = cx; ui->cur_y = cy; } return UI_UPDATE; } else { int dx, dy, mx, my, jx, jy; /* We're jumping; if the requested direction has a hole, and * there's a peg in the way, */ assert(state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG); dx = (button == CURSOR_RIGHT) ? 1 : (button == CURSOR_LEFT) ? -1 : 0; dy = (button == CURSOR_DOWN) ? 1 : (button == CURSOR_UP) ? -1 : 0; mx = ui->cur_x+dx; my = ui->cur_y+dy; jx = mx+dx; jy = my+dy; ui->cur_jumping = false; /* reset, whatever. */ if (jx >= 0 && jy >= 0 && jx < w && jy < h && state->grid[my*w+mx] == GRID_PEG && state->grid[jy*w+jx] == GRID_HOLE) { /* Move cursor to the jumped-to location (this felt more * natural while playtesting) */ sprintf(buf, "%d,%d-%d,%d", ui->cur_x, ui->cur_y, jx, jy); ui->cur_x = jx; ui->cur_y = jy; return dupstr(buf); } return UI_UPDATE; } } else if (IS_CURSOR_SELECT(button)) { if (!ui->cur_visible) { ui->cur_visible = true; return UI_UPDATE; } if (ui->cur_jumping) { ui->cur_jumping = false; return UI_UPDATE; } if (state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG) { /* cursor is on peg: next arrow-move wil jump. */ ui->cur_jumping = true; return UI_UPDATE; } return NULL; } return NULL; } static game_state *execute_move(const game_state *state, const char *move) { int w = state->w, h = state->h; int sx, sy, tx, ty; game_state *ret; if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty) == 4) { int mx, my, dx, dy; if (sx < 0 || sx >= w || sy < 0 || sy >= h) return NULL; /* source out of range */ if (tx < 0 || tx >= w || ty < 0 || ty >= h) return NULL; /* target out of range */ dx = tx - sx; dy = ty - sy; if (max(abs(dx),abs(dy)) != 2 || min(abs(dx),abs(dy)) != 0) return NULL; /* move length was wrong */ mx = sx + dx/2; my = sy + dy/2; if (state->grid[sy*w+sx] != GRID_PEG || state->grid[my*w+mx] != GRID_PEG || state->grid[ty*w+tx] != GRID_HOLE) return NULL; /* grid contents were invalid */ ret = dup_game(state); ret->grid[sy*w+sx] = GRID_HOLE; ret->grid[my*w+mx] = GRID_HOLE; ret->grid[ty*w+tx] = GRID_PEG; /* * Opinion varies on whether getting to a single peg counts as * completing the game, or whether that peg has to be at a * specific location (central in the classic cross game, for * instance). For now we take the former, rather lax position. */ if (!ret->completed) { int count = 0, i; for (i = 0; i < w*h; i++) if (ret->grid[i] == GRID_PEG) count++; if (count == 1) ret->completed = true; } return ret; } return NULL; } /* ---------------------------------------------------------------------- * Drawing routines. */ static void game_compute_size(const game_params *params, int tilesize, int *x, int *y) { /* Ick: fake up `ds->tilesize' for macro expansion purposes */ struct { int tilesize; } ads, *ds = &ads; ads.tilesize = tilesize; *x = TILESIZE * params->w + 2 * BORDER; *y = TILESIZE * params->h + 2 * BORDER; } static void game_set_size(drawing *dr, game_drawstate *ds, const game_params *params, int tilesize) { ds->tilesize = tilesize; assert(TILESIZE > 0); assert(!ds->drag_background); /* set_size is never called twice */ ds->drag_background = blitter_new(dr, TILESIZE, TILESIZE); } static float *game_colours(frontend *fe, int *ncolours) { float *ret = snewn(3 * NCOLOURS, float); game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); ret[COL_PEG * 3 + 0] = 0.0F; ret[COL_PEG * 3 + 1] = 0.0F; ret[COL_PEG * 3 + 2] = 1.0F; ret[COL_CURSOR * 3 + 0] = 0.5F; ret[COL_CURSOR * 3 + 1] = 0.5F; ret[COL_CURSOR * 3 + 2] = 1.0F; *ncolours = NCOLOURS; return ret; } static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state) { int w = state->w, h = state->h; struct game_drawstate *ds = snew(struct game_drawstate); ds->tilesize = 0; /* not decided yet */ /* We can't allocate the blitter rectangle for the drag background * until we know what size to make it. */ ds->drag_background = NULL; ds->dragging = false; ds->w = w; ds->h = h; ds->grid = snewn(w*h, unsigned char); memset(ds->grid, 255, w*h); ds->started = false; ds->bgcolour = -1; return ds; } static void game_free_drawstate(drawing *dr, game_drawstate *ds) { if (ds->drag_background) blitter_free(dr, ds->drag_background); sfree(ds->grid); sfree(ds); } static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, int v, int bgcolour) { bool cursor = false, jumping = false; int bg; if (bgcolour >= 0) { draw_rect(dr, x, y, TILESIZE, TILESIZE, bgcolour); } if (v >= GRID_JUMPING) { jumping = true; v -= GRID_JUMPING; } if (v >= GRID_CURSOR) { cursor = true; v -= GRID_CURSOR; } if (v == GRID_HOLE) { bg = cursor ? COL_HIGHLIGHT : COL_LOWLIGHT; assert(!jumping); /* can't jump from a hole! */ draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4, bg, bg); } else if (v == GRID_PEG) { bg = (cursor || jumping) ? COL_CURSOR : COL_PEG; draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3, bg, bg); bg = (!cursor || jumping) ? COL_PEG : COL_CURSOR; draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4, bg, bg); } draw_update(dr, x, y, TILESIZE, TILESIZE); } static void game_redraw(drawing *dr, game_drawstate *ds, const game_state *oldstate, const game_state *state, int dir, const game_ui *ui, float animtime, float flashtime) { int w = state->w, h = state->h; int x, y; int bgcolour; if (flashtime > 0) { int frame = (int)(flashtime / FLASH_FRAME); bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); } else bgcolour = COL_BACKGROUND; /* * Erase the sprite currently being dragged, if any. */ if (ds->dragging) { assert(ds->drag_background); blitter_load(dr, ds->drag_background, ds->dragx, ds->dragy); draw_update(dr, ds->dragx, ds->dragy, TILESIZE, TILESIZE); ds->dragging = false; } if (!ds->started) { draw_rect(dr, 0, 0, TILESIZE * state->w + 2 * BORDER, TILESIZE * state->h + 2 * BORDER, COL_BACKGROUND); /* * Draw relief marks around all the squares that aren't * GRID_OBST. */ for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (state->grid[y*w+x] != GRID_OBST) { /* * First pass: draw the full relief square. */ int coords[6]; coords[0] = COORD(x+1) + HIGHLIGHT_WIDTH - 1; coords[1] = COORD(y) - HIGHLIGHT_WIDTH; coords[2] = COORD(x) - HIGHLIGHT_WIDTH; coords[3] = COORD(y+1) + HIGHLIGHT_WIDTH - 1; coords[4] = COORD(x) - HIGHLIGHT_WIDTH; coords[5] = COORD(y) - HIGHLIGHT_WIDTH; draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); coords[4] = COORD(x+1) + HIGHLIGHT_WIDTH - 1; coords[5] = COORD(y+1) + HIGHLIGHT_WIDTH - 1; draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); } for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (state->grid[y*w+x] != GRID_OBST) { /* * Second pass: draw everything but the two * diagonal corners. */ draw_rect(dr, COORD(x) - HIGHLIGHT_WIDTH, COORD(y) - HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, COL_HIGHLIGHT); draw_rect(dr, COORD(x), COORD(y), TILESIZE + HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, COL_LOWLIGHT); } for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (state->grid[y*w+x] != GRID_OBST) { /* * Third pass: draw a trapezium on each edge. */ int coords[8]; int dx, dy, s, sn, c; for (dx = 0; dx < 2; dx++) { dy = 1 - dx; for (s = 0; s < 2; s++) { sn = 2*s - 1; c = s ? COL_LOWLIGHT : COL_HIGHLIGHT; coords[0] = COORD(x) + (s*dx)*(TILESIZE-1); coords[1] = COORD(y) + (s*dy)*(TILESIZE-1); coords[2] = COORD(x) + (s*dx+dy)*(TILESIZE-1); coords[3] = COORD(y) + (s*dy+dx)*(TILESIZE-1); coords[4] = coords[2] - HIGHLIGHT_WIDTH * (dy-sn*dx); coords[5] = coords[3] - HIGHLIGHT_WIDTH * (dx-sn*dy); coords[6] = coords[0] + HIGHLIGHT_WIDTH * (dy+sn*dx); coords[7] = coords[1] + HIGHLIGHT_WIDTH * (dx+sn*dy); draw_polygon(dr, coords, 4, c, c); } } } for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (state->grid[y*w+x] != GRID_OBST) { /* * Second pass: draw everything but the two * diagonal corners. */ draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE, COL_BACKGROUND); } ds->started = true; draw_update(dr, 0, 0, TILESIZE * state->w + 2 * BORDER, TILESIZE * state->h + 2 * BORDER); } /* * Loop over the grid redrawing anything that looks as if it * needs it. */ for (y = 0; y < h; y++) for (x = 0; x < w; x++) { int v; v = state->grid[y*w+x]; /* * Blank the source of a drag so it looks as if the * user picked the peg up physically. */ if (ui->dragging && ui->sx == x && ui->sy == y && v == GRID_PEG) v = GRID_HOLE; if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y) v += ui->cur_jumping ? GRID_JUMPING : GRID_CURSOR; if (v != GRID_OBST && (bgcolour != ds->bgcolour || /* always redraw when flashing */ v != ds->grid[y*w+x])) {