/
script.lua
300 lines (267 loc) · 7.84 KB
/
script.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
local minBrushSize = 3
local maxBrushSize = 25
local treeDrafts
local density
local roughness
local brushSize
local treeSelection
-- Cache drawing functions for performance reasons
local setTile = Drawing.setTile
local drawTileFrame = Drawing.drawTileFrame
-- Change functions used for undo actions
local startCapture
local endCapture
-- Gets the storage table we use for saving the settings
local function getStorage()
return Util.optStorage(TheoTown.getStorage(), script:getDraft():getId())
end
-- Save settings to storage
local function saveState()
local stg = getStorage()
stg.density = density
stg.roughness = roughness
stg.brushSize = brushSize
stg.treeSelection = {}
treeSelection:forEach(function(d)
table.insert(stg.treeSelection, d:getId())
end)
end
-- Load settings from storage
local function loadState()
local stg = getStorage()
treeSelection = Array()
density = stg.density or 0.5
roughness = stg.roughness or 0
brushSize = stg.brushSize or 5
if stg.treeSelection then
for _,id in pairs(stg.treeSelection) do
local draft = Draft.getDraft(id)
if draft and draft:getType() == 'tree' and draft:isVisible() then
treeSelection:add(draft)
end
end
end
if treeSelection:isEmpty() then
treeSelection:add(Draft.getDraft('$tree00'))
end
end
-- Show about dialog
local function about()
-- Create the dialog
local dialog = GUI.createDialog{
icon = Icon.ABOUT,
title = Translation.control_about,
text = [[Tap on configure to configure the tool. Tap on the map to spawn trees.
Tree planter tool by THEMAX and Lobby.]],
w=200,
h=150,
actions={
icon=Icon.OK,
text=Translation.control_ok,
golden=true
}
}
end
-- Show configuration dialog
local function configure()
-- Create the dialog
local dialog = GUI.createDialog{
icon = Icon.MENU,
title = Translation.treeplanter_cmdconfigure,
w=220,
h=125,
onClose=function() saveState() end
}
-- Create a vertical layout to add settings to
local layout = dialog.content:addLayout{
vertical = true,
spacing=2
}
-- Prepare drawers
local selectedDrawers
local selectionChanged = function()
selectedDrawers = Array()
treeSelection:forEach(function(t)
selectedDrawers:add(City.createDraftDrawer(t))
end)
end
selectionChanged()
local lw = 60
local lh = 20
local line
-- Species
line = layout:addCanvas{h=lh}
line:addLabel{w=lw,text=Translation.treeplanter_species}
line:addCanvas{x=lw,w=-24,h=lh,onDraw=function(self,x,y,w,h)
Drawing.drawRect(x,y,w,h)
if #selectedDrawers>0 then
Drawing.setClipping(x,y,w,h)
local size = self:getClientHeight()
local part = self:getClientWidth() / #selectedDrawers
for i=1,#selectedDrawers do
selectedDrawers[i].draw(x + (i-1) * part,y,part,size)
end
Drawing.resetClipping()
end
end}
line:addButton{text='...',x=-24,onClick=function(self)
GUI.createSelectDraftDialog{
drafts=treeDrafts,
selection=treeSelection,
multiple=true,
minSelection=1,
onSelect=function(selection)
treeSelection = selection
selectionChanged()
end
}
end}
-- Brush size
line = layout:addCanvas{h=lh}
line:addLabel{w=lw,text=Translation.treeplanter_brushsize}
line:addSlider{
x=lw,
minValue=minBrushSize,
maxValue=maxBrushSize,
getValue=function() return brushSize end,
setValue=function(v) brushSize = math.floor(v+0.5) end,
getText=function(v) return ''..math.floor(v+0.5) end
}
-- Density
line = layout:addCanvas{h=lh}
line:addLabel{w=lw,text=Translation.treeplanter_density}
line:addSlider{
x=lw,
getValue=function() return density end,
setValue=function(v) density = v end
}
-- Roughness
line = layout:addCanvas{h=lh}
line:addLabel{w=lw,text=Translation.treeplanter_roughness}
line:addSlider{
x=lw,
getValue=function() return roughness end,
setValue=function(v) roughness = v end
}
end
-- True if trees can be placed here in general
local function isValid(x, y)
return Tile.isValid(x, y)
and Tile.isLand(x, y)
and not Tile.hasRoad(x, y)
end
-- Initialize stuff
function script:init()
treeDrafts = Draft.getDrafts()
:filter(function(d) return d:getType() == 'tree' and d:isVisible() end)
loadState()
end
-- Reset capture functions to prevent memory leaks
function script:leaveCity()
startCapture = nil
endCapture = nil
end
-- Setup tool
function script:event(x, y, level, event)
if event == Script.EVENT_TOOL_ENTER then
-- Setup marker that will draw trees normally
TheoTown.setToolMarker{
markTree = true
}
-- Add tool action button to configure tool
TheoTown.registerToolAction{
icon = Icon.MENU,
name = Translation.treeplanter_cmdconfigure,
onClick = function()
configure()
end
}
TheoTown.registerToolAction{
icon = Icon.ABOUT,
name = Translation.control_about,
onClick = function()
about()
end
}
-- Use filter for draw function for better performance since 1.9.33
if TheoTown.setToolFilter then
TheoTown.setToolFilter{
water = true, -- Call it for water
building = true, -- or for buildings
road = true, -- or for road
mouse = true -- or for mouse location (desktop only)
}
end
-- Add undo button for history and get the capture functions for
-- using it
if TheoTown.addUndoButton then
startCapture, endCapture = TheoTown.addUndoButton()
end
end
end
-- Draw tile based overlay
function script:draw(tileX, tileY, hoverX, hoverY)
-- Mark green if mouse is over it
if tileX == hoverX and tileY == hoverY then
setTile(tileX, tileY)
drawTileFrame(Icon.TOOLMARK + 16 + 1)
end
-- Mark red if not suitable
if not isValid(tileX, tileY) then
setTile(tileX, tileY)
drawTileFrame(Icon.TOOLMARK + 16 + 2)
end
end
-- Execute action
function script:click(tileX, tileY)
if isValid(tileX, tileY) and not treeSelection:isEmpty() then
local radius = math.ceil(brushSize / 2)
if startCapture then
startCapture(tileX - radius, tileY - radius, tileX + radius, tileY + radius, tileX, tileY)
end
local price = 0
-- Iterate over all possible tiles and decided whether to spawn a tree
for y=tileY-radius,tileY+radius do
for x=tileX-radius,tileX+radius do
-- Collect trees that can be placed here
local candidates = Array()
for i=1,#treeSelection do
if Builder.isTreeBuildable(treeSelection[i], x, y) then
candidates:add(i)
end
end
if not candidates:isEmpty() then
-- Select a tree
local treeIdx = candidates:pick()
local tree = treeSelection[treeIdx]
-- Calculcate probability to spawn that tree
local dx = x - tileX
local dy = y - tileY
local dist = math.sqrt(dx * dx + dy * dy)
local prob = math.sqrt(1 - dist / (brushSize / 2))
local alpha = prob*prob*prob*prob
local scaling = 0.01 * brushSize
local noise = City.noise(scaling * x, scaling * y, treeIdx)
prob = prob * density * (alpha + (roughness+(1-roughness)*(1-alpha) * noise))
-- Spawn the tree considering the probability
if prob >= math.random() then
local treePrice = Builder.getTreePrice(tree, x, y)
if not City.canSpend(price + treePrice) then
City.spendMoney(price, tileX, tileY)
Debug.toast(Translation.treeplanter_outofcash)
return
else
Builder.buildTree(tree, x, y)
price = price + treePrice
end
end
end
end
end
-- Spend the money for the trees
City.spendMoney(price, tileX, tileY)
if endCapture then
endCapture()
end
end
end