]> git.lizzy.rs Git - lua-star.git/blob - example/main.lua
dfea8495d2b40323dabcf32f68753c988ddfbef1
[lua-star.git] / example / main.lua
1 --[[
2     Lua star example - Run with love (https://love2d.org/)
3     Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
4
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:
6
7     The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
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.
10 ]]--
11
12 local luastar = require("lua-star")
13
14 -- a 2D map where true is open and false is blocked
15 local map = { }
16 local mapsize = 25
17 local screensize = 500
18 local tilesize = screensize / mapsize
19
20 -- path start and end
21 local path = nil
22 local start = { x = 1, y = mapsize }
23 local goal = { x = mapsize, y = 1 }
24
25 -- remember the tile the mouse is hovered over
26 local hoveredTile
27
28 -- fonts
29 local largeFont = love.graphics.newFont (30)
30 local smallFont = love.graphics.newFont (10)
31
32 function randomizeMap ()
33
34     -- build an open map
35     for x=1, mapsize do
36         map[x] = {}
37         for y=1, mapsize do
38             map[x][y] = true
39         end
40     end
41
42     -- add random walls
43     math.randomseed (os.clock ())
44     for i = 1, 25 do
45         -- start point
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
50             for n = 1, 5 do
51                 map[x][math.min (mapsize, y+n)] = false
52             end
53         else
54             for n = 1, 5 do
55                 map[math.min (mapsize, x+n)][y] = false
56             end
57         end
58     end
59
60     requestPath()
61
62 end
63
64 function love.load ()
65
66     love.window.setMode (screensize, screensize)
67     randomizeMap()
68
69 end
70
71 function love.keypressed (key)
72
73     if key == "escape" then
74         love.event.quit()
75     elseif key == "space" then
76         randomizeMap()
77     end
78
79 end
80
81 function love.draw ()
82
83     -- draw walls
84     love.graphics.setColor(.6, .6, .6)
85     for x=1, mapsize do
86         for y=1, mapsize do
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)
90         end
91     end
92
93     -- draw the path
94     love.graphics.setFont (smallFont)
95     if path then
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)
101         end
102     end
103
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)
109
110 end
111
112 function love.mousemoved (x, y, dx, dy, istouch)
113
114     local dx = math.floor(x / tilesize) + 1
115     local dy = math.floor(y / tilesize) + 1
116
117     if hoveredTile then
118         if hoveredTile.dx == dx and hoveredTile.dy == dy then
119             return
120         end
121     end
122
123     hoveredTile = { dx = dx, dy = dy }
124     if love.mouse.isDown (1) then
125         map[dx][dy] = not map[dx][dy]
126         requestPath()
127     end
128
129 end
130
131 function love.mousepressed (x, y, button, istouch)
132
133     local dx = math.floor(x / tilesize) + 1
134     local dy = math.floor(y / tilesize) + 1
135     map[dx][dy] = not map[dx][dy]
136     requestPath()
137
138 end
139
140 function positionIsOpenFunc (x, y)
141
142     -- should return true if the position is open to walk
143     return map[x][y]
144
145 end
146
147 function requestPath ()
148
149     path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)
150
151 end