tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "vehicle.h" truelight@0: #include "engine.h" truelight@0: #include "command.h" truelight@0: #include "station.h" truelight@0: #include "news.h" truelight@0: #include "pathfind.h" matthijs@1247: #include "npf.h" truelight@0: #include "player.h" tron@337: #include "sound.h" truelight@1313: #include "depot.h" matthijs@1752: #include "vehicle_gui.h" truelight@0: truelight@0: void ShowRoadVehViewWindow(Vehicle *v); truelight@0: truelight@0: static const uint16 _roadveh_images[63] = { truelight@0: 0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14, truelight@0: 0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74, truelight@0: 0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C, truelight@0: 0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC, truelight@0: 0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC, truelight@0: 0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4, truelight@0: 0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C, truelight@0: 0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4 truelight@0: }; truelight@0: truelight@0: static const uint16 _roadveh_full_adder[63] = { truelight@0: 0, 88, 0, 0, 0, 0, 48, 48, truelight@0: 48, 48, 0, 0, 64, 64, 0, 16, truelight@0: 16, 0, 88, 0, 0, 0, 0, 48, truelight@0: 48, 48, 48, 0, 0, 64, 64, 0, truelight@0: 16, 16, 0, 88, 0, 0, 0, 0, truelight@0: 48, 48, 48, 48, 0, 0, 64, 64, truelight@0: 0, 16, 16, 0, 8, 8, 8, 8, truelight@0: 0, 0, 0, 8, 8, 8, 8 truelight@0: }; truelight@0: truelight@0: truelight@0: static const uint16 _road_veh_fp_ax_or[4] = { truelight@0: 0x100,0x200,1,2, truelight@0: }; truelight@0: truelight@0: static const uint16 _road_veh_fp_ax_and[4] = { truelight@0: 0x1009, 0x16, 0x520, 0x2A00 truelight@0: }; truelight@0: truelight@0: static const byte _road_reverse_table[4] = { truelight@0: 6, 7, 14, 15 truelight@0: }; truelight@0: truelight@0: static const uint16 _road_pf_table_3[4] = { truelight@0: 0x910, 0x1600, 0x2005, 0x2A truelight@0: }; truelight@0: Darkvater@1790: int GetRoadVehImage(const Vehicle *v, byte direction) truelight@0: { truelight@0: int img = v->spritenum; truelight@0: int image; truelight@0: truelight@0: if (is_custom_sprite(img)) { truelight@0: image = GetCustomVehicleSprite(v, direction); truelight@0: if (image) return image; peter1138@2464: img = orig_road_vehicle_info[v->engine_type - ROAD_ENGINES_INDEX].image_index; truelight@0: } truelight@193: truelight@0: image = direction + _roadveh_images[img]; truelight@0: if (v->cargo_count >= (v->cargo_cap >> 1)) truelight@0: image += _roadveh_full_adder[img]; truelight@0: return image; truelight@0: } truelight@0: tron@2477: void DrawRoadVehEngine(int x, int y, EngineID engine, uint32 image_ormod) truelight@0: { tron@538: int spritenum = RoadVehInfo(engine)->image_index; celestar@378: celestar@378: if (is_custom_sprite(spritenum)) { celestar@378: int sprite = GetCustomVehicleIcon(engine, 6); celestar@378: celestar@378: if (sprite) { celestar@378: DrawSprite(sprite | image_ormod, x, y); celestar@378: return; celestar@378: } peter1138@2464: spritenum = orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index; celestar@378: } celestar@378: DrawSprite((6 + _roadveh_images[spritenum]) | image_ormod, x, y); truelight@0: } truelight@0: Darkvater@1793: int32 EstimateRoadVehCost(EngineID engine_type) truelight@0: { tron@538: return ((_price.roadveh_base >> 3) * RoadVehInfo(engine_type)->base_cost) >> 5; truelight@0: } truelight@0: Darkvater@1793: /** Build a road vehicle. Darkvater@1793: * @param x,y tile coordinates of depot where road vehicle is built Darkvater@1793: * @param p1 bus/truck type being built (engine) Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdBuildRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 cost; truelight@0: Vehicle *v; truelight@1282: UnitID unit_num; tron@1980: TileIndex tile = TileVirtXY(x, y); truelight@0: Engine *e; truelight@193: tron@1197: if (!IsEngineBuildable(p1, VEH_Road)) return CMD_ERROR; bjarni@1196: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: truelight@0: cost = EstimateRoadVehCost(p1); Darkvater@1793: if (flags & DC_QUERY_COST) return cost; truelight@0: pasky@1442: /* The ai_new queries the vehicle cost before building the route, pasky@1442: * so we must check against cheaters no sooner than now. --pasky */ pasky@1442: if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return CMD_ERROR; tron@1901: if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; pasky@1442: truelight@0: v = AllocateVehicle(); truelight@1024: if (v == NULL || IsOrderPoolFull()) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@0: truelight@0: /* find the first free roadveh id */ truelight@0: unit_num = GetFreeUnitNumber(VEH_Road); truelight@0: if (unit_num > _patches.max_roadveh) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@0: truelight@0: if (flags & DC_EXEC) { tron@538: const RoadVehicleInfo *rvi = RoadVehInfo(p1); tron@538: truelight@0: v->unitnumber = unit_num; truelight@0: v->direction = 0; truelight@0: v->owner = _current_player; truelight@0: truelight@0: v->tile = tile; tron@926: x = TileX(tile) * 16 + 8; tron@926: y = TileY(tile) * 16 + 8; truelight@0: v->x_pos = x; truelight@0: v->y_pos = y; truelight@0: v->z_pos = GetSlopeZ(x,y); truelight@0: v->z_height = 6; truelight@0: truelight@0: v->u.road.state = 254; truelight@0: v->vehstatus = VS_HIDDEN|VS_STOPPED|VS_DEFPAL; truelight@0: tron@538: v->spritenum = rvi->image_index; tron@538: v->cargo_type = rvi->cargo_type; tron@538: v->cargo_cap = rvi->capacity; truelight@0: // v->cargo_count = 0; truelight@0: v->value = cost; truelight@0: // v->day_counter = 0; truelight@0: // v->next_order_param = v->next_order = 0; truelight@0: // v->load_unload_time_rem = 0; truelight@0: // v->progress = 0; truelight@0: truelight@0: // v->u.road.unk2 = 0; truelight@0: // v->u.road.overtaking = 0; truelight@0: celestar@1217: v->u.road.slot = NULL; celestar@1217: v->u.road.slotindex = 0; celestar@1217: v->u.road.slot_age = 0; celestar@1217: truelight@1266: v->last_station_visited = INVALID_STATION; tron@538: v->max_speed = rvi->max_speed; truelight@0: v->engine_type = (byte)p1; truelight@0: tron@1926: e = GetEngine(p1); truelight@0: v->reliability = e->reliability; truelight@0: v->reliability_spd_dec = e->reliability_spd_dec; truelight@0: v->max_age = e->lifelength * 366; truelight@0: _new_roadveh_id = v->index; truelight@193: truelight@0: v->string_id = STR_SV_ROADVEH_NAME; truelight@0: truelight@0: v->service_interval = _patches.servint_roadveh; truelight@0: truelight@0: v->date_of_last_service = _date; truelight@0: v->build_year = _cur_year; truelight@0: truelight@0: v->type = VEH_Road; truelight@0: v->cur_image = 0xC15; truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); Darkvater@1793: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: Darkvater@1793: /** Start/Stop a road vehicle. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 road vehicle ID to start/stop Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdStartStopRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1793: if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->vehstatus ^= VS_STOPPED; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1680: void ClearSlot(Vehicle *v, RoadStop *rs) Darkvater@1664: { Darkvater@1664: DEBUG(ms, 3) ("Multistop: Clearing slot %d at 0x%x", v->u.road.slotindex, rs->xy); Darkvater@1664: v->u.road.slot = NULL; Darkvater@1664: v->u.road.slot_age = 0; Darkvater@1680: if (rs != NULL) { Darkvater@1680: // check that the slot is indeed assigned to the same vehicle Darkvater@1680: assert(rs->slot[v->u.road.slotindex] == v->index); Darkvater@1664: rs->slot[v->u.road.slotindex] = INVALID_SLOT; Darkvater@1680: } Darkvater@1664: } Darkvater@1664: Darkvater@1793: /** Sell a road vehicle. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 vehicle ID to be sold Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdSellRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; truelight@193: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1793: if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: bjarni@1237: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); bjarni@1237: matthijs@1330: if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) truelight@0: return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: // Invalidate depot truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); Darkvater@1664: ClearSlot(v, v->u.road.slot); truelight@0: DeleteVehicle(v); truelight@0: } bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window truelight@193: truelight@0: return -(int32)v->value; truelight@0: } truelight@0: truelight@0: typedef struct RoadFindDepotData { truelight@0: uint best_length; tron@1977: TileIndex tile; truelight@0: byte owner; truelight@0: } RoadFindDepotData; truelight@0: truelight@0: static const byte _road_pf_directions[16] = { truelight@0: 0, 1, 0, 1, 2, 1, 255, 255, truelight@0: 2, 3, 3, 2, 3, 0, 255, 255, truelight@0: }; truelight@0: tron@1977: static bool EnumRoadSignalFindDepot(TileIndex tile, RoadFindDepotData *rfdd, int track, uint length, byte *state) truelight@0: { tron@904: tile += TileOffsByDir(_road_pf_directions[track]); truelight@0: tron@1035: if (IsTileType(tile, MP_STREET) && tron@2493: GB(_m[tile].m5, 4, 4) == 2 && tron@1901: IsTileOwner(tile, rfdd->owner)) { truelight@193: truelight@0: if (length < rfdd->best_length) { truelight@0: rfdd->best_length = length; truelight@0: rfdd->tile = tile; truelight@0: } truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@1313: static Depot *FindClosestRoadDepot(Vehicle *v) truelight@0: { tron@1977: TileIndex tile = v->tile; truelight@0: int i; truelight@193: tron@2549: if (v->u.road.state == 255) tile = GetVehicleOutOfTunnelTile(v); truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { matthijs@1247: NPFFoundTargetData ftd; matthijs@1247: /* See where we are now */ matthijs@1942: Trackdir trackdir = GetVehicleTrackdir(v); matthijs@1745: matthijs@2006: ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE); matthijs@1247: if (ftd.best_bird_dist == 0) matthijs@1247: return GetDepotByTile(ftd.node.tile); /* Target found */ matthijs@1247: else truelight@1313: return NULL; /* Target not found */ matthijs@1247: /* We do not search in two directions here, why should we? We can't reverse right now can we? */ matthijs@1247: } else { matthijs@1247: RoadFindDepotData rfdd; matthijs@1247: rfdd.owner = v->owner; matthijs@1247: rfdd.best_length = (uint)-1; truelight@0: matthijs@1247: /* search in all directions */ matthijs@1247: for(i=0; i!=4; i++) matthijs@1247: FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadSignalFindDepot, NULL, &rfdd); truelight@0: matthijs@1247: if (rfdd.best_length == (uint)-1) truelight@1313: return NULL; truelight@0: matthijs@1247: return GetDepotByTile(rfdd.tile); matthijs@1247: } truelight@0: } truelight@0: Darkvater@1793: /** Send a road vehicle to the depot. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 vehicle ID to send to the depot Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { bjarni@1237: Vehicle *v; Darkvater@1793: const Depot *dep; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: bjarni@1237: v = GetVehicle(p1); bjarni@1237: Darkvater@1793: if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: Darkvater@1793: if (v->vehstatus & VS_CRASHED) return CMD_ERROR; matthijs@1757: Darkvater@1793: /* If the current orders are already goto-depot */ tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { truelight@0: if (flags & DC_EXEC) { Darkvater@1793: /* If the orders to 'goto depot' are in the orders list (forced servicing), Darkvater@1793: * then skip to the next order; effectively cancelling this forced service */ celestar@1530: if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) truelight@0: v->cur_order_index++; celestar@1530: tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1793: dep = FindClosestRoadDepot(v); Darkvater@1793: if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT); truelight@0: truelight@0: if (flags & DC_EXEC) { tron@555: v->current_order.type = OT_GOTO_DEPOT; celestar@1530: v->current_order.flags = OF_NON_STOP | OF_HALT_IN_DEPOT; Darkvater@1793: v->current_order.station = dep->index; Darkvater@1793: v->dest_tile = dep->xy; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1793: /** Turn a roadvehicle around. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 vehicle ID to turn Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdTurnRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1793: if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: truelight@0: if (v->vehstatus & (VS_HIDDEN|VS_STOPPED) || truelight@0: v->u.road.crashed_ctr != 0 || truelight@0: v->breakdown_ctr != 0 || truelight@0: v->u.road.overtaking != 0 || truelight@0: v->cur_speed < 5) { truelight@0: return CMD_ERROR; truelight@0: } truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: v->u.road.reverse_ctr = 180; truelight@193: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1790: /** Change the service interval for road vehicles. Darkvater@1790: * @param x,y unused Darkvater@1790: * @param p1 vehicle ID that is being service-interval-changed Darkvater@1790: * @param p2 new service interval Darkvater@1790: */ truelight@0: int32 CmdChangeRoadVehServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; Darkvater@1790: uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */ truelight@0: Darkvater@1790: if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1790: if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { Darkvater@1790: v->service_interval = serv_int; truelight@0: InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: truelight@0: static void MarkRoadVehDirty(Vehicle *v) truelight@0: { truelight@0: v->cur_image = GetRoadVehImage(v, v->direction); truelight@0: MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); truelight@0: } truelight@0: truelight@0: static void UpdateRoadVehDeltaXY(Vehicle *v) truelight@0: { truelight@0: #define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) truelight@0: static const uint32 _delta_xy_table[8] = { truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(3, 7, -1, -3), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(7, 3, -3, -1), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(3, 7, -1, -3), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(7, 3, -3, -1), truelight@0: }; truelight@0: #undef MKIT truelight@0: uint32 x = _delta_xy_table[v->direction]; tron@2150: v->x_offs = GB(x, 0, 8); tron@2150: v->y_offs = GB(x, 8, 8); tron@2150: v->sprite_width = GB(x, 16, 8); tron@2150: v->sprite_height = GB(x, 24, 8); truelight@0: } truelight@0: truelight@0: static void ClearCrashedStation(Vehicle *v) truelight@0: { tron@1977: TileIndex tile = v->tile; truelight@0: byte *b, bb; truelight@0: celestar@1217: RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile)); celestar@1217: b = &rs->status; truelight@0: truelight@0: bb = *b; truelight@0: truelight@0: // mark station as not busy truelight@0: bb &= ~0x80; truelight@193: truelight@0: // free parking bay truelight@0: bb |= (v->u.road.state&0x02)?2:1; truelight@193: truelight@0: *b = bb; truelight@0: } truelight@0: truelight@0: static void RoadVehDelete(Vehicle *v) truelight@0: { truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@193: tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: tron@1035: if (IsTileType(v->tile, MP_STATION)) truelight@0: ClearCrashedStation(v); truelight@193: truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@193: celestar@1665: ClearSlot(v, v->u.road.slot); truelight@0: DeleteVehicle(v); truelight@0: } truelight@0: truelight@0: static byte SetRoadVehPosition(Vehicle *v, int x, int y) truelight@0: { truelight@0: byte new_z, old_z; truelight@0: truelight@0: // need this hint so it returns the right z coordinate on bridges. truelight@0: _get_z_hint = v->z_pos; truelight@0: new_z = GetSlopeZ(v->x_pos=x, v->y_pos=y); truelight@0: _get_z_hint = 0; truelight@0: truelight@0: old_z = v->z_pos; truelight@0: v->z_pos = new_z; truelight@193: truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return old_z; truelight@0: } truelight@0: truelight@0: static void RoadVehSetRandomDirection(Vehicle *v) truelight@0: { truelight@0: static const int8 _turn_prob[4] = { -1, 0, 0, 1 }; truelight@0: uint32 r = Random(); truelight@0: v->direction = (v->direction+_turn_prob[r&3])&7; truelight@0: BeginVehicleMove(v); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: v->cur_image = GetRoadVehImage(v, v->direction); truelight@0: SetRoadVehPosition(v, v->x_pos, v->y_pos); truelight@0: } truelight@0: truelight@0: static void RoadVehIsCrashed(Vehicle *v) truelight@0: { truelight@0: v->u.road.crashed_ctr++; truelight@0: if (v->u.road.crashed_ctr == 2) { tron@1359: CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); truelight@0: } else if (v->u.road.crashed_ctr <= 45) { truelight@0: if ((v->tick_counter&7)==0) truelight@0: RoadVehSetRandomDirection(v); truelight@0: } else if (v->u.road.crashed_ctr >= 2220) { truelight@0: RoadVehDelete(v); truelight@0: } truelight@0: } truelight@0: truelight@0: static void *EnumCheckRoadVehCrashTrain(Vehicle *v, Vehicle *u) truelight@0: { truelight@0: if (v->type != VEH_Train || truelight@0: myabs(v->z_pos - u->z_pos) > 6 || truelight@0: myabs(v->x_pos - u->x_pos) > 4 || truelight@0: myabs(v->y_pos - u->y_pos) > 4) truelight@0: return NULL; truelight@0: return v; truelight@0: } truelight@0: truelight@0: static void RoadVehCrash(Vehicle *v) truelight@0: { truelight@0: uint16 pass; truelight@0: truelight@0: v->u.road.crashed_ctr++; truelight@0: v->vehstatus |= VS_CRASHED; truelight@0: darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: truelight@0: pass = 1; truelight@0: if (v->cargo_type == 0) truelight@0: pass += v->cargo_count; truelight@0: v->cargo_count = 0; tron@534: SetDParam(0, pass); truelight@0: tron@2260: AddNewsItem( tron@2260: (pass == 1) ? tron@2260: STR_9031_ROAD_VEHICLE_CRASH_DRIVER : STR_9032_ROAD_VEHICLE_CRASH_DIE, truelight@0: NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), truelight@0: v->index, truelight@0: 0); truelight@0: truelight@0: ModifyStationRatingAround(v->tile, v->owner, -160, 22); tron@541: SndPlayVehicleFx(SND_12_EXPLOSION, v); truelight@0: } truelight@0: truelight@0: static void RoadVehCheckTrainCrash(Vehicle *v) truelight@0: { tron@1977: TileIndex tile; truelight@0: truelight@0: if (v->u.road.state == 255) truelight@0: return; truelight@0: truelight@0: tile = v->tile; truelight@0: truelight@0: // Make sure it's a road/rail crossing Darkvater@1927: if (!IsTileType(tile, MP_STREET) || !IsLevelCrossing(tile)) Darkvater@1927: return; truelight@0: truelight@0: if (VehicleFromPos(tile, v, (VehicleFromPosProc*)EnumCheckRoadVehCrashTrain) != NULL) truelight@0: RoadVehCrash(v); truelight@0: } truelight@0: truelight@0: static void HandleBrokenRoadVeh(Vehicle *v) truelight@0: { truelight@0: if (v->breakdown_ctr != 1) { truelight@0: v->breakdown_ctr = 1; truelight@0: v->cur_speed = 0; truelight@0: truelight@0: if (v->breakdowns_since_last_service != 255) truelight@0: v->breakdowns_since_last_service++; truelight@193: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@193: tron@541: SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? tron@541: SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v); truelight@0: truelight@0: if (!(v->vehstatus & VS_HIDDEN)) { truelight@0: Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); truelight@0: if (u) truelight@0: u->u.special.unk0 = v->breakdown_delay * 2; truelight@0: } truelight@0: } truelight@0: truelight@0: if (!(v->tick_counter & 1)) { truelight@0: if (!--v->breakdown_delay) { truelight@0: v->breakdown_ctr = 0; truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void ProcessRoadVehOrder(Vehicle *v) truelight@0: { truelight@1024: const Order *order; truelight@1024: const Station *st; truelight@0: tron@555: if (v->current_order.type >= OT_GOTO_DEPOT && v->current_order.type <= OT_LEAVESTATION) { truelight@1024: // Let a depot order in the orderlist interrupt. tron@555: if (v->current_order.type != OT_GOTO_DEPOT || tron@555: !(v->current_order.flags & OF_UNLOAD)) truelight@0: return; truelight@0: } truelight@0: tron@555: if (v->current_order.type == OT_GOTO_DEPOT && celestar@2214: (v->current_order.flags & (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED)) == (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED) && bjarni@1520: !VehicleNeedsService(v)) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: if (v->cur_order_index >= v->num_orders) truelight@0: v->cur_order_index = 0; truelight@0: truelight@1024: order = GetVehicleOrder(v, v->cur_order_index); truelight@0: truelight@1024: if (order == NULL) { tron@555: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; truelight@0: v->dest_tile = 0; truelight@0: return; truelight@0: } truelight@0: truelight@1024: if (order->type == v->current_order.type && truelight@1024: order->flags == v->current_order.flags && truelight@1024: order->station == v->current_order.station) truelight@0: return; truelight@0: truelight@1024: v->current_order = *order; truelight@0: truelight@0: v->dest_tile = 0; truelight@0: truelight@1024: if (order->type == OT_GOTO_STATION) { truelight@1024: if (order->station == v->last_station_visited) truelight@1266: v->last_station_visited = INVALID_STATION; celestar@1217: st = GetStation(order->station); truelight@1024: celestar@1217: { celestar@1709: uint mindist = 0xFFFFFFFF; celestar@1217: int num; celestar@1217: RoadStopType type; celestar@1217: RoadStop *rs; celestar@1217: celestar@1217: type = (v->cargo_type == CT_PASSENGERS) ? RS_BUS : RS_TRUCK; celestar@1217: num = GetNumRoadStops(st, type); celestar@1217: rs = GetPrimaryRoadStop(st, type); celestar@1217: celestar@1231: if (rs == NULL) { celestar@1231: //There is no stop left at the station, so don't even TRY to go there celestar@1231: v->cur_order_index++; celestar@1231: InvalidateVehicleOrder(v); celestar@1231: celestar@1231: return; celestar@1231: } celestar@1217: celestar@1709: for (rs = GetPrimaryRoadStop(st, type); rs != NULL; rs = rs->next) { celestar@1709: if (DistanceManhattan(v->tile, rs->xy) < mindist) { celestar@1217: v->dest_tile = rs->xy; celestar@1217: } celestar@1709: } celestar@1217: celestar@1217: } truelight@1024: } else if (order->type == OT_GOTO_DEPOT) { truelight@1313: v->dest_tile = GetDepot(order->station)->xy; truelight@0: } truelight@0: truelight@1024: InvalidateVehicleOrder(v); truelight@0: } truelight@0: truelight@0: static void HandleRoadVehLoading(Vehicle *v) truelight@0: { tron@555: if (v->current_order.type == OT_NOTHING) truelight@0: return; truelight@193: tron@555: if (v->current_order.type != OT_DUMMY) { tron@555: if (v->current_order.type != OT_LOADING) truelight@0: return; truelight@0: truelight@0: if (--v->load_unload_time_rem) truelight@0: return; truelight@0: tron@555: if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { truelight@0: SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC); truelight@0: if (LoadUnloadVehicle(v)) { truelight@0: InvalidateWindow(WC_ROADVEH_LIST, v->owner); truelight@0: MarkRoadVehDirty(v); truelight@0: } truelight@0: return; truelight@0: } truelight@193: truelight@0: { tron@555: Order b = v->current_order; tron@555: v->current_order.type = OT_LEAVESTATION; tron@555: v->current_order.flags = 0; tron@555: if (!(b.flags & OF_NON_STOP)) truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: v->cur_order_index++; truelight@1024: InvalidateVehicleOrder(v); truelight@0: } truelight@0: truelight@0: static void StartRoadVehSound(Vehicle *v) truelight@0: { tron@541: SoundFx s = RoadVehInfo(v->engine_type)->sfx; tron@541: if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0) tron@541: s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN; truelight@0: SndPlayVehicleFx(s, v); truelight@0: } truelight@0: truelight@0: typedef struct RoadVehFindData { truelight@0: int x,y; truelight@0: Vehicle *veh; truelight@0: byte dir; truelight@0: } RoadVehFindData; truelight@0: tron@1095: static void *EnumCheckRoadVehClose(Vehicle *v, RoadVehFindData *rvf) truelight@0: { truelight@193: static const short _dists[] = { dominik@63: -4, -8, -4, -1, 4, 8, 4, 1, dominik@63: -4, -1, 4, 8, 4, 1, -4, -8, truelight@0: }; truelight@193: dominik@63: short x_diff = v->x_pos - rvf->x; dominik@63: short y_diff = v->y_pos - rvf->y; truelight@0: truelight@193: if (rvf->veh == v || truelight@193: v->type != VEH_Road || truelight@0: v->u.road.state == 254 || truelight@0: myabs(v->z_pos - rvf->veh->z_pos) > 6 || truelight@0: v->direction != rvf->dir || dominik@63: (_dists[v->direction] < 0 && (x_diff <= _dists[v->direction] || x_diff > 0)) || dominik@63: (_dists[v->direction] > 0 && (x_diff >= _dists[v->direction] || x_diff < 0)) || dominik@63: (_dists[v->direction+8] < 0 && (y_diff <= _dists[v->direction+8] || y_diff > 0)) || dominik@63: (_dists[v->direction+8] > 0 && (y_diff >= _dists[v->direction+8] || y_diff < 0))) bjarni@1151: return NULL; truelight@0: truelight@0: return v; truelight@0: } truelight@0: truelight@0: static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, byte dir) truelight@0: { truelight@0: RoadVehFindData rvf; truelight@0: Vehicle *u; truelight@0: truelight@0: if (v->u.road.reverse_ctr != 0) truelight@0: return NULL; truelight@193: truelight@0: rvf.x = x; truelight@0: rvf.y = y; truelight@0: rvf.dir = dir; truelight@0: rvf.veh = v; tron@1980: u = VehicleFromPos(TileVirtXY(x, y), &rvf, (VehicleFromPosProc*)EnumCheckRoadVehClose); truelight@193: dominik@63: // This code protects a roadvehicle from being blocked for ever miham@826: // If more than 1480 / 74 days a road vehicle is blocked, it will dominik@63: // drive just through it. The ultimate backup-code of TTD. dominik@63: // It can be disabled. truelight@0: if (u == NULL) { truelight@0: v->u.road.unk2 = 0; truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: if (++v->u.road.unk2 > 1480) truelight@0: return NULL; truelight@193: truelight@0: return u; truelight@0: } truelight@0: truelight@0: static void RoadVehArrivesAt(Vehicle *v, Station *st) truelight@0: { tron@2550: if (v->cargo_type == CT_PASSENGERS) { truelight@0: /* Check if station was ever visited before */ truelight@0: if (!(st->had_vehicle_of_type & HVOT_BUS)) { truelight@0: uint32 flags; truelight@0: truelight@0: st->had_vehicle_of_type |= HVOT_BUS; tron@534: SetDParam(0, st->index); truelight@0: flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); truelight@0: AddNewsItem( truelight@0: STR_902F_CITIZENS_CELEBRATE_FIRST, truelight@0: flags, truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: } else { truelight@0: /* Check if station was ever visited before */ truelight@0: if (!(st->had_vehicle_of_type & HVOT_TRUCK)) { truelight@0: uint32 flags; truelight@0: truelight@0: st->had_vehicle_of_type |= HVOT_TRUCK; tron@534: SetDParam(0, st->index); truelight@0: flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); truelight@0: AddNewsItem( truelight@0: STR_9030_CITIZENS_CELEBRATE_FIRST, truelight@0: flags, truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static bool RoadVehAccelerate(Vehicle *v) truelight@0: { truelight@0: uint spd = v->cur_speed + 1 + ((v->u.road.overtaking != 0)?1:0); truelight@0: byte t; truelight@193: truelight@0: // Clamp truelight@0: spd = min(spd, v->max_speed); truelight@0: truelight@0: //updates statusbar only if speed have changed to save CPU time truelight@0: if (spd != v->cur_speed) { truelight@193: v->cur_speed = spd; truelight@0: if (_patches.vehicle_speed) darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: // Decrease somewhat when turning truelight@0: if (!(v->direction&1)) truelight@0: spd = spd * 3 >> 2; truelight@0: truelight@0: if (spd == 0) truelight@0: return false; truelight@0: truelight@0: if ((byte)++spd == 0) truelight@0: return true; truelight@0: truelight@0: v->progress = (t = v->progress) - (byte)spd; truelight@0: truelight@0: return (t < v->progress); truelight@0: } truelight@0: truelight@0: static byte RoadVehGetNewDirection(Vehicle *v, int x, int y) truelight@0: { truelight@0: static const byte _roadveh_new_dir[11] = { truelight@0: 0, 7, 6, 0, truelight@0: 1, 0, 5, 0, truelight@0: 2, 3, 4 truelight@0: }; truelight@193: truelight@0: x = x - v->x_pos + 1; truelight@0: y = y - v->y_pos + 1; truelight@0: truelight@0: if ((uint)x > 2 || (uint)y > 2) truelight@0: return v->direction; truelight@0: return _roadveh_new_dir[y*4+x]; truelight@0: } truelight@0: truelight@0: static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y) truelight@0: { truelight@0: byte b = RoadVehGetNewDirection(v,x,y); truelight@0: byte d = v->direction; truelight@0: if (b == d) return d; truelight@0: d = (d+1)&7; truelight@0: if (b==d) return d; truelight@0: d = (d-2)&7; truelight@0: if (b==d) return d; truelight@0: if (b==((d-1)&7)) return d; truelight@0: if (b==((d-2)&7)) return d; truelight@193: return (d+2)&7; truelight@0: } truelight@0: truelight@0: typedef struct OvertakeData { truelight@0: Vehicle *u, *v; tron@1977: TileIndex tile; truelight@0: byte tilebits; truelight@0: } OvertakeData; truelight@0: tron@1095: static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od) truelight@0: { tron@1986: if (v->tile != od->tile || v->type != VEH_Road || v == od->u || v == od->v) tron@1986: return NULL; truelight@193: return v; truelight@0: } truelight@0: truelight@0: static bool FindRoadVehToOvertake(OvertakeData *od) truelight@0: { truelight@0: uint32 bits; truelight@0: truelight@159: bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD)&0x3F; truelight@0: truelight@0: if (!(od->tilebits & bits) || (bits&0x3C) || (bits & 0x3F3F0000)) truelight@0: return true; truelight@0: return VehicleFromPos(od->tile, od, (VehicleFromPosProc*)EnumFindVehToOvertake) != NULL; truelight@0: } truelight@0: truelight@0: static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u) truelight@0: { truelight@0: OvertakeData od; truelight@0: byte tt; truelight@0: truelight@0: od.v = v; truelight@0: od.u = u; truelight@0: truelight@0: if (u->max_speed >= v->max_speed && truelight@0: !(u->vehstatus&VS_STOPPED) && truelight@0: u->cur_speed != 0) truelight@0: return; truelight@0: truelight@0: if (v->direction != u->direction || !(v->direction&1)) truelight@0: return; truelight@0: truelight@0: if (v->u.road.state >= 32 || (v->u.road.state&7) > 1 ) truelight@0: return; truelight@0: tron@2150: tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F; truelight@0: if ((tt & 3) == 0) truelight@0: return; truelight@0: if ((tt & 0x3C) != 0) truelight@0: return; truelight@0: truelight@0: if (tt == 3) { truelight@0: tt = (v->direction&2)?2:1; truelight@0: } truelight@0: od.tilebits = tt; truelight@0: truelight@0: od.tile = v->tile; truelight@0: if (FindRoadVehToOvertake(&od)) truelight@0: return; truelight@0: tron@900: od.tile = v->tile + TileOffsByDir(v->direction >> 1); truelight@0: if (FindRoadVehToOvertake(&od)) truelight@0: return; truelight@0: truelight@0: if (od.u->cur_speed == 0 || od.u->vehstatus&VS_STOPPED) { truelight@0: v->u.road.overtaking_ctr = 0x11; truelight@0: v->u.road.overtaking = 0x10; truelight@0: } else { truelight@0: // if (FindRoadVehToOvertake(&od)) truelight@0: // return; truelight@0: v->u.road.overtaking_ctr = 0; truelight@0: v->u.road.overtaking = 0x10; truelight@0: } truelight@0: } truelight@0: truelight@0: static void RoadZPosAffectSpeed(Vehicle *v, byte old_z) truelight@0: { truelight@0: if (old_z == v->z_pos) truelight@0: return; truelight@0: truelight@0: if (old_z < v->z_pos) { truelight@0: v->cur_speed = v->cur_speed * 232 >> 8; truelight@0: } else { truelight@0: uint16 spd = v->cur_speed + 2; truelight@0: if (spd <= v->max_speed) truelight@0: v->cur_speed = spd; truelight@0: } truelight@0: } truelight@0: truelight@0: static int PickRandomBit(uint bits) truelight@0: { truelight@0: uint num = 0; truelight@0: uint b = bits; truelight@0: uint i; truelight@0: truelight@0: do { truelight@0: if (b & 1) truelight@0: num++; truelight@0: } while (b >>= 1); truelight@0: tron@2484: num = GB(Random(), 0, 16) * num >> 16; truelight@0: truelight@0: for(i=0; !((bits & 1) && ((int)--num) < 0); bits>>=1,i++); truelight@0: return i; truelight@0: } truelight@0: truelight@0: typedef struct { truelight@0: TileIndex dest; truelight@0: uint maxtracklen; truelight@0: uint mindist; truelight@0: } FindRoadToChooseData; truelight@0: tron@1977: static bool EnumRoadTrackFindDist(TileIndex tile, FindRoadToChooseData *frd, int track, uint length, byte *state) truelight@0: { tron@1245: uint dist = DistanceManhattan(tile, frd->dest); truelight@0: if (dist <= frd->mindist) { truelight@0: if (dist != frd->mindist || length < frd->maxtracklen) { truelight@0: frd->maxtracklen = length; truelight@0: } truelight@0: frd->mindist = dist; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@0: // Returns direction to choose truelight@0: // or -1 if the direction is currently blocked tron@1977: static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir) truelight@0: { truelight@0: #define return_track(x) {best_track = x; goto found_best_track; } truelight@0: truelight@0: uint16 signal; truelight@0: uint bitmask; tron@1977: TileIndex desttile; truelight@0: FindRoadToChooseData frd; truelight@0: int best_track; truelight@0: uint best_dist, best_maxlen; truelight@0: uint i; truelight@0: byte m5; truelight@0: truelight@0: { truelight@0: uint32 r; darkvater@241: r = GetTileTrackStatus(tile, TRANSPORT_ROAD); tron@2484: signal = GB(r, 16, 16); tron@2484: bitmask = GB(r, 0, 16); truelight@0: } truelight@0: tron@1035: if (IsTileType(tile, MP_STREET)) { tron@2549: if (GB(_m[tile].m5, 4, 4) == 2 && IsTileOwner(tile, v->owner)) { tron@2549: /* Road depot */ tron@2549: bitmask |= _road_veh_fp_ax_or[GB(_m[tile].m5, 0, 2)]; tron@2549: } tron@1035: } else if (IsTileType(tile, MP_STATION)) { tron@1901: if (IsTileOwner(tile, OWNER_NONE) || IsTileOwner(tile, v->owner)) { darkvater@241: /* Our station */ tron@2549: const Station* st = GetStation(_m[tile].m2); tron@2049: byte val = _m[tile].m5; darkvater@241: if (v->cargo_type != CT_PASSENGERS) { celestar@1217: if (IS_BYTE_INSIDE(val, 0x43, 0x47) && (_patches.roadveh_queue || st->truck_stops->status&3)) darkvater@241: bitmask |= _road_veh_fp_ax_or[(val-0x43)&3]; darkvater@241: } else { celestar@1217: if (IS_BYTE_INSIDE(val, 0x47, 0x4B) && (_patches.roadveh_queue || st->bus_stops->status&3)) darkvater@241: bitmask |= _road_veh_fp_ax_or[(val-0x47)&3]; darkvater@241: } darkvater@241: } darkvater@241: } darkvater@241: /* The above lookups should be moved to GetTileTrackStatus in the darkvater@241: * future, but that requires more changes to the pathfinder and other darkvater@241: * stuff, probably even more arguments to GTTS. darkvater@241: */ truelight@0: truelight@159: /* remove unreachable tracks */ matthijs@1247: bitmask &= _road_veh_fp_ax_and[enterdir]; truelight@0: if (bitmask == 0) { matthijs@1247: /* No reachable tracks, so we'll reverse */ matthijs@1247: return_track(_road_reverse_table[enterdir]); truelight@0: } truelight@0: truelight@0: if (v->u.road.reverse_ctr != 0) { matthijs@1247: /* What happens here?? */ truelight@0: v->u.road.reverse_ctr = 0; tron@1986: if (v->tile != tile) { matthijs@1247: return_track(_road_reverse_table[enterdir]); truelight@0: } truelight@0: } truelight@0: truelight@0: desttile = v->dest_tile; truelight@0: if (desttile == 0) { matthijs@1247: // Pick a random track truelight@0: return_track(PickRandomBit(bitmask)); truelight@0: } truelight@0: matthijs@1247: // Only one track to choose between? matthijs@1247: if (!(KillFirstBit2x64(bitmask))) { truelight@0: return_track(FindFirstBit2x64(bitmask)); truelight@0: } truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { matthijs@1247: NPFFindStationOrTileData fstd; matthijs@1247: NPFFoundTargetData ftd; matthijs@1247: byte trackdir; matthijs@1247: matthijs@1247: NPFFillWithOrderData(&fstd, v); matthijs@1942: trackdir = DiagdirToDiagTrackdir(enterdir); matthijs@1247: //debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir); matthijs@1247: hackykid@2008: ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE, PBS_MODE_NONE); matthijs@1698: if (ftd.best_trackdir == 0xff) { matthijs@1698: /* We are already at our target. Just do something */ matthijs@1247: //TODO: maybe display error? matthijs@1247: //TODO: go straight ahead if possible? matthijs@1247: return_track(FindFirstBit2x64(bitmask)); matthijs@1247: } else { matthijs@1698: /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains matthijs@1698: the direction we need to take to get there, if ftd.best_bird_dist is not 0, matthijs@1698: we did not find our target, but ftd.best_trackdir contains the direction leading matthijs@1698: to the tile closest to our target. */ matthijs@1247: return_track(ftd.best_trackdir); matthijs@1247: } matthijs@1247: } else { matthijs@1247: if (IsTileType(desttile, MP_STREET)) { tron@2049: m5 = _m[desttile].m5; matthijs@1247: if ((m5&0xF0) == 0x20) matthijs@1247: /* We are heading for a Depot */ matthijs@1247: goto do_it; matthijs@1247: } else if (IsTileType(desttile, MP_STATION)) { tron@2049: m5 = _m[desttile].m5; matthijs@1247: if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) { matthijs@1247: /* We are heading for a station */ matthijs@1247: m5 -= 0x43; truelight@0: do_it:; matthijs@1247: /* When we are heading for a depot or station, we just matthijs@1247: * pretend we are heading for the tile in front, we'll matthijs@1247: * see from there */ matthijs@1247: desttile += TileOffsByDir(m5 & 3); matthijs@1247: if (desttile == tile && bitmask&_road_pf_table_3[m5&3]) { matthijs@1247: /* If we are already in front of the matthijs@1247: * station/depot and we can get in from here, matthijs@1247: * we enter */ matthijs@1247: return_track(FindFirstBit2x64(bitmask&_road_pf_table_3[m5&3])); matthijs@1247: } truelight@0: } truelight@193: } matthijs@1247: // do pathfind matthijs@1247: frd.dest = desttile; truelight@0: matthijs@1247: best_track = -1; matthijs@1247: best_dist = (uint)-1; matthijs@1247: best_maxlen = (uint)-1; matthijs@1247: i = 0; matthijs@1247: do { matthijs@1247: if (bitmask & 1) { matthijs@1247: if (best_track == -1) best_track = i; // in case we don't find the path, just pick a track matthijs@1247: frd.maxtracklen = (uint)-1; matthijs@1247: frd.mindist = (uint)-1; matthijs@1247: FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _road_pf_directions[i], (TPFEnumProc*)EnumRoadTrackFindDist, NULL, &frd); truelight@0: matthijs@1247: if (frd.mindist < best_dist || (frd.mindist==best_dist && frd.maxtracklen < best_maxlen)) { matthijs@1247: best_dist = frd.mindist; matthijs@1247: best_maxlen = frd.maxtracklen; matthijs@1247: best_track = i; matthijs@1247: } truelight@0: } matthijs@1247: } while (++i,(bitmask>>=1) != 0); matthijs@1247: } truelight@0: truelight@0: found_best_track:; truelight@0: truelight@0: if (HASBIT(signal, best_track)) truelight@0: return -1; truelight@0: truelight@193: return best_track; truelight@0: } truelight@0: Darkvater@2107: #if 0 celestar@1709: static uint RoadFindPathToStation(const Vehicle *v, TileIndex tile) celestar@1217: { celestar@1709: NPFFindStationOrTileData fstd; matthijs@1752: byte trackdir = GetVehicleTrackdir(v); matthijs@1752: assert(trackdir != 0xFF); celestar@1217: celestar@1709: fstd.dest_coords = tile; celestar@1709: fstd.station_index = -1; // indicates that the destination is a tile, not a station celestar@1217: hackykid@2008: return NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE, PBS_MODE_NONE).best_path_dist; celestar@1217: } Darkvater@2107: #endif celestar@1217: truelight@0: typedef struct RoadDriveEntry { truelight@0: byte x,y; truelight@0: } RoadDriveEntry; truelight@0: truelight@0: #include "table/roadveh.h" truelight@0: truelight@0: static const byte _road_veh_data_1[] = { truelight@0: 20, 20, 16, 16, 0, 0, 0, 0, truelight@0: 19, 19, 15, 15, 0, 0, 0, 0, truelight@0: 16, 16, 12, 12, 0, 0, 0, 0, truelight@0: 15, 15, 11, 11 truelight@0: }; truelight@0: truelight@0: static const byte _roadveh_data_2[4] = { 0,1,8,9 }; truelight@0: Darkvater@1401: static void RoadVehController(Vehicle *v) truelight@0: { truelight@0: GetNewVehiclePosResult gp; tron@555: byte new_dir, old_dir; truelight@0: RoadDriveEntry rd; truelight@0: int x,y; truelight@0: Station *st; truelight@0: uint32 r; truelight@0: Vehicle *u; truelight@0: truelight@0: // decrease counters truelight@0: v->tick_counter++; truelight@0: if (v->u.road.reverse_ctr != 0) truelight@0: v->u.road.reverse_ctr--; truelight@0: truelight@0: // handle crashed truelight@0: if (v->u.road.crashed_ctr != 0) { truelight@0: RoadVehIsCrashed(v); truelight@0: return; truelight@0: } truelight@0: truelight@0: RoadVehCheckTrainCrash(v); truelight@0: truelight@0: // road vehicle has broken down? truelight@0: if (v->breakdown_ctr != 0) { truelight@0: if (v->breakdown_ctr <= 2) { truelight@0: HandleBrokenRoadVeh(v); truelight@0: return; truelight@0: } truelight@0: v->breakdown_ctr--; truelight@0: } truelight@0: truelight@0: // exit if vehicle is stopped truelight@0: if (v->vehstatus & VS_STOPPED) truelight@0: return; truelight@0: truelight@0: ProcessRoadVehOrder(v); truelight@0: HandleRoadVehLoading(v); truelight@0: tron@555: if (v->current_order.type == OT_LOADING) truelight@0: return; truelight@0: truelight@0: if (v->u.road.state == 254) { truelight@0: int dir; truelight@0: const RoadDriveEntry*rdp; truelight@0: byte rd2; truelight@0: truelight@0: v->cur_speed = 0; truelight@193: tron@2493: dir = GB(_m[v->tile].m5, 0, 2); truelight@0: v->direction = dir*2+1; truelight@0: truelight@0: rd2 = _roadveh_data_2[dir]; truelight@0: rdp = _road_drive_data[(_opt.road_side<<4) + rd2]; truelight@193: tron@926: x = TileX(v->tile) * 16 + (rdp[6].x & 0xF); tron@926: y = TileY(v->tile) * 16 + (rdp[6].y & 0xF); truelight@0: truelight@0: if (RoadVehFindCloseTo(v,x,y,v->direction)) truelight@0: return; truelight@0: bjarni@578: VehicleServiceInDepot(v); bjarni@578: truelight@0: StartRoadVehSound(v); truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: v->u.road.state = rd2; truelight@0: v->u.road.frame = 6; truelight@193: truelight@0: v->cur_image = GetRoadVehImage(v, v->direction); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: SetRoadVehPosition(v,x,y); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!RoadVehAccelerate(v)) truelight@0: return; truelight@0: truelight@0: if (v->u.road.overtaking != 0) { truelight@0: if (++v->u.road.overtaking_ctr >= 35) truelight@0: v->u.road.overtaking = 0; truelight@0: } truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: if (v->u.road.state == 255) { truelight@0: GetNewVehiclePos(v, &gp); truelight@0: truelight@0: if (RoadVehFindCloseTo(v, gp.x, gp.y, v->direction)) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: tron@1035: if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && tron@2493: GB(_m[gp.new_tile].m5, 4, 4) == 0 && truelight@0: (VehicleEnterTile(v, gp.new_tile, gp.x, gp.y)&4)) { truelight@193: truelight@0: //new_dir = RoadGetNewDirection(v, gp.x, gp.y) truelight@0: v->cur_image = GetRoadVehImage(v, v->direction); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: SetRoadVehPosition(v,gp.x,gp.y); truelight@0: return; truelight@0: } truelight@0: truelight@0: v->x_pos = gp.x; truelight@0: v->y_pos = gp.y; truelight@0: VehiclePositionChanged(v); truelight@0: return; truelight@0: } truelight@0: truelight@0: rd = _road_drive_data[(v->u.road.state + (_opt.road_side<<4)) ^ v->u.road.overtaking][v->u.road.frame+1]; truelight@0: truelight@0: // switch to another tile truelight@0: if (rd.x & 0x80) { tron@1977: TileIndex tile = v->tile + TileOffsByDir(rd.x & 3); truelight@0: int dir = RoadFindPathToDest(v, tile, rd.x&3); truelight@0: int tmp; truelight@0: uint32 r; truelight@0: byte newdir; truelight@0: const RoadDriveEntry *rdp; truelight@0: truelight@0: if (dir == -1) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: truelight@0: again: truelight@0: if ((dir & 7) >= 6) { matthijs@1247: /* Turning around */ truelight@0: tile = v->tile; truelight@0: } truelight@0: truelight@0: tmp = (dir+(_opt.road_side<<4))^v->u.road.overtaking; truelight@0: rdp = _road_drive_data[tmp]; truelight@0: truelight@0: tmp &= ~0x10; truelight@0: tron@926: x = TileX(tile) * 16 + rdp[0].x; tron@926: y = TileY(tile) * 16 + rdp[0].y; truelight@0: truelight@0: if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) truelight@0: return; truelight@0: truelight@0: r = VehicleEnterTile(v, tile, x, y); truelight@0: if (r & 8) { tron@1035: if (!IsTileType(tile, MP_TUNNELBRIDGE)) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: dir = _road_reverse_table[rd.x&3]; truelight@0: goto again; truelight@0: } truelight@0: tron@1035: if (IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && IsTileType(v->tile, MP_STATION)) { truelight@0: if ((tmp&7) >= 6) { v->cur_speed = 0; return; } tron@2049: if (IS_BYTE_INSIDE(_m[v->tile].m5, 0x43, 0x4B)) { celestar@1217: RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile)); celestar@1217: byte *b = &rs->status; truelight@0: celestar@1217: //we have reached a loading bay, mark it as used celestar@1217: //and clear the usage bit (0x80) of the stop celestar@1217: *b = (*b | ((v->u.road.state&2)?2:1)) & ~0x80; truelight@0: } truelight@0: } truelight@0: truelight@0: if (!(r & 4)) { truelight@0: v->tile = tile; truelight@0: v->u.road.state = (byte)tmp; truelight@0: v->u.road.frame = 0; truelight@0: } truelight@0: if (newdir != v->direction) { truelight@0: v->direction = newdir; truelight@0: v->cur_speed -= v->cur_speed >> 2; truelight@0: } truelight@0: truelight@0: v->cur_image = GetRoadVehImage(v, newdir); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (rd.x & 0x40) { truelight@0: int dir = RoadFindPathToDest(v, v->tile, rd.x&3); truelight@0: uint32 r; truelight@0: int tmp; truelight@0: byte newdir; truelight@0: const RoadDriveEntry *rdp; truelight@0: truelight@0: if (dir == -1) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: truelight@0: tmp = (_opt.road_side<<4) + dir; truelight@0: rdp = _road_drive_data[tmp]; truelight@0: tron@926: x = TileX(v->tile) * 16 + rdp[1].x; tron@926: y = TileY(v->tile) * 16 + rdp[1].y; truelight@0: truelight@0: if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) truelight@0: return; truelight@0: truelight@0: r = VehicleEnterTile(v, v->tile, x, y); truelight@0: if (r & 8) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: truelight@0: v->u.road.state = tmp & ~16; truelight@0: v->u.road.frame = 1; truelight@0: truelight@0: if (newdir != v->direction) { truelight@0: v->direction = newdir; truelight@0: v->cur_speed -= v->cur_speed >> 2; truelight@0: } truelight@0: truelight@0: v->cur_image = GetRoadVehImage(v, newdir); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); truelight@0: return; truelight@0: } truelight@0: truelight@0: x = (v->x_pos&~15)+(rd.x&15); truelight@0: y = (v->y_pos&~15)+(rd.y&15); truelight@193: truelight@0: new_dir = RoadVehGetSlidingDirection(v, x, y); truelight@0: truelight@0: if (!IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && (u=RoadVehFindCloseTo(v, x, y, new_dir)) != NULL) { truelight@0: if (v->u.road.overtaking == 0) truelight@0: RoadVehCheckOvertake(v, u); truelight@0: return; truelight@0: } truelight@0: truelight@0: old_dir = v->direction; truelight@0: if (new_dir != old_dir) { truelight@0: v->direction = new_dir; truelight@0: v->cur_speed -= (v->cur_speed >> 2); truelight@0: if (old_dir != v->u.road.state) { truelight@0: v->cur_image = GetRoadVehImage(v, new_dir); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: SetRoadVehPosition(v, v->x_pos, v->y_pos); truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: if (v->u.road.state >= 0x20 && truelight@0: _road_veh_data_1[v->u.road.state - 0x20 + (_opt.road_side<<4)] == v->u.road.frame) { celestar@1217: RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile)); celestar@1217: byte *b = &rs->status; truelight@0: tron@2049: st = GetStation(_m[v->tile].m2); truelight@0: tron@555: if (v->current_order.type != OT_LEAVESTATION && tron@555: v->current_order.type != OT_GOTO_DEPOT) { tron@555: Order old_order; truelight@193: truelight@0: *b &= ~0x80; truelight@0: tron@2049: v->last_station_visited = _m[v->tile].m2; truelight@0: truelight@0: RoadVehArrivesAt(v, st); truelight@0: tron@555: old_order = v->current_order; tron@555: v->current_order.type = OT_LOADING; tron@555: v->current_order.flags = 0; truelight@0: tron@555: if (old_order.type == OT_GOTO_STATION && tron@555: v->current_order.station == v->last_station_visited) { tron@555: v->current_order.flags = celestar@1935: (old_order.flags & (OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER)) | OF_NON_STOP; truelight@0: } truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC); truelight@0: if (LoadUnloadVehicle(v)) { truelight@0: InvalidateWindow(WC_ROADVEH_LIST, v->owner); truelight@0: MarkRoadVehDirty(v); truelight@0: } darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: return; truelight@0: } truelight@0: tron@555: if (v->current_order.type != OT_GOTO_DEPOT) { truelight@0: if (*b&0x80) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } tron@555: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; truelight@0: } truelight@0: *b |= 0x80; truelight@0: celestar@1217: if (rs == v->u.road.slot) { celestar@1217: //we have arrived at the correct station celestar@1217: ClearSlot(v, rs); celestar@1217: } else if (v->u.road.slot != NULL) { celestar@1217: //we have arrived at the wrong station celestar@1217: //XXX The question is .. what to do? Actually we shouldn't be here celestar@1217: //but I guess we need to clear the slot celestar@1295: DEBUG(ms, 1) ("Multistop: Wrong station, force a slot clearing. Vehicle %d at 0x%x, should go to 0x%x of station %d (%x), destination 0x%x", v->unitnumber, v->tile, v->u.road.slot->xy, st->index, st->xy, v->dest_tile); celestar@1239: ClearSlot(v, v->u.road.slot); celestar@1217: } celestar@1217: truelight@0: StartRoadVehSound(v); darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: r = VehicleEnterTile(v, v->tile, x, y); truelight@0: if (r & 8) { truelight@0: v->cur_speed = 0; truelight@0: return; truelight@0: } truelight@0: truelight@0: if ((r & 4) == 0) { truelight@0: v->u.road.frame++; truelight@0: } truelight@0: truelight@0: v->cur_image = GetRoadVehImage(v, v->direction); truelight@0: UpdateRoadVehDeltaXY(v); truelight@0: RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); truelight@0: } truelight@0: truelight@0: void RoadVehEnterDepot(Vehicle *v) truelight@0: { truelight@0: v->u.road.state = 254; truelight@0: v->vehstatus |= VS_HIDDEN; truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: bjarni@842: MaybeReplaceVehicle(v); truelight@0: bjarni@578: VehicleServiceInDepot(v); bjarni@578: tron@445: TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); truelight@193: tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { tron@555: Order t; tron@555: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@193: tron@555: t = v->current_order; tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; truelight@0: truelight@1024: // Part of the orderlist? celestar@1530: if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { tron@555: v->cur_order_index++; celestar@1530: } else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { truelight@0: v->vehstatus |= VS_STOPPED; truelight@0: if (v->owner == _local_player) { tron@534: SetDParam(0, v->unitnumber); truelight@0: AddNewsItem( truelight@0: STR_9016_ROAD_VEHICLE_IS_WAITING, truelight@0: NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); bjarni@1151: InvalidateWindowClasses(WC_ROADVEH_LIST); truelight@0: } truelight@0: truelight@0: static void AgeRoadVehCargo(Vehicle *v) truelight@0: { truelight@0: if (_age_cargo_skip_counter != 0) truelight@0: return; truelight@0: if (v->cargo_days != 255) truelight@0: v->cargo_days++; truelight@0: } truelight@0: truelight@0: void RoadVeh_Tick(Vehicle *v) truelight@0: { truelight@0: AgeRoadVehCargo(v); Darkvater@1401: RoadVehController(v); truelight@0: } truelight@0: truelight@0: static void CheckIfRoadVehNeedsService(Vehicle *v) truelight@0: { truelight@1313: Depot *depot; truelight@0: bjarni@1520: if (_patches.servint_roadveh == 0) dominik@11: return; dominik@11: bjarni@1520: if (!VehicleNeedsService(v)) truelight@0: return; truelight@0: truelight@0: if (v->vehstatus & VS_STOPPED) truelight@0: return; truelight@0: truelight@1024: if (_patches.gotodepot && VehicleHasDepotOrders(v)) truelight@0: return; truelight@193: truelight@0: // Don't interfere with a depot visit scheduled by the user, or a truelight@0: // depot visit by the order list. tron@555: if (v->current_order.type == OT_GOTO_DEPOT && celestar@1530: (v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0) truelight@0: return; truelight@0: celestar@1217: //If we already got a slot at a stop, use that FIRST, and go to a depot later celestar@1217: if (v->u.road.slot != NULL) celestar@1217: return; celestar@1217: truelight@1313: depot = FindClosestRoadDepot(v); truelight@0: truelight@1313: if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) { bjarni@1520: if (v->current_order.type == OT_GOTO_DEPOT) { tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: return; truelight@0: } truelight@0: tron@555: if (v->current_order.type == OT_GOTO_DEPOT && tron@555: v->current_order.flags & OF_NON_STOP && tron@555: !CHANCE16(1,20)) truelight@0: return; truelight@0: tron@555: v->current_order.type = OT_GOTO_DEPOT; tron@555: v->current_order.flags = OF_NON_STOP; truelight@1313: v->current_order.station = depot->index; truelight@1313: v->dest_tile = depot->xy; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: celestar@1217: int dist_compare(const void *a, const void *b) celestar@1217: { celestar@1217: return ( *(const uint32 *)a) - ( *(const uint32 *) b); celestar@1217: } celestar@1217: truelight@0: void OnNewDay_RoadVeh(Vehicle *v) truelight@0: { truelight@0: int32 cost; truelight@0: Station *st; truelight@0: truelight@0: if ((++v->day_counter & 7) == 0) truelight@0: DecreaseVehicleValue(v); truelight@0: truelight@0: if (v->u.road.unk2 == 0) truelight@0: CheckVehicleBreakdown(v); truelight@0: truelight@0: AgeVehicle(v); truelight@0: CheckIfRoadVehNeedsService(v); truelight@0: celestar@1053: CheckOrders(v->index, OC_INIT); dominik@19: truelight@0: /* update destination */ celestar@1769: if (v->current_order.type == OT_GOTO_STATION && !(v->vehstatus & VS_CRASHED)) { celestar@1217: RoadStopType type = (v->cargo_type == CT_PASSENGERS) ? RS_BUS : RS_TRUCK; celestar@1217: truelight@919: st = GetStation(v->current_order.station); celestar@1217: celestar@1217: //Current slot has expired ludde@2089: if ( (v->u.road.slot_age++ <= 0) && (v->u.road.slot != NULL)) celestar@1217: ClearSlot(v, v->u.road.slot); celestar@1217: celestar@1217: //We do not have a slot, so make one ludde@2089: if (v->u.road.slot == NULL) { ludde@2089: RoadStop *rs = GetPrimaryRoadStop(st, type); ludde@2089: RoadStop *first_stop = rs; ludde@2089: RoadStop *best_stop = NULL; ludde@2093: uint32 mindist = 12, dist; // 12 is threshold distance. celestar@1217: ludde@2089: //first we need to find out how far our stations are away. ludde@2089: DEBUG(ms, 2) ("Multistop: Attempting to obtain a slot for vehicle %d at station %d (0x%x)", v->unitnumber, st->index, st->xy); ludde@2089: for(; rs != NULL; rs = rs->next) { ludde@2089: // Only consider those with at least a free slot. ludde@2089: if (!(rs->slot[0] == INVALID_SLOT || rs->slot[1] == INVALID_SLOT)) ludde@2089: continue; celestar@1217: ludde@2089: // Previously the NPF pathfinder was used here even if NPF is OFF.. WTF? ludde@2089: assert(NUM_SLOTS == 2); ludde@2089: dist = DistanceManhattan(v->tile, rs->xy); celestar@1217: ludde@2093: // Check if the station is located BEHIND the vehicle.. ludde@2093: // In that case, add penalty. ludde@2093: switch(v->direction) { ludde@2093: case 1: // going north east,x position decreasing Darkvater@2107: if (v->x_pos <= (int32)TileX(rs->xy) * 16 + 15) ludde@2093: dist += 6; ludde@2093: break; ludde@2093: case 3: // Going south east, y position increasing Darkvater@2107: if (v->y_pos >= (int32)TileY(rs->xy) * 16) ludde@2093: dist += 6; ludde@2093: break; ludde@2093: case 5: // Going south west, x position increasing Darkvater@2107: if (v->x_pos >= (int32)TileX(rs->xy) * 16) ludde@2093: dist += 6; ludde@2093: break; ludde@2093: case 7: // Going north west, y position decrasing. Darkvater@2107: if (v->y_pos <= (int32)TileY(rs->xy) * 16 + 15) ludde@2093: dist += 6; ludde@2093: break; ludde@2093: } ludde@2093: ludde@2089: // Remember the one with the shortest distance ludde@2089: if (dist < mindist) { ludde@2089: mindist = dist; ludde@2089: best_stop = rs; celestar@1217: } ludde@2089: DEBUG(ms, 3) ("Multistop: Distance to stop at 0x%x is %d", rs->xy, dist); celestar@1217: } celestar@1217: ludde@2089: // best_stop now contains the best stop we found. ludde@2089: if (best_stop) { ludde@2089: int slot; ludde@2089: // Find a free slot in this stop. We know that at least one is free. ludde@2089: assert(best_stop->slot[0] == INVALID_SLOT || best_stop->slot[1] == INVALID_SLOT); ludde@2089: slot = (best_stop->slot[0] == INVALID_SLOT) ? 0 : 1; ludde@2089: best_stop->slot[slot] = v->index; ludde@2089: v->u.road.slot = best_stop; ludde@2089: v->dest_tile = best_stop->xy; ludde@2093: v->u.road.slot_age = -5; ludde@2089: v->u.road.slotindex = slot; ludde@2093: DEBUG(ms, 1) ("Multistop: Slot %d at 0x%x assigned to vehicle %d (0x%x)", slot, best_stop->xy, v->unitnumber, v->tile); ludde@2089: } else if (first_stop) { ludde@2089: //now we couldn't assign a slot for one reason or another. ludde@2089: //so we just go towards the first station celestar@1295: DEBUG(ms, 1) ("Multistop: No free slot found for vehicle %d, going to default station", v->unitnumber); ludde@2089: v->dest_tile = first_stop->xy; celestar@1295: } celestar@1217: } truelight@0: } truelight@0: truelight@0: if (v->vehstatus & VS_STOPPED) truelight@0: return; truelight@0: tron@538: cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364; truelight@0: truelight@0: v->profit_this_year -= cost >> 8; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN); truelight@0: SubtractMoneyFromPlayerFract(v->owner, cost); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); bjarni@1151: InvalidateWindowClasses(WC_ROADVEH_LIST); truelight@0: } truelight@0: truelight@0: void HandleClickOnRoadVeh(Vehicle *v) truelight@0: { truelight@0: ShowRoadVehViewWindow(v); truelight@0: } truelight@0: tron@1093: void RoadVehiclesYearlyLoop(void) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type == VEH_Road) { truelight@0: v->profit_last_year = v->profit_this_year; truelight@0: v->profit_this_year = 0; truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: } truelight@0: } truelight@0: }