tron@2186: /* $Id$ */ tron@2186: 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@2163: #include "functions.h" tron@679: #include "map.h" tron@2154: #include "player.h" tron@1349: #include "spritecache.h" tron@1363: #include "table/sprites.h" tron@1209: #include "tile.h" truelight@0: #include truelight@0: #include "viewport.h" truelight@0: #include "command.h" truelight@0: #include "vehicle.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" truelight@4300: #include "heightmap.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, celestar@5593: _tile_type_tunnel_procs, celestar@5590: _tile_type_unmovable_procs, celestar@5593: _tile_type_bridge_procs, celestar@5593: _tile_type_bridge_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, celestar@5593: &_tile_type_tunnel_procs, truelight@0: &_tile_type_unmovable_procs, celestar@5593: &_tile_type_bridge_procs, celestar@5593: &_tile_type_bridge_procs, celestar@5590: NULL, celestar@5590: NULL, celestar@5590: NULL 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: celestar@5650: const Slope _inclined_tileh[] = { rubidium@4344: SLOPE_SW, SLOPE_NW, SLOPE_SW, SLOPE_SE, SLOPE_NE, SLOPE_SE, SLOPE_NE, SLOPE_NW, rubidium@4344: SLOPE_E, SLOPE_N, SLOPE_W, SLOPE_S, tron@4253: SLOPE_NWS, SLOPE_WSE, SLOPE_SEN, SLOPE_ENW tron@1958: }; tron@1958: truelight@0: tron@3636: uint GetPartialZ(int x, int y, Slope corners) truelight@0: { truelight@0: int z = 0; truelight@0: tron@2952: switch (corners) { tron@3636: case SLOPE_W: truelight@0: if (x - y >= 0) truelight@0: z = (x - y) >> 1; truelight@0: break; truelight@183: tron@3636: case SLOPE_S: truelight@0: y^=0xF; truelight@0: if ( (x - y) >= 0) truelight@0: z = (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_SW: truelight@0: z = (x>>1) + 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_E: truelight@0: if (y - x >= 0) truelight@0: z = (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_EW: tron@3636: case SLOPE_NS: tron@3636: case SLOPE_ELEVATED: truelight@0: z = 4; truelight@0: break; truelight@0: tron@3636: case SLOPE_SE: truelight@0: z = (y>>1) + 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_WSE: truelight@0: z = 8; truelight@0: y^=0xF; truelight@0: if (x - y < 0) truelight@0: z += (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_N: truelight@0: y ^= 0xF; truelight@0: if (y - x >= 0) truelight@0: z = (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NW: truelight@0: z = (y^0xF)>>1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NWS: truelight@0: z = 8; truelight@0: if (x - y < 0) truelight@0: z += (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NE: truelight@0: z = (x^0xF)>>1; truelight@0: break; truelight@0: tron@3636: case SLOPE_ENW: truelight@0: z = 8; truelight@0: y ^= 0xF; truelight@0: if (y - x < 0) truelight@0: z += (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_SEN: truelight@0: z = 8; truelight@0: if (y - x < 0) truelight@0: z += (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_S: truelight@0: z = 1 + ((x+y)>>1); truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_W: truelight@0: z = 1 + ((x+(y^0xF))>>1); truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_N: truelight@0: z = 1 + (((x^0xF)+(y^0xF))>>1); truelight@0: break; truelight@183: tron@3636: case SLOPE_STEEP_E: truelight@0: z = 1 + (((x^0xF)+(y^0xF))>>1); truelight@0: 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: tron@4061: tron@4061: static Slope GetFoundationSlope(TileIndex tile, uint* z) dominik@37: { tron@4061: Slope tileh = GetTileSlope(tile, z); tron@3636: Slope slope = _tile_type_procs[GetTileType(tile)]->get_slope_tileh_proc(tile, tileh); dominik@50: tron@4061: // Flatter slope -> higher base height tron@4061: if (slope < tileh) *z += TILE_HEIGHT; tron@4061: return slope; 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: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z); tron@4061: tron@4061: return tron@4990: ( tron@4990: z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_E ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_E ? TILE_HEIGHT : 0) tron@4990: ) || ( tron@4990: z_here + (slope_here & SLOPE_W ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_W ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_S ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_S ? TILE_HEIGHT : 0) tron@4990: ); 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: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z); tron@4061: tron@4061: return tron@4990: ( tron@4990: z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_W ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_W ? TILE_HEIGHT : 0) tron@4990: ) || ( tron@4990: z_here + (slope_here & SLOPE_E ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_E ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_S ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_S ? TILE_HEIGHT : 0) tron@4990: ); tron@4061: } tron@4061: tron@4061: truelight@0: void DrawFoundation(TileInfo *ti, uint f) truelight@0: { tron@4240: uint32 sprite_base = SPR_SLOPES_BASE - 15; tron@4061: Slope slope; tron@4061: uint z; dominik@39: tron@4061: slope = GetFoundationSlope(ti->tile, &z); tron@4061: if (!HasFoundationNW(ti->tile, slope, z)) sprite_base += 22; tron@4061: if (!HasFoundationNE(ti->tile, slope, z)) sprite_base += 44; dominik@37: tron@4253: if (IsSteepSlope(ti->tileh)) { tron@4253: uint32 lower_base; dominik@37: tron@4253: // Lower part of foundation tron@4253: lower_base = sprite_base; tron@4253: if (lower_base == SPR_SLOPES_BASE - 15) lower_base = SPR_FOUNDATION_BASE; tron@4253: AddSortableSpriteToDraw( tron@4253: lower_base + (ti->tileh & ~SLOPE_STEEP), ti->x, ti->y, 16, 16, 7, ti->z tron@4253: ); tron@3645: ti->z += TILE_HEIGHT; tron@4253: ti->tileh = _inclined_tileh[f - 15]; tron@4253: if (f < 15 + 8) { tron@4253: // inclined tron@4253: AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); tron@4253: } else if (f >= 15 + 8 + 4) { tron@4253: // three corners raised tron@4253: uint32 upper = sprite_base + 15 + (f - 15 - 8 - 4) * 2; tron@4253: tron@4253: AddSortableSpriteToDraw(upper, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: AddChildSpriteScreen(upper + 1, 31, 9); tron@4253: OffsetGroundSprite(31, 9); tron@4253: } else { tron@4253: // one corner raised tron@4253: OffsetGroundSprite(31, 1); tron@4253: } truelight@0: } else { tron@4253: if (f < 15) { tron@4253: // leveled foundation tron@4253: // Use the original slope sprites if NW and NE borders should be visible tron@4253: if (sprite_base == SPR_SLOPES_BASE - 15) sprite_base = SPR_FOUNDATION_BASE; tron@4246: tron@4253: AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 7, ti->z); tron@4246: ti->z += TILE_HEIGHT; tron@4253: ti->tileh = SLOPE_FLAT; tron@4253: OffsetGroundSprite(31, 1); tron@4253: } else { tron@4253: // inclined foundation tron@4253: AddSortableSpriteToDraw(sprite_base + f, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: ti->tileh = _inclined_tileh[f - 15]; tron@4253: OffsetGroundSprite(31, 9); tron@4246: } 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: tron@1589: uint32 GetTileTrackStatus(TileIndex tile, TransportType mode) truelight@0: { tron@1214: return _tile_type_procs[GetTileType(tile)]->get_tile_track_status_proc(tile, mode); truelight@0: } truelight@0: celestar@5650: 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: Darkvater@1775: /** Clear a piece of landscape tron@3491: * @param tile tile to clear Darkvater@1775: * @param p1 unused Darkvater@1775: * @param p2 unused truelight@0: */ tron@3491: int32 CmdLandscapeClear(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); 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 Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1793: int32 cost, ret, money; tron@3491: int ex; tron@3491: int ey; truelight@0: int sx,sy; truelight@0: int x,y; truelight@0: bool success = false; truelight@0: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1793: darkvater@889: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); darkvater@889: truelight@0: // make sure sx,sy are smaller than ex,ey tron@3493: ex = TileX(tile); tron@3493: ey = TileY(tile); tron@3493: sx = TileX(p1); tron@3493: sy = TileY(p1); truelight@0: if (ex < sx) intswap(ex, sx); truelight@0: if (ey < sy) intswap(ey, sy); truelight@0: truelight@0: money = GetAvailableMoneyForCommand(); truelight@0: cost = 0; truelight@0: tron@3493: for (x = sx; x <= ex; ++x) { tron@3493: for (y = sy; y <= ey; ++y) { tron@3493: ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR); Darkvater@1793: if (CmdFailed(ret)) continue; truelight@0: cost += ret; truelight@0: success = true; truelight@0: truelight@0: if (flags & DC_EXEC) { Darkvater@1793: if (ret > 0 && (money -= ret) < 0) { truelight@0: _additional_cash_required = ret; truelight@0: return cost - ret; truelight@0: } tron@3493: DoCommand(TileXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: truelight@0: // draw explosion animation... Darkvater@1793: if ((x == sx || x == ex) && (y == sy || y == ey)) { truelight@0: // 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: } truelight@0: } truelight@0: } truelight@0: Darkvater@1793: return (success) ? cost : CMD_ERROR; truelight@0: } truelight@0: truelight@0: truelight@0: #define TILELOOP_BITS 4 truelight@0: #define TILELOOP_SIZE (1 << TILELOOP_BITS) tron@927: #define TILELOOP_ASSERTMASK ((TILELOOP_SIZE-1) + ((TILELOOP_SIZE-1) << MapLogX())) tron@927: #define TILELOOP_CHKMASK (((1 << (MapLogX() - TILELOOP_BITS))-1) << TILELOOP_BITS) truelight@0: tron@1093: void RunTileLoop(void) truelight@0: { tron@1589: TileIndex tile; truelight@0: uint count; truelight@0: truelight@0: tile = _cur_tileloop_tile; truelight@0: truelight@0: assert( (tile & ~TILELOOP_ASSERTMASK) == 0); tron@863: 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) { truelight@0: 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: } truelight@0: } while (--count); truelight@0: assert( (tile & ~TILELOOP_ASSERTMASK) == 0); truelight@0: truelight@0: tile += 9; truelight@0: if (tile & TILELOOP_CHKMASK) tron@863: tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK; truelight@0: _cur_tileloop_tile = tile; truelight@0: } truelight@0: ludde@2055: void InitializeLandscape(void) truelight@0: { tron@3078: uint maxx = MapMaxX(); tron@3078: uint maxy = MapMaxY(); tron@3078: uint sizex = MapSizeX(); tron@3078: uint x; tron@3078: uint y; tron@1218: tron@3078: for (y = 0; y < maxy; y++) { tron@3078: for (x = 0; x < maxx; x++) { tron@3447: MakeClear(sizex * y + x, CLEAR_GRASS, 3); tron@3078: SetTileHeight(sizex * y + x, 0); celestar@5573: _m[sizex * y + x].extra = 0; celestar@5573: ClearBridgeMiddle(sizex * y + x); tron@3078: } tron@3078: MakeVoid(sizex * y + x); tron@2049: } tron@3078: for (x = 0; x < sizex; x++) MakeVoid(sizex * y + x); truelight@0: } truelight@0: tron@1093: void ConvertGroundTilesIntoWaterTiles(void) truelight@0: { tron@4000: TileIndex tile; truelight@4300: uint z; truelight@4300: Slope slope; truelight@0: tron@1275: for (tile = 0; tile < MapSize(); ++tile) { truelight@4300: slope = GetTileSlope(tile, &z); truelight@4300: if (IsTileType(tile, MP_CLEAR) && z == 0) { truelight@4300: /* Make both water for tiles at level 0 truelight@4300: * and make shore, as that looks much better truelight@4300: * during the generation. */ truelight@4300: switch (slope) { truelight@4300: case SLOPE_FLAT: truelight@4300: MakeWater(tile); truelight@4300: break; truelight@4300: truelight@4300: case SLOPE_N: truelight@4300: case SLOPE_E: truelight@4300: case SLOPE_S: truelight@4300: case SLOPE_W: truelight@4300: case SLOPE_NW: truelight@4300: case SLOPE_SW: truelight@4300: case SLOPE_SE: truelight@4300: case SLOPE_NE: truelight@4300: MakeShore(tile); truelight@4300: break; truelight@4300: truelight@4300: default: truelight@4300: break; truelight@4300: } truelight@0: } truelight@0: } 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: truelight@0: static void GenerateTerrain(int type, int flag) truelight@0: { truelight@0: uint32 r; tron@1275: uint x; tron@1275: uint y; tron@1275: uint w; tron@1275: uint h; celestar@5650: const Sprite* templ; tron@1275: const byte *p; tron@2049: Tile* tile; truelight@0: byte direction; truelight@0: truelight@0: r = Random(); celestar@5650: templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); truelight@0: tron@856: x = r & MapMaxX(); tron@927: y = (r >> MapLogX()) & MapMaxY(); truelight@0: truelight@0: tron@2951: if (x < 2 || y < 2) return; truelight@0: tron@2140: direction = GB(r, 22, 2); tron@1275: if (direction & 1) { celestar@5650: w = templ->height; celestar@5650: h = templ->width; tron@1275: } else { celestar@5650: w = templ->width; celestar@5650: h = templ->height; tron@1275: } celestar@5650: p = templ->data; truelight@0: truelight@0: if (flag & 4) { 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) { 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: tron@2049: tile = &_m[TileXY(x, y)]; truelight@0: tron@1275: switch (direction) { tron@1275: case 0: truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint w_cur; tron@1275: tron@1275: for (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: tron@1275: case 1: truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint h_cur; tron@1275: tron@1275: for (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: } tron@1275: tile++; tron@1275: } while (--w != 0); tron@1275: break; tron@1275: tron@1275: case 2: tron@1981: tile += TileDiffXY(w - 1, 0); truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint w_cur; tron@1275: tron@1275: for (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: tron@1275: case 3: tron@1981: tile += TileDiffXY(0, h - 1); tron@1275: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint h_cur; tron@1275: tron@1275: for (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: } tron@1275: tile++; tron@1275: } while (--w != 0); tron@1275: break; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: #include "table/genland.h" truelight@0: tron@1093: static void CreateDesertOrRainForest(void) truelight@0: { tron@1275: TileIndex tile; truelight@4300: TileIndex update_freq = MapSize() / 4; tron@909: const TileIndexDiffC *data; tron@1275: uint i; truelight@0: tron@909: for (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: truelight@4300: for (i = 0; i != 256; i++) { truelight@4300: if ((i % 64) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@0: RunTileLoop(); truelight@4300: } truelight@0: tron@909: for (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: { truelight@4300: const int gwp_desert_amount = 4 + 8; tron@1275: uint i; tron@1275: uint flag; truelight@0: uint32 r; truelight@183: truelight@4300: if (mode == GW_HEIGHTMAP) { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_DESERT) ? 1 + gwp_desert_amount : 1); truelight@4300: LoadHeightmap(_file_to_saveload.name); truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: } else if (_patches.land_generator == LG_TERRAGENESIS) { truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_DESERT) ? 3 + gwp_desert_amount : 3); truelight@4300: GenerateTerrainPerlin(); truelight@4300: } else { truelight@4300: switch (_opt.landscape) { truelight@4300: case LT_HILLY: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 2); truelight@0: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 950); i != 0; --i) { truelight@4300: GenerateTerrain(2, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); tron@3017: truelight@4300: r = Random(); truelight@4300: flag = GB(r, 0, 2) | 4; truelight@4300: for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) { truelight@4300: GenerateTerrain(4, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; tron@3017: truelight@4300: case LT_DESERT: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 3 + gwp_desert_amount); truelight@4300: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 170); i != 0; --i) { truelight@4300: GenerateTerrain(0, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: r = Random(); truelight@4300: flag = GB(r, 0, 2) | 4; truelight@4300: for (i = ScaleByMapSize(GB(r, 16, 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: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 410); i != 0; --i) { truelight@4300: GenerateTerrain(3, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; truelight@4300: truelight@4300: default: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 1); truelight@4300: truelight@4300: i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100); truelight@4300: for (; i != 0; --i) { truelight@4300: GenerateTerrain(_opt.diff.terrain_type, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; truelight@4300: } truelight@0: } truelight@0: truelight@0: ConvertGroundTilesIntoWaterTiles(); truelight@0: tron@3017: if (_opt.landscape == LT_DESERT) CreateDesertOrRainForest(); truelight@0: } truelight@0: tron@1093: void OnTick_Town(void); tron@1093: void OnTick_Trees(void); tron@1093: void OnTick_Station(void); tron@1093: void OnTick_Industry(void); truelight@0: tron@1093: void OnTick_Players(void); tron@1093: void OnTick_Train(void); truelight@0: tron@1093: void CallLandscapeTick(void) 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: } truelight@0: tron@1589: bool IsValidTile(TileIndex tile) truelight@0: { tron@926: return (tile < MapSizeX() * MapMaxY() && TileX(tile) != MapMaxX()); truelight@0: }