From fa0a3683fdd0ff622c56e58111c22de21426b4ad Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:31:45 +0100 Subject: [PATCH 01/13] Make isWalkable allow an array of images !! UNTESTED !! --- a-star.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/a-star.ts b/a-star.ts index d6cd3e9..0622f8a 100644 --- a/a-star.ts +++ b/a-star.ts @@ -37,7 +37,7 @@ namespace scene { //% onTilesOf.decompileIndirectFixedInstances=true //% help=github:arcade-tilemap-a-star/docs/a-star //% group="Path Following" weight=10 - export function aStar(start: tiles.Location, end: tiles.Location, onTilesOf: Image = null) { + export function aStar(start: tiles.Location, end: tiles.Location, onTilesOf: Image | Image[] = null) { const tm = game.currentScene().tileMap; if (!tm || !start || !end) return undefined; @@ -52,7 +52,7 @@ namespace scene { l => l.col == end1.col && l.row == end1.row); } - export function aStarToAnyOfType(start: tiles.Location, tile: Image, onTilesOf: Image) { + export function aStarToAnyOfType(start: tiles.Location, tile: Image, onTilesOf: Image | Image[]) { const tm = game.currentScene().tileMap; if (!tm || !start) return undefined; @@ -70,7 +70,7 @@ namespace scene { }); } - export function generalAStar(tm: tiles.TileMap, start: SimpleLocation, onTilesOf: Image, + export function generalAStar(tm: tiles.TileMap, start: SimpleLocation, onTilesOf: Image | Image[], heuristic: (tile: SimpleLocation) => number, isEnd: (tile: SimpleLocation) => boolean): tiles.Location[] { @@ -228,10 +228,11 @@ namespace scene { (DIAGONAL_COST - NEIGHBOR_COST) } - function isWalkable(loc: SimpleLocation, onTilesOf: Image, tm: tiles.TileMap): boolean { + function isWalkable(loc: SimpleLocation, onTilesOf: Image | Image[], tm: tiles.TileMap): boolean { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) + if (typeof(onTilesOf) == "array") return onTilesOf.some(item => img.equals(item)); return img.equals(onTilesOf); } } From 455420f1b9b16f84c6af183338aba92445e8b48f Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:44:36 +0100 Subject: [PATCH 02/13] Update pxt.json bump the version --- pxt.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pxt.json b/pxt.json index af8d0c5..3d16279 100644 --- a/pxt.json +++ b/pxt.json @@ -1,6 +1,6 @@ { "name": "arcade-tilemap-a-star", - "version": "0.4.0", + "version": "0.4.1", "description": "", "dependencies": { "device": "*" From 14b0746ec243473ba4370499a3a5feb8713e99c7 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:48:10 +0100 Subject: [PATCH 03/13] Update a-star.ts fix ln235 --- a-star.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index 0622f8a..cce8214 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,7 +232,7 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (typeof(onTilesOf) == "array") return onTilesOf.some(item => img.equals(item)); + if (onTilesOf instanceof Array) return onTilesOf.some(item => img.equals(item)); return img.equals(onTilesOf); } } From 8793335f9832be5f5257c6a27e1b91d688063e18 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:54:23 +0100 Subject: [PATCH 04/13] push to activate workflows --- a-star.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index cce8214..31c4089 100644 --- a/a-star.ts +++ b/a-star.ts @@ -1,4 +1,4 @@ -//+array+SimpleLocation sim:7.5%, meowbit:10.5% ms comparing with origin +//+array+SimpleLocation sim:7.5%, meowbit:10.5% ms comparing with origin namespace scene { //costs, scaled up by 1000 const NEIGHBOR_COST = 1000; From 9558539cbbd5436b61b629e95745c2ecd0a002d7 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:06:38 +0100 Subject: [PATCH 05/13] Use a better array checker --- a-star.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/a-star.ts b/a-star.ts index 31c4089..d88ad69 100644 --- a/a-star.ts +++ b/a-star.ts @@ -1,4 +1,4 @@ -//+array+SimpleLocation sim:7.5%, meowbit:10.5% ms comparing with origin +//+array+SimpleLocation sim:7.5%, meowbit:10.5% ms comparing with origin namespace scene { //costs, scaled up by 1000 const NEIGHBOR_COST = 1000; @@ -232,7 +232,7 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (onTilesOf instanceof Array) return onTilesOf.some(item => img.equals(item)); + if (Object.prototype.toString.call(onTilesOf) === '[object Array]') return onTilesOf.some(item => img.equals(item)); return img.equals(onTilesOf); } } From 425a19284c4dc8f984e55903c01009497bbd30f1 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:19:37 +0100 Subject: [PATCH 06/13] please just work --- a-star.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index d88ad69..50e718d 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,7 +232,7 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (Object.prototype.toString.call(onTilesOf) === '[object Array]') return onTilesOf.some(item => img.equals(item)); + if ("length" in onTilesOf) return onTilesOf.some(item => img.equals(item)); return img.equals(onTilesOf); } } From bc11c2ff7057a246dd7b57e485a35a166c46ed09 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:34:01 +0100 Subject: [PATCH 07/13] WORK GODDAMNIT --- a-star.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/a-star.ts b/a-star.ts index 50e718d..7ea3b5f 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,7 +232,6 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if ("length" in onTilesOf) return onTilesOf.some(item => img.equals(item)); - return img.equals(onTilesOf); + ("length" in onTilesOf ? onTilesOf : [onTilesOf]).some(item => img.equals(item)); } } From 05603dcd07e4b5e59d653d5029e0eec365a11e0e Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:48:56 +0100 Subject: [PATCH 08/13] # --- a-star.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index 7ea3b5f..86a340b 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,6 +232,6 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - ("length" in onTilesOf ? onTilesOf : [onTilesOf]).some(item => img.equals(item)); + return ("length" in onTilesOf ? onTilesOf : [onTilesOf]).some((item: Image) => img.equals(item)); } } From fc65bcae41e87bf292e1a286d515dad818efeaaf Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:00:48 +0100 Subject: [PATCH 09/13] back to instanceof, but checking the other way around this time --- a-star.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index 86a340b..02afef2 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,6 +232,7 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - return ("length" in onTilesOf ? onTilesOf : [onTilesOf]).some((item: Image) => img.equals(item)); + if (onTilesOf instanceof Image) return img.equals(onTilesOf) + else return onTilesOf.some((item: Image) => img.equals(item)); } } From 60f66140d35c860266df960e324565fb88218742 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:13:42 +0100 Subject: [PATCH 10/13] Alright fine. Type guard. --- a-star.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/a-star.ts b/a-star.ts index 02afef2..3f54c76 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,7 +232,11 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (onTilesOf instanceof Image) return img.equals(onTilesOf) + if (!isArrayLike(onTilesOf)) return img.equals(onTilesOf) else return onTilesOf.some((item: Image) => img.equals(item)); } + + function isArrayLike(val: any): val is Array { + return val && typeof(val) === "object" && val.length; + } } From d952ff67bc0f6379c8a1130dffd3b2a781d242fc Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:31:15 +0100 Subject: [PATCH 11/13] Ok fine. Tested in makecode itself, not typescriptlang.org/play. Works there. --- a-star.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/a-star.ts b/a-star.ts index 3f54c76..5118e50 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,11 +232,12 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (!isArrayLike(onTilesOf)) return img.equals(onTilesOf) + if (!isArrayLike(onTilesOf)) return img.equals(onTilesOf) else return onTilesOf.some((item: Image) => img.equals(item)); } - function isArrayLike(val: any): val is Array { - return val && typeof(val) === "object" && val.length; + function isArrayLike(val: T | T[]): val is Array { + return val && typeof(val) === "object" && + Object.keys(val).some(k => k == "length"); } } From 2dd4beba87df1a25950653790973034bf4ca8836 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:36:08 +0100 Subject: [PATCH 12/13] FINE. I'LL GIVE YOU *ANOTHER* TYPE GUARD. --- a-star.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/a-star.ts b/a-star.ts index 5118e50..015ed82 100644 --- a/a-star.ts +++ b/a-star.ts @@ -232,12 +232,17 @@ namespace scene { if (tm.isObstacle(loc.col, loc.row)) return false; if (!onTilesOf) return true; const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) - if (!isArrayLike(onTilesOf)) return img.equals(onTilesOf) - else return onTilesOf.some((item: Image) => img.equals(item)); + if (isImageLike(onTilesOf)) return img.equals(onTilesOf) + if (isArrayLike(onTilesOf)) return onTilesOf.some((item: Image) => img.equals(item)); } function isArrayLike(val: T | T[]): val is Array { return val && typeof(val) === "object" && Object.keys(val).some(k => k == "length"); } + function isImageLike(val: any): val is Image { + return val && typeof (val) === "object" && + Object.keys(val).some(k => k == "width") && + Object.keys(val).some(k => k == "height"); + } } From e818a1db0aee13a9e3a41fcc12d2ca831aa359a7 Mon Sep 17 00:00:00 2001 From: Nif <73426353+Unexian@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:50:45 +0100 Subject: [PATCH 13/13] alright fine. return true for all i care --- a-star.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/a-star.ts b/a-star.ts index 015ed82..c7ac34a 100644 --- a/a-star.ts +++ b/a-star.ts @@ -234,6 +234,7 @@ namespace scene { const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row)) if (isImageLike(onTilesOf)) return img.equals(onTilesOf) if (isArrayLike(onTilesOf)) return onTilesOf.some((item: Image) => img.equals(item)); + return true } function isArrayLike(val: T | T[]): val is Array {