-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathruledisplay.ts
349 lines (317 loc) · 15.2 KB
/
ruledisplay.ts
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
module tileworld.ruleediting {
// IMPORTANT: the order of direction-oriented images matches directions from rule.ts
export const moveImages = [leftArrow, upArrow, rightArrow, downArrow, stopSign, uTurn];
export const movedImages = [leftArrowOutline, upArrowOutline, rightArrowOutline, downArrowOutline, restingOutline, allFourOutline, anyOutline];
export const moveText = ["left", "up", "right", "down", "stop", "u-turn"];
export const buttonImages = [leftButton, upButton, rightButton, downButton, AButton];
// this mapping has a level of indirection, allowing reorganization of the UI
export const attrValues = [AttrType.Include, AttrType.Include2, AttrType.Exclude, AttrType.OK ];
export const attrImages = [include, include2, exclude, ok ];
// arguments to instructions here
export const gameImages = [ trophyUp, trophyDown, scoreUp10 ];
export const gameText = [ "win", "lose", "score+10" ];
export const editorRow = 2;
export class RuleDisplay extends RuleVisualsBase {
protected all: AllExport;
private otherCursor: Sprite; // show correspondence between left and right
constructor(p: Project, protected rule: RuleView) {
super(p);
this.all = new AllExport(p);
// linked cursor
this.otherCursor = sprites.create(cursorOut)
this.otherCursor.setFlag(SpriteFlag.Invisible, true)
this.otherCursor.x = 88
this.otherCursor.y = yoff+40
this.otherCursor.z = 50;
}
protected getDir(): number {
return this.rule.getDirFromRule();
}
protected getType(): RuleType {
return this.rule.getRuleType();
}
protected getKind(): number {
const kinds = this.rule.getSpriteKinds();
if (kinds.length > 0)
return kinds[0];
return -1;
}
protected centerImage(): Image {
return ok;
}
protected getDirectionImage(): Image {
const dir = this.rule.getDirFromRule();
return this.getType() == RuleType.ButtonPress ? buttonImages[dir] : moveImages[dir];
}
private otherCursorMove(): void {
if (this.col() >= 5 && this.row() >= editorRow) {
// map from Do section to When section
const row = this.row() - editorRow;
this.otherCursor.setFlag(SpriteFlag.Invisible, false);
// compute mapping from right to left hand side
this.otherCursor.x = this.rowToColCoord(row) * 16 + 8;
this.otherCursor.y = this.rowToRowCoord(row) * 16 + 8 + yoff + (editorRow *16);
} else {
// TOD: map from When section to Do section
this.otherCursor.setFlag(SpriteFlag.Invisible, true);
}
}
protected cursorMove(dir: MoveDirection, pressed: boolean): void {
this.otherCursorMove();
}
protected collideCol: number;
protected collideRow: number;
protected showCollision(col: number, row: number, dir: MoveDirection, arrowImg: Image, rt: RuleType): void {
this.collideCol = col;
this.collideRow = row - editorRow;
this.drawImage(col, row, collisionSprite);
const x = (dir == MoveDirection.Left) ? 7 : (dir == MoveDirection.Right) ? -7 : 0;
const y = (dir == MoveDirection.Up) ? 7 : (dir == MoveDirection.Down) ? -7 : 0;
this.drawImageAbs((col << 4) + x, (row << 4) + yoff + y, arrowImg);
}
protected showRuleType(rt: RuleType, rd: MoveRest, x: number, y: number, center = true): void {
if (center) this.drawImage(x, y, this.centerImage());
if (rt == RuleType.ContextChange) {
this.drawImage(x, y, movedImages[rd])
} else if (rt == RuleType.Collision) {
const ax = rd == MoveDirection.Left ? 1 : (rd == MoveDirection.Right ? -1 : 0)
const ay = rd == MoveDirection.Down ? -1 : (rd == MoveDirection.Up ? 1 : 0)
this.showCollision(x - ax, y - ay, rd, moveImages[rd], rt);
} else if (rt == RuleType.NegationCheck) {
this.drawImage(x, y, negate);
}
}
protected update(): void {
this.collideCol = this.collideRow = -1;
screen.fill(0);
screen.print("When", 0, (editorRow << 4) + 8);
if (this.p.debug)
screen.print(this.rule.getRuleId().toString(), 30, 0);
screen.print("Do", 70, (editorRow << 4) + 8);
// sets collideCol and collideRow
this.showRuleType(this.rule.getRuleType(), this.rule.getDirFromRule(), 2, 2 + editorRow);
this.makeContext();
if (this.getType() != RuleType.NegationCheck)
this.showRuleType(this.rule.getRuleType(), this.rule.getDirFromRule(), 2, 2 + editorRow);
this.showCommands();
if (this.getType() == RuleType.ButtonPress) {
const image = this.getDirectionImage();
if (image)
this.drawImage(0, 3, image);
} else if (this.getType() == RuleType.NegationCheck) {
this.drawImage(0, 3, negate);
}
}
private makeContext(): void {
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (this.active(i, j)) {
this.drawImage(i, j + editorRow, emptyTile);
this.showAttributes(i, j);
}
}
}
}
protected active(col: number, row: number): boolean {
if (this.collideCol != -1) {
return col == 2 && row == 2 || col == this.collideCol && row == this.collideRow;
}
return true;
}
// map from row 0-4 to (col,row) in diamond
protected rowToColCoord(lr: number): number { return lr % 2 == 0 ? 2 : lr; }
protected rowToRowCoord(lr: number): number { return lr == 0 ? 1 : (lr == 4 ? 3 : 2); }
// compute number of commands in each row, for editing
protected commandLengths: number[];
private showCommands(): void {
this.commandLengths = [];
for (let lr = 0; lr < 5; lr++) {
const col = this.rowToColCoord(lr);
const row = this.rowToRowCoord(lr);
const len = this.active(col, row) ? this.showCommandsAt(lr, col, row) : -1;
this.commandLengths.push(len);
}
}
protected instToImage(inst: number, arg: number): Image {
if (inst == 0xff || arg == 0xff)
return emptyTile;
switch (inst) {
case CommandType.Move: return moveImages[arg];
case CommandType.Paint: {
const ret = this.p.backgroundImages()[arg].clone();
ret.drawTransparentImage(smallPaint, 0, 0);
return ret;
}
case CommandType.Sprite: return eat;
case CommandType.Game: return gameImages[arg];
case CommandType.Spawn:
case CommandType.BlockSpriteRules:
{
const ret = this.p.spriteImages()[arg].clone();
ret.drawTransparentImage(inst == CommandType.Spawn ? spawn : exclude, 0, 0);
return ret;
}
case CommandType.Portal: {
const ret = this.p.backgroundImages()[arg].clone();
ret.drawTransparentImage(portal, 0, 0);
return ret;
}
}
return emptyTile;
}
protected tokens: number[];
protected showCommandsAt(crow: number, wcol: number, wrow: number, draw = true): number {
// TODO: need to special case on rule type
// TODO: - collision (no direction expression on central sprite)
// TODO: - negation (no witness)
if (draw) {
// draw the sprite witness, if any
const kind = this.rule.findWitnessColRow(wcol, wrow);
const img = kind == -1 ? genericSprite : this.getWhenDoImage(wcol, wrow);
this.drawImage(5, crow + editorRow, img);
// overlay the direction
if (kind != -1 && (this.getType() != RuleType.Collision || wcol != 2 || wrow != 2)) {
const whendo = this.rule.getWhenDo(wcol, wrow);
this.drawImage(5, crow + editorRow, movedImages[this.rule.getWitnessDirection(whendo)])
}
if (this.p.help) {
// print the rows numbers in the Do section
screen.print((crow +1).toString(), (5 << 4) + 10, ((editorRow + crow) << 4)+13);
// where they lie in the When section
screen.print((crow + 1).toString(), (wcol << 4) + 10, ((editorRow + wrow) << 4) + 13);
}
}
// show the existing commands
const whendo = this.rule.getWhenDo(wcol, wrow);
let col = 6;
const tokens = this.startTokens(wcol, wrow);
if (!draw) {
this.tokens = tokens;
}
let cid = 0
for (; whendo != -1 && cid < this.rule.getCmdsLen(whendo); cid++ , col++) {
this.showCommand(col, crow, whendo, cid, tokens, draw);
}
if (whendo == -1 || cid < MaxCommands && tokens.length > 0) {
this.showCommand(col, crow, whendo, cid, tokens, draw);
return cid + 1;
}
return cid;
}
private showCommand(col: number, row: number,
whendo: number, cid: number, tokens: number[],
draw: boolean): number {
if (whendo == -1) {
if (draw) this.drawImage(col, row + editorRow, emptyTile);
} else {
const inst = this.rule.getCmdInst(whendo, cid);
const arg = this.rule.getCmdArg(whendo, cid);
if (draw) this.drawImage(col, row + editorRow, this.instToImage(inst, arg));
this.updateTokens(tokens, inst);
col++;
}
return col;
}
// what instructions are possible, given rule type and witness
// this defines the menu to present at the top-level
private startTokens(col: number, row: number): number[] {
let tokens: number[] = [];
if (this.rule.findWitnessColRow(col, row) != -1) {
tokens = [CommandType.Move, CommandType.Sprite];
}
tokens = tokens.concat([
CommandType.Paint, CommandType.Spawn,
CommandType.BlockSpriteRules, CommandType.Portal,
CommandType.Game
]);
return tokens;
}
private updateTokens(tokens: number[], inst: number): void {
if (inst == 0xff)
return;
// at-most-once: paint, spawn, destroy
tokens.removeElement(inst);
// always remove move and destroy
tokens.removeElement(CommandType.Move);
tokens.removeElement(CommandType.Sprite);
if (inst == CommandType.Spawn) {
tokens.insertAt(0, CommandType.Move);
}
}
protected getWhenDoImage(col: number, row: number): Image {
const whenDo = this.rule.getWhenDo(col, row);
if (whenDo == -1)
return ok;
// look up includes and excludes
const include = this.attrIndex(whenDo, AttrType.Include);
const include2 = include == -1 ? -1 : this.attrIndex(whenDo, AttrType.Include, include + 1);
const exclude = this.attrIndex(whenDo, AttrType.Exclude);
const exclude2 = exclude == -1 ? -1 : this.attrIndex(whenDo, AttrType.Exclude, exclude + 1);
// favor includes over excludes
const index = include == -1 ? exclude : include;
// do split images when there are multiple includes/excludes
if (include != -1 && include2 != -1)
return utilities.splitImage(this.all.getImage(include), this.all.getImage(include2));
else if (include == -1 && exclude != -1 && exclude2 != -1)
return utilities.splitImage(this.all.getImage(exclude), this.all.getImage(exclude2));
else if (index != -1)
return this.all.getImage(index);
else
return ok;
}
protected showAttributes(col: number, row: number, show = true): void {
const whenDo = this.rule.getWhenDo(col, row);
if (whenDo >= 0) {
this.drawImage(col, row + editorRow, this.getWhenDoImage(col, row));
// show attributes
const begin = 0;
const end = this.p.allCnt() - 1;
const project = this.projectAttrs(whenDo, begin, end);
project.forEach(a => {
const i = attrValues.indexOf(a);
screen.drawTransparentImage(attrImages[i], (col << 4) + 8, ((row + editorRow) << 4) + 8 + yoff);
});
// show direction
if (this.getType() != RuleType.Collision && this.rule.findWitnessColRow(col, row) != -1) {
this.drawImage(col, row + editorRow, movedImages[this.rule.getWitnessDirection(whenDo)])
}
// ginve a peek into attributions under the main menu
if (show && this.col() == col && this.row() - editorRow == row) {
let x = 0;
screen.fillRect(0, 16 + yoff, 160, 16, 0);
this.all.getImages().forEach((image, i) => {
const a = this.all.getSetAttr(this.rule, whenDo, i);
if (a != AttrType.OK) {
this.drawImage(x, 1, image);
this.drawImage(x, 1, attrImages[attrValues.indexOf(a)]);
x++;
}
});
}
}
}
private projectAttrs(whendo: number, begin: number, end: number): number[] {
if (this.rule.whendoTrue(whendo))
return [];
const res: number[] = [];
for (let i = begin; i <= end; i++) {
const a = this.all.getSetAttr(this.rule, whendo, i);
if (a != AttrType.OK && res.indexOf(a) == -1) res.push(a);
}
if (res.length > 0) {
if (res.length == 1 && res.indexOf(AttrType.Exclude) != -1)
return [AttrType.Exclude];
else
return [];
}
return res;
}
private attrIndex(whendo: number, a: AttrType, begin = 0) {
for (let i = begin; i < this.p.allCnt(); i++) {
if (this.all.getSetAttr(this.rule, whendo, i) == a)
return i;
}
return -1;
}
}
}