tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file landscape.cpp Functions related to the landscape (slopes etc.). */ rubidium@10455: 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@9723: #include "viewport_func.h" rubidium@9723: #include "command_func.h" glx@9505: #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@9723: #include "tile_cmd.h" rubidium@9723: #include "core/alloc_func.hpp" rubidium@9723: #include "fios.h" rubidium@9723: #include "window_func.h" rubidium@9723: #include "functions.h" rubidium@9723: #include "date_func.h" rubidium@9723: #include "vehicle_func.h" rubidium@9724: #include "settings_type.h" rubidium@9724: #include "water.h" glx@10294: #include "effectvehicle_func.h" rubidium@10455: #include "landscape_type.h" rubidium@9724: rubidium@9724: #include "table/sprites.h" truelight@0: 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@9694: SnowLine *_snow_line = NULL; tron@1958: rubidium@9694: /** rubidium@9694: * Applys a foundation to a slope. rubidium@9694: * rubidium@9694: * @pre Foundation and slope must be valid combined. rubidium@9694: * @param f The #Foundation. rubidium@9694: * @param s The #Slope to modify. rubidium@9694: * @return Increment to the tile Z coordinate. rubidium@9694: */ rubidium@9694: uint ApplyFoundationToSlope(Foundation f, Slope *s) rubidium@9694: { rubidium@9694: if (!IsFoundation(f)) return 0; rubidium@9694: rubidium@9694: if (IsLeveledFoundation(f)) { glx@10955: uint dz = TILE_HEIGHT + (IsSteepSlope(*s) ? TILE_HEIGHT : 0); rubidium@9694: *s = SLOPE_FLAT; glx@10955: return dz; rubidium@9694: } rubidium@9694: rubidium@9722: if (f != FOUNDATION_STEEP_BOTH && IsNonContinuousFoundation(f)) { rubidium@9722: *s = HalftileSlope(*s, GetHalftileFoundationCorner(f)); rubidium@9722: return 0; rubidium@9722: } rubidium@9722: rubidium@9722: if (IsSpecialRailFoundation(f)) { rubidium@9722: *s = SlopeWithThreeCornersRaised(OppositeCorner(GetRailFoundationCorner(f))); rubidium@9722: return 0; rubidium@9722: } rubidium@9722: rubidium@9694: uint dz = IsSteepSlope(*s) ? TILE_HEIGHT : 0; glx@9704: Corner highest_corner = GetHighestSlopeCorner(*s); rubidium@9694: rubidium@9694: switch (f) { rubidium@9694: case FOUNDATION_INCLINED_X: glx@9704: *s = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? SLOPE_SW : SLOPE_NE); rubidium@9694: break; rubidium@9694: rubidium@9694: case FOUNDATION_INCLINED_Y: glx@9704: *s = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? SLOPE_SE : SLOPE_NW); rubidium@9694: break; rubidium@9694: rubidium@9694: case FOUNDATION_STEEP_LOWER: glx@9704: *s = SlopeWithOneCornerRaised(highest_corner); rubidium@9694: break; rubidium@9694: rubidium@9722: case FOUNDATION_STEEP_BOTH: rubidium@9722: *s = HalftileSlope(SlopeWithOneCornerRaised(highest_corner), highest_corner); rubidium@9694: break; rubidium@9694: rubidium@9694: default: NOT_REACHED(); rubidium@9694: } rubidium@9694: return dz; rubidium@9694: } rubidium@9694: truelight@0: rubidium@9869: /** rubidium@9869: * Determines height at given coordinate of a slope rubidium@9869: * @param x x coordinate rubidium@9869: * @param y y coordinate rubidium@9869: * @param corners slope to examine rubidium@9869: * @return height of given point of given slope rubidium@9869: */ tron@3636: uint GetPartialZ(int x, int y, Slope corners) truelight@0: { rubidium@9722: if (IsHalftileSlope(corners)) { rubidium@9722: switch (GetHalftileSlopeCorner(corners)) { rubidium@9722: case CORNER_W: rubidium@9722: if (x - y >= 0) return GetSlopeMaxZ(corners); rubidium@9722: break; rubidium@9722: rubidium@9722: case CORNER_S: rubidium@9722: if (x - (y ^ 0xF) >= 0) return GetSlopeMaxZ(corners); rubidium@9722: break; rubidium@9722: rubidium@9722: case CORNER_E: rubidium@9722: if (y - x >= 0) return GetSlopeMaxZ(corners); rubidium@9722: break; rubidium@9722: rubidium@9722: case CORNER_N: rubidium@9722: if ((y ^ 0xF) - x >= 0) return GetSlopeMaxZ(corners); rubidium@9722: break; rubidium@9722: rubidium@9722: default: NOT_REACHED(); rubidium@9722: } rubidium@9722: } rubidium@9722: truelight@0: int z = 0; truelight@0: rubidium@9724: switch (RemoveHalftileSlope(corners)) { rubidium@9869: case SLOPE_W: rubidium@9869: if (x - y >= 0) { rubidium@9869: z = (x - y) >> 1; rubidium@9869: } rubidium@9869: break; truelight@0: rubidium@9869: case SLOPE_S: rubidium@9869: y ^= 0xF; rubidium@9869: if ((x - y) >= 0) { rubidium@9869: z = (x - y) >> 1; rubidium@9869: } rubidium@9869: break; truelight@0: rubidium@9869: case SLOPE_SW: rubidium@9869: z = (x >> 1) + 1; rubidium@9869: break; truelight@0: rubidium@9869: case SLOPE_E: rubidium@9869: if (y - x >= 0) { rubidium@9869: z = (y - x) >> 1; rubidium@9869: } rubidium@9869: break; truelight@0: rubidium@9869: case SLOPE_EW: rubidium@9869: case SLOPE_NS: rubidium@9869: case SLOPE_ELEVATED: rubidium@9869: z = 4; rubidium@9869: break; truelight@0: rubidium@9869: case SLOPE_SE: rubidium@9869: z = (y >> 1) + 1; rubidium@9869: break; truelight@183: rubidium@9869: case SLOPE_WSE: rubidium@9869: z = 8; rubidium@9869: y ^= 0xF; rubidium@9869: if (x - y < 0) { rubidium@9869: z += (x - y) >> 1; rubidium@9869: } rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_N: rubidium@9869: y ^= 0xF; rubidium@9869: if (y - x >= 0) { rubidium@9869: z = (y - x) >> 1; rubidium@9869: } rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_NW: rubidium@9869: z = (y ^ 0xF) >> 1; rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_NWS: rubidium@9869: z = 8; rubidium@9869: if (x - y < 0) { rubidium@9869: z += (x - y) >> 1; rubidium@9869: } rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_NE: rubidium@9869: z = (x ^ 0xF) >> 1; rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_ENW: rubidium@9869: z = 8; rubidium@9869: y ^= 0xF; rubidium@9869: if (y - x < 0) { rubidium@9869: z += (y - x) >> 1; rubidium@9869: } rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_SEN: rubidium@9869: z = 8; rubidium@9869: if (y - x < 0) { rubidium@9869: z += (y - x) >> 1; rubidium@9869: } rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_STEEP_S: rubidium@9869: z = 1 + ((x + y) >> 1); rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_STEEP_W: rubidium@9869: z = 1 + ((x + (y ^ 0xF)) >> 1); rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_STEEP_N: rubidium@9869: z = 1 + (((x ^ 0xF) + (y ^ 0xF)) >> 1); rubidium@9869: break; rubidium@9869: rubidium@9869: case SLOPE_STEEP_E: rubidium@9869: z = 1 + (((x ^ 0xF) + y) >> 1); rubidium@9869: 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: truelight@9718: /** rubidium@9722: * Determine the Z height of a corner relative to TileZ. rubidium@9722: * rubidium@9722: * @pre The slope must not be a halftile slope. rubidium@9722: * rubidium@9722: * @param tileh The slope. rubidium@9722: * @param corner The corner. rubidium@9722: * @return Z position of corner relative to TileZ. rubidium@9722: */ rubidium@9722: int GetSlopeZInCorner(Slope tileh, Corner corner) rubidium@9722: { rubidium@9722: assert(!IsHalftileSlope(tileh)); rubidium@9724: return ((tileh & SlopeWithOneCornerRaised(corner)) != 0 ? TILE_HEIGHT : 0) + (tileh == SteepSlope(corner) ? TILE_HEIGHT : 0); rubidium@9722: } rubidium@9722: rubidium@9722: /** truelight@9718: * Determine the Z height of the corners of a specific tile edge truelight@9718: * rubidium@9722: * @note If a tile has a non-continuous halftile foundation, a corner can have different heights wrt. it's edges. rubidium@9722: * truelight@9718: * @pre z1 and z2 must be initialized (typ. with TileZ). The corner heights just get added. truelight@9718: * truelight@9718: * @param tileh The slope of the tile. truelight@9718: * @param edge The edge of interest. truelight@9718: * @param z1 Gets incremented by the height of the first corner of the edge. (near corner wrt. the camera) truelight@9718: * @param z2 Gets incremented by the height of the second corner of the edge. (far corner wrt. the camera) truelight@9718: */ truelight@9718: void GetSlopeZOnEdge(Slope tileh, DiagDirection edge, int *z1, int *z2) truelight@9718: { truelight@9718: static const Slope corners[4][4] = { truelight@9718: /* corner | steep slope truelight@9718: * z1 z2 | z1 z2 */ truelight@9718: {SLOPE_E, SLOPE_N, SLOPE_STEEP_E, SLOPE_STEEP_N}, // DIAGDIR_NE, z1 = E, z2 = N truelight@9718: {SLOPE_S, SLOPE_E, SLOPE_STEEP_S, SLOPE_STEEP_E}, // DIAGDIR_SE, z1 = S, z2 = E truelight@9718: {SLOPE_S, SLOPE_W, SLOPE_STEEP_S, SLOPE_STEEP_W}, // DIAGDIR_SW, z1 = S, z2 = W truelight@9718: {SLOPE_W, SLOPE_N, SLOPE_STEEP_W, SLOPE_STEEP_N}, // DIAGDIR_NW, z1 = W, z2 = N truelight@9718: }; truelight@9718: rubidium@9722: int halftile_test = (IsHalftileSlope(tileh) ? SlopeWithOneCornerRaised(GetHalftileSlopeCorner(tileh)) : 0); rubidium@9722: if (halftile_test == corners[edge][0]) *z2 += TILE_HEIGHT; // The slope is non-continuous in z2. z2 is on the upper side. rubidium@9722: if (halftile_test == corners[edge][1]) *z1 += TILE_HEIGHT; // The slope is non-continuous in z1. z1 is on the upper side. rubidium@9722: truelight@9718: if ((tileh & corners[edge][0]) != 0) *z1 += TILE_HEIGHT; // z1 is raised truelight@9718: if ((tileh & corners[edge][1]) != 0) *z2 += TILE_HEIGHT; // z2 is raised rubidium@9724: if (RemoveHalftileSlope(tileh) == corners[edge][2]) *z1 += TILE_HEIGHT; // z1 is highest corner of a steep slope rubidium@9724: if (RemoveHalftileSlope(tileh) == corners[edge][3]) *z2 += TILE_HEIGHT; // z2 is highest corner of a steep slope truelight@9718: } tron@4061: rubidium@9724: /** rubidium@9724: * Get slope of a tile on top of a (possible) foundation rubidium@9724: * If a tile does not have a foundation, the function returns the same as GetTileSlope. rubidium@9724: * rubidium@9724: * @param tile The tile of interest. rubidium@9724: * @param z returns the z of the foundation slope. (Can be NULL, if not needed) rubidium@9724: * @return The slope on top of the foundation. rubidium@9724: */ rubidium@9724: Slope GetFoundationSlope(TileIndex tile, uint* z) dominik@37: { tron@4061: Slope tileh = GetTileSlope(tile, z); rubidium@9694: Foundation f = _tile_type_procs[GetTileType(tile)]->get_foundation_proc(tile, tileh); rubidium@9724: uint z_inc = ApplyFoundationToSlope(f, &tileh); rubidium@9724: if (z != NULL) *z += z_inc; rubidium@9694: 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: truelight@9718: int z_W_here = z_here; truelight@9718: int z_N_here = z_here; truelight@9718: GetSlopeZOnEdge(slope_here, DIAGDIR_NW, &z_W_here, &z_N_here); truelight@9718: truelight@9718: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z); truelight@9718: int z_W = z; truelight@9718: int z_N = z; truelight@9718: GetSlopeZOnEdge(slope, DIAGDIR_SE, &z_W, &z_N); truelight@9718: truelight@9718: 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: truelight@9718: int z_E_here = z_here; truelight@9718: int z_N_here = z_here; truelight@9718: GetSlopeZOnEdge(slope_here, DIAGDIR_NE, &z_E_here, &z_N_here); truelight@9718: truelight@9718: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z); truelight@9718: int z_E = z; truelight@9718: int z_N = z; truelight@9718: GetSlopeZOnEdge(slope, DIAGDIR_SW, &z_E, &z_N); truelight@9718: truelight@9718: return (z_N_here > z_N) || (z_E_here > z_E); tron@4061: } tron@4061: tron@4061: rubidium@9694: void DrawFoundation(TileInfo *ti, Foundation f) truelight@0: { rubidium@9694: if (!IsFoundation(f)) return; rubidium@9694: rubidium@9722: /* Two part foundations must be drawn separately */ rubidium@9722: assert(f != FOUNDATION_STEEP_BOTH); rubidium@9722: rubidium@9722: uint sprite_block = 0; tron@4061: uint z; rubidium@9694: Slope slope = GetFoundationSlope(ti->tile, &z); dominik@39: rubidium@9722: /* Select the needed block of foundations sprites rubidium@9722: * Block 0: Walls at NW and NE edge rubidium@9722: * Block 1: Wall at NE edge rubidium@9722: * Block 2: Wall at NW edge rubidium@9722: * Block 3: No walls at NW or NE edge rubidium@9722: */ rubidium@9722: if (!HasFoundationNW(ti->tile, slope, z)) sprite_block += 1; rubidium@9722: if (!HasFoundationNE(ti->tile, slope, z)) sprite_block += 2; rubidium@9722: rubidium@9722: /* Use the original slope sprites if NW and NE borders should be visible */ rubidium@9722: SpriteID leveled_base = (sprite_block == 0 ? (int)SPR_FOUNDATION_BASE : (SPR_SLOPES_VIRTUAL_BASE + sprite_block * SPR_TRKFOUND_BLOCK_SIZE)); rubidium@9722: SpriteID inclined_base = SPR_SLOPES_VIRTUAL_BASE + SPR_SLOPES_INCLINED_OFFSET + sprite_block * SPR_TRKFOUND_BLOCK_SIZE; rubidium@9722: SpriteID halftile_base = SPR_HALFTILE_FOUNDATION_BASE + sprite_block * SPR_HALFTILE_BLOCK_SIZE; dominik@37: tron@4253: if (IsSteepSlope(ti->tileh)) { rubidium@9722: if (!IsNonContinuousFoundation(f)) { rubidium@9722: /* Lower part of foundation */ rubidium@9722: AddSortableSpriteToDraw( rubidium@9722: leveled_base + (ti->tileh & ~SLOPE_STEEP), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z rubidium@9722: ); rubidium@9722: } rubidium@9694: glx@9704: Corner highest_corner = GetHighestSlopeCorner(ti->tileh); rubidium@9694: ti->z += ApplyFoundationToSlope(f, &ti->tileh); rubidium@9694: rubidium@9694: if (IsInclinedFoundation(f)) { rubidium@9694: /* inclined foundation */ rubidium@9694: byte inclined = highest_corner * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@9694: rubidium@9722: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); glx@10955: } else if (IsLeveledFoundation(f)) { glx@10955: AddSortableSpriteToDraw(leveled_base + SlopeWithOneCornerRaised(highest_corner), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z - TILE_HEIGHT); glx@10955: OffsetGroundSprite(31, 1); rubidium@9722: } else if (f == FOUNDATION_STEEP_LOWER) { belugas@6527: /* one corner raised */ tron@4253: OffsetGroundSprite(31, 1); rubidium@9722: } else { rubidium@9722: /* halftile foundation */ rubidium@9722: int x_bb = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? 8 : 0); rubidium@9722: int y_bb = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? 8 : 0); rubidium@9722: rubidium@9722: AddSortableSpriteToDraw(halftile_base + highest_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z + TILE_HEIGHT); rubidium@9722: OffsetGroundSprite(31, 9); tron@4253: } truelight@0: } else { rubidium@9694: if (IsLeveledFoundation(f)) { rubidium@9722: /* leveled foundation */ rubidium@9722: AddSortableSpriteToDraw(leveled_base + ti->tileh, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); rubidium@9722: OffsetGroundSprite(31, 1); rubidium@9722: } else if (IsNonContinuousFoundation(f)) { rubidium@9722: /* halftile foundation */ rubidium@9722: Corner halftile_corner = GetHalftileFoundationCorner(f); rubidium@9722: int x_bb = (((halftile_corner == CORNER_W) || (halftile_corner == CORNER_S)) ? 8 : 0); rubidium@9722: int y_bb = (((halftile_corner == CORNER_S) || (halftile_corner == CORNER_E)) ? 8 : 0); tron@4246: rubidium@9722: AddSortableSpriteToDraw(halftile_base + halftile_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z); rubidium@9722: OffsetGroundSprite(31, 9); rubidium@9722: } else if (IsSpecialRailFoundation(f)) { rubidium@9722: /* anti-zig-zag foundation */ rubidium@9722: SpriteID spr; rubidium@9722: if (ti->tileh == SLOPE_NS || ti->tileh == SLOPE_EW) { rubidium@9722: /* half of leveled foundation under track corner */ rubidium@9722: spr = leveled_base + SlopeWithThreeCornersRaised(GetRailFoundationCorner(f)); rubidium@9722: } else { rubidium@9722: /* tile-slope = sloped along X/Y, foundation-slope = three corners raised */ rubidium@9722: spr = inclined_base + 2 * GetRailFoundationCorner(f) + ((ti->tileh == SLOPE_SW || ti->tileh == SLOPE_NE) ? 1 : 0); rubidium@9722: } rubidium@9722: AddSortableSpriteToDraw(spr, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); rubidium@9722: OffsetGroundSprite(31, 9); tron@4253: } else { belugas@6527: /* inclined foundation */ rubidium@9694: byte inclined = GetHighestSlopeCorner(ti->tileh) * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@9694: rubidium@9722: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); tron@4246: } rubidium@9694: 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: glx@9732: /** Returns information about trackdirs and signal states. glx@9732: * If there is any trackbit at 'side', return all trackdirbits. glx@9732: * For TRANSPORT_ROAD, return no trackbits if there is no roadbit (of given subtype) at given side. glx@9732: * @param tile tile to get info about glx@9732: * @param mode transport type glx@9732: * @param sub_mode for TRANSPORT_ROAD, roadtypes to check glx@9732: * @param side side we are entering from, INVALID_DIAGDIR to return all trackbits glx@9732: * @return trackdirbits and other info depending on 'mode' glx@9732: */ glx@9732: TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) truelight@0: { glx@9732: 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: glx@9505: /** glx@9505: * Has a snow line table already been loaded. glx@9505: * @return true if the table has been loaded already. glx@9505: */ glx@9505: bool IsSnowLineSet(void) glx@9505: { glx@9505: return _snow_line != NULL; glx@9505: } glx@9505: glx@9505: /** glx@9505: * Set a variable snow line, as loaded from a newgrf file. glx@9505: * @param table the 12 * 32 byte table containing the snowline for each day glx@9505: */ glx@9505: void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]) glx@9505: { glx@9505: _snow_line = CallocT(1); glx@9505: memcpy(_snow_line->table, table, sizeof(_snow_line->table)); glx@9505: glx@9505: for (uint i = 0; i < SNOW_LINE_MONTHS; i++) { glx@9505: for (uint j = 0; j < SNOW_LINE_DAYS; j++) { glx@9505: _snow_line->highest_value = max(_snow_line->highest_value, table[i][j]); glx@9505: } glx@9505: } glx@9505: } glx@9505: glx@9505: /** glx@9505: * Get the current snow line, either variable or static. glx@9505: * @return the snow line height. glx@9505: */ glx@9505: byte GetSnowLine(void) glx@9505: { glx@10776: if (_snow_line == NULL) return _settings_game.game_creation.snow_line; glx@9505: glx@9505: YearMonthDay ymd; glx@9505: ConvertDateToYMD(_date, &ymd); glx@9505: return _snow_line->table[ymd.month][ymd.day]; glx@9505: } glx@9505: glx@9505: /** glx@9505: * Get the highest possible snow line height, either variable or static. glx@9505: * @return the highest snow line height. glx@9505: */ glx@9505: byte HighestSnowLine(void) glx@9505: { glx@10776: return _snow_line == NULL ? _settings_game.game_creation.snow_line : _snow_line->highest_value; glx@9505: } glx@9505: glx@9505: /** glx@9505: * Clear the variable snow line table and free the memory. glx@9505: */ glx@9505: void ClearSnowLine(void) glx@9505: { glx@9505: free(_snow_line); glx@9505: _snow_line = NULL; glx@9505: } glx@9505: 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: */ glx@9629: 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: */ glx@9629: 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 */ rubidium@9869: int ex = TileX(tile); rubidium@9869: int ey = TileY(tile); rubidium@9869: int sx = TileX(p1); rubidium@9869: int sy = TileY(p1); tron@6432: if (ex < sx) Swap(ex, sx); tron@6432: if (ey < sy) Swap(ey, sy); truelight@0: rubidium@9869: Money money = GetAvailableMoneyForCommand(); rubidium@9869: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@9869: bool success = false; truelight@0: rubidium@9869: for (int x = sx; x <= ex; ++x) { rubidium@9869: for (int y = sy; y <= ey; ++y) { rubidium@9869: 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) { rubidium@9869: money -= ret.GetCost(); rubidium@9869: if (ret.GetCost() > 0 && money < 0) { glx@9629: _additional_cash_required = ret.GetCost(); glx@9629: 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: } glx@9629: cost.AddCost(ret); truelight@0: } truelight@0: } truelight@0: Darkvater@1793: return (success) ? cost : CMD_ERROR; truelight@0: } truelight@0: truelight@0: rubidium@10249: TileIndex _cur_tileloop_tile; truelight@0: #define TILELOOP_BITS 4 truelight@0: #define TILELOOP_SIZE (1 << TILELOOP_BITS) rubidium@9601: #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: { rubidium@9869: TileIndex tile = _cur_tileloop_tile; truelight@0: rubidium@9869: assert((tile & ~TILELOOP_ASSERTMASK) == 0); rubidium@9869: 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: } rubidium@9869: } while (--count != 0); rubidium@9869: assert((tile & ~TILELOOP_ASSERTMASK) == 0); truelight@0: truelight@0: tile += 9; rubidium@9869: if (tile & TILELOOP_CHKMASK) { tron@863: tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK; rubidium@9869: } 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(); rubidium@9869: tron@3078: uint y; tron@3078: for (y = 0; y < maxy; y++) { rubidium@9869: 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); rubidium@9724: SetTropicZone(sizex * y + x, TROPICZONE_NORMAL); celestar@5573: ClearBridgeMiddle(sizex * y + x); tron@3078: } tron@3078: MakeVoid(sizex * y + x); tron@2049: } rubidium@9869: 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: rubidium@9869: static void GenerateTerrain(int type, uint flag) truelight@0: { rubidium@9869: uint32 r = Random(); truelight@0: rubidium@9869: const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); truelight@0: rubidium@9869: uint x = r & MapMaxX(); rubidium@9869: uint y = (r >> MapLogX()) & MapMaxY(); truelight@0: tron@2951: if (x < 2 || y < 2) return; truelight@0: rubidium@9869: DiagDirection direction = (DiagDirection)GB(r, 22, 2); rubidium@9869: uint w = templ->width; rubidium@9869: uint h = templ->height; truelight@0: rubidium@9869: if (DiagDirToAxis(direction) == AXIS_Y) Swap(w, h); rubidium@9869: rubidium@9869: const byte *p = templ->data; rubidium@9869: rubidium@9869: 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) { rubidium@9869: 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: rubidium@9869: Tile *tile = &_m[TileXY(x, y)]; truelight@0: tron@1275: switch (direction) { rubidium@9869: default: NOT_REACHED(); rubidium@9869: case DIAGDIR_NE: truelight@0: do { rubidium@9869: Tile *tile_cur = tile; tron@1275: rubidium@9869: 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: rubidium@9869: case DIAGDIR_SE: truelight@0: do { rubidium@9869: Tile *tile_cur = tile; tron@1275: rubidium@9869: 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: } rubidium@9869: tile += TileDiffXY(1, 0); tron@1275: } while (--w != 0); tron@1275: break; tron@1275: rubidium@9869: case DIAGDIR_SW: tron@1981: tile += TileDiffXY(w - 1, 0); truelight@0: do { rubidium@9869: Tile *tile_cur = tile; tron@1275: rubidium@9869: 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: rubidium@9869: case DIAGDIR_NW: tron@1981: tile += TileDiffXY(0, h - 1); tron@1275: do { rubidium@9869: Tile *tile_cur = tile; tron@1275: rubidium@9869: 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: } rubidium@9869: 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: rubidium@9869: 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: rubidium@9869: 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: rubidium@9869: 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: { rubidium@9869: static const int gwp_desert_amount = 4 + 8; truelight@183: truelight@4300: if (mode == GW_HEIGHTMAP) { glx@10776: 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); glx@10776: } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) { glx@10776: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_settings_game.game_creation.landscape == LT_TROPIC) ? 3 + gwp_desert_amount : 3); truelight@4300: GenerateTerrainPerlin(); truelight@4300: } else { glx@10776: switch (_settings_game.game_creation.landscape) { rubidium@9869: case LT_ARCTIC: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 2); truelight@0: rubidium@9869: uint32 r = Random(); rubidium@9869: rubidium@9869: 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: rubidium@9869: uint flag = GB(r, 7, 2) | 4; rubidium@9869: for (uint i = ScaleByMapSize(GB(r, 9, 7) + 450); i != 0; --i) { truelight@4300: GenerateTerrain(4, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); rubidium@9869: } break; tron@3017: rubidium@9869: case LT_TROPIC: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 3 + gwp_desert_amount); truelight@4300: rubidium@9869: uint32 r = Random(); rubidium@9869: rubidium@9869: 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: rubidium@9869: uint flag = GB(r, 7, 2) | 4; rubidium@9869: 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: rubidium@9869: for (uint i = ScaleByMapSize(GB(r, 17, 7) + 410); i != 0; --i) { truelight@4300: GenerateTerrain(3, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); rubidium@9869: } break; truelight@4300: rubidium@9869: default: { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 1); truelight@4300: rubidium@9869: uint32 r = Random(); rubidium@9869: glx@10776: uint i = ScaleByMapSize(GB(r, 0, 7) + (3 - _settings_game.difficulty.quantity_sea_lakes) * 256 + 100); truelight@4300: for (; i != 0; --i) { glx@10776: GenerateTerrain(_settings_game.difficulty.terrain_type, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); rubidium@9869: } break; truelight@4300: } truelight@0: } truelight@0: truelight@0: ConvertGroundTilesIntoWaterTiles(); truelight@0: glx@10776: 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: }