Files @ r12648:24c912bd53f6
Branch filter:

Location: cpp/openttd-patchpack/source/src/unmovable_cmd.cpp - annotation

smatz
(svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
-Change [FS#1134]: subsidies aren't bound to stations after awarding anymore, they still apply to town or industry, no matter what station is used for loading and unloading. Awarded subsidies from older savegames are lost
-Change [NoAI]: due to these changes, AISubsidy::GetSource and AISubsidy::GetDestination now return STATION_INVALID for awarded subsidies
  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
r5584:545d748cc681
r5584:545d748cc681
r9111:983de9c5a848
r6423:c3b8b9853091
r5584:545d748cc681
r5584:545d748cc681
r6453:b0b56773284a
r8116:df67d3c5e4fd
r8224:194097dc7288
r10208:ef8fcc3dc4ca
r5584:545d748cc681
r5584:545d748cc681
r7202:73a0810e750e
r5584:545d748cc681
r5584:545d748cc681
r7582:dfefb1216e6f
r7849:cfd99cb69695
r8131:7a50db7be0ff
r8131:7a50db7be0ff
r8210:40db2b3d3085
r10208:ef8fcc3dc4ca
r8962:cfbdc736c8db
r10960:e97ebf9cf99b
r9126:35955a7b9d9e
r11010:14865490174c
r12648:24c912bd53f6
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r11052:3304bfe8a9d7
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r6484:7a9ab9f50ee4
r5584:545d748cc681
r11090:9276cea703d4
r5584:545d748cc681
r11917:612c11f7ab47
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r11009:2e1d764313f1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10555:6bae964dabbf
r10207:a1fc2f2a33db
r12648:24c912bd53f6
r12648:24c912bd53f6
r5584:545d748cc681
r5584:545d748cc681
r6423:c3b8b9853091
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10555:6bae964dabbf
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11009:2e1d764313f1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11090:9276cea703d4
r6423:c3b8b9853091
r5584:545d748cc681
r5584:545d748cc681
r6423:c3b8b9853091
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11090:9276cea703d4
r5584:545d748cc681
r11917:612c11f7ab47
r8230:33d57fce0ec2
r5584:545d748cc681
r6950:1a54b1afb12a
r6950:1a54b1afb12a
r5584:545d748cc681
r10555:6bae964dabbf
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8210:40db2b3d3085
r8210:40db2b3d3085
r10207:a1fc2f2a33db
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r11090:9276cea703d4
r8210:40db2b3d3085
r8230:33d57fce0ec2
r8210:40db2b3d3085
r10207:a1fc2f2a33db
r11725:57bc99fdc1bc
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r10207:a1fc2f2a33db
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r11010:14865490174c
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r10207:a1fc2f2a33db
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r11090:9276cea703d4
r8210:40db2b3d3085
r8210:40db2b3d3085
r10207:a1fc2f2a33db
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r8210:40db2b3d3085
r11010:14865490174c
r8210:40db2b3d3085
r8210:40db2b3d3085
r7549:c84f0cb9f86a
r7549:c84f0cb9f86a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12022:f3eef0751aff
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r5584:545d748cc681
r7335:93bf5f4b6ce4
r5584:545d748cc681
r5584:545d748cc681
r8806:1fc0d9e1c2bf
r8806:1fc0d9e1c2bf
r5584:545d748cc681
r8576:05dfb0d471cd
r8576:05dfb0d471cd
r7849:cfd99cb69695
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7549:c84f0cb9f86a
r7549:c84f0cb9f86a
r7549:c84f0cb9f86a
r5668:e62c2fbafb7b
r5584:545d748cc681
r8806:1fc0d9e1c2bf
r8806:1fc0d9e1c2bf
r11085:c87a330fb4c2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11085:c87a330fb4c2
r7601:2327d699b237
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10951:c8234203bc5f
r5584:545d748cc681
r7335:93bf5f4b6ce4
r5584:545d748cc681
r11085:c87a330fb4c2
r5584:545d748cc681
r10951:c8234203bc5f
r8571:ede148e241b1
r5584:545d748cc681
r8806:1fc0d9e1c2bf
r8806:1fc0d9e1c2bf
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r8570:8edf335fd1b4
r5584:545d748cc681
r5584:545d748cc681
r7333:94b25d4d956c
r7849:cfd99cb69695
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5587:034e5e185dc2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7335:93bf5f4b6ce4
r5584:545d748cc681
r7335:93bf5f4b6ce4
r5584:545d748cc681
r5584:545d748cc681
r11090:9276cea703d4
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r11725:57bc99fdc1bc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6423:c3b8b9853091
r12077:baf868e4baf0
r11725:57bc99fdc1bc
r5584:545d748cc681
r6257:cacff70f4f56
r11725:57bc99fdc1bc
r8461:0b4ecbdeaf7d
r6257:cacff70f4f56
r11917:612c11f7ab47
r6257:cacff70f4f56
r6257:cacff70f4f56
r6257:cacff70f4f56
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6950:1a54b1afb12a
r5584:545d748cc681
r5584:545d748cc681
r12253:9cc92893e2d0
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r9056:d759c828f7dc
r5584:545d748cc681
r6423:c3b8b9853091
r6423:c3b8b9853091
r12251:58d0d73149cb
r5584:545d748cc681
r6423:c3b8b9853091
r6423:c3b8b9853091
r6423:c3b8b9853091
r6423:c3b8b9853091
r12251:58d0d73149cb
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11010:14865490174c
r9322:3f83b0034795
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r6423:c3b8b9853091
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12648:24c912bd53f6
r5584:545d748cc681
r5584:545d748cc681
r6423:c3b8b9853091
r6423:c3b8b9853091
r6423:c3b8b9853091
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12648:24c912bd53f6
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8616:656db5986c9e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10532:23a12232250b
r5584:545d748cc681
r10532:23a12232250b
r10532:23a12232250b
r10532:23a12232250b
r10532:23a12232250b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r5584:545d748cc681
r12519:e52fcaadf579
r5584:545d748cc681
r12519:e52fcaadf579
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r9413:fcf267325763
r5584:545d748cc681
r5584:545d748cc681
r11053:7d976eea2243
r9413:fcf267325763
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r11092:8211360a7a54
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r11053:7d976eea2243
r5584:545d748cc681
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r7202:73a0810e750e
r5584:545d748cc681
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r11053:7d976eea2243
r5584:545d748cc681
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r5584:545d748cc681
r9056:d759c828f7dc
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r9056:d759c828f7dc
r11231:db031b23683b
r9056:d759c828f7dc
r9056:d759c828f7dc
r11229:0d2d21cc618d
r11229:0d2d21cc618d
r9056:d759c828f7dc
r9056:d759c828f7dc
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r8468:1874b30da9a2
r8468:1874b30da9a2
r11917:612c11f7ab47
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r8468:1874b30da9a2
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r8468:1874b30da9a2
r8468:1874b30da9a2
r8468:1874b30da9a2
r8468:1874b30da9a2
r8468:1874b30da9a2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11090:9276cea703d4
r7494:2e43e3339051
r7494:2e43e3339051
r7494:2e43e3339051
r7494:2e43e3339051
r7582:dfefb1216e6f
r8230:33d57fce0ec2
r7582:dfefb1216e6f
r7582:dfefb1216e6f
r7494:2e43e3339051
r7494:2e43e3339051
r7494:2e43e3339051
r5587:034e5e185dc2
r11363:6906c490a00e
r11363:6906c490a00e
r11363:6906c490a00e
r12234:fd1a494e7620
r11363:6906c490a00e
r11363:6906c490a00e
r11363:6906c490a00e
r12235:f6faf5ffa926
r11363:6906c490a00e
r11363:6906c490a00e
r12248:7acc3688a3f0
r11363:6906c490a00e
r11363:6906c490a00e
r11363:6906c490a00e
r5584:545d748cc681
/* $Id$ */

/** @file unmovable_cmd.cpp Handling of unmovable tiles. */

#include "stdafx.h"
#include "openttd.h"
#include "landscape.h"
#include "command_func.h"
#include "viewport_func.h"
#include "company_base.h"
#include "town.h"
#include "sprite.h"
#include "bridge_map.h"
#include "unmovable_map.h"
#include "genworld.h"
#include "autoslope.h"
#include "transparency.h"
#include "functions.h"
#include "window_func.h"
#include "vehicle_func.h"
#include "company_gui.h"
#include "economy_func.h"
#include "cheat_type.h"
#include "landscape_type.h"
#include "unmovable.h"
#include "cargopacket.h"

#include "table/strings.h"
#include "table/sprites.h"
#include "table/unmovable_land.h"

/**
 * Accessor for array _original_unmovable.
 * This will ensure at once : proper access and
 * not allowing modifications of it.
 * @param type of unmovable (which is the index in _original_unmovable)
 * @pre type < UNMOVABLE_MAX
 * @return a pointer to the corresponding unmovable spec
 **/
static inline const UnmovableSpec *GetUnmovableSpec(UnmovableType type)
{
	assert(type < UNMOVABLE_MAX);
	return &_original_unmovable[type];
}

/** Destroy a HQ.
 * During normal gameplay you can only implicitely destroy a HQ when you are
 * rebuilding it. Otherwise, only water can destroy it.
 * @param cid Company requesting the destruction of his HQ
 * @param flags docommand flags of calling function
 * @return cost of the operation
 */
static CommandCost DestroyCompanyHQ(CompanyID cid, DoCommandFlag flags)
{
	Company *c = Company::Get(cid);

	if (flags & DC_EXEC) {
		TileIndex t = c->location_of_HQ;

		DoClearSquare(t);
		DoClearSquare(t + TileDiffXY(0, 1));
		DoClearSquare(t + TileDiffXY(1, 0));
		DoClearSquare(t + TileDiffXY(1, 1));
		c->location_of_HQ = INVALID_TILE; // reset HQ position
		InvalidateWindow(WC_COMPANY, cid);

		CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, cid);
	}

	/* cost of relocating company is 1% of company value */
	return CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
}

void UpdateCompanyHQ(Company *c, uint score)
{
	byte val;
	TileIndex tile = c->location_of_HQ;

	if (tile == INVALID_TILE) return;

	(val = 0, score < 170) ||
	(val++, score < 350) ||
	(val++, score < 520) ||
	(val++, score < 720) ||
	(val++, true);

	EnlargeCompanyHQ(tile, val);

	MarkTileDirtyByTile(tile);
	MarkTileDirtyByTile(tile + TileDiffXY(0, 1));
	MarkTileDirtyByTile(tile + TileDiffXY(1, 0));
	MarkTileDirtyByTile(tile + TileDiffXY(1, 1));
}

extern CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, DoCommandFlag flags, uint invalid_dirs, StationID *station, bool check_clear = true);

/** Build or relocate the HQ. This depends if the HQ is already built or not
 * @param tile tile where the HQ will be built or relocated to
 * @param flags type of operation
 * @param p1 unused
 * @param p2 unused
 */
CommandCost CmdBuildCompanyHQ(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
	Company *c = Company::Get(_current_company);
	CommandCost cost(EXPENSES_PROPERTY);

	cost = CheckFlatLandBelow(tile, 2, 2, flags, 0, NULL);
	if (CmdFailed(cost)) return cost;

	if (c->location_of_HQ != INVALID_TILE) { // Moving HQ
		cost.AddCost(DestroyCompanyHQ(_current_company, flags));
	}

	if (flags & DC_EXEC) {
		int score = UpdateCompanyRatingAndValue(c, false);

		c->location_of_HQ = tile;

		MakeCompanyHQ(tile, _current_company);

		UpdateCompanyHQ(c, score);
		InvalidateWindow(WC_COMPANY, c->index);
	}

	return cost;
}

/** Purchase a land area. Actually you only purchase one tile, so
 * the name is a bit confusing ;p
 * @param tile the tile the company is purchasing
 * @param flags for this command type
 * @param p1 unused
 * @param p2 unused
 * @return error of cost of operation
 */
CommandCost CmdPurchaseLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
	CommandCost cost(EXPENSES_CONSTRUCTION);

	if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_company)) {
		return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
	}

	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
	if (CmdFailed(cost)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		MakeOwnedLand(tile, _current_company);
		MarkTileDirtyByTile(tile);
	}

	return cost.AddCost(GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetBuildingCost());
}

/** Sell a land area. Actually you only sell one tile, so
 * the name is a bit confusing ;p
 * @param tile the tile the company is selling
 * @param flags for this command type
 * @param p1 unused
 * @param p2 unused
 * @return error or cost of operation
 */
CommandCost CmdSellLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
	if (!IsOwnedLandTile(tile)) return CMD_ERROR;
	if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;

	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;

	if (flags & DC_EXEC) DoClearSquare(tile);

	return CommandCost(EXPENSES_CONSTRUCTION, - GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetRemovalCost());
}

static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh);

static void DrawTile_Unmovable(TileInfo *ti)
{

	switch (GetUnmovableType(ti->tile)) {
		default: NOT_REACHED();
		case UNMOVABLE_TRANSMITTER:
		case UNMOVABLE_LIGHTHOUSE: {
			const DrawTileSeqStruct *dtu = &_draw_tile_transmitterlighthouse_data[GetUnmovableType(ti->tile)];

			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
			DrawClearLandTile(ti, 2);

			if (IsInvisibilitySet(TO_STRUCTURES)) break;

			AddSortableSpriteToDraw(
				dtu->image.sprite, PAL_NONE, ti->x | dtu->delta_x, ti->y | dtu->delta_y,
				dtu->size_x, dtu->size_y, dtu->size_z, ti->z,
				IsTransparencySet(TO_STRUCTURES)
			);
			break;
		}

		case UNMOVABLE_STATUE:
			/* This should prevent statues from sinking into the ground when on a slope. */
			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, GetFoundation_Unmovable(ti->tile, ti->tileh));

			DrawGroundSprite(SPR_CONCRETE_GROUND, PAL_NONE);

			if (IsInvisibilitySet(TO_STRUCTURES)) break;

			AddSortableSpriteToDraw(SPR_STATUE_COMPANY, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)), ti->x, ti->y, 16, 16, 25, ti->z, IsTransparencySet(TO_STRUCTURES));
			break;

		case UNMOVABLE_OWNED_LAND:
			DrawClearLandTile(ti, 0);

			AddSortableSpriteToDraw(
				SPR_BOUGHT_LAND, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)),
				ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, BB_HEIGHT_UNDER_BRIDGE, GetSlopeZ(ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2)
			);
			DrawBridgeMiddle(ti);
			break;

		case UNMOVABLE_HQ:{
			assert(IsCompanyHQ(ti->tile));
			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);

			SpriteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));

			const DrawTileSprites *t = &_unmovable_display_datas[GetCompanyHQSize(ti->tile) << 2 | GetCompanyHQSection(ti->tile)];
			DrawGroundSprite(t->ground.sprite, palette);

			if (IsInvisibilitySet(TO_STRUCTURES)) break;

			const DrawTileSeqStruct *dtss;
			foreach_draw_tile_seq(dtss, t->seq) {
				AddSortableSpriteToDraw(
					dtss->image.sprite, palette,
					ti->x + dtss->delta_x, ti->y + dtss->delta_y,
					dtss->size_x, dtss->size_y,
					dtss->size_z, ti->z + dtss->delta_z,
					IsTransparencySet(TO_STRUCTURES)
				);
			}
			break;
		}
	}
}

static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
{
	if (IsOwnedLand(tile)) {
		uint z;
		Slope tileh = GetTileSlope(tile, &z);

		return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
	} else {
		return GetTileMaxZ(tile);
	}
}

static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh)
{
	return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
}

static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
{
	if (IsCompanyHQ(tile)) {
		if (_current_company == OWNER_WATER) {
			return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
		} else {
			return_cmd_error(flags & DC_AUTO ? STR_ERROR_COMPANY_HEADQUARTERS_IN : INVALID_STRING_ID);
		}
	}

	if (IsOwnedLand(tile)) {
		return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
	}

	/* checks if you're allowed to remove unmovable things */
	if (_game_mode != GM_EDITOR && _current_company != OWNER_WATER && ((flags & DC_AUTO) || !_cheats.magic_bulldozer.value) )
		return_cmd_error(flags & DC_AUTO ? STR_ERROR_OBJECT_IN_THE_WAY : INVALID_STRING_ID);

	if (IsStatue(tile)) {
		if (flags & DC_AUTO) return_cmd_error(STR_ERROR_OBJECT_IN_THE_WAY);

		TownID town = GetStatueTownID(tile);
		ClrBit(Town::Get(town)->statues, GetTileOwner(tile));
		InvalidateWindow(WC_TOWN_AUTHORITY, town);
	}

	if (flags & DC_EXEC) {
		DoClearSquare(tile);
	}

	return CommandCost();
}

static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray &acceptance)
{
	if (!IsCompanyHQ(tile)) return;

	/* HQ accepts passenger and mail; but we have to divide the values
	 * between 4 tiles it occupies! */

	/* HQ level (depends on company performance) in the range 1..5. */
	uint level = GetCompanyHQSize(tile) + 1;

	/* Top town building generates 10, so to make HQ interesting, the top
	 * type makes 20. */
	acceptance[CT_PASSENGERS] += max(1U, level);

	/* Top town building generates 4, HQ can make up to 8. The
	 * proportion passengers:mail is different because such a huge
	 * commercial building generates unusually high amount of mail
	 * correspondence per physical visitor. */
	acceptance[CT_MAIL] += max(1U, level / 2);
}


static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
{
	td->str = GetUnmovableSpec(GetUnmovableType(tile))->name;
	td->owner[0] = GetTileOwner(tile);
}

static void TileLoop_Unmovable(TileIndex tile)
{
	if (!IsCompanyHQ(tile)) return;

	/* HQ accepts passenger and mail; but we have to divide the values
	 * between 4 tiles it occupies! */

	/* HQ level (depends on company performance) in the range 1..5. */
	uint level = GetCompanyHQSize(tile) + 1;
	assert(level < 6);

	uint r = Random();
	/* Top town buildings generate 250, so the top HQ type makes 256. */
	if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
		uint amt = GB(r, 0, 8) / 8 / 4 + 1;
		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
		MoveGoodsToStation(tile, 2, 2, CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile));
	}

	/* Top town building generates 90, HQ can make up to 196. The
	 * proportion passengers:mail is about the same as in the acceptance
	 * equations. */
	if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
		uint amt = GB(r, 8, 8) / 8 / 4 + 1;
		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
		MoveGoodsToStation(tile, 2, 2, CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile));
	}
}


static TrackStatus GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
{
	return 0;
}

static bool ClickTile_Unmovable(TileIndex tile)
{
	if (!IsCompanyHQ(tile)) return false;

	ShowCompany(GetTileOwner(tile));
	return true;
}


/* checks, if a radio tower is within a 9x9 tile square around tile */
static bool IsRadioTowerNearby(TileIndex tile)
{
	TileIndex tile_s = tile - TileDiffXY(min(TileX(tile), 4U), min(TileY(tile), 4U));
	uint w = min(TileX(tile), 4U) + 1 + min(MapMaxX() - TileX(tile), 4U);
	uint h = min(TileY(tile), 4U) + 1 + min(MapMaxY() - TileY(tile), 4U);

	TILE_LOOP(tile, w, h, tile_s) {
		if (IsTransmitterTile(tile)) return true;
	}

	return false;
}

void GenerateUnmovables()
{
	if (_settings_game.game_creation.landscape == LT_TOYLAND) return;

	/* add radio tower */
	int radiotower_to_build = ScaleByMapSize(15); // maximum number of radio towers on the map
	int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);

	/* Scale the amount of lighthouses with the amount of land at the borders. */
	if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
		uint num_water_tiles = 0;
		for (uint x = 0; x < MapMaxX(); x++) {
			if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
			if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
		}
		for (uint y = 1; y < MapMaxY() - 1; y++) {
			if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
			if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
		}
		/* The -6 is because the top borders are MP_VOID (-2) and all corners
		 * are counted twice (-4). */
		lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
	}

	SetGeneratingWorldProgress(GWP_UNMOVABLE, radiotower_to_build + lighthouses_to_build);

	for (uint i = ScaleByMapSize(1000); i != 0; i--) {
		TileIndex tile = RandomTile();

		uint h;
		if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4 && !IsBridgeAbove(tile)) {
			if (IsRadioTowerNearby(tile)) continue;

			MakeTransmitter(tile);
			IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
			if (--radiotower_to_build == 0) break;
		}
	}

	/* add lighthouses */
	uint maxx = MapMaxX();
	uint maxy = MapMaxY();
	for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0; loop_count++) {
		uint r = Random();

		/* Scatter the lighthouses more evenly around the perimeter */
		int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
		DiagDirection dir;
		for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
			perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
		}

		TileIndex tile;
		switch (dir) {
			default:
			case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
			case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
			case DIAGDIR_SW: tile = TileXY(1,        r % maxy); break;
			case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
		}

		/* Only build lighthouses at tiles where the border is sea. */
		if (!IsTileType(tile, MP_WATER)) continue;

		for (int j = 0; j < 19; j++) {
			uint h;
			if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2 && !IsBridgeAbove(tile)) {
				MakeLighthouse(tile);
				IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
				lighthouses_to_build--;
				assert(tile < MapSize());
				break;
			}
			tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
			if (tile == INVALID_TILE) break;
		}
	}
}

static void ChangeTileOwner_Unmovable(TileIndex tile, Owner old_owner, Owner new_owner)
{
	if (!IsTileOwner(tile, old_owner)) return;

	if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
		SetTileOwner(tile, new_owner);
	} else if (IsStatueTile(tile)) {
		TownID town = GetStatueTownID(tile);
		Town *t = Town::Get(town);
		ClrBit(t->statues, old_owner);
		if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
			/* Transfer ownership to the new company */
			SetBit(t->statues, new_owner);
			SetTileOwner(tile, new_owner);
		} else {
			DoClearSquare(tile);
		}

		InvalidateWindow(WC_TOWN_AUTHORITY, town);
	} else {
		DoClearSquare(tile);
	}
}

static CommandCost TerraformTile_Unmovable(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
{
	/* Owned land remains unsold */
	if (IsOwnedLand(tile) && CheckTileOwnership(tile)) return CommandCost();

	if (AutoslopeEnabled() && (IsStatue(tile) || IsCompanyHQ(tile))) {
		if (!IsSteepSlope(tileh_new) && (z_new + GetSlopeMaxZ(tileh_new) == GetTileMaxZ(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
	}

	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
}

extern const TileTypeProcs _tile_type_unmovable_procs = {
	DrawTile_Unmovable,             // draw_tile_proc
	GetSlopeZ_Unmovable,            // get_slope_z_proc
	ClearTile_Unmovable,            // clear_tile_proc
	AddAcceptedCargo_Unmovable,     // add_accepted_cargo_proc
	GetTileDesc_Unmovable,          // get_tile_desc_proc
	GetTileTrackStatus_Unmovable,   // get_tile_track_status_proc
	ClickTile_Unmovable,            // click_tile_proc
	NULL,                           // animate_tile_proc
	TileLoop_Unmovable,             // tile_loop_clear
	ChangeTileOwner_Unmovable,      // change_tile_owner_clear
	NULL,                           // add_produced_cargo_proc
	NULL,                           // vehicle_enter_tile_proc
	GetFoundation_Unmovable,        // get_foundation_proc
	TerraformTile_Unmovable,        // terraform_tile_proc
};