diff --git a/src/ride/ride.c b/src/ride/ride.c index 7364537a3e..78bae0b6a4 100644 --- a/src/ride/ride.c +++ b/src/ride/ride.c @@ -4220,6 +4220,272 @@ static int sub_69ED9E() return max(0, miscSpriteCount + unkCount - 300); } +const rct_xy16 word_9A3AB4[4] = { + { 0, 0 }, + { 0, -96 }, + { -96, -96 }, + { -96, 0 }, +}; + +const rct_xy16 word_9A2A60[] = { + { 0, 16 }, + { 16, 31 }, + { 31, 16 }, + { 16, 0 }, + { 16, 16 }, + { 64, 64 }, + { 64, -32 }, + { -32, -32 }, + { -32, 64 }, +}; + +/** + * + * rct2: 0x006DD90D + */ +rct_vehicle *vehicle_create_car( + int rideIndex, + int vehicleEntryIndex, + int carIndex, + int *x, + int *y, + int z, + rct_map_element *mapElement +) { + registers regs = { 0 }; + + rct_ride *ride = GET_RIDE(rideIndex); + rct_ride_type *rideEntry = GET_RIDE_ENTRY(ride->subtype); + rct_ride_type_vehicle *vehicleEntry = &rideEntry->vehicles[vehicleEntryIndex]; + + rct_vehicle *vehicle = (rct_vehicle*)create_sprite(1); + vehicle->sprite_identifier = SPRITE_IDENTIFIER_VEHICLE; + vehicle->ride = rideIndex; + vehicle->ride_subtype = ride->subtype; + + vehicle->vehicle_type = vehicleEntryIndex; + vehicle->is_child = carIndex == 0 ? 0 : 1; + vehicle->var_44 = ror32(vehicleEntry->var_04, 10) & 0xFFFF; + regs.edx = vehicleEntry->var_04 >> 1; + regs.ebx = carIndex - regs.edx; + vehicle->var_24 = regs.ebx; + if (!(vehicleEntry->var_14 & 0x4000)) { + regs.ebx -= regs.edx; + } + + // loc_6DD9A5: + vehicle->sprite_width = vehicleEntry->sprite_width; + vehicle->sprite_height_negative = vehicleEntry->sprite_height_negative; + vehicle->sprite_height_positive = vehicleEntry->sprite_height_positive; + vehicle->friction = vehicleEntry->car_friction; + vehicle->num_seats = vehicleEntry->num_seats; + vehicle->speed = vehicleEntry->powered_max_speed; + vehicle->acceleration = vehicleEntry->powered_acceleration; + vehicle->velocity = 0; + vehicle->var_2C = 0; + vehicle->var_4A = 0; + vehicle->var_4C = 0; + vehicle->var_4E = 0; + vehicle->var_B5 = 0; + vehicle->var_BA = 0; + vehicle->var_B6 = 0; + vehicle->var_B8 = 0; + vehicle->sound1_id = 0xFF; + vehicle->sound2_id = 0xFF; + vehicle->next_vehicle_on_train = SPRITE_INDEX_NULL; + vehicle->var_C4 = 0; + vehicle->var_C5 = 0; + vehicle->var_C8 = 0; + vehicle->var_CC = 255; + vehicle->var_1F = 0; + vehicle->var_20 = 0; + vehicle->var_D9 = 4; + vehicle->var_D8 = 4; + for (int i = 0; i < 32; i++) { + vehicle->peep[i] = SPRITE_INDEX_NULL; + } + + if (vehicleEntry->var_14 & 0x8000) { + // loc_6DDCA4: + vehicle->var_CD = 0; + int direction = mapElement->type & MAP_ELEMENT_DIRECTION_MASK; + *x += word_9A3AB4[direction].x; + *y += word_9A3AB4[direction].y; + z = mapElement->base_height * 8; + vehicle->track_x = *x; + vehicle->track_y = *y; + vehicle->track_z = z; + vehicle->current_station = (mapElement->properties.track.sequence & 0x70) << 4; + + z += RCT2_GLOBAL(0x0097D21A + (ride->type * 8), sint8); + + vehicle->var_36 = mapElement->properties.track.type << 2; + vehicle->var_34 = 0; + vehicle->status = 0; + vehicle->var_51 = 0; + vehicle->var_48 = 0; + + // loc_6DDD26: + do { + regs.al = scenario_rand() & 0x1E; + vehicle->sprite_direction = regs.al; + regs.eax = (regs.eax >> 5) & 0xFF; + regs.cx += regs.ax; + regs.eax = (regs.eax >> 16) & 0xFF; + regs.ax += *x; + } while (sub_6DD365(vehicle)); + + sprite_move(*x, *y, z, (rct_sprite*)vehicle); + } else { + regs.dl = 0; + if (vehicleEntry->var_14 & 0x1000) { + regs.dl = 1; + } + + if (vehicleEntry->var_14 & 0x4000) { + regs.dl = 5; + if (!(ride->num_vehicles & 1)) { + regs.dl = 6; + } + } + if (vehicleEntry->var_12 & 8) { + regs.dl = 9; + vehicle->var_D3 = 0; + vehicle->var_D4 = 0; + vehicle->var_D5 = 0; + } + if (vehicleEntry->var_12 & 0x10) { + if (!vehicle->is_child) { + regs.dl = 15; + } + } + if (vehicleEntry->var_12 & 0x20) { + regs.dl = 16; + } + vehicle->var_CD = regs.dl; + + vehicle->track_x = *x; + vehicle->track_y = *y; + + int direction = mapElement->type & MAP_ELEMENT_DIRECTION_MASK; + vehicle->sprite_direction = direction << 3; + + if (ride->type == RIDE_TYPE_SPACE_RINGS) { + direction = 4; + } else { + if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_16)) { + if (RCT2_GLOBAL(0x0097CC68 + (ride->type * 2), uint8) != 119) { + if (RCT2_GLOBAL(0x0097CC68 + (ride->type * 2), uint8) != 95) { + if (ride->type == RIDE_TYPE_ENTERPRISE) { + direction += 5; + } else { + direction = 4; + } + } + } + } + } + + *x += word_9A2A60[direction].x; + *y += word_9A2A60[direction].y; + vehicle->track_z = mapElement->base_height * 8; + + vehicle->current_station = map_get_station(mapElement); + z = mapElement->base_height * 8; + z += RCT2_GLOBAL(0x0097D21A + (ride->type * 8), uint8); + + sprite_move(*x, *y, z, (rct_sprite*)vehicle); + vehicle->var_36 = (mapElement->properties.track.type << 2) | (vehicle->sprite_direction >> 3); + vehicle->var_34 = 31; + if (vehicleEntry->var_12 & 8) { + vehicle->var_34 = 15; + } + vehicle->var_48 = 2; + if (vehicleEntry->var_12 & 0x40) { + if (mapElement->properties.track.colour & 4) { + vehicle->var_48 |= 0x800; + } + } + vehicle->status = VEHICLE_STATUS_MOVING_TO_END_OF_STATION; + vehicle->var_51 = 0; + } + + // loc_6DDD5E: + vehicle->num_peeps = 0; + vehicle->next_free_seat = 0; + return vehicle; +} + +/** + * + * rct2: 0x006DD84C + */ +train_ref vehicle_create_train(int rideIndex, int x, int y, int z, rct_map_element *mapElement) +{ + rct_ride *ride = GET_RIDE(rideIndex); + + uint8 trainLayout[42]; + ride_entry_get_train_layout(ride->subtype, ride->num_cars_per_train, trainLayout); + + int sx = x; + int sy = y; + + train_ref train = { NULL, NULL }; + for (int carIndex = 0; carIndex < ride->num_cars_per_train; carIndex++) { + rct_vehicle *car = vehicle_create_car(rideIndex, trainLayout[carIndex], carIndex, &sx, &sy, z, mapElement); + if (carIndex == 0) { + train.head = car; + } else { + // Link the previous car with this car + train.tail->next_vehicle_on_train = car->sprite_index; + train.tail->next_vehicle_on_ride = car->sprite_index; + car->prev_vehicle_on_ride = train.tail->sprite_index; + } + train.tail = car; + } + return train; +} + +void vehicle_create_trains(int rideIndex, int x, int y, int z, rct_map_element *mapElement) +{ + rct_ride *ride = GET_RIDE(rideIndex); + train_ref firstTrain, lastTrain; + + for (int vehicleIndex = 0; vehicleIndex < ride->num_vehicles; vehicleIndex++) { + if (ride_is_block_sectioned(ride)) { + // regs.ebx = 0; + } + train_ref train = vehicle_create_train(rideIndex, x, y, z, mapElement); + if (vehicleIndex == 0) { + firstTrain = train; + } else { + // Link the end of the previous train with the front of this train + lastTrain.tail->next_vehicle_on_ride = train.head->sprite_index; + train.head->prev_vehicle_on_ride = lastTrain.tail->sprite_index; + } + lastTrain = train; + + // Add train to ride vehicle list + move_sprite_to_list((rct_sprite*)train.head, SPRITE_LINKEDLIST_OFFSET_VEHICLE); + for (int i = 0; i < 32; i++) { + if (ride->vehicles[i] == SPRITE_INDEX_NULL) { + ride->vehicles[i] = train.head->sprite_index; + break; + } + } + } + + // Link the first train and last train together + firstTrain.head->prev_vehicle_on_ride = lastTrain.tail->sprite_index; + lastTrain.tail->next_vehicle_on_ride = firstTrain.head->sprite_index; +} + +rct_ride_type_vehicle *vehicle_get_vehicle_entry(rct_vehicle *vehicle) +{ + rct_ride_type *rideEntry = GET_RIDE_ENTRY(vehicle->ride_subtype); + return &rideEntry->vehicles[vehicle->vehicle_type]; +} + void vehicle_unset_var_48_b1(rct_vehicle *head) { uint16 spriteIndex; @@ -4305,6 +4571,141 @@ void ride_create_vehicles_find_first_block(rct_ride *ride, rct_xy_element *outXY outXYElement->element = firstElement; } +/** + * + * rct2: 0x006DDF9C + */ +void loc_6DDF9C(rct_ride *ride, rct_map_element *mapElement) +{ + registers regs; + rct_vehicle *train, *car; + + for (int i = 0; i < ride->num_vehicles; i++) { + train = GET_VEHICLE(ride->vehicles[i]); + if (i == 0) { + sub_6DAB4C(train, NULL); + vehicle_unset_var_48_b1(train); + continue; + } + + do { + mapElement->flags |= (1 << 5); + car = train; + while (true) { + car->velocity = 0; + car->var_2C = 0; + car->var_4A = 0; + car->var_24 += 13962; + + uint16 spriteIndex = car->next_vehicle_on_train; + if (spriteIndex == SPRITE_INDEX_NULL) { + break; + } + car = GET_VEHICLE(spriteIndex); + } + } while (sub_6DAB4C(train, NULL) & 0x400); + + mapElement->flags |= (1 << 5); + car = train; + while (true) { + car->var_48 &= ~(1 << 1); + car->status = VEHICLE_STATUS_TRAVELLING; + regs.ax = car->var_36 >> 2; + if (regs.al == 1) { + car->status = VEHICLE_STATUS_MOVING_TO_END_OF_STATION; + } + + uint16 spriteIndex = car->next_vehicle_on_train; + if (spriteIndex == SPRITE_INDEX_NULL) { + break; + } + car = GET_VEHICLE(spriteIndex); + } + } +} + +/** + * + * rct2: 0x006DD84C + */ +bool ride_create_vehicles(rct_ride *ride, int rideIndex, rct_xy_element *element, int isApplying) +{ + // bool b = !(RCT2_CALLPROC_X(0x006DD84C, element->x, isApplying, element->y, rideIndex, (int)ride, (int)element->element, 0) & 0x100); + // return b; + + ride_update_max_vehicles(rideIndex); + if (ride->subtype == 0xFF) { + return true; + } + + // Check if there are enough free sprite slots for all the vehicles + int totalCars = ride->num_vehicles * ride->num_cars_per_train; + if (totalCars > sub_69ED9E()) { + RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id) = STR_UNABLE_TO_CREATE_ENOUGH_VEHICLES; + return false; + } + + if (!isApplying) { + return true; + } + + rct_map_element *mapElement = element->element; + int x = element->x; + int y = element->y; + int z = element->element->base_height; + int direction = mapElement->type & MAP_ELEMENT_DIRECTION_MASK; + + // + if (ride->mode == RIDE_MODE_STATION_TO_STATION) { + x = element->x - TileDirectionDelta[direction].x; + y = element->y - TileDirectionDelta[direction].y; + + mapElement = map_get_first_element_at(x >> 5, y >> 5); + do { + if (mapElement->base_height != z) continue; + if (map_element_get_type(mapElement) != MAP_ELEMENT_TYPE_TRACK) continue; + break; + } while (!map_element_is_last_for_tile(mapElement++)); + + z = mapElement->base_height; + direction = mapElement->type & MAP_ELEMENT_DIRECTION_MASK; + } + + vehicle_create_trains(rideIndex, x, y, z, mapElement); + // return true; + + // Initialise station departs +// 006DDDD0: + ride->lifecycle_flags |= RIDE_LIFECYCLE_ON_TRACK; + for (int i = 0; i < 4; i++) { + ride->station_depart[i] = (ride->station_depart[i] & 0x80) | 1; + } + + // + if (ride->type != RIDE_TYPE_SPACE_RINGS && !ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_16)) { + if (ride_is_block_sectioned(ride)) { + rct_xy_element firstBlock; + ride_create_vehicles_find_first_block(ride, &firstBlock); + loc_6DDF9C(ride, firstBlock.element); + } else { + for (int i = 0; i < ride->num_vehicles; i++) { + rct_vehicle *vehicle = GET_VEHICLE(ride->vehicles[i]); + + rct_ride_type *rideType = GET_RIDE_ENTRY(vehicle->vehicle_type); + rct_ride_type_vehicle *vehicleEntry = vehicle_get_vehicle_entry(vehicle); + + if (!(vehicleEntry->var_14 & 0x8000)) { + sub_6DAB4C(vehicle, NULL); + } + + vehicle_unset_var_48_b1(vehicle); + } + } + } + ride_update_vehicle_colours(rideIndex); + return true; +} + /** * * rct2: 0x006DDF9C diff --git a/src/ride/track.c b/src/ride/track.c index 5ba6a141de..e17b436b65 100644 --- a/src/ride/track.c +++ b/src/ride/track.c @@ -5149,4 +5149,4 @@ bool track_element_is_covered(int trackElementType) default: return false; } -} +} \ No newline at end of file diff --git a/src/ride/vehicle.c b/src/ride/vehicle.c index 360f472835..f51e93c51e 100644 --- a/src/ride/vehicle.c +++ b/src/ride/vehicle.c @@ -1186,8 +1186,6 @@ static void vehicle_update(rct_vehicle *vehicle) vehicle_update_sound(vehicle); } - - /** * * rct2: 0x006D7BCC @@ -8325,4 +8323,4 @@ void vehicle_invalidate_window(rct_vehicle *vehicle) return; window_invalidate(w); -} +} \ No newline at end of file diff --git a/src/world/map.c b/src/world/map.c index 0395d83faf..8675b1e291 100644 --- a/src/world/map.c +++ b/src/world/map.c @@ -5135,4 +5135,4 @@ rct_map_element *map_get_track_element_at_of_type_seq(int x, int y, int z, int t } while (!map_element_is_last_for_tile(mapElement++)); return NULL; -} +} \ No newline at end of file