Feature: Influence industry production changes from GS (#7912)

This commit is contained in:
Niels Martin Hansen
2020-12-22 14:21:31 +01:00
committed by GitHub
parent 547e5fdb65
commit b7751c483e
11 changed files with 185 additions and 2 deletions

View File

@@ -21,6 +21,10 @@
* \li GSEventStoryPageButtonClick
* \li GSEventStoryPageTileSelect
* \li GSEventStoryPageVehicleSelect
* \li GSIndustry::GetCargoLastAcceptedDate
* \li GSIndustry::GetControlFlags
* \li GSIndustry::GetLastProductionYear
* \li GSIndustry::SetControlFlags
* \li GSStoryPage::MakePushButtonReference
* \li GSStoryPage::MakeTileButtonReference
* \li GSStoryPage::MakeVehicleButtonReference

View File

@@ -57,6 +57,7 @@ public:
/* Note: these values represent part of the in-game CargoTypes enum */
CT_AUTO_REFIT = ::CT_AUTO_REFIT, ///< Automatically choose cargo type when doing auto-refitting.
CT_NO_REFIT = ::CT_NO_REFIT, ///< Do not refit cargo of a vehicle.
CT_INVALID = ::CT_INVALID, ///< An invalid cargo type.
};
/**

View File

@@ -16,6 +16,7 @@
#include "../../station_base.h"
#include "../../newgrf_industries.h"
#include "table/strings.h"
#include <numeric>
#include "../../safeguards.h"
@@ -204,3 +205,39 @@
return ::Industry::Get(industry_id)->type;
}
int32 ScriptIndustry::GetLastProductionYear(IndustryID industry_id)
{
Industry *i = Industry::GetIfValid(industry_id);
if (i == nullptr) return 0;
return i->last_prod_year;
}
ScriptDate::Date ScriptIndustry::GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type)
{
Industry *i = Industry::GetIfValid(industry_id);
if (i == nullptr) return ScriptDate::DATE_INVALID;
if (cargo_type == CT_INVALID) {
return (ScriptDate::Date)std::accumulate(std::begin(i->last_cargo_accepted_at), std::end(i->last_cargo_accepted_at), 0, [](Date a, Date b) { return std::max(a, b); });
} else {
int index = i->GetCargoAcceptedIndex(cargo_type);
if (index < 0) return ScriptDate::DATE_INVALID;
return (ScriptDate::Date)i->last_cargo_accepted_at[index];
}
}
uint32 ScriptIndustry::GetControlFlags(IndustryID industry_id)
{
Industry *i = Industry::GetIfValid(industry_id);
if (i == nullptr) return 0;
return i->ctlflags;
}
bool ScriptIndustry::SetControlFlags(IndustryID industry_id, uint32 control_flags)
{
if (ScriptObject::GetCompany() != OWNER_DEITY) return false;
if (!IsValidIndustry(industry_id)) return false;
return ScriptObject::DoCommand(0, industry_id, 0 | ((control_flags & ::INDCTL_MASK) << 8), CMD_INDUSTRY_CTRL);
}

View File

@@ -11,6 +11,8 @@
#define SCRIPT_INDUSTRY_HPP
#include "script_object.hpp"
#include "script_date.hpp"
#include "../../industry.h"
/**
* Class that handles all industry related functions.
@@ -25,6 +27,27 @@ public:
CAS_TEMP_REFUSED, ///< The industry temporarily refuses to accept this CargoID but may do so again in the future.
};
/**
* Control flags for industry
* @api -ai
*/
enum IndustryControlFlags {
/**
* When industry production change is evaluated, rolls to decrease are ignored.
* This also prevents industry closure due to production dropping to the lowest level.
*/
INDCTL_NO_PRODUCTION_DECREASE = ::INDCTL_NO_PRODUCTION_DECREASE,
/**
* When industry production change is evaluated, rolls to increase are ignored.
*/
INDCTL_NO_PRODUCTION_INCREASE = ::INDCTL_NO_PRODUCTION_INCREASE,
/**
* Industry can not close regardless of production level or time since last delivery.
* This does not prevent a closure already announced.
*/
INDCTL_NO_CLOSURE = ::INDCTL_NO_CLOSURE,
};
/**
* Gets the number of industries.
* @return The number of industries.
@@ -196,6 +219,46 @@ public:
* @return The IndustryType of the industry.
*/
static IndustryType GetIndustryType(IndustryID industry_id);
/**
* Get the last year this industry had any production output.
* @param industry_id The index of the industry.
* @pre IsValidIndustry(industry_id).
* @return Year the industry last had production, 0 if error.
* @api -ai
*/
static int32 GetLastProductionYear(IndustryID industry_id);
/**
* Get the last date this industry accepted any cargo delivery.
* @param industry_id The index of the industry.
* @param cargo_type The cargo to query, or CT_INVALID to query latest of all accepted cargoes.
* @pre IsValidIndustry(industry_id).
* @pre IsValidCargo(cargo_type) || cargo_type == CT_INVALID.
* @return Date the industry last received cargo from a delivery, or ScriptDate::DATE_INVALID on error.
* @api -ai
*/
static ScriptDate::Date GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type);
/**
* Get the current control flags for an industry.
* @param industry_id The index of the industry.
* @pre IsValidIndustry(industry_id).
* @return Bit flags of the IndustryControlFlags enumeration.
* @api -ai
*/
static uint32 GetControlFlags(IndustryID industry_id);
/**
* Change the control flags for an industry.
* @param industry_id The index of the industry.
* @param control_flags New flags as a combination of IndustryControlFlags values.
* @pre IsValidIndustry(industry_id).
* @pre No ScriptCompanyMode may be in scope.
* @return True if the action succeeded.
* @api -ai
*/
static bool SetControlFlags(IndustryID industry_id, uint32 control_flags);
};
#endif /* SCRIPT_INDUSTRY_HPP */