Changeset - r7922:d7c3cc15726d
[Not reviewed]
src/aircraft_cmd.cpp
Show inline comments
 
@@ -830,18 +830,18 @@ static void SetAircraftPosition(Vehicle 
 
	BeginVehicleMove(v);
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 

	
 
	Vehicle *u = v->Next();
 

	
 
	int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	int safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
	int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
	u->x_pos = x;
 
	u->y_pos = y - ((v->z_pos-GetSlopeZ(safe_x, safe_y)) >> 3);;
 

	
 
	safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
 
	safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
 
	u->z_pos = GetSlopeZ(safe_x, safe_y);
 
	u->cur_image = v->cur_image;
 

	
 
	BeginVehicleMove(u);
 
	VehiclePositionChanged(u);
 
	EndVehicleMove(u);
src/depot.h
Show inline comments
 
@@ -48,13 +48,13 @@ void ShowDepotWindow(TileIndex tile, Veh
 
 * within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
 
 * @param index proposed service interval
 
 * @return service interval
 
 */
 
static inline Date GetServiceIntervalClamped(uint index)
 
{
 
	return (_patches.servint_ispercent) ? clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
 
	return (_patches.servint_ispercent) ? Clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
 
}
 

	
 
/**
 
 * Check if a tile is a depot of the given type.
 
 */
 
static inline bool IsTileDepotType(TileIndex tile, TransportType type)
src/disaster_cmd.cpp
Show inline comments
 
@@ -156,19 +156,19 @@ static void SetDisasterVehiclePos(Vehicl
 

	
 
	DisasterVehicleUpdateImage(v);
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 

	
 
	if ((u = v->Next()) != NULL) {
 
		int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
 
		int safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
		int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
 
		int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
		BeginVehicleMove(u);
 

	
 
		u->x_pos = x;
 
		u->y_pos = y - 1 - (max(z - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
 
		safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
 
		safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
 
		u->z_pos = GetSlopeZ(safe_x, safe_y);
 
		u->direction = v->direction;
 

	
 
		DisasterVehicleUpdateImage(u);
 
		VehiclePositionChanged(u);
 
		EndVehicleMove(u);
src/economy.cpp
Show inline comments
 
@@ -209,13 +209,13 @@ int UpdateCompanyRatingAndValue(Player *
 
		int s;
 
		score = 0;
 
		for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
 
			/* Skip the total */
 
			if (i == SCORE_TOTAL) continue;
 
			/*  Check the score */
 
			s = clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
 
			s = Clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
 
			score += s;
 
			total_score += _score_info[i].score;
 
		}
 

	
 
		_score_part[owner][SCORE_TOTAL] = score;
 

	
src/genworld_gui.cpp
Show inline comments
 
@@ -384,13 +384,13 @@ static void GenerateLandscapeWndProc(Win
 
		case GLAND_START_DATE_DOWN: case GLAND_START_DATE_UP: // Year buttons
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				HandleButtonClick(w, e->we.click.widget);
 
				SetWindowDirty(w);
 

	
 
				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - GLAND_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
 
				_patches_newgame.starting_year = Clamp(_patches_newgame.starting_year + e->we.click.widget - GLAND_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case GLAND_START_DATE_TEXT: // Year text
 
			WP(w, generate_d).widget_id = GLAND_START_DATE_TEXT;
 
			SetDParam(0, _patches_newgame.starting_year);
 
@@ -399,13 +399,13 @@ static void GenerateLandscapeWndProc(Win
 
		case GLAND_SNOW_LEVEL_DOWN: case GLAND_SNOW_LEVEL_UP: // Snow line buttons
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				HandleButtonClick(w, e->we.click.widget);
 
				SetWindowDirty(w);
 

	
 
				_patches_newgame.snow_line_height = clamp(_patches_newgame.snow_line_height + e->we.click.widget - GLAND_SNOW_LEVEL_TEXT, 2, MAX_SNOWLINE_HEIGHT);
 
				_patches_newgame.snow_line_height = Clamp(_patches_newgame.snow_line_height + e->we.click.widget - GLAND_SNOW_LEVEL_TEXT, 2, MAX_SNOWLINE_HEIGHT);
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case GLAND_SNOW_LEVEL_TEXT: // Snow line text
 
			WP(w, generate_d).widget_id = GLAND_SNOW_LEVEL_TEXT;
 
			SetDParam(0, _patches_newgame.snow_line_height);
 
@@ -491,17 +491,17 @@ static void GenerateLandscapeWndProc(Win
 
		if (e->we.edittext.str != NULL) {
 
			int32 value = atoi(e->we.edittext.str);
 

	
 
			switch (WP(w, generate_d).widget_id) {
 
			case GLAND_START_DATE_TEXT:
 
				InvalidateWidget(w, GLAND_START_DATE_TEXT);
 
				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
 
				_patches_newgame.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
 
				break;
 
			case GLAND_SNOW_LEVEL_TEXT:
 
				InvalidateWidget(w, GLAND_SNOW_LEVEL_TEXT);
 
				_patches_newgame.snow_line_height = clamp(value, 2, MAX_SNOWLINE_HEIGHT);
 
				_patches_newgame.snow_line_height = Clamp(value, 2, MAX_SNOWLINE_HEIGHT);
 
				break;
 
			}
 

	
 
			SetWindowDirty(w);
 
		}
 
		break;
 
@@ -654,13 +654,13 @@ static void CreateScenarioWndProc(Window
 
		case CSCEN_START_DATE_DOWN: case CSCEN_START_DATE_UP: // Year buttons
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				HandleButtonClick(w, e->we.click.widget);
 
				SetWindowDirty(w);
 

	
 
				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - CSCEN_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
 
				_patches_newgame.starting_year = Clamp(_patches_newgame.starting_year + e->we.click.widget - CSCEN_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case CSCEN_START_DATE_TEXT: // Year text
 
			WP(w, generate_d).widget_id = CSCEN_START_DATE_TEXT;
 
			SetDParam(0, _patches_newgame.starting_year);
 
@@ -669,13 +669,13 @@ static void CreateScenarioWndProc(Window
 
		case CSCEN_FLAT_LAND_HEIGHT_DOWN: case CSCEN_FLAT_LAND_HEIGHT_UP: // Height level buttons
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				HandleButtonClick(w, e->we.click.widget);
 
				SetWindowDirty(w);
 

	
 
				_patches_newgame.se_flat_world_height = clamp(_patches_newgame.se_flat_world_height + e->we.click.widget - CSCEN_FLAT_LAND_HEIGHT_TEXT, 0, MAX_TILE_HEIGHT);
 
				_patches_newgame.se_flat_world_height = Clamp(_patches_newgame.se_flat_world_height + e->we.click.widget - CSCEN_FLAT_LAND_HEIGHT_TEXT, 0, MAX_TILE_HEIGHT);
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case CSCEN_FLAT_LAND_HEIGHT_TEXT: // Height level text
 
			WP(w, generate_d).widget_id = CSCEN_FLAT_LAND_HEIGHT_TEXT;
 
			SetDParam(0, _patches_newgame.se_flat_world_height);
 
@@ -696,17 +696,17 @@ static void CreateScenarioWndProc(Window
 
		if (e->we.edittext.str != NULL) {
 
			int32 value = atoi(e->we.edittext.str);
 

	
 
			switch (WP(w, generate_d).widget_id) {
 
			case CSCEN_START_DATE_TEXT:
 
				InvalidateWidget(w, CSCEN_START_DATE_TEXT);
 
				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
 
				_patches_newgame.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
 
				break;
 
			case CSCEN_FLAT_LAND_HEIGHT_TEXT:
 
				InvalidateWidget(w, CSCEN_FLAT_LAND_HEIGHT_TEXT);
 
				_patches_newgame.se_flat_world_height = clamp(value, 0, MAX_TILE_HEIGHT);
 
				_patches_newgame.se_flat_world_height = Clamp(value, 0, MAX_TILE_HEIGHT);
 
				break;
 
			}
 

	
 
			SetWindowDirty(w);
 
		}
 
		break;
src/graph_gui.cpp
Show inline comments
 
@@ -1003,23 +1003,23 @@ static void PerformanceRatingDetailWndPr
 

	
 
				/* Draw the score */
 
				SetDParam(0, score);
 
				DrawStringRightAligned(107, y, SET_PERFORMANCE_DETAIL_INT, TC_FROMSTRING);
 

	
 
				/* Calculate the %-bar */
 
				x = clamp(val, 0, needed) * 50 / needed;
 
				x = Clamp(val, 0, needed) * 50 / needed;
 

	
 
				/* SCORE_LOAN is inversed */
 
				if (val < 0 && i == SCORE_LOAN) x = 0;
 

	
 
				/* Draw the bar */
 
				if (x !=  0) GfxFillRect(112,     y - 2, 112 + x,  y + 10, color_done);
 
				if (x != 50) GfxFillRect(112 + x, y - 2, 112 + 50, y + 10, color_notdone);
 

	
 
				/* Calculate the % */
 
				x = clamp(val, 0, needed) * 100 / needed;
 
				x = Clamp(val, 0, needed) * 100 / needed;
 

	
 
				/* SCORE_LOAN is inversed */
 
				if (val < 0 && i == SCORE_LOAN) x = 0;
 

	
 
				/* Draw it */
 
				SetDParam(0, x);
src/industry_cmd.cpp
Show inline comments
 
@@ -1625,13 +1625,13 @@ CommandCost CmdBuildIndustry(TileIndex t
 
				}
 
			}
 
		}
 
	} else {
 
		int count = indspec->num_table;
 
		const IndustryTileTable * const *itt = indspec->table;
 
		int num = clamp(p2, 0, count - 1);
 
		int num = Clamp(p2, 0, count - 1);
 

	
 
		_error_message = STR_0239_SITE_UNSUITABLE;
 
		do {
 
			if (--count < 0) return CMD_ERROR;
 
			if (--num < 0) num = indspec->num_table - 1;
 
		} while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, p1));
 
@@ -2044,13 +2044,13 @@ static void ChangeIndustryProduction(Ind
 
				if (CHANCE16I(20, 1024, r)) new_prod -= max(((RandomRange(50) + 10) * old_prod) >> 8, 1U);
 
				/* Chance of increasing becomes better when more is transported */
 
				if (CHANCE16I(20 + (i->last_month_pct_transported[j] * 20 >> 8), 1024, r >> 16) && !only_decrease) {
 
					new_prod += max(((RandomRange(50) + 10) * old_prod) >> 8, 1U);
 
				}
 

	
 
				new_prod = clamp(new_prod, 1, 255);
 
				new_prod = Clamp(new_prod, 1, 255);
 
				/* Do not stop closing the industry when it has the lowest possible production rate */
 
				if (new_prod == old_prod && old_prod > 1) {
 
					closeit = false;
 
					continue;
 
				}
 

	
src/industry_gui.cpp
Show inline comments
 
@@ -580,13 +580,13 @@ static void IndustryViewWndProc(Window *
 

	
 
	case WE_ON_EDIT_TEXT:
 
		if (e->we.edittext.str[0] != '\0') {
 
			Industry* i = GetIndustry(w->window_number);
 
			int line = WP(w, indview_d).editbox_line;
 

	
 
			i->production_rate[line] = clampu(atoi(e->we.edittext.str), 0, 255);
 
			i->production_rate[line] = ClampU(atoi(e->we.edittext.str), 0, 255);
 
			UpdateIndustryProduction(i);
 
			SetWindowDirty(w);
 
		}
 
	}
 
}
 

	
src/macros.h
Show inline comments
 
@@ -148,15 +148,15 @@ static inline uint minu(const uint a, co
 
 * @note The min value must be less or equal of max or you get some
 
 *       unexpected results.
 
 * @param a The value to clamp/truncate.
 
 * @param min The minimum of the interval.
 
 * @param max the maximum of the interval.
 
 * @returns A value between min and max which is closest to a.
 
 * @see clampu(uint, uint, uint)
 
 * @see ClampU(uint, uint, uint)
 
 */
 
static inline int clamp(const int a, const int min, const int max)
 
static inline int Clamp(const int a, const int min, const int max)
 
{
 
	if (a <= min) return min;
 
	if (a >= max) return max;
 
	return a;
 
}
 

	
 
@@ -171,15 +171,15 @@ static inline int clamp(const int a, con
 
 * @note The min value must be less or equal of max or you get some
 
 *       unexpected results.
 
 * @param a The value to clamp/truncate.
 
 * @param min The minimum of the interval.
 
 * @param max the maximum of the interval.
 
 * @returns A value between min and max which is closest to a.
 
 * @see clamp(int, int, int)
 
 * @see Clamp(int, int, int)
 
 */
 
static inline uint clampu(const uint a, const uint min, const uint max)
 
static inline uint ClampU(const uint a, const uint min, const uint max)
 
{
 
	if (a <= min) return min;
 
	if (a >= max) return max;
 
	return a;
 
}
 

	
 
@@ -192,13 +192,13 @@ static inline uint clampu(const uint a, 
 
 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
 
 * this value is returned. In all other cases the 64-bit value 'fits' in a
 
 * 32-bits integer field and so the value is casted to int32 and returned.
 
 *
 
 * @param a The 64-bit value to clamps
 
 * @return The 64-bit value reduced to a 32-bit value
 
 * @see clamp(int, int, int)
 
 * @see Clamp(int, int, int)
 
 */
 
static inline int32 ClampToI32(const int64 a)
 
{
 
	if (a <= (int32)0x80000000) return 0x80000000;
 
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
 
	return (int32)a;
src/main_gui.cpp
Show inline comments
 
@@ -93,13 +93,13 @@ void HandleOnEditText(const char *str)
 
		break;
 
#ifdef ENABLE_NETWORK
 
	case 3: { // Give money, you can only give money in excess of loan
 
		const Player *p = GetPlayer(_current_player);
 
		Money money = min(p->player_money - p->current_loan, (Money)(atoi(str) / _currency->rate));
 

	
 
		uint32 money_c = clamp(ClampToI32(money), 0, 20000000); // Clamp between 20 million and 0
 
		uint32 money_c = Clamp(ClampToI32(money), 0, 20000000); // Clamp between 20 million and 0
 

	
 
		/* Give 'id' the money, and substract it from ourself */
 
		DoCommandP(0, money_c, id, CcGiveMoney, CMD_GIVE_MONEY | CMD_MSG(STR_INSUFFICIENT_FUNDS));
 
	} break;
 
#endif /* ENABLE_NETWORK */
 
		default: NOT_REACHED();
 
@@ -713,13 +713,13 @@ static Window *PopupMainToolbMenu(Window
 
	/* Extend the dropdown toolbar to the longest string in the list and
 
	 * also make sure the dropdown is fully visible within the window.
 
	 * x + w->left because x is supposed to be the offset of the toolbar-button
 
	 * we clicked on and w->left the toolbar window itself. So meaning that
 
	 * the default position is aligned with the left side of the clicked button */
 
	width = max(GetStringListMaxWidth(base_string, item_count) + 6, 140);
 
	x = w->left + clamp(x, 0, w->width - width); // or alternatively '_screen.width - width'
 
	x = w->left + Clamp(x, 0, w->width - width); // or alternatively '_screen.width - width'
 

	
 
	w = AllocateWindow(x, 22, width, item_count * 10 + 2, MenuWndProc, WC_TOOLBAR_MENU, _menu_widgets);
 
	w->widget[0].bottom = item_count * 10 + 1;
 
	w->flags4 &= ~WF_WHITE_BORDER_MASK;
 

	
 
	WP(w,menu_d).item_count = item_count;
 
@@ -992,26 +992,26 @@ static void ToolbarScenDateBackward(Wind
 
{
 
	/* don't allow too fast scrolling */
 
	if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
		HandleButtonClick(w, 6);
 
		SetWindowDirty(w);
 

	
 
		_patches_newgame.starting_year = clamp(_patches_newgame.starting_year - 1, MIN_YEAR, MAX_YEAR);
 
		_patches_newgame.starting_year = Clamp(_patches_newgame.starting_year - 1, MIN_YEAR, MAX_YEAR);
 
		SetDate(ConvertYMDToDate(_patches_newgame.starting_year, 0, 1));
 
	}
 
	_left_button_clicked = false;
 
}
 

	
 
static void ToolbarScenDateForward(Window *w)
 
{
 
	/* don't allow too fast scrolling */
 
	if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
		HandleButtonClick(w, 7);
 
		SetWindowDirty(w);
 

	
 
		_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + 1, MIN_YEAR, MAX_YEAR);
 
		_patches_newgame.starting_year = Clamp(_patches_newgame.starting_year + 1, MIN_YEAR, MAX_YEAR);
 
		SetDate(ConvertYMDToDate(_patches_newgame.starting_year, 0, 1));
 
	}
 
	_left_button_clicked = false;
 
}
 

	
 
static void ToolbarScenMapTownDir(Window *w)
src/misc_gui.cpp
Show inline comments
 
@@ -606,14 +606,14 @@ void ShowErrorMessage(StringID msg_1, St
 
		}
 
		w = AllocateWindow(pt.x, pt.y, 240, 46, ErrmsgWndProc, WC_ERRMSG, _errmsg_widgets);
 
	} else {
 
		if ( (x|y) != 0) {
 
			pt = RemapCoords2(x, y);
 
			vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
			pt.x = clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (334/2), 0, _screen.width - 334);
 
			pt.y = clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (137/2), 22, _screen.height - 137);
 
			pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (334/2), 0, _screen.width - 334);
 
			pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (137/2), 22, _screen.height - 137);
 
		} else {
 
			pt.x = (_screen.width - 334) >> 1;
 
			pt.y = (_screen.height - 137) >> 1;
 
		}
 
		w = AllocateWindow(pt.x, pt.y, 334, 137, ErrmsgWndProc, WC_ERRMSG, _errmsg_face_widgets);
 
	}
 
@@ -743,15 +743,15 @@ void GuiShowTooltipsWithArgs(StringID st
 
		br.width = 200;
 
	}
 

	
 
	/* Correctly position the tooltip position, watch out for window and cursor size
 
	 * Clamp value to below main toolbar and above statusbar. If tooltip would
 
	 * go below window, flip it so it is shown above the cursor */
 
	y = clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
 
	y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
 
	if (y + br.height > _screen.height - 12) y = _cursor.pos.y + _cursor.offs.y - br.height - 5;
 
	x = clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
 
	x = Clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
 

	
 
	w = AllocateWindow(x, y, br.width, br.height, TooltipsWndProc, WC_TOOLTIPS, _tooltips_widgets);
 

	
 
	WP(w, tooltips_d).string_id = str;
 
	assert(sizeof(WP(w, tooltips_d).params[0]) == sizeof(params[0]));
 
	memcpy(WP(w, tooltips_d).params, params, sizeof(WP(w, tooltips_d).params[0]) * paramcount);
 
@@ -1935,13 +1935,13 @@ static void CheatsWndProc(Window *w, Win
 
				 * 50-steps you should be able to get from min to max */
 
				uint16 step = ((ce->max - ce->min) / 20);
 
				if (step == 0) step = 1;
 

	
 
				/* Increase or decrease the value and clamp it to extremes */
 
				value += (x >= 30) ? step : -step;
 
				value = clamp(value, ce->min, ce->max);
 
				value = Clamp(value, ce->min, ce->max);
 

	
 
				/* take whatever the function returns */
 
				value = ce->proc(value, (x >= 30) ? 1 : -1);
 

	
 
				if (value != oldvalue) {
 
					WP(w,def_d).data_1 = btn * 2 + 1 + ((x >= 30) ? 1 : 0);
src/network/core/udp.cpp
Show inline comments
 
@@ -227,14 +227,14 @@ void NetworkUDPSocketHandler::Recv_Netwo
 
				/* Append GRFConfig to the list */
 
				*dst = c;
 
				dst = &c->next;
 
			}
 
		} /* Fallthrough */
 
		case 3:
 
			info->game_date      = clamp(p->Recv_uint32(), 0, MAX_DATE);
 
			info->start_date     = clamp(p->Recv_uint32(), 0, MAX_DATE);
 
			info->game_date      = Clamp(p->Recv_uint32(), 0, MAX_DATE);
 
			info->start_date     = Clamp(p->Recv_uint32(), 0, MAX_DATE);
 
			/* Fallthrough */
 
		case 2:
 
			info->companies_max  = p->Recv_uint8 ();
 
			info->companies_on   = p->Recv_uint8 ();
 
			info->spectators_max = p->Recv_uint8 ();
 
			/* Fallthrough */
src/network/network_gui.cpp
Show inline comments
 
@@ -689,15 +689,15 @@ static void NetworkStartServerWindowWndP
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				HandleButtonClick(w, e->we.click.widget);
 
				SetWindowDirty(w);
 
				switch (e->we.click.widget) {
 
					default: NOT_REACHED();
 
					case  9: case 11: _network_game_info.clients_max    = clamp(_network_game_info.clients_max    + e->we.click.widget - 10, 2, MAX_CLIENTS); break;
 
					case 12: case 14: _network_game_info.companies_max  = clamp(_network_game_info.companies_max  + e->we.click.widget - 13, 1, MAX_PLAYERS); break;
 
					case 15: case 17: _network_game_info.spectators_max = clamp(_network_game_info.spectators_max + e->we.click.widget - 16, 0, MAX_CLIENTS); break;
 
					case  9: case 11: _network_game_info.clients_max    = Clamp(_network_game_info.clients_max    + e->we.click.widget - 10, 2, MAX_CLIENTS); break;
 
					case 12: case 14: _network_game_info.companies_max  = Clamp(_network_game_info.companies_max  + e->we.click.widget - 13, 1, MAX_PLAYERS); break;
 
					case 15: case 17: _network_game_info.spectators_max = Clamp(_network_game_info.spectators_max + e->we.click.widget - 16, 0, MAX_CLIENTS); break;
 
				}
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case 10: // Click on number of players
 
			nd->widget_id = 10;
 
@@ -786,15 +786,15 @@ static void NetworkStartServerWindowWndP
 
			_network_game_info.use_password = (_network_server_password[0] != '\0');
 
		} else {
 
			int32 value = atoi(e->we.edittext.str);
 
			InvalidateWidget(w, nd->widget_id);
 
			switch (nd->widget_id) {
 
				default: NOT_REACHED();
 
				case 10: _network_game_info.clients_max    = clamp(value, 2, MAX_CLIENTS); break;
 
				case 13: _network_game_info.companies_max  = clamp(value, 1, MAX_PLAYERS); break;
 
				case 16: _network_game_info.spectators_max = clamp(value, 0, MAX_CLIENTS); break;
 
				case 10: _network_game_info.clients_max    = Clamp(value, 2, MAX_CLIENTS); break;
 
				case 13: _network_game_info.companies_max  = Clamp(value, 1, MAX_PLAYERS); break;
 
				case 16: _network_game_info.spectators_max = Clamp(value, 0, MAX_CLIENTS); break;
 
			}
 
		}
 

	
 
		SetWindowDirty(w);
 
		break;
 
	}
src/newgrf.cpp
Show inline comments
 
@@ -1190,13 +1190,13 @@ static bool BridgeChangeInfo(uint brid, 
 

	
 
			case 0x0E: // Flags; bit 0 - disable far pillars
 
				bridge->flags = grf_load_byte(&buf);
 
				break;
 

	
 
			case 0x0F: // Long format year of availability (year since year 0)
 
				bridge->avail_year = clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
 
				bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
 
				break;
 

	
 
			default:
 
				ret = true;
 
				break;
 
		}
 
@@ -1367,13 +1367,13 @@ static bool TownHouseChangeInfo(uint hid
 

	
 
			case 0x1A: // Animation frames
 
				housespec->animation_frames = grf_load_byte(&buf);
 
				break;
 

	
 
			case 0x1B: // Animation speed
 
				housespec->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
 
				housespec->animation_speed = Clamp(grf_load_byte(&buf), 2, 16);
 
				break;
 

	
 
			case 0x1C: // Class of the building type
 
				housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
 
				break;
 

	
 
@@ -3406,13 +3406,13 @@ static void SkipAct5(byte *buf, int len)
 
}
 

	
 
static uint32 GetParamVal(byte param, uint32 *cond_val)
 
{
 
	switch (param) {
 
		case 0x81: // current year
 
			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
			return Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 

	
 
		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
 
			return _opt.landscape;
 

	
 
		case 0x84: { // GRF loading stage
 
			uint32 res = 0;
src/newgrf_engine.cpp
Show inline comments
 
@@ -493,13 +493,13 @@ static uint32 VehicleGetVariable(const R
 
	if (v == NULL) {
 
		/* Vehicle does not exist, so we're in a purchase list */
 
		switch (variable) {
 
			case 0x43: return _current_player | (LiveryHelper(object->u.vehicle.self_type, NULL) << 24); // Owner information
 
			case 0x46: return 0;               // Motion counter
 
			case 0x48: return GetEngine(object->u.vehicle.self_type)->flags; // Vehicle Type Info
 
			case 0xC4: return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; // Build year
 
			case 0xC4: return Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; // Build year
 
			case 0xDA: return INVALID_VEHICLE; // Next vehicle
 
			case 0x7F: return GetGRFParameter(object->u.vehicle.self_type, parameter); // Read GRF parameter
 
		}
 

	
 
		*available = false;
 
		return UINT_MAX;
 
@@ -708,13 +708,13 @@ static uint32 VehicleGetVariable(const R
 
		case 0x3E: return v->cargo.Source();
 
		case 0x3F: return v->cargo.DaysInTransit();
 
		case 0x40: return v->age;
 
		case 0x41: return GB(v->age, 8, 8);
 
		case 0x42: return v->max_age;
 
		case 0x43: return GB(v->max_age, 8, 8);
 
		case 0x44: return clamp(v->build_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
		case 0x44: return Clamp(v->build_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
		case 0x45: return v->unitnumber;
 
		case 0x46: return v->engine_type;
 
		case 0x47: return GB(v->engine_type, 8, 8);
 
		case 0x48: return v->spritenum;
 
		case 0x49: return v->day_counter;
 
		case 0x4A: return v->breakdowns_since_last_service;
src/newgrf_house.cpp
Show inline comments
 
@@ -208,13 +208,13 @@ static uint32 HouseGetVariable(const Res
 

	
 
	switch (variable) {
 
		/* Construction stage. */
 
		case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | OriginalTileRandomiser(TileX(tile), TileY(tile)) << 2;
 

	
 
		/* Building age. */
 
		case 0x41: return clamp(_cur_year - GetHouseConstructionYear(tile), 0, 0xFF);
 
		case 0x41: return Clamp(_cur_year - GetHouseConstructionYear(tile), 0, 0xFF);
 

	
 
		/* Town zone */
 
		case 0x42: return GetTownRadiusGroup(town, tile);
 

	
 
		/* Terrain type */
 
		case 0x43: return GetTerrainType(tile);
 
@@ -366,26 +366,26 @@ void DrawNewHouseTile(TileInfo *ti, Hous
 
	if (group == NULL || group->type != SGT_TILELAYOUT) {
 
		/* XXX: This is for debugging purposes really, and shouldn't stay. */
 
		DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
 
	} else {
 
		/* Limit the building stage to the number of stages supplied. */
 
		byte stage = GetHouseBuildingStage(ti->tile);
 
		stage = clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
 
		stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
 
		DrawTileLayout(ti, group, stage, house_id);
 
	}
 
}
 

	
 
void AnimateNewHouseTile(TileIndex tile)
 
{
 
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
 
	byte animation_speed = hs->animation_speed;
 
	bool frame_set_by_callback = false;
 

	
 
	if (HASBIT(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
 
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 
		if (callback_res != CALLBACK_FAILED) animation_speed = clamp(callback_res & 0xFF, 2, 16);
 
		if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
 
	}
 

	
 
	/* An animation speed of 2 means the animation frame changes 4 ticks, and
 
	 * increasing this value by one doubles the wait. 2 is the minimum value
 
	 * allowed for animation_speed, which corresponds to 120ms, and 16 is the
 
	 * maximum, corresponding to around 33 minutes. */
src/newgrf_industries.cpp
Show inline comments
 
@@ -312,20 +312,20 @@ uint32 IndustryGetVariable(const Resolve
 
		case 0xA4: return industry->last_month_transported[1];
 
		case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
 

	
 
		case 0xA6: return industry->type;
 
		case 0xA7: return industry->founder;
 
		case 0xA8: return industry->random_color;
 
		case 0xA9: return clamp(0, industry->last_prod_year - 1920, 255);
 
		case 0xA9: return Clamp(0, industry->last_prod_year - 1920, 255);
 
		case 0xAA: return industry->counter;
 
		case 0xAB: return GB(industry->counter, 8, 8);
 
		case 0xAC: return industry->was_cargo_delivered;
 

	
 
		case 0xB0: return clamp(0, industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date when built since 1920 (in days)
 
		case 0xB0: return Clamp(0, industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date when built since 1920 (in days)
 
		case 0xB3: return industry->construction_type; // Construction type
 
		case 0xB4: return clamp(0, industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date last cargo accepted since 1920 (in days)
 
		case 0xB4: return Clamp(0, industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date last cargo accepted since 1920 (in days)
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
 

	
 
	*available = false;
 
	return (uint32)-1;
 
@@ -512,16 +512,16 @@ void IndustryProductionCallback(Industry
 
		const SpriteGroup *group = Resolve(spec->grf_prop.spritegroup, &object);
 
		if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
 

	
 
		bool deref = (group->g.indprod.version == 1);
 

	
 
		for (uint i = 0; i < 3; i++) {
 
			ind->incoming_cargo_waiting[i] = clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref), 0, 0xFFFF);
 
			ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref), 0, 0xFFFF);
 
		}
 
		for (uint i = 0; i < 2; i++) {
 
			ind->produced_cargo_waiting[i] = clamp(ind->produced_cargo_waiting[i] + DerefIndProd(group->g.indprod.add_output[i], deref), 0, 0xFFFF);
 
			ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + DerefIndProd(group->g.indprod.add_output[i], deref), 0, 0xFFFF);
 
		}
 

	
 
		int32 again = DerefIndProd(group->g.indprod.again, deref);
 
		if (again == 0) break;
 

	
 
		SB(object.callback_param2, 24, 8, again);
src/newgrf_industrytiles.cpp
Show inline comments
 
@@ -241,13 +241,13 @@ bool DrawNewIndustryTile(TileInfo *ti, I
 
	group = Resolve(inds->grf_prop.spritegroup, &object);
 
	if (group == NULL || group->type != SGT_TILELAYOUT) {
 
		return false;
 
	} else {
 
		/* Limit the building stage to the number of stages supplied. */
 
		byte stage = GetIndustryConstructionStage(ti->tile);
 
		stage = clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
 
		stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
 
		IndustryDrawTileLayout(ti, group, i->random_color, stage, gfx);
 
		return true;
 
	}
 
}
 

	
 
extern bool IsSlopeRefused(Slope current, Slope refused);
 
@@ -288,13 +288,13 @@ void AnimateNewIndustryTile(TileIndex ti
 
	IndustryGfx gfx = GetIndustryGfx(tile);
 
	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
 
	byte animation_speed = itspec->animation_speed;
 

	
 
	if (HASBIT(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
 
		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
 
		if (callback_res != CALLBACK_FAILED) animation_speed = clamp(callback_res & 0xFF, 0, 16);
 
		if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
 
	}
 

	
 
	/* An animation speed of 2 means the animation frame changes 4 ticks, and
 
	 * increasing this value by one doubles the wait. 0 is the minimum value
 
	 * allowed for animation_speed, which corresponds to 30ms, and 16 is the
 
	 * maximum, corresponding to around 33 minutes. */
src/newgrf_spritegroup.cpp
Show inline comments
 
@@ -81,13 +81,13 @@ TemporaryStorageArray<uint32, 0x110> _te
 

	
 
static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
 
{
 
	/* Return common variables */
 
	switch (variable) {
 
		case 0x00: return max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
 
		case 0x01: return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
		case 0x01: return Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
		case 0x02: return _cur_month;
 
		case 0x03: return _opt.landscape;
 
		case 0x09: return _date_fract;
 
		case 0x0A: return _tick_counter;
 
		case 0x0C: return object->callback;
 
		case 0x10: return object->callback_param1;
src/newgrf_station.cpp
Show inline comments
 
@@ -389,13 +389,13 @@ static uint32 StationGetVariable(const R
 
			case 0x46:
 
			case 0x47:
 
			case 0x49: return 0x2110000;       // Platforms, tracks & position
 
			case 0x42: return 0;               // Rail type (XXX Get current type from GUI?)
 
			case 0x43: return _current_player; // Station owner
 
			case 0x44: return 2;               // PBS status
 
			case 0xFA: return clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
 
			case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
 
		}
 

	
 
		*available = false;
 
		return UINT_MAX;
 
	}
 

	
 
@@ -448,13 +448,13 @@ static uint32 StationGetVariable(const R
 
		case 0xF0: return st->facilities;
 
		case 0xF1: return st->airport_type;
 
		case 0xF2: return st->truck_stops->status;
 
		case 0xF3: return st->bus_stops->status;
 
		case 0xF6: return st->airport_flags;
 
		case 0xF7: return GB(st->airport_flags, 8, 8);
 
		case 0xFA: return clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
 
		case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
 
	}
 

	
 
	/* Handle cargo variables with parameter, 0x60 to 0x65 */
 
	if (variable >= 0x60 && variable <= 0x65) {
 
		CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile);
 

	
src/npf.cpp
Show inline comments
 
@@ -101,16 +101,16 @@ static TileIndex CalcClosestStationTile(
 
	uint maxy = miny + st->trainst_h - 1;
 
	uint x;
 
	uint y;
 

	
 
	/* we are going the aim for the x coordinate of the closest corner
 
	 * but if we are between those coordinates, we will aim for our own x coordinate */
 
	x = clamp(TileX(tile), minx, maxx);
 
	x = Clamp(TileX(tile), minx, maxx);
 

	
 
	/* same for y coordinate, see above comment */
 
	y = clamp(TileY(tile), miny, maxy);
 
	y = Clamp(TileY(tile), miny, maxy);
 

	
 
	/* return the tile of our target coordinates */
 
	return TileXY(x, y);
 
}
 

	
 
/* Calcs the heuristic to the target station or tile. For train stations, it
src/oldloader.cpp
Show inline comments
 
@@ -1632,13 +1632,13 @@ static bool LoadOldMain(LoadgameState *l
 
	FixOldStations();
 
	FixOldVehicles();
 

	
 
	AddTypeToEngines();
 

	
 
	/* We have a new difficulty setting */
 
	_opt.diff.town_council_tolerance = clamp(_opt.diff_level, 0, 2);
 
	_opt.diff.town_council_tolerance = Clamp(_opt.diff_level, 0, 2);
 

	
 
	DEBUG(oldloader, 3, "Finished converting game data");
 
	DEBUG(oldloader, 1, "TTD(Patch) savegame successfully converted");
 

	
 
	return true;
 
}
src/openttd.cpp
Show inline comments
 
@@ -277,14 +277,14 @@ static void ParseResolution(int res[2], 
 
	const char *t = strchr(s, 'x');
 
	if (t == NULL) {
 
		ShowInfoF("Invalid resolution '%s'", s);
 
		return;
 
	}
 

	
 
	res[0] = clamp(strtoul(s, NULL, 0), 64, MAX_SCREEN_WIDTH);
 
	res[1] = clamp(strtoul(t + 1, NULL, 0), 64, MAX_SCREEN_HEIGHT);
 
	res[0] = Clamp(strtoul(s, NULL, 0), 64, MAX_SCREEN_WIDTH);
 
	res[1] = Clamp(strtoul(t + 1, NULL, 0), 64, MAX_SCREEN_HEIGHT);
 
}
 

	
 
static void InitializeDynamicVariables()
 
{
 
	/* Dynamic stuff needs to be initialized somewhere... */
 
	_town_sort     = NULL;
src/players.cpp
Show inline comments
 
@@ -92,13 +92,13 @@ PlayerFace ConvertFromOldPlayerFace(uint
 

	
 
	if (HASBIT(face, 31)) SetBitT(ge, GENDER_FEMALE);
 
	if (HASBIT(face, 27) && (HASBIT(face, 26) == HASBIT(face, 19))) SetBitT(ge, ETHNICITY_BLACK);
 

	
 
	SetPlayerFaceBits(pf, PFV_GEN_ETHN,    ge, ge);
 
	SetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
 
	SetPlayerFaceBits(pf, PFV_EYE_COLOUR,  ge, HASBIT(ge, ETHNICITY_BLACK) ? 0 : clampu(GB(face, 20, 3), 5, 7) - 5);
 
	SetPlayerFaceBits(pf, PFV_EYE_COLOUR,  ge, HASBIT(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
 
	SetPlayerFaceBits(pf, PFV_CHIN,        ge, ScalePlayerFaceValue(PFV_CHIN,     ge, GB(face,  4, 2)));
 
	SetPlayerFaceBits(pf, PFV_EYEBROWS,    ge, ScalePlayerFaceValue(PFV_EYEBROWS, ge, GB(face,  6, 4)));
 
	SetPlayerFaceBits(pf, PFV_HAIR,        ge, ScalePlayerFaceValue(PFV_HAIR,     ge, GB(face, 16, 4)));
 
	SetPlayerFaceBits(pf, PFV_JACKET,      ge, ScalePlayerFaceValue(PFV_JACKET,   ge, GB(face, 20, 2)));
 
	SetPlayerFaceBits(pf, PFV_COLLAR,      ge, ScalePlayerFaceValue(PFV_COLLAR,   ge, GB(face, 22, 2)));
 
	SetPlayerFaceBits(pf, PFV_GLASSES,     ge, GB(face, 28, 1));
src/rail_gui.cpp
Show inline comments
 
@@ -1106,13 +1106,13 @@ static void ShowStationBuilder()
 
		w = AllocateWindowDesc(&_newstation_builder_desc);
 
		_railstation.newstations = true;
 
		_railstation.station_count = GetNumCustomStations(_railstation.station_class);
 

	
 
		w->vscroll.count = _railstation.station_count;
 
		w->vscroll.cap   = 5;
 
		w->vscroll.pos   = clamp(_railstation.station_type - 2, 0, w->vscroll.count - w->vscroll.cap);
 
		w->vscroll.pos   = Clamp(_railstation.station_type - 2, 0, w->vscroll.count - w->vscroll.cap);
 
	}
 
}
 

	
 
/** Enum referring to the widgets of the build rail depot window */
 
enum BuildRailDepotWidgets {
 
	BRDW_CLOSEBOX = 0,
src/roadveh_cmd.cpp
Show inline comments
 
@@ -126,13 +126,13 @@ static CommandCost EstimateRoadVehCost(E
 
byte GetRoadVehLength(const Vehicle *v)
 
{
 
	byte length = 8;
 

	
 
	uint16 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
 
	if (veh_len != CALLBACK_FAILED) {
 
		length -= clamp(veh_len, 0, 7);
 
		length -= Clamp(veh_len, 0, 7);
 
	}
 

	
 
	return length;
 
}
 

	
 
void RoadVehUpdateCache(Vehicle *v)
src/screenshot.cpp
Show inline comments
 
@@ -120,13 +120,13 @@ static bool MakeBmpImage(const char *nam
 
	/* write file header and info header and palette */
 
	if (fwrite(&bfh, sizeof(bfh), 1, f) != 1) return false;
 
	if (fwrite(&bih, sizeof(bih), 1, f) != 1) return false;
 
	if (pixelformat == 8) if (fwrite(rq, sizeof(rq), 1, f) != 1) return false;
 

	
 
	/* use by default 64k temp memory */
 
	maxlines = clamp(65536 / padw, 16, 128);
 
	maxlines = Clamp(65536 / padw, 16, 128);
 

	
 
	/* now generate the bitmap bits */
 
	void *buff = MallocT<uint8>(padw * maxlines * bpp); // by default generate 128 lines at a time.
 
	if (buff == NULL) {
 
		fclose(f);
 
		return false;
 
@@ -248,13 +248,13 @@ static bool MakePNGImage(const char *nam
 
#else
 
		png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
 
#endif
 
	}
 

	
 
	/* use by default 64k temp memory */
 
	maxlines = clamp(65536 / w, 16, 128);
 
	maxlines = Clamp(65536 / w, 16, 128);
 

	
 
	/* now generate the bitmap bits */
 
	void *buff = MallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
 
	if (buff == NULL) {
 
		png_destroy_write_struct(&png_ptr, &info_ptr);
 
		fclose(f);
 
@@ -348,13 +348,13 @@ static bool MakePCXImage(const char *nam
 
	if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
 
		fclose(f);
 
		return false;
 
	}
 

	
 
	/* use by default 64k temp memory */
 
	maxlines = clamp(65536 / w, 16, 128);
 
	maxlines = Clamp(65536 / w, 16, 128);
 

	
 
	/* now generate the bitmap bits */
 
	uint8 *buff = MallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
 
	if (buff == NULL) {
 
		fclose(f);
 
		return false;
src/settings.cpp
Show inline comments
 
@@ -662,18 +662,18 @@ static void Write_ValidateSetting(void *
 
			case SLE_VAR_U8:
 
			case SLE_VAR_I16:
 
			case SLE_VAR_U16:
 
			case SLE_VAR_I32: {
 
				/* Override the minimum value. No value below sdb->min, except special value 0 */
 
				int32 min = ((sdb->flags & SGF_0ISDISABLED) && val <= sdb->min) ? 0 : sdb->min;
 
				val = clamp(val, min, sdb->max);
 
				val = Clamp(val, min, sdb->max);
 
			} break;
 
			case SLE_VAR_U32: {
 
				/* Override the minimum value. No value below sdb->min, except special value 0 */
 
				uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
 
				WriteValue(ptr, SLE_VAR_U32, (int64)clampu(val, min, sdb->max));
 
				WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
 
				return;
 
			}
 
			case SLE_VAR_I64:
 
			case SLE_VAR_U64:
 
			default: NOT_REACHED(); break;
 
		}
src/settings_gui.cpp
Show inline comments
 
@@ -426,13 +426,13 @@ void CheckDifficultyLevels()
 
{
 
	if (_opt_newgame.diff_level != 3) {
 
		SetDifficultyLevel(_opt_newgame.diff_level, &_opt_newgame);
 
	} else {
 
		for (uint i = 0; i < GAME_DIFFICULTY_NUM; i++) {
 
			GDType *diff = ((GDType*)&_opt_newgame.diff) + i;
 
			*diff = clamp(*diff, _game_setting_info[i].min, _game_setting_info[i].max);
 
			*diff = Clamp(*diff, _game_setting_info[i].min, _game_setting_info[i].max);
 
			*diff -= *diff % _game_setting_info[i].step;
 
		}
 
	}
 
}
 

	
 
extern void StartupEconomy();
 
@@ -1146,13 +1146,13 @@ static void CustCurrencyWndProc(Window *
 
						if (x < 20) {
 
							_custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ?
 
								CF_NOEURO : _custom_currency.to_euro - 1;
 
							WP(w,def_d).data_1 = 1 << (line * 2 + 0);
 
						} else {
 
							_custom_currency.to_euro =
 
								clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
 
								Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
 
							WP(w,def_d).data_1 = 1 << (line * 2 + 1);
 
						}
 
					} else { // enter text
 
						SetDParam(0, _custom_currency.to_euro);
 
						str = STR_CONFIG_PATCHES_INT32;
 
						len = 4;
 
@@ -1173,13 +1173,13 @@ static void CustCurrencyWndProc(Window *
 

	
 
		case WE_ON_EDIT_TEXT: {
 
				const char *b = e->we.edittext.str;
 

	
 
				switch (WP(w,def_d).data_2) {
 
					case 0: /* Exchange rate */
 
						_custom_currency.rate = clamp(atoi(b), 1, 5000);
 
						_custom_currency.rate = Clamp(atoi(b), 1, 5000);
 
						break;
 

	
 
					case 1: /* Thousands seperator */
 
						_custom_currency.separator = (b[0] == '\0') ? ' ' : b[0];
 
						ttd_strlcpy(_str_separator, b, lengthof(_str_separator));
 
						break;
src/sound.cpp
Show inline comments
 
@@ -145,13 +145,13 @@ static void StartSound(uint sound, int p
 

	
 
	if (volume == 0) return;
 
	mc = MxAllocateChannel();
 
	if (mc == NULL) return;
 
	if (!SetBankSource(mc, sound)) return;
 

	
 
	panning = clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
 
	panning = Clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
 
	left_vol = (volume * PANNING_LEVELS) - (volume * panning);
 
	right_vol = (volume * PANNING_LEVELS) + (volume * panning);
 
	MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS);
 
	MxActivateChannel(mc);
 
}
 

	
src/station_cmd.cpp
Show inline comments
 
@@ -668,13 +668,13 @@ static void UpdateStationSignCoord(Stati
 
{
 
	const StationRect *r = &st->rect;
 

	
 
	if (r->IsEmpty()) return; /* no tiles belong to this station */
 

	
 
	/* clamp sign coord to be inside the station rect */
 
	st->xy = TileXY(clampu(TileX(st->xy), r->left, r->right), clampu(TileY(st->xy), r->top, r->bottom));
 
	st->xy = TileXY(ClampU(TileX(st->xy), r->left, r->right), ClampU(TileY(st->xy), r->top, r->bottom));
 
	UpdateStationVirtCoordDirty(st);
 
}
 

	
 
/** This is called right after a station was deleted.
 
 * It checks if the whole station is free of substations, and if so, the station will be
 
 * deleted after a little while.
 
@@ -2484,13 +2484,13 @@ static void UpdateStationRating(Station 
 
			(rating += 10, true);
 

	
 
			{
 
				int or_ = ge->rating; // old rating
 

	
 
				/* only modify rating in steps of -2, -1, 0, 1 or 2 */
 
				ge->rating = rating = or_ + clamp(clamp(rating, 0, 255) - or_, -2, 2);
 
				ge->rating = rating = or_ + Clamp(Clamp(rating, 0, 255) - or_, -2, 2);
 

	
 
				/* if rating is <= 64 and more than 200 items waiting,
 
				 * remove some random amount of goods from the station */
 
				if (rating <= 64 && waiting >= 200) {
 
					int dec = Random() & 0x1F;
 
					if (waiting < 400) dec &= 7;
 
@@ -2574,13 +2574,13 @@ void ModifyStationRatingAround(TileIndex
 
		if (st->owner == owner &&
 
				DistanceManhattan(tile, st->xy) <= radius) {
 
			for (CargoID i = 0; i < NUM_CARGO; i++) {
 
				GoodsEntry* ge = &st->goods[i];
 

	
 
				if (ge->acceptance_pickup != 0) {
 
					ge->rating = clamp(ge->rating + amount, 0, 255);
 
					ge->rating = Clamp(ge->rating + amount, 0, 255);
 
				}
 
			}
 
		}
 
	}
 
}
 

	
src/town_map.h
Show inline comments
 
@@ -280,13 +280,13 @@ static inline void IncHouseConstructionT
 
	AB(_m[t].m5, 0, 5, 1);
 

	
 
	if (GB(_m[t].m5, 3, 2) == TOWN_HOUSE_COMPLETED) {
 
		/* House is now completed.
 
		 * Store the year of construction as well, for newgrf house purpose */
 
		SetHouseCompleted(t, true);
 
		_m[t].m5 = clamp(_cur_year - ORIGINAL_BASE_YEAR, 0, 0xFF);
 
		_m[t].m5 = Clamp(_cur_year - ORIGINAL_BASE_YEAR, 0, 0xFF);
 
	}
 
}
 

	
 
/**
 
 * Get the year that this house was constructed (between 1920 and 2175).
 
 * @param t the tile of this house
src/train_cmd.cpp
Show inline comments
 
@@ -241,13 +241,13 @@ void TrainConsistChanged(Vehicle* v)
 
		/* check the vehicle length (callback) */
 
		uint16 veh_len = CALLBACK_FAILED;
 
		if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
 
			veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
 
		}
 
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
 
		veh_len = clamp(veh_len, 0, u->Next() == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code
 
		veh_len = Clamp(veh_len, 0, u->Next() == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code
 
		u->u.rail.cached_veh_length = 8 - veh_len;
 
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
 
	}
 

	
 
	/* store consist weight/max speed in cache */
 
	v->u.rail.cached_max_speed = max_speed;
 
@@ -349,13 +349,13 @@ static int GetTrainAcceleration(Vehicle 
 
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
 
		int total = curvecount[0] + curvecount[1];
 

	
 
		if (curvecount[0] == 1 && curvecount[1] == 1) {
 
			max_speed = 0xFFFF;
 
		} else if (total > 1) {
 
			max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12));
 
			max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
 
		}
 
	}
 

	
 
	max_speed += (max_speed / 2) * v->u.rail.railtype;
 

	
 
	if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) {
 
@@ -452,13 +452,13 @@ static void UpdateTrainAcceleration(Vehi
 

	
 
	v->max_speed = v->u.rail.cached_max_speed;
 

	
 
	uint power = v->u.rail.cached_power;
 
	uint weight = v->u.rail.cached_weight;
 
	assert(weight != 0);
 
	v->acceleration = clamp(power / weight * 4, 1, 255);
 
	v->acceleration = Clamp(power / weight * 4, 1, 255);
 
}
 

	
 
int Train::GetImage(Direction direction) const
 
{
 
	int img = this->spritenum;
 
	int base;
 
@@ -2510,13 +2510,13 @@ static int UpdateTrainSpeed(Vehicle *v)
 
	uint spd = v->subspeed + accel * 2;
 
	v->subspeed = (byte)spd;
 
	{
 
		int tempmax = v->max_speed;
 
		if (v->cur_speed > v->max_speed)
 
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
 
		v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
 
		v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
 
	}
 

	
 
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
 

	
 
	spd += v->progress;
 
	v->progress = (byte)spd;
src/vehicle.cpp
Show inline comments
 
@@ -1349,14 +1349,14 @@ Vehicle *CreateEffectVehicle(int x, int 
 
	}
 
	return v;
 
}
 

	
 
Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type)
 
{
 
	int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	int safe_y = clamp(y, 0, MapMaxY() * TILE_SIZE);
 
	int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	int safe_y = Clamp(y, 0, MapMaxY() * TILE_SIZE);
 
	return CreateEffectVehicle(x, y, GetSlopeZ(safe_x, safe_y) + z, type);
 
}
 

	
 
Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type)
 
{
 
	return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type);
src/video/cocoa_v.mm
Show inline comments
 
@@ -1241,13 +1241,13 @@ static const char* QZ_SetVideoWindowed(u
 
		[ _cocoa_video_data.window setDelegate: [ [ [ OTTD_QuartzWindowDelegate alloc ] init ] autorelease ] ];
 
	} else {
 
		/* We already have a window, just change its size */
 
		if (!isCustom) {
 
			[ _cocoa_video_data.window setContentSize:contentRect.size ];
 
			// Ensure frame height - title bar height >= view height
 
			contentRect.size.height = clamp(height, 0, [ _cocoa_video_data.window frame ].size.height - 22 /* 22 is the height of title bar of window*/);
 
			contentRect.size.height = Clamp(height, 0, [ _cocoa_video_data.window frame ].size.height - 22 /* 22 is the height of title bar of window*/);
 
			height = contentRect.size.height;
 
			[ _cocoa_video_data.qdview setFrameSize:contentRect.size ];
 
		}
 
	}
 

	
 
	// Update again
src/video/sdl_v.cpp
Show inline comments
 
@@ -417,14 +417,14 @@ static int PollEvent()
 
			} else {
 
				HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym));
 
			}
 
			break;
 

	
 
		case SDL_VIDEORESIZE: {
 
			int w = clamp(ev.resize.w, 64, MAX_SCREEN_WIDTH);
 
			int h = clamp(ev.resize.h, 64, MAX_SCREEN_HEIGHT);
 
			int w = Clamp(ev.resize.w, 64, MAX_SCREEN_WIDTH);
 
			int h = Clamp(ev.resize.h, 64, MAX_SCREEN_HEIGHT);
 
			ChangeResInGame(w, h);
 
			break;
 
		}
 
	}
 
	return -1;
 
}
src/video/win32_v.cpp
Show inline comments
 
@@ -542,14 +542,14 @@ static LRESULT CALLBACK WndProcGdi(HWND 
 

	
 
			SetRect(&r2, 0, 0, 0, 0);
 
			AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
 

	
 
			w = r->right - r->left - (r2.right - r2.left);
 
			h = r->bottom - r->top - (r2.bottom - r2.top);
 
			w = clamp(w, 64, MAX_SCREEN_WIDTH);
 
			h = clamp(h, 64, MAX_SCREEN_HEIGHT);
 
			w = Clamp(w, 64, MAX_SCREEN_WIDTH);
 
			h = Clamp(h, 64, MAX_SCREEN_HEIGHT);
 
			SetRect(&r2, 0, 0, w, h);
 

	
 
			AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
 
			w = r2.right - r2.left;
 
			h = r2.bottom - r2.top;
 

	
 
@@ -674,14 +674,14 @@ static void RegisterWndClass()
 
static bool AllocateDibSection(int w, int h)
 
{
 
	BITMAPINFO *bi;
 
	HDC dc;
 
	int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
 

	
 
	w = clamp(w, 64, MAX_SCREEN_WIDTH);
 
	h = clamp(h, 64, MAX_SCREEN_HEIGHT);
 
	w = Clamp(w, 64, MAX_SCREEN_WIDTH);
 
	h = Clamp(h, 64, MAX_SCREEN_HEIGHT);
 

	
 
	if (bpp == 0) error("Can't use a blitter that blits 0 bpp for normal visuals");
 

	
 
	if (w == _screen.width && h == _screen.height)
 
		return false;
 

	
src/viewport.cpp
Show inline comments
 
@@ -369,14 +369,14 @@ static Point TranslateXYToTileCoord(cons
 
	a = y-x;
 
	b = y+x;
 

	
 
	/* we need to move variables in to the valid range, as the
 
	 * GetTileZoomCenterWindow() function can call here with invalid x and/or y,
 
	 * when the user tries to zoom out along the sides of the map */
 
	a = clamp(a, 0, (int)(MapMaxX() * TILE_SIZE) - 1);
 
	b = clamp(b, 0, (int)(MapMaxY() * TILE_SIZE) - 1);
 
	a = Clamp(a, 0, (int)(MapMaxX() * TILE_SIZE) - 1);
 
	b = Clamp(b, 0, (int)(MapMaxY() * TILE_SIZE) - 1);
 

	
 
	/* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
 
	 * Now find the Z-world coordinate by fix point iteration.
 
	 * This is a bit tricky because the tile height is non-continuous at foundations.
 
	 * The clicked point should be approached from the back, otherwise there are regions that are not clickable.
 
	 * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
 
@@ -1646,14 +1646,14 @@ static inline void ClampViewportToMap(co
 
	/* Convert viewport coordinates to map coordinates
 
	 * Calculation is scaled by 4 to avoid rounding errors */
 
	int vx = -x + y * 2;
 
	int vy =  x + y * 2;
 

	
 
	/* clamp to size of map */
 
	vx = clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
 
	vy = clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
 
	vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
 
	vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
 

	
 
	/* Convert map coordinates to viewport coordinates */
 
	x = (-vx + vy) / 2;
 
	y = ( vx + vy) / 4;
 

	
 
	/* Remove centreing */
 
@@ -1678,14 +1678,14 @@ void UpdateViewportPosition(Window *w)
 
		int delta_y = WP(w, vp_d).dest_scrollpos_y - WP(w, vp_d).scrollpos_y;
 

	
 
		if (delta_x != 0 || delta_y != 0) {
 
			if (_patches.smooth_scroll) {
 
				int max_scroll = ScaleByMapSize1D(512);
 
				/* Not at our desired positon yet... */
 
				WP(w, vp_d).scrollpos_x += clamp(delta_x / 4, -max_scroll, max_scroll);
 
				WP(w, vp_d).scrollpos_y += clamp(delta_y / 4, -max_scroll, max_scroll);
 
				WP(w, vp_d).scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
 
				WP(w, vp_d).scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
 
			} else {
 
				WP(w, vp_d).scrollpos_x = WP(w, vp_d).dest_scrollpos_x;
 
				WP(w, vp_d).scrollpos_y = WP(w, vp_d).dest_scrollpos_y;
 
			}
 
		}
 

	
 
@@ -2716,14 +2716,14 @@ calc_heightdiff_single_direction:;
 

	
 
				GuiShowTooltipsWithArgs(measure_strings_length[index], index, params);
 
			} break;
 

	
 
		case VPM_X_AND_Y_LIMITED: { /* drag an X by Y constrained rect area */
 
			int limit = (_thd.sizelimit - 1) * TILE_SIZE;
 
			x = sx + clamp(x - sx, -limit, limit);
 
			y = sy + clamp(y - sy, -limit, limit);
 
			x = sx + Clamp(x - sx, -limit, limit);
 
			y = sy + Clamp(y - sy, -limit, limit);
 
			} /* Fallthrough */
 
		case VPM_X_AND_Y: { /* drag an X by Y area */
 
			if (_patches.measure_tooltip) {
 
				static const StringID measure_strings_area[] = {
 
					STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
 
				};
src/window.cpp
Show inline comments
 
@@ -193,13 +193,13 @@ static void DispatchMouseWheelEvent(Wind
 
	 * XXX - should be rewritten as a widget-dependent scroller but that's
 
	 * not happening until someone rewrites the whole widget-code */
 
	if ((sb = &w->vscroll,  wi1->type == WWT_SCROLLBAR)  || (sb = &w->vscroll2, wi1->type == WWT_SCROLL2BAR)  ||
 
			(sb = &w->vscroll2, wi2->type == WWT_SCROLL2BAR) || (sb = &w->vscroll, wi2->type == WWT_SCROLLBAR) ) {
 

	
 
		if (sb->count > sb->cap) {
 
			int pos = clamp(sb->pos + wheel, 0, sb->count - sb->cap);
 
			int pos = Clamp(sb->pos + wheel, 0, sb->count - sb->cap);
 
			if (pos != sb->pos) {
 
				sb->pos = pos;
 
				SetWindowDirty(w);
 
			}
 
		}
 
	}
 
@@ -1292,14 +1292,14 @@ static bool HandleWindowDragging()
 
					}
 
				}
 
			}
 

	
 
			/* Make sure the window doesn't leave the screen
 
			 * 13 is the height of the title bar */
 
			nx = clamp(nx, 13 - t->right, _screen.width - 13 - t->left);
 
			ny = clamp(ny, 0, _screen.height - 13);
 
			nx = Clamp(nx, 13 - t->right, _screen.width - 13 - t->left);
 
			ny = Clamp(ny, 0, _screen.height - 13);
 

	
 
			/* Make sure the title bar isn't hidden by behind the main tool bar */
 
			v = FindWindowById(WC_MAIN_TOOLBAR, 0);
 
			if (v != NULL) {
 
				int v_bottom = v->top + v->height;
 
				int v_right = v->left + v->width;
 
@@ -2110,19 +2110,19 @@ void RelocateAllWindows(int neww, int ne
 
			case WC_NEWS_WINDOW:
 
				top = newh - w->height;
 
				left = (neww - w->width) >> 1;
 
				break;
 

	
 
			case WC_STATUS_BAR:
 
				ResizeWindow(w, clamp(neww, 320, 640) - w->width, 0);
 
				ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
 
				top = newh - w->height;
 
				left = (neww - w->width) >> 1;
 
				break;
 

	
 
			case WC_SEND_NETWORK_MSG:
 
				ResizeWindow(w, clamp(neww, 320, 640) - w->width, 0);
 
				ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
 
				top = (newh - 26); // 26 = height of status bar + height of chat bar
 
				left = (neww - w->width) >> 1;
 
				break;
 

	
 
			case WC_CONSOLE:
 
				IConsoleResize(w);
0 comments (0 inline, 0 general)