1 /***************************************************************************
2                           prefsdaten.cpp  -  description
3                              -------------------
4     begin                : Fri Jul 21 2000
5     copyright            : (C) 2000 by Immi
6     email                : cuyo@pcpool.mathematik.uni-freiburg.de
7 
8 Modified 2003,2006,2008,2010,2011,2014 by the cuyo developers
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  *                                                                         *
14  *   This program is free software; you can redistribute it and/or modify  *
15  *   it under the terms of the GNU General Public License as published by  *
16  *   the Free Software Foundation; either version 2 of the License, or     *
17  *   (at your option) any later version.                                   *
18  *                                                                         *
19  ***************************************************************************/
20 
21 #include <cstdlib>
22 #include <cstdio>
23 #include <cmath>
24 
25 #include "stringzeug.h"
26 #include "prefsdaten.h"
27 #include "cuyointl.h"
28 #include "fehler.h"
29 #include "leveldaten.h"
30 #include "configdatei.h"
31 #include "global.h"
32 
33 #include "sdltools.h"
34 
35 
36 namespace PrefsDaten {
37 
38 
39 
40 
41 /** Die Steuer-Tasten... */
42 SDLKey mTasten[2][4];
43 /** Welche Level wurden schon gewonnen: im 1-Spieler-Modus und im
44     Mehr-Spieler-Modus */
45 std::set<Str> mGewonneneLevel[2];
46 
47 /** Geschwindigkeit des KI-Players, so wie man's einstellen m�chte */
48 int mKIGeschwLog;
49 /** Die Zahl, an der der KI-Player interessiert ist. */
50 double mKIGeschwLin;
51 
52 bool mSound;
53 
54 Str mLastLevel;
55 int mPlayers;
56 
57 /* The following are stored as strings in .cuyo, but it is safe to use ints
58    here, because the semantics can only change between different versions of
59    cuyo, not during one invocation. */
60 int mLevelTrack;
61 int mDifficulty;
62 
63 
64 
65 /** L�dt die Preferences aus wo-auch-immer-sie-abgespeichert-werden
66 in die Variablen. */
67 void liesPreferences();
68 
69 
70 
calcKILangsamLin()71 void calcKILangsamLin() {
72   /* 9 -> 1
73      6 -> 2
74      3 -> 4
75      0 -> 8 */
76   mKIGeschwLin = exp((9 - mKIGeschwLog) * log(2.0) / 3);
77 }
78 
79 
80 
81 
82 
83 
84 enum PrefsVersion {
85   PV_first = 0,
86   PV_SDLKeys = 1,
87   PV_current = PV_SDLKeys
88 };
89 
90 
91 
92 
init()93 void init() {
94   try {
95     liesPreferences();
96   } catch (Fehler f) {
97     print_to_stderr("Error reading preferences file '.cuyo':\n");
98     print_to_stderr(f.getText()+"\n");
99     exit(1);
100   }
101 }
102 
103 
104 /** Returns true if level lnr has already been won. */
getLevelGewonnen(bool sp2,int lnr)105 bool getLevelGewonnen(bool sp2, int lnr) {
106   return mGewonneneLevel[sp2].find(ld->getIntLevelName(lnr))!=mGewonneneLevel[sp2].end();
107 }
108 
109 
110 /** sp2: true bei zweispielermodus */
schreibGewonnenenLevel(bool sp2,const Str intlena)111 void schreibGewonnenenLevel(bool sp2, const Str intlena) {
112   if (mGewonneneLevel[sp2].find(intlena)==mGewonneneLevel[sp2].end()) {
113     /* Level war bisher noch nie gewonnen; also in Liste einf�gen */
114     mGewonneneLevel[sp2].insert(intlena);
115     schreibPreferences();
116   }
117 }
118 
119 
120 
121 /** Liefert true, wenn die Taste k belegt ist, und speichert dann
122     in sp und t ab, was die Taste tut. */
getTaste(SDLKey k,int & sp,int & t)123 bool getTaste(SDLKey k, int & sp, int & t) {
124   for (sp = 0; sp < 2; sp++)
125     for (t = 0; t < taste_anz; t++)
126       if (k == mTasten[sp][t])
127         return true;
128   return false;
129 }
130 
131 
132 
133 
134 
135 /** Liefert den Namen und Pfad der Prefs-Datei zur�ck
136     ($HOME/.cuyo) */
getPrefsName()137 Str getPrefsName() {
138   char * ho = getenv("HOME");
139   if (!ho) {
140     /* Unter Windows zum Beispiel... */
141     // TRANSLATORS: ".cuyo" is a file name and should not be translated
142     print_to_stderr("Warning: Env-Variable $HOME not found. Using the current directory for .cuyo");
143     return ".cuyo";
144   }
145   if (ho[strlen(ho) - 1] == '/')
146     return Str(ho) + ".cuyo";
147   else
148     return Str(ho) + "/.cuyo";
149 }
150 
151 
152 
153 
154 
155 /** L�dt die Preferences aus wo-auch-immer-sie-abgespeichert-werden
156 in die Variablen. */
liesPreferences()157 void liesPreferences() {
158 
159   int i;
160   ConfigDatei prd(getPrefsName());
161 
162   /* Welche Version hat die Prefs-Datei? */
163   PrefsVersion version = (PrefsVersion)
164                  prd.getZahlEintrag("prefsVersion", PV_first);
165 
166 
167   /* Tastenbelegung */
168 
169   /* Default-Tasten */
170   //int dt[2][4] = {{Qt::Key_A, Qt::Key_D, Qt::Key_W, Qt::Key_S},
171   //  {Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, Qt::Key_Down}};
172   SDLKey dt[2][4] = {{SDLK_a, SDLK_d, SDLK_w, SDLK_s},
173                      {SDLK_LEFT, SDLK_RIGHT, SDLK_UP, SDLK_DOWN}};
174   char tn[4][9] = {"left", "right", "turn", "down"};
175 
176   for (i = 0; i < 2; i++) {
177     prd.setAbschnitt(_sprintf("keys %d", i + 1));
178     for (int j = 0; j < 4; j++) {
179       if (version >= PV_SDLKeys) {
180         mTasten[i][j] = (SDLKey) prd.getZahlEintrag(tn[j], dt[i][j]);
181 
182       } else {
183         /* Alte Version: Tasten stehen noch im Qt-Format in der Datei */
184         int t = prd.getZahlEintrag(tn[j], -1111);
185 	if (t == -1111)
186 	  mTasten[i][j] = dt[i][j];
187 	else
188           mTasten[i][j] = SDLTools::qtKey2sdlKey(t);
189       }
190     }
191   }
192 
193   /* Gewonnene Level */
194   prd.setAbschnitt();
195   for (i = 0; i < 2; i++) {
196     prd.getMengenEintrag(i ? "wonMultiPlayer" : "wonOnePlayer",
197 			 mGewonneneLevel[i]);
198     /* Debug-Ausgabe: */
199     /*Str t = prd->readEntry(i ? "wonMultiPlayer" : "wonOnePlayer");
200       print_to_stderr(_sprintf("blub = %s\n", t.data()));*/
201   }
202 
203   /* KI-Player-Geschwindigkeit */
204   mKIGeschwLog = prd.getZahlEintrag("AISpeed", 6);
205   calcKILangsamLin();
206 
207   /* Sound */
208   mSound = prd.getZahlEintrag("Sound", 1);
209 
210   mPlayers = prd.getZahlEintrag("Players",1);
211   mLevelTrack = Version::gLevelpack.suchMerkmal(
212     prd.getEintrag("LevelTrack","main"));
213   if (mLevelTrack==-1) mLevelTrack=Version::gLevelpack.suchMerkmal("main");
214   mDifficulty = Version::gSchwierig.suchMerkmal(
215     prd.getEintrag("Difficulty",""));
216   if (mDifficulty==-1) mDifficulty=Version::gSchwierig.suchMerkmal("");
217   mLastLevel = prd.getEintrag("LastLevel", "");
218 }
219 
220 
221 
222 /** Schreibt die Preferences nach wo-auch-immer-sie-abgespeichert-werden. */
schreibPreferences()223 void schreibPreferences() {
224 
225   Str na = getPrefsName();
226 
227   FILE * f = fopen(na.data(), "w");
228   if (!f) {
229     print_to_stderr(_sprintf("Warning: Could not write preferences file \"%s\".",
230 			     na.data()));
231     return;
232   }
233 
234   fprintf(f, "# cuyo Preferences File\n\n");
235   fprintf(f, "prefsVersion=%d\n\n", PV_current);
236   for (int i = 0; i < 2; i++) {
237     fprintf(f, i ? "wonMultiPlayer=" : "wonOnePlayer=");
238     Str lena;
239     std::set<Str>::const_iterator e = mGewonneneLevel[i].end();
240     for (std::set<Str>::const_iterator j = mGewonneneLevel[i].begin();
241 	 j!=e; ++j)
242       fprintf(f, "%s,", j->data());
243     fprintf(f, "\n");
244   }
245   fprintf(f, "\n");
246   fprintf(f, "AISpeed=%d\n\n", mKIGeschwLog);
247 
248   fprintf(f, "\n");
249   fprintf(f, "Sound=%d\n\n", mSound);
250 
251   fprintf(f, "\n");
252   fprintf(f, "Players=%d\n\n", mPlayers);
253 
254   fprintf(f, "\n");
255   fprintf(f, "LevelTrack=%s\n\n",
256     Version::gLevelpack.mMerkmale[mLevelTrack].data());
257 
258   fprintf(f, "\n");
259   fprintf(f, "Difficulty=%s\n\n",
260     Version::gSchwierig.mMerkmale[mDifficulty].data());
261 
262   fprintf(f, "\n");
263   fprintf(f, "LastLevel=%s\n\n", mLastLevel.data());
264 
265   char tn[4][9] = {"left", "right", "turn", "down"};
266   for (int i = 0; i < 2; i++) {
267     fprintf(f, "[keys %d]\n", i + 1);
268     for (int j = 0; j < 4; j++)
269       fprintf(f, "%s=%d\n", tn[j], mTasten[i][j]);
270     fprintf(f, "\n");
271   }
272 
273   fclose(f);
274 }
275 
276 
277 
278 
getTaste(int sp,int t)279 SDLKey getTaste(int sp, int t) { return mTasten[sp][t]; }
getKIGeschwLin()280 double getKIGeschwLin() { return mKIGeschwLin; }
getKIGeschwLog()281 int getKIGeschwLog() { return mKIGeschwLog; }
282 
setTaste(int sp,int t,SDLKey code)283 void setTaste(int sp, int t, SDLKey code) { mTasten[sp][t] = code; }
setKIGeschwLog(int kigl)284 void setKIGeschwLog(int kigl) { mKIGeschwLog = kigl; calcKILangsamLin(); }
285 
getSound()286 bool getSound() { return mSound; }
setSound(bool s)287 void setSound(bool s) { mSound = s; }
288 
getPlayers()289 int getPlayers() { return mPlayers; }
setPlayers(int n)290 void setPlayers(int n) { mPlayers = n; }
291 
getLevelTrack()292 int getLevelTrack() { return mLevelTrack; }
setLevelTrack(int t)293 void setLevelTrack(int t) { mLevelTrack = t; }
294 
getDifficulty()295 int getDifficulty() { return mDifficulty; }
setDifficulty(int d)296 void setDifficulty(int d) { mDifficulty = d; }
297 
getLastLevel()298 Str getLastLevel() { return mLastLevel; }
setLastLevel(const Str l)299 void setLastLevel(const Str l) { mLastLevel = l; }
300 
301 }
302 
303