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)
32 function randomizeMap ()
43 math.randomseed (os.clock ())
46 local x = math.random (2, mapsize-2)
47 local y = math.random (2, mapsize-2)
48 -- vertical or horizontal
49 if math.random() > .5 then
51 map[x][math.min (mapsize, y+n)] = false
55 map[math.min (mapsize, x+n)][y] = false
66 love.window.setMode (screensize, screensize)
71 function love.keypressed (key)
73 if key == "escape" then
75 elseif key == "space" then
84 love.graphics.setColor(.6, .6, .6)
87 local fillstyle = "line"
88 if map[x][y] == false then fillstyle = "fill" end
89 love.graphics.rectangle(fillstyle, (x-1)*tilesize, (y-1)*tilesize, tilesize, tilesize)
94 love.graphics.setFont (smallFont)
96 for i, p in ipairs(path) do
97 love.graphics.setColor(0, 1, 0)
98 love.graphics.rectangle("fill", (p.x-1)*tilesize, (p.y-1)*tilesize, tilesize, tilesize)
99 love.graphics.setColor(0, 0, 0)
100 love.graphics.print(i, (p.x-1) * tilesize, (p.y-1) * tilesize)
104 -- draw start and end
105 love.graphics.setColor(1, 0, 0)
106 love.graphics.setFont (largeFont)
107 love.graphics.print("*", (start.x-1) * tilesize, (start.y-1) * tilesize)
108 love.graphics.print("*", (goal.x-1) * tilesize, (goal.y-1) * tilesize)
112 function love.mousemoved (x, y, dx, dy, istouch)
114 local dx = math.floor(x / tilesize) + 1
115 local dy = math.floor(y / tilesize) + 1
118 if hoveredTile.dx == dx and hoveredTile.dy == dy then
123 hoveredTile = { dx = dx, dy = dy }
124 if love.mouse.isDown (1) then
125 map[dx][dy] = not map[dx][dy]
131 function love.mousepressed (x, y, button, istouch)
133 local dx = math.floor(x / tilesize) + 1
134 local dy = math.floor(y / tilesize) + 1
135 map[dx][dy] = not map[dx][dy]
140 function positionIsOpenFunc (x, y)
142 -- should return true if the position is open to walk
147 function requestPath ()
149 path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)