Skip to content
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

Expose suggestedGuestMaximum via a hook. #21521

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Harry-Hopkinson
Copy link
Contributor

@Harry-Hopkinson Harry-Hopkinson commented Mar 3, 2024

Exposes the suggestedGuestMaximum variable through the API - it used to only be a readonly variable. This adds the functionality for plugins like requested in #21507. I have created a demo using this functionality. Also implemented a hook called "park.calculateGuestCap" to handle setting this value.

@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from 83a759d to 738bebe Compare March 3, 2024 16:41
@Harry-Hopkinson Harry-Hopkinson changed the title Expose suggestedGuestMaximum to be edited through the API. Expose suggestedGuestMaximum in API Mar 3, 2024
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from a679eba to b37e54c Compare March 3, 2024 16:43
@Harry-Hopkinson Harry-Hopkinson marked this pull request as ready for review March 3, 2024 16:50
@Harry-Hopkinson Harry-Hopkinson marked this pull request as draft March 3, 2024 17:13
@Harry-Hopkinson
Copy link
Contributor Author

How could I stop the max guest calculations being run every 13 seconds as it overwrites the effects of the plugin?

https://github.com/OpenRCT2/OpenRCT2/blob/4ccecd6e680727b46dc8001c778707a49960b831/src/openrct2/world/Park.cpp#L299C5-L312C6

@ocalhoun6
Copy link
Contributor

Maybe the plugin code should go here instead?

uint32_t Park::CalculateSuggestedMaxGuests() const
{
uint32_t suggestedMaxGuests = 0;
uint32_t difficultGenerationBonus = 0;
auto& gameState = GetGameState();
for (auto& ride : GetRideManager())
{
if (ride.status != RideStatus::Open)
continue;
if (ride.lifecycle_flags & RIDE_LIFECYCLE_BROKEN_DOWN)
continue;
if (ride.lifecycle_flags & RIDE_LIFECYCLE_CRASHED)
continue;
// Add guest score for ride type
suggestedMaxGuests += ride.GetRideTypeDescriptor().BonusValue;
// If difficult guest generation, extra guests are available for good rides
if (gameState.ParkFlags & PARK_FLAGS_DIFFICULT_GUEST_GENERATION)
{
if (!(ride.lifecycle_flags & RIDE_LIFECYCLE_TESTED))
continue;
if (!ride.GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_TRACK))
continue;
if (!ride.GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_DATA_LOGGING))
continue;
if (ride.GetStation().SegmentLength < (600 << 16))
continue;
if (ride.excitement < RIDE_RATING(6, 00))
continue;
// Bonus guests for good ride
difficultGenerationBonus += ride.GetRideTypeDescriptor().BonusValue * 2;
}
}
if (gameState.ParkFlags & PARK_FLAGS_DIFFICULT_GUEST_GENERATION)
{
suggestedMaxGuests = std::min<uint32_t>(suggestedMaxGuests, 1000);
suggestedMaxGuests += difficultGenerationBonus;
}
suggestedMaxGuests = std::min<uint32_t>(suggestedMaxGuests, 65535);
return suggestedMaxGuests;
}

Then the plugin code would be part of that calculation being run every 13 seconds, so it could override that code every time it gets run.

Downside: when initially changing the soft guest cap, it might not update instantly, so you might have to wait up to 13 seconds for the change to be applied.

@duncanspumpkin
Copy link
Contributor

This was purposely made readonly as it shouldn't be settable from plugins.

@Harry-Hopkinson
Copy link
Contributor Author

Can you only edit the variable if it is not made readonly? Or can you edit the variable another way?

@fidwell
Copy link
Contributor

fidwell commented Mar 5, 2024

My idea when starting the discussion topic was that there would be some new separate variable. You would expose that to the API, let plugins set it, and then add it to the calculated soft guest cap internally. You'd have to save it to the park data though; not sure how easy that would be to add.

@ZehMatt
Copy link
Member

ZehMatt commented Mar 5, 2024

My idea when starting the discussion topic was that there would be some new separate variable. You would expose that to the API, let plugins set it, and then add it to the calculated soft guest cap internally. You'd have to save it to the park data though; not sure how easy that would be to add.

I think it might be best to just call a hook at this point, if the hook returns something take that value otherwise use whatever the builtin returns, I think this function isn't called as much so it should be safe from performance issues. I'm however not sure how hooks currently work when multiple plugins want to hook this function, but the same problem also exists with fighting over the variable I guess.

@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch 2 times, most recently from 146cee1 to f8c8bca Compare March 11, 2024 13:43
@Harry-Hopkinson Harry-Hopkinson marked this pull request as ready for review March 17, 2024 13:44
@Basssiiie
Copy link
Member

I'm not sure the current implementation of the hook is correct.

What if I want to double the suggested max, e.g. (suggestedMaximumByGame) => (suggestedMaximumByGame * 2), would this be a preferred scenario? It would not be possible with the current implementation.

For having multiple plugins hook it, it would also be nice if you could also get the current suggested max, so the hooks for multiple plugins can be executed in a chain, e.g.

  • Plugin A: (suggestedMaximum) => (suggestedMaximum * 2)
  • Plugin B: (suggestedMaximum) => (suggestedMaximum + (100 * amountOfEntertainers))
  • Plugin C: (suggestedMaximum) => (suggestedMaximum - (200 * amountOfHeartlineCoasters))

Of course, a plugin could still discard the original suggestion and override it completely, but it wouldn't be a necessity.

@Basssiiie Basssiiie self-requested a review March 23, 2024 11:50
@Harry-Hopkinson Harry-Hopkinson marked this pull request as draft March 26, 2024 12:44
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch 3 times, most recently from 7c54991 to 043f79f Compare April 11, 2024 16:27
@Harry-Hopkinson Harry-Hopkinson marked this pull request as ready for review April 13, 2024 07:25
Copy link
Member

@Basssiiie Basssiiie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply, I've been a bit busy!

distribution/openrct2.d.ts Outdated Show resolved Hide resolved
distribution/openrct2.d.ts Outdated Show resolved Hide resolved
distribution/openrct2.d.ts Outdated Show resolved Hide resolved
src/openrct2/world/Park.cpp Outdated Show resolved Hide resolved
@Basssiiie
Copy link
Member

Also might be due to my delay, but this PR will need a plugin API version increment. 🙂

@Basssiiie Basssiiie added the plugin api version Plugin API version needs updating - double check before merging! label Apr 21, 2024
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch 4 times, most recently from de82030 to a5219df Compare April 21, 2024 17:06
src/openrct2/world/Park.cpp Outdated Show resolved Hide resolved
src/openrct2/world/Park.cpp Outdated Show resolved Hide resolved
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from 70c7781 to 4631df9 Compare April 22, 2024 18:42
@Harry-Hopkinson Harry-Hopkinson changed the title Expose suggestedGuestMaximum in API Expose suggestedGuestMaximum via a hook. Apr 22, 2024
Copy link
Member

@Basssiiie Basssiiie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, thanks for the quick changes. I've tested it and it looks good to me! 🙂

image

For reference, click here for my test code
var frozenGuestCap = false;
var originalGuestCap = 0;
var modifiedGuestCap = 0;
var guestCapEvent;

registerPlugin({
	name: "Test for guest cap",
	version: "1",
	authors: ['Basssiiie'],
	targetApiVersion: 82,
	type: "local",
	licence: "MIT",
	main() {
		ui.registerMenuItem("Test guest cap", function() {
			originalGuestCap = park.suggestedGuestMaximum;
			var window = ui.openWindow({
				classification: "test-guest-cap",
				title: "Set guest cap",
				width: 200,
				height: 125,
				onUpdate: function()
				{
					var label1 = window.findWidget("original-guest-cap-label");
					var label2 = window.findWidget("current-guest-cap-label");
					label1.text = "Original guest cap: " + originalGuestCap;
					label2.text = "Current guest cap: " + park.suggestedGuestMaximum;
				},
				widgets: [
					{
						name: "original-guest-cap-label",
						type: "label",
						x: 10,
						y: 25,
						width: 180,
						height: 15,
					},
					{
						name: "current-guest-cap-label",
						type: "label",
						x: 10,
						y: 50,
						width: 180,
						height: 15,
					},
					{
						type: "checkbox",
						x: 10,
						y: 75,
						width: 180,
						height: 15,
						text: "Freeze guest cap",
						isChecked: frozenGuestCap,
						onChange: function(check)
						{
							frozenGuestCap = check;
							if (check)
							{
								guestCapEvent = context.subscribe("park.calculateGuestCap", function(evt)
								{
									originalGuestCap = evt.suggestedGuestMaximum;
									evt.suggestedGuestMaximum = modifiedGuestCap;
								})
							}
							else if (guestCapEvent)
							{
								guestCapEvent.dispose();
								guestCapEvent = null;
							}
						}
					},
					{
						type: "textbox",
						x: 10,
						y: 100,
						width: 180,
						height: 15,
						text: modifiedGuestCap.toString(),
						onChange: function(text)
						{
							modifiedGuestCap = Number.parseInt(text);
						}
					}
				]
			});
		})
	}
});

@Basssiiie Basssiiie removed the plugin api version Plugin API version needs updating - double check before merging! label Apr 22, 2024
@Harry-Hopkinson
Copy link
Contributor Author

Thanks for the help and patience.

@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from 4631df9 to 56db057 Compare April 29, 2024 17:08
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from 56db057 to ea6403d Compare May 17, 2024 20:09
@Harry-Hopkinson
Copy link
Contributor Author

Is there anything else that I need to implement for this PR - or is it all good?

Copy link
Contributor

@Sadret Sadret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested, but I like the implementation via the hook.

distribution/openrct2.d.ts Outdated Show resolved Hide resolved
distribution/openrct2.d.ts Outdated Show resolved Hide resolved
@Harry-Hopkinson Harry-Hopkinson force-pushed the expose-suggestedGuestMaximum-to-the-API branch from ea6403d to f429e7c Compare June 1, 2024 08:23
@Harry-Hopkinson
Copy link
Contributor Author

I have renamed CalculateGuestCapArgs to ParkCalculateGuestCapArgs

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.

None yet

7 participants