Skip to content

[Plugin] Fix track origin being miscalculated for plugins on downward slopes #24142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Basssiiie
Copy link
Member

@Basssiiie Basssiiie commented Apr 3, 2025

Hey all,

This PR fixes quite an obscure bug in the plugin API's TrackIterator and the recently added car.moveToTrack(), where if it selected a non-origin track element on a large downward slope (for example), it would report the z position of the selected non-origin track element as the z of the origin track piece. I unfortunately only found this today while attempting to wrap up the RVE implementation and not during testing of the car.moveToTrack() PR, so my apologies for that.

This issue happens with all pieces where the track piece origin has a non-zero z-offset, for example large downward slopes and downward turns. For reference, the downwards slope has 4 sequence parts:

  1. z = 80 (origin)
  2. z = 64
  3. z = 32
  4. z = 0

Except for these few track pieces, most of the other track pieces have a origin z of 0, and are thus not affected by this issue.

Example screenshot of the issue, with the numbered sequences and the vehicle having been moved to the track piece via sequence 3:
image

The fix adds the origin z to the equation to ensure this weird offset at the origin is accounted for, which brings it in line with how similar code works within the OpenRCT2 code base. The affected function here is only used by the ScTrackIterator and ScVehicle::moveToTrack(), so impact is minimal.

Thank you for your time. 🙂

@Gymnasiast
Copy link
Member

@Sadret Can you review this? (You are much more familiar with the ins and outs of the scripting system.)

@Sadret
Copy link
Contributor

Sadret commented Apr 4, 2025

@Sadret Can you review this? (You are much more familiar with the ins and outs of the scripting system.)

I am just barely aware that there are some anomalities with track z positions, and I have not yet worked with the TrackIterator api. So unfortunately I do not have anything to add here.

@Basssiiie
Copy link
Member Author

Oh I forgot, if anyone wants to test it, they can do so with this plugin:

Click to show plugin
/// <reference path="../bin/openrct2.d.ts" />
// @ts-check

registerPlugin({
	name: "Test track location",
	version: "1",
	authors: ['Basssiiie'],
	targetApiVersion: 82,
	type: "local",
	licence: "MIT",
	main() {
		ui.registerMenuItem("Test track location", function() {
			ui.activateTool({
				id: 'test-pick-vehicle',
				cursor: 'cross_hair',
				filter: ["entity"],
				onDown: function(args) {
					var entityId = args.entityId;
					if (entityId === undefined) return;
					ui.tool.cancel();
					console.log("Entity "+entityId+" selected");
					var entity = map.getEntity(entityId);
					var window = ui.openWindow({
						classification: "test-track-location",
						title: "Test track location",
						width: 200,
						height: 170,
						onUpdate: function()
						{
							var trackLocation = entity.trackLocation;
							window.findWidget("info-label").text =
								"Entity id: "+entityId
								+"\nTile: "+[Math.floor(trackLocation.x / 32), Math.floor(trackLocation.y / 32)].join(", ")
								+"\nTrack: "+[trackLocation.x, trackLocation.y, trackLocation.z].join(", ")
								+"\nDirection: "+trackLocation.direction
								+"\nType: "+trackLocation.trackType
								+"\nProgress: "+entity.trackProgress+" / "+context.getTrackSegment(trackLocation.trackType).getSubpositionLength(entity.subposition, trackLocation.direction);
						},
						widgets: [
							{
								name: "info-label",
								type: "label",
								x: 10,
								y: 25,
								width: 180,
								height: 15,
							},
							{
								name: "move-location-button",
								type: "button",
								x: 10,
								y: 125,
								width: 180,
								height: 15,
								text: "Move track location",
								onClick: function() {
									ui.activateTool({
										id: "test-move-track-location",
										cursor: "cross_hair",
										filter: ["ride"],
										onDown: function(args) {
											var coords = args.mapCoords;
											var index = args.tileElementIndex;
											if (!coords || index === undefined) return;
											ui.tool.cancel();
											var x = Math.floor(coords.x / 32);
											var y = Math.floor(coords.y / 32);
											console.log("Move location to", x, y, index);
											entity.moveToTrack(x, y, index);
										}
									})
								}
							},
							{
								name: "travel-by",
								type: "spinner",
								text: "Travel by",
								x: 10,
								y: 145,
								width: 180,
								height: 15,
								onDecrement: function() {
									console.log("Travel backwards by 1.000.000");
									entity.travelBy(-1000000)
								},
								onIncrement: function() {
									console.log("Travel forwards by 1.000.000");
									entity.travelBy(1000000)
								},
							},
						]
					})
				}
			})
		})
	}
});

@Gymnasiast
Copy link
Member

Can you rebase and then amend the changelog?

@Gymnasiast Gymnasiast force-pushed the plugin-fix-track-origin-on-downward-slopes branch from 09aa506 to 75de5b8 Compare April 9, 2025 17:18
@Gymnasiast Gymnasiast enabled auto-merge (squash) April 9, 2025 17:18
@Gymnasiast Gymnasiast added this to the v0.4.22 milestone Apr 9, 2025
@Gymnasiast Gymnasiast merged commit 0ae9848 into OpenRCT2:develop Apr 9, 2025
22 checks passed
AaronVanGeffen added a commit that referenced this pull request May 4, 2025
- Feature: [#24206] [Plugin] Add APIs for breaking down rides, reading the current breakdown, and for fixing broken down rides.
- Improved: [#20073] The OpenGL drawing engine now supports screen invalidation which avoids the redrawing of unchanged regions.
- Improved: [#21767] RCT Classic for macOS can now be used as the source game.
- Improved: [#23590] Title bars are now drawn bigger when “Enlarged UI” is enabled.
- Improved: [#23626] Add small, medium and large flat and sloped turns, S-bends and diagonal track to the Go-Karts.
- Improved: [#23982] The scenario objective window has been merged into the scenario options window.
- Improved: [#24233] RCT Classic+ from Apple Arcade can now be used as the source game, and is detected automatically.
- Improved: [#24260] Better performance on parks that have a lot of Guests and Entertainers.
- Improved: [#24319] RCT Classic installs via Steam are now detected automatically on Windows.
- Change: [#23803] Lightning strikes and thunder now happen at the same frequency independently of the game speed.
- Change: [#23857] Replace display options tab with custom sprites.
- Change: [#24069] [Plugin] Plugins are now available in the scenario editor and track designer.
- Change: [#24135] Compress Emscripten js/wasm files.
- Change: [#24194] The advanced options tab has been reworked to make it easier to find the RCT1 path setting.
- Change: [#24235] Small changes to RCT1 theme.
- Change: [#24236] Controls and Interface options now both have their own tabs in the Options window.
- Change: [#24308] “Software” and “Software (hardware display)” renderers have been merged into a single “Software” renderer.
- Change: [#24317] The scenery window now shows at least one row of scenery objects, even if there are multiple rows of tabs.
- Fix: [#18479] Tile elements ordered beneath water do not draw correctly.
- Fix: [#19782] Game stops counting inversions and golf holes after 31 (original bug).
- Fix: [#21207] Track List window gets positioned incorrectly.
- Fix: [#21919] Non-recolourable cars still show colour picker (original bug).
- Fix: [#22182] [Plugin] Crash when using map.getAllEntities("car").
- Fix: [#22634] Asset packs with sound effect overrides are not loaded correctly at startup.
- Fix: [#23108] Missing pieces on Hypercoaster and Hyper-Twister, even with the ‘all drawable track pieces’ cheat enabled.
- Fix: [#24013] Failure to load a scenario preview image (minimap) could lead to an uncaught exception error message.
- Fix: [#24045] [Plugin] Data storage is not cleared when converting save game to scenario.
- Fix: [#24121] Checkbox labels run beyond the edge of the window if they’re too long to fit.
- Fix: [#24142] [Plugin] Track origin is miscalculated on downward slopes.
- Fix: [#24220] Narrow station platforms have missing sides on certain rotations.
- Fix: [#24286] Steam installs of RCT1 and RCT2 are not autodetected on macOS.
- Fix: [#24310] [Plugin] Missing invalidation on various plugin api setters for entities.
ZehMatt added a commit that referenced this pull request May 25, 2025
[Plugin] Fix regression from #24142 breaking `ScTrackIterator` on specific track pieces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants