2 Lua star example - Run with love (https://love2d.org/)
3 Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 local luastar = require("lua-star")
14 -- a 2D map where true is open and false is blocked
17 local screensize = 500
18 local tilesize = screensize / mapsize
22 local start = { x = 1, y = mapsize }
23 local goal = { x = mapsize, y = 1 }
25 -- remember the tile the mouse is hovered over
29 local largeFont = love.graphics.newFont (30)
30 local smallFont = love.graphics.newFont (10)
33 local saveScreenshot = false
35 function randomizeMap ()
46 math.randomseed (os.clock ())
49 local x = math.random (1, mapsize-2)
50 local y = math.random (1, mapsize-2)
51 -- vertical or horizontal
52 if math.random() > .5 then
54 map[x][math.min (mapsize, y+n)] = false
58 map[math.min (mapsize, x+n)][y] = false
64 --saveScreenshot = true
70 love.window.setMode (screensize, screensize)
75 function love.keypressed (key)
77 if key == "escape" then
79 elseif key == "space" then
88 love.graphics.setColor(.6, .6, .6)
91 local fillstyle = "line"
92 if map[x][y] == false then fillstyle = "fill" end
93 love.graphics.rectangle(fillstyle, (x-1)*tilesize, (y-1)*tilesize, tilesize, tilesize)
98 love.graphics.setFont (smallFont)
100 for i, p in ipairs(path) do
101 love.graphics.setColor(0, 1, 0)
102 love.graphics.rectangle("fill", (p.x-1)*tilesize, (p.y-1)*tilesize, tilesize, tilesize)
103 love.graphics.setColor(0, 0, 0)
104 love.graphics.print(i, (p.x-1) * tilesize, (p.y-1) * tilesize)
108 -- draw start and end
109 love.graphics.setColor(1, 0, 0)
110 love.graphics.setFont (largeFont)
111 love.graphics.print("*", (start.x-1) * tilesize, (start.y-1) * tilesize)
112 love.graphics.print("*", (goal.x-1) * tilesize, (goal.y-1) * tilesize)
114 if saveScreenshot then
115 saveScreenshot = false
116 local filename = string.format("screenshot-%d.png", os.time())
117 love.graphics.captureScreenshot(filename)
118 print (string.format("written %s", filename))
123 function love.mousemoved (x, y, dx, dy, istouch)
125 local dx = math.floor(x / tilesize) + 1
126 local dy = math.floor(y / tilesize) + 1
129 if hoveredTile.dx == dx and hoveredTile.dy == dy then
134 hoveredTile = { dx = dx, dy = dy }
135 if love.mouse.isDown (1) then
136 map[dx][dy] = not map[dx][dy]
142 function love.mousepressed (x, y, button, istouch)
144 local dx = math.floor(x / tilesize) + 1
145 local dy = math.floor(y / tilesize) + 1
146 map[dx][dy] = not map[dx][dy]
151 function positionIsOpenFunc (x, y)
153 -- should return true if the position is open to walk
158 function requestPath ()
160 path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)