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

Add: [Script] Game script control of industry production level. #11141

Merged
merged 2 commits into from Sep 2, 2023

Conversation

michicc
Copy link
Member

@michicc michicc commented Jul 16, 2023

Motivation / Problem

Game scripts can override industry production in a limited way. They can prohibit production increases and/or decreases and prevent closure. But they can't actually change production, just hold it in place.

This half-way control isn't very useful if a GS wants to implement custom industry production rules.

Description

This PR adds GS API methods to get and set the production multiplier of an industry. NewGRFs are notified by a new flag in var 47 (GameScript control status). Setting the production multiplier does not prohibit industry closure, GS can use the existing control flag for this.

A script can optionally let OTTD generate a production change news message if needed.

Limitations

I'm not sure about the actual GS API. Maybe it would be better to use a magic prod_level value to restore the default production behaviour instead of the need to clear the control flag.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@michicc michicc added needs review: NewGRF Review requested from a NewGRF expert needs review: Script API Review requested from GS/AI expert labels Jul 16, 2023
@andythenorth
Copy link
Contributor

andythenorth commented Jul 16, 2023

Works for me in initial (limited) test.

News message would be useful, but a separate call to GSNews.Create() does work. However, we do lack an appropriate GSNews::NewsType. NT_GENERAL and NT_ECONOMY aren't the right ones.

OpenTTD/src/news_type.h

Lines 21 to 39 in 9c262b0

enum NewsType : byte {
NT_ARRIVAL_COMPANY, ///< First vehicle arrived for company
NT_ARRIVAL_OTHER, ///< First vehicle arrived for competitor
NT_ACCIDENT, ///< An accident or disaster has occurred
NT_ACCIDENT_OTHER, ///< An accident or disaster has occurred
NT_COMPANY_INFO, ///< Company info (new companies, bankruptcy messages)
NT_INDUSTRY_OPEN, ///< Opening of industries
NT_INDUSTRY_CLOSE, ///< Closing of industries
NT_ECONOMY, ///< Economic changes (recession, industry up/dowm)
NT_INDUSTRY_COMPANY,///< Production changes of industry serviced by local company
NT_INDUSTRY_OTHER, ///< Production changes of industry serviced by competitor(s)
NT_INDUSTRY_NOBODY, ///< Other industry production changes
NT_ADVICE, ///< Bits of news about vehicles of the company
NT_NEW_VEHICLES, ///< New vehicle has become available
NT_ACCEPTANCE, ///< A type of cargo is (no longer) accepted
NT_SUBSIDIES, ///< News about subsidies (announcements, expirations, acceptance)
NT_GENERAL, ///< General news (from towns)
NT_END, ///< end-of-array marker
};

@michicc michicc force-pushed the pr/gs_prod_level branch 2 times, most recently from 8d1b9e6 to 0f331eb Compare July 16, 2023 21:24
@andythenorth
Copy link
Contributor

andythenorth commented Jul 16, 2023

Tested the version with news text option. Works, but the default news texts aren't going to be appropriate in most contexts (limited to 'expected to double', or 'falls by 50%') 😄
image

@andythenorth
Copy link
Contributor

andythenorth commented Jul 17, 2023

With the news flag set to false, seems a news message is triggered appropriately by the game without intervention?

(News log shows farm constructed, a news message following production increase by GS, and then GS posting a separate 'cabbage' message as a test).

EDIT: this was caused by an outdated version of FIRS grf which was triggering the news messages.

GSIndustry.SetProductionLevel(industry, GSIndustry.GetProductionLevel(industry) + 8, false);
// we issue the news message ourselves as of Jul 2023, this may or may not be needed depending on https://github.com/OpenTTD/OpenTTD/pull/11141
GSNews.Create(GSNews.NT_GENERAL, "Cabbage", GSCompany.COMPANY_INVALID, GSNews.NR_INDUSTRY, industry);
image

This comment doesn't have any effect on the (usually) inappropriate news messages emitted when news flag is set true.

@michicc michicc marked this pull request as ready for review July 18, 2023 18:34
@michicc
Copy link
Member Author

michicc commented Aug 6, 2023

Now with a custom news message option.

@andythenorth
Copy link
Contributor

andythenorth commented Aug 21, 2023

News message works, thanks :)

If show_news is false, null (or similar) must be passed as the parameter for custom_news.
Haven't checked if we have optional parameters for GS, but eh, possibly the docstring should mention this?

Copy link
Member

@2TallTyler 2TallTyler left a comment

Choose a reason for hiding this comment

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

I agree, the docstring should explain more explicitly what argument a GS author should pass if no news message is desired.

@michicc
Copy link
Member Author

michicc commented Aug 21, 2023

So something like If set to true and the production changed, generate a production change news message. If set to false, no news message is shown.?

If show_news is false, null (or similar) must be passed as the parameter for custom_news.

If show_news is false, any custom news text is simply ignored, so it can be null or some text. Unless you found some actual code bug where a message is still displayed.

@andythenorth
Copy link
Contributor

andythenorth commented Aug 21, 2023

If show_news is false, any custom news text is simply ignored, so it can be null or some text. Unless you found some actual code bug where a message is still displayed.

I verified that doesn't happen, show_news=false works as expected :)

@michicc michicc merged commit 0089323 into OpenTTD:master Sep 2, 2023
20 checks passed
@michicc michicc deleted the pr/gs_prod_level branch September 2, 2023 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review: NewGRF Review requested from a NewGRF expert needs review: Script API Review requested from GS/AI expert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants