Index: src/rail_cmd.cpp =================================================================== --- src/rail_cmd.cpp (revisión: 17581) +++ src/rail_cmd.cpp (copia de trabajo) @@ -1268,7 +1268,7 @@ if (t->IsArticulatedPart()) return NULL; const RailVehicleInfo *rvi = RailVehInfo(t->engine_type); - if (GetVehicleProperty(t, 0x0B, rvi->power) != 0) TrainPowerChanged(t->First()); + if (GetVehicleProperty(t, POWER, rvi->power) != 0) TrainPowerChanged(t->First()); return NULL; } Index: src/ship_cmd.cpp =================================================================== --- src/ship_cmd.cpp (revisión: 17581) +++ src/ship_cmd.cpp (copia de trabajo) @@ -158,7 +158,7 @@ Money Ship::GetRunningCost() const { - return GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running; + return GetVehicleProperty(this, RUNNING_COST_FACTOR, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running; } void Ship::OnNewDay() @@ -332,7 +332,7 @@ uint spd; byte t; - spd = min(v->cur_speed + 1, GetVehicleProperty(v, 0x0B, v->max_speed)); + spd = min(v->cur_speed + 1, GetVehicleProperty(v, SPEED, v->max_speed)); /* updates statusbar only if speed have changed to save CPU time */ if (spd != v->cur_speed) { @@ -813,7 +813,7 @@ v->InvalidateNewGRFCacheOfChain(); - v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity); + v->cargo_cap = GetVehicleProperty(v, CARGO_CAPACITY, svi->capacity); v->InvalidateNewGRFCacheOfChain(); @@ -940,7 +940,7 @@ } if (capacity == CALLBACK_FAILED) { - capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity); + capacity = GetVehicleProperty(v, CARGO_CAPACITY, ShipVehInfo(v->engine_type)->capacity); } _returned_refit_capacity = capacity; Index: src/train_cmd.cpp =================================================================== --- src/train_cmd.cpp (revisión: 17581) +++ src/train_cmd.cpp (copia de trabajo) @@ -107,14 +107,14 @@ const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); if (engine_has_power) { - uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power); + uint16 power = GetVehicleProperty(u, POWER, rvi_u->power); if (power != 0) { /* Halve power for multiheaded parts */ if (u->IsMultiheaded()) power /= 2; total_power += power; /* Tractive effort in (tonnes * 1000 * 10 =) N */ - max_te += (u->tcache.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256; + max_te += (u->tcache.cached_veh_weight * 10000 * GetVehicleProperty(u, TRACTIVE_EFFORT, rvi_u->tractive_effort)) / 256; } } } @@ -151,7 +151,7 @@ /* Vehicle weight is not added for articulated parts. */ if (!u->IsArticulatedPart()) { /* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */ - vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->weight); + vweight += GetVehicleProperty(u, WEIGHT, RailVehInfo(u->engine_type)->weight); } /* powered wagons have extra weight added */ @@ -253,7 +253,7 @@ for (Train *u = v; u != NULL; u = u->Next()) { /* Update user defined data (must be done before other properties) */ - u->tcache.user_def_data = GetVehicleProperty(u, 0x25, u->tcache.user_def_data); + u->tcache.user_def_data = GetVehicleProperty(u, USER_DATA, u->tcache.user_def_data); v->InvalidateNewGRFCache(); u->InvalidateNewGRFCache(); } @@ -316,14 +316,14 @@ /* max speed is the minimum of the speed limits of all vehicles in the consist */ if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) { - uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed); + uint16 speed = GetVehicleProperty(u, SPEED, rvi_u->max_speed); if (speed != 0) max_speed = min(speed, max_speed); } } if (e_u->CanCarryCargo() && u->cargo_type == e_u->GetDefaultCargoType() && u->cargo_subtype == 0) { /* Set cargo capacity if we've not been refitted */ - u->cargo_cap = GetVehicleProperty(u, 0x14, rvi_u->capacity); + u->cargo_cap = GetVehicleProperty(u, CARGO_CAPACITY, rvi_u->capacity); } /* check the vehicle length (callback) */ @@ -4489,7 +4489,7 @@ do { const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); - byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost); + byte cost_factor = GetVehicleProperty(v, RUNNING_COST_FACTOR, rvi->running_cost); if (cost_factor == 0) continue; /* Halve running cost for multiheaded parts */ Index: src/newgrf_engine.h =================================================================== --- src/newgrf_engine.h (revisión: 17581) +++ src/newgrf_engine.h (copia de trabajo) @@ -43,10 +43,23 @@ #define GetCustomVehicleSprite(v, direction) GetCustomEngineSprite(v->engine_type, v, direction) #define GetCustomVehicleIcon(et, direction) GetCustomEngineSprite(et, NULL, direction) +/* List of all vehicle properties supported by callback 36 */ +enum CallbackProperty { + COST_FACTOR = 0, //Supported for trains, road vehicles, ships and planes. + SPEED = 1, //Supported for trains, ships and planes. + RUNNING_COST_FACTOR = 2, //Supported for trains, ships and planes. + CARGO_CAPACITY = 3, //Supported for trains, road vehicles and ships. + POWER = 4, //Supported for trains. + WEIGHT = 5, //Supported for trains. + TRACTIVE_EFFORT = 6, //Supported for trains. + USER_DATA = 7, //Supported for trains. + CALLBACK_PROPERTY_SIZE = 8, +}; + /* Handler to Evaluate callback 36. If the callback fails (i.e. most of the * time) orig_value is returned */ -uint GetVehicleProperty(const Vehicle *v, uint8 property, uint orig_value); -uint GetEngineProperty(EngineID engine, uint8 property, uint orig_value); +uint GetVehicleProperty(const Vehicle *v, int property, uint orig_value); +uint GetEngineProperty(EngineID engine, int property, uint orig_value); enum VehicleTrigger { VEHICLE_TRIGGER_NEW_CARGO = 0x01, Index: src/roadveh_cmd.cpp =================================================================== --- src/roadveh_cmd.cpp (revisión: 17581) +++ src/roadveh_cmd.cpp (copia de trabajo) @@ -288,7 +288,7 @@ for (RoadVehicle *u = v; u != NULL; u = u->Next()) { u->rcache.cached_veh_length = GetRoadVehLength(u); /* Cargo capacity is zero if and only if the vehicle cannot carry anything */ - if (u->cargo_cap != 0) u->cargo_cap = GetVehicleProperty(u, 0x0F, u->cargo_cap); + if (u->cargo_cap != 0) u->cargo_cap = GetVehicleProperty(u, CARGO_CAPACITY, u->cargo_cap); v->InvalidateNewGRFCache(); u->InvalidateNewGRFCache(); } @@ -2052,7 +2052,7 @@ * carry twice as much mail/goods as normal cargo, and four times as * many passengers */ - capacity = GetVehicleProperty(v, 0x0F, e->u.road.capacity); + capacity = GetVehicleProperty(v, CARGO_CAPACITY, e->u.road.capacity); switch (old_cid) { case CT_PASSENGERS: break; case CT_MAIL: Index: src/newgrf_engine.cpp =================================================================== --- src/newgrf_engine.cpp (revisión: 17581) +++ src/newgrf_engine.cpp (copia de trabajo) @@ -1024,20 +1024,73 @@ return group->GetCallbackResult(); } +static const int NOT_SUPPORTED = 255; +static const uint8 _property_list[][CALLBACK_PROPERTY_SIZE] = {{ + /* Trains */ + 0x17, //COST_FACTOR + 0x09, //SPEED + 0x0D, //RUNNING_COST_FACTOR + 0x14, //CARGO_CAPACITY + 0x0B, //POWER + 0x16, //WEIGHT + 0x1F, //TRACTIVE_EFFORT + 0x25, //USER_DATA +}, { + /* Road vehicles */ + 0x11, //COST_FACTOR + NOT_SUPPORTED, //SPEED + NOT_SUPPORTED, //RUNNING_COST_FACTOR + 0x0F, //CARGO_CAPACITY + NOT_SUPPORTED, //POWER + NOT_SUPPORTED, //WEIGHT + NOT_SUPPORTED, //TRACTIVE_EFFORT + NOT_SUPPORTED, //USER_DATA +}, { + /* Ships */ + 0x0A, //COST_FACTOR + 0x0B, //SPEED + 0x0F, //RUNNING_COST_FACTOR + 0x0D, //CARGO_CAPACITY + NOT_SUPPORTED, //POWER + NOT_SUPPORTED, //WEIGHT + NOT_SUPPORTED, //TRACTIVE_EFFORT + NOT_SUPPORTED, //USER_DATA +}, { + /* Aircraft */ + 0x0B, //COST_FACTOR + 0x0C, //SPEED + 0x0E, //RUNNING_COST_FACTOR + NOT_SUPPORTED, //CARGO_CAPACITY + NOT_SUPPORTED, //POWER + NOT_SUPPORTED, //WEIGHT + NOT_SUPPORTED, //TRACTIVE_EFFORT + NOT_SUPPORTED, //USER_DATA +}}; + /* Callback 36 handlers */ -uint GetVehicleProperty(const Vehicle *v, uint8 property, uint orig_value) +uint GetVehicleProperty(const Vehicle *v, int property, uint orig_value) { - uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, property, 0, v->engine_type, v); + if (property >= CALLBACK_PROPERTY_SIZE) return orig_value; + + uint8 newgrf_property = _property_list[v->type][property]; + if (newgrf_property == NOT_SUPPORTED) return orig_value; + + uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, newgrf_property, 0, v->engine_type, v); if (callback != CALLBACK_FAILED) return callback; return orig_value; } -uint GetEngineProperty(EngineID engine, uint8 property, uint orig_value) +uint GetEngineProperty(EngineID engine, int property, uint orig_value) { - uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, property, 0, engine, NULL); + if (property >= CALLBACK_PROPERTY_SIZE) return orig_value; + + uint8 newgrf_property = _property_list[Engine::Get(engine)->type][property]; + if (newgrf_property == NOT_SUPPORTED) return orig_value; + + uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, newgrf_property, 0, engine, NULL); if (callback != CALLBACK_FAILED) return callback; return orig_value; Index: src/engine.cpp =================================================================== --- src/engine.cpp (revisión: 17581) +++ src/engine.cpp (copia de trabajo) @@ -172,13 +172,13 @@ if (!this->CanCarryCargo()) return 0; switch (type) { case VEH_TRAIN: - return GetEngineProperty(this->index, 0x14, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0); + return GetEngineProperty(this->index, CARGO_CAPACITY, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0); case VEH_ROAD: - return GetEngineProperty(this->index, 0x0F, this->u.road.capacity); + return GetEngineProperty(this->index, CARGO_CAPACITY, this->u.road.capacity); case VEH_SHIP: - return GetEngineProperty(this->index, 0x0D, this->u.ship.capacity); + return GetEngineProperty(this->index, CARGO_CAPACITY, this->u.ship.capacity); case VEH_AIRCRAFT: return AircraftDefaultCargoCapacity(this->GetDefaultCargoType(), &this->u.air); @@ -194,13 +194,13 @@ return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8; case VEH_TRAIN: - return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8; + return GetEngineProperty(this->index, RUNNING_COST_FACTOR, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8; case VEH_SHIP: - return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8; + return GetEngineProperty(this->index, RUNNING_COST_FACTOR, this->u.ship.running_cost) * _price.ship_running >> 8; case VEH_AIRCRAFT: - return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8; + return GetEngineProperty(this->index, RUNNING_COST_FACTOR, this->u.air.running_cost) * _price.aircraft_running >> 8; default: NOT_REACHED(); } @@ -210,19 +210,19 @@ { switch (this->type) { case VEH_ROAD: - return GetEngineProperty(this->index, 0x11, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5; + return GetEngineProperty(this->index, COST_FACTOR, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5; case VEH_TRAIN: if (this->u.rail.railveh_type == RAILVEH_WAGON) { - return (GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * _price.build_railwagon) >> 8; + return (GetEngineProperty(this->index, COST_FACTOR, this->u.rail.cost_factor) * _price.build_railwagon) >> 8; } else { - return GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5; + return GetEngineProperty(this->index, COST_FACTOR, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5; } case VEH_SHIP: - return GetEngineProperty(this->index, 0x0A, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5; + return GetEngineProperty(this->index, COST_FACTOR, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5; case VEH_AIRCRAFT: - return GetEngineProperty(this->index, 0x0B, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5; + return GetEngineProperty(this->index, COST_FACTOR, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5; default: NOT_REACHED(); } @@ -236,13 +236,13 @@ { switch (this->type) { case VEH_TRAIN: - return GetEngineProperty(this->index, 0x09, this->u.rail.max_speed); + return GetEngineProperty(this->index, SPEED, this->u.rail.max_speed); case VEH_ROAD: return this->u.road.max_speed / 2; case VEH_SHIP: - return GetEngineProperty(this->index, 0x0B, this->u.ship.max_speed) / 2; + return GetEngineProperty(this->index, SPEED, this->u.ship.max_speed) / 2; case VEH_AIRCRAFT: return this->u.air.max_speed; @@ -256,7 +256,7 @@ /* Currently only trains have 'power' */ switch (this->type) { case VEH_TRAIN: - return GetEngineProperty(this->index, 0x0B, this->u.rail.power); + return GetEngineProperty(this->index, POWER, this->u.rail.power); default: NOT_REACHED(); } @@ -272,7 +272,7 @@ /* Currently only trains have 'weight' */ switch (this->type) { case VEH_TRAIN: - return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0); + return GetEngineProperty(this->index, WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0); default: NOT_REACHED(); } @@ -288,7 +288,7 @@ /* Currently only trains have 'tractive effort' */ switch (this->type) { case VEH_TRAIN: - return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, 0x1F, this->u.rail.tractive_effort)) / 256; + return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256; default: NOT_REACHED(); } Index: src/aircraft_cmd.cpp =================================================================== --- src/aircraft_cmd.cpp (revisión: 17581) +++ src/aircraft_cmd.cpp (copia de trabajo) @@ -619,7 +619,7 @@ Money Aircraft::GetRunningCost() const { - return GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running; + return GetVehicleProperty(this, RUNNING_COST_FACTOR, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running; } void Aircraft::OnNewDay() @@ -753,7 +753,7 @@ void UpdateAircraftCache(Aircraft *v) { - uint max_speed = GetVehicleProperty(v, 0x0C, 0); + uint max_speed = GetVehicleProperty(v, SPEED, 0); if (max_speed != 0) { /* Convert from original units to (approx) km/h */ max_speed = (max_speed * 129) / 10;