From 65fb15704e8968298af46d87b5771a0af0e09486 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Thu, 10 Feb 2022 17:15:33 +0100 Subject: [PATCH] calculate cost for exit tiles --- front/src/Phaser/Game/GameMap.ts | 38 +++++ front/src/Phaser/Game/GameScene.ts | 9 +- front/src/Utils/PathfindingManager.ts | 21 ++- maps/tests/PathfinderAvoidExits/map1.json | 177 +++++++++++++++++++++ maps/tests/PathfinderAvoidExits/map2.json | 184 ++++++++++++++++++++++ maps/tests/index.html | 8 + 6 files changed, 427 insertions(+), 10 deletions(-) create mode 100644 maps/tests/PathfinderAvoidExits/map1.json create mode 100644 maps/tests/PathfinderAvoidExits/map2.json diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index f4791a8f..53153c6a 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -133,6 +133,18 @@ export class GameMap { return grid; } + public getWalkingCostGrid(): number[][] { + const grid: number[][] = []; + for (let y = 0; y < this.map.height; y += 1) { + const row: number[] = []; + for (let x = 0; x < this.map.width; x += 1) { + row.push(this.getWalkingCostAt(x, y)); + } + grid.push(row); + } + return grid; + } + public getTileDimensions(): { width: number; height: number } { return { width: this.map.tilewidth, height: this.map.tileheight }; } @@ -339,6 +351,32 @@ export class GameMap { return false; } + private getWalkingCostAt(x: number, y: number): number { + const bigCost = 100; + for (const layer of this.phaserLayers) { + if (!layer.visible) { + continue; + } + const tile = layer.getTileAt(x, y); + if (!tile) { + continue; + } + if ( + tile && + (tile.properties[GameMapProperties.EXIT_URL] || tile.properties[GameMapProperties.EXIT_SCENE_URL]) + ) { + return bigCost; + } + for (const property of layer.layer.properties) { + //@ts-ignore + if (property.name && property.name === "exitUrl") { + return bigCost; + } + } + } + return 0; + } + private triggerAllProperties(): void { const newProps = this.getProperties(this.key ?? 0); const oldProps = this.lastProperties; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 9b5a9918..45ec3968 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -570,6 +570,7 @@ export class GameScene extends DirtyScene { this.pathfindingManager = new PathfindingManager( this, this.gameMap.getCollisionGrid(), + this.gameMap.getWalkingCostGrid(), this.gameMap.getTileDimensions() ); @@ -583,12 +584,6 @@ export class GameScene extends DirtyScene { waScaleManager ); - this.pathfindingManager = new PathfindingManager( - this, - this.gameMap.getCollisionGrid(), - this.gameMap.getTileDimensions() - ); - this.activatablesManager = new ActivatablesManager(this.CurrentPlayer); biggestAvailableAreaStore.recompute(); @@ -1530,7 +1525,7 @@ ${escapedMessage} phaserLayers[i].setCollisionByProperty({ collides: true }, visible); } } - this.pathfindingManager.setCollisionGrid(this.gameMap.getCollisionGrid()); + this.pathfindingManager.setCollisionGrid(this.gameMap.getCollisionGrid(), this.gameMap.getWalkingCostGrid()); this.markDirty(); } diff --git a/front/src/Utils/PathfindingManager.ts b/front/src/Utils/PathfindingManager.ts index c5057ed8..496ed112 100644 --- a/front/src/Utils/PathfindingManager.ts +++ b/front/src/Utils/PathfindingManager.ts @@ -8,19 +8,26 @@ export class PathfindingManager { private grid: number[][]; private tileDimensions: { width: number; height: number }; - constructor(scene: Phaser.Scene, collisionsGrid: number[][], tileDimensions: { width: number; height: number }) { + constructor( + scene: Phaser.Scene, + collisionsGrid: number[][], + walkingCostGrid: number[][], + tileDimensions: { width: number; height: number } + ) { this.scene = scene; this.easyStar = new EasyStar.js(); - this.easyStar.enableDiagonals(); + // this.easyStar.enableDiagonals(); this.grid = collisionsGrid; this.tileDimensions = tileDimensions; this.setEasyStarGrid(collisionsGrid); + this.setWalkingCostGrid(walkingCostGrid); } - public setCollisionGrid(collisionGrid: number[][]): void { + public setCollisionGrid(collisionGrid: number[][], walkingCostGrid: number[][]): void { this.setEasyStarGrid(collisionGrid); + this.setWalkingCostGrid(walkingCostGrid); } public async findPath( @@ -115,6 +122,14 @@ export class PathfindingManager { this.easyStar.setAcceptableTiles([0]); // zeroes are walkable } + private setWalkingCostGrid(grid: number[][]): void { + for (let y = 0; y < grid.length; y += 1) { + for (let x = 0; x < grid[y].length; x += 1) { + this.easyStar.setAdditionalPointCost(x, y, grid[y][x]); + } + } + } + private logGridToTheConsole(grid: number[][]): void { let rowNumber = 0; for (const row of grid) { diff --git a/maps/tests/PathfinderAvoidExits/map1.json b/maps/tests/PathfinderAvoidExits/map1.json new file mode 100644 index 00000000..51361ced --- /dev/null +++ b/maps/tests/PathfinderAvoidExits/map1.json @@ -0,0 +1,177 @@ +{ "compressionlevel":-1, + "height":10, + "infinite":false, + "layers":[ + { + "data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + "height":10, + "id":1, + "name":"floor", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":2, + "name":"start", + "opacity":0.9, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17], + "height":10, + "id":7, + "name":"walls", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 34, 34, 34, 34, 34, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 34, 34, 34, 34, 34, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 34, 34, 34, 34, 34, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":8, + "name":"exit", + "opacity":1, + "properties":[ + { + "name":"exitUrl", + "type":"string", + "value":"map2.json" + }], + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":9, + "name":"from_exit2", + "opacity":1, + "properties":[ + { + "name":"startLayer", + "type":"bool", + "value":true + }], + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":10, + "name":"funritures", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "draworder":"topdown", + "id":3, + "name":"floorLayer", + "objects":[ + { + "height":58.0929629319722, + "id":2, + "name":"", + "rotation":0, + "text": + { + "text":"DO NOT fall into the water or you will drown! Use right-click to move", + "wrap":true + }, + "type":"", + "visible":true, + "width":207.776169358213, + "x":78.9920090369007, + "y":34.4126432934483 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }], + "nextlayerid":11, + "nextobjectid":3, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.7.2", + "tileheight":32, + "tilesets":[ + { + "columns":11, + "firstgid":1, + "image":"..\/tileset1.png", + "imageheight":352, + "imagewidth":352, + "margin":0, + "name":"tileset1", + "spacing":0, + "tilecount":121, + "tileheight":32, + "tiles":[ + { + "id":16, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":17, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":18, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":19, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }], + "tilewidth":32 + }], + "tilewidth":32, + "type":"map", + "version":"1.6", + "width":10 +} \ No newline at end of file diff --git a/maps/tests/PathfinderAvoidExits/map2.json b/maps/tests/PathfinderAvoidExits/map2.json new file mode 100644 index 00000000..449de40f --- /dev/null +++ b/maps/tests/PathfinderAvoidExits/map2.json @@ -0,0 +1,184 @@ +{ "compressionlevel":-1, + "height":10, + "infinite":false, + "layers":[ + { + "data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + "height":10, + "id":1, + "name":"floor", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":2, + "name":"start", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17], + "height":10, + "id":7, + "name":"walls", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":8, + "name":"exit", + "opacity":1, + "properties":[ + { + "name":"exitUrl", + "type":"string", + "value":"map1.json#from_exit2" + }], + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "draworder":"topdown", + "id":3, + "name":"floorLayer", + "objects":[ + { + "height":19, + "id":2, + "name":"", + "rotation":0, + "text": + { + "text":"YOU ", + "wrap":true + }, + "type":"", + "visible":true, + "width":33.1966362647477, + "x":143.254413856581, + "y":65.4728056229604 + }, + { + "height":39.3497615262321, + "id":3, + "name":"", + "rotation":0, + "text": + { + "fontfamily":"MS Shell Dlg 2", + "pixelsize":32, + "text":"ARE", + "wrap":true + }, + "type":"", + "visible":true, + "width":81.3934036147603, + "x":130.134191004937, + "y":102.423688394277 + }, + { + "height":124.497486386076, + "id":4, + "name":"", + "rotation":0, + "text": + { + "color":"#ff0000", + "fontfamily":"MS Shell Dlg 2", + "pixelsize":96, + "text":"DEAD", + "wrap":true + }, + "type":"", + "visible":true, + "width":246.869092410677, + "x":41.7733861852564, + "y":157.582233294285 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }], + "nextlayerid":9, + "nextobjectid":5, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.7.2", + "tileheight":32, + "tilesets":[ + { + "columns":11, + "firstgid":1, + "image":"..\/tileset1.png", + "imageheight":352, + "imagewidth":352, + "margin":0, + "name":"tileset1", + "spacing":0, + "tilecount":121, + "tileheight":32, + "tiles":[ + { + "id":16, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":17, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":18, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":19, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }], + "tilewidth":32 + }], + "tilewidth":32, + "type":"map", + "version":"1.6", + "width":10 +} \ No newline at end of file diff --git a/maps/tests/index.html b/maps/tests/index.html index e8bf4369..3b3c700a 100644 --- a/maps/tests/index.html +++ b/maps/tests/index.html @@ -88,6 +88,14 @@ Test exits + + + Success Failure Pending + + + Test Pathfinder Avoid Exits + + Success Failure Pending