Changeset - r5955:5be18be294d0
[Not reviewed]
master
0 13 0
bjarni - 17 years ago 2007-02-07 19:10:19
bjarni@openttd.org
(svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
This means that "v->type" can be used as array indexes instead of VehTypeToIndex() (or "v->type - VEH_Train/0x10 as the code still used in some places)
Surprisingly this can be done without changing the savegame format
13 files changed with 45 insertions and 55 deletions:
0 comments (0 inline, 0 general)
src/autoreplace_gui.cpp
Show inline comments
 
@@ -251,7 +251,7 @@ static void ReplaceVehicleWndProc(Window
 
										 !EngineHasReplacementForPlayer(p, selected_id[0]));
 

	
 
			/* now the actual drawing of the window itself takes place */
 
			SetDParam(0, _vehicle_type_names[VehTypeToIndex(w->window_number)]);
 
			SetDParam(0, _vehicle_type_names[w->window_number]);
 

	
 
			if (w->window_number == VEH_Train) {
 
				/* set on/off for renew_keep_length */
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -712,7 +712,7 @@ static void GenerateBuildList(Window *w)
 
			break;
 
	}
 
	_internal_sort_order = bv->descending_sort_order;
 
	EngList_Sort(&bv->eng_list, _sorter[VehTypeToIndex(bv->vehicle_type)][bv->sort_criteria]);
 
	EngList_Sort(&bv->eng_list, _sorter[bv->vehicle_type][bv->sort_criteria]);
 
}
 

	
 
static void DrawVehicleEngine(byte type, int x, int y, EngineID engine, SpriteID pal)
 
@@ -799,7 +799,7 @@ static void DrawBuildVehicleWindow(Windo
 
		DrawVehiclePurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, bv->sel_engine);
 
	}
 

	
 
	DrawString(85, 15, _sort_listing[VehTypeToIndex(bv->vehicle_type)][bv->sort_criteria], 0x10);
 
	DrawString(85, 15, _sort_listing[bv->vehicle_type][bv->sort_criteria], 0x10);
 
	DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10);
 
}
 

	
 
@@ -810,7 +810,7 @@ static void BuildVehicleClickEvent(Windo
 
	switch (e->we.click.widget) {
 
		case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
 
			bv->descending_sort_order ^= true;
 
			_last_sort_order[VehTypeToIndex(bv->vehicle_type)] = bv->descending_sort_order;
 
			_last_sort_order[bv->vehicle_type] = bv->descending_sort_order;
 
			bv->regenerate_list = true;
 
			SetWindowDirty(w);
 
			break;
 
@@ -824,7 +824,7 @@ static void BuildVehicleClickEvent(Windo
 
		}
 

	
 
		case BUILD_VEHICLE_WIDGET_SORT_TEXT: case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu
 
			ShowDropDownMenu(w, _sort_listing[VehTypeToIndex(bv->vehicle_type)], bv->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
 
			ShowDropDownMenu(w, _sort_listing[bv->vehicle_type], bv->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
 
			break;
 

	
 
		case BUILD_VEHICLE_WIDGET_BUILD: {
 
@@ -912,7 +912,7 @@ static void NewVehicleWndProc(Window *w,
 
		case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */
 
			if (bv->sort_criteria != e->we.dropdown.index) {
 
				bv->sort_criteria = e->we.dropdown.index;
 
				_last_sort_criteria[VehTypeToIndex(bv->vehicle_type)] = bv->sort_criteria;
 
				_last_sort_criteria[bv->vehicle_type] = bv->sort_criteria;
 
				bv->regenerate_list = true;
 
			}
 
			SetWindowDirty(w);
 
@@ -961,8 +961,8 @@ void ShowBuildVehicleWindow(TileIndex ti
 
	bv->vehicle_type    = type;
 
	bv->regenerate_list = false;
 

	
 
	bv->sort_criteria         = _last_sort_criteria[VehTypeToIndex(type)];
 
	bv->descending_sort_order = _last_sort_order[VehTypeToIndex(type)];
 
	bv->sort_criteria         = _last_sort_criteria[type];
 
	bv->descending_sort_order = _last_sort_order[type];
 

	
 
	switch (type) {
 
		case VEH_Train:
src/date.cpp
Show inline comments
 
@@ -215,7 +215,7 @@ static void RunVehicleDayProc(uint dayti
 
	for (i = daytick; i < total; i += DAY_TICKS) {
 
		Vehicle *v = GetVehicle(i);
 

	
 
		if (IsValidVehicle(v)) _on_new_vehicle_day_proc[v->type - 0x10](v);
 
		if (IsValidVehicle(v)) _on_new_vehicle_day_proc[v->type](v);
 
	}
 
}
 

	
src/depot_gui.cpp
Show inline comments
 
@@ -743,7 +743,7 @@ static void DepotWndProc(Window *w, Wind
 
						};
 

	
 
						_place_clicked_vehicle = NULL;
 
						SetObjectToPlaceWnd(clone_icons[WP(w, depot_d).type - VEH_Train], PAL_NONE, VHM_RECT, w);
 
						SetObjectToPlaceWnd(clone_icons[WP(w, depot_d).type], PAL_NONE, VHM_RECT, w);
 
					} else {
 
						ResetObjectToPlace();
 
					}
 
@@ -770,7 +770,7 @@ static void DepotWndProc(Window *w, Wind
 

	
 
						SetDParam(0, (vehtype == VEH_Aircraft) ? GetStationIndex(tile) : GetDepotByTile(tile)->town_index);
 
						ShowQuery(
 
							confirm_captions[vehtype - VEH_Train],
 
							confirm_captions[vehtype],
 
							STR_DEPOT_SELL_CONFIRMATION_TEXT,
 
							w,
 
							DepotSellAllConfirmationCallback
src/economy.cpp
Show inline comments
 
@@ -1503,7 +1503,7 @@ int LoadUnloadVehicle(Vehicle *v, bool j
 
		 * on the vehicle type - the values here are those found in TTDPatch */
 
		uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
 

	
 
		unloading_time = gradual_loading_wait_time[v->type - VEH_Train];
 
		unloading_time = gradual_loading_wait_time[v->type];
 
		if (HASBIT(v->load_status, LS_LOADING_FINISHED)) {
 
			if (anything_loaded) {
 
				unloading_time += 20;
src/engine.h
Show inline comments
 
@@ -163,7 +163,7 @@ static inline EngineID GetFirstEngineOfT
 
{
 
	const EngineID start[] = {0, ROAD_ENGINES_INDEX, SHIP_ENGINES_INDEX, AIRCRAFT_ENGINES_INDEX};
 

	
 
	return start[VehTypeToIndex(type)];
 
	return start[type];
 
}
 

	
 
static inline EngineID GetLastEngineOfType(byte type)
 
@@ -174,7 +174,7 @@ static inline EngineID GetLastEngineOfTy
 
		SHIP_ENGINES_INDEX + NUM_SHIP_ENGINES,
 
		AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES};
 

	
 
	return end[VehTypeToIndex(type)];
 
	return end[type];
 
}
 

	
 
VARDEF Engine _engines[TOTAL_NUM_ENGINES];
src/newgrf.cpp
Show inline comments
 
@@ -3617,7 +3617,7 @@ static void CalculateRefitMasks(void)
 
							RailVehInfo(engine)->railveh_type != RAILVEH_WAGON
 
						)
 
					)) {
 
				xor_mask = _default_refitmasks[GetEngine(engine)->type - VEH_Train];
 
				xor_mask = _default_refitmasks[GetEngine(engine)->type];
 
			}
 
		}
 
		_engine_info[engine].refit_mask = ((mask & ~not_mask) ^ xor_mask) & _landscape_global_cargo_mask[_opt.landscape];
src/order_cmd.cpp
Show inline comments
 
@@ -160,10 +160,10 @@ void AssignOrder(Order *order, Order dat
 
 */
 
static void DeleteOrderWarnings(const Vehicle* v)
 
{
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_TOO_FEW_ORDERS  + (v->type - VEH_Train) * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_VOID_ORDER      + (v->type - VEH_Train) * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_DUPLICATE_ENTRY + (v->type - VEH_Train) * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_INVALID_ENTRY   + (v->type - VEH_Train) * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_TOO_FEW_ORDERS  + v->type * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_VOID_ORDER      + v->type * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_DUPLICATE_ENTRY + v->type * 4);
 
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_INVALID_ENTRY   + v->type * 4);
 
}
 

	
 

	
 
@@ -1005,7 +1005,7 @@ void CheckOrders(const Vehicle* v)
 
		/* We don't have a problem */
 
		if (problem_type < 0) return;
 

	
 
		message = STR_TRAIN_HAS_TOO_FEW_ORDERS + ((v->type - VEH_Train) << 2) + problem_type;
 
		message = STR_TRAIN_HAS_TOO_FEW_ORDERS + (v->type << 2) + problem_type;
 
		//DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
 

	
 
		SetDParam(0, v->unitnumber);
src/smallmap_gui.cpp
Show inline comments
 
@@ -724,7 +724,7 @@ skip_column:
 

	
 
				// Calculate pointer to pixel and the color
 
				ptr = dpi->dst_ptr + y * dpi->pitch + x;
 
				color = (type == 1) ? _vehicle_type_colors[v->type-0x10] : 0xF;
 
				color = (type == 1) ? _vehicle_type_colors[v->type] : 0xF;
 

	
 
				// And draw either one or two pixels depending on clipping
 
				ptr[0] = color;
src/vehicle.cpp
Show inline comments
 
@@ -84,7 +84,10 @@ static void VehiclePoolNewBlock(uint sta
 

	
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 * TODO - This is just a temporary stage, this will be removed. */
 
	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
 
	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
 
		v->index = start_item++;
 
		v->type  = VEH_Invalid;
 
	}
 
}
 

	
 
/* Initialize the vehicle-pool */
 
@@ -263,6 +266,7 @@ static Vehicle *InitializeVehicle(Vehicl
 

	
 
	assert(v->orders == NULL);
 

	
 
	v->type = VEH_Invalid;
 
	v->left_coord = INVALID_COORD;
 
	v->first = NULL;
 
	v->next = NULL;
 
@@ -656,7 +660,7 @@ void CallVehicleTicks(void)
 
	_first_veh_in_depot_list = NULL; // now we are sure it's initialized at the start of each tick
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		_vehicle_tick_procs[v->type - 0x10](v);
 
		_vehicle_tick_procs[v->type](v);
 

	
 
		switch (v->type) {
 
			case VEH_Train:
 
@@ -1568,7 +1572,7 @@ static void ShowVehicleGettingOld(Vehicl
 
	// Do not show getting-old message if autorenew is active
 
	if (GetPlayer(v->owner)->engine_renew) return;
 

	
 
	SetDParam(0, _vehicle_type_names[v->type - 0x10]);
 
	SetDParam(0, _vehicle_type_names[v->type]);
 
	SetDParam(1, v->unitnumber);
 
	AddNewsItem(msg, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
}
 
@@ -2569,7 +2573,7 @@ void VehicleEnterDepot(Vehicle *v)
 
				v->leave_depot_instantly = false; // We ensure that the vehicle stays in the depot
 
				if (v->owner == _local_player) {
 
					/* Notify the user that we stopped the vehicle */
 
					SetDParam(0, _vehicle_type_names[v->type - 0x10]);
 
					SetDParam(0, _vehicle_type_names[v->type]);
 
					SetDParam(1, v->unitnumber);
 
					AddNewsItem(STR_ORDER_REFIT_FAILED, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
				}
 
@@ -3212,7 +3216,7 @@ static void Save_VEHS(void)
 
	// Write the vehicles
 
	FOR_ALL_VEHICLES(v) {
 
		SlSetArrayIndex(v->index);
 
		SlObject(v, (SaveLoad*)_veh_descs[v->type - 0x10]);
 
		SlObject(v, (SaveLoad*)_veh_descs[v->type]);
 
	}
 
}
 

	
src/vehicle.h
Show inline comments
 
@@ -8,13 +8,13 @@
 
#include "rail.h"
 

	
 
enum {
 
	VEH_Invalid  = 0x00,
 
	VEH_Train    = 0x10,
 
	VEH_Road     = 0x11,
 
	VEH_Ship     = 0x12,
 
	VEH_Aircraft = 0x13,
 
	VEH_Special  = 0x14,
 
	VEH_Disaster = 0x15,
 
	VEH_Train,
 
	VEH_Road,
 
	VEH_Ship,
 
	VEH_Aircraft,
 
	VEH_Special,
 
	VEH_Disaster,
 
	VEH_Invalid = 0xFF,
 
} ;
 

	
 
enum VehStatus {
 
@@ -400,7 +400,7 @@ static inline uint GetNumVehicles(void)
 
 */
 
static inline bool IsValidVehicle(const Vehicle *v)
 
{
 
	return v->type != 0;
 
	return v->type != VEH_Invalid;
 
}
 

	
 
void DestroyVehicle(Vehicle *v);
 
@@ -408,7 +408,7 @@ void DestroyVehicle(Vehicle *v);
 
static inline void DeleteVehicle(Vehicle *v)
 
{
 
	DestroyVehicle(v);
 
	v->type = 0;
 
	v->type = VEH_Invalid;
 
}
 

	
 
static inline bool IsPlayerBuildableVehicleType(byte type)
 
@@ -428,20 +428,6 @@ static inline bool IsPlayerBuildableVehi
 
	return IsPlayerBuildableVehicleType(v->type);
 
}
 

	
 
/** Function to give index of a vehicle type
 
 *  Since the return value is 0 for VEH_train, it's perfect for index to arrays
 
 */
 
static inline byte VehTypeToIndex(byte type)
 
{
 
	assert(IsPlayerBuildableVehicleType(type));
 
	return type - VEH_Train;
 
}
 

	
 
static inline byte VehTypeToIndex(const Vehicle *v)
 
{
 
	return VehTypeToIndex(v->type);
 
}
 

	
 
#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
 
#define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
 

	
 
@@ -523,7 +509,7 @@ extern const uint32 _send_to_depot_proc_
 
/* Functions to find the right command for certain vehicle type */
 
static inline uint32 GetCmdBuildVeh(byte type)
 
{
 
	return _veh_build_proc_table[VehTypeToIndex(type)];
 
	return _veh_build_proc_table[type];
 
}
 

	
 
static inline uint32 GetCmdBuildVeh(const Vehicle *v)
 
@@ -533,7 +519,7 @@ static inline uint32 GetCmdBuildVeh(cons
 

	
 
static inline uint32 GetCmdSellVeh(byte type)
 
{
 
	return _veh_sell_proc_table[VehTypeToIndex(type)];
 
	return _veh_sell_proc_table[type];
 
}
 

	
 
static inline uint32 GetCmdSellVeh(const Vehicle *v)
 
@@ -543,7 +529,7 @@ static inline uint32 GetCmdSellVeh(const
 

	
 
static inline uint32 GetCmdRefitVeh(byte type)
 
{
 
	return _veh_refit_proc_table[VehTypeToIndex(type)];
 
	return _veh_refit_proc_table[type];
 
}
 

	
 
static inline uint32 GetCmdRefitVeh(const Vehicle *v)
 
@@ -553,7 +539,7 @@ static inline uint32 GetCmdRefitVeh(cons
 

	
 
static inline uint32 GetCmdSendToDepot(byte type)
 
{
 
	return _send_to_depot_proc_table[VehTypeToIndex(type)];
 
	return _send_to_depot_proc_table[type];
 
}
 

	
 
static inline uint32 GetCmdSendToDepot(const Vehicle *v)
src/vehicle_gui.cpp
Show inline comments
 
@@ -1132,7 +1132,7 @@ void PlayerVehWndProc(Window *w, WindowE
 
					};
 

	
 
					/* XXX - Substite string since the dropdown cannot handle dynamic strings */
 
					action_str[2] = depot_name[vl->vehicle_type - VEH_Train];
 
					action_str[2] = depot_name[vl->vehicle_type];
 
					ShowDropDownMenu(w, action_str, 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN, 0, 0);
 
					break;
 
				}
src/viewport.cpp
Show inline comments
 
@@ -1738,7 +1738,7 @@ void HandleViewportClicked(const ViewPor
 
	v = CheckClickOnVehicle(vp, x, y);
 
	if (v != NULL) {
 
		DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
 
		_on_vehicle_click_proc[v->type - 0x10](v);
 
		_on_vehicle_click_proc[v->type](v);
 
	}
 
}
 

	
0 comments (0 inline, 0 general)