src/train_cmd.cpp
changeset 8230 64f28fe2d5c8
parent 8226 9c72921cf666
child 8236 8a5dd0b42e47
equal deleted inserted replaced
8229:00e7467ceeee 8230:64f28fe2d5c8
   509 	DrawSprite(image, pal, x, y);
   509 	DrawSprite(image, pal, x, y);
   510 }
   510 }
   511 
   511 
   512 static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   512 static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   513 {
   513 {
   514 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   515 
       
   516 	const RailVehicleInfo *rvi = RailVehInfo(engine);
   514 	const RailVehicleInfo *rvi = RailVehInfo(engine);
   517 	CommandCost value((GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
   515 	CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
   518 
   516 
   519 	uint num_vehicles = 1 + CountArticulatedParts(engine, false);
   517 	uint num_vehicles = 1 + CountArticulatedParts(engine, false);
   520 
   518 
   521 	if (!(flags & DC_QUERY_COST)) {
   519 	if (!(flags & DC_QUERY_COST)) {
   522 		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
   520 		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
   598 			}
   596 			}
   599 			GetPlayer(_current_player)->num_engines[engine]++;
   597 			GetPlayer(_current_player)->num_engines[engine]++;
   600 		}
   598 		}
   601 	}
   599 	}
   602 
   600 
   603 	return CommandCost(value);
   601 	return value;
   604 }
   602 }
   605 
   603 
   606 /** Move all free vehicles in the depot to the train */
   604 /** Move all free vehicles in the depot to the train */
   607 static void NormalizeTrainVehInDepot(const Vehicle* u)
   605 static void NormalizeTrainVehInDepot(const Vehicle* u)
   608 {
   606 {
   619 	}
   617 	}
   620 }
   618 }
   621 
   619 
   622 static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo* rvi)
   620 static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo* rvi)
   623 {
   621 {
   624 	return CommandCost(GetEngineProperty(engine, 0x17, rvi->base_cost) * (_price.build_railvehicle >> 3) >> 5);
   622 	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x17, rvi->base_cost) * (_price.build_railvehicle >> 3) >> 5);
   625 }
   623 }
   626 
   624 
   627 static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building)
   625 static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building)
   628 {
   626 {
   629 	u = new (u) Train();
   627 	u = new (u) Train();
   669 	if (!(flags & DC_QUERY_COST)) {
   667 	if (!(flags & DC_QUERY_COST)) {
   670 		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
   668 		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
   671 		if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
   669 		if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
   672 	}
   670 	}
   673 
   671 
   674 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   675 
       
   676 	const RailVehicleInfo *rvi = RailVehInfo(p1);
   672 	const RailVehicleInfo *rvi = RailVehInfo(p1);
   677 
   673 
   678 	/* Check if depot and new engine uses the same kind of tracks */
   674 	/* Check if depot and new engine uses the same kind of tracks */
   679 	/* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
   675 	/* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
   680 	if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
   676 	if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
  1311 
  1307 
  1312 	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
  1308 	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
  1313 
  1309 
  1314 	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
  1310 	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
  1315 
  1311 
  1316 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
  1317 
       
  1318 	while (IsArticulatedPart(v)) v = v->Previous();
  1312 	while (IsArticulatedPart(v)) v = v->Previous();
  1319 	Vehicle *first = v->First();
  1313 	Vehicle *first = v->First();
  1320 
  1314 
  1321 	/* make sure the vehicle is stopped in the depot */
  1315 	/* make sure the vehicle is stopped in the depot */
  1322 	if (CheckTrainStoppedInDepot(first) < 0) {
  1316 	if (CheckTrainStoppedInDepot(first) < 0) {
  1332 		}
  1326 		}
  1333 		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
  1327 		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
  1334 		RebuildVehicleLists();
  1328 		RebuildVehicleLists();
  1335 	}
  1329 	}
  1336 
  1330 
  1337 	CommandCost cost;
  1331 	CommandCost cost(EXPENSES_NEW_VEHICLES);
  1338 	switch (p2) {
  1332 	switch (p2) {
  1339 		case 0: case 2: { /* Delete given wagon */
  1333 		case 0: case 2: { /* Delete given wagon */
  1340 			bool switch_engine = false;    // update second wagon to engine?
  1334 			bool switch_engine = false;    // update second wagon to engine?
  1341 			byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes
  1335 			byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes
  1342 
  1336 
  1802 	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
  1796 	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
  1803 
  1797 
  1804 	/* Check cargo */
  1798 	/* Check cargo */
  1805 	if (new_cid >= NUM_CARGO) return CMD_ERROR;
  1799 	if (new_cid >= NUM_CARGO) return CMD_ERROR;
  1806 
  1800 
  1807 	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
  1801 	CommandCost cost(EXPENSES_TRAIN_RUN);
  1808 
       
  1809 	CommandCost cost;
       
  1810 	uint num = 0;
  1802 	uint num = 0;
  1811 
  1803 
  1812 	do {
  1804 	do {
  1813 		/* XXX: We also refit all the attached wagons en-masse if they
  1805 		/* XXX: We also refit all the attached wagons en-masse if they
  1814 		 * can be refitted. This is how TTDPatch does it.  TODO: Have
  1806 		 * can be refitted. This is how TTDPatch does it.  TODO: Have
  3479 			if (tile != 0) v->dest_tile = tile;
  3471 			if (tile != 0) v->dest_tile = tile;
  3480 		}
  3472 		}
  3481 
  3473 
  3482 		if ((v->vehstatus & VS_STOPPED) == 0) {
  3474 		if ((v->vehstatus & VS_STOPPED) == 0) {
  3483 			/* running costs */
  3475 			/* running costs */
  3484 			CommandCost cost(v->GetRunningCost() / 364);
  3476 			CommandCost cost(EXPENSES_TRAIN_RUN, v->GetRunningCost() / 364);
  3485 
  3477 
  3486 			v->profit_this_year -= cost.GetCost() >> 8;
  3478 			v->profit_this_year -= cost.GetCost() >> 8;
  3487 
  3479 
  3488 			SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
       
  3489 			SubtractMoneyFromPlayerFract(v->owner, cost);
  3480 			SubtractMoneyFromPlayerFract(v->owner, cost);
  3490 
  3481 
  3491 			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
  3482 			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
  3492 			InvalidateWindowClasses(WC_TRAINS_LIST);
  3483 			InvalidateWindowClasses(WC_TRAINS_LIST);
  3493 		}
  3484 		}