1 /***************************************************************************
2                userconfiguration.h  -  Manages game preferences
3                              -------------------
4     begin                : Sat Feb 14 2004
5     copyright            : (C) 2004 by Daroth-U
6     email                : daroth-u@ifrance.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef USER_CONFIGURATION_H
19 #define USER_CONFIGURATION_H
20 #pragma once
21 
22 #include <fstream>
23 #include <iostream>
24 #include <string>
25 #include <map>
26 #include "preferences.h"
27 
28 // set to non-zero for debugging
29 #define DEBUG_USER_CONFIG 0
30 
31 /// Interface to the saved game options.
32 class UserConfiguration : public Preferences {
33 
34 private:
35 	static const char * ENGINE_ACTION_NAMES[];
36 	static const char * ENGINE_ACTION_UP_NAMES[];
37 	static const char * ENGINE_ACTION_DESCRIPTION[];
38 	static const char default_key[][40];
39 
40 	// becomes true every time loadConfiguration is called
41 	// and false every time getConfigurationChanged is called
42 	bool configurationChanged;
43 
44 	// mappings to speed-up search processing
45 	std::map<std::string, int> keyDownBindings;      // string keyName -> int ea
46 	std::map<std::string, int> keyUpBindings;        // string keyName -> int ea
47 	std::map<Uint8, int> mouseDownBindings;     // uint8 mouseButton -> int ea
48 	std::map<Uint8, int> mouseUpBindings;       // uint8 mouseButton -> int ea
49 	std::map<std::string, int> engineActionUpNumber; // string ea -> int ea
50 	std::map<std::string, int> engineActionNumber;   // string ea -> int ea
51 	std::map<int, std::string> keyForEngineAction;   // int ea -> string keyName
52 	std::map<int, std::string> engineActionName;     // int ea -> string ea
53 
54 	// return next word from string or empty string
55 	std::string getNextWord( const std::string theInput, int fromPos, int &endWord );
56 
57 	// replace " " by "_" in a string
58 	void replaceSpaces( std::string& s );
59 
60 	void writeFile( std::ofstream *fileOut, char *text );
61 
62 	// engine variables (video settings)
63 	bool fullscreen;
64 	bool doublebuf;
65 	bool hwpal;
66 	bool resizeable;
67 	bool force_hwsurf;
68 	bool force_swsurf;
69 	bool hwaccel;
70 	bool test;
71 	bool multitexturing;
72 	bool stencilbuf;
73 	int bpp;
74 	int w;
75 	int h;
76 	int shadows;
77 	bool alwaysShowPath;
78 	bool tooltipEnabled;
79 	int tooltipInterval;
80 	bool enableScreenshots;
81 	bool aniso_filter;
82 
83 	// game settings
84 	int gamespeed;
85 	bool centermap;
86 	bool keepMapSize;
87 	bool frameOnFullScreen;
88 	bool turnBasedBattle;
89 	bool ovalCutoutShown;
90 	bool outlineInteractiveItems;
91 	int combatInfoDetail;
92 	bool hideInventoriesOnMove;
93 	int logLevel;
94 	int pathFindingQuality;
95 	long maxPathNodes;
96 
97 	// audio settings
98 	bool soundEnabled;
99 	int soundFreq;
100 	int musicVolume;
101 	int effectsVolume;
102 
103 	int standAloneMode;
104 	std::string host;
105 	std::string userName;
106 	int port;
107 	int monsterToughness;
108 	bool debugTheme;
109 	bool flaky;
110 
111 public:
112 
113 	UserConfiguration();
114 	~UserConfiguration();
115 
116 	const char * getEngineActionDescription( int i );
117 	const char * getEngineActionKeyName( int i );
118 
119 	// engine variables
getFlaky()120 	inline bool getFlaky()     {
121 		return flaky;
122 	}
getFullscreen()123 	inline bool getFullscreen() {
124 		return fullscreen;
125 	}
getDoublebuf()126 	inline bool getDoublebuf() {
127 		return doublebuf;
128 	}
getHwpal()129 	inline bool getHwpal()     {
130 		return hwpal;
131 	}
getResizeable()132 	inline bool getResizeable() {
133 		return resizeable;
134 	}
getForce_hwsurf()135 	inline bool getForce_hwsurf() {
136 		return force_hwsurf;
137 	}
getForce_swsurf()138 	inline bool getForce_swsurf() {
139 		return force_swsurf;
140 	}
getHwaccel()141 	inline bool getHwaccel()   {
142 		return hwaccel;
143 	}
getTest()144 	inline bool getTest()      {
145 		return test;
146 	}
getStencilbuf()147 	inline bool getStencilbuf() {
148 		return stencilbuf;
149 	}
getMultitexturing()150 	inline bool getMultitexturing() {
151 		return Constants::multitexture;
152 	}
getBpp()153 	inline int getBpp()        {
154 		return bpp;
155 	}
getW()156 	inline int getW()          {
157 		return w;
158 	}
getH()159 	inline int getH()          {
160 		return h;
161 	}
getShadows()162 	inline int getShadows()    {
163 		return shadows;
164 	}
getAnisoFilter()165 	inline bool getAnisoFilter() {
166 		return aniso_filter;
167 	}
getGameSpeedLevel()168 	inline int getGameSpeedLevel()  {
169 		return gamespeed;
170 	} // [0, 1, 2, 3, 4]
getAlwaysCenterMap()171 	inline bool getAlwaysCenterMap() {
172 		return centermap;
173 	}
getKeepMapSize()174 	inline bool getKeepMapSize() {
175 		return keepMapSize;
176 	}
getFrameOnFullScreen()177 	inline bool getFrameOnFullScreen() {
178 		return frameOnFullScreen;
179 	}
isBattleTurnBased()180 	inline bool isBattleTurnBased() {
181 		return turnBasedBattle;
182 	}
isOvalCutoutShown()183 	inline bool isOvalCutoutShown() {
184 		return ovalCutoutShown;
185 	}
isOutlineInteractiveItems()186 	inline bool isOutlineInteractiveItems() {
187 		return outlineInteractiveItems;
188 	}
isHideInventoriesOnMove()189 	inline bool isHideInventoriesOnMove() {
190 		return hideInventoriesOnMove;
191 	}
getCombatInfoDetail()192 	inline int getCombatInfoDetail() {
193 		return combatInfoDetail;
194 	}
getSoundFreq()195 	inline int getSoundFreq() {
196 		return soundFreq;
197 	}
isSoundEnabled()198 	inline bool isSoundEnabled() {
199 		return soundEnabled;
200 	}
getMusicVolume()201 	inline int getMusicVolume() {
202 		return musicVolume;
203 	}
getEffectsVolume()204 	inline int getEffectsVolume() {
205 		return effectsVolume;
206 	}
getAlwaysShowPath()207 	inline bool getAlwaysShowPath() {
208 		return alwaysShowPath;
209 	}
getTooltipEnabled()210 	inline bool getTooltipEnabled() {
211 		return tooltipEnabled;
212 	}
getEnableScreenshots()213 	inline bool getEnableScreenshots() {
214 		return enableScreenshots;
215 	}
getTooltipInterval()216 	inline int getTooltipInterval() {
217 		return tooltipInterval;
218 	}
getLogLevel()219 	inline int getLogLevel() {
220 		return logLevel;
221 	}
getPathFindingQuality()222 	inline int getPathFindingQuality() {
223 		return pathFindingQuality;
224 	}
getMaxPathNodes()225 	inline long getMaxPathNodes() {
226 		return maxPathNodes;
227 	}
getMonsterToughness()228 	inline int getMonsterToughness() {
229 		return monsterToughness;
230 	}
231 
setFullscreen(bool t)232 	inline void setFullscreen( bool t ) {
233 		fullscreen = t;
234 	}
setDoublebuf(bool t)235 	inline void setDoublebuf( bool t ) {
236 		doublebuf = t;
237 	}
setHwpal(bool t)238 	inline void setHwpal( bool t )     {
239 		hwpal = t;
240 	}
setResizeable(bool t)241 	inline void setResizeable( bool t ) {
242 		resizeable = t;
243 	}
setForce_hwsurf(bool t)244 	inline void setForce_hwsurf( bool t ) {
245 		force_hwsurf = t;
246 	}
setForce_swsurf(bool t)247 	inline void setForce_swsurf( bool t ) {
248 		force_swsurf = t;
249 	}
setHwaccel(bool t)250 	inline void setHwaccel( bool t )   {
251 		hwaccel = t;
252 	}
setStencilbuf(bool t)253 	inline void setStencilbuf( bool t ) {
254 		stencilbuf = t;
255 	}
setMultitexturing(bool t)256 	inline void setMultitexturing( bool t ) {
257 		Constants::multitexture = t;
258 	}
setBpp(int t)259 	inline void setBpp( int t )         {
260 		bpp = t;
261 	}
setW(int t)262 	inline void setW( int t )           {
263 		w = t;
264 	}
setH(int t)265 	inline void setH( int t )           {
266 		h = t;
267 	}
setShadows(int t)268 	inline void setShadows( int t )     {
269 		shadows = t;
270 	}
setAnisoFilter(bool t)271 	inline void setAnisoFilter( bool t ) {
272 		aniso_filter = t;
273 	}
setGameSpeedLevel(int t)274 	inline void setGameSpeedLevel( int t )   {
275 		if ( t >= 0 && t <= 4 ) gamespeed = t;
276 	} // [0, 1, 2, 3, 4]
setAlwaysCenterMap(bool t)277 	inline void setAlwaysCenterMap( bool t ) {
278 		centermap = t;
279 	}
setKeepMapSize(bool t)280 	inline void setKeepMapSize( bool t ) {
281 		keepMapSize = t;
282 	}
setFrameOnFullScreen(bool t)283 	inline void setFrameOnFullScreen( bool t ) {
284 		frameOnFullScreen = t;
285 	}
setBattleTurnBased(bool b)286 	inline void setBattleTurnBased( bool b ) {
287 		turnBasedBattle = b;
288 	}
setOvalCutoutShown(bool b)289 	inline void setOvalCutoutShown( bool b ) {
290 		ovalCutoutShown = b;
291 	}
setOutlineInteractiveItems(bool b)292 	inline void setOutlineInteractiveItems( bool b ) {
293 		outlineInteractiveItems = b;
294 	}
setCombatInfoDetail(int n)295 	inline void setCombatInfoDetail( int n ) {
296 		combatInfoDetail = n;
297 	}
setSoundFreq(int n)298 	inline void setSoundFreq( int n ) {
299 		soundFreq = n;
300 	}
setSoundEnabled(bool b)301 	inline void setSoundEnabled( bool b ) {
302 		soundEnabled = b;
303 	}
setMusicVolume(int n)304 	inline void setMusicVolume( int n ) {
305 		musicVolume = n;
306 	}
setEffectsVolume(int n)307 	inline void setEffectsVolume( int n ) {
308 		effectsVolume = n;
309 	}
setAlwaysShowPath(bool b)310 	inline void setAlwaysShowPath( bool b ) {
311 		alwaysShowPath = b;
312 	}
setTooltipEnabled(bool b)313 	inline void setTooltipEnabled( bool b ) {
314 		tooltipEnabled = b;
315 	}
setEnableScreenshots(bool b)316 	inline void setEnableScreenshots( bool b ) {
317 		enableScreenshots = b;
318 	}
setTooltipInterval(int n)319 	inline void setTooltipInterval( int n ) {
320 		tooltipInterval = n;
321 	}
setLogLevel(int t)322 	inline void setLogLevel( int t )   {
323 		if ( t >= 0 && t <= 3 ) logLevel = t;
324 	} // [0, 1, 2, 3]
setPathFindingQuality(int t)325 	inline void setPathFindingQuality( int t ) {
326 		if ( t >= 0 && t <= 2 ) {
327 			pathFindingQuality = t; maxPathNodes =  pow( 10., t + 1. ) * 50;
328 		}
329 	}
setMonsterToughness(int n)330 	inline void setMonsterToughness( int n ) {
331 		monsterToughness = n;
332 	}
333 
334 	// return gameSpeed in ticks
335 	int getGameSpeedTicks();
336 
337 
338 	// reads the configuration file where keys are binded
339 	void loadConfiguration();
340 
341 	// save configuration into file
342 	void saveConfiguration();
343 
344 	// Associate SDL events to an engine action
345 	void bind( std::string s1, std::string s2, int lineNumber );
346 
347 	// Read in engine variables from file
348 	void set( std::string s1, std::string s2, int lineNumber );
349 
350 	// returns the action to do for this event
351 	int getEngineAction( SDL_Event *event );
352 
353 	void parseCommandLine( int argc, char *argv[] );
354 	void setKeyForEngineAction( std::string keyName, int ea );
355 
getStandAloneMode()356 	inline int getStandAloneMode() {
357 		return standAloneMode;
358 	}
getHost()359 	inline char const* getHost() {
360 		return host.c_str();
361 	}
getUserName()362 	inline char const* getUserName() {
363 		return userName.c_str();
364 	}
getPort()365 	inline int getPort() {
366 		return port;
367 	}
isDebugTheme()368 	inline bool isDebugTheme() {
369 		return debugTheme;
370 	}
371 
372 	void createConfigDir();
373 
374 protected:
375 	void createDefaultConfigFile();
376 };
377 
378 #endif
379 
380