From b8c9988d9fce02a3bfb958b0ebc7cf39961ef10c Mon Sep 17 00:00:00 2001 From: rubidium Date: Tue, 18 Jan 2011 21:30:59 +0000 Subject: (svn r21842) -Feature [FS#4393]: [NewGRF] Introduction dates/required types for rail types; e.g. introduce a particular rail type in 1960 (or when a vehicle using it is introduced), but also allow limiting its introduction to only happen when the required railtypes are available --- src/engine.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'src/engine.cpp') diff --git a/src/engine.cpp b/src/engine.cpp index 198434a88..048fcc82f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -44,6 +44,11 @@ EngineOverrideManager _engine_mngr; */ static Year _year_engine_aging_stops; +/** + * The railtypes that have been or never will be introduced, or + * an inverse bitmap of rail types that have to be introduced. */ +static uint16 _introduced_railtypes; + /** Number of engines of each vehicle type in original engine data */ const uint8 _engine_counts[4] = { lengthof(_orig_rail_vehicle_info), @@ -474,8 +479,30 @@ void SetupEngines() const Engine *e = new Engine(eid->type, eid->internal_id); assert(e->index == index); } + + _introduced_railtypes = 0; } +/** + * Check whether the railtypes should be introduced. + */ +static void CheckRailIntroduction() +{ + /* All railtypes have been introduced. */ + if (_introduced_railtypes == UINT16_MAX || Company::GetPoolSize() == 0) return; + + /* We need to find the railtypes that are known to all companies. */ + RailTypes rts = (RailTypes)UINT16_MAX; + + /* We are at, or past the introduction date of the rail. */ + Company *c; + FOR_ALL_COMPANIES(c) { + c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, _date); + rts &= c->avail_railtypes; + } + + _introduced_railtypes |= rts; +} void ShowEnginePreviewWindow(EngineID engine); @@ -602,6 +629,19 @@ void StartupEngines() c->avail_roadtypes = GetCompanyRoadtypes(c->index); } + /* Rail types that are invalid or never introduced are marked as + * being introduced upon start. That way we can easily check whether + * there is any date related introduction that is still going to + * happen somewhere in the future. */ + for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { + const RailtypeInfo *rti = GetRailTypeInfo(rt); + if (rti->label != 0 && IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue; + + SetBit(_introduced_railtypes, rt); + } + + CheckRailIntroduction(); + /* Invalidate any open purchase lists */ InvalidateWindowClassesData(WC_BUILD_VEHICLE); } @@ -614,7 +654,7 @@ static void AcceptEnginePreview(EngineID eid, CompanyID company) SetBit(e->company_avail, company); if (e->type == VEH_TRAIN) { assert(e->u.rail.railtype < RAILTYPE_END); - c->avail_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes; + c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date); } else if (e->type == VEH_ROAD) { SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD); } @@ -663,6 +703,8 @@ static CompanyID GetBestCompany(uint8 pp) /** Daily check to offer an exclusive engine preview to the companies. */ void EnginesDailyLoop() { + CheckRailIntroduction(); + if (_cur_year >= _year_engine_aging_stops) return; Engine *e; @@ -760,7 +802,7 @@ static void NewVehicleAvailable(Engine *e) /* maybe make another rail type available */ RailType railtype = e->u.rail.railtype; assert(railtype < RAILTYPE_END); - FOR_ALL_COMPANIES(c) c->avail_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes; + FOR_ALL_COMPANIES(c) c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date); } else if (e->type == VEH_ROAD) { /* maybe make another road type available */ FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD); -- cgit v1.2.3-54-g00ecf