src/roadveh_cmd.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
   114 		spritenum = orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
   114 		spritenum = orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
   115 	}
   115 	}
   116 	DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
   116 	DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
   117 }
   117 }
   118 
   118 
   119 static int32 EstimateRoadVehCost(EngineID engine_type)
   119 static CommandCost EstimateRoadVehCost(EngineID engine_type)
   120 {
   120 {
   121 	return ((_eco->GetPrice(CEconomy::ROADVEH_BASE) >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->base_cost)) >> 5;
   121 	return ((_eco->GetPrice(CEconomy::ROADVEH_BASE) >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->base_cost)) >> 5;
   122 }
   122 }
   123 
   123 
   124 byte GetRoadVehLength(const Vehicle *v)
   124 byte GetRoadVehLength(const Vehicle *v)
   154  * @param tile tile of depot where road vehicle is built
   154  * @param tile tile of depot where road vehicle is built
   155  * @param flags operation to perform
   155  * @param flags operation to perform
   156  * @param p1 bus/truck type being built (engine)
   156  * @param p1 bus/truck type being built (engine)
   157  * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
   157  * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
   158  */
   158  */
   159 int32 CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   159 CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   160 {
   160 {
   161 	int32 cost;
   161 	CommandCost cost;
   162 	Vehicle *v;
   162 	Vehicle *v;
   163 	UnitID unit_num;
   163 	UnitID unit_num;
   164 	Engine *e;
   164 	Engine *e;
   165 
   165 
   166 	if (!IsEngineBuildable(p1, VEH_ROAD, _current_player)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE);
   166 	if (!IsEngineBuildable(p1, VEH_ROAD, _current_player)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE);
   280  * @param tile unused
   280  * @param tile unused
   281  * @param flags operation to perform
   281  * @param flags operation to perform
   282  * @param p1 road vehicle ID to start/stop
   282  * @param p1 road vehicle ID to start/stop
   283  * @param p2 unused
   283  * @param p2 unused
   284  */
   284  */
   285 int32 CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   285 CommandCost CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   286 {
   286 {
   287 	Vehicle *v;
   287 	Vehicle *v;
   288 	uint16 callback;
   288 	uint16 callback;
   289 
   289 
   290 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   290 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   345  * @param tile unused
   345  * @param tile unused
   346  * @param flags operation to perform
   346  * @param flags operation to perform
   347  * @param p1 vehicle ID to be sold
   347  * @param p1 vehicle ID to be sold
   348  * @param p2 unused
   348  * @param p2 unused
   349  */
   349  */
   350 int32 CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   350 CommandCost CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   351 {
   351 {
   352 	Vehicle *v;
   352 	Vehicle *v;
   353 
   353 
   354 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   354 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   355 
   355 
   445  * @param p1 vehicle ID to send to the depot
   445  * @param p1 vehicle ID to send to the depot
   446  * @param p2 various bitmasked elements
   446  * @param p2 various bitmasked elements
   447  * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
   447  * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
   448  * - p2 bit 8-10 - VLW flag (for mass goto depot)
   448  * - p2 bit 8-10 - VLW flag (for mass goto depot)
   449  */
   449  */
   450 int32 CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   450 CommandCost CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   451 {
   451 {
   452 	Vehicle *v;
   452 	Vehicle *v;
   453 	const Depot *dep;
   453 	const Depot *dep;
   454 
   454 
   455 	if (p2 & DEPOT_MASS_SEND) {
   455 	if (p2 & DEPOT_MASS_SEND) {
   518  * @param tile unused
   518  * @param tile unused
   519  * @param flags operation to perform
   519  * @param flags operation to perform
   520  * @param p1 vehicle ID to turn
   520  * @param p1 vehicle ID to turn
   521  * @param p2 unused
   521  * @param p2 unused
   522  */
   522  */
   523 int32 CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   523 CommandCost CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   524 {
   524 {
   525 	Vehicle *v;
   525 	Vehicle *v;
   526 
   526 
   527 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   527 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
   528 
   528 
   707 
   707 
   708 		TileIndex tile = u->tile;
   708 		TileIndex tile = u->tile;
   709 
   709 
   710 		if (!IsLevelCrossingTile(tile)) continue;
   710 		if (!IsLevelCrossingTile(tile)) continue;
   711 
   711 
   712 		if (VehicleFromPos(tile, u, EnumCheckRoadVehCrashTrain) != NULL) {
   712 		if (VehicleFromPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain) != NULL) {
   713 			RoadVehCrash(v);
   713 			RoadVehCrash(v);
   714 			return;
   714 			return;
   715 		}
   715 		}
   716 	}
   716 	}
   717 }
   717 }
   887 
   887 
   888 	rvf.x = x;
   888 	rvf.x = x;
   889 	rvf.y = y;
   889 	rvf.y = y;
   890 	rvf.dir = dir;
   890 	rvf.dir = dir;
   891 	rvf.veh = v;
   891 	rvf.veh = v;
   892 	u = (Vehicle*)VehicleFromPos(TileVirtXY(x, y), &rvf, EnumCheckRoadVehClose);
   892 	u = (Vehicle*)VehicleFromPosXY(x, y, &rvf, EnumCheckRoadVehClose);
   893 
   893 
   894 	/* This code protects a roadvehicle from being blocked for ever
   894 	/* This code protects a roadvehicle from being blocked for ever
   895 	 * If more than 1480 / 74 days a road vehicle is blocked, it will
   895 	 * If more than 1480 / 74 days a road vehicle is blocked, it will
   896 	 * drive just through it. The ultimate backup-code of TTD.
   896 	 * drive just through it. The ultimate backup-code of TTD.
   897 	 * It can be disabled. */
   897 	 * It can be disabled. */
  1083 	}
  1083 	}
  1084 }
  1084 }
  1085 
  1085 
  1086 static int PickRandomBit(uint bits)
  1086 static int PickRandomBit(uint bits)
  1087 {
  1087 {
  1088 	uint num = 0;
       
  1089 	uint b = bits;
       
  1090 	uint i;
  1088 	uint i;
  1091 
  1089 	uint num = RandomRange(CountBitsSet(bits));
  1092 	do {
       
  1093 		if (b & 1) num++;
       
  1094 	} while (b >>= 1);
       
  1095 
       
  1096 	num = RandomRange(num);
       
  1097 
  1090 
  1098 	for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
  1091 	for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
  1099 	return i;
  1092 	return i;
  1100 }
  1093 }
  1101 
  1094 
  1891 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1884 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1892 }
  1885 }
  1893 
  1886 
  1894 void OnNewDay_RoadVeh(Vehicle *v)
  1887 void OnNewDay_RoadVeh(Vehicle *v)
  1895 {
  1888 {
  1896 	int32 cost;
  1889 	CommandCost cost;
  1897 
  1890 
  1898 	if (!IsRoadVehFront(v)) return;
  1891 	if (!IsRoadVehFront(v)) return;
  1899 
  1892 
  1900 	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
  1893 	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
  1901 	if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
  1894 	if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
  2006  * - p2 = (bit 0-7) - the new cargo type to refit to
  1999  * - p2 = (bit 0-7) - the new cargo type to refit to
  2007  * - p2 = (bit 8-15) - the new cargo subtype to refit to
  2000  * - p2 = (bit 8-15) - the new cargo subtype to refit to
  2008  * - p2 = (bit 16) - refit only this vehicle (ignored)
  2001  * - p2 = (bit 16) - refit only this vehicle (ignored)
  2009  * @return cost of refit or error
  2002  * @return cost of refit or error
  2010  */
  2003  */
  2011 int32 CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2004 CommandCost CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2012 {
  2005 {
  2013 	Vehicle *v;
  2006 	Vehicle *v;
  2014 	int32 cost;
  2007 	CommandCost cost;
  2015 	CargoID new_cid = GB(p2, 0, 8);
  2008 	CargoID new_cid = GB(p2, 0, 8);
  2016 	byte new_subtype = GB(p2, 8, 8);
  2009 	byte new_subtype = GB(p2, 8, 8);
  2017 	uint16 capacity = CALLBACK_FAILED;
  2010 	uint16 capacity = CALLBACK_FAILED;
  2018 
  2011 
  2019 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
  2012 	if (!IsValidVehicleID(p1)) return CMD_ERROR;