From 386ebec3f8ce1a4f6ba7696f0d0606d72ff8a8f9 Mon Sep 17 00:00:00 2001 From: J. Tang Date: Wed, 1 Dec 2010 23:38:37 -0500 Subject: [PATCH 1/6] Declare a new class, DisasterTimer, in file src/disaster.h, and then add the new file to the project. Signed-off-by: J. Tang --- projects/openttd_vs100.vcxproj | 1 + projects/openttd_vs100.vcxproj.filters | 3 + projects/openttd_vs80.vcproj | 4 + projects/openttd_vs90.vcproj | 4 + source.list | 1 + src/disaster.h | 103 ++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 0 deletions(-) create mode 100644 src/disaster.h diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 384c5db..7ddf728 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -413,6 +413,7 @@ + diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index f84eff7..14e7f52 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -459,6 +459,9 @@ Header Files + + Header Files + Header Files diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 3119c59..9c6c7cb 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -927,6 +927,10 @@ > + + diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 3503a55..43d9515 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -924,6 +924,10 @@ > + + diff --git a/source.list b/source.list index 2d07280..22af47a 100644 --- a/source.list +++ b/source.list @@ -146,6 +146,7 @@ depot_map.h depot_type.h direction_func.h direction_type.h +disaster.h music/dmusic.h driver.h economy_base.h diff --git a/src/disaster.h b/src/disaster.h new file mode 100644 index 0000000..de37668 --- /dev/null +++ b/src/disaster.h @@ -0,0 +1,103 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file disaster.h Declarations for in-game disasters. */ + +#ifndef DISASTER_H +#define DISASTER_H + +#include "industry.h" +#include "vehicle_base.h" + +void ReleaseDisastersTargetingIndustry(IndustryID i); +void ReleaseDisastersTargetingVehicle(VehicleID vehicle); + +/** + * Function to call when a disaster is invoked (either by its timer + * hitting 0 or manually from the console). + */ +typedef void DisasterInitProc(); + +/** + * Class that holds a timer for every non-vehicle disaster. The + * Tick() method is called each day, to decrement the timer. When the + * timer hits 0, a disaster /may/ occur based on the current setting + * for the disaster and the current year. + */ +class DisasterTimer { + public: + static void Startup(); + static void DailyLoop(); + static bool Command(const int argc, char *argv[]); + + private: + /** Base constant of how many days before invoking this Disaster. */ + static const uint16 DISASTER_BASE = 750; + + const char *disaster_id; + /** + * Reference to setting within DisasterSetting. This works + * because _settings_game is a statically allocated item, and + * thus the reference is always valid. + */ + uint8 &disaster_setting; + Year min_year, max_year; + DisasterInitProc *init_proc; + /** When timer reaches 0, a disaster might randomly occur. */ + uint16 timer; + + public: + DisasterTimer(const char *id, uint8 &setting, Year min, Year max, DisasterInitProc *init) : + disaster_id(id), disaster_setting(setting), min_year(min), max_year(max), init_proc(init) + { + /* do nothing special in this constructor */ + } + + const char *GetID() const + { + return disaster_id; + } + + void ShowInfo() const; + + uint16 Get() const + { + return this->timer; + } + + /** + * Forcibly set a DisasterTimer to a given value. If the + * value being set is 0, it is changed to 1, so as to prevent + * underflow upon the next call to Tick(). + * + * @param new_timer New value for this DisasterTimer. + * @return Value set (which may differ from @a new_timer). + */ + uint16 Set(uint16 new_timer) + { + this->timer = new_timer; + if (this->timer == 0) { + this->timer = 1; + } + return this->timer; + } + + void Reset(); + + void Invoke() + { + this->init_proc(); + } + + void Tick(); +}; + +extern SmallVector _disaster_timers; + +#endif /* DISASTER_H */ -- 1.7.3.2