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 "game/C4GraphicsSystem.h"
24 #include "graphics/C4Draw.h"
25 #include "lib/C4Random.h"
26 #include "object/C4Object.h"
27 #include "platform/C4SoundSystem.h"
28 
C4Weather()29 C4Weather::C4Weather()
30 {
31 	Default();
32 }
33 
~C4Weather()34 C4Weather::~C4Weather()
35 {
36 	Clear();
37 }
38 
Init(bool fScenario)39 void C4Weather::Init(bool fScenario)
40 {
41 	if (fScenario)
42 	{
43 		// Season
44 		Season=Game.C4S.Weather.StartSeason.Evaluate();
45 		YearSpeed=Game.C4S.Weather.YearSpeed.Evaluate();
46 		// Temperature
47 		Climate=100-Game.C4S.Weather.Climate.Evaluate()-50;
48 		Temperature=Climate;
49 		// Wind
50 		Wind=TargetWind=Game.C4S.Weather.Wind.Evaluate();
51 		// gamma?
52 		NoGamma=Game.C4S.Weather.NoGamma;
53 	}
54 	// set gamma
55 	SetSeasonGamma();
56 }
57 
Execute()58 void C4Weather::Execute()
59 {
60 	// Season
61 	if (!::Game.iTick35)
62 	{
63 		SeasonDelay+=YearSpeed;
64 		if (SeasonDelay>=200)
65 		{
66 			SeasonDelay=0;
67 			Season++;
68 			if (Season>Game.C4S.Weather.StartSeason.Max)
69 				Season=Game.C4S.Weather.StartSeason.Min;
70 			SetSeasonGamma();
71 		}
72 	}
73 	// Temperature
74 	if (!::Game.iTick35)
75 	{
76 		int32_t iTemperature = Climate - fixtoi(Cos(itofix(36 * Season, 10)), TemperatureRange);
77 		if (Temperature<iTemperature) Temperature++;
78 		else if (Temperature>iTemperature) Temperature--;
79 	}
80 	// Wind
81 	if (!::Game.iTick1000)
82 		TargetWind=Game.C4S.Weather.Wind.Evaluate();
83 	if (!::Game.iTick10)
84 		Wind=Clamp<int32_t>(Wind+Sign(TargetWind-Wind),
85 		                      Game.C4S.Weather.Wind.Min,
86 		                      Game.C4S.Weather.Wind.Max);
87 }
88 
Clear()89 void C4Weather::Clear()
90 {
91 
92 }
93 
GetWind(int32_t x,int32_t y)94 int32_t C4Weather::GetWind(int32_t x, int32_t y)
95 {
96 	if (Landscape.GetBackPix(x,y) != 0) return 0;
97 	return Wind;
98 }
99 
GetTemperature()100 int32_t C4Weather::GetTemperature()
101 {
102 	return Temperature;
103 }
104 
Default()105 void C4Weather::Default()
106 {
107 	Season=0; YearSpeed=0; SeasonDelay=0;
108 	Wind=TargetWind=0;
109 	Temperature=Climate=0;
110 	TemperatureRange=30;
111 	NoGamma=true;
112 }
113 
SetWind(int32_t iWind)114 void C4Weather::SetWind(int32_t iWind)
115 {
116 	Wind=Clamp<int32_t>(iWind,-100,+100);
117 	TargetWind=Clamp<int32_t>(iWind,-100,+100);
118 }
119 
SetTemperature(int32_t iTemperature)120 void C4Weather::SetTemperature(int32_t iTemperature)
121 {
122 	Temperature = Clamp<int32_t>(iTemperature,-100,100);
123 	SetSeasonGamma();
124 }
125 
SetSeason(int32_t iSeason)126 void C4Weather::SetSeason(int32_t iSeason)
127 {
128 	Season = Clamp<int32_t>(iSeason,0,100);
129 	SetSeasonGamma();
130 }
131 
GetSeason()132 int32_t C4Weather::GetSeason()
133 {
134 	return Season;
135 }
136 
SetClimate(int32_t iClimate)137 void C4Weather::SetClimate(int32_t iClimate)
138 {
139 	Climate = Clamp<int32_t>(iClimate,-50,+50);
140 	SetSeasonGamma();
141 }
142 
GetClimate()143 int32_t C4Weather::GetClimate()
144 {
145 	return Climate;
146 }
147 
148 static float SeasonColors[4][3] =
149 {
150 	{ 0.90f, 0.90f, 1.00f }, // winter: slightly blue; blued out by temperature
151 	{ 1.00f, 1.05f, 0.90f }, // spring: green to yellow
152 	{ 1.00f, 1.00f, 1.00f }, // summer: regular ramp
153 	{ 1.00f, 0.95f, 0.90f }  // fall:   dark, brown ramp
154 };
155 
SetSeasonGamma()156 void C4Weather::SetSeasonGamma()
157 {
158 	if (NoGamma) return;
159 	// get season num and offset
160 	int32_t iSeason1=(Season/25)%4; int32_t iSeason2=(iSeason1+1)%4;
161 	int32_t iSeasonOff1=Clamp(Season%25, 5, 19)-5; int32_t iSeasonOff2=15-iSeasonOff1;
162 	float gamma[3] = { 0.0f, 0.0f, 0.0f };
163 	// interpolate between season colors
164 	for (int32_t iChan=0; iChan<3; iChan+=8)
165 	{
166 		float c1 = SeasonColors[iSeason1][iChan],
167 		      c2 = SeasonColors[iSeason2][iChan];
168 		gamma[iChan] = (c1*iSeasonOff2 + c2*iSeasonOff1) / 15;
169 	}
170 	// apply gamma ramp
171 	pDraw->SetGamma(gamma[0], gamma[1], gamma[2], C4GRI_SEASON);
172 }
173 
CompileFunc(StdCompiler * pComp)174 void C4Weather::CompileFunc(StdCompiler *pComp)
175 {
176 	pComp->Value(mkNamingAdapt(Season,           "Season",                0));
177 	pComp->Value(mkNamingAdapt(YearSpeed,        "YearSpeed",             0));
178 	pComp->Value(mkNamingAdapt(SeasonDelay,      "SeasonDelay",           0));
179 	pComp->Value(mkNamingAdapt(Wind,             "Wind",                  0));
180 	pComp->Value(mkNamingAdapt(TargetWind,       "TargetWind",            0));
181 	pComp->Value(mkNamingAdapt(Temperature,      "Temperature",           0));
182 	pComp->Value(mkNamingAdapt(TemperatureRange, "TemperatureRange",      30));
183 	pComp->Value(mkNamingAdapt(Climate,          "Climate",               0));
184 	pComp->Value(mkNamingAdapt(NoGamma,          "NoGamma",               0));
185 
186 	int32_t gamma[C4MaxGammaRamps*3],
187 	        gammaDefaults[C4MaxGammaRamps*3];
188 	for (int32_t i=0; i<C4MaxGammaRamps; ++i)
189 	{
190 		gammaDefaults[i*3+0] = 100;
191 		gammaDefaults[i*3+1] = 100;
192 		gammaDefaults[i*3+2] = 100;
193 		gamma[i*3+0] = int(pDraw->gamma[i][0] * 100.0f);
194 		gamma[i*3+1] = int(pDraw->gamma[i][1] * 100.0f);
195 		gamma[i*3+2] = int(pDraw->gamma[i][2] * 100.0f);
196 	}
197 	pComp->Value(mkNamingAdapt(mkArrayAdaptM(gamma), "Gamma", gammaDefaults));
198 	for (int32_t i=0; i<C4MaxGammaRamps; ++i)
199 	{
200 		pDraw->gamma[i][0] = float(gamma[i*3+0]) / 100.0f;
201 		pDraw->gamma[i][1] = float(gamma[i*3+1]) / 100.0f;
202 		pDraw->gamma[i][2] = float(gamma[i*3+2]) / 100.0f;
203 	}
204 }
205 
206 C4Weather Weather;
207