src/terraform_cmd.cpp
author belugas
Tue, 04 Mar 2008 17:07:33 +0000
changeset 9168 a35d94d8501c
parent 8760 ce0891c412ce
child 9297 94a4efa5de6e
permissions -rw-r--r--
(svn r12337) -Change: update some documentation.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
8706
e76561c3dd66 (svn r11773) -Codechange: move some non-clear-land functions from clear_cmd.cpp to a more correct location.
rubidium
parents: 8692
diff changeset
     3
/** @file terraform_cmd.cpp Commands related to terraforming. */
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1878
diff changeset
     6
#include "openttd.h"
8706
e76561c3dd66 (svn r11773) -Codechange: move some non-clear-land functions from clear_cmd.cpp to a more correct location.
rubidium
parents: 8692
diff changeset
     7
#include "strings_type.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8604
diff changeset
     8
#include "command_func.h"
8706
e76561c3dd66 (svn r11773) -Codechange: move some non-clear-land functions from clear_cmd.cpp to a more correct location.
rubidium
parents: 8692
diff changeset
     9
#include "tile_map.h"
3156
028b6756b279 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3144
diff changeset
    10
#include "tunnel_map.h"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4815
diff changeset
    11
#include "bridge_map.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 2150
diff changeset
    12
#include "variables.h"
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8615
diff changeset
    13
#include "functions.h"
8634
5ffca02f9115 (svn r11700) -Codechange: reduce the amount of unnecessary includes.
rubidium
parents: 8627
diff changeset
    14
#include "economy_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8726
diff changeset
    16
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8726
diff changeset
    17
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    18
/*
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    19
 * In one terraforming command all four corners of a initial tile can be raised/lowered (though this is not available to the player).
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    20
 * The maximal amount of height modifications is archieved when raising a complete flat land from sea level to MAX_TILE_HEIGHT or vice versa.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    21
 * This affects all corners with a manhatten distance smaller than MAX_TILE_HEIGHT to one of the initial 4 corners.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    22
 * Their maximal amount is computed to 4 * \sum_{i=1}^{h_max} i  =  2 * h_max * (h_max + 1).
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    23
 */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    24
static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    25
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    26
/*
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    27
 * The maximal amount of affected tiles (i.e. the tiles that incident with one of the corners above, is computed similiar to
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    28
 * 1 + 4 * \sum_{i=1}^{h_max} (i+1)  =  1 + 2 * h_max + (h_max + 3).
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    29
 */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    30
static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3);
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    31
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    32
struct TerraformerHeightMod {
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    33
	TileIndex tile;   ///< Referenced tile.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    34
	byte height;      ///< New TileHeight (height of north corner) of the tile.
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    35
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    37
struct TerraformerState {
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    38
	int modheight_count;                                         ///< amount of entries in "modheight".
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    39
	int tile_table_count;                                        ///< amount of entries in "tile_table".
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    40
8041
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    41
	/**
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    42
	 * Dirty tiles, i.e.\ at least one corner changed.
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    43
	 *
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    44
	 * This array contains the tiles which are or will be marked as dirty.
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    45
	 *
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    46
	 * @ingroup dirty
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    47
	 */
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
    48
	TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE];
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    49
	TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE];  ///< Height modifications.
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    50
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
8706
e76561c3dd66 (svn r11773) -Codechange: move some non-clear-land functions from clear_cmd.cpp to a more correct location.
rubidium
parents: 8692
diff changeset
    52
TileIndex _terraform_err_tile;
e76561c3dd66 (svn r11773) -Codechange: move some non-clear-land functions from clear_cmd.cpp to a more correct location.
rubidium
parents: 8692
diff changeset
    53
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    54
/**
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    55
 * Gets the TileHeight (height of north corner) of a tile as of current terraforming progress.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    56
 *
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    57
 * @param ts TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    58
 * @param tile Tile.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    59
 * @return TileHeight.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    60
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
static int TerraformGetHeightOfTile(TerraformerState *ts, TileIndex tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
	TerraformerHeightMod *mod = ts->modheight;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
	int count;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    65
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    66
	for (count = ts->modheight_count; count != 0; count--, mod++) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
    67
		if (mod->tile == tile) return mod->height;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    70
	/* TileHeight unchanged so far, read value from map. */
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1035
diff changeset
    71
	return TileHeight(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
    74
/**
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    75
 * Stores the TileHeight (height of north corner) of a tile in a TerraformerState.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    76
 *
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    77
 * @param ts TerraformerState.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    78
 * @param tile Tile.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    79
 * @param height New TileHeight.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    80
 */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    81
static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    82
{
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    83
	/* Find tile in the "modheight" table.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    84
	 * Note: In a normal user-terraform command the tile will not be found in the "modheight" table.
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    85
	 *       But during house- or industry-construction multiple corners can be terraformed at once. */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    86
	TerraformerHeightMod *mod = ts->modheight;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    87
	int count = ts->modheight_count;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    88
	while ((count > 0) && (mod->tile != tile)) {
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    89
		mod++;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    90
		count--;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    91
	}
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    92
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    93
	/* New entry? */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    94
	if (count == 0) {
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    95
		assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    96
		ts->modheight_count++;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    97
	}
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    98
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
    99
	/* Finally store the new value */
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   100
	mod->tile = tile;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   101
	mod->height = (byte)height;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   102
}
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   103
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   104
/**
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   105
 * Adds a tile to the "tile_table" in a TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   106
 *
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   107
 * @param ts TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   108
 * @param tile Tile.
8041
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
   109
 * @ingroup dirty
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   110
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	TileIndex *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
	count = ts->tile_table_count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6683
diff changeset
   118
	for (t = ts->tile_table; count != 0; count--, t++) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   119
		if (*t == tile) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   122
	assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   123
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
	ts->tile_table[ts->tile_table_count++] = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   127
/**
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   128
 * Adds all tiles that incident with the north corner of a specific tile to the "tile_table" in a TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   129
 *
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   130
 * @param ts TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   131
 * @param tile Tile.
8041
63e760418a15 (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 8013
diff changeset
   132
 * @ingroup dirty
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   133
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
{
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   136
	TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   137
	TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   138
	TerraformAddDirtyTile(ts, tile + TileDiffXY(-1,  0));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	TerraformAddDirtyTile(ts, tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   142
/**
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   143
 * Terraform the north corner of a tile to a specific height.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   144
 *
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   145
 * @param ts TerraformerState.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   146
 * @param tile Tile.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   147
 * @param height Aimed height.
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   148
 * @param return Error code or cost.
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   149
 */
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   150
static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8706
diff changeset
   152
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   153
863
8d09f9331a80 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 856
diff changeset
   154
	assert(tile < MapSize());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   156
	/* Check range of destination height */
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   157
	if (height < 0) return_cmd_error(STR_1003_ALREADY_AT_SEA_LEVEL);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   158
	if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_1004_TOO_HIGH);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   159
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   160
	/*
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   161
	 * Check if the terraforming has any effect.
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   162
	 * This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.).
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   163
	 * In this case the terraforming should fail. (Don't know why.)
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   164
	 */
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   165
	if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   166
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   167
	/* Check "too close to edge of map" */
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   168
	uint x = TileX(tile);
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   169
	uint y = TileY(tile);
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   170
	if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   171
		/*
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   172
		 * Determine a sensible error tile
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   173
		 * Note: If x and y are both zero this will disable the error tile. (Tile 0 cannot be highlighted :( )
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   174
		 */
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   175
		if ((x == 1) && (y != 0)) x = 0;
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   176
		if ((y == 1) && (x != 0)) y = 0;
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   177
		_terraform_err_tile = TileXY(x, y);
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   178
		return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   179
	}
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   180
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   181
	/* Mark incident tiles, that are involved in the terraforming */
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   182
	TerraformAddDirtyTileAround(ts, tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   184
	/* Store the height modification */
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   185
	TerraformSetHeightOfTile(ts, tile, height);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   187
	/* Increment cost */
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   188
	total_cost.AddCost(_price.terraform);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   190
	/* Recurse to neighboured corners if height difference is larger than 1 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
	{
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   192
		const TileIndexDiffC *ttm;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   194
		static const TileIndexDiffC _terraform_tilepos[] = {
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   195
			{ 1,  0}, // move to tile in SE
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   196
			{-2,  0}, // undo last move, and move to tile in NW
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   197
			{ 1,  1}, // undo last move, and move to tile in SW
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   198
			{ 0, -2}  // undo last move, and move to tile in NE
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   199
		};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   201
		for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   202
			tile += ToTileIndexDiff(*ttm);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   204
			/* Get TileHeight of neighboured tile as of current terraform progress */
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   205
			int r = TerraformGetHeightOfTile(ts, tile);
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   206
			int height_diff = height - r;
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   207
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   208
			/* Is the height difference to the neighboured corner greater than 1? */
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   209
			if (abs(height_diff) > 1) {
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   210
				/* Terraform the neighboured corner. The resulting height difference should be 1. */
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   211
				height_diff += (height_diff < 0 ? 1 : -1);
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   212
				CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   213
				if (CmdFailed(cost)) return cost;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   214
				total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   219
	return total_cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1571
diff changeset
   222
/** Terraform land
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   223
 * @param tile tile to terraform
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   224
 * @param flags for this command type
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   225
 * @param p1 corners to terraform (SLOPE_xxx)
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   226
 * @param p2 direction; eg up (non-zero) or down (zero)
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   227
 * @return error or cost of terraforming
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7214
diff changeset
   229
CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
	TerraformerState ts;
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8706
diff changeset
   232
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   233
	int direction = (p2 != 0 ? 1 : -1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
	_terraform_err_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
	ts.modheight_count = ts.tile_table_count = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
1878
622765748231 (svn r2384) - Fix: Check selling land and setting player colour. Also an extra map-bounds check for terraforming; inspired by the monkey (and Tron :) )
Darkvater
parents: 1796
diff changeset
   239
	/* Make an extra check for map-bounds cause we add tiles to the originating tile */
2934
3b7eef9871f8 (svn r3490) -Fix: A bunch (10) of off-by-one errors when checking if a TileIndex points to a tile on the map
tron
parents: 2705
diff changeset
   240
	if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR;
1878
622765748231 (svn r2384) - Fix: Check selling land and setting player colour. Also an extra map-bounds check for terraforming; inspired by the monkey (and Tron :) )
Darkvater
parents: 1796
diff changeset
   241
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   242
	/* Compute the costs and the terraforming result in a model of the landscape */
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   243
	if ((p1 & SLOPE_W) != 0) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   244
		TileIndex t = tile + TileDiffXY(1, 0);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   245
		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   246
		if (CmdFailed(cost)) return cost;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   247
		total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   250
	if ((p1 & SLOPE_S) != 0) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   251
		TileIndex t = tile + TileDiffXY(1, 1);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   252
		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   253
		if (CmdFailed(cost)) return cost;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   254
		total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   257
	if ((p1 & SLOPE_E) != 0) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   258
		TileIndex t = tile + TileDiffXY(0, 1);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   259
		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   260
		if (CmdFailed(cost)) return cost;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   261
		total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   264
	if ((p1 & SLOPE_N) != 0) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   265
		TileIndex t = tile + TileDiffXY(0, 0);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   266
		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   267
		if (CmdFailed(cost)) return cost;
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   268
		total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   271
	/* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4815
diff changeset
   272
	{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
		int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
		TileIndex *ti = ts.tile_table;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   275
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1571
diff changeset
   276
		for (count = ts.tile_table_count; count != 0; count--, ti++) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   277
			TileIndex tile = *ti;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   279
			/* Find new heights of tile corners */
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   280
			uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   281
			uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   282
			uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   283
			uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   284
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   285
			/* Find min and max height of tile */
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   286
			uint z_min = min(min(z_N, z_W), min(z_S, z_E));
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   287
			uint z_max = max(max(z_N, z_W), max(z_S, z_E));
7214
ae8dcfa02ec8 (svn r9950) -Feature(tte): allow terraforming under bridges.
rubidium
parents: 7179
diff changeset
   288
7990
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   289
			/* Compute tile slope */
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   290
			uint tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   291
			if (z_W > z_min) tileh += SLOPE_W;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   292
			if (z_S > z_min) tileh += SLOPE_S;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   293
			if (z_E > z_min) tileh += SLOPE_E;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   294
			if (z_N > z_min) tileh += SLOPE_N;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   295
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   296
			/* Check if bridge would take damage */
7214
ae8dcfa02ec8 (svn r9950) -Feature(tte): allow terraforming under bridges.
rubidium
parents: 7179
diff changeset
   297
			if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
ae8dcfa02ec8 (svn r9950) -Feature(tte): allow terraforming under bridges.
rubidium
parents: 7179
diff changeset
   298
					GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   299
				_terraform_err_tile = tile; // highlight the tile under the bridge
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4815
diff changeset
   300
				return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4815
diff changeset
   301
			}
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   302
			/* Check if tunnel would take damage */
7214
ae8dcfa02ec8 (svn r9950) -Feature(tte): allow terraforming under bridges.
rubidium
parents: 7179
diff changeset
   303
			if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   304
				_terraform_err_tile = tile; // highlight the tile above the tunnel
7214
ae8dcfa02ec8 (svn r9950) -Feature(tte): allow terraforming under bridges.
rubidium
parents: 7179
diff changeset
   305
				return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   306
			}
7983
06d272a9618b (svn r10998) -Codechange: refactor of the "core" of the terraforming code to make it possible to push the tile type specific terraforming requirements out of the generic part of the terraforming code. Patch by frosch. For more information take a look at FS#1147.
rubidium
parents: 7976
diff changeset
   307
			/* Check tiletype-specific things, and add extra-cost */
8133
597f682ff48d (svn r11168) -Fix [FS#1256]: assert on terraforming of industries. Patch by frosch.
rubidium
parents: 8041
diff changeset
   308
			CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh);
7990
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   309
			if (CmdFailed(cost)) {
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   310
				_terraform_err_tile = tile;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   311
				return cost;
70039e33e893 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7983
diff changeset
   312
			}
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   313
			total_cost.AddCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
		/* change the height */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
			int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
			TerraformerHeightMod *mod;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
			mod = ts.modheight;
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1571
diff changeset
   324
			for (count = ts.modheight_count; count != 0; count--, mod++) {
1979
f4462d4e8e62 (svn r2485) Missed two uint -> TileIndex, thanks _Luca_
tron
parents: 1977
diff changeset
   325
				TileIndex til = mod->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   326
1059
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   327
				SetTileHeight(til, mod->height);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		/* finally mark the dirty tiles dirty */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
			int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
			TileIndex *ti = ts.tile_table;
1775
08ff0f12ccdc (svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
Darkvater
parents: 1571
diff changeset
   335
			for (count = ts.tile_table_count; count != 0; count--, ti++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
				MarkTileDirtyByTile(*ti);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   338
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
	}
8013
67c6bcc81914 (svn r11032) -Codechange: unmagicify some table sizes and removal of some unnecessary variables. Patch by frosch.
rubidium
parents: 8005
diff changeset
   340
	return total_cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   344
/** Levels a selected (rectangle) area of land
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   345
 * @param tile end tile of area-drag
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   346
 * @param flags for this command type
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   347
 * @param p1 start tile of area drag
8692
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   348
 * @param p2 height difference; eg raise (+1), lower (-1) or level (0)
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   349
 * @return  error or cost of terraforming
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7214
diff changeset
   351
CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
	int size_x, size_y;
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   354
	int ex;
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   355
	int ey;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
	int sx, sy;
8692
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   357
	uint h, oldh, curh;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   358
	CommandCost money;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7214
diff changeset
   359
	CommandCost ret;
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8706
diff changeset
   360
	CommandCost cost(EXPENSES_CONSTRUCTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
2934
3b7eef9871f8 (svn r3490) -Fix: A bunch (10) of off-by-one errors when checking if a TileIndex points to a tile on the map
tron
parents: 2705
diff changeset
   362
	if (p1 >= MapSize()) return CMD_ERROR;
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   363
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   364
	/* remember level height */
8692
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   365
	oldh = TileHeight(p1);
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   366
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   367
	/* compute new height */
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   368
	h = oldh + p2;
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   369
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   370
	/* Check range of destination height */
5408c091b204 (svn r11759) -Feature: Add drag-n-drop support to the raise/lower land tools. Land is raised/lowered at the start and the rest of the area levelled to match. Patch by Roujin.
peter1138
parents: 8640
diff changeset
   371
	if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
6449
e520244dc71e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6432
diff changeset
   373
	/* make sure sx,sy are smaller than ex,ey */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   374
	ex = TileX(tile);
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3447
diff changeset
   375
	ey = TileY(tile);
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   376
	sx = TileX(p1);
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   377
	sy = TileY(p1);
6432
8fb778a7f2d7 (svn r8841) -Fix
tron
parents: 5919
diff changeset
   378
	if (ex < sx) Swap(ex, sx);
8fb778a7f2d7 (svn r8841) -Fix
tron
parents: 5919
diff changeset
   379
	if (ey < sy) Swap(ey, sy);
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   380
	tile = TileXY(sx, sy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6683
diff changeset
   382
	size_x = ex - sx + 1;
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6683
diff changeset
   383
	size_y = ey - sy + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   385
	money.AddCost(GetAvailableMoneyForCommand());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   387
	BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) {
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1035
diff changeset
   388
		curh = TileHeight(tile2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
		while (curh != h) {
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   390
			ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   391
			if (CmdFailed(ret)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
			if (flags & DC_EXEC) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   394
				money.AddCost(-ret.GetCost());
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   395
				if (money.GetCost() < 0) {
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   396
					_additional_cash_required = ret.GetCost();
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   397
					return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
				}
7976
5d93386b748b (svn r10991) -Codechange: add documentation, enumification and some code simplifications to the terraforming code. Patch by frosch.
rubidium
parents: 7831
diff changeset
   399
				DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   402
			cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
			curh += (curh > h) ? -1 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
		}
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1782
diff changeset
   405
	} END_TILE_LOOP(tile2, size_x, size_y, tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   407
	return (cost.GetCost() == 0) ? CMD_ERROR : cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
}