/* $Id$ */ /** @file smallmap_gui.cpp */ #include "stdafx.h" #include "openttd.h" #include "functions.h" #include "bridge_map.h" #include "clear_map.h" #include "industry_map.h" #include "station_map.h" #include "table/strings.h" #include "table/sprites.h" #include "landscape.h" #include "map.h" #include "tile.h" #include "gui.h" #include "tree_map.h" #include "tunnel_map.h" #include "window.h" #include "gfx.h" #include "viewport.h" #include "player.h" #include "vehicle.h" #include "town.h" #include "sound.h" #include "variables.h" static const Widget _smallmap_widgets[] = { { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, { WWT_CAPTION, RESIZE_RIGHT, 13, 11, 433, 0, 13, STR_00B0_MAP, STR_018C_WINDOW_TITLE_DRAG_THIS}, { WWT_STICKYBOX, RESIZE_LR, 13, 434, 445, 0, 13, 0x0, STR_STICKY_BUTTON}, { WWT_PANEL, RESIZE_RB, 13, 0, 445, 14, 257, 0x0, STR_NULL}, { WWT_INSET, RESIZE_RB, 13, 2, 443, 16, 255, 0x0, STR_NULL}, { WWT_IMGBTN, RESIZE_LRTB, 13, 380, 401, 258, 279, SPR_IMG_SHOW_COUNTOURS, STR_0191_SHOW_LAND_CONTOURS_ON_MAP}, { WWT_IMGBTN, RESIZE_LRTB, 13, 402, 423, 258, 279, SPR_IMG_SHOW_VEHICLES, STR_0192_SHOW_VEHICLES_ON_MAP}, { WWT_IMGBTN, RESIZE_LRTB, 13, 424, 445, 258, 279, SPR_IMG_INDUSTRY, STR_0193_SHOW_INDUSTRIES_ON_MAP}, { WWT_IMGBTN, RESIZE_LRTB, 13, 380, 401, 280, 301, SPR_IMG_SHOW_ROUTES, STR_0194_SHOW_TRANSPORT_ROUTES_ON}, { WWT_IMGBTN, RESIZE_LRTB, 13, 402, 423, 280, 301, SPR_IMG_PLANTTREES, STR_0195_SHOW_VEGETATION_ON_MAP}, { WWT_IMGBTN, RESIZE_LRTB, 13, 424, 445, 280, 301, SPR_IMG_COMPANY_GENERAL, STR_0196_SHOW_LAND_OWNERS_ON_MAP}, { WWT_IMGBTN, RESIZE_LRTB, 13, 358, 379, 258, 279, SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER}, { WWT_IMGBTN, RESIZE_LRTB, 13, 358, 379, 280, 301, SPR_IMG_TOWN, STR_0197_TOGGLE_TOWN_NAMES_ON_OFF}, { WWT_PANEL, RESIZE_RTB, 13, 0, 357, 258, 301, 0x0, STR_NULL}, { WWT_PANEL, RESIZE_RTB, 13, 0, 433, 302, 313, 0x0, STR_NULL}, { WWT_RESIZEBOX, RESIZE_LRTB, 13, 434, 445, 302, 313, 0x0, STR_RESIZE_BUTTON}, { WIDGETS_END}, }; static int _smallmap_type; static bool _smallmap_show_towns = true; #define MK(a,b) a, b #define MKEND() 0xFFFF #define MS(a,b) (a | 0x100), b /* Legend text giving the colours to look for on the minimap */ static const uint16 _legend_land_contours[] = { MK(0x5A, STR_00F0_100M), MK(0x5C, STR_00F1_200M), MK(0x5E, STR_00F2_300M), MK(0x1F, STR_00F3_400M), MK(0x27, STR_00F4_500M), MS(0xD7, STR_00EB_ROADS), MK(0x0A, STR_00EC_RAILROADS), MK(0x98, STR_00ED_STATIONS_AIRPORTS_DOCKS), MK(0xB5, STR_00EE_BUILDINGS_INDUSTRIES), MK(0x0F, STR_00EF_VEHICLES), MKEND() }; static const uint16 _legend_vehicles[] = { MK(0xB8, STR_00F5_TRAINS), MK(0xBF, STR_00F6_ROAD_VEHICLES), MK(0x98, STR_00F7_SHIPS), MK(0x0F, STR_00F8_AIRCRAFT), MS(0xD7, STR_00F9_TRANSPORT_ROUTES), MK(0xB5, STR_00EE_BUILDINGS_INDUSTRIES), MKEND() }; static const uint16 _legend_industries_normal[] = { MK(0xD7, STR_00FA_COAL_MINE), MK(0xB8, STR_00FB_POWER_STATION), MK(0x56, STR_00FC_FOREST), MK(0xC2, STR_00FD_SAWMILL), MK(0xBF, STR_00FE_OIL_REFINERY), MK(0x0F, STR_0105_BANK), MS(0x30, STR_00FF_FARM), MK(0xAE, STR_0100_FACTORY), MK(0x98, STR_0102_OIL_WELLS), MK(0x37, STR_0103_IRON_ORE_MINE), MK(0x0A, STR_0104_STEEL_MILL), MKEND() }; static const uint16 _legend_industries_hilly[] = { MK(0xD7, STR_00FA_COAL_MINE), MK(0xB8, STR_00FB_POWER_STATION), MK(0x56, STR_00FC_FOREST), MK(0x0A, STR_0106_PAPER_MILL), MK(0xBF, STR_00FE_OIL_REFINERY), MK(0x37, STR_0108_FOOD_PROCESSING_PLANT), MS(0x30, STR_00FF_FARM), MK(0xAE, STR_0101_PRINTING_WORKS), MK(0x98, STR_0102_OIL_WELLS), MK(0xC2, STR_0107_GOLD_MINE), MK(0x0F, STR_0105_BANK), MKEND() }; static const uint16 _legend_industries_desert[] = { MK(0xBF, STR_00FE_OIL_REFINERY), MK(0x98, STR_0102_OIL_WELLS), MK(0x0F, STR_0105_BANK), MK(0xB8, STR_0109_DIAMOND_MINE), MK(0x37, STR_0108_FOOD_PROCESSING_PLANT), MK(0x0A, STR_010A_COPPER_ORE_MINE), MK(0x30, STR_00FF_FARM), MS(0x56, STR_010B_FRUIT_PLANTATION), MK(0x27, STR_010C_RUBBER_PLANTATION), MK(0x25, STR_010D_WATER_SUPPLY), MK(0xD0, STR_010E_WATER_TOWER), MK(0xAE, STR_0100_FACTORY), MK(0xC2, STR_010F_LUMBER_MILL), MKEND() }; static const uint16 _legend_industries_candy[] = { MK(0x30, STR_0110_COTTON_CANDY_FOREST), MK(0xAE, STR_0111_CANDY_FACTORY), MK(0x27, STR_0112_BATTERY_FARM), MK(0x37, STR_0113_COLA_WELLS), MK(0xD0, STR_0114_TOY_SHOP), MK(0x0A, STR_0115_TOY_FACTORY), MS(0x25, STR_0116_PLASTIC_FOUNTAINS), MK(0xB8, STR_0117_FIZZY_DRINK_FACTORY), MK(0x98, STR_0118_BUBBLE_GENERATOR), MK(0xC2, STR_0119_TOFFEE_QUARRY), MK(0x0F, STR_011A_SUGAR_MINE), MKEND() }; static const uint16 _legend_routes[] = { MK(0xD7, STR_00EB_ROADS), MK(0x0A, STR_00EC_RAILROADS), MK(0xB5, STR_00EE_BUILDINGS_INDUSTRIES), MS(0x56, STR_011B_RAILROAD_STATION), MK(0xC2, STR_011C_TRUCK_LOADING_BAY), MK(0xBF, STR_011D_BUS_STATION), MK(0xB8, STR_011E_AIRPORT_HELIPORT), MK(0x98, STR_011F_DOCK), MKEND() }; static const uint16 _legend_vegetation[] = { MK(0x52, STR_0120_ROUGH_LAND), MK(0x54, STR_0121_GRASS_LAND), MK(0x37, STR_0122_BARE_LAND), MK(0x25, STR_0123_FIELDS), MK(0x57, STR_0124_TREES), MK(0xD0, STR_00FC_FOREST), MS(0x0A, STR_0125_ROCKS), MK(0xC2, STR_012A_DESERT), MK(0x98, STR_012B_SNOW), MK(0xD7, STR_00F9_TRANSPORT_ROUTES), MK(0xB5, STR_00EE_BUILDINGS_INDUSTRIES), MKEND() }; static const uint16 _legend_land_owners[] = { MK(0xCA, STR_0126_WATER), MK(0x54, STR_0127_NO_OWNER), MK(0xB4, STR_0128_TOWNS), MK(0x20, STR_0129_INDUSTRIES), MKEND() }; #undef MK #undef MS #undef MKEND enum { IND_OFFS = 6 }; static const uint16 * const _legend_table[] = { _legend_land_contours, _legend_vehicles, NULL, _legend_routes, _legend_vegetation, _legend_land_owners, _legend_industries_normal, _legend_industries_hilly, _legend_industries_desert, _legend_industries_candy, }; #if defined(OTTD_ALIGNMENT) static inline void WRITE_PIXELS(Pixel* d, uint32 val) { # if defined(TTD_BIG_ENDIAN) d[0] = GB(val, 24, 8); d[1] = GB(val, 16, 8); d[2] = GB(val, 8, 8); d[3] = GB(val, 0, 8); # elif defined(TTD_LITTLE_ENDIAN) d[0] = GB(val, 0, 8); d[1] = GB(val, 8, 8); d[2] = GB(val, 16, 8); d[3] = GB(val, 24, 8); # endif } /* need to use OR, otherwise we will overwrite the wrong pixels at the edges :( */ static inline void WRITE_PIXELS_OR(Pixel *d, uint32 val) { # if defined(TTD_BIG_ENDIAN) d[0] |= GB(val, 24, 8); d[1] |= GB(val, 16, 8); d[2] |= GB(val, 8, 8); d[3] |= GB(val, 0, 8); # elif defined(TTD_LITTLE_ENDIAN) d[0] |= GB(val, 0, 8); d[1] |= GB(val, 8, 8); d[2] |= GB(val, 16, 8); d[3] |= GB(val, 24, 8); # endif } #else # define WRITE_PIXELS(dst, val) *(uint32*)(dst) = (val); # define WRITE_PIXELS_OR(dst,val) *(uint32*)(dst) |= (val); #endif #define MKCOLOR(x) TO_LE32X(x) /* Height encodings; 16 levels XXX - needs updating for more/finer heights! */ static const uint32 _map_height_bits[16] = { MKCOLOR(0x5A5A5A5A), MKCOLOR(0x5A5B5A5B), MKCOLOR(0x5B5B5B5B), MKCOLOR(0x5B5C5B5C), MKCOLOR(0x5C5C5C5C), MKCOLOR(0x5C5D5C5D), MKCOLOR(0x5D5D5D5D), MKCOLOR(0x5D5E5D5E), MKCOLOR(0x5E5E5E5E), MKCOLOR(0x5E5F5E5F), MKCOLOR(0x5F5F5F5F), MKCOLOR(0x5F1F5F1F), MKCOLOR(0x1F1F1F1F), MKCOLOR(0x1F271F27), MKCOLOR(0x27272727), MKCOLOR(0x27272727), }; struct AndOr { uint32 mor; uint32 mand; }; static inline uint32 ApplyMask(uint32 colour, const AndOr *mask) { return (colour & mask->mand) | mask->mor; } static const AndOr _smallmap_contours_andor[] = { {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x000A0A00), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x98989898), MKCOLOR(0x00000000)}, {MKCOLOR(0xCACACACA), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0xB5B5B5B5), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x000A0A00), MKCOLOR(0xFF0000FF)}, }; static const AndOr _smallmap_vehicles_andor[] = { {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0xCACACACA), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0xB5B5B5B5), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, }; static const AndOr _smallmap_vegetation_andor[] = { {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00575700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0xCACACACA), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0xB5B5B5B5), MKCOLOR(0x00000000)}, {MKCOLOR(0x00000000), MKCOLOR(0xFFFFFFFF)}, {MKCOLOR(0x00B5B500), MKCOLOR(0xFF0000FF)}, {MKCOLOR(0x00D7D700), MKCOLOR(0xFF0000FF)}, }; typedef uint32 GetSmallMapPixels(TileIndex tile); // typedef callthrough function /** * Draws one column of the small map in a certain mode onto the screen buffer. This * function looks exactly the same for all types * * @param dst Pointer to a part of the screen buffer to write to. * @param xc The X coordinate of the first tile in the column. * @param yc The Y coordinate of the first tile in the column * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written. * @param reps Number of lines to draw * @param mask ? * @param proc Pointer to the colour function * @see GetSmallMapPixels(TileIndex) */ static void DrawSmallMapStuff(Pixel *dst, uint xc, uint yc, int pitch, int reps, uint32 mask, GetSmallMapPixels *proc) { Pixel *dst_ptr_end = _screen.dst_ptr + _screen.width * _screen.height - _screen.width; do { /* check if the tile (xc,yc) is within the map range */ if (xc < MapMaxX() && yc < MapMaxY()) { /* check if the dst pointer points to a pixel inside the screen buffer */ if (dst > _screen.dst_ptr && dst < dst_ptr_end) WRITE_PIXELS_OR(dst, proc(TileXY(xc, yc)) & mask); } /* switch to next tile in the column */ } while (xc++, yc++, dst += pitch, --reps != 0); } static inline TileType GetEffectiveTileType(TileIndex tile) { TileType t = GetTileType(tile); if (t == MP_TUNNELBRIDGE) { TransportType tt; if (IsTunnel(tile)) { tt = GetTunnelTransportType(tile); } else { tt = GetBridgeTransportType(tile); } switch (tt) { case TRANSPORT_RAIL: t = MP_RAILWAY; break; case TRANSPORT_ROAD: t = MP_STREET; break; default: t = MP_WATER; break; } } return t; } /** * Return the color a tile would be displayed with in the small map in mode "Contour". * @param tile The tile of which we would like to get the color. * @return The color of tile in the small map in mode "Contour" */ static inline uint32 GetSmallMapContoursPixels(TileIndex tile) { TileType t = GetEffectiveTileType(tile); return ApplyMask(_map_height_bits[TileHeight(tile)], &_smallmap_contours_andor[t]); } /** * Return the color a tile would be displayed with in the small map in mode "Vehicles". * * @param t The tile of which we would like to get the color. * @return The color of tile in the small map in mode "Vehicles" */ static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile) { TileType t = GetEffectiveTileType(tile); return ApplyMask(MKCOLOR(0x54545454), &_smallmap_vehicles_andor[t]); } /** * Return the color a tile would be displayed with in the small map in mode "Industries". * * @param tile The tile of which we would like to get the color. * @return The color of tile in the small map in mode "Industries" */ static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile) { TileType t = GetEffectiveTileType(tile); if (t == MP_INDUSTRY) { return GetIndustrySpec(GetIndustryByTile(tile)->type)->map_colour * 0x01010101; } return ApplyMask(MKCOLOR(0x54545454), &_smallmap_vehicles_andor[t]); } /** * Return the color a tile would be displayed with in the small map in mode "Routes". * * @param t The tile of which we would like to get the color. * @return The color of tile in the small map in mode "Routes" */ static inline uint32 GetSmallMapRoutesPixels(TileIndex tile) { TileType t = GetEffectiveTileType(tile); uint32 bits; if (t == MP_STATION) { switch (GetStationType(tile)) { case STATION_RAIL: bits = MKCOLOR(0x56565656); break; case STATION_AIRPORT: bits = MKCOLOR(0xB8B8B8B8); break; case STATION_TRUCK: bits = MKCOLOR(0xC2C2C2C2); break; case STATION_BUS: bits = MKCOLOR(0xBFBFBFBF); break; case STATION_DOCK: bits = MKCOLOR(0x98989898); break; default: bits = MKCOLOR(0xFFFFFFFF); break; } } else { /* ground color */ bits = ApplyMask(MKCOLOR(0x54545454), &_smallmap_contours_andor[t]); } return bits; } static const uint32 _vegetation_clear_bits[] = { MKCOLOR(0x54545454), ///< full grass MKCOLOR(0x52525252), ///< rough land MKCOLOR(0x0A0A0A0A), ///< rocks MKCOLOR(0x25252525), ///< fields MKCOLOR(0x98989898), ///< snow MKCOLOR(0xC2C2C2C2), ///< desert MKCOLOR(0x54545454), ///< unused MKCOLOR(0x54545454), ///< unused }; static inline uint32 GetSmallMapVegetationPixels(TileIndex tile) { TileType t = GetEffectiveTileType(tile); uint32 bits; switch (t) { case MP_CLEAR: if (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) { bits = MKCOLOR(0x37373737); } else { bits = _vegetation_clear_bits[GetClearGround(tile)]; } break; case MP_INDUSTRY: bits = GetIndustryType(tile) == IT_FOREST ? MKCOLOR(0xD0D0D0D0) : MKCOLOR(0xB5B5B5B5); break; case MP_TREES: if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT) { bits = (_opt.landscape == LT_ARCTIC) ? MKCOLOR(0x98575798) : MKCOLOR(0xC25757C2); } else { bits = MKCOLOR(0x54575754); } break; default: bits = ApplyMask(MKCOLOR(0x54545454), &_smallmap_vehicles_andor[t]); break; } return bits; } static uint32 _owner_colors[OWNER_END + 1]; /** * Return the color a tile would be displayed with in the small map in mode "Owner". * * @param t The tile of which we would like to get the color. * @return The color of tile in the small map in mode "Owner" */ static inline uint32 GetSmallMapOwnerPixels(TileIndex tile) { Owner o; switch (GetTileType(tile)) { case MP_INDUSTRY: o = OWNER_END; break; case MP_HOUSE: o = OWNER_TOWN; break; default: o = GetTileOwner(tile); break; } return _owner_colors[o]; } static const uint32 _smallmap_mask_left[3] = { MKCOLOR(0xFF000000), MKCOLOR(0xFFFF0000), MKCOLOR(0xFFFFFF00), }; static const uint32 _smallmap_mask_right[] = { MKCOLOR(0x000000FF), MKCOLOR(0x0000FFFF), MKCOLOR(0x00FFFFFF), }; /* each tile has 4 x pixels and 1 y pixel */ static GetSmallMapPixels *_smallmap_draw_procs[] = { GetSmallMapContoursPixels, GetSmallMapVehiclesPixels, GetSmallMapIndustriesPixels, GetSmallMapRoutesPixels, GetSmallMapVegetationPixels, GetSmallMapOwnerPixels, }; static const byte _vehicle_type_colors[6] = { 184, 191, 152, 15, 215, 184 }; static void DrawVertMapIndicator(int x, int y, int x2, int y2) { GfxFillRect(x, y, x2, y + 3, 69); GfxFillRect(x, y2 - 3, x2, y2, 69); } static void DrawHorizMapIndicator(int x, int y, int x2, int y2) { GfxFillRect(x, y, x + 3, y2, 69); GfxFillRect(x2 - 3, y, x2, y2, 69); } /** * Draws the small map. * * Basically, the small map is draw column of pixels by column of pixels. The pixels * are drawn directly into the screen buffer. The final map is drawn in multiple passes. * The passes are: *