truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@1349: #include "spritecache.h" tron@1364: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "vehicle.h" truelight@0: #include "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@1279: /* Max vehicles: 64000 (512 * 125) */ truelight@1279: VEHICLES_POOL_BLOCK_SIZE_BITS = 9, /* In bits, so (1 << 9) == 512 */ truelight@1279: VEHICLES_POOL_MAX_BLOCKS = 125, truelight@1279: truelight@1279: BLOCKS_FOR_SPECIAL_VEHICLES = 2, //! Blocks needed for special vehicles truelight@919: }; truelight@919: truelight@1279: /** truelight@1279: * Called if a new block is added to the vehicle-pool truelight@1279: */ truelight@1279: static void VehiclePoolNewBlock(uint start_item) truelight@1279: { truelight@1279: Vehicle *v; truelight@1279: truelight@1279: FOR_ALL_VEHICLES_FROM(v, start_item) truelight@1279: v->index = start_item++; truelight@1279: } truelight@1279: truelight@1279: /* Initialize the vehicle-pool */ truelight@1279: MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, 0, 0, NULL }; truelight@1279: 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: { darkvater@1037: if (_patches.no_servicing_if_no_breakdowns && _opt.diff.vehicle_breakdowns == 0) darkvater@1037: return false; darkvater@1037: 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: 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: darkvater@1082: static inline uint Correct_Z(uint tileh) darkvater@1082: { darkvater@1082: // needs z correction for slope-type graphics that have the NORTHERN tile lowered darkvater@1082: // 1, 2, 3, 4, 5, 6 and 7 darkvater@1082: return (CORRECT_Z(tileh)) ? 8 : 0; darkvater@1082: } darkvater@1082: darkvater@1082: uint GetCorrectTileHeight(TileIndex tile) darkvater@1082: { tron@1192: return Correct_Z(GetTileSlope(tile, NULL)); darkvater@1082: } darkvater@1082: truelight@0: bool EnsureNoVehicleZ(TileIndex tile, byte z) truelight@0: { truelight@0: TileInfo ti; tron@537: truelight@0: FindLandscapeHeightByTile(&ti, tile); darkvater@1082: ti.z = z + Correct_Z(ti.tileh); 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 tron@1093: void AfterLoadVehicles(void) 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) { bjarni@1067: if (v->subtype == TS_Front_Engine) truelight@0: UpdateTrainAcceleration(v); truelight@0: } 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@1024: assert(v->orders == 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@1111: v->next_shared = NULL; truelight@1111: v->prev_shared = NULL; bjarni@1139: v->set_for_replacement = false; 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: tron@1093: Vehicle *ForceAllocateSpecialVehicle(void) truelight@0: { truelight@1279: /* This stays a strange story.. there should always be room for special truelight@1279: * vehicles (special effects all over the map), but with 65k of vehicles truelight@1279: * is this realistic to double-check for that? For now we just reserve truelight@1279: * BLOCKS_FOR_SPECIAL_VEHICLES times block_size vehicles that may only truelight@1279: * be used for special vehicles.. should work nicely :) */ truelight@0: truelight@1279: Vehicle *v; truelight@0: truelight@919: FOR_ALL_VEHICLES(v) { truelight@1279: /* No more room for the special vehicles, return NULL */ truelight@1279: if (v->index >= (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES) truelight@919: return NULL; truelight@919: truelight@0: if (v->type == 0) truelight@0: return InitializeVehicle(v); truelight@0: } truelight@1279: truelight@0: return NULL; truelight@0: } truelight@0: tron@1093: Vehicle *AllocateVehicle(void) truelight@0: { truelight@1279: /* See note by ForceAllocateSpecialVehicle() why we skip the truelight@1279: * first blocks */ truelight@0: Vehicle *v; truelight@0: truelight@1279: FOR_ALL_VEHICLES_FROM(v, (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES) { truelight@1279: if (v->type == 0) truelight@1279: return InitializeVehicle(v); truelight@0: } truelight@0: truelight@1279: /* Check if we can add a block to the pool */ truelight@1279: if (AddBlockToPool(&_vehicle_pool)) truelight@1279: return AllocateVehicle(); truelight@1279: truelight@1279: return NULL; 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: tron@1093: void InitializeVehicles(void) truelight@0: { truelight@0: int i; truelight@0: truelight@1279: /* Clean the vehicle pool, and reserve enough blocks truelight@1279: * for the special vehicles, plus one for all the other truelight@1279: * vehicles (which is increased on-the-fly) */ truelight@1279: CleanPool(&_vehicle_pool); truelight@1279: AddBlockToPool(&_vehicle_pool); truelight@1279: for (i = 0; i < BLOCKS_FOR_SPECIAL_VEHICLES; i++) truelight@1279: AddBlockToPool(&_vehicle_pool); truelight@1279: truelight@0: // clear it... darkvater@395: memset(&_waypoints, 0, sizeof(_waypoints)); truelight@193: truelight@0: memset(_vehicle_position_hash, -1, sizeof(_vehicle_position_hash)); 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: tron@1093: Waypoint *AllocateWaypoint(void) 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: 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@1024: if (v->orders != NULL) truelight@1024: DeleteVehicleOrders(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: tron@1093: void CallVehicleTicks(void) 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 truelight@941: 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: { tron@1058: TileIndex tile = v->tile; truelight@0: tron@1058: if (IsTileType(tile, MP_STATION) || tron@1058: (v->type == VEH_Ship && ( tron@1058: IsTileType(TILE_ADDXY(tile, 1, 0), MP_STATION) || tron@1058: IsTileType(TILE_ADDXY(tile, -1, 0), MP_STATION) || tron@1058: IsTileType(TILE_ADDXY(tile, 0, 1), MP_STATION) || tron@1058: IsTileType(TILE_ADDXY(tile, 0, -1), MP_STATION) || tron@1058: IsTileType(TILE_ADDXY(tile, -2, 0), MP_STATION) tron@1058: ))) { 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); tron@1035: if (!IsTileType(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: tron@1364: static void BulldozerInit(Vehicle *v) truelight@0: { tron@1364: v->cur_image = SPR_BULLDOZER_NE; truelight@0: v->progress = 0; truelight@0: v->u.special.unk0 = 0; truelight@0: v->u.special.unk2 = 0; truelight@0: } truelight@0: tron@1364: typedef struct BulldozerMovement { tron@1364: byte image:2; tron@1364: byte direction:2; tron@1364: byte duration:3; tron@1364: } BulldozerMovement; 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@1364: if ((++v->progress & 7) == 0) { tron@1364: const BulldozerMovement* b = &_bulldozer_movement[v->u.special.unk0]; 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: tron@1364: v->u.special.unk2++; tron@1364: if (v->u.special.unk2 < b->duration) { truelight@0: v->u.special.unk2 = 0; truelight@0: v->u.special.unk0++; tron@1364: if (v->u.special.unk0 == lengthof(_bulldozer_movement)) { 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); tron@1035: if (IsTileType(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, tron@1364: BulldozerInit, 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, tron@1364: BulldozerTick, truelight@0: EffectTick_9, 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: 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: tron@1359: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type) truelight@0: { truelight@0: return CreateEffectVehicle(x, y, GetSlopeZ(x, 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: 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; bjarni@1237: Vehicle *v, *u; bjarni@1060: int cost, build_cost, rear_engine_cost = 0; bjarni@1237: byte old_engine_type; darkvater@889: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; tron@915: bjarni@1237: v = u = GetVehicle(p1); bjarni@1237: bjarni@1237: old_engine_type = v->engine_type; 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@1185: // makes sure that we do not replace a plane with a helicopter or vise versa bjarni@1187: if (v->type == VEH_Aircraft) { bjarni@1187: if (HASBIT(AircraftVehInfo(old_engine_type)->subtype, 0) != HASBIT(AircraftVehInfo(new_engine_type)->subtype, 0)) return CMD_ERROR; bjarni@1187: } bjarni@1185: bjarni@1187: // makes sure that the player can actually buy the new engine. Renewing is still allowed to outdated engines bjarni@1187: if (!HASBIT(DEREF_ENGINE(new_engine_type)->player_avail, v->owner) && old_engine_type != new_engine_type) return CMD_ERROR; 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@1060: if (old_engine_type == new_engine_type && v->age == 0) truelight@812: return CMD_ERROR; tron@1109: bjarni@1060: if ( v->type == VEH_Train ) { bjarni@1060: u = GetLastVehicleInChain(v); bjarni@1060: if ( RailVehInfo(new_engine_type)->flags & RVI_MULTIHEAD ) bjarni@1060: build_cost = build_cost >> 1; //multiheaded engines have EstimateTrainCost() for both engines tron@1109: bjarni@1060: if ( old_engine_type != new_engine_type ) { tron@1109: bjarni@1060: // prevent that the rear engine can get replaced to something else than the front engine bjarni@1060: if ( v->u.rail.first_engine != 0xffff && RailVehInfo(old_engine_type)->flags & RVI_MULTIHEAD && RailVehInfo(old_engine_type)->flags ) { bjarni@1060: Vehicle *first = GetFirstVehicleInChain(v); bjarni@1060: if ( first->engine_type != new_engine_type ) return CMD_ERROR; bjarni@1060: } tron@1109: bjarni@1060: // checks if the engine is the first one bjarni@1060: if ( v->u.rail.first_engine == 0xffff ) { bjarni@1060: if ( RailVehInfo(new_engine_type)->flags & RVI_MULTIHEAD ) { bjarni@1060: if ( u->engine_type == old_engine_type && v->next != NULL) { bjarni@1060: rear_engine_cost = build_cost - u->value; bjarni@1060: } else { bjarni@1060: rear_engine_cost = build_cost; bjarni@1060: } bjarni@1060: } else { bjarni@1060: if ( u->engine_type == old_engine_type && RailVehInfo(old_engine_type)->flags & RVI_MULTIHEAD) { darkvater@1068: if (v->next != NULL) rear_engine_cost = -(int32)u->value; bjarni@1060: } bjarni@1060: } bjarni@1060: } bjarni@1060: } bjarni@1060: } 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@1060: if ( DEREF_PLAYER(v->owner)->money64 < (int32)(autorefit_money + build_cost + rear_engine_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: } bjarni@1060: cost = build_cost - v->value + rear_engine_cost; truelight@812: truelight@812: truelight@812: if (flags & DC_EXEC) { bjarni@1060: /* We do not really buy a new vehicle, we upgrade the old one */ truelight@812: Engine *e; bjarni@907: e = DEREF_ENGINE(new_engine_type); tron@915: bjarni@1139: v->set_for_replacement = false; bjarni@1060: v->reliability = e->reliability; bjarni@1060: v->reliability_spd_dec = e->reliability_spd_dec; bjarni@1060: v->age = 0; truelight@812: bjarni@1060: v->date_of_last_service = _date; bjarni@1060: v->build_year = _cur_year; bjarni@1060: bjarni@1060: v->value = build_cost; bjarni@1060: bjarni@1060: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); bjarni@1060: tron@1109: truelight@812: if (v->engine_type != new_engine_type) { bjarni@933: byte sprite = v->spritenum; 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: { bjarni@842: const RailVehicleInfo *rvi = RailVehInfo(new_engine_type); bjarni@1060: const RailVehicleInfo *rvi2 = RailVehInfo(old_engine_type); bjarni@842: byte capacity = rvi->capacity; bjarni@1060: Vehicle *first = GetFirstVehicleInChain(v); bjarni@842: bjarni@1128: //if (v->owner == _local_player) InvalidateWindowClasses(WC_TRAINS_LIST); bjarni@933: /* rvi->image_index is the new sprite for the engine. Adding +1 makes the engine head the other way bjarni@933: if it is a multiheaded engine (rear engine) bjarni@939: (rvi->flags & RVI_MULTIHEAD && sprite - rvi2->image_index) is true if the engine is heading the other way, otherwise 0*/ bjarni@939: v->spritenum = rvi->image_index + (( rvi->flags & RVI_MULTIHEAD && sprite - rvi2->image_index) ? 1 : 0); truelight@941: bjarni@939: // turn the last engine in a multiheaded train if needed bjarni@1060: if ( v->next == NULL && v->u.rail.first_engine != 0xffff && rvi->flags & RVI_MULTIHEAD && v->spritenum == rvi->image_index ) bjarni@939: v->spritenum++; truelight@956: 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: } tron@1109: bjarni@1060: if ( rvi2->flags & RVI_MULTIHEAD && !(rvi->flags & RVI_MULTIHEAD) && v->index == first->index) { bjarni@1060: if (old_engine_type == u->engine_type ) { tron@1063: Vehicle *w; tron@1063: bjarni@1060: u = GetLastVehicleInChain(v); tron@1063: w = GetPrevVehicleInChain(u); bjarni@1060: w->next = NULL; bjarni@1060: DeleteVehicle(u); bjarni@1060: } bjarni@1060: } tron@1109: bjarni@1060: if ( rvi->flags & RVI_MULTIHEAD && rvi2->flags & RVI_MULTIHEAD && v->index == first->index ) { bjarni@1060: CmdReplaceVehicle(x, y, flags, u->index, p2); bjarni@1060: } tron@1109: bjarni@1060: if ( rvi->flags & RVI_MULTIHEAD && !(rvi2->flags & RVI_MULTIHEAD) && v->index == first->index ) { bjarni@1060: if ( old_engine_type != u->engine_type ) { bjarni@1060: Vehicle *w; bjarni@1060: if ( (w=AllocateVehicle()) != NULL ) { bjarni@1060: AddRearEngineToMultiheadedTrain(v,w, false); bjarni@1060: u->next = w; bjarni@1060: } bjarni@1060: } bjarni@1060: } tron@1109: bjarni@1062: // updates the id of the front engine in the other units, since the front engine just got a new engine_id bjarni@1062: // this is needed for wagon override bjarni@1062: if ( v->u.rail.first_engine == 0xffff && v->next != NULL ) { bjarni@1062: Vehicle *veh = v->next; bjarni@1062: do { bjarni@1062: veh->u.rail.first_engine = new_engine_type; bjarni@1062: } while ( (veh=veh->next) != NULL ); bjarni@1062: } bjarni@1128: InvalidateWindowClasses(WC_TRAINS_LIST); bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Road: bjarni@1060: { 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@1128: InvalidateWindowClasses(WC_ROADVEH_LIST); bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Ship: bjarni@1060: { 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@1128: InvalidateWindowClasses(WC_SHIPS_LIST); bjarni@842: break; bjarni@842: } bjarni@842: case VEH_Aircraft: bjarni@1060: { 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@1128: InvalidateWindowClasses(WC_AIRCRAFT_LIST); 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: } bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, v->type); bjarni@1128: ResortVehicleLists(); 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: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: truelight@0: if (!CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: tron@1328: str = AllocateNameUnique((const char*)_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: /* 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; tron@1214: uint32 result = _tile_type_procs[GetTileType(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) { tron@1214: VehicleLeaveTileProc *proc = _tile_type_procs[GetTileType(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@1282: UnitID GetFreeUnitNumber(byte type) truelight@0: { truelight@1282: UnitID 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@938: SLE_REF(Vehicle,next, REF_VEHICLE_OLD), truelight@0: SLE_VAR(Vehicle,string_id, SLE_STRINGID), truelight@1282: SLE_CONDVAR(Vehicle,unitnumber, SLE_FILE_U8 | SLE_VAR_U16, 0, 7), truelight@1282: SLE_CONDVAR(Vehicle,unitnumber, SLE_UINT16, 8, 255), truelight@0: SLE_VAR(Vehicle,owner, SLE_UINT8), tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_UINT32, 6, 255), tron@1174: SLE_CONDVAR(Vehicle,dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,dest_tile, SLE_UINT32, 6, 255), truelight@0: tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_UINT32, 6, 255), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_UINT32, 6, 255), 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@1266: SLE_CONDVAR(Vehicle,cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6), truelight@1266: SLE_CONDVAR(Vehicle,cargo_source, SLE_UINT16, 7, 255), 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), truelight@956: truelight@956: /* This next line is for version 4 and prior compatibility.. it temporarily reads truelight@956: type and flags (which were both 4 bits) into type. Later on this is truelight@956: converted correctly */ truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, type), SLE_UINT8, 0, 4), truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, station), SLE_FILE_U8 | SLE_VAR_U16, 0, 4), truelight@956: truelight@956: /* Orders for version 5 and on */ truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, type), SLE_UINT8, 5, 255), truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, flags), SLE_UINT8, 5, 255), truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, station), SLE_UINT16, 5, 255), truelight@956: truelight@1024: SLE_REF(Vehicle,orders, REF_ORDER), 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: truelight@1024: SLE_REF(Vehicle,next_shared, REF_VEHICLE), truelight@1024: SLE_REF(Vehicle,prev_shared, REF_VEHICLE), truelight@1024: truelight@1024: // reserve extra space in savegame here. (currently 10 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_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: celestar@1217: SLE_CONDREFX(offsetof(Vehicle,u)+offsetof(VehicleRoad,slot), REF_ROADSTOPS, 6, 255), celestar@1217: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,slotindex), SLE_UINT8, 6, 255), celestar@1217: SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,slot_age), SLE_UINT8, 6, 255), 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: tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_UINT32, 6, 255), truelight@0: tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_INT32, 6, 255), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_INT32, 6, 255), 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@938: SLE_REF(Vehicle,next, REF_VEHICLE_OLD), truelight@0: truelight@0: SLE_VAR(Vehicle,subtype, SLE_UINT8), tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,tile, SLE_UINT32, 6, 255), tron@1174: SLE_CONDVAR(Vehicle,dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,dest_tile, SLE_UINT32, 6, 255), truelight@0: tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,x_pos, SLE_INT32, 6, 255), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5), tron@1174: SLE_CONDVAR(Vehicle,y_pos, SLE_INT32, 6, 255), 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), truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, station), SLE_FILE_U8 | SLE_VAR_U16, 0, 4), truelight@956: SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, station), SLE_UINT16, 5, 255), 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. tron@1093: static void Save_VEHS(void) truelight@0: { truelight@0: Vehicle *v; truelight@0: // Write the vehicles truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type != 0) { truelight@0: SlSetArrayIndex(v->index); 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. tron@1093: static void Load_VEHS(void) truelight@0: { truelight@0: int index; truelight@0: Vehicle *v; truelight@0: truelight@0: while ((index = SlIterateArray()) != -1) { truelight@1279: Vehicle *v; truelight@919: truelight@1279: if (!AddBlockIfNeeded(&_vehicle_pool, index)) truelight@1279: error("Vehicles: failed loading savegame: too many vehicles"); truelight@1279: truelight@1279: v = GetVehicle(index); truelight@0: SlObject(v, _veh_descs[SlReadByte()]); truelight@1279: 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@956: truelight@956: if (_sl.version < 5) { truelight@956: /* Convert the current_order.type (which is a mix of type and flags, because truelight@956: in those versions, they both were 4 bits big) to type and flags */ truelight@956: v->current_order.flags = (v->current_order.type & 0xF0) >> 4; truelight@956: v->current_order.type = v->current_order.type & 0x0F; truelight@956: } 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: bjarni@1067: if (v->type != VEH_Train || v->subtype != TS_Front_Engine) 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@1024: /* Check for shared order-lists (we now use pointers for that) */ truelight@1024: if (_sl.full_version < 0x502) { truelight@1024: FOR_ALL_VEHICLES(v) { truelight@1024: Vehicle *u; truelight@1024: truelight@1024: if (v->type == 0) truelight@1024: continue; truelight@1024: truelight@1024: FOR_ALL_VEHICLES_FROM(u, v->index + 1) { truelight@1024: if (u->type == 0) truelight@1024: continue; truelight@1024: truelight@1024: /* If a vehicle has the same orders, add the link to eachother truelight@1024: in both vehicles */ truelight@1024: if (v->orders == u->orders) { truelight@1024: v->next_shared = u; truelight@1024: u->prev_shared = v; truelight@1024: break; truelight@1024: } truelight@1024: } truelight@1024: } truelight@1024: } truelight@1024: truelight@919: /* This is to ensure all pointers are within the limits of truelight@919: _vehicles_size */ truelight@1279: if (_vehicle_id_ctr_day >= GetVehiclePoolSize()) truelight@919: _vehicle_id_ctr_day = 0; truelight@0: } truelight@0: darkvater@395: static const byte _waypoint_desc[] = { tron@1174: SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, 255), 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: tron@1093: static void Save_CHKP(void) 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: tron@1093: static void Load_CHKP(void) 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: const ChunkHandler _veh_chunk_handlers[] = { truelight@0: { 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY}, truelight@0: { 'CHKP', Save_CHKP, Load_CHKP, CH_ARRAY | CH_LAST}, truelight@0: }; truelight@0: truelight@0: