aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2019-09-18 01:48:35 +0100
committerNat Lasseter <user@4574.co.uk>2019-09-18 01:48:35 +0100
commit5da7fa5f8d0cbbf73edd3902de96c720f399de53 (patch)
tree9b9d3f26a0dbd7b60a927f6f12620de513bbaf48
Initial CommitHEADmaster
-rw-r--r--LICENSE9
-rw-r--r--lib/maze.rb63
-rw-r--r--lib/path.rb134
-rw-r--r--public/maze.html11
-rw-r--r--public/maze.js88
-rw-r--r--public/maze.mml7
-rw-r--r--public/path.pml20
-rw-r--r--readme.textile55
-rwxr-xr-xserver.rb44
9 files changed, 431 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d174b87
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+Copyright 2019 Nat Lasseter
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/lib/maze.rb b/lib/maze.rb
new file mode 100644
index 0000000..4001bea
--- /dev/null
+++ b/lib/maze.rb
@@ -0,0 +1,63 @@
+class Maze
+ def initialize(w, h, sx = 0, sy = 0, tx = 0, ty = 0)
+ @width = w
+ @height = h
+ @start = [sx, sy]
+ @target = [tx, ty]
+ @blocks = []
+ end
+
+ attr_reader :width, :height, :start, :target
+
+ def generate_random(t = -1)
+ @start = [rand(@width), rand(@height)]
+ @target = [rand(@width), rand(@height)]
+
+ t = rand(100) if t == -1
+ t.times do
+ x = rand(@width)
+ y = rand(@height)
+ w = [rand(5), @width - x].min
+ h = [rand(5), @height - y].min
+ @blocks << [x, y, w, h]
+ end
+
+ @blocks.reject! do |blk|
+ sx = @start[0]
+ sy = @start[1]
+ tx = @target[0]
+ ty = @target[1]
+ bx1 = blk[0]
+ by1 = blk[1]
+ bx2 = blk[0] + blk[2]
+ by2 = blk[1] + blk[3]
+
+ sx >= bx1 && sx < bx2 && sy >= by1 && sy < by2 ||
+ tx >= bx1 && tx < bx2 && ty >= by1 && ty < by2
+ end
+
+ self
+ end
+
+ def blocked(x, y)
+ @blocks.any? do |blk|
+ bx1 = blk[0]
+ by1 = blk[1]
+ bx2 = blk[0] + blk[2]
+ by2 = blk[1] + blk[3]
+
+ x >= bx1 && x < bx2 && y >= by1 && y < by2
+ end
+ end
+
+ def walkable(x,y)
+ !blocked(x,y)
+ end
+
+ def mml
+ "#{@width} #{@height}\n" +
+ "#{@start[0]} #{@start[1]}\n" +
+ "#{@target[0]} #{@target[1]}\n" +
+ @blocks.map{|b| b.join(" ")}.join("\n")
+ end
+end
diff --git a/lib/path.rb b/lib/path.rb
new file mode 100644
index 0000000..365df9c
--- /dev/null
+++ b/lib/path.rb
@@ -0,0 +1,134 @@
+class Node
+ O_cost = 1000
+ D_cost = 1414
+
+ def initialize(x, y, sx, sy, tx, ty, w, h, g = 0, parent = nil)
+ @x = x
+ @y = y
+ @sx = sx
+ @sy = sy
+ @tx = tx
+ @ty = ty
+ @w = w
+ @h = h
+ @g = g
+ @parent = parent
+ end
+
+ attr_reader :x, :y
+ attr_accessor :g, :parent
+
+ def h
+ dx = (@x - @tx).abs
+ dy = (@y - @ty).abs
+
+ if dy > dx then
+ D_cost * dx + (O_cost * (dy - dx))
+ else
+ D_cost * dy + (O_cost * (dx - dy))
+ end
+ end
+
+ def f
+ g + h
+ end
+
+ def start?
+ @x == @sx && @y == @sy
+ end
+
+ def target?
+ @x == @tx && @y == @ty
+ end
+
+ def ==(oth)
+ @x == oth.x && @y == oth.y
+ end
+
+ def neighbours
+ ns = []
+ ns << neighbour(@x-1, @y-1, true ) if @x > 0 && @y > 0
+ ns << neighbour(@x-1, @y , false) if @x > 0
+ ns << neighbour(@x-1, @y+1, true ) if @x > 0 && @y < (@h-1)
+ ns << neighbour(@x , @y+1, false) if @y < (@h-1)
+ ns << neighbour(@x+1, @y+1, true ) if @x < (@w-1) && @y < (@h-1)
+ ns << neighbour(@x+1, @y , false) if @x < (@w-1)
+ ns << neighbour(@x+1, @y-1, true ) if @x < (@w-1) && @y > 0
+ ns << neighbour(@x , @y-1, false) if @y > 0
+ ns
+ end
+
+ private
+ def neighbour(x, y, d)
+ Node.new(x, y, @sx, @sy, @tx, @ty, @w, @h, @g + (d ? D_cost : O_cost), self)
+ end
+end
+
+class Path
+ def initialize(maze)
+ @maze = maze
+ @path = []
+ @pathvalid = false
+ end
+
+ def valid?
+ @pathvalid
+ end
+
+ def generate_astar
+ open = []
+ closed = []
+
+ open << Node.new(@maze.start[0], @maze.start[1],
+ @maze.start[0], @maze.start[1],
+ @maze.target[0], @maze.target[1],
+ @maze.width, @maze.height)
+ loop do
+ break if open.empty?
+
+ open.sort! { |a, b| a.f <=> b.f }
+ current = open.shift
+ closed << current
+
+ if current.target? then
+ n = current.parent
+ until n.start?
+ @path.unshift([n.x, n.y])
+ n = n.parent
+ end
+ @pathvalid = true
+ break
+ end
+
+ current.neighbours.each do |n|
+ next if !traversable(current.x, current.y, n.x, n.y) || closed.include?(n)
+
+ if open.include?(n) then
+ ni = open.index(n)
+ m = open[ni]
+ if n.f < m.f then
+ m.g = n.g
+ m.parent = current
+ end
+ else
+ open << n unless open.include?(n)
+ end
+ end
+ end
+
+ self
+ end
+
+ def traversable(sx, sy, tx, ty)
+ if sx == tx || sy == ty then
+ @maze.walkable(tx, ty)
+ else
+ @maze.walkable(tx, ty) &&
+ (@maze.walkable(sx, ty) || @maze.walkable(tx, sy))
+ end
+ end
+
+ def pml
+ @path.map{|p|p.join(" ")}.join("\n")
+ end
+end
diff --git a/public/maze.html b/public/maze.html
new file mode 100644
index 0000000..d94c70e
--- /dev/null
+++ b/public/maze.html
@@ -0,0 +1,11 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Maze</title>
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>
+ <script src="maze.js"></script>
+ </head>
+ <body>
+ </body>
+</html>
diff --git a/public/maze.js b/public/maze.js
new file mode 100644
index 0000000..97452a5
--- /dev/null
+++ b/public/maze.js
@@ -0,0 +1,88 @@
+const scale = 10;
+let mml = null;
+let pml = null;
+
+function newmaze() {
+ fetch("/api/v0.1/maze", {method: "POST"}).then(function(resp) {
+ resp.text().then(function(text) {
+ let t = text.split("\n");
+ mml = t;
+ pml = null;
+ });
+ });
+}
+
+function newpath() {
+ fetch("/api/v0.1/path", {method: "POST"}).then(function(resp) {
+ fetch("/api/v0.1/path/valid").then(function(resp) {
+ resp.text().then(function(text) {
+ if(text.includes("true")) {
+ fetch("/api/v0.1/path").then(function(resp) {
+ resp.text().then(function(text) {
+ pml = text.split("\n");
+ });
+ });
+ }
+ });
+ });
+ });
+}
+
+function setup() {
+ fetch("/api/v0.1/maze").then(function(resp) {
+ resp.text().then(function(text) {
+ let t = text.split("\n");
+ const wh = t[0].split(" ");
+ createCanvas(wh[0] * scale + 50, wh[1] * scale + 50);
+ createButton("Generate Maze").mousePressed(newmaze);
+ createButton("Find Path").mousePressed(newpath);
+ mml = t;
+ });
+ });
+}
+
+function draw() {
+ if(mml) {
+ background(255);
+
+ stroke(0);
+ strokeWeight(1);
+ noFill();
+
+ let wh = mml[0].split(" ");
+ for(let col = 0; col < wh[0]; col++) {
+ for(let row = 0; row < wh[1]; row++) {
+ rect(col * scale, row * scale, scale, scale);
+ }
+ }
+
+ fill(0, 128, 0);
+ let start = mml[1].split(" ");
+ rect(start[0] * scale, start[1] * scale, scale, scale);
+
+ fill(128, 0, 0);
+ let target = mml[2].split(" ");
+ rect(target[0] * scale, target[1] * scale, scale, scale);
+
+ noStroke();
+ fill(0);
+
+ for(let b = 3; b < mml.length; b++) {
+ let blk = mml[b].split(" ");
+ rect(blk[0] * scale, blk[1] * scale, blk[2] * scale, blk[3] * scale);
+ }
+
+ if(pml) {
+ stroke(0);
+ strokeWeight(1);
+ fill(0, 0, 128);
+
+ for(let b = 0; b < pml.length; b++) {
+ let blk = pml[b].split(" ");
+ rect(blk[0] * scale, blk[1] * scale, scale, scale);
+ }
+ } else {
+ text("No path", 10, wh[1] * scale + 20);
+ }
+ }
+}
diff --git a/public/maze.mml b/public/maze.mml
new file mode 100644
index 0000000..d5399ab
--- /dev/null
+++ b/public/maze.mml
@@ -0,0 +1,7 @@
+40 30 width and height
+5 5 start
+20 20 end
+0 0 40 1 block
+0 0 1 30 block
+39 0 1 30 block
+0 29 40 1 block
diff --git a/public/path.pml b/public/path.pml
new file mode 100644
index 0000000..cb2898c
--- /dev/null
+++ b/public/path.pml
@@ -0,0 +1,20 @@
+6 6
+7 7
+8 8
+9 9
+10 10
+11 11
+12 12
+12 13
+12 14
+12 15
+13 15
+14 15
+15 15
+16 15
+17 15
+18 15
+18 16
+18 17
+18 18
+19 19
diff --git a/readme.textile b/readme.textile
new file mode 100644
index 0000000..b9f846a
--- /dev/null
+++ b/readme.textile
@@ -0,0 +1,55 @@
+h1. Maze
+
+Maze generator and A* path finder.
+
+It's horrible, and needs refactoring, but it works.
+
+h2. Server/client
+
+There is a ruby/sinatra server, which does all of the hard work, and exposes an api.
+
+There is a JS/p5.js front end to it, which only does UI.
+
+h2. API
+
+- GET /api/v0.1/maze := Get the MML definition of the maze
+- POST /api/v0.1/maze := Generate a new maze (returns MML)
+- GET /api/v0.1/path := Get the PML definition of the path, if any
+- GET /api/v0.1/path/valid := Returns the work "true" or "false" if a path has been found
+- POST /api/v0.1/path := Generate a new path (returns nothing)
+
+h2. Maze Markup Language / Path Markup Language
+
+h3. MML
+
+MML describes the maze in the following format:
+
+bc. 40 30
+10 11
+22 13
+1 3 4 5
+30 23 2 2
+[...]
+
+* Line 1 is the width and height of the maze
+* Line 2 is the starting x and y coordinates
+* Line 3 is the target x and y coordinates
+* Line 4 and beyond are untraversable "blocks":
+** The first two numbers are the anchor x and y coordinates of the block
+** The second two numbers are the width and height of the block
+* All other nodes are considered traversable
+* There is no diagonal path between two untraversable nodes touching at a corner
+
+h3. PML
+
+PML describes the path in the following format:
+
+bc. 11 12
+12 13
+13 13
+14 13
+15 13
+[...]
+
+* Each line contains the x and y coordinates of the next step in the path
+* The start and target do not appear in the path
diff --git a/server.rb b/server.rb
new file mode 100755
index 0000000..dbe6ea9
--- /dev/null
+++ b/server.rb
@@ -0,0 +1,44 @@
+#!/usr/bin/env ruby
+
+require "sinatra"
+require "./lib/maze"
+require "./lib/path"
+
+def newmaze
+ Maze.new(40, 30).generate_random(150)
+end
+
+def newpath(maze)
+ Path.new(maze).generate_astar
+end
+
+maze = newmaze
+path = newpath(maze)
+
+get "/" do
+ redirect "maze.html"
+end
+
+get "/api/v0.1/maze" do
+ #redirect "maze.mml"
+ maze.mml
+end
+
+post "/api/v0.1/maze" do
+ maze = newmaze
+ maze.mml
+end
+
+get "/api/v0.1/path" do
+ #redirect "path.pml"
+ path.pml
+end
+
+get "/api/v0.1/path/valid" do
+ #redirect "path.pml"
+ path.valid? ? "true" : "false"
+end
+
+post "/api/v0.1/path" do
+ path = newpath(maze)
+end