src/newgrf_engine.cpp
branchcpp_gui
changeset 6268 4b5241e5dd10
parent 6254 abc6ad7c035c
child 6285 187e3ef04cc9
equal deleted inserted replaced
6267:7c8ec33959b1 6268:4b5241e5dd10
    16 #include "newgrf_station.h"
    16 #include "newgrf_station.h"
    17 #include "newgrf_spritegroup.h"
    17 #include "newgrf_spritegroup.h"
    18 #include "newgrf_cargo.h"
    18 #include "newgrf_cargo.h"
    19 #include "date.h"
    19 #include "date.h"
    20 #include "helpers.hpp"
    20 #include "helpers.hpp"
    21 
    21 #include "cargotype.h"
    22 
    22 
    23 
       
    24 /* Default cargo classes */
       
    25 static const uint16 _cargo_classes[NUM_GLOBAL_CID] = {
       
    26 	CC_PASSENGERS,
       
    27 	CC_BULK,
       
    28 	CC_MAIL,
       
    29 	CC_LIQUID,
       
    30 	CC_PIECE_GOODS,
       
    31 	CC_EXPRESS,
       
    32 	CC_BULK,
       
    33 	CC_PIECE_GOODS,
       
    34 	CC_BULK,
       
    35 	CC_PIECE_GOODS,
       
    36 	CC_ARMOURED,
       
    37 	CC_PIECE_GOODS,
       
    38 	CC_REFRIGERATED | CC_EXPRESS,
       
    39 	CC_REFRIGERATED | CC_EXPRESS,
       
    40 	CC_BULK,
       
    41 	CC_LIQUID,
       
    42 	CC_LIQUID,
       
    43 	CC_BULK,
       
    44 	CC_PIECE_GOODS,
       
    45 	CC_PIECE_GOODS,
       
    46 	CC_EXPRESS,
       
    47 	CC_BULK,
       
    48 	CC_LIQUID,
       
    49 	CC_BULK,
       
    50 	CC_PIECE_GOODS,
       
    51 	CC_LIQUID,
       
    52 	CC_PIECE_GOODS,
       
    53 	CC_PIECE_GOODS,
       
    54 	CC_NOAVAILABLE,
       
    55 	CC_NOAVAILABLE,
       
    56 	CC_NOAVAILABLE,
       
    57 };
       
    58 
    23 
    59 int _traininfo_vehicle_pitch = 0;
    24 int _traininfo_vehicle_pitch = 0;
    60 int _traininfo_vehicle_width = 29;
    25 int _traininfo_vehicle_width = 29;
    61 
    26 
    62 typedef struct WagonOverride {
    27 typedef struct WagonOverride {
    77 {
    42 {
    78 	WagonOverrides *wos;
    43 	WagonOverrides *wos;
    79 	WagonOverride *wo;
    44 	WagonOverride *wo;
    80 
    45 
    81 	assert(engine < TOTAL_NUM_ENGINES);
    46 	assert(engine < TOTAL_NUM_ENGINES);
    82 	assert(cargo < NUM_GLOBAL_CID);
    47 	assert(cargo < NUM_CARGO + 1); // Include CT_DEFAULT pseudo cargo. CT_PURCHASE does not apply to overrides.
    83 
    48 
    84 	wos = &_engine_wagon_overrides[engine];
    49 	wos = &_engine_wagon_overrides[engine];
    85 	wos->overrides_count++;
    50 	wos->overrides_count++;
    86 	wos->overrides = ReallocT(wos->overrides, wos->overrides_count);
    51 	wos->overrides = ReallocT(wos->overrides, wos->overrides_count);
    87 
    52 
   109 	for (i = 0; i < wos->overrides_count; i++) {
    74 	for (i = 0; i < wos->overrides_count; i++) {
   110 		const WagonOverride *wo = &wos->overrides[i];
    75 		const WagonOverride *wo = &wos->overrides[i];
   111 		int j;
    76 		int j;
   112 
    77 
   113 		for (j = 0; j < wo->trains; j++) {
    78 		for (j = 0; j < wo->trains; j++) {
   114 			if (wo->train_id[j] == overriding_engine && (wo->cargo == cargo || wo->cargo == GC_DEFAULT)) return wo->group;
    79 			if (wo->train_id[j] == overriding_engine && (wo->cargo == cargo || wo->cargo == CT_DEFAULT)) return wo->group;
   115 		}
    80 		}
   116 	}
    81 	}
   117 	return NULL;
    82 	return NULL;
   118 }
    83 }
   119 
    84 
   138 		wos->overrides_count = 0;
   103 		wos->overrides_count = 0;
   139 		wos->overrides = NULL;
   104 		wos->overrides = NULL;
   140 	}
   105 	}
   141 }
   106 }
   142 
   107 
   143 // 0 - 28 are cargos, 29 is default, 30 is the advert (purchase list)
   108 /* Space for NUM_CARGO real cargos and 2 pseudo cargos, CT_DEFAULT and CT_PURCHASE */
   144 // (It isn't and shouldn't be like this in the GRF files since new cargo types
   109 static const SpriteGroup *_engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_CARGO + 2];
   145 // may appear in future - however it's more convenient to store it like this in
       
   146 // memory. --pasky)
       
   147 static const SpriteGroup *engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_GLOBAL_CID];
       
   148 static const GRFFile *_engine_grf[TOTAL_NUM_ENGINES];
   110 static const GRFFile *_engine_grf[TOTAL_NUM_ENGINES];
   149 
   111 
   150 void SetCustomEngineSprites(EngineID engine, byte cargo, const SpriteGroup *group)
   112 void SetCustomEngineSprites(EngineID engine, byte cargo, const SpriteGroup *group)
   151 {
   113 {
   152 	assert(engine < TOTAL_NUM_ENGINES);
   114 	assert(engine < lengthof(_engine_custom_sprites));
   153 	assert(cargo < NUM_GLOBAL_CID);
   115 	assert(cargo < lengthof(*_engine_custom_sprites));
   154 
   116 
   155 	if (engine_custom_sprites[engine][cargo] != NULL) {
   117 	if (_engine_custom_sprites[engine][cargo] != NULL) {
   156 		grfmsg(6, "SetCustomEngineSprites: engine %d cargo %d already has group -- replacing", engine, cargo);
   118 		grfmsg(6, "SetCustomEngineSprites: engine %d cargo %d already has group -- replacing", engine, cargo);
   157 	}
   119 	}
   158 	engine_custom_sprites[engine][cargo] = group;
   120 	_engine_custom_sprites[engine][cargo] = group;
   159 }
   121 }
   160 
   122 
   161 /**
   123 /**
   162  * Unload all engine sprite groups.
   124  * Unload all engine sprite groups.
   163  */
   125  */
   164 void UnloadCustomEngineSprites(void)
   126 void UnloadCustomEngineSprites(void)
   165 {
   127 {
   166 	EngineID engine;
   128 	memset(_engine_custom_sprites, 0, sizeof(_engine_custom_sprites));
   167 	CargoID cargo;
   129 	memset(_engine_grf, 0, sizeof(_engine_grf));
   168 
       
   169 	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
       
   170 		for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
       
   171 			engine_custom_sprites[engine][cargo] = NULL;
       
   172 		}
       
   173 		_engine_grf[engine] = 0;
       
   174 	}
       
   175 }
   130 }
   176 
   131 
   177 static const SpriteGroup *heli_rotor_custom_sprites[NUM_AIRCRAFT_ENGINES];
   132 static const SpriteGroup *heli_rotor_custom_sprites[NUM_AIRCRAFT_ENGINES];
   178 
   133 
   179 /** Load a rotor override sprite group for an aircraft */
   134 /** Load a rotor override sprite group for an aircraft */
   572 		case 0x42: { /* Consist cargo information */
   527 		case 0x42: { /* Consist cargo information */
   573 			/* XXX Missing support for common refit cycle and property 25 */
   528 			/* XXX Missing support for common refit cycle and property 25 */
   574 			const Vehicle *u;
   529 			const Vehicle *u;
   575 			byte cargo_classes = 0;
   530 			byte cargo_classes = 0;
   576 			uint common_cargo_best = 0;
   531 			uint common_cargo_best = 0;
   577 			uint common_cargos[NUM_GLOBAL_CID];
   532 			uint common_cargos[NUM_CARGO];
   578 			byte user_def_data = 0;
   533 			byte user_def_data = 0;
   579 			CargoID cargo;
   534 			CargoID common_cargo_type = CT_PASSENGERS;
   580 			CargoID common_cargo_type = GC_PASSENGERS;
       
   581 
   535 
   582 			/* Reset our arrays */
   536 			/* Reset our arrays */
   583 			memset(common_cargos, 0, sizeof(common_cargos));
   537 			memset(common_cargos, 0, sizeof(common_cargos));
   584 
   538 
   585 			for (u = v; u != NULL; u = u->next) {
   539 			for (u = v; u != NULL; u = u->next) {
   586 				/* Skip empty engines */
   540 				/* Skip empty engines */
   587 				if (u->cargo_cap == 0) continue;
   541 				if (u->cargo_cap == 0) continue;
   588 				/* Map from climate to global cargo ID */
   542 
   589 				cargo = _global_cargo_id[_opt.landscape][u->cargo_type];
   543 				cargo_classes |= GetCargo(u->cargo_type)->classes;
   590 				cargo_classes |= _cargo_classes[cargo];
   544 				common_cargos[u->cargo_type]++;
   591 				common_cargos[cargo]++;
       
   592 				user_def_data |= RailVehInfo(u->engine_type)->user_def_data;
   545 				user_def_data |= RailVehInfo(u->engine_type)->user_def_data;
   593 			}
   546 			}
   594 
   547 
   595 			/* Pick the most common cargo type */
   548 			/* Pick the most common cargo type */
   596 			for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
   549 			for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) {
   597 				if (common_cargos[cargo] > common_cargo_best) {
   550 				if (common_cargos[cargo] > common_cargo_best) {
   598 					common_cargo_best = common_cargos[cargo];
   551 					common_cargo_best = common_cargos[cargo];
   599 					common_cargo_type = cargo;
   552 					common_cargo_type = GetCargo(cargo)->bitnum;
   600 				}
   553 				}
   601 			}
   554 			}
   602 
   555 
   603 			return cargo_classes | (common_cargo_type << 8) | (user_def_data << 24);
   556 			return cargo_classes | (common_cargo_type << 8) | (user_def_data << 24);
   604 		}
   557 		}
   641 			 * tt - the cargo type transported by the vehicle,
   594 			 * tt - the cargo type transported by the vehicle,
   642 			 *     translated if a translation table has been installed.
   595 			 *     translated if a translation table has been installed.
   643 			 * ww - cargo unit weight in 1/16 tons, same as cargo prop. 0F.
   596 			 * ww - cargo unit weight in 1/16 tons, same as cargo prop. 0F.
   644 			 * cccc - the cargo class value of the cargo transported by the vehicle.
   597 			 * cccc - the cargo class value of the cargo transported by the vehicle.
   645 			 */
   598 			 */
   646 			CargoID cid = _global_cargo_id[_opt.landscape][v->cargo_type];
   599 			const CargoSpec *cs = GetCargo(v->cargo_type);
   647 
   600 
   648 			return (_cargo_classes[cid] << 16) | (_cargoc.weights[v->cargo_type] << 8) | cid;
   601 			return (cs->classes << 16) | (cs->weight << 8) | GetEngineGRF(v->engine_type)->cargo_map[v->cargo_type];
   649 		}
   602 		}
   650 
   603 
   651 		case 0x48: return GetVehicleTypeInfo(v->engine_type); /* Vehicle Type Info */
   604 		case 0x48: return GetVehicleTypeInfo(v->engine_type); /* Vehicle Type Info */
   652 
   605 
   653 		/* Variables which use the parameter */
   606 		/* Variables which use the parameter */
   853 {
   806 {
   854 	const SpriteGroup *group;
   807 	const SpriteGroup *group;
   855 	CargoID cargo;
   808 	CargoID cargo;
   856 
   809 
   857 	if (v == NULL) {
   810 	if (v == NULL) {
   858 		cargo = GC_PURCHASE;
   811 		cargo = CT_PURCHASE;
   859 	} else {
   812 	} else {
   860 		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
   813 		cargo = v->cargo_type;
   861 		assert(cargo != GC_INVALID);
       
   862 
   814 
   863 		if (v->type == VEH_Train) {
   815 		if (v->type == VEH_Train) {
   864 			group = GetWagonOverrideSpriteSet(engine, cargo, v->u.rail.first_engine);
   816 			group = GetWagonOverrideSpriteSet(engine, cargo, v->u.rail.first_engine);
   865 
   817 
   866 			if (group != NULL) return group;
   818 			if (group != NULL) return group;
   867 		}
   819 		}
   868 	}
   820 	}
   869 
   821 
   870 	group = engine_custom_sprites[engine][cargo];
   822 	group = _engine_custom_sprites[engine][cargo];
   871 	if (group != NULL) return group;
   823 	if (group != NULL) return group;
   872 
   824 
   873 	/* Fall back to the default set if the selected cargo type is not defined */
   825 	/* Fall back to the default set if the selected cargo type is not defined */
   874 	return engine_custom_sprites[engine][GC_DEFAULT];
   826 	return _engine_custom_sprites[engine][CT_DEFAULT];
   875 }
   827 }
   876 
   828 
   877 
   829 
   878 SpriteID GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction direction)
   830 SpriteID GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction direction)
   879 {
   831 {
   921  * @return true if it is using an override, false otherwise
   873  * @return true if it is using an override, false otherwise
   922  */
   874  */
   923 bool UsesWagonOverride(const Vehicle* v)
   875 bool UsesWagonOverride(const Vehicle* v)
   924 {
   876 {
   925 	assert(v->type == VEH_Train);
   877 	assert(v->type == VEH_Train);
   926 	return GetWagonOverrideSpriteSet(v->engine_type, _global_cargo_id[_opt.landscape][v->cargo_type], v->u.rail.first_engine) != NULL;
   878 	return GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->u.rail.first_engine) != NULL;
   927 }
   879 }
   928 
   880 
   929 /**
   881 /**
   930  * Evaluate a newgrf callback for vehicles
   882  * Evaluate a newgrf callback for vehicles
   931  * @param callback The callback to evalute
   883  * @param callback The callback to evalute