-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.lua
184 lines (157 loc) · 3.96 KB
/
map.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
local map = {}
function map:setup(w, h)
self.width = w
self.height = h
self.waterline = 4
for x=1, self.width do
self[x] = {}
for y=1, self.height do
self[x][y] = { underlays = {}}
end
end
self:generate_terrain()
-- region = self:circle(6,4,5)
-- for i,v in pairs(region) do
-- self[v.x][v.y].terrain = "dirt"
-- end
end
function map:on_grid(x, y)
if x == nil or y == nil then return false end
return x>=1 and x<=self.width and y>=1 and y<=self.height
end
function map:in_bounds(x, y)
return self:on_grid(x, y) and self[x][y].terrain ~= "void"
end
function map:terrain_type(x, y)
if not self:on_grid(x, y) then
return "void"
else
return self[x][y].terrain
end
end
function map:elev(x, y)
if not self:in_bounds(x, y) then
return 0
else
return self[x][y].elev
end
end
function map:pawn_at(x, y)
if not self:in_bounds(x, y) then
return nil
else
return self[x][y].pawn
end
end
function map:generate_terrain()
-- XXX
local noise_x = love.math.random() * 1238.1
local noise_y = love.math.random() * 1238.1
for x=1, self.width do
for y=1, self.height do
if x+y <= 12 or x+y >= 36 then
self[x][y].terrain = "void"
self[x][y].elev = 0
else
-- set heights
-- self[x][y].elev = math.floor(6 - math.sqrt(love.math.random(1,25)))
self[x][y].elev = 1 + math.floor(5 * math.pow(love.math.noise(noise_x + (x / 17.17) + (y / 34.34), noise_y - (y / 17.17)), 1.5)
+ 2 * math.pow(love.math.noise(noise_x - (x / 5.04) - (y / 10.08), noise_y + (y / 5.04)), 1.5))
-- set terrain
if self.waterline - self[x][y].elev >= 2 then
self[x][y].terrain = "depths"
elseif self.waterline - self[x][y].elev == 1 then
self[x][y].terrain = "shallows"
elseif self[x][y].elev == self.waterline then
self[x][y].terrain = "dirt"
elseif mymath.one_chance_in(3) then
self[x][y].terrain = "dirt"
else
self[x][y].terrain = "grass"
end
end
end
end
pathfinder:reset()
clear_selection()
redraw = true
end
function map:navtype(x, y, movetype, faction)
-- for now, -1: impassable, 1: normal, 2: difficult (ends movement). in future maybe terrain should have varying costs
-- movetype unused right now
if not self:in_bounds(x, y) then return -1
else
pid = map:pawn_at(x, y)
if pid and pawns[pid].faction ~= faction then
return -1
end
end
local tt = self:terrain_type(x, y)
if tt == "void" then
return -1
end
local c = Hex(x,y)
for d = 1, 6 do
neighbor = c:adjacent(d)
pid = map:pawn_at(neighbor.x, neighbor.y)
if pid and pawns[pid].faction ~= faction then
return 2
end
end
if tt == "shallows" or tt == "depths" then
return 2
end
if tt == "dirt" or tt == "grass" then
return 1
end
end
-- function map:tile_at(x, y)
-- if not self:on_grid(x, y) then
-- return img.tile["void"]
-- end
-- feat = self:feat_at(x, y)
-- if feat == "void" then
-- return img.tile["void"]
-- end
-- function map:is_solid(x, y)
-- f = self:feat_at(x, y)
-- return f == "void"
-- end
-- function map:is_floor(x, y)
-- return not self:is_solid(x, y)
-- end
-- function map:is_transparent(x, y)
-- return not self:is_solid(x, y)
-- -- could be glass or something
-- end
-- function map:blocker_at(x, y)
-- --what is in this space that would block us walking there
-- if not self:is_floor(x, y) then return self:feat_at(x, y) end
-- if player.x == x and player.y == y then return "player" end
-- for i,v in ipairs(enemies) do
-- if v then
-- if v.x == x and v.y == y then
-- return v.id
-- end
-- end
-- end
-- for i,v in ipairs(objects) do
-- if v then
-- if v.x == x and v.y == y then
-- return v.id
-- end
-- end
-- end
-- return false -- nothing there
-- end
function map:find_empty_floor()
local x = love.math.random(2, self.width-1)
local y = love.math.random(2, self.height-1)
--ew
while self:navtype(x, y) == -1 or self[x][y].pawn do
x = love.math.random(2, self.width-1)
y = love.math.random(2, self.height-1)
end
return x, y
end
return map