fix syntax

This commit is contained in:
2026-02-17 09:15:15 -06:00
parent f310c18b84
commit 4e1b63fd0e
52 changed files with 2169 additions and 1754 deletions

View File

@@ -13,7 +13,8 @@ var bunnyTex = graphics.texture("bunny")
var bunnies = []
// Start with some initial bunnies:
for (var i = 0; i < 100; i++) {
var i = 0;
for (i = 0; i < 100; i++) {
push(bunnies, {
x: random.random() * config.width,
y: random.random() * config.height,
@@ -25,8 +26,10 @@ for (var i = 0; i < 100; i++) {
this.update = function(dt) {
// If left mouse is down, spawn some more bunnies:
var mouse = input.mousestate()
var i = 0;
var b = null;
if (mouse.left)
for (var i = 0; i < 50; i++) {
for (i = 0; i < 50; i++) {
push(bunnies, {
x: mouse.x,
y: mouse.y,
@@ -36,8 +39,8 @@ this.update = function(dt) {
}
// Update bunny positions and bounce them inside the screen:
for (var i = 0; i < length(bunnies); i++) {
var b = bunnies[i]
for (i = 0; i < length(bunnies); i++) {
b = bunnies[i]
b.x += b.vx * dt
b.y += b.vy * dt

View File

@@ -28,26 +28,21 @@ var isMyTurn = false;
function updateTitle() {
var title = "Misty Chess - ";
switch(gameState) {
case 'waiting':
title += "Press S to start server or J to join";
break;
case 'searching':
title += "Searching for server...";
break;
case 'server_waiting':
title += "Waiting for player to join...";
break;
case 'connected':
if (myColor) {
title += (mover.turn == myColor ? "Your turn (" + myColor + ")" : "Opponent's turn (" + mover.turn + ")");
} else {
title += mover.turn + " turn";
}
break;
if (gameState == 'waiting') {
title += "Press S to start server or J to join";
} else if (gameState == 'searching') {
title += "Searching for server...";
} else if (gameState == 'server_waiting') {
title += "Waiting for player to join...";
} else if (gameState == 'connected') {
if (myColor) {
title += (mover.turn == myColor ? "Your turn (" + myColor + ")" : "Opponent's turn (" + mover.turn + ")");
} else {
title += mover.turn + " turn";
}
}
log.console(title)
}
@@ -179,13 +174,19 @@ var opponentMouseColor = [1.0, 0.0, 0.0, 1.0]; // Red for opponent mouse
/* ── draw one 8×8 chess board ──────────────────────────────────── */
function drawBoard() {
for (var y = 0; y < 8; ++y)
for (var x = 0; x < 8; ++x) {
var isMyHover = hoverPos && hoverPos[0] == x && hoverPos[1] == y;
var isOpponentHover = opponentMousePos && opponentMousePos[0] == x && opponentMousePos[1] == y;
var isValidMove = selectPos && holdingPiece && isValidMoveForTurn(selectPos, [x, y]);
var color = ((x+y)&1) ? dark : light;
var y = 0;
var x = 0;
var isMyHover = null;
var isOpponentHover = null;
var isValidMove = null;
var color = null;
for (y = 0; y < 8; ++y)
for (x = 0; x < 8; ++x) {
isMyHover = hoverPos && hoverPos[0] == x && hoverPos[1] == y;
isOpponentHover = opponentMousePos && opponentMousePos[0] == x && opponentMousePos[1] == y;
isValidMove = selectPos && holdingPiece && isValidMoveForTurn(selectPos, [x, y]);
color = ((x+y)&1) ? dark : light;
if (isValidMove) {
color = allowedColor; // Gold for allowed moves
@@ -220,47 +221,51 @@ function isValidMoveForTurn(from, to) {
/* ── draw every live piece ─────────────────────────────────────── */
function drawPieces() {
grid.each(function (piece) {
if (piece.captured) return;
var piece = null;
var r = null;
var opponentPiece = null;
grid.each(function (p) {
if (p.captured) return;
// Skip drawing the piece being held (by me or opponent)
if (holdingPiece && selectPos &&
piece.coord[0] == selectPos[0] &&
piece.coord[1] == selectPos[1]) {
return;
}
// Skip drawing the piece being held by opponent
if (opponentHoldingPiece && opponentSelectPos &&
piece.coord[0] == opponentSelectPos[0] &&
piece.coord[1] == opponentSelectPos[1]) {
if (holdingPiece && selectPos &&
p.coord[0] == selectPos[0] &&
p.coord[1] == selectPos[1]) {
return;
}
var r = { x: piece.coord[0]*S, y: piece.coord[1]*S,
// Skip drawing the piece being held by opponent
if (opponentHoldingPiece && opponentSelectPos &&
p.coord[0] == opponentSelectPos[0] &&
p.coord[1] == opponentSelectPos[1]) {
return;
}
var pr = { x: p.coord[0]*S, y: p.coord[1]*S,
width:S, height:S };
draw2d.image(piece.sprite, r);
draw2d.image(p.sprite, pr);
});
// Draw the held piece at the mouse position if we're holding one
if (holdingPiece && selectPos && hoverPos) {
var piece = grid.at(selectPos)[0];
piece = grid.at(selectPos)[0];
if (piece) {
var r = { x: hoverPos[0]*S, y: hoverPos[1]*S,
r = { x: hoverPos[0]*S, y: hoverPos[1]*S,
width:S, height:S };
draw2d.image(piece.sprite, r);
}
}
// Draw opponent's held piece if they're dragging one
if (opponentHoldingPiece && opponentSelectPos && opponentMousePos) {
var opponentPiece = grid.at(opponentSelectPos)[0];
opponentPiece = grid.at(opponentSelectPos)[0];
if (opponentPiece) {
var r = { x: opponentMousePos[0]*S, y: opponentMousePos[1]*S,
r = { x: opponentMousePos[0]*S, y: opponentMousePos[1]*S,
width:S, height:S };
// Draw with slight transparency to show it's the opponent's piece
draw2d.image(opponentPiece.sprite, r);
}
@@ -325,13 +330,16 @@ function joinServer() {
}
$receiver(e => {
var fromCell = null;
var piece = null;
if (e.kind == 'update')
send(e, update(e.dt))
else if (e.kind == 'draw')
send(e, draw())
else if (e.type == 'game_start' || e.type == 'move' || e.type == 'greet')
log.console("Receiver got message:", e.type, e);
if (e.type == 'greet') {
log.console("Server received greet from client");
// Store the client's actor object for ongoing communication
@@ -339,7 +347,7 @@ $receiver(e => {
log.console("Stored client actor:", opponent);
gameState = 'connected';
updateTitle();
// Send game_start to the client
log.console("Sending game_start to client");
send(opponent, {
@@ -357,9 +365,9 @@ $receiver(e => {
} else if (e.type == 'move') {
log.console("Received move from opponent:", e.from, "to", e.to);
// Apply opponent's move
var fromCell = grid.at(e.from);
fromCell = grid.at(e.from);
if (length(fromCell)) {
var piece = fromCell[0];
piece = fromCell[0];
if (mover.tryMove(piece, e.to)) {
isMyTurn = true; // It's now our turn
updateTitle();

View File

@@ -2,11 +2,13 @@ function grid(w, h) {
var newgrid = meme(grid_prototype)
newgrid.width = w;
newgrid.height = h;
// create a height×width array of empty lists
// create a height*width array of empty lists
newgrid.cells = array(h);
for (var y = 0; y < h; y++) {
var y = 0;
var x = 0;
for (y = 0; y < h; y++) {
newgrid.cells[y] = array(w);
for (var x = 0; x < w; x++) {
for (x = 0; x < w; x++) {
newgrid.cells[y][x] = []; // each cell holds its own list
}
}
@@ -45,9 +47,12 @@ var grid_prototype = {
// call fn(entity, coord) for every entity in every cell
each(fn) {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
def list = this.cells[y][x]
var list = null;
var y = 0;
var x = 0;
for (y = 0; y < this.height; y++) {
for (x = 0; x < this.width; x++) {
list = this.cells[y][x]
arrfor(list, function(entity) {
fn(entity, entity.coord);
})
@@ -57,9 +62,11 @@ var grid_prototype = {
// printable representation
toString() {
var out = `grid [${this.width}×${this.height}]\n`;
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var out = `grid [${this.width}x${this.height}]\n`;
var y = 0;
var x = 0;
for (y = 0; y < this.height; y++) {
for (x = 0; x < this.width; x++) {
out += length(this.cells[y][x]);
}
if (y != this.height - 1) out += "\n";

View File

@@ -10,9 +10,9 @@ var MovementSystem_prototype = {
tryMove: function (piece, to) {
if (piece.colour != this.turn) return false;
// normalise to into our hybrid coord
var dest = [to.x ?? t[0],
to.y ?? to[1]];
// normalise 'to' into our hybrid coord
var dest = [!is_null(to.x) ? to.x : to[0],
!is_null(to.y) ? to.y : to[1]];
if (!this.grid.inBounds(dest)) return false;
if (!this.rules.canMove(piece, piece.coord, dest, this.grid)) return false;

View File

@@ -10,7 +10,7 @@ function Piece(kind, colour) {
}
function startingPosition(grid) {
var W = 'white', B = 'black', x;
var W = 'white', B = 'black', x = 0;
// pawns
for (x = 0; x < 8; x++) {

View File

@@ -1,30 +1,30 @@
/* helper robust coord access */
function cx(c) { return c.x ?? c[0] }
function cy(c) { return c.y ?? c[1] }
/* helper -- robust coord access */
function cx(c) { return !is_null(c.x) ? c.x : c[0] }
function cy(c) { return !is_null(c.y) ? c.y : c[1] }
/* simple move-shape checks */
var deltas = {
pawn: function (pc, dx, dy, grid, to) {
pawn: function (pc, dx, dy, ctx) {
var dir = (pc.colour == 'white') ? -1 : 1;
var base = (pc.colour == 'white') ? 6 : 1;
var one = (dy == dir && dx == 0 && length(grid.at(to)) == 0);
var one = (dy == dir && dx == 0 && length(ctx.grid.at(ctx.to)) == 0);
var two = (dy == 2 * dir && dx == 0 && cy(pc.coord) == base &&
length(grid.at({ x: cx(pc.coord), y: cy(pc.coord)+dir })) == 0 &&
length(grid.at(to)) == 0);
var cap = (dy == dir && Math.abs(dx) == 1 && length(grid.at(to)));
length(ctx.grid.at({ x: cx(pc.coord), y: cy(pc.coord)+dir })) == 0 &&
length(ctx.grid.at(ctx.to)) == 0);
var cap = (dy == dir && abs(dx) == 1 && length(ctx.grid.at(ctx.to)));
return one || two || cap;
},
rook : function (pc, dx, dy) { return (dx == 0 || dy == 0); },
bishop: function (pc, dx, dy) { return Math.abs(dx) == Math.abs(dy); },
queen : function (pc, dx, dy) { return (dx == 0 || dy == 0 || Math.abs(dx) == Math.abs(dy)); },
knight: function (pc, dx, dy) { return (Math.abs(dx) == 1 && Math.abs(dy) == 2) ||
(Math.abs(dx) == 2 && Math.abs(dy) == 1); },
king : function (pc, dx, dy) { return Math.max(Math.abs(dx), Math.abs(dy)) == 1; }
bishop: function (pc, dx, dy) { return abs(dx) == abs(dy); },
queen : function (pc, dx, dy) { return (dx == 0 || dy == 0 || abs(dx) == abs(dy)); },
knight: function (pc, dx, dy) { return (abs(dx) == 1 && abs(dy) == 2) ||
(abs(dx) == 2 && abs(dy) == 1); },
king : function (pc, dx, dy) { return max(abs(dx), abs(dy)) == 1; }
};
function clearLine(from, to, grid) {
var dx = Math.sign(cx(to) - cx(from));
var dy = Math.sign(cy(to) - cy(from));
var dx = sign(cx(to) - cx(from));
var dy = sign(cy(to) - cy(from));
var x = cx(from) + dx, y = cy(from) + dy;
while (x != cx(to) || y != cy(to)) {
if (length(grid.at({ x: x, y: y }))) return false;
@@ -37,7 +37,7 @@ function canMove(piece, from, to, grid) {
var dx = cx(to) - cx(from);
var dy = cy(to) - cy(from);
var f = deltas[piece.kind];
if (!f || !f(piece, dx, dy, grid, to)) return false;
if (!f || !f(piece, dx, dy, {grid: grid, to: to})) return false;
if (piece.kind == 'knight') return true;
return clearLine(from, to, grid);
}

View File

@@ -13,7 +13,7 @@ var cellSize = 20
var gridW = floor(config.width / cellSize)
var gridH = floor(config.height / cellSize)
var snake, direction, nextDirection, apple
var snake = null, direction = null, nextDirection = null, apple = null
var moveInterval = 0.1
var moveTimer = 0
var gameState = "playing"
@@ -36,7 +36,8 @@ function resetGame() {
function spawnApple() {
apple = {x:floor(random.random()*gridW), y:floor(random.random()*gridH)}
// Re-spawn if apple lands on snake
for (var i=0; i<length(snake); i++)
var i = 0;
for (i=0; i<length(snake); i++)
if (snake[i].x == apple.x && snake[i].y == apple.y) { spawnApple(); return }
}
@@ -63,7 +64,8 @@ this.update = function(dt) {
wrap(head)
// Check collision with body
for (var i=0; i<length(snake); i++) {
var i = 0;
for (i=0; i<length(snake); i++) {
if (snake[i].x == head.x && snake[i].y == head.y) {
gameState = "gameover"
return
@@ -81,10 +83,13 @@ this.update = function(dt) {
this.hud = function() {
// Optional clear screen
draw.rectangle({x:0, y:0, width:config.width, height:config.height}, [0,0,0,1])
// Draw snake
for (var i=0; i<length(snake); i++) {
var s = snake[i]
var i = 0;
var s = null;
var msg = null;
for (i=0; i<length(snake); i++) {
s = snake[i]
draw.rectangle({x:s.x*cellSize, y:s.y*cellSize, width:cellSize, height:cellSize}, color.green)
}
@@ -92,7 +97,7 @@ this.hud = function() {
draw.rectangle({x:apple.x*cellSize, y:apple.y*cellSize, width:cellSize, height:cellSize}, color.red)
if (gameState == "gameover") {
var msg = "GAME OVER! Press SPACE to restart."
msg = "GAME OVER! Press SPACE to restart."
draw.text(msg, {x:0, y:config.height*0.5-10, width:config.width, height:20}, null, 0, color.white)
}
}

View File

@@ -79,7 +79,7 @@ function unlock_achievement(achievement_name) {
function update_stat(stat_name, value, is_float) {
if (!steam_available || !stats_loaded) return false;
var success;
var success = null;
if (is_float) {
success = steam.stats.stats_set_float(stat_name, value);
} else {
@@ -145,7 +145,7 @@ function end_game(score) {
function save_to_cloud(save_data) {
if (!steam_available) return false;
var json_data = JSON.stringify(save_data);
var json_data = json.encode(save_data);
return steam.cloud.cloud_write("savegame.json", json_data);
}
@@ -153,9 +153,10 @@ function load_from_cloud() {
if (!steam_available) return null;
var data = steam.cloud.cloud_read("savegame.json");
var json_str = null;
if (data) {
var json_str = text(data)
return JSON.parse(json_str);
json_str = text(data)
return json.decode(json_str);
}
return null;
@@ -169,17 +170,5 @@ function cleanup_steam() {
}
}
// Export the API
module.exports = {
init: init_steam,
update: update_steam,
cleanup: cleanup_steam,
unlock_achievement: unlock_achievement,
update_stat: update_stat,
get_stat: get_stat,
start_game: start_game,
end_game: end_game,
save_to_cloud: save_to_cloud,
load_from_cloud: load_from_cloud,
is_available: function() { return steam_available; }
};
// Initialize on start
init_steam();

View File

@@ -56,9 +56,12 @@ var shapeKeys = array(SHAPES)
// Initialize board with empty (0)
function initBoard() {
board = []
for (var r=0; r<ROWS; r++) {
var row = []
for (var c=0; c<COLS; c++) push(row, 0)
var r = 0;
var row = null;
var c = 0;
for (r=0; r<ROWS; r++) {
row = []
for (c=0; c<COLS; c++) push(row, 0)
push(board, row)
}
}
@@ -66,7 +69,7 @@ initBoard()
function randomShape() {
var key = shapeKeys[floor(random.random()*length(shapeKeys))]
// Make a copy of the shapes blocks
// Make a copy of the shape's blocks
return {
type: key,
color: SHAPES[key].color,
@@ -84,9 +87,12 @@ function spawnPiece() {
}
function collides(px, py, blocks) {
for (var i=0; i<length(blocks); i++) {
var x = px + blocks[i][0]
var y = py + blocks[i][1]
var i = 0;
var x = 0;
var y = 0;
for (i=0; i<length(blocks); i++) {
x = px + blocks[i][0]
y = py + blocks[i][1]
if (x<0 || x>=COLS || y<0 || y>=ROWS) return true
if (y>=0 && board[y][x]) return true
}
@@ -95,9 +101,12 @@ function collides(px, py, blocks) {
// Lock piece into board
function lockPiece() {
for (var i=0; i<length(piece.blocks); i++) {
var x = pieceX + piece.blocks[i][0]
var y = pieceY + piece.blocks[i][1]
var i = 0;
var x = 0;
var y = 0;
for (i=0; i<length(piece.blocks); i++) {
x = pieceX + piece.blocks[i][0]
y = pieceY + piece.blocks[i][1]
if (y>=0) board[y][x] = piece.color
}
}
@@ -105,9 +114,12 @@ function lockPiece() {
// Rotate 90° clockwise
function rotate(blocks) {
// (x,y) => (y,-x)
for (var i=0; i<length(blocks); i++) {
var x = blocks[i][0]
var y = blocks[i][1]
var i = 0;
var x = 0;
var y = 0;
for (i=0; i<length(blocks); i++) {
x = blocks[i][0]
y = blocks[i][1]
blocks[i][0] = y
blocks[i][1] = -x
}
@@ -115,14 +127,17 @@ function rotate(blocks) {
function clearLines() {
var lines = 0
for (var r=ROWS-1; r>=0;) {
var r = ROWS-1;
var newRow = null;
var c = 0;
for (r=ROWS-1; r>=0;) {
if (every(board[r], cell => cell)) {
lines++
// remove row
board = array(array(board, 0, r), array(board, r+1))
// add empty row on top
var newRow = []
for (var c=0; c<COLS; c++) push(newRow, 0)
newRow = []
for (c=0; c<COLS; c++) push(newRow, 0)
board.unshift(newRow)
} else {
r--
@@ -195,10 +210,11 @@ this.update = function(dt) {
// ======= End Horizontal Movement Gate =======
// Rotate with W (once per press, no spinning)
var test = null;
if (input.keyboard.down('w')) {
if (!rotateHeld) {
rotateHeld = true
var test = array(piece.blocks, b => [b[0], b[1]])
test = array(piece.blocks, b => [b[0], b[1]])
rotate(test)
if (!collides(pieceX, pieceY, test)) piece.blocks = test
}
@@ -232,9 +248,19 @@ this.hud = function() {
draw.rectangle({x:0, y:0, width:config.width, height:config.height}, [0,0,0,1])
// Draw board
for (var r=0; r<ROWS; r++) {
for (var c=0; c<COLS; c++) {
var cell = board[r][c]
var r = 0;
var c = 0;
var cell = null;
var i = 0;
var x = 0;
var y = 0;
var nx = 0;
var ny = 0;
var dx = 0;
var dy = 0;
for (r=0; r<ROWS; r++) {
for (c=0; c<COLS; c++) {
cell = board[r][c]
if (!cell) continue
draw.rectangle({x:c*TILE, y:(ROWS-1-r)*TILE, width:TILE, height:TILE}, cell)
}
@@ -242,9 +268,9 @@ this.hud = function() {
// Draw falling piece
if (!gameOver && piece) {
for (var i=0; i<length(piece.blocks); i++) {
var x = pieceX + piece.blocks[i][0]
var y = pieceY + piece.blocks[i][1]
for (i=0; i<length(piece.blocks); i++) {
x = pieceX + piece.blocks[i][0]
y = pieceY + piece.blocks[i][1]
draw.rectangle({x:x*TILE, y:(ROWS-1-y)*TILE, width:TILE, height:TILE}, piece.color)
}
}
@@ -252,11 +278,11 @@ this.hud = function() {
// Next piece window
draw.text("Next", {x:70, y:5, width:50, height:10}, null, 0, color.white)
if (nextPiece) {
for (var i=0; i<length(nextPiece.blocks); i++) {
var nx = nextPiece.blocks[i][0]
var ny = nextPiece.blocks[i][1]
var dx = 12 + nx
var dy = 16 - ny
for (i=0; i<length(nextPiece.blocks); i++) {
nx = nextPiece.blocks[i][0]
ny = nextPiece.blocks[i][1]
dx = 12 + nx
dy = 16 - ny
draw.rectangle({x:dx*TILE, y:(ROWS-1-dy)*TILE, width:TILE, height:TILE}, nextPiece.color)
}
}
@@ -269,4 +295,3 @@ this.hud = function() {
draw.text("GAME OVER", {x:10, y:config.height*0.5-5, width:config.width-20, height:20}, null, 0, color.red)
}
}