tron@2186: /* $Id$ */ tron@2186: belugas@6919: /** @file vehicle.cpp */ belugas@6919: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3957: #include "road_map.h" tron@3959: #include "roadveh.h" tron@3961: #include "ship.h" tron@1349: #include "spritecache.h" rubidium@8615: #include "tile_cmd.h" maedhros@6949: #include "landscape.h" maedhros@7476: #include "timetable.h" rubidium@8720: #include "viewport_func.h" rubidium@8720: #include "gfx_func.h" rubidium@9259: #include "news_func.h" rubidium@8612: #include "command_func.h" truelight@0: #include "saveload.h" rubidium@8750: #include "player_func.h" celestar@1601: #include "debug.h" matthijs@1752: #include "vehicle_gui.h" matthijs@1758: #include "depot.h" rubidium@8599: #include "rail_type.h" bjarni@2676: #include "train.h" bjarni@4662: #include "aircraft.h" peter1138@3428: #include "industry_map.h" celestar@3404: #include "station_map.h" tron@3957: #include "water_map.h" rubidium@5720: #include "network/network.h" KUDr@4130: #include "yapf/yapf.h" peter1138@5968: #include "newgrf_callbacks.h" peter1138@4603: #include "newgrf_engine.h" peter1138@4656: #include "newgrf_sound.h" rubidium@7139: #include "group.h" rubidium@9280: #include "order_func.h" rubidium@8610: #include "strings_func.h" rubidium@8619: #include "zoom_func.h" rubidium@8627: #include "functions.h" rubidium@8636: #include "date_func.h" rubidium@8627: #include "window_func.h" rubidium@8640: #include "vehicle_func.h" smatz@8734: #include "signal_func.h" rubidium@8653: #include "sound_func.h" rubidium@8707: #include "variables.h" rubidium@8708: #include "autoreplace_func.h" rubidium@8708: #include "autoreplace_gui.h" rubidium@8710: #include "string_func.h" rubidium@8766: #include "settings_type.h" smatz@9343: #include "oldpool_func.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: rubidium@7400: #define INVALID_COORD (0x7fffffff) tron@4174: #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6)) truelight@0: rubidium@8640: VehicleID _vehicle_id_ctr_day; rubidium@8640: Vehicle *_place_clicked_vehicle; rubidium@8640: VehicleID _new_vehicle_id; rubidium@8640: uint16 _returned_refit_capacity; rubidium@8640: bjarni@6043: bjarni@6043: /* Tables used in vehicle.h to find the right command for a certain vehicle type */ bjarni@6043: const uint32 _veh_build_proc_table[] = { bjarni@2552: CMD_BUILD_RAIL_VEHICLE, bjarni@2552: CMD_BUILD_ROAD_VEH, bjarni@2552: CMD_BUILD_SHIP, bjarni@2552: CMD_BUILD_AIRCRAFT, bjarni@2552: }; bjarni@6043: const uint32 _veh_sell_proc_table[] = { bjarni@2552: CMD_SELL_RAIL_WAGON, bjarni@2552: CMD_SELL_ROAD_VEH, bjarni@2552: CMD_SELL_SHIP, bjarni@2552: CMD_SELL_AIRCRAFT, bjarni@2552: }; tron@2753: bjarni@6043: const uint32 _veh_refit_proc_table[] = { bjarni@2552: CMD_REFIT_RAIL_VEHICLE, peter1138@3990: CMD_REFIT_ROAD_VEH, bjarni@2552: CMD_REFIT_SHIP, bjarni@2552: CMD_REFIT_AIRCRAFT, bjarni@2552: }; bjarni@2552: bjarni@4451: const uint32 _send_to_depot_proc_table[] = { Darkvater@4495: CMD_SEND_TRAIN_TO_DEPOT, bjarni@4451: CMD_SEND_ROADVEH_TO_DEPOT, bjarni@4451: CMD_SEND_SHIP_TO_DEPOT, bjarni@4451: CMD_SEND_AIRCRAFT_TO_HANGAR, bjarni@4451: }; bjarni@4451: bjarni@2552: truelight@1279: /* Initialize the vehicle-pool */ rubidium@7896: DEFINE_OLD_POOL_GENERIC(Vehicle, Vehicle) truelight@1279: bjarni@9125: /** Function to tell if a vehicle needs to be autorenewed bjarni@9125: * @param *p The vehicle owner bjarni@9125: * @return true if the vehicle is old enough for replacement bjarni@9125: */ bjarni@9125: bool Vehicle::NeedsAutorenewing(const Player *p) const bjarni@9125: { bjarni@9125: /* We can always generate the Player pointer when we have the vehicle. bjarni@9125: * However this takes time and since the Player pointer is often present bjarni@9125: * when this function is called then it's faster to pass the pointer as an bjarni@9125: * argument rather than finding it again. */ bjarni@9125: assert(p == GetPlayer(this->owner)); bjarni@9125: bjarni@9125: if (!p->engine_renew) return false; bjarni@9125: if (this->age - this->max_age < (p->engine_renew_months * 30)) return false; bjarni@9125: bjarni@9125: return true; bjarni@9125: } bjarni@9125: bjarni@578: void VehicleServiceInDepot(Vehicle *v) bjarni@578: { bjarni@578: v->date_of_last_service = _date; bjarni@578: v->breakdowns_since_last_service = 0; tron@1926: v->reliability = GetEngine(v->engine_type)->reliability; bjarni@4725: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated bjarni@578: } truelight@0: frosch@10098: bool Vehicle::NeedsServicing() const tron@593: { frosch@10098: if (this->vehstatus & (VS_STOPPED | VS_CRASHED)) return false; matthijs@1757: bjarni@4262: if (_patches.no_servicing_if_no_breakdowns && _opt.diff.vehicle_breakdowns == 0) { frosch@10098: /* Vehicles set for autoreplacing needs to go to a depot even if breakdowns are turned off. frosch@10098: * Note: If servicing is enabled, we postpone replacement till next service. */ frosch@10098: return EngineHasReplacementForPlayer(GetPlayer(this->owner), this->engine_type, this->group_id); bjarni@4262: } bjarni@4262: truelight@812: return _patches.servint_ispercent ? frosch@10098: (this->reliability < GetEngine(this->engine_type)->reliability * (100 - this->service_interval) / 100) : frosch@10098: (this->date_of_last_service + this->service_interval < _date); frosch@10098: } frosch@10098: frosch@10098: bool Vehicle::NeedsAutomaticServicing() const frosch@10098: { frosch@10098: if (_patches.gotodepot && VehicleHasDepotOrders(this)) return false; frosch@10098: if (this->current_order.IsType(OT_LOADING)) return false; frosch@10098: if (this->current_order.IsType(OT_GOTO_DEPOT) && this->current_order.GetDepotActionType() & ODATFB_HALT) return false; frosch@10098: return NeedsServicing(); tron@593: } tron@593: tron@3881: StringID VehicleInTheWayErrMsg(const Vehicle* v) truelight@0: { tron@2631: switch (v->type) { rubidium@6585: case VEH_TRAIN: return STR_8803_TRAIN_IN_THE_WAY; rubidium@6585: case VEH_ROAD: return STR_9000_ROAD_VEHICLE_IN_THE_WAY; rubidium@6585: case VEH_AIRCRAFT: return STR_A015_AIRCRAFT_IN_THE_WAY; tron@3881: default: return STR_980E_SHIP_IN_THE_WAY; tron@2631: } truelight@0: } truelight@0: truelight@0: static void *EnsureNoVehicleProcZ(Vehicle *v, void *data) truelight@0: { smatz@8577: byte z = *(byte*)data; smatz@8577: smatz@8577: if (v->type == VEH_DISASTER || (v->type == VEH_AIRCRAFT && v->subtype == AIR_SHADOW)) return NULL; smatz@8577: if (v->z_pos > z) return NULL; truelight@0: tron@3881: _error_message = VehicleInTheWayErrMsg(v); tron@537: return v; truelight@0: } truelight@0: truelight@1605: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z) truelight@1605: { smatz@8577: return (Vehicle*)VehicleFromPos(tile, &z, &EnsureNoVehicleProcZ); truelight@1605: } truelight@1605: smatz@8574: bool EnsureNoVehicleOnGround(TileIndex tile) smatz@8574: { smatz@8574: return FindVehicleOnTileZ(tile, GetTileMaxZ(tile)) == NULL; smatz@8574: } smatz@8574: rubidium@6191: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z, bool without_crashed) truelight@0: { tron@926: int x1 = TileX(from); tron@926: int y1 = TileY(from); tron@926: int x2 = TileX(to); tron@926: int y2 = TileY(to); truelight@0: Vehicle *veh; truelight@0: truelight@0: /* Make sure x1 < x2 or y1 < y2 */ truelight@0: if (x1 > x2 || y1 > y2) { tron@6432: Swap(x1, x2); tron@6432: Swap(y1, y2); truelight@0: } truelight@919: FOR_ALL_VEHICLES(veh) { rubidium@6191: if (without_crashed && (veh->vehstatus & VS_CRASHED) != 0) continue; rubidium@6987: if ((veh->type == VEH_TRAIN || veh->type == VEH_ROAD) && (z == 0xFF || veh->z_pos == z)) { rubidium@6987: if ((veh->x_pos >> 4) >= x1 && (veh->x_pos >> 4) <= x2 && rubidium@6987: (veh->y_pos >> 4) >= y1 && (veh->y_pos >> 4) <= y2) { truelight@0: return veh; truelight@0: } truelight@0: } truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: tron@2817: smatz@8568: /** Procedure called for every vehicle found in tunnel/bridge in the hash map */ smatz@8568: static void *GetVehicleTunnelBridgeProc(Vehicle *v, void *data) smatz@8568: { smatz@8577: if (v->type != VEH_TRAIN && v->type != VEH_ROAD) return NULL; smatz@8568: smatz@8568: _error_message = VehicleInTheWayErrMsg(v); smatz@8568: return v; smatz@8568: } smatz@8568: smatz@8568: /** smatz@8568: * Finds vehicle in tunnel / bridge smatz@8568: * @param tile first end smatz@8568: * @param endtile second end smatz@8568: * @return pointer to vehicle found smatz@8568: */ smatz@8568: Vehicle *GetVehicleTunnelBridge(TileIndex tile, TileIndex endtile) smatz@8568: { smatz@8577: Vehicle *v = (Vehicle*)VehicleFromPos(tile, NULL, &GetVehicleTunnelBridgeProc); smatz@8568: if (v != NULL) return v; smatz@8568: smatz@8577: return (Vehicle*)VehicleFromPos(endtile, NULL, &GetVehicleTunnelBridgeProc); smatz@8568: } smatz@8568: smatz@8568: tron@2817: static void UpdateVehiclePosHash(Vehicle* v, int x, int y); tron@2817: truelight@0: void VehiclePositionChanged(Vehicle *v) truelight@0: { truelight@0: int img = v->cur_image; truelight@0: Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos); tron@2319: const Sprite* spr = GetSprite(img); truelight@0: tron@2319: pt.x += spr->x_offs; tron@2319: pt.y += spr->y_offs; truelight@0: truelight@0: UpdateVehiclePosHash(v, pt.x, pt.y); truelight@0: truelight@0: v->left_coord = pt.x; truelight@0: v->top_coord = pt.y; tron@2319: v->right_coord = pt.x + spr->width + 2; tron@2319: v->bottom_coord = pt.y + spr->height + 2; truelight@0: } truelight@0: belugas@6919: /** Called after load to update coordinates */ peter1138@8668: void AfterLoadVehicles(bool clear_te_id) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { rubidium@7993: /* Reinstate the previous pointer */ rubidium@7993: if (v->Next() != NULL) v->Next()->previous = v; rubidium@7993: rubidium@7054: v->UpdateDeltaXY(v->direction); rubidium@7054: peter1138@8668: if (clear_te_id) v->fill_percent_te_id = INVALID_TE_ID; celestar@1601: v->first = NULL; rubidium@6585: if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE; maedhros@7353: if (v->type == VEH_ROAD) v->u.road.first_engine = INVALID_ENGINE; rubidium@7506: rubidium@7506: v->cargo.InvalidateCache(); celestar@3355: } celestar@3355: celestar@3355: FOR_ALL_VEHICLES(v) { rubidium@7993: /* Fill the first pointers */ rubidium@7993: if (v->Previous() == NULL) { rubidium@7993: for (Vehicle *u = v; u != NULL; u = u->Next()) { rubidium@7993: u->first = v; rubidium@7993: } rubidium@7993: } rubidium@7993: } rubidium@7993: rubidium@7993: FOR_ALL_VEHICLES(v) { rubidium@7993: assert(v->first != NULL); rubidium@7993: maedhros@7353: if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) { smatz@8959: if (IsFrontEngine(v)) v->u.rail.last_speed = v->cur_speed; // update displayed train speed peter1138@2994: TrainConsistChanged(v); maedhros@7353: } else if (v->type == VEH_ROAD && IsRoadVehFront(v)) { maedhros@7353: RoadVehUpdateCache(v); maedhros@7353: } peter1138@2994: } peter1138@2994: peter1138@2994: FOR_ALL_VEHICLES(v) { truelight@4346: switch (v->type) { rubidium@7179: case VEH_ROAD: skidd13@8424: v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD; rubidium@7179: v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype); rubidium@7630: /* FALL THROUGH */ rubidium@7630: case VEH_TRAIN: rubidium@7630: case VEH_SHIP: rubidium@7630: v->cur_image = v->GetImage(v->direction); rubidium@7179: break; rubidium@7179: rubidium@6585: case VEH_AIRCRAFT: Darkvater@6105: if (IsNormalAircraft(v)) { rubidium@7630: v->cur_image = v->GetImage(v->direction); Darkvater@6117: Darkvater@6117: /* The plane's shadow will have the same image as the plane */ rubidium@7988: Vehicle *shadow = v->Next(); Darkvater@6117: shadow->cur_image = v->cur_image; Darkvater@6117: Darkvater@6117: /* In the case of a helicopter we will update the rotor sprites */ Darkvater@6117: if (v->subtype == AIR_HELICOPTER) { rubidium@7988: Vehicle *rotor = shadow->Next(); Darkvater@6117: rotor->cur_image = GetRotorImage(v); Darkvater@6117: } peter1138@6986: peter1138@6986: UpdateAircraftCache(v); truelight@4346: } truelight@4346: break; truelight@4346: default: break; truelight@0: } truelight@4346: truelight@4346: v->left_coord = INVALID_COORD; truelight@4346: VehiclePositionChanged(v); truelight@0: } truelight@0: } truelight@0: rubidium@7894: Vehicle::Vehicle() truelight@0: { rubidium@7894: this->type = VEH_INVALID; rubidium@7894: this->left_coord = INVALID_COORD; rubidium@7894: this->group_id = DEFAULT_GROUP; rubidium@7894: this->fill_percent_te_id = INVALID_TE_ID; rubidium@7993: this->first = this; glx@8298: this->colormap = PAL_NONE; truelight@0: } truelight@0: peter1138@2804: /** peter1138@2804: * Get a value for a vehicle's random_bits. peter1138@2804: * @return A random value from 0 to 255. peter1138@2804: */ rubidium@6573: byte VehicleRandomBits() peter1138@2804: { peter1138@2804: return GB(Random(), 0, 8); peter1138@2804: } peter1138@2804: rubidium@7894: rubidium@7894: /* static */ bool Vehicle::AllocateList(Vehicle **vl, int num) truelight@0: { rubidium@7894: uint counter = _Vehicle_pool.first_free_index; rubidium@7894: rubidium@7894: for (int i = 0; i != num; i++) { rubidium@7894: Vehicle *v = AllocateRaw(counter); rubidium@7894: rubidium@7894: if (v == NULL) return false; rubidium@7894: v = new (v) InvalidVehicle(); rubidium@7894: bjarni@2606: if (vl != NULL) { bjarni@2606: vl[i] = v; bjarni@2606: } rubidium@7894: counter++; bjarni@2601: } bjarni@2601: bjarni@2606: return true; bjarni@2601: } bjarni@2601: peter1138@7367: /* Size of the hash, 6 = 64 x 64, 7 = 128 x 128. Larger sizes will (in theory) reduce hash peter1138@7367: * lookup times at the expense of memory usage. */ peter1138@7367: const int HASH_BITS = 7; peter1138@7367: const int HASH_SIZE = 1 << HASH_BITS; peter1138@7367: const int HASH_MASK = HASH_SIZE - 1; peter1138@7367: const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2); peter1138@7367: const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1; peter1138@7367: peter1138@7367: /* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc. peter1138@7367: * Profiling results show that 0 is fastest. */ peter1138@7367: const int HASH_RES = 0; peter1138@7367: peter1138@7367: static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE]; peter1138@7367: peter1138@7367: static void *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc) peter1138@7367: { peter1138@7367: for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) { peter1138@7367: for (int x = xl; ; x = (x + 1) & HASH_MASK) { peter1138@7367: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@7367: for (; v != NULL; v = v->next_new_hash) { peter1138@7367: void *a = proc(v, data); peter1138@7367: if (a != NULL) return a; peter1138@7367: } peter1138@7367: if (x == xu) break; peter1138@7367: } peter1138@7367: if (y == yu) break; peter1138@7367: } peter1138@7367: peter1138@7367: return NULL; peter1138@7367: } peter1138@7367: peter1138@7367: peter1138@7367: void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc) peter1138@7367: { peter1138@7367: const int COLL_DIST = 6; peter1138@7367: peter1138@7367: /* Hash area to scan is from xl,yl to xu,yu */ peter1138@7367: int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS); peter1138@7367: int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS); peter1138@7367: int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS; peter1138@7367: int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS; peter1138@7367: peter1138@7367: return VehicleFromHash(xl, yl, xu, yu, data, proc); peter1138@7367: } peter1138@7367: tron@2651: truelight@0: void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc) truelight@0: { peter1138@7367: int x = GB(TileX(tile), HASH_RES, HASH_BITS); peter1138@7367: int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS; peter1138@7367: peter1138@7367: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@7367: for (; v != NULL; v = v->next_new_hash) { peter1138@7367: if (v->tile != tile) continue; peter1138@7367: peter1138@7367: void *a = proc(v, data); peter1138@7367: if (a != NULL) return a; truelight@0: } peter1138@7367: truelight@0: return NULL; truelight@0: } truelight@0: peter1138@7382: static void UpdateNewVehiclePosHash(Vehicle *v, bool remove) peter1138@7367: { peter1138@7367: Vehicle **old_hash = v->old_new_hash; peter1138@7367: Vehicle **new_hash; peter1138@7367: peter1138@7382: if (remove) { peter1138@7367: new_hash = NULL; peter1138@7367: } else { peter1138@7867: int x = GB(TileX(v->tile), HASH_RES, HASH_BITS); peter1138@7867: int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS; peter1138@7367: new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@7367: } peter1138@7367: peter1138@7367: if (old_hash == new_hash) return; peter1138@7367: peter1138@7367: /* Remove from the old position in the hash table */ peter1138@7367: if (old_hash != NULL) { peter1138@7367: Vehicle *last = NULL; peter1138@7367: Vehicle *u = *old_hash; peter1138@7367: while (u != v) { peter1138@7367: last = u; peter1138@7367: u = u->next_new_hash; peter1138@7367: assert(u != NULL); peter1138@7367: } peter1138@7367: peter1138@7367: if (last == NULL) { peter1138@7367: *old_hash = v->next_new_hash; peter1138@7367: } else { peter1138@7367: last->next_new_hash = v->next_new_hash; peter1138@7367: } peter1138@7367: } peter1138@7367: peter1138@7367: /* Insert vehicle at beginning of the new position in the hash table */ peter1138@7367: if (new_hash != NULL) { peter1138@7367: v->next_new_hash = *new_hash; peter1138@7367: *new_hash = v; peter1138@7367: assert(v != v->next_new_hash); peter1138@7367: } peter1138@7367: peter1138@7367: /* Remember current hash position */ peter1138@7367: v->old_new_hash = new_hash; peter1138@7367: } peter1138@7367: peter1138@7367: static Vehicle *_vehicle_position_hash[0x1000]; truelight@0: tron@2817: static void UpdateVehiclePosHash(Vehicle* v, int x, int y) truelight@0: { peter1138@7382: UpdateNewVehiclePosHash(v, x == INVALID_COORD); peter1138@7367: peter1138@5825: Vehicle **old_hash, **new_hash; truelight@0: int old_x = v->left_coord; truelight@0: int old_y = v->top_coord; truelight@0: rubidium@6987: new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)]; truelight@0: old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)]; truelight@193: tron@2639: if (old_hash == new_hash) return; truelight@0: truelight@0: /* remove from hash table? */ truelight@0: if (old_hash != NULL) { truelight@0: Vehicle *last = NULL; peter1138@5825: Vehicle *u = *old_hash; peter1138@5825: while (u != v) { truelight@0: last = u; peter1138@5825: u = u->next_hash; peter1138@5825: assert(u != NULL); truelight@0: } truelight@0: tron@2639: if (last == NULL) { truelight@0: *old_hash = v->next_hash; tron@2639: } else { truelight@0: last->next_hash = v->next_hash; tron@2639: } truelight@0: } truelight@0: truelight@0: /* insert into hash table? */ truelight@0: if (new_hash != NULL) { truelight@0: v->next_hash = *new_hash; peter1138@5825: *new_hash = v; truelight@0: } truelight@0: } truelight@0: rubidium@6573: void ResetVehiclePosHash() Darkvater@5352: { glx@7379: Vehicle *v; glx@7379: FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; } peter1138@5825: memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash)); peter1138@7367: memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash)); Darkvater@5352: } Darkvater@5352: glx@8298: void ResetVehicleColorMap() glx@8298: { glx@8298: Vehicle *v; glx@8298: FOR_ALL_VEHICLES(v) { v->colormap = PAL_NONE; } glx@8298: } glx@8298: rubidium@6573: void InitializeVehicles() truelight@0: { rubidium@7896: _Vehicle_pool.CleanPool(); rubidium@7896: _Vehicle_pool.AddBlockToPool(); Darkvater@5352: Darkvater@5352: ResetVehiclePosHash(); truelight@0: } truelight@0: truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v) truelight@0: { rubidium@7988: while (v->Next() != NULL) v = v->Next(); truelight@0: return v; truelight@0: } truelight@0: tron@2630: uint CountVehiclesInChain(const Vehicle* v) truelight@0: { tron@2639: uint count = 0; rubidium@7988: do count++; while ((v = v->Next()) != NULL); truelight@0: return count; truelight@0: } truelight@0: bjarni@4574: /** Check if a vehicle is counted in num_engines in each player struct bjarni@4574: * @param *v Vehicle to test bjarni@4574: * @return true if the vehicle is counted in num_engines bjarni@4574: */ bjarni@4574: bool IsEngineCountable(const Vehicle *v) bjarni@4574: { bjarni@4574: switch (v->type) { rubidium@6585: case VEH_AIRCRAFT: return IsNormalAircraft(v); // don't count plane shadows and helicopter rotors rubidium@6585: case VEH_TRAIN: bjarni@4574: return !IsArticulatedPart(v) && // tenders and other articulated parts bjarni@8022: !IsRearDualheaded(v); // rear parts of multiheaded engines maedhros@7353: case VEH_ROAD: return IsRoadVehFront(v); maedhros@7353: case VEH_SHIP: return true; bjarni@4574: default: return false; // Only count player buildable vehicles bjarni@4574: } bjarni@4574: } bjarni@4574: rubidium@7908: void Vehicle::PreDestructor() truelight@0: { rubidium@7909: if (CleaningPool()) return; rubidium@7909: rubidium@7894: if (IsValidStationID(this->last_station_visited)) { rubidium@7894: GetStation(this->last_station_visited)->loading_vehicles.remove(this); rubidium@7894: rubidium@7894: HideFillingPercent(this->fill_percent_te_id); rubidium@7894: this->fill_percent_te_id = INVALID_TE_ID; rubidium@6996: } rubidium@6996: rubidium@7894: if (IsEngineCountable(this)) { rubidium@7894: GetPlayer(this->owner)->num_engines[this->engine_type]--; rubidium@7894: if (this->owner == _local_player) InvalidateAutoreplaceWindow(this->engine_type, this->group_id); rubidium@7894: rubidium@7894: if (IsValidGroupID(this->group_id)) GetGroup(this->group_id)->num_engines[this->engine_type]--; rubidium@7894: if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id); bjarni@6195: } bjarni@4574: rubidium@7894: if (this->type == VEH_ROAD) ClearSlot(this); rubidium@7894: rubidium@7894: if (this->type != VEH_TRAIN || (this->type == VEH_TRAIN && (IsFrontEngine(this) || IsFreeWagon(this)))) { rubidium@7894: InvalidateWindowData(WC_VEHICLE_DEPOT, this->tile); bjarni@4739: } bjarni@4739: rubidium@7894: this->cargo.Truncate(0); rubidium@7908: DeleteVehicleOrders(this); truelight@4404: truelight@4404: /* Now remove any artic part. This will trigger an other truelight@4404: * destroy vehicle, which on his turn can remove any truelight@4404: * other artic parts. */ rubidium@7894: if ((this->type == VEH_TRAIN && EngineHasArticPart(this)) || (this->type == VEH_ROAD && RoadVehHasArticPart(this))) { rubidium@7988: delete this->Next(); maedhros@7353: } rubidium@8261: rubidium@8567: Window *w = FindWindowById(WC_MAIN_WINDOW, 0); rubidium@8567: if (WP(w, vp_d).follow_vehicle == this->index) { rubidium@8567: ScrollMainWindowTo(this->x_pos, this->y_pos, true); // lock the main view on the vehicle's last position rubidium@8446: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; rubidium@8261: } rubidium@7908: } rubidium@7908: rubidium@7908: Vehicle::~Vehicle() rubidium@7908: { peter1138@8754: free(this->name); rubidium@7909: rubidium@7909: if (CleaningPool()) return; rubidium@7909: rubidium@7995: this->SetNext(NULL); rubidium@7908: UpdateVehiclePosHash(this, INVALID_COORD, 0); rubidium@7908: this->next_hash = NULL; rubidium@7908: this->next_new_hash = NULL; rubidium@7908: rubidium@7908: DeleteVehicleNews(this->index, INVALID_STRING_ID); rubidium@7894: rubidium@7894: new (this) InvalidVehicle(); rubidium@7894: } rubidium@7894: maedhros@7278: /** maedhros@7278: * Deletes all vehicles in a chain. maedhros@7278: * @param v The first vehicle in the chain. maedhros@7278: */ truelight@0: void DeleteVehicleChain(Vehicle *v) truelight@0: { rubidium@8198: assert(v->First() == v); maedhros@7278: truelight@0: do { truelight@0: Vehicle *u = v; smatz@8813: /* sometimes, eg. for disaster vehicles, when company bankrupts, when removing crashed/flooded vehicles, smatz@8813: * it may happen that vehicle chain is deleted when visible */ smatz@8813: if (!(v->vehstatus & VS_HIDDEN)) MarkSingleVehicleDirty(v); rubidium@7988: v = v->Next(); rubidium@7894: delete u; Darkvater@1765: } while (v != NULL); truelight@0: } truelight@0: belugas@6919: /** head of the linked list to tell what vehicles that visited a depot in a tick */ tron@2630: static Vehicle* _first_veh_in_depot_list; bjarni@2574: bjarni@2574: /** Adds a vehicle to the list of vehicles, that visited a depot this tick rubidium@4549: * @param *v vehicle to add rubidium@4549: */ bjarni@2574: void VehicleEnteredDepotThisTick(Vehicle *v) bjarni@2574: { smatz@9078: /* We need to set v->leave_depot_instantly as we have no control of it's contents at this time. smatz@9078: * Vehicle should stop in the depot if it was in 'stopping' state - train intered depot while slowing down. */ rubidium@10079: if (((v->current_order.GetDepotActionType() & ODATFB_HALT) && !(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) && v->current_order.IsType(OT_GOTO_DEPOT)) || smatz@9078: (v->vehstatus & VS_STOPPED)) { belugas@6919: /* we keep the vehicle in the depot since the user ordered it to stay */ bjarni@2590: v->leave_depot_instantly = false; bjarni@2590: } else { belugas@6919: /* the vehicle do not plan on stopping in the depot, so we stop it to ensure that it will not reserve the path belugas@6919: * out of the depot before we might autoreplace it to a different engine. The new engine would not own the reserved path belugas@6919: * we store that we stopped the vehicle, so autoreplace can start it again */ bjarni@2579: v->vehstatus |= VS_STOPPED; bjarni@2579: v->leave_depot_instantly = true; bjarni@2579: } bjarni@2579: bjarni@2574: if (_first_veh_in_depot_list == NULL) { bjarni@2574: _first_veh_in_depot_list = v; bjarni@2574: } else { bjarni@2574: Vehicle *w = _first_veh_in_depot_list; bjarni@2574: while (w->depot_list != NULL) w = w->depot_list; bjarni@2574: w->depot_list = v; bjarni@2574: } bjarni@2574: } truelight@0: rubidium@6573: void CallVehicleTicks() truelight@0: { rubidium@4434: _first_veh_in_depot_list = NULL; // now we are sure it's initialized at the start of each tick bjarni@2574: rubidium@7112: Station *st; rubidium@7112: FOR_ALL_STATIONS(st) LoadUnloadStation(st); rubidium@7112: rubidium@7112: Vehicle *v; truelight@0: FOR_ALL_VEHICLES(v) { rubidium@7631: v->Tick(); peter1138@4656: peter1138@4656: switch (v->type) { rubidium@7117: default: break; rubidium@7117: rubidium@6585: case VEH_TRAIN: rubidium@6585: case VEH_ROAD: rubidium@6585: case VEH_AIRCRAFT: rubidium@6585: case VEH_SHIP: rubidium@6585: if (v->type == VEH_TRAIN && IsTrainWagon(v)) continue; rubidium@6585: if (v->type == VEH_AIRCRAFT && v->subtype != AIR_HELICOPTER) continue; maedhros@7353: if (v->type == VEH_ROAD && !IsRoadVehFront(v)) continue; peter1138@4656: peter1138@4656: v->motion_counter += (v->direction & 1) ? (v->cur_speed * 3) / 4 : v->cur_speed; peter1138@4656: /* Play a running sound if the motion counter passes 256 (Do we not skip sounds?) */ peter1138@4656: if (GB(v->motion_counter, 0, 8) < v->cur_speed) PlayVehicleSound(v, VSE_RUNNING); peter1138@4656: peter1138@4656: /* Play an alterate running sound every 16 ticks */ peter1138@4656: if (GB(v->tick_counter, 0, 4) == 0) PlayVehicleSound(v, v->cur_speed > 0 ? VSE_RUNNING_16 : VSE_STOPPED_16); peter1138@4656: } bjarni@2574: } bjarni@2574: belugas@6919: /* now we handle all the vehicles that entered a depot this tick */ bjarni@2574: v = _first_veh_in_depot_list; bjarni@2574: while (v != NULL) { bjarni@2574: Vehicle *w = v->depot_list; rubidium@4434: v->depot_list = NULL; // it should always be NULL at the end of each tick bjarni@4676: MaybeReplaceVehicle(v, false, true); bjarni@2574: v = w; truelight@0: } truelight@0: } truelight@0: peter1138@2704: /** Check if a given engine type can be refitted to a given cargo peter1138@2704: * @param engine_type Engine type to check Darkvater@1802: * @param cid_to check refit to this cargo-type Darkvater@1802: * @return true if it is possible, false otherwise Darkvater@1802: */ peter1138@2704: bool CanRefitTo(EngineID engine_type, CargoID cid_to) Darkvater@1802: { skidd13@8424: return HasBit(EngInfo(engine_type)->refit_mask, cid_to); Darkvater@1802: } Darkvater@1802: peter1138@3973: /** Find the first cargo type that an engine can be refitted to. belugas@6980: * @param engine_type Which engine to find cargo for. peter1138@3973: * @return A climate dependent cargo type. CT_INVALID is returned if not refittable. peter1138@3973: */ peter1138@3973: CargoID FindFirstRefittableCargo(EngineID engine_type) peter1138@3973: { peter1138@3973: uint32 refit_mask = EngInfo(engine_type)->refit_mask; peter1138@3973: peter1138@3973: if (refit_mask != 0) { peter1138@6676: for (CargoID cid = 0; cid < NUM_CARGO; cid++) { skidd13@8424: if (HasBit(refit_mask, cid)) return cid; peter1138@3973: } peter1138@3973: } peter1138@3973: peter1138@3973: return CT_INVALID; peter1138@3973: } peter1138@3973: bjarni@4544: /** Learn the price of refitting a certain engine belugas@6980: * @param engine_type Which engine to refit bjarni@4544: * @return Price for refitting bjarni@4544: */ rubidium@7439: CommandCost GetRefitCost(EngineID engine_type) bjarni@4544: { rubidium@8726: Money base_cost; rubidium@8726: ExpensesType expense_type; bjarni@4544: switch (GetEngine(engine_type)->type) { rubidium@8726: case VEH_SHIP: rubidium@8726: base_cost = _price.ship_base; rubidium@8726: expense_type = EXPENSES_SHIP_RUN; rubidium@8726: break; rubidium@8726: rubidium@8726: case VEH_ROAD: rubidium@8726: base_cost = _price.roadveh_base; rubidium@8726: expense_type = EXPENSES_ROADVEH_RUN; rubidium@8726: break; rubidium@8726: rubidium@8726: case VEH_AIRCRAFT: rubidium@8726: base_cost = _price.aircraft_base; rubidium@8726: expense_type = EXPENSES_AIRCRAFT_RUN; rubidium@8726: break; rubidium@8726: rubidium@6585: case VEH_TRAIN: rubidium@8726: base_cost = 2 * ((RailVehInfo(engine_type)->railveh_type == RAILVEH_WAGON) ? rubidium@8726: _price.build_railwagon : _price.build_railvehicle); rubidium@8726: expense_type = EXPENSES_TRAIN_RUN; bjarni@4544: break; rubidium@8726: rubidium@8726: default: NOT_REACHED(); bjarni@4544: } rubidium@8726: return CommandCost(expense_type, (EngInfo(engine_type)->refit_cost * base_cost) >> 10); bjarni@4544: } peter1138@3973: Darkvater@2436: static void DoDrawVehicle(const Vehicle *v) truelight@0: { peter1138@5919: SpriteID image = v->cur_image; peter1138@5919: SpriteID pal; truelight@193: rubidium@7829: if (v->vehstatus & VS_DEFPAL) { peter1138@5919: pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); peter1138@5919: } else { peter1138@5919: pal = PAL_NONE; truelight@193: } truelight@0: peter1138@5919: AddSortableSpriteToDraw(image, pal, v->x_pos + v->x_offs, v->y_pos + v->y_offs, frosch@9289: v->x_extent, v->y_extent, v->z_extent, v->z_pos, (v->vehstatus & VS_SHADOW) != 0); truelight@0: } truelight@0: truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi) truelight@0: { belugas@6919: /* The bounding rectangle */ tron@4174: const int l = dpi->left; tron@4174: const int r = dpi->left + dpi->width; tron@4174: const int t = dpi->top; tron@4174: const int b = dpi->top + dpi->height; tron@4174: belugas@6919: /* The hash area to scan */ rubidium@7401: int xl, xu, yl, yu; rubidium@7401: rubidium@7401: if (dpi->width + 70 < (1 << (7 + 6))) { rubidium@7401: xl = GB(l - 70, 7, 6); rubidium@7401: xu = GB(r, 7, 6); rubidium@7401: } else { rubidium@7401: /* scan whole hash row */ rubidium@7401: xl = 0; rubidium@7401: xu = 0x3F; rubidium@7401: } rubidium@7401: rubidium@7401: if (dpi->height + 70 < (1 << (6 + 6))) { rubidium@7401: yl = GB(t - 70, 6, 6) << 6; rubidium@7401: yu = GB(b, 6, 6) << 6; rubidium@7401: } else { rubidium@7401: /* scan whole column */ rubidium@7401: yl = 0; rubidium@7401: yu = 0x3F << 6; rubidium@7401: } rubidium@7401: rubidium@7401: for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) { rubidium@7401: for (int x = xl;; x = (x + 1) & 0x3F) { rubidium@7401: const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF peter1138@5825: peter1138@5825: while (v != NULL) { truelight@193: if (!(v->vehstatus & VS_HIDDEN) && tron@4174: l <= v->right_coord && tron@4174: t <= v->bottom_coord && tron@4174: r >= v->left_coord && tron@4174: b >= v->top_coord) { truelight@0: DoDrawVehicle(v); truelight@0: } peter1138@5825: v = v->next_hash; truelight@0: } truelight@0: tron@4174: if (x == xu) break; truelight@0: } tron@4174: tron@4174: if (y == yu) break; truelight@0: } truelight@0: } truelight@0: tron@1371: static void ChimneySmokeInit(Vehicle *v) truelight@0: { truelight@0: uint32 r = Random(); tron@2140: v->cur_image = SPR_CHIMNEY_SMOKE_0 + GB(r, 0, 3); tron@2140: v->progress = GB(r, 16, 3); truelight@0: } truelight@0: tron@1371: static void ChimneySmokeTick(Vehicle *v) truelight@0: { tron@1371: if (v->progress > 0) { tron@1371: v->progress--; tron@1371: } else { tron@1371: TileIndex tile; truelight@0: truelight@0: BeginVehicleMove(v); truelight@193: tron@1980: tile = TileVirtXY(v->x_pos, v->y_pos); tron@1035: if (!IsTileType(tile, MP_INDUSTRY)) { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: return; truelight@0: } truelight@0: tron@1371: if (v->cur_image != SPR_CHIMNEY_SMOKE_7) { tron@1371: v->cur_image++; tron@1371: } else { tron@1371: v->cur_image = SPR_CHIMNEY_SMOKE_0; tron@1371: } truelight@0: v->progress = 7; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void SteamSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_STEAM_SMOKE_0; truelight@0: v->progress = 12; truelight@0: } truelight@0: tron@1371: static void SteamSmokeTick(Vehicle *v) truelight@0: { tron@1371: bool moved = false; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: tron@1371: v->progress++; truelight@193: tron@1371: if ((v->progress & 7) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: tron@1371: if ((v->progress & 0xF) == 4) { tron@1371: if (v->cur_image != SPR_STEAM_SMOKE_4) { tron@1371: v->cur_image++; tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: return; truelight@0: } truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if (moved) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void DieselSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_DIESEL_SMOKE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void DieselSmokeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); truelight@0: v->z_pos++; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } else if ((v->progress & 7) == 1) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_DIESEL_SMOKE_5) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void ElectricSparkInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_ELECTRIC_SPARK_0; truelight@0: v->progress = 1; truelight@0: } truelight@0: tron@1371: static void ElectricSparkTick(Vehicle *v) truelight@0: { tron@1371: if (v->progress < 2) { tron@1371: v->progress++; tron@1371: } else { truelight@0: v->progress = 0; truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_ELECTRIC_SPARK_5) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void SmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_SMOKE_0; truelight@0: v->progress = 12; truelight@0: } truelight@0: tron@1371: static void SmokeTick(Vehicle *v) truelight@0: { tron@1371: bool moved = false; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: tron@1371: v->progress++; truelight@193: tron@1371: if ((v->progress & 3) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: tron@1371: if ((v->progress & 0xF) == 4) { tron@1371: if (v->cur_image != SPR_SMOKE_4) { tron@1371: v->cur_image++; tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: return; truelight@0: } truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if (moved) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void ExplosionLargeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_EXPLOSION_LARGE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void ExplosionLargeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_EXPLOSION_LARGE_F) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void BreakdownSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_BREAKDOWN_SMOKE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void BreakdownSmokeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 7) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_BREAKDOWN_SMOKE_3) { tron@1371: v->cur_image++; tron@1371: } else { tron@1371: v->cur_image = SPR_BREAKDOWN_SMOKE_0; tron@1371: } truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: rubidium@7830: v->u.special.animation_state--; rubidium@7830: if (v->u.special.animation_state == 0) { truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: } truelight@0: } truelight@0: tron@1371: static void ExplosionSmallInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_EXPLOSION_SMALL_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void ExplosionSmallTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_EXPLOSION_SMALL_B) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1364: static void BulldozerInit(Vehicle *v) truelight@0: { tron@1364: v->cur_image = SPR_BULLDOZER_NE; truelight@0: v->progress = 0; rubidium@7830: v->u.special.animation_state = 0; rubidium@7830: v->u.special.animation_substate = 0; truelight@0: } truelight@0: rubidium@6574: struct BulldozerMovement { tron@1365: byte direction:2; tron@1364: byte image:2; tron@1364: byte duration:3; rubidium@6574: }; truelight@0: tron@1364: static const BulldozerMovement _bulldozer_movement[] = { tron@1364: { 0, 0, 4 }, tron@1364: { 3, 3, 4 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 1, 1, 3 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 1, 1, 3 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 3, 3, 6 }, tron@1364: { 2, 2, 6 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 }, tron@1364: { 0, 0, 3 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 }, tron@1364: { 0, 0, 3 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 } truelight@0: }; truelight@0: tron@1364: static const struct { tron@1364: int8 x; tron@1364: int8 y; tron@1364: } _inc_by_dir[] = { tron@1364: { -1, 0 }, tron@1364: { 0, 1 }, tron@1364: { 1, 0 }, tron@1364: { 0, -1 } tron@1364: }; truelight@0: tron@1364: static void BulldozerTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 7) == 0) { rubidium@7830: const BulldozerMovement* b = &_bulldozer_movement[v->u.special.animation_state]; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: tron@1364: v->cur_image = SPR_BULLDOZER_NE + b->image; truelight@0: tron@1364: v->x_pos += _inc_by_dir[b->direction].x; tron@1364: v->y_pos += _inc_by_dir[b->direction].y; truelight@0: rubidium@7830: v->u.special.animation_substate++; rubidium@7830: if (v->u.special.animation_substate >= b->duration) { rubidium@7830: v->u.special.animation_substate = 0; rubidium@7830: v->u.special.animation_state++; rubidium@7830: if (v->u.special.animation_state == lengthof(_bulldozer_movement)) { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: return; truelight@0: } truelight@0: } truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void BubbleInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_BUBBLE_GENERATE_0; truelight@0: v->spritenum = 0; truelight@0: v->progress = 0; truelight@0: } truelight@0: rubidium@6574: struct BubbleMovement { tron@1371: int8 x:4; tron@1371: int8 y:4; tron@1371: int8 z:4; tron@1371: byte image:4; rubidium@6574: }; truelight@0: tron@1371: #define MK(x, y, z, i) { x, y, z, i } tron@1371: #define ME(i) { i, 4, 0, 0 } tron@1371: tron@1371: static const BubbleMovement _bubble_float_sw[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: truelight@0: tron@1371: static const BubbleMovement _bubble_float_ne[] = { rubidium@4344: MK( 0, 0, 1, 0), rubidium@4344: MK(-1, 0, 1, 1), rubidium@4344: MK( 0, 0, 1, 0), rubidium@4344: MK(-1, 0, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_float_se[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 1, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 1, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_float_nw[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, -1, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, -1, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_burst[] = { rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 7), rubidium@4344: MK(0, 0, 1, 8), rubidium@4344: MK(0, 0, 1, 9), tron@1371: ME(0) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_absorb[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 2), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), tron@1371: ME(2), rubidium@4344: MK(0, 0, 0, 0xA), rubidium@4344: MK(0, 0, 0, 0xB), rubidium@4344: MK(0, 0, 0, 0xC), rubidium@4344: MK(0, 0, 0, 0xD), rubidium@4344: MK(0, 0, 0, 0xE), tron@1371: ME(0) truelight@0: }; tron@1371: #undef ME truelight@0: #undef MK truelight@0: tron@1371: static const BubbleMovement * const _bubble_movement[] = { tron@1371: _bubble_float_sw, tron@1371: _bubble_float_ne, tron@1371: _bubble_float_se, tron@1371: _bubble_float_nw, tron@1371: _bubble_burst, tron@1371: _bubble_absorb, truelight@0: }; truelight@0: tron@1371: static void BubbleTick(Vehicle *v) truelight@0: { truelight@543: /* truelight@543: * Warning: those effects can NOT use Random(), and have to use truelight@543: * InteractiveRandom(), because somehow someone forgot to save truelight@543: * spritenum to the savegame, and so it will cause desyncs in truelight@543: * multiplayer!! (that is: in ToyLand) truelight@543: */ tron@1371: uint et; tron@1371: const BubbleMovement *b; truelight@0: tron@1371: v->progress++; tron@1371: if ((v->progress & 3) != 0) truelight@0: return; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: if (v->spritenum == 0) { tron@1371: v->cur_image++; tron@1371: if (v->cur_image < SPR_BUBBLE_GENERATE_3) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return; truelight@0: } rubidium@7830: if (v->u.special.animation_substate != 0) { tron@2635: v->spritenum = GB(InteractiveRandom(), 0, 2) + 1; truelight@0: } else { truelight@0: v->spritenum = 6; truelight@0: } truelight@0: et = 0; tron@1371: } else { tron@1371: et = v->engine_type + 1; truelight@0: } truelight@0: tron@1371: b = &_bubble_movement[v->spritenum - 1][et]; truelight@0: tron@1371: if (b->y == 4 && b->x == 0) { truelight@0: EndVehicleMove(v); rubidium@7894: delete v; truelight@0: return; truelight@193: } truelight@193: tron@1371: if (b->y == 4 && b->x == 1) { skidd13@8463: if (v->z_pos > 180 || Chance16I(1, 96, InteractiveRandom())) { truelight@0: v->spritenum = 5; tron@541: SndPlayVehicleFx(SND_2F_POP, v); truelight@0: } truelight@0: et = 0; truelight@193: } truelight@193: tron@1371: if (b->y == 4 && b->x == 2) { tron@1371: TileIndex tile; truelight@0: truelight@0: et++; tron@541: SndPlayVehicleFx(SND_31_EXTRACT, v); truelight@0: tron@1980: tile = TileVirtXY(v->x_pos, v->y_pos); belugas@7962: if (IsTileType(tile, MP_INDUSTRY) && GetIndustryGfx(tile) == GFX_BUBBLE_CATCHER) AddAnimatedTile(tile); truelight@0: } truelight@0: tron@1371: v->engine_type = et; tron@1371: b = &_bubble_movement[v->spritenum - 1][et]; tron@1371: tron@1371: v->x_pos += b->x; tron@1371: v->y_pos += b->y; tron@1371: v->z_pos += b->z; tron@1371: v->cur_image = SPR_BUBBLE_0 + b->image; truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: truelight@0: truelight@0: typedef void EffectInitProc(Vehicle *v); truelight@0: typedef void EffectTickProc(Vehicle *v); truelight@0: truelight@0: static EffectInitProc * const _effect_init_procs[] = { tron@1371: ChimneySmokeInit, tron@1371: SteamSmokeInit, tron@1371: DieselSmokeInit, tron@1371: ElectricSparkInit, tron@1371: SmokeInit, tron@1371: ExplosionLargeInit, tron@1371: BreakdownSmokeInit, tron@1371: ExplosionSmallInit, tron@1364: BulldozerInit, tron@1371: BubbleInit, truelight@0: }; truelight@0: truelight@0: static EffectTickProc * const _effect_tick_procs[] = { tron@1371: ChimneySmokeTick, tron@1371: SteamSmokeTick, tron@1371: DieselSmokeTick, tron@1371: ElectricSparkTick, tron@1371: SmokeTick, tron@1371: ExplosionLargeTick, tron@1371: BreakdownSmokeTick, tron@1371: ExplosionSmallTick, tron@1364: BulldozerTick, tron@1371: BubbleTick, truelight@0: }; truelight@0: truelight@0: tron@1359: Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type) truelight@0: { truelight@0: Vehicle *v; truelight@193: rubidium@7894: v = new SpecialVehicle(); truelight@0: if (v != NULL) { truelight@0: v->subtype = type; truelight@0: v->x_pos = x; truelight@0: v->y_pos = y; truelight@0: v->z_pos = z; truelight@0: v->tile = 0; rubidium@7054: v->UpdateDeltaXY(INVALID_DIR); truelight@0: v->vehstatus = VS_UNCLICKABLE; truelight@0: truelight@0: _effect_init_procs[type](v); truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: return v; truelight@0: } truelight@0: tron@1359: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type) truelight@0: { skidd13@8418: int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE); skidd13@8418: int safe_y = Clamp(y, 0, MapMaxY() * TILE_SIZE); rubidium@5295: return CreateEffectVehicle(x, y, GetSlopeZ(safe_x, safe_y) + z, type); truelight@0: } truelight@0: tron@1359: Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type) truelight@0: { truelight@0: return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type); truelight@0: } truelight@0: rubidium@7631: void SpecialVehicle::Tick() truelight@0: { rubidium@7631: _effect_tick_procs[this->subtype](this); truelight@0: } truelight@0: tron@2116: Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y) truelight@0: { truelight@0: Vehicle *found = NULL, *v; truelight@0: uint dist, best_dist = (uint)-1; truelight@0: truelight@0: if ( (uint)(x -= vp->left) >= (uint)vp->width || truelight@0: (uint)(y -= vp->top) >= (uint)vp->height) truelight@0: return NULL; truelight@0: truelight@7122: x = ScaleByZoom(x, vp->zoom) + vp->virtual_left; truelight@7122: y = ScaleByZoom(y, vp->zoom) + vp->virtual_top; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@4346: if ((v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 && truelight@0: x >= v->left_coord && x <= v->right_coord && truelight@0: y >= v->top_coord && y <= v->bottom_coord) { truelight@193: truelight@0: dist = max( skidd13@8419: abs( ((v->left_coord + v->right_coord)>>1) - x ), skidd13@8419: abs( ((v->top_coord + v->bottom_coord)>>1) - y ) truelight@0: ); truelight@0: truelight@0: if (dist < best_dist) { truelight@0: found = v; truelight@0: best_dist = dist; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return found; truelight@0: } truelight@0: glx@8299: void CheckVehicle32Day(Vehicle *v) glx@8299: { glx@8299: if ((v->day_counter & 0x1F) != 0) return; glx@8299: glx@8299: uint16 callback = GetVehicleCallback(CBID_VEHICLE_32DAY_CALLBACK, 0, 0, v->engine_type, v); glx@8299: if (callback == CALLBACK_FAILED) return; skidd13@8424: if (HasBit(callback, 0)) TriggerVehicle(v, VEHICLE_TRIGGER_CALLBACK_32); // Trigger vehicle trigger 10 skidd13@8424: if (HasBit(callback, 1)) v->colormap = PAL_NONE; // Update colormap via callback 2D glx@8299: } truelight@0: truelight@0: void DecreaseVehicleValue(Vehicle *v) truelight@0: { truelight@0: v->value -= v->value >> 8; truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: } truelight@0: truelight@0: static const byte _breakdown_chance[64] = { rubidium@4344: 3, 3, 3, 3, 3, 3, 3, 3, rubidium@4344: 4, 4, 5, 5, 6, 6, 7, 7, rubidium@4344: 8, 8, 9, 9, 10, 10, 11, 11, rubidium@4344: 12, 13, 13, 13, 13, 14, 15, 16, rubidium@4344: 17, 19, 21, 25, 28, 31, 34, 37, rubidium@4344: 40, 44, 48, 52, 56, 60, 64, 68, rubidium@4344: 72, 80, 90, 100, 110, 120, 130, 140, truelight@0: 150, 170, 190, 210, 230, 250, 250, 250, truelight@0: }; truelight@0: truelight@0: void CheckVehicleBreakdown(Vehicle *v) truelight@0: { truelight@0: int rel, rel_old; truelight@0: uint32 r; truelight@0: int chance; truelight@0: truelight@0: /* decrease reliability */ truelight@0: v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0); truelight@0: if ((rel_old >> 8) != (rel >> 8)) truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: tron@2639: if (v->breakdown_ctr != 0 || v->vehstatus & VS_STOPPED || tron@2639: v->cur_speed < 5 || _game_mode == GM_MENU) { tron@2639: return; tron@2639: } truelight@0: truelight@0: r = Random(); truelight@0: truelight@0: /* increase chance of failure */ truelight@0: chance = v->breakdown_chance + 1; skidd13@8463: if (Chance16I(1,25,r)) chance += 25; truelight@0: v->breakdown_chance = min(255, chance); truelight@0: truelight@0: /* calculate reliability value to use in comparison */ truelight@0: rel = v->reliability; rubidium@6585: if (v->type == VEH_SHIP) rel += 0x6666; truelight@193: truelight@0: /* disabled breakdowns? */ tron@2639: if (_opt.diff.vehicle_breakdowns < 1) return; truelight@0: truelight@0: /* reduced breakdowns? */ truelight@0: if (_opt.diff.vehicle_breakdowns == 1) rel += 0x6666; truelight@0: truelight@0: /* check if to break down */ truelight@0: if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) { tron@2140: v->breakdown_ctr = GB(r, 16, 6) + 0x3F; tron@2140: v->breakdown_delay = GB(r, 24, 7) + 0x80; truelight@0: v->breakdown_chance = 0; truelight@0: } truelight@0: } truelight@0: truelight@0: static const StringID _vehicle_type_names[4] = { truelight@0: STR_019F_TRAIN, truelight@0: STR_019C_ROAD_VEHICLE, truelight@0: STR_019E_SHIP, truelight@0: STR_019D_AIRCRAFT, truelight@0: }; truelight@0: truelight@0: static void ShowVehicleGettingOld(Vehicle *v, StringID msg) truelight@0: { tron@2639: if (v->owner != _local_player) return; truelight@812: rubidium@8040: /* Do not show getting-old message if autorenew is active (and it can replace the vehicle) */ rubidium@8040: if (GetPlayer(v->owner)->engine_renew && GetEngine(v->engine_type)->player_avail != 0) return; truelight@0: bjarni@6206: SetDParam(0, _vehicle_type_names[v->type]); tron@534: SetDParam(1, v->unitnumber); rubidium@9259: AddNewsItem(msg, NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE, v->index, 0); truelight@0: } truelight@0: truelight@0: void AgeVehicle(Vehicle *v) truelight@0: { rubidium@8040: if (v->age < 65535) v->age++; rubidium@8040: rubidium@8040: int age = v->age - v->max_age; rubidium@8040: if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4) v->reliability_spd_dec <<= 1; truelight@193: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: truelight@0: if (age == -366) { truelight@0: ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD); truelight@0: } else if (age == 0) { truelight@0: ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD); rubidium@8041: } else if (age > 0 && (age % 366) == 0) { truelight@0: ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND); truelight@0: } truelight@0: } truelight@0: bjarni@4640: /** Starts or stops a lot of vehicles bjarni@4673: * @param tile Tile of the depot where the vehicles are started/stopped (only used for depots) belugas@6919: * @param flags type of operation bjarni@4762: * @param p1 Station/Order/Depot ID (only used for vehicle list windows) bjarni@4673: * @param p2 bitmask bjarni@4762: * - bit 0-4 Vehicle type bjarni@4762: * - bit 5 false = start vehicles, true = stop vehicles bjarni@4762: * - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case bjarni@4673: * - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set) bjarni@4640: */ rubidium@7439: CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4640: { bjarni@4640: Vehicle **vl = NULL; bjarni@4640: uint16 engine_list_length = 0; bjarni@4640: uint16 engine_count = 0; rubidium@7439: CommandCost return_value = CMD_ERROR; bjarni@4640: uint i; bjarni@4640: uint stop_command; rubidium@7137: VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5); skidd13@8424: bool start_stop = HasBit(p2, 5); skidd13@8424: bool vehicle_list_window = HasBit(p2, 6); bjarni@4640: bjarni@4640: switch (vehicle_type) { rubidium@6585: case VEH_TRAIN: stop_command = CMD_START_STOP_TRAIN; break; rubidium@6585: case VEH_ROAD: stop_command = CMD_START_STOP_ROADVEH; break; rubidium@6585: case VEH_SHIP: stop_command = CMD_START_STOP_SHIP; break; rubidium@6585: case VEH_AIRCRAFT: stop_command = CMD_START_STOP_AIRCRAFT; break; bjarni@4640: default: return CMD_ERROR; bjarni@4640: } bjarni@4640: bjarni@4673: if (vehicle_list_window) { bjarni@5999: uint32 id = p1; bjarni@4673: uint16 window_type = p2 & VLW_MASK; bjarni@4673: bjarni@5998: engine_count = GenerateVehicleSortList((const Vehicle***)&vl, &engine_list_length, vehicle_type, _current_player, id, window_type); bjarni@4673: } else { bjarni@4673: /* Get the list of vehicles in the depot */ bjarni@4673: BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL); bjarni@4673: } bjarni@4640: bjarni@4640: for (i = 0; i < engine_count; i++) { bjarni@4640: const Vehicle *v = vl[i]; rubidium@7439: CommandCost ret; bjarni@4640: bjarni@4640: if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue; bjarni@4673: bjarni@4673: if (!vehicle_list_window) { rubidium@6585: if (vehicle_type == VEH_TRAIN) { bjarni@4673: if (CheckTrainInDepot(v, false) == -1) continue; bjarni@4673: } else { bjarni@4673: if (!(v->vehstatus & VS_HIDDEN)) continue; bjarni@4673: } bjarni@4648: } bjarni@4648: bjarni@4640: ret = DoCommand(tile, v->index, 0, flags, stop_command); bjarni@4640: rubidium@7442: if (CmdSucceeded(ret)) { rubidium@7446: return_value = CommandCost(); bjarni@4640: /* We know that the command is valid for at least one vehicle. bjarni@4640: * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */ bjarni@4640: if (!(flags & DC_EXEC)) break; bjarni@4640: } bjarni@4640: } bjarni@4640: bjarni@4648: free(vl); bjarni@4640: return return_value; bjarni@4640: } bjarni@4640: bjarni@4659: /** Sells all vehicles in a depot belugas@6919: * @param tile Tile of the depot where the depot is belugas@6919: * @param flags type of operation belugas@6919: * @param p1 Vehicle type belugas@6919: * @param p2 unused belugas@6919: */ rubidium@7439: CommandCost CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4659: { bjarni@4659: Vehicle **engines = NULL; bjarni@4659: Vehicle **wagons = NULL; bjarni@4659: uint16 engine_list_length = 0; bjarni@4659: uint16 engine_count = 0; bjarni@4659: uint16 wagon_list_length = 0; bjarni@4659: uint16 wagon_count = 0; bjarni@4659: smatz@8741: CommandCost cost(EXPENSES_NEW_VEHICLES); bjarni@4659: uint i, sell_command, total_number_vehicles; rubidium@7137: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8); bjarni@4659: bjarni@4659: switch (vehicle_type) { rubidium@6585: case VEH_TRAIN: sell_command = CMD_SELL_RAIL_WAGON; break; rubidium@6585: case VEH_ROAD: sell_command = CMD_SELL_ROAD_VEH; break; rubidium@6585: case VEH_SHIP: sell_command = CMD_SELL_SHIP; break; rubidium@6585: case VEH_AIRCRAFT: sell_command = CMD_SELL_AIRCRAFT; break; bjarni@4659: default: return CMD_ERROR; bjarni@4659: } bjarni@4659: bjarni@4659: /* Get the list of vehicles in the depot */ bjarni@4659: BuildDepotVehicleList(vehicle_type, tile, &engines, &engine_list_length, &engine_count, bjarni@4659: &wagons, &wagon_list_length, &wagon_count); bjarni@4659: bjarni@4659: total_number_vehicles = engine_count + wagon_count; bjarni@4659: for (i = 0; i < total_number_vehicles; i++) { bjarni@4659: const Vehicle *v; rubidium@7439: CommandCost ret; bjarni@4659: bjarni@4659: if (i < engine_count) { bjarni@4659: v = engines[i]; bjarni@4659: } else { bjarni@4659: v = wagons[i - engine_count]; bjarni@4659: } bjarni@4659: bjarni@4659: ret = DoCommand(tile, v->index, 1, flags, sell_command); bjarni@4659: rubidium@7446: if (CmdSucceeded(ret)) cost.AddCost(ret); bjarni@4659: } bjarni@4659: bjarni@4659: free(engines); bjarni@4659: free(wagons); rubidium@7446: if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell bjarni@4659: return cost; bjarni@4659: } bjarni@4659: bjarni@4662: /** Autoreplace all vehicles in the depot bjarni@9232: * Note: this command can make incorrect cost estimations bjarni@9232: * Luckily the final price can only drop, not increase. This is due to the fact that bjarni@9232: * estimation can't predict wagon removal so it presumes worst case which is no income from selling wagons. belugas@6919: * @param tile Tile of the depot where the vehicles are belugas@6919: * @param flags type of operation belugas@6919: * @param p1 Type of vehicle bjarni@9232: * @param p2 If bit 0 is set, then either replace all or nothing (instead of replacing until money runs out) belugas@6919: */ rubidium@7439: CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4662: { bjarni@4662: Vehicle **vl = NULL; bjarni@4662: uint16 engine_list_length = 0; bjarni@4662: uint16 engine_count = 0; bjarni@9232: uint i; bjarni@9232: CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES); rubidium@7137: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8); bjarni@9232: bool all_or_nothing = HasBit(p2, 0); bjarni@4662: rubidium@8008: if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR; bjarni@4662: bjarni@4662: /* Get the list of vehicles in the depot */ bjarni@9232: BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, &vl, &engine_list_length, &engine_count); bjarni@4662: bjarni@4662: bjarni@4662: for (i = 0; i < engine_count; i++) { bjarni@4662: Vehicle *v = vl[i]; bjarni@4662: bool stopped = !(v->vehstatus & VS_STOPPED); rubidium@7439: CommandCost ret; bjarni@4662: bjarni@4662: /* Ensure that the vehicle completely in the depot */ rubidium@7986: if (!v->IsInDepot()) continue; bjarni@4662: bjarni@4676: if (stopped) { bjarni@4676: v->vehstatus |= VS_STOPPED; // Stop the vehicle bjarni@4676: v->leave_depot_instantly = true; bjarni@4676: } bjarni@4676: ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false); bjarni@4662: rubidium@7442: if (CmdSucceeded(ret)) { rubidium@7446: cost.AddCost(ret); bjarni@9232: } else { bjarni@9232: if (all_or_nothing) { bjarni@9232: /* We failed to replace a vehicle even though we set all or nothing. bjarni@9232: * We should never reach this if DC_EXEC is set since then it should bjarni@9232: * have failed the estimation guess. */ bjarni@9232: assert(!(flags & DC_EXEC)); bjarni@9232: /* Now we will have to return an error. bjarni@9232: * This goto will leave the loop and it's ok to do so because bjarni@9232: * there is no point in the rest of the loop. */ bjarni@9232: goto error; bjarni@9232: } bjarni@4662: } bjarni@4662: } bjarni@4662: rubidium@7446: if (cost.GetCost() == 0) { bjarni@9232: error: bjarni@9232: /* Either we didn't replace anything or something went wrong. bjarni@9232: * Either way we want to return an error and not execute this command. */ bjarni@4662: cost = CMD_ERROR; bjarni@4662: } bjarni@4662: bjarni@4662: free(vl); bjarni@4662: return cost; bjarni@4662: } bjarni@4662: bjarni@2244: /** Clone a vehicle. If it is a train, it will clone all the cars too rubidium@4549: * @param tile tile of the depot where the cloned vehicle is build belugas@6919: * @param flags type of operation rubidium@4549: * @param p1 the original vehicle's index rubidium@4549: * @param p2 1 = shared orders, else copied orders rubidium@4549: */ rubidium@7439: CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@2244: { bjarni@2563: Vehicle *v_front, *v; bjarni@2563: Vehicle *w_front, *w, *w_rear; rubidium@8726: CommandCost cost, total_cost(EXPENSES_NEW_VEHICLES); bjarni@3816: uint32 build_argument = 2; bjarni@2244: truelight@4352: if (!IsValidVehicleID(p1)) return CMD_ERROR; bjarni@2244: v = GetVehicle(p1); bjarni@2563: v_front = v; bjarni@2563: w = NULL; bjarni@2563: w_front = NULL; bjarni@2563: w_rear = NULL; bjarni@2563: maedhros@6940: bjarni@2563: /* bjarni@2563: * v_front is the front engine in the original vehicle maedhros@6940: * v is the car/vehicle of the original vehicle, that is currently being copied bjarni@2563: * w_front is the front engine of the cloned vehicle bjarni@2563: * w is the car/vehicle currently being cloned bjarni@2563: * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains bjarni@2563: */ bjarni@2244: tron@2639: if (!CheckOwnership(v->owner)) return CMD_ERROR; bjarni@2244: rubidium@6585: if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || v->u.rail.crash_anim_pos >= 4400)) return CMD_ERROR; bjarni@2244: belugas@6919: /* check that we can allocate enough vehicles */ bjarni@2601: if (!(flags & DC_EXEC)) { bjarni@2601: int veh_counter = 0; bjarni@2601: do { bjarni@2601: veh_counter++; rubidium@7988: } while ((v = v->Next()) != NULL); bjarni@2601: rubidium@7894: if (!Vehicle::AllocateList(NULL, veh_counter)) { bjarni@2606: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); bjarni@2601: } bjarni@2601: } bjarni@2601: bjarni@2601: v = v_front; bjarni@2601: bjarni@2563: do { bjarni@8022: if (v->type == VEH_TRAIN && IsRearDualheaded(v)) { bjarni@2676: /* we build the rear ends of multiheaded trains with the front ones */ bjarni@2676: continue; bjarni@2676: } bjarni@2676: bjarni@6043: cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v)); bjarni@3819: build_argument = 3; // ensure that we only assign a number to the first engine bjarni@2563: bjarni@2563: if (CmdFailed(cost)) return cost; bjarni@2563: rubidium@7446: total_cost.AddCost(cost); bjarni@2563: bjarni@2563: if (flags & DC_EXEC) { tron@2639: w = GetVehicle(_new_vehicle_id); bjarni@2563: skidd13@8424: if (v->type == VEH_TRAIN && HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) { skidd13@8427: SetBit(w->u.rail.flags, VRF_REVERSE_DIRECTION); bjarni@3896: } bjarni@2563: rubidium@6585: if (v->type == VEH_TRAIN && !IsFrontEngine(v)) { belugas@6919: /* this s a train car belugas@6919: * add this unit to the end of the train */ rubidium@7439: CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE); bjarni@7334: if (CmdFailed(result)) { bjarni@7334: /* The train can't be joined to make the same consist as the original. bjarni@7334: * Sell what we already made (clean up) and return an error. */ bjarni@7334: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front)); bjarni@7334: DoCommand(w_front->tile, w->index, 1, flags, GetCmdSellVeh(w)); bjarni@7334: return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE bjarni@7334: } bjarni@2563: } else { rubidium@8346: /* this is a front engine or not a train. */ bjarni@2563: w_front = w; bjarni@3679: w->service_interval = v->service_interval; bjarni@2563: } rubidium@4434: w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop bjarni@2563: } rubidium@6585: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL); rubidium@6585: rubidium@6585: if (flags & DC_EXEC && v_front->type == VEH_TRAIN) { belugas@6919: /* for trains this needs to be the front engine due to the callback function */ tron@3948: _new_vehicle_id = w_front->index; bjarni@2244: } peter1138@5062: rubidium@7139: if (flags & DC_EXEC) { rubidium@7139: /* Cloned vehicles belong to the same group */ rubidium@7139: DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP); rubidium@7139: } rubidium@7139: rubidium@7139: maedhros@7042: /* Take care of refitting. */ maedhros@7042: w = w_front; maedhros@7042: v = v_front; maedhros@7042: maedhros@7042: /* Both building and refitting are influenced by newgrf callbacks, which maedhros@7042: * makes it impossible to accurately estimate the cloning costs. In maedhros@7042: * particular, it is possible for engines of the same type to be built with maedhros@7042: * different numbers of articulated parts, so when refitting we have to maedhros@7042: * loop over real vehicles first, and then the articulated parts of those maedhros@7042: * vehicles in a different loop. */ maedhros@7042: do { maedhros@7042: do { maedhros@7042: if (flags & DC_EXEC) { maedhros@7042: assert(w != NULL); maedhros@7042: rubidium@8308: if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_subtype) { maedhros@7042: cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v)); rubidium@7446: if (CmdSucceeded(cost)) total_cost.AddCost(cost); maedhros@7042: } maedhros@7042: maedhros@7042: if (w->type == VEH_TRAIN && EngineHasArticPart(w)) { maedhros@7042: w = GetNextArticPart(w); maedhros@7353: } else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) { rubidium@7988: w = w->Next(); maedhros@7042: } else { maedhros@7042: break; maedhros@7042: } maedhros@7042: } else { maedhros@7042: CargoID initial_cargo = GetEngineCargoType(v->engine_type); maedhros@7042: maedhros@7042: if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) { rubidium@7446: total_cost.AddCost(GetRefitCost(v->engine_type)); maedhros@7042: } maedhros@7042: } maedhros@7353: maedhros@7353: if (v->type == VEH_TRAIN && EngineHasArticPart(v)) { maedhros@7353: v = GetNextArticPart(v); maedhros@7353: } else if (v->type == VEH_ROAD && RoadVehHasArticPart(v)) { rubidium@7988: v = v->Next(); maedhros@7353: } else { maedhros@7353: break; maedhros@7353: } maedhros@7353: } while (v != NULL); maedhros@7042: maedhros@7223: if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle(w); maedhros@7042: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL); maedhros@7042: rubidium@8346: if (flags & DC_EXEC) { rubidium@8346: /* rubidium@8346: * Set the orders of the vehicle. Cannot do it earlier as we need rubidium@8346: * the vehicle refitted before doing this, otherwise the moved rubidium@8346: * cargo types might not match (passenger vs non-passenger) rubidium@8346: */ rubidium@8346: DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER); rubidium@8346: } rubidium@8346: maedhros@7042: /* Since we can't estimate the cost of cloning a vehicle accurately we must maedhros@7042: * check whether the player has enough money manually. */ maedhros@7042: if (!CheckPlayerHasMoney(total_cost)) { maedhros@7042: if (flags & DC_EXEC) { maedhros@7042: /* The vehicle has already been bought, so now it must be sold again. */ maedhros@7042: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front)); maedhros@7042: } maedhros@7042: return CMD_ERROR; maedhros@7042: } maedhros@7042: bjarni@2244: return total_cost; bjarni@2244: } bjarni@2244: bjarni@2552: bjarni@4635: /* Extend the list size for BuildDepotVehicleList() */ bjarni@4678: static inline void ExtendVehicleListSize(const Vehicle ***engine_list, uint16 *engine_list_length, uint16 step_size) bjarni@4635: { matthijs@5247: *engine_list_length = min(*engine_list_length + step_size, GetMaxVehicleIndex() + 1); KUDr@5860: *engine_list = ReallocT(*engine_list, *engine_list_length); bjarni@4635: } bjarni@4635: bjarni@4635: /** Generates a list of vehicles inside a depot bjarni@4635: * Will enlarge allocated space for the list if they are too small, so it's ok to call with (pointer to NULL array, pointer to uninitised uint16, pointer to 0) bjarni@4635: * If one of the lists is not needed (say wagons when finding ships), all the pointers regarding that list should be set to NULL belugas@6980: * @param type Type of vehicle bjarni@4635: * @param tile The tile the depot is located in bjarni@4635: * @param ***engine_list Pointer to a pointer to an array of vehicles in the depot (old list is freed and a new one is malloced) bjarni@4635: * @param *engine_list_length Allocated size of engine_list. Needs to be set to 0 when engine_list points to a NULL array bjarni@4635: * @param *engine_count The number of engines stored in the list bjarni@4635: * @param ***wagon_list Pointer to a pointer to an array of free wagons in the depot (old list is freed and a new one is malloced) bjarni@4635: * @param *wagon_list_length Allocated size of wagon_list. Needs to be set to 0 when wagon_list points to a NULL array bjarni@4635: * @param *wagon_count The number of engines stored in the list bjarni@4635: */ rubidium@7137: void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count) bjarni@4635: { bjarni@4635: Vehicle *v; bjarni@4635: bjarni@4635: /* This function should never be called without an array to store results */ rubidium@6585: assert(!(engine_list == NULL && type != VEH_TRAIN)); rubidium@6585: assert(!(type == VEH_TRAIN && engine_list == NULL && wagon_list == NULL)); bjarni@4635: bjarni@4635: /* Both array and the length should either be NULL to disable the list or both should not be NULL */ bjarni@4635: assert((engine_list == NULL && engine_list_length == NULL) || (engine_list != NULL && engine_list_length != NULL)); bjarni@4635: assert((wagon_list == NULL && wagon_list_length == NULL) || (wagon_list != NULL && wagon_list_length != NULL)); bjarni@4635: bjarni@4635: assert(!(engine_list != NULL && engine_count == NULL)); bjarni@4635: assert(!(wagon_list != NULL && wagon_count == NULL)); bjarni@4635: bjarni@4635: if (engine_count != NULL) *engine_count = 0; bjarni@4635: if (wagon_count != NULL) *wagon_count = 0; bjarni@4635: bjarni@4635: switch (type) { rubidium@6585: case VEH_TRAIN: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@6585: if (v->tile == tile && v->type == VEH_TRAIN && v->u.rail.track == TRACK_BIT_DEPOT) { bjarni@4635: if (IsFrontEngine(v)) { bjarni@4635: if (engine_list == NULL) continue; bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } else if (IsFreeWagon(v)) { bjarni@4635: if (wagon_list == NULL) continue; bjarni@4678: if (*wagon_count == *wagon_list_length) ExtendVehicleListSize((const Vehicle***)wagon_list, wagon_list_length, 25); bjarni@4635: (*wagon_list)[(*wagon_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6585: case VEH_ROAD: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@7986: if (v->tile == tile && v->type == VEH_ROAD && v->IsInDepot() && IsRoadVehFront(v)) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6585: case VEH_SHIP: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@7986: if (v->tile == tile && v->type == VEH_SHIP && v->IsInDepot()) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6585: case VEH_AIRCRAFT: bjarni@4635: FOR_ALL_VEHICLES(v) { bjarni@4635: if (v->tile == tile && rubidium@6585: v->type == VEH_AIRCRAFT && IsNormalAircraft(v) && rubidium@7986: v->IsInDepot()) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: bjarni@4635: default: NOT_REACHED(); bjarni@4635: } bjarni@4635: } bjarni@4635: bjarni@4497: /** bjarni@4678: * @param sort_list list to store the list in. Either NULL or the length length_of_array tells bjarni@4678: * @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL bjarni@4554: * @param type type of vehicle bjarni@4554: * @param owner PlayerID of owner to generate a list for belugas@6980: * @param index This parameter has different meanings depending on window_type belugas@6980: