summaryrefslogtreecommitdiff
path: root/src/engine.cpp
diff options
context:
space:
mode:
authorPavel Stupnikov <dp@dpointer.org>2020-01-15 20:46:26 +0300
committerNiels Martin Hansen <nielsm@indvikleren.dk>2020-01-15 18:46:26 +0100
commitd7a928a08b0d614bc06f4a6e8a275582a100599f (patch)
treeefd1163e334ea02f8ba5811df5ff4ca175c37374 /src/engine.cpp
parent4366f8e46a601aa1edaa5f5c3a7e41d453637b4c (diff)
downloadopenttd-d7a928a08b0d614bc06f4a6e8a275582a100599f.tar.xz
Feature: GS method to control engine availability for a specific company (#7791)
* Feature: GS method to allow company to use an engine before its introduction date * Feature: GS method to retire an engine early for a specific company
Diffstat (limited to 'src/engine.cpp')
-rw-r--r--src/engine.cpp79
1 files changed, 70 insertions, 9 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index 66e68fd3c..c3e9329fb 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -710,11 +710,11 @@ void StartupEngines()
}
/**
- * Company \a company accepts engine \a eid for preview.
- * @param eid Engine being accepted (is under preview).
- * @param company Current company previewing the engine.
+ * Allows engine \a eid to be used by a company \a company.
+ * @param eid The engine to enable.
+ * @param company The company to allow using the engine.
*/
-static void AcceptEnginePreview(EngineID eid, CompanyID company)
+static void EnableEngineForCompany(EngineID eid, CompanyID company)
{
Engine *e = Engine::Get(eid);
Company *c = Company::Get(company);
@@ -728,15 +728,45 @@ static void AcceptEnginePreview(EngineID eid, CompanyID company)
c->avail_roadtypes = AddDateIntroducedRoadTypes(c->avail_roadtypes | GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes, _date);
}
- e->preview_company = INVALID_COMPANY;
- e->preview_asked = (CompanyMask)-1;
if (company == _local_company) {
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
+
+ /* Update the toolbar. */
+ InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
+ if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
+ if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
}
+}
- /* Update the toolbar. */
- if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
- if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
+/**
+ * Forbids engine \a eid to be used by a company \a company.
+ * @param eid The engine to disable.
+ * @param company The company to forbid using the engine.
+ */
+static void DisableEngineForCompany(EngineID eid, CompanyID company)
+{
+ Engine *e = Engine::Get(eid);
+
+ ClrBit(e->company_avail, company);
+
+ if (company == _local_company) {
+ AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
+ }
+}
+
+/**
+ * Company \a company accepts engine \a eid for preview.
+ * @param eid Engine being accepted (is under preview).
+ * @param company Current company previewing the engine.
+ */
+static void AcceptEnginePreview(EngineID eid, CompanyID company)
+{
+ Engine *e = Engine::Get(eid);
+
+ e->preview_company = INVALID_COMPANY;
+ e->preview_asked = (CompanyMask)-1;
+
+ EnableEngineForCompany(eid, company);
/* Notify preview window, that it might want to close.
* Note: We cannot directly close the window.
@@ -892,6 +922,37 @@ CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1,
}
/**
+ * Allow or forbid a specific company to use an engine
+ * @param tile unused
+ * @param flags operation to perform
+ * @param p1 engine id
+ * @param p2 various bitstuffed elements
+ * - p2 = (bit 0 - 7) - Company to allow/forbid the use of an engine.
+ * - p2 = (bit 31) - 0 to forbid, 1 to allow.
+ * @param text unused
+ * @return the cost of this operation or an error
+ */
+CommandCost CmdEngineCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+ if (_current_company != OWNER_DEITY) return CMD_ERROR;
+ EngineID engine_id = (EngineID)p1;
+ CompanyID company_id = (CompanyID)GB(p2, 0, 8);
+ bool allow = HasBit(p2, 31);
+
+ if (!Engine::IsValidID(engine_id) || !Company::IsValidID(company_id)) return CMD_ERROR;
+
+ if (flags & DC_EXEC) {
+ if (allow) {
+ EnableEngineForCompany(engine_id, company_id);
+ } else {
+ DisableEngineForCompany(engine_id, company_id);
+ }
+ }
+
+ return CommandCost();
+}
+
+/**
* An engine has become available for general use.
* Also handle the exclusive engine preview contract.
* @param e Engine generally available as of now.