fix/workaround: removed glitches with scaling and quads
[uboot.git] / map.lua
blob72e0a50121f6f0ccda3c6e5641b9a7f23f5cee22
1 Map = {}
3 --[[
4 Map constructor
5 Use this with parameters matrix,noOfTiles,tilesPath,realTileSize,displayBuffer
6 ]]--
7 function Map:new(o)
8 setmetatable(o, self)
9 self.__index = self
10 o.mapHeight = #o.matrix
11 o.mapWidth = #o.matrix[1]
12 o:loadTiles()
13 return o
14 end
16 --TODO verschieben nach graphics
17 function Map:loadTiles()
18 local quads = {}
19 --[[
20 for i = 0, (self.noOfTiles-1) do
21 quads[i+1] = love.graphics.newQuad((((i+1)*self.realTileSize)), 0, self.realTileSize, self.realTileSize, self.realTileSize*self.noOfTiles, self.realTileSize)
22 end
23 ]]--
25 quads[1] = love.graphics.newQuad(1,1,32,32,102,34)
26 quads[2] = love.graphics.newQuad(35,1,32,32,102,34)
27 quads[3] = love.graphics.newQuad(69,32,32,32,102,34)
29 local image = love.graphics.newImage( self.tilesPath )
30 image:setFilter("nearest","linear")
31 image:setWrap("repeat","repeat")
32 self.spriteBatch = love.graphics.newSpriteBatch(image, 10000 )
33 for x = 1, self.mapWidth do
34 for y = 1, self.mapHeight do
35 self.spriteBatch:addq(quads[(self.matrix[x][y])+1],((x-1)*self.realTileSize),((y-1)*self.realTileSize),0,1,1,0,0)
36 end
37 end
38 end
40 --[[
41 it's necessary to recalculate the tile sizes for each cam
42 TODO: really used?? check this
43 ]]--
44 function Map:adjustMapProperties(camWidth,camHeight,zoom)
45 self.tileSize = math.floor(self.realTileSize/zoom)
46 self.displayWidth = math.ceil(camWidth/self.tileSize)
47 self.displayHeight = math.ceil(camHeight/self.tileSize)
48 end
50 --map
51 -- x*y points as base
52 --the walls are lines between these points
53 --a line can be straight top-down ( | ) or left-right ( - ), 90° ( 0,2:1,1 / ) or 45° ( 0,0:2,1 )
55 --map generation algorithm
56 function Map:generateAdjacentFields(width,height)
57 local startx = math.random(width)
58 local starty = math.random(height)
60 --helper for watching mapsize
61 local room_count = 1
63 --set up the map grid 100*100 in size
64 for i = 1,width do
65 map_grid[i] = {}
66 for j=1,height do
67 map_grid[i][j] = 1 --set every cell of the map to wall
68 end
69 end
71 --mark start point as room ( 0 )
72 map_grid[startx][starty] = 0
74 --start borders
76 map_grid[startx-1][starty] = 2
77 map_grid[startx][starty-1] = 2
78 map_grid[startx][starty+1] = 2
79 map_grid[startx+1][starty] = 2
82 --active cells are acted on to continue the path
83 local active_cells = {{startx,starty}}
85 while true do
86 cell_set = #active_cells
87 for i = 1,cell_set do
89 --representation of the adaject cells
90 -- 1 2 3
91 -- 4 x 5
92 -- 6 7 8
93 current_cell = { active_cells[i][1],active_cells[i][2] } --select x and y coords
97 for j = 1,8 do --mark random adjacent cells as active
98 if math.random(30) < 12 then
99 if j == 1 or j == 2 or j == 3 then
100 temp_x = -1
101 elseif j == 4 or j == 5 then
102 temp_x = 0
103 else
104 temp_x = 1
107 if j == 1 or j == 4 or j == 6 then
108 temp_y = -1
109 elseif j == 2 or j == 7 then
110 temp_y = 0
111 else
112 temp_y = 1
114 --mark cell as active and room if not already a room cell and if not out ouf map borders
115 new_x = current_cell[1]+temp_x --x coord
116 new_y = current_cell[2]+temp_y --y coord
117 if (new_x < 98 and new_x > 1 and new_y < 98 and new_y > 1) then --obey map borders
118 if (map_grid[ new_x ][ new_y ] == 2 or map_grid[ new_x ][ new_y ] == 1) then --only do something if this cell is not already a room
119 table.insert(active_cells, {new_x,new_y} ) --insert to the active cell list
120 map_grid[new_x][new_y] = 0 --mark active cell as room
122 for k = 1,4 do --mark borders
123 if k == 1 then
124 temp_x2 = -1
125 elseif k == 2 or k == 3 then
126 temp_x2 = 0
127 else
128 temp_x2 = 1
131 if k == 2 then
132 temp_y2 = -1
133 elseif k == 1 or k == 4 then
134 temp_y2 = 0
135 else
136 temp_y2 = 1
139 border_x = new_x+temp_x2 --x coord
140 border_y = new_y+temp_y2 --y coord
142 if map_grid[ border_x ][ border_y ] == 1 then
143 map_grid[ border_x ][ border_y ] = 2 --mark as border if its still wall
145 end --for k = 1,4 do
147 room_count = room_count+1
154 for i = 1,cell_set do
155 table.remove(active_cells,i)
157 if cell_set == 0 then
158 break
162 print(room_count)
163 if room_count < 1500 then
164 return false
165 else
166 return true
172 --TODO kick this function?? is not used
173 function generate_borders()
174 ii = 100
175 jj = 100
176 for ii = 1,ii do
177 for jj = 1,jj do
178 if map_grid[ii][jj] == 2 then
179 select_tile(ii,jj)
185 --TODO:
186 -- there are some possibilities for selecting the tile for the border depending on the sorrounding
188 -- again here:
189 -- 1 2 3
190 -- 4 x 5
191 -- 6 7 8
192 -- these blocks can be wall, border or space, so there are 3^8 combinations
194 -- orientation of border
195 -- bottom 10
196 -- top 11
197 -- left 12
198 -- right 13
199 -- 45° / 14
200 -- 45° \ 15
201 -- 22,5° / 1. part 16
202 -- 22,5° / 2. part 17
203 -- 22,5° \ 1. part 18
204 -- 22,5° \ 2. part 19
205 -- 22,5° bottom-left,top-right 1. part 20
206 -- 22,5° bottom-left,top-right 2. part 21
207 -- 22,5* top-left, bottom-right 1. part 22
208 -- 22,5* top-left, bottom-right 2. part 23
210 --function select_tile(x,y)
211 -- if
212 --end
215 --[[
216 Generates spawn points in the water
217 ]]--
218 function Map:generateSpawnPoints(width,height)
219 repeat
220 randx = math.random(width)
221 randy = math.random(height)
222 until map_grid[randx][randy] == 0
223 startcoords = {randx*self.realTileSize,randy*self.realTileSize}
224 return startcoords
227 function Map:generateMap()
228 seed = os.time()
229 math.randomseed( seed ) --pseudo-random, good enough for us
231 while Map:generateAdjacentFields(100,100) == false do
232 print "fail"
234 print("map #",seed)
235 --generate_borders()