tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file landscape.cpp Functions related to the landscape (slopes etc.). */ rubidium@10429: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" celestar@5573: #include "bridge_map.h" truelight@4300: #include "heightmap.h" tron@3144: #include "clear_map.h" tron@1349: #include "spritecache.h" truelight@0: #include rubidium@8721: #include "viewport_func.h" rubidium@8612: #include "command_func.h" maedhros@6669: #include "landscape.h" tron@2159: #include "variables.h" tron@3144: #include "void_map.h" tron@3111: #include "water_map.h" truelight@4300: #include "tgp.h" truelight@4300: #include "genworld.h" rubidium@8615: #include "tile_cmd.h" rubidium@8626: #include "core/alloc_func.hpp" belugas@8647: #include "fios.h" rubidium@8627: #include "window_func.h" rubidium@8627: #include "functions.h" rubidium@8636: #include "date_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8766: #include "settings_type.h" frosch@8876: #include "water.h" rubidium@10272: #include "effectvehicle_func.h" rubidium@10444: #include "landscape_type.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: truelight@183: extern const TileTypeProcs truelight@0: _tile_type_clear_procs, truelight@0: _tile_type_rail_procs, truelight@0: _tile_type_road_procs, truelight@0: _tile_type_town_procs, truelight@0: _tile_type_trees_procs, truelight@0: _tile_type_station_procs, truelight@0: _tile_type_water_procs, truelight@0: _tile_type_dummy_procs, truelight@0: _tile_type_industry_procs, truelight@0: _tile_type_tunnelbridge_procs, truelight@0: _tile_type_unmovable_procs; truelight@0: truelight@0: const TileTypeProcs * const _tile_type_procs[16] = { truelight@0: &_tile_type_clear_procs, truelight@0: &_tile_type_rail_procs, truelight@0: &_tile_type_road_procs, truelight@0: &_tile_type_town_procs, truelight@0: &_tile_type_trees_procs, truelight@0: &_tile_type_station_procs, truelight@0: &_tile_type_water_procs, truelight@0: &_tile_type_dummy_procs, truelight@0: &_tile_type_industry_procs, truelight@0: &_tile_type_tunnelbridge_procs, truelight@0: &_tile_type_unmovable_procs, truelight@0: }; truelight@0: truelight@0: /* landscape slope => sprite */ truelight@0: const byte _tileh_to_sprite[32] = { rubidium@4344: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, rubidium@4344: 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 17, 0, 15, 18, 0, truelight@0: }; truelight@0: rubidium@7831: SnowLine *_snow_line = NULL; tron@1958: rubidium@7831: /** rubidium@7831: * Applys a foundation to a slope. rubidium@7831: * rubidium@7831: * @pre Foundation and slope must be valid combined. rubidium@7831: * @param f The #Foundation. rubidium@7831: * @param s The #Slope to modify. rubidium@7831: * @return Increment to the tile Z coordinate. rubidium@7831: */ rubidium@7831: uint ApplyFoundationToSlope(Foundation f, Slope *s) rubidium@7831: { rubidium@7831: if (!IsFoundation(f)) return 0; rubidium@7831: rubidium@7831: if (IsLeveledFoundation(f)) { frosch@10945: uint dz = TILE_HEIGHT + (IsSteepSlope(*s) ? TILE_HEIGHT : 0); rubidium@7831: *s = SLOPE_FLAT; frosch@10945: return dz; rubidium@7831: } rubidium@7831: rubidium@8266: if (f != FOUNDATION_STEEP_BOTH && IsNonContinuousFoundation(f)) { rubidium@8266: *s = HalftileSlope(*s, GetHalftileFoundationCorner(f)); rubidium@8266: return 0; rubidium@8266: } rubidium@8266: rubidium@8266: if (IsSpecialRailFoundation(f)) { rubidium@8266: *s = SlopeWithThreeCornersRaised(OppositeCorner(GetRailFoundationCorner(f))); rubidium@8266: return 0; rubidium@8266: } rubidium@8266: rubidium@7831: uint dz = IsSteepSlope(*s) ? TILE_HEIGHT : 0; rubidium@8174: Corner highest_corner = GetHighestSlopeCorner(*s); rubidium@7831: rubidium@7831: switch (f) { rubidium@7831: case FOUNDATION_INCLINED_X: rubidium@8174: *s = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? SLOPE_SW : SLOPE_NE); rubidium@7831: break; rubidium@7831: rubidium@7831: case FOUNDATION_INCLINED_Y: rubidium@8174: *s = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? SLOPE_SE : SLOPE_NW); rubidium@7831: break; rubidium@7831: rubidium@7831: case FOUNDATION_STEEP_LOWER: rubidium@8174: *s = SlopeWithOneCornerRaised(highest_corner); rubidium@7831: break; rubidium@7831: rubidium@8266: case FOUNDATION_STEEP_BOTH: rubidium@8266: *s = HalftileSlope(SlopeWithOneCornerRaised(highest_corner), highest_corner); rubidium@7831: break; rubidium@7831: rubidium@7831: default: NOT_REACHED(); rubidium@7831: } rubidium@7831: return dz; rubidium@7831: } rubidium@7831: truelight@0: smatz@9313: /** smatz@9313: * Determines height at given coordinate of a slope smatz@9313: * @param x x coordinate smatz@9313: * @param y y coordinate smatz@9313: * @param corners slope to examine smatz@9313: * @return height of given point of given slope smatz@9313: */ tron@3636: uint GetPartialZ(int x, int y, Slope corners) truelight@0: { rubidium@8260: if (IsHalftileSlope(corners)) { rubidium@8260: switch (GetHalftileSlopeCorner(corners)) { rubidium@8260: case CORNER_W: rubidium@8260: if (x - y >= 0) return GetSlopeMaxZ(corners); rubidium@8260: break; rubidium@8260: rubidium@8260: case CORNER_S: rubidium@8260: if (x - (y ^ 0xF) >= 0) return GetSlopeMaxZ(corners); rubidium@8260: break; rubidium@8260: rubidium@8260: case CORNER_E: rubidium@8260: if (y - x >= 0) return GetSlopeMaxZ(corners); rubidium@8260: break; rubidium@8260: rubidium@8260: case CORNER_N: rubidium@8260: if ((y ^ 0xF) - x >= 0) return GetSlopeMaxZ(corners); rubidium@8260: break; rubidium@8260: rubidium@8260: default: NOT_REACHED(); rubidium@8260: } rubidium@8260: } rubidium@8260: truelight@0: int z = 0; truelight@0: frosch@8909: switch (RemoveHalftileSlope(corners)) { smatz@9313: case SLOPE_W: smatz@9313: if (x - y >= 0) { smatz@9313: z = (x - y) >> 1; smatz@9313: } smatz@9313: break; truelight@0: smatz@9313: case SLOPE_S: smatz@9313: y ^= 0xF; smatz@9313: if ((x - y) >= 0) { smatz@9313: z = (x - y) >> 1; smatz@9313: } smatz@9313: break; truelight@0: smatz@9313: case SLOPE_SW: smatz@9313: z = (x >> 1) + 1; smatz@9313: break; truelight@0: smatz@9313: case SLOPE_E: smatz@9313: if (y - x >= 0) { smatz@9313: z = (y - x) >> 1; smatz@9313: } smatz@9313: break; truelight@0: smatz@9313: case SLOPE_EW: smatz@9313: case SLOPE_NS: smatz@9313: case SLOPE_ELEVATED: smatz@9313: z = 4; smatz@9313: break; truelight@0: smatz@9313: case SLOPE_SE: smatz@9313: z = (y >> 1) + 1; smatz@9313: break; truelight@183: smatz@9313: case SLOPE_WSE: smatz@9313: z = 8; smatz@9313: y ^= 0xF; smatz@9313: if (x - y < 0) { smatz@9313: z += (x - y) >> 1; smatz@9313: } smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_N: smatz@9313: y ^= 0xF; smatz@9313: if (y - x >= 0) { smatz@9313: z = (y - x) >> 1; smatz@9313: } smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_NW: smatz@9313: z = (y ^ 0xF) >> 1; smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_NWS: smatz@9313: z = 8; smatz@9313: if (x - y < 0) { smatz@9313: z += (x - y) >> 1; smatz@9313: } smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_NE: smatz@9313: z = (x ^ 0xF) >> 1; smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_ENW: smatz@9313: z = 8; smatz@9313: y ^= 0xF; smatz@9313: if (y - x < 0) { smatz@9313: z += (y - x) >> 1; smatz@9313: } smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_SEN: smatz@9313: z = 8; smatz@9313: if (y - x < 0) { smatz@9313: z += (y - x) >> 1; smatz@9313: } smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_STEEP_S: smatz@9313: z = 1 + ((x + y) >> 1); smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_STEEP_W: smatz@9313: z = 1 + ((x + (y ^ 0xF)) >> 1); smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_STEEP_N: smatz@9313: z = 1 + (((x ^ 0xF) + (y ^ 0xF)) >> 1); smatz@9313: break; smatz@9313: smatz@9313: case SLOPE_STEEP_E: smatz@9313: z = 1 + (((x ^ 0xF) + y) >> 1); smatz@9313: break; tron@3636: tron@3636: default: break; truelight@0: } truelight@0: truelight@0: return z; truelight@0: } truelight@0: tron@2951: uint GetSlopeZ(int x, int y) truelight@0: { tron@4231: TileIndex tile = TileVirtXY(x, y); truelight@0: tron@4231: return _tile_type_procs[GetTileType(tile)]->get_slope_z_proc(tile, x, y); truelight@0: } truelight@0: rubidium@8224: /** rubidium@8266: * Determine the Z height of a corner relative to TileZ. rubidium@8266: * rubidium@8266: * @pre The slope must not be a halftile slope. rubidium@8266: * rubidium@8266: * @param tileh The slope. rubidium@8266: * @param corner The corner. rubidium@8266: * @return Z position of corner relative to TileZ. rubidium@8266: */ rubidium@8266: int GetSlopeZInCorner(Slope tileh, Corner corner) rubidium@8266: { rubidium@8266: assert(!IsHalftileSlope(tileh)); frosch@8909: return ((tileh & SlopeWithOneCornerRaised(corner)) != 0 ? TILE_HEIGHT : 0) + (tileh == SteepSlope(corner) ? TILE_HEIGHT : 0); rubidium@8266: } rubidium@8266: rubidium@8266: /** rubidium@8224: * Determine the Z height of the corners of a specific tile edge rubidium@8224: * rubidium@8260: * @note If a tile has a non-continuous halftile foundation, a corner can have different heights wrt. it's edges. rubidium@8260: * rubidium@8224: * @pre z1 and z2 must be initialized (typ. with TileZ). The corner heights just get added. rubidium@8224: * rubidium@8224: * @param tileh The slope of the tile. rubidium@8224: * @param edge The edge of interest. rubidium@8224: * @param z1 Gets incremented by the height of the first corner of the edge. (near corner wrt. the camera) rubidium@8224: * @param z2 Gets incremented by the height of the second corner of the edge. (far corner wrt. the camera) rubidium@8224: */ rubidium@8224: void GetSlopeZOnEdge(Slope tileh, DiagDirection edge, int *z1, int *z2) rubidium@8224: { rubidium@8224: static const Slope corners[4][4] = { rubidium@8224: /* corner | steep slope rubidium@8224: * z1 z2 | z1 z2 */ rubidium@8224: {SLOPE_E, SLOPE_N, SLOPE_STEEP_E, SLOPE_STEEP_N}, // DIAGDIR_NE, z1 = E, z2 = N rubidium@8224: {SLOPE_S, SLOPE_E, SLOPE_STEEP_S, SLOPE_STEEP_E}, // DIAGDIR_SE, z1 = S, z2 = E rubidium@8224: {SLOPE_S, SLOPE_W, SLOPE_STEEP_S, SLOPE_STEEP_W}, // DIAGDIR_SW, z1 = S, z2 = W rubidium@8224: {SLOPE_W, SLOPE_N, SLOPE_STEEP_W, SLOPE_STEEP_N}, // DIAGDIR_NW, z1 = W, z2 = N rubidium@8224: }; rubidium@8224: rubidium@8260: int halftile_test = (IsHalftileSlope(tileh) ? SlopeWithOneCornerRaised(GetHalftileSlopeCorner(tileh)) : 0); rubidium@8260: if (halftile_test == corners[edge][0]) *z2 += TILE_HEIGHT; // The slope is non-continuous in z2. z2 is on the upper side. rubidium@8260: if (halftile_test == corners[edge][1]) *z1 += TILE_HEIGHT; // The slope is non-continuous in z1. z1 is on the upper side. rubidium@8260: rubidium@8224: if ((tileh & corners[edge][0]) != 0) *z1 += TILE_HEIGHT; // z1 is raised rubidium@8224: if ((tileh & corners[edge][1]) != 0) *z2 += TILE_HEIGHT; // z2 is raised frosch@8909: if (RemoveHalftileSlope(tileh) == corners[edge][2]) *z1 += TILE_HEIGHT; // z1 is highest corner of a steep slope frosch@8909: if (RemoveHalftileSlope(tileh) == corners[edge][3]) *z2 += TILE_HEIGHT; // z2 is highest corner of a steep slope rubidium@8224: } tron@4061: frosch@8909: /** frosch@8909: * Get slope of a tile on top of a (possible) foundation frosch@8909: * If a tile does not have a foundation, the function returns the same as GetTileSlope. frosch@8909: * frosch@8909: * @param tile The tile of interest. frosch@8909: * @param z returns the z of the foundation slope. (Can be NULL, if not needed) frosch@8909: * @return The slope on top of the foundation. frosch@8909: */ frosch@8876: Slope GetFoundationSlope(TileIndex tile, uint* z) dominik@37: { tron@4061: Slope tileh = GetTileSlope(tile, z); rubidium@7831: Foundation f = _tile_type_procs[GetTileType(tile)]->get_foundation_proc(tile, tileh); frosch@8876: uint z_inc = ApplyFoundationToSlope(f, &tileh); frosch@8876: if (z != NULL) *z += z_inc; rubidium@7831: return tileh; tron@4061: } dominik@39: tron@4061: tron@4061: static bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here) tron@4061: { tron@4061: uint z; tron@4061: rubidium@8224: int z_W_here = z_here; rubidium@8224: int z_N_here = z_here; rubidium@8224: GetSlopeZOnEdge(slope_here, DIAGDIR_NW, &z_W_here, &z_N_here); rubidium@8224: rubidium@8224: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z); rubidium@8224: int z_W = z; rubidium@8224: int z_N = z; rubidium@8224: GetSlopeZOnEdge(slope, DIAGDIR_SE, &z_W, &z_N); rubidium@8224: rubidium@8224: return (z_N_here > z_N) || (z_W_here > z_W); dominik@37: } dominik@37: tron@4061: tron@4061: static bool HasFoundationNE(TileIndex tile, Slope slope_here, uint z_here) tron@4061: { tron@4061: uint z; tron@4061: rubidium@8224: int z_E_here = z_here; rubidium@8224: int z_N_here = z_here; rubidium@8224: GetSlopeZOnEdge(slope_here, DIAGDIR_NE, &z_E_here, &z_N_here); rubidium@8224: rubidium@8224: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z); rubidium@8224: int z_E = z; rubidium@8224: int z_N = z; rubidium@8224: GetSlopeZOnEdge(slope, DIAGDIR_SW, &z_E, &z_N); rubidium@8224: rubidium@8224: return (z_N_here > z_N) || (z_E_here > z_E); tron@4061: } tron@4061: tron@4061: rubidium@7831: void DrawFoundation(TileInfo *ti, Foundation f) truelight@0: { rubidium@7831: if (!IsFoundation(f)) return; rubidium@7831: rubidium@8266: /* Two part foundations must be drawn separately */ rubidium@8266: assert(f != FOUNDATION_STEEP_BOTH); rubidium@8266: rubidium@8263: uint sprite_block = 0; tron@4061: uint z; rubidium@7831: Slope slope = GetFoundationSlope(ti->tile, &z); dominik@39: rubidium@8263: /* Select the needed block of foundations sprites rubidium@8263: * Block 0: Walls at NW and NE edge rubidium@8263: * Block 1: Wall at NE edge rubidium@8263: * Block 2: Wall at NW edge rubidium@8263: * Block 3: No walls at NW or NE edge rubidium@8263: */ rubidium@8263: if (!HasFoundationNW(ti->tile, slope, z)) sprite_block += 1; rubidium@8263: if (!HasFoundationNE(ti->tile, slope, z)) sprite_block += 2; rubidium@8263: rubidium@8263: /* Use the original slope sprites if NW and NE borders should be visible */ rubidium@8263: SpriteID leveled_base = (sprite_block == 0 ? (int)SPR_FOUNDATION_BASE : (SPR_SLOPES_VIRTUAL_BASE + sprite_block * SPR_TRKFOUND_BLOCK_SIZE)); rubidium@8263: SpriteID inclined_base = SPR_SLOPES_VIRTUAL_BASE + SPR_SLOPES_INCLINED_OFFSET + sprite_block * SPR_TRKFOUND_BLOCK_SIZE; rubidium@8266: SpriteID halftile_base = SPR_HALFTILE_FOUNDATION_BASE + sprite_block * SPR_HALFTILE_BLOCK_SIZE; dominik@37: tron@4253: if (IsSteepSlope(ti->tileh)) { rubidium@8266: if (!IsNonContinuousFoundation(f)) { rubidium@8266: /* Lower part of foundation */ rubidium@8266: AddSortableSpriteToDraw( rubidium@8266: leveled_base + (ti->tileh & ~SLOPE_STEEP), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z rubidium@8266: ); rubidium@8266: } rubidium@7831: rubidium@8174: Corner highest_corner = GetHighestSlopeCorner(ti->tileh); rubidium@7831: ti->z += ApplyFoundationToSlope(f, &ti->tileh); rubidium@7831: rubidium@7831: if (IsInclinedFoundation(f)) { rubidium@7831: /* inclined foundation */ rubidium@7831: byte inclined = highest_corner * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@7831: rubidium@8263: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); frosch@10945: } else if (IsLeveledFoundation(f)) { frosch@10945: AddSortableSpriteToDraw(leveled_base + SlopeWithOneCornerRaised(highest_corner), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z - TILE_HEIGHT); frosch@10945: OffsetGroundSprite(31, 1); rubidium@8266: } else if (f == FOUNDATION_STEEP_LOWER) { belugas@6527: /* one corner raised */ tron@4253: OffsetGroundSprite(31, 1); rubidium@8266: } else { rubidium@8266: /* halftile foundation */ rubidium@8266: int x_bb = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? 8 : 0); rubidium@8266: int y_bb = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? 8 : 0); rubidium@8266: rubidium@8266: AddSortableSpriteToDraw(halftile_base + highest_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z + TILE_HEIGHT); rubidium@8266: OffsetGroundSprite(31, 9); tron@4253: } truelight@0: } else { rubidium@7831: if (IsLeveledFoundation(f)) { rubidium@8263: /* leveled foundation */ rubidium@8263: AddSortableSpriteToDraw(leveled_base + ti->tileh, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); tron@4253: OffsetGroundSprite(31, 1); rubidium@8266: } else if (IsNonContinuousFoundation(f)) { rubidium@8266: /* halftile foundation */ rubidium@8266: Corner halftile_corner = GetHalftileFoundationCorner(f); rubidium@8266: int x_bb = (((halftile_corner == CORNER_W) || (halftile_corner == CORNER_S)) ? 8 : 0); rubidium@8266: int y_bb = (((halftile_corner == CORNER_S) || (halftile_corner == CORNER_E)) ? 8 : 0); rubidium@8266: rubidium@8266: AddSortableSpriteToDraw(halftile_base + halftile_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z); rubidium@8266: OffsetGroundSprite(31, 9); rubidium@8266: } else if (IsSpecialRailFoundation(f)) { rubidium@8266: /* anti-zig-zag foundation */ rubidium@8266: SpriteID spr; rubidium@8266: if (ti->tileh == SLOPE_NS || ti->tileh == SLOPE_EW) { rubidium@8266: /* half of leveled foundation under track corner */ rubidium@8266: spr = leveled_base + SlopeWithThreeCornersRaised(GetRailFoundationCorner(f)); rubidium@8266: } else { rubidium@8266: /* tile-slope = sloped along X/Y, foundation-slope = three corners raised */ rubidium@8266: spr = inclined_base + 2 * GetRailFoundationCorner(f) + ((ti->tileh == SLOPE_SW || ti->tileh == SLOPE_NE) ? 1 : 0); rubidium@8266: } rubidium@8266: AddSortableSpriteToDraw(spr, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); rubidium@8266: OffsetGroundSprite(31, 9); tron@4253: } else { belugas@6527: /* inclined foundation */ rubidium@7831: byte inclined = GetHighestSlopeCorner(ti->tileh) * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@7831: rubidium@8263: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); tron@4246: } rubidium@7831: ti->z += ApplyFoundationToSlope(f, &ti->tileh); truelight@0: } truelight@0: } truelight@0: tron@1589: void DoClearSquare(TileIndex tile) truelight@0: { tron@3447: MakeClear(tile, CLEAR_GRASS, _generating_world ? 3 : 0); tron@2955: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: frosch@9112: /** Returns information about trackdirs and signal states. smatz@9092: * If there is any trackbit at 'side', return all trackdirbits. frosch@9112: * For TRANSPORT_ROAD, return no trackbits if there is no roadbit (of given subtype) at given side. smatz@9092: * @param tile tile to get info about smatz@9092: * @param mode transport type smatz@9092: * @param sub_mode for TRANSPORT_ROAD, roadtypes to check smatz@9092: * @param side side we are entering from, INVALID_DIAGDIR to return all trackbits smatz@9092: * @return trackdirbits and other info depending on 'mode' smatz@9092: */ frosch@9112: TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) truelight@0: { smatz@9092: return _tile_type_procs[GetTileType(tile)]->get_tile_track_status_proc(tile, mode, sub_mode, side); truelight@0: } truelight@0: rubidium@5838: void ChangeTileOwner(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->change_tile_owner_proc(tile, old_player, new_player); truelight@0: } truelight@0: tron@1589: void GetAcceptedCargo(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: memset(ac, 0, sizeof(AcceptedCargo)); tron@1214: _tile_type_procs[GetTileType(tile)]->get_accepted_cargo_proc(tile, ac); truelight@0: } truelight@0: tron@1589: void AnimateTile(TileIndex tile) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->animate_tile_proc(tile); truelight@0: } truelight@0: tron@1589: void ClickTile(TileIndex tile) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->click_tile_proc(tile); truelight@0: } truelight@0: tron@1589: void GetTileDesc(TileIndex tile, TileDesc *td) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->get_tile_desc_proc(tile, td); truelight@0: } truelight@0: maedhros@6669: /** maedhros@6669: * Has a snow line table already been loaded. maedhros@6669: * @return true if the table has been loaded already. maedhros@6669: */ maedhros@6669: bool IsSnowLineSet(void) maedhros@6669: { maedhros@6669: return _snow_line != NULL; maedhros@6669: } maedhros@6669: maedhros@6669: /** maedhros@6669: * Set a variable snow line, as loaded from a newgrf file. maedhros@6669: * @param table the 12 * 32 byte table containing the snowline for each day maedhros@6669: */ maedhros@6669: void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]) maedhros@6669: { maedhros@6669: _snow_line = CallocT(1); maedhros@6669: memcpy(_snow_line->table, table, sizeof(_snow_line->table)); maedhros@6669: maedhros@6669: for (uint i = 0; i < SNOW_LINE_MONTHS; i++) { maedhros@6669: for (uint j = 0; j < SNOW_LINE_DAYS; j++) { maedhros@6669: _snow_line->highest_value = max(_snow_line->highest_value, table[i][j]); maedhros@6669: } maedhros@6669: } maedhros@6669: } maedhros@6669: maedhros@6669: /** maedhros@6669: * Get the current snow line, either variable or static. maedhros@6669: * @return the snow line height. maedhros@6669: */ maedhros@6669: byte GetSnowLine(void) maedhros@6669: { rubidium@10775: if (_snow_line == NULL) return _settings_game.game_creation.snow_line; maedhros@6669: maedhros@6669: YearMonthDay ymd; maedhros@6669: ConvertDateToYMD(_date, &ymd); maedhros@6669: return _snow_line->table[ymd.month][ymd.day]; maedhros@6669: } maedhros@6669: maedhros@6669: /** maedhros@6669: * Get the highest possible snow line height, either variable or static. maedhros@6669: * @return the highest snow line height. maedhros@6669: */ maedhros@6669: byte HighestSnowLine(void) maedhros@6669: { rubidium@10775: return _snow_line == NULL ? _settings_game.game_creation.snow_line : _snow_line->highest_value; maedhros@6669: } maedhros@6669: maedhros@6669: /** maedhros@6669: * Clear the variable snow line table and free the memory. maedhros@6669: */ maedhros@6669: void ClearSnowLine(void) maedhros@6669: { maedhros@6669: free(_snow_line); maedhros@6669: _snow_line = NULL; maedhros@6669: } maedhros@6669: Darkvater@1775: /** Clear a piece of landscape tron@3491: * @param tile tile to clear belugas@6527: * @param flags of operation to conduct Darkvater@1775: * @param p1 unused Darkvater@1775: * @param p2 unused truelight@0: */ rubidium@7439: CommandCost CmdLandscapeClear(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@1214: return _tile_type_procs[GetTileType(tile)]->clear_tile_proc(tile, flags); truelight@0: } truelight@0: Darkvater@1793: /** Clear a big piece of landscape tron@3491: * @param tile end tile of area dragging Darkvater@1793: * @param p1 start tile of area dragging belugas@6527: * @param flags of operation to conduct Darkvater@1793: * @param p2 unused Darkvater@1793: */ rubidium@7439: CommandCost CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1793: belugas@6527: /* make sure sx,sy are smaller than ex,ey */ smatz@9313: int ex = TileX(tile); smatz@9313: int ey = TileY(tile); smatz@9313: int sx = TileX(p1); smatz@9313: int sy = TileY(p1); tron@6432: if (ex < sx) Swap(ex, sx); tron@6432: if (ey < sy) Swap(ey, sy); truelight@0: smatz@9313: Money money = GetAvailableMoneyForCommand(); smatz@9313: CommandCost cost(EXPENSES_CONSTRUCTION); smatz@9313: bool success = false; truelight@0: smatz@9313: for (int x = sx; x <= ex; ++x) { smatz@9313: for (int y = sy; y <= ey; ++y) { smatz@9313: CommandCost ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR); Darkvater@1793: if (CmdFailed(ret)) continue; truelight@0: success = true; truelight@0: truelight@0: if (flags & DC_EXEC) { smatz@9313: money -= ret.GetCost(); smatz@9313: if (ret.GetCost() > 0 && money < 0) { rubidium@7446: _additional_cash_required = ret.GetCost(); rubidium@7446: return cost; truelight@0: } tron@3493: DoCommand(TileXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: belugas@6527: /* draw explosion animation... */ Darkvater@1793: if ((x == sx || x == ex) && (y == sy || y == ey)) { belugas@6527: /* big explosion in each corner, or small explosion for single tiles */ tron@3645: CreateEffectVehicleAbove(x * TILE_SIZE + TILE_SIZE / 2, y * TILE_SIZE + TILE_SIZE / 2, 2, tron@1359: sy == ey && sx == ex ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE tron@1359: ); truelight@0: } truelight@0: } rubidium@7446: cost.AddCost(ret); truelight@0: } truelight@0: } truelight@0: Darkvater@1793: return (success) ? cost : CMD_ERROR; truelight@0: } truelight@0: truelight@0: rubidium@10233: TileIndex _cur_tileloop_tile; truelight@0: #define TILELOOP_BITS 4 truelight@0: #define TILELOOP_SIZE (1 << TILELOOP_BITS) rubidium@6987: #define TILELOOP_ASSERTMASK ((TILELOOP_SIZE - 1) + ((TILELOOP_SIZE - 1) << MapLogX())) tron@927: #define TILELOOP_CHKMASK (((1 << (MapLogX() - TILELOOP_BITS))-1) << TILELOOP_BITS) truelight@0: rubidium@6573: void RunTileLoop() truelight@0: { smatz@9313: TileIndex tile = _cur_tileloop_tile; truelight@0: smatz@9313: assert((tile & ~TILELOOP_ASSERTMASK) == 0); smatz@9313: uint count = (MapSizeX() / TILELOOP_SIZE) * (MapSizeY() / TILELOOP_SIZE); truelight@0: do { tron@1214: _tile_type_procs[GetTileType(tile)]->tile_loop_proc(tile); truelight@0: tron@926: if (TileX(tile) < MapSizeX() - TILELOOP_SIZE) { belugas@6527: tile += TILELOOP_SIZE; // no overflow truelight@0: } else { tron@1981: tile = TILE_MASK(tile - TILELOOP_SIZE * (MapSizeX() / TILELOOP_SIZE - 1) + TileDiffXY(0, TILELOOP_SIZE)); /* x would overflow, also increase y */ truelight@0: } smatz@9313: } while (--count != 0); smatz@9313: assert((tile & ~TILELOOP_ASSERTMASK) == 0); truelight@0: truelight@0: tile += 9; smatz@9313: if (tile & TILELOOP_CHKMASK) { tron@863: tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK; smatz@9313: } truelight@0: _cur_tileloop_tile = tile; truelight@0: } truelight@0: rubidium@6573: void InitializeLandscape() truelight@0: { tron@3078: uint maxx = MapMaxX(); tron@3078: uint maxy = MapMaxY(); tron@3078: uint sizex = MapSizeX(); smatz@9313: tron@3078: uint y; tron@3078: for (y = 0; y < maxy; y++) { smatz@9313: uint x; tron@3078: for (x = 0; x < maxx; x++) { tron@3447: MakeClear(sizex * y + x, CLEAR_GRASS, 3); tron@3078: SetTileHeight(sizex * y + x, 0); frosch@8946: SetTropicZone(sizex * y + x, TROPICZONE_NORMAL); celestar@5573: ClearBridgeMiddle(sizex * y + x); tron@3078: } tron@3078: MakeVoid(sizex * y + x); tron@2049: } smatz@9313: for (uint x = 0; x < sizex; x++) MakeVoid(sizex * y + x); truelight@0: } truelight@0: rubidium@4344: static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4 }; rubidium@4344: static const byte _genterrain_tbl_2[5] = { 0, 0, 0, 0, 33 }; truelight@0: smatz@9313: static void GenerateTerrain(int type, uint flag) truelight@0: { smatz@9313: uint32 r = Random(); truelight@0: smatz@9313: const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); truelight@0: smatz@9313: uint x = r & MapMaxX(); smatz@9313: uint y = (r >> MapLogX()) & MapMaxY(); truelight@0: tron@2951: if (x < 2 || y < 2) return; truelight@0: smatz@9313: DiagDirection direction = (DiagDirection)GB(r, 22, 2); smatz@9313: uint w = templ->width; smatz@9313: uint h = templ->height; truelight@0: smatz@9313: if (DiagDirToAxis(direction) == AXIS_Y) Swap(w, h); smatz@9313: smatz@9313: const byte *p = templ->data; smatz@9313: smatz@9313: if ((flag & 4) != 0) { tron@1273: uint xw = x * MapSizeY(); tron@1273: uint yw = y * MapSizeX(); tron@1273: uint bias = (MapSizeX() + MapSizeY()) * 16; tron@1273: tron@1273: switch (flag & 3) { smatz@9313: default: NOT_REACHED(); tron@1273: case 0: tron@1273: if (xw + yw > MapSize() - bias) return; tron@1273: break; tron@1273: tron@1273: case 1: tron@1273: if (yw < xw + bias) return; tron@1273: break; tron@1273: tron@1273: case 2: tron@1273: if (xw + yw < MapSize() + bias) return; tron@1273: break; tron@1273: tron@1273: case 3: tron@1273: if (xw < yw + bias) return; tron@1273: break; truelight@0: } truelight@0: } truelight@0: tron@2951: if (x + w >= MapMaxX() - 1) return; tron@2951: if (y + h >= MapMaxY() - 1) return; truelight@0: smatz@9313: Tile *tile = &_m[TileXY(x, y)]; truelight@0: tron@1275: switch (direction) { smatz@9313: default: NOT_REACHED(); smatz@9313: case DIAGDIR_NE: truelight@0: do { smatz@9313: Tile *tile_cur = tile; tron@1275: smatz@9313: for (uint w_cur = w; w_cur != 0; --w_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1275: tile_cur++; tron@1275: } tron@1981: tile += TileDiffXY(0, 1); tron@1275: } while (--h != 0); tron@1275: break; tron@1275: smatz@9313: case DIAGDIR_SE: truelight@0: do { smatz@9313: Tile *tile_cur = tile; tron@1275: smatz@9313: for (uint h_cur = h; h_cur != 0; --h_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1981: tile_cur += TileDiffXY(0, 1); tron@1275: } smatz@9313: tile += TileDiffXY(1, 0); tron@1275: } while (--w != 0); tron@1275: break; tron@1275: smatz@9313: case DIAGDIR_SW: tron@1981: tile += TileDiffXY(w - 1, 0); truelight@0: do { smatz@9313: Tile *tile_cur = tile; tron@1275: smatz@9313: for (uint w_cur = w; w_cur != 0; --w_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1275: tile_cur--; tron@1275: } tron@1981: tile += TileDiffXY(0, 1); tron@1275: } while (--h != 0); tron@1275: break; tron@1275: smatz@9313: case DIAGDIR_NW: tron@1981: tile += TileDiffXY(0, h - 1); tron@1275: do { smatz@9313: Tile *tile_cur = tile; tron@1275: smatz@9313: for (uint h_cur = h; h_cur != 0; --h_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1981: tile_cur -= TileDiffXY(0, 1); tron@1278: } smatz@9313: tile += TileDiffXY(1, 0); tron@1275: } while (--w != 0); tron@1275: break; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: #include "table/genland.h" truelight@0: rubidium@6573: static void CreateDesertOrRainForest() truelight@0: { truelight@4300: TileIndex update_freq = MapSize() / 4; tron@909: const TileIndexDiffC *data; truelight@0: smatz@9313: for (TileIndex tile = 0; tile != MapSize(); ++tile) { truelight@4300: if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: tron@909: for (data = _make_desert_or_rainforest_data; tron@909: data != endof(_make_desert_or_rainforest_data); ++data) { tron@1184: TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data)); tron@1044: if (TileHeight(t) >= 4 || IsTileType(t, MP_WATER)) break; tron@909: } tron@909: if (data == endof(_make_desert_or_rainforest_data)) belugas@3379: SetTropicZone(tile, TROPICZONE_DESERT); tron@909: } truelight@183: smatz@9313: for (uint i = 0; i != 256; i++) { truelight@4300: if ((i % 64) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@0: RunTileLoop(); truelight@4300: } truelight@0: smatz@9313: for (TileIndex tile = 0; tile != MapSize(); ++tile) { truelight@4300: if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: tron@909: for (data = _make_desert_or_rainforest_data; tron@909: data != endof(_make_desert_or_rainforest_data); ++data) { tron@909: TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data)); tron@3447: if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_DESERT)) break; tron@909: } tron@909: if (data == endof(_make_desert_or_rainforest_data)) belugas@3379: SetTropicZone(tile, TROPICZONE_RAINFOREST); tron@909: } truelight@0: } truelight@0: truelight@4300: void GenerateLandscape(byte mode) truelight@0: { smatz@9313: static const int gwp_desert_amount = 4 + 8; truelight@183: truelight@4300: if (mode == GW_HEIGHTMAP) { rubidium@10775: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_settings_game.game_creation.landscape == LT_TROPIC) ? 1 + gwp_desert_amount : 1); truelight@4300: LoadHeightmap(_file_to_saveload.name); truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); rubidium@10775: } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) { rubidium@10775: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_settings_game.game_creation.landscape == LT_TROPIC) ? 3 + gwp_desert_amount : 3); truelight@4300: GenerateTerrainPerlin(); truelight@4300: } else { rubidium@10775: switch (_settings_game.game_creation.landscape) { smatz@9313: case LT_ARCTIC: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 2); truelight@0: smatz@9313: uint32 r = Random(); smatz@9313: smatz@9313: for (uint i = ScaleByMapSize(GB(r, 0, 7) + 950); i != 0; --i) { truelight@4300: GenerateTerrain(2, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); tron@3017: smatz@9313: uint flag = GB(r, 7, 2) | 4; smatz@9313: for (uint i = ScaleByMapSize(GB(r, 9, 7) + 450); i != 0; --i) { truelight@4300: GenerateTerrain(4, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); smatz@9313: } break; tron@3017: smatz@9313: case LT_TROPIC: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 3 + gwp_desert_amount); truelight@4300: smatz@9313: uint32 r = Random(); smatz@9313: smatz@9313: for (uint i = ScaleByMapSize(GB(r, 0, 7) + 170); i != 0; --i) { truelight@4300: GenerateTerrain(0, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: smatz@9313: uint flag = GB(r, 7, 2) | 4; smatz@9313: for (uint i = ScaleByMapSize(GB(r, 9, 8) + 1700); i != 0; --i) { truelight@4300: GenerateTerrain(0, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: flag ^= 2; truelight@4300: smatz@9313: for (uint i = ScaleByMapSize(GB(r, 17, 7) + 410); i != 0; --i) { truelight@4300: GenerateTerrain(3, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); smatz@9313: } break; truelight@4300: smatz@9313: default: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 1); truelight@4300: smatz@9313: uint32 r = Random(); smatz@9313: rubidium@10775: uint i = ScaleByMapSize(GB(r, 0, 7) + (3 - _settings_game.difficulty.quantity_sea_lakes) * 256 + 100); truelight@4300: for (; i != 0; --i) { rubidium@10775: GenerateTerrain(_settings_game.difficulty.terrain_type, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); smatz@9313: } break; truelight@4300: } truelight@0: } truelight@0: truelight@0: ConvertGroundTilesIntoWaterTiles(); truelight@0: rubidium@10775: if (_settings_game.game_creation.landscape == LT_TROPIC) CreateDesertOrRainForest(); truelight@0: } truelight@0: rubidium@6573: void OnTick_Town(); rubidium@6573: void OnTick_Trees(); rubidium@6573: void OnTick_Station(); rubidium@6573: void OnTick_Industry(); truelight@0: rubidium@6573: void OnTick_Players(); rubidium@6573: void OnTick_Train(); truelight@0: rubidium@6573: void CallLandscapeTick() truelight@0: { truelight@0: OnTick_Town(); truelight@0: OnTick_Trees(); truelight@0: OnTick_Station(); truelight@0: OnTick_Industry(); truelight@0: truelight@0: OnTick_Players(); truelight@0: OnTick_Train(); truelight@0: } truelight@0: truelight@0: TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng) truelight@0: { truelight@0: int rn = rng; truelight@0: uint32 r = Random(); truelight@0: tron@1981: return TILE_MASK(TileXY( tron@2150: TileX(a) + (GB(r, 0, 8) * rn * 2 >> 8) - rn, tron@2150: TileY(a) + (GB(r, 8, 8) * rn * 2 >> 8) - rn tron@1174: )); truelight@0: }