(svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
authorbjarni
Wed, 07 Feb 2007 19:10:19 +0000
changeset 5955 fe61588d5188
parent 5954 7ca0068d5cdb
child 5956 6f1e520db64d
(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
src/autoreplace_gui.cpp
src/build_vehicle_gui.cpp
src/date.cpp
src/depot_gui.cpp
src/economy.cpp
src/engine.h
src/newgrf.cpp
src/order_cmd.cpp
src/smallmap_gui.cpp
src/vehicle.cpp
src/vehicle.h
src/vehicle_gui.cpp
src/viewport.cpp
--- a/src/autoreplace_gui.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/autoreplace_gui.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -251,7 +251,7 @@
 										 !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 */
--- a/src/build_vehicle_gui.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/build_vehicle_gui.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -712,7 +712,7 @@
 			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 @@
 		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 @@
 	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 @@
 		}
 
 		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 @@
 		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 @@
 	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:
--- a/src/date.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/date.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -215,7 +215,7 @@
 	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);
 	}
 }
 
--- a/src/depot_gui.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/depot_gui.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -743,7 +743,7 @@
 						};
 
 						_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 @@
 
 						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
--- a/src/economy.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/economy.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -1503,7 +1503,7 @@
 		 * 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;
--- a/src/engine.h	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/engine.h	Wed Feb 07 19:10:19 2007 +0000
@@ -163,7 +163,7 @@
 {
 	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 @@
 		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];
--- a/src/newgrf.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/newgrf.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -3617,7 +3617,7 @@
 							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];
--- a/src/order_cmd.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/order_cmd.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -160,10 +160,10 @@
  */
 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 @@
 		/* 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);
--- a/src/smallmap_gui.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/smallmap_gui.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -724,7 +724,7 @@
 
 				// 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;
--- a/src/vehicle.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/vehicle.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -84,7 +84,10 @@
 
 	/* 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 @@
 
 	assert(v->orders == NULL);
 
+	v->type = VEH_Invalid;
 	v->left_coord = INVALID_COORD;
 	v->first = NULL;
 	v->next = NULL;
@@ -656,7 +660,7 @@
 	_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 @@
 	// 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 @@
 				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 @@
 	// 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]);
 	}
 }
 
--- a/src/vehicle.h	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/vehicle.h	Wed Feb 07 19:10:19 2007 +0000
@@ -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 bool IsValidVehicle(const Vehicle *v)
 {
-	return v->type != 0;
+	return v->type != VEH_Invalid;
 }
 
 void DestroyVehicle(Vehicle *v);
@@ -408,7 +408,7 @@
 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 @@
 	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 @@
 /* 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 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 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 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)
--- a/src/vehicle_gui.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/vehicle_gui.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -1132,7 +1132,7 @@
 					};
 
 					/* 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;
 				}
--- a/src/viewport.cpp	Wed Feb 07 18:01:26 2007 +0000
+++ b/src/viewport.cpp	Wed Feb 07 19:10:19 2007 +0000
@@ -1738,7 +1738,7 @@
 	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);
 	}
 }