3 /** @file clear_cmd.cpp */ |
3 /** @file clear_cmd.cpp */ |
4 |
4 |
5 #include "stdafx.h" |
5 #include "stdafx.h" |
6 #include "openttd.h" |
6 #include "openttd.h" |
7 #include "clear_map.h" |
7 #include "clear_map.h" |
8 #include "rail_map.h" |
|
9 #include "table/strings.h" |
8 #include "table/strings.h" |
10 #include "functions.h" |
|
11 #include "map.h" |
|
12 #include "player.h" |
9 #include "player.h" |
13 #include "tile.h" |
10 #include "command_func.h" |
14 #include "viewport.h" |
|
15 #include "command.h" |
|
16 #include "tunnel_map.h" |
|
17 #include "bridge_map.h" |
|
18 #include "bridge.h" |
11 #include "bridge.h" |
19 #include "landscape.h" |
12 #include "landscape.h" |
20 #include "variables.h" |
13 #include "variables.h" |
21 #include "table/sprites.h" |
14 #include "table/sprites.h" |
22 #include "unmovable_map.h" |
15 #include "unmovable_map.h" |
23 #include "genworld.h" |
16 #include "genworld.h" |
24 #include "industry.h" |
17 #include "industry.h" |
25 #include "water_map.h" |
18 #include "water_map.h" |
26 |
19 #include "tile_cmd.h" |
27 /* |
20 #include "functions.h" |
28 * In one terraforming command all four corners of a initial tile can be raised/lowered (though this is not available to the player). |
21 #include "economy_func.h" |
29 * The maximal amount of height modifications is archieved when raising a complete flat land from sea level to MAX_TILE_HEIGHT or vice versa. |
22 #include "table/clear_land.h" |
30 * This affects all corners with a manhatten distance smaller than MAX_TILE_HEIGHT to one of the initial 4 corners. |
23 #include "viewport_func.h" |
31 * Their maximal amount is computed to 4 * \sum_{i=1}^{h_max} i = 2 * h_max * (h_max + 1). |
|
32 */ |
|
33 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1); |
|
34 |
|
35 /* |
|
36 * The maximal amount of affected tiles (i.e. the tiles that incident with one of the corners above, is computed similiar to |
|
37 * 1 + 4 * \sum_{i=1}^{h_max} (i+1) = 1 + 2 * h_max + (h_max + 3). |
|
38 */ |
|
39 static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3); |
|
40 |
|
41 struct TerraformerHeightMod { |
|
42 TileIndex tile; ///< Referenced tile. |
|
43 byte height; ///< New TileHeight (height of north corner) of the tile. |
|
44 }; |
|
45 |
|
46 struct TerraformerState { |
|
47 int modheight_count; ///< amount of entries in "modheight". |
|
48 int tile_table_count; ///< amount of entries in "tile_table". |
|
49 |
|
50 /** |
|
51 * Dirty tiles, i.e.\ at least one corner changed. |
|
52 * |
|
53 * This array contains the tiles which are or will be marked as dirty. |
|
54 * |
|
55 * @ingroup dirty |
|
56 */ |
|
57 TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE]; |
|
58 TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE]; ///< Height modifications. |
|
59 }; |
|
60 |
|
61 /** |
|
62 * Gets the TileHeight (height of north corner) of a tile as of current terraforming progress. |
|
63 * |
|
64 * @param ts TerraformerState. |
|
65 * @param tile Tile. |
|
66 * @return TileHeight. |
|
67 */ |
|
68 static int TerraformGetHeightOfTile(TerraformerState *ts, TileIndex tile) |
|
69 { |
|
70 TerraformerHeightMod *mod = ts->modheight; |
|
71 int count; |
|
72 |
|
73 for (count = ts->modheight_count; count != 0; count--, mod++) { |
|
74 if (mod->tile == tile) return mod->height; |
|
75 } |
|
76 |
|
77 /* TileHeight unchanged so far, read value from map. */ |
|
78 return TileHeight(tile); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Stores the TileHeight (height of north corner) of a tile in a TerraformerState. |
|
83 * |
|
84 * @param ts TerraformerState. |
|
85 * @param tile Tile. |
|
86 * @param height New TileHeight. |
|
87 */ |
|
88 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height) |
|
89 { |
|
90 /* Find tile in the "modheight" table. |
|
91 * Note: In a normal user-terraform command the tile will not be found in the "modheight" table. |
|
92 * But during house- or industry-construction multiple corners can be terraformed at once. */ |
|
93 TerraformerHeightMod *mod = ts->modheight; |
|
94 int count = ts->modheight_count; |
|
95 while ((count > 0) && (mod->tile != tile)) { |
|
96 mod++; |
|
97 count--; |
|
98 } |
|
99 |
|
100 /* New entry? */ |
|
101 if (count == 0) { |
|
102 assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE); |
|
103 ts->modheight_count++; |
|
104 } |
|
105 |
|
106 /* Finally store the new value */ |
|
107 mod->tile = tile; |
|
108 mod->height = (byte)height; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Adds a tile to the "tile_table" in a TerraformerState. |
|
113 * |
|
114 * @param ts TerraformerState. |
|
115 * @param tile Tile. |
|
116 * @ingroup dirty |
|
117 */ |
|
118 static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile) |
|
119 { |
|
120 int count; |
|
121 TileIndex *t; |
|
122 |
|
123 count = ts->tile_table_count; |
|
124 |
|
125 for (t = ts->tile_table; count != 0; count--, t++) { |
|
126 if (*t == tile) return; |
|
127 } |
|
128 |
|
129 assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE); |
|
130 |
|
131 ts->tile_table[ts->tile_table_count++] = tile; |
|
132 } |
|
133 |
|
134 /** |
|
135 * Adds all tiles that incident with the north corner of a specific tile to the "tile_table" in a TerraformerState. |
|
136 * |
|
137 * @param ts TerraformerState. |
|
138 * @param tile Tile. |
|
139 * @ingroup dirty |
|
140 */ |
|
141 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile) |
|
142 { |
|
143 TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1)); |
|
144 TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1)); |
|
145 TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, 0)); |
|
146 TerraformAddDirtyTile(ts, tile); |
|
147 } |
|
148 |
|
149 /** |
|
150 * Terraform the north corner of a tile to a specific height. |
|
151 * |
|
152 * @param ts TerraformerState. |
|
153 * @param tile Tile. |
|
154 * @param height Aimed height. |
|
155 * @param return Error code or cost. |
|
156 */ |
|
157 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height) |
|
158 { |
|
159 CommandCost total_cost = CommandCost(); |
|
160 |
|
161 assert(tile < MapSize()); |
|
162 |
|
163 /* Check range of destination height */ |
|
164 if (height < 0) return_cmd_error(STR_1003_ALREADY_AT_SEA_LEVEL); |
|
165 if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_1004_TOO_HIGH); |
|
166 |
|
167 /* |
|
168 * Check if the terraforming has any effect. |
|
169 * This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.). |
|
170 * In this case the terraforming should fail. (Don't know why.) |
|
171 */ |
|
172 if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR; |
|
173 |
|
174 /* Check "too close to edge of map" */ |
|
175 uint x = TileX(tile); |
|
176 uint y = TileY(tile); |
|
177 if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) { |
|
178 /* |
|
179 * Determine a sensible error tile |
|
180 * Note: If x and y are both zero this will disable the error tile. (Tile 0 cannot be highlighted :( ) |
|
181 */ |
|
182 if ((x == 1) && (y != 0)) x = 0; |
|
183 if ((y == 1) && (x != 0)) y = 0; |
|
184 _terraform_err_tile = TileXY(x, y); |
|
185 return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP); |
|
186 } |
|
187 |
|
188 /* Mark incident tiles, that are involved in the terraforming */ |
|
189 TerraformAddDirtyTileAround(ts, tile); |
|
190 |
|
191 /* Store the height modification */ |
|
192 TerraformSetHeightOfTile(ts, tile, height); |
|
193 |
|
194 /* Increment cost */ |
|
195 total_cost.AddCost(_price.terraform); |
|
196 |
|
197 /* Recurse to neighboured corners if height difference is larger than 1 */ |
|
198 { |
|
199 const TileIndexDiffC *ttm; |
|
200 |
|
201 static const TileIndexDiffC _terraform_tilepos[] = { |
|
202 { 1, 0}, // move to tile in SE |
|
203 {-2, 0}, // undo last move, and move to tile in NW |
|
204 { 1, 1}, // undo last move, and move to tile in SW |
|
205 { 0, -2} // undo last move, and move to tile in NE |
|
206 }; |
|
207 |
|
208 for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) { |
|
209 tile += ToTileIndexDiff(*ttm); |
|
210 |
|
211 /* Get TileHeight of neighboured tile as of current terraform progress */ |
|
212 int r = TerraformGetHeightOfTile(ts, tile); |
|
213 int height_diff = height - r; |
|
214 |
|
215 /* Is the height difference to the neighboured corner greater than 1? */ |
|
216 if (abs(height_diff) > 1) { |
|
217 /* Terraform the neighboured corner. The resulting height difference should be 1. */ |
|
218 height_diff += (height_diff < 0 ? 1 : -1); |
|
219 CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff); |
|
220 if (CmdFailed(cost)) return cost; |
|
221 total_cost.AddCost(cost); |
|
222 } |
|
223 } |
|
224 } |
|
225 |
|
226 return total_cost; |
|
227 } |
|
228 |
|
229 /** Terraform land |
|
230 * @param tile tile to terraform |
|
231 * @param flags for this command type |
|
232 * @param p1 corners to terraform (SLOPE_xxx) |
|
233 * @param p2 direction; eg up (non-zero) or down (zero) |
|
234 * @return error or cost of terraforming |
|
235 */ |
|
236 CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
237 { |
|
238 TerraformerState ts; |
|
239 CommandCost total_cost = CommandCost(); |
|
240 int direction = (p2 != 0 ? 1 : -1); |
|
241 |
|
242 SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
243 |
|
244 _terraform_err_tile = 0; |
|
245 |
|
246 ts.modheight_count = ts.tile_table_count = 0; |
|
247 |
|
248 /* Make an extra check for map-bounds cause we add tiles to the originating tile */ |
|
249 if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR; |
|
250 |
|
251 /* Compute the costs and the terraforming result in a model of the landscape */ |
|
252 if ((p1 & SLOPE_W) != 0) { |
|
253 TileIndex t = tile + TileDiffXY(1, 0); |
|
254 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction); |
|
255 if (CmdFailed(cost)) return cost; |
|
256 total_cost.AddCost(cost); |
|
257 } |
|
258 |
|
259 if ((p1 & SLOPE_S) != 0) { |
|
260 TileIndex t = tile + TileDiffXY(1, 1); |
|
261 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction); |
|
262 if (CmdFailed(cost)) return cost; |
|
263 total_cost.AddCost(cost); |
|
264 } |
|
265 |
|
266 if ((p1 & SLOPE_E) != 0) { |
|
267 TileIndex t = tile + TileDiffXY(0, 1); |
|
268 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction); |
|
269 if (CmdFailed(cost)) return cost; |
|
270 total_cost.AddCost(cost); |
|
271 } |
|
272 |
|
273 if ((p1 & SLOPE_N) != 0) { |
|
274 TileIndex t = tile + TileDiffXY(0, 0); |
|
275 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction); |
|
276 if (CmdFailed(cost)) return cost; |
|
277 total_cost.AddCost(cost); |
|
278 } |
|
279 |
|
280 /* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */ |
|
281 { |
|
282 int count; |
|
283 TileIndex *ti = ts.tile_table; |
|
284 |
|
285 for (count = ts.tile_table_count; count != 0; count--, ti++) { |
|
286 TileIndex tile = *ti; |
|
287 |
|
288 /* Find new heights of tile corners */ |
|
289 uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0)); |
|
290 uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0)); |
|
291 uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1)); |
|
292 uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1)); |
|
293 |
|
294 /* Find min and max height of tile */ |
|
295 uint z_min = min(min(z_N, z_W), min(z_S, z_E)); |
|
296 uint z_max = max(max(z_N, z_W), max(z_S, z_E)); |
|
297 |
|
298 /* Compute tile slope */ |
|
299 uint tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT); |
|
300 if (z_W > z_min) tileh += SLOPE_W; |
|
301 if (z_S > z_min) tileh += SLOPE_S; |
|
302 if (z_E > z_min) tileh += SLOPE_E; |
|
303 if (z_N > z_min) tileh += SLOPE_N; |
|
304 |
|
305 /* Check if bridge would take damage */ |
|
306 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) && |
|
307 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) { |
|
308 _terraform_err_tile = tile; // highlight the tile under the bridge |
|
309 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); |
|
310 } |
|
311 /* Check if tunnel would take damage */ |
|
312 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) { |
|
313 _terraform_err_tile = tile; // highlight the tile above the tunnel |
|
314 return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE); |
|
315 } |
|
316 /* Check tiletype-specific things, and add extra-cost */ |
|
317 CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh); |
|
318 if (CmdFailed(cost)) { |
|
319 _terraform_err_tile = tile; |
|
320 return cost; |
|
321 } |
|
322 total_cost.AddCost(cost); |
|
323 } |
|
324 } |
|
325 |
|
326 if (flags & DC_EXEC) { |
|
327 /* change the height */ |
|
328 { |
|
329 int count; |
|
330 TerraformerHeightMod *mod; |
|
331 |
|
332 mod = ts.modheight; |
|
333 for (count = ts.modheight_count; count != 0; count--, mod++) { |
|
334 TileIndex til = mod->tile; |
|
335 |
|
336 SetTileHeight(til, mod->height); |
|
337 } |
|
338 } |
|
339 |
|
340 /* finally mark the dirty tiles dirty */ |
|
341 { |
|
342 int count; |
|
343 TileIndex *ti = ts.tile_table; |
|
344 for (count = ts.tile_table_count; count != 0; count--, ti++) { |
|
345 MarkTileDirtyByTile(*ti); |
|
346 } |
|
347 } |
|
348 } |
|
349 return total_cost; |
|
350 } |
|
351 |
|
352 |
|
353 /** Levels a selected (rectangle) area of land |
|
354 * @param tile end tile of area-drag |
|
355 * @param flags for this command type |
|
356 * @param p1 start tile of area drag |
|
357 * @param p2 unused |
|
358 * @return error or cost of terraforming |
|
359 */ |
|
360 CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
361 { |
|
362 int size_x, size_y; |
|
363 int ex; |
|
364 int ey; |
|
365 int sx, sy; |
|
366 uint h, curh; |
|
367 CommandCost money; |
|
368 CommandCost ret; |
|
369 CommandCost cost; |
|
370 |
|
371 if (p1 >= MapSize()) return CMD_ERROR; |
|
372 |
|
373 SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
374 |
|
375 /* remember level height */ |
|
376 h = TileHeight(p1); |
|
377 |
|
378 /* make sure sx,sy are smaller than ex,ey */ |
|
379 ex = TileX(tile); |
|
380 ey = TileY(tile); |
|
381 sx = TileX(p1); |
|
382 sy = TileY(p1); |
|
383 if (ex < sx) Swap(ex, sx); |
|
384 if (ey < sy) Swap(ey, sy); |
|
385 tile = TileXY(sx, sy); |
|
386 |
|
387 size_x = ex - sx + 1; |
|
388 size_y = ey - sy + 1; |
|
389 |
|
390 money.AddCost(GetAvailableMoneyForCommand()); |
|
391 |
|
392 BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) { |
|
393 curh = TileHeight(tile2); |
|
394 while (curh != h) { |
|
395 ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND); |
|
396 if (CmdFailed(ret)) break; |
|
397 |
|
398 if (flags & DC_EXEC) { |
|
399 money.AddCost(-ret.GetCost()); |
|
400 if (money.GetCost() < 0) { |
|
401 _additional_cash_required = ret.GetCost(); |
|
402 return cost; |
|
403 } |
|
404 DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND); |
|
405 } |
|
406 |
|
407 cost.AddCost(ret); |
|
408 curh += (curh > h) ? -1 : 1; |
|
409 } |
|
410 } END_TILE_LOOP(tile2, size_x, size_y, tile) |
|
411 |
|
412 return (cost.GetCost() == 0) ? CMD_ERROR : cost; |
|
413 } |
|
414 |
|
415 /** Purchase a land area. Actually you only purchase one tile, so |
|
416 * the name is a bit confusing ;p |
|
417 * @param tile the tile the player is purchasing |
|
418 * @param flags for this command type |
|
419 * @param p1 unused |
|
420 * @param p2 unused |
|
421 * @return error of cost of operation |
|
422 */ |
|
423 CommandCost CmdPurchaseLandArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
424 { |
|
425 CommandCost cost; |
|
426 |
|
427 SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
428 |
|
429 if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_player)) { |
|
430 return_cmd_error(STR_5807_YOU_ALREADY_OWN_IT); |
|
431 } |
|
432 |
|
433 cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
|
434 if (CmdFailed(cost)) return CMD_ERROR; |
|
435 |
|
436 if (flags & DC_EXEC) { |
|
437 MakeOwnedLand(tile, _current_player); |
|
438 MarkTileDirtyByTile(tile); |
|
439 } |
|
440 |
|
441 return cost.AddCost(_price.purchase_land * 10); |
|
442 } |
|
443 |
|
444 |
24 |
445 static CommandCost ClearTile_Clear(TileIndex tile, byte flags) |
25 static CommandCost ClearTile_Clear(TileIndex tile, byte flags) |
446 { |
26 { |
447 static const Money* clear_price_table[] = { |
27 static const Money* clear_price_table[] = { |
448 &_price.clear_1, |
28 &_price.clear_grass, |
449 &_price.purchase_land, |
29 &_price.clear_roughland, |
450 &_price.clear_2, |
30 &_price.clear_rocks, |
451 &_price.clear_3, |
31 &_price.clear_fields, |
452 &_price.purchase_land, |
32 &_price.clear_roughland, |
453 &_price.purchase_land, |
33 &_price.clear_roughland, |
454 &_price.clear_2, // XXX unused? |
|
455 }; |
34 }; |
456 CommandCost price; |
35 CommandCost price(EXPENSES_CONSTRUCTION); |
457 |
36 |
458 if (!IsClearGround(tile, CLEAR_GRASS) || GetClearDensity(tile) != 0) { |
37 if (!IsClearGround(tile, CLEAR_GRASS) || GetClearDensity(tile) != 0) { |
459 price.AddCost(*clear_price_table[GetClearGround(tile)]); |
38 price.AddCost(*clear_price_table[GetClearGround(tile)]); |
460 } |
39 } |
461 |
40 |
462 if (flags & DC_EXEC) DoClearSquare(tile); |
41 if (flags & DC_EXEC) DoClearSquare(tile); |
463 |
42 |
464 return price; |
43 return price; |
465 } |
44 } |
466 |
|
467 /** Sell a land area. Actually you only sell one tile, so |
|
468 * the name is a bit confusing ;p |
|
469 * @param tile the tile the player is selling |
|
470 * @param flags for this command type |
|
471 * @param p1 unused |
|
472 * @param p2 unused |
|
473 * @return error or cost of operation |
|
474 */ |
|
475 CommandCost CmdSellLandArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
476 { |
|
477 SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
478 |
|
479 if (!IsOwnedLandTile(tile)) return CMD_ERROR; |
|
480 if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR; |
|
481 |
|
482 |
|
483 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; |
|
484 |
|
485 if (flags & DC_EXEC) DoClearSquare(tile); |
|
486 |
|
487 return CommandCost(- _price.purchase_land * 2); |
|
488 } |
|
489 |
|
490 |
|
491 #include "table/clear_land.h" |
|
492 |
|
493 |
45 |
494 void DrawClearLandTile(const TileInfo *ti, byte set) |
46 void DrawClearLandTile(const TileInfo *ti, byte set) |
495 { |
47 { |
496 DrawGroundSprite(SPR_FLAT_BARE_LAND + _tileh_to_sprite[ti->tileh] + set * 19, PAL_NONE); |
48 DrawGroundSprite(SPR_FLAT_BARE_LAND + _tileh_to_sprite[ti->tileh] + set * 19, PAL_NONE); |
497 } |
49 } |