Files @ r3478:523ece58cb16
Branch filter:

Location: cpp/openttd-patchpack/source/tree_cmd.c - annotation

tron
(svn r4323) -Regression: Clear the slot assignments of all vehicles heading twoards a road stop if that road stop gets removed
This issue was fixed in r2210 and reintroduced in r4259 when the multistop handling was overhauled.
  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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
r2186:5ee653b1b5e1
r2186:5ee653b1b5e1
r0:d63b455452f6
r1891:c5c5466afa35
r3144:5b175fed4b0b
r507:b1b0a0a3c313
r2148:f279899fdbd0
r1784:15b805156766
r2163:ae001e2aa5b0
r679:3a7b08cc8504
r1209:5955f8748394
r3144:5b175fed4b0b
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r337:998f55d0b04c
r2153:b45e3461c6c4
r0:d63b455452f6
r2981:799e4c11fa26
r0:d63b455452f6
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3441:39bd962fe338
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3441:39bd962fe338
r0:d63b455452f6
r1286:3a5a4a038ec8
r3379:e72a0eb00c30
r3441:39bd962fe338
r3441:39bd962fe338
r3441:39bd962fe338
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3441:39bd962fe338
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2958:7c0285bafd42
r0:d63b455452f6
r2981:799e4c11fa26
r2088:e194e2d1f74b
r3441:39bd962fe338
r3441:39bd962fe338
r0:d63b455452f6
r0:d63b455452f6
r1370:df4c59614da2
r3441:39bd962fe338
r2981:799e4c11fa26
r2088:e194e2d1f74b
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r0:d63b455452f6
r2955:af9992213b85
r2955:af9992213b85
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r2958:7c0285bafd42
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r0:d63b455452f6
r1093:18f56ef2d029
r0:d63b455452f6
r2243:d117fdfb517f
r0:d63b455452f6
r2051:04d7c352ccc4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1093:18f56ef2d029
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r1202:95cf4c0fd209
r0:d63b455452f6
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2955:af9992213b85
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r2958:7c0285bafd42
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1202:95cf4c0fd209
r0:d63b455452f6
r0:d63b455452f6
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r3379:e72a0eb00c30
r2958:7c0285bafd42
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1093:18f56ef2d029
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1784:15b805156766
r1784:15b805156766
r1784:15b805156766
r1784:15b805156766
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r3183:d3ef2fb28a8a
r0:d63b455452f6
r2118:0aaacbc7667d
r1286:3a5a4a038ec8
r2892:8cb35230ee5d
r1784:15b805156766
r1784:15b805156766
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2118:0aaacbc7667d
r2118:0aaacbc7667d
r3422:cca72d7a228c
r3422:cca72d7a228c
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r2118:0aaacbc7667d
r2118:0aaacbc7667d
r2088:e194e2d1f74b
r1286:3a5a4a038ec8
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3003:935ed916c4eb
r3183:d3ef2fb28a8a
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r0:d63b455452f6
r1286:3a5a4a038ec8
r2981:799e4c11fa26
r2088:e194e2d1f74b
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r0:d63b455452f6
r1286:3a5a4a038ec8
r2088:e194e2d1f74b
r3183:d3ef2fb28a8a
r1286:3a5a4a038ec8
r0:d63b455452f6
r0:d63b455452f6
r2955:af9992213b85
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r2955:af9992213b85
r2955:af9992213b85
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r2981:799e4c11fa26
r3079:e4f78ffd69d5
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r2088:e194e2d1f74b
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r2981:799e4c11fa26
r3441:39bd962fe338
r2981:799e4c11fa26
r3441:39bd962fe338
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3079:e4f78ffd69d5
r2981:799e4c11fa26
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r1286:3a5a4a038ec8
r3003:935ed916c4eb
r1286:3a5a4a038ec8
r3441:39bd962fe338
r3379:e72a0eb00c30
r0:d63b455452f6
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r1286:3a5a4a038ec8
r3183:d3ef2fb28a8a
r1286:3a5a4a038ec8
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r3183:d3ef2fb28a8a
r3183:d3ef2fb28a8a
r3183:d3ef2fb28a8a
r3183:d3ef2fb28a8a
r3183:d3ef2fb28a8a
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2654:85bc53a2472e
r0:d63b455452f6
r0:d63b455452f6
r2981:799e4c11fa26
r3441:39bd962fe338
r3441:39bd962fe338
r3441:39bd962fe338
r0:d63b455452f6
r0:d63b455452f6
r2220:ffd09009ed77
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2085:757cdc338c24
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r959:bb0ac3e56084
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r2981:799e4c11fa26
r193:6aa65dc5a4b4
r0:d63b455452f6
r3441:39bd962fe338
r2981:799e4c11fa26
r3441:39bd962fe338
r3441:39bd962fe338
r2981:799e4c11fa26
r193:6aa65dc5a4b4
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r2088:e194e2d1f74b
r0:d63b455452f6
r0:d63b455452f6
r3271:7fe55a2d4518
r0:d63b455452f6
r3271:7fe55a2d4518
r2639:e298192a7c03
r0:d63b455452f6
r2654:85bc53a2472e
r2654:85bc53a2472e
r0:d63b455452f6
r2654:85bc53a2472e
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2952:e97f823d2dbe
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r3271:7fe55a2d4518
r0:d63b455452f6
r2644:d5140ebca347
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2088:e194e2d1f74b
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2537:eb2232d81b9c
r2537:eb2232d81b9c
r2243:d117fdfb517f
r0:d63b455452f6
r0:d63b455452f6
r3418:cff6e454339a
r2548:9570bbbdacab
r3418:cff6e454339a
r39:c8e54423ac24
r39:c8e54423ac24
r1977:1f8b99c96041
r1977:1f8b99c96041
r2243:d117fdfb517f
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r1005:0796061bc2a6
r0:d63b455452f6
r0:d63b455452f6
r2981:799e4c11fa26
r3441:39bd962fe338
r0:d63b455452f6
r2243:d117fdfb517f
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r3441:39bd962fe338
r2981:799e4c11fa26
r3441:39bd962fe338
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r1901:e806afa50723
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r3379:e72a0eb00c30
r3379:e72a0eb00c30
r3441:39bd962fe338
r3441:39bd962fe338
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2243:d117fdfb517f
r3379:e72a0eb00c30
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r3379:e72a0eb00c30
r3379:e72a0eb00c30
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r2981:799e4c11fa26
r0:d63b455452f6
r3422:cca72d7a228c
r3441:39bd962fe338
r3441:39bd962fe338
r0:d63b455452f6
r3422:cca72d7a228c
r2981:799e4c11fa26
r3441:39bd962fe338
r3441:39bd962fe338
r3441:39bd962fe338
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r909:898755bd4512
r0:d63b455452f6
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r193:6aa65dc5a4b4
r2981:799e4c11fa26
r2981:799e4c11fa26
r3379:e72a0eb00c30
r3441:39bd962fe338
r3379:e72a0eb00c30
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2955:af9992213b85
r2955:af9992213b85
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r2981:799e4c11fa26
r3447:1cf4fdb2e0fa
r2981:799e4c11fa26
r3441:39bd962fe338
r2981:799e4c11fa26
r2981:799e4c11fa26
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r2981:799e4c11fa26
r0:d63b455452f6
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r2981:799e4c11fa26
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1093:18f56ef2d029
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r2955:af9992213b85
r3017:1bf2ce772ffa
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r3379:e72a0eb00c30
r1035:d35ec5ea5f73
r3447:1cf4fdb2e0fa
r3441:39bd962fe338
r3447:1cf4fdb2e0fa
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2088:e194e2d1f74b
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1035:d35ec5ea5f73
r3447:1cf4fdb2e0fa
r3441:39bd962fe338
r2955:af9992213b85
r3447:1cf4fdb2e0fa
r3447:1cf4fdb2e0fa
r3441:39bd962fe338
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2436:963efe8b84cc
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1093:18f56ef2d029
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r39:c8e54423ac24
r0:d63b455452f6
/* $Id$ */

#include "stdafx.h"
#include "openttd.h"
#include "clear_map.h"
#include "table/strings.h"
#include "table/sprites.h"
#include "table/tree_land.h"
#include "functions.h"
#include "map.h"
#include "tile.h"
#include "tree_map.h"
#include "viewport.h"
#include "command.h"
#include "town.h"
#include "sound.h"
#include "variables.h"

static TreeType GetRandomTreeType(TileIndex tile, uint seed)
{
	switch (_opt.landscape) {
		case LT_NORMAL:
			return seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE;

		case LT_HILLY:
			return seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC;

		case LT_DESERT:
			switch (GetTropicZone(tile)) {
				case TROPICZONE_INVALID: return seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL;
				case TROPICZONE_DESERT:  return (seed > 12) ? TREE_INVALID : TREE_CACTUS;
				default:                 return seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST;
			}

		default:
			return seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND;
	}
}

static void PlaceTree(TileIndex tile, uint32 r)
{
	TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));

	if (tree != TREE_INVALID) {
		MakeTree(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6), TREE_GROUND_GRASS, 0);

		// above snowline?
		if (_opt.landscape == LT_HILLY && GetTileZ(tile) > _opt.snow_line) {
			SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
			SetTreeCounter(tile, GB(r, 24, 3));
		} else {
			SetTreeGroundDensity(tile, GB(r, 28, 1), 0);
			SetTreeCounter(tile, GB(r, 24, 4));
		}
	}
}

static void DoPlaceMoreTrees(TileIndex tile)
{
	uint i;

	for (i = 0; i < 1000; i++) {
		uint32 r = Random();
		int x = GB(r, 0, 5) - 16;
		int y = GB(r, 8, 5) - 16;
		uint dist = myabs(x) + myabs(y);
		TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));

		if (dist <= 13 &&
				IsTileType(cur_tile, MP_CLEAR) &&
				!IsClearGround(cur_tile, CLEAR_FIELDS) &&
				!IsClearGround(cur_tile, CLEAR_ROCKS)) {
			PlaceTree(cur_tile, r);
		}
	}
}

static void PlaceMoreTrees(void)
{
	uint i = ScaleByMapSize(GB(Random(), 0, 5) + 25);
	do {
		DoPlaceMoreTrees(RandomTile());
	} while (--i);
}

void PlaceTreesRandomly(void)
{
	uint i;

	i = ScaleByMapSize(1000);
	do {
		uint32 r = Random();
		TileIndex tile = RandomTileSeed(r);
		if (IsTileType(tile, MP_CLEAR) &&
				!IsClearGround(tile, CLEAR_FIELDS) &&
				!IsClearGround(tile, CLEAR_ROCKS)) {
			PlaceTree(tile, r);
		}
	} while (--i);

	/* place extra trees at rainforest area */
	if (_opt.landscape == LT_DESERT) {
		i = ScaleByMapSize(15000);

		do {
			uint32 r = Random();
			TileIndex tile = RandomTileSeed(r);
			if (IsTileType(tile, MP_CLEAR) && GetTropicZone(tile) == TROPICZONE_RAINFOREST) {
				PlaceTree(tile, r);
			}
		} while (--i);
	}
}

void GenerateTrees(void)
{
	uint i;

	if (_opt.landscape != LT_CANDY) PlaceMoreTrees();

	for (i = _opt.landscape == LT_HILLY ? 15 : 6; i != 0; i--) {
		PlaceTreesRandomly();
	}
}

/** Plant a tree.
 * @param x,y start tile of area-drag of tree plantation
 * @param p1 tree type, -1 means random.
 * @param p2 end tile of area-drag
 */
int32 CmdPlantTree(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
{
	StringID msg = INVALID_STRING_ID;
	int32 cost;
	int sx, sy, x, y;

	if (p2 >= MapSize()) return CMD_ERROR;
	/* Check the tree type. It can be random or some valid value within the current climate */
	if (p1 != (uint)-1 && p1 - _tree_base_by_landscape[_opt.landscape] >= _tree_count_by_landscape[_opt.landscape]) return CMD_ERROR;

	SET_EXPENSES_TYPE(EXPENSES_OTHER);

	// make sure sx,sy are smaller than ex,ey
	sx = TileX(p2);
	sy = TileY(p2);
	ex /= TILE_SIZE;
	ey /= TILE_SIZE;
	if (ex < sx) intswap(ex, sx);
	if (ey < sy) intswap(ey, sy);

	cost = 0; // total cost

	for (x = sx; x <= ex; x++) {
		for (y = sy; y <= ey; y++) {
			TileIndex tile = TileXY(x, y);

			if (!EnsureNoVehicle(tile)) continue;

			switch (GetTileType(tile)) {
				case MP_TREES:
					// no more space for trees?
					if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 3) {
						msg = STR_2803_TREE_ALREADY_HERE;
						continue;
					}

					if (flags & DC_EXEC) {
						AddTreeCount(tile, 1);
						MarkTileDirtyByTile(tile);
					}
					// 2x as expensive to add more trees to an existing tile
					cost += _price.build_trees * 2;
					break;

				case MP_CLEAR:
					if (!IsTileOwner(tile, OWNER_NONE)) {
						msg = STR_2804_SITE_UNSUITABLE;
						continue;
					}

					switch (GetClearGround(tile)) {
						case CLEAR_FIELDS: cost += _price.clear_3; break;
						case CLEAR_ROCKS:  cost += _price.clear_2; break;
						default: break;
					}

					if (flags & DC_EXEC) {
						TreeType treetype;
						uint growth;

						if (_game_mode != GM_EDITOR && _current_player < MAX_PLAYERS) {
							Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
							if (t != NULL)
								ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
						}

						treetype = p1;
						if (treetype == TREE_INVALID) {
							treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
							if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
						}

						growth = _game_mode == GM_EDITOR ? 3 : 0;
						switch (GetClearGround(tile)) {
							case CLEAR_ROUGH: MakeTree(tile, treetype, 0, growth, TREE_GROUND_ROUGH, 0); break;
							case CLEAR_SNOW:  MakeTree(tile, treetype, 0, growth, TREE_GROUND_SNOW_DESERT, GetClearDensity(tile)); break;
							default:          MakeTree(tile, treetype, 0, growth, TREE_GROUND_GRASS, 0); break;
						}
						MarkTileDirtyByTile(tile);

						if (_game_mode == GM_EDITOR && IS_INT_INSIDE(treetype, TREE_RAINFOREST, TREE_CACTUS))
							SetTropicZone(tile, TROPICZONE_RAINFOREST);
					}
					cost += _price.build_trees;
					break;

				default:
					msg = STR_2804_SITE_UNSUITABLE;
					break;
			}
		}
	}

	if (cost == 0) {
		return_cmd_error(msg);
	} else {
		return cost;
	}
}

typedef struct TreeListEnt {
	uint32 image;
	byte x,y;
} TreeListEnt;

static void DrawTile_Trees(TileInfo *ti)
{
	const uint32 *s;
	const TreePos* d;
	byte z;

	switch (GetTreeGround(ti->tile)) {
		case TREE_GROUND_GRASS: DrawClearLandTile(ti, 3); break;
		case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
		default: DrawGroundSprite(_tree_sprites_1[GetTreeDensity(ti->tile)] + _tileh_to_sprite[ti->tileh]); break;
	}

	DrawClearLandFence(ti);

	z = ti->z;
	if (ti->tileh != 0) {
		z += 4;
		if (IsSteepTileh(ti->tileh))
			z += 4;
	}

	{
		uint16 tmp = ti->x;
		uint index;

		tmp = ROR(tmp, 2);
		tmp -= ti->y;
		tmp = ROR(tmp, 3);
		tmp -= ti->x;
		tmp = ROR(tmp, 1);
		tmp += ti->y;

		d = _tree_layout_xy[GB(tmp, 4, 2)];

		index = GB(tmp, 6, 2) + (GetTreeType(ti->tile) << 2);

		/* different tree styles above one of the grounds */
		if (GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT &&
				GetTreeDensity(ti->tile) >= 2 &&
				IS_INT_INSIDE(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
			index += 164 - (TREE_SUB_ARCTIC << 2);
		}

		assert(index < lengthof(_tree_layout_sprite));
		s = _tree_layout_sprite[index];
	}

	StartSpriteCombine();

	if (!(_display_opt & DO_TRANS_BUILDINGS) || !_patches.invisible_trees) {
		TreeListEnt te[4];
		uint i;

		/* put the trees to draw in a list */
		i = GetTreeCount(ti->tile) + 1;
		do {
			uint32 image = s[0] + (--i == 0 ? GetTreeGrowth(ti->tile) : 3);
			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
			te[i].image = image;
			te[i].x = d->x;
			te[i].y = d->y;
			s++;
			d++;
		} while (i);

		/* draw them in a sorted way */
		for (;;) {
			byte min = 0xFF;
			TreeListEnt *tep = NULL;

			i = GetTreeCount(ti->tile) + 1;
			do {
				if (te[--i].image != 0 && te[i].x + te[i].y < min) {
					min = te[i].x + te[i].y;
					tep = &te[i];
				}
			} while (i);

			if (tep == NULL) break;

			AddSortableSpriteToDraw(tep->image, ti->x + tep->x, ti->y + tep->y, 5, 5, 0x10, z);
			tep->image = 0;
		}
	}

	EndSpriteCombine();
}


static uint GetSlopeZ_Trees(const TileInfo* ti)
{
	return GetPartialZ(ti->x & 0xF, ti->y & 0xF, ti->tileh) + ti->z;
}

static uint GetSlopeTileh_Trees(TileIndex tile, uint tileh)
{
	return tileh;
}

static int32 ClearTile_Trees(TileIndex tile, byte flags)
{
	uint num;

	if (flags & DC_EXEC && _current_player < MAX_PLAYERS) {
		Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
		if (t != NULL)
			ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM);
	}

	num = GetTreeCount(tile) + 1;
	if (IS_INT_INSIDE(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;

	if (flags & DC_EXEC) DoClearSquare(tile);

	return num * _price.remove_trees;
}

static void GetAcceptedCargo_Trees(TileIndex tile, AcceptedCargo ac)
{
	/* not used */
}

static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
{
	TreeType tt = GetTreeType(tile);

	if (IS_INT_INSIDE(tt, TREE_RAINFOREST, TREE_CACTUS)) {
		td->str = STR_280F_RAINFOREST;
	} else if (tt == TREE_CACTUS) {
		td->str = STR_2810_CACTUS_PLANTS;
	} else {
		td->str = STR_280E_TREES;
	}

	td->owner = GetTileOwner(tile);
}

static void AnimateTile_Trees(TileIndex tile)
{
	/* not used */
}

static void TileLoopTreesDesert(TileIndex tile)
{
	switch (GetTropicZone(tile)) {
		case TROPICZONE_DESERT:
			if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
				SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
				MarkTileDirtyByTile(tile);
			}
			break;

		case TROPICZONE_RAINFOREST: {
			static const SoundFx forest_sounds[] = {
				SND_42_LOON_BIRD,
				SND_43_LION,
				SND_44_MONKEYS,
				SND_48_DISTANT_BIRD
			};
			uint32 r = Random();

			if (CHANCE16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
			break;
		}

		default: break;
	}
}

static void TileLoopTreesAlps(TileIndex tile)
{
	int k = GetTileZ(tile) - _opt.snow_line;

	if (k < -TILE_HEIGHT) {
		if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) return;
		SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 0);
	} else {
		uint density = min((uint)(k + TILE_HEIGHT) / TILE_HEIGHT, 3);

		if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT ||
				GetTreeDensity(tile) != density) {
			SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, density);
		} else {
			if (GetTreeDensity(tile) == 3) {
				uint32 r = Random();
				if (CHANCE16I(1, 200, r)) {
					SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
				}
			}
			return;
		}
	}
	MarkTileDirtyByTile(tile);
}

static void TileLoop_Trees(TileIndex tile)
{
	static const TileIndexDiffC _tileloop_trees_dir[] = {
		{-1, -1},
		{ 0, -1},
		{ 1, -1},
		{-1,  0},
		{ 1,  0},
		{-1,  1},
		{ 0,  1},
		{ 1,  1}
	};

	switch (_opt.landscape) {
		case LT_DESERT: TileLoopTreesDesert(tile); break;
		case LT_HILLY:  TileLoopTreesAlps(tile);   break;
	}

	TileLoopClearHelper(tile);

	if (GetTreeCounter(tile) < 15) {
		AddTreeCounter(tile, 1);
		return;
	}
	SetTreeCounter(tile, 0);

	switch (GetTreeGrowth(tile)) {
		case 3: /* regular sized tree */
			if (_opt.landscape == LT_DESERT &&
					GetTreeType(tile) != TREE_CACTUS &&
					GetTropicZone(tile) == TROPICZONE_DESERT) {
				AddTreeGrowth(tile, 1);
			} else {
				switch (GB(Random(), 0, 3)) {
					case 0: /* start destructing */
						AddTreeGrowth(tile, 1);
						break;

					case 1: /* add a tree */
						if (GetTreeCount(tile) < 3) {
							AddTreeCount(tile, 1);
							SetTreeGrowth(tile, 0);
							break;
						}
						/* FALL THROUGH */

					case 2: { /* add a neighbouring tree */
						TreeType treetype = GetTreeType(tile);

						tile += ToTileIndexDiff(_tileloop_trees_dir[Random() & 7]);

						if (!IsTileType(tile, MP_CLEAR)) return;

						switch (GetClearGround(tile)) {
							case CLEAR_GRASS:
								if (GetClearDensity(tile) != 3) return;
								MakeTree(tile, treetype, 0, 0, TREE_GROUND_GRASS, 0);
								break;

							case CLEAR_ROUGH: MakeTree(tile, treetype, 0, 0, TREE_GROUND_ROUGH, 0); break;
							case CLEAR_SNOW:  MakeTree(tile, treetype, 0, 0, TREE_GROUND_SNOW_DESERT, GetClearDensity(tile)); break;
							default: return;
						}
						break;
					}

					default:
						return;
				}
			}
			break;

		case 6: /* final stage of tree destruction */
			if (GetTreeCount(tile) > 0) {
				/* more than one tree, delete it */
				AddTreeCount(tile, -1);
				SetTreeGrowth(tile, 3);
			} else {
				/* just one tree, change type into MP_CLEAR */
				switch (GetTreeGround(tile)) {
					case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, 3); break;
					case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
					default: MakeClear(tile, CLEAR_SNOW, GetTreeDensity(tile)); break;
				}
			}
			break;

		default:
			AddTreeGrowth(tile, 1);
			break;
	}

	MarkTileDirtyByTile(tile);
}

void OnTick_Trees(void)
{
	uint32 r;
	TileIndex tile;
	ClearGround ct;
	TreeType tree;

	/* place a tree at a random rainforest spot */
	if (_opt.landscape == LT_DESERT &&
			(r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
			IsTileType(tile, MP_CLEAR) &&
			(ct = GetClearGround(tile), ct == CLEAR_GRASS || ct == CLEAR_ROUGH) &&
			(tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
		MakeTree(tile, tree, 0, 0, ct == CLEAR_ROUGH ? TREE_GROUND_ROUGH : TREE_GROUND_GRASS, 0);
	}

	// byte underflow
	if (--_trees_tick_ctr != 0) return;

	/* place a tree at a random spot */
	r = Random();
	tile = TILE_MASK(r);
	if (IsTileType(tile, MP_CLEAR) &&
			(ct = GetClearGround(tile), ct == CLEAR_GRASS || ct == CLEAR_ROUGH || ct == CLEAR_SNOW) &&
			(tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
		switch (ct) {
			case CLEAR_GRASS: MakeTree(tile, tree, 0, 0, TREE_GROUND_GRASS, 0); break;
			case CLEAR_ROUGH: MakeTree(tile, tree, 0, 0, TREE_GROUND_ROUGH, 0); break;
			default: MakeTree(tile, tree, 0, 0, TREE_GROUND_SNOW_DESERT, GetClearDensity(tile)); break;
		}
	}
}

static void ClickTile_Trees(TileIndex tile)
{
	/* not used */
}

static uint32 GetTileTrackStatus_Trees(TileIndex tile, TransportType mode)
{
	return 0;
}

static void ChangeTileOwner_Trees(TileIndex tile, PlayerID old_player, PlayerID new_player)
{
	/* not used */
}

void InitializeTrees(void)
{
	_trees_tick_ctr = 0;
}


const TileTypeProcs _tile_type_trees_procs = {
	DrawTile_Trees,						/* draw_tile_proc */
	GetSlopeZ_Trees,					/* get_slope_z_proc */
	ClearTile_Trees,					/* clear_tile_proc */
	GetAcceptedCargo_Trees,		/* get_accepted_cargo_proc */
	GetTileDesc_Trees,				/* get_tile_desc_proc */
	GetTileTrackStatus_Trees,	/* get_tile_track_status_proc */
	ClickTile_Trees,					/* click_tile_proc */
	AnimateTile_Trees,				/* animate_tile_proc */
	TileLoop_Trees,						/* tile_loop_clear */
	ChangeTileOwner_Trees,		/* change_tile_owner_clear */
	NULL,											/* get_produced_cargo_proc */
	NULL,											/* vehicle_enter_tile_proc */
	NULL,											/* vehicle_leave_tile_proc */
	GetSlopeTileh_Trees,			/* get_slope_tileh_proc */
};