1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Martiño Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #include "time_flow.h"
13 
14 #include "sound_renderer.h"
15 #include "config.h"
16 #include "game_constants.h"
17 #include "util.h"
18 #include "conversion.h"
19 #include "leak_dumper.h"
20 
21 using namespace Shared::Util;
22 
23 namespace Glest{ namespace Game{
24 
25 // =====================================================
26 // 	class TimeFlow
27 // =====================================================
28 
29 const float TimeFlow::dusk= 18.f;
30 const float TimeFlow::dawn= 6.f;
31 
TimeFlow()32 TimeFlow::TimeFlow() {
33 	firstTime = false;
34 	tileset = NULL;
35 	time = 0.0f;
36 	lastTime = 0.0f;
37 	timeInc = 0.0f;
38 	//printf("#1a timeInc = %f\n",timeInc);
39 }
40 
init(Tileset * tileset)41 void TimeFlow::init(Tileset *tileset){
42 	firstTime= true;
43 	this->tileset= tileset;
44 	time= dawn+1.5f;
45 	lastTime= time;
46 	Config &config= Config::getInstance();
47 	timeInc= 24.f * (1.f / config.getFloat("DayTime")) / GameConstants::updateFps;
48 	//printf("#1 timeInc = %f\n",timeInc);
49 
50 	if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] timeInc = %f\n",__FILE__,__FUNCTION__,__LINE__,timeInc);
51 }
52 
update()53 void TimeFlow::update() {
54 	//printf("START TimeFlow::update() time = %f\n",time);
55 	//update time
56 	time += isDay()? timeInc: timeInc*2;
57 	if(time > 24.f){
58 		time -= 24.f;
59 	}
60 
61 	//printf("END TimeFlow::update() time = %f\n",time);
62 
63 	//sounds
64 	SoundRenderer &soundRenderer= SoundRenderer::getInstance();
65 	AmbientSounds *ambientSounds= NULL;
66 	if(tileset != NULL) {
67 		ambientSounds = tileset->getAmbientSounds();
68 	}
69 
70 	//day
71 	if(lastTime<dawn && time>=dawn){
72 		if(ambientSounds != NULL) {
73 			soundRenderer.stopAmbient(ambientSounds->getNight());
74 		}
75 		UnitParticleSystem::isNight=false;
76 	}
77 	UnitParticleSystem::lightColor=computeLightColor();
78 
79 	if((lastTime<dawn && time>=dawn) || firstTime){
80 
81 		//day sound
82 		if(ambientSounds != NULL) {
83 			if(ambientSounds->isEnabledDayStart() && !firstTime){
84 				soundRenderer.playFx(ambientSounds->getDayStart());
85 			}
86 			if(ambientSounds->isEnabledDay()){
87 				if(ambientSounds->getAlwaysPlayDay() || tileset->getWeather()==wSunny){
88 					soundRenderer.playAmbient(ambientSounds->getDay());
89 				}
90 			}
91 		}
92 		firstTime= false;
93 	}
94 
95 	//night
96 	if(lastTime<dusk && time>=dusk){
97 		if(ambientSounds != NULL) {
98 			soundRenderer.stopAmbient(ambientSounds->getDay());
99 		}
100 		UnitParticleSystem::isNight=true;
101 	}
102 
103 	if(lastTime<dusk && time>=dusk){
104 		//night
105 		if(ambientSounds != NULL) {
106 			if(ambientSounds->isEnabledNightStart()){
107 				soundRenderer.playFx(ambientSounds->getNightStart());
108 			}
109 			if(ambientSounds->isEnabledNight()){
110 				if(ambientSounds->getAlwaysPlayNight() || tileset->getWeather()==wSunny){
111 					soundRenderer.playAmbient(ambientSounds->getNight());
112 				}
113 			}
114 		}
115 	}
116 	lastTime= time;
117 }
118 
119 //bool TimeFlow::isAproxTime(float time) const {
120 //	return (this->time>=time) && (this->time<time+timeInc);
121 //}
122 
computeLightColor() const123 Vec3f TimeFlow::computeLightColor() const {
124 	Vec3f color;
125 
126 	if(tileset != NULL) {
127 		float time=getTime();
128 
129 		const float transition= 2;
130 		const float dayStart= TimeFlow::dawn;
131 		const float dayEnd= TimeFlow::dusk-transition;
132 		const float nightStart= TimeFlow::dusk;
133 		const float nightEnd= TimeFlow::dawn-transition;
134 
135 		if(time>dayStart && time<dayEnd) {
136 			color= tileset->getSunLightColor();
137 		}
138 		else if(time>nightStart || time<nightEnd) {
139 			color= tileset->getMoonLightColor();
140 		}
141 		else if(time>=dayEnd && time<=nightStart) {
142 			color= tileset->getSunLightColor().lerp((time-dayEnd)/transition, tileset->getMoonLightColor());
143 		}
144 		else if(time>=nightEnd && time<=dayStart) {
145 			color= tileset->getMoonLightColor().lerp((time-nightEnd)/transition, tileset->getSunLightColor());
146 		}
147 		else {
148 			assert(false);
149 			color= tileset->getSunLightColor();
150 		}
151 	}
152 	return color;
153 }
154 
saveGame(XmlNode * rootNode)155 void TimeFlow::saveGame(XmlNode *rootNode) {
156 	std::map<string,string> mapTagReplacements;
157 	XmlNode *timeflowNode = rootNode->addChild("TimeFlow");
158 
159 	timeflowNode->addAttribute("firstTime",intToStr(firstTime), mapTagReplacements);
160 //	bool firstTime;
161 //	Tileset *tileset;
162 //	float time;
163 	timeflowNode->addAttribute("time",floatToStr(time,6), mapTagReplacements);
164 //	float lastTime;
165 	timeflowNode->addAttribute("lastTime",floatToStr(lastTime,6), mapTagReplacements);
166 //	float timeInc;
167 	//printf("#2 timeInc = %f\n",timeInc);
168 	timeflowNode->addAttribute("timeInc",floatToStr(timeInc,6), mapTagReplacements);
169 	//printf("#3 timeInc = %f\n",timeInc);
170 }
171 
loadGame(const XmlNode * rootNode)172 void TimeFlow::loadGame(const XmlNode *rootNode) {
173 	const XmlNode *timeflowNode = rootNode->getChild("TimeFlow");
174 
175 	firstTime = timeflowNode->getAttribute("firstTime")->getFloatValue() != 0;
176 	time = timeflowNode->getAttribute("time")->getFloatValue();
177 	lastTime = timeflowNode->getAttribute("lastTime")->getFloatValue();
178 	timeInc = timeflowNode->getAttribute("timeInc")->getFloatValue();
179 	//printf("#4 timeInc = %f\n",timeInc);
180 }
181 
182 }}//end namespace
183