MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CalcRaildirsDrawstyle

Function CalcRaildirsDrawstyle

src/viewport.cpp:3070–3282  ·  view source on GitHub ↗

while dragging */

Source from the content-addressed store, hash-verified

3068
3069/** while dragging */
3070static void CalcRaildirsDrawstyle(int x, int y, int method)
3071{
3072 HighLightStyle b;
3073
3074 int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
3075 int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
3076 uint w = abs(dx) + TILE_SIZE;
3077 uint h = abs(dy) + TILE_SIZE;
3078
3079 if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
3080 /* We 'force' a selection direction; first four rail buttons. */
3081 method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
3082 int raw_dx = _thd.selstart.x - _thd.selend.x;
3083 int raw_dy = _thd.selstart.y - _thd.selend.y;
3084 switch (method) {
3085 case VPM_FIX_X:
3086 b = HT_LINE | HT_DIR_Y;
3087 x = _thd.selstart.x;
3088 break;
3089
3090 case VPM_FIX_Y:
3091 b = HT_LINE | HT_DIR_X;
3092 y = _thd.selstart.y;
3093 break;
3094
3095 case VPM_FIX_HORIZONTAL:
3096 if (dx == -dy) {
3097 /* We are on a straight horizontal line. Determine the 'rail'
3098 * to build based the sub tile location. */
3099 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
3100 } else {
3101 /* We are not on a straight line. Determine the rail to build
3102 * based on whether we are above or below it. */
3103 b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
3104
3105 /* Calculate where a horizontal line through the start point and
3106 * a vertical line from the selected end point intersect and
3107 * use that point as the end point. */
3108 int offset = (raw_dx - raw_dy) / 2;
3109 x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
3110 y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
3111
3112 /* 'Build' the last half rail tile if needed */
3113 if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
3114 if (dx + dy >= (int)TILE_SIZE) {
3115 x -= (int)TILE_SIZE;
3116 } else {
3117 y += (int)TILE_SIZE;
3118 }
3119 }
3120
3121 /* Make sure we do not overflow the map! */
3122 CheckUnderflow(x, y, 1);
3123 CheckUnderflow(y, x, 1);
3124 CheckOverflow(x, y, (Map::MaxX() - 1) * TILE_SIZE, 1);
3125 CheckOverflow(y, x, (Map::MaxY() - 1) * TILE_SIZE, 1);
3126 assert(x >= 0 && y >= 0 && x <= (int)(Map::MaxX() * TILE_SIZE) && y <= (int)(Map::MaxY() * TILE_SIZE));
3127 }

Callers 1

VpSelectTilesWithMethodFunction · 0.85

Calls 13

absFunction · 0.85
CheckUnderflowFunction · 0.85
CheckOverflowFunction · 0.85
NOT_REACHEDFunction · 0.85
TileVirtXYFunction · 0.85
GetAutorailHTFunction · 0.85
Check2x1AutoRailFunction · 0.85
DistanceManhattanFunction · 0.85
HideMeasurementTooltipsFunction · 0.85
CalcHeightdiffFunction · 0.85
CeilDivFunction · 0.85
ShowMeasurementTooltipsFunction · 0.85

Tested by

no test coverage detected