truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@507: #include "table/strings.h" tron@679: #include "map.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 "gfx.h" truelight@0: #include "pathfind.h" truelight@0: #include "player.h" tron@337: #include "sound.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: truelight@0: int GetRoadVehImage(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; truelight@0: img = _engine_original_sprites[v->engine_type]; 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: truelight@0: void DrawRoadVehEngine(int x, int y, int 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: } celestar@378: spritenum = _engine_original_sprites[engine]; celestar@378: } celestar@378: DrawSprite((6 + _roadveh_images[spritenum]) | image_ormod, x, y); truelight@0: } truelight@0: truelight@0: void DrawRoadVehEngineInfo(int engine, int x, int y, int maxw) truelight@0: { tron@538: const RoadVehicleInfo *rvi = RoadVehInfo(engine); truelight@0: tron@538: SetDParam(0, ((_price.roadveh_base >> 3) * rvi->base_cost) >> 5); tron@538: SetDParam(1, rvi->max_speed * 10 >> 5); tron@538: SetDParam(2, rvi->running_cost * _price.roadveh_running >> 8); tron@538: tron@538: SetDParam(4, rvi->capacity); tron@538: SetDParam(3, _cargoc.names_long_p[rvi->cargo_type]); truelight@193: truelight@0: DrawStringMultiCenter(x, y, STR_902A_COST_SPEED_RUNNING_COST, maxw); truelight@0: } truelight@0: truelight@812: int32 EstimateRoadVehCost(byte engine_type) truelight@0: { tron@538: return ((_price.roadveh_base >> 3) * RoadVehInfo(engine_type)->base_cost) >> 5; truelight@0: } truelight@0: bjarni@842: // p1 = engine_type bjarni@842: // p2 not used 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@0: byte unit_num; truelight@0: uint tile = TILE_FROM_XY(x,y); truelight@0: Engine *e; truelight@193: bjarni@1196: 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); truelight@0: if (flags & DC_QUERY_COST) truelight@0: return cost; truelight@0: 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: truelight@817: v->last_station_visited = 0xFFFF; tron@538: v->max_speed = rvi->max_speed; truelight@0: v->engine_type = (byte)p1; truelight@0: truelight@0: e = &_engines[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); truelight@0: } truelight@0: bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window bjarni@1128: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: // p1 = vehicle truelight@0: int32 CmdStartStopRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (v->type != VEH_Road || !CheckOwnership(v->owner)) truelight@0: 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: truelight@919: // p1 = vehicle index in GetVehicle() bjarni@842: // p2 not used truelight@0: int32 CmdSellRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@193: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (v->type != VEH_Road || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!IsRoadDepotTile(v->tile) || 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); 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; truelight@0: uint 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: truelight@0: static bool EnumRoadSignalFindDepot(uint 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) && truelight@0: (_map5[tile] & 0xF0) == 0x20 && truelight@0: _map_owner[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@0: static int FindClosestRoadDepot(Vehicle *v) truelight@0: { truelight@0: uint tile = v->tile; truelight@0: int i; truelight@0: RoadFindDepotData rfdd; truelight@193: truelight@0: if (v->u.road.state == 255) { tile = GetVehicleOutOfTunnelTile(v); } truelight@0: truelight@0: rfdd.owner = v->owner; truelight@0: rfdd.best_length = (uint)-1; truelight@0: truelight@0: /* search in all directions */ truelight@0: for(i=0; i!=4; i++) truelight@159: FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadSignalFindDepot, NULL, &rfdd); truelight@0: truelight@0: if (rfdd.best_length == (uint)-1) truelight@0: return -1; truelight@0: truelight@0: return GetDepotByTile(rfdd.tile); truelight@0: } truelight@0: truelight@0: int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@919: Vehicle *v = GetVehicle(p1); truelight@0: int depot; truelight@0: truelight@0: if (v->type != VEH_Road || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { truelight@0: if (flags & DC_EXEC) { tron@555: if (v->current_order.flags & OF_UNLOAD) truelight@0: v->cur_order_index++; 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: truelight@0: depot = FindClosestRoadDepot(v); truelight@0: if (depot < 0) truelight@0: 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; tron@555: v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; tron@555: v->current_order.station = (byte)depot; truelight@0: v->dest_tile = _depots[depot].xy; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: int32 CmdTurnRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (v->type != VEH_Road || !CheckOwnership(v->owner)) truelight@0: 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: _error_message = STR_EMPTY; 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: truelight@0: int32 CmdChangeRoadVehServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (v->type != VEH_Road || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->service_interval = (uint16)p2; 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]; truelight@0: v->x_offs = (byte)x; truelight@0: v->y_offs = (byte)(x>>=8); truelight@0: v->sprite_width = (byte)(x>>=8); truelight@0: v->sprite_height = (byte)(x>>=8); truelight@0: } truelight@0: truelight@0: static void ClearCrashedStation(Vehicle *v) truelight@0: { truelight@0: uint tile = v->tile; truelight@919: Station *st = GetStation(_map2[tile]); truelight@0: byte *b, bb; truelight@0: truelight@0: b = (_map5[tile] >= 0x47) ? &st->bus_stop_status : &st->truck_stop_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: 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) { truelight@0: CreateEffectVehicleRel(v,4,4,8,EV_CRASHED_SMOKE); 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: truelight@0: AddNewsItem(STR_9031_ROAD_VEHICLE_CRASH_DRIVER+(pass!=1), 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: { truelight@0: uint 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 tron@1035: if (!IsTileType(tile, MP_STREET) || bjarni@1151: (_map5[tile] & 0xF0) != 0x10) truelight@0: 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 && tron@555: (v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && tron@593: !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@817: v->last_station_visited = 0xFFFF; truelight@1024: truelight@1024: st = GetStation(order->station); truelight@1024: v->dest_tile = v->cargo_type == CT_PASSENGERS ? st->bus_tile : st->lorry_tile; truelight@1024: } else if (order->type == OT_GOTO_DEPOT) { truelight@1024: v->dest_tile = _depots[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; truelight@0: u = VehicleFromPos(TILE_FROM_XY(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: { truelight@0: if (v->engine_type < 123) { 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; truelight@0: uint tile; truelight@0: byte tilebits; truelight@0: } OvertakeData; truelight@0: tron@1095: static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od) truelight@0: { truelight@0: if (v->tile != (TileIndex)od->tile || truelight@193: v->type != VEH_Road || truelight@193: v == od->u || truelight@0: v == od->v) truelight@0: 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: truelight@159: tt = (byte)(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: truelight@0: num = ((uint16)Random() * 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: truelight@0: static bool EnumRoadTrackFindDist(uint tile, FindRoadToChooseData *frd, int track, uint length, byte *state) truelight@0: { truelight@0: uint dist = GetTileDist(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 truelight@0: static int RoadFindPathToDest(Vehicle *v, uint tile, int direction) 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; truelight@0: uint 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); truelight@0: signal = (uint16)(r >> 16); truelight@0: bitmask = (uint16)r; truelight@0: } truelight@0: tron@1035: if (IsTileType(tile, MP_STREET)) { darkvater@241: if ((_map5[tile]&0xF0) == 0x20 && v->owner == _map_owner[tile]) darkvater@241: /* Road crossing */ darkvater@241: bitmask |= _road_veh_fp_ax_or[_map5[tile]&3]; tron@1035: } else if (IsTileType(tile, MP_STATION)) { darkvater@241: if (_map_owner[tile] == OWNER_NONE || _map_owner[tile] == v->owner) { darkvater@241: /* Our station */ truelight@919: Station *st = GetStation(_map2[tile]); darkvater@241: byte val = _map5[tile]; darkvater@241: if (v->cargo_type != CT_PASSENGERS) { darkvater@241: if (IS_BYTE_INSIDE(val, 0x43, 0x47) && (_patches.roadveh_queue || st->truck_stop_status&3)) darkvater@241: bitmask |= _road_veh_fp_ax_or[(val-0x43)&3]; darkvater@241: } else { darkvater@241: if (IS_BYTE_INSIDE(val, 0x47, 0x4B) && (_patches.roadveh_queue || st->bus_stop_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 */ truelight@0: bitmask &= _road_veh_fp_ax_and[direction]; truelight@0: if (bitmask == 0) { truelight@0: // reverse truelight@0: return_track(_road_reverse_table[direction]); truelight@0: } truelight@0: truelight@0: if (v->u.road.reverse_ctr != 0) { truelight@0: v->u.road.reverse_ctr = 0; truelight@0: if (v->tile != (TileIndex)tile) { truelight@0: return_track(_road_reverse_table[direction]); truelight@0: } truelight@0: } truelight@0: truelight@0: desttile = v->dest_tile; truelight@0: if (desttile == 0) { truelight@0: // Pick a random direction truelight@0: return_track(PickRandomBit(bitmask)); truelight@0: } truelight@0: truelight@0: // Only one direction to choose between? truelight@0: if (!(bitmask & (bitmask - 1))) { truelight@0: return_track(FindFirstBit2x64(bitmask)); truelight@0: } truelight@0: tron@1035: if (IsTileType(desttile, MP_STREET)) { truelight@0: m5 = _map5[desttile]; truelight@0: if ((m5&0xF0) == 0x20) truelight@0: goto do_it; tron@1035: } else if (IsTileType(desttile, MP_STATION)) { truelight@0: m5 = _map5[desttile]; truelight@0: if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) { truelight@0: m5 -= 0x43; truelight@0: do_it:; tron@900: desttile += TileOffsByDir(m5 & 3); truelight@0: if (desttile == tile && bitmask&_road_pf_table_3[m5&3]) { truelight@0: return_track(FindFirstBit2x64(bitmask&_road_pf_table_3[m5&3])); truelight@0: } truelight@193: } truelight@0: } truelight@0: // do pathfind truelight@0: frd.dest = desttile; truelight@0: truelight@0: best_track = -1; truelight@0: best_dist = (uint)-1; truelight@0: best_maxlen = (uint)-1; truelight@0: i = 0; truelight@0: do { truelight@0: if (bitmask & 1) { truelight@0: if (best_track == -1) best_track = i; // in case we don't find the path, just pick a direction truelight@0: frd.maxtracklen = (uint)-1; truelight@0: frd.mindist = (uint)-1; truelight@159: FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _road_pf_directions[i], (TPFEnumProc*)EnumRoadTrackFindDist, NULL, &frd); truelight@0: truelight@0: if (frd.mindist < best_dist || (frd.mindist==best_dist && frd.maxtracklen < best_maxlen)) { truelight@0: best_dist = frd.mindist; truelight@0: best_maxlen = frd.maxtracklen; truelight@0: best_track = i; truelight@0: } truelight@0: } truelight@0: } while (++i,(bitmask>>=1) != 0); 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: 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: truelight@0: static void RoadVehEventHandler(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: truelight@0: dir = _map5[v->tile]&3; 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) && truelight@0: (_map5[gp.new_tile]&0xF0) == 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@900: uint 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) { 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; } truelight@0: if (IS_BYTE_INSIDE(_map5[v->tile], 0x43, 0x4B)) { truelight@919: Station *st = GetStation(_map2[v->tile]); truelight@0: byte *b; truelight@0: truelight@0: if (_map5[v->tile] >= 0x47) { truelight@0: b = &st->bus_stop_status; truelight@0: } else { truelight@0: b = &st->truck_stop_status; truelight@0: } truelight@0: *b = (*b | ((v->u.road.state&2)?2:1)) & 0x7F; 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) { truelight@0: byte *b; truelight@0: truelight@919: st = GetStation(_map2[v->tile]); truelight@0: b = IS_BYTE_INSIDE(_map5[v->tile], 0x43, 0x47) ? &st->truck_stop_status : &st->bus_stop_status; 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: truelight@0: v->last_station_visited = _map2[v->tile]; 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 = tron@555: (old_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) | 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: 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? tron@555: if (t.flags & OF_UNLOAD) { tron@555: v->cur_order_index++; tron@555: } else if (t.flags & OF_FULL_LOAD) { 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); truelight@0: RoadVehEventHandler(v); truelight@0: } truelight@0: truelight@0: static void CheckIfRoadVehNeedsService(Vehicle *v) truelight@0: { truelight@0: int i; truelight@0: truelight@76: if (_patches.servint_roadveh == 0) dominik@11: return; dominik@11: tron@593: 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 && tron@555: (v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0) truelight@0: return; truelight@0: truelight@0: i = FindClosestRoadDepot(v); truelight@0: truelight@0: if (i < 0 || GetTileDist(v->tile, (&_depots[i])->xy) > 12) { tron@555: 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; tron@555: v->current_order.station = (byte)i; truelight@0: v->dest_tile = (&_depots[i])->xy; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: void OnNewDay_RoadVeh(Vehicle *v) truelight@0: { truelight@0: int32 cost; truelight@0: Station *st; truelight@0: uint tile; 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 */ tron@555: if (v->current_order.type == OT_GOTO_STATION) { truelight@919: st = GetStation(v->current_order.station); truelight@0: if ((tile=(v->cargo_type==CT_PASSENGERS ? st->bus_tile : st->lorry_tile)) != 0) truelight@0: v->dest_tile = tile; 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: } truelight@0: