Mercurial > garten
annotate weather.c @ 12:8db6497d6065
merged everything to only one program
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 15:19:45 +0200 |
parents | 176ee28e7464 |
children | 5e6c9260913a |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
8 | 3 #include <time.h> |
3 | 4 #include "db.h" |
5 #include "game.h" | |
6 | |
7 | |
8 | 8 enum { |
9 Nlast = 2, | |
10 }; | |
11 | |
12 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; | |
13 | |
14 struct weather { | |
15 float temp; | |
16 float sun; | |
17 float rain; | |
18 float wind; | |
19 float hum; | |
20 }; | |
21 | |
22 float mean_temp[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
23 float mean_sun[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
24 float mean_rain[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
25 float mean_wind[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
26 float mean_hum[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
3 | 27 |
28 | |
8 | 29 float rand_limit(float limit) { |
30 /* generates random number between -limit and +limit */ | |
31 int r; | |
32 | |
33 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); | |
34 return r - limit; | |
35 } | |
36 | |
37 | |
38 | |
3 | 39 |
8 | 40 void getlastweather(struct weather lastw[], int nlast) { |
41 char query[512]; | |
42 int i; | |
43 | |
44 sprintf(query, | |
45 " select temp, sun, rain, wind, hum from weather " | |
46 " order by tick desc " | |
47 " limit %d " | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
48 , nlast); |
3 | 49 db_query(query); |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
50 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) { |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
51 lastw[i].temp = sqlite3_column_double(stmt, 0); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
52 lastw[i].sun = sqlite3_column_double(stmt, 1); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
53 lastw[i].rain = sqlite3_column_double(stmt, 2); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
54 lastw[i].wind = sqlite3_column_double(stmt, 3); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
55 lastw[i].hum = sqlite3_column_double(stmt, 4); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
56 } |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
57 sqlite3_finalize(stmt); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
58 |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
59 /* |
3 | 60 result = mysql_store_result(conn); |
8 | 61 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { |
62 lastw[i].temp = atof(row[0]); | |
63 lastw[i].sun = atof(row[1]); | |
64 lastw[i].rain = atof(row[2]); | |
65 lastw[i].wind = atof(row[3]); | |
66 lastw[i].hum = atof(row[4]); | |
3 | 67 } |
68 mysql_free_result(result); | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
69 */ |
3 | 70 |
8 | 71 } |
3 | 72 |
73 | |
8 | 74 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { |
75 w->temp = 0.2 * mean_temp[month] + 0.4 * lastw[0].temp + 0.2 * lastw[1].temp + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
76 w->sun = 0.2 * mean_sun[month] + 0.4 * lastw[0].sun + 0.2 * lastw[1].sun + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
77 w->rain = 0.2 * mean_rain[month] + 0.4 * lastw[0].rain + 0.2 * lastw[1].rain + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
78 w->wind = 0.2 * mean_wind[month] + 0.4 * lastw[0].wind + 0.2 * lastw[1].wind + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
79 w->hum = 0.2 * mean_hum[month] + 0.4 * lastw[0].hum + 0.2 * lastw[1].hum + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
80 } | |
81 | |
82 | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
83 void setweather(struct weather* w) { |
8 | 84 char query[512]; |
85 sprintf(query, | |
86 " insert into weather " | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
87 " (tick, temp, sun, rain, wind, hum) " |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
88 " values ('%d', '%f', '%f', '%f', '%f', '%f') " |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
89 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum); |
12 | 90 /* puts(query); */ |
91 if (!db_update(query)) { | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
92 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db)); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
93 } |
3 | 94 } |
95 | |
96 | |
12 | 97 void weather(void) { |
8 | 98 struct weather w; |
99 struct weather lastn[Nlast]; | |
100 | |
101 getlastweather(lastn, Nlast); | |
102 genweather(&w, lastn, Nlast, May); | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
103 setweather(&w); |
3 | 104 } |