src/weather.cpp

Go to the documentation of this file.
00001 #include "weather.h"
00002 
00003 #define TEMP_MIN_TEMP 65
00004 #define TEMP_MAX_TEMP 100
00005 #define FOG_TEMP_MAX 75
00006 #define DEW_MIN_TEMP 55
00007 #define DEW_MAX_TEMP 90
00008 #define FOG_MAX 0.005f
00009 #define FOG_MIN 0.0005f
00010 
00011 void WEATHER::Init()
00012 {
00013         //set the temperature to vary between 50 and 90 degrees, and change
00014         // every half a day or every 5 days
00015         //temperature.Init(50, 90, 0.5, 5.0f);
00016         
00017         temperature.Init(TEMP_MIN_TEMP, TEMP_MAX_TEMP, 1.0, 3.0f, 10.0f);
00018         
00019         dewpoint.Init(DEW_MIN_TEMP, DEW_MAX_TEMP, 0.2, 1.5f);
00020         
00021         globalwind_highfreq.Init(-0.0005f, 0.0005f, 0.0003f, 0.0006f);
00022         globalwind_lowfreq.Init(-0.002f, 0.002f, 0.01f, 0.04f);
00023 }
00024 
00025 void WEATHER::Update(double cur_time)
00026 {
00027         abs_time = cur_time;
00028         
00029         temperature.Update(abs_time);
00030         dewpoint.Update(abs_time);
00031         globalwind_lowfreq.Update(abs_time);
00032         globalwind_highfreq.Update(abs_time);
00033         
00034         //cout << temperature.GetVal(abs_time) << ", " << dewpoint.GetVal(abs_time) << ":  "; GetGlobalWind().DebugPrint();
00035 }
00036 
00037 void WEATHERVARIABLE::Update(double abs_time)
00038 {
00039         if (abs_time > start_time + duration)
00040                 CalcNew(abs_time);
00041 }
00042 
00043 void WEATHERVARIABLE::CalcNew(double abs_time)
00044 {
00045         value = next_value;
00046         start_time = abs_time;
00047         
00048         //is this needed?  are we seeding the random generator somewhere else??
00049         //srand((unsigned int) (abs_time*1000.0));
00050         
00051         duration = (rand()/(float)RAND_MAX)*(durmax-durmin)+durmin;
00052         next_value = (rand()/(float)RAND_MAX)*(valmax-valmin)+valmin;
00053 }
00054 
00055 void WEATHERVARIABLE::Init(float minval, float maxval, float mindur, float maxdur, float newnightdip)
00056 {
00057         valmin = minval;
00058         valmax = maxval;
00059         durmin = mindur;
00060         durmax = maxdur;
00061         
00062         night_dip = newnightdip;
00063         
00064         CalcNew(-0.1);
00065         CalcNew(0.0);
00066 }
00067 
00068 void WEATHERVARIABLE::Init(float minval, float maxval, float mindur, float maxdur)
00069 {
00070         valmin = minval;
00071         valmax = maxval;
00072         durmin = mindur;
00073         durmax = maxdur;
00074         
00075         night_dip = 0.0f;
00076         
00077         CalcNew(-0.1);
00078         CalcNew(0.0);
00079 }
00080 
00081 float WEATHERVARIABLE::GetVal(double abs_time)
00082 {
00083         float outval;
00084         double timeindex = abs_time - start_time;
00085         float timefactor = timeindex / duration;
00086         
00087         outval = value*(1.0f-timefactor)+next_value*timefactor;
00088         
00089         //night dip stuff
00090         if (night_dip > 0)
00091         {
00092                 float night=0;
00093                 float ntime;
00094                 int itime = (int) abs_time;
00095                 ntime = abs_time - itime;
00096                 float nightthresh = 0.2f;
00097                 if (ntime <= 1.0f-nightthresh && ntime >= 0.5f+nightthresh )
00098                         night = 1.0f;
00099                 else if (ntime <= 0.5)
00100                         night = 0.0f;
00101                 else if (ntime > 1.0f-nightthresh)
00102                         night = (1.0f-ntime)/nightthresh;
00103                 else if (ntime > 0.5f && ntime < 0.5f + nightthresh)
00104                         night = (ntime - 0.5f)/nightthresh;
00105                 if (night > 1.0f)
00106                         night = 1.0f;
00107                 outval -= night*night_dip;
00108         }
00109         
00110         return outval;
00111 }
00112 
00113 int WEATHER::GetCloudCover()
00114 {
00115         float ccover, temp, dew;
00116         temp = temperature.GetVal(abs_time);
00117         dew = dewpoint.GetVal(abs_time);
00118         
00119         //knock temp about 10 degrees at night
00120         
00121         if (temp > dew + CLOUD_FORMATION)
00122                 ccover = 0.0f;
00123         else if (temp < dew)
00124                 ccover = 1.0f;
00125         else
00126                 ccover = 1.0f - (temp-dew)/CLOUD_FORMATION;
00127         
00128         ccover = 1.0f - ccover;
00129         
00130         ccover *= 255.0f;
00131         
00132         //cout << ccover - 128 << endl;
00133         
00134         return (int) (ccover - 128);
00135         //return -128;
00136         //return 120;
00137 }
00138 
00139 int WEATHER::GetCloudFuzziness()
00140 {
00141         //eventually, this should depend a bit on wind
00142         return 50;
00143 }
00144 
00145 void WEATHERVERTEX::Update(double abs_time)
00146 {
00147         int i;
00148         for (i = 0; i < 3; i++)
00149                 components[i].Update(abs_time);
00150 }
00151 
00152 void WEATHERVERTEX::CalcNew(double abs_time)
00153 {
00154         int i;
00155         for (i = 0; i < 3; i++)
00156         {
00157                 components[i].CalcNew(abs_time);
00158         }
00159 }
00160 
00161 void WEATHERVERTEX::Init(float minval, float maxval, float mindur, float maxdur)
00162 {
00163         int i;
00164         for (i = 0; i < 3; i++)
00165         {
00166                 components[i].Init(minval, maxval, mindur, maxdur);
00167         }
00168 }
00169 
00170 VERTEX WEATHERVERTEX::GetVal(double abs_time)
00171 {
00172         VERTEX outval;
00173         outval.x = components[0].GetVal(abs_time);
00174         outval.y = components[1].GetVal(abs_time);
00175         outval.z = components[2].GetVal(abs_time);
00176         return outval;
00177 }
00178 
00179 VERTEX WEATHER::GetGlobalWind()
00180 {
00181         return globalwind_lowfreq.GetVal(abs_time)+globalwind_highfreq.GetVal(abs_time);
00182         //VERTEX zero;
00183         //return zero;
00184 }
00185 
00186 int WEATHER::GetRainDrops()
00187 {
00188         float dew = dewpoint.GetVal(abs_time);
00189         float temp = temperature.GetVal(abs_time);
00190         
00191         if (temp > dew)
00192                 return 0;
00193         
00194         //raindrops per degree of difference between the dew point and temperature
00195         float rainfactor = 100.0f;
00196         float maxrain = 500.0f;
00197         
00198         float raindrops = (dew-temp)*rainfactor;
00199         if (raindrops > maxrain)
00200                 raindrops = maxrain;
00201         
00202         return (int) raindrops;
00203         //return 500;
00204 }
00205 
00206 float WEATHER::GetFogDensity()
00207 {
00208         float dew = dewpoint.GetVal(abs_time);
00209         float temp = temperature.GetVal(abs_time);
00210         
00211         float fogfactor;
00212         
00213         //fog based on temperature
00214         if (temp > FOG_TEMP_MAX)
00215                 fogfactor = 0.0f;
00216         else
00217                 fogfactor = (FOG_TEMP_MAX-temp)/(FOG_TEMP_MAX-TEMP_MIN_TEMP);
00218         
00219         //add dewpoint coefficient
00220         float dewcoeff;
00221         if (dew > FOG_TEMP_MAX)
00222                 dewcoeff = 0.0f;
00223         else
00224                 dewcoeff = (FOG_TEMP_MAX-dew)/(FOG_TEMP_MAX-DEW_MIN_TEMP);
00225         fogfactor *= dewcoeff;
00226         
00227         float fogdensity = FOG_MIN*(1.0f-fogfactor)+FOG_MAX*(fogfactor);
00228         
00229         //cout << fogdensity << endl;
00230         
00231         return fogdensity;
00232 }

Generated on Thu Oct 19 04:05:56 2006 by  doxygen 1.4.6