1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #include "../../interface/Viewport.h"
11 #include "../../paint/Paint.h"
12 #include "../../paint/Supports.h"
13 #include "../../world/Entity.h"
14 #include "../Track.h"
15 #include "../TrackPaint.h"
16 #include "../Vehicle.h"
17 
18 /** rct2: 0x0142805C */
19 static constexpr const uint32_t merry_go_round_rider_offsets[] = {
20     0, 32, 64, 96, 16, 48, 80, 112,
21 };
22 
23 /** rct2: 0x0142807C */
24 static constexpr const uint16_t merry_go_round_breakdown_vibration[] = {
25     0, 1, 2, 3, 4, 3, 2, 1, 0, 0,
26 };
27 
28 /**
29  * rct2: 0x0076287D
30  */
paint_merry_go_round_structure(paint_session * session,const Ride * ride,uint8_t direction,int8_t xOffset,int8_t yOffset,uint16_t height)31 static void paint_merry_go_round_structure(
32     paint_session* session, const Ride* ride, uint8_t direction, int8_t xOffset, int8_t yOffset, uint16_t height)
33 {
34     const TileElement* savedTileElement = static_cast<const TileElement*>(session->CurrentlyDrawnItem);
35     height += 7;
36 
37     if (ride == nullptr)
38         return;
39 
40     auto rideEntry = ride->GetRideEntry();
41     if (rideEntry == nullptr)
42         return;
43 
44     uint32_t baseImageId = rideEntry->vehicles[0].base_image_id;
45     auto vehicle = GetEntity<Vehicle>(ride->vehicles[0]);
46     if (ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK && vehicle != nullptr)
47     {
48         session->InteractionType = ViewportInteractionItem::Entity;
49         session->CurrentlyDrawnItem = vehicle;
50 
51         if (ride->lifecycle_flags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN)
52             && ride->breakdown_reason_pending == BREAKDOWN_CONTROL_FAILURE && ride->breakdown_sound_modifier >= 128)
53         {
54             height += merry_go_round_breakdown_vibration[(vehicle->current_time >> 1) & 7];
55         }
56     }
57 
58     uint32_t rotationOffset = 0;
59     if (vehicle != nullptr)
60     {
61         uint32_t rotation = ((vehicle->sprite_direction >> 3) + session->CurrentRotation) << 5;
62         rotationOffset = (vehicle->Pitch + rotation) % 128;
63     }
64 
65     uint32_t imageOffset = rotationOffset & 0x1F;
66 
67     uint32_t imageColourFlags = session->TrackColours[SCHEME_MISC];
68     if (imageColourFlags == IMAGE_TYPE_REMAP)
69     {
70         imageColourFlags = SPRITE_ID_PALETTE_COLOUR_2(ride->vehicle_colours[0].Body, ride->vehicle_colours[0].Trim);
71     }
72 
73     uint32_t imageId = (baseImageId + imageOffset) | imageColourFlags;
74     PaintAddImageAsParent(
75         session, imageId, { xOffset, yOffset, height }, { 24, 24, 48 }, { xOffset + 16, yOffset + 16, height });
76 
77     rct_drawpixelinfo* dpi = &session->DPI;
78     if (dpi->zoom_level <= 0 && ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK && vehicle != nullptr)
79     {
80         for (int32_t peep = 0; peep <= 14; peep += 2)
81         {
82             if (vehicle->num_peeps <= peep)
83             {
84                 break;
85             }
86 
87             imageOffset = (merry_go_round_rider_offsets[peep / 2] + rotationOffset) % 128;
88             imageOffset -= 13;
89 
90             if (imageOffset >= 68)
91             {
92                 continue;
93             }
94 
95             imageColourFlags = SPRITE_ID_PALETTE_COLOUR_2(
96                 vehicle->peep_tshirt_colours[peep], vehicle->peep_tshirt_colours[peep + 1]);
97             imageId = (baseImageId + 32 + imageOffset) | imageColourFlags;
98             PaintAddImageAsChild(session, imageId, xOffset, yOffset, 24, 24, 48, height, xOffset + 16, yOffset + 16, height);
99         }
100     }
101 
102     session->CurrentlyDrawnItem = savedTileElement;
103     session->InteractionType = ViewportInteractionItem::Ride;
104 }
105 
106 /**
107  * rct2: 0x00761B0C
108  */
paint_merry_go_round(paint_session * session,const Ride * ride,uint8_t trackSequence,uint8_t direction,int32_t height,const TrackElement & trackElement)109 static void paint_merry_go_round(
110     paint_session* session, const Ride* ride, uint8_t trackSequence, uint8_t direction, int32_t height,
111     const TrackElement& trackElement)
112 {
113     trackSequence = track_map_3x3[direction][trackSequence];
114 
115     int32_t edges = edges_3x3[trackSequence];
116 
117     wooden_a_supports_paint_setup(session, (direction & 1), 0, height, session->TrackColours[SCHEME_MISC]);
118 
119     StationObject* stationObject = nullptr;
120     if (ride != nullptr)
121         stationObject = ride_get_station_object(ride);
122 
123     track_paint_util_paint_floor(session, edges, session->TrackColours[SCHEME_TRACK], height, floorSpritesCork, stationObject);
124 
125     if (ride != nullptr)
126     {
127         track_paint_util_paint_fences(
128             session, edges, session->MapPosition, trackElement, ride, session->TrackColours[SCHEME_MISC], height,
129             fenceSpritesRope, session->CurrentRotation);
130     }
131 
132     switch (trackSequence)
133     {
134         case 1:
135             paint_merry_go_round_structure(session, ride, direction, 32, 32, height);
136             break;
137         case 3:
138             paint_merry_go_round_structure(session, ride, direction, 32, -32, height);
139             break;
140         case 5:
141             paint_merry_go_round_structure(session, ride, direction, 0, -32, height);
142             break;
143         case 6:
144             paint_merry_go_round_structure(session, ride, direction, -32, 32, height);
145             break;
146         case 7:
147             paint_merry_go_round_structure(session, ride, direction, -32, -32, height);
148             break;
149         case 8:
150             paint_merry_go_round_structure(session, ride, direction, -32, 0, height);
151             break;
152     }
153 
154     int32_t cornerSegments = 0;
155     switch (trackSequence)
156     {
157         case 1:
158             // top
159             cornerSegments = SEGMENT_B4 | SEGMENT_C8 | SEGMENT_CC;
160             break;
161         case 3:
162             // right
163             cornerSegments = SEGMENT_CC | SEGMENT_BC | SEGMENT_D4;
164             break;
165         case 6:
166             // left
167             cornerSegments = SEGMENT_C8 | SEGMENT_B8 | SEGMENT_D0;
168             break;
169         case 7:
170             // bottom
171             cornerSegments = SEGMENT_D0 | SEGMENT_C0 | SEGMENT_D4;
172             break;
173     }
174 
175     paint_util_set_segment_support_height(session, cornerSegments, height + 2, 0x20);
176     paint_util_set_segment_support_height(session, SEGMENTS_ALL & ~cornerSegments, 0xFFFF, 0);
177     paint_util_set_general_support_height(session, height + 64, 0x20);
178 }
179 
180 /**
181  * rct2: 0x0076190C
182  */
get_track_paint_function_merry_go_round(int32_t trackType)183 TRACK_PAINT_FUNCTION get_track_paint_function_merry_go_round(int32_t trackType)
184 {
185     if (trackType != TrackElemType::FlatTrack3x3)
186     {
187         return nullptr;
188     }
189 
190     return paint_merry_go_round;
191 }
192