OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Weather.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Controls temperature, wind, and natural disasters */
19 
20 #include "C4Include.h"
21 #include "landscape/C4Weather.h"
22 
23 #include "object/C4Object.h"
24 #include "lib/C4Random.h"
25 #include "game/C4GraphicsSystem.h"
26 #include "game/C4Game.h"
27 #include "platform/C4SoundSystem.h"
28 #include "graphics/C4Draw.h"
29 
31 {
32  Default();
33 }
34 
36 {
37  Clear();
38 }
39 
40 void C4Weather::Init(bool fScenario)
41 {
42  if (fScenario)
43  {
44  // Season
47  // Temperature
50  // Wind
52  // gamma?
54  }
55  // set gamma
57 }
58 
60 {
61  // Season
62  if (!::Game.iTick35)
63  {
65  if (SeasonDelay>=200)
66  {
67  SeasonDelay=0;
68  Season++;
72  }
73  }
74  // Temperature
75  if (!::Game.iTick35)
76  {
77  int32_t iTemperature = Climate - fixtoi(Cos(itofix(36 * Season, 10)), TemperatureRange);
78  if (Temperature<iTemperature) Temperature++;
79  else if (Temperature>iTemperature) Temperature--;
80  }
81  // Wind
82  if (!::Game.iTick1000)
84  if (!::Game.iTick10)
85  Wind=Clamp<int32_t>(Wind+Sign(TargetWind-Wind),
88 }
89 
91 {
92 
93 }
94 
95 int32_t C4Weather::GetWind(int32_t x, int32_t y)
96 {
97  if (Landscape.GetBackPix(x,y) != 0) return 0;
98  return Wind;
99 }
100 
102 {
103  return Temperature;
104 }
105 
107 {
108  Season=0; YearSpeed=0; SeasonDelay=0;
109  Wind=TargetWind=0;
111  TemperatureRange=30;
112  NoGamma=true;
113 }
114 
115 void C4Weather::SetWind(int32_t iWind)
116 {
117  Wind=Clamp<int32_t>(iWind,-100,+100);
118  TargetWind=Clamp<int32_t>(iWind,-100,+100);
119 }
120 
121 void C4Weather::SetTemperature(int32_t iTemperature)
122 {
123  Temperature = Clamp<int32_t>(iTemperature,-100,100);
124  SetSeasonGamma();
125 }
126 
127 void C4Weather::SetSeason(int32_t iSeason)
128 {
129  Season = Clamp<int32_t>(iSeason,0,100);
130  SetSeasonGamma();
131 }
132 
134 {
135  return Season;
136 }
137 
138 void C4Weather::SetClimate(int32_t iClimate)
139 {
140  Climate = Clamp<int32_t>(iClimate,-50,+50);
141  SetSeasonGamma();
142 }
143 
145 {
146  return Climate;
147 }
148 
149 static float SeasonColors[4][3] =
150 {
151  { 0.90f, 0.90f, 1.00f }, // winter: slightly blue; blued out by temperature
152  { 1.00f, 1.05f, 0.90f }, // spring: green to yellow
153  { 1.00f, 1.00f, 1.00f }, // summer: regular ramp
154  { 1.00f, 0.95f, 0.90f } // fall: dark, brown ramp
155 };
156 
158 {
159  if (NoGamma) return;
160  // get season num and offset
161  int32_t iSeason1=(Season/25)%4; int32_t iSeason2=(iSeason1+1)%4;
162  int32_t iSeasonOff1=Clamp(Season%25, 5, 19)-5; int32_t iSeasonOff2=15-iSeasonOff1;
163  float gamma[3] = { 0.0f, 0.0f, 0.0f };
164  // interpolate between season colors
165  for (int32_t iChan=0; iChan<3; iChan+=8)
166  {
167  float c1 = SeasonColors[iSeason1][iChan],
168  c2 = SeasonColors[iSeason2][iChan];
169  gamma[iChan] = (c1*iSeasonOff2 + c2*iSeasonOff1) / 15;
170  }
171  // apply gamma ramp
172  pDraw->SetGamma(gamma[0], gamma[1], gamma[2], C4GRI_SEASON);
173 }
174 
176 {
177  pComp->Value(mkNamingAdapt(Season, "Season", 0));
178  pComp->Value(mkNamingAdapt(YearSpeed, "YearSpeed", 0));
179  pComp->Value(mkNamingAdapt(SeasonDelay, "SeasonDelay", 0));
180  pComp->Value(mkNamingAdapt(Wind, "Wind", 0));
181  pComp->Value(mkNamingAdapt(TargetWind, "TargetWind", 0));
182  pComp->Value(mkNamingAdapt(Temperature, "Temperature", 0));
183  pComp->Value(mkNamingAdapt(TemperatureRange, "TemperatureRange", 30));
184  pComp->Value(mkNamingAdapt(Climate, "Climate", 0));
185  pComp->Value(mkNamingAdapt(NoGamma, "NoGamma", 0));
186 
187  int32_t gamma[C4MaxGammaRamps*3],
188  gammaDefaults[C4MaxGammaRamps*3];
189  for (int32_t i=0; i<C4MaxGammaRamps; ++i)
190  {
191  gammaDefaults[i*3+0] = 100;
192  gammaDefaults[i*3+1] = 100;
193  gammaDefaults[i*3+2] = 100;
194  gamma[i*3+0] = int(pDraw->gamma[i][0] * 100.0f);
195  gamma[i*3+1] = int(pDraw->gamma[i][1] * 100.0f);
196  gamma[i*3+2] = int(pDraw->gamma[i][2] * 100.0f);
197  }
198  pComp->Value(mkNamingAdapt(mkArrayAdaptM(gamma), "Gamma", gammaDefaults));
199  for (int32_t i=0; i<C4MaxGammaRamps; ++i)
200  {
201  pDraw->gamma[i][0] = float(gamma[i*3+0]) / 100.0f;
202  pDraw->gamma[i][1] = float(gamma[i*3+1]) / 100.0f;
203  pDraw->gamma[i][2] = float(gamma[i*3+2]) / 100.0f;
204  }
205 }
206 
int32_t Max
Definition: C4Scenario.h:31
C4SVal Climate
Definition: C4Scenario.h:197
C4SVal StartSeason
Definition: C4Scenario.h:198
void SetTemperature(int32_t iTemperature)
Definition: C4Weather.cpp:121
int32_t NoGamma
Definition: C4Weather.h:33
#define C4GRI_SEASON
Definition: C4Constants.h:39
C4Game Game
Definition: C4Globals.cpp:52
int32_t iTick35
Definition: C4Game.h:131
C4Scenario C4S
Definition: C4Game.h:76
C4SVal YearSpeed
Definition: C4Scenario.h:198
const int C4MaxGammaRamps
Definition: C4Constants.h:35
int Sign(T val)
Definition: Standard.h:47
void SetSeasonGamma()
Definition: C4Weather.cpp:157
C4SVal Wind
Definition: C4Scenario.h:199
int32_t Wind
Definition: C4Weather.h:31
int32_t TemperatureRange
Definition: C4Weather.h:32
void SetWind(int32_t iWind)
Definition: C4Weather.cpp:115
bool NoGamma
Definition: C4Scenario.h:200
#define mkArrayAdaptM(A)
Definition: StdAdaptors.h:312
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:93
int32_t GetClimate()
Definition: C4Weather.cpp:144
int32_t iTick1000
Definition: C4Game.h:131
void Clear()
Definition: C4Weather.cpp:90
C4Landscape Landscape
int32_t Climate
Definition: C4Weather.h:32
int32_t TargetWind
Definition: C4Weather.h:31
int32_t GetTemperature()
Definition: C4Weather.cpp:101
C4Fixed itofix(int32_t x)
Definition: C4Real.h:261
C4Draw * pDraw
Definition: C4Draw.cpp:45
void Init(bool fScenario)
Definition: C4Weather.cpp:40
C4Weather Weather
Definition: C4Weather.cpp:207
int32_t YearSpeed
Definition: C4Weather.h:30
void Value(const T &rStruct)
Definition: StdCompiler.h:171
int32_t GetSeason()
Definition: C4Weather.cpp:133
int32_t iTick10
Definition: C4Game.h:131
void Execute()
Definition: C4Weather.cpp:59
BYTE GetBackPix(int32_t x, int32_t y) const
int32_t Temperature
Definition: C4Weather.h:32
void SetClimate(int32_t iClimate)
Definition: C4Weather.cpp:138
void CompileFunc(StdCompiler *pComp)
Definition: C4Weather.cpp:175
int32_t SeasonDelay
Definition: C4Weather.h:30
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259
C4SWeather Weather
Definition: C4Scenario.h:236
void SetGamma(float r, float g, float b, int32_t iRampIndex)
Definition: C4Draw.cpp:747
int32_t Evaluate()
Definition: C4Scenario.cpp:50
C4Real Cos(const C4Real &fAngle)
Definition: C4Real.h:266
void SetSeason(int32_t iSeason)
Definition: C4Weather.cpp:127
int32_t Season
Definition: C4Weather.h:30
void Default()
Definition: C4Weather.cpp:106
int32_t GetWind(int32_t x, int32_t y)
Definition: C4Weather.cpp:95
int32_t Min
Definition: C4Scenario.h:31
float gamma[C4MaxGammaRamps][3]
Definition: C4Draw.h:97