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 "gfx.h" truelight@0: #include "viewport.h" truelight@0: #include "news.h" truelight@0: #include "command.h" truelight@0: #include "saveload.h" truelight@0: #include "player.h" truelight@0: #include "engine.h" tron@337: #include "sound.h" truelight@0: truelight@0: #define INVALID_COORD (-0x8000) truelight@0: #define GEN_HASH(x,y) (((x & 0x1F80)>>7) + ((y & 0xFC0))) truelight@0: truelight@919: enum { truelight@919: VEHICLES_MIN_FREE_FOR_AI = 90 truelight@919: }; truelight@919: bjarni@578: void VehicleServiceInDepot(Vehicle *v) bjarni@578: { bjarni@578: v->date_of_last_service = _date; bjarni@578: v->breakdowns_since_last_service = 0; bjarni@578: v->reliability = _engines[v->engine_type].reliability; bjarni@578: } truelight@0: tron@593: bool VehicleNeedsService(const Vehicle *v) tron@593: { truelight@812: return _patches.servint_ispercent ? truelight@812: (v->reliability < _engines[v->engine_type].reliability * (100 - v->service_interval) / 100) : tron@593: (v->date_of_last_service + v->service_interval < _date); tron@593: } tron@593: tron@577: Order UnpackOldOrder(uint16 packed) tron@577: { tron@577: Order order; tron@577: order.type = (packed & 0x000f); tron@584: order.flags = (packed & 0x00f0) >> 4; tron@577: order.station = (packed & 0xff00) >> 8; tron@577: tron@577: // Sanity check tron@577: // TTD stores invalid orders as OT_NOTHING with non-zero flags/station tron@577: if (order.type == OT_NOTHING && (order.flags != 0 || order.station != 0)) { tron@577: order.type = OT_DUMMY; tron@577: order.flags = 0; tron@577: } tron@577: tron@577: return order; tron@577: } tron@577: tron@577: truelight@0: void VehicleInTheWayErrMsg(Vehicle *v) truelight@0: { truelight@0: StringID id; truelight@0: truelight@0: (id = STR_8803_TRAIN_IN_THE_WAY,v->type == VEH_Train) || truelight@0: (id = STR_9000_ROAD_VEHICLE_IN_THE_WAY,v->type == VEH_Road) || truelight@0: (id = STR_A015_AIRCRAFT_IN_THE_WAY,v->type == VEH_Aircraft) || truelight@0: (id = STR_980E_SHIP_IN_THE_WAY, true); truelight@0: truelight@0: _error_message = id; truelight@0: } truelight@0: truelight@0: static void *EnsureNoVehicleProc(Vehicle *v, void *data) truelight@0: { tron@537: if (v->tile != *(const TileIndex*)data || v->type == VEH_Disaster) truelight@0: return NULL; truelight@193: truelight@193: VehicleInTheWayErrMsg(v); tron@537: return v; truelight@193: } truelight@0: truelight@0: bool EnsureNoVehicle(TileIndex tile) truelight@0: { tron@537: return VehicleFromPos(tile, &tile, EnsureNoVehicleProc) == NULL; truelight@0: } truelight@0: truelight@0: static void *EnsureNoVehicleProcZ(Vehicle *v, void *data) truelight@0: { tron@537: const TileInfo *ti = data; tron@537: tron@537: if (v->tile != ti->tile || v->z_pos != ti->z || v->type == VEH_Disaster) truelight@0: return NULL; truelight@0: truelight@0: VehicleInTheWayErrMsg(v); tron@537: return v; truelight@0: } truelight@0: truelight@0: bool EnsureNoVehicleZ(TileIndex tile, byte z) truelight@0: { truelight@0: TileInfo ti; tron@537: truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: // needs z correction for slope-type graphics that have the NORTHERN tile lowered truelight@0: // 1, 2, 3, 4, 5, 6 and 7 truelight@0: if (CORRECT_Z(ti.tileh)) truelight@0: z += 8; truelight@0: tron@537: ti.z = z; tron@537: tron@537: return VehicleFromPos(tile, &ti, EnsureNoVehicleProcZ) == NULL; truelight@0: } truelight@0: truelight@0: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z) 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) { truelight@0: intswap(x1,x2); truelight@0: intswap(y1,y2); truelight@0: } truelight@919: FOR_ALL_VEHICLES(veh) { truelight@0: if ((veh->type == VEH_Train || veh->type == VEH_Road) && (z==0xFF || veh->z_pos == z)) { truelight@0: if ((veh->x_pos>>4) >= x1 && (veh->x_pos>>4) <= x2 && truelight@0: (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: truelight@0: void VehiclePositionChanged(Vehicle *v) truelight@0: { truelight@0: int img = v->cur_image; truelight@0: const SpriteDimension *sd; truelight@0: Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos); truelight@0: truelight@0: sd = GetSpriteDimension(img); truelight@0: truelight@0: pt.x += sd->xoffs; truelight@0: pt.y += sd->yoffs; 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; truelight@0: v->right_coord = pt.x + sd->xsize + 2; truelight@0: v->bottom_coord = pt.y + sd->ysize + 2; truelight@0: } truelight@0: darkvater@395: void UpdateWaypointSign(Waypoint *cp) truelight@0: { tron@926: Point pt = RemapCoords2(TileX(cp->xy) * 16, TileY(cp->xy) * 16); tron@534: SetDParam(0, cp - _waypoints); darkvater@395: UpdateViewportSignPos(&cp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT); truelight@0: } truelight@0: darkvater@395: void RedrawWaypointSign(Waypoint *cp) truelight@0: { truelight@0: MarkAllViewportsDirty( truelight@0: cp->sign.left - 6, truelight@0: cp->sign.top, truelight@0: cp->sign.left + (cp->sign.width_1 << 2) + 12, truelight@0: cp->sign.top + 48); truelight@0: } truelight@0: truelight@0: // Called after load to update coordinates truelight@0: void AfterLoadVehicles() truelight@0: { truelight@0: Vehicle *v; darkvater@395: Waypoint *cp; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != 0) { truelight@0: v->left_coord = INVALID_COORD; truelight@0: VehiclePositionChanged(v); truelight@0: truelight@0: if (v->type == VEH_Train) { truelight@0: if (v->subtype == 0) truelight@0: UpdateTrainAcceleration(v); truelight@0: } truelight@0: truelight@0: #if defined(_DEBUG) truelight@0: if (!(v->schedule_ptr == NULL || (v->schedule_ptr >= _order_array && v->schedule_ptr < _ptr_to_next_order))) truelight@0: v->schedule_ptr = NULL; truelight@0: #endif truelight@0: } truelight@0: } truelight@0: darkvater@395: // update waypoint signs darkvater@395: for(cp=_waypoints; cp != endof(_waypoints); cp++) if (cp->xy) UpdateWaypointSign(cp); truelight@0: } truelight@0: truelight@0: truelight@0: static Vehicle *InitializeVehicle(Vehicle *v) truelight@0: { truelight@0: VehicleID index = v->index; truelight@0: memset(v, 0, sizeof(Vehicle)); truelight@0: v->index = index; truelight@0: truelight@0: assert(v->schedule_ptr == NULL); truelight@0: truelight@0: v->left_coord = INVALID_COORD; truelight@0: v->next = NULL; truelight@0: v->next_hash = 0xffff; truelight@0: v->string_id = 0; truelight@548: /* random_bits is used to pick out a random sprite for vehicles truelight@548: which are technical the same (newgrf stuff). truelight@548: Because RandomRange() results in desyncs, and because it does miham@826: not really matter that one client has other visual vehicles than truelight@548: the other, it can be InteractiveRandomRange() without any problem truelight@548: */ truelight@547: v->random_bits = InteractiveRandomRange(256); truelight@0: return v; truelight@0: } truelight@0: truelight@0: Vehicle *ForceAllocateSpecialVehicle() truelight@0: { truelight@0: Vehicle *v; truelight@919: FOR_ALL_VEHICLES_FROM(v, NUM_NORMAL_VEHICLES) { truelight@0: if (v->type == 0) truelight@0: return InitializeVehicle(v); truelight@0: } truelight@0: return NULL; truelight@0: truelight@0: } truelight@0: truelight@0: Vehicle *ForceAllocateVehicle() truelight@0: { truelight@0: Vehicle *v; truelight@919: FOR_ALL_VEHICLES(v) { truelight@919: if (v->index >= NUM_NORMAL_VEHICLES) truelight@919: return NULL; truelight@919: truelight@0: if (v->type == 0) truelight@0: return InitializeVehicle(v); truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: Vehicle *AllocateVehicle() truelight@0: { truelight@0: Vehicle *v; truelight@0: int num; truelight@0: truelight@0: if (IS_HUMAN_PLAYER(_current_player)) { truelight@0: num = 0; truelight@0: truelight@919: FOR_ALL_VEHICLES(v) { truelight@919: if (v->index >= NUM_NORMAL_VEHICLES) truelight@919: break; truelight@919: truelight@0: if (v->type == 0) truelight@0: num++; truelight@0: } truelight@0: truelight@919: if (num <= VEHICLES_MIN_FREE_FOR_AI) truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: return ForceAllocateVehicle(); truelight@0: } truelight@0: truelight@0: void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc) truelight@0: { truelight@0: int x,y,x2,y2; truelight@0: VehicleID veh; tron@926: Point pt = RemapCoords(TileX(tile) * 16, TileY(tile) * 16, 0); truelight@0: truelight@0: x2 = ((pt.x + 104) & 0x1F80) >> 7; truelight@0: x = ((pt.x - 174) & 0x1F80) >> 7; truelight@0: truelight@0: y2 = ((pt.y + 56) & 0xFC0); truelight@0: y = ((pt.y - 294) & 0xFC0); truelight@0: truelight@0: for(;;) { truelight@0: int xb = x; truelight@0: for(;;) { truelight@0: veh = _vehicle_position_hash[ (x+y)&0xFFFF ]; truelight@0: while (veh != INVALID_VEHICLE) { truelight@919: Vehicle *v = GetVehicle(veh); truelight@0: void *a; truelight@193: truelight@0: if ((a = proc(v, data)) != NULL) truelight@0: return a; truelight@0: veh = v->next_hash; truelight@0: } truelight@0: truelight@0: if (x == x2) truelight@0: break; truelight@0: truelight@0: x = (x + 1) & 0x3F; truelight@0: } truelight@0: x = xb; truelight@0: truelight@0: if (y == y2) truelight@0: break; truelight@0: truelight@0: y = (y + 0x40) & ((0x3F) << 6); truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: truelight@0: truelight@0: void UpdateVehiclePosHash(Vehicle *v, int x, int y) truelight@0: { truelight@0: VehicleID *old_hash, *new_hash; truelight@0: int old_x = v->left_coord; truelight@0: int old_y = v->top_coord; truelight@0: Vehicle *u; truelight@0: truelight@0: 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: truelight@0: if (old_hash == new_hash) truelight@0: return; truelight@0: truelight@0: /* remove from hash table? */ truelight@0: if (old_hash != NULL) { truelight@0: Vehicle *last = NULL; truelight@0: int idx = *old_hash; truelight@919: while ((u = GetVehicle(idx)) != v) { truelight@0: idx = u->next_hash; truelight@0: assert(idx != INVALID_VEHICLE); truelight@0: last = u; truelight@0: } truelight@0: truelight@0: if (last == NULL) truelight@0: *old_hash = v->next_hash; truelight@0: else truelight@0: last->next_hash = v->next_hash; truelight@0: } truelight@0: truelight@0: /* insert into hash table? */ truelight@0: if (new_hash != NULL) { truelight@0: v->next_hash = *new_hash; truelight@0: *new_hash = v->index; truelight@0: } truelight@0: } truelight@0: truelight@0: void InitializeVehicles() truelight@0: { truelight@0: Vehicle *v; truelight@0: int i; truelight@0: truelight@0: // clear it... truelight@919: memset(&_vehicles, 0, sizeof(_vehicles[0]) * _vehicles_size); darkvater@395: memset(&_waypoints, 0, sizeof(_waypoints)); truelight@0: memset(&_depots, 0, sizeof(_depots)); truelight@193: truelight@0: // setup indexes.. truelight@0: i = 0; truelight@0: FOR_ALL_VEHICLES(v) truelight@0: v->index = i++; truelight@0: truelight@0: memset(_vehicle_position_hash, -1, sizeof(_vehicle_position_hash)); truelight@0: truelight@0: _ptr_to_next_order = _order_array; truelight@0: } truelight@0: truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v) truelight@0: { truelight@0: while (v->next != NULL) v = v->next; truelight@0: return v; truelight@0: } truelight@0: truelight@0: Vehicle *GetPrevVehicleInChain(Vehicle *v) truelight@0: { truelight@0: Vehicle *org = v; truelight@0: truelight@919: FOR_ALL_VEHICLES(v) { truelight@919: if (v->type == VEH_Train && org == v->next) truelight@919: return v; truelight@0: } truelight@0: truelight@919: return NULL; truelight@0: } truelight@0: truelight@0: Vehicle *GetFirstVehicleInChain(Vehicle *v) truelight@0: { truelight@919: Vehicle *u; truelight@919: truelight@919: while (true) { truelight@919: u = v; truelight@919: v = GetPrevVehicleInChain(v); truelight@919: /* If there is no such vehicle, truelight@919: 'v' == NULL and so 'u' is the first vehicle in chain */ truelight@919: if (v == NULL) truelight@919: return u; truelight@193: } truelight@0: } truelight@0: truelight@0: int CountVehiclesInChain(Vehicle *v) truelight@0: { truelight@0: int count = 0; truelight@0: do count++; while ( (v=v->next) != NULL); truelight@0: return count; truelight@0: } truelight@0: truelight@0: truelight@0: Depot *AllocateDepot() truelight@0: { truelight@0: Depot *dep, *free_dep = NULL; truelight@0: int num_free = 0; truelight@0: truelight@0: for(dep = _depots; dep != endof(_depots); dep++) { truelight@0: if (dep->xy == 0) { truelight@0: num_free++; truelight@0: if (free_dep==NULL) truelight@0: free_dep = dep; truelight@0: } truelight@0: } truelight@0: truelight@193: if (free_dep == NULL || truelight@0: (num_free < 30 && IS_HUMAN_PLAYER(_current_player))) { truelight@0: _error_message = STR_1009_TOO_MANY_DEPOTS; truelight@0: return NULL; truelight@0: } truelight@0: truelight@193: return free_dep; truelight@0: } truelight@0: darkvater@395: Waypoint *AllocateWaypoint() truelight@0: { darkvater@395: Waypoint *cp; truelight@0: darkvater@395: for(cp = _waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy == 0) truelight@0: return cp; truelight@0: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: darkvater@395: uint GetWaypointByTile(uint tile) truelight@0: { darkvater@395: Waypoint *cp; truelight@0: int i=0; darkvater@395: for(cp=_waypoints; cp->xy != (TileIndex)tile; cp++) { i++; } truelight@0: return i; truelight@0: } truelight@0: truelight@0: truelight@0: Vehicle *IsScheduleShared(Vehicle *u) truelight@0: { tron@555: const Order *sched = u->schedule_ptr; truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->schedule_ptr == sched && u != v && v->type != 0) truelight@0: return v; truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: void DeleteVehicleSchedule(Vehicle *v) truelight@0: { tron@555: Order *sched; tron@555: Order *cur; truelight@0: int num; truelight@0: Vehicle *u; truelight@0: truelight@0: // if the schedule is shared, don't delete it. truelight@0: if ((u = IsScheduleShared(v)) != NULL) { truelight@0: v->schedule_ptr = NULL; truelight@0: InvalidateWindow(WC_VEHICLE_ORDERS, u->index); truelight@0: return; truelight@0: } truelight@0: truelight@0: sched = v->schedule_ptr; truelight@0: v->schedule_ptr = NULL; truelight@0: truelight@0: num = v->num_orders + 1; truelight@0: truelight@0: _ptr_to_next_order -= num; truelight@0: truelight@0: cur = sched; truelight@0: while (cur != _ptr_to_next_order) { truelight@0: assert(cur < _ptr_to_next_order); truelight@0: cur[0] = cur[num]; truelight@0: cur++; truelight@0: } truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->schedule_ptr != NULL && sched < v->schedule_ptr) { truelight@0: v->schedule_ptr -= num; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@555: void DeleteCommandFromVehicleSchedule(Order cmd) truelight@0: { truelight@0: Vehicle *v; truelight@0: bool need_invalidate; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != 0 && v->schedule_ptr != NULL) { tron@555: Order *sched; truelight@193: truelight@0: // clear last station visited tron@555: if (v->last_station_visited == cmd.station && cmd.type == OT_GOTO_STATION) truelight@817: v->last_station_visited = 0xFFFF; truelight@0: truelight@0: // check the next order tron@555: if (v->current_order.type == cmd.type && tron@555: v->current_order.station == cmd.station) { tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: } truelight@0: truelight@0: // clear the order list truelight@0: need_invalidate = false; tron@555: for (sched = v->schedule_ptr; sched->type != OT_NOTHING; ++sched) { tron@555: if (sched->type == cmd.type && sched->station == cmd.station) { tron@555: sched->type = OT_DUMMY; tron@555: sched->flags = 0; truelight@0: need_invalidate = true; truelight@0: } truelight@0: } truelight@0: truelight@0: if (need_invalidate) truelight@0: InvalidateWindow(WC_VEHICLE_ORDERS, v->index); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void DoDeleteDepot(uint tile) truelight@0: { tron@555: Order order; truelight@0: byte dep_index; truelight@0: Depot *d; truelight@0: truelight@0: // Clear it truelight@0: DoClearSquare(tile); truelight@0: truelight@0: // Nullify the depot struct truelight@0: for(d=_depots,dep_index=0; d->xy != (TileIndex)tile; d++) {dep_index++;} truelight@0: d->xy = 0; truelight@0: tron@555: order.type = OT_GOTO_DEPOT; tron@555: order.flags = 0; tron@555: order.station = dep_index; tron@555: DeleteCommandFromVehicleSchedule(order); truelight@0: truelight@0: // Delete the depot truelight@0: DeleteWindowById(WC_VEHICLE_DEPOT, tile); truelight@0: } truelight@0: truelight@0: void DeleteVehicle(Vehicle *v) truelight@0: { truelight@0: DeleteName(v->string_id); truelight@0: v->type = 0; truelight@0: UpdateVehiclePosHash(v, INVALID_COORD, 0); truelight@0: v->next_hash = 0xffff; truelight@0: truelight@0: if (v->schedule_ptr != NULL) truelight@0: DeleteVehicleSchedule(v); truelight@0: } truelight@0: truelight@0: void DeleteVehicleChain(Vehicle *v) truelight@0: { truelight@0: do { truelight@0: Vehicle *u = v; truelight@0: v = v->next; truelight@0: DeleteVehicle(u); truelight@0: } while (v); truelight@0: } truelight@0: truelight@0: truelight@0: void Aircraft_Tick(Vehicle *v); truelight@0: void RoadVeh_Tick(Vehicle *v); truelight@0: void Ship_Tick(Vehicle *v); truelight@0: void Train_Tick(Vehicle *v); tron@410: static void EffectVehicle_Tick(Vehicle *v); truelight@0: void DisasterVehicle_Tick(Vehicle *v); truelight@0: truelight@0: VehicleTickProc *_vehicle_tick_procs[] = { truelight@0: Train_Tick, truelight@0: RoadVeh_Tick, truelight@0: Ship_Tick, truelight@0: Aircraft_Tick, truelight@0: EffectVehicle_Tick, truelight@0: DisasterVehicle_Tick, truelight@0: }; truelight@0: truelight@0: void CallVehicleTicks() truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != 0) truelight@0: _vehicle_tick_procs[v->type - 0x10](v); truelight@0: } truelight@0: } truelight@0: truelight@0: static bool CanFillVehicle_FullLoadAny(Vehicle *v) truelight@0: { truelight@0: uint32 full = 0, not_full = 0; truelight@193: celestar@924: //special handling of aircraft celestar@924: celestar@924: //if the aircraft carries passengers and is NOT full, then celestar@924: //continue loading, no matter how much mail is in celestar@924: if ((v->type == VEH_Aircraft) && (v->cargo_type == CT_PASSENGERS) && (v->cargo_cap != v->cargo_count)) { celestar@924: return true; celestar@924: } celestar@924: truelight@0: // patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded. truelight@0: do { truelight@0: //Should never happen, but just in case future additions change this truelight@0: assert(v->cargo_type<32); truelight@0: truelight@0: if (v->cargo_cap != 0) { truelight@0: uint32 mask = 1 << v->cargo_type; truelight@0: if (v->cargo_cap == v->cargo_count) full |= mask; else not_full |= mask; truelight@0: } truelight@0: } while ( (v=v->next) != NULL); truelight@0: truelight@0: // continue loading if there is a non full cargo type and no cargo type that is full truelight@0: return not_full && (full & ~not_full) == 0; truelight@0: } truelight@0: truelight@0: bool CanFillVehicle(Vehicle *v) truelight@0: { truelight@0: byte *t = &_map_type_and_height[v->tile]; truelight@0: truelight@0: if (t[0] >> 4 == MP_STATION || truelight@193: (v->type == VEH_Ship && truelight@0: (t[TILE_XY(1,0)] >> 4 == MP_STATION || truelight@0: t[TILE_XY(-1,0)] >> 4 == MP_STATION || truelight@0: t[TILE_XY(0,1)] >> 4 == MP_STATION || truelight@0: t[TILE_XY(0,-1)] >> 4 == MP_STATION || truelight@0: t[TILE_XY(-2,0)] >> 4 == MP_STATION))) { truelight@0: truelight@0: // If patch is active, use alternative CanFillVehicle-function truelight@0: if (_patches.full_load_any) truelight@0: return CanFillVehicle_FullLoadAny(v); truelight@0: truelight@0: do { truelight@0: if (v->cargo_count != v->cargo_cap) truelight@0: return true; truelight@0: } while ( (v=v->next) != NULL); truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@0: static void DoDrawVehicle(Vehicle *v) truelight@0: { truelight@0: uint32 image = v->cur_image; truelight@193: truelight@0: if (v->vehstatus & VS_DISASTER) { truelight@0: image |= 0x3224000; truelight@0: } else if (v->vehstatus & VS_DEFPAL) { truelight@0: image |= (v->vehstatus & VS_CRASHED) ? 0x3248000 : SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner)); truelight@193: } truelight@0: truelight@193: AddSortableSpriteToDraw(image, v->x_pos + v->x_offs, v->y_pos + v->y_offs, truelight@0: v->sprite_width, v->sprite_height, v->z_height, v->z_pos); truelight@0: } truelight@0: truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi) truelight@0: { truelight@0: int x,xb, y, x2, y2; truelight@0: VehicleID veh; truelight@0: Vehicle *v; truelight@0: truelight@0: x = ((dpi->left - 70) & 0x1F80) >> 7; truelight@0: x2 = ((dpi->left + dpi->width) & 0x1F80) >> 7; truelight@0: truelight@0: y = ((dpi->top - 70) & 0xFC0); truelight@0: y2 = ((dpi->top + dpi->height) & 0xFC0); truelight@0: truelight@0: for(;;) { truelight@0: xb = x; truelight@0: for(;;) { truelight@0: veh = _vehicle_position_hash[ (x+y)&0xFFFF ]; truelight@0: while (veh != INVALID_VEHICLE) { truelight@919: v = GetVehicle(veh); truelight@193: truelight@193: if (!(v->vehstatus & VS_HIDDEN) && truelight@0: dpi->left <= v->right_coord && truelight@0: dpi->top <= v->bottom_coord && truelight@0: dpi->left + dpi->width >= v->left_coord && truelight@193: dpi->top + dpi->height >= v->top_coord) { truelight@0: DoDrawVehicle(v); truelight@0: } truelight@0: veh = v->next_hash; truelight@0: } truelight@0: truelight@0: if (x == x2) truelight@0: break; truelight@0: x = (x + 1) & 0x3F; truelight@0: } truelight@0: x = xb; truelight@0: truelight@0: if (y == y2) truelight@0: break; truelight@0: y = (y + 0x40) & ((0x3F) << 6); truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_0(Vehicle *v) truelight@0: { truelight@0: uint32 r = Random(); truelight@0: v->cur_image = (uint16)((r & 7) + 3701); truelight@0: v->progress = (byte)((r >> 16)&7); truelight@0: } truelight@0: truelight@0: static void EffectTick_0(Vehicle *v) truelight@0: { truelight@0: uint tile; truelight@0: uint img; truelight@0: truelight@0: if (--v->progress & 0x80) { truelight@0: BeginVehicleMove(v); truelight@193: truelight@0: tile = TILE_FROM_XY(v->x_pos, v->y_pos); truelight@0: if (!IS_TILETYPE(tile, MP_INDUSTRY)) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: return; truelight@0: } truelight@0: truelight@0: img = v->cur_image + 1; truelight@0: if (img > 3708) img = 3701; truelight@0: v->cur_image = img; truelight@0: v->progress = 7; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_1(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3079; truelight@0: v->progress = 12; truelight@0: } truelight@0: truelight@0: static void EffectTick_1(Vehicle *v) truelight@0: { truelight@0: bool moved; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: truelight@0: moved = false; truelight@193: truelight@0: if ((++v->progress & 7) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if ((v->progress & 0xF)==4) { truelight@0: if (++v->cur_image > 3083) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(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: truelight@0: static void EffectInit_2(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3073; truelight@0: v->progress = 0; truelight@0: } truelight@0: truelight@0: static void EffectTick_2(Vehicle *v) truelight@0: { truelight@0: 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); truelight@0: if (++v->cur_image > 3078) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: } else { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_3(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3084; truelight@0: v->progress = 1; truelight@0: } truelight@0: truelight@0: static void EffectTick_3(Vehicle *v) truelight@0: { truelight@0: if (++v->progress > 2) { truelight@0: v->progress = 0; truelight@0: BeginVehicleMove(v); truelight@0: if (++v->cur_image > 3089) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: } else { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_4(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 2040; truelight@0: v->progress = 12; truelight@0: } truelight@0: truelight@0: static void EffectTick_4(Vehicle *v) truelight@0: { truelight@0: bool moved; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: truelight@0: moved = false; truelight@193: truelight@0: if ((++v->progress & 3) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if ((v->progress & 0xF)==4) { truelight@0: if (++v->cur_image > 2044) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(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: truelight@0: static void EffectInit_5(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3709; truelight@0: v->progress = 0; truelight@0: } truelight@0: truelight@0: static void EffectTick_5(Vehicle *v) truelight@0: { truelight@0: if (!(++v->progress & 3)) { truelight@0: BeginVehicleMove(v); truelight@0: if (++v->cur_image > 3724) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: } else { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_6(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3737; truelight@0: v->progress = 0; truelight@0: } truelight@0: truelight@0: static void EffectTick_6(Vehicle *v) truelight@0: { truelight@0: if (!(++v->progress & 7)) { truelight@0: BeginVehicleMove(v); truelight@0: if (++v->cur_image > 3740) v->cur_image = 3737; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: truelight@0: if (!--v->u.special.unk0) { truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_7(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 3725; truelight@0: v->progress = 0; truelight@0: } truelight@0: truelight@0: static void EffectTick_7(Vehicle *v) truelight@0: { truelight@0: if (!(++v->progress & 3)) { truelight@0: BeginVehicleMove(v); truelight@0: if (++v->cur_image > 3736) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: } else { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_8(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 1416; truelight@0: v->progress = 0; truelight@0: v->u.special.unk0 = 0; truelight@0: v->u.special.unk2 = 0; truelight@0: } truelight@0: truelight@0: #define MK(imag,dir,dur) (imag<<6)+(dir<<4)+dur truelight@0: static const byte _effecttick8_data[] = { truelight@0: MK(0,0,4), truelight@0: MK(3,3,4), truelight@0: MK(2,2,7), truelight@0: MK(0,2,7), truelight@0: MK(1,1,3), truelight@0: MK(2,2,7), truelight@0: MK(0,2,7), truelight@0: MK(1,1,3), truelight@0: MK(2,2,7), truelight@0: MK(0,2,7), truelight@0: MK(3,3,6), truelight@0: MK(2,2,6), truelight@0: MK(1,1,7), truelight@0: MK(3,1,7), truelight@0: MK(0,0,3), truelight@0: MK(1,1,7), truelight@0: MK(3,1,7), truelight@0: MK(0,0,3), truelight@0: MK(1,1,7), truelight@0: MK(3,1,7), truelight@0: 255 truelight@0: }; truelight@0: #undef MK truelight@0: truelight@0: static const int8 _xy_inc_by_dir[5] = { truelight@0: -1, 0, 1, 0, -1, truelight@0: }; truelight@0: truelight@0: #define GET_X_INC_BY_DIR(d) _xy_inc_by_dir[d] truelight@0: #define GET_Y_INC_BY_DIR(d) _xy_inc_by_dir[(d)+1] truelight@0: truelight@0: static void EffectTick_8(Vehicle *v) truelight@0: { truelight@0: byte b; truelight@0: truelight@0: if (!(++v->progress & 7)) { truelight@0: v->u.special.unk2++; truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: b = _effecttick8_data[v->u.special.unk0]; truelight@0: truelight@0: v->cur_image = 0x588 + (b>>6); truelight@0: truelight@0: v->x_pos += GET_X_INC_BY_DIR((b>>4)&3); truelight@0: v->y_pos += GET_X_INC_BY_DIR((b>>4)&3); truelight@0: truelight@0: if (v->u.special.unk2 < (b & 7)) { truelight@0: v->u.special.unk2 = 0; truelight@0: v->u.special.unk0++; truelight@0: if (_effecttick8_data[v->u.special.unk0] == 0xFF) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: return; truelight@0: } truelight@0: } truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: truelight@0: static void EffectInit_9(Vehicle *v) truelight@0: { truelight@0: v->cur_image = 4751; truelight@0: v->spritenum = 0; truelight@0: v->progress = 0; truelight@0: } truelight@0: truelight@0: #define MK(x,y,z,i) (x+4)+(y+4)*16,(z+4)+i*16 truelight@0: truelight@0: /* -1,0,1,2 = 2*/ truelight@0: /* -1,0,1 = 2*/ truelight@0: /* */ truelight@0: static const byte _effecttick9_data1[] = { truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,2), truelight@0: 0x81, truelight@0: }; truelight@0: truelight@0: truelight@0: static const byte _effecttick9_data2[] = { truelight@0: MK(0,0,1,0), truelight@0: MK(-1,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(-1,0,1,2), truelight@0: 0x81, truelight@0: }; truelight@0: truelight@0: static const byte _effecttick9_data3[] = { truelight@0: MK(0,0,1,0), truelight@0: MK(0,1,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,1,1,2), truelight@0: 0x81, truelight@0: }; truelight@0: truelight@0: static const byte _effecttick9_data4[] = { truelight@0: MK(0,0,1,0), truelight@0: MK(0,-1,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,-1,1,2), truelight@0: 0x81, truelight@0: }; truelight@0: truelight@0: static const byte _effecttick9_data5[] = { truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,7), truelight@0: MK(0,0,1,8), truelight@0: MK(0,0,1,9), truelight@0: 0x80, truelight@0: }; truelight@0: truelight@0: static const byte _effecttick9_data6[] = { truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(0,0,1,1), truelight@0: MK(2,1,3,0), truelight@0: MK(1,1,3,1), truelight@0: MK(2,1,3,0), truelight@0: MK(1,1,3,2), truelight@0: MK(2,1,3,0), truelight@0: MK(1,1,3,1), truelight@0: MK(2,1,3,0), truelight@0: MK(1,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,2), truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,1), truelight@0: MK(0,0,1,0), truelight@0: MK(1,0,1,2), truelight@0: 0x82,0, truelight@0: MK(0,0,0,0xA), truelight@0: MK(0,0,0,0xB), truelight@0: MK(0,0,0,0xC), truelight@0: MK(0,0,0,0xD), truelight@0: MK(0,0,0,0xE), truelight@0: 0x80 truelight@0: }; truelight@0: #undef MK truelight@0: truelight@0: static const byte * const _effecttick9_data[6] = { truelight@0: _effecttick9_data1, truelight@0: _effecttick9_data2, truelight@0: _effecttick9_data3, truelight@0: _effecttick9_data4, truelight@0: _effecttick9_data5, truelight@0: _effecttick9_data6, truelight@0: }; truelight@0: truelight@0: static void EffectTick_9(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: */ truelight@0: int et; truelight@0: const byte *b; truelight@0: truelight@0: if (((++v->progress)&3) != 0) truelight@0: return; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: et = v->engine_type + 1; truelight@0: truelight@0: if (v->spritenum == 0) { truelight@0: if (++v->cur_image < 4754) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return; truelight@0: } truelight@0: if (v->u.special.unk2 != 0) { truelight@543: v->spritenum = (byte)((InteractiveRandom()&3)+1); truelight@0: } else { truelight@0: v->spritenum = 6; truelight@0: } truelight@0: et = 0; truelight@0: } truelight@0: truelight@0: again: truelight@0: v->engine_type = et; truelight@0: b = &_effecttick9_data[v->spritenum - 1][et*2]; truelight@0: truelight@0: if (*b == 0x80) { truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: return; truelight@193: } truelight@193: truelight@0: if (*b == 0x81) { truelight@543: 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@0: goto again; truelight@193: } truelight@193: truelight@0: if (*b == 0x82) { truelight@0: uint tile; truelight@0: truelight@0: et++; tron@541: SndPlayVehicleFx(SND_31_EXTRACT, v); truelight@0: truelight@0: tile = TILE_FROM_XY(v->x_pos, v->y_pos); truelight@0: if (IS_TILETYPE(tile, MP_INDUSTRY) && truelight@0: _map5[tile]==0xA2) { truelight@0: AddAnimatedTile(tile); truelight@0: } truelight@0: goto again; truelight@0: } truelight@0: truelight@0: v->x_pos += (b[0]&0xF) - 4; truelight@0: v->y_pos += (b[0]>>4) - 4; truelight@0: v->z_pos += (b[1]&0xF) - 4; truelight@0: v->cur_image = 4748 + (b[1] >> 4); 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[] = { truelight@0: EffectInit_0, truelight@0: EffectInit_1, truelight@0: EffectInit_2, truelight@0: EffectInit_3, truelight@0: EffectInit_4, truelight@0: EffectInit_5, truelight@0: EffectInit_6, truelight@0: EffectInit_7, truelight@0: EffectInit_8, truelight@0: EffectInit_9, truelight@0: }; truelight@0: truelight@0: static EffectTickProc * const _effect_tick_procs[] = { truelight@0: EffectTick_0, truelight@0: EffectTick_1, truelight@0: EffectTick_2, truelight@0: EffectTick_3, truelight@0: EffectTick_4, truelight@0: EffectTick_5, truelight@0: EffectTick_6, truelight@0: EffectTick_7, truelight@0: EffectTick_8, truelight@0: EffectTick_9, truelight@0: }; truelight@0: truelight@0: truelight@0: Vehicle *CreateEffectVehicle(int x, int y, int z, int type) truelight@0: { truelight@0: Vehicle *v; truelight@193: truelight@0: v = ForceAllocateSpecialVehicle(); truelight@0: if (v != NULL) { truelight@0: v->type = VEH_Special; 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->z_height = v->sprite_width = v->sprite_height = 1; truelight@0: v->x_offs = v->y_offs = 0; truelight@0: v->tile = 0; 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: truelight@0: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, int type) truelight@0: { truelight@0: return CreateEffectVehicle(x, y, GetSlopeZ(x, y) + z, type); truelight@0: } truelight@0: truelight@0: Vehicle *CreateEffectVehicleRel(Vehicle *v, int x, int y, int z, int type) truelight@0: { truelight@0: return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type); truelight@0: } truelight@0: tron@410: static void EffectVehicle_Tick(Vehicle *v) truelight@0: { truelight@0: _effect_tick_procs[v->subtype](v); truelight@0: } truelight@0: truelight@0: Vehicle *CheckClickOnVehicle(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@0: x = (x << vp->zoom) + vp->virtual_left; truelight@0: y = (y << vp->zoom) + vp->virtual_top; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@193: if (v->type != 0 && (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( truelight@0: myabs( ((v->left_coord + v->right_coord)>>1) - x ), truelight@0: myabs( ((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: 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] = { truelight@0: 3, 3, 3, 3, 3, 3, 3, 3, truelight@0: 4, 4, 5, 5, 6, 6, 7, 7, truelight@0: 8, 8, 9, 9, 10, 10, 11, 11, truelight@0: 12, 13, 13, 13, 13, 14, 15, 16, truelight@0: 17, 19, 21, 25, 28, 31, 34, 37, truelight@0: 40, 44, 48, 52, 56, 60, 64, 68, truelight@0: 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: truelight@0: if (v->breakdown_ctr != 0 || (v->vehstatus & VS_STOPPED) != 0 || truelight@0: v->cur_speed < 5 || _game_mode == GM_MENU) truelight@0: return; truelight@0: truelight@0: r = Random(); truelight@0: truelight@0: /* increase chance of failure */ truelight@0: chance = v->breakdown_chance + 1; truelight@0: 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; truelight@0: if (v->type == VEH_Ship) rel += 0x6666; truelight@193: truelight@0: /* disabled breakdowns? */ truelight@0: if (_opt.diff.vehicle_breakdowns < 1) truelight@0: 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) { truelight@0: v->breakdown_ctr = (byte)(((r >> 16) & 0x3F) + 0x3F); truelight@0: v->breakdown_delay = (byte)(((r >> 24) & 0x7F) | 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: { truelight@0: if (v->owner != _local_player) truelight@0: return; truelight@812: truelight@26: // Do not show getting-old message if autorenew is active truelight@26: if (_patches.autorenew) truelight@26: return; truelight@0: tron@534: SetDParam(0, _vehicle_type_names[v->type - 0x10]); tron@534: SetDParam(1, v->unitnumber); truelight@0: AddNewsItem(msg, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); truelight@0: } truelight@0: truelight@0: void AgeVehicle(Vehicle *v) truelight@0: { truelight@0: int age; truelight@0: truelight@0: if (v->age < 65535) truelight@0: v->age++; truelight@0: truelight@0: age = v->age - v->max_age; truelight@0: if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4) truelight@193: 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); truelight@0: } else if (age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4 || age == 366*5) { truelight@0: ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND); truelight@0: } truelight@0: } truelight@0: truelight@812: extern int32 EstimateTrainCost(const RailVehicleInfo *rvi); truelight@812: extern int32 EstimateRoadVehCost(byte engine_type); truelight@812: extern int32 EstimateShipCost(uint16 engine_type); truelight@812: extern int32 EstimateAircraftCost(uint16 engine_type); bjarni@842: extern int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2); bjarni@842: extern int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2); bjarni@842: extern int32 CmdRefitAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2); truelight@812: bjarni@842: /* Replaces a vehicle (used to be called autorenew) truelight@812: p1 - Index of vehicle truelight@812: p2 - Type of new engine */ bjarni@842: int32 CmdReplaceVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { bjarni@842: /* makesvariables to inform about how much money the player wants to have left after replacing bjarni@842: and which engine to replace with out of p2. bjarni@842: the first 16 bit is the money. The last 5 digits (all 0) were removed when sent, so we add them again. bjarni@842: This way the max is 6553 millions and it is more than the 32 bit that is stored in _patches bjarni@842: This is a nice way to send 32 bit and only use 16 bit bjarni@842: the last 8 bit is the engine. The 8 bits in front of the engine is free so it have room for 16 bit engine entries */ bjarni@842: uint16 new_engine_type = (uint16)(p2 & 0xFFFF); tron@915: uint32 autorefit_money = (p2 >> 16) * 100000; truelight@919: Vehicle *v = GetVehicle(p1); truelight@812: int cost, build_cost; darkvater@889: darkvater@889: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); tron@915: tron@915: bjarni@890: // first we make sure that it's a valid type the user requested bjarni@890: // check that it's an engine that is in the engine array bjarni@890: if (new_engine_type >= TOTAL_NUM_ENGINES ) return CMD_ERROR; tron@915: bjarni@890: // check that the new vehicle type is the same as the original one bjarni@907: if (v->type != DEREF_ENGINE(new_engine_type)->type) return CMD_ERROR; truelight@812: bjarni@890: // check that it's the vehicle's owner that requested the replace bjarni@890: if (!CheckOwnership(v->owner)) return CMD_ERROR; bjarni@890: bjarni@890: truelight@812: switch (v->type) { bjarni@842: case VEH_Train: build_cost = EstimateTrainCost(RailVehInfo(new_engine_type)); break; bjarni@842: case VEH_Road: build_cost = EstimateRoadVehCost(new_engine_type); break; bjarni@842: case VEH_Ship: build_cost = EstimateShipCost(new_engine_type); break; bjarni@842: case VEH_Aircraft: build_cost = EstimateAircraftCost(new_engine_type); break; truelight@812: default: return CMD_ERROR; truelight@812: } truelight@812: truelight@812: /* In a rare situation, when 2 clients are connected to 1 company and have the same truelight@812: settings, a vehicle can be replaced twice.. check if this is the situation here */ bjarni@842: if (v->engine_type == new_engine_type && v->age == 0) truelight@812: return CMD_ERROR; truelight@812: truelight@812: /* Check if there is money for the upgrade.. if not, give a nice news-item truelight@812: (that is needed, because this CMD is called automaticly) */ bjarni@842: if ( DEREF_PLAYER(v->owner)->money64 < (int32)(autorefit_money + build_cost - v->value)) { bjarni@842: if (( _local_player == v->owner ) && ( v->unitnumber != 0 )) { //v->unitnumber = 0 for train cars truelight@812: int message; truelight@812: SetDParam(0, v->unitnumber); truelight@812: switch (v->type) { truelight@812: case VEH_Train: message = STR_TRAIN_AUTORENEW_FAILED; break; truelight@812: case VEH_Road: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; truelight@812: case VEH_Ship: message = STR_SHIP_AUTORENEW_FAILED; break; truelight@812: case VEH_Aircraft: message = STR_AIRCRAFT_AUTORENEW_FAILED; break; truelight@812: // This should never happen truelight@812: default: message = 0; break; truelight@812: } truelight@812: truelight@812: AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); truelight@812: } truelight@812: truelight@812: return CMD_ERROR; truelight@812: } truelight@812: cost = build_cost - v->value; truelight@812: truelight@812: truelight@812: if (flags & DC_EXEC) { truelight@812: Engine *e; bjarni@907: e = DEREF_ENGINE(new_engine_type); tron@915: bjarni@842: // TODO make it check if refit is possible before actually doing it truelight@812: truelight@812: /* We do not really buy a new vehicle, we upgrade the old one */ truelight@812: if (v->engine_type != new_engine_type) { bjarni@842: byte cargo_type = v->cargo_type; bjarni@842: v->engine_type = new_engine_type; bjarni@842: v->max_age = e->lifelength * 366; tron@915: bjarni@842: /* Update limits of the vehicle (for when upgraded) */ bjarni@842: switch (v->type) { bjarni@842: case VEH_Train: bjarni@842: // using if (true) to declare the const bjarni@842: { bjarni@842: const RailVehicleInfo *rvi = RailVehInfo(new_engine_type); bjarni@842: byte capacity = rvi->capacity; bjarni@842: bjarni@842: v->spritenum = rvi->image_index; bjarni@842: v->cargo_type = rvi->cargo_type; bjarni@842: v->cargo_cap = rvi->capacity; bjarni@842: v->max_speed = rvi->max_speed; bjarni@842: bjarni@842: v->u.rail.railtype = e->railtype; tron@915: bjarni@842: // 0x0100 means that we skip the check for being stopped inside the depot bjarni@842: // since we do not stop it for autorefitting bjarni@842: if (v->cargo_type != cargo_type && capacity) { bjarni@842: // BUG: somehow v->index is not transfered properly bjarni@842: //CmdRefitRailVehicle(v->x_pos, v->y_pos, DC_EXEC, v->index , cargo_type + 0x0100 ); bjarni@842: v->cargo_type = cargo_type; // workaround, but it do not check the refit table bjarni@842: } else { bjarni@842: v->cargo_type = rvi->cargo_type; bjarni@842: } bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Road: bjarni@842: // using if (true) to declare the const bjarni@842: if (true) { bjarni@842: const RoadVehicleInfo *rvi = RoadVehInfo(new_engine_type); bjarni@842: bjarni@842: v->spritenum = rvi->image_index; bjarni@842: v->cargo_type = rvi->cargo_type; bjarni@842: v->cargo_cap = rvi->capacity; bjarni@842: v->max_speed = rvi->max_speed; bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Ship: bjarni@842: // using if (true) to declare the const bjarni@842: if (true) { bjarni@842: const ShipVehicleInfo *svi = ShipVehInfo(new_engine_type); bjarni@842: bjarni@842: v->spritenum = svi->image_index; bjarni@842: v->cargo_type = svi->cargo_type; bjarni@842: v->cargo_cap = svi->capacity; bjarni@842: v->max_speed = svi->max_speed; tron@915: bjarni@842: // 0x0100 means that we skip the check for being stopped inside the depot bjarni@842: // since we do not stop it for autorefitting bjarni@842: if (v->cargo_type != cargo_type) bjarni@842: CmdRefitShip(v->x_pos, v->y_pos, DC_EXEC, v->index , cargo_type + 0x0100 ); bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Aircraft: bjarni@842: // using if (true) to declare the const bjarni@842: if (true) { bjarni@842: const AircraftVehicleInfo *avi = AircraftVehInfo(new_engine_type); bjarni@842: Vehicle *u; bjarni@842: bjarni@842: v->max_speed = avi->max_speed; bjarni@842: v->acceleration = avi->acceleration; bjarni@842: v->spritenum = avi->image_index; bjarni@842: bjarni@842: if ( cargo_type == CT_PASSENGERS ) { celestar@922: v->cargo_cap = avi->passenger_capacity; bjarni@842: u = v->next; bjarni@842: u->cargo_cap = avi->mail_capacity; bjarni@842: } else { bjarni@842: // 0x0100 means that we skip the check for being stopped inside the hangar bjarni@842: // since we do not stop it for autorefitting bjarni@842: CmdRefitAircraft(v->x_pos, v->y_pos, DC_EXEC, v->index , cargo_type + 0x0100 ); bjarni@842: } bjarni@842: break; bjarni@842: } bjarni@842: default: return CMD_ERROR; bjarni@842: } bjarni@842: // makes sure that the cargo is still valid compared to new capacity bjarni@842: if (v->cargo_count != 0) { bjarni@842: if ( v->cargo_type != cargo_type ) bjarni@842: v->cargo_count = 0; bjarni@842: else if ( v->cargo_count > v->cargo_cap ) bjarni@842: v->cargo_count = v->cargo_cap; bjarni@842: } truelight@812: } tron@915: truelight@812: v->reliability = e->reliability; truelight@812: v->reliability_spd_dec = e->reliability_spd_dec; truelight@812: v->age = 0; truelight@812: truelight@812: v->date_of_last_service = _date; truelight@812: v->build_year = _cur_year; truelight@812: truelight@812: v->value = build_cost; truelight@812: truelight@812: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@812: } bjarni@842: //needs to be down here because refitting will change SET_EXPENSES_TYPE if called bjarni@842: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@812: truelight@812: return cost; truelight@812: } truelight@812: bjarni@842: void MaybeReplaceVehicle(Vehicle *v) truelight@812: { bjarni@842: uint32 new_engine_and_autoreplace_money; tron@915: truelight@812: if (v->owner != _local_player) truelight@812: return; bjarni@842: // uncomment next line if you want to see what engine type just entered a depot bjarni@842: //printf("engine type: %d\n", v->engine_type); truelight@0: truelight@26: // A vehicle is autorenewed when it it gets the amount of months truelight@26: // give by _patches.autorenew_months away for his max age. truelight@26: // Standard is -6, meaning 6 months before his max age truelight@26: // It can be any value between -12 and 12. bjarni@842: // Here it also checks if the vehicles is listed for replacement bjarni@842: if (!_patches.autorenew || v->age - v->max_age < (_patches.autorenew_months * 30)) { //replace if engine is too old bjarni@842: if (_autoreplace_array[v->engine_type] == v->engine_type && v->type != VEH_Train) //updates to a new model bjarni@842: return; bjarni@842: } bjarni@842: /* Now replace the vehicle */ truelight@829: _current_player = v->owner; tron@915: bjarni@842: /* makes the variable to inform about how much money the player wants to have left after replacing bjarni@842: and which engine to replace with bjarni@842: the first 16 bit is the money. Since we know the last 5 digits is 0, they are thrown away. bjarni@842: This way the max is 6553 millions and it is more than the 32 bit that is stored in _patches bjarni@842: This is a nice way to send 32 bit and only use 16 bit bjarni@842: the last 8 bit is the engine. The 8 bits in front of the engine is free so it have room for 16 bit engine entries */ bjarni@892: new_engine_and_autoreplace_money = ((_patches.autorenew_money / 100000) << 16) + _autoreplace_array[v->engine_type]; tron@915: bjarni@907: assert(v->type == DEREF_ENGINE(_autoreplace_array[v->engine_type])->type); tron@915: bjarni@842: if ( v->type != VEH_Train ) { bjarni@842: DoCommandP(v->tile, v->index, new_engine_and_autoreplace_money, NULL, CMD_REPLACE_VEHICLE | CMD_SHOW_NO_ERROR); bjarni@842: } else { bjarni@892: // checks if any of the engines in the train are either old or listed for replacement bjarni@892: do { bjarni@907: if ( v->engine_type != _autoreplace_array[v->engine_type] || (_patches.autorenew && (v->age - v->max_age) > (_patches.autorenew_months * 30))) { bjarni@892: new_engine_and_autoreplace_money = (new_engine_and_autoreplace_money & 0xFFFF0000) + _autoreplace_array[v->engine_type]; // sets the new engine replacement type bjarni@892: DoCommandP(v->tile, v->index, new_engine_and_autoreplace_money, NULL, CMD_REPLACE_VEHICLE | CMD_SHOW_NO_ERROR); bjarni@842: } bjarni@892: } while ((v=v->next) != NULL); bjarni@842: } bjarni@842: _current_player = OWNER_NONE; truelight@0: } truelight@0: truelight@0: truelight@0: int32 CmdNameVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: StringID str; truelight@0: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (!CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@543: str = AllocateNameUnique((byte*)_decode_parameters, 2); truelight@0: if (str == 0) truelight@0: return CMD_ERROR; truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: StringID old_str = v->string_id; truelight@0: v->string_id = str; truelight@0: DeleteName(old_str); tron@588: ResortVehicleLists(); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: truelight@0: truelight@0: static Rect _old_vehicle_coords; truelight@0: truelight@0: void BeginVehicleMove(Vehicle *v) { truelight@0: _old_vehicle_coords.left = v->left_coord; truelight@0: _old_vehicle_coords.top = v->top_coord; truelight@0: _old_vehicle_coords.right = v->right_coord; truelight@0: _old_vehicle_coords.bottom = v->bottom_coord; truelight@0: } truelight@0: truelight@0: void EndVehicleMove(Vehicle *v) truelight@0: { truelight@0: MarkAllViewportsDirty( truelight@0: min(_old_vehicle_coords.left,v->left_coord), truelight@0: min(_old_vehicle_coords.top,v->top_coord), truelight@0: max(_old_vehicle_coords.right,v->right_coord)+1, truelight@0: max(_old_vehicle_coords.bottom,v->bottom_coord)+1 truelight@0: ); truelight@0: } truelight@0: truelight@0: void InvalidateVehicleOrderWidget(Vehicle *v) truelight@0: { darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: InvalidateWindowWidget(WC_VEHICLE_ORDERS, v->index, 2); truelight@0: } truelight@0: truelight@0: /* returns true if staying in the same tile */ truelight@0: bool GetNewVehiclePos(Vehicle *v, GetNewVehiclePosResult *gp) truelight@0: { truelight@0: static const int8 _delta_coord[16] = { truelight@0: -1,-1,-1, 0, 1, 1, 1, 0, /* x */ truelight@0: -1, 0, 1, 1, 1, 0,-1,-1, /* y */ truelight@0: }; truelight@0: truelight@0: int x = v->x_pos + _delta_coord[v->direction]; truelight@0: int y = v->y_pos + _delta_coord[v->direction + 8]; truelight@0: truelight@0: gp->x = x; truelight@0: gp->y = y; truelight@0: gp->old_tile = v->tile; truelight@0: gp->new_tile = TILE_FROM_XY(x,y); truelight@0: return gp->old_tile == gp->new_tile; truelight@0: } truelight@0: truelight@0: static const byte _new_direction_table[9] = { truelight@0: 0, 7, 6, truelight@0: 1, 3, 5, truelight@0: 2, 3, 4, truelight@0: }; truelight@0: truelight@0: byte GetDirectionTowards(Vehicle *v, int x, int y) truelight@0: { truelight@0: byte dirdiff, dir; truelight@0: int i = 0; truelight@0: truelight@0: if (y >= v->y_pos) { truelight@0: if (y != v->y_pos) i+=3; truelight@0: i+=3; truelight@0: } truelight@0: truelight@0: if (x >= v->x_pos) { truelight@0: if (x != v->x_pos) i++; truelight@0: i++; truelight@0: } truelight@0: truelight@0: dir = v->direction; truelight@0: truelight@0: dirdiff = _new_direction_table[i] - dir; truelight@0: if (dirdiff == 0) truelight@0: return dir; truelight@0: return (dir+((dirdiff&7)<5?1:-1)) & 7; truelight@0: } truelight@0: darkvater@22: /* Return value has bit 0x2 set, when the vehicle enters a station. Then, darkvater@22: * result << 8 contains the id of the station entered. If the return value has darkvater@22: * bit 0x8 set, the vehicle could not and did not enter the tile. Are there darkvater@22: * other bits that can be set? */ truelight@0: uint32 VehicleEnterTile(Vehicle *v, uint tile, int x, int y) truelight@193: { truelight@0: uint old_tile = v->tile; truelight@0: uint32 result = _tile_type_procs[GET_TILETYPE(tile)]->vehicle_enter_tile_proc(v, tile, x, y); truelight@193: darkvater@22: /* When vehicle_enter_tile_proc returns 8, that apparently means that darkvater@22: * we cannot enter the tile at all. In that case, don't call darkvater@22: * leave_tile. */ truelight@0: if (!(result & 8) && old_tile != tile) { truelight@0: VehicleLeaveTileProc *proc = _tile_type_procs[GET_TILETYPE(old_tile)]->vehicle_leave_tile_proc; truelight@0: if (proc != NULL) truelight@0: proc(v, old_tile, x, y); truelight@0: } truelight@0: return result; truelight@0: } truelight@0: truelight@0: uint GetFreeUnitNumber(byte type) truelight@0: { truelight@0: uint unit_num = 0; truelight@0: Vehicle *u; truelight@0: truelight@0: restart: truelight@0: unit_num++; truelight@0: FOR_ALL_VEHICLES(u) { truelight@193: if (u->type == type && u->owner == _current_player && truelight@0: unit_num == u->unitnumber) truelight@0: goto restart; truelight@0: } truelight@0: return unit_num; truelight@0: } truelight@0: truelight@0: truelight@0: // Save and load of vehicles truelight@0: const byte _common_veh_desc[] = { truelight@0: SLE_VAR(Vehicle,subtype, SLE_UINT8), truelight@193: truelight@0: SLE_VAR(Vehicle,next_in_chain_old, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,string_id, SLE_STRINGID), truelight@0: SLE_VAR(Vehicle,unitnumber, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,owner, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,tile, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,dest_tile, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,x_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,y_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,z_pos, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,direction, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,cur_image, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,spritenum, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,sprite_width, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,sprite_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,z_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,x_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,y_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,engine_type, SLE_UINT16), tron@445: truelight@0: SLE_VAR(Vehicle,max_speed, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,cur_speed, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,subspeed, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,acceleration, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,progress, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,vehstatus, SLE_UINT8), truelight@817: SLE_CONDVAR(Vehicle,last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, 0, 4), truelight@817: SLE_CONDVAR(Vehicle,last_station_visited, SLE_UINT16, 5, 255), truelight@0: truelight@0: SLE_VAR(Vehicle,cargo_type, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,cargo_days, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,cargo_source, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,cargo_cap, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,cargo_count, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,day_counter, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,tick_counter, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,cur_order_index, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,num_orders, SLE_UINT8), tron@555: SLE_VAR(Vehicle,current_order, SLE_UINT8), /* XXX hack to avoid version bump */ darkvater@556: #if _MSC_VER != 1200 tron@555: SLE_VAR(Vehicle,current_order.station, SLE_UINT8), darkvater@556: #else /* XXX workaround for MSVC6 */ darkvater@556: SLE_VAR2(Vehicle, current_order, Order, station, SLE_UINT8), darkvater@556: #endif truelight@0: SLE_REF(Vehicle,schedule_ptr, REF_SCHEDULE), truelight@0: truelight@0: SLE_VAR(Vehicle,age, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,max_age, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,date_of_last_service,SLE_UINT16), truelight@0: SLE_VAR(Vehicle,service_interval, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,reliability, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,reliability_spd_dec,SLE_UINT16), truelight@0: SLE_VAR(Vehicle,breakdown_ctr, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,breakdown_delay, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,breakdowns_since_last_service, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,breakdown_chance, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,build_year, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,load_unload_time_rem, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,profit_this_year, SLE_INT32), truelight@0: SLE_VAR(Vehicle,profit_last_year, SLE_INT32), truelight@0: SLE_VAR(Vehicle,value, SLE_UINT32), truelight@0: tron@445: SLE_VAR(Vehicle,random_bits, SLE_UINT8), tron@445: SLE_VAR(Vehicle,waiting_triggers, SLE_UINT8), tron@445: tron@445: // reserve extra space in savegame here. (currently 14 bytes) tron@445: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 2, 2, 255), /* 2 */ tron@445: SLE_CONDARR(NullStruct,null,SLE_FILE_U16 | SLE_VAR_NULL, 2, 2, 255), /* 4 */ tron@445: SLE_CONDARR(NullStruct,null,SLE_FILE_U32 | SLE_VAR_NULL, 2, 2, 255), /* 8 */ truelight@193: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: truelight@0: static const byte _train_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Train, 0), // Train type. VEH_Train in mem, 0 in file. truelight@0: SLE_INCLUDEX(0, INC_VEHICLE_COMMON), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,crash_anim_pos), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,force_proceed), SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,railtype), SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,track), SLE_UINT8), truelight@0: truelight@0: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRail,flags), SLE_UINT8, 2, 255), truelight@0: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRail,days_since_order_progr), SLE_UINT16, 2, 255), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 13 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 13, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _roadveh_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Road, 1), // Road type. VEH_Road in mem, 1 in file. truelight@0: SLE_INCLUDEX(0, INC_VEHICLE_COMMON), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,state), SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,frame), SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,unk2), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,overtaking), SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,overtaking_ctr),SLE_UINT8), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,crashed_ctr), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,reverse_ctr), SLE_UINT8), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 16 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _ship_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Ship, 2), // Ship type. VEH_Ship in mem, 2 in file. truelight@0: SLE_INCLUDEX(0, INC_VEHICLE_COMMON), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleShip,state), SLE_UINT8), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 16 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _aircraft_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Aircraft, 3), // Aircraft type. VEH_Aircraft in mem, 3 in file. truelight@0: SLE_INCLUDEX(0, INC_VEHICLE_COMMON), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,crashed_counter), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,pos), SLE_UINT8), truelight@817: truelight@817: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleAir,targetairport), SLE_FILE_U8 | SLE_VAR_U16, 0, 4), truelight@817: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleAir,targetairport), SLE_UINT16, 5, 255), truelight@817: truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,state), SLE_UINT8), truelight@193: truelight@0: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleAir,previous_pos), SLE_UINT8, 2, 255), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 15 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 15, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _special_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Special, 4), truelight@0: truelight@0: SLE_VAR(Vehicle,subtype, SLE_UINT8), truelight@193: truelight@0: SLE_VAR(Vehicle,tile, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,x_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,y_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,z_pos, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,cur_image, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,sprite_width, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,sprite_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,z_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,x_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,y_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,progress, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,vehstatus, SLE_UINT8), truelight@0: truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleSpecial,unk0), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleSpecial,unk2), SLE_UINT8), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 16 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _disaster_desc[] = { truelight@0: SLE_WRITEBYTE(Vehicle,type,VEH_Disaster, 5), truelight@0: truelight@0: SLE_VAR(Vehicle,next_in_chain_old,SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,subtype, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,tile, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,dest_tile, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,x_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,y_pos, SLE_INT16), truelight@0: SLE_VAR(Vehicle,z_pos, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,direction, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Vehicle,x_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,y_offs, SLE_INT8), truelight@0: SLE_VAR(Vehicle,sprite_width, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,sprite_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,z_height, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,owner, SLE_UINT8), truelight@0: SLE_VAR(Vehicle,vehstatus, SLE_UINT8), darkvater@556: #if _MSC_VER != 1200 tron@555: SLE_VAR(Vehicle,current_order.station, SLE_UINT8), darkvater@556: #else /* XXX workaround for MSVC6 */ darkvater@556: SLE_VAR2(Vehicle, current_order, Order, station, SLE_UINT8), darkvater@556: #endif truelight@0: truelight@0: SLE_VAR(Vehicle,cur_image, SLE_UINT16), truelight@0: SLE_VAR(Vehicle,age, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Vehicle,tick_counter, SLE_UINT8), truelight@0: truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleDisaster,image_override), SLE_UINT16), truelight@0: SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleDisaster,unk2), SLE_UINT16), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 16 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: truelight@0: static const void *_veh_descs[] = { truelight@0: _train_desc, truelight@0: _roadveh_desc, truelight@0: _ship_desc, truelight@0: _aircraft_desc, truelight@0: _special_desc, truelight@0: _disaster_desc, truelight@0: }; truelight@0: truelight@0: // Will be called when the vehicles need to be saved. truelight@0: static void Save_VEHS() truelight@0: { truelight@0: Vehicle *v; truelight@0: // Write the vehicles truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != 0) { truelight@817: /* XXX - Here for now, because we did not bump the savegame to version 5 yet */ truelight@817: if (_sl.version < 5 && v->last_station_visited == 0xFFFF) truelight@817: v->last_station_visited = 0xFF; truelight@817: truelight@0: SlSetArrayIndex(v->index); truelight@0: v->next_in_chain_old = v->next ? v->next->index : INVALID_VEHICLE; truelight@0: SlObject(v, _veh_descs[v->type - 0x10]); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // Will be called when vehicles need to be loaded. truelight@0: static void Load_VEHS() truelight@0: { truelight@0: int index; truelight@0: Vehicle *v; truelight@0: truelight@0: while ((index = SlIterateArray()) != -1) { truelight@919: Vehicle *v = GetVehicle(index); truelight@919: truelight@0: v->next_in_chain_old = INVALID_VEHICLE; truelight@0: SlObject(v, _veh_descs[SlReadByte()]); truelight@919: v->next = (v->next_in_chain_old == INVALID_VEHICLE) ? NULL : GetVehicle(v->next_in_chain_old); truelight@0: if (v->type == VEH_Train) truelight@0: v->u.rail.first_engine = 0xffff; truelight@817: truelight@817: /* Old savegames used 'last_station_visited = 0xFF', should be 0xFFFF */ truelight@817: if (_sl.version < 5 && v->last_station_visited == 0xFF) truelight@817: v->last_station_visited = 0xFFFF; truelight@0: } truelight@0: truelight@0: // Iterate through trains and set first_engine appropriately. truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: Vehicle *w; truelight@0: truelight@0: if (v->type != VEH_Train || v->subtype != 0) truelight@0: continue; truelight@0: truelight@0: for (w = v->next; w; w = w->next) truelight@0: w->u.rail.first_engine = v->engine_type; truelight@0: } truelight@919: truelight@919: /* This is to ensure all pointers are within the limits of truelight@919: _vehicles_size */ truelight@919: if (_vehicle_id_ctr_day >= _vehicles_size) truelight@919: _vehicle_id_ctr_day = 0; truelight@0: } truelight@0: truelight@0: static const byte _depot_desc[] = { truelight@0: SLE_VAR(Depot,xy, SLE_UINT16), truelight@0: SLE_VAR(Depot,town_index, SLE_UINT16), truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static void Save_DEPT() truelight@0: { truelight@0: Depot *d; truelight@0: int i; truelight@0: for(i=0,d=_depots; i!=lengthof(_depots); i++,d++) { truelight@0: if (d->xy != 0) { truelight@0: SlSetArrayIndex(i); truelight@0: SlObject(d, _depot_desc); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void Load_DEPT() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { truelight@0: SlObject(&_depots[index], _depot_desc); truelight@0: } truelight@0: } truelight@0: darkvater@395: static const byte _waypoint_desc[] = { darkvater@395: SLE_VAR(Waypoint,xy, SLE_UINT16), darkvater@395: SLE_VAR(Waypoint,town_or_string, SLE_UINT16), darkvater@395: SLE_VAR(Waypoint,deleted, SLE_UINT8), truelight@0: darkvater@395: SLE_CONDVAR(Waypoint, build_date, SLE_UINT16, 3, 255), darkvater@395: SLE_CONDVAR(Waypoint, stat_id, SLE_UINT8, 3, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static void Save_CHKP() truelight@0: { darkvater@395: Waypoint *cp; truelight@0: int i; darkvater@395: for(i=0,cp=_waypoints; i!=lengthof(_waypoints); i++,cp++) { truelight@0: if (cp->xy != 0) { truelight@0: SlSetArrayIndex(i); darkvater@395: SlObject(cp, _waypoint_desc); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void Load_CHKP() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { darkvater@395: SlObject(&_waypoints[index], _waypoint_desc); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static void Save_ORDR() truelight@0: { tron@555: uint16 orders[lengthof(_order_array)]; truelight@0: uint len = _ptr_to_next_order - _order_array; tron@555: uint i; tron@555: tron@555: assert (len <= lengthof(orders)); tron@555: tron@555: for (i = 0; i < len; ++i) tron@555: orders[i] = PackOrder(&_order_array[i]); tron@555: tron@555: SlArray(orders, len, SLE_UINT16); truelight@0: } truelight@0: truelight@0: static void Load_ORDR() truelight@0: { tron@555: uint16 orders[lengthof(_order_array)]; truelight@0: uint len = SlGetFieldLength() >> 1; tron@555: uint i; tron@555: tron@555: assert (len <= lengthof(orders)); tron@555: truelight@0: _ptr_to_next_order = _order_array + len; tron@555: SlArray(orders, len, SLE_UINT16); tron@555: tron@555: for (i = 0; i < len; ++i) tron@585: _order_array[i] = UnpackOrder(orders[i]); truelight@0: } truelight@0: truelight@0: const ChunkHandler _veh_chunk_handlers[] = { truelight@0: { 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY}, truelight@0: { 'ORDR', Save_ORDR, Load_ORDR, CH_RIFF}, truelight@0: { 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY}, truelight@0: { 'CHKP', Save_CHKP, Load_CHKP, CH_ARRAY | CH_LAST}, truelight@0: }; truelight@0: truelight@0: