roadveh_cmd.c
changeset 1520 c85dc79795e0
parent 1467 c8a9a122dac4
child 1530 caa16c506a22
equal deleted inserted replaced
1519:b3689662209e 1520:c85dc79795e0
   318 
   318 
   319 		return GetDepotByTile(rfdd.tile);
   319 		return GetDepotByTile(rfdd.tile);
   320 	}
   320 	}
   321 }
   321 }
   322 
   322 
   323 /*  Send a road vehicle to the nearest depot
       
   324 	p1 = index of the road vehicle
       
   325 	p2 = bit 0 = do not stop in depot
       
   326 		 bit 1 = set v->set_for_replacement
       
   327 		 bit 2 = clear v->set_for_replacement */
       
   328 int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   323 int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   329 {
   324 {
   330 	Vehicle *v;
   325 	Vehicle *v;
   331 	Depot *depot;
   326 	Depot *depot;
   332 
   327 
   334 
   329 
   335 	v = GetVehicle(p1);
   330 	v = GetVehicle(p1);
   336 
   331 
   337 	if (v->type != VEH_Road || !CheckOwnership(v->owner))
   332 	if (v->type != VEH_Road || !CheckOwnership(v->owner))
   338 		return CMD_ERROR;
   333 		return CMD_ERROR;
   339 
       
   340 	if (HASBIT(p2, 0)) v->set_for_replacement = true;
       
   341 	if (HASBIT(p2, 2)) v->set_for_replacement = false;
       
   342 
       
   343 	if (HASBIT(p2, 1) || HASBIT(p2, 2)) return CMD_ERROR;   // vehicle has a depot in schedule. It just needed to alter set_for_replacement
       
   344 
   334 
   345 	if (v->current_order.type == OT_GOTO_DEPOT) {
   335 	if (v->current_order.type == OT_GOTO_DEPOT) {
   346 		if (flags & DC_EXEC) {
   336 		if (flags & DC_EXEC) {
   347 			if (v->current_order.flags & OF_UNLOAD)
   337 			if (v->current_order.flags & OF_UNLOAD)
   348 				v->cur_order_index++;
   338 				v->cur_order_index++;
   357 	if (depot == NULL)
   347 	if (depot == NULL)
   358 		return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
   348 		return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
   359 
   349 
   360 	if (flags & DC_EXEC) {
   350 	if (flags & DC_EXEC) {
   361 		v->current_order.type = OT_GOTO_DEPOT;
   351 		v->current_order.type = OT_GOTO_DEPOT;
   362 		v->current_order.flags = p2 == 0 ? OF_NON_STOP | OF_FULL_LOAD : 0;
   352 		v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD;
   363 		v->current_order.station = depot->index;
   353 		v->current_order.station = depot->index;
   364 		v->dest_tile = depot->xy;
   354 		v->dest_tile = depot->xy;
   365 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
   355 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
   366 	}
   356 	}
   367 
   357 
   613 			return;
   603 			return;
   614 	}
   604 	}
   615 
   605 
   616 	if (v->current_order.type == OT_GOTO_DEPOT &&
   606 	if (v->current_order.type == OT_GOTO_DEPOT &&
   617 			(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) &&
   607 			(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) &&
   618 			!VehicleNeedsService(v) && !v->set_for_replacement) {
   608 			!VehicleNeedsService(v)) {
   619 		v->cur_order_index++;
   609 		v->cur_order_index++;
   620 	}
   610 	}
   621 
   611 
   622 	if (v->cur_order_index >= v->num_orders)
   612 	if (v->cur_order_index >= v->num_orders)
   623 		v->cur_order_index = 0;
   613 		v->cur_order_index = 0;
   714 		}
   704 		}
   715 	}
   705 	}
   716 
   706 
   717 	v->cur_order_index++;
   707 	v->cur_order_index++;
   718 	InvalidateVehicleOrder(v);
   708 	InvalidateVehicleOrder(v);
   719 
       
   720 	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
       
   721 		// only the vehicle owner needs to calculate the rest (locally)
       
   722 		if ((_autoreplace_array[v->engine_type] != v->engine_type) ||
       
   723 			(_patches.autorenew && v->age - v->max_age > (_patches.autorenew_months * 30))) {
       
   724 			byte flags = 1;
       
   725 			// the flags means, bit 0 = needs to go to depot, bit 1 = have depot in orders
       
   726 			if (VehicleHasDepotOrders(v)) SETBIT(flags, 1);
       
   727 			if (!(HASBIT(flags, 1) && v->set_for_replacement)) {
       
   728 				_current_player = _local_player;
       
   729 				DoCommandP(v->tile, v->index, flags, NULL, CMD_SEND_ROADVEH_TO_DEPOT | CMD_SHOW_NO_ERROR);
       
   730 				_current_player = OWNER_NONE;
       
   731 			}
       
   732 		} else { // no need to go to a depot
       
   733 			if (v->set_for_replacement) {
       
   734 				// it seems that the user clicked "Stop replacing"
       
   735 				_current_player = _local_player;
       
   736 				DoCommandP(v->tile, v->index, 1 | (1 << 2), NULL, CMD_SEND_ROADVEH_TO_DEPOT | CMD_SHOW_NO_ERROR);
       
   737 				_current_player = OWNER_NONE;
       
   738 			}
       
   739 		}
       
   740 	}
       
   741 }
   709 }
   742 
   710 
   743 static void StartRoadVehSound(Vehicle *v)
   711 static void StartRoadVehSound(Vehicle *v)
   744 {
   712 {
   745 	SoundFx s = RoadVehInfo(v->engine_type)->sfx;
   713 	SoundFx s = RoadVehInfo(v->engine_type)->sfx;
  1598 
  1566 
  1599 static void CheckIfRoadVehNeedsService(Vehicle *v)
  1567 static void CheckIfRoadVehNeedsService(Vehicle *v)
  1600 {
  1568 {
  1601 	Depot *depot;
  1569 	Depot *depot;
  1602 
  1570 
  1603 	if (_patches.servint_roadveh == 0 && !v->set_for_replacement)
  1571 	if (_patches.servint_roadveh == 0)
  1604 		return;
  1572 		return;
  1605 
  1573 
  1606 	if (!VehicleNeedsService(v) && !v->set_for_replacement)
  1574 	if (!VehicleNeedsService(v))
  1607 		return;
  1575 		return;
  1608 
  1576 
  1609 	if (v->vehstatus & VS_STOPPED)
  1577 	if (v->vehstatus & VS_STOPPED)
  1610 		return;
  1578 		return;
  1611 
  1579 
  1623 		return;
  1591 		return;
  1624 
  1592 
  1625 	depot = FindClosestRoadDepot(v);
  1593 	depot = FindClosestRoadDepot(v);
  1626 
  1594 
  1627 	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
  1595 	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
  1628 		if (v->current_order.type == OT_GOTO_DEPOT && !(
  1596 		if (v->current_order.type == OT_GOTO_DEPOT) {
  1629 			DistanceManhattan(v->tile, v->dest_tile) > 25 && v->set_for_replacement)) {
       
  1630 			/*  a vehicle needs a greater distance to a depot to loose it than to find it since
       
  1631 			they can circle forever othervise if they are in a loop with an unlucky distance	*/
       
  1632 			v->current_order.type = OT_DUMMY;
  1597 			v->current_order.type = OT_DUMMY;
  1633 			v->current_order.flags = 0;
  1598 			v->current_order.flags = 0;
  1634 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1599 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1635 		}
  1600 		}
  1636 		return;
  1601 		return;