-
Notifications
You must be signed in to change notification settings - Fork 5
/
mp_toys.mpsl
375 lines (297 loc) · 8.96 KB
/
mp_toys.mpsl
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
/*
Minimum Profit 5.x
A Programmer's Text Editor
Useless things.
Copyright (C) 1991-2012 Angel Ortega <angel@triptico.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
http://triptico.com
*/
/** editor actions **/
mp.actions['pipes'] = sub (d) {
mp.add(new(mp_pipes))->init();
};
/** action descriptions **/
mp.actdesc['pipes'] = LL("Play \"Pipes\"");
/** code **/
sub mp.maze(doc, keycode)
{
local tx = mp.window.tx;
local ty = mp.window.ty;
if (doc == NULL)
doc = mp.open('<maze>');
local maze = doc.maze;
if (maze == NULL || maze.tx != tx || maze.ty != ty) {
/* new or changed: rebuild everything */
maze = {};
maze.tx = tx;
maze.ty = ty;
doc.maze = maze;
/* ensure odd size */
if (!(tx & 1))
tx--;
if (!(ty & 1))
ty--;
/* init */
maze.map = map(
[1 .. ty],
sub {
map(
[1 .. tx],
sub { '#'; }
);
}
);
/* build */
local x = 1;
local y = 1;
local stack = [];
while (1) {
local d = [];
/* clear */
maze.map[y][x] = ' ';
foreach (t, [[0, -1], [1, 0], [0, 1], [-1, 0]]) {
/* can space be opened? */
local ny = y + t[0] * 2;
local nx = x + t[1] * 2;
if (nx > 0 && ny > 0 && maze.map[ny][nx] eq '#')
push(d, t);
}
if (size(d)) {
/* more than one way? stack this position */
if (size(d) > 1)
push(stack, [y, x]);
/* pick one direction at random and move there */
local m = d[random(size(d))];
y += m[0];
x += m[1];
maze.map[y][x] = ' ';
y += m[0];
x += m[1];
}
else {
/* no way from here: pop previous position */
if ((d = pop(stack)) == NULL)
break;
y = d[0];
x = d[1];
}
}
maze.x = 1 + random(tx / 2) * 2;
maze.y = 1 + random(ty / 2) * 2;
maze.map[maze.y][maze.x] = '@';
x = 1 + random(tx / 2) * 2;
y = 1 + random(ty / 2) * 2;
maze.map[y][x] = 'X';
doc.keypress = sub(d, k) { mp.maze(d, k); };
doc.render = sub(d) { map(d.maze.map, sub(e) { [8, join(e)];}); };
}
local x = maze.x;
local y = maze.y;
maze.map[y][x] = ' ';
if (keycode eq 'cursor-up')
y--;
if (keycode eq 'cursor-down')
y++;
if (keycode eq 'cursor-left')
x--;
if (keycode eq 'cursor-right')
x++;
if (maze.map[y][x] eq 'X') {
mp.alert(L("You won!"));
doc.maze = NULL;
return mp.maze(doc);
}
if (maze.map[y][x] eq ' ') {
maze.x = x;
maze.y = y;
}
maze.map[maze.y][maze.x] = '@';
doc->undo();
return doc;
}
/** pipes **/
global mp_pipes = new(mp_base, {
name: '<pipes>',
x: 0,
y: 1,
tx: -1,
ty: -1,
wx: -1,
wy: -1,
sx: -1,
state: 'RST',
game_map: NULL,
attr: NULL,
get_status: sub (pipes) {
L("Pipes - carry water from top to bottom by turning the pipes");
},
init: sub (pipes) {
if (state eq 'RST' || tx != mp.window.tx || ty != mp.window.ty) {
tx = mp.window.tx;
ty = mp.window.ty;
/* convert to integers */
wx = sprintf('%d', ((tx - 2) / 3));
wy = sprintf('%d', (((ty - 1) / 2) - 1));
sx = random(wx);
game_map = map([1 .. wy - 1],
sub {map([1 .. wx], sub { random(size(pieces) - 3); });}
);
/* add first line */
ins(game_map, map([1 .. wx], sub { 6; }), 0);
game_map[0][sx] = 7;
/* add last line */
push(game_map, map([1 .. wx], sub { 6; }));
game_map[wy][5] = 8;
game_map[wy][wx - 5] = 8;
state = 'PLAY';
pipes->verify();
}
return pipes;
},
keypress: sub (pipes, keycode) {
local toggle = 0;
pipes->init();
if (keycode eq 'cursor-up' && y > 1)
y--;
if (keycode eq 'cursor-down'&& y < wy - 1)
y++;
if (keycode eq 'cursor-left' && x > 0)
x--;
if (keycode eq 'cursor-right' && x < wx - 1)
x++;
if (keycode eq 'enter')
toggle = 1;
if (keycode eq 'mouse-left-button') {
local ty = sprintf("%d", mp.mouse_y / 2);
if (ty >= 1 && ty < wy) {
local tx = sprintf("%d", mp.mouse_x / 3);
if (tx >= 0 && tx < wx) {
x = tx; y = ty;
toggle = 1;
}
}
}
if (toggle) {
local n = game_map[y][x];
game_map[y][x] = pieces[n].next;
pipes->verify();
}
return pipes;
},
verify: sub (pipes) {
/* reset attributes */
attr = map([0 .. wy], sub { [];});
/* the water input is blue */
attr[0][sx] = mp.colors.quotes.attr;
/* calculate the path */
local px = sx;
local py = 0;
local c = 'D'; /* downwards */
local p;
/* while there is a valid path */
while ((p = pieces[game_map[py][px]]) && (c = p.out[c]) && dir[c]) {
/* mark as valid path */
attr[py][px] = mp.colors.quotes.attr;
/* move in new direction */
px += dir[c][0];
py += dir[c][1];
}
if (c eq 'WIN') {
attr[py][px] = mp.colors.quotes.attr;
state = 'WON';
}
return pipes;
},
render: sub (pipes) {
local out = [];
pipes->init();
foreach (n, [0 .. wy]) {
local l0 = [];
local l1 = [];
foreach (m, [0 .. (wx - 1)]) {
local p = game_map[n][m];
local s = pieces[p].shape;
local c;
if (n == y && m == x)
c = mp.colors.cursor.attr;
else
c = attr[n][m] || mp.colors.normal.attr;
push(l0, c, s[0]);
push(l1, c, s[1]);
}
push(out, l0, l1);
}
if (state eq 'WON') {
out[0] = [
mp.colors.cursor.attr,
L("You won! Press any key.")
];
state = 'RST';
}
return out;
},
dir: {
U: [ 0, -1],
R: [ 1, 0],
D: [ 0, 1],
L: [-1, 0]
},
pieces: [
{
shape: ["\x{2502} \x{2514}", "\x{2514}\x{2500}\x{2500}"],
out: { D: 'R', L: 'U' },
next: 1
},
{
shape: ["\x{250c}\x{2500}\x{2500}", "\x{2502} \x{250c}"],
out: { U: 'R', L: 'D' },
next: 2
},
{
shape: ["\x{2500}\x{2500}\x{2510}", "\x{2510} \x{2502}"],
out: { U: 'L', R: 'D' },
next: 3
},
{
shape: ["\x{2518} \x{2502}", "\x{2500}\x{2500}\x{2518}"],
out: { R: 'U', D: 'L' },
next: 0
},
{
shape: ["\x{2502} \x{2502}", "\x{2502} \x{2502}"],
out: { U: 'U', D: 'D' },
next: 5
},
{
shape: ["\x{2500}\x{2500}\x{2500}", "\x{2500}\x{2500}\x{2500}"],
out: { R: 'R', L: 'L' },
next: 4
},
{ /* blank */
shape: [" ", " "],
out: {},
next: 6
},
{ /* entry pipe */
shape: ["\x{2502} \x{2502}", "\\ /"],
out: { D: 'D' },
next: 7
},
{ /* out pipe */
shape: ["\x{2502} \x{2502}", "/ \\"],
out: { D: 'WIN' },
next: 8
}
]
}
);